@@ -65,6 +65,9 @@ def generate_atom_list(cfg: GenerateConfig, verbosity: int = 1) -> np.ndarray:
65
65
"""
66
66
Generate a random molecule with a random number of atoms.
67
67
"""
68
+ # initialize a default random number generator
69
+ rng = np .random .default_rng ()
70
+
68
71
# Define a new set of all elements that can be included
69
72
set_all_elem = set (range (0 , MAX_ELEM ))
70
73
if cfg .forbidden_elements :
@@ -166,17 +169,17 @@ def add_random(min_adds: int, max_adds: int, min_nat: int, max_nat: int):
166
169
"""
167
170
Default random atom generation.
168
171
"""
169
- numatoms_all = np . random . randint (
170
- min_adds , max_adds
172
+ numatoms_all = rng . integers (
173
+ low = min_adds , high = max_adds
171
174
) # with range(1, 7) -> mean value: 3.5
172
175
for _ in range (numatoms_all ):
173
176
# Define the atom type to be added via a random choice from the set of valid elements
174
- ati = np . random .choice (list (valid_elems ))
177
+ ati = rng .choice (list (valid_elems ))
175
178
if verbosity > 1 :
176
179
print (f"Adding atom type { ati } ..." )
177
180
# Add a random number of atoms of the defined type
178
- natoms [ati ] = natoms [ati ] + np . random . randint (
179
- min_nat , max_nat
181
+ natoms [ati ] = natoms [ati ] + rng . integers (
182
+ low = min_nat , high = max_nat
180
183
) # with range(0, 3) -> mean value: 1
181
184
# max value of this section with commented settings: 12
182
185
@@ -193,11 +196,11 @@ def add_organic(num_adds: int, min_nat: int, max_nat: int) -> None:
193
196
return
194
197
for _ in range (num_adds ): # with range(5) -> mean value 1.5
195
198
# go through the elements B to F (4-9 in 0-based indexing)
196
- ati = np . random .choice (valid_organic )
199
+ ati = rng .choice (valid_organic )
197
200
if verbosity > 1 :
198
201
print (f"Adding atom type { ati } ..." )
199
- natoms [ati ] = natoms [ati ] + np . random . randint (
200
- min_nat , max_nat
202
+ natoms [ati ] = natoms [ati ] + rng . integers (
203
+ low = min_nat , high = max_nat
201
204
) # with range(0, 3) -> mean value: 1
202
205
# max value of this section with commented settings: 8
203
206
@@ -243,7 +246,7 @@ def add_hydrogen():
243
246
# If no H is included, add H atoms
244
247
if natoms [0 ] == 0 :
245
248
nat = np .sum (natoms )
246
- randint = np .random . rand ()
249
+ randint = rng .random ()
247
250
j = 1 + round (randint * nat * 1.2 )
248
251
natoms [0 ] = natoms [0 ] + j
249
252
# Example: For 5 atoms at this point,
@@ -257,7 +260,7 @@ def check_min_max_atoms():
257
260
f"Minimal number of atoms: { cfg .min_num_atoms } ; "
258
261
+ f"Actual number of atoms: { np .sum (natoms )} .\n Adding atoms..."
259
262
)
260
- ati = np . random .choice (list (valid_elems ))
263
+ ati = rng .choice (list (valid_elems ))
261
264
max_limit = cfg .element_composition .get (ati , (None , None ))[1 ]
262
265
if max_limit is not None and natoms [ati ] >= max_limit :
263
266
continue
@@ -285,7 +288,7 @@ def check_min_max_atoms():
285
288
# randomly select an atom type from the list, thereby weighting the selection
286
289
# for reduction by the current occurrence
287
290
# generate a random number between 0 and the number of atoms in the list
288
- random_index = np . random . randint ( len (atom_list ))
291
+ random_index = rng . integers ( 0 , len (atom_list ))
289
292
i = atom_list [int (random_index )]
290
293
if natoms [i ] > 0 :
291
294
min_limit = cfg .element_composition .get (i , (None , None ))[0 ]
@@ -361,13 +364,15 @@ def generate_random_coordinates(at: np.ndarray) -> tuple[np.ndarray, np.ndarray]
361
364
atilist : list [int ] = []
362
365
xyz = np .zeros ((sum (at ), 3 ))
363
366
numatoms = 0
367
+ rng = np .random .default_rng ()
364
368
for elem , count in enumerate (at ):
365
369
for m in range (count ):
366
370
# different rules for hydrogen
367
371
if elem == 0 :
368
- xyz [numatoms + m , :] = np .random . rand (3 ) * 3 - 1.5
372
+ xyz [numatoms + m , :] = rng .random (3 ) * 3 - 1.5
369
373
else :
370
- xyz [numatoms + m , :] = np .random .rand (3 ) * 2 - 1
374
+ xyz [numatoms + m , :] = rng .random (3 ) * 2 - 1
375
+
371
376
atilist .append (elem )
372
377
373
378
numatoms += count
0 commit comments