#[cfg(feature = "gpu")]
mod hargreaves_gpu;
#[cfg(feature = "gpu")]
mod mc_et0_gpu;
#[cfg(feature = "gpu")]
mod seasonal_gpu;
#[cfg(feature = "gpu")]
pub mod gpu;
#[cfg(feature = "gpu")]
pub use gpu::{
Fao56BaseInputs, Fao56Uncertainties, HargreavesBatchGpu, McEt0PropagateGpu, SeasonalGpuParams,
SeasonalGpuParamsBuilder, SeasonalOutput, SeasonalPipelineF64,
};
const HARGREAVES_COEFF: f64 = 0.0023;
const HARGREAVES_TEMP_OFFSET: f64 = 17.8;
#[must_use]
pub fn hargreaves_et0(ra: f64, t_max: f64, t_min: f64) -> Option<f64> {
let delta = t_max - t_min;
if delta < 0.0 || ra < 0.0 {
return None;
}
let t_mean = (t_max + t_min) * 0.5;
Some(HARGREAVES_COEFF * ra * (t_mean + HARGREAVES_TEMP_OFFSET) * delta.sqrt())
}
#[must_use]
pub fn hargreaves_et0_batch(ra: &[f64], t_max: &[f64], t_min: &[f64]) -> Option<Vec<f64>> {
if ra.len() != t_max.len() || ra.len() != t_min.len() {
return None;
}
ra.iter()
.zip(t_max)
.zip(t_min)
.map(|((&r, &tx), &tn)| hargreaves_et0(r, tx, tn))
.collect()
}
#[must_use]
pub fn crop_coefficient(kc_prev: f64, kc_next: f64, day_in_stage: u32, stage_length: u32) -> f64 {
if stage_length == 0 {
return kc_prev;
}
let frac = f64::from(day_in_stage) / f64::from(stage_length);
(kc_next - kc_prev).mul_add(frac, kc_prev)
}
#[must_use]
pub fn soil_water_balance(
theta: f64,
precip: f64,
irrigation: f64,
et_c: f64,
field_capacity: f64,
) -> f64 {
let raw = theta + precip + irrigation - et_c;
raw.clamp(0.0, field_capacity)
}
#[must_use]
pub fn thornthwaite_et0(
t_mean: f64,
heat_index: f64,
daylight_hours: f64,
days_in_month: f64,
) -> Option<f64> {
if heat_index <= 0.0 || t_mean < 0.0 {
return None;
}
let a = 1.792e-2f64.mul_add(
heat_index,
6.75e-7f64.mul_add(heat_index.powi(3), -(7.71e-5 * heat_index.powi(2))),
) + 0.49239;
let et_unadj = 16.0 * (10.0 * t_mean / heat_index).powf(a);
Some(et_unadj * (daylight_hours / 12.0) * (days_in_month / 30.0))
}
#[must_use]
pub fn thornthwaite_heat_index(monthly_temps: &[f64; 12]) -> f64 {
monthly_temps
.iter()
.filter(|&&t| t > 0.0)
.map(|&t| (t / 5.0).powf(1.514))
.sum()
}
#[must_use]
pub fn makkink_et0(t_mean: f64, rs: f64) -> Option<f64> {
if rs < 0.0 {
return None;
}
let e_tmean = 0.6108 * (17.27 * t_mean / (t_mean + 237.3)).exp();
let delta = 4098.0 * e_tmean / (t_mean + 237.3).powi(2);
let gamma = 0.0674;
let lambda = 2.45;
let et0 = (0.61 * (delta / (delta + gamma))).mul_add(rs / lambda, -0.12);
Some(et0.max(0.0))
}
#[must_use]
pub fn turc_et0(t_mean: f64, rs_mj: f64, rh_mean: f64) -> Option<f64> {
if rs_mj < 0.0 {
return None;
}
let rs_cal = rs_mj * 23.8846;
let base = 0.013 * (t_mean / (t_mean + 15.0)) * (rs_cal + 50.0);
let et0 = if rh_mean >= 50.0 {
base
} else {
base * (1.0 + (50.0 - rh_mean) / 70.0)
};
Some(et0.max(0.0))
}
#[must_use]
pub fn hamon_et0(t_mean: f64, daylight_hours: f64) -> Option<f64> {
if daylight_hours < 0.0 {
return None;
}
let d = daylight_hours / 12.0;
let es = 6.108 * (17.27 * t_mean / (t_mean + 237.3)).exp();
let et0 = 0.55 * d * d * es / 100.0;
Some(et0.max(0.0))
}
#[must_use]
pub fn fao56_et0(
t_max: f64,
t_min: f64,
rh_max: f64,
rh_min: f64,
wind_2m: f64,
rs: f64,
elevation: f64,
lat_deg: f64,
doy: u32,
) -> Option<f64> {
use std::f64::consts::PI;
if t_max < t_min || wind_2m < 0.0 || rs < 0.0 {
return None;
}
let tmean = (t_max + t_min) * 0.5;
let p = 101.3 * (0.0065f64.mul_add(-elevation, 293.0) / 293.0).powf(5.26);
let gamma = 0.000_665 * p;
let e_tmax = 0.6108 * (17.27 * t_max / (t_max + 237.3)).exp();
let e_tmin = 0.6108 * (17.27 * t_min / (t_min + 237.3)).exp();
let es = (e_tmax + e_tmin) * 0.5;
let ea = (e_tmin * rh_max / 100.0 + e_tmax * rh_min / 100.0) * 0.5;
let e_tmean = 0.6108 * (17.27 * tmean / (tmean + 237.3)).exp();
let delta = 4098.0 * e_tmean / (tmean + 237.3).powi(2);
let lat_rad = lat_deg.to_radians();
let dr = 0.033f64.mul_add((2.0 * PI * f64::from(doy) / 365.0).cos(), 1.0);
let decl = 0.409 * (2.0 * PI * f64::from(doy) / 365.0 - 1.39).sin();
let ws_arg = -lat_rad.tan() * decl.tan();
let ws = if ws_arg.abs() > 1.0 {
PI
} else {
ws_arg.acos()
};
let ra = 24.0 * 60.0 / PI
* 0.0820
* dr
* (ws * lat_rad.sin()).mul_add(decl.sin(), lat_rad.cos() * decl.cos() * ws.sin());
let rso = 0.000_02f64.mul_add(elevation, 0.75) * ra;
let rns = (1.0 - 0.23) * rs;
let sigma = 4.903e-9;
let tmax_k = t_max + 273.16;
let tmin_k = t_min + 273.16;
let rnl = sigma
* (tmax_k.powi(4) + tmin_k.powi(4))
* 0.5
* 0.14f64.mul_add(-ea.sqrt(), 0.34)
* (1.35 * rs / rso.max(0.001) - 0.35);
let rn = rns - rnl;
let numerator =
(0.408 * delta).mul_add(rn, gamma * 900.0 / (tmean + 273.0) * wind_2m * (es - ea));
let denominator = delta + gamma * 0.34f64.mul_add(wind_2m, 1.0);
Some(numerator / denominator)
}
#[must_use]
pub fn fao56_et0_with_ea(
t_max: f64,
t_min: f64,
actual_vapour_pressure: f64,
wind_2m: f64,
rs: f64,
elevation: f64,
lat_deg: f64,
doy: u32,
) -> Option<f64> {
use std::f64::consts::PI;
if t_max < t_min || wind_2m < 0.0 || rs < 0.0 {
return None;
}
let tmean = (t_max + t_min) * 0.5;
let p = 101.3 * (0.0065f64.mul_add(-elevation, 293.0) / 293.0).powf(5.26);
let gamma = 0.000_665 * p;
let e_tmax = 0.6108 * (17.27 * t_max / (t_max + 237.3)).exp();
let e_tmin = 0.6108 * (17.27 * t_min / (t_min + 237.3)).exp();
let es = (e_tmax + e_tmin) * 0.5;
let ea = actual_vapour_pressure;
let e_tmean = 0.6108 * (17.27 * tmean / (tmean + 237.3)).exp();
let delta = 4098.0 * e_tmean / (tmean + 237.3).powi(2);
let lat_rad = lat_deg.to_radians();
let dr = 0.033f64.mul_add((2.0 * PI * f64::from(doy) / 365.0).cos(), 1.0);
let decl = 0.409 * (2.0 * PI * f64::from(doy) / 365.0 - 1.39).sin();
let ws_arg = -lat_rad.tan() * decl.tan();
let ws = if ws_arg.abs() > 1.0 {
PI
} else {
ws_arg.acos()
};
let ra = 24.0 * 60.0 / PI
* 0.0820
* dr
* (ws * lat_rad.sin()).mul_add(decl.sin(), lat_rad.cos() * decl.cos() * ws.sin());
let rso = 0.000_02f64.mul_add(elevation, 0.75) * ra;
let rns = (1.0 - 0.23) * rs;
let sigma = 4.903e-9;
let tmax_k = t_max + 273.16;
let tmin_k = t_min + 273.16;
let rnl = sigma
* (tmax_k.powi(4) + tmin_k.powi(4))
* 0.5
* 0.14f64.mul_add(-ea.sqrt(), 0.34)
* (1.35 * rs / rso.max(0.001) - 0.35);
let rn = rns - rnl;
let numerator =
(0.408 * delta).mul_add(rn, gamma * 900.0 / (tmean + 273.0) * wind_2m * (es - ea));
let denominator = delta + gamma * 0.34f64.mul_add(wind_2m, 1.0);
Some(numerator / denominator)
}
#[must_use]
pub fn hamon_et0_brock(t_mean: f64, daylight_hours: f64) -> Option<f64> {
if daylight_hours < 0.0 {
return None;
}
let d_ratio = daylight_hours / 12.0;
let pt = 4.95 * (0.062 * t_mean).exp() / 100.0;
Some((13.97 * d_ratio * d_ratio * pt).max(0.0))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hargreaves_typical() {
let et0 = hargreaves_et0(35.0, 32.0, 18.0).unwrap();
assert!(et0 > 0.0 && et0 < 15.0, "ET₀={et0} out of range");
}
#[test]
fn test_hargreaves_zero_delta() {
let et0 = hargreaves_et0(35.0, 25.0, 25.0).unwrap();
assert!(et0.abs() < 1e-12, "zero ΔT → zero ET₀");
}
#[test]
fn test_hargreaves_invalid() {
assert!(hargreaves_et0(35.0, 18.0, 32.0).is_none());
assert!(hargreaves_et0(-1.0, 30.0, 20.0).is_none());
}
#[test]
fn test_hargreaves_batch() {
let ra = vec![30.0, 35.0, 40.0];
let tmax = vec![28.0, 32.0, 35.0];
let tmin = vec![15.0, 18.0, 20.0];
let et0 = hargreaves_et0_batch(&ra, &tmax, &tmin).unwrap();
assert_eq!(et0.len(), 3);
for &e in &et0 {
assert!(e > 0.0);
}
}
#[test]
fn test_hargreaves_batch_mismatched() {
assert!(hargreaves_et0_batch(&[1.0], &[2.0, 3.0], &[1.0]).is_none());
}
#[test]
fn test_crop_coefficient_start() {
assert!((crop_coefficient(0.3, 1.2, 0, 30) - 0.3).abs() < 1e-12);
}
#[test]
fn test_crop_coefficient_end() {
assert!((crop_coefficient(0.3, 1.2, 30, 30) - 1.2).abs() < 1e-12);
}
#[test]
fn test_crop_coefficient_mid() {
let kc = crop_coefficient(0.3, 1.2, 15, 30);
assert!((kc - 0.75).abs() < 1e-12);
}
#[test]
fn test_crop_coefficient_zero_stage() {
assert!((crop_coefficient(0.3, 1.2, 0, 0) - 0.3).abs() < 1e-12);
}
#[test]
fn test_soil_water_balance_basic() {
let theta = soil_water_balance(100.0, 10.0, 5.0, 8.0, 200.0);
assert!((theta - 107.0).abs() < 1e-12);
}
#[test]
fn test_soil_water_balance_saturated() {
let theta = soil_water_balance(190.0, 50.0, 0.0, 3.0, 200.0);
assert!((theta - 200.0).abs() < 1e-12);
}
#[test]
fn test_soil_water_balance_dry() {
let theta = soil_water_balance(5.0, 0.0, 0.0, 20.0, 200.0);
assert!(theta.abs() < 1e-12);
}
#[test]
fn test_fao56_et0_example18() {
let et0 = fao56_et0(21.5, 12.3, 84.0, 63.0, 2.78, 22.07, 100.0, 50.8, 187).unwrap();
assert!(
(et0 - 3.88).abs() < 0.15,
"FAO-56 Example 18: expected ~3.88, got {et0}"
);
}
#[test]
fn test_fao56_et0_invalid() {
assert!(fao56_et0(10.0, 20.0, 80.0, 50.0, 2.0, 15.0, 100.0, 45.0, 180).is_none());
}
#[test]
fn test_hargreaves_fao56_example() {
let et0 = hargreaves_et0(40.6, 26.6, 14.8).unwrap();
assert!(
(et0 - 12.35).abs() < 0.1,
"Hargreaves: expected ~12.35, got {et0}"
);
}
#[test]
fn test_thornthwaite_typical() {
let monthly = [
3.0, 4.0, 8.0, 12.0, 17.0, 21.0, 24.0, 23.0, 19.0, 13.0, 8.0, 4.0,
];
let hi = thornthwaite_heat_index(&monthly);
assert!(hi > 30.0 && hi < 80.0, "heat index {hi} out of range");
let et0 = thornthwaite_et0(21.0, hi, 14.5, 30.0).unwrap();
assert!(
et0 > 0.0 && et0 < 200.0,
"Thornthwaite ET₀={et0} out of range"
);
}
#[test]
fn test_thornthwaite_invalid() {
assert!(thornthwaite_et0(-5.0, 50.0, 12.0, 30.0).is_none());
assert!(thornthwaite_et0(20.0, 0.0, 12.0, 30.0).is_none());
}
#[test]
fn test_makkink_typical() {
let et0 = makkink_et0(20.0, 18.0).unwrap();
assert!(et0 > 0.0 && et0 < 10.0, "Makkink ET₀={et0} out of range");
}
#[test]
fn test_makkink_invalid() {
assert!(makkink_et0(20.0, -1.0).is_none());
}
#[test]
fn test_turc_typical() {
let et0_humid = turc_et0(20.0, 18.0, 70.0).unwrap();
let et0_dry = turc_et0(20.0, 18.0, 30.0).unwrap();
assert!(et0_humid > 0.0, "Turc humid ET₀ should be positive");
assert!(
et0_dry > et0_humid,
"Turc dry should exceed humid at same T/Rs"
);
}
#[test]
fn test_turc_invalid() {
assert!(turc_et0(20.0, -1.0, 50.0).is_none());
}
#[test]
fn test_hamon_typical() {
let et0 = hamon_et0(20.0, 14.0).unwrap();
assert!(et0 > 0.0 && et0 < 10.0, "Hamon ET₀={et0} out of range");
}
#[test]
fn test_hamon_invalid() {
assert!(hamon_et0(20.0, -1.0).is_none());
}
#[test]
fn test_hamon_cold() {
let et0 = hamon_et0(0.0, 10.0).unwrap();
assert!(
(0.0..1.0).contains(&et0),
"Hamon cold ET₀={et0} should be near zero"
);
}
#[test]
fn test_fao56_with_ea() {
let rh_et0 = fao56_et0(32.0, 18.0, 80.0, 40.0, 2.0, 20.0, 200.0, 35.0, 180).unwrap();
let ea_kpa = (0.6108 * (17.27 * 18.0 / (18.0 + 237.3_f64)).exp() * 80.0 / 100.0
+ 0.6108 * (17.27 * 32.0 / (32.0 + 237.3_f64)).exp() * 40.0 / 100.0)
* 0.5;
let ea_et0 = fao56_et0_with_ea(32.0, 18.0, ea_kpa, 2.0, 20.0, 200.0, 35.0, 180).unwrap();
assert!(
(rh_et0 - ea_et0).abs() < 0.01,
"fao56 with derived ea should match rh-based: {rh_et0} vs {ea_et0}"
);
}
#[test]
fn test_hamon_brock() {
let et0 = hamon_et0_brock(20.0, 14.0).unwrap();
assert!(
et0 > 0.0 && et0 < 10.0,
"Hamon-Brock ET₀={et0} out of range"
);
assert!(hamon_et0_brock(20.0, -1.0).is_none());
}
}