pub trait Integrator {
// Required method
fn step<R: Real, S: VectorSpace<R>, F: Fn(R, S) -> S>(
&self,
time: R,
state: &mut [S],
dt: R,
force: F,
) -> S;
// Provided method
fn init<R: Real, S: VectorSpace<R>, F: Fn(R, S) -> S>(
&self,
state: S,
_dt: R,
_force: F,
) -> Box<[S]> { ... }
}
Required Methods§
fn step<R: Real, S: VectorSpace<R>, F: Fn(R, S) -> S>( &self, time: R, state: &mut [S], dt: R, force: F, ) -> S
Provided Methods§
Sourcefn init<R: Real, S: VectorSpace<R>, F: Fn(R, S) -> S>(
&self,
state: S,
_dt: R,
_force: F,
) -> Box<[S]>
fn init<R: Real, S: VectorSpace<R>, F: Fn(R, S) -> S>( &self, state: S, _dt: R, _force: F, ) -> Box<[S]>
Examples found in repository?
examples/integral.rs (line 23)
6fn main() {
7
8 use std::f64::consts::*;
9
10 //
11 //Computes pi by numerically approximating the integral of e^(-x^2)
12 //
13
14 //the function e^(-t^2) that we are trying to integrate
15 fn f(t: f64, _y: f64) -> f64 { (-t*t).exp() }
16
17 let n = 1000; //the number of steps
18 let l = 100.0; //the size of the interval to integrate over
19 let dt = l / (n as f64);
20
21 //init
22 let mut t = -l/2.0;
23 let mut y1 = EULER.init(0.0, dt, &f);
24 let mut y2 = RK4.init(0.0, dt, &f);
25
26 for _ in 0..n {
27 println!("{} {} {}", EULER.step(t, y1.as_mut(), dt, &f), RK4.step(t, y2.as_mut(), dt, &f), PI.sqrt());
28 t += dt;
29 }
30
31 //the result should be sqrt(PI)
32 println!("{} {} {}", y1[0]*y1[0], y2[0]*y2[0], PI);
33
34
35}
More examples
examples/exp_comparison.rs (line 22)
8fn main() {
9
10 //
11 //A comparison between Euler, RK4, and intrinsics for computing the exponential of a real number
12 //
13
14 //the derivative of the exponential is itself
15 fn f(_t: f64, y: f64) -> f64 { y }
16
17 //the time-step
18 let dt = 0.125;
19
20 //the initial time and values
21 let mut t = 0.0;
22 let mut y1 = EULER.init(1.0, dt, &f);
23 let mut y2 = RK4.init(1.0, dt, &f);
24
25 //table column lables
26 for _ in 0..(9+11*3) { print!("_"); }
27 println!();
28 println!("| t| Euler| RK4|f64::exp()|");
29
30 for _ in 0..100 {
31 //compute the next step and print
32 println!(
33 "|{: >7.3}|{: >10.2}|{: >10.2}|{: >10.2}|",
34 t+dt,
35 EULER.step(t, y1.as_mut(), dt, &f),
36 RK4.step(t, y2.as_mut(), dt, &f),
37 (t+dt).exp()
38 );
39 t += dt;
40 }
41
42 for _ in 0..(9+11*3) { print!("_"); }
43 println!();
44
45
46}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.