Skip to content

Commit 6b4ed07

Browse files
committed
feat: add general show methods for param structs
1 parent e138e0f commit 6b4ed07

File tree

10 files changed

+250
-66
lines changed

10 files changed

+250
-66
lines changed

src/parameters/AbstractTypes.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ abstract type ParametersType end
99
# Temporary fallback until we stop checking eltype from the parameters
1010
Base.eltype(p::ParametersType) = fieldcount(typeof(p)) > 0 ? eltype(getfield(p, 1)) : Any
1111
Base.broadcastable(x::ParametersType) = tuple(x)
12+
Base.show(io::IO, x::ParametersType) =
13+
ShowMethods.parseable_show_with_fields_no_type_header(io, x; with_module_prefix = false)
14+
Base.show(io::IO, mime::MIME"text/plain", x::ParametersType) =
15+
ShowMethods.show_type_and_fields(io, mime, x)
1216

1317
"""
1418
AerosolType

src/parameters/AirProperties.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ $(DocStringExtensions.FIELDS)
1111
@kwdef struct AirProperties{FT} <: ParametersType
1212
"thermal conductivity of air [w/m/K]"
1313
K_therm::FT
14-
"diffusivity of water vapor [m2/s]"
14+
"diffusivity of water vapor [/s]"
1515
D_vapor::FT
16-
"kinematic viscosity of air [m2/s]"
16+
"kinematic viscosity of air [/s]"
1717
ν_air::FT
1818
end
1919

20+
ShowMethods.field_units(::AirProperties) =
21+
(; K_therm = "W/m/K", D_vapor = "m²/s", ν_air = "m²/s")
2022

2123
function AirProperties(td::CP.ParamDict)
2224
name_map = (;

src/parameters/Microphysics1M.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ $(DocStringExtensions.FIELDS)
178178
"assumed number concentration for cloud sedimentation [1/m3]"
179179
N_0::FT
180180
end
181+
Base.show(io::IO, mime::MIME"text/plain", vel::CloudIce) =
182+
ShowMethods.verbose_show_type_and_fields(io, mime, vel)
181183

182184
function CloudIce(toml_dict::CP.ParamDict)
183185
name_map = (;

src/parameters/Microphysics1MParams.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Parameters for cloud-phase (non-precipitating) hydrometeors in 1-moment scheme.
1414
ice::ICL
1515
end
1616

17+
Base.show(io::IO, mime::MIME"text/plain", x::CloudPhaseParams1M) =
18+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
19+
1720
"""
1821
PrecipPhaseParams1M{FT, RAI, SNO}
1922
@@ -28,6 +31,9 @@ Parameters for precipitating hydrometeors in 1-moment scheme.
2831
snow::SNO
2932
end
3033

34+
Base.show(io::IO, mime::MIME"text/plain", x::PrecipPhaseParams1M) =
35+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
36+
3137
"""
3238
Microphysics1MParams{FT, CP, PP, CE, AP, VL, VA}
3339
@@ -62,6 +68,8 @@ mp_with_2M = CMP.Microphysics1MParams(Float64; with_2M_autoconv = true)
6268
autoconv_2M::VA
6369
prescribed_Nc::FT
6470
end
71+
Base.show(io::IO, mime::MIME"text/plain", x::Microphysics1MParams) =
72+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
6573

6674
"""
6775
Microphysics1MParams(toml_dict::CP.ParamDict; with_2M_autoconv = false)

src/parameters/Microphysics2M.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ RainParticlePDF_SB2006(toml_dict::CP.ParamDict; is_limited = true) =
324324
RainParticlePDF_SB2006_limited(toml_dict) :
325325
RainParticlePDF_SB2006_notlimited(toml_dict)
326326

327+
Base.show(io::IO, mime::MIME"text/plain", x::RainParticlePDF_SB2006) =
328+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
329+
327330
"""
328331
RainParticlePDF_SB2006_limited
329332
@@ -653,6 +656,8 @@ $(DocStringExtensions.FIELDS)
653656
"Number concentration adjustment parameter"
654657
numadj::NA
655658
end
659+
Base.show(io::IO, mime::MIME"text/plain", x::SB2006) =
660+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
656661

657662
# Construct SB2006 from a ClimaParams TOML dict
658663
SB2006(toml_dict::CP.ParamDict; is_limited = true) =

src/parameters/Microphysics2MParams.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ WarmRainParams2M(toml_dict::CP.ParamDict; is_limited = true) =
2020
air_properties = AirProperties(toml_dict),
2121
)
2222

23+
Base.show(io::IO, mime::MIME"text/plain", x::WarmRainParams2M) =
24+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
25+
2326
"""
2427
P3IceParams
2528
@@ -37,6 +40,8 @@ Parameters for P3 ice-phase processes (optional).
3740
cloud_pdf::PDc
3841
rain_pdf::PDr
3942
end
43+
Base.show(io::IO, mime::MIME"text/plain", x::P3IceParams) =
44+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
4045

4146
P3IceParams(toml_dict::CP.ParamDict; is_limited = true) =
4247
P3IceParams(;
@@ -73,6 +78,8 @@ mp_p3 = CMP.Microphysics2MParams(Float64; with_ice = true)
7378
warm_rain::WR
7479
ice::ICE
7580
end
81+
Base.show(io::IO, mime::MIME"text/plain", x::Microphysics2MParams) =
82+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
7683

7784
"""
7885
Microphysics2MParams(::Type{FT}; with_ice = false, is_limited = true) where {FT <: AbstractFloat}

src/parameters/MicrophysicsP3.jl

Lines changed: 12 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -306,67 +306,15 @@ end
306306
### ----- UTILS ----- ###
307307
### ----------------- ###
308308

309-
function Base.show(
310-
io::IO,
311-
p::Union{
312-
ParametersP3,
313-
MassPowerLaw,
314-
AreaPowerLaw,
315-
SlopePowerLaw,
316-
SlopeConstant,
317-
VentilationFactor,
318-
LocalRimeDensity,
319-
},
320-
)
321-
indent = get(io, :indent, "")
322-
prefix = get(io, :prefix, "")
323-
324-
# Get type information
325-
T = typeof(p)
326-
FT = eltype(p)
327-
type = (FT != get(io, :typeinfo, Any)) ? "{$FT}" : ""
328-
329-
# Print type name, handling nested types
330-
type_name = T.name.name
331-
println(io, "$(prefix)$(type_name)$type")
332-
333-
# Print each field
334-
fields = fieldnames(T)
335-
for (i, field) in enumerate(fields)
336-
value = getfield(p, field)
337-
is_last = i == length(fields)
338-
prefix_char = is_last ? "" : ""
339-
340-
if typeof(value) <: Number
341-
# Simple value - print directly with unit
342-
unit = _get_parameter_unit(field)
343-
println(
344-
io,
345-
"$(indent)$(prefix_char)── $(field) = $(value) [$(unit)]",
346-
)
347-
else
348-
# Nested struct - recursively show with proper context
349-
nested_io = IOContext(
350-
io,
351-
:prefix => "$(prefix_char)── $(field): ",
352-
:indent => "$(indent)",
353-
:typeinfo => FT,
354-
)
355-
show(nested_io, value)
356-
end
357-
end
358-
end
359-
360-
function _get_parameter_unit(field::Symbol)
361-
units = Dict(
362-
:α_va => "kg μm^(-β_va)",
363-
=> "μm^(2-σ)",
364-
:a => "m^b",
365-
:τ_wet => "s",
366-
:ρ_i => "kg m⁻³",
367-
:ρ_l => "kg m⁻³",
368-
:T_freeze => "K",
369-
)
370-
# unitless parameters: β_va, σ, b, c, μ_max, μ, aᵥ, bᵥ
371-
return get(units, field, "-")
372-
end
309+
Base.show(io::IO, mime::MIME"text/plain", p::ParametersP3) =
310+
ShowMethods.verbose_show_type_and_fields(io, mime, p)
311+
312+
# Unit annotations for verbose show (used by ShowMethods.verbose_show_type_and_fields)
313+
ShowMethods.field_units(::MassPowerLaw) = (; α_va = "kg m^(-β_va)")
314+
ShowMethods.field_units(::AreaPowerLaw) = (; γ = "μm^(2-σ)")
315+
ShowMethods.field_units(::SlopePowerLaw) = (; a = "m^b")
316+
ShowMethods.field_units(::SlopeConstant) = (;)
317+
ShowMethods.field_units(::VentilationFactor) = (;)
318+
ShowMethods.field_units(::LocalRimeDensity) = (; ρ_ice = "kg m⁻³")
319+
ShowMethods.field_units(::ParametersP3) =
320+
(; τ_wet = "s", ρ_i = "kg m⁻³", ρ_l = "kg m⁻³", T_freeze = "K")

src/parameters/Parameters.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ using DocStringExtensions
1010
import ClimaParams as CP
1111
import SpecialFunctions as SF
1212

13+
include("../show.jl")
14+
import .ShowMethods
15+
1316
# Super-types (dispatch, broadcasting, etc...)
1417
include("AbstractTypes.jl")
1518

src/parameters/TerminalVelocity.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ $(DocStringExtensions.FIELDS)
3131
"pre-computed gamma(ae + ve + Δa + Δv + 1) for accretion [-]"
3232
gamma_accr::FT
3333
end
34+
Base.show(io::IO, mime::MIME"text/plain", vel::Blk1MVelTypeRain) =
35+
ShowMethods.verbose_show_type_and_fields(io, mime, vel)
3436

3537
function Blk1MVelTypeRain(td::CP.ParamDict)
3638
vel_map = (;
@@ -86,6 +88,8 @@ $(DocStringExtensions.FIELDS)
8688
"pre-computed gamma(ae + ve + Δa + Δv + 1) for accretion [-]"
8789
gamma_accr::FT
8890
end
91+
Base.show(io::IO, mime::MIME"text/plain", vel::Blk1MVelTypeSnow) =
92+
ShowMethods.verbose_show_type_and_fields(io, mime, vel)
8993

9094
function Blk1MVelTypeSnow(td::CP.ParamDict)
9195
vel_map = (;
@@ -127,6 +131,8 @@ $(DocStringExtensions.FIELDS)
127131
rain::R
128132
snow::S
129133
end
134+
Base.show(io::IO, mime::MIME"text/plain", x::Blk1MVelType) =
135+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
130136

131137
Blk1MVelType(toml_dict::CP.ParamDict) =
132138
Blk1MVelType(;
@@ -209,6 +215,8 @@ $(DocStringExtensions.FIELDS)
209215
"cutoff for small vs large ice particle dimension [m]"
210216
cutoff::FT
211217
end
218+
Base.show(io::IO, mime::MIME"text/plain", x::Chen2022VelTypeSmallIce) =
219+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
212220

213221
function Chen2022VelTypeSmallIce(td::CP.ParamDict)
214222
# TODO: These should be array parameters.
@@ -286,6 +294,8 @@ $(DocStringExtensions.FIELDS)
286294
b_ρ::FT
287295
c::NTuple{N, FT}
288296
end
297+
Base.show(io::IO, mime::MIME"text/plain", x::Chen2022VelTypeRain) =
298+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
289299

290300
function Chen2022VelTypeRain(td::CP.ParamDict)
291301
name_map = (;
@@ -325,6 +335,9 @@ Chen2022VelType(toml_dict::CP.ParamDict) =
325335
large_ice = Chen2022VelTypeLargeIce(toml_dict),
326336
)
327337

338+
Base.show(io::IO, mime::MIME"text/plain", x::Chen2022VelType) =
339+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
340+
328341
"""
329342
TerminalVelocityParams
330343

0 commit comments

Comments
 (0)