conspire/domain/fem/block/element/
mod.rs1#[cfg(test)]
2mod test;
3
4pub mod cohesive;
5pub mod composite;
6pub mod linear;
7pub mod quadratic;
8pub mod serendipity;
9pub mod solid;
10pub mod surface;
11pub mod thermal;
12
13use crate::{
14 defeat_message,
15 math::{Scalar, ScalarList, TensorRank1, TensorRank1List, TensorRank1List2D, TestError},
16 mechanics::{CoordinateList, CurrentCoordinates, ReferenceCoordinates, VectorList2D},
17};
18use std::fmt::{self, Debug, Display, Formatter};
19
20const A: usize = 9;
21const FRAC_1_SQRT_3: Scalar = 0.577_350_269_189_625_8; const FRAC_SQRT_3_5: Scalar = 0.774_596_669_241_483;
23
24pub type ElementNodalCoordinates<const N: usize> = CurrentCoordinates<N>;
25pub type ElementNodalVelocities<const N: usize> = CurrentCoordinates<N>;
26pub type ElementNodalEitherCoordinates<const I: usize, const N: usize> = CoordinateList<I, N>;
27pub type ElementNodalReferenceCoordinates<const N: usize> = ReferenceCoordinates<N>;
28pub type GradientVectors<const G: usize, const N: usize> = VectorList2D<0, N, G>;
29pub type ParametricCoordinate<const M: usize> = TensorRank1<M, A>;
30pub type ParametricCoordinates<const G: usize, const M: usize> = TensorRank1List<M, A, G>;
31pub type ParametricReference<const M: usize, const N: usize> = TensorRank1List<M, A, N>;
32pub type ShapeFunctions<const N: usize> = TensorRank1<N, A>;
33pub type ShapeFunctionsAtIntegrationPoints<const G: usize, const N: usize> =
34 TensorRank1List<N, A, G>;
35pub type ShapeFunctionsGradients<const M: usize, const N: usize> = TensorRank1List<M, 0, N>;
36pub type StandardGradientOperators<const M: usize, const O: usize, const P: usize> =
37 TensorRank1List2D<M, 0, O, P>;
38pub type StandardGradientOperatorsTransposed<const M: usize, const O: usize, const P: usize> =
39 TensorRank1List2D<M, 0, P, O>;
40
41pub trait FiniteElement<const G: usize, const M: usize, const N: usize, const P: usize>
42where
43 Self: Debug,
44{
45 fn integration_points() -> ParametricCoordinates<G, M>;
46 fn integration_weights(&self) -> &ScalarList<G>;
47 fn minimum_scaled_jacobian<const I: usize>(
48 nodal_coordinates: ElementNodalEitherCoordinates<I, N>,
49 ) -> Scalar {
50 Self::scaled_jacobians(nodal_coordinates)
51 .into_iter()
52 .reduce(Scalar::min)
53 .unwrap()
54 }
55 fn parametric_reference() -> ParametricReference<M, N>;
56 fn parametric_weights() -> ScalarList<G>;
57 fn scaled_jacobians<const I: usize>(
58 nodal_coordinates: ElementNodalEitherCoordinates<I, N>,
59 ) -> ScalarList<P>;
60 fn shape_functions(parametric_coordinate: ParametricCoordinate<M>) -> ShapeFunctions<P>;
61 fn shape_functions_at_integration_points() -> ShapeFunctionsAtIntegrationPoints<G, P> {
62 Self::integration_points()
63 .into_iter()
64 .map(|integration_point| Self::shape_functions(integration_point))
65 .collect()
66 }
67 fn shape_functions_gradients(
68 parametric_coordinate: ParametricCoordinate<M>,
69 ) -> ShapeFunctionsGradients<M, P>;
70 fn shape_functions_gradients_at_integration_points() -> StandardGradientOperators<M, P, G> {
71 Self::integration_points()
72 .into_iter()
73 .map(|integration_point| Self::shape_functions_gradients(integration_point))
74 .collect()
75 }
76 fn volume(&self) -> Scalar {
77 self.integration_weights().into_iter().sum()
78 }
79}
80
81pub struct Element<const G: usize, const N: usize, const O: usize> {
82 gradient_vectors: GradientVectors<G, N>,
83 integration_weights: ScalarList<G>,
84}
85
86impl<const G: usize, const N: usize, const O: usize> Element<G, N, O> {
87 fn gradient_vectors(&self) -> &GradientVectors<G, N> {
88 &self.gradient_vectors
89 }
90}
91
92impl<const G: usize, const N: usize, const O: usize> Debug for Element<G, N, O> {
93 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
94 let element = match (G, N, O) {
95 (8, 8, 1) => "LinearHexahedron",
96 (8, 5, 1) => "LinearPyramid",
97 (1, 4, 1) => "LinearTetrahedron",
98 (6, 6, 1) => "LinearWedge",
99 (27, 27, 2) => "QuadraticHexahedron",
100 (4, 10, 2) => "QuadraticTetrahedron",
101 (27, 13, 2) => "QuadraticPyramid",
102 (18, 15, 2) => "QuadraticWedge",
103 (27, 20, 2) => "SerendipityHexahedron",
104 (4, 10, 0) => "CompositeTetrahedron",
105 _ => panic!(),
106 };
107 write!(f, "{element} {{ integration points: {G}, nodes: {N} }}",)
108 }
109}
110
111impl<const G: usize, const N: usize, const O: usize> Default for Element<G, N, O>
112where
113 Self: FiniteElement<G, 3, N, N> + From<ElementNodalReferenceCoordinates<N>>,
114{
115 fn default() -> Self {
116 ElementNodalReferenceCoordinates::from(Self::parametric_reference()).into()
117 }
118}
119
120fn basic_from<const G: usize, const N: usize, const O: usize>(
121 reference_nodal_coordinates: ElementNodalReferenceCoordinates<N>,
122) -> Element<G, N, O>
123where
124 Element<G, N, O>: FiniteElement<G, 3, N, N>,
125{
126 let gradient_vectors = Element::shape_functions_gradients_at_integration_points()
127 .into_iter()
128 .map(|standard_gradient_operator| {
129 (&reference_nodal_coordinates * &standard_gradient_operator).inverse_transpose()
130 * standard_gradient_operator
131 })
132 .collect();
133 let integration_weights = Element::shape_functions_gradients_at_integration_points()
134 .into_iter()
135 .zip(Element::parametric_weights())
136 .map(|(standard_gradient_operator, integration_weight)| {
137 (&reference_nodal_coordinates * standard_gradient_operator).determinant()
138 * integration_weight
139 })
140 .collect();
141 Element {
142 gradient_vectors,
143 integration_weights,
144 }
145}
146
147pub enum FiniteElementError {
148 Upstream(String, String),
149}
150
151impl From<FiniteElementError> for TestError {
152 fn from(error: FiniteElementError) -> Self {
153 Self {
154 message: error.to_string(),
155 }
156 }
157}
158
159impl Debug for FiniteElementError {
160 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
161 let error = match self {
162 Self::Upstream(error, element) => {
163 format!(
164 "{error}\x1b[0;91m\n\
165 In finite element: {element}."
166 )
167 }
168 };
169 write!(f, "\n{error}\n\x1b[0;2;31m{}\x1b[0m\n", defeat_message())
170 }
171}
172
173impl Display for FiniteElementError {
174 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
175 let error = match self {
176 Self::Upstream(error, element) => {
177 format!(
178 "{error}\x1b[0;91m\n\
179 In finite element: {element}."
180 )
181 }
182 };
183 write!(f, "{error}\x1b[0m")
184 }
185}