Skip to content

dftd3: add D3(0) zero damping as an optional last token - #1935

Merged
brucefan1983 merged 14 commits into
brucefan1983:masterfrom
mkatouda:dftd3zero-upstream
Nov 29, 2026
Merged

brucefan1983 merged 14 commits into
brucefan1983:masterfrom
mkatouda:dftd3zero-upstream

Conversation

@mkatouda

@mkatouda mkatouda commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds D3(0) zero damping to the existing dftd3 keyword:

dftd3 <functional> <potential_cutoff> <coordination_number_cutoff> [zero|bj]

Without the last token the behaviour is unchanged (bj).
Two-body terms only (no ATM three-body term), as for BJ.
As agreed in #1915 this PR contains only the D3(0) part.

Motivation: reference data computed with zero damping (for example Quantum ESPRESSO with vdw_corr = 'dft-d3', dftd3_version = 3) need the same dispersion function to be subtracted before training and added at run time.

Changes

  • src/force/dftd3/dftd3.cu, dftd3.cuh: the small-box and the large-box force kernels are templates on the damping (template <bool zero_damping>); the damping-dependent pair term is an inline helper (d3_pair), with the BJ expressions unchanged and d3_zero_pair() for zero damping. The neighbour traversal, C6 interpolation, force/virial accumulation and the coordination-number kernels are shared. The specialization is selected at kernel launch.
  • src/force/dftd3/dftd3zero_table.cuh (new): R0 pair radii and the parameters s6, sr6, s8, sr8 of 48 functionals.
  • src/force/nep/nep.cu: optional fifth token (bj or zero); DFTD3::initialize() validates the damping and the functional and reports errors with PRINT_INPUT_ERROR. nep_charge is unchanged (BJ only).
  • doc/gpumd/input_parameters/dftd3.rst.

Validation

RTX A6000 (sm_86), CUDA 12.3.103, g++ 11.4.0. The reference build is master (63fab11), the revised build is this branch; both were built with the same flags (once with -DDEBUG for the regression, once as release for the other checks). Both build without new warnings.

  • tests_regression --suite full, -DDEBUG builds of both: 254 passed, 1 failed, relations 25/25. The failing case is qnep_pppm_future_bec (a qNEP PPPM case without D3; one thermo.out field differs by 6e-11 relative; the same case also fails occasionally when master is compared with itself).
  • Energy, force and virial of the D3 term against simple-dftd3 1.7.0 (zero damping, two-body, same cutoffs), 21 structure/functional cases and 48 functionals on one H/C/O cell. Largest errors: energy 2.8e-6 eV (PBE, water, rc 25), force 4.9e-7 eV/Å, virial 1.0e-5 eV (PBE, graphite; about 2e-6 of the largest virial component). Energy and force errors for the 48 functionals are at most 4.9e-7.
  • Finite differences of the energy for forces and virial (cubic, open and triclinic cells), agreement of the small-box and large-box paths, and finite results at r = 0.05, 0.002 and 1e-4 Å: all pass for both dampings.
  • BJ output is bit-identical to master (energy, forces, virial; all-zero NEP on five structures and NEP89 on two). The BJ kernels use the same registers and stack as before.
  • Speed (release builds, NEP89, dftd3 pbe 12 6, sum of the D3 kernels per step, idle GPU, median of 3 alternated runs):
system master BJ (ms) this PR BJ (ms) this PR zero (ms) zero / BJ
648 atoms (small box) 4.032 4.035 4.296 1.07
5 184 atoms 3.809 3.800 4.474 1.18
41 472 atoms 8.048 8.038 9.192 1.14
Cu, 4 000 atoms 3.555 3.529 4.360 1.24

At 648 atoms the whole step takes 6.721 s per 1 000 steps with master BJ, 6.722 s with this PR's BJ and 6.985 s with zero damping.

Limitations

  • Two-body terms only; zero damping for Z ≤ 86; single GPU, as for BJ; not available for the nep_charge models.
  • Zero damping is slower than BJ (table above).
  • HIP is not built (no AMD GPU available).

…tional> <rc> <rc_cn> [zero|bj]`)

Two-body D3(0) with its own force kernels (small-box and large-box); the BJ
kernels are unchanged. With zero damping the small-box coordination number is
summed in double. R0 pair radii and the parameters of 48 functionals are in a
new generated header (source: Grimme DFT-D3 parameters as in dftd3-lib, commit
3c99ee981266548a37588abe3a6f5cb56092d51a, lib/core.f90), with the generator in
tools/Miscellaneous/for_coding/dftd3_zero_tables. Elements H-Rn (Z <= 86);
a functional not available for the chosen damping stops the run. Not available
for the nep_charge models.

Adds the manual entry, an example, and tests_pytest/test_dftd3_zero.py
(simple-dftd3 reference values, all 48 functionals, finite differences,
small-box vs large-box, BJ unchanged).
# Conflicts:
#	doc/gpumd/input_parameters/dftd3.rst
@brucefan1983

Copy link
Copy Markdown
Owner

Could you explain why a separate double-precision implementation is needed for the coordination number and interpolation weights only in the zero-damping small-box path, while the BJ and large-box paths remain in single precision?

Please compare the energy, force, and virial errors against the reference using the existing single-precision implementation and the new double-precision implementation, and report the performance cost. Please also explain the basis for the accuracy thresholds.

If the existing implementation provides sufficient accuracy, please reuse it. If double precision is necessary, we can discuss how to support it without duplicating the CN implementation. Local validation results in the PR discussion are sufficient; no additional test files are needed.

…t (validation results are reported in the PR discussion)
@mkatouda

Copy link
Copy Markdown
Contributor Author

Thank you for the review. Short answers first, then the numbers.

Why double precision only in the zero-damping small-box path. I added it because, with single precision, the virial of one test structure (graphite, PBE) exceeded the accuracy threshold I had set in my own fork (2e-6 relative to the largest virial component). It is the only quantity where the two versions differ noticeably. I left BJ unchanged on purpose (so that existing results do not change), and I left the large-box path in single precision because making it double precision did not improve the large-box results in my earlier tests (one structure got worse).

Single vs double CN for D3(0), small-box path, errors against simple-dftd3 (D3 term alone, all-zero NEP; ratio = single/double):

  • Energy and forces: of the same size in both versions. For all 48 functionals on an H/C/O cell the largest errors are 4.9e-7 eV (energy) and 4.9e-7 eV/Å (force) for both versions. On the 21 structure/functional cases the energy and force errors differ between the versions by at most a factor of about 1.5 (for example the force error of PBE water27_rc25 is 1.5e-7 vs 1.0e-7 eV/Å); there is no systematic trend.
  • Virial: the same except for graphite, where the cancellation between the CN-gradient term and the pair term is large:
graphite virial error (eV) single CN double CN BJ in master (single)
PBE 1.01e-5 7.5e-6 5.4e-6
BLYP 1.74e-5 1.29e-5
TPSS 1.27e-5 9.4e-6
B3LYP 1.39e-5 1.01e-5

The largest virial component of graphite is about 4.5 eV, so 1e-5 eV is about 2e-6 relative. For comparison, BJ in master has 5.4e-6 eV on the same structure, so zero damping with single precision is about 1.9 times, and with double precision about 1.4 times, the error of BJ. For the other PBE structures (water, benzene dimer, copper, up to several thousand atoms) the virial errors of both versions are the same and are 0.3 to 0.8 times the BJ errors.

Basis of the thresholds. They are not derived from an error model of D3(0). I took them from my fork, where they were set from the errors of the BJ implementation against simple-dftd3: 2e-6 relative for energy and virial (float errors scale with the size of the quantity, about 5–8e-7 of the D3 energy for unmodified BJ) and 1e-5 eV/Å for forces. In absolute terms the zero-damping force errors are 1e-8 to 5e-7 eV/Å, well below that. If you prefer a different criterion, I can apply it.

Performance. I could not measure it today because the GPU is occupied by other jobs. The previous measurement (RTX A6000, idle, nsys kernel time per step): the double-precision CN kernel costs 0.109 ms per step at 648 atoms (0.050 → 0.159 ms), about 2.7 % of the D3 time (about 4 ms); large-box systems are not affected because their CN is single precision. I will re-measure on an idle GPU and update this comment.

Options. (1) Reuse the existing single-precision CN kernel for zero damping: this removes the duplicated kernel (about 60 lines); the graphite virial error is then 1.0e-5 eV instead of 7.5e-6 eV, everything else is unchanged. (2) Keep double precision, but share the code with BJ by making the CN kernel a template whose single-precision instantiation is the current BJ kernel. I can do either; please tell me which you prefer.

Tests. I have removed the test file and its fixtures from the PR (new commit); the validation numbers are in this comment and in the PR description.

@mkatouda

Copy link
Copy Markdown
Contributor Author

Performance numbers for the double-precision CN kernel of the zero-damping small-box path, as promised above. RTX A6000, CUDA 12.3, idle GPU (checked before every batch), NEP89, dftd3 pbe 12 6 zero, NVE, kernel times from nsys, median of 3 alternated runs, first steps dropped.

system zero, single-precision CN (D3 total per step) zero, double-precision CN difference
648 atoms (small box) 4.257 ms 4.369 ms +0.111 ms (+2.6 % of the D3 time)
5 184 atoms (large box) 4.437 ms 4.436 ms none (the large-box path has no double-precision CN)
  • The CN kernel itself goes from 0.050 ms (the BJ kernel) to 0.160 ms at 648 atoms (about 3.2 times); neighbour list (2.74 ms), force kernel (1.37 ms) and the other kernels are the same in both versions.
  • Whole MD run at 648 atoms (NEP89 + D3 zero, 1 000 steps, wall time of the process including start-up, 3 alternated runs): 6.91 s with single precision, 7.05 s with double precision, i.e. +0.14 ms per step, about 2 % of the whole step.
  • For reference, BJ takes 4.00 ms (648 atoms) and 3.76 ms (5 184 atoms) for the D3 kernels, so zero damping with double-precision CN is 1.09 and 1.18 times the BJ time, respectively.

So the double-precision CN costs about 2 % of a small-box step; the accuracy difference is the graphite virial discussed above. I am happy to go either way (reuse the single-precision kernel, or keep double precision with a shared template).

@brucefan1983

brucefan1983 commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

The largest difference virial is still reasonable, so I don't think it will cause problem. For simplicity, I prefer keep the single-precision kernel.

Remove the double-precision coordination-number kernel of the zero-damping
small-box path, as requested in the review: both dampings now use
find_dftd3_coordination_number_small_box. The BJ kernels are unchanged.
@mkatouda

Copy link
Copy Markdown
Contributor Author

Thank you. I removed the double-precision CN kernel (commit 19c30f6): zero damping now uses the existing single-precision CN kernel of the small-box path, as BJ does. The BJ kernels are untouched. I checked that the energy, forces and virial are identical (within the run-to-run noise of the atomic adds) to the single-precision numbers I reported above on graphite, water and the benzene dimer, and that BJ output is still identical to master (all-zero NEP, two test structures). The PR description is updated accordingly.

Two points from the description are still open, if you have a moment: (1) whether the table values taken from dftd3-lib ("GPL version 1 or later", numbers only) are acceptable given the planned LGPL move of src/, and (2) whether you prefer the new constants in dftd3zero_table.cuh or appended to dftd3para.cuh.

@brucefan1983

Copy link
Copy Markdown
Owner

Could you confirm that the new D3(0) implementation was independently written for GPUMD, apart from reuse of existing GPUMD code, rather than copied or adapted from another program? This question concerns the implementation code, not the numerical parameters, which are expected to agree across implementations. Please cite the original method and parameter publications. We do not want to introduce external software dependencies or require GPUMD users to cite other DFT-D3 programs.

@mkatouda

mkatouda commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes. The zero-damping code was written for GPUMD, on top of the existing dftd3.cu: it reuses GPUMD's neighbour lists, coordination-number kernels, C6 interpolation and force/virial accumulation, and adds its own pair kernels (including an overflow-safe evaluation of the damping function for very small r, which I believe is not how other programs do it) and the keyword parser. I did not copy or adapt code from dftd3-lib, simple-dftd3 or any other DFT-D3 program, and nothing is linked or called from them at build or run time.

What does come from elsewhere:

  1. Method and formulas (published): E = −Σ s6·C6/r⁶·f6 − Σ s8·C8/r⁸·f8 with the zero-damping function f_n = 1 / (1 + 6 (r / (sr_n·R0))^(−α_n)), α6 = 14, α8 = 16, C8 from the r2r4 expectation values already in dftd3para.cuh. Reference: S. Grimme, J. Antony, S. Ehrlich, H. Krieg, J. Chem. Phys. 132, 154104 (2010), which also defines the C6 references and the R0 pair radii.
  2. Numerical data only: the R0 pair radii (4 465 values) and the functional parameters (s6, sr6, s8, sr8) of the 48 functionals are numbers of the DFT-D3 reference parameter set. I extracted them with a script from the dftd3-lib distribution (the script and the checksum of the source file are in tools/Miscellaneous/for_coding/dftd3_zero_tables/) and checked them entry by entry against a second implementation. I cite the 2010 paper above as the reference for the method and the parameter set.
  3. Checks: dftd3-lib and simple-dftd3 were used as numerical references for validation (energies, forces, virials), and I consulted dftd3-lib to confirm conventions (units, α8 = α6 + 2, parameter naming); no code was taken from them.

For transparency: I used an AI coding assistant (Claude Code) to develop this code, and I reviewed the result.

So that users are not asked to cite other programs, the documentation now refers to the method paper only ([Grimme2010]) for the parameters (commit cb5c2e5); the provenance of the numbers stays in the header of the generated file and in the PR description.

@brucefan1983

Copy link
Copy Markdown
Owner

Thank you for the clarification. Please make further revision with the following changes:

  1. Reduce duplication in the force kernels. In dftd3.cu, combine each BJ/zero pair into one template <bool zero_damping> kernel: one for the small-box path and one for the large-box path. Share the neighbor traversal, C6 interpolation, and energy, force, virial, and CN-derivative accumulation. Isolate the damping-dependent pair calculation in an inline helper, retaining d3_zero_pair() for zero damping and preserving the existing BJ arithmetic. Select the template specialization at kernel launch.

  2. Simplify input parsing. In nep.cu, use a single argument-count check, tokens.size() != 4 && tokens.size() != 5, then select the damping with tokens.size() == 5 ? tokens[4] : "bj" and pass it to DFTD3::initialize(). Validate bj or zero inside DFTD3::initialize(), before setting the damping mode. Use concise error messages through PRINT_INPUT_ERROR.

  3. Keep the runtime parameter table and remove the generation files. Retain dftd3zero_table.cuh as a separate header, with Michio Katouda: initial implementation in its contributors section. Keep table comments limited to physical meaning, units, and index layout; remove the source and generation descriptions. Remove the entire tools/Miscellaneous/for_coding/dftd3_zero_tables/ directory.

  4. Keep the usage documentation compact. Remove examples/gpumd_dftd3_zero/, its entry in examples/README.md, and its reference in the manual. The existing inline usage examples are sufficient. Keep the supported options and actual limitations, but remove development details about conflicting parameter tables and when element checks occur. Also correct the statement that functional names must be lowercase, since the implementation already converts them to lowercase.

  5. Validate the changes and update the PR description. Run the full regression suite using src_old as the reference and src as the revised version, with both built using the same compiler and flags, including -DDEBUG. Also recheck energies, forces, and virials for both damping choices in both box paths, including the existing small-distance checks for zero damping. Use matched release builds for performance comparisons. Report the full regression summary and D3 validation results in the PR discussion. Keep the PR description focused on the final changes, validation, and remaining limitations, and remove resolved questions and intermediate development details.

@mkatouda

Copy link
Copy Markdown
Contributor Author

The revision is pushed (head b673a278): the BJ and zero-damping force kernels are now one template <bool zero_damping> kernel per box path with the damping-dependent pair term in an inline helper (BJ expressions unchanged, d3_zero_pair() for zero); parsing in nep.cu uses one argument-count check and DFTD3::initialize() validates the damping and the functional with PRINT_INPUT_ERROR; the table generation files and the example are removed; the manual is shortened (functional names are case-insensitive). The PR description is updated.

Validation (RTX A6000, CUDA 12.3.103, g++ 11.4.0; reference = master 63fab11, revised = this branch, same flags; -DDEBUG builds for the regression, release builds otherwise):

  • Full regression (--suite full, both built with -DDEBUG): 254 passed, 1 failed, relations 25/25. The failing case is qnep_pppm_future_bec (qNEP PPPM, no D3; one thermo.out field differs by 6e-11 relative). It fluctuates also when master is compared with itself (2 failures in 6 runs earlier), and in this session it failed in 1 of 3 reruns against the revised build and 0 of 3 against master itself.
  • D3 against simple-dftd3 1.7.0 (zero damping, two-body, same cutoffs; D3 term alone with an all-zero NEP): 21 structure/functional cases, largest errors energy 2.8e-6 eV (PBE water, rc 25), force 4.9e-7 eV/Å, virial 1.0e-5 eV (PBE graphite, ≈2e-6 of the largest virial component, as discussed above); 48 functionals on an H/C/O cell: at most 4.9e-7 (energy and force).
  • Finite differences of the energy for forces and virial, small-box vs large-box agreement, and finite results at r = 0.05, 0.002 and 1e-4 Å: pass for both dampings (cubic, open and triclinic cells).
  • BJ output is bit-identical to master (energy, forces, virial, five structures with an all-zero NEP, two with NEP89); register count and stack of the BJ kernels are unchanged.
  • Speed, matched release builds, idle GPU, D3 kernel time per step (median of 3 alternated runs):
system master BJ this PR BJ this PR zero zero / BJ
648 atoms 4.032 ms 4.035 ms 4.296 ms 1.07
5 184 atoms 3.809 ms 3.800 ms 4.474 ms 1.18
41 472 atoms 8.048 ms 8.038 ms 9.192 ms 1.14
Cu 4 000 atoms 3.555 ms 3.529 ms 4.360 ms 1.24

At 648 atoms, 1 000 steps take 6.721 s (master BJ), 6.722 s (BJ here) and 6.985 s (zero).

The numerical checks above come from a local test file that is not part of the PR.

@brucefan1983

Copy link
Copy Markdown
Owner

Could you contact me via email: brucenju@gmail.com
We plan to release GPUMD-v6.0 and write the release paper.

@brucefan1983

Copy link
Copy Markdown
Owner
  1. Move elements_checked and check_elements() into the private section of DFTD3. Expose only interfaces that are actually needed outside the class.

  2. Put the internal symbols in dftd3zero_table.cuh, including the parameter arrays and D3ZeroParam, inside an anonymous namespace, consistent with dftd3para.cuh.

  3. Reduce comments throughout the added code. For example, remove the explanatory block above d3_zero_term(). Also remove comments that repeat the code or describe implementation history, such as “skips the BJ table below” and “the printout of BJ damping is unchanged”. Prefer clear code over explanatory comments. Retain only essential notes on physical meaning, units, and index layout, together with the standard file header and contributor credits.

…ble in an anonymous namespace, reduce comments
@mkatouda

Copy link
Copy Markdown
Contributor Author

Thank you for the review. All three points are done in the latest commit (dca208c3):

  1. elements_checked and check_elements() are now in the private section of DFTD3.
  2. The contents of dftd3zero_table.cuh (the R0 table, D3ZeroParam and the parameter array) are inside an anonymous namespace, as in dftd3para.cuh.
  3. The explanatory comments are removed, including the block above d3_zero_term() and the comments on the BJ table and printout. What remains is the header with the contributor credits, and short notes on units and index layout (the R0 table, the units of the pair arguments).

No behavior is changed. I rebuilt (sm_86, CUDA 12.3) and re-ran the D3 tests: the results are the same as before this commit.

Regarding your request to contact you by email: I will send you an email separately.

@brucefan1983
brucefan1983 merged commit a0555f0 into brucefan1983:master Nov 29, 2026
3 checks passed
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.

2 participants