Skip to content

Commit

Permalink
Use Thermodynamics.jl for reference profiles
Browse files Browse the repository at this point in the history
Undo T_freeze changes

Re-apply T_freeze mod, fix more constructors
  • Loading branch information
charleskawczynski committed Aug 9, 2021
1 parent c71343c commit 575f049
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Thermodynamics = "b60c26fb-14c3-4610-9d3e-2d17fe7ff00c"
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
7 changes: 5 additions & 2 deletions docs/src/ReferenceStates.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ for case_name in (
"life_cycle_Tan2018",
"Soares",
"Rico",
"TRMM_LBA",
"ARM_SGP",
"GATE_III",
"DYCOMS_RF01",
"GABLS",
"SP",
"DryBubble",
)
export_ref_profile(case_name)
end;
# Note: temperatures in this case become extremely low.
CLIMAParameters.Planet.T_freeze(::EarthParameterSet) = 100.0
export_ref_profile("TRMM_LBA")
export_ref_profile("GATE_III")
```

## Bomex
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/GATE_III.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ include(joinpath("utils", "generate_namelist.jl"))
include(joinpath("utils", "compute_mse.jl"))
using .NameList

# Note: temperatures in this case become extremely low.
CLIMAParameters.Planet.T_freeze(::EarthParameterSet) = 100.0

@testset "GATE_III" begin
println("Running GATE_III...")
namelist = default_namelist("GATE_III")
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/TRMM_LBA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ include(joinpath("utils", "generate_namelist.jl"))
include(joinpath("utils", "compute_mse.jl"))
using .NameList

# Note: temperatures in this case become extremely low.
CLIMAParameters.Planet.T_freeze(::EarthParameterSet) = 100.0

best_mse = OrderedDict()
best_mse["qt_mean"] = 1.7430285968163504e+00
best_mse["updraft_area"] = 3.2545401197014067e+04
Expand Down
66 changes: 28 additions & 38 deletions src/ReferenceState.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,33 @@ specific initialization function defined in Initialization.pyx
"""
function initialize(self::ReferenceState, grid::Grid, Stats::NetCDFIO_Stats)

self.sg = t_to_entropy_c(self.Pg, self.Tg, self.qtg, 0.0, 0.0)
FT = eltype(grid)
P_g = self.Pg
T_g = self.Tg
q_tot_g = self.qtg
param_set = parameter_set(self)
q_pt_g = TD.PhasePartition(q_tot_g)
ts_g = TD.PhaseEquil_pTq(param_set, P_g, T_g, q_tot_g)
θ_liq_ice_g = TD.liquid_ice_pottemp(ts_g)

# We are integrating the log pressure so need to take the log of the
# surface pressure
logp = log(P_g)

# Form a right hand side for integrating the hydrostatic equation to
# determine the reference pressure

function rhs(p, u, z)
ret = eos(exp(p), self.qtg, self.sg; t_to_prog = t_to_entropy_c, prog_to_t = eos_first_guess_entropy)
q_i = 0.0
q_l = ret.ql
T = ret.T
return -g / (Rd * T * (1.0 - self.qtg + eps_vi * (self.qtg - q_l - q_i)))
function rhs(logp, u, z)
p_ = exp(logp)
ts = TD.PhaseEquil_pθq(param_set, p_, θ_liq_ice_g, q_tot_g)
R_m = TD.gas_constant_air(ts)
T = TD.air_temperature(ts)
return -FT(CPP.grav(param_set)) / (T * R_m)
end

# We are integrating the log pressure so need to take the log of the
# surface pressure
p0 = log(self.Pg)
logp = p0

p = face_field(grid)
p_half = center_field(grid)

# Perform the integration
# TODO: replace with OrdinaryDiffEq

z_span = (grid.zmin, grid.zmax)
@show z_span
prob = ODEProblem(rhs, logp, z_span)
Expand Down Expand Up @@ -109,35 +112,22 @@ function initialize(self::ReferenceState, grid::Grid, Stats::NetCDFIO_Stats)
qv_half = center_field(grid)

# Compute reference state thermodynamic profiles

@inbounds for k in center_indicies(grid)
ret = eos(p_half_[k], self.qtg, self.sg; t_to_prog = t_to_entropy_c, prog_to_t = eos_first_guess_entropy)
temperature_half[k] = ret.T
ql_half[k] = ret.ql
qv_half[k] = self.qtg - (ql_half[k] + qi_half[k])
alpha_half[k] = alpha_c(p_half_[k], temperature_half[k], self.qtg, qv_half[k])
ts = TD.PhaseEquil_pθq(param_set, p_half_[k], θ_liq_ice_g, q_tot_g)
temperature_half[k] = TD.air_temperature(ts)
ql_half[k] = TD.liquid_specific_humidity(ts)
qv_half[k] = TD.vapor_specific_humidity(ts)
alpha_half[k] = TD.specific_volume(ts)
end

@inbounds for k in face_indicies(grid)
ret = eos(p_[k], self.qtg, self.sg; t_to_prog = t_to_entropy_c, prog_to_t = eos_first_guess_entropy)
temperature[k] = ret.T
ql[k] = ret.ql
qv[k] = self.qtg - (ql[k] + qi[k])
alpha[k] = alpha_c(p_[k], temperature[k], self.qtg, qv[k])
end

# Now do a sanity check to make sure that the Reference State entropy profile is uniform following
# saturation adjustment
local s
@inbounds for k in center_indicies(grid)
s = t_to_entropy_c(p_half[k], temperature_half[k], self.qtg, ql_half[k], qi_half[k])
if abs(s - self.sg) / self.sg > 0.01
println("Error in reference profiles entropy not constant !")
println("Likely error in saturation adjustment")
end
ts = TD.PhaseEquil_pθq(param_set, p_[k], θ_liq_ice_g, q_tot_g)
temperature[k] = TD.air_temperature(ts)
ql[k] = TD.liquid_specific_humidity(ts)
qv[k] = TD.vapor_specific_humidity(ts)
alpha[k] = TD.specific_volume(ts)
end


self.alpha0_half = alpha_half
self.alpha0 = alpha
self.p0 = p_
Expand Down
6 changes: 5 additions & 1 deletion src/TurbulenceConvection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ using LinearAlgebra
import Dierckx
import Statistics
import LambertW
import Thermodynamics
import Distributions
import FastGaussQuadrature

import CLIMAParameters

const TD = Thermodynamics

const CP = CLIMAParameters
const CPP = CP.Planet
const APS = CP.AbstractEarthParameterSet

const CPMP = CP.Atmos.Microphysics
Expand Down

0 comments on commit 575f049

Please sign in to comment.