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
use crate::math::{Point, Real, Vector, DIM};
use na::vector;
#[cfg(not(feature = "std"))]
use na::ComplexField;

pub struct QuadraticKernel;

impl QuadraticKernel {
    #[inline(always)]
    pub fn inv_d(cell_width: Real) -> Real {
        4.0 / (cell_width * cell_width)
    }

    #[inline(always)]
    pub fn eval_all(x: Real) -> [Real; 3] {
        [
            0.5 * (1.5 - x).powi(2),
            0.75 - (x - 1.0).powi(2),
            0.5 * (x - 0.5).powi(2),
        ]
    }

    // #[inline(always)]
    // pub fn eval_all_simd(x: SimdReal) -> [SimdReal; 3] {
    //     let a = SimdReal::splat(1.5) - x;
    //     let b = x - SimdReal::splat(1.0);
    //     let c = x - SimdReal::splat(0.5);
    //
    //     [
    //         SimdReal::splat(0.5) * a * a,
    //         SimdReal::splat(0.75) * b * b,
    //         SimdReal::splat(0.5) * c * c,
    //     ]
    // }

    #[inline(always)]
    pub fn eval(x: Real) -> Real {
        let x_abs = x.abs();

        if x_abs < 0.5 {
            3.0 / 4.0 - x_abs.powi(2)
        } else if x_abs < 3.0 / 2.0 {
            0.5 * (3.0 / 2.0 - x_abs).powi(2)
        } else {
            0.0
        }
    }

    #[inline(always)]
    pub fn eval_derivative(x: Real) -> Real {
        let x_abs = x.abs();

        if x_abs < 0.5 {
            -2.0 * x.signum() * x_abs
        } else if x_abs < 3.0 / 2.0 {
            -x.signum() * (3.0 / 2.0 - x_abs)
        } else {
            0.0
        }
    }

    #[inline(always)]
    pub fn precompute_weights(
        ref_elt_pos_minus_particle_pos: Vector<Real>,
        h: Real,
    ) -> [[Real; 3]; DIM] {
        [
            Self::eval_all(-ref_elt_pos_minus_particle_pos.x / h),
            Self::eval_all(-ref_elt_pos_minus_particle_pos.y / h),
            #[cfg(feature = "dim3")]
            Self::eval_all(-ref_elt_pos_minus_particle_pos.z / h),
        ]
    }

    // #[inline(always)]
    // pub fn precompute_weights_simd(
    //     ref_elt_pos_minus_particle_pos: Vector<SimdReal>,
    //     h: SimdReal,
    // ) -> [[SimdReal; 3]; DIM] {
    //     [
    //         Self::eval_all_simd(-ref_elt_pos_minus_particle_pos.x / h),
    //         Self::eval_all_simd(-ref_elt_pos_minus_particle_pos.y / h),
    //         #[cfg(feature = "dim3")]
    //         Self::eval_all_simd(-ref_elt_pos_minus_particle_pos.z / h),
    //     ]
    // }

    #[inline(always)]
    pub fn stencil_with_dir(elt_pos_minus_particle_pos: Vector<Real>, h: Real) -> Real {
        let dpt = -elt_pos_minus_particle_pos / h;
        #[cfg(feature = "dim2")]
        return Self::eval(dpt.x) * Self::eval(dpt.y);
        #[cfg(feature = "dim3")]
        return Self::eval(dpt.x) * Self::eval(dpt.y) * Self::eval(dpt.z);
    }

    #[inline(always)]
    pub fn stencil(elt_pos: Point<Real>, particle_pos: Point<Real>, h: Real) -> Real {
        Self::stencil_with_dir(elt_pos - particle_pos, h)
    }

    #[inline(always)]
    pub fn stencil_gradient_with_dir(
        elt_pos_minus_particle_pos: Vector<Real>,
        h: Real,
    ) -> Vector<Real> {
        let inv_h = 1.0 / h;
        let dpt = -elt_pos_minus_particle_pos * inv_h;
        let val_x = Self::eval(dpt.x);
        let val_y = Self::eval(dpt.y);

        #[cfg(feature = "dim3")]
        let val_z = Self::eval(dpt.z);

        #[cfg(feature = "dim2")]
        return vector![
            inv_h * Self::eval_derivative(dpt.x) * val_y,
            inv_h * val_x * Self::eval_derivative(dpt.y)
        ];
        #[cfg(feature = "dim3")]
        return vector![
            inv_h * Self::eval_derivative(dpt.x) * val_y * val_z,
            inv_h * val_x * Self::eval_derivative(dpt.y) * val_z,
            inv_h * val_x * val_y * Self::eval_derivative(dpt.z)
        ];
    }

    #[inline(always)]
    pub fn stencil_gradient(
        elt_pos: Point<Real>,
        particle_pos: Point<Real>,
        h: Real,
    ) -> Vector<Real> {
        Self::stencil_gradient_with_dir(elt_pos - particle_pos, h)
    }
}