Added some benchmarking and small performance improvements

This commit is contained in:
Connor Johnstone
2025-08-11 18:34:01 -04:00
parent e27ef0a07c
commit 9075dac669
5 changed files with 141 additions and 13 deletions

View File

@@ -18,7 +18,7 @@ pub mod prelude {
mod tests {
use crate::prelude::*;
use approx::assert_relative_eq;
use nalgebra::{Vector2, Vector6};
use nalgebra::{Vector1, Vector2, Vector6};
use std::f64::consts::PI;
#[test]
@@ -54,6 +54,32 @@ mod tests {
let _interpolated_answer = solution.interpolate(4.4);
}
#[test]
fn test_correctness() {
// Define the system (parameters, derivative, and initial state)
type Params = ();
let params = ();
fn derivative(_t: f64, y: Vector1<f64>, _p: &Params) -> Vector1<f64> {
Vector1::new(5.0 * y[0] - 3.0)
}
let y0 = Vector1::new(1.0);
// Set up the problem (ODE, Integrator, Controller, and Callbacks)
let ode = ODE::new(&derivative, 2.0, 3.0, y0, params);
let dp45 = DormandPrince45::new(1e-8_f64, 1e-8_f64);
let controller = PIController::default();
// Solve the problem
let mut problem = Problem::new(ode, dp45, controller);
let solution = problem.solve();
for (time, state) in solution.times.iter().zip(solution.states.iter()) {
let exact = 0.4 * (5.0 * (time - 2.0)).exp() + 0.6;
assert_relative_eq!(state[0], exact, max_relative = 1e-7);
}
}
#[test]
fn test_orbit() {
// Calculate one period