Coverage for gamdpy/tools/save_configuration.py: 86%

7 statements  

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

1 

2 

3def save_configuration(configuration, filename:str, format="xyz", append=False): 

4 """ Saves configuration to file. 

5 

6 Helpful for, e.g., VMD visualization 

7 This current version only supports the standard xyz-format. 

8 

9 Example 

10 ------- 

11 

12 >>> import os 

13 >>> import gamdpy as gp 

14 >>> import numpy as np 

15 >>> conf = gp.Configuration(D=3, N=10) 

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

17 >>> os.remove("final.xyz") # Removes file (for doctests) 

18 

19 """ 

20 

21 import numpy as np 

22 append = "a" if append else "w" 

23 

24 if format=="xyz": 

25 with open(filename, append) as f: 

26 np.savetxt(f, np.c_[configuration.ptype, configuration['r']], 

27 header=f"{configuration.N}\n#Position xyz format - generated by gamdpy", comments="") 

28 else: 

29 raise ValueError("Format not supported") 

30 

31