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
use std::fmt::Debug;

// Internal dependencies
use crate::solvers::fem::basis::single_variable::{
    linear_basis::LinearBasis, polynomials_1d::FirstDegreePolynomial
};
use crate::solvers::basis::functions::{Differentiable1D,Function1D};
use crate::solvers::{quadrature::gauss_legendre, matrix_solver, solver_trait::DiffEquationSolver};
use crate::Error;

// External dependencies
use ndarray::{Array1, Array2};

/// # General Information
/// 
/// Parameters needed for solving Stokes equation in 1d.
/// If one of it's properties is not set, it will default to zero.
/// Boundary conditions accepted are only Dirichlet for now.
/// 
/// # Parameters
/// 
/// * `rho` - Constant density
/// * `pressure` - Pressure [0] at index [1]
/// * `boundary_condition_pressure` - Pressure boundary condition
/// 
pub struct StokesParams1D {
    pub rho: f64,
    pub hydrostatic_pressure: f64,
    pub force_function: Box<dyn Fn(f64) -> f64>,
}


impl Default for StokesParams1D {
    fn default() -> Self {
        Self {
            rho: 0_f64,
            hydrostatic_pressure: 0_f64,
            force_function: Box::new(|x| x)
        }
    }
}

impl Debug for StokesParams1D {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let ff = &self.force_function;
        let eval = ff(0_f64);
        let content = format!("{{ rho: {},\nhydrostatic_pressure: {},\n force_function: f(0) -> {} }}", self.rho, self.hydrostatic_pressure,eval);
        write!(f, "{}", content)
    }
}

#[derive(Debug)]
/// # General Information
///
/// A Stokes solver 1d abstracts the equation: "(1/ρ)p_x = f" with constant velocity and hydrostatic pressure "ρ"
///
/// # Fields
///
/// * `boundary_condition_pressure` - Original boundary conditions (Only Dirichlet is supported for now).
/// * `stiffness_matrix` - Left-side matrix of resulting discrete equation.
/// * `b_vector` - Right-side vector of the resulting discrete equation.
/// * `gauss_step` - Precision of quadrature.
/// * `speed` - Constant speed.
/// * `rho` - Constant density.
///
pub struct StokesSolver1D {
    pub(crate) stiffness_matrix: Array2<f64>,
    pub(crate) b_vector: Array1<f64>,
    pub gauss_step: usize,
    pub hydrostatic_pressure: f64,
    pub rho: f64,
}

impl StokesSolver1D {

    /// Creates a new instance of solver from params
    pub fn new(params: &StokesParams1D, mesh: Vec<f64>, gauss_step: usize) -> Result<Self,Error> {

        let (stiffness_matrix, b_vector) = Self::gauss_legendre_integration(
            params.rho,
            params.hydrostatic_pressure,
            &mesh,
            gauss_step,
            &params.force_function
        )?;
        Ok(Self {
            stiffness_matrix,
            gauss_step,
            b_vector,
            hydrostatic_pressure: params.hydrostatic_pressure,
            rho: params.rho
        })

    }

    /// # General Information
    ///
    /// First, it generates the basis for a solver from the linear basis constructor.
    /// Then the stiffnes matrix and vector b are generated based on linear basis integration via Gauss-Legendre and returned.
    /// Note that vector and matrix will have one on their diagonals' boundaries and zero on other boundary elements to make boundary conditions permanent. 
    ///
    /// # Parameters
    ///
    /// * `rho` - density
    /// * `hydrostatic_pressure`
    /// * `mesh` - Vector of f64 representing a line
    /// * `gauss_step` - How many nodes will be calculated for a given integration
    /// * `function` - Force acting on the fluid
    ///
    /// # Returns
    ///
    /// A tuple with both the stiffness matrix and the vector b.
    ///
    pub fn gauss_legendre_integration(rho: f64, hydrostatic_pressure: f64, mesh: &Vec<f64>, gauss_step: usize, function: &Box<dyn Fn(f64) -> f64>) -> Result<(Array2<f64>, Array1<f64>),Error> {

        let basis = LinearBasis::new(mesh)?;
        let basis_len = basis.basis.len();

        let mut stiffness_matrix =
            ndarray::Array::from_elem((basis_len, basis_len), 0_f64);

        let mut b_vector = Array1::from_elem(basis_len, 0_f64);


        for i in 1..(basis_len - 1) {

            let derivative_phi = basis.basis[i].differentiate()?;

            let transform_function_prev = FirstDegreePolynomial::transformation_from_m1_p1(
                mesh[i - 1],
                mesh[i],
            );
            let transform_function_next = FirstDegreePolynomial::transformation_from_m1_p1(
                mesh[i],
                mesh[i + 1],
            );
            let transform_function_square =
                FirstDegreePolynomial::transformation_from_m1_p1(
                    mesh[i - 1],
                    mesh[i + 1],
                );

            let derivative_t_prev = transform_function_prev.differentiate()?;
            let derivative_t_next = transform_function_next.differentiate()?;
            let derivative_t_square = transform_function_square.differentiate()?;

            let derivative_prev = basis.basis[i - 1].differentiate()?;
            let derivative_next = basis.basis[i + 1].differentiate()?;

            let mut integral_prev_approximation = 0_f64;
            let mut integral_next_approximation = 0_f64;
            let mut integral_square_approximation = 0_f64;
            let mut b_integral_approximation = 0_f64;

            // integrate
            for j in 1..gauss_step {
                // Obtaining arccos(node) and weight
                let (theta, w) = gauss_legendre::quad_pair(gauss_step, j)?;
                let x = theta.cos();

                // translated from -1,1
                let translated_point_prev = transform_function_prev.evaluate(x);
                let translated_point_next = transform_function_next.evaluate(x);
                let translated_point_square = transform_function_square.evaluate(x);

                integral_prev_approximation +=
                    basis.basis[i].evaluate(translated_point_prev)*
                    derivative_prev.evaluate(translated_point_prev)*
                    derivative_t_prev.evaluate(x)*
                    w;
                integral_next_approximation +=
                    basis.basis[i].evaluate(translated_point_next)*
                    derivative_next.evaluate(translated_point_next)*
                    derivative_t_next.evaluate(x)*
                    w;
                integral_square_approximation +=
                    basis.basis[i].evaluate(translated_point_square)*
                    derivative_phi.evaluate(translated_point_square)*
                    derivative_t_square.evaluate(x)*
                    w;
                b_integral_approximation += rho*
                    function(translated_point_square)*
                    basis.basis[i].evaluate(translated_point_square)*
                    derivative_t_square.evaluate(x)*
                    w;
            }

            stiffness_matrix[[i, i]] = integral_square_approximation;
            stiffness_matrix[[i, i - 1]] = integral_prev_approximation;
            stiffness_matrix[[i, i + 1]] = integral_next_approximation;
            b_vector[i] = b_integral_approximation;
        
        }
        
        let derivative_phi_0 = basis.basis[0].differentiate()?;
        let derivative_phi_1 = basis.basis[1].differentiate()?;

        let transform_function_square_0 =
            FirstDegreePolynomial::transformation_from_m1_p1(
                mesh[0],
                mesh[1],
            );
        let derivative_t_square_0 = transform_function_square_0.differentiate()?;

        let mut integral_0_approximation = 0_f64;
        let mut integral_0_next_approximation = 0_f64;
        let mut b_first_integral_approximation = 0_f64;


        for j in 1..gauss_step {

            // Obtaining arccos(node) and weight
            let (theta, w) = gauss_legendre::quad_pair(gauss_step, j)?;
            let x = theta.cos();

            let translated_0 = transform_function_square_0.evaluate(x);

            integral_0_approximation += basis.basis[0].evaluate(translated_0) * 
                derivative_phi_0.evaluate(translated_0) * 
                derivative_t_square_0.evaluate(x) * w;
            
            integral_0_next_approximation += basis.basis[0].evaluate(translated_0) * 
            derivative_phi_1.evaluate(translated_0) * 
            derivative_t_square_0.evaluate(x) * w;

            b_first_integral_approximation += rho * function(translated_0) *
            basis.basis[0].evaluate(translated_0) *
            derivative_t_square_0.evaluate(x) * w;

        }

        stiffness_matrix[[0, 0]] = integral_0_approximation;
        stiffness_matrix[[0, 1]] = integral_0_next_approximation;
        stiffness_matrix[[basis_len-1,basis_len-1]] = 1_f64;
        b_vector[0] = b_first_integral_approximation;
        b_vector[basis_len - 1] = hydrostatic_pressure;

        Ok((stiffness_matrix, b_vector))

    }
}

impl DiffEquationSolver for StokesSolver1D {
    /// # Specific implementation
    ///
    /// Solving starts by obtaining stiffness matrix and vector b (Ax=b).
    /// Then both are used inside function `solve_by_thomas` to obtain the result vector.
    ///
    fn solve(&mut self, _time_step: f64) -> Result<Vec<f64>, Error> {

        let res = matrix_solver::solve_by_thomas(&self.stiffness_matrix, &self.b_vector)?;

        Ok(res)
    }
}

#[cfg(test)]
mod test {
    use crate::StokesParams;

    use super::{StokesSolver1D,DiffEquationSolver};

    #[test]
    fn regular_mesh_matrix_4p_nav() {
        
        let params = StokesParams::normal_1d().force_function(Box::new(|_| 10_f64))
            .hydrostatic_pressure(1_f64).density(1_f64).build();

        let mut eq = StokesSolver1D::new(&params, vec![0_f64,0.333,0.666,1_f64], 150).unwrap();

        assert!(eq.stiffness_matrix[[0, 0]] <= -0.4 && eq.stiffness_matrix[[0, 0]] >= -0.6);
        assert!(eq.stiffness_matrix[[0, 1]] <= 0.6 && eq.stiffness_matrix[[0, 1]] >= 0.4);
        assert!(eq.stiffness_matrix[[1, 0]] <= -0.4 && eq.stiffness_matrix[[1, 0]] >= -0.6);
        assert!(eq.stiffness_matrix[[1, 1]] <= 0.1 && eq.stiffness_matrix[[1, 1]] >= -0.1);
        assert!(eq.stiffness_matrix[[1, 2]] <= 0.6 && eq.stiffness_matrix[[1, 2]] >= 0.4);
        assert!(eq.stiffness_matrix[[2, 1]] <= -0.4 && eq.stiffness_matrix[[2, 1]] >= -0.6);
        assert!(eq.stiffness_matrix[[2, 2]] <= 0.1 && eq.stiffness_matrix[[2, 2]] >= -0.1);
        assert!(eq.stiffness_matrix[[2, 3]] <= 0.6 && eq.stiffness_matrix[[2, 3]] >= 0.4);
        assert!(eq.stiffness_matrix[[3, 2]] == 0_f64);
        assert!(eq.stiffness_matrix[[3, 3]] == 1_f64);
    
        println!("{:?}",eq.b_vector);
        assert!(eq.b_vector[[0]] <= 1.75 && eq.b_vector[[0]] >= 1.55);       
        assert!(eq.b_vector[[1]] <= 3.45 && eq.b_vector[[1]] >= 3.25);       
        assert!(eq.b_vector[[2]] <= 3.45 && eq.b_vector[[2]] >= 3.25);       
        assert!(eq.b_vector[[3]] == 1_f64);
        
        let solution = eq.solve(0_f64).unwrap();

        assert!(solution[0] <= -8.9 && solution[0] >= -9.1);
        assert!(solution[1] <= -5.5 && solution[1] >= -5.7);
        assert!(solution[2] <= -2.2 && solution[2] >= -2.4);

    }
}