Small optimization

This commit is contained in:
Nuwan Yapa 2025-01-14 12:25:56 -05:00
parent 42a63d6957
commit e5346e1ef4
1 changed files with 10 additions and 8 deletions

8
EC.jl
View File

@ -66,7 +66,8 @@ end
"Extrapolate using a trained EC model for a given range of c values "Extrapolate using a trained EC model for a given range of c values
If a list is provided for ref_eval, they are used as reference values for picking the closest eigenvalues at each point. If a list is provided for ref_eval, they are used as reference values for picking the closest eigenvalues at each point.
If a single number is provided for ref_eval, it is used as a reference for the first point, and the previous eigenvalue is used as the reference for each successive point." If a single number is provided for ref_eval, it is used as a reference for the first point, and the previous eigenvalue is used as the reference for each successive point.
If precalculated_exact_E is provided, ref_eval is ignored."
function extrapolate!(EC::affine_EC, c_vals; ref_eval=EC.training_E[end], verbose=true, tol=1e-5, precalculated_exact_E=nothing) function extrapolate!(EC::affine_EC, c_vals; ref_eval=EC.training_E[end], verbose=true, tol=1e-5, precalculated_exact_E=nothing)
@assert EC.trained "EC model must be trained using train() before extrapolation" @assert EC.trained "EC model must be trained using train() before extrapolation"
@ -74,6 +75,8 @@ function extrapolate!(EC::affine_EC, c_vals; ref_eval=EC.training_E[end], verbos
verbose && println("Extact solution for c = $c") verbose && println("Extact solution for c = $c")
global current_E global current_E
if isnothing(precalculated_exact_E)
if ref_eval isa Number if ref_eval isa Number
current_E = ref_eval current_E = ref_eval
ref_eval = nothing ref_eval = nothing
@ -81,8 +84,8 @@ function extrapolate!(EC::affine_EC, c_vals; ref_eval=EC.training_E[end], verbos
current_E = popfirst!(ref_eval) current_E = popfirst!(ref_eval)
end end
if isnothing(precalculated_exact_E)
verbose && println("Extact solution for c = $c") verbose && println("Extact solution for c = $c")
H = EC.H0 + c .* EC.H1 H = EC.H0 + c .* EC.H1
evals, _ = eigs(H, sigma=current_E, maxiter=5000, tol=tol, ritzvec=false, check=1) evals, _ = eigs(H, sigma=current_E, maxiter=5000, tol=tol, ritzvec=false, check=1)
current_E = nearest(evals, current_E) current_E = nearest(evals, current_E)
@ -95,7 +98,6 @@ function extrapolate!(EC::affine_EC, c_vals; ref_eval=EC.training_E[end], verbos
verbose && println("Extrapolating for c = $c") verbose && println("Extrapolating for c = $c")
H_EC = EC.H0_EC + c .* EC.H1_EC H_EC = EC.H0_EC + c .* EC.H1_EC
evals = isnothing(EC.N_EC) ? eigvals(H_EC) : eigvals(H_EC, EC.N_EC) evals = isnothing(EC.N_EC) ? eigvals(H_EC) : eigvals(H_EC, EC.N_EC)
push!(EC.extrapolated_E, nearest(evals, current_E)) push!(EC.extrapolated_E, nearest(evals, current_E))
end end