1#[cfg(test)]
4pub mod test;
5
6use crate::{
7    defeat_message,
8    math::{
9        Rank2, Tensor, TensorRank0List, TensorRank1, TensorRank1List, TensorRank1List2D,
10        TensorRank2, TensorRank2List, TensorRank2List2D, TensorRank2Vec, TensorRank4,
11        TensorRank4List,
12    },
13};
14use std::fmt::{self, Debug, Display, Formatter};
15
16pub use crate::math::Scalar;
17
18pub enum DeformationError {
20    InvalidJacobian(Scalar),
21}
22
23impl Debug for DeformationError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        let error = match self {
26            Self::InvalidJacobian(jacobian) => {
27                format!("\x1b[1;91mInvalid Jacobian: {jacobian:.6e}fdsafdsa.\x1b[0;91m")
28            }
29        };
30        write!(f, "\n{error}\n\x1b[0;2;31m{}\x1b[0m\n", defeat_message())
31    }
32}
33
34impl Display for DeformationError {
35    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
36        let error = match self {
37            Self::InvalidJacobian(jacobian) => {
38                format!("\x1b[1;91mInvalid Jacobian: {jacobian:.6e}asdfasdf.\x1b[0;91m")
39            }
40        };
41        write!(f, "{error}\x1b[0m")
42    }
43}
44
45pub trait Deformation {
47    fn jacobian(&self) -> Result<Scalar, DeformationError>;
53    fn left_cauchy_green(&self) -> LeftCauchyGreenDeformation;
59    fn right_cauchy_green(&self) -> RightCauchyGreenDeformation;
65}
66
67impl Deformation for DeformationGradient {
68    fn jacobian(&self) -> Result<Scalar, DeformationError> {
69        let jacobian = self.determinant();
70        if jacobian > 0.0 {
71            Ok(jacobian)
72        } else {
73            Err(DeformationError::InvalidJacobian(jacobian))
74        }
75    }
76    fn left_cauchy_green(&self) -> LeftCauchyGreenDeformation {
77        self.iter()
78            .map(|deformation_gradient_i| {
79                self.iter()
80                    .map(|deformation_gradient_j| deformation_gradient_i * deformation_gradient_j)
81                    .collect()
82            })
83            .collect()
84    }
85    fn right_cauchy_green(&self) -> RightCauchyGreenDeformation {
86        let deformation_gradient_transpose = self.transpose();
87        deformation_gradient_transpose
88            .iter()
89            .map(|deformation_gradient_transpose_i| {
90                deformation_gradient_transpose
91                    .iter()
92                    .map(|deformation_gradient_transpose_j| {
93                        deformation_gradient_transpose_i * deformation_gradient_transpose_j
94                    })
95                    .collect()
96            })
97            .collect()
98    }
99}
100
101pub type CauchyStress = TensorRank2<3, 1, 1>;
103
104pub type CauchyStresses<const W: usize> = TensorRank2List<3, 1, 1, W>;
106
107pub type CauchyTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
109
110pub type CauchyRateTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
112
113pub type Coordinates<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
115
116pub type CurrentCoordinate = TensorRank1<3, 1>;
118
119pub type CurrentCoordinates<const W: usize> = TensorRank1List<3, 1, W>;
121
122pub type CurrentVelocity = TensorRank1<3, 1>;
124
125pub type DeformationGradient = TensorRank2<3, 1, 0>;
127
128pub type DeformationGradientElastic = TensorRank2<3, 1, 2>;
130
131pub type DeformationGradientGeneral<const I: usize, const J: usize> = TensorRank2<3, I, J>;
133
134pub type DeformationGradientPlastic = TensorRank2<3, 2, 0>;
136
137pub type DeformationGradientRate = TensorRank2<3, 1, 0>;
139
140pub type DeformationGradientRatePlastic = TensorRank2<3, 2, 0>;
142
143pub type DeformationGradientList<const W: usize> = TensorRank2List<3, 1, 0, W>;
145
146pub type DeformationGradientRateList<const W: usize> = TensorRank2List<3, 1, 0, W>;
148
149pub type DeformationGradients = TensorRank2Vec<3, 1, 0>;
151
152pub type DeformationGradientRates = TensorRank2Vec<3, 1, 0>;
154
155pub type Displacement = TensorRank1<3, 1>;
157
158pub type FirstPiolaKirchhoffStress = TensorRank2<3, 1, 0>;
160
161pub type FirstPiolaKirchhoffStresses<const W: usize> = TensorRank2List<3, 1, 0, W>;
163
164pub type FirstPiolaKirchhoffTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
166
167pub type FirstPiolaKirchhoffTangentStiffnesses<const W: usize> = TensorRank4List<3, 1, 0, 1, 0, W>;
169
170pub type FirstPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
172
173pub type FirstPiolaKirchhoffRateTangentStiffnesses<const W: usize> =
175    TensorRank4List<3, 1, 0, 1, 0, W>;
176
177pub type Force = TensorRank1<3, 1>;
179
180pub type Forces<const W: usize> = TensorRank1List<3, 1, W>;
182
183pub type FrameSpin = TensorRank2<3, 1, 1>;
185
186pub type HeatFlux = TensorRank1<3, 1>;
188
189pub type LeftCauchyGreenDeformation = TensorRank2<3, 1, 1>;
191
192pub type MandelStress = TensorRank2<3, 2, 2>;
194
195pub type Normal = TensorRank1<3, 1>;
197
198pub type ReferenceCoordinate = TensorRank1<3, 0>;
200
201pub type ReferenceCoordinates<const W: usize> = TensorRank1List<3, 0, W>;
203
204pub type RightCauchyGreenDeformation = TensorRank2<3, 0, 0>;
206
207pub type RotationCurrentConfiguration = TensorRank2<3, 1, 1>;
209
210pub type RotationRateCurrentConfiguration = TensorRank2<3, 1, 1>;
212
213pub type RotationReferenceConfiguration = TensorRank2<3, 0, 0>;
215
216pub type Scalars<const W: usize> = TensorRank0List<W>;
218
219pub type SecondPiolaKirchhoffStress = TensorRank2<3, 0, 0>;
221
222pub type SecondPiolaKirchhoffTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
224
225pub type SecondPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
227
228pub type Stiffness = TensorRank2<3, 1, 1>;
230
231pub type Stiffnesses<const W: usize> = TensorRank2List2D<3, 1, 1, W, W>;
233
234pub type StretchingRate = TensorRank2<3, 1, 1>;
236
237pub type StretchingRatePlastic = TensorRank2<3, 2, 2>;
239
240pub type TemperatureGradient = TensorRank1<3, 1>;
242
243pub type Times = crate::math::Vector;
245
246pub type Traction = TensorRank1<3, 1>;
248
249pub type Vector<const I: usize> = TensorRank1<3, I>;
251
252pub type Vectors<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
254
255pub type Vectors2D<const I: usize, const W: usize, const X: usize> = TensorRank1List2D<3, I, W, X>;