Skip to content

Commit 1f4a917

Browse files
committed
feat: add general show methods for param structs
1 parent 017a994 commit 1f4a917

14 files changed

+279
-91
lines changed

docs/src/API.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,10 @@ Parameters.Microphysics2MParams
430430
PrecipitationSusceptibility.precipitation_susceptibility_autoconversion
431431
PrecipitationSusceptibility.precipitation_susceptibility_accretion
432432
```
433+
434+
435+
# Developer docs
436+
437+
```@autodocs
438+
Modules = [ShowMethods]
439+
```

src/CloudMicrophysics.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module CloudMicrophysics
22

3+
include("show.jl")
4+
35
include("parameters/Parameters.jl")
46
import .Parameters
57
const CMP = Parameters

src/P3.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import CloudMicrophysics.Common as CO
2020
import CloudMicrophysics.DistributionTools as DT
2121
import CloudMicrophysics.HetIceNucleation as CM_HetIce
2222
import CloudMicrophysics.Microphysics2M as CM2
23+
import CloudMicrophysics: ShowMethods
2324

2425
include("P3_particle_properties.jl")
2526
include("P3_size_distribution.jl")

src/P3_particle_properties.jl

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ $(FIELDS)
2525
"Rime density"
2626
ρ_rim::FT
2727
end
28+
Base.show(io::IO, mime::MIME"text/plain", x::P3State) =
29+
ShowMethods.verbose_show_type_and_fields(io, mime, x)
30+
ShowMethods.field_units(::P3State) = (; L_ice = "kg/m³", N_ice = "1/m³", ρ_rim = "kg/m³")
2831

2932
"""
3033
get_state(params; L_ice, N_ice, F_rim, ρ_rim)
@@ -51,10 +54,21 @@ Create a [`P3State`](@ref) from [`CMP.ParametersP3`](@ref) and rime state parame
5154
julia> params = CMP.ParametersP3(FT);
5255
5356
julia> state = P3.get_state(params; F_rim = FT(0.5), ρ_rim = FT(916.7))
54-
P3State{Float32}
55-
├── params = {MassPowerLaw, AreaPowerLaw, SlopePowerLaw, VentilationFactor}
56-
├── F_rim = 0.5 [-]
57-
└── ρ_rim = 916.7 [kg/m^3]
57+
P3State
58+
├─ params: ParametersP3
59+
│ ├─ mass: MassPowerLaw(α_va = 0.018537706f0 [kg m^(-β_va)], β_va = 1.9f0 [-])
60+
│ ├─ area: AreaPowerLaw(γ = 0.2285f0 [μm^(2-σ)], σ = 1.88f0 [-])
61+
│ ├─ slope: SlopePowerLaw(a = 0.00191f0 [m^b], b = 0.8f0 [-], c = 2.0f0 [-], μ_max = 6.0f0 [-])
62+
│ ├─ vent: VentilationFactor(aᵥ = 0.78f0 [-], bᵥ = 0.308f0 [-])
63+
│ ├─ ρ_rim_local: LocalRimeDensity(a = 51.0f0 [-], b = 114.0f0 [-], c = -5.5f0 [-], ρ_ice = 916.7f0 [kg m⁻³])
64+
│ ├─ τ_wet = 100.0 [s]
65+
│ ├─ ρ_i = 916.7 [kg m⁻³]
66+
│ ├─ ρ_l = 1000.0 [kg m⁻³]
67+
│ └─ T_freeze = 273.15 [K]
68+
├─ L_ice = 0.3
69+
├─ N_ice = 1.0e6
70+
├─ F_rim = 0.5
71+
└─ ρ_rim = 916.7
5872
```
5973
"""
6074
function get_state(params::CMP.ParametersP3; L_ice, N_ice, F_rim, ρ_rim)
@@ -408,19 +422,3 @@ function ϕᵢ(params::CMP.ParametersP3, F_rim, ρ_rim, D)
408422

409423
return ifelse(D == 0, FT(0), ϕ_ob)
410424
end
411-
412-
### ----------------- ###
413-
### ----- UTILS ----- ###
414-
### ----------------- ###
415-
416-
function Base.show(io::IO, state::P3State)
417-
FT = eltype(state)
418-
_name(state, field) = typeof(getfield(state.params, field)).name.name
419-
param_types = join(_name.(state, (:mass, :area, :slope, :vent)), ", ")
420-
println(io, "P3State{$FT}")
421-
println(io, "├── params = {$param_types}")
422-
println(io, "├── L_ice = $(state.L_ice) [kg/m³]")
423-
println(io, "├── N_ice = $(state.N_ice) [1/m³]")
424-
println(io, "├── F_rim = $(state.F_rim) [-]")
425-
println(io, "└── ρ_rim = $(state.ρ_rim) [kg/m³]")
426-
end

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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ $(DocStringExtensions.FIELDS)
5252
"pre-computed gamma(me + Δm + 1) for performance [-]"
5353
gamma_coeff::FT
5454
end
55+
ShowMethods.field_units(::ParticleMass) = (; r0 = "m", m0 = "kg")
5556

5657
"""
5758
ParticleArea{FT}
@@ -65,7 +66,7 @@ a(r) = a0 χa (r/r0)^(ae + Δa)
6566
$(DocStringExtensions.FIELDS)
6667
"""
6768
@kwdef struct ParticleArea{FT} <: ParametersType
68-
"cross section size relation coefficient [m2]"
69+
"cross section size relation coefficient []"
6970
a0::FT
7071
"cross section size relation coefficient [-]"
7172
ae::FT
@@ -74,6 +75,7 @@ $(DocStringExtensions.FIELDS)
7475
"cross section size relation coefficient [-]"
7576
χa::FT
7677
end
78+
ShowMethods.field_units(::ParticleArea) = (; a0 = "")
7779

7880
"""
7981
Ventilation{FT}
@@ -133,13 +135,15 @@ $(DocStringExtensions.FIELDS)
133135
@kwdef struct CloudLiquid{FT} <: CloudCondensateType
134136
"condensation evaporation non_equil microphysics relaxation timescale [s]"
135137
τ_relax::FT
136-
"water density [kg/m3]"
138+
"water density [kg/]"
137139
ρw::FT
138140
"effective radius [m]"
139141
r_eff::FT
140-
"assumed number concentration for cloud sedimentation [1/m3]"
142+
"assumed number concentration for cloud sedimentation [1/]"
141143
N_0::FT
142144
end
145+
ShowMethods.field_units(::CloudLiquid) =
146+
(; τ_relax = "s", ρw = "kg/m³", r_eff = "m", N_0 = "1/m³")
143147

144148
function CloudLiquid(toml_dict::CP.ParamDict)
145149
name_map = (;
@@ -171,13 +175,15 @@ $(DocStringExtensions.FIELDS)
171175
r_ice_snow::FT
172176
"deposition sublimation non_equil microphysics relaxation timescale [s]"
173177
τ_relax::FT
174-
"cloud ice apparent density [kg/m3]"
178+
"cloud ice apparent density [kg/]"
175179
ρᵢ::FT
176180
"effective radius [m]"
177181
r_eff::FT
178-
"assumed number concentration for cloud sedimentation [1/m3]"
182+
"assumed number concentration for cloud sedimentation [1/]"
179183
N_0::FT
180184
end
185+
ShowMethods.field_units(::CloudIce) =
186+
(; r0 = "m", r_ice_snow = "m", τ_relax = "s", ρᵢ = "kg/m³", r_eff = "m", N_0 = "1/m³")
181187

182188
function CloudIce(toml_dict::CP.ParamDict)
183189
name_map = (;
@@ -232,6 +238,7 @@ $(DocStringExtensions.FIELDS)
232238
"particle length scale [m]"
233239
r0::FT
234240
end
241+
ShowMethods.field_units(::Rain) = (; r0 = "m")
235242

236243
function Rain(toml_dict::CP.ParamDict)
237244
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: 3 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

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(toml_dict::CP.ParamDict; with_ice = false, is_limited = true)

0 commit comments

Comments
 (0)