Skip to content

Commit

Permalink
Implement Logging (#2)
Browse files Browse the repository at this point in the history
* initial logging implementation

* formatting

* lift Printf compat
  • Loading branch information
VictorVanthilt authored Jan 13, 2025
1 parent f5185f1 commit d13c886
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ version = "0.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
TensorKit = "07d1fe3e-3e46-537d-9eac-e9e13d0d4cec"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
LoggingExtras = "1.1.0"
TensorKit = "0.14"

[extras]
Expand Down
1 change: 1 addition & 0 deletions src/TRGKit.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module TRGKit
using TensorKit, LinearAlgebra
using LoggingExtras, Printf

# stop criteria
include("utility/stopping.jl")
Expand Down
9 changes: 9 additions & 0 deletions src/schemes/btrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ end

# example convcrit function
btrg_convcrit(steps::Int, data) = abs(log(data[end]) * 2.0^(-steps))

function Base.show(io::IO, scheme::BTRG)
println(io, "BTRG - Bond-weighted TRG scheme")
println(io, " T: $(summary(scheme.T))")
println(io, " S1: $(summary(scheme.S1))")
println(io, " S2: $(summary(scheme.S2))")
println(io, " k: $(scheme.k)")
return nothing
end
6 changes: 6 additions & 0 deletions src/schemes/hotrg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ end

# example convcrit function
hotrg_convcrit(steps::Int, data) = abs(log(data[end]) * 2.0^(-steps))

function Base.show(io::IO, scheme::HOTRG)
println(io, "HOTRG - Higher Order TRG scheme")
println(io, " T: $(summary(scheme.T))")
return nothing
end
6 changes: 6 additions & 0 deletions src/schemes/trg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ end

# example convcrit function
trg_convcrit(steps::Int, data) = abs(log(data[end]) * 2.0^(-steps))

function Base.show(io::IO, scheme::TRG)
println(io, "TRG - Tensor Renormalization Group scheme")
println(io, " T: $(summary(scheme.T))")
return nothing
end
32 changes: 19 additions & 13 deletions src/schemes/trgscheme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ function step! end
function finalize! end

function run!(scheme::TRGScheme, trscheme::TensorKit.TruncationScheme, criterion::stopcrit;
finalize_beginning=true)
finalize_beginning=true, verbosity=1)
data = []
if finalize_beginning
@info "Finalizing beginning"
push!(data, scheme.finalize!(scheme))
end

steps = 0
crit = true
LoggingExtras.withlevel(; verbosity) do
@infov 1 "Starting simulation\n $(scheme)\n"
if finalize_beginning
push!(data, scheme.finalize!(scheme))
end

steps = 0
crit = true

t = @elapsed while crit
@infov 2 "Step $(steps + 1), data[end]: $(!isempty(data) ? data[end] : "empty")"
step!(scheme, trscheme)
push!(data, scheme.finalize!(scheme))
steps += 1
crit = criterion(steps, data)
end

while crit
@info "Step $(steps + 1), data[end]: $(!isempty(data) ? data[end] : "empty")"
step!(scheme, trscheme)
push!(data, scheme.finalize!(scheme))
steps += 1
crit = criterion(steps, data)
@infov 1 "Simulation finished\n $(stopping_info(criterion, steps, data))\n Elapsed time: $(t)s\n Iterations: $steps"
# @infov 1 "Elapsed time: $(t)s"
end
return data
end
Expand Down
17 changes: 17 additions & 0 deletions src/utility/stopping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,20 @@ Base.:&(a::stopcrit, b::stopcrit) = MultipleCrit([a, b])

# evaluate every criterion with short circuiting
(crit::MultipleCrit)(steps::Int, data) = !any(c -> !c(steps, data), crit.crits)

# information about which criterion is stopping the simulation
function stopping_info(crit::MultipleCrit, steps::Int, data)
for c in crit.crits
if !c(steps, data)
return stopping_info(c, steps, data)
end
end
end

function stopping_info(::maxiter, steps::Int, data)
return "Maximum amount of iterations reached: $(steps)"
end

function stopping_info(crit::convcrit, steps::Int, data)
return @sprintf "Convergence criterion reached: %.3e ≤ %.3e" crit.f(steps, data) crit.Δ
end

0 comments on commit d13c886

Please sign in to comment.