Skip to content

Commit

Permalink
Update to new ClimaParams, CloudMicrophysics and Thermodynamics (#140)
Browse files Browse the repository at this point in the history
* Update to new ClimaParams, CloudMicrophysics, Thermodynamics, ClimaCore and NCdatasets

Co-authored-by: Sajjad Azimi <[email protected]>
  • Loading branch information
trontrytel and sajjadazimi authored Apr 22, 2024
1 parent 1d11cd1 commit 0b23aa0
Show file tree
Hide file tree
Showing 27 changed files with 218 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/JuliaFormatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- uses: julia-actions/setup-julia@latest
if: steps.filter.outputs.julia_file_change == 'true'
with:
version: 1.8.1
version: '1.10'

- name: Apply JuliaFormatter
if: steps.filter.outputs.julia_file_change == 'true'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
version: 1.8.1
version: '1.10'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
Expand Down
13 changes: 8 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name = "Kinematic1D"
uuid = "a8a3bb05-6cc5-4342-abf6-5cc636bd2b35"
authors = ["Climate Modeling Alliance"]
version = "0.6.1"
version = "0.7.0"

[deps]
ClimaCore = "d414da3d-4745-48bb-8d80-42e94e092884"
ClimaCorePlots = "cf7c7e5a-b407-4c48-9047-11a94a308626"
ClimaParams = "5c42b081-d73a-476f-9059-fd94b934656c"
CloudMicrophysics = "6a9e3e04-43cd-43ba-94b9-e8782df3c71b"
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
EnsembleKalmanProcesses = "aa8a2aa5-91d8-4396-bcef-d4f2ec43552d"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Expand All @@ -26,25 +28,26 @@ Thermodynamics = "b60c26fb-14c3-4610-9d3e-2d17fe7ff00c"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"

[compat]
ClimaCore = "0.11"
ClimaCore = "0.13"
ClimaCorePlots = "0.2"
CloudMicrophysics = "0.15"
ClimaParams = "0.10.5"
CloudMicrophysics = "0.18.0"
DiffEqBase = "6.75"
Distributions = "0.25"
EnsembleKalmanProcesses = "1.1"
Interpolations = "0.15"
JLD2 = "0.4"
LinearAlgebra = "1"
Logging = "1"
NCDatasets = "0.12"
NCDatasets = "0.14"
Optim = "1.7"
OrdinaryDiffEq = "6.29"
Plots = "1.29"
ProgressLogging = "0.1"
Random = "1"
SpecialFunctions = "2.3"
TerminalLoggers = "0.1"
Thermodynamics = "0.11"
Thermodynamics = "0.12.6"
UnPack = "1.0"
julia = "1.8"

Expand Down
1 change: 0 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
Kinematic1D = "a8a3bb05-6cc5-4342-abf6-5cc636bd2b35"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[compat]
Expand Down
15 changes: 9 additions & 6 deletions src/CalibrateCMP/DistributionUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ function initial_parameter_ensemble(priors, N_ensemble; rng_seed = 10)
return initial_ensemble
end

function construct_priors(params::Dict)
function construct_priors(
params::Dict{String, @NamedTuple{mean::FT, var::FT, lbound::FT, ubound::FT}},
) where {FT <: Real}
u_names = collect(keys(params))
u_values = collect(values(params))
u_means = collect([v.mean for v in u_values])
u_vars = collect([v.var for v in u_values])
u_lbounds = collect([v.lbound for v in u_values])
u_ubounds = collect([v.ubound for v in u_values])
n_params = length(u_names)

# All vars are approximated as Gaussian in unconstrained space
marginal_priors = constrained_gaussian.(u_names, u_means, u_vars, u_lbounds, u_ubounds)
marginal_priors = Vector{ParameterDistribution}(undef, n_params)
for i in 1:n_params
marginal_priors[i] =
constrained_gaussian(u_names[i], u_values[i].mean, u_values[i].var, u_values[i].lbound, u_values[i].ubound)
end
return combine_distributions(marginal_priors)
end
2 changes: 1 addition & 1 deletion src/CalibrateCMP/IOUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function read_pysdm_data(file)
# iterate over all variables
vars = Dict()
for (varname, var) in ds
vars[varname] = var[:]
vars[varname] = reshape(var[:], size(var))
end

close(ds)
Expand Down
9 changes: 3 additions & 6 deletions src/CalibrateCMP/KiDUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ function run_KiD(u::Array{FT, 1}, u_names::Array{String, 1}, model_settings::Dic
κ = model_settings["κ"],
)

moisture = CO.get_moisture_type(FT, model_settings["moisture_choice"], model_settings["toml_dict"])
moisture = CO.get_moisture_type(model_settings["moisture_choice"], model_settings["toml_dict"])
precip = CO.get_precipitation_type(
FT,
model_settings["precipitation_choice"],
model_settings["toml_dict"];
rain_formation_choice = model_settings["rain_formation_choice"],
Expand Down Expand Up @@ -152,18 +151,16 @@ function run_KiD_col_sed(u::Array{FT, 1}, u_names::Array{String, 1}, model_setti

update_parameters!(model_settings, u, u_names)
apply_param_dependency!(model_settings)
model_settings["toml_dict"]["νc_SB2006"]["value"] = model_settings["k"]
model_settings["toml_dict"]["SB2006_cloud_gamma_distribution_parameter"]["value"] = model_settings["k"]
common_params = create_common_parameters(
FT,
precip_sources = model_settings["precip_sources"],
precip_sinks = model_settings["precip_sinks"],
Nd = model_settings["Nd"],
)
kid_params = create_kid_parameters(FT)

moisture = CO.get_moisture_type(FT, "NonEquilibriumMoisture", model_settings["toml_dict"])
moisture = CO.get_moisture_type("NonEquilibriumMoisture", model_settings["toml_dict"])
precip = CO.get_precipitation_type(
FT,
model_settings["precipitation_choice"],
model_settings["toml_dict"];
rain_formation_choice = model_settings["rain_formation_choice"],
Expand Down
39 changes: 19 additions & 20 deletions src/Common/helper_functions.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
Returns moisture type
"""
function get_moisture_type(FT, moisture_choice::String, toml_dict)
function get_moisture_type(moisture_choice::String, toml_dict)
if moisture_choice == "EquilibriumMoisture"
moisture = EquilibriumMoisture_ρdTq()
elseif moisture_choice == "NonEquilibriumMoisture"
moisture = NonEquilibriumMoisture_ρdTq(CMP.CloudLiquid(FT, toml_dict), CMP.CloudIce(FT, toml_dict))
moisture = NonEquilibriumMoisture_ρdTq(CMP.CloudLiquid(toml_dict), CMP.CloudIce(toml_dict))
else
error("Invalid moisture choice: $moisture_choice")
end
Expand All @@ -16,7 +16,6 @@ end
Returns precipitation type
"""
function get_precipitation_type(
FT,
precipitation_choice::String,
toml_dict;
rain_formation_choice::Union{Nothing, String} = nothing,
Expand All @@ -25,50 +24,50 @@ function get_precipitation_type(
if precipitation_choice == "NoPrecipitation"
precip = NoPrecipitation()
elseif precipitation_choice == "Precipitation0M"
precip = Precipitation0M(CMP.Parameters0M(FT, toml_dict))
precip = Precipitation0M(CMP.Parameters0M(toml_dict))
elseif precipitation_choice == "Precipitation1M"
if sedimentation_choice == "CliMA_1M" || sedimentation_choice === nothing
st = CMP.Blk1MVelType(FT, toml_dict)
st = CMP.Blk1MVelType(toml_dict)
elseif sedimentation_choice == "Chen2022"
st = CMP.Chen2022VelType(FT, toml_dict)
st = CMP.Chen2022VelType(toml_dict)
else
error("Invalid sedimentation choice: $sedimentation_choice")
end
if rain_formation_choice == "CliMA_1M" || rain_formation_choice === nothing
rain_params = CMP.Rain(FT, toml_dict)
rain_params = CMP.Rain(toml_dict)
rf = rain_params.acnv1M
elseif rain_formation_choice == "KK2000"
rf = CMP.KK2000(FT, toml_dict)
rf = CMP.KK2000(toml_dict)
elseif rain_formation_choice == "B1994"
rf = CMP.B1994(FT, toml_dict)
rf = CMP.B1994(toml_dict)
elseif rain_formation_choice == "TC1980"
rf = CMP.TC1980(FT, toml_dict)
rf = CMP.TC1980(toml_dict)
elseif rain_formation_choice == "LD2004"
rf = CMP.LD2004(FT, toml_dict)
rf = CMP.LD2004(toml_dict)
elseif rain_formation_choice == "VarTimeScaleAcnv"
rf = CMP.VarTimescaleAcnv(FT, toml_dict)
rf = CMP.VarTimescaleAcnv(toml_dict)
else
error("Invalid rain formation choice: $rain_formation_choice")
end
precip = Precipitation1M(
CMP.CloudLiquid(FT, toml_dict),
CMP.CloudIce(FT, toml_dict),
CMP.Rain(FT, toml_dict),
CMP.Snow(FT, toml_dict),
CMP.CollisionEff(FT, toml_dict),
CMP.CloudLiquid(toml_dict),
CMP.CloudIce(toml_dict),
CMP.Rain(toml_dict),
CMP.Snow(toml_dict),
CMP.CollisionEff(toml_dict),
rf,
st,
)
elseif precipitation_choice == "Precipitation2M"
if sedimentation_choice == "SB2006" || sedimentation_choice === nothing
st = CMP.SB2006VelType(FT, toml_dict)
st = CMP.SB2006VelType(toml_dict)
elseif sedimentation_choice == "Chen2022"
st = CMP.Chen2022VelTypeRain(FT, toml_dict)
st = CMP.Chen2022VelTypeRain(toml_dict)
else
error("Invalid sedimentation choice: $sedimentation_choice")
end
if rain_formation_choice == "SB2006" || rain_formation_choice === nothing
precip = Precipitation2M(CMP.SB2006(FT, toml_dict), st)
precip = Precipitation2M(CMP.SB2006(toml_dict), st)
else
error("Invalid rain formation choice: $rain_formation_choice")
end
Expand Down
36 changes: 27 additions & 9 deletions src/Common/parameters.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
"""
Create common model parameters.
(Overwriting the default values happens in the driver file.
Here we are just creating the CommonParameters struct)
"""
module Parameters

using DocStringExtensions
import ClimaParams as CP

abstract type AbstractCommonParameters end
const ACP = AbstractCommonParameters

# Define KiD parameters
"""
CommonParameters{FT}
Common parameters for the kinematic driver simulations
#Fields
$(DocStringExtensions.FIELDS)
"""
Base.@kwdef struct CommonParameters{FT} <: ACP
precip_sources::Int
precip_sinks::Int
"Switch to include precipitation formation terms"
precip_sources::Bool
"Switch to include precipitation sink terms"
precip_sinks::Bool
"Prescribed number concentration of cloud droplets (needed for some schemes) [1/m3]"
prescribed_Nd::FT
end

function CommonParameters(td::CP.AbstractTOMLDict)
name_map = (;
:precipitation_sources_flag => :precip_sources,
:precipitation_sinks_flag => :precip_sinks,
:prescribed_Nd => :prescribed_Nd,
)
parameters = CP.get_parameter_values(td, name_map, "Common")
FT = CP.float_type(td)
return CommonParameters{FT}(; parameters...)
end

precip_sources(ps::ACP) = ps.precip_sources
precip_sinks(ps::ACP) = ps.precip_sinks
prescribed_Nd(ps::ACP) = ps.prescribed_Nd
Expand Down
6 changes: 3 additions & 3 deletions src/Common/tendency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ end
# autoconversion liquid to rain and ice to snow
# TODO - can we do it in a more elegant way?
if rf isa CMP.Acnv1M{FT}
tmp = CM1.conv_q_liq_to_q_rai(rf, q.liq, smooth_transition = true)
tmp = CM1.conv_q_liq_to_q_rai(rf, q.liq, true)
elseif typeof(rf) in [CMP.LD2004{FT}, CMP.VarTimescaleAcnv{FT}]
tmp = CM2.conv_q_liq_to_q_rai(rf, q.liq, ρ; N_d = common_params.prescribed_Nd)
tmp = CM2.conv_q_liq_to_q_rai(rf, q.liq, ρ, common_params.prescribed_Nd)
elseif typeof(rf.acnv) in [CMP.AcnvKK2000{FT}, CMP.AcnvB1994{FT}, CMP.AcnvTC1980{FT}]
tmp = CM2.conv_q_liq_to_q_rai(rf, q.liq, ρ; N_d = common_params.prescribed_Nd)
tmp = CM2.conv_q_liq_to_q_rai(rf, q.liq, ρ, common_params.prescribed_Nd)
else
error("Unrecognized rain formation scheme")
end
Expand Down
8 changes: 4 additions & 4 deletions src/K1DModel/ode_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function make_function_space(FT, z_min, z_max, n_elem)
domain = CC.Domains.IntervalDomain(
CC.Geometry.ZPoint{FT}(z_min),
CC.Geometry.ZPoint{FT}(z_max),
boundary_tags = (:bottom, :top),
boundary_names = (:bottom, :top),
)
mesh = CC.Meshes.IntervalMesh(domain, nelems = n_elem)

Expand All @@ -22,8 +22,8 @@ end

"""
Interface to ODE solver. Returns the function needed to compute the
right hand side of the solved ODE for advection, condensation,
collision, sedimentation and evaporation processes. The rhs is
right hand side of the solved ODE for advection, condensation,
collision, sedimentation and evaporation processes. The rhs is
assembled via dispatch based on the moisture and precipitation types.
"""
function make_rhs_function(ms::CO.AbstractMoistureStyle, ps::CO.AbstractPrecipitationStyle)
Expand All @@ -50,7 +50,7 @@ end

"""
Interface to ODE solver. Returns the function needed to compute the
right hand side of the solved ODE for collision and sedimentation
right hand side of the solved ODE for collision and sedimentation
processes. The rhs is assembled via dispatch based on precipitation type.
"""
function make_rhs_function_col_sed(ms::CO.AbstractMoistureStyle, ps::CO.AbstractPrecipitationStyle)
Expand Down
40 changes: 33 additions & 7 deletions src/K1DModel/parameters.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
"""
Create Kinematic1D model parameters.
(Overwriting the default values happens in the driver file.
Here we are just creating the Kinematic1DParameters struct)
"""
module Parameters

using DocStringExtensions
import ClimaParams as CP

abstract type AbstractKinematicParameters end
const AKP = AbstractKinematicParameters

# Define KiD parameters
"""
Kinematic1DParameters{FT}
Free parameters for the kinematic 1-dimensional simulation
#Fields
$(DocStringExtensions.FIELDS)
"""
Base.@kwdef struct Kinematic1DParameters{FT} <: AKP
"Maximum updraft momentum flux m/s kg/m3"
w1::FT
"Time when the updraft is switched off [s]"
t1::FT
"Surface pressure [Pa]"
p0::FT
"Switch to include flux correction for moisture transport"
qtot_flux_correction::Int
"Assumed aerosol dry radius [m]"
r_dry::FT
"Assumed aerosol standard deviation [-]"
std_dry::FT
"Assumed aerosol hygroscopicity"
κ::FT
end

function Kinematic1DParameters(td::CP.AbstractTOMLDict)
name_map = (;
:prescribed_flow_w1 => :w1,
:prescribed_flow_t1 => :t1,
:surface_pressure => :p0,
:qtot_flux_correction_flag => :qtot_flux_correction,
:r_dry => :r_dry,
:std_dry => :std_dry,
:kappa => ,
)
parameters = CP.get_parameter_values(td, name_map, "Kinematic1D")
FT = CP.float_type(td)
return Kinematic1DParameters{FT}(; parameters...)
end

w1(ps::AKP) = ps.w1
t1(ps::AKP) = ps.t1
p0(ps::AKP) = ps.p0
Expand Down
Loading

2 comments on commit 0b23aa0

@sajjadazimi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/105342

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.0 -m "<description of version>" 0b23aa05ac9b219c11c21d7b627b0330142ec902
git push origin v0.7.0

Please sign in to comment.