Errors still aren't quite right

This commit is contained in:
Connor Johnstone
2023-03-10 17:57:18 -07:00
parent fc96e2e9be
commit 2ec474a77a
4 changed files with 52 additions and 34 deletions

View File

@@ -11,38 +11,55 @@ pub mod problem;
mod tests {
use nalgebra::Vector6;
use approx::assert_relative_eq;
use crate::integrator::{AdaptiveSixthOrder, RUNGE_KUTTA_FEHLBERG_54_TABLEAU};
use crate::integrator::*;
use crate::controller::{PIController};
use crate::ode::{SystemTrait, ODE};
use crate::problem::Problem;
use std::f64::consts::PI;
#[test]
fn test_ode_creation() {
fn test_orbit() {
struct System {
mu: f64,
}
impl SystemTrait<f64,6> for System {
fn derivative(&self, _t: f64, state: Vector6<f64>) -> Vector6<f64> {
let acc = -(self.mu * state.fixed_rows<3>(0)) / (state.fixed_rows<3>(0).norm.powi(3));
let acc = -(self.mu * state.fixed_rows::<3>(0)) / (state.fixed_rows::<3>(0).norm().powi(3));
Vector6::new(state[3], state[4], state[5], acc[0], acc[1], acc[2])
}
}
let system = System { mu: 3.98600441500000e14 };
let y0 = Vector3::new(1.0, 0.0, 0.0);
let ode = ODE::new(system, 0.0, 10.0, y0);
let controller = PIController::new(0.17, 0.04, 10.0, 0.2, 10.0, 0.9, 1e-4);
let y0 = Vector6::new(
4.26387250e+06,
5.14619397e+06,
1.13102192e+06,
-5.92345023e+03,
4.49679662e+03,
1.87038714e+03,
);
let a = 6.7781363e6_f64;
let period = 2.0 * PI * (a.powi(3)/system.mu).sqrt();
println!("{}", period);
let ode = ODE::new(system, 0.0, period, y0);
let controller = PIController::new(0.17, 0.04, 10.0, 0.2, 10000.0, 0.9, 1.0);
let rkf54 = AdaptiveSixthOrder {
tableau: RUNGE_KUTTA_FEHLBERG_54_TABLEAU,
atol: 1e-8_f64,
rtol: 1e-8_f64,
atol: 1e-12_f64,
rtol: 1e-7_f64,
};
let mut problem = Problem::new(ode, rkf54, controller);
let solution = problem.solve();
solution.times.iter().zip(solution.states.iter()).for_each(|(time, state)| {
assert_relative_eq!(state[0], time.exp(), max_relative=1e-7);
})
println!("{}", solution.times.len());
// panic!();
assert_relative_eq!(solution.times[solution.states.len()-1], period, max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][0], y0[0], max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][1], y0[1], max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][2], y0[2], max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][3], y0[3], max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][4], y0[4], max_relative=1e-7);
assert_relative_eq!(solution.states[solution.states.len()-1][5], y0[5], max_relative=1e-7);
}
}