Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce logger type, remove module methods #158

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/src/saturation_adjustment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Thermodynamics.Parameters as TP
import CLIMAParameters as CP

FT = Float64

const param_set = TP.ThermodynamicsParameters(FT)

profiles = TD.TestedProfiles.PhaseEquilProfiles(param_set, Array{FT});
Expand Down Expand Up @@ -55,19 +54,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 = TP.ThermodynamicsParameters(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 @@ -56,26 +56,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.
@inline error_on_non_convergence() = true

# Allow users to skip printing warnings on non-convergence
@inline 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)