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
use crate::algebra::abstr::Real;
use crate::algebra::linear::Vector;
use crate::analysis::differential_equation::ordinary::ImplicitODE;

#[derive(Clone)]
pub struct ImplicitInitialValueProblem<'a, T, O>
where
    T: Real,
    O: ImplicitODE<T>,
{
    ode: &'a O,
    t_start: T,
    init_cond: Vector<T>,
    t_end: Option<T>,
    callback: Option<&'a (dyn Fn(&T, &Vector<T>) -> bool + 'a)>,
    //dense_output: Option<Vec<T>>
}

impl<'a, T, O> ImplicitInitialValueProblem<'a, T, O>
where
    T: Real,
    O: ImplicitODE<T>,
{
    pub fn ode(&self) -> &'a O {
        self.ode
    }

    pub fn t_start(&self) -> T {
        self.t_start
    }

    pub fn t_end(&self) -> Option<T> {
        self.t_end
    }

    pub fn callback(&self) -> Option<&'a dyn Fn(&T, &Vector<T>) -> bool> {
        self.callback
    }

    pub fn init_cond(&self) -> Vector<T> {
        self.init_cond.clone()
    }
}

#[derive(Clone)]
pub struct ImplicitInitialValueProblemBuilder<'a, T, O>
where
    T: Real,
    O: ImplicitODE<T>,
{
    ode: &'a O,
    t_start: T,
    init_cond: Vector<T>,
    t_end: Option<T>,
    callback: Option<&'a (dyn Fn(&T, &Vector<T>) -> bool + 'a)>,
    //dense_output: Option<Vec<T>>
}

impl<'a, T, O> ImplicitInitialValueProblemBuilder<'a, T, O>
where
    T: Real,
    O: ImplicitODE<T>,
{
    ///
    pub fn new(
        ode: &'a O,
        t_start: T,
        init_cond: Vector<T>,
    ) -> ImplicitInitialValueProblemBuilder<'a, T, O> {
        ImplicitInitialValueProblemBuilder {
            ode,
            t_start,
            init_cond,
            callback: None,
            t_end: None,
            //dense_output: None,
        }
    }

    pub fn t_end(&mut self, t: T) -> &mut Self {
        self.t_end = Some(t);
        self
    }

    pub fn callback(&mut self, callback: &'a (dyn Fn(&T, &Vector<T>) -> bool + 'a)) -> &mut Self {
        self.callback = Some(callback);
        self
    }

    ///
    /// # Panics
    ///
    pub fn build(&self) -> ImplicitInitialValueProblem<'a, T, O> {
        if self.callback.is_none() && self.t_end.is_none() {
            panic!("Either callback or t_end has to be set")
        }

        ImplicitInitialValueProblem {
            ode: self.ode,
            t_start: self.t_start,
            init_cond: self.init_cond.clone(),
            t_end: self.t_end,
            callback: self.callback,
            //dense_output: self.dense_output.clone()
        }
    }
}