use serde::{Deserialize, Serialize};
use crate::validation::{validate_field, ValidationError};
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Foundation {
pub foundation_depth: Option<f64>,
pub foundation_length: Option<f64>,
pub foundation_width: Option<f64>,
pub foundation_area: Option<f64>,
pub base_tilt_angle: Option<f64>,
pub slope_angle: Option<f64>,
pub effective_length: Option<f64>,
pub effective_width: Option<f64>,
pub surface_friction_coefficient: Option<f64>,
}
impl Foundation {
pub fn new(
depth: Option<f64>,
length: Option<f64>,
width: Option<f64>,
angle: Option<f64>,
slope: Option<f64>,
area: Option<f64>,
surface_friction_coefficient: Option<f64>,
) -> Self {
Self {
foundation_depth: depth,
foundation_length: length,
foundation_width: width,
foundation_area: area,
base_tilt_angle: angle,
slope_angle: slope,
effective_length: None,
effective_width: None,
surface_friction_coefficient,
}
}
pub fn calc_effective_lengths(&mut self, ex: f64, ey: f64) {
let b_ = self.foundation_width.unwrap() - 2.0 * ex;
let l_ = self.foundation_length.unwrap() - 2.0 * ey;
self.effective_width = Some(f64::min(b_, l_).max(0.0));
self.effective_length = Some(f64::max(b_, l_).max(0.0));
}
pub fn validate(&self, fields: &[&str]) -> Result<(), ValidationError> {
for &field in fields {
let result = match field {
"foundation_depth" => validate_field(
"foundation_depth",
self.foundation_depth,
Some(0.0),
None,
"foundation",
),
"foundation_length" => validate_field(
"foundation_length",
self.foundation_length,
Some(0.0001),
None,
"foundation",
),
"foundation_width" => validate_field(
"foundation_width",
self.foundation_width,
Some(0.001),
self.foundation_length,
"foundation",
),
"foundation_area" => validate_field(
"foundation_area",
self.foundation_area,
Some(0.001),
None,
"foundation",
),
"base_tilt_angle" => validate_field(
"base_tilt_angle",
self.base_tilt_angle,
Some(0.0),
Some(45.0),
"foundation",
),
"slope_angle" => validate_field(
"slope_angle",
self.slope_angle,
Some(0.0),
Some(90.0),
"foundation",
),
"effective_width" => validate_field(
"effective_width",
self.effective_width,
Some(0.0),
None,
"foundation",
),
"effective_length" => validate_field(
"effective_length",
self.effective_length,
Some(0.0),
None,
"foundation",
),
"surface_friction_coefficient" => validate_field(
"surface_friction_coefficient",
self.surface_friction_coefficient,
Some(0.0),
Some(1.0),
"foundation",
),
unknown => Err(ValidationError {
code: "foundation.invalid_field".into(),
message: format!("Field '{}' is not valid for Foundation.", unknown),
}),
};
result?; }
Ok(())
}
}