Fix Float32 precision upcast in lda_c_pw under AD - #33
Merged
Conversation
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
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 is actually an upstream ForwardDiff bug, but might as well fix it here too.