Coverage for ase/calculators/gaussian.py: 41.46%

82 statements  

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

1import copy 

2import os 

3from collections.abc import Iterable 

4 

5from ase.calculators.calculator import FileIOCalculator 

6from ase.io import read, write 

7 

8 

9class GaussianDynamics: 

10 calctype = 'optimizer' 

11 delete = ['force'] 

12 keyword: str | None = None 

13 special_keywords: dict[str, str] = {} 

14 

15 def __init__(self, atoms, calc=None): 

16 self.atoms = atoms 

17 if calc is not None: 

18 self.calc = calc 

19 else: 

20 if self.atoms.calc is None: 

21 raise ValueError( 

22 '{} requires a valid Gaussian calculator object!'.format( 

23 self.__class__.__name__ 

24 ) 

25 ) 

26 

27 self.calc = self.atoms.calc 

28 

29 def todict(self): 

30 return {'type': self.calctype, 'optimizer': self.__class__.__name__} 

31 

32 def delete_keywords(self, kwargs): 

33 """removes list of keywords (delete) from kwargs""" 

34 for d in self.delete: 

35 kwargs.pop(d, None) 

36 

37 def set_keywords(self, kwargs): 

38 args = kwargs.pop(self.keyword, []) 

39 if isinstance(args, str): 

40 args = [args] 

41 elif isinstance(args, Iterable): 

42 args = list(args) 

43 

44 for key, template in self.special_keywords.items(): 

45 if key in kwargs: 

46 val = kwargs.pop(key) 

47 args.append(template.format(val)) 

48 

49 kwargs[self.keyword] = args 

50 

51 def run(self, **kwargs): 

52 calc_old = self.atoms.calc 

53 params_old = copy.deepcopy(self.calc.parameters) 

54 

55 self.delete_keywords(kwargs) 

56 self.delete_keywords(self.calc.parameters) 

57 self.set_keywords(kwargs) 

58 

59 self.calc.set(**kwargs) 

60 self.atoms.calc = self.calc 

61 

62 try: 

63 self.atoms.get_potential_energy() 

64 except OSError: 

65 converged = False 

66 else: 

67 converged = True 

68 

69 atoms = read(self.calc.label + '.log') 

70 self.atoms.cell = atoms.cell 

71 self.atoms.positions = atoms.positions 

72 

73 self.calc.parameters = params_old 

74 self.calc.reset() 

75 if calc_old is not None: 

76 self.atoms.calc = calc_old 

77 

78 return converged 

79 

80 

81class GaussianOptimizer(GaussianDynamics): 

82 keyword = 'opt' 

83 special_keywords = { 

84 'fmax': '{}', 

85 'steps': 'maxcycle={}', 

86 } 

87 

88 

89class GaussianIRC(GaussianDynamics): 

90 keyword = 'irc' 

91 special_keywords = { 

92 'direction': '{}', 

93 'steps': 'maxpoints={}', 

94 } 

95 

96 

97class Gaussian(FileIOCalculator): 

98 _legacy_default_command = 'g16 < PREFIX.com > PREFIX.log' 

99 implemented_properties = ['energy', 'forces', 'dipole'] 

100 discard_results_on_any_change = True 

101 

102 fileio_rules = FileIOCalculator.ruleset( 

103 stdin_name='{prefix}.com', stdout_name='{prefix}.log' 

104 ) 

105 

106 def __init__(self, *args, label='Gaussian', **kwargs): 

107 super().__init__(*args, label=label, **kwargs) 

108 

109 def write_input(self, atoms, properties=None, system_changes=None): 

110 super().write_input(atoms, properties, system_changes) 

111 write( 

112 self.label + '.com', 

113 atoms, 

114 properties=properties, 

115 format='gaussian-in', 

116 parallel=False, 

117 **self.parameters, 

118 ) 

119 

120 def read_results(self): 

121 output = read(self.label + '.log', format='gaussian-out') 

122 self.calc = output.calc 

123 self.results = output.calc.results 

124 

125 # Method(s) defined in the old calculator, added here for 

126 # backwards compatibility 

127 def clean(self): 

128 for suffix in ['.com', '.chk', '.log']: 

129 try: 

130 os.remove(os.path.join(self.directory, self.label + suffix)) 

131 except OSError: 

132 pass 

133 

134 def get_version(self): 

135 raise NotImplementedError # not sure how to do this yet