Skip to content

Fix Float32 precision upcast in lda_c_pw under AD - #33

Merged
mfherbst merged 1 commit into
masterfrom
fix-lda-c-pw-float32-dual
Jul 16, 2026
Merged

Fix Float32 precision upcast in lda_c_pw under AD#33
mfherbst merged 1 commit into
masterfrom
fix-lda-c-pw-float32-dual

Conversation

@antoine-levitt

Copy link
Copy Markdown
Member

This is actually an upstream ForwardDiff bug, but might as well fix it here too.

In `energy_per_particle_c_pw` the two `(1 ± ζ)` bases of equation (9) are
already typed `T`, but the remaining base was the integer literal `2`. With a
`Dual` exponent an integer base is differentiated via `log(2)` in Float64, so a
`Dual{Tag,Float32}` density came back as `Dual{Tag,Float64}`.

Only `energy_density` was affected: `potential_terms` pins the type via
`arithmetic_type` + `similar(ρ, TT, …)`. Downstream in DFTK this made the XC
energy the only Float64-valued term in an otherwise Float32 forward-mode
evaluation, which broke Float32 stress computations.

The change is value-neutral for non-`Dual` types (`2^x === T(2)^x` bit-identical
for Float32 and Float64); for a plain `Float32`, `promote(2, ::Float32)` is
already `Float32`, which is why this only showed up under AD.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017an8Gie5b1ULeSjhep3Sjb
@mfherbst
mfherbst merged commit 2409662 into master Jul 16, 2026
5 checks passed
@mfherbst
mfherbst deleted the fix-lda-c-pw-float32-dual branch July 16, 2026 11:27
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