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, DeformationGradient),
21}
22
23impl Debug for DeformationError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
25        let error = match self {
26            Self::InvalidJacobian(jacobian, deformation_gradient) => {
27                format!(
28                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
29                     From deformation gradient: {deformation_gradient}.",
30                )
31            }
32        };
33        write!(f, "\n{error}\n\x1b[0;2;31m{}\x1b[0m\n", defeat_message())
34    }
35}
36
37impl Display for DeformationError {
38    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39        let error = match self {
40            Self::InvalidJacobian(jacobian, deformation_gradient) => {
41                format!(
42                    "\x1b[1;91mInvalid Jacobian: {jacobian:.6e}.\x1b[0;91m\n\
43                     From deformation gradient: {deformation_gradient}."
44                )
45            }
46        };
47        write!(f, "{error}\x1b[0m")
48    }
49}
50
51/// Methods for deformation gradients.
52pub trait Deformation {
53    /// Calculates and returns the Jacobian.
54    ///
55    /// ```math
56    /// J = \mathrm{det}(\mathbf{F})
57    /// ```
58    fn jacobian(&self) -> Result<Scalar, DeformationError>;
59    /// Calculates and returns the left Cauchy-Green deformation.
60    ///
61    /// ```math
62    /// \mathbf{B} = \mathbf{F}\cdot\mathbf{F}^T
63    /// ```
64    fn left_cauchy_green(&self) -> LeftCauchyGreenDeformation;
65    /// Calculates and returns the right Cauchy-Green deformation.
66    ///
67    /// ```math
68    /// \mathbf{C} = \mathbf{F}^T\cdot\mathbf{F}
69    /// ```
70    fn right_cauchy_green(&self) -> RightCauchyGreenDeformation;
71}
72
73impl Deformation for DeformationGradient {
74    fn jacobian(&self) -> Result<Scalar, DeformationError> {
75        let jacobian = self.determinant();
76        if jacobian > 0.0 {
77            Ok(jacobian)
78        } else {
79            Err(DeformationError::InvalidJacobian(jacobian, self.clone()))
80        }
81    }
82    fn left_cauchy_green(&self) -> LeftCauchyGreenDeformation {
83        self.iter()
84            .map(|deformation_gradient_i| {
85                self.iter()
86                    .map(|deformation_gradient_j| deformation_gradient_i * deformation_gradient_j)
87                    .collect()
88            })
89            .collect()
90    }
91    fn right_cauchy_green(&self) -> RightCauchyGreenDeformation {
92        let deformation_gradient_transpose = self.transpose();
93        deformation_gradient_transpose
94            .iter()
95            .map(|deformation_gradient_transpose_i| {
96                deformation_gradient_transpose
97                    .iter()
98                    .map(|deformation_gradient_transpose_j| {
99                        deformation_gradient_transpose_i * deformation_gradient_transpose_j
100                    })
101                    .collect()
102            })
103            .collect()
104    }
105}
106
107/// The Cauchy stress $`\boldsymbol{\sigma}`$.
108pub type CauchyStress = TensorRank2<3, 1, 1>;
109
110/// A list of Cauchy stresses.
111pub type CauchyStresses<const W: usize> = TensorRank2List<3, 1, 1, W>;
112
113/// The tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{T}}`$.
114pub type CauchyTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
115
116/// The rate tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{V}}`$.
117pub type CauchyRateTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
118
119/// A list of coordinates.
120pub type Coordinates<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
121
122/// A coordinate in the current configuration.
123pub type CurrentCoordinate = TensorRank1<3, 1>;
124
125/// A list of coordinates in the current configuration.
126pub type CurrentCoordinates<const W: usize> = TensorRank1List<3, 1, W>;
127
128/// A velocity in the current configuration.
129pub type CurrentVelocity = TensorRank1<3, 1>;
130
131/// The deformation gradient $`\mathbf{F}`$.
132pub type DeformationGradient = TensorRank2<3, 1, 0>;
133
134/// The elastic deformation gradient $`\mathbf{F}_\mathrm{e}`$.
135pub type DeformationGradientElastic = TensorRank2<3, 1, 2>;
136
137/// A general deformation gradient.
138pub type DeformationGradientGeneral<const I: usize, const J: usize> = TensorRank2<3, I, J>;
139
140/// The plastic deformation gradient $`\mathbf{F}_\mathrm{p}`$.
141pub type DeformationGradientPlastic = TensorRank2<3, 2, 0>;
142
143/// The deformation gradient rate $`\dot{\mathbf{F}}`$.
144pub type DeformationGradientRate = TensorRank2<3, 1, 0>;
145
146/// The plastic deformation gradient rate $`\dot{\mathbf{F}}_\mathrm{p}`$.
147pub type DeformationGradientRatePlastic = TensorRank2<3, 2, 0>;
148
149/// A list of deformation gradients.
150pub type DeformationGradientList<const W: usize> = TensorRank2List<3, 1, 0, W>;
151
152/// A list of deformation gradient rates.
153pub type DeformationGradientRateList<const W: usize> = TensorRank2List<3, 1, 0, W>;
154
155/// A vector of deformation gradients.
156pub type DeformationGradients = TensorRank2Vec<3, 1, 0>;
157
158/// A vector of deformation gradient rates.
159pub type DeformationGradientRates = TensorRank2Vec<3, 1, 0>;
160
161/// A displacement.
162pub type Displacement = TensorRank1<3, 1>;
163
164/// The first Piola-Kirchhoff stress $`\mathbf{P}`$.
165pub type FirstPiolaKirchhoffStress = TensorRank2<3, 1, 0>;
166
167/// A list of first Piola-Kirchhoff stresses.
168pub type FirstPiolaKirchhoffStresses<const W: usize> = TensorRank2List<3, 1, 0, W>;
169
170/// The tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}`$.
171pub type FirstPiolaKirchhoffTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
172
173/// A list of first Piola-Kirchhoff tangent stiffnesses.
174pub type FirstPiolaKirchhoffTangentStiffnesses<const W: usize> = TensorRank4List<3, 1, 0, 1, 0, W>;
175
176/// The rate tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{U}}`$.
177pub type FirstPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
178
179/// A list of first Piola-Kirchhoff rate tangent stiffnesses.
180pub type FirstPiolaKirchhoffRateTangentStiffnesses<const W: usize> =
181    TensorRank4List<3, 1, 0, 1, 0, W>;
182
183/// A force.
184pub type Force = TensorRank1<3, 1>;
185
186/// A list of forces.
187pub type Forces<const W: usize> = TensorRank1List<3, 1, W>;
188
189/// The frame spin $`\mathbf{\Omega}=\dot{\mathbf{Q}}\cdot\mathbf{Q}^T`$.
190pub type FrameSpin = TensorRank2<3, 1, 1>;
191
192/// The heat flux.
193pub type HeatFlux = TensorRank1<3, 1>;
194
195/// The left Cauchy-Green deformation $`\mathbf{B}`$.
196pub type LeftCauchyGreenDeformation = TensorRank2<3, 1, 1>;
197
198/// The Mandel stress $`\mathbf{M}`$.
199pub type MandelStress = TensorRank2<3, 2, 2>;
200
201/// A normal.
202pub type Normal = TensorRank1<3, 1>;
203
204/// A coordinate in the reference configuration.
205pub type ReferenceCoordinate = TensorRank1<3, 0>;
206
207/// A list of coordinates in the reference configuration.
208pub type ReferenceCoordinates<const W: usize> = TensorRank1List<3, 0, W>;
209
210/// The right Cauchy-Green deformation $`\mathbf{C}`$.
211pub type RightCauchyGreenDeformation = TensorRank2<3, 0, 0>;
212
213/// The rotation of the current configuration $`\mathbf{Q}`$.
214pub type RotationCurrentConfiguration = TensorRank2<3, 1, 1>;
215
216/// The rate of rotation of the current configuration $`\dot{\mathbf{Q}}`$.
217pub type RotationRateCurrentConfiguration = TensorRank2<3, 1, 1>;
218
219/// The rotation of the reference configuration $`\mathbf{Q}_0`$.
220pub type RotationReferenceConfiguration = TensorRank2<3, 0, 0>;
221
222/// A list of scalars.
223pub type Scalars<const W: usize> = TensorRank0List<W>;
224
225/// The second Piola-Kirchhoff stress $`\mathbf{S}`$.
226pub type SecondPiolaKirchhoffStress = TensorRank2<3, 0, 0>;
227
228/// The tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}`$.
229pub type SecondPiolaKirchhoffTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
230
231/// The rate tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{W}}`$.
232pub type SecondPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
233
234/// A stiffness resulting from a force.
235pub type Stiffness = TensorRank2<3, 1, 1>;
236
237/// A list of stiffnesses.
238pub type Stiffnesses<const W: usize> = TensorRank2List2D<3, 1, 1, W, W>;
239
240/// The stretching rate $`\mathbf{D}`$.
241pub type StretchingRate = TensorRank2<3, 1, 1>;
242
243/// The plastic stretching rate $`\mathbf{D}^\mathrm{p}`$.
244pub type StretchingRatePlastic = TensorRank2<3, 2, 2>;
245
246/// The temperature gradient.
247pub type TemperatureGradient = TensorRank1<3, 1>;
248
249/// A vector of times.
250pub type Times = crate::math::Vector;
251
252/// A traction.
253pub type Traction = TensorRank1<3, 1>;
254
255/// A vector.
256pub type Vector<const I: usize> = TensorRank1<3, I>;
257
258/// A list of vectors.
259pub type Vectors<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
260
261/// A 2D list of vectors.
262pub type Vectors2D<const I: usize, const W: usize, const X: usize> = TensorRank1List2D<3, I, W, X>;