From cc2da3406b32e1caecf68c48d0c76c92da3626a4 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Fri, 19 Sep 2025 14:32:50 -0700 Subject: [PATCH] Print the edge distortions --- app-proto/main.css | 8 +++- app-proto/src/assembly.rs | 11 ++++- app-proto/src/components/diagnostics.rs | 59 ++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/app-proto/main.css b/app-proto/main.css index 0ec33e9..ef8aaf9 100644 --- a/app-proto/main.css +++ b/app-proto/main.css @@ -227,8 +227,14 @@ details[open]:has(li) .element-switch::after { border-radius: 8px; } -#distortion-gauge { +#distortion-bar { + display: flex; margin-top: 8px; + gap: 8px; +} + +#distortion-gauge { + flex-grow: 1; } /* display */ diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 6550238..9cd4533 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -367,6 +367,9 @@ pub trait Regulator: Serial + ProblemPoser + OutlineItem { fn subjects(&self) -> Vec>; fn measurement(&self) -> ReadSignal; fn set_point(&self) -> Signal; + fn soft(&self) -> Option> { + None + } fn distortion(&self) -> Option> { /* KLUDGE */ None } @@ -426,8 +429,8 @@ impl InversiveDistanceRegulator { match set_point_opt { None => 0.0, Some(set_point_val) => SQRT_2 * ( - (-set_point_val).sqrt() - (-measurement_val).sqrt() - ).abs(), + (-measurement_val).sqrt() - (-set_point_val).sqrt() + ), } })) } else { @@ -453,6 +456,10 @@ impl Regulator for InversiveDistanceRegulator { self.set_point } + fn soft(&self) -> Option> { + Some(self.soft) + } + fn distortion(&self) -> Option> { self.distortion } diff --git a/app-proto/src/components/diagnostics.rs b/app-proto/src/components/diagnostics.rs index ce4a0b4..52e7ffa 100644 --- a/app-proto/src/components/diagnostics.rs +++ b/app-proto/src/components/diagnostics.rs @@ -119,7 +119,7 @@ fn DistortionGauge() -> View { let mut total = 0.0; for reg in regs { if let Some(distortion) = reg.distortion() { - total += distortion.get(); + total += distortion.get().abs(); } } total @@ -133,6 +133,58 @@ fn DistortionGauge() -> View { } } +#[component] +fn DistortionPrintButton() -> View { + view! { + button( + on:click = |_| { + let state = use_context::(); + let mut hard_distortion_table = String::new(); + let mut soft_distortion_table = String::new(); + let mut highest_distortion = f64::NEG_INFINITY; + let mut lowest_distortion = f64::INFINITY; + let mut largest_hard_distortion = f64::NEG_INFINITY; + state.assembly.regulators.with_untracked(|regs| { + for reg in regs { + if let Some(distortion) = reg.distortion() { + let distortion_val = distortion.get(); + let subjects = reg.subjects(); + let distortion_line = format!( + "{}, {}: {distortion_val}\n", + subjects[0].id(), + subjects[1].id(), + ); + match reg.soft() { + Some(soft) if soft.get() => { + soft_distortion_table += &distortion_line; + highest_distortion = highest_distortion.max(distortion_val); + lowest_distortion = lowest_distortion.min(distortion_val); + }, + _ => { + hard_distortion_table += &distortion_line; + largest_hard_distortion = largest_hard_distortion.max(distortion_val.abs()); + } + }; + } + } + }); + console_log!("\ + === Distortions of flexible edges (for labels) ===\n\n\ + --- Range ---\n\n\ + Highest: {highest_distortion}\n\ + Lowest: {lowest_distortion}\n\n\ + --- Table ---\n\n{soft_distortion_table}\n\ + === Distortions of rigid edges (for validation) ===\n\n\ + These values should be small relative to the ones for the flexible edges\n\n\ + --- Range ---\n\n\ + Largest absolute: {largest_hard_distortion}\n\n\ + --- Table ---\n\n{hard_distortion_table}\ + "); + }, + ) { "Print" } + } +} + fn into_log10_time_point((step, value): (usize, f64)) -> Vec> { vec![ Some(step as f64), @@ -337,7 +389,10 @@ pub fn Diagnostics() -> View { } DiagnosticsPanel(name = "loss") { LossHistory {} } DiagnosticsPanel(name = "spectrum") { SpectrumHistory {} } - DistortionGauge {} + div(id = "distortion-bar") { + DistortionGauge {} + DistortionPrintButton {} + } } } } \ No newline at end of file