measurement: remove the eval
method
This commit is contained in:
@@ -5,7 +5,6 @@ use crate::sim::AbstractSim;
|
|||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub trait AbstractMeasurement<S>: Send + Sync {
|
pub trait AbstractMeasurement<S>: Send + Sync {
|
||||||
fn eval(&self, state: &S) -> String;
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement>;
|
fn key_value(&self, state: &S) -> Vec<Measurement>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,9 +87,6 @@ impl Measurement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S> AbstractMeasurement<S> for Measurement {
|
impl<S> AbstractMeasurement<S> for Measurement {
|
||||||
fn eval(&self, _state: &S) -> String {
|
|
||||||
format!("{}: {}", self.name, self.str_value())
|
|
||||||
}
|
|
||||||
fn key_value(&self, _state: &S) -> Vec<Measurement> {
|
fn key_value(&self, _state: &S) -> Vec<Measurement> {
|
||||||
vec![self.clone()]
|
vec![self.clone()]
|
||||||
}
|
}
|
||||||
@@ -173,9 +169,6 @@ impl SiScale {
|
|||||||
pub struct Time;
|
pub struct Time;
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Time {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Time {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
format!("{} (step {})", SiScale::format_short(state.time(), "s"), state.step_no())
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
vec![
|
vec![
|
||||||
Measurement::new_unitless("step", state.step_no()),
|
Measurement::new_unitless("step", state.step_no()),
|
||||||
@@ -188,9 +181,6 @@ impl<S: AbstractSim> AbstractMeasurement<S> for Time {
|
|||||||
pub struct Meta;
|
pub struct Meta;
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Meta {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Meta {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
format!("{}x{}x{} feat: {:.1e}m", state.width(), state.height(), state.depth(), state.feature_size())
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
vec![
|
vec![
|
||||||
Measurement::new_unitless("dim", state.size().0),
|
Measurement::new_unitless("dim", state.size().0),
|
||||||
@@ -221,12 +211,6 @@ impl Volume {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Volume {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Volume {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
format!("Vol({}): {:.2e} um^3",
|
|
||||||
self.name,
|
|
||||||
self.data(state),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
vec![
|
vec![
|
||||||
Measurement::new(&format!("Vol({})", self.name), self.data(state), "um^3"),
|
Measurement::new(&format!("Vol({})", self.name), self.data(state), "um^3"),
|
||||||
@@ -304,13 +288,6 @@ impl std::iter::Sum for FieldSamples<[FieldSample; 3]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Current {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Current {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let (mean_current_mag, mean_current_vec) = self.data(state);
|
|
||||||
format!("I/cell({}): {:.2e} {:.2e}",
|
|
||||||
self.name,
|
|
||||||
mean_current_mag,
|
|
||||||
mean_current_vec)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let (mean_current_mag, mean_current_vec) = self.data(state);
|
let (mean_current_mag, mean_current_vec) = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -320,7 +297,7 @@ impl<S: AbstractSim> AbstractMeasurement<S> for Current {
|
|||||||
"A",
|
"A",
|
||||||
),
|
),
|
||||||
Measurement::new(
|
Measurement::new(
|
||||||
&format!("/cell({})", self.name),
|
&format!("I/cell({})", self.name),
|
||||||
mean_current_vec,
|
mean_current_vec,
|
||||||
"A",
|
"A",
|
||||||
),
|
),
|
||||||
@@ -359,10 +336,6 @@ impl CurrentLoop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for CurrentLoop {
|
impl<S: AbstractSim> AbstractMeasurement<S> for CurrentLoop {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let cross_sectional_current = self.data(state);
|
|
||||||
format!("I({}): {:.2e}", self.name, cross_sectional_current)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let cross_sectional_current = self.data(state);
|
let cross_sectional_current = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -431,15 +404,6 @@ impl MagneticLoop {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticLoop {
|
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticLoop {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let (mean_directed_m, mean_directed_b, mean_directed_h) = self.data(state);
|
|
||||||
format!(
|
|
||||||
"M({}): {:.2e}; B({}): {:.2e}; H({}): {:.2e}",
|
|
||||||
self.name, mean_directed_m,
|
|
||||||
self.name, mean_directed_b,
|
|
||||||
self.name, mean_directed_h,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let (mean_directed_m, mean_directed_b, mean_directed_h) = self.data(state);
|
let (mean_directed_m, mean_directed_b, mean_directed_h) = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -476,10 +440,6 @@ impl MagneticFlux {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticFlux {
|
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticFlux {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let mean_mag = self.data(state);
|
|
||||||
format!("Bavg({}): {:.2e}", self.name, mean_mag)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let mean_mag = self.data(state);
|
let mean_mag = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -517,10 +477,6 @@ impl Magnetization {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Magnetization {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Magnetization {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let mean_mag = self.data(state);
|
|
||||||
format!("Mavg({}): {:.2e}", self.name, mean_mag)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let mean_mag = self.data(state);
|
let mean_mag = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -540,10 +496,6 @@ fn loc(v: Meters) -> String {
|
|||||||
pub struct MagnetizationAt(pub Meters);
|
pub struct MagnetizationAt(pub Meters);
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for MagnetizationAt {
|
impl<S: AbstractSim> AbstractMeasurement<S> for MagnetizationAt {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let m = state.sample(self.0).m();
|
|
||||||
format!("M{}: {:.2e}", loc(self.0), m)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let m = state.sample(self.0).m();
|
let m = state.sample(self.0).m();
|
||||||
vec![
|
vec![
|
||||||
@@ -557,10 +509,6 @@ impl<S: AbstractSim> AbstractMeasurement<S> for MagnetizationAt {
|
|||||||
pub struct MagneticFluxAt(pub Meters);
|
pub struct MagneticFluxAt(pub Meters);
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticFluxAt {
|
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticFluxAt {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let b = state.sample(self.0).b();
|
|
||||||
format!("B{}: {:.2e}", loc(self.0), b)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let b = state.sample(self.0).b();
|
let b = state.sample(self.0).b();
|
||||||
vec![
|
vec![
|
||||||
@@ -576,10 +524,6 @@ impl<S: AbstractSim> AbstractMeasurement<S> for MagneticFluxAt {
|
|||||||
pub struct MagneticStrengthAt(pub Meters);
|
pub struct MagneticStrengthAt(pub Meters);
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticStrengthAt {
|
impl<S: AbstractSim> AbstractMeasurement<S> for MagneticStrengthAt {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let h = state.sample(self.0).h();
|
|
||||||
format!("H{}: {:.2e}", loc(self.0), h)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let h = state.sample(self.0).h();
|
let h = state.sample(self.0).h();
|
||||||
vec![
|
vec![
|
||||||
@@ -594,10 +538,6 @@ impl<S: AbstractSim> AbstractMeasurement<S> for MagneticStrengthAt {
|
|||||||
pub struct ElectricField(pub Meters);
|
pub struct ElectricField(pub Meters);
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for ElectricField {
|
impl<S: AbstractSim> AbstractMeasurement<S> for ElectricField {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let e = state.sample(self.0).e();
|
|
||||||
format!("E{}: {}", loc(self.0), e)
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let e = state.sample(self.0).e();
|
let e = state.sample(self.0).e();
|
||||||
vec![
|
vec![
|
||||||
@@ -642,10 +582,6 @@ impl Energy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Energy {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Energy {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let e = self.data(state);
|
|
||||||
format!("U({}): {}", self.name, SiScale::format_short(e, "J"))
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let e = self.data(state);
|
let e = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
@@ -685,10 +621,6 @@ impl Power {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<S: AbstractSim> AbstractMeasurement<S> for Power {
|
impl<S: AbstractSim> AbstractMeasurement<S> for Power {
|
||||||
fn eval(&self, state: &S) -> String {
|
|
||||||
let power = self.data(state);
|
|
||||||
format!("P({}): {}", self.name, SiScale::format_short(power, "W"))
|
|
||||||
}
|
|
||||||
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
fn key_value(&self, state: &S) -> Vec<Measurement> {
|
||||||
let power = self.data(state);
|
let power = self.data(state);
|
||||||
vec![
|
vec![
|
||||||
|
@@ -294,8 +294,8 @@ impl<'a, S: AbstractSim> RenderSteps<'a, S> {
|
|||||||
self.im = im;
|
self.im = im;
|
||||||
}
|
}
|
||||||
fn render_measurements(&mut self) {
|
fn render_measurements(&mut self) {
|
||||||
for (meas_no, m) in self.meas.iter().enumerate() {
|
for (meas_no, m) in meas::eval_multiple_kv(self.sim, &self.meas).into_iter().enumerate() {
|
||||||
let meas_string = m.eval(self.sim);
|
let meas_string = m.str_value();
|
||||||
for (i, c) in meas_string.chars().enumerate() {
|
for (i, c) in meas_string.chars().enumerate() {
|
||||||
let glyph = BASIC_FONTS.get(c)
|
let glyph = BASIC_FONTS.get(c)
|
||||||
.or_else(|| GREEK_FONTS.get(c))
|
.or_else(|| GREEK_FONTS.get(c))
|
||||||
@@ -425,6 +425,7 @@ impl<S: AbstractSim> Renderer<S> for ColorTermRenderer {
|
|||||||
measurements: &[&dyn AbstractMeasurement<S>],
|
measurements: &[&dyn AbstractMeasurement<S>],
|
||||||
config: RenderConfig,
|
config: RenderConfig,
|
||||||
) {
|
) {
|
||||||
|
let measurements = meas::eval_multiple_kv(state, measurements);
|
||||||
let (max_w, mut max_h) = crossterm::terminal::size().unwrap();
|
let (max_w, mut max_h) = crossterm::terminal::size().unwrap();
|
||||||
max_h = max_h.saturating_sub(2 + measurements.len() as u16);
|
max_h = max_h.saturating_sub(2 + measurements.len() as u16);
|
||||||
let im = RenderSteps::render_configured(state, &[], z, (max_w as _, max_h as _), config);
|
let im = RenderSteps::render_configured(state, &[], z, (max_w as _, max_h as _), config);
|
||||||
@@ -453,7 +454,7 @@ impl<S: AbstractSim> Renderer<S> for ColorTermRenderer {
|
|||||||
for m in measurements {
|
for m in measurements {
|
||||||
// Measurements can be slow to compute
|
// Measurements can be slow to compute
|
||||||
stdout.flush().unwrap();
|
stdout.flush().unwrap();
|
||||||
let meas_string = m.eval(state);
|
let meas_string = m.str_value();
|
||||||
stdout.queue(cursor::MoveDown(1)).unwrap();
|
stdout.queue(cursor::MoveDown(1)).unwrap();
|
||||||
stdout.queue(cursor::MoveToColumn(1)).unwrap();
|
stdout.queue(cursor::MoveToColumn(1)).unwrap();
|
||||||
stdout.queue(PrintStyledContent(style(meas_string))).unwrap();
|
stdout.queue(PrintStyledContent(style(meas_string))).unwrap();
|
||||||
|
Reference in New Issue
Block a user