@@ -155,27 +155,27 @@ def __init__(
155
155
156
156
nframes = self .numFrames
157
157
if box is None :
158
- self .box = np .zeros ((3 , nframes ), np . float32 )
158
+ self .box = np .zeros ((3 , nframes ), Molecule . _dtypes [ "box" ] )
159
159
if boxangles is None :
160
- self .boxangles = np .zeros ((3 , nframes ), np . float32 )
160
+ self .boxangles = np .zeros ((3 , nframes ), Molecule . _dtypes [ "boxangles" ] )
161
161
if step is None :
162
- self .step = np .arange (nframes , dtype = int )
162
+ self .step = np .arange (nframes , dtype = Molecule . _dtypes [ "step" ] )
163
163
if time is None :
164
- self .time = np .zeros (nframes , dtype = np . float32 )
164
+ self .time = np .zeros (nframes , dtype = Molecule . _dtypes [ "time" ] )
165
165
if box is not None :
166
- self .box = box
166
+ self .box = np . array ( box , dtype = Molecule . _dtypes [ "box" ])
167
167
if boxangles is None :
168
168
raise ValueError ("boxangles must be provided if box is provided" )
169
169
if boxangles is not None :
170
- self .boxangles = boxangles
170
+ self .boxangles = np . array ( boxangles , dtype = Molecule . _dtypes [ "boxangles" ])
171
171
if box is None :
172
172
raise ValueError ("box must be provided if boxangles is provided" )
173
173
if fileloc is not None :
174
174
self .fileloc = fileloc
175
175
if step is not None :
176
- self .step = step
176
+ self .step = np . array ( step , dtype = Molecule . _dtypes [ "step" ])
177
177
if time is not None :
178
- self .time = time
178
+ self .time = np . array ( time , dtype = Molecule . _dtypes [ "time" ])
179
179
180
180
@property
181
181
def numFrames (self ):
@@ -410,25 +410,27 @@ def _parseTraj(mol, traj, filename, frame):
410
410
setattr (traj , field , np .ascontiguousarray (getattr (traj , field )))
411
411
412
412
if len (traj .coords ):
413
- mol .coords = traj .coords
413
+ mol .coords = np . array ( traj .coords , dtype = Molecule . _dtypes [ "coords" ])
414
414
415
415
if traj .box is None :
416
416
mol .box = np .zeros ((3 , 1 ), dtype = Molecule ._dtypes ["box" ])
417
417
else :
418
- mol .box = np .array (traj .box )
418
+ mol .box = np .array (traj .box , dtype = Molecule . _dtypes [ "box" ] )
419
419
if mol .box .ndim == 1 :
420
420
mol .box = mol .box [:, np .newaxis ]
421
421
422
422
if traj .boxangles is None :
423
423
mol .boxangles = np .zeros ((3 , 1 ), dtype = Molecule ._dtypes ["boxangles" ])
424
424
else :
425
- mol .boxangles = np .array (traj .boxangles )
425
+ mol .boxangles = np .array (
426
+ traj .boxangles , dtype = Molecule ._dtypes ["boxangles" ]
427
+ )
426
428
if mol .boxangles .ndim == 1 :
427
429
mol .boxangles = mol .boxangles [:, np .newaxis ]
428
430
429
431
# mol.fileloc = traj.fileloc
430
- mol .step = np .hstack (traj .step ).astype (int )
431
- mol .time = np .hstack (traj .time )
432
+ mol .step = np .hstack (traj .step ).astype (Molecule . _dtypes [ "step" ] )
433
+ mol .time = np .hstack (traj .time ). astype ( Molecule . _dtypes [ "time" ])
432
434
433
435
if ext in _TRAJECTORY_READERS and frame is None and len (traj .coords ):
434
436
# Writing hidden index file containing number of frames in trajectory file
@@ -1251,15 +1253,17 @@ def _fix_formal_charge(val):
1251
1253
]
1252
1254
1253
1255
if len (topo .bonds ):
1254
- topo .bonds = np .array (topo .bonds , dtype = np . uint32 )
1256
+ topo .bonds = np .array (topo .bonds , dtype = Molecule . _dtypes [ "bonds" ] )
1255
1257
badidx = ~ np .all (np .isin (topo .bonds , topo .serial ), axis = 1 )
1256
1258
if np .any (badidx ):
1257
1259
# Some PDBs have bonds to non-existing serials... go figure
1258
1260
topo .bonds = topo .bonds [~ badidx ]
1259
1261
logger .info (
1260
1262
f"Discarded { np .sum (badidx )} bonds to non-existing indexes in the PDB file."
1261
1263
)
1262
- topo .bonds = np .array (mapserials [topo .bonds [:]], dtype = np .uint32 )
1264
+ topo .bonds = np .array (
1265
+ mapserials [topo .bonds [:]], dtype = Molecule ._dtypes ["bonds" ]
1266
+ )
1263
1267
1264
1268
# If no segid was read, use the TER rows to define segments
1265
1269
if len (topo .segid ) and np .all (np .array (topo .segid ) == "" ) and currter != 0 :
@@ -1559,9 +1563,9 @@ def XTCread(filename, frame=None, topoloc=None):
1559
1563
time *= 1e3 # Convert from ps to fs. This seems to be ACEMD3 specific. GROMACS writes other units in time
1560
1564
nframes = coords .shape [2 ]
1561
1565
if len (step ) != nframes or np .sum (step ) == 0 :
1562
- step = np .arange (nframes )
1566
+ step = np .arange (nframes , dtype = Molecule . _dtypes [ "step" ] )
1563
1567
if len (time ) != nframes or np .sum (time ) == 0 :
1564
- time = np .zeros (nframes , dtype = np . float32 )
1568
+ time = np .zeros (nframes , dtype = Molecule . _dtypes [ "time" ] )
1565
1569
1566
1570
bx , by , bz , alpha , beta , gamma = box_vectors_to_lengths_and_angles (
1567
1571
boxvectors [0 ].T , boxvectors [1 ].T , boxvectors [2 ].T
@@ -1594,16 +1598,16 @@ def XSCread(filename, frame=None, topoloc=None):
1594
1598
bx , by , bz , alpha , beta , gamma = box_vectors_to_lengths_and_angles (
1595
1599
pieces [1 :4 ], pieces [4 :7 ], pieces [7 :10 ]
1596
1600
)
1597
- box = np .array ([bx , by , bz ], dtype = np . float32 )
1598
- boxangles = np .array ([alpha , beta , gamma ], dtype = np . float32 )
1601
+ box = np .array ([bx , by , bz ])
1602
+ boxangles = np .array ([alpha , beta , gamma ])
1599
1603
1600
1604
return MolFactory .construct (
1601
1605
None ,
1602
1606
Trajectory (
1603
1607
box = box ,
1604
1608
boxangles = boxangles ,
1605
1609
step = [step ],
1606
- time = np . zeros ( 1 , dtype = np . float32 ) ,
1610
+ time = [ 0 ] ,
1607
1611
),
1608
1612
filename ,
1609
1613
frame ,
@@ -1710,7 +1714,9 @@ def BINCOORread(filename, frame=None, topoloc=None):
1710
1714
dat = f .read (natoms * 3 * 8 )
1711
1715
fmt = "d" * (natoms * 3 )
1712
1716
coords = struct .unpack (fmt , dat )
1713
- coords = np .array (coords , dtype = np .float32 ).reshape ((natoms , 3 , 1 ))
1717
+ coords = np .array (coords , dtype = Molecule ._dtypes ["coords" ]).reshape (
1718
+ (natoms , 3 , 1 )
1719
+ )
1714
1720
return MolFactory .construct (None , Trajectory (coords = coords ), filename , frame )
1715
1721
1716
1722
@@ -1772,7 +1778,10 @@ def MDTRAJTOPOread(filename, frame=None, topoloc=None, validateElements=True):
1772
1778
for k in table .keys ():
1773
1779
topo .__dict__ [translate [k ]] = table [k ].tolist ()
1774
1780
1775
- coords = np .array (mdstruct .xyz .swapaxes (0 , 1 ).swapaxes (1 , 2 ) * 10 , dtype = np .float32 )
1781
+ coords = np .array (
1782
+ mdstruct .xyz .swapaxes (0 , 1 ).swapaxes (1 , 2 ) * 10 ,
1783
+ dtype = Molecule ._dtypes ["coords" ],
1784
+ )
1776
1785
topo .bonds = bonds [:, :2 ]
1777
1786
return MolFactory .construct (
1778
1787
topo ,
@@ -2322,9 +2331,11 @@ def fixDefault(val, dtype):
2322
2331
if len (ideal_coords ) != 0 :
2323
2332
ideal_allcoords .append (ideal_coords )
2324
2333
2325
- allcoords = np .stack (allcoords , axis = 2 ).astype (np . float32 )
2334
+ allcoords = np .stack (allcoords , axis = 2 ).astype (Molecule . _dtypes [ "coords" ] )
2326
2335
if len (ideal_allcoords ):
2327
- ideal_allcoords = np .stack (ideal_allcoords , axis = 2 ).astype (np .float32 )
2336
+ ideal_allcoords = np .stack (ideal_allcoords , axis = 2 ).astype (
2337
+ Molecule ._dtypes ["coords" ]
2338
+ )
2328
2339
2329
2340
coords = allcoords
2330
2341
if np .any (np .all (allcoords == 0 , axis = 1 )) and len (ideal_allcoords ):
@@ -2423,10 +2434,12 @@ def _guessMass(element):
2423
2434
topo .element = np .array (
2424
2435
[element_by_type [t ].capitalize () for t in topo .atomtype ], dtype = object
2425
2436
)
2426
- topo .masses = np .array ([mass_by_type [t ] for t in topo .atomtype ], dtype = np .float32 )
2427
- topo .bonds = np .vstack (bonds )
2437
+ topo .masses = np .array (
2438
+ [mass_by_type [t ] for t in topo .atomtype ], dtype = Molecule ._dtypes ["masses" ]
2439
+ )
2440
+ topo .bonds = np .vstack (bonds ).astype (Molecule ._dtypes ["bonds" ])
2428
2441
2429
- improper_indices = np .array (improper_indices ).astype (np . uint32 )
2442
+ improper_indices = np .array (improper_indices ).astype (Molecule . _dtypes [ "impropers" ] )
2430
2443
if improper_indices .ndim == 1 :
2431
2444
improper_indices = improper_indices [:, np .newaxis ]
2432
2445
topo .impropers = improper_indices
@@ -2483,7 +2496,7 @@ def PREPIread(filename, frame=None, topoloc=None):
2483
2496
continue
2484
2497
impropers .append ([names .index (impn ) for impn in impropernames ])
2485
2498
2486
- impropers = np .array (impropers ).astype (np . uint32 )
2499
+ impropers = np .array (impropers ).astype (Molecule . _dtypes [ "impropers" ] )
2487
2500
if impropers .ndim == 1 :
2488
2501
impropers = impropers [:, np .newaxis ]
2489
2502
@@ -2496,7 +2509,7 @@ def PREPIread(filename, frame=None, topoloc=None):
2496
2509
topo = Topology ()
2497
2510
topo .name = np .array (names , dtype = object )
2498
2511
topo .atomtype = np .array (atomtypes , dtype = object )
2499
- topo .charge = np .array (charges , dtype = np . float32 )
2512
+ topo .charge = np .array (charges , dtype = Molecule . _dtypes [ "charge" ] )
2500
2513
topo .impropers = impropers
2501
2514
2502
2515
return MolFactory .construct (topo , None , filename , frame )
0 commit comments