-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ForwardDiff for lattice strain DFPT response #1054
base: master
Are you sure you want to change the base?
Conversation
src/terms/Hamiltonian.jl
Outdated
# need `deepcopy` here to copy the elements of the array of arrays ψ (not just pointers) | ||
Base.:*(H::Hamiltonian, ψ) = mul!(deepcopy(ψ), H, ψ) | ||
function Base.:*(H::Hamiltonian, ψ) | ||
result = ψ * one(eltype(H.basis)) # Includes type promotion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the same thing as a deepcopy, be careful !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is allocating new memory recursively since the multiplication-by-scalar is broadcasted automatically for abstract arrays. I now expanded the comment to clarify the intent of allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my edification, what was wrong with the deepcopy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The deepcopy was missing a type promotion to be able to hold the result, for the case when the Hamiltonian contains Duals but psi is Float64
I'm opening this as an ongoing draft to fix ForwardDiff derivatives for strain (lattice distortion), which were not giving correct results yet.
TODO list:
(i.e. scaling
lattice(x::Real) = (1 + x) * lattice
)(i.e.
dir = randn(3,3); lattice(x::Real) = lattice + x * dir
)