Skip to content

Commit b2ce3eb

Browse files
glwagnernavidcy
andauthored
Change AtmosphereThermodynamics to ThermodynamicConstants, plus more tests (#62)
* Add tests and change AtmosphereThermodynamics to ThermoydnamicConstants * fix tests * fix pressure solver tests * fix buoyancy tests * code alignment --------- Co-authored-by: Navid C. Constantinou <[email protected]>
1 parent 220a5ee commit b2ce3eb

16 files changed

+229
-79
lines changed

docs/src/microphysics/saturation_adjustment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ The saturation specific humidity is then
3434
using Breeze
3535
using Breeze.MoistAirBuoyancies: saturation_specific_humidity, HeightReferenceThermodynamicState
3636
37-
thermo = AtmosphereThermodynamics()
37+
thermo = ThermodynamicConstants()
3838
ref = ReferenceStateConstants(base_pressure=101325, potential_temperature=288)
3939
4040
z = 0.0 # [m] height

docs/src/thermodynamics.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
```@setup thermo
44
using Breeze
5-
thermo = AtmosphereThermodynamics()
5+
thermo = ThermodynamicConstants()
66
```
77

88
Breeze implements thermodynamic relations for moist atmospheres --
@@ -148,7 +148,7 @@ using Breeze
148148
using Breeze.Thermodynamics: reference_pressure, reference_density
149149
using CairoMakie
150150
151-
thermo = AtmosphereThermodynamics()
151+
thermo = ThermodynamicConstants()
152152
constants = ReferenceStateConstants(base_pressure=101325, potential_temperature=288)
153153
grid = RectilinearGrid(size=160, z=(0, 12_000), topology=(Flat, Flat, Bounded))
154154
@@ -212,7 +212,7 @@ Central to Breeze's implementation of moist thermodynamics is a struct that
212212
holds parameters like the molar gas constant and molar masses,
213213

214214
```@example thermo
215-
thermo = AtmosphereThermodynamics()
215+
thermo = ThermodynamicConstants()
216216
```
217217

218218
The default parameter evince basic facts about water vapor air typical to Earth's atmosphere:
@@ -298,7 +298,7 @@ The saturation vapor pressure is
298298
using Breeze
299299
using Breeze.Thermodynamics: saturation_vapor_pressure
300300
301-
thermo = AtmosphereThermodynamics()
301+
thermo = ThermodynamicConstants()
302302
303303
T = collect(200:0.1:320)
304304
pᵛˡ⁺ = [saturation_vapor_pressure(Tⁱ, thermo, thermo.liquid) for Tⁱ in T]
@@ -327,7 +327,7 @@ and this is what it looks like:
327327
using Breeze
328328
using Breeze.MoistAirBuoyancies: saturation_specific_humidity
329329
330-
thermo = AtmosphereThermodynamics()
330+
thermo = ThermodynamicConstants()
331331
ref = ReferenceStateConstants(base_pressure=101325, potential_temperature=288)
332332
333333
z = 0

examples/JRA55_saturation_specific_humidity.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using JLD2
22
using Breeze
3-
using Breeze: saturation_specific_humidity, AtmosphereThermodynamics
3+
using Breeze: saturation_specific_humidity, ThermodynamicConstants
44
using GLMakie
55

66
@load "JRA55_atmospheric_state_Jan_1_1991.jld2" q T p
77

8-
thermo = AtmosphereThermodynamics()
8+
thermo = ThermodynamicConstants()
99

1010
ρ = 1.2
1111
qᵛ★ = saturation_specific_humidity.(T, 1.2, Ref(thermo))

examples/large_yeager_saturation_specific_humidity.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using GLMakie
22
using Breeze
33

4-
thermo = Breeze.AtmosphereThermodynamics()
4+
thermo = Breeze.ThermodynamicConstants()
55

66
saturation_specific_humidity_large_yeager(T, ρ) = 640380 * exp(-5107.4 / T) / ρ
77

src/AtmosphereModels/anelastic_formulation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ..Thermodynamics:
2-
AtmosphereThermodynamics,
2+
ThermodynamicConstants,
33
ReferenceStateConstants,
44
reference_pressure,
55
reference_density,

src/AtmosphereModels/atmosphere_model.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using ..Thermodynamics:
2-
AtmosphereThermodynamics,
2+
ThermodynamicConstants,
33
ReferenceStateConstants,
44
reference_pressure,
55
reference_density,
@@ -69,7 +69,7 @@ end
6969
"""
7070
AtmosphereModel(grid;
7171
clock = Clock(grid),
72-
thermodynamics = AtmosphereThermodynamics(eltype(grid)),
72+
thermodynamics = ThermodynamicConstants(eltype(grid)),
7373
formulation = default_formulation(grid, thermodynamics),
7474
absolute_humidity = DefaultValue(),
7575
tracers = tuple(),
@@ -109,7 +109,7 @@ Pauluis, O. (2008). Thermodynamic consistency of the anelastic approximation for
109109
"""
110110
function AtmosphereModel(grid;
111111
clock = Clock(grid),
112-
thermodynamics = AtmosphereThermodynamics(eltype(grid)),
112+
thermodynamics = ThermodynamicConstants(eltype(grid)),
113113
formulation = default_formulation(grid, thermodynamics),
114114
absolute_humidity = DefaultValue(),
115115
tracers = tuple(),

src/Breeze.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Breeze
77

88
export
99
MoistAirBuoyancy,
10-
AtmosphereThermodynamics,
10+
ThermodynamicConstants,
1111
ReferenceStateConstants,
1212
AnelasticFormulation,
1313
AtmosphereModel,

src/MoistAirBuoyancies.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Oceananigans.BuoyancyFormulations: AbstractBuoyancyFormulation,
1919
required_tracers
2020

2121
using ..Thermodynamics:
22-
AtmosphereThermodynamics,
22+
ThermodynamicConstants,
2323
ReferenceStateConstants,
2424
mixture_heat_capacity,
2525
mixture_gas_constant,
@@ -38,7 +38,7 @@ end
3838

3939
"""
4040
MoistAirBuoyancy(FT=Oceananigans.defaults.FloatType;
41-
thermodynamics = AtmosphereThermodynamics(FT),
41+
thermodynamics = ThermodynamicConstants(FT),
4242
reference_constants = ReferenceStateConstants{FT}(101325, 290))
4343
4444
Return a MoistAirBuoyancy formulation that can be provided as input to an `AtmosphereModel`
@@ -56,7 +56,7 @@ julia> using Breeze, Oceananigans
5656
julia> buoyancy = MoistAirBuoyancy()
5757
MoistAirBuoyancy
5858
├── reference_constants: Breeze.Thermodynamics.ReferenceStateConstants{Float64}
59-
└── thermodynamics: AtmosphereThermodynamics
59+
└── thermodynamics: ThermodynamicConstants
6060
6161
julia> model = NonhydrostaticModel(; grid = RectilinearGrid(size=(8, 8, 8), extent=(1, 2, 3)),
6262
buoyancy, tracers = (:θ, :q))
@@ -71,7 +71,7 @@ NonhydrostaticModel{CPU, RectilinearGrid}(time = 0 seconds, iteration = 0)
7171
```
7272
"""
7373
function MoistAirBuoyancy(FT=Oceananigans.defaults.FloatType;
74-
thermodynamics = AtmosphereThermodynamics(FT),
74+
thermodynamics = ThermodynamicConstants(FT),
7575
reference_constants = ReferenceStateConstants{FT}(101325, 290))
7676

7777
AT = typeof(thermodynamics)

src/Thermodynamics/Thermodynamics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Thermodynamics
22

3-
export AtmosphereThermodynamics, ReferenceStateConstants, IdealGas,
3+
export ThermodynamicConstants, ReferenceStateConstants, IdealGas,
44
PhaseTransitionConstants, CondensedPhase,
55
mixture_gas_constant, mixture_heat_capacity
66

src/Thermodynamics/atmosphere_thermodynamics.jl

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ end
9191
liquid_water(FT) = CondensedPhase(FT; latent_heat=2500800, heat_capacity=4181)
9292
water_ice(FT) = CondensedPhase(FT; latent_heat=2834000, heat_capacity=2108)
9393

94-
struct AtmosphereThermodynamics{FT, C, S}
94+
struct ThermodynamicConstants{FT, C, S}
9595
molar_gas_constant :: FT
9696
gravitational_acceleration :: FT
9797
energy_reference_temperature :: FT
@@ -103,9 +103,9 @@ struct AtmosphereThermodynamics{FT, C, S}
103103
solid :: S
104104
end
105105

106-
Base.summary(at::AtmosphereThermodynamics{FT}) where FT = "AtmosphereThermodynamics{$FT}"
106+
Base.summary(at::ThermodynamicConstants{FT}) where FT = "ThermodynamicConstants{$FT}"
107107

108-
function Base.show(io::IO, at::AtmosphereThermodynamics)
108+
function Base.show(io::IO, at::ThermodynamicConstants)
109109
print(io, summary(at), ":", '\n',
110110
"├── molar_gas_constant: ", at.molar_gas_constant, "\n",
111111
"├── gravitational_acceleration: ", at.gravitational_acceleration, "\n",
@@ -118,9 +118,9 @@ function Base.show(io::IO, at::AtmosphereThermodynamics)
118118
"└── solid: ", at.solid)
119119
end
120120

121-
Base.eltype(::AtmosphereThermodynamics{FT}) where FT = FT
121+
Base.eltype(::ThermodynamicConstants{FT}) where FT = FT
122122

123-
function Adapt.adapt_structure(to, thermo::AtmosphereThermodynamics)
123+
function Adapt.adapt_structure(to, thermo::ThermodynamicConstants)
124124
molar_gas_constant = adapt(to, thermo.molar_gas_constant)
125125
gravitational_acceleration = adapt(to, thermo.gravitational_acceleration)
126126
dry_air = adapt(to, thermo.dry_air)
@@ -133,38 +133,37 @@ function Adapt.adapt_structure(to, thermo::AtmosphereThermodynamics)
133133
FT = typeof(molar_gas_constant)
134134
C = typeof(liquid)
135135
S = typeof(solid)
136-
return AtmosphereThermodynamics{FT, C, S}(molar_gas_constant,
137-
gravitational_acceleration,
138-
energy_reference_temperature,
139-
triple_point_temperature,
140-
triple_point_pressure,
141-
dry_air,
142-
vapor,
143-
liquid,
144-
solid)
136+
return ThermodynamicConstants{FT, C, S}(molar_gas_constant,
137+
gravitational_acceleration,
138+
energy_reference_temperature,
139+
triple_point_temperature,
140+
triple_point_pressure,
141+
dry_air,
142+
vapor,
143+
liquid,
144+
solid)
145145
end
146146

147147
"""
148-
AtmosphereThermodynamics(FT = Oceananigans.defaults.FloatType;
149-
gravitational_acceleration = 9.81,
150-
molar_gas_constant = 8.314462618,
151-
energy_reference_temperature = 273.16,
152-
triple_point_temperature = 273.16,
153-
triple_point_pressure = 611.657,
154-
dry_air_molar_mass = 0.02897,
155-
dry_air_heat_capacity = 1005,
156-
vapor_molar_mass = 0.018015,
157-
vapor_heat_capacity = 1850,
158-
liquid = liquid_water(FT),
159-
solid = water_ice(FT),
160-
condensed_phases = nothing)
161-
162-
Create `AtmosphereThermodynamics` with parameters that represent gaseous mixture of dry "air"
148+
ThermodynamicConstants(FT = Oceananigans.defaults.FloatType;
149+
molar_gas_constant = 8.314462618,
150+
gravitational_acceleration = 9.81,
151+
energy_reference_temperature = 273.16,
152+
triple_point_temperature = 273.16,
153+
triple_point_pressure = 611.657,
154+
dry_air_molar_mass = 0.02897,
155+
dry_air_heat_capacity = 1005,
156+
vapor_molar_mass = 0.018015,
157+
vapor_heat_capacity = 1850,
158+
liquid = liquid_water(FT),
159+
solid = water_ice(FT))
160+
161+
Create `ThermodynamicConstants` with parameters that represent gaseous mixture of dry "air"
163162
and vapor, as well as condensed liquid and solid phases.
164163
The `triple_point_temperature` and `triple_point_pressure` may be combined with
165164
internal energy parameters for condensed phases to compute the vapor pressure
166165
at the boundary between vapor and a homogeneous sample of the condensed phase.
167-
The `gravitational_acceleration` parameter is included to compute reference_state
166+
The `gravitational_acceleration` parameter is included to compute `reference_state`
168167
quantities associated with hydrostatic balance.
169168
170169
The Clausius-Clapeyron relation describes the pressure-temperature relationship during phase
@@ -198,43 +197,43 @@ Note: any reference values for pressure and temperature can be used in principle
198197
The advantage of using reference values at the triple point is that the same values
199198
can then be used for both condensation (vapor → liquid) and deposition (vapor → ice).
200199
"""
201-
function AtmosphereThermodynamics(FT = Oceananigans.defaults.FloatType;
202-
molar_gas_constant = 8.314462618,
203-
gravitational_acceleration = 9.81,
204-
energy_reference_temperature = 273.16,
205-
triple_point_temperature = 273.16,
206-
triple_point_pressure = 611.657,
207-
dry_air_molar_mass = 0.02897,
208-
dry_air_heat_capacity = 1005,
209-
vapor_molar_mass = 0.018015,
210-
vapor_heat_capacity = 1850,
211-
liquid = liquid_water(FT),
212-
solid = water_ice(FT))
200+
function ThermodynamicConstants(FT = Oceananigans.defaults.FloatType;
201+
molar_gas_constant = 8.314462618,
202+
gravitational_acceleration = 9.81,
203+
energy_reference_temperature = 273.16,
204+
triple_point_temperature = 273.16,
205+
triple_point_pressure = 611.657,
206+
dry_air_molar_mass = 0.02897,
207+
dry_air_heat_capacity = 1005,
208+
vapor_molar_mass = 0.018015,
209+
vapor_heat_capacity = 1850,
210+
liquid = liquid_water(FT),
211+
solid = water_ice(FT))
213212

214213
dry_air = IdealGas(FT; molar_mass = dry_air_molar_mass,
215214
heat_capacity = dry_air_heat_capacity)
216215

217216
vapor = IdealGas(FT; molar_mass = vapor_molar_mass,
218217
heat_capacity = vapor_heat_capacity)
219218

220-
return AtmosphereThermodynamics(convert(FT, molar_gas_constant),
221-
convert(FT, gravitational_acceleration),
222-
convert(FT, energy_reference_temperature),
223-
convert(FT, triple_point_temperature),
224-
convert(FT, triple_point_pressure),
225-
dry_air,
226-
vapor,
227-
liquid,
228-
solid)
219+
return ThermodynamicConstants(convert(FT, molar_gas_constant),
220+
convert(FT, gravitational_acceleration),
221+
convert(FT, energy_reference_temperature),
222+
convert(FT, triple_point_temperature),
223+
convert(FT, triple_point_pressure),
224+
dry_air,
225+
vapor,
226+
liquid,
227+
solid)
229228
end
230229

231-
const AT = AtmosphereThermodynamics
230+
const TC = ThermodynamicConstants
232231
const IG = IdealGas
233232

234-
@inline vapor_gas_constant(thermo::AT) = thermo.molar_gas_constant / thermo.vapor.molar_mass
235-
@inline dry_air_gas_constant(thermo::AT) = thermo.molar_gas_constant / thermo.dry_air.molar_mass
233+
@inline vapor_gas_constant(thermo::TC) = thermo.molar_gas_constant / thermo.vapor.molar_mass
234+
@inline dry_air_gas_constant(thermo::TC) = thermo.molar_gas_constant / thermo.dry_air.molar_mass
236235

237-
const NonCondensingAtmosphereThermodynamics{FT} = AtmosphereThermodynamics{FT, Nothing, Nothing}
236+
const NonCondensingThermodynamicConstants{FT} = ThermodynamicConstants{FT, Nothing, Nothing}
238237

239238
"""
240239
mixture_gas_constant(q, thermo)
@@ -256,12 +255,12 @@ where:
256255
257256
# Arguments
258257
- `q`: Specific humidity (dimensionless)
259-
- `thermo`: `AtmosphereThermodynamics` instance containing gas constants
258+
- `thermo`: `ThermodynamicConstants` instance containing gas constants
260259
261260
# Returns
262261
- Gas constant of the moist air mixture in J/(kg·K)
263262
"""
264-
@inline function mixture_gas_constant(q, thermo::AT)
263+
@inline function mixture_gas_constant(q, thermo::TC)
265264
Rᵈ = dry_air_gas_constant(thermo)
266265
Rᵛ = vapor_gas_constant(thermo)
267266
return Rᵈ * (1 - q) + Rᵛ * q
@@ -274,7 +273,7 @@ Compute the heat capacity of state air given the total specific humidity q
274273
and assuming that condensate mass ratio qᶜ ≪ q, where qℓ is the mass ratio of
275274
liquid condensate.
276275
"""
277-
@inline function mixture_heat_capacity(q, thermo::AT)
276+
@inline function mixture_heat_capacity(q, thermo::TC)
278277
cᵖᵈ = thermo.dry_air.heat_capacity
279278
cᵖᵛ = thermo.vapor.heat_capacity
280279
return cᵖᵈ * (1 - q) + cᵖᵛ * q

0 commit comments

Comments
 (0)