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, TensorRank1, TensorRank1List, TensorRank1List2D, TensorRank1RefVec,
10        TensorRank1Vec, TensorRank1Vec2D, TensorRank2, TensorRank2List, TensorRank2List2D,
11        TensorRank2Vec, TensorRank2Vec2D, TensorRank4, TensorRank4List, TensorRank4Vec,
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}.\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}.\x1b[0;91m")
39            }
40        };
41        write!(f, "{error}\x1b[0m")
42    }
43}
44
45/// Methods for deformation gradients.
46pub trait Deformation<const I: usize, const J: usize> {
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) -> TensorRank2<3, I, I>;
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) -> TensorRank2<3, J, J>;
65}
66
67impl<const I: usize, const J: usize> Deformation<I, J> for DeformationGradientGeneral<I, J> {
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) -> TensorRank2<3, I, I> {
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) -> TensorRank2<3, J, J> {
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/// A basis.
102pub type Basis = TensorRank1List<3, 1, 3>;
103
104/// A list of bases.
105pub type Bases<const N: usize> = TensorRank1List2D<3, 1, 3, N>;
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 tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{T}}_1`$.
117pub type CauchyTangentStiffness1 = TensorRank4<3, 1, 1, 1, 2>;
118
119/// The tangent stiffness associated with the elastic Cauchy stress $`\boldsymbol{\mathcal{T}}_\mathrm{e}`$.
120pub type CauchyTangentStiffnessElastic = TensorRank4<3, 1, 1, 1, 2>;
121
122/// The rate tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{V}}`$.
123pub type CauchyRateTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
124
125/// A coordinate.
126pub type Coordinate<const I: usize> = TensorRank1<3, I>;
127
128/// A list of coordinates.
129pub type CoordinateList<const I: usize, const N: usize> = TensorRank1List<3, I, N>;
130
131/// A vector of coordinates.
132pub type Coordinates<const I: usize> = TensorRank1Vec<3, I>;
133
134/// A vector of references to coordinates.
135pub type CoordinatesRef<'a, const I: usize> = TensorRank1RefVec<'a, 3, I>;
136
137/// A coordinate in the current configuration.
138pub type CurrentCoordinate = TensorRank1<3, 1>;
139
140/// A list of coordinates in the current configuration.
141pub type CurrentCoordinates<const W: usize> = TensorRank1List<3, 1, W>;
142
143/// A vector of references to current coordinates.
144pub type CurrentCoordinatesRef<'a> = TensorRank1RefVec<'a, 3, 1>;
145
146/// A velocity in the current configuration.
147pub type CurrentVelocity = TensorRank1<3, 1>;
148
149/// The deformation gradient $`\mathbf{F}`$.
150pub type DeformationGradient = TensorRank2<3, 1, 0>;
151
152/// The second deformation gradient $`\mathbf{F}_2`$.
153pub type DeformationGradient2 = TensorRank2<3, 2, 0>;
154
155/// The elastic deformation gradient $`\mathbf{F}_\mathrm{e}`$.
156pub type DeformationGradientElastic = TensorRank2<3, 1, 2>;
157
158/// A general deformation gradient.
159pub type DeformationGradientGeneral<const I: usize, const J: usize> = TensorRank2<3, I, J>;
160
161/// The plastic deformation gradient $`\mathbf{F}_\mathrm{p}`$.
162pub type DeformationGradientPlastic = TensorRank2<3, 2, 0>;
163
164/// The deformation gradient rate $`\dot{\mathbf{F}}`$.
165pub type DeformationGradientRate = TensorRank2<3, 1, 0>;
166
167/// The plastic deformation gradient rate $`\dot{\mathbf{F}}_\mathrm{p}`$.
168pub type DeformationGradientRatePlastic = TensorRank2<3, 2, 0>;
169
170/// A list of deformation gradients.
171pub type DeformationGradientList<const W: usize> = TensorRank2List<3, 1, 0, W>;
172
173/// A list of deformation gradient rates.
174pub type DeformationGradientRateList<const W: usize> = TensorRank2List<3, 1, 0, W>;
175
176/// A vector of deformation gradients.
177pub type DeformationGradients = TensorRank2Vec<3, 1, 0>;
178
179/// A vector of plastic deformation gradients.
180pub type DeformationGradientsPlastic = TensorRank2Vec<3, 2, 0>;
181
182/// A vector of deformation gradient rates.
183pub type DeformationGradientRates = TensorRank2Vec<3, 1, 0>;
184
185/// A vector of plastic deformation gradient rates.
186pub type DeformationGradientRatesPlastic = TensorRank2Vec<3, 2, 0>;
187
188/// A displacement.
189pub type Displacement = TensorRank1<3, 1>;
190
191/// The first Piola-Kirchhoff stress $`\mathbf{P}`$.
192pub type FirstPiolaKirchhoffStress = TensorRank2<3, 1, 0>;
193
194/// The first Piola-Kirchhoff stress $`\mathbf{P}_1`$.
195pub type FirstPiolaKirchhoffStress1 = TensorRank2<3, 1, 2>;
196
197/// The first Piola-Kirchhoff stress $`\mathbf{P}_2`$.
198pub type FirstPiolaKirchhoffStress2 = TensorRank2<3, 2, 0>;
199
200/// The elastic first Piola-Kirchhoff stress $`\mathbf{P}_\mathrm{e}`$.
201pub type FirstPiolaKirchhoffStressElastic = FirstPiolaKirchhoffStress1;
202
203/// A list of first Piola-Kirchhoff stresses.
204pub type FirstPiolaKirchhoffStressList<const N: usize> = TensorRank2List<3, 1, 0, N>;
205
206/// A vector of first Piola-Kirchhoff stresses.
207pub type FirstPiolaKirchhoffStresses = TensorRank2Vec<3, 1, 0>;
208
209/// The tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}`$.
210pub type FirstPiolaKirchhoffTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
211
212/// The first tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_1`$.
213pub type FirstPiolaKirchhoffTangentStiffness1 = TensorRank4<3, 1, 2, 1, 2>;
214
215/// The second tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_2`$.
216pub type FirstPiolaKirchhoffTangentStiffness2 = TensorRank4<3, 2, 0, 2, 0>;
217
218/// The elastic tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_\mathrm{e}`$.
219pub type FirstPiolaKirchhoffTangentStiffnessElastic = FirstPiolaKirchhoffTangentStiffness1;
220
221/// A list of first Piola-Kirchhoff tangent stiffnesses.
222pub type FirstPiolaKirchhoffTangentStiffnessList<const N: usize> =
223    TensorRank4List<3, 1, 0, 1, 0, N>;
224
225/// A vector of first Piola-Kirchhoff tangent stiffnesses.
226pub type FirstPiolaKirchhoffTangentStiffnesses = TensorRank4Vec<3, 1, 0, 1, 0>;
227
228/// The rate tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{U}}`$.
229pub type FirstPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
230
231/// A list of first Piola-Kirchhoff rate tangent stiffnesses.
232pub type FirstPiolaKirchhoffRateTangentStiffnesses<const W: usize> =
233    TensorRank4List<3, 1, 0, 1, 0, W>;
234
235/// A force.
236pub type Force = TensorRank1<3, 1>;
237
238/// A list of forces.
239pub type ForceList<const N: usize> = TensorRank1List<3, 1, N>;
240
241/// A vector of forces.
242pub type Forces = TensorRank1Vec<3, 1>;
243
244/// The frame spin $`\mathbf{\Omega}=\dot{\mathbf{Q}}\cdot\mathbf{Q}^T`$.
245pub type FrameSpin = TensorRank2<3, 1, 1>;
246
247/// The heat flux.
248pub type HeatFlux = TensorRank1<3, 0>;
249
250/// A list of heat fluxes.
251pub type HeatFluxes<const N: usize> = TensorRank1List<3, 0, N>;
252
253/// The heat flux tangent.
254pub type HeatFluxTangent = TensorRank2<3, 0, 0>;
255
256/// A list of heat flux tangents.
257pub type HeatFluxTangents<const N: usize> = TensorRank2List<3, 0, 0, N>;
258
259/// The left Cauchy-Green deformation $`\mathbf{B}`$.
260pub type LeftCauchyGreenDeformation = TensorRank2<3, 1, 1>;
261
262/// The Mandel stress $`\mathbf{M}`$.
263pub type MandelStress = TensorRank2<3, 0, 0>;
264
265/// The elastic stress $`\mathbf{M}_e`$.
266pub type MandelStressElastic = TensorRank2<3, 2, 2>;
267
268/// A normal.
269pub type Normal = TensorRank1<3, 1>;
270
271/// A list of normals.
272pub type Normals<const N: usize> = TensorRank1List<3, 1, N>;
273
274/// A list of normal gradients.
275pub type NormalGradients<const O: usize, const P: usize> = TensorRank2List2D<3, 1, 1, O, P>;
276
277/// A normal rate.
278pub type NormalRate = TensorRank1<3, 1>;
279
280/// A list of normal rates.
281pub type NormalRates<const N: usize> = TensorRank1List<3, 1, N>;
282
283/// A coordinate in the reference configuration.
284pub type ReferenceCoordinate = TensorRank1<3, 0>;
285
286/// A list of coordinates in the reference configuration.
287pub type ReferenceCoordinates<const W: usize> = TensorRank1List<3, 0, W>;
288
289/// A reference normal.
290pub type ReferenceNormal = TensorRank1<3, 0>;
291
292/// A list of reference normals.
293pub type ReferenceNormals<const N: usize> = TensorRank1List<3, 0, N>;
294
295/// The right Cauchy-Green deformation $`\mathbf{C}`$.
296pub type RightCauchyGreenDeformation = TensorRank2<3, 0, 0>;
297
298/// The rotation of the current configuration $`\mathbf{Q}`$.
299pub type RotationCurrentConfiguration = TensorRank2<3, 1, 1>;
300
301/// A list of rotations of the current configuration.
302pub type RotationCurrentConfigurationList<const N: usize> = TensorRank2List<3, 1, 1, N>;
303
304/// The rate of rotation of the current configuration $`\dot{\mathbf{Q}}`$.
305pub type RotationRateCurrentConfiguration = TensorRank2<3, 1, 1>;
306
307/// The rotation of the reference configuration $`\mathbf{Q}_0`$.
308pub type RotationReferenceConfiguration = TensorRank2<3, 0, 0>;
309
310/// A separation.
311pub type Separation = Displacement;
312
313/// The second Piola-Kirchhoff stress $`\mathbf{S}`$.
314pub type SecondPiolaKirchhoffStress = TensorRank2<3, 0, 0>;
315
316/// The elastic second Piola-Kirchhoff stress $`\mathbf{S}`$.
317pub type SecondPiolaKirchhoffStressElastic = TensorRank2<3, 2, 2>;
318
319/// The tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}`$.
320pub type SecondPiolaKirchhoffTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
321
322/// The elastic tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}_\mathrm{e}`$.
323pub type SecondPiolaKirchhoffTangentStiffnessElastic = TensorRank4<3, 2, 2, 1, 2>;
324
325/// The rate tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{W}}`$.
326pub type SecondPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
327
328/// A stiffness resulting from a force.
329pub type Stiffness = TensorRank2<3, 1, 1>;
330
331/// A list of stiffnesses.
332pub type StiffnessList<const N: usize> = TensorRank2List<3, 1, 1, N>;
333
334/// A 2D list of stiffnesses.
335pub type StiffnessList2D<const N: usize> = TensorRank2List2D<3, 1, 1, N, N>;
336
337/// A vector of stiffnesses.
338pub type Stiffnesses = TensorRank2Vec2D<3, 1, 1>;
339
340/// The stretching rate $`\mathbf{D}`$.
341pub type StretchingRate = TensorRank2<3, 1, 1>;
342
343/// The plastic stretching rate $`\mathbf{D}^\mathrm{p}`$.
344pub type StretchingRatePlastic = TensorRank2<3, 2, 2>;
345
346/// A surface basis.
347pub type SurfaceBasis<const I: usize> = TensorRank1List<3, I, 2>;
348
349/// A list of surface bases.
350pub type SurfaceBases<const I: usize, const N: usize> = TensorRank1List2D<3, I, 2, N>;
351
352/// The temperature gradient.
353pub type TemperatureGradient = TensorRank1<3, 0>;
354
355/// A list of temperature gradients.
356pub type TemperatureGradients<const N: usize> = TensorRank1List<3, 0, N>;
357
358/// A vector of times.
359pub type Times = crate::math::Vector;
360
361/// A traction.
362pub type Traction = TensorRank1<3, 1>;
363
364/// A list of tractions.
365pub type TractionList<const N: usize> = TensorRank1List<3, 1, N>;
366
367/// A vector.
368pub type Vector<const I: usize> = TensorRank1<3, I>;
369
370/// A list of vectors.
371pub type VectorList<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
372
373/// A 2D list of vectors.
374pub type VectorList2D<const I: usize, const W: usize, const X: usize> =
375    TensorRank1List2D<3, I, W, X>;
376
377/// A vector of vectors.
378pub type Vectors<const I: usize> = TensorRank1Vec<3, I>;
379
380/// A 2D vector of vectors.
381pub type Vectors2D<const I: usize> = TensorRank1Vec2D<3, I>;