Skip to content

Commit 7292bfb

Browse files
committed
enable asu option for from_random
1 parent 4617efa commit 7292bfb

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

pyxtal/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def from_random(
292292
random_state=None,
293293
tm=None,
294294
use_hall=False,
295+
use_asu=False,
295296
):
296297
"""
297298
The main function to generate random crystals.
@@ -318,6 +319,7 @@ def from_random(
318319
random_state (optional): numpy random state
319320
tm (optional): Tol_matrix define the distances
320321
use_hall (bool): whether to use Hall number. Default is False
322+
use_asu (bool): whether to generate points within ASU. Default is False
321323
322324
Returns:
323325
Crystal structure object if successful
@@ -370,6 +372,7 @@ def from_random(
370372
tm=tm,
371373
use_hall=use_hall,
372374
random_state=random_state,
375+
use_asu=use_asu,
373376
)
374377
if force_pass or struc.valid:
375378
quit = True

pyxtal/crystal.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
tm=None,
6262
use_hall=False,
6363
random_state: int | None | Generator = None,
64+
use_asu: bool = False,
6465
):
6566
# Initialize
6667
self.rng = np.random.default_rng(random_state)
@@ -71,6 +72,7 @@ def __init__(
7172
self.valid = False
7273
self.factor = factor
7374
self.min_density = 0.75
75+
self.use_asu = use_asu
7476

7577
# Dimesion
7678
self.dim = dim
@@ -377,7 +379,13 @@ def _set_ion_wyckoffs(self, numIon, specie, cell, wyks):
377379
else:
378380
if site is not None:
379381
wp = self.group[site]
380-
pt = self.lattice.generate_point()
382+
if self.use_asu:
383+
xmin, xmax, ymin, ymax, zmin, zmax = self.group.get_ASU()
384+
# Generate a random point based on (min, max) ranges
385+
pt = self.rng.uniform([xmin, ymin, zmin], [xmax, ymax, zmax])
386+
else:
387+
pt = self.lattice.generate_point()
388+
381389
# avoid using the merge function
382390
if len(wp.short_distances(pt, cell, tol)) > 0:
383391
# print('bad pt', pt, wp.short_distances(pt, cell, tol))
@@ -392,7 +400,12 @@ def _set_ion_wyckoffs(self, numIon, specie, cell, wyks):
392400
if wp is not False:
393401
# print(wp.letter)
394402
# Generate a list of coords from ops
395-
pt = self.lattice.generate_point()
403+
if self.use_asu:
404+
xmin, xmax, ymin, ymax, zmin, zmax = self.group.get_ASU()
405+
# Generate a random point based on (min, max) ranges
406+
pt = self.rng.uniform([xmin, ymin, zmin], [xmax, ymax, zmax])
407+
else:
408+
pt = self.lattice.generate_point()
396409
pt, wp, _ = wp.merge(pt, cell, tol, group=self.group)
397410
# print('good pt', pt)
398411
if wp is not False:

tests/test_crystal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ def test_single_specie(self):
5858
os.remove("tmp-3d.cif")
5959
assert struc.valid
6060

61+
6162
def test_mutiple_species(self):
6263
struc = pyxtal()
6364
struc.from_random(3, 99, ["Ba", "Ti", "O"], [1, 1, 3], 1.2)
6465
assert struc.valid
6566

67+
struc.from_random(3, 99, ["Ba", "Ti", "O"], [1, 1, 3], 1.2, use_asu=True)
68+
assert struc.valid
69+
6670
def test_preassigned_sites(self):
6771
sites = [["1b"], ["1b"], ["2c", "1b"]]
6872
struc = pyxtal()
@@ -73,6 +77,12 @@ def test_preassigned_sites(self):
7377
struc.from_random(3, 225, ["C"], [12], 1.0, sites=[["4a", "8c"]])
7478
assert struc.valid
7579

80+
struc.from_random(3, 99, ["Ba", "Ti", "O"], [1, 1, 3], 1.0, sites=sites, use_asu=True)
81+
assert struc.valid
82+
83+
struc.from_random(3, 225, ["C"], [12], 1.0, sites=[["4a", "8c"]], use_asu=True)
84+
assert struc.valid
85+
7686
def test_read(self):
7787
# test reading xtal from cif
7888
for name in ["FAU", "NaSb3F10", "PVO", "lt_quartz"]:

0 commit comments

Comments
 (0)