Skip to content

ode #

vsl.ode

solve_rk23 integrates first-order systems with an adaptive Bogacki-Shampine 3(2) explicit Runge-Kutta method. It supports vector states, forward or backward integration, error tolerances, a maximum step size, and execution limits. The method is intended for non-stiff systems.

Set RK23Params.output_times to request states at intermediate times. Times must be ordered in the integration direction and lie within the integration interval. The solver uses cubic Hermite interpolation over accepted steps; duplicate times are allowed. RK23Result.sampled_times and RK23Result.sampled_states contain completed samples in the same order. If a step/evaluation limit stops the solve early, only samples through the last accepted step are returned.

import math
import vsl.ode

fn exponential_system(_ f64, state []f64, _ []f64) []f64 {
    return [state[0]]
}

result := ode.solve_rk23(exponential_system, ode.RK23Params{
    t_start:       0.0
    t_end:         1.0
    initial_state: [1.0]
    output_times:  [0.25, 0.75]
    abs_tol:       1e-9
    rel_tol:       1e-7
})!
assert math.abs(result.state[0] - math.e) < 1e-5
assert result.sampled_times == [0.25, 0.75]
assert math.abs(result.sampled_states[0][0] - math.exp(0.25)) < 1e-4

The result includes the last accepted state, scaled local error norm, function evaluation count, accepted/rejected step counts, and a termination status. Requested-time interpolation is opt-in and does not retain a full trajectory. This first ODE slice does not provide stiff methods or event handling.

fn solve_rk23 #

fn solve_rk23(f ODEFunction, params RK23Params) !RK23Result

solve_rk23 integrates y'=f(t,y) from t_start to t_end using adaptive Bogacki-Shampine 3(2) steps. It is intended for non-stiff systems.

fn RK23Status.from #

fn RK23Status.from[W](input W) !RK23Status

from returns the RK23Status value of an integer or of a field name, or an error if there is none.

type ODEFunction #

type ODEFunction = fn (t f64, state []f64, params []f64) []f64

ODEFunction computes the derivative vector for the state at time t.

enum RK23Status #

enum RK23Status {
	converged
	max_steps
	max_evaluations
	non_finite_function_value
	invalid_function_output
	precision_limit
	non_finite_estimate
}

RK23Status reports why the adaptive integrator stopped.

struct RK23Params #

@[params]
struct RK23Params {
pub:
	t_start         f64
	t_end           f64
	initial_state   []f64 @[required]
	function_params []f64
	output_times    []f64
	initial_step    f64
	max_step        f64
	abs_tol         f64 = 1e-9
	rel_tol         f64 = 1e-6
	max_steps       int = 100000
	max_evals       int = 400000
}

RK23Params configures an adaptive Bogacki-Shampine 3(2) initial-value solve.

struct RK23Result #

struct RK23Result {
pub:
	t              f64
	state          []f64
	sampled_times  []f64
	sampled_states [][]f64
	error_norm     f64
	n_calls        int
	n_steps        int
	n_accepted     int
	n_rejected     int
	status         RK23Status
}

RK23Result contains the last accepted state and adaptive-step diagnostics.