From d13c886e771d8226735d6f212ad92a429c3b0750 Mon Sep 17 00:00:00 2001 From: Victor Vanthilt <73738005+VictorVanthilt@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:30:54 +0100 Subject: [PATCH] Implement Logging (#2) * initial logging implementation * formatting * lift Printf compat --- Project.toml | 3 +++ src/TRGKit.jl | 1 + src/schemes/btrg.jl | 9 +++++++++ src/schemes/hotrg.jl | 6 ++++++ src/schemes/trg.jl | 6 ++++++ src/schemes/trgscheme.jl | 32 +++++++++++++++++++------------- src/utility/stopping.jl | 17 +++++++++++++++++ 7 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Project.toml b/Project.toml index 6358ec8..364fe8e 100644 --- a/Project.toml +++ b/Project.toml @@ -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] diff --git a/src/TRGKit.jl b/src/TRGKit.jl index 7a1e14a..235781a 100644 --- a/src/TRGKit.jl +++ b/src/TRGKit.jl @@ -1,5 +1,6 @@ module TRGKit using TensorKit, LinearAlgebra +using LoggingExtras, Printf # stop criteria include("utility/stopping.jl") diff --git a/src/schemes/btrg.jl b/src/schemes/btrg.jl index 979dc33..bc1e055 100644 --- a/src/schemes/btrg.jl +++ b/src/schemes/btrg.jl @@ -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 diff --git a/src/schemes/hotrg.jl b/src/schemes/hotrg.jl index 7fbfe2e..cff13dc 100644 --- a/src/schemes/hotrg.jl +++ b/src/schemes/hotrg.jl @@ -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 diff --git a/src/schemes/trg.jl b/src/schemes/trg.jl index 9a67752..caeff71 100644 --- a/src/schemes/trg.jl +++ b/src/schemes/trg.jl @@ -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 diff --git a/src/schemes/trgscheme.jl b/src/schemes/trgscheme.jl index 425c348..4bb9324 100644 --- a/src/schemes/trgscheme.jl +++ b/src/schemes/trgscheme.jl @@ -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 diff --git a/src/utility/stopping.jl b/src/utility/stopping.jl index f9e4f2f..53e8c9c 100644 --- a/src/utility/stopping.jl +++ b/src/utility/stopping.jl @@ -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