Coverage for ase/calculators/abc.py: 96.61%

59 statements  

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

1""" 

2This module defines abstract helper classes with the objective of reducing 

3boilerplace method definitions (i.e. duplication) in calculators. 

4""" 

5 

6from abc import ABC, abstractmethod 

7from collections.abc import Mapping 

8from typing import Any 

9 

10 

11class GetPropertiesMixin(ABC): 

12 """Mixin class which provides get_forces(), get_stress() and so on. 

13 

14 Inheriting class must implement get_property().""" 

15 

16 @abstractmethod 

17 def get_property(self, name, atoms=None, allow_calculation=True): 

18 """Get the named property.""" 

19 

20 def get_potential_energy(self, atoms=None, force_consistent=False): 

21 if force_consistent: 

22 name = 'free_energy' 

23 else: 

24 name = 'energy' 

25 return self.get_property(name, atoms) 

26 

27 def get_potential_energies(self, atoms=None): 

28 return self.get_property('energies', atoms) 

29 

30 def get_forces(self, atoms=None): 

31 return self.get_property('forces', atoms) 

32 

33 def get_stress(self, atoms=None): 

34 return self.get_property('stress', atoms) 

35 

36 def get_stresses(self, atoms=None): 

37 """the calculator should return intensive stresses, i.e., such that 

38 stresses.sum(axis=0) == stress 

39 """ 

40 return self.get_property('stresses', atoms) 

41 

42 def get_dipole_moment(self, atoms=None): 

43 return self.get_property('dipole', atoms) 

44 

45 def get_charges(self, atoms=None): 

46 return self.get_property('charges', atoms) 

47 

48 def get_magnetic_moment(self, atoms=None): 

49 return self.get_property('magmom', atoms) 

50 

51 def get_magnetic_moments(self, atoms=None): 

52 """Calculate magnetic moments projected onto atoms.""" 

53 return self.get_property('magmoms', atoms) 

54 

55 

56class GetOutputsMixin(ABC): 

57 """Mixin class for providing get_fermi_level() and others. 

58 

59 Effectively this class expresses data in calc.results as 

60 methods such as get_fermi_level(). 

61 

62 Inheriting class must implement _outputmixin_get_results(), 

63 typically returning self.results, which must be a mapping 

64 using the naming defined in ase.outputs.Properties. 

65 """ 

66 

67 @abstractmethod 

68 def _outputmixin_get_results(self) -> Mapping[str, Any]: 

69 """Return Mapping of names to result value. 

70 

71 This may be called many times and should hence not be 

72 expensive (except possibly the first time).""" 

73 

74 def _get(self, name): 

75 # Cyclic import, should restructure. 

76 from ase.calculators.calculator import PropertyNotPresent 

77 

78 dct = self._outputmixin_get_results() 

79 try: 

80 return dct[name] 

81 except KeyError: 

82 raise PropertyNotPresent(name) 

83 

84 def get_fermi_level(self): 

85 return self._get('fermi_level') 

86 

87 def get_ibz_k_points(self): 

88 return self._get('ibz_kpoints') 

89 

90 def get_k_point_weights(self): 

91 return self._get('kpoint_weights') 

92 

93 def get_eigenvalues(self, kpt=0, spin=0): 

94 eigs = self._get('eigenvalues') 

95 return eigs[spin, kpt] 

96 

97 def _eigshape(self): 

98 # We don't need this if we already have a Properties object. 

99 return self._get('eigenvalues').shape 

100 

101 def get_occupation_numbers(self, kpt=0, spin=0): 

102 occs = self._get('occupations') 

103 return occs[spin, kpt] 

104 

105 def get_number_of_bands(self): 

106 return self._eigshape()[2] 

107 

108 def get_number_of_spins(self): 

109 nspins = self._eigshape()[0] 

110 assert nspins in [1, 2] 

111 return nspins 

112 

113 def get_spin_polarized(self): 

114 return self.get_number_of_spins() == 2