Coverage for tests/test_savers.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1"""Test the functions which saves/load configurations. """ 

2 

3def test_savers(): 

4 import os 

5 import gamdpy as gp 

6 import numpy as np 

7 import h5py 

8 # Create a configuration to save 

9 conf = gp.Configuration(D=3) 

10 conf.make_positions(N=10, rho=1.0) 

11 # Save as xyz 

12 gp.tools.save_configuration(configuration=conf, filename="final.xyz", format="xyz") 

13 a = np.loadtxt("final.xyz", skiprows=1) 

14 assert (a==np.c_[conf.ptype, conf['r']]).all(), "Failure in save_configuration" 

15 os.remove("final.xyz") 

16 # Save as h5 

17# gp.configuration_to_hdf5(configuration=conf, filename="final.h5") 

18# with h5py.File("final.h5", "r") as f: 

19# assert [key for key in f.keys()] == ['m', 'ptype', 'r', 'r_im', 'v'] 

20# assert (f['r'] == conf['r']).all() 

21# assert (f['v'] == conf['v']).all() 

22# assert (f['ptype'] == conf.ptype).all() 

23# assert (f['m'] == conf['m']).all() 

24# os.remove("final.h5") 

25 # Save as lammps 

26 assert isinstance(gp.configuration_to_lammps(configuration=conf), str) 

27 # Save/load to/from rumd3 

28 gp.configuration_to_rumd3(configuration=conf, filename="restart.xyz.gz") 

29 read_conf = gp.configuration_from_rumd3("restart.xyz.gz") 

30 assert (read_conf['r'] == conf['r']).all() 

31 assert (read_conf['v'] == conf['v']).all() 

32 os.remove("restart.xyz.gz") 

33 

34if __name__ == '__main__': 

35 test_savers()