Skip to content

Commit

Permalink
Change printing (#606)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael F. Herbst <[email protected]>
  • Loading branch information
antoine-levitt and mfherbst authored Feb 28, 2022
1 parent 87ab504 commit dc103ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/scf/potential_mixing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ trial_damping(damping::FixedDamping, args...) = damping.α
α_trial = trial_damping(damping)
diagtol = determine_diagtol((ρin=ρ, Vin=V, n_iter=n_iter))
info = EVρ(V; diagtol=diagtol, ψ=ψ)
Pinv_δV = mix_potential(mixing, basis, info.Vout - info.Vin; info...)
Pinv_δV = mix_potential(mixing, basis, info.Vout - info.Vin; n_iter, info...)
info = merge(info, (α=NaN, diagonalization=[info.diagonalization], ρin=ρ,
n_iter=n_iter, Pinv_δV=Pinv_δV))
ΔEdown = 0.0
Expand Down Expand Up @@ -307,7 +307,8 @@ trial_damping(damping::FixedDamping, args...) = damping.α
Vnext = info.Vin .+ α .* δV

info_next = EVρ(Vnext; ψ=guess, diagtol=diagtol)
Pinv_δV_next = mix_potential(mixing, basis, info_next.Vout - info_next.Vin; info_next...)
Pinv_δV_next = mix_potential(mixing, basis, info_next.Vout - info_next.Vin;
n_iter, info_next...)
push!(diagonalization, info_next.diagonalization)
info_next = merge(info_next, (α=α, diagonalization=diagonalization,
ρin=info.ρout, n_iter=n_iter,
Expand Down
23 changes: 15 additions & 8 deletions src/scf/scf_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function ScfPlotTrace end # implementation in src/plotting.jl
"""
Default callback function for `self_consistent_field` and `newton`, which prints a convergence table.
"""
function ScfDefaultCallback()
function ScfDefaultCallback(; show_damping=true)
prev_energy = NaN
function callback(info)
show_magn = info.basis.model.spin_polarization == :collinear
show_diag = hasproperty(info, :diagonalization)
show_damp = hasproperty(info, )
show_damp = hasproperty(info, ) && show_damping

if show_diag
# Gather MPI-distributed information
Expand All @@ -40,28 +40,35 @@ function ScfDefaultCallback()

# TODO We should really do this properly ... this is really messy
if info.n_iter == 1
E_label = haskey(info.energies, "Entropy") ? "Free energy" : "Energy"
label_magn = show_magn ? (" Magnet", " ------") : ("", "")
label_damp = show_damp ? (" α ", " ----") : ("", "")
label_diag = show_diag ? (" Diag", " ----") : ("", "")
@printf "n %-12s Eₙ-Eₙ₋₁ ρout-ρin" E_label
@printf "n Energy log10(ΔE) log10(Δρ)"
println(label_magn[1], label_damp[1], label_diag[1])
@printf "--- --------------- --------- --------"
@printf "--- --------------- --------- ---------"
println(label_magn[2], label_damp[2], label_diag[2])
end
E = isnothing(info.energies) ? Inf : info.energies.total
Δρ = norm(info.ρout - info.ρin) * sqrt(info.basis.dvol)
magn = sum(spin_density(info.ρout)) * info.basis.dvol

format_log8(e) = @sprintf "%8.2f" log10(abs(e))

Estr = (@sprintf "%+15.12f" round(E, sigdigits=13))[1:15]
ΔE = isnan(prev_energy) ? " NaN" : @sprintf "% 3.2e" E - prev_energy
if isnan(prev_energy)
ΔE = " "^9
else
sign = E < prev_energy ? " " : "+"
ΔE = sign * format_log8(E - prev_energy)
end
Δρstr = " " * format_log8(Δρ)
Mstr = show_magn ? " $((@sprintf "%6.3f" round(magn, sigdigits=4))[1:6])" : ""
diagstr = show_diag ? " $(@sprintf "% 5.1f" diagiter)" : ""

αstr = ""
show_damp && (αstr = isnan(info.α) ? " NaN" : @sprintf " % 4.2f" info.α)
show_damp && (αstr = isnan(info.α) ? " " : @sprintf " % 4.2f" info.α)

@printf "% 3d %s %s %2.2e" info.n_iter Estr ΔE Δρ
@printf "% 3d %s %s %s" info.n_iter Estr ΔE Δρstr
println(Mstr, αstr, diagstr)
prev_energy = info.energies.total

Expand Down
18 changes: 9 additions & 9 deletions src/scf/self_consistent_field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Solve the Kohn-Sham equations with a SCF algorithm, starting at ρ.
damping=0.8, # Damping parameter
mixing=LdosMixing(),
is_converged=ScfConvergenceEnergy(tol),
callback=ScfDefaultCallback(),
callback=ScfDefaultCallback(; show_damping=false),
compute_consistent_energies=true,
response=(; ) # Dummy here, only needed
# for forward-diff.
Expand Down Expand Up @@ -90,18 +90,18 @@ Solve the Kohn-Sham equations with a SCF algorithm, starting at ρ.
# Note that ρin is not the density of ψ, and the eigenvalues
# are not the self-consistent ones, which makes this energy non-variational
energies, ham = energy_hamiltonian(basis, ψ, occupation;
ρ=ρin, eigenvalues=eigenvalues, εF=εF)
ρ=ρin, eigenvalues, εF)
end

# Diagonalize `ham` to get the new state
nextstate = next_density(ham; n_bands=n_bands, ψ=ψ, eigensolver=eigensolver,
nextstate = next_density(ham; n_bands, ψ, eigensolver,
miniter=1, tol=determine_diagtol(info),
n_ep_extra=n_ep_extra)
n_ep_extra)
ψ, eigenvalues, occupation, εF, ρout = nextstate

# Update info with results gathered so far
info = (ham=ham, basis=basis, converged=converged, stage=:iterate, algorithm="SCF",
ρin=ρin, ρout=ρout, α=damping, n_iter=n_iter, n_ep_extra=n_ep_extra,
info = (; ham, basis, converged, stage=:iterate, algorithm="SCF",
ρin, ρout, α=damping, n_iter, n_ep_extra,
nextstate..., diagonalization=[nextstate.diagonalization])

# Compute the energy of the new state
Expand Down Expand Up @@ -138,9 +138,9 @@ Solve the Kohn-Sham equations with a SCF algorithm, starting at ρ.
norm_Δρ = norm(info.ρout - info.ρin) * sqrt(basis.dvol)

# Callback is run one last time with final state to allow callback to clean up
info = (ham=ham, basis=basis, energies=energies, converged=converged,
ρ=ρout, eigenvalues=eigenvalues, occupation=occupation, εF=εF,
n_iter=n_iter, n_ep_extra=n_ep_extra, ψ=ψ, diagonalization=info.diagonalization,
info = (; ham, basis, energies, converged,
ρ=ρout, α=damping, eigenvalues, occupation, εF,
n_iter, n_ep_extra, ψ, info.diagonalization,
stage=:finalize, algorithm="SCF", norm_Δρ)
callback(info)
info
Expand Down

0 comments on commit dc103ba

Please sign in to comment.