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
#[cfg(all(feature = "dim2", feature = "f32"))]
pub extern crate parry2d as parry;
#[cfg(all(feature = "dim2", feature = "f64"))]
pub extern crate parry2d_f64 as parry;
#[cfg(all(feature = "dim3", feature = "f32"))]
pub extern crate parry3d as parry;
#[cfg(all(feature = "dim3", feature = "f64"))]
pub extern crate parry3d_f64 as parry;

#[cfg(all(feature = "dim2", feature = "f32"))]
pub extern crate rapier2d as rapier;
#[cfg(all(feature = "dim2", feature = "f64"))]
pub extern crate rapier2d_f64 as rapier;
#[cfg(all(feature = "dim3", feature = "f32"))]
pub extern crate rapier3d as rapier;
#[cfg(all(feature = "dim3", feature = "f64"))]
pub extern crate rapier3d_f64 as rapier;

#[cfg(all(feature = "dim2", feature = "rapier-testbed"))]
extern crate rapier_testbed2d as rapier_testbed;
#[cfg(all(feature = "dim3", feature = "rapier-testbed"))]
extern crate rapier_testbed3d as rapier_testbed;

#[cfg(feature = "dim2")]
pub extern crate sparkl2d_core;
#[cfg(all(feature = "dim2", feature = "cuda"))]
pub extern crate sparkl2d_kernels as kernels;

#[cfg(feature = "cuda")]
pub extern crate cust;
#[cfg(feature = "dim3")]
pub extern crate sparkl3d_core;
#[cfg(all(feature = "dim3", feature = "cuda"))]
pub extern crate sparkl3d_kernels as kernels;

pub extern crate nalgebra as na;

#[macro_use]
extern crate log;

#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;

#[cfg(feature = "dim2")]
pub use sparkl2d_core as core;
#[cfg(feature = "dim3")]
pub use sparkl3d_core as core;

pub mod prelude {
    pub use crate::dynamics::models::*;
    pub use crate::dynamics::solver::*;
    pub use crate::dynamics::*;
    pub use crate::geometry::*;
    pub use crate::math::*;
}

pub mod math {
    pub use super::parry::math::*;
    pub type Kernel = crate::core::dynamics::solver::QuadraticKernel;

    #[derive(Copy, Clone, Debug, PartialEq)]
    pub struct DecomposedTensor {
        pub deviatoric_part: Matrix<Real>,
        pub spherical_part: Real,
    }

    impl DecomposedTensor {
        pub fn decompose(tensor: &Matrix<Real>) -> Self {
            let spherical_part = tensor.trace() / (DIM as Real);
            let mut deviatoric_part = *tensor;

            for i in 0..DIM {
                deviatoric_part[(i, i)] -= spherical_part;
            }

            Self {
                deviatoric_part,
                spherical_part,
            }
        }

        pub fn zero() -> Self {
            Self {
                deviatoric_part: Matrix::zeros(),
                spherical_part: 0.0,
            }
        }

        pub fn recompose(&self) -> Matrix<Real> {
            self.deviatoric_part + Matrix::identity() * self.spherical_part
        }
    }

    impl std::ops::Add<DecomposedTensor> for DecomposedTensor {
        type Output = DecomposedTensor;

        #[inline]
        fn add(self, rhs: DecomposedTensor) -> DecomposedTensor {
            DecomposedTensor {
                deviatoric_part: self.deviatoric_part + rhs.deviatoric_part,
                spherical_part: self.spherical_part + rhs.spherical_part,
            }
        }
    }

    impl std::ops::AddAssign<DecomposedTensor> for DecomposedTensor {
        #[inline]
        fn add_assign(&mut self, rhs: DecomposedTensor) {
            self.deviatoric_part += rhs.deviatoric_part;
            self.spherical_part += rhs.spherical_part;
        }
    }

    impl std::ops::Mul<Vector<Real>> for DecomposedTensor {
        type Output = Vector<Real>;

        #[inline]
        fn mul(self, rhs: Vector<Real>) -> Self::Output {
            self.deviatoric_part * rhs + self.spherical_part * rhs
        }
    }
}

#[cfg(feature = "cuda")]
pub mod cuda;
pub mod dynamics;
pub mod geometry;
pub mod pipelines;
pub mod third_party;
pub mod utils;