Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set up barebones Convergence Struct and print
- Loading branch information
1 parent
e45d0e6
commit 0408558
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,73 @@ | ||
|
||
export @logconvergence | ||
|
||
@enum Direction::UInt8 Descending Ascending | ||
|
||
const FN = Union{Float64,Nothing} | ||
const DN = Union{Direction,Nothing} | ||
|
||
struct Convergence | ||
id::UUID | ||
parentid::UUID | ||
names::Vector{String} | ||
values::Vector{FN} | ||
thresholds::Vector{FN} | ||
directions::Vector{DN} | ||
done::Bool | ||
step::Union{Int,Nothing} | ||
function Convergence(id, parentid, names, values, thresholds, directions, done, step) | ||
values = convert(Vector{FN}, values) | ||
thresholds = convert(Vector{FN}, thresholds) | ||
@. values[isnan_safe(values)] = nothing | ||
@. thresholds[isnan_safe(thresholds)] = nothing | ||
return new(id, parentid, names, values, thresholds, directions, done, step) | ||
end | ||
end | ||
|
||
isnan_safe(v::Real) = isnan(v) | ||
isnan_safe(::Nothing) = false | ||
|
||
Convergence(; | ||
id::UUID, | ||
parentid::UUID = ROOTID, # not nested by default | ||
values=FN[nothing], | ||
names=fill("", length(values)), | ||
thresholds=Vector{FN}(nothing, length(values)), | ||
directions=Vector{DN}(nothing, length(values)), | ||
done::Bool = false, | ||
step::Union{Int,Nothing} = nothing | ||
) = Convergence(id, parentid, names, values, thresholds, directions, done, step) | ||
|
||
Convergence(id::UUID; kwargs...) = Convergence(; id = id, kwargs...) | ||
|
||
# Define `string`/`print` so that progress log records are (somewhat) | ||
# readable even without specific log monitors. | ||
function Base.print(io::IO, c::Convergence) | ||
for args in zip(c.names, c.values, c.thresholds, c.directions) | ||
c.step !== nothing && print(io, "$(c.step) ") | ||
_print_convergence(io, c.parentid, args...) | ||
println(io) | ||
end | ||
return nothing | ||
end | ||
|
||
dir_char(d::Direction) = d === Descending ? '\u2193' : '\u2191' # ↓ : ↑ | ||
dir_char(::Nothing) = '|' | ||
|
||
# print a single minutia of the convergence struct | ||
function _print_convergence(io, parentid, name, value, threshold, direction) | ||
print(io, isempty(name) ? "Convergence" : name) | ||
if parentid !== ROOTID | ||
print(io, " (sub)") | ||
end | ||
print(io, ": ") | ||
value === nothing ? print(io, "??") : print(io, value) | ||
print(io, dir_char(direction)) | ||
threshold === nothing ? print(io, "??") : print(io, threshold) | ||
|
||
return nothing | ||
end | ||
|
||
macro logconvergence() | ||
:(nothing) | ||
end |