Skip to content

Refactor: split energy database model#302

Merged
martin-schlipf merged 5 commits into
masterfrom
refactor/split-energy-database-model
Jul 14, 2026
Merged

Refactor: split energy database model#302
martin-schlipf merged 5 commits into
masterfrom
refactor/split-energy-database-model

Conversation

@martin-schlipf

Copy link
Copy Markdown
Collaborator

VASP writes energy data in three formats, distinguished only by which labels the raw data carries (there is no explicit type field): relaxation, MD, and AFQMC. Previously Calculation._to_database() squeezed all three into a single flat Energy_DB, so for any given calculation most of its ~60 fields were None - obscuring which format a record came from.

This PR makes to_database() detect the format from the labels and return a dedicated, fully-populated model for each. The aggregates each model keeps match the physics of the run:

Format Model Aggregates per term
Relaxation (converges to a minimum) EnergyRelaxation_DB initial / min / step_min / final
MD (fluctuates around a mean) EnergyMD_DB initial / average / final
AFQMC (fluctuates while sampling) EnergyAfqmc_DB initial / average / final (+ step initial/final)

A minimum is meaningful only for a converging relaxation; MD/AFQMC use the average instead. The old catch-all other_energy_data field is dropped - every model is now a fixed, deterministic set of fields.

What changed (commit by commit)

  1. AFQMC - add EnergyAfqmc_DB, the label-based _detect_energy_format helper, and route AFQMC data to it.
  2. MD - add EnergyMD_DB and route MD data to it.
  3. Relaxation - add EnergyRelaxation_DB, route relaxation data to it, and remove the now-unreachable legacy _to_legacy_database path.
  4. Cleanup - remove the obsolete flat Energy_DB class. The docs/schema introspection in _util/database.py assumed one *_DB class per quantity, which can't express energy's three models, so it now maps the selections explicitly (energy -> EnergyRelaxation_DB as the representative default, energy:afqmc -> EnergyAfqmc_DB) and injects all three models so their fields still appear in generated docs.

Detection

Format detection keys only on labels unique to a format, checking AFQMC's markers before the shared free energy TOTEN label is ever consulted - so the one cross-format label overlap (relaxation vs. AFQMC) can't cause misclassification. An unrecognized label set raises rather than silently falling through.

Energy can be written to file in three formats (relaxation, MD, AFQMC).
Until now a single flat Energy_DB modeled all of them, leaving most fields
unset for any given record. This introduces the first of three
format-specific models.

EnergyHandler.to_database now infers the format from the labels present in
the raw data and, for AFQMC, returns a dedicated EnergyAfqmc_DB. AFQMC
energies fluctuate rather than converge, so each term is summarized by its
initial, average, and final value instead of a minimum; the sampling step
keeps only initial/final. MD and relaxation are unchanged and still return
Energy_DB for now.
The energy to-database method now detects molecular-dynamics data from its
labels and returns a dedicated EnergyMD_DB instead of the flat, mostly-empty
Energy_DB. Because MD energies fluctuate around a value rather than converging
to a minimum, each term is summarized by its initial, average, and final value
(dropping the min/step_min aggregates that are only meaningful for relaxation).

Relaxation energy still falls through to the legacy Energy_DB; its dedicated
model and the eventual Energy_DB removal are follow-up cycles.
The energy to-database method now detects ionic-relaxation data from its labels
and returns a dedicated EnergyRelaxation_DB. Because a relaxation converges toward
a minimum, each term keeps the initial, minimum, step-of-minimum, and final value.

Relaxation was the last format still routed through the flat Energy_DB, so the
now-unreachable _to_legacy_database helper (including its other_energy_data
catch-all) is removed. All three formats now return a format-specific model. The
Energy_DB class itself and the docs-introspection update remain a follow-up cleanup.
All three energy formats now return a dedicated model (EnergyRelaxation_DB,
EnergyMD_DB, EnergyAfqmc_DB), so the flat union Energy_DB -- whose fields were
mostly None for any single record -- is no longer produced anywhere and is removed.

The docs/schema introspection in _util/database.py auto-discovers one *_DB class
per quantity, which cannot express the energy quantity mapping to three models.
It now maps the selections explicitly (energy -> EnergyRelaxation_DB as the
representative default, energy:afqmc -> EnergyAfqmc_DB) and injects all three
energy models so their fields still appear in the generated documentation.

Also adds a test that the label-based format detection raises on unknown labels
instead of silently falling through.
Comment thread src/py4vasp/_raw/data_db.py Fixed
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
@martin-schlipf martin-schlipf enabled auto-merge (squash) July 14, 2026 06:52
@martin-schlipf martin-schlipf merged commit 48cdd20 into master Jul 14, 2026
29 checks passed
@martin-schlipf martin-schlipf deleted the refactor/split-energy-database-model branch July 14, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant