Repository navigation
Conversation
mohanchen
reviewed
Dec 4, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
deepks_gradto export the descriptor derivatives and contracted operators needed for gradient-based DeePKS training. A single calculation provides the underlying functional (base) and the model-corrected result (tot) at the same sampled density. The training workflow can combine these outputs from separate calculations to construct its targets.Descriptor-gradient labels
Enable the new mode with:
The mode uses eigenvalue descriptors with
nspin=1,deepks_bandgap=0, anddeepks_v_delta=0.deepks_graddefaults to false and its label-selection constraints are inactive whendeepks_out_labels=0. INPUT metadata describes the new outputs and checks these combinations.Let
ddenote the flattened descriptor vector,g = dE_model/ddthe model gradient, andBthe real-space Hamiltonian precalculation operator, with rows indexed by lattice translation and orbital pair. The model Hamiltonian correction is linear ing, andB^T Bsupplies its quadratic training term. The output is organized as follows:deepks_dm_eig.npydeepks_gevdm.npydeepks_dot_phialpha_hamilt.npygevdmgives the descriptor-space Hamiltonian projection.deepks_dot_phialpha_hamilt_tot.npydeepks_vdrpre_square.npyB^T B, with shape(nat * des_per_atom, nat * des_per_atom).deepks_gradvx.npyanddeepks_gradvx_square.npydeepks_gvepsl.npyanddeepks_gvepsl_square.npyEnergy and physical-property labels accompany these operators. With a model active, the base Hamiltonian projection is obtained by subtracting the projected model contribution from the total projection. Without a model, base and total coincide. The force and virial operators reconstruct the model contributions by contraction with
g.The Hamiltonian contractions are implemented in
deepks_vdrpre, and the force and virial Gram calculations extenddeepks_fpreanddeepks_spre. Real-space precalculation contracts projector indices into descriptor indices during assembly. The Gram calculation accumulates orbital-pair blocks directly, reducing the need to materialize the full real-space derivative tensor. The Hamiltonian projection supports real and complex matrix elements and distributed orbital ownership.Stress and strain derivatives
The projected-density strain derivative previously paired
grad_overlap[j]withr[i]. It now pairsgrad_overlap[i]withr[j], consistent with the deformation conventionr' = (I + epsilon) r: changingepsilon_ijchanges coordinater_iin proportion tor_j. This corrects the off-diagonal strain components used to constructgdmepslandgvepsl.The virial components are ordered
xx, xy, xz, yy, yz, zz. Contractinggvepslwith the model gradient produces the virial in Hartree, corresponding to0.5 * cell_volume * stress_deltafor the internal stress in Ry/Bohr^3. Overlap derivatives are allocated and built when eithercal_forceorcal_stressis enabled, including stress-only calculations. The force-correction routine also sizes its output matrix tonat x 3before accumulation.Independent central differences deform the cell at fixed AO density matrix and fixed reduced k points. At strain step
1e-5, the maximum descriptor-derivative error changes from6.18e-3to5.59e-10for the Gamma-point case and from2.04e-2to2.13e-11for the multi-k case. The resulting model-energy derivatives agree with central differences to approximately3.87e-12and6.56e-13Hartree, respectively. These results motivate the updatedgdmepsl_ref.txtandgvepsl_ref.txtfiles for both unit-test fixtures.Electronic-step and unconverged output
deepks_out_freq_elecnow requiresdeepks_out_labels>0and controls sampling for both direct and gradient labels. A value of zero disables periodic sampling; a positive value saves the first electronic step and subsequent interval-selected steps. These samples useDeePKS_Labels_Elec/and the_eNsuffix.The SCF-end caller now passes the actual convergence state to
out_deepks_labels. The output routine handles the two stages centrally:deepks_out_freq_elec. That state appears in the final output only, without an additional_eNintermediate sample._efinal, for exampledeepks_etot_efinal.npy. This snapshot is saved even when the periodic sampling frequency is zero. Its contents follow the electronic-state output path; final force/virial precalculation operators and final geometry files are produced by the converged path.Physical force and stress labels follow the ordinary force/stress postprocessing calculations, including calculations performed after an unconverged SCF. Filename selection is centralized in
get_filename.Diagnostic output and reference updates
deepks_projdm.datanddeepks_desc.datare diagnostic text dumps. They are now written by rank zero whendeepks_out_unittestis enabled. Routine descriptor output usesdeepks_dm_eig.npy.The previous integration-property collector read
deepks_desc.datas well as the NPY descriptor output. It now checks the NPY data directly with the existingget_sum_abs.pyhelper. Consequently, each of the 27 affected existingresult.reffiles drops itsdeepks_descentry. This is the reason the PR touches many integration reference files. The four strain-derivative reference files described above reflect the separate numerical correction.Four existing integration cases also require numerical reference updates:
09_NO_GO_deepks_basicand10_NO_KP_deepks_basic: updatedeepks_spre, the absolute-value sum of the descriptor strain derivatives, to reflect the correctedgrad_overlap[i] * r[j]convention.25_NO_GO_deepks_out_freq_elecand26_NO_KP_deepks_out_freq_elec: update the same strain-derivative summary and the six electronic-step summaries for energy, energy difference, Hamiltonian, Hamiltonian difference, Hamiltonian precalculation, and overlap. These cases converge at electronic step 5 with an output interval of 1. The old references summed five intermediate samples; the updated output contains steps 1–4 inDeePKS_Labels_Elec/and writes the converged state through the final-output path. The changed sums therefore exclude the formerly duplicated fifth sample.These reference entries are regenerated from the corresponding calculations. The numerical updates are limited to the affected strain-derivative and electronic-sampling summaries.
The force/stress, force-operator, strain-operator, and real-space Hamiltonian tests move to
unittests/test_deepks_{force,fpre,spre,vdrpre}.cpp. They cover Gram construction, Hamiltonian projection, model-gradient reconstruction, and the relevant units. Two Autotest cases add Gamma-point data without a model and multi-k data with a model and electronic-step sampling. Their references are generated from actual calculations, and the existing property collector checks the new NPY outputs through absolute-value sums.The unit-test migration in this PR is partial. The 16 reference files used directly by the migrated checks move to
unittests/support/with the.txtextension, preserving their contents during relocation. The standalone strain-PDM check moves intotest_deepks_spre.cpp, alongside the strain-operator checks.The migrated tests still use shared runners, input preparation, and prerequisite checks from
test/. Their numerical targets are created intest/CMakeLists.txtand receive the migrated sources throughtarget_sources()inunittests/CMakeLists.txt; the test processes also use data from both support directories. These cross-directory references are a temporary arrangement. A separate follow-up PR will separate input preparation from feature-specific assertions and consolidate the remaining test infrastructure and data inunittests/.