meas: backfill tests for CurrentLoop

This commit is contained in:
2022-08-05 17:35:55 -07:00
parent 06379ffd30
commit 7d1ee0ad50
2 changed files with 198 additions and 0 deletions

View File

@@ -86,6 +86,15 @@ impl Measurement {
Dim(v) => format!("{}x{}x{}{}", v.x(), v.y(), v.z(), self.unit), Dim(v) => format!("{}x{}x{}{}", v.x(), v.y(), v.z(), self.unit),
} }
} }
/// retrieve the float value of this measurement -- if it's of float type.
/// useful for tests
pub fn get_float(&self) -> Option<f32> {
match self.value {
MeasurementValue::Float(f) => Some(f),
_ => None,
}
}
} }
impl<S> AbstractMeasurement<S> for Measurement { impl<S> AbstractMeasurement<S> for Measurement {
@@ -665,3 +674,181 @@ impl<S: AbstractSim> AbstractMeasurement<S> for Power {
] ]
} }
} }
#[cfg(test)]
pub mod test {
use super::*;
use crate::cross::mat::AnisomorphicConductor;
use crate::cross::step::SimMeta;
use crate::geom::Index;
use crate::sim::{Fields, GenericSim, StaticSim};
use crate::stim::AbstractStimulus;
struct MockSim {
e_field: Vec3<f32>,
dim: Vec3u,
feature_size: f32,
mat: AnisomorphicConductor<f32>,
}
impl AbstractSim for MockSim {
type Real = f32;
type Material = AnisomorphicConductor<f32>;
fn meta(&self) -> SimMeta<f32> {
SimMeta::new(self.dim, self.feature_size, 1e-9)
}
fn step_no(&self) -> u64 {
unimplemented!()
}
fn fields_at_index(&self, _pos: Index) -> Fields<Self::Real> {
Fields::new(self.e_field, Vec3::zero(), Vec3::zero())
}
fn get_material_index(&self, _at: Index) -> &Self::Material {
&self.mat
}
fn put_material_index(&mut self, _at: Index, _m: Self::Material) {
unimplemented!()
}
fn step_multiple<S: AbstractStimulus>(&mut self, _num_steps: u32, _s: &S) {
unimplemented!()
}
fn to_static(&self) -> StaticSim {
unimplemented!()
}
fn to_generic(&self) -> GenericSim<Self::Real> {
unimplemented!()
}
}
#[derive(Clone, Serialize, Deserialize)]
struct MockRegion {
normal: Vec3<f32>,
}
impl HasCrossSection for MockRegion {
fn cross_section_normal(&self, _p: Meters) -> Vec3<f32> {
self.normal
}
}
#[typetag::serde]
impl Region for MockRegion {
fn contains(&self, _p: Meters) -> bool {
true
}
}
#[test]
fn current_loop_trivial() {
let sim = MockSim {
e_field: Vec3::new(1.0, 0.0, 0.0),
dim: Vec3u::new(1, 1, 1),
feature_size: 1.0,
mat: AnisomorphicConductor::new(Vec3::new(1.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(1.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// measured area is 1 m^2
// region cross-section is 1 m^2
// conductivity is 1 S/m
assert_eq!(kv[0].get_float().unwrap(), 1.0);
}
#[test]
fn current_loop_multi_cell() {
let sim = MockSim {
e_field: Vec3::new(1.0, 0.0, 0.0),
dim: Vec3u::new(4, 4, 4),
feature_size: 0.25,
mat: AnisomorphicConductor::new(Vec3::new(1.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(1.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// measured area is 1 m^2
// region cross-section is 1 m^2
// conductivity is 1 S/m
assert_eq!(kv[0].get_float().unwrap(), 1.0);
}
#[test]
fn current_loop_off_conductor() {
let sim = MockSim {
e_field: Vec3::new(1.0, 1.0, 1.0),
dim: Vec3u::new(4, 4, 4),
feature_size: 0.25,
mat: AnisomorphicConductor::new(Vec3::new(0.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(1.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// material is not conductive in the direction being queried
assert_eq!(kv[0].get_float().unwrap(), 0.0);
}
#[test]
fn current_loop_e_field() {
let sim = MockSim {
e_field: Vec3::new(4.0, 2.0, 1.0),
dim: Vec3u::new(4, 4, 4),
feature_size: 0.25,
mat: AnisomorphicConductor::new(Vec3::new(1.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(1.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// measured area is 1 m^2
// region cross-section is 1 m^2
// conductivity is 1 S/m
// e field is 4 V/m
assert_eq!(kv[0].get_float().unwrap(), 4.0);
}
#[test]
fn current_loop_conductivity() {
let sim = MockSim {
e_field: Vec3::new(4.0, 2.0, 1.0),
dim: Vec3u::new(4, 4, 4),
feature_size: 0.25,
mat: AnisomorphicConductor::new(Vec3::new(3.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(1.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// measured area is 1 m^2
// region cross-section is 1 m^2
// conductivity is 3 S/m
// e field is 4 V/m
assert_eq!(kv[0].get_float().unwrap(), 3.0*4.0);
}
#[test]
fn current_loop_cross_section() {
let sim = MockSim {
e_field: Vec3::new(4.0, 2.0, 1.0),
dim: Vec3u::new(4, 4, 4),
feature_size: 0.5,
mat: AnisomorphicConductor::new(Vec3::new(3.0, 1.0, 1.0)),
};
let region = MockRegion {
normal: Vec3::new(16.0, 0.0, 0.0),
};
let kv = CurrentLoop::new("test", region).key_value(&sim);
assert_eq!(kv.len(), 1);
// measured area is 2 m^2
// region cross-section is 16 m^2
// conductivity is 3 S/m
// e field is 4 V/m
assert_eq!(kv[0].get_float().unwrap(), 3.0*4.0*16.0);
}
}

View File

@@ -20,6 +20,17 @@ pub struct SimMeta<R> {
pub feature_size: R, pub feature_size: R,
} }
impl<R: Real> SimMeta<R> {
pub fn new(dim: Vec3u, feature_size: R, time_step: R) -> Self {
Self {
dim,
inv_feature_size: feature_size.inv(),
time_step,
feature_size
}
}
}
impl<R: Copy> SimMeta<R> { impl<R: Copy> SimMeta<R> {
pub fn dim(&self) -> Vec3u { pub fn dim(&self) -> Vec3u {
self.dim self.dim