Results display (M4) and offscreen full-UI capture (#28)

* ui.capture: the whole window drawn offscreen and rasterised on the CPU

ui.screenshot falls back to it when no frame is presented (hidden window,
locked screen), so agents can look at panels and the toolbar.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* Draw the open design and imported surfaces in the viewport

DisplayMesh keeps node labels on every element primitive (for results),
gains add_surface (geometry faces and their boundaries, coincident points
welded) and displaced (elements moved by nodal offsets). Bodies are
tessellated at a thousandth of their size; meshed models show the geometry
as boundaries only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* results.* commands: deformed shape, contours, mode shapes, animation, reaction sums

The results display is session state (ResultsView), set by commands that are
not edits. Nodal fields (displacement, von Mises, principal and component
stresses; principal stresses in closed form after Smith 1961) and reaction
force and moment sums per set. Results kept from an earlier revision stay
viewable, marked stale; solve.probe says when a result is stale. The Results
tab's display and reaction entries are ready.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* Results display: deformed shape, contours with legend, mode shapes and animation, probe, reaction sums

The viewport draws the session's results view: elements moved by their nodes'
displacements or a mode shape (automatic or given scale), faces split exactly
along contour band boundaries into flat-coloured pieces (same on GPU and CPU),
a legend with levels and extremes, a results panel whose controls run the
results.* commands, a click probing the nearest node (solve.probe), and stale
results marked. Solving from the form shows the result on the Results tab.
sample::cantilever is a solvable model for tests and demos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* Simplify the results display (review)

One Contour in the plot; the results scene reuses scene::build; a solve shows
its result through the engine (ResultsView::for_result) and the UI raises the
Results tab when a view appears; the plot's scale and contour values are kept
across animation frames; design bodies are tessellated once per design; the
first content is framed where the display is rebuilt; field_values in one
match; probe reuses results::displacements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* Results display for explicit steps: field frames, energy histories

The results view works on the engine's step results (static, frequency,
explicit): ResultsView.frame and results.frame {frame | time} pick an explicit
field frame; the deformed shape (true scale) and contours (element stresses
averaged to the nodes, velocity) follow it; animation runs through the frames.
The Results panel has a frame slider and plots the kinetic, internal, hourglass
and external energies with their values at the frame. sample::impact is a
steel bar flying onto a rigid wall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

* docs/features.md after the rebase

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NmAttHhUZTokgPeEJia8sm

---------

Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-13 19:23:05 -04:00
1 parent 1899ecbd2f
commit 0e97f8dd1d
30 files changed
+2989 -86

No files matched your search

+406
View File
@@ -0,0 +1,406 @@
//! What the results display shows ([`ResultsView`], set by the `results.*` commands) and the
//! nodal values it draws: displacements, mode shapes, explicit field frames and stress fields,
//! read from a solved step's [`StepResult`]. An explicit frame's one-point element stresses are
//! averaged to the nodes (unweighted, like the implicit solver's nodal stresses).
//!
//! The view is session state, like the camera: changing it is not an edit, so it is neither
//! journalled nor undone, and it does not change the session revision. A result kept from an
//! earlier revision is still shown, marked stale ([`Session::latest_result`]).
use modelcraft_explicit::{ExplicitResult, Frame};
use modelcraft_model::Model;
use modelcraft_solver::{FrequencyResult, StaticResult};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use crate::{EngineError, Result, Session, StepResult};
/// A result by its procedure.
pub enum Kind<'a> {
Static(&'a StaticResult),
Frequency(&'a FrequencyResult),
Explicit(&'a ExplicitResult),
}
impl StepResult {
pub fn kind(&self) -> Kind<'_> {
match self {
StepResult::Implicit(modelcraft_solver::StepResult::Static(r)) => Kind::Static(r),
StepResult::Implicit(modelcraft_solver::StepResult::Frequency(r)) => Kind::Frequency(r),
StepResult::Explicit(r) => Kind::Explicit(r),
}
}
}
/// Which part of a result is shown: the mode (from 1, frequency steps) and the field frame
/// (explicit steps; `None` is the last).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Pick {
pub mode: usize,
pub frame: Option<usize>,
}
/// The field frame of an explicit result `pick` asks for.
pub fn frame(r: &ExplicitResult, pick: Pick) -> Result<&Frame> {
let last = r.frames.len().checked_sub(1).ok_or_else(|| EngineError::Params(format!("step `{}` has no field frames", r.step)))?;
let k = pick.frame.unwrap_or(last);
r.frames.get(k).ok_or_else(|| EngineError::Params(format!("step `{}` has frames 0 to {last}; there is no frame {k}", r.step)))
}
/// A nodal quantity drawn as contours.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Field {
/// Displacement magnitude (a mode's shape magnitude for a frequency step).
Displacement,
DisplacementX,
DisplacementY,
DisplacementZ,
/// Velocity magnitude (explicit steps).
Velocity,
VonMises,
MaxPrincipal,
MidPrincipal,
MinPrincipal,
StressXx,
StressYy,
StressZz,
StressXy,
StressXz,
StressYz,
}
impl Field {
pub const ALL: [Field; 15] = [
Field::Displacement,
Field::DisplacementX,
Field::DisplacementY,
Field::DisplacementZ,
Field::Velocity,
Field::VonMises,
Field::MaxPrincipal,
Field::MidPrincipal,
Field::MinPrincipal,
Field::StressXx,
Field::StressYy,
Field::StressZz,
Field::StressXy,
Field::StressXz,
Field::StressYz,
];
/// The id commands take (`von_mises`…).
pub fn id(self) -> &'static str {
match self {
Field::Displacement => "displacement",
Field::DisplacementX => "displacement_x",
Field::DisplacementY => "displacement_y",
Field::DisplacementZ => "displacement_z",
Field::Velocity => "velocity",
Field::VonMises => "von_mises",
Field::MaxPrincipal => "max_principal",
Field::MidPrincipal => "mid_principal",
Field::MinPrincipal => "min_principal",
Field::StressXx => "stress_xx",
Field::StressYy => "stress_yy",
Field::StressZz => "stress_zz",
Field::StressXy => "stress_xy",
Field::StressXz => "stress_xz",
Field::StressYz => "stress_yz",
}
}
pub fn label(self) -> &'static str {
match self {
Field::Displacement => "Displacement magnitude",
Field::DisplacementX => "Displacement X",
Field::DisplacementY => "Displacement Y",
Field::DisplacementZ => "Displacement Z",
Field::Velocity => "Velocity magnitude",
Field::VonMises => "Von Mises stress",
Field::MaxPrincipal => "Max. principal stress",
Field::MidPrincipal => "Mid. principal stress",
Field::MinPrincipal => "Min. principal stress",
Field::StressXx => "Stress XX",
Field::StressYy => "Stress YY",
Field::StressZz => "Stress ZZ",
Field::StressXy => "Stress XY",
Field::StressXz => "Stress XZ",
Field::StressYz => "Stress YZ",
}
}
pub fn parse(id: &str) -> Option<Field> {
Field::ALL.into_iter().find(|f| f.id() == id)
}
/// Whether it is a stress.
pub fn is_stress(self) -> bool {
!matches!(self, Field::Displacement | Field::DisplacementX | Field::DisplacementY | Field::DisplacementZ | Field::Velocity)
}
/// The fields a result has: displacements everywhere, stresses for static steps and explicit
/// frames that kept them (mode shapes have no stress scale), velocity for explicit frames.
pub fn available(r: &StepResult) -> impl Iterator<Item = Field> + '_ {
let (stress, velocity) = match r.kind() {
Kind::Static(_) => (true, false),
Kind::Frequency(_) => (false, false),
Kind::Explicit(e) => e.frames.last().map_or((false, false), |f| (!f.stress.is_empty(), !f.velocity.is_empty())),
};
Field::ALL.into_iter().filter(move |f| if *f == Field::Velocity { velocity } else { stress || !f.is_stress() })
}
}
/// Most contour bands.
pub const MAX_BANDS: u32 = 24;
/// Fewest contour bands.
pub const MIN_BANDS: u32 = 2;
/// Contour bands when none are asked for.
pub const DEFAULT_BANDS: u32 = 12;
/// What the results display shows.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ResultsView {
/// The solved step shown.
pub step: String,
/// Draw the deformed shape.
pub deformed: bool,
/// Deformation scale factor; `None` scales the largest displacement to a tenth of the
/// model's size.
pub scale: Option<f64>,
/// Contours of this field; `None` draws the deformed shape in element colours.
pub contour: Option<Field>,
pub bands: u32,
/// The mode shown, from 1 (frequency steps).
pub mode: usize,
/// The field frame shown (explicit steps); `None` is the last.
#[serde(default)]
pub frame: Option<usize>,
/// Animate the deformation (a mode swings through ±, a static step grows from 0).
pub animate: bool,
}
impl ResultsView {
/// How a step's result is shown first: deformed, with von Mises contours for a static step
/// (and an explicit step that kept stresses), the first mode with displacement contours for
/// a frequency step; an explicit step at its last frame, to true scale.
pub fn for_result(r: &StepResult) -> ResultsView {
let contour = if Field::available(r).any(|f| f == Field::VonMises) { Field::VonMises } else { Field::Displacement };
let scale = matches!(r.kind(), Kind::Explicit(_)).then_some(1.0);
ResultsView {
step: r.step().to_string(),
deformed: true,
scale,
contour: Some(contour),
bands: DEFAULT_BANDS,
mode: 1,
frame: None,
animate: false,
}
}
pub fn pick(&self) -> Pick {
Pick { mode: self.mode, frame: self.frame }
}
}
/// The nodal displacements a result's deformed shape uses: the static displacement, the shape
/// of the picked mode or the picked frame's displacement, in [`StepResult::nodes`] order.
pub fn displacements(r: &StepResult, pick: Pick) -> Result<&[[f64; 3]]> {
match r.kind() {
Kind::Static(s) => Ok(&s.displacement),
Kind::Frequency(f) => pick
.mode
.checked_sub(1)
.and_then(|k| f.modes.get(k))
.map(|m| m.shape.as_slice())
.ok_or_else(|| EngineError::Params(format!("step `{}` has modes 1 to {}; there is no mode {}", f.step, f.modes.len(), pick.mode))),
Kind::Explicit(e) => {
let f = frame(e, pick)?;
if f.displacement.is_empty() {
return Err(EngineError::Params(format!("step `{}` kept no displacements in its field frames", e.step)));
}
Ok(&f.displacement)
}
}
}
/// A field's value at every node of the result, in [`StepResult::nodes`] order. `model` gives
/// the element connectivity an explicit frame's element stresses are averaged over.
pub fn field_values(model: &Model, r: &StepResult, field: Field, pick: Pick) -> Result<Vec<f64>> {
let unavailable = || EngineError::Params(format!("{} is not available for step `{}`", field.label(), r.step()));
let component = |f: fn(&[f64; 3]) -> f64| -> Result<Vec<f64>> { Ok(displacements(r, pick)?.iter().map(f).collect()) };
let stresses = || -> Result<std::borrow::Cow<'_, [[f64; 6]]>> {
match r.kind() {
Kind::Static(s) => Ok(std::borrow::Cow::Borrowed(s.stress.as_slice())),
Kind::Frequency(_) => Err(unavailable()),
Kind::Explicit(e) => {
let f = frame(e, pick)?;
if f.stress.is_empty() {
return Err(unavailable());
}
Ok(std::borrow::Cow::Owned(nodal_average(model, &e.nodes, &e.elements, &f.stress)))
}
}
};
let each = |f: &dyn Fn(&[f64; 6]) -> f64| -> Result<Vec<f64>> { Ok(stresses()?.iter().map(f).collect()) };
let principal = |k: usize| each(&|t| modelcraft_fem::principal(t).get(k).copied().unwrap_or(f64::NAN));
match field {
Field::Displacement => component(|v| magnitude(*v)),
Field::DisplacementX => component(|v| v[0]),
Field::DisplacementY => component(|v| v[1]),
Field::DisplacementZ => component(|v| v[2]),
Field::Velocity => match r.kind() {
Kind::Explicit(e) => {
let f = frame(e, pick)?;
if f.velocity.is_empty() { Err(unavailable()) } else { Ok(f.velocity.iter().map(|v| magnitude(*v)).collect()) }
}
Kind::Static(_) | Kind::Frequency(_) => Err(unavailable()),
},
Field::VonMises => each(&modelcraft_fem::von_mises),
Field::MaxPrincipal => principal(0),
Field::MidPrincipal => principal(1),
Field::MinPrincipal => principal(2),
Field::StressXx => each(&|t| t[0]),
Field::StressYy => each(&|t| t[1]),
Field::StressZz => each(&|t| t[2]),
Field::StressXy => each(&|t| t[3]),
Field::StressXz => each(&|t| t[4]),
Field::StressYz => each(&|t| t[5]),
}
}
/// Element values (in `elements` order) averaged at the nodes (in `nodes` order) of the
/// elements around them; a node no element reaches gets NaN.
fn nodal_average(model: &Model, nodes: &[u32], elements: &[u32], values: &[[f64; 6]]) -> Vec<[f64; 6]> {
let mut sum = vec![[0.0; 6]; nodes.len()];
let mut count = vec![0u32; nodes.len()];
for (label, v) in elements.iter().zip(values) {
let Some(e) = model.element(*label) else { continue };
for n in &e.nodes {
let Ok(i) = nodes.binary_search(n) else { continue };
if let (Some(s), Some(c)) = (sum.get_mut(i), count.get_mut(i)) {
for (a, b) in s.iter_mut().zip(v) {
*a += b;
}
*c += 1;
}
}
}
sum.into_iter().zip(count).map(|(s, c)| if c == 0 { [f64::NAN; 6] } else { s.map(|x| x / f64::from(c)) }).collect()
}
/// The smallest and largest finite value, with the nodes they are at (`labels` in the same
/// order as `values`).
pub fn range(labels: &[u32], values: &[f64]) -> Option<((u32, f64), (u32, f64))> {
let mut out: Option<((u32, f64), (u32, f64))> = None;
for (&l, &v) in labels.iter().zip(values).filter(|(_, v)| v.is_finite()) {
out = Some(match out {
None => ((l, v), (l, v)),
Some((lo, hi)) => (if v < lo.1 { (l, v) } else { lo }, if v > hi.1 { (l, v) } else { hi }),
});
}
out
}
pub fn magnitude(v: [f64; 3]) -> f64 {
(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
}
impl Session {
/// The latest result of a step and whether it describes the current state (`false`: the
/// model changed since it was solved; it is stale).
pub fn latest_result(&self, step: &str) -> Option<(&std::sync::Arc<StepResult>, bool)> {
self.results.get(step).map(|(revision, r)| (r, *revision == self.revision))
}
/// What the results display shows: the view, whether its result is stale, the contour
/// field's range, a frequency step's modes and an explicit step's frame times (the
/// `results.*` commands' reply).
pub fn results_report(&self) -> Value {
let Some(v) = &self.results_view else { return json!({"view": null}) };
let Some((r, current)) = self.latest_result(&v.step) else { return json!({"view": v}) };
let mut out = json!({"view": v, "stale": !current});
if let Some(f) = v.contour
&& let Ok(values) = field_values(&self.model, r, f, v.pick())
{
let ends =
range(r.nodes(), &values).map(|((ln, lv), (hn, hv))| json!({"min": {"node": ln, "value": lv}, "max": {"node": hn, "value": hv}}));
out["contour"] = json!({"field": f, "label": f.label(), "range": ends});
}
match r.kind() {
Kind::Frequency(fr) => {
out["modes"] = fr.modes.iter().enumerate().map(|(i, m)| json!({"mode": i + 1, "frequency": m.frequency})).collect();
}
Kind::Explicit(e) => {
out["frames"] = e.frames.iter().map(|f| f.time).collect();
if let Ok(f) = frame(e, v.pick()) {
out["time"] = json!(f.time);
}
}
Kind::Static(_) => {}
}
out
}
/// Every step with a result, current or stale, by name.
pub fn result_steps(&self) -> impl Iterator<Item = (&str, bool)> {
self.results.iter().map(|(name, (revision, _))| (name.as_str(), *revision == self.revision))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cmd::solve::tests::cantilever;
use serde_json::json;
#[test]
fn fields_follow_the_result() {
let mut s = Session::default();
cantilever(&mut s);
s.execute("solve.static", &json!({})).unwrap();
s.execute("solve.frequency", &json!({})).unwrap();
let (bend, current) = s.latest_result("Bend").unwrap();
assert!(current);
let mag = field_values(&s.model, bend, Field::Displacement, Pick { mode: 1, frame: None }).unwrap();
let uz = field_values(&s.model, bend, Field::DisplacementZ, Pick { mode: 1, frame: None }).unwrap();
assert_eq!(mag.len(), bend.nodes().len());
for (m, z) in mag.iter().zip(&uz) {
assert!(*m >= z.abs() - 1e-12);
}
// The tip bends down the most; the clamped root does not move.
let ((lo_node, lo), (_, hi)) = range(bend.nodes(), &uz).unwrap();
assert!(lo < -3.8 && hi.abs() < 1e-12, "{lo} {hi}");
assert!([11, 22, 33, 44].contains(&lo_node), "{lo_node}");
let vm = field_values(&s.model, bend, Field::VonMises, Pick { mode: 1, frame: None }).unwrap();
let max = field_values(&s.model, bend, Field::MaxPrincipal, Pick { mode: 1, frame: None }).unwrap();
let min = field_values(&s.model, bend, Field::MinPrincipal, Pick { mode: 1, frame: None }).unwrap();
for ((v, a), b) in vm.iter().zip(&max).zip(&min) {
assert!(a >= b && *v >= 0.0, "{v} {a} {b}");
}
let (modes, _) = s.latest_result("Modes").unwrap();
assert_eq!(Field::available(modes).count(), 4);
assert_eq!(Field::available(bend).count(), Field::ALL.len() - 1, "all but velocity");
assert!(field_values(&s.model, modes, Field::VonMises, Pick { mode: 1, frame: None }).is_err());
assert!(field_values(&s.model, modes, Field::Displacement, Pick { mode: 9, frame: None }).is_err());
assert_eq!(field_values(&s.model, modes, Field::Displacement, Pick { mode: 2, frame: None }).unwrap().len(), modes.nodes().len());
// An edit leaves the results stale, not gone.
s.execute("model.material.edit", &json!({"name": "Mat", "elastic": {"type": "isotropic", "young": 2000, "poisson": 0}, "density": 1}))
.unwrap();
assert!(s.result("Bend").is_none());
assert_eq!(s.latest_result("Bend").map(|(_, current)| current), Some(false));
assert_eq!(s.result_steps().collect::<Vec<_>>(), vec![("Bend", false), ("Modes", false)]);
}
#[test]
fn field_ids_round_trip() {
for f in Field::ALL {
assert_eq!(Field::parse(f.id()), Some(f));
assert_eq!(serde_json::to_value(f).unwrap(), json!(f.id()));
}
assert_eq!(Field::parse("nope"), None);
assert_eq!(range(&[1, 2], &[f64::NAN, f64::INFINITY]), None);
assert_eq!(range(&[1, 2, 3], &[2.0, -1.0, 5.0]), Some(((2, -1.0), (3, 5.0))));
}
}