Skip to main content

Module scripted

Module scripted 

Source
Expand description

Dynamical systems written in a script, reloaded while the program runs.

The built-in AttractorTypes are compiled in. A ScriptedSystem is the same idea with the equations in a Rhai script instead, so you can try a new system, or retune sigma and rho, without recompiling, and see it change as soon as the file is saved.

A script sees the state as x, y, z and the time as t, all floats, and ends with the derivative as a three element array:

// Lorenz
let sigma = 10.0;
let rho = 28.0;
let beta = 8.0 / 3.0;
[sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

Rhai’s standard maths is available (sin, cos, exp, sqrt, abs, PI() and so on). Integer division is integer division: write 8.0 / 3.0, not 8 / 3. Every evaluation is capped at a fixed number of operations, so a script with an endless loop returns an error instead of freezing the frame.

use proof_engine::math::scripted::ScriptedSystem;
use proof_engine::prelude::Vec3;

let thomas = ScriptedSystem::compile("
    let b = 0.208186;
    [sin(y) - b * x, sin(z) - b * y, sin(x) - b * z]
").unwrap();
let mut p = Vec3::new(0.1, 0.0, 0.0);
for i in 0..1000 {
    p = thomas.rk4_step(p, i as f32 * 0.05, 0.05).unwrap();
}
assert!(p.length() < 6.0, "Thomas' attractor is bounded");

Structs§

ScriptedError
A script that failed to load, compile or run.
ScriptedSystem
A three-dimensional ODE dp/dt = f(p, t) whose f is a Rhai script.