Skip to main content

conspire/mechanics/
mod.rs

1//! Mechanics library.
2
3#[cfg(test)]
4pub mod test;
5
6use crate::math::{
7    Rank2, Style, StyledError, Tensor, TensorRank1, TensorRank1List, TensorRank1List2D,
8    TensorRank1RefVec, TensorRank1Vec, TensorRank1Vec2D, TensorRank2, TensorRank2List,
9    TensorRank2List2D, TensorRank2Vec, TensorRank2Vec2D, TensorRank4, TensorRank4List,
10    TensorRank4Vec, styled_error,
11};
12
13pub use crate::math::Scalar;
14
15/// Possible errors for deformation gradients.
16pub enum DeformationError {
17    InvalidJacobian(Scalar),
18}
19
20impl StyledError for DeformationError {
21    fn message(&self, style: &Style) -> String {
22        let (h, c) = (style.headline, style.frame);
23        match self {
24            Self::InvalidJacobian(jacobian) => {
25                format!("{h}Invalid Jacobian: {jacobian:.6e}.{c}")
26            }
27        }
28    }
29}
30
31styled_error!(DeformationError);
32
33/// Methods for deformation gradients.
34pub trait Deformation<const I: usize, const J: usize> {
35    /// Calculates and returns the Jacobian.
36    ///
37    /// ```math
38    /// J = \mathrm{det}(\mathbf{F})
39    /// ```
40    fn jacobian(&self) -> Result<Scalar, DeformationError>;
41    /// Calculates and returns the left Cauchy-Green deformation.
42    ///
43    /// ```math
44    /// \mathbf{B} = \mathbf{F}\cdot\mathbf{F}^T
45    /// ```
46    fn left_cauchy_green(&self) -> TensorRank2<3, I, I>;
47    /// Calculates and returns the right Cauchy-Green deformation.
48    ///
49    /// ```math
50    /// \mathbf{C} = \mathbf{F}^T\cdot\mathbf{F}
51    /// ```
52    fn right_cauchy_green(&self) -> TensorRank2<3, J, J>;
53}
54
55impl<const I: usize, const J: usize> Deformation<I, J> for DeformationGradientGeneral<I, J> {
56    fn jacobian(&self) -> Result<Scalar, DeformationError> {
57        let jacobian = self.determinant();
58        if jacobian > 0.0 {
59            Ok(jacobian)
60        } else {
61            Err(DeformationError::InvalidJacobian(jacobian))
62        }
63    }
64    fn left_cauchy_green(&self) -> TensorRank2<3, I, I> {
65        self.iter()
66            .map(|deformation_gradient_i| {
67                self.iter()
68                    .map(|deformation_gradient_j| deformation_gradient_i * deformation_gradient_j)
69                    .collect()
70            })
71            .collect()
72    }
73    fn right_cauchy_green(&self) -> TensorRank2<3, J, J> {
74        let deformation_gradient_transpose = self.transpose();
75        deformation_gradient_transpose
76            .iter()
77            .map(|deformation_gradient_transpose_i| {
78                deformation_gradient_transpose
79                    .iter()
80                    .map(|deformation_gradient_transpose_j| {
81                        deformation_gradient_transpose_i * deformation_gradient_transpose_j
82                    })
83                    .collect()
84            })
85            .collect()
86    }
87}
88
89/// A basis.
90pub type Basis = TensorRank1List<3, 1, 3>;
91
92/// A list of bases.
93pub type Bases<const N: usize> = TensorRank1List2D<3, 1, 3, N>;
94
95/// The Cauchy stress $`\boldsymbol{\sigma}`$.
96pub type CauchyStress = TensorRank2<3, 1, 1>;
97
98/// A list of Cauchy stresses.
99pub type CauchyStresses<const W: usize> = TensorRank2List<3, 1, 1, W>;
100
101/// The tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{T}}`$.
102pub type CauchyTangentStiffness = TensorRank4<3, 1, 1, 1, 0>;
103
104/// The tangent stiffness associated with the Cauchy stress $`\boldsymbol{\mathcal{T}}_1`$.
105pub type CauchyTangentStiffness1 = TensorRank4<3, 1, 1, 1, 2>;
106
107/// The tangent stiffness associated with the elastic Cauchy stress $`\boldsymbol{\mathcal{T}}_\mathrm{e}`$.
108pub type CauchyTangentStiffnessElastic = TensorRank4<3, 1, 1, 1, 2>;
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 coordinate.
114pub type Coordinate<const I: usize> = TensorRank1<3, I>;
115
116/// A list of coordinates.
117pub type CoordinateList<const I: usize, const N: usize> = TensorRank1List<3, I, N>;
118
119/// A vector of coordinates.
120pub type Coordinates<const I: usize> = TensorRank1Vec<3, I>;
121
122/// A vector of references to coordinates.
123pub type CoordinatesRef<'a, const I: usize> = TensorRank1RefVec<'a, 3, I>;
124
125/// A coordinate in the current configuration.
126pub type CurrentCoordinate = TensorRank1<3, 1>;
127
128/// A list of coordinates in the current configuration.
129pub type CurrentCoordinates<const W: usize> = TensorRank1List<3, 1, W>;
130
131/// A vector of references to current coordinates.
132pub type CurrentCoordinatesRef<'a> = TensorRank1RefVec<'a, 3, 1>;
133
134/// A velocity in the current configuration.
135pub type CurrentVelocity = TensorRank1<3, 1>;
136
137/// The deformation gradient $`\mathbf{F}`$.
138pub type DeformationGradient = TensorRank2<3, 1, 0>;
139
140/// The second deformation gradient $`\mathbf{F}_2`$.
141pub type DeformationGradient2 = TensorRank2<3, 2, 0>;
142
143/// The elastic deformation gradient $`\mathbf{F}_\mathrm{e}`$.
144pub type DeformationGradientElastic = TensorRank2<3, 1, 2>;
145
146/// A general deformation gradient.
147pub type DeformationGradientGeneral<const I: usize, const J: usize> = TensorRank2<3, I, J>;
148
149/// The plastic deformation gradient $`\mathbf{F}_\mathrm{p}`$.
150pub type DeformationGradientPlastic = TensorRank2<3, 2, 0>;
151
152/// The deformation gradient rate $`\dot{\mathbf{F}}`$.
153pub type DeformationGradientRate = TensorRank2<3, 1, 0>;
154
155/// The plastic deformation gradient rate $`\dot{\mathbf{F}}_\mathrm{p}`$.
156pub type DeformationGradientRatePlastic = TensorRank2<3, 2, 0>;
157
158/// A list of deformation gradients.
159pub type DeformationGradientList<const W: usize> = TensorRank2List<3, 1, 0, W>;
160
161/// A list of deformation gradient rates.
162pub type DeformationGradientRateList<const W: usize> = TensorRank2List<3, 1, 0, W>;
163
164/// A vector of deformation gradients.
165pub type DeformationGradients = TensorRank2Vec<3, 1, 0>;
166
167/// A vector of plastic deformation gradients.
168pub type DeformationGradientsPlastic = TensorRank2Vec<3, 2, 0>;
169
170/// A vector of deformation gradient rates.
171pub type DeformationGradientRates = TensorRank2Vec<3, 1, 0>;
172
173/// A vector of plastic deformation gradient rates.
174pub type DeformationGradientRatesPlastic = TensorRank2Vec<3, 2, 0>;
175
176/// A displacement.
177pub type Displacement = TensorRank1<3, 1>;
178
179/// The first Piola-Kirchhoff stress $`\mathbf{P}`$.
180pub type FirstPiolaKirchhoffStress = TensorRank2<3, 1, 0>;
181
182/// The first Piola-Kirchhoff stress $`\mathbf{P}_1`$.
183pub type FirstPiolaKirchhoffStress1 = TensorRank2<3, 1, 2>;
184
185/// The first Piola-Kirchhoff stress $`\mathbf{P}_2`$.
186pub type FirstPiolaKirchhoffStress2 = TensorRank2<3, 2, 0>;
187
188/// The elastic first Piola-Kirchhoff stress $`\mathbf{P}_\mathrm{e}`$.
189pub type FirstPiolaKirchhoffStressElastic = FirstPiolaKirchhoffStress1;
190
191/// A list of first Piola-Kirchhoff stresses.
192pub type FirstPiolaKirchhoffStressList<const N: usize> = TensorRank2List<3, 1, 0, N>;
193
194/// A vector of first Piola-Kirchhoff stresses.
195pub type FirstPiolaKirchhoffStresses = TensorRank2Vec<3, 1, 0>;
196
197/// The tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}`$.
198pub type FirstPiolaKirchhoffTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
199
200/// The first tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_1`$.
201pub type FirstPiolaKirchhoffTangentStiffness1 = TensorRank4<3, 1, 2, 1, 2>;
202
203/// The second tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_2`$.
204pub type FirstPiolaKirchhoffTangentStiffness2 = TensorRank4<3, 2, 0, 2, 0>;
205
206/// The elastic tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{C}}_\mathrm{e}`$.
207pub type FirstPiolaKirchhoffTangentStiffnessElastic = FirstPiolaKirchhoffTangentStiffness1;
208
209/// A list of first Piola-Kirchhoff tangent stiffnesses.
210pub type FirstPiolaKirchhoffTangentStiffnessList<const N: usize> =
211    TensorRank4List<3, 1, 0, 1, 0, N>;
212
213/// A vector of first Piola-Kirchhoff tangent stiffnesses.
214pub type FirstPiolaKirchhoffTangentStiffnesses = TensorRank4Vec<3, 1, 0, 1, 0>;
215
216/// The rate tangent stiffness associated with the first Piola-Kirchhoff stress $`\boldsymbol{\mathcal{U}}`$.
217pub type FirstPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 1, 0, 1, 0>;
218
219/// A list of first Piola-Kirchhoff rate tangent stiffnesses.
220pub type FirstPiolaKirchhoffRateTangentStiffnesses<const W: usize> =
221    TensorRank4List<3, 1, 0, 1, 0, W>;
222
223/// A force.
224pub type Force = TensorRank1<3, 1>;
225
226/// A list of forces.
227pub type ForceList<const N: usize> = TensorRank1List<3, 1, N>;
228
229/// A vector of forces.
230pub type Forces = TensorRank1Vec<3, 1>;
231
232/// The frame spin $`\mathbf{\Omega}=\dot{\mathbf{Q}}\cdot\mathbf{Q}^T`$.
233pub type FrameSpin = TensorRank2<3, 1, 1>;
234
235/// The heat flux.
236pub type HeatFlux = TensorRank1<3, 0>;
237
238/// A list of heat fluxes.
239pub type HeatFluxes<const N: usize> = TensorRank1List<3, 0, N>;
240
241/// The heat flux tangent.
242pub type HeatFluxTangent = TensorRank2<3, 0, 0>;
243
244/// A list of heat flux tangents.
245pub type HeatFluxTangents<const N: usize> = TensorRank2List<3, 0, 0, N>;
246
247/// The left Cauchy-Green deformation $`\mathbf{B}`$.
248pub type LeftCauchyGreenDeformation = TensorRank2<3, 1, 1>;
249
250/// The Mandel stress $`\mathbf{M}`$.
251pub type MandelStress = TensorRank2<3, 0, 0>;
252
253/// The elastic stress $`\mathbf{M}_e`$.
254pub type MandelStressElastic = TensorRank2<3, 2, 2>;
255
256/// A normal.
257pub type Normal = TensorRank1<3, 1>;
258
259/// A list of normals.
260pub type Normals<const N: usize> = TensorRank1List<3, 1, N>;
261
262/// A list of normal gradients.
263pub type NormalGradients<const O: usize, const P: usize> = TensorRank2List2D<3, 1, 1, O, P>;
264
265/// A normal rate.
266pub type NormalRate = TensorRank1<3, 1>;
267
268/// A list of normal rates.
269pub type NormalRates<const N: usize> = TensorRank1List<3, 1, N>;
270
271/// A coordinate in the reference configuration.
272pub type ReferenceCoordinate = TensorRank1<3, 0>;
273
274/// A list of coordinates in the reference configuration.
275pub type ReferenceCoordinates<const W: usize> = TensorRank1List<3, 0, W>;
276
277/// A reference normal.
278pub type ReferenceNormal = TensorRank1<3, 0>;
279
280/// A list of reference normals.
281pub type ReferenceNormals<const N: usize> = TensorRank1List<3, 0, N>;
282
283/// The right Cauchy-Green deformation $`\mathbf{C}`$.
284pub type RightCauchyGreenDeformation = TensorRank2<3, 0, 0>;
285
286/// The rotation of the current configuration $`\mathbf{Q}`$.
287pub type RotationCurrentConfiguration = TensorRank2<3, 1, 1>;
288
289/// A list of rotations of the current configuration.
290pub type RotationCurrentConfigurationList<const N: usize> = TensorRank2List<3, 1, 1, N>;
291
292/// The rate of rotation of the current configuration $`\dot{\mathbf{Q}}`$.
293pub type RotationRateCurrentConfiguration = TensorRank2<3, 1, 1>;
294
295/// The rotation of the reference configuration $`\mathbf{Q}_0`$.
296pub type RotationReferenceConfiguration = TensorRank2<3, 0, 0>;
297
298/// A separation.
299pub type Separation = Displacement;
300
301/// The second Piola-Kirchhoff stress $`\mathbf{S}`$.
302pub type SecondPiolaKirchhoffStress = TensorRank2<3, 0, 0>;
303
304/// The elastic second Piola-Kirchhoff stress $`\mathbf{S}`$.
305pub type SecondPiolaKirchhoffStressElastic = TensorRank2<3, 2, 2>;
306
307/// The tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}`$.
308pub type SecondPiolaKirchhoffTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
309
310/// The elastic tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{G}}_\mathrm{e}`$.
311pub type SecondPiolaKirchhoffTangentStiffnessElastic = TensorRank4<3, 2, 2, 1, 2>;
312
313/// The rate tangent stiffness associated with the second Piola-Kirchhoff stress $`\boldsymbol{\mathcal{W}}`$.
314pub type SecondPiolaKirchhoffRateTangentStiffness = TensorRank4<3, 0, 0, 1, 0>;
315
316/// A stiffness resulting from a force.
317pub type Stiffness = TensorRank2<3, 1, 1>;
318
319/// A list of stiffnesses.
320pub type StiffnessList<const N: usize> = TensorRank2List<3, 1, 1, N>;
321
322/// A 2D list of stiffnesses.
323pub type StiffnessList2D<const N: usize> = TensorRank2List2D<3, 1, 1, N, N>;
324
325/// A vector of stiffnesses.
326pub type Stiffnesses = TensorRank2Vec2D<3, 1, 1>;
327
328/// The stretching rate $`\mathbf{D}`$.
329pub type StretchingRate = TensorRank2<3, 1, 1>;
330
331/// The plastic stretching rate $`\mathbf{D}^\mathrm{p}`$.
332pub type StretchingRatePlastic = TensorRank2<3, 2, 2>;
333
334/// A surface basis.
335pub type SurfaceBasis<const I: usize> = TensorRank1List<3, I, 2>;
336
337/// A list of surface bases.
338pub type SurfaceBases<const I: usize, const N: usize> = TensorRank1List2D<3, I, 2, N>;
339
340/// The temperature gradient.
341pub type TemperatureGradient = TensorRank1<3, 0>;
342
343/// A list of temperature gradients.
344pub type TemperatureGradients<const N: usize> = TensorRank1List<3, 0, N>;
345
346/// A vector of times.
347pub type Times = crate::math::Vector;
348
349/// A traction.
350pub type Traction = TensorRank1<3, 1>;
351
352/// A list of tractions.
353pub type TractionList<const N: usize> = TensorRank1List<3, 1, N>;
354
355/// A vector.
356pub type Vector<const I: usize> = TensorRank1<3, I>;
357
358/// A list of vectors.
359pub type VectorList<const I: usize, const W: usize> = TensorRank1List<3, I, W>;
360
361/// A 2D list of vectors.
362pub type VectorList2D<const I: usize, const W: usize, const X: usize> =
363    TensorRank1List2D<3, I, W, X>;
364
365/// A vector of vectors.
366pub type Vectors<const I: usize> = TensorRank1Vec<3, I>;
367
368/// A 2D vector of vectors.
369pub type Vectors2D<const I: usize> = TensorRank1Vec2D<3, I>;