Batch 11 of expansion-v2. Extends discovery to spatiotemporal + conservation
structure, adds principled hyperparameter selection, and surfaces analysis in
the web app. Deterministic/offline/std-only (Rust) or typed/tested (TS).
crates/lawsynth-pde (new): evolution-PDE discovery (PDE-FIND). Central-difference
spatial/time derivatives on the interior + differential-term library (uᵖ·D_m) +
STLSQ on flattened u_t. Recovers heat (0.2002·u_xx), advection (−0.801·u_x), and
Burgers (−1.000·u·u_x + 0.100·u_xx) from snapshots; two-mode fixtures break the
u/u_xx collinearity; a finer-grid test confirms O(dx²). 32 tests.
crates/lawsynth-invariants (new): conserved-quantity detection. H parametrized
over a library; conserved ⇔ ∇H·f=0; the SVD nullspace of the sampled
Lie-derivative matrix yields invariants (constant excluded, canonically
normalized). Recovers x²+y² energy, two independent invariants for a decoupled
system, y²−2cos x for the pendulum (trig basis), and honestly returns EMPTY for
a damped oscillator (no false positive). 21 tests.
crates/lawsynth-modelselect (new): time-series cross-validated hyperparameter
selection. Forward-chaining/rolling folds (no shuffling), discover-on-train +
simulate-on-test predictive scoring via lawsynth-score, full auditable table,
simpler-model tie-break. Selects degree 3 for cubic Duffing, degree 1 for a
linear system, degree 2 for logistic. 27 tests.
apps/studio: analysis module surfacing all seven engine reports (stability/
control/domains/bifurcation/sensitivity/estimate/reduce) as UI view models via
the api-client parsers, wired as a navigable route. Honest cases handled
(inconclusive classification, empty fixed-point set, null validation). 16 groups.
specs/{pde-discovery,invariant-detection,model-selection}: boundary specs.
63 lines
2.5 KiB
Rust
63 lines
2.5 KiB
Rust
//! Heat equation `u_t = α u_xx` from an exact two-mode analytic field.
|
||
//!
|
||
//! Because the field is an exact solution (no solver error), the only error is
|
||
//! the `O(dx²)`/`O(dt²)` truncation of the discovery's own finite differences.
|
||
//! A finer grid tightens the recovered `α`.
|
||
|
||
mod common;
|
||
|
||
use common::heat_two_mode;
|
||
use lawsynth_pde::{PdeConfig, discover_pde};
|
||
|
||
const ALPHA: f64 = 0.2;
|
||
|
||
fn heat_model(nx: usize, nt: usize) -> lawsynth_pde::PdeModel {
|
||
// Modes k1 = 1, k2 = 2 break the single-mode u/u_xx collinearity.
|
||
let fixture = heat_two_mode(ALPHA, 1.0, 2.0, 0.5, nx, nt, 0.01);
|
||
discover_pde(&fixture.field, fixture.dx, fixture.dt, &PdeConfig::default()).unwrap()
|
||
}
|
||
|
||
#[test]
|
||
fn recovers_the_diffusion_coefficient() {
|
||
let model = heat_model(96, 40);
|
||
let coefficient = model.coefficient_of(0, 2);
|
||
assert!((coefficient - ALPHA).abs() < 0.02, "u_xx coefficient {coefficient} not ≈ α = {ALPHA}");
|
||
}
|
||
|
||
#[test]
|
||
fn discovered_law_is_a_single_uxx_term() {
|
||
let model = heat_model(96, 40);
|
||
// Exactly one active term, and it is u_xx (u_power 0, derivative order 2).
|
||
let active: Vec<_> = model.active_terms().collect();
|
||
assert_eq!(active.len(), 1, "expected a single term, got {}", model.describe());
|
||
assert_eq!(active[0].u_power, 0);
|
||
assert_eq!(active[0].derivative_order, 2);
|
||
assert_eq!(active[0].label, "u_xx");
|
||
}
|
||
|
||
#[test]
|
||
fn spurious_terms_are_negligible() {
|
||
let model = heat_model(96, 40);
|
||
// No advection, no reaction: u_x, u, u², u·u_x all ~ 0.
|
||
assert!(model.coefficient_of(0, 1).abs() < 1e-3, "spurious u_x");
|
||
assert!(model.coefficient_of(1, 0).abs() < 1e-3, "spurious u");
|
||
assert!(model.coefficient_of(2, 0).abs() < 1e-3, "spurious u^2");
|
||
assert!(model.coefficient_of(1, 1).abs() < 1e-3, "spurious u*u_x");
|
||
}
|
||
|
||
#[test]
|
||
fn finer_grid_tightens_recovery() {
|
||
// Honest truncation claim: refining dx reduces the coefficient error.
|
||
let coarse = (heat_model(48, 40).coefficient_of(0, 2) - ALPHA).abs();
|
||
let fine = (heat_model(128, 40).coefficient_of(0, 2) - ALPHA).abs();
|
||
assert!(fine <= coarse, "finer grid {fine} should not be worse than coarse {coarse}");
|
||
}
|
||
|
||
#[test]
|
||
fn residual_is_small_relative_to_the_dynamics() {
|
||
let model = heat_model(96, 40);
|
||
// A genuine fit: RSS per interior point is tiny next to the u_t scale.
|
||
let mean_squared_residual = model.residual_sum_squares / model.interior_points as f64;
|
||
assert!(mean_squared_residual < 1e-4, "mean squared residual {mean_squared_residual}");
|
||
}
|