mag4.extract
J-value extraction from a completed four-state DFT campaign.
Pipeline (mirrors what the user did manually before this module existed):
Parse the
four_state_report.datwritten bymag4.cli.fourstate.main()to recover, for each shellJₙ, which 4 unique configurations contribute to the formulaJS² = (E_uu + E_dd − E_ud − E_du) / 4.Read each configuration’s converged DFT total energy from
configN/viamag4.energy.get_energy().Combine energies with the formulas to produce
JS²(andJif a spin S is given).
This module is the pure-Python core; the CLI in mag4.cli.extract
adds argument parsing, pretty-printing and j_values.dat output.
Module Attributes
Name of the report file produced by |
Functions
|
Apply each formula to the matching energies and return the J table. |
|
Read per-config |
|
|
|
Extract every |
|
Read the supercell multipliers |
|
Read the total atom count in the supercell from a four_state_report.dat header. |
|
|
|
Walk |
|
For every |
|
Sum the |
|
Persist the four-state results to a fixed-width text file. |
Classes
|
One row of the EXTRACTION FORMULAS section. |
|
One row of the final J table emitted by |
- mag4.extract.DEFAULT_REPORT_NAME = 'four_state_report.dat'
Name of the report file produced by
mag4-magnetic(default).
- class mag4.extract.Formula(name, distance_A, config_uu=None, config_dd=None, config_ud=None, config_du=None, incomplete=False, divisor=4, raw='')[source]
Bases:
objectOne row of the EXTRACTION FORMULAS section.
- Parameters:
- config_uu, config_dd, config_ud, config_du
Unique-configuration indices (1-based, matching the
configN/directory names) whose energies are combined as(E_uu + E_dd − E_ud − E_du) / 4.- Type:
- incomplete
True when the report flagged this shell as INCOMPLETE (cell too small to give a distinct dimer). In that case the four config indices are unset and J cannot be computed.
- Type:
- mag4.extract.parse_report_formulas(report_path)[source]
Extract every
J … = ( E[#…] + …) / 4line from a report file.- Parameters:
report_path (str) – Path to a
four_state_report.datproduced bymag4-magnetic.- Returns:
One entry per coupling shell discovered in the report, in the order they appear. Shells flagged INCOMPLETE in the report are included with
incomplete=True(so the caller can warn about them) but with no config indices.- Return type:
list of
Formula- Raises:
FileNotFoundError – If
report_pathdoesn’t exist.ValueError – If the file exists but contains no recognisable formula or INCOMPLETE line — most likely the wrong file was passed.
- mag4.extract.parse_report_config_cells(report_path)[source]
Read per-config
n_atomsvalues from a four_state_report.dat.The report emits a
Cell: a=… | … | n_atoms=N | …line under eachConfig #uid:block when WIEN2k cell reduction is on. Returns{config_index → n_atoms}. Older reports without the line yield an empty dict and the caller falls back to assuming every config sits in the same cell (today’s behaviour, no rescaling).
- mag4.extract.read_wien2k_struct_atom_count(struct_path)[source]
Sum the
MULT=Nfields across a WIEN2kcase.structfile.This is the authoritative atom count WIEN2k’s
:ENErefers to. Wheneverinit_lapw’ssgrouprewrites the struct (cell reduction, lattice-letter correction), mag4’s pre-emption fromfour_state_report.datcan disagree – the post-sgroupfile on disk is the truth. ReturnsNoneif the file is missing or noMULT=field could be parsed.Example: a P6_3/mmc primitive cell with 2 Cr + 2 Sb listed (each MULT=2) returns 4.
- mag4.extract.read_struct_atom_counts(compound_dir, n_configs)[source]
For every
configN/directory undercompound_dir, sum theMULT=fields ofconfigN.struct(whatever WIEN2k’sinit_lapwleft on disk after possibly acceptingsgroup’s rewrite) and return{config_index → atom_count}.Missing struct files or unparseable ones are simply skipped – callers should fall back on the report’s recorded counts.
- mag4.extract.parse_report_supercell_atoms(report_path)[source]
Read the total atom count in the supercell from a four_state_report.dat header.
The four-state formula is defined on the supercell mag4 built (e.g. NiO 1×1×2 → 16 atoms). When configs are computed in reduced cells (Fm-3m conv → 2 atoms, etc.), their WIEN2k :ENE values must be scaled up to the supercell size before applying the formula. This helper returns the integer that
mag4-extractshould use as the scaling target.Returns
Nonefor reports written by mag4 versions before theTotal atoms in supercellline was added; the caller then falls back tomath.lcm(per-config n_atoms).
- mag4.extract.parse_report_supercell(report_path)[source]
Read the supercell multipliers
(na, nb, nc)from a four-state report header lineSupercell : na×nb×nc.Noneif absent.
- mag4.extract.parse_report_config_sz(report_path)[source]
{config_index: Sz}from the per-configSpins:lines, whereSz = Σ s_iof the ±1 spin vector (the net collinear moment quantum number of that configuration).
- mag4.extract.parse_report_tr_pairs(report_path)[source]
[(configA, configB), …]from the TIME-REVERSAL DEGENERACY CHECK section written bymag4-magnetic --check-degeneracy(any method).Each pair is related only by an antiunitary operation (unitary crystal op × time reversal), so the two DFT energies must coincide; the caller reports
|ΔE|per pair. Empty when the report has no such section.
- class mag4.extract.JResult(name, distance_A, formula, energies_eV=<factory>, JS2_eV=None, JS2_meV=None, J_meV=None, J_K=None, error=None)[source]
Bases:
objectOne row of the final J table emitted by
compute_j_values().- Parameters:
- energies_eV
Per-state total energies in eV. Keys:
"uu","dd","ud","du".Nonewhen the corresponding config did not converge (or didn’t exist).
- JS2_eV, JS2_meV
Computed
JS² = (E_uu + E_dd − E_ud − E_du) / 4.Noneif the formula could not be evaluated (any energy missing).- Type:
float or None
- J_meV, J_K
Computed
J = JS² / S²in meV and Kelvin (J / k_B). Only populated when a positive spinSwas passed.- Type:
float or None
- mag4.extract.compute_j_values(formulas, energies, spin=None, config_n_atoms=None, n_supercell_atoms=None)[source]
Apply each formula to the matching energies and return the J table.
- Parameters:
formulas (list of
Formula) – Output ofparse_report_formulas().energies (dict[int, EnergyResult]) – Map
config_index → EnergyResultfor every config the formulas reference. Indices come from theconfigN/dir name, 1-based. Configs that didn’t converge MUST still be present withEnergyResult.converged = Falseso this function can attribute the failure correctly.spin (float, optional) – Magnetic spin quantum number
S. When given (>0), eachJS²is divided byS²to also reportJin meV and in Kelvin.config_n_atoms (dict[int, int], optional) – Map
config_index → n_atomsfromparse_report_config_cells(). When supplied, each contributing config’s total energy is rescaled byN_LCM / N_Xbefore applying the four-state formula — necessary whenmag4-magneticwrote each config in its own primitive cell (--reduce-cell, the default). When omitted every config is assumed to sit in the same cell (legacy behaviour, no rescaling).n_supercell_atoms (int | None)
- Returns:
One entry per input formula, same order.
- Return type:
list of
JResult
- mag4.extract.read_all_energies(compound_dir, code, max_index=None, scf_basename=None)[source]
Walk
<compound_dir>/configN/and run the energy parser on each.- Parameters:
compound_dir (str) – Root directory containing the
configN/subdirectories.code ({'vasp', 'wien2k'}) – Forwarded to
mag4.energy.get_energy().max_index (int, optional) – If given, only configs
config1…configNare read. Useful to short-circuit on very large compound dirs.scf_basename (str | None)
- Returns:
Map
config_index → EnergyResult. Empty if noconfigN/directory was found.- Return type:
- mag4.extract.write_j_values_dat(results, path, compound_dir, code, spin=None, supercell=None, energies=None, config_sz=None, mags=None)[source]
Persist the four-state results to a fixed-width text file.
Beyond the J table this records the supercell used and a per-configuration block (total energy, SCF iterations, converged moment vs the configuration’s
Sz) so the file is self-contained — everything shown on screen, plus the moment/Sz consistency check.# mag4-extract — four-state J values # compound: <dir> code: vasp spin: 3.5 # supercell: 1x1x2 # # Per-configuration energies # config iter energy (eV) Sz mag exp|mag| status # … # # name d_mean(A) JS2(eV) JS2(meV) J(meV) J(K) status J1 3.8987 -0.12345678 -123.456789 -10.082 -116.99 OK