Coverage for ase/io/amber.py: 86.52%

89 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3import ase.units as units 

4 

5 

6def write_amber_coordinates(atoms, filename): 

7 from scipy.io import netcdf_file 

8 

9 with netcdf_file(filename, 'w', mmap=False) as fout: 

10 _write_amber_coordinates(atoms, fout) 

11 

12 

13def read_amber_coordinates(filename): 

14 from scipy.io import netcdf_file 

15 

16 with netcdf_file(filename, 'r', mmap=False) as fin: 

17 return _read_amber_coordinates(fin) 

18 

19 

20def _write_amber_coordinates(atoms, fout): 

21 fout.Conventions = 'AMBERRESTART' 

22 fout.ConventionVersion = '1.0' 

23 fout.title = 'Ase-generated-amber-restart-file' 

24 fout.application = 'AMBER' 

25 fout.program = 'ASE' 

26 fout.programVersion = '1.0' 

27 fout.createDimension('cell_spatial', 3) 

28 fout.createDimension('label', 5) 

29 fout.createDimension('cell_angular', 3) 

30 fout.createDimension('time', 1) 

31 time = fout.createVariable('time', 'd', ('time',)) 

32 time.units = 'picosecond' 

33 time[0] = 0 

34 fout.createDimension('spatial', 3) 

35 spatial = fout.createVariable('spatial', 'c', ('spatial',)) 

36 spatial[:] = np.asarray(list('xyz')) 

37 

38 natom = len(atoms) 

39 fout.createDimension('atom', natom) 

40 coordinates = fout.createVariable('coordinates', 'd', ('atom', 'spatial')) 

41 coordinates.units = 'angstrom' 

42 coordinates[:] = atoms.get_positions() 

43 

44 velocities = fout.createVariable('velocities', 'd', ('atom', 'spatial')) 

45 velocities.units = 'angstrom/picosecond' 

46 # Amber's units of time are 1/20.455 ps 

47 # Any other units are ignored in restart files, so these 

48 # are the only ones it is safe to print 

49 # See: http://ambermd.org/Questions/units.html 

50 # Apply conversion factor from ps: 

51 velocities.scale_factor = 20.455 

52 # get_velocities call returns velocities with units sqrt(eV/u) 

53 # so convert to Ang/ps 

54 factor = units.fs * 1000 / velocities.scale_factor 

55 velocities[:] = atoms.get_velocities() * factor 

56 

57 fout.createVariable('cell_angular', 'c', ('cell_angular', 'label')) 

58 

59 cell_spatial = fout.createVariable('cell_spatial', 'c', ('cell_spatial',)) 

60 cell_spatial[:] = ['a', 'b', 'c'] 

61 

62 cell_lengths = fout.createVariable('cell_lengths', 'd', ('cell_spatial',)) 

63 cell_lengths.units = 'angstrom' 

64 cell_lengths[:] = atoms.cell.lengths() 

65 

66 if not atoms.cell.orthorhombic: 

67 raise ValueError('Non-orthorhombic cell not supported with amber') 

68 

69 cell_angles = fout.createVariable('cell_angles', 'd', ('cell_angular',)) 

70 cell_angles[:3] = 90.0 

71 cell_angles.units = 'degree' 

72 

73 

74def _read_amber_coordinates(fin): 

75 from ase import Atoms 

76 

77 all_coordinates = fin.variables['coordinates'][:] 

78 get_last_frame = False 

79 if hasattr(all_coordinates, 'ndim'): 

80 if all_coordinates.ndim == 3: 

81 get_last_frame = True 

82 elif hasattr(all_coordinates, 'shape'): 

83 if len(all_coordinates.shape) == 3: 

84 get_last_frame = True 

85 if get_last_frame: 

86 all_coordinates = all_coordinates[-1] 

87 

88 atoms = Atoms(positions=all_coordinates) 

89 

90 if 'velocities' in fin.variables: 

91 all_velocities = fin.variables['velocities'] 

92 if hasattr(all_velocities, 'units'): 

93 if all_velocities.units != b'angstrom/picosecond': 

94 raise Exception(f'Unrecognised units {all_velocities.units}') 

95 if hasattr(all_velocities, 'scale_factor'): 

96 scale_factor = all_velocities.scale_factor 

97 else: 

98 scale_factor = 1.0 

99 all_velocities = all_velocities[:] * scale_factor 

100 all_velocities = all_velocities / (1000 * units.fs) 

101 if get_last_frame: 

102 all_velocities = all_velocities[-1] 

103 atoms.set_velocities(all_velocities) 

104 if 'cell_lengths' in fin.variables: 

105 all_abc = fin.variables['cell_lengths'] 

106 if get_last_frame: 

107 all_abc = all_abc[-1] 

108 a, b, c = all_abc 

109 all_angles = fin.variables['cell_angles'] 

110 if get_last_frame: 

111 all_angles = all_angles[-1] 

112 alpha, beta, gamma = all_angles 

113 

114 if all(angle > 89.99 for angle in [alpha, beta, gamma]) and all( 

115 angle < 90.01 for angle in [alpha, beta, gamma] 

116 ): 

117 atoms.set_cell(np.array([[a, 0, 0], [0, b, 0], [0, 0, c]])) 

118 atoms.set_pbc(True) 

119 else: 

120 raise NotImplementedError( 

121 'only rectangular cells are implemented in ASE-AMBER' 

122 ) 

123 

124 else: 

125 atoms.set_pbc(False) 

126 

127 return atoms