Skip to content

Commit

Permalink
Use subparser instead of multiple quantities
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaelman committed Jan 17, 2025
1 parent 7a9288e commit 6dec427
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions electronicparsers/quantumespresso/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2037,13 +2037,14 @@ def str_to_sticks(val_in):
),
Quantity(
'number_of_electrons',
rf'number of electrons\s*=\s*({re_float})',
dtype=float,
),
Quantity(
'number_of_spin_electrons',
rf'number of electrons\s*=[\s\S]*up:\s*({re_float})\s*,\s*down:\s*({re_float})',
dtype=float,
rf'(number of electrons\s*=[^\n]*)',
sub_parser=TextParser(
quantities=[
Quantity('total', rf'number of electrons\s*=\s*({re_float})', dtype=float),
Quantity('up', rf'up:\s*({re_float})', dtype=float),
Quantity('down', rf'down:\s*({re_float})', dtype=float),
],
),
),
Quantity(
'x_qe_number_of_states',
Expand Down Expand Up @@ -2795,6 +2796,16 @@ def __init__(self):
}
self._re_label = re.compile(r'([A-Z][a-z]?)')

def get_n_electrons_safe(self) -> float:
n_electrons = self.out_parser.get('run', [])
if n_electrons:
n_electrons = n_electrons[0].get_header('number_of_electrons', {})
if total_n_electrons := n_electrons.get('total'):
return total_n_electrons
elif (up := n_electrons.get('up')) and (down := n_electrons.get('down')):
self.logger.warning('Number of electrons not found. Using spin up + down.')
return up + down

def parse_scc(self, run, calculation):
sec_run = self.archive.run[-1]
initial_time = (
Expand Down Expand Up @@ -2914,8 +2925,7 @@ def parse_scc(self, run, calculation):
if np.array(fermi_energy).dtype == float:
sec_energy.fermi = fermi_energy * ureg.eV

n_electrons = run.get_header('number_of_electrons', [])
if homo is None and fermi_energy is None and len(n_electrons) == 0:
if homo is None and fermi_energy is None and len(self.get_n_electrons_safe()) == 0:
self.logger.error('Reference energy is not defined')

for key in ['magnetization_total', 'magnetization_absolute']:
Expand Down Expand Up @@ -3494,15 +3504,7 @@ def parse_method(self, run):
if atom_sp[i] is not None:
setattr(sec_method_atom_kind, atom_species_names[i], atom_sp[i])

if (n_electrons := run.get_header('number_of_electrons')) is not None:
sec_method.electronic.n_electrons = n_electrons
elif (
len(
n_spin_electrons := list(run.get_header('number_of_spin_electrons', []))
)
== 2
):
sec_method.electronic.n_electrons = sum(n_spin_electrons)
sec_method.electronic.n_electrons = self.get_n_electrons_safe()

def init_parser(self):
self.out_parser.mainfile = self.filepath
Expand Down

0 comments on commit 6dec427

Please sign in to comment.