Atmospheric cloud microphysics in an adiabatic air-parcel model
This example constitutes a numerical implementation of a simple cloud physics model following : "An Elementary Parcel Model with Explicit Condensation and Supersaturation" by R.R. Rogers (1975). The purpose of this examples is twofold: to provide an atmospheric-physics example for the package, and to demonstrate a robust way of using DifferentialEquations.jl with the Unitful.jl dimensional analysis package. The example uses Unitful to check dimensions in the model equations. For the simulation, it converts constants, initial conditions, and parameters to plain SI magnitudes before constructing the ODEProblem.
The example code below reproduces all figures from the original paper by simulating the temporal evolution of relative humidity (i.e., saturation ratio), droplet radius, temperature, and liquid water content in an ascending air parcel.
Physical Background
Clouds are suspensions of water droplets (and/or ice particles) in the air. Formation of a cloud is a thermodynamic process. To analyze it, we employ a simplified model based on the so-called air parcel framework, in which an adiabatically isolated "parcel" of air ascends along a hydrostatic atmospheric pressure profile. Adiabatic cooling of air due to its expansion causes increase of relative humidity, which upon reaching supersaturation, triggers diffusional growth of droplets and concurrent latent heat release associated with the vapor-liquid phase transition.
In this simplified model:
- an air parcel rises at a constant velocity;
- water vapor condenses on a fixed number of pre-existing monodisperse droplets (i.e., no aerosol interactions are modeled);
- coalescence, sedimentation, radiative cooling and mixing are neglected.
Symbols used throughout the example below follow the notation from the Rogers 1975 paper.
Model Setup
Initial conditions for the simulation include physical constants:
import DifferentialEquations as DE
import SciMLBase
using Plots
using LaTeXStrings
using Unitful
using Test
consts_u = (
R_prime = 287.053 * Unitful.u"J/(kg*K)",
diffusion_constant = uconvert(Unitful.u"Pa*m^2/(K*s)", 8.28e2 *
Unitful.u"dyn/(K*s)"),
g = 9.81 * Unitful.u"m/(s^2)",
ε = 0.622 * Unitful.u"1",
ρ_l = 1000.0 * Unitful.u"kg/(m^3)",
cp = 1005 * Unitful.u"J/(kg*K)",
L = 2.5e6 * Unitful.u"J/kg",
convection_constant = uconvert(Unitful.u"W/(m*K)", 2.42e3 * Unitful.u"erg/(cm*s*K)"),
# Equation A.1 and A.2 on page 204
temp_273K = 273.0*Unitful.u"K",
temp_393K = 393.0*Unitful.u"K",
temp_120K = 120.0*Unitful.u"K",
# Equation A.3 on page 204
temp_eq_vapor = 5.44e3 * Unitful.u"K",
eq_vapor_pressure = uconvert(Unitful.u"Pa", 2.75e12 * Unitful.u"dyn/cm^2"))(R_prime = 287.053 J kg^-1 K^-1, diffusion_constant = 0.008280000000000001 m^2 Pa K^-1 s^-1, g = 9.81 m s^-2, ε = 0.622, ρ_l = 1000.0 kg m^-3, cp = 1005 J kg^-1 K^-1, L = 2.5e6 J kg^-1, convection_constant = 0.024200000000000003 W K^-1 m^-1, temp_273K = 273.0 K, temp_393K = 393.0 K, temp_120K = 120.0 K, temp_eq_vapor = 5440.0 K, eq_vapor_pressure = 2.75e11 Pa)and following parameters:
- Droplet concentration: $n_\text{0}$
- Updraft speed: $U$
- Initial droplet radius: $r_\text{0}$
- Initial temperature: $T_\text{0}$
- Initial pressure: $p_\text{0}$
- Initial saturation ratio: $S_\text{0}$
They are defined by an initial state vector u0 and a vector of parameters params. In the paper, these values are used as representative of a rapidly developing cumulonimbus clouds in the lower troposphere.
The evolution of the state vector u is governed by the following equations:
- Evolution of pressure in time (hydrostatic profile, eq. 5 on page 194):
eqns = Dict{Symbol, Any}()
eqns[:p_time_rate] = (
consts, eqns, params, p, T) -> -eqns.ρ(p, T) * consts.g * params.U#2 (generic function with 1 method)where ρ(p,T) is diagnosed from the ideal gas law:
eqns[:ρ] = (consts, p, T) -> p / consts.R_prime / T#5 (generic function with 1 method)- Evolution in time of droplet radius (diffusion of vapor in air, eq. 1 on page 193):
eqns[:r_time_rate] = (consts, eqns, S, T, p, r) -> eqns.σ(eqns, S, T, p) / r#8 (generic function with 1 method)where σ is defined as (eq. 2 on page 193):
eqns[:σ] = (
consts, eqns, S, T, p) -> begin
Fk, Fd = eqns.calculate_fk_fd(eqns, T, p)
return (S - 1.0) / (Fk + Fd)
end
eqns[:calculate_fk_fd] = (consts,
eqns,
T,
p) -> begin
D = consts.diffusion_constant * T / p *
(consts.temp_393K / (T + consts.temp_120K)) *
(T / (consts.temp_273K))^(3/2)
K = consts.convection_constant * (consts.temp_393K / (T + consts.temp_120K)) *
(T / (consts.temp_273K))^(3/2)
Fk = consts.L^2 * consts.ε * consts.ρ_l / (K * consts.R_prime * T^2)
Fd = consts.R_prime * T * consts.ρ_l / (consts.ε * D * eqns.es(T))
(Fk, Fd)
end#14 (generic function with 1 method)- Evolution in time of liquid water mixing ratio (time derivative of eq. 4):
eqns[:ksi_time_rate] = (consts, params, r,
r_time_rate) -> 4.0 * π * consts.ρ_l * params.n_0 * r^2 * r_time_rate#17 (generic function with 1 method)where $n_0$ is a constant parameter expressing the ratio of droplet number per mass of air.
- Evolution in time of temperature (derived from eq. 6):
eqns[:T_time_rate] = (consts,
T,
dp_dt,
p,
dksi_dt) -> T * consts.R_prime / consts.cp * dp_dt / p +
consts.L / consts.cp * dksi_dt#20 (generic function with 1 method)- Evolution of saturation ratio (eq. 10):
eqns[:S_time_rate] = (consts, eqns, params, p, T,
dksi_dt)->begin
Q1, Q2 = eqns.calculate_q1_q2(eqns, T, p)
return Q1 * params.U - eqns.ρ(p, T) * Q2 * dksi_dt
end#23 (generic function with 1 method)where coefficients Q1 and Q2 are calculated as:
eqns[:calculate_q1_q2] = (consts,
eqns,
T,
p) -> begin
Q1 = consts.L * consts.g * consts.ε /
(consts.R_prime * consts.cp * T^2) - consts.g / (consts.R_prime * T)
Q2 = consts.R_prime * T / (consts.ε * eqns.es(T)) +
consts.ε * consts.L^2 / (consts.cp * T * p)
(Q1, Q2)
end
eqns[:es] = (
consts, T) -> consts.eq_vapor_pressure * exp(-(consts.temp_eq_vapor) / T)#29 (generic function with 1 method)Addidionally we should define additional equations mentioned in the article such as proportionality factor G:
eqns[:G] = (
consts, eqns, T, p) -> begin
Q1, Q2 = eqns.calculate_q1_q2(eqns, T, p)
Fk, Fd = eqns.calculate_fk_fd(eqns, T, p)
Q1 / Q2 * (Fk + Fd) / (4 * pi * consts.ρ_l * eqns.ρ(p, T))
end#32 (generic function with 1 method)which is used to define the limiting supersaturation (s=S-1, see eq. 16):
eqns[:s_inf] = (consts, eqns, T, p, r, params) -> begin
G_p_T = eqns.G(eqns, T, p)
return G_p_T * params.U / r / params.n_0
end#35 (generic function with 1 method)As a first step in our calculation we will create a NamedTuple from the dictionary 'eqns' declared above.
eqns = (; eqns...)(p_time_rate = Main.var"#2#3"(), s_inf = Main.var"#35#36"(), ksi_time_rate = Main.var"#17#18"(), σ = Main.var"#11#12"(), calculate_fk_fd = Main.var"#14#15"(), es = Main.var"#29#30"(), calculate_q1_q2 = Main.var"#26#27"(), S_time_rate = Main.var"#23#24"(), T_time_rate = Main.var"#20#21"(), ρ = Main.var"#5#6"(), G = Main.var"#32#33"(), r_time_rate = Main.var"#8#9"())Testing dimensional correctness with Unitful.jl
We defined our constants using units from the article. To be sure our algorithm works well with units using Unitful.jl package. First, let's set first argument in our equations as constants with units.
mapvalues(f, nt::NamedTuple) = NamedTuple{keys(nt)}(map(f, values(nt)))
eqns_u = mapvalues(f -> ((args...) -> f(consts_u, args...)), eqns)(p_time_rate = Main.var"#41#42"{Main.var"#2#3"}(Main.var"#2#3"()), s_inf = Main.var"#41#42"{Main.var"#35#36"}(Main.var"#35#36"()), ksi_time_rate = Main.var"#41#42"{Main.var"#17#18"}(Main.var"#17#18"()), σ = Main.var"#41#42"{Main.var"#11#12"}(Main.var"#11#12"()), calculate_fk_fd = Main.var"#41#42"{Main.var"#14#15"}(Main.var"#14#15"()), es = Main.var"#41#42"{Main.var"#29#30"}(Main.var"#29#30"()), calculate_q1_q2 = Main.var"#41#42"{Main.var"#26#27"}(Main.var"#26#27"()), S_time_rate = Main.var"#41#42"{Main.var"#23#24"}(Main.var"#23#24"()), T_time_rate = Main.var"#41#42"{Main.var"#20#21"}(Main.var"#20#21"()), ρ = Main.var"#41#42"{Main.var"#5#6"}(Main.var"#5#6"()), G = Main.var"#41#42"{Main.var"#32#33"}(Main.var"#32#33"()), r_time_rate = Main.var"#41#42"{Main.var"#8#9"}(Main.var"#8#9"()))Now we can test whether calculated density of air for some test values is within expected range.
Test.@test 1 * Unitful.u"kg/(m^3)" <
eqns_u.ρ(1000 * Unitful.u"hPa", 300 * Unitful.u"K") <
1.5 * Unitful.u"kg/(m^3)"Test PassedThe contour plot below shows the model's proportionality factor G over a range of temperatures and pressures for visual comparison with Figure 7 in the paper.
p_values = range(400 * Unitful.u"mbar", 1000 * Unitful.u"mbar", length = 50)
T_values = range(240 * Unitful.u"K", 300 * Unitful.u"K", length = 50)
Z = [uconvert(Unitful.u"s/g", eqns_u.G(eqns_u, T, p)*10^4)
for p in p_values, T in T_values]
plt7 = Plots.contour(T_values, p_values, Z,
xlabel = "T", ylabel = "p", yflip = true,
title = "Function G(p,T) isolines")
plot(plt7)Finally, it is possible to define a function that calculates the next step in time of state vector u using differential equations shown above.
@enum vars p=1 T=2 S=3 r=4
function rhs!(du_dt, u, arguments, t)
params = arguments.p
f = arguments.f
du_dt[Int(p)] = f.p_time_rate(f, params, u[Int(p)], u[Int(T)])
du_dt[Int(r)] = f.r_time_rate(f, u[Int(S)], u[Int(T)], u[Int(p)], u[Int(r)])
dksi_dt = f.ksi_time_rate(params, u[Int(r)], du_dt[Int(r)])
du_dt[Int(T)] = f.T_time_rate(u[Int(T)], du_dt[Int(p)], u[Int(p)], dksi_dt)
du_dt[Int(S)] = f.S_time_rate(f, params, u[Int(p)], u[Int(T)], dksi_dt)
endrhs! (generic function with 1 method)To begin our simulation we have to define our initial conditions and parameters of the simulation.
u0 = Vector{Quantity{Float64}}(undef, 4)
u0[Int(p)] = uconvert(Unitful.u"Pa", 800 * Unitful.u"mbar")
u0[Int(T)] = uconvert(Unitful.u"K", 7.0 * Unitful.u"°C")
u0[Int(S)] = 1.0 * Unitful.u"1"
u0[Int(r)] = uconvert(Unitful.u"m", 8 * Unitful.u"µm")
params = let
U = 10Unitful.u"m/s"
rho_0 = u0[Int(p)] / consts_u.R_prime / u0[Int(T)]
n_0 = uconvert(Unitful.m^-3, 200Unitful.u"1/cm^3") / rho_0
(U = U, rho_0 = rho_0, n_0 = n_0)
end(U = 10 m s^-1, rho_0 = 0.9948034211207581 kg Pa J^-1, n_0 = 2.01044744875e8 J kg^-1 m^-3 Pa^-1)We can now check the units of all four rates at saturation and in supersaturated air. The latter case exercises droplet growth.
@testset "Unitful RHS" begin
for saturation in (1.0, 1.001)
state = copy(u0)
state[Int(S)] = saturation
rates = similar(state)
rhs!(rates, state, (p = params, f = eqns_u), 0.0u"s")
rate_units = (u"Pa/s", u"K/s", u"s^-1", u"m/s")
@test all(isfinite, ustrip.(rate_units, rates))
@test saturation == 1.0 ? iszero(rates[Int(r)]) : rates[Int(r)] > 0u"m/s"
end
@test eqns_u.s_inf(eqns_u, u0[Int(T)], u0[Int(p)], u0[Int(r)], params) > 0
endTest.DefaultTestSet("Unitful RHS", false, true, false, "cloud_microphysics_with_Unitful.md", 1.796257890156783e9, Random.Xoshiro(0xd1b13e4af70b4529, 0xbe0623ed00348c3f, 0xddae7b78cb1a363c, 0xda03f0b33a40c3f5, 0x5647a85486f154c5), 5, 1.796257891660188e9, 0x01, ReentrantLock(), Any[])Numerical solution using ODEProblem
For this solve, we represent the state and parameters as numerical values in SI units and use seconds for time. We strip the units from the SI constants to build the corresponding equation tuple, then check that both models give the same saturation vapor pressure. The Buck correlation for liquid water gives about 1002 Pa at 7 °C. Buck Research Instruments, Appendix 1
consts_n = map(Unitful.ustrip, consts_u)
eqns_n = mapvalues(f -> ((args...) -> f(consts_n, args...)), eqns)
@testset "Saturation vapor pressure" begin
temperature = 280.15u"K"
pressure_pa = ustrip(u"Pa", eqns_u.es(temperature))
pressure_si = eqns_n.es(ustrip(u"K", temperature))
buck_pa = 611.21 * exp((18.678 - 7 / 234.5) * 7 / (257.14 + 7))
@test pressure_pa ≈ pressure_si
@test isapprox(pressure_pa, buck_pa; rtol = 0.02)
endTest.DefaultTestSet("Saturation vapor pressure", false, true, false, "cloud_microphysics_with_Unitful.md", 1.79625789215882e9, Random.Xoshiro(0xd1b13e4af70b4529, 0xbe0623ed00348c3f, 0xddae7b78cb1a363c, 0xda03f0b33a40c3f5, 0x5647a85486f154c5), 2, 1.796257892226852e9, 0x01, ReentrantLock(), Any[])We convert Unitful parameters to SI before stripping their units. Plain numeric parameters are interpreted as SI values. We pack the converted parameters and the equations into a NamedTuple for ODEProblem.
strip_si(unit, value) = ustrip(unit, value)
strip_si(unit, value::Real) = value
function simulate(;
time_range = (0.0, 20.0), initial_saturation = 1, parameters = params, solver_options...
)
initial_state = Unitful.ustrip.(u0)
initial_state[Int(S)] = initial_saturation
params_si = (
U = strip_si(u"m/s", parameters.U),
rho_0 = strip_si(u"kg/m^3", parameters.rho_0),
n_0 = strip_si(u"kg^-1", parameters.n_0),
)
arguments = (p = params_si, f = eqns_n)
return (
DE.solve(DE.ODEProblem(rhs!, initial_state, time_range, arguments); saveat = 0.1, solver_options...),
arguments.p)
end
solution, params_si = simulate()Test.DefaultTestSet("SI simulation parameters", false, true, false, "cloud_microphysics_with_Unitful.md", 1.796257901406528e9, Random.Xoshiro(0xd1b13e4af70b4529, 0xbe0623ed00348c3f, 0xddae7b78cb1a363c, 0xda03f0b33a40c3f5, 0x5647a85486f154c5), 3, 1.796257910229754e9, 0x01, ReentrantLock(), Any[])Figure 1 — Droplet Growth and Supersaturation
Now we can recreate Fig.1 from the article depicting profiles of pressure and radius.
Plots.plot(solution.t, solution[Int(r), :] .* 1e6,
ylabel = "Radius " * LaTeXStrings.L" [\mathrm{µm}]",
xlabel = "Time t [s]",
label = "r(t)")
Plots.plot!(twiny(), solution[Int(p), :] ./ 100, solution[Int(r), :] .* 1e6,
xflip = true,
label = false,
xlabel = "Pressure [mbar]"
)
Plots.plot!(twinx(), solution.t, (solution[Int(S), :] .- 1) .* 100,
ylabel = "Supersaturation [%]",
color = :red,
xticks = :none,
label = "S(t)"
)Figure 2 — Liquid Water Content
Figure 2 gives the liquid water mixing ratio in grams per kilogram of air. Each spherical droplet has volume $4\pi r^3/3$. The initial radius of 8 µm and concentration of 200 droplets/cm³ give about 0.43117 g/kg.
ksi = 1000 * (4π / 3) * consts_n.ρ_l * params_si.n_0 * (solution[Int(r), :] .^ 3)
Plots.plot(solution.t, solution[Int(T), :],
ylabel = "Temperature T [K]",
xlabel = "Time t [s]",
label = "T(t)",
legend = :left
)
Plots.plot!(twiny(), solution[Int(p), :] ./ 100, solution[Int(T), :],
xflip = true,
label = false,
xlabel = "Pressure [mbar]"
)
Plots.plot!(twinx(), solution.t, ksi,
ylabel = "Liquid water mixing ratio [g/kg]",
color = :red,
xticks = :none,
label = "x(t)",
legend = :right
)Figure 3 and 4 - saturation ratio and radius initial conditions
To recreate Figre 3 and Figure 4, it is necessary to change the initial saturation ratio.
plt3 = plot(title = "Supersaturation for different initial conditions",
xlabel = "Time t [s]", ylabel = "S-1 [%]", framestyle = :box)
plt4 = plot(title = "Radius for different initial conditions", xlabel = "Time t [s]",
ylabel = "Radius "*L" [\mathrm{µm}]", framestyle = :box)
for saturation in [1.02, 1.01, 1.0, 0.99, 0.98]
solution2,_ = simulate(initial_saturation=saturation, time_range=(0, 30))
Plots.plot!(plt3, solution2.t, (solution2[Int(S), :] .- 1)*100,
label = "S(t=0) = $(round((saturation-1)*100,digits=2)) %"
)
Plots.plot!(plt4, solution2.t, solution2[Int(r), :] .* 1e6,
label = "S(t=0) = $(round((saturation-1)*100, digits = 2)) %"
)
end
plot(plt3)plot(plt4)Figure 5 - impact of the initial speed
For Figure 5, we need to change parameters of out simulation so we need to create new NamedTuple with our parameters each time.
plt = Plots.plot(title = "Supersaturation for different updraft speed",
xlabel = "Time t [s]",
ylabel = "S-1 [%]"
)
for u in [1.0 * Unitful.u"m/s", 5.0 * Unitful.u"m/s", 15.0 * Unitful.u"m/s", 10.0*Unitful.u"m/s"]
parameters = merge(params, (U = u,))
solution3, _ = simulate(; parameters)
Plots.plot!(plt, solution3.t, (solution3[Int(S), :] .- 1)*100,
label = "U = $(u) "*LaTeXStrings.L" [\frac{\mathrm{m}}{\mathrm{s}}]"
)
end
plot(plt)Figure 6 - impact of initial droplet concentration
Figure 6 compares the approach to limiting supersaturation at several initial droplet concentrations. The logarithm is plotted where the residual ratio is positive and finite. A dotted vertical line marks the first saved time where it is undefined for a given concentration. Near the zero crossing, the residual is a small difference of supersaturations. These solves use a tighter relative tolerance and scale each absolute tolerance by the magnitude of the corresponding initial state component.
plt = Plots.plot(title = "Supersaturation at different droplet concentrations",
xlabel = "Time t [s]",
ylabel = LaTeXStrings.L"\ln\left(\frac{s_\infty(t)-s(t)}{s_\infty(t)-s(0)}\right)",
ylims = (-4, 0.5)
)
taus = Dict(50.0 => 5.45, 100.0 => 3.17, 500.0 => 0.73, 200.0 => 1.73)
colors = Dict(50.0 => "red", 100.0 => "green", 500.0 => "blue", 200.0 => "deeppink")
abstol = 1e-10 .* abs.(Unitful.ustrip.(u0))
for n in [50.0, 100.0, 500.0, 200.0]
n_0 = uconvert(u"m^-3", n * u"cm^-3") / params.rho_0
parameters = merge(params, (; n_0))
solution4, concentration_params = simulate(;
time_range = (0.0, 6.0), parameters, reltol = 1e-9, abstol,
)
s_infty = eqns_n.s_inf.(Ref(eqns_n), solution4[Int(T), :], solution4[Int(p), :],
solution4[Int(r), :], Ref(concentration_params))
s = solution4[Int(S), :] .- 1
residual = s_infty .- s
ratio = residual ./ (s_infty .- s[1])
log_ratio = map(ratio) do value
isfinite(value) && value > 0 ? log(value) : NaN
end
Plots.plot!(plt, solution4.t, log_ratio,
label = LaTeXStrings.L"n_0 = "*"$(n) "*LaTeXStrings.L" [\mathrm{cm}^{-3}]",
color = colors[n]
)
first_undefined = findfirst(isnan, log_ratio)
if first_undefined !== nothing
Plots.vline!(plt, [solution4.t[first_undefined]];
color = colors[n], linestyle = :dot, label = "")
end
exponential = -solution4.t ./ taus[n]
Plots.plot!(plt, solution4.t, exponential;
label = "",
linestyle = :dash,
color = colors[n]
)
end
plot(plt)Reference
R.R. Rogers (1975), An Elementary Parcel Model with Explicit Condensation and Supersaturation, Atmosphere, Vol. 13, No. 4, pp. 192–204. DOI: 10.1080/00046973.1975.9648397