Coverage for ase/_4/optimize/symopt.py: 71.70%
53 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1from dataclasses import asdict, dataclass
2from typing import ClassVar
4import numpy as np
6from ase._4.optimize._symopt.relax import SymmetryAdaptedAtoms
9@dataclass
10class SymGradient:
11 gradient: np.ndarray
12 fnorm: float
13 snorm: float
14 converged: bool
15 backF_av: np.ndarray
16 backS_vv: np.ndarray
17 symmetry_force_violation: float
18 symmetry_stress_violation: float
19 volume: float
21 def loginfo(self):
22 return {
23 'F': self.fnorm,
24 'S': self.snorm,
25 'Ferr': self.symmetry_force_violation,
26 'Serr': self.symmetry_stress_violation,
27 'V': self.volume,
28 }
30 def datafy(self):
31 return asdict(self)
34@dataclass
35class SymOpt:
36 iotype: ClassVar[str] = 'symopt'
38 symmatoms: SymmetryAdaptedAtoms
39 fmax: float
40 smax: float
42 def step_to_string(self, step):
43 return self.symmatoms.step_to_string(
44 step.i, self.fmax, self.smax, step.gradient_obj
45 )
47 def log_headers(self):
48 return self.symmatoms.logheaders()
50 def gradient_norm(self, gradient):
51 # Symmetry-reduced coordinates need their own norm,
52 # not the Cartesian default.
53 return self.symmatoms.gradient_norm(gradient)
55 def datafy(self):
56 # What do we need to save?
57 # symmatoms defines the initial reduced positions as 0, so the
58 # mapping absolutely needs saving aside from the Hessian.
60 return {
61 'fmax': self.fmax,
62 'smax': self.smax,
63 'symmatoms': self.symmatoms.datafy(),
64 }
66 @classmethod
67 def undatafy(cls, dct, calc):
68 dct = dct.copy()
69 dct['symmatoms'] = SymmetryAdaptedAtoms.undatafy(dct['symmatoms'], calc)
70 return cls(**dct)
72 @classmethod
73 def undatafy_gradient(cls, dct):
74 return SymGradient(**dct)
76 def get_value(self):
77 return self.symmatoms.get_value()
79 def get_gradient(self):
80 return self.symmatoms._get_gradient(fmax=self.fmax, smax=self.smax)
82 def get_x(self):
83 return self.symmatoms.get_x()
85 def set_x(self, x):
86 self.symmatoms.set_x(x)
88 def initial_hessian(self):
89 raise NotImplementedError
91 def iterimages(self):
92 yield self.symmatoms.actual_atoms