Debian Codegraph @

miniapps/hooke/operators/elasticity_operator.hpp

raw
1 // Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11
12 #ifndef MFEM_ELASTICITY_OP_HPP
13 #define MFEM_ELASTICITY_OP_HPP
14
15 #include "../kernels/elasticity_kernels.hpp"
16 #include "mfem.hpp"
17
18 namespace mfem
19 {
20 class ElasticityOperator : public Operator
21 {
22 public:
23 ElasticityOperator(ParMesh &mesh, const int order);
24
25 /**
26 * @brief Compute the residual Y = R(U) representing the elasticity equation
27 * with a material model chosen by calling SetMaterial.
28 *
29 * The output vector @a Y has essential degrees of freedom applied by setting
30 * them to zero. This ensures R(U)_i = 0 being satisfied for each essential
31 * dof i.
32 *
33 * @param U U
34 * @param Y Residual R(U)
35 */
36 virtual void Mult(const Vector &U, Vector &Y) const override;
37
38 /**
39 * @brief Get the Gradient object
40 *
41 * Update and cache the state vector @a U, used to compute the linearization
42 * dR(U)/dU.
43 *
44 * @param U
45 * @return Operator&
46 */
47 Operator &GetGradient(const Vector &U) const override;
48
49 /**
50 * @brief Multiply the linearization of the residual R(U) wrt to the current
51 * state U by a perturbation @a dX.
52 *
53 * Y = dR(U)/dU * dX = K(U) dX
54 *
55 * @param dX
56 * @param Y
57 */
58 void GradientMult(const Vector &dX, Vector &Y) const;
59
60 /**
61 * @brief Assemble the linearization of the residual K = dR(U)/dU.
62 *
63 * This method needs three input vectors which also act as output vectors.
64 * They don't have to be the right size on the first call, but it is advised
65 * that memory is kept alive during successive call. The data layout of the
66 * outputs will be
67 *
68 * @a Ke_diag: dofs x dofs x dofs x dim x ne x dim
69 *
70 * @a K_diag_local: width(H1_Restriction) x dim
71 *
72 * @a K_diag: width(H1_Prolongation) x dim
73 *
74 * This data layout is needed due to the Ordering::byNODES. See method
75 * implementation comments for more details. The output @a K_diag has
76 * modified entries when essential boundaries are defined. Each essential dof
77 * row and column are set to zero with it's diagonal entry set to 1.
78 *
79 * @param Ke_diag
80 * @param K_diag_local
81 * @param K_diag
82 */
83 void AssembleGradientDiagonal(Vector &Ke_diag, Vector &K_diag_local,
84 Vector &K_diag) const;
85
86 ~ElasticityOperator();
87
88 ParMesh &mesh_;
89 /// Polynomial order of the FE space
90 const int order_;
91 const int dim_;
92 const int vdim_;
93 /// Number of elements in the mesh (rank local)
94 const int ne_;
95 /// H1 finite element collection
96 H1_FECollection h1_fec_;
97 /// H1 finite element space
98 ParFiniteElementSpace h1_fes_;
99 // Integration rule
100 IntegrationRule *ir_ = nullptr;
101 /// Number of degrees of freedom in 1D
102 int d1d_;
103 /// Number of quadrature points in 1D
104 int q1d_;
105 const Operator *h1_element_restriction_;
106 const Operator *h1_prolongation_;
107 Array<int> ess_tdof_list_;
108 Array<int> displaced_tdof_list_;
109 Operator *gradient_;
110 const GeometricFactors *geometric_factors_;
111 const DofToQuad *maps_;
112 /// Input state L-vector
113 mutable Vector X_local_;
114 /// Input state E-vector
115 mutable Vector X_el_;
116 /// Output state L-vector
117 mutable Vector Y_local_;
118 /// Output state E-Vector
119 mutable Vector Y_el_;
120 /// Cached current state. Used to determine the state on which to compute the
121 /// linearization on during the Newton method.
122 mutable Vector current_state;
123 mutable Vector cstate_local_;
124 mutable Vector cstate_el_;
125 /// Temporary vector for the perturbation of the solution with essential
126 /// boundaries eliminated. Defined as a T-vector.
127 mutable Vector dX_ess_;
128
129 /// Flag to enable caching of the gradient. If enabled, during linear
130 /// iterations the operator only applies the gradient on each quadrature
131 /// point rather than recompute the action.
132 bool use_cache_ = true;
133 mutable bool recompute_cache_ = false;
134 Vector dsigma_cache_;
135
136 /**
137 * @brief Wrapper for the application of the residual R(U).
138 *
139 * The wrapper is used in SetMaterial to instantiate the chosen kernel and
140 * erase the material type kernel. This is purely an interface design choice
141 * and could be replaced by an abstract base class for the material including
142 * virtual function calls.
143 */
144 std::function<void(const int, const Array<real_t> &, const Array<real_t> &,
145 const Array<real_t> &, const Vector &, const Vector &,
146 const Vector &, Vector &)>
147 element_apply_kernel_wrapper;
148
149 /**
150 * @brief Wrapper for the application of the gradient of the residual
151 *
152 * K(U) dX = dR(U)/dU dX
153 */
154 std::function<void(const int, const Array<real_t> &, const Array<real_t> &,
155 const Array<real_t> &, const Vector &, const Vector &,
156 const Vector &, Vector &, const Vector &)>
157 element_apply_gradient_kernel_wrapper;
158
159 /**
160 * @brief Wrapper for the assembly of the gradient on each diagonal element
161 *
162 * Ke_ii(U) = dRe_ii(U)/dU
163 */
164 std::function<void(const int, const Array<real_t> &, const Array<real_t> &,
165 const Array<real_t> &, const Vector &, const Vector &,
166 const Vector &, Vector &)>
167 element_kernel_assemble_diagonal_wrapper;
168
169 /**
170 * @brief Set the material type.
171 *
172 * This method sets the material type by instantiating the kernels with a
173 * material_type object.
174 *
175 * @tparam material_type
176 * @param[in] material
177 */
178 template <typename material_type>
179 void SetMaterial(const material_type &material)
180 {
181 if (dim_ != 3)
182 {
183 MFEM_ABORT("dim != 3 not implemented");
184 }
185
186 element_apply_kernel_wrapper =
187 [=](const int ne, const Array<real_t> &B_, const Array<real_t> &G_,
188 const Array<real_t> &W_, const Vector &Jacobian_,
189 const Vector &detJ_, const Vector &X_, Vector &Y_)
190 {
191 const int id = (d1d_ << 4) | q1d_;
192 switch (id)
193 {
194 case 0x22:
195 {
196 ElasticityKernels::Apply3D<2, 2, material_type>(
197 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
198 break;
199 }
200 case 0x33:
201 {
202 ElasticityKernels::Apply3D<3, 3, material_type>(
203 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
204 break;
205 }
206 case 0x44:
207 ElasticityKernels::Apply3D<4, 4, material_type>(
208 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
209 break;
210 default:
211 MFEM_ABORT("Not implemented: " << std::hex << id << std::dec);
212 }
213 };
214
215 element_apply_gradient_kernel_wrapper =
216 [=](const int ne, const Array<real_t> &B_, const Array<real_t> &G_,
217 const Array<real_t> &W_, const Vector &Jacobian_,
218 const Vector &detJ_, const Vector &dU_, Vector &dF_,
219 const Vector &U_)
220 {
221 const int id = (d1d_ << 4) | q1d_;
222 switch (id)
223 {
224 case 0x22:
225 ElasticityKernels::ApplyGradient3D<2, 2, material_type>(
226 ne, B_, G_, W_, Jacobian_, detJ_, dU_, dF_, U_, material,
227 use_cache_, recompute_cache_, dsigma_cache_);
228 break;
229 case 0x33:
230 ElasticityKernels::ApplyGradient3D<3, 3, material_type>(
231 ne, B_, G_, W_, Jacobian_, detJ_, dU_, dF_, U_, material,
232 use_cache_, recompute_cache_, dsigma_cache_);
233 break;
234 case 0x44:
235 ElasticityKernels::ApplyGradient3D<4, 4, material_type>(
236 ne, B_, G_, W_, Jacobian_, detJ_, dU_, dF_, U_, material,
237 use_cache_, recompute_cache_, dsigma_cache_);
238 break;
239 default:
240 MFEM_ABORT("Not implemented for D1D=" << d1d_ << " and Q1D=" << q1d_);
241 }
242 };
243
244 element_kernel_assemble_diagonal_wrapper =
245 [=](const int ne, const Array<real_t> &B_, const Array<real_t> &G_,
246 const Array<real_t> &W_, const Vector &Jacobian_,
247 const Vector &detJ_, const Vector &X_, Vector &Y_)
248 {
249 const int id = (d1d_ << 4) | q1d_;
250 switch (id)
251 {
252 case 0x22:
253 ElasticityKernels::AssembleGradientDiagonal3D<2, 2, material_type>(
254 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
255 break;
256 case 0x33:
257 ElasticityKernels::AssembleGradientDiagonal3D<3, 3, material_type>(
258 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
259 break;
260 case 0x44:
261 ElasticityKernels::AssembleGradientDiagonal3D<4, 4, material_type>(
262 ne, B_, G_, W_, Jacobian_, detJ_, X_, Y_, material);
263 break;
264 default:
265 MFEM_ABORT("Not implemented: " << std::hex << id << std::dec);
266 }
267 };
268 }
269
270 /**
271 * @brief Set the essential attributes which mark degrees of freedom for the
272 * solving process.
273 *
274 * Can be either a fixed boundary or a prescribed displacement.
275 *
276 * @param attr
277 */
278 void SetEssentialAttributes(const Array<int> attr)
279 {
280 h1_fes_.GetEssentialTrueDofs(attr, ess_tdof_list_);
281 }
282
283 /**
284 * @brief Set the attributes which mark the degrees of freedom that have a
285 * fixed displacement.
286 *
287 * @param[in] attr
288 */
289 void SetPrescribedDisplacement(const Array<int> attr)
290 {
291 h1_fes_.GetEssentialTrueDofs(attr, displaced_tdof_list_);
292 }
293
294 /**
295 * @brief Return the T-vector degrees of freedom that have been marked as
296 * displaced.
297 *
298 * @return T-vector degrees of freedom that have been marked as displaced
299 */
300 const Array<int> &GetPrescribedDisplacementTDofs() { return displaced_tdof_list_; };
301 };
302
303 } // namespace mfem
304
305 #endif