Change meas.rs to use meter coordinates instead of pixel coordinates

This commit is contained in:
Colin 2020-09-26 14:01:10 -07:00
parent 729f9f77a6
commit 452141dd0d
3 changed files with 75 additions and 54 deletions

View File

@ -6,68 +6,71 @@ fn main() {
coremem::init_logging();
let feat_size = 5e-6; // feature size
let from_m = |m| (m/feat_size) as u32;
let to_um = |px| px * (feat_size*1e6) as u32;
let m_to_um = |px| (px * 1e6) as u32;
let to_m = |px| px as Flt * feat_size;
let width = from_m(2500e-6);
let conductor_inner_rad = from_m(0e-6);
let conductor_outer_rad = from_m(200e-6);
let ferro_inner_rad = from_m(220e-6);
let ferro_outer_rad = from_m(300e-6);
let buffer = from_m(200e-6);
let width = 2500e-6;
let conductor_inner_rad = 0e-6;
let conductor_outer_rad = 200e-6;
let ferro_inner_rad = 220e-6;
let ferro_outer_rad = 300e-6;
let buffer = 200e-6;
let peak_current = 100.0;
let conductivity = 1.0e5;
let half_width = width / 2;
let mut driver = Driver::new(width, width, feat_size);
let half_width = width * 0.5;
let width_px = from_m(width);
let mut driver = Driver::new(width_px, width_px, feat_size);
//driver.set_steps_per_frame(8);
//driver.set_steps_per_frame(40);
//driver.set_steps_per_frame(200);
driver.add_y4m_renderer(&*format!("toroid25d-flt{}-feat{}um-{}A--radii{}um-{}um-{}um.y4m",
std::mem::size_of::<Flt>() * 8,
(feat_size * 1e6) as u32,
m_to_um(feat_size),
peak_current as u32,
to_um(conductor_outer_rad),
to_um(ferro_inner_rad),
to_um(ferro_outer_rad),
m_to_um(conductor_outer_rad),
m_to_um(ferro_inner_rad),
m_to_um(ferro_outer_rad),
));
// driver.add_term_renderer();
driver.add_measurement(meas::Label(format!("Conductivity: {}, Imax: {:.2e}", conductivity, peak_current)));
driver.add_measurement(meas::Current(CylinderZ::new(Vec2::new(
to_m(half_width), to_m(half_width)),
to_m(conductor_outer_rad))));
driver.add_measurement(meas::Magnetization(
(half_width + ferro_inner_rad + 2, half_width, 0).into()
));
driver.add_measurement(meas::MagneticFlux(
(half_width + ferro_inner_rad + 2, half_width, 0).into()
));
driver.add_measurement(meas::MagneticStrength(
(half_width + ferro_inner_rad + 2, half_width, 0).into()
driver.add_measurement(meas::Current(CylinderZ::new(
Vec2::new(half_width, half_width),
conductor_outer_rad)
));
driver.add_measurement(meas::Magnetization(
(half_width + ferro_inner_rad + 1, half_width, 0).into()
(half_width + ferro_inner_rad + 2.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticFlux(
(half_width + ferro_inner_rad + 1, half_width, 0).into()
(half_width + ferro_inner_rad + 2.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticStrength(
(half_width + ferro_inner_rad + 1, half_width, 0).into()
(half_width + ferro_inner_rad + 2.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::Magnetization(
(half_width + (ferro_inner_rad + ferro_outer_rad) / 2, half_width, 0).into()
(half_width + ferro_inner_rad + 1.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticFlux(
(half_width + (ferro_inner_rad + ferro_outer_rad) / 2, half_width, 0).into()
(half_width + ferro_inner_rad + 1.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticStrength(
(half_width + (ferro_inner_rad + ferro_outer_rad) / 2, half_width, 0).into()
(half_width + ferro_inner_rad + 1.0*feat_size, half_width, 0.0).into()
));
driver.add_measurement(meas::Magnetization(
(half_width + 0.5 * (ferro_inner_rad + ferro_outer_rad), half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticFlux(
(half_width + 0.5 * (ferro_inner_rad + ferro_outer_rad), half_width, 0.0).into()
));
driver.add_measurement(meas::MagneticStrength(
(half_width + 0.5 * (ferro_inner_rad + ferro_outer_rad), half_width, 0.0).into()
));
let center = Vec2::new(half_width as _, half_width as _);
for y in 0..width {
for x in 0..width {
let loc = Coord::new(x, y, 0);
let d = Vec2::new(loc.x().into(), loc.y().into()) - center;
for y_px in 0..width_px {
for x_px in 0..width_px {
let loc = Coord::new(x_px, y_px, 0);
let d = Vec2::new(to_m(x_px), to_m(y_px)) - center;
let r = d.mag();
if (conductor_inner_rad as _..conductor_outer_rad as _).contains(&r) {
*driver.state.get_mut(loc).mat_mut() = mat::Static::conductor(conductivity).into();
@ -77,8 +80,8 @@ fn main() {
}
}
let boundary = half_width - ferro_outer_rad - buffer;
println!("boundary: {}", boundary);
driver.add_upml_boundary(boundary);
println!("boundary: {}um", m_to_um(boundary));
driver.add_upml_boundary(from_m(boundary));
loop {
// let drive_current = peak_current * match driver.state.step_no() {
@ -91,13 +94,13 @@ fn main() {
// I = \sigma*E*Area
// E = I / \sigma / Area
//let e = v/(2.0*feat_size);
let area = consts::PI*((to_m(conductor_outer_rad)*to_m(conductor_outer_rad) - to_m(conductor_inner_rad)*to_m(conductor_inner_rad)) as Flt);
let area = consts::PI*(conductor_outer_rad*conductor_outer_rad - conductor_inner_rad*conductor_inner_rad);
let e = drive_current/conductivity/area;
for y in half_width-conductor_outer_rad..half_width+conductor_outer_rad {
for x in half_width-conductor_outer_rad..half_width+conductor_outer_rad {
let loc = Coord::new(x, y, 0);
let d = Vec2::new(loc.x().into(), loc.y().into()) - center;
if (conductor_inner_rad as _..conductor_outer_rad as _).contains(&d.mag()) {
for y_px in from_m(half_width-conductor_outer_rad)..from_m(half_width+conductor_outer_rad) {
for x_px in from_m(half_width-conductor_outer_rad)..from_m(half_width+conductor_outer_rad) {
let loc = Coord::new(x_px, y_px, 0);
let d = Vec2::new(to_m(x_px), to_m(y_px)) - center;
if (conductor_inner_rad..conductor_outer_rad).contains(&d.mag()) {
driver.state.impulse_ez(loc, e);
}
}

View File

@ -1,4 +1,5 @@
use crate::coord::Coord;
use crate::flt::Flt;
use crate::geom::{Vec3, Region};
use crate::mat::Material as _;
use crate::sim::{Cell, GenericSim};
@ -57,42 +58,47 @@ impl<R: Region + Display + Sync> AbstractMeasurement for Current<R> {
}
}
fn loc(v: Vec3) -> String {
let (x, y, z): (Flt, Flt, Flt) = (v * 1e6).into();
format!("({}, {}, {}) um", x as i32, y as i32, z as i32)
}
/// M
pub struct Magnetization(pub Coord);
pub struct Magnetization(pub Vec3);
impl AbstractMeasurement for Magnetization {
fn eval(&self, state: &dyn GenericSim) -> String {
let m = state.get(self.0).mat().m();
format!("M{}: ({:.2e}, {:.2e}, {:.2e})", self.0, m.x(), m.y(), m.z())
let m = state.sample(self.0).mat().m();
format!("M{}: ({:.2e}, {:.2e}, {:.2e})", loc(self.0), m.x(), m.y(), m.z())
}
}
/// B
pub struct MagneticFlux(pub Coord);
pub struct MagneticFlux(pub Vec3);
impl AbstractMeasurement for MagneticFlux {
fn eval(&self, state: &dyn GenericSim) -> String {
let b = state.get(self.0).b();
format!("B{}: ({:.2e}, {:.2e}, {:.2e})", self.0, b.x(), b.y(), b.z())
let b = state.sample(self.0).b();
format!("B{}: ({:.2e}, {:.2e}, {:.2e})", loc(self.0), b.x(), b.y(), b.z())
}
}
/// H
pub struct MagneticStrength(pub Coord);
pub struct MagneticStrength(pub Vec3);
impl AbstractMeasurement for MagneticStrength {
fn eval(&self, state: &dyn GenericSim) -> String {
let h = state.get(self.0).h();
format!("H{}: ({:.2e}, {:.2e}, {:.2e})", self.0, h.x(), h.y(), h.z())
let h = state.sample(self.0).h();
format!("H{}: ({:.2e}, {:.2e}, {:.2e})", loc(self.0), h.x(), h.y(), h.z())
}
}
pub struct ElectricField(pub Coord);
pub struct ElectricField(pub Vec3);
impl AbstractMeasurement for ElectricField {
fn eval(&self, state: &dyn GenericSim) -> String {
let e = state.get(self.0).e();
format!("E{}: ({:.2e}, {:.2e}, {:.2e})", self.0, e.x(), e.y(), e.z())
let e = state.sample(self.0).e();
format!("E{}: ({:.2e}, {:.2e}, {:.2e})", loc(self.0), e.x(), e.y(), e.z())
}
}

View File

@ -179,6 +179,18 @@ impl Vec3 {
}
}
impl Into<(Flt, Flt, Flt)> for Vec3 {
fn into(self) -> (Flt, Flt, Flt) {
(self.x(), self.y(), self.z())
}
}
impl From<(Flt, Flt, Flt)> for Vec3 {
fn from((x, y, z): (Flt, Flt, Flt)) -> Self {
Self::new(x, y, z)
}
}
impl From<(Real, Real, Real)> for Vec3 {
fn from((x, y, z): (Real, Real, Real)) -> Self {
Self { x, y, z }