ui: results show units, clean numbers, extremes, reaction sums and a solve summary

One formatter (four significant digits, thousands separators, round-off as
0) and the model's units through every results readout:
- legend title with its unit, a mode shape normalised; Max/Min with units,
  clickable to show and probe the node; markers at both in the viewport
  (#321, #330)
- the deformation scale field shows the scale Deformed would use, never 0,
  in the shared format; true scale named (#325, #326)
- setup symbols off over results by default (Show › Loads & BCs), and then
  only the shown step's (#327)
- reaction sums in the results panel however they are run: per set and in
  all, force and moment with units, applied beside reacted (#328)
- the probe: U [mm], |U|, Mises, stresses and principals with units, the
  label kept inside the viewport, rows in the results panel (#329)
- hiding the mesh over a plot hides the element edges, not the contours
  (#317); a hidden body's contours go with it, the range follows what is
  shown (#365)
- the solve summary: peaks with units, n/a rows left out, counts grouped,
  internals under Details (#321, #319)
- the energies plot: axes with units, a cursor at the frame (click or drag
  to move it), hover to read (#331)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UhhZMBTviDQxZvwnyP89fo
This commit is contained in:
jyvikoandClaude Opus 5.5 authored and Irakli Kourtis committed 2026-10-15 09:36:54 -04:00
1 parent 91e8467e4f
commit 7a078b1c04
5 files changed
+675 -146

No files matched your search

+293 -78
View File
@@ -1,29 +1,63 @@
//! What the viewport shows over a results plot: the legend (field, bands, range, deformation
//! scale, a stale-result warning) and the probed node; and the results controls of the context
//! panel (right), which run the `results.*` commands.
//! What the viewport shows over a results plot: the legend (field and unit, bands, range,
//! deformation scale, a stale-result warning), markers at the contour's largest and smallest
//! value, and the probed node; and the results controls of the context panel (right), which run
//! the `results.*` commands. Numbers go through one formatter ([`crate::results::number`]) with
//! the units of the model's unit system ([`Quantity`]).
use egui::{Align2, Color32, FontId, Rect, RichText, Stroke, pos2, vec2};
use fieldcraft_engine::results::{Field, Kind};
use egui::{Align2, Color32, FontId, Pos2, Rect, RichText, Stroke, pos2, vec2};
use fieldcraft_engine::results::{Field, Kind, Quantity, true_scale, with_unit as titled};
use fieldcraft_explicit::{Energies, ExplicitResult};
use serde_json::{Value, json};
use crate::ModelApp;
use crate::results::{Plot, number};
use crate::results::{Plot, components, count, number, number_in, with_unit};
use crate::theme::Tokens;
/// Width of the panel's drop-down lists.
const COMBO_WIDTH: f32 = 160.0;
const SWATCH: egui::Vec2 = vec2(18.0, 15.0);
/// Height of the energy plot (points), and the most points one of its lines draws.
const PLOT_HEIGHT: f32 = 90.0;
/// Height of the energy plot's drawing area (points), the room its axis labels take, and the
/// most points one of its lines draws.
const PLOT_HEIGHT: f32 = 110.0;
const AXIS_LEFT: f32 = 48.0;
const AXIS_BOTTOM: f32 = 16.0;
const MAX_PLOT_POINTS: usize = 400;
/// Gap between a marker and its label, and the label's padding (points).
const LABEL_GAP: f32 = 12.0;
const LABEL_PAD: egui::Vec2 = vec2(8.0, 4.0);
/// Draw the legend and the probe when a results plot is shown.
/// Draw the legend, the extremes' markers and the probe when a results plot is shown. A click
/// on the legend's Max or Min line turns the view to that node and probes it.
pub fn show(app: &mut ModelApp, ui: &mut egui::Ui, rect: Rect, t: &Tokens) {
let Some(plot) = app.plot() else { return };
let painter = ui.painter_at(rect);
legend(&plot, &painter, rect, t);
probe(app, &plot, &painter, t);
let extremes = legend(&plot, &painter, rect, t);
if app.ui.result_extremes {
markers(app, &plot, &painter, rect, t);
}
probe(app, &plot, &painter, rect, t);
let mut focus = None;
for (i, (line, node)) in extremes.into_iter().enumerate() {
let r = ui.interact(line, ui.id().with(("fc_legend_extreme", i)), egui::Sense::click()).on_hover_text(format!("Show and probe node {node}"));
if r.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
if r.clicked() {
focus = Some(node);
}
}
if let Some(node) = focus {
focus_node(app, &plot, node);
}
}
/// Turn the view about node `node` of the plot (keeping the zoom) and probe it.
fn focus_node(app: &mut ModelApp, plot: &Plot, node: u32) {
if let Some(at) = plot.node_position(node).filter(|p| p.is_finite()) {
app.cam.target = at;
}
// A failure (a stale result) shows in the status bar.
let _ = app.probe_node(node);
}
/// What the legend shows and where: its frame, title and text lines.
@@ -40,28 +74,56 @@ pub fn legend_rect(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens)
legend_layout(plot, painter, rect, t).frame
}
/// The legend's title: the field with its unit (a mode shape normalised), or the shape drawn.
fn title(plot: &Plot) -> String {
match (&plot.contour, plot.mode()) {
(Some(l), _) => l.field.title(&plot.result, plot.units),
(None, Some(_)) => "Mode shape (normalised)".into(),
(None, None) => "Deformed shape".into(),
}
}
/// The deformation line: its scale, and whether it is automatic or true.
fn deformation(plot: &Plot) -> String {
if !plot.view.deformed {
return "Undeformed".into();
}
let how = match plot.view.scale {
_ if plot.scale == 1.0 && true_scale(&plot.result) => " (true scale)",
None => " (auto)",
Some(_) => "",
};
format!("Deformation × {}{how}", number(plot.scale))
}
/// The contour's Max and Min lines.
fn extreme_lines(plot: &Plot) -> Option<[(String, u32); 2]> {
let l = plot.contour.as_ref()?;
let unit = l.field.unit(&plot.result, plot.units);
let scale = l.max.1.abs().max(l.min.1.abs());
let line = |what: &str, (node, v): (u32, f64)| (format!("{what} {} at node {node}", with_unit(number_in(v, scale), unit.as_deref())), node);
Some([line("Max", l.max), line("Min", l.min)])
}
/// The legend's text and frame: top left, as wide as its longest line and as tall as its lines
/// and bands.
fn legend_layout(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) -> Legend {
let mut lines: Vec<(String, Color32)> = Vec::new();
let title = match (&plot.contour, plot.mode()) {
(Some(l), _) => l.field.label().to_string(),
(None, Some(_)) => "Mode shape".into(),
(None, None) => "Deformed shape".into(),
};
let title = title(plot);
let mut step = format!("Step {}", plot.view.step);
if let Some((mode, value)) = plot.mode() {
step.push_str(&format!(", mode {mode}: {value}"));
}
if let Some((k, time, n)) = plot.frame() {
step.push_str(&format!(", frame {k} of {}: t = {}", n.saturating_sub(1), number(time)));
if let Some((k, at, n)) = plot.frame() {
let last = n.saturating_sub(1);
match plot.result.kind() {
// A nonlinear static step's frames are its increments, at their load factors.
Kind::Static(_) => step.push_str(&format!(", increment {k} of {last}: load factor {}", number(at))),
_ => step.push_str(&format!(", frame {k} of {last}: t = {}", with_unit(number(at), Quantity::Time.unit(plot.units).as_deref()))),
}
}
lines.push((step, t.overlay_text));
if plot.scale > 0.0 {
lines.push((format!("Deformation × {}", number(plot.scale)), t.text_dim));
} else {
lines.push(("Undeformed".into(), t.text_dim));
}
lines.push((deformation(plot), t.text_dim));
if plot.stale {
lines.push(("Stale: the model changed since this step was solved".into(), t.warm));
}
@@ -69,8 +131,14 @@ fn legend_layout(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) -
let font = FontId::proportional(12.0);
let line_h = LEGEND_LINE;
let bands = plot.contour.as_ref().map_or(0, |l| l.bands.count) as f32;
// As wide as its longest line (and the range labels beside the swatches).
let widest = lines.iter().map(|(text, _)| painter.layout_no_wrap(text.clone(), font.clone(), t.overlay_text).size().x).fold(0.0_f32, f32::max);
// As wide as its longest line (and the extremes' lines under the swatches).
let extremes = extreme_lines(plot).into_iter().flatten().map(|(text, _)| text);
let widest = lines
.iter()
.map(|(text, _)| text.clone())
.chain(extremes)
.map(|text| painter.layout_no_wrap(text, font.clone(), t.overlay_text).size().x)
.fold(0.0_f32, f32::max);
let title_w = painter.layout_no_wrap(title.clone(), FontId::proportional(14.0), t.overlay_text).size().x;
let width = (widest.max(title_w) + 20.0).max(170.0);
// Padding and title, the lines, then the swatches with the range lines under them.
@@ -78,8 +146,9 @@ fn legend_layout(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) -
Legend { frame: Rect::from_min_size(origin, vec2(width, height)), title, lines }
}
/// The legend, top left under the view buttons.
fn legend(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
/// The legend, top left under the view buttons. Returns the Max and Min lines' rects with their
/// nodes.
fn legend(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) -> Vec<(Rect, u32)> {
let Legend { frame, title, lines } = legend_layout(plot, painter, rect, t);
let origin = frame.min;
let (font, line_h) = (FontId::proportional(12.0), LEGEND_LINE);
@@ -91,9 +160,11 @@ fn legend(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
painter.text(pos2(origin.x + 10.0, y), Align2::LEFT_TOP, text, font.clone(), *color);
y += line_h;
}
let Some(l) = &plot.contour else { return };
let (Some(l), Some(extremes)) = (&plot.contour, extreme_lines(plot)) else { return Vec::new() };
y += 6.0;
let stops = t.contour_stops();
// Round-off among the levels (a range from -1e-15 to 3) reads as 0.
let scale = l.max.1.abs().max(l.min.1.abs());
// High values on top, each band's lower boundary written beside its swatch's bottom edge.
for k in (0..l.bands.count).rev() {
let row = (l.bands.count - 1 - k) as f32;
@@ -101,9 +172,9 @@ fn legend(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
let c = fieldcraft_render::contour::band_color(&stops, k, l.bands.count);
painter.rect_filled(r, 0.0, Color32::from_rgb(c.0, c.1, c.2));
if k + 1 == l.bands.count {
painter.text(pos2(r.right() + 6.0, r.top()), Align2::LEFT_CENTER, number(l.bands.level(k + 1)), font.clone(), t.overlay_text);
painter.text(pos2(r.right() + 6.0, r.top()), Align2::LEFT_CENTER, number_in(l.bands.level(k + 1), scale), font.clone(), t.overlay_text);
}
painter.text(pos2(r.right() + 6.0, r.bottom()), Align2::LEFT_CENTER, number(l.bands.level(k)), font.clone(), t.overlay_text);
painter.text(pos2(r.right() + 6.0, r.bottom()), Align2::LEFT_CENTER, number_in(l.bands.level(k), scale), font.clone(), t.overlay_text);
}
painter.rect_stroke(
Rect::from_min_size(pos2(origin.x + 10.0, y), vec2(SWATCH.x, SWATCH.y * l.bands.count as f32)),
@@ -112,24 +183,54 @@ fn legend(plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
egui::StrokeKind::Outside,
);
y += SWATCH.y * l.bands.count as f32 + 10.0;
painter.text(pos2(origin.x + 10.0, y), Align2::LEFT_TOP, format!("Max {} at node {}", number(l.max.1), l.max.0), font.clone(), t.text_dim);
painter.text(pos2(origin.x + 10.0, y + line_h), Align2::LEFT_TOP, format!("Min {} at node {}", number(l.min.1), l.min.0), font, t.text_dim);
let mut out = Vec::new();
for (i, (text, node)) in extremes.into_iter().enumerate() {
let r = painter.text(pos2(origin.x + 10.0, y + line_h * i as f32), Align2::LEFT_TOP, text, font.clone(), t.text_dim);
out.push((r.expand(2.0), node));
}
out
}
/// The probed node: a marker on it and its values beside it.
fn probe(app: &ModelApp, plot: &Plot, painter: &egui::Painter, t: &Tokens) {
/// A box of `text` beside the screen point `at`, kept inside `bounds`: right of the point, or left
/// of it when it would run off the right edge; held inside top and bottom.
fn label_beside(painter: &egui::Painter, at: Pos2, text: String, bounds: Rect, t: &Tokens) {
let galley = painter.layout_no_wrap(text, FontId::proportional(12.0), t.overlay_text);
let size = galley.size() + 2.0 * LABEL_PAD;
let right = at + vec2(LABEL_GAP, -size.y / 2.0);
let x = if right.x + size.x > bounds.right() { at.x - LABEL_GAP - size.x } else { right.x };
let y = right.y.clamp(bounds.top(), (bounds.bottom() - size.y).max(bounds.top()));
let box_rect = Rect::from_min_size(pos2(x.max(bounds.left()), y), size);
painter.rect_filled(box_rect, 4.0, t.overlay);
painter.galley(box_rect.left_top() + LABEL_PAD, galley, t.overlay_text);
}
/// Markers with their values at the contour's largest and smallest value.
fn markers(app: &ModelApp, plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
let Some(l) = &plot.contour else { return };
let unit = l.field.unit(&plot.result, plot.units);
let scale = l.max.1.abs().max(l.min.1.abs());
let (high, low) = (t.contour[4], t.contour[0]);
for (what, (node, v), color) in [("Max", l.max, high), ("Min", l.min, low)] {
let Some(at) = plot.node_position(node).and_then(|w| crate::viewport::to_screen(app, w)) else { continue };
if !rect.contains(at) {
continue;
}
painter.circle(at, 5.0, color, Stroke::new(2.0, t.overlay_text));
label_beside(painter, at, format!("{what} {}", with_unit(number_in(v, scale), unit.as_deref())), rect, t);
}
}
/// The probed node: a marker on it and its values beside it, inside the viewport.
fn probe(app: &ModelApp, plot: &Plot, painter: &egui::Painter, rect: Rect, t: &Tokens) {
let Some(p) = app.probe.as_ref().filter(|p| p.step == plot.view.step && p.mode == plot.pick.mode && p.frame == plot.pick.frame) else { return };
let Some(at) = plot.node_position(p.node).and_then(|w| crate::viewport::to_screen(app, w)) else { return };
painter.circle(at, 5.0, t.accent, Stroke::new(2.0, t.overlay_text));
let font = FontId::proportional(12.0);
let galley = painter.layout_no_wrap(p.lines.join("\n"), font, t.overlay_text);
let box_rect = Rect::from_min_size(at + vec2(12.0, -10.0), galley.size() + vec2(16.0, 8.0));
painter.rect_filled(box_rect, 4.0, t.overlay);
painter.galley(box_rect.left_top() + vec2(8.0, 4.0), galley, t.overlay_text);
label_beside(painter, at, p.lines.join("\n"), rect, t);
}
/// The results controls (the context panel's Results section): step, contour field,
/// deformation, modes, animation, reaction sums. Every control runs a command.
/// deformation, modes, animation, what the viewport marks, reaction sums and the probed node.
/// Every control runs a command or a view setting.
pub fn controls(app: &mut ModelApp, plot: &Plot, ui: &mut egui::Ui, t: &Tokens) {
let mut run: Vec<(&'static str, Value)> = Vec::new();
let steps: Vec<(String, bool)> = app.session.result_steps().map(|(n, c)| (n.to_string(), c)).collect();
@@ -164,11 +265,22 @@ pub fn controls(app: &mut ModelApp, plot: &Plot, ui: &mut egui::Ui, t: &Tokens)
run.push(("results.deformed", json!({"show": deformed})));
}
let mut auto = plot.view.scale.is_none();
if ui.checkbox(&mut auto, "Auto").on_hover_text("Scale the largest displacement to a tenth of the model").changed() {
let tip = if true_scale(&plot.result) {
"Draw the displacements at true scale (they are large already)"
} else {
"Scale the largest displacement to a tenth of the model"
};
if ui.checkbox(&mut auto, "Auto").on_hover_text(tip).changed() {
run.push(("results.deformed", json!({"scale": if auto { json!("auto") } else { json!(plot.scale) }})));
}
// The scale ticking Deformed draws with, also while it is off.
let mut scale = plot.scale;
let drag = egui::DragValue::new(&mut scale).speed(plot.scale.abs().max(1e-6) * 0.01).range(0.0..=1e12).prefix("× ");
let drag = egui::DragValue::new(&mut scale)
.speed(plot.scale.abs().max(1e-6) * 0.01)
.range(0.0..=1e12)
.prefix("× ")
.custom_formatter(|v, _| number(v))
.custom_parser(|s| s.replace(',', "").trim().parse::<f64>().ok());
if ui.add_enabled(plot.view.deformed, drag).changed() {
run.push(("results.deformed", json!({"scale": scale})));
}
@@ -180,6 +292,14 @@ pub fn controls(app: &mut ModelApp, plot: &Plot, ui: &mut egui::Ui, t: &Tokens)
run.push(("results.animate", json!({"on": animate})));
}
ui.end_row();
ui.label("Show");
ui.horizontal(|ui| {
if plot.contour.is_some() {
ui.checkbox(&mut app.ui.result_extremes, "Max/Min").on_hover_text("Mark the nodes of the largest and smallest value");
}
ui.checkbox(&mut app.ui.result_symbols, "Loads & BCs").on_hover_text("The symbols of the step's boundary conditions and loads");
});
ui.end_row();
});
if let Kind::Explicit(e) = plot.result.kind() {
ui.separator();
@@ -215,27 +335,45 @@ pub fn controls(app: &mut ModelApp, plot: &Plot, ui: &mut egui::Ui, t: &Tokens)
} else if let Kind::Heat(h) = plot.result.kind() {
ui.separator();
heat_flows(ui, &fieldcraft_engine::results::heat_flows(h), t);
} else {
}
if matches!(plot.result.kind(), Kind::Static(_)) {
ui.separator();
if ui.button("Reaction sums").on_hover_text("Reaction force and moment per boundary-condition set (results.reactions)").clicked() {
run.push(("results.reactions", json!({"step": plot.view.step})));
}
if let Some(r) = app.reactions.as_ref().filter(|r| r["step"] == plot.view.step.as_str()) {
reactions_table(ui, r, t);
reactions_table(ui, r, plot, t);
}
}
ui.label(RichText::new("Click the model to probe a node").small().color(t.text_dim));
ui.separator();
probed(app, plot, ui, t);
for (id, params) in run {
let reply = app.run(id, params);
if id == "results.reactions" {
app.reactions = reply.ok();
}
// The reaction sums land in `app.reactions` (ModelApp::run_form).
let _ = app.run(id, params);
}
}
/// The probed node's values, one row each, or how to probe one.
fn probed(app: &ModelApp, plot: &Plot, ui: &mut egui::Ui, t: &Tokens) {
let Some(p) = app.probe.as_ref().filter(|p| p.step == plot.view.step && p.mode == plot.pick.mode && p.frame == plot.pick.frame) else {
ui.label(RichText::new("Click the model to probe a node").small().color(t.text_dim));
return;
};
ui.label(RichText::new(format!("Node {}", p.node)).strong());
egui::Grid::new("fc_results_probe").num_columns(2).spacing(vec2(8.0, 2.0)).show(ui, |ui| {
for (what, value) in &p.rows {
ui.label(RichText::new(what).color(t.text_dim));
ui.add(egui::Label::new(value).extend());
ui.end_row();
}
});
}
/// An explicit step: a frame slider (the deformed shape and contours follow it) and the energy
/// histories, plotted and read at the frame's time.
/// histories, plotted against time with a cursor at the frame shown (a click or drag moves it)
/// and read there.
fn explicit(ui: &mut egui::Ui, e: &ExplicitResult, plot: &Plot, t: &Tokens, run: &mut Vec<(&'static str, Value)>) {
let time_unit = Quantity::Time.unit(plot.units);
if let Some((k, time, n)) = plot.frame() {
ui.horizontal(|ui| {
ui.label("Frame");
@@ -244,7 +382,7 @@ fn explicit(ui: &mut egui::Ui, e: &ExplicitResult, plot: &Plot, t: &Tokens, run:
run.push(("results.frame", json!({"frame": frame})));
}
});
ui.label(RichText::new(format!("t = {}", number(time))).color(t.text_dim));
ui.label(RichText::new(format!("t = {}", with_unit(number(time), time_unit.as_deref()))).color(t.text_dim));
}
let h = &e.history;
if h.time.is_empty() {
@@ -254,17 +392,35 @@ fn explicit(ui: &mut egui::Ui, e: &ExplicitResult, plot: &Plot, t: &Tokens, run:
("Kinetic", t.accent, |e| e.kinetic),
("Internal", t.ok, |e| e.internal),
("Hourglass", t.warm, |e| e.hourglass),
("External", t.error, |e| e.external),
("External work", t.error, |e| e.external),
];
ui.label(RichText::new("Energies").color(t.text_dim));
let (rect, _) = ui.allocate_exact_size(vec2(ui.available_width(), PLOT_HEIGHT), egui::Sense::hover());
let painter = ui.painter_at(rect);
let energy_unit = Quantity::Energy.unit(plot.units);
ui.label(RichText::new(titled("Energies", energy_unit.as_deref())).color(t.text_dim));
let (outer, response) = ui.allocate_exact_size(vec2(ui.available_width(), PLOT_HEIGHT + AXIS_BOTTOM), egui::Sense::click_and_drag());
let rect = Rect::from_min_max(outer.min + vec2(AXIS_LEFT, 0.0), outer.max - vec2(0.0, AXIS_BOTTOM));
let painter = ui.painter_at(outer);
painter.rect_filled(rect, 2.0, t.field);
let t_end = h.time.last().copied().unwrap_or(0.0).max(f64::MIN_POSITIVE);
let top = h.energies.iter().flat_map(|e| series.iter().map(move |(_, _, f)| f(e).abs())).filter(|v| v.is_finite()).fold(0.0, f64::max);
let top = if top > 0.0 { top } else { 1.0 };
let at =
|time: f64, v: f64| pos2(rect.left() + (time / t_end) as f32 * rect.width(), rect.bottom() - 2.0 - (v / top) as f32 * (rect.height() - 4.0));
// The value axis spans every curve and 0 (a curve below 0 stays in the frame).
let values = || h.energies.iter().flat_map(|e| series.iter().map(move |(_, _, f)| f(e))).filter(|v| v.is_finite());
let hi = values().fold(0.0, f64::max);
let lo = values().fold(0.0, f64::min);
let span = if hi > lo { hi - lo } else { 1.0 };
let at = |time: f64, v: f64| {
pos2(rect.left() + (time / t_end) as f32 * rect.width(), rect.bottom() - 2.0 - ((v - lo) / span) as f32 * (rect.height() - 4.0))
};
// Axes: the value range left, the time range under it.
let small = FontId::proportional(10.0);
let scale = hi.abs().max(lo.abs());
painter.text(pos2(rect.left() - 4.0, rect.top()), Align2::RIGHT_TOP, number_in(hi, scale), small.clone(), t.text_dim);
painter.text(pos2(rect.left() - 4.0, rect.bottom()), Align2::RIGHT_BOTTOM, number_in(lo, scale), small.clone(), t.text_dim);
if lo < 0.0 && hi > 0.0 {
let zero = at(0.0, 0.0).y;
painter.line_segment([pos2(rect.left(), zero), pos2(rect.right(), zero)], Stroke::new(1.0, t.border));
}
painter.text(pos2(rect.left(), rect.bottom() + 2.0), Align2::LEFT_TOP, "0", small.clone(), t.text_dim);
let end = format!("t {}", with_unit(number(t_end), time_unit.as_deref()));
painter.text(pos2(rect.right(), rect.bottom() + 2.0), Align2::RIGHT_TOP, end, small, t.text_dim);
// At most a few hundred points per line, however long the history.
let stride = (h.time.len() / MAX_PLOT_POINTS).max(1);
for (_, color, f) in &series {
@@ -272,19 +428,38 @@ fn explicit(ui: &mut egui::Ui, e: &ExplicitResult, plot: &Plot, t: &Tokens, run:
h.time.iter().zip(&h.energies).step_by(stride).filter(|(_, e)| f(e).is_finite()).map(|(time, e)| at(*time, f(e))).collect();
painter.add(egui::Shape::line(points, Stroke::new(1.5, *color)));
}
// The frame's time, and the sample nearest it.
let nearest = |time: f64| h.time.iter().enumerate().min_by(|a, b| (a.1 - time).abs().total_cmp(&(b.1 - time).abs())).map_or(0, |(i, _)| i);
let time_at = |x: f32| f64::from(((x - rect.left()) / rect.width()).clamp(0.0, 1.0)) * t_end;
// A click or drag on the plot shows the frame nearest that time.
if (response.clicked() || response.dragged())
&& let Some(p) = response.interact_pointer_pos()
{
run.push(("results.frame", json!({"time": time_at(p.x)})));
}
// Hover reads the curves at the pointer's time.
if let Some(p) = response.hover_pos().filter(|p| rect.contains(*p)) {
let x = p.x;
painter.line_segment([pos2(x, rect.top()), pos2(x, rect.bottom())], Stroke::new(1.0, t.border));
if let Some(e) = h.energies.get(nearest(time_at(x))) {
let mut lines = vec![format!("t = {}", with_unit(number(time_at(x)), time_unit.as_deref()))];
lines.extend(series.iter().map(|(name, _, f)| format!("{name} {}", number_in(f(e), scale))));
response.clone().on_hover_text(lines.join("\n"));
}
}
// The frame's time: the cursor, and the values there.
let now = plot.frame().map_or(t_end, |(_, time, _)| time);
let x = at(now, 0.0).x;
painter.line_segment([pos2(x, rect.top()), pos2(x, rect.bottom())], Stroke::new(1.0, t.text_dim));
let i = h.time.iter().enumerate().min_by(|a, b| (a.1 - now).abs().total_cmp(&(b.1 - now).abs())).map_or(0, |(i, _)| i);
let Some(at_now) = h.energies.get(i) else { return };
painter.line_segment([pos2(x, rect.top()), pos2(x, rect.bottom())], Stroke::new(1.5, t.text));
let Some(at_now) = h.energies.get(nearest(now)) else { return };
let frame = plot.frame().map_or_else(String::new, |(k, ..)| format!(", frame {k}"));
ui.label(RichText::new(format!("At t = {}{frame}", with_unit(number(now), time_unit.as_deref()))).small().color(t.text_dim));
egui::Grid::new("fc_results_energies").num_columns(2).spacing(vec2(8.0, 2.0)).show(ui, |ui| {
for (name, color, f) in &series {
ui.label(RichText::new(*name).color(*color));
ui.label(number(f(at_now)));
ui.label(with_unit(number_in(f(at_now), scale), energy_unit.as_deref()));
ui.end_row();
}
ui.label(RichText::new("Balance").color(t.text_dim));
ui.label(RichText::new("Balance").color(t.text_dim)).on_hover_text("The energy balance's relative error");
ui.label(number(at_now.balance));
ui.end_row();
});
@@ -308,23 +483,63 @@ fn heat_flows(ui: &mut egui::Ui, flows: &Value, t: &Tokens) {
});
}
fn reactions_table(ui: &mut egui::Ui, r: &Value, t: &Tokens) {
let row = |ui: &mut egui::Ui, name: &str, v: &Value| {
let f = |k: usize| v.pointer(&format!("/force/{k}")).and_then(Value::as_f64).map_or("—".into(), number);
ui.label(name);
ui.label(f(0));
ui.label(f(1));
ui.label(f(2));
ui.end_row();
/// The reaction sums: per set and over the model, the force and the moment about the point, and
/// the applied total beside the reacted one (they balance in equilibrium). Round-off below a
/// billionth of the largest force (moment) is 0.
fn reactions_table(ui: &mut egui::Ui, r: &Value, plot: &Plot, t: &Tokens) {
let vector = |v: &Value| -> Option<[f64; 3]> { serde_json::from_value(v.clone()).ok() };
let rows: Vec<(String, Option<[f64; 3]>, Option<[f64; 3]>)> = r["sets"]
.as_array()
.into_iter()
.flatten()
.map(|s| {
let nodes = s["nodes"].as_u64().unwrap_or(0);
(format!("{} ({} nodes)", s["set"].as_str().unwrap_or("?"), count(nodes)), vector(&s["force"]), vector(&s["moment"]))
})
.chain(std::iter::once(("All nodes".to_string(), vector(&r["total"]["force"]), vector(&r["total"]["moment"]))))
.collect();
let applied = vector(&r["applied"]);
let largest = |vs: &mut dyn Iterator<Item = [f64; 3]>| vs.flatten().map(f64::abs).filter(|x| x.is_finite()).fold(0.0, f64::max);
let force_scale = largest(&mut rows.iter().filter_map(|r| r.1).chain(applied));
let moment_scale = largest(&mut rows.iter().filter_map(|r| r.2));
let (force, moment) = (Quantity::Force.unit(plot.units), Quantity::Moment.unit(plot.units));
let about = vector(&r["about"]).unwrap_or_default();
ui.label(RichText::new(format!("Moments about ({})", components(&about, 0.0))).small().color(t.text_dim));
let cells = |ui: &mut egui::Ui, v: Option<[f64; 3]>, scale: f64| {
for k in 0..3 {
ui.add(egui::Label::new(v.and_then(|v| v.get(k).copied()).map_or("—".into(), |x| number_in(x, scale))).extend());
}
};
egui::Grid::new("fc_results_reactions").num_columns(4).spacing(vec2(6.0, 2.0)).show(ui, |ui| {
for h in ["Set", "Fx", "Fy", "Fz"] {
egui::Grid::new("fc_results_reactions").num_columns(4).spacing(vec2(8.0, 2.0)).striped(true).show(ui, |ui| {
for h in ["", "X", "Y", "Z"] {
ui.label(RichText::new(h).small().color(t.text_dim));
}
ui.end_row();
for s in r["sets"].as_array().into_iter().flatten() {
row(ui, s["set"].as_str().unwrap_or("?"), s);
for (name, f, m) in &rows {
ui.label(RichText::new(name).strong());
ui.end_row();
ui.label(RichText::new(titled("F", force.as_deref())).color(t.text_dim));
cells(ui, *f, force_scale);
ui.end_row();
ui.label(RichText::new(titled("M", moment.as_deref())).color(t.text_dim));
cells(ui, *m, moment_scale);
ui.end_row();
}
if applied.is_some() {
ui.label(RichText::new(titled("Applied F", force.as_deref())).color(t.text_dim));
cells(ui, applied, force_scale);
ui.end_row();
}
row(ui, "All", &r["total"]);
});
// Equilibrium: the reactions over the whole model balance the applied loads.
if let (Some(total), Some(applied)) = (rows.last().and_then(|r| r.1), applied) {
let residual = (0..3).map(|k| (total[k] + applied[k]).abs()).fold(0.0, f64::max);
let balanced = residual <= 1e-6 * force_scale.max(f64::MIN_POSITIVE);
let (text, color) = if balanced {
("✔ Reactions balance the applied loads".to_string(), t.ok)
} else {
(format!("Reactions and applied loads differ by {}", with_unit(number(residual), force.as_deref())), t.warm)
};
ui.label(RichText::new(text).small().color(color));
}
}