conspire/mechanics/
mod.rs

1//! Mechanics library.
2
3#[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
18/// Possible errors for deformation gradients.
19pub 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
45/// Methods for deformation gradients.
46pub trait Deformation {
47    /// Calculates and returns the Jacobian.
48    ///
49    /// ```math
50    /// J = \mathrm{det}(\mathbf{F})
51    /// ```
52    fn jacobian(&self) -> Result<Scalar, DeformationError>;
53    /// Calculates and returns the left Cauchy-Green deformation.
54    ///
55    /// ```math
56    /// \mathbf{B} = \mathbf{F}\cdot\mathbf{F}^T
57    /// ```
58    fn left_cauchy_green(&self) -> LeftCauchyGreenDeformation;
59    /// Calculates and returns the right Cauchy-Green deformation.
60    ///
61    /// ```math
62    /// \mathbf{C} = \mathbf{F}^T\cdot\mathbf{F}
63    /// ```
64    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
101/// The Cauchy stress $`\boldsymbol{\sigma}`$.
102pub type CauchyStress = TensorRank2<3, 1, 1>;
103
104/// A list of Cauchy stresses.
105pub type CauchyStresses<const W: usize> = TensorRank2List<3, 1, 1, W>;
106
107/// The tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{T}}`$.
108pub type CauchyTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
109
110/// The rate tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{V}}`$.
111pub type CauchyRateTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
112
113/// A list of coordinates.
114pub type Coordinates<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
115
116/// A coordinate in the current configuration.
117pub type CurrentCoordinate = TensorRank1<3, 1>;
118
119/// A list of coordinates in the current configuration.
120pub type CurrentCoordinates<const W: usize> = TensorRank1List<3, 1, W>;
121
122/// A velocity in the current configuration.
123pub type CurrentVelocity = TensorRank1<3, 1>;
124
125/// The deformation gradient $`\mathbf{F}`$.
126pub type DeformationGradient = TensorRank2<3, 1, 0>;
127
128/// The elastic deformation gradient $`\mathbf{F}_\mathrm{e}`$.
129pub type DeformationGradientElastic = TensorRank2<3, 1, 2>;
130
131/// A general deformation gradient.
132pub type DeformationGradientGeneral<const I: usize, const J: usize> = TensorRank2<3, I, J>;
133
134/// The plastic deformation gradient $`\mathbf{F}_\mathrm{p}`$.
135pub type DeformationGradientPlastic = TensorRank2<3, 2, 0>;
136
137/// The deformation gradient rate $`\dot{\mathbf{F}}`$.
138pub type DeformationGradientRate = TensorRank2<3, 1, 0>;
139
140/// The plastic deformation gradient rate $`\dot{\mathbf{F}}_\mathrm{p}`$.
141pub type DeformationGradientRatePlastic = TensorRank2<3, 2, 0>;
142
143/// A list of deformation gradients.
144pub type DeformationGradientList<const W: usize> = TensorRank2List<3, 1, 0, W>;
145
146/// A list of deformation gradient rates.
147pub type DeformationGradientRateList<const W: usize> = TensorRank2List<3, 1, 0, W>;
148
149/// A vector of deformation gradients.
150pub type DeformationGradients = TensorRank2Vec<3, 1, 0>;
151
152/// A vector of deformation gradient rates.
153pub type DeformationGradientRates = TensorRank2Vec<3, 1, 0>;
154
155/// A displacement.
156pub type Displacement = TensorRank1<3, 1>;
157
158/// The first Piola-Kirchhoff stress $`\mathbf{P}`$.
159pub type FirstPiolaKirchhoffStress = TensorRank2<3, 1, 0>;
160
161/// A list of first Piola-Kirchhoff stresses.
162pub type FirstPiolaKirchhoffStresses<const W: usize> = TensorRank2List<3, 1, 0, W>;
163
164/// The tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}`$.
165pub type FirstPiolaKirchhoffTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
166
167/// A list of first Piola-Kirchhoff tangent stiffnesses.
168pub type FirstPiolaKirchhoffTangentStiffnesses<const W: usize> = TensorRank4List<3, 1, 0, 1, 0, W>;
169
170/// The rate tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{U}}`$.
171pub type FirstPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
172
173/// A list of first Piola-Kirchhoff rate tangent stiffnesses.
174pub type FirstPiolaKirchhoffRateTangentStiffnesses<const W: usize> =
175    TensorRank4List<3, 1, 0, 1, 0, W>;
176
177/// A force.
178pub type Force = TensorRank1<3, 1>;
179
180/// A list of forces.
181pub type Forces<const W: usize> = TensorRank1List<3, 1, W>;
182
183/// The frame spin $`\mathbf{\Omega}=\dot{\mathbf{Q}}\cdot\mathbf{Q}^T`$.
184pub type FrameSpin = TensorRank2<3, 1, 1>;
185
186/// The heat flux.
187pub type HeatFlux = TensorRank1<3, 1>;
188
189/// The left Cauchy-Green deformation $`\mathbf{B}`$.
190pub type LeftCauchyGreenDeformation = TensorRank2<3, 1, 1>;
191
192/// The Mandel stress $`\mathbf{M}`$.
193pub type MandelStress = TensorRank2<3, 2, 2>;
194
195/// A normal.
196pub type Normal = TensorRank1<3, 1>;
197
198/// A coordinate in the reference configuration.
199pub type ReferenceCoordinate = TensorRank1<3, 0>;
200
201/// A list of coordinates in the reference configuration.
202pub type ReferenceCoordinates<const W: usize> = TensorRank1List<3, 0, W>;
203
204/// The right Cauchy-Green deformation $`\mathbf{C}`$.
205pub type RightCauchyGreenDeformation = TensorRank2<3, 0, 0>;
206
207/// The rotation of the current configuration $`\mathbf{Q}`$.
208pub type RotationCurrentConfiguration = TensorRank2<3, 1, 1>;
209
210/// The rate of rotation of the current configuration $`\dot{\mathbf{Q}}`$.
211pub type RotationRateCurrentConfiguration = TensorRank2<3, 1, 1>;
212
213/// The rotation of the reference configuration $`\mathbf{Q}_0`$.
214pub type RotationReferenceConfiguration = TensorRank2<3, 0, 0>;
215
216/// A list of scalars.
217pub type Scalars<const W: usize> = TensorRank0List<W>;
218
219/// The second Piola-Kirchhoff stress $`\mathbf{S}`$.
220pub type SecondPiolaKirchhoffStress = TensorRank2<3, 0, 0>;
221
222/// The tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}`$.
223pub type SecondPiolaKirchhoffTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
224
225/// The rate tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{W}}`$.
226pub type SecondPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
227
228/// A stiffness resulting from a force.
229pub type Stiffness = TensorRank2<3, 1, 1>;
230
231/// A list of stiffnesses.
232pub type Stiffnesses<const W: usize> = TensorRank2List2D<3, 1, 1, W, W>;
233
234/// The stretching rate $`\mathbf{D}`$.
235pub type StretchingRate = TensorRank2<3, 1, 1>;
236
237/// The plastic stretching rate $`\mathbf{D}^\mathrm{p}`$.
238pub type StretchingRatePlastic = TensorRank2<3, 2, 2>;
239
240/// The temperature gradient.
241pub type TemperatureGradient = TensorRank1<3, 1>;
242
243/// A vector of times.
244pub type Times = crate::math::Vector;
245
246/// A traction.
247pub type Traction = TensorRank1<3, 1>;
248
249/// A vector.
250pub type Vector<const I: usize> = TensorRank1<3, I>;
251
252/// A list of vectors.
253pub type Vectors<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
254
255/// A 2D list of vectors.
256pub type Vectors2D<const I: usize, const W: usize, const X: usize> = TensorRank1List2D<3, I, W, X>;