-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Niklas Schmitz <[email protected]> Co-authored-by: Michael F. Herbst <[email protected]>
- Loading branch information
1 parent
26df5ee
commit acb79f9
Showing
10 changed files
with
211 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# This uses the `compute_forces(term, ψ, occ; kwargs...)` function defined by all terms | ||
""" | ||
Compute the forces of an obtained SCF solution. Returns the forces wrt. the fractional | ||
lattice vectors. To get cartesian forces use [`compute_forces_cart`](@ref). | ||
Returns a list of lists of forces | ||
`[[force for atom in positions] for (element, positions) in atoms]` | ||
which has the same structure as the `atoms` object passed to the underlying [`Model`](@ref). | ||
""" | ||
@timing function compute_forces(basis::PlaneWaveBasis, ψ, occ; kwargs...) | ||
# TODO optimize allocs here | ||
T = eltype(basis) | ||
forces = [zeros(Vec3{T}, length(positions)) for (element, positions) in basis.model.atoms] | ||
for term in basis.terms | ||
f_term = compute_forces(term, ψ, occ; kwargs...) | ||
if !isnothing(f_term) | ||
forces += f_term | ||
end | ||
end | ||
forces | ||
end | ||
|
||
""" | ||
Compute the cartesian forces of an obtained SCF solution in Hartree / Bohr. | ||
Returns a list of lists of forces | ||
`[[force for atom in positions] for (element, positions) in atoms]` | ||
which has the same structure as the `atoms` object passed to the underlying [`Model`](@ref). | ||
""" | ||
function compute_forces_cart(basis::PlaneWaveBasis, ψ, occ; kwargs...) | ||
lattice = basis.model.lattice | ||
forces = compute_forces(basis::PlaneWaveBasis, ψ, occ; kwargs...) | ||
[[lattice \ f for f in forces_for_element] for forces_for_element in forces] | ||
end | ||
|
||
function compute_forces(scfres) | ||
compute_forces(scfres.basis, scfres.ψ, scfres.occupation; ρ=scfres.ρ) | ||
end | ||
function compute_forces_cart(scfres) | ||
compute_forces_cart(scfres.basis, scfres.ψ, scfres.occupation; ρ=scfres.ρ) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using ForwardDiff | ||
""" | ||
Compute the stresses (= 1/Vol dE/d(M*lattice), taken at M=I) of an obtained SCF solution. | ||
""" | ||
@timing function compute_stresses(scfres) | ||
# TODO optimize by only computing derivatives wrt 6 independent parameters | ||
scfres = unfold_bz(scfres) | ||
# compute the Hellmann-Feynman energy (with fixed ψ/occ/ρ) | ||
function HF_energy(lattice) | ||
T = eltype(lattice) | ||
basis = scfres.basis | ||
model = basis.model | ||
new_model = Model(lattice; | ||
model.n_electrons, | ||
model.atoms, | ||
magnetic_moments=[], # not used because we give symmetries explicitly | ||
terms=model.term_types, | ||
model.temperature, | ||
model.smearing, | ||
model.spin_polarization, | ||
model.symmetries) | ||
new_basis = PlaneWaveBasis(new_model, | ||
basis.Ecut, basis.fft_size, basis.variational, | ||
basis.kcoords_global, basis.ksymops_global, | ||
basis.kgrid, basis.kshift, basis.symmetries, | ||
basis.comm_kpts) | ||
ρ = DFTK.compute_density(new_basis, scfres.ψ, scfres.occupation) | ||
energies, _ = energy_hamiltonian(new_basis, scfres.ψ, scfres.occupation; | ||
ρ, scfres.eigenvalues, scfres.εF) | ||
energies.total | ||
end | ||
L = scfres.basis.model.lattice | ||
Ω = scfres.basis.model.unit_cell_volume | ||
ForwardDiff.gradient(M -> HF_energy((I+M) * L), zero(L)) / Ω | ||
end |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.