Skip to content

Commit

Permalink
hamiltonian locally
Browse files Browse the repository at this point in the history
  • Loading branch information
epolack committed Apr 21, 2023
1 parent e309215 commit b5f4224
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
8 changes: 3 additions & 5 deletions src/scf/newton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ end
# Projections on the space tangent to ψ
function proj_tangent_kpt!(δψk, ψk)
# δψk = δψk - ψk * (ψk'δψk)
δψk_flat = reshape(δψk, :, size(δψk)[end])
mul!(δψk_flat, ψk[1, :, :], ψk[1, :, :]'δψk_flat, -1, 1)
mul!(δψk, ψk, ψk'δψk, -1, 1)
end
proj_tangent_kpt(δψk, ψk) = proj_tangent_kpt!(deepcopy(δψk), ψk)

Expand All @@ -84,7 +83,6 @@ function newton(basis::PlaneWaveBasis{T}, ψ0;
tol=1e-6, tol_cg=tol / 100, maxiter=20,
callback=ScfDefaultCallback(),
is_converged=ScfConvergenceDensity(tol)) where {T}
@assert basis.model.n_components == 1

# setting parameters
model = basis.model
Expand All @@ -95,11 +93,11 @@ function newton(basis::PlaneWaveBasis{T}, ψ0;
filled_occ = filled_occupation(model)
n_spin = model.n_spin_components
n_bands = div(model.n_electrons, n_spin * filled_occ, RoundUp)
@assert n_bands == size(ψ0[1], 3)
@assert n_bands == size(ψ0[1], 2)

# number of kpoints and occupation
Nk = length(basis.kpoints)
occupation = [filled_occ * ones(T, n_bands) for _ in 1:Nk]
occupation = [filled_occ * ones(T, n_bands) for ik = 1:Nk]

# iterators
n_iter = 0
Expand Down
10 changes: 4 additions & 6 deletions src/terms/Hamiltonian.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ end

if have_divAgrad
@timeit to "divAgrad" begin
for σ in 1:size(Hψ, 1)
apply!((fourier=Hψ[σ, :, iband], real=nothing),
H.divAgrad_op,
(fourier=ψ[σ, :, iband], real=nothing),
ψ_real[σ, :, :, :]) # ψ_real used as scratch
end
apply!((fourier=Hψ[:, :, iband], real=nothing),
H.divAgrad_op,
(fourier=ψ[:, :, iband], real=nothing),
ψ_real) # ψ_real used as scratch
end
end

Expand Down
7 changes: 4 additions & 3 deletions src/terms/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ struct DivAgradOperator{T <: Real, AT} <: RealFourierOperator
A::AT
end
function apply!(Hψ, op::DivAgradOperator, ψ,
ψ_scratch=zeros(complex(eltype(op.basis)), op.basis.fft_size...))
ψ_scratch=zeros(complex(eltype(op.basis)), op.basis.model.n_components, op.basis.fft_size...))
@assert op.basis.model.n_components == 1
# TODO: Performance improvements: Unscaled plans, avoid remaining allocations
# (which are only on the small k-point-specific Fourier grid
G_plus_k = [[Gk[α] for Gk in Gplusk_vectors_cart(op.basis, op.kpoint)] for α in 1:3]
for α = 1:3
∂αψ_real = ifft!(ψ_scratch, op.basis, op.kpoint, im .* G_plus_k[α] .* ψ.fourier)
∂αψ_real = ifft!(ψ_scratch[1, :, :, :], op.basis, op.kpoint, im .* G_plus_k[α] .* ψ.fourier[1, :])
A∇ψ = fft(op.basis, op.kpoint, ∂αψ_real .* op.A)
.fourier .-= im .* G_plus_k[α] .* A∇ψ ./ 2
.fourier[1, :, :] .-= im .* G_plus_k[α] .* A∇ψ ./ 2

end
end
Expand Down

0 comments on commit b5f4224

Please sign in to comment.