aligrator

Crates.ioaligrator
lib.rsaligrator
version0.1.2
created_at2025-06-27 09:52:48.903121+00
updated_at2025-07-01 08:27:48.904911+00
descriptionA lightweight numerical integration library.
homepage
repositoryhttps://github.com/alexlovric/aligrator
max_upload_size
id1728400
size128,297
Aleksander Lovric (alexlovric)

documentation

https://docs.rs/aligrator

README

Aligrator

Build & Test

A lightweight, dependency-free Rust library for numerical integration of ordinary differential equations (ODEs). Supports multiple Runge-Kutta multistage integrators and is easily extensible.

Features

  • Forward Euler
  • Rk4, Rk45, Rk89
  • Adaptive time-stepping

Example: Solving a Simple Harmonic Oscillator

This example demonstrates how to use Aligrator to solve the initial value problem (IVP) for a simple 1D harmonic oscillator (SHO) with equation of motion:

x''(t) + \omega^2x(t) = 0

1. Define the ODE System

/// Simple Harmonic Oscillator (SHO) implementation.
struct SimpleHarmonicOscillator {
    omega: f64,
}

impl IvpFunction<1> for SimpleHarmonicOscillator {
    fn compute(&mut self, t: &f64, x: &[f64; 1], xdot: &[f64; 1]) -> [f64; 1] {
        // x''(t) = -ω²x(t)
        [-self.omega.powi(2) * x[0]]
    }
}
  • Note: Aligrator requires that you implement the IvpFunction trait for your ODE system.

2. Set Initial Conditions

let x0 = [1.0];      // Initial position
let xdot0 = [0.0];   // Initial velocity
let t0 = 0.0;        // Start time
let tf = 15.0;       // End time
let dt = 1.0;        // Initial time step

3. Choose and Configure the Integrator

let mut integrator = Rk89::new(dt, None); // Adaptive disabled (second argument)
  • Here we use the high-order Runge-Kutta 8/9 method.
  • For adaptive time-stepping, pass Some(AdaptiveDt::new(Some(1e-6), None, None)) as the second argument. Here the first argument is tolerance, the second is minimum time step, and the third is maximum time step.

4. Integrate the System

let (times, positions, _) = integrate(
    &mut integrator,
    &mut SimpleHarmonicOscillator { omega: 1.0 },
    x0,
    xdot0,
    t0,
    tf,
);
  • Solves the IVP from t0 to tf.
  • Returns time points, positions, and velocities (not used here).
  • Here we use the integrate function to manage the looping, but you can easily just call the integrator.step(...) in your own integration loop.

The response should look like this: Response

If we check the accuracy, its consistent with what we expect from this integrator: Order

6. Run the Example

Build and run the example with:

cargo run --example sho_response

License

MIT

Commit count: 0

cargo fmt