Skip to content

Commit

Permalink
feat: add distance_at_time function
Browse files Browse the repository at this point in the history
  • Loading branch information
calebissharp committed May 17, 2024
1 parent 28df885 commit dd94b31
Show file tree
Hide file tree
Showing 3 changed files with 2,912 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/fbp/distance_at_time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::f64::consts::E;

use super::FbpFuelType;

/// Calculate head spread distance of a fire at `time`
///
/// * `ros` - Rate of spread (m/min)
/// * `time` - Elapsed time (min)
/// * `cfb` - Crow fraction burned
pub fn distance_at_time(fuel_type: FbpFuelType, ros: f64, time: f64, cfb: f64) -> f64 {
let alpha = match fuel_type {
FbpFuelType::C1
| FbpFuelType::O1a
| FbpFuelType::O1b
| FbpFuelType::S1
| FbpFuelType::S2
| FbpFuelType::S3
| FbpFuelType::D1 => 0.115,
_ => 0.115 - 18.8 * cfb.powf(2.5) * E.powf(-8. * cfb),
};

ros * (time + E.powf(-alpha * time) / alpha - 1. / alpha)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test_util::precision_f64;

#[derive(Debug, serde::Deserialize)]
struct TestRow {
fuel_type: FbpFuelType,
ros: f64,
time: f64,
cfb: f64,
distance: f64,
}

#[test]
fn test_distance_at_time() -> Result<(), Box<dyn std::error::Error>> {
let fixture = std::fs::File::open("./tests/fixtures/distance_at_time.csv")?;
let mut rdr = csv::Reader::from_reader(fixture);

for result in rdr.deserialize() {
let record: TestRow = result?;
let distance = distance_at_time(record.fuel_type, record.ros, record.time, record.cfb);

assert_eq!(precision_f64(distance, 4), record.distance);
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/fbp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod crown_base_height;
mod crown_fraction_burned;
mod crown_fuel_consumption;
mod crown_fuel_load;
mod distance_at_time;
mod fire_intensity;
mod foliar_moisture_content;
mod length_to_breadth;
Expand All @@ -19,6 +20,7 @@ pub use crown_base_height::*;
pub use crown_fraction_burned::*;
pub use crown_fuel_consumption::*;
pub use crown_fuel_load::*;
pub use distance_at_time::*;
pub use fire_intensity::*;
pub use foliar_moisture_content::*;
pub use length_to_breadth::*;
Expand Down
Loading

0 comments on commit dd94b31

Please sign in to comment.