Coverage for ase/_4/pes.py: 95.83%

48 statements  

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

1import numpy as np 

2 

3from ase._4.atoms import Atoms 

4from ase._4.calculators.calculator import BaseCalculator 

5from ase._4.calculators.results import CalculationResults 

6from ase.calculators.calculator import PropertyNotImplementedError 

7 

8 

9class PotentialEnergySurface: 

10 """Caches and manages evaluations of atoms. 

11 

12 Used by geometry optimisation, molecular dynamics, and 

13 similar, wherever caching is required. 

14 

15 Replacing ``pes.atoms`` or ``pes.calc`` invalidates the cache 

16 automatically. Mutating the existing atoms in place, however, 

17 bypasses the cache, so such operations must go through 

18 PotentialEnergySurface methods (e.g., ``pes.set_positions``) 

19 and not directly (NOT ``atoms.set_positions``). 

20 

21 DEV: Some requested properties are pre-set at initialization, 

22 but we might want to rethink this. 

23 

24 """ 

25 

26 def __init__( 

27 self, 

28 atoms: Atoms, 

29 calc: BaseCalculator, 

30 requested_properties: tuple[str, ...] | None = None, 

31 ): 

32 """Initialize the potential energy surface. 

33 

34 Parameters 

35 ---------- 

36 atoms: Atoms 

37 Atomic configuration to evaluate. 

38 calc: BaseCalculator 

39 Calculator used to compute properties of ``atoms``. 

40 requested_properties: tuple of str, optional 

41 Properties to request from ``calc`` on each evaluate(). 

42 Defaults to ``('free_energy', 'forces')`` if the calculator 

43 implements ``free_energy``, otherwise ``('energy', 'forces')``. 

44 """ 

45 self.atoms = atoms 

46 self.calc = calc 

47 

48 # requested properties follow general ASEv3 behaviour 

49 # for picking "energy" vs "free_energy". Likely to be 

50 # reconsidered in the future, but keeping it faithfully here. 

51 if requested_properties is None: 

52 if 'free_energy' in calc.implemented_properties: 

53 requested_properties = ('free_energy', 'forces') 

54 else: 

55 requested_properties = ('energy', 'forces') 

56 self.requested_properties = tuple(requested_properties) 

57 unsupported = set(self.requested_properties) - set( 

58 calc.implemented_properties 

59 ) 

60 if unsupported: 

61 raise ValueError( 

62 f'requested_properties {sorted(unsupported)} not in ' 

63 f'calc.implemented_properties ' 

64 f'{sorted(calc.implemented_properties)}' 

65 ) 

66 self.results: CalculationResults | None = None 

67 

68 @property 

69 def atoms(self) -> Atoms: 

70 return self._atoms 

71 

72 @atoms.setter 

73 def atoms(self, atoms: Atoms) -> None: 

74 self.invalidate() 

75 self._atoms = atoms 

76 

77 @property 

78 def calc(self) -> BaseCalculator: 

79 return self._calc 

80 

81 @calc.setter 

82 def calc(self, calc: BaseCalculator) -> None: 

83 self.invalidate() 

84 self._calc = calc 

85 

86 def evaluate(self) -> CalculationResults: 

87 if self.results is None: 

88 self.results = self.calc.evaluate( 

89 self.atoms, properties=list(self.requested_properties) 

90 ) 

91 return self.results 

92 

93 def invalidate(self) -> None: 

94 self.results = None 

95 

96 def get_positions(self) -> np.ndarray: 

97 return self.atoms.get_positions() 

98 

99 def set_positions(self, x: np.ndarray) -> None: 

100 """Assumes x is positions-shaped. Invalidates the cache.""" 

101 self.invalidate() 

102 self.atoms.set_positions(x) 

103 

104 def get_property(self, prop: str): 

105 results = self.evaluate() 

106 if prop not in results.properties: 

107 raise PropertyNotImplementedError( 

108 f'Tried to get {prop} which has not been evaluated. ' 

109 f'Available properties are {list(results.properties.keys())}.' 

110 ) 

111 return results.properties[prop]