mag4.energy
DFT total-energy parsers (VASP OUTCAR, WIEN2k case.scf).
The get_energy() dispatcher returns an EnergyResult describing
both the converged total energy (in eV) and a structured convergence
status, which the upcoming mag4-extract CLI uses to decide whether each
configuration is safe to feed into the J-extraction formulas.
Only the fields actually needed for the four-state pipeline are extracted:
the last total energy printed in the file (assumed converged);
a coarse convergence flag (the calculation finished cleanly);
for WIEN2k: the last
:DIS(charge distance) value and the verbatim:DIS/:ENElines, so the user can verify the parser picked up the right data and that the SCF converged tightly enough.
Both parsers are streaming (line-by-line) and tolerate large output files without loading them entirely into memory.
Module Attributes
1 Rydberg in electron-volts (CODATA 2018). |
Functions
|
Parse a VASP |
|
Band gap from the eigenvalue/occupation block at the end of a VASP OUTCAR. |
|
Parse a WIEN2k SCF file and return its converged total energy in eV. |
|
Band gap from a WIEN2k SCF file. |
|
Convenience dispatcher: parse the right output for the requested code. |
|
Band gap (eV) + metallicity for one config, or |
|
Total magnetization (μ_B) for the requested code, or |
|
Per-site magnetic moments for the requested code (dispatcher). |
|
Total magnetization (μ_B) of a converged VASP run, or |
|
Per-ion magnetic moments (μ_B) from a VASP |
|
Total magnetic moment in the cell (μ_B) from the last |
Classes
|
Band gap of one configuration (eV). |
|
Outcome of parsing one DFT output file. |
- class mag4.energy.EnergyResult(energy_eV, converged, source, n_iter=0, diagnostics=<factory>, last_dis=None, last_dis_line=None, last_ene_line=None)[source]
Bases:
objectOutcome of parsing one DFT output file.
- Parameters:
- energy_eV
Last total energy reported in the file, expressed in eV.
Nonewhen no energy could be parsed (typically: the calculation never produced one — crashed early, file truncated, etc.).- Type:
float or None
- converged
Coarse convergence flag (see backend-specific notes in
extract_vasp_energy()/extract_wien2k_energy()).- Type:
- mag4.energy.extract_vasp_energy(outcar_path)[source]
Parse a VASP
OUTCARand return its converged total energy.- Convergence rule (simple, code-agnostic):
convergedisTruewhen all of the following hold:the file exists and is readable,
at least one
free energy TOTENline was parsed,no
WARNING: ... NELM = Nline was emitted,the SCF actually converged — either VASP’s electronic marker
aborting loop because EDIFF is reachedor the ionicreached required accuracyappears — or the run finished cleanly (Voluntary context switches:).
The clean-finish marker is sufficient but not required: a job OOM-killed or timed out during the final clean-up drops that tail without invalidating the already-converged energy, so the EDIFF marker rescues it (see
diagnostics).Tighter convergence checks (per-element forces, stress, EDIFF / EDIFFG numerical thresholds) are intentionally NOT enforced here — they vary per project; see
diagnosticsfor the raw flags so the caller can decide what to do.
- Returns:
Always non-
None; check.energy_eVand.convergedto interpret the outcome.- Return type:
- Parameters:
outcar_path (str)
- mag4.energy.extract_wien2k_energy(scf_path)[source]
Parse a WIEN2k SCF file and return its converged total energy in eV.
- Return type:
- Parameters:
scf_path (str)
- Convergence rule (simple):
convergedisTruewhen all of the following hold:the file exists and is readable,
at least one
:ENEline was parsed,at least one
:ITE…:iteration block was detected,the energy was emitted by the last iteration block of the file (i.e. the calculation didn’t crash mid-iteration).
This does not enforce a numeric threshold on
:DIS(charge distance) — the value is surfaced inEnergyResult.last_disand the verbatim:DISline inEnergyResult.last_dis_lineso the user can eyeball that the SCF converged tightly enough.
Conversion:
E [eV] = E [Ry] × 13.605693122994.
- mag4.energy.get_energy(config_dir, code, scf_basename=None)[source]
Convenience dispatcher: parse the right output for the requested code.
- Parameters:
config_dir (str) – Path to a single
configN/directory.code ({'vasp', 'wien2k'}) –
Which output file to look for inside
config_dir:vasp→OUTCARwien2k→ SCF file (seescf_basename).
scf_basename (str, optional) – WIEN2k only. Basename (with or without the
.scfsuffix) of the SCF file to parse insideconfig_dir. Useful when several SCF runs are stored side by side under names likepbe.scf,r2scan.scf. When omitted (default) the standard WIEN2k convention<basename(config_dir)>.scfis used — i.e.config5/config5.scfforconfig5/.
- Return type:
- mag4.energy.read_vasp_magnetization(config_dir)[source]
Total magnetization (μ_B) of a converged VASP run, or
None.Prefers the last
mag=inOSZICAR; falls back to theOUTCARnumber of electron … magnetization …line. Collinear (ISPIN=2) runs report a single scalar moment.
- mag4.energy.read_vasp_site_moments(config_dir, ion_indices)[source]
Per-ion magnetic moments (μ_B) from a VASP
OUTCAR.Parses the last
magnetization (x)block (the converged one; needsLORBIT=10/11in the INCAR — mag4 setsLORBIT=11by default) and returns thetotcolumn (last field of each ion row) for the requested 1-based ion indices. Ion order follows the POSCAR; mag4 writes the magnetic atoms first, so they are ions1 … n_mag.Returns
{ion: float or None}(Nonefor an ion the block didn’t list, or{}-like all-None when no block/OUTCAR was found).
- mag4.energy.read_site_moments(config_dir, code, ion_indices, scf_basename=None)[source]
Per-site magnetic moments for the requested code (dispatcher).
- mag4.energy.read_wien2k_magnetization(config_dir, scf_basename=None)[source]
Total magnetic moment in the cell (μ_B) from the last
:MMTOTline.The SCF file is resolved exactly as in
get_energy()(<basename(config_dir)>.scfunlessscf_basenameoverrides it).
- mag4.energy.read_magnetization(config_dir, code, scf_basename=None)[source]
Total magnetization (μ_B) for the requested code, or
None.
- class mag4.energy.BandGap(gap_eV, metallic, vbm_eV=None, cbm_eV=None, source='', note='')[source]
Bases:
objectBand gap of one configuration (eV).
gap_eVis the conduction–valence separation (0.0 when metallic);metallicisTruewhen occupied and empty states overlap (no gap),Nonewhen the file carried no usable eigenvalue information.- Parameters:
- mag4.energy.extract_vasp_gap(outcar_path, *, occ_tol=0.5, gap_tol=0.05)[source]
Band gap from the eigenvalue/occupation block at the end of a VASP OUTCAR.
Reads the last eigenvalue dump (after the final
E-fermiline, all k-points and spin channels), splits bands into occupied (occ >occ_tol) and empty, and reportsCBM − VBM. Metallic when they overlap.
- mag4.energy.extract_wien2k_gap(scf_path, *, occ_tol=0.5, gap_tol=0.05)[source]
Band gap from a WIEN2k SCF file.
Prefers the
:GAPline (printed when lapw2 finds a gap). Otherwise — e.g. with temperature smearing — uses theBandranges/:BANnnnnnblock: VBM = highestemaxof an occupied band (occ >occ_tol), CBM = lowesteminof an empty band; energies are in Ry and converted to eV.