Coverage for gpaw/lrtddft/kssrestrictor.py: 96%
46 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-07-14 00:18 +0000
1import sys
2from ase.units import Hartree
5class KSSRestrictor:
6 """Object to handle KSSingles restrictions"""
7 defaults = {'eps': 0.01,
8 'istart': 0,
9 'jend': sys.maxsize,
10 'energy_range': None,
11 'from': None,
12 'to': None}
14 def __init__(self, dictionary=None):
15 if dictionary is None:
16 dictionary = {}
17 self._vals = {}
18 self.update(dictionary)
20 def __getitem__(self, index):
21 assert index in self.defaults
22 return self._vals.get(index, self.defaults[index])
24 def __setitem__(self, index, value):
25 assert index in self.defaults
26 self._vals[index] = value
28 def update(self, dictionary):
29 for key, value in dictionary.items():
30 self[key] = value
32 def emin_emax(self):
33 emin = -sys.float_info.max
34 emax = sys.float_info.max
35 if self['energy_range'] is not None:
36 try:
37 emin, emax = self['energy_range']
38 emin /= Hartree
39 emax /= Hartree
40 except TypeError:
41 emax = self['energy_range'] / Hartree
42 return emin, emax
44 @property
45 def values(self):
46 dct = {}
47 dct.update(self._vals)
48 return dct
50 def __str__(self):
51 return str(self.values)
53 def is_good(self, ks) -> bool:
54 """Check if Kohn-Sham single fulfills the criterion"""
55 emin, emax = self.emin_emax()
57 ok = (ks.fij / ks.weight) > self['eps']
58 ok &= ks.i >= self['istart'] and ks.j <= self['jend']
59 ok &= ks.energy >= emin and ks.energy <= emax
60 if self['from'] is not None:
61 ok &= ks.i in self['from']
62 if self['to'] is not None:
63 ok &= ks.j in self['to']
64 return ok