1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//!Functions to calculate potential temperature of dry air in K.

use float_cmp::approx_eq;
use crate::Float;
use crate::{
    constants::{C_P, R_D},
    errors::InputError,
};

#[cfg(feature="debug")]
use floccus_proc::logerr;

///Formula for computing potential temperature of dry air from temperature, pressure and vapour pressure.
///
///Provided by R. Davies-Jones (2009) [(doi:10.1175/2009MWR2774.1)](https://doi.org/10.1175/2009MWR2774.1)
///
///# Errors
///
///Returns [`InputError::OutOfRange`] when one of inputs is out of range.\
///Valid `temperature` range: 253K - 324K\
///Valid `pressure` range: 100Pa - 150000Pa\
///Valid `vapour_pressure` range: 0Pa - 10000Pa
///
///Returns [`InputError::IncorrectArgumentSet`] when `pressure` and `vapour_pressure` are equal,
///in which case division by 0 occurs.
///
///Returns [`InputError::IncorrectArgumentSet`] when `pressure` is lower than `vapour_pressure`,
///in which case floating-point exponentation of negative number occurs.
#[cfg_attr(feature = "debug", logerr)]
pub fn davies_jones1(
    temperature: Float,
    pressure: Float,
    vapour_pressure: Float,
) -> Result<Float, InputError> {
    if !(253.0..=324.0).contains(&temperature) {
        return Err(InputError::OutOfRange(String::from("temperature")));
    }

    if !(100.0..=150_000.0).contains(&pressure) {
        return Err(InputError::OutOfRange(String::from("pressure")));
    }

    if !(0.0..=10_000.0).contains(&vapour_pressure) {
        return Err(InputError::OutOfRange(String::from("vapour_pressure")));
    }

    if approx_eq!(Float, pressure, vapour_pressure, ulps = 2) {
        return Err(InputError::IncorrectArgumentSet(String::from(
            "pressure and vapour_pressure cannot be equal",
        )));
    }

    if vapour_pressure > pressure {
        return Err(InputError::IncorrectArgumentSet(String::from(
            "vapour_pressure cannot be higher than pressure",
        )));
    }

    let kappa = R_D / C_P;

    let result = temperature * (100_000.0 / (pressure - vapour_pressure)).powf(kappa);

    Ok(result)
}

#[cfg(test)]
mod tests {
    use crate::{
        tests_framework::{self, Argument},
        potential_temperature,
    };

    #[test]
    fn davies_jones1() {
        assert!(tests_framework::test_with_3args(
            &potential_temperature::davies_jones1,
            Argument {
                name: "temperature",
                def_val: 300.0,
                range: [253.0, 324.0]
            },
            Argument {
                name: "pressure",
                def_val: 101325.0,
                range: [100.0, 150_000.0]
            },
            Argument {
                name: "vapour_pressure",
                def_val: 3000.0,
                range: [0.0, 10_000.0]
            },
            301.45136519081666
        ));
    }
}