#![forbid(unsafe_code)]
mod chemistry_3d;
mod error;
mod gcm;
mod radiative_transfer;
pub use chemistry_3d::Tracer3D;
pub use error::ClimateError;
pub use gcm::AtmosphereGcm;
pub use radiative_transfer::{Band, CorrelatedKBand, CorrelatedKRt, MultiBandRadiativeTransfer};
pub const SIGMA: f64 = tpt_sci_units::STEFAN_BOLTZMANN.value();
pub const CO2_PREINDUSTRIAL: f64 = 280.0;
#[derive(Debug, Clone)]
pub struct EnergyBalanceModel {
pub heat_capacity: f64,
pub albedo: f64,
pub emissivity: f64,
pub solar_constant: f64,
pub co2: f64,
pub t: f64,
}
impl EnergyBalanceModel {
pub fn new(
heat_capacity: f64,
albedo: f64,
emissivity: f64,
co2: f64,
) -> Result<Self, ClimateError> {
if heat_capacity <= 0.0 {
return Err(ClimateError::InvalidModel(
"heat capacity must be > 0".into(),
));
}
if !(0.0..=1.0).contains(&albedo) {
return Err(ClimateError::InvalidModel("albedo must be in [0,1]".into()));
}
if emissivity <= 0.0 {
return Err(ClimateError::InvalidModel("emissivity must be > 0".into()));
}
if co2 <= 0.0 {
return Err(ClimateError::InvalidModel("CO2 must be > 0".into()));
}
Ok(Self {
heat_capacity,
albedo,
emissivity,
solar_constant: 1361.0,
co2,
t: 288.0,
})
}
#[must_use]
pub fn forcing(&self) -> f64 {
5.35 * (self.co2 / CO2_PREINDUSTRIAL).ln()
}
#[must_use]
pub fn net_flux(&self) -> f64 {
(1.0 - self.albedo) * self.solar_constant / 4.0 - self.emissivity * SIGMA * self.t.powi(4)
+ self.forcing()
}
#[must_use]
pub fn temperature(&self) -> f64 {
self.t
}
pub fn step(&mut self, dt: f64) {
let f = self.net_flux();
let damping = self.heat_capacity + 4.0 * dt * self.emissivity * SIGMA * self.t.powi(3);
self.t += dt * f / damping;
if self.t < 0.0 {
self.t = 0.0;
}
}
#[must_use]
pub fn equilibrium_temperature(&self) -> f64 {
let mut t = self.t.max(1.0);
for _ in 0..50 {
let f = (1.0 - self.albedo) * self.solar_constant / 4.0
- self.emissivity * SIGMA * t.powi(4)
+ self.forcing();
let df = -4.0 * self.emissivity * SIGMA * t.powi(3);
t = (t - f / df).max(1.0);
}
t
}
}
pub fn grey_radiative_transfer(s: f64, alpha: f64, eps: f64) -> Result<f64, ClimateError> {
if s.is_nan() || s <= 0.0 {
return Err(ClimateError::InvalidModel("S must be > 0".into()));
}
if !(0.0..=1.0).contains(&alpha) {
return Err(ClimateError::InvalidModel(
"albedo must be in [0, 1]".into(),
));
}
if !(0.0..1.0).contains(&eps) {
return Err(ClimateError::InvalidModel("eps must be in [0, 1)".into()));
}
Ok(((s * (1.0 - alpha)) / (4.0 * SIGMA * (1.0 - eps / 2.0))).powf(0.25))
}
#[derive(Debug, Clone)]
pub struct ChemistryBox {
pub concentration: f64,
pub production: f64,
pub loss: f64,
}
impl ChemistryBox {
pub fn new(concentration: f64, production: f64, loss: f64) -> Result<Self, ClimateError> {
if concentration < 0.0 {
return Err(ClimateError::InvalidModel(
"concentration must be >= 0".into(),
));
}
if loss < 0.0 {
return Err(ClimateError::InvalidModel("loss must be >= 0".into()));
}
Ok(Self {
concentration,
production,
loss,
})
}
#[must_use]
pub fn steady_state(&self) -> f64 {
if self.loss > 0.0 {
self.production / self.loss
} else {
f64::INFINITY
}
}
pub fn step(&mut self, dt: f64) {
self.concentration += dt * (self.production - self.loss * self.concentration);
if self.concentration < 0.0 {
self.concentration = 0.0;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_abs_diff_eq;
#[test]
fn ebm_relaxes_to_equilibrium() {
let mut ebm = EnergyBalanceModel::new(1.0e7, 0.3, 0.61, 280.0).unwrap();
for _ in 0..2000 {
ebm.step(1.0);
}
let teq = ebm.equilibrium_temperature();
assert!(teq.is_finite() && teq > 250.0);
let ebm2 = EnergyBalanceModel::new(1.0e7, 0.3, 0.61, 560.0).unwrap();
let teq2 = ebm2.equilibrium_temperature();
assert!(teq2 > teq, "doubling CO2 should warm the EBM");
}
#[test]
fn grey_transfer_monotonic_in_greenhouse() {
let clear = grey_radiative_transfer(1361.0, 0.3, 0.0).unwrap();
let greenhouse = grey_radiative_transfer(1361.0, 0.3, 0.5).unwrap();
assert!(greenhouse > clear);
}
#[test]
fn chemistry_reaches_steady_state() {
let mut c = ChemistryBox::new(0.0, 1.0, 0.1).unwrap();
for _ in 0..1000 {
c.step(0.1);
}
assert_abs_diff_eq!(c.concentration, c.steady_state(), epsilon = 1e-3);
}
}