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.
87 lines
3.1 KiB
Rust
87 lines
3.1 KiB
Rust
//! Shared deterministic data fixtures for the model-selection integration tests.
|
|
//!
|
|
//! Every trajectory is produced by a fixed-step classical RK4 integrator with no
|
|
//! randomness, so the datasets — and therefore every downstream selection report
|
|
//! — are bit-reproducible run to run.
|
|
|
|
use lawsynth_core::Identifier;
|
|
use lawsynth_data::{Dataset, NumericColumn, TimeAxis};
|
|
|
|
/// Builds an identifier from a static name.
|
|
pub fn id(name: &str) -> Identifier {
|
|
Identifier::new(name).unwrap()
|
|
}
|
|
|
|
/// One RK4 step of a 2-D autonomous system `f(x, v) -> (dx, dv)`.
|
|
fn rk4_step_2d(f: impl Fn(f64, f64) -> (f64, f64), x: f64, v: f64, dt: f64) -> (f64, f64) {
|
|
let (k1x, k1v) = f(x, v);
|
|
let (k2x, k2v) = f(x + dt * k1x / 2.0, v + dt * k1v / 2.0);
|
|
let (k3x, k3v) = f(x + dt * k2x / 2.0, v + dt * k2v / 2.0);
|
|
let (k4x, k4v) = f(x + dt * k3x, v + dt * k3v);
|
|
(
|
|
x + dt * (k1x + 2.0 * k2x + 2.0 * k3x + k4x) / 6.0,
|
|
v + dt * (k1v + 2.0 * k2v + 2.0 * k3v + k4v) / 6.0,
|
|
)
|
|
}
|
|
|
|
/// One RK4 step of a scalar autonomous system `f(x) -> dx`.
|
|
fn rk4_step_1d(f: impl Fn(f64) -> f64, x: f64, dt: f64) -> f64 {
|
|
let k1 = f(x);
|
|
let k2 = f(x + dt * k1 / 2.0);
|
|
let k3 = f(x + dt * k2 / 2.0);
|
|
let k4 = f(x + dt * k3);
|
|
x + dt * (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0
|
|
}
|
|
|
|
/// Integrates a 2-D system into an aligned `(x, v)` dataset.
|
|
fn integrate_2d(
|
|
f: impl Fn(f64, f64) -> (f64, f64),
|
|
x0: f64,
|
|
v0: f64,
|
|
dt: f64,
|
|
steps: usize,
|
|
) -> Dataset {
|
|
let mut xs = vec![x0];
|
|
let mut vs = vec![v0];
|
|
for _ in 0..steps {
|
|
let (nx, nv) = rk4_step_2d(&f, *xs.last().unwrap(), *vs.last().unwrap(), dt);
|
|
xs.push(nx);
|
|
vs.push(nv);
|
|
}
|
|
let time = (0..xs.len()).map(|k| k as f64 * dt).collect::<Vec<_>>();
|
|
Dataset::new(
|
|
TimeAxis::new(time).unwrap(),
|
|
[NumericColumn::new(id("x"), xs), NumericColumn::new(id("v"), vs)],
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
/// A **linear** damped oscillator (true polynomial degree 1):
|
|
/// `dx/dt = v`, `dv/dt = -0.4 v - x`. Oscillatory and decaying, so every fold
|
|
/// segment carries variance to score against.
|
|
pub fn linear_oscillator() -> Dataset {
|
|
integrate_2d(|x, v| (v, -0.4 * v - x), 1.5, 0.0, 0.02, 600)
|
|
}
|
|
|
|
/// An **unforced damped cubic (Duffing)** oscillator (true polynomial degree 3):
|
|
/// `dx/dt = v`, `dv/dt = -0.3 v - x - x^3`. The cubic restoring term is
|
|
/// significant at the chosen amplitude, so a degree-1 or degree-2 library cannot
|
|
/// represent it. Single-well and damped, hence non-chaotic and simulatable.
|
|
pub fn cubic_oscillator() -> Dataset {
|
|
integrate_2d(|x, v| (v, -0.3 * v - x - x.powi(3)), 1.5, 0.0, 0.02, 600)
|
|
}
|
|
|
|
/// A scalar **logistic** system (true polynomial degree 2): `dx/dt = x - x^2`.
|
|
/// The saturating transient is nonlinear, so degree 1 underfits while degree 2
|
|
/// recovers it.
|
|
pub fn logistic() -> Dataset {
|
|
let dt = 0.02;
|
|
let steps = 300;
|
|
let mut xs = vec![0.15];
|
|
for _ in 0..steps {
|
|
xs.push(rk4_step_1d(|x| x - x * x, *xs.last().unwrap(), dt));
|
|
}
|
|
let time = (0..xs.len()).map(|k| k as f64 * dt).collect::<Vec<_>>();
|
|
Dataset::new(TimeAxis::new(time).unwrap(), [NumericColumn::new(id("x"), xs)]).unwrap()
|
|
}
|