Skip to content

Commit

Permalink
Introduce logger type, remove module methods
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Sep 6, 2023
1 parent e7a012f commit 47ad077
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 176 deletions.
5 changes: 2 additions & 3 deletions docs/src/saturation_adjustment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Thermodynamics as TD
const TP = TD.Parameters
import CLIMAParameters as CP

TD.print_warning() = false
toml_dict = CP.create_toml_dict(FT; dict_type = "alias")
aliases = string.(fieldnames(TP.ThermodynamicsParameters))
param_pairs = CP.get_parameter_values!(toml_dict, aliases, "Thermodynamics")
Expand Down Expand Up @@ -59,19 +58,19 @@ ts_no_err = Dict(
)

@inbounds for NM in numerical_methods
TD.error_on_non_convergence() = false
ts_no_err[NM][n] = TD.PhaseEquil_dev_only(
TD.NullLogger(),
param_set,
ρ[i],
e_int,
q_tot[k];
sat_adjust_method = NM,
maxiter = 10,
)
TD.error_on_non_convergence() = true
# @show n / prod(dims) * 100
try
ts[NM][n] = TD.PhaseEquil_dev_only(
TD.ErrorLogger(),
param_set,
ρ[i],
e_int,
Expand Down
6 changes: 1 addition & 5 deletions perf/jet.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
include("common_micro_bm.jl")

# JET does not like KernelAbstractions.@print calls,
# so we disable them here.
TD.print_warning() = false

function jet_thermo_states(::Type{FT}) where {FT}
param_set = get_parameter_set(FT)
ArrayType = Array{FT}
Expand All @@ -22,7 +18,7 @@ function jet_thermo_states(::Type{FT}) where {FT}
)
for cond in conditions(C)
args = sample_args(profiles, param_set, cond, C)
JET.@test_opt C(param_set, args...)
JET.@test_opt C(TD.NullLogger(), param_set, args...)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions src/DataCollection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import Thermodynamics as TD
import RootSolvers as RS
function do_work()
# Calls TD.PhaseEquil_ρeq()..., possibly many times
# Calls TD.PhaseEquil_ρeq(VerboseLogger(WarningLogger()), )..., possibly many times
end
TD.solution_type() = RS.VerboseSolution()
do_work()
TD.DataCollection.print_summary()
```
Expand Down
18 changes: 3 additions & 15 deletions src/Thermodynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,14 @@ import .Parameters
const TP = Parameters
const APS = TP.ThermodynamicsParameters

# Allow users to skip error on non-convergence
# by importing:
# ```julia
# import Thermodynamics
# Thermodynamics.error_on_non_convergence() = false
# ```
# Error on convergence must be the default
# behavior because this can result in printing
# very large logs resulting in CI to seemingly hang.
error_on_non_convergence() = true

# Allow users to skip printing warnings on non-convergence
print_warning() = true

@inline q_pt_0(::Type{FT}) where {FT} = PhasePartition(FT(0), FT(0), FT(0))

@inline solution_type() = RS.CompactSolution()
include("DataCollection.jl")
import .DataCollection

include("logger.jl")
solution_type(::AbstractThermodynamicsLogger) = RS.CompactSolution()
solution_type(::VerboseLogger) = RS.CompactSolution()
include("states.jl")
include("relations.jl")
include("isentropic.jl")
Expand Down
63 changes: 63 additions & 0 deletions src/logger.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
AbstractThermodynamicsLogger
A Thermodynamics logger can be passed to
thermo state constructors that (may) perform
saturation adjustment. This logger will statically
decide if a warning statement is printed, or
if the function should error on non-convergence.
"""
abstract type AbstractThermodynamicsLogger end
const ATL = AbstractThermodynamicsLogger

"""
WarningLogger
Warn when saturation adjustment did not converge.
"""
struct WarningLogger <: AbstractThermodynamicsLogger end

"""
ErrorLogger
Error (but do not warn) when saturation adjustment
did not converge.
This is really only useful in our development docs.
"""
struct ErrorLogger <: AbstractThermodynamicsLogger end

"""
WarnAndErrorLogger
Warn and error (default) when saturation adjustment did not converge.
"""
struct WarnAndErrorLogger <: AbstractThermodynamicsLogger end

"""
VerboseLogger
A verbose logger, for when we use RootSolvers `VerboseSolution` type.
"""
struct VerboseLogger{L} <: AbstractThermodynamicsLogger
logger::L
end

"""
NullLogger
Do nothing when saturation adjustment did not converge.
"""
struct NullLogger <: AbstractThermodynamicsLogger end

warn_msg(::WarningLogger) = true
warn_msg(::WarnAndErrorLogger) = true
warn_msg(l::VerboseLogger) = warn_msg(l.logger)
warn_msg(::AbstractThermodynamicsLogger) = false

Check warning on line 56 in src/logger.jl

View check run for this annotation

Codecov / codecov/patch

src/logger.jl#L56

Added line #L56 was not covered by tests

error_msg(::ErrorLogger) = true

Check warning on line 58 in src/logger.jl

View check run for this annotation

Codecov / codecov/patch

src/logger.jl#L58

Added line #L58 was not covered by tests
error_msg(::WarnAndErrorLogger) = true
error_msg(l::VerboseLogger) = error_msg(l.logger)
error_msg(::AbstractThermodynamicsLogger) = false

Base.broadcastable(x::AbstractThermodynamicsLogger) = tuple(x)

0 comments on commit 47ad077

Please sign in to comment.