Skip to content
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

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

niklasschmitz
Copy link
Collaborator

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:

  • Add failing test case on ForwardDiff DFPT under uniform strain
    (i.e. scaling lattice(x::Real) = (1 + x) * lattice)
  • Fix test case for uniform strain
  • Add test case on ForwardDiff DFPT under general strain
    (i.e. dir = randn(3,3); lattice(x::Real) = lattice + x * dir)
  • Fix test case for general strain

# 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
Copy link
Member

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 !

Copy link
Collaborator Author

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.

Copy link
Member

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?

Copy link
Collaborator Author

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

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.

3 participants