use crate::points::Point2;
use crate::{MagnetError, M4_PI};
pub fn sheet_field(x: &f64, y: &f64, h: &f64, kr: &f64) -> Result<Point2, MagnetError> {
let x_sq = x.powi(2);
let y_sq = y.powi(2);
let h_sq = h.powi(2);
let prefac = kr / M4_PI;
let y_plus_h = y + h;
let y_minus_h = y - h;
let bx = prefac * ((x_sq + y_minus_h.powi(2)) / (x_sq + y_plus_h.powi(2))).ln();
let by = 2.0 * prefac * (2.0 * h * x).atan2(x_sq + y_sq - h_sq);
Ok(Point2::new(bx, by))
}
#[cfg(test)]
mod tests {
use crate::magnets::sheet_field;
use crate::points::Point2;
use crate::PI;
#[test]
fn test_mid_point() {
let point = Point2::new(0.0, 0.0);
let h = 1.0;
let kr = 1.0;
let field = sheet_field(&point.x, &point.y, &h, &kr).unwrap();
let comp_field = Point2::new(0.0, 0.5);
assert_eq!(field, comp_field);
}
#[test]
fn test_offset_right() {
let point = Point2::new(0.5, 0.0);
let h = 1.0;
let kr = 1.0;
let field = sheet_field(&point.x, &point.y, &h, &kr).unwrap();
let comp_field = Point2::new(0.0, 2.0_f64.atan2(1.0) / PI);
assert_eq!(field, comp_field);
}
}