Add diagram workbench UI with Modelica DoF coaching and ISO glyphs.

Ship the Next.js cycle editor with CAD chrome, technical HX symbols, Fixed/Free boundary guidance, and secondary water/air pressure drop support in the solver stack.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-17 22:46:46 +02:00
parent 62efea0646
commit 3358b74342
275 changed files with 70187 additions and 5230 deletions

View File

@@ -1,4 +1,4 @@
//! Integration tests for MacroComponent (Story 3.6).
//! Integration tests for MacroComponent (Story 3.6).
//!
//! Tests cover:
//! - AC #1: MacroComponent implements Component trait
@@ -73,12 +73,13 @@ fn make_port(fluid: &str, p: f64, h: f64) -> ConnectedPort {
}
/// Build a 4-component refrigerant cycle: A→B→C→D→A (4 edges).
/// Each component contributes 3 equations (2 thermo + 1 mass-flow) per CM1.3.
fn build_4_component_cycle() -> System {
let mut sys = System::new();
let a = sys.add_component(pass(2)); // compressor
let b = sys.add_component(pass(2)); // condenser
let c = sys.add_component(pass(2)); // valve
let d = sys.add_component(pass(2)); // evaporator
let a = sys.add_component(pass(3)); // compressor
let b = sys.add_component(pass(3)); // condenser
let c = sys.add_component(pass(3)); // valve
let d = sys.add_component(pass(3)); // evaporator
sys.add_edge(a, b).unwrap();
sys.add_edge(b, c).unwrap();
sys.add_edge(c, d).unwrap();
@@ -96,14 +97,14 @@ fn test_4_component_cycle_macro_creation() {
let internal = build_4_component_cycle();
let mc = MacroComponent::new(internal);
// 4 components × 2 eqs = 8 internal equations, 0 exposed ports
// 4 components × 3 equations = 12 internal equations (pass(3)×4), 0 exposed ports
assert_eq!(
mc.n_equations(),
8,
"should have 8 internal equations with no exposed ports"
12,
"should have 12 internal equations (4 components × 3 eqs) with no exposed ports"
);
// 4 edges × 2 vars = 8 internal state vars
assert_eq!(mc.internal_state_len(), 8);
// CM1.4: 4-edge series cycle → 1 branch + 4×2 P,h = 9 internal state vars
assert_eq!(mc.internal_state_len(), 9);
assert!(mc.get_ports().is_empty());
}
@@ -116,11 +117,11 @@ fn test_4_component_cycle_expose_two_ports() {
mc.expose_port(0, "refrig_in", make_port("R134a", 1e5, 4e5));
mc.expose_port(2, "refrig_out", make_port("R134a", 5e5, 4.5e5));
// 8 internal + 4 coupling (2 per port) = 12 equations
// 12 internal (4 components × 3 eqs) + 4 coupling (2 per port × 2 ports) = 16
assert_eq!(
mc.n_equations(),
12,
"should have 12 equations with 2 exposed ports"
16,
"should have 16 equations with 2 exposed ports"
);
assert_eq!(mc.get_ports().len(), 2);
assert_eq!(mc.port_mappings()[0].name, "refrig_in");
@@ -154,8 +155,10 @@ fn test_4_component_cycle_in_parent_system() {
assert_eq!(parent.node_count(), 2);
assert_eq!(parent.edge_count(), 1);
// Parent state vector: 1 edge × 2 = 2 state vars + 8 internal vars = 10 vars
assert_eq!(parent.state_vector_len(), 10);
// CM1.4: parent has 1 edge → 1 branch + 2 P,h = 3 parent edge vars.
// MacroComponent internal: 1 branch + 4×2 P,h = 9 internal vars.
// Total = 3 + 9 = 12.
assert_eq!(parent.state_vector_len(), 12);
}
// ─────────────────────────────────────────────────────────────────────────────
@@ -170,33 +173,33 @@ fn test_coupling_residuals_are_zero_at_consistent_state() {
mc.expose_port(0, "refrig_in", make_port("R134a", 1e5, 4e5));
// Internal block starts at offset 2 (2 parent-edge state vars before it).
// External edge for port 0 is at (p=0, h=1).
mc.set_global_state_offset(2);
mc.set_system_context(2, &[(0, 1)]);
// External edge occupies state[0..3]: m_ext=0, p_ext=1, h_ext=2.
// Internal block starts at offset 3 (3 parent-edge state vars before it).
mc.set_global_state_offset(3);
mc.set_system_context(3, &[(0, 1, 2)]);
// State layout: [P_ext=1e5, h_ext=4e5, P_int_e0=1e5, h_int_e0=4e5, ...]
// indices: 0 1 2 3
let mut state = vec![0.0; 2 + 8]; // 2 parent + 8 internal
state[0] = 1.0e5; // P_ext
state[1] = 4.0e5; // h_ext
state[2] = 1.0e5; // P_int_e0 (consistent with port)
state[3] = 4.0e5; // h_int_e0
// State layout: external edge (ṁ@0, P@1, h@2), internal block at offset 3:
// edge0: (ṁ@3, P@4, h@5), edge1: (ṁ@6, P@7, h@8), ...
let mut state = vec![0.0; 3 + 12]; // 3 parent + 12 internal (4 edges × 3)
state[1] = 1.0e5; // P_ext
state[2] = 4.0e5; // h_ext
state[4] = 1.0e5; // P_int_e0 (consistent with port: offset 3 + 1 = 4)
state[5] = 4.0e5; // h_int_e0 (consistent with port: offset 3 + 2 = 5)
let n_eqs = mc.n_equations(); // 8 + 2 = 10
let n_eqs = mc.n_equations(); // 12 internal + 2 coupling = 14
let mut residuals = vec![0.0; n_eqs];
mc.compute_residuals(&state, &mut residuals).unwrap();
// Coupling residuals at indices 8, 9 should be zero (consistent state)
// Coupling residuals at indices 12, 13 should be zero (consistent state)
assert!(
residuals[8].abs() < 1e-10,
residuals[12].abs() < 1e-10,
"P coupling residual should be 0, got {}",
residuals[8]
residuals[12]
);
assert!(
residuals[9].abs() < 1e-10,
residuals[13].abs() < 1e-10,
"h coupling residual should be 0, got {}",
residuals[9]
residuals[13]
);
}
@@ -206,29 +209,29 @@ fn test_coupling_residuals_nonzero_at_inconsistent_state() {
let mut mc = MacroComponent::new(internal);
mc.expose_port(0, "refrig_in", make_port("R134a", 1e5, 4e5));
mc.set_global_state_offset(2);
mc.set_system_context(2, &[(0, 1)]);
mc.set_global_state_offset(3);
mc.set_system_context(3, &[(0, 1, 2)]);
let mut state = vec![0.0; 10];
state[0] = 2.0e5; // P_ext (different from internal)
state[1] = 5.0e5; // h_ext
state[2] = 1.0e5; // P_int_e0
state[3] = 4.0e5; // h_int_e0
let mut state = vec![0.0; 15];
state[1] = 2.0e5; // P_ext (different from internal, p_ext=1)
state[2] = 5.0e5; // h_ext (h_ext=2)
state[4] = 1.0e5; // P_int_e0 (offset 3+1=4)
state[5] = 4.0e5; // h_int_e0 (offset 3+2=5)
let n_eqs = mc.n_equations();
let mut residuals = vec![0.0; n_eqs];
mc.compute_residuals(&state, &mut residuals).unwrap();
// Coupling: r[8] = P_ext - P_int = 2e5 - 1e5 = 1e5
// Coupling: r[12] = P_ext - P_int = 2e5 - 1e5 = 1e5
assert!(
(residuals[8] - 1.0e5).abs() < 1.0,
(residuals[12] - 1.0e5).abs() < 1.0,
"P coupling residual mismatch: {}",
residuals[8]
residuals[12]
);
assert!(
(residuals[9] - 1.0e5).abs() < 1.0,
(residuals[13] - 1.0e5).abs() < 1.0,
"h coupling residual mismatch: {}",
residuals[9]
residuals[13]
);
}
@@ -238,11 +241,11 @@ fn test_jacobian_coupling_entries_correct() {
let mut mc = MacroComponent::new(internal);
mc.expose_port(0, "refrig_in", make_port("R134a", 1e5, 4e5));
// external edge: (p_ext=0, h_ext=1), internal starts at offset=2
mc.set_global_state_offset(2);
mc.set_system_context(2, &[(0, 1)]);
// external edge: (m_ext=0, p_ext=1, h_ext=2), internal starts at offset=3
mc.set_global_state_offset(3);
mc.set_system_context(3, &[(0, 1, 2)]);
let state = vec![0.0; 10];
let state = vec![0.0; 15];
let mut jac = JacobianBuilder::new();
mc.jacobian_entries(&state, &mut jac).unwrap();
@@ -254,11 +257,11 @@ fn test_jacobian_coupling_entries_correct() {
.map(|&(_, _, v)| v)
};
// Coupling rows 8 (P) and 9 (h)
assert_eq!(find(8, 0), Some(1.0), "∂r_P/∂p_ext should be +1");
assert_eq!(find(8, 2), Some(-1.0), "∂r_P/∂int_p should be -1");
assert_eq!(find(9, 1), Some(1.0), "∂r_h/∂h_ext should be +1");
assert_eq!(find(9, 3), Some(-1.0), "∂r_h/∂int_h should be -1");
// Coupling rows 12 (P) and 13 (h); internal edge0 (P@offset+1=4, h@offset+2=5)
assert_eq!(find(12, 1), Some(1.0), "∂r_P/∂p_ext should be +1");
assert_eq!(find(12, 4), Some(-1.0), "∂r_P/∂int_p should be -1");
assert_eq!(find(13, 2), Some(1.0), "∂r_h/∂h_ext should be +1");
assert_eq!(find(13, 5), Some(-1.0), "∂r_h/∂int_h should be -1");
}
// ─────────────────────────────────────────────────────────────────────────────
@@ -273,15 +276,15 @@ fn test_macro_component_snapshot_serialization() {
mc.expose_port(2, "refrig_out", make_port("R134a", 5e5, 4.5e5));
mc.set_global_state_offset(0);
// Simulate a converged global state (8 internal vars, all nonzero)
let global_state: Vec<f64> = (0..8).map(|i| (i as f64 + 1.0) * 1e4).collect();
// CM1.4: 4-edge series cycle → internal_state_len = 1 branch + 4×2 P,h = 9 vars.
let global_state: Vec<f64> = (0..9).map(|i| (i as f64 + 1.0) * 1e4).collect();
let snap = mc
.to_snapshot(&global_state, Some("chiller_A".into()))
.expect("snapshot should succeed");
assert_eq!(snap.label.as_deref(), Some("chiller_A"));
assert_eq!(snap.internal_edge_states.len(), 8);
assert_eq!(snap.internal_edge_states.len(), 9);
assert_eq!(snap.port_names, vec!["refrig_in", "refrig_out"]);
// JSON round-trip
@@ -299,7 +302,7 @@ fn test_snapshot_fails_on_short_state() {
let mut mc = MacroComponent::new(internal);
mc.set_global_state_offset(0);
// Only 4 values, but internal needs 8
// Only 4 values, but internal needs 12
let short_state = vec![0.0; 4];
let snap = mc.to_snapshot(&short_state, None);
assert!(snap.is_none(), "should return None for short state vector");
@@ -349,27 +352,28 @@ fn test_two_macro_chillers_in_parallel_topology() {
result.err()
);
// 4 parent edges × 2 = 8 state variables in the parent
// 2 chillers × 8 internal variables = 16 internal variables
// Total state vector length = 24
assert_eq!(parent.state_vector_len(), 24);
// CM1.4: 4 parent edges form 2 series branches (S→A→M and S→B→M).
// Parent state: 2 branches + 4×2 P,h = 10 parent edge vars.
// 2 chillers × 9 internal vars (1 branch + 4×2 P,h each) = 18 internal vars.
// Total state vector length = 10 + 18 = 28.
assert_eq!(parent.state_vector_len(), 28);
// 4 nodes
assert_eq!(parent.node_count(), 4);
// 4 edges
assert_eq!(parent.edge_count(), 4);
// Total equations:
// chiller_a: 8 internal + 4 coupling (2 ports) = 12
// chiller_b: 8 internal + 4 coupling (2 ports) = 12
// Total component equations (CM1.3):
// chiller_a: 12 internal (4 components × 3 eqs) + 4 coupling (2 ports × 2) = 16
// chiller_b: 12 internal + 4 coupling = 16
// splitter: 1
// merger: 1
// total: 26
// total: 34
let total_eqs: usize = parent
.traverse_for_jacobian()
.map(|(_, c, _)| c.n_equations())
.sum();
assert_eq!(
total_eqs, 26,
total_eqs, 34,
"total equation count mismatch: {}",
total_eqs
);
@@ -392,8 +396,8 @@ fn test_two_macro_chillers_residuals_are_computable() {
mc
};
// Each chiller has 8 internal state variables (4 edges × 2)
let internal_state_len_each = chiller_a.internal_state_len(); // = 8
// CM1.4: each chiller has 9 internal state variables (1 branch + 4×2 P,h)
let _internal_state_len_each = chiller_a.internal_state_len(); // = 9
let mut parent = System::new();
let ca = parent.add_component(Box::new(chiller_a));
@@ -406,20 +410,23 @@ fn test_two_macro_chillers_residuals_are_computable() {
parent.add_edge(cb, merger).unwrap();
parent.finalize().unwrap();
// The parent's own state vector covers its 4 edges (8 vars).
// CM1.4: parent has 4 edges forming 2 series branches → 2 + 4×2 = 10 parent vars.
// Each MacroComponent's internal state block starts at offsets assigned cumulatively
// by System::finalize().
// chiller_a offset = 8
// chiller_b offset = 16
// Total state len = 8 parent + 8 chiller_a + 8 chiller_b = 24 total.
// chiller_a offset = 10 (after parent edge state)
// chiller_b offset = 19 (after parent + chiller_a)
// Total state len = 10 parent + 9 chiller_a + 9 chiller_b = 28 total.
let full_state_len = parent.state_vector_len();
assert_eq!(full_state_len, 24);
assert_eq!(full_state_len, 28);
let state = vec![0.0; full_state_len];
// Residual vector must cover every component equation plus the parent's own
// per-edge mass-flow closures (CM1.2).
let total_eqs: usize = parent
.traverse_for_jacobian()
.map(|(_, c, _)| c.n_equations())
.sum();
.sum::<usize>()
+ parent.mass_flow_closure_count();
let mut residuals = vec![0.0; total_eqs];
let result = parent.compute_residuals(&state, &mut residuals);
assert!(