bacon-sci 0.16.2

Scientific computing in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/* This file is part of bacon.
 * Copyright (c) Wyatt Campbell.
 *
 * See repository LICENSE for information.
 */

use crate::{BVector, Dimension, DimensionError};
use nalgebra::{allocator::Allocator, ComplexField, DefaultAllocator, Dim, RealField, U1};
use num_traits::{FromPrimitive, Zero};
use std::{error::Error, marker::PhantomData};
use thiserror::Error;

pub mod adams;
pub mod bdf;
pub mod rk;

/// Status returned from the [`IVPStepper`]
/// Used by the [`IVPIterator`] struct to correctly step through
/// the IVP solution.
#[derive(Error, Clone, Debug)]
pub enum IVPStatus<T: Error> {
    #[error("the solver needs the step to be re-done")]
    Redo,
    #[error("the solver is complete")]
    Done,
    #[error("unspecified solver error: {0}")]
    Failure(#[from] T),
}

/// An error generated in a derivative function
pub type UserError = Box<dyn Error>;

/// A function that can be used as a derivative for the solver
pub trait Derivative<N: ComplexField + Copy, D: Dim, T: Clone>:
    FnMut(N::RealField, &[N], &mut T) -> Result<BVector<N, D>, UserError>
where
    DefaultAllocator: Allocator<N, D>,
{
}

impl<N, D: Dim, T, F> Derivative<N, D, T> for F
where
    N: ComplexField + Copy,
    T: Clone,
    F: FnMut(N::RealField, &[N], &mut T) -> Result<BVector<N, D>, UserError>,
    DefaultAllocator: Allocator<N, D>,
{
}

#[derive(Error, Debug)]
pub enum IVPError {
    #[error("the solver does not have all required parameters set")]
    MissingParameters,
    #[error("the solver hit an error from the user-provided derivative function: {0}")]
    UserError(#[from] UserError),
    #[error("the provided tolerance was out-of-bounds")]
    ToleranceOOB,
    #[error("the provided time delta was out-of-bounds")]
    TimeDeltaOOB,
    #[error("the provided ending time was before the provided starting time")]
    TimeEndOOB,
    #[error("the provided starting time was after the provided ending time")]
    TimeStartOOB,
    #[error("a conversion from a necessary primitive failed")]
    FromPrimitiveFailure,
    #[error("the time step fell below the paramater minimum allowed value")]
    MinimumTimeDeltaExceeded,
    #[error("the number of iterations exceeded the maximum allowable")]
    MaximumIterationsExceeded,
    #[error("a matrix was unable to be inverted")]
    SingularMatrix,
    #[error("attempted to build a dynamic solver with static dimension")]
    DynamicOnStatic,
    #[error("attempted to build a static solver with dynamic dimension")]
    StaticOnDynamic,
}

impl From<UserError> for IVPStatus<IVPError> {
    fn from(value: UserError) -> Self {
        Self::Failure(IVPError::UserError(value))
    }
}

impl From<DimensionError> for IVPError {
    fn from(value: DimensionError) -> Self {
        match value {
            DimensionError::DynamicOnStatic => Self::DynamicOnStatic,
            DimensionError::StaticOnDynamic => Self::StaticOnDynamic,
        }
    }
}

/// A type alias for a Result of a [`IVPStepper`] step
/// Ok is a tuple of the time and solution at that time
/// Err is an IVPError
pub type Step<R, C, D, E> = Result<(R, BVector<C, D>), IVPStatus<E>>;

/// Implementing this trait is providing the main functionality of
/// an initial value problem solver. This should be used only when
/// implementing an [`IVPSolver`], users should use the solver via the [`IVPSolver`]
/// trait's interface.
pub trait IVPStepper<D: Dimension>: Sized
where
    DefaultAllocator: Allocator<Self::Field, D>,
{
    /// Error type. IVPError must be able to convert to the error type.
    type Error: Error + From<IVPError>;
    /// The field, complex or real, that the solver is operating on.
    type Field: ComplexField + Copy;
    /// The real field associated with the solver's Field.
    type RealField: RealField;
    /// Arbitrary data provided by the user for the derivative function
    /// It must be clone because for any intermediate time steps (e.g. in runge-kutta)
    /// gives the derivative function a clone of the params: only normal time steps get to update
    /// the internal UserData state
    type UserData: Clone;

    /// Step forward in the solver.
    /// The solver may request a step be redone, signal that the
    /// solution is finished, or give the value of the next solution value.
    fn step(&mut self) -> Step<Self::RealField, Self::Field, D, Self::Error>;

    /// Get the current time of the solver.
    fn time(&self) -> Self::RealField;
}

/// Trait covering all initial value problem solvers.
/// Build up the solver using the parameter builder functions and then use solve.
///
/// This is used as a builder pattern, setting parameters of the solver.
/// [`IVPSolver`] implementations should implement a step function that
/// returns an IVPStatus, then a blanket impl will allow it to be used as an
/// IntoIterator for the user to iterate over the results.
pub trait IVPSolver<'a, D: Dimension>: Sized
where
    DefaultAllocator: Allocator<Self::Field, D>,
{
    /// Error type. IVPError must be able to convert to the error type.
    type Error: Error + From<IVPError>;
    /// The field, complex or real, that the solver is operating on.
    type Field: ComplexField + Copy;
    /// The real field associated with the solver's Field.
    type RealField: RealField;
    /// Arbitrary data provided by the user for the derivative function
    type UserData: Clone;
    /// The type signature of the derivative function to use
    type Derivative: Derivative<Self::Field, D, Self::UserData> + 'a;
    /// The type that actually does the solving.
    type Solver: IVPStepper<
        D,
        Error = Self::Error,
        Field = Self::Field,
        RealField = Self::RealField,
        UserData = Self::UserData,
    >;

    /// Create the solver.
    /// Will fail for dynamically sized solvers
    fn new() -> Result<Self, Self::Error>;

    /// Create the solver with a run-time dimension.
    /// Will fail for statically sized solvers
    fn new_dyn(size: usize) -> Result<Self, Self::Error>;

    /// Gets the dimension of the solver
    fn dim(&self) -> D;

    /// Set the error tolerance for any condition needing needing a float epsilon
    fn with_tolerance(self, tol: Self::RealField) -> Result<Self, Self::Error>;

    fn with_maximum_dt(self, max: Self::RealField) -> Result<Self, Self::Error>;
    fn with_minimum_dt(self, min: Self::RealField) -> Result<Self, Self::Error>;
    fn with_initial_time(self, initial: Self::RealField) -> Result<Self, Self::Error>;
    fn with_ending_time(self, ending: Self::RealField) -> Result<Self, Self::Error>;

    /// The initial conditions of the problem, should reset any previous values.
    fn with_initial_conditions_slice(self, start: &[Self::Field]) -> Result<Self, Self::Error> {
        let svector = BVector::from_column_slice_generic(self.dim(), U1::from_usize(1), start);
        self.with_initial_conditions(svector)
    }

    /// The initial conditions of the problem, in a BVector. Should reset any previous values.
    fn with_initial_conditions(self, start: BVector<Self::Field, D>) -> Result<Self, Self::Error>;

    /// Sets the derivative function to use during the solve
    fn with_derivative(self, derivative: Self::Derivative) -> Self;

    /// Turns the solver into an iterator over the solution, using IVPStep::step
    fn solve(self, data: Self::UserData) -> Result<IVPIterator<D, Self::Solver>, Self::Error>;
}

pub struct IVPIterator<D: Dimension, T: IVPStepper<D>>
where
    DefaultAllocator: Allocator<T::Field, D>,
{
    solver: T,
    finished: bool,
    _dim: PhantomData<D>,
}

/// A type alias for collecting all Steps into a Result
/// of a Vec of the solution ((time, system state))
pub type Path<R, C, D, E> = Result<Vec<(R, BVector<C, D>)>, E>;

impl<D: Dimension, T: IVPStepper<D>> IVPIterator<D, T>
where
    DefaultAllocator: Allocator<T::Field, D>,
{
    pub fn collect_vec(self) -> Path<T::RealField, T::Field, D, T::Error> {
        self.collect::<Result<Vec<_>, _>>()
    }
}

impl<D: Dimension, T: IVPStepper<D>> Iterator for IVPIterator<D, T>
where
    DefaultAllocator: Allocator<T::Field, D>,
{
    type Item = Result<(T::RealField, BVector<T::Field, D>), T::Error>;

    fn next(&mut self) -> Option<Self::Item> {
        use IVPStatus as IE;

        if self.finished {
            return None;
        }

        loop {
            match self.solver.step() {
                Ok(vec) => break Some(Ok(vec)),
                Err(IE::Done) => break None,
                Err(IE::Redo) => continue,
                Err(IE::Failure(e)) => {
                    self.finished = true;
                    break Some(Err(e));
                }
            }
        }
    }
}

/// Euler solver for an IVP.
///
/// Solves an initial value problem using Euler's method.
///
/// # Examples
/// ```
/// use std::error::Error;
/// use bacon_sci::{BSVector, ivp::{Euler, IVPSolver, IVPError}};
/// fn derivative(_t: f64, state: &[f64], _p: &mut ()) -> Result<BSVector<f64, 1>, Box<dyn Error>> {
///     Ok(BSVector::<f64, 1>::from_column_slice(state))
/// }
///
/// fn example() -> Result<(), IVPError> {
///     let solver = Euler::new()?
///         .with_maximum_dt(0.001)?
///         .with_initial_conditions_slice(&[1.0])?
///         .with_initial_time(0.0)?
///         .with_ending_time(1.0)?
///         .with_derivative(derivative)
///         .solve(())?;
///     let path = solver.collect_vec()?;
///
///     for (time, state) in &path {
///         assert!((time.exp() - state.column(0)[0]).abs() <= 0.001);
///     }
///     Ok(())
/// }
/// ```
pub struct Euler<'a, N, D, T, F>
where
    N: ComplexField + Copy,
    D: Dimension,
    T: Clone,
    F: Derivative<N, D, T> + 'a,
    DefaultAllocator: Allocator<N, D>,
{
    init_dt: Option<N::RealField>,
    init_time: Option<N::RealField>,
    init_end: Option<N::RealField>,
    init_state: Option<BVector<N, D>>,
    init_derivative: Option<F>,
    dim: D,
    _data: PhantomData<&'a T>,
}

/// The struct that actually solves an IVP with Euler's method
/// Is the associated [`IVPStepper`] for Euler (the IVPSolver)
/// You should use Euler and not this type directly
pub struct EulerSolver<'a, N, D, T, F>
where
    N: ComplexField + Copy,
    D: Dimension,
    T: Clone,
    F: Derivative<N, D, T> + 'a,
    DefaultAllocator: Allocator<N, D>,
{
    dt: N,
    time: N,
    end: N,
    state: BVector<N, D>,
    derivative: F,
    data: T,
    _lifetime: PhantomData<&'a ()>,
}

impl<'a, N, D, T, F> IVPStepper<D> for EulerSolver<'a, N, D, T, F>
where
    N: ComplexField + Copy,
    D: Dimension,
    T: Clone,
    F: Derivative<N, D, T> + 'a,
    DefaultAllocator: Allocator<N, D>,
{
    type Error = IVPError;
    type Field = N;
    type RealField = N::RealField;
    type UserData = T;

    fn step(
        &mut self,
    ) -> Result<(Self::RealField, BVector<Self::Field, D>), IVPStatus<Self::Error>> {
        if self.time.real() >= self.end.real() {
            return Err(IVPStatus::Done);
        }
        if (self.time + self.dt).real() >= self.end.real() {
            self.dt = self.end - self.time;
        }

        let derivative = (self.derivative)(self.time.real(), self.state.as_slice(), &mut self.data)
            .map_err(IVPError::UserError)?;

        let old_time = self.time.real();
        let old_state = self.state.clone();

        self.state += derivative * self.dt;
        self.time += self.dt;

        Ok((old_time, old_state))
    }

    fn time(&self) -> Self::RealField {
        self.time.real()
    }
}

impl<'a, N, D, T, F> IVPSolver<'a, D> for Euler<'a, N, D, T, F>
where
    N: ComplexField + Copy,
    D: Dimension,
    T: Clone,
    F: Derivative<N, D, T> + 'a,
    DefaultAllocator: Allocator<N, D>,
{
    type Error = IVPError;
    type Field = N;
    type RealField = N::RealField;
    type Derivative = F;
    type UserData = T;
    type Solver = EulerSolver<'a, N, D, T, F>;

    fn new() -> Result<Self, Self::Error> {
        Ok(Self {
            init_dt: None,
            init_time: None,
            init_end: None,
            init_state: None,
            init_derivative: None,
            dim: D::dim()?,
            _data: PhantomData,
        })
    }

    fn new_dyn(size: usize) -> Result<Self, Self::Error> {
        Ok(Self {
            init_dt: None,
            init_time: None,
            init_end: None,
            init_state: None,
            init_derivative: None,
            dim: D::dim_dyn(size)?,
            _data: PhantomData,
        })
    }

    fn dim(&self) -> D {
        self.dim
    }

    /// Unused for Euler, call is a no-op
    fn with_tolerance(self, _tol: Self::RealField) -> Result<Self, Self::Error> {
        Ok(self)
    }

    /// If there is not time step already, set, then set the time step.
    /// If there is, set the time step to the average of that and the max passed in.
    fn with_maximum_dt(mut self, max: Self::RealField) -> Result<Self, Self::Error> {
        if max <= <Self::RealField as Zero>::zero() {
            return Err(IVPError::TimeDeltaOOB);
        }

        self.init_dt = if let Some(dt) = self.init_dt {
            Some((dt + max) / Self::RealField::from_u8(2).ok_or(IVPError::FromPrimitiveFailure)?)
        } else {
            Some(max)
        };
        Ok(self)
    }

    /// If there is not time step already, set, then set the time step.
    /// If there is, set the time step to the average of that and the max passed in.
    fn with_minimum_dt(mut self, min: Self::RealField) -> Result<Self, Self::Error> {
        if min <= <Self::RealField as Zero>::zero() {
            return Err(IVPError::TimeDeltaOOB);
        }

        self.init_dt = if let Some(dt) = self.init_dt {
            Some((dt + min) / Self::RealField::from_u8(2).ok_or(IVPError::FromPrimitiveFailure)?)
        } else {
            Some(min)
        };
        Ok(self)
    }

    fn with_initial_time(mut self, initial: Self::RealField) -> Result<Self, Self::Error> {
        self.init_time = Some(initial.clone());

        if let Some(end) = self.init_end.as_ref() {
            if *end <= initial {
                return Err(IVPError::TimeStartOOB);
            }
        }

        Ok(self)
    }

    fn with_ending_time(mut self, ending: Self::RealField) -> Result<Self, Self::Error> {
        self.init_end = Some(ending.clone());

        if let Some(initial) = self.init_time.as_ref() {
            if *initial >= ending {
                return Err(IVPError::TimeEndOOB);
            }
        }

        Ok(self)
    }

    fn with_initial_conditions(
        mut self,
        start: BVector<Self::Field, D>,
    ) -> Result<Self, Self::Error> {
        self.init_state = Some(start);
        Ok(self)
    }

    fn with_derivative(mut self, derivative: Self::Derivative) -> Self {
        self.init_derivative = Some(derivative);
        self
    }

    fn solve(mut self, data: Self::UserData) -> Result<IVPIterator<D, Self::Solver>, Self::Error> {
        let dt = self.init_dt.ok_or(IVPError::MissingParameters)?;
        let time = self.init_time.ok_or(IVPError::MissingParameters)?;
        let end = self.init_end.ok_or(IVPError::MissingParameters)?;
        let state = self.init_state.take().ok_or(IVPError::MissingParameters)?;
        let derivative = self
            .init_derivative
            .take()
            .ok_or(IVPError::MissingParameters)?;

        Ok(IVPIterator {
            solver: EulerSolver {
                dt: N::from_real(dt),
                time: N::from_real(time),
                end: N::from_real(end),
                state,
                derivative,
                data,
                _lifetime: PhantomData,
            },
            finished: false,
            _dim: PhantomData,
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::BSVector;
    use nalgebra::{DimName, Dyn};

    type Path<D> = Vec<(f64, BVector<f64, D>)>;

    fn solve_ivp<'a, D, F>(
        (initial, end): (f64, f64),
        dt: f64,
        initial_conds: &[f64],
        derivative: F,
    ) -> Result<Path<D>, IVPError>
    where
        D: Dimension,
        F: Derivative<f64, D, ()> + 'a,
        DefaultAllocator: Allocator<f64, D>,
    {
        let ivp = Euler::new()?
            .with_initial_time(initial)?
            .with_ending_time(end)?
            .with_maximum_dt(dt)?
            .with_initial_conditions_slice(initial_conds)?
            .with_derivative(derivative);
        ivp.solve(())?.collect()
    }

    fn exp_deriv(_: f64, y: &[f64], _: &mut ()) -> Result<BSVector<f64, 1>, UserError> {
        Ok(BSVector::from_column_slice(y))
    }

    fn quadratic_deriv(t: f64, _y: &[f64], _: &mut ()) -> Result<BSVector<f64, 1>, UserError> {
        Ok(BSVector::from_column_slice(&[-2.0 * t]))
    }

    fn sine_deriv(t: f64, y: &[f64], _: &mut ()) -> Result<BSVector<f64, 1>, UserError> {
        Ok(BSVector::from_iterator(y.iter().map(|_| t.cos())))
    }

    fn cos_deriv(_t: f64, y: &[f64], _: &mut ()) -> Result<BSVector<f64, 2>, UserError> {
        Ok(BSVector::from_column_slice(&[y[1], -y[0]]))
    }

    fn dynamic_cos_deriv(_t: f64, y: &[f64], _: &mut ()) -> Result<BVector<f64, Dyn>, UserError> {
        Ok(BVector::from_column_slice_generic(
            Dyn::from_usize(y.len()),
            U1::name(),
            &[y[1], -y[0]],
        ))
    }

    #[test]
    #[should_panic]
    fn euler_dynamic_cos_panics() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let path = solve_ivp((t_initial, t_final), 0.01, &[1.0, 0.0], dynamic_cos_deriv).unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                step.0.cos(),
                epsilon = 0.01
            ));
        }
    }

    #[test]
    fn euler_dynamic_cos() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let ivp = Euler::new_dyn(2)
            .unwrap()
            .with_initial_time(t_initial)
            .unwrap()
            .with_ending_time(t_final)
            .unwrap()
            .with_maximum_dt(0.01)
            .unwrap()
            .with_initial_conditions_slice(&[1.0, 0.0])
            .unwrap()
            .with_derivative(dynamic_cos_deriv)
            .solve(())
            .unwrap();
        let path = ivp.collect_vec().unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                step.0.cos(),
                epsilon = 0.01
            ));
        }
    }

    #[test]
    fn euler_cos() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let path = solve_ivp((t_initial, t_final), 0.01, &[1.0, 0.0], cos_deriv).unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                step.0.cos(),
                epsilon = 0.01
            ));
        }
    }

    #[test]
    fn euler_exp() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let path = solve_ivp((t_initial, t_final), 0.005, &[1.0], exp_deriv).unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                step.0.exp(),
                epsilon = 0.01
            ));
        }
    }

    #[test]
    fn euler_quadratic() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let path = solve_ivp((t_initial, t_final), 0.01, &[1.0], quadratic_deriv).unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                1.0 - step.0.powi(2),
                epsilon = 0.01
            ));
        }
    }

    #[test]
    fn euler_sin() {
        let t_initial = 0.0;
        let t_final = 1.0;

        let path = solve_ivp((t_initial, t_final), 0.01, &[0.0], sine_deriv).unwrap();

        for step in path {
            assert!(approx_eq!(
                f64,
                step.1.column(0)[0],
                step.0.sin(),
                epsilon = 0.01
            ));
        }
    }
}