Debian Codegraph @

fem/tmop/tools/energy3.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 #include "../../tmop.hpp"
13 #include "../../kernels.hpp"
14 #include "../../../general/forall.hpp"
15 #include "../../../linalg/kernels.hpp"
16
17 namespace mfem
18 {
19
20 class TMOPEnergyPA3D
21 {
22 const Vector &X;
23 Vector &E, &L;
24 const Vector &O;
25 const bool use_detA;
26
27 real_t metric_energy, limiting_energy;
28
29 int ndof, nqpt;
30 real_t metric_normal;
31 int ne;
32 TMOP_QualityMetric *metric;
33 const Array<real_t> &B, &G;
34 const DenseTensor &J;
35 const IntegrationRule &ir;
36 const Vector &metric_coeff;
37
38 public:
39 TMOPEnergyPA3D(const TMOP_Integrator *ti,
40 const Vector &x,
41 Vector &l,
42 const bool detA):
43 X(x),
44 E(ti->PA.E),
45 L(l),
46 O(ti->PA.O),
47 use_detA(detA),
48 metric_energy{},
49 limiting_energy{},
50 ndof(ti->PA.maps->ndof),
51 nqpt(ti->PA.maps->nqpt),
52 metric_normal(ti->metric_normal),
53 ne(ti->PA.ne),
54 metric(ti->metric),
55 B(ti->PA.maps->B),
56 G(ti->PA.maps->G),
57 J(ti->PA.Jtr),
58 ir(*ti->PA.ir),
59 metric_coeff(ti->PA.MC)
60 { }
61
62 TMOPEnergyPA3D(const TMOP_Integrator *ti,
63 const Vector &x,
64 Vector &l,
65 const real_t normal,
66 const Vector &coeff,
67 const bool detA):
68 X(x),
69 E(ti->PA.E),
70 L(l),
71 O(ti->PA.O),
72 use_detA(detA),
73 metric_energy{},
74 limiting_energy{},
75 ndof(ti->PA.maps->ndof),
76 nqpt(ti->PA.maps->nqpt),
77 metric_normal(normal),
78 ne(ti->PA.ne),
79 metric(ti->metric),
80 B(ti->PA.maps->B),
81 G(ti->PA.maps->G),
82 J(ti->PA.Jtr),
83 ir(*ti->PA.ir),
84 metric_coeff(coeff)
85 { }
86
87 TMOPEnergyPA3D(const Vector &x,
88 Vector &e,
89 Vector &l,
90 const Vector &o,
91 const bool detA,
92 const int d1d,
93 const int q1d,
94 const real_t normal,
95 const int NE,
96 TMOP_QualityMetric *quality_metric,
97 const Array<real_t> &b,
98 const Array<real_t> &g,
99 const DenseTensor &J,
100 const IntegrationRule &integration_rule,
101 const Vector &mc):
102 X(x),
103 E(e),
104 L(l),
105 O(o),
106 use_detA(detA),
107 metric_energy{},
108 limiting_energy{},
109 ndof(d1d),
110 nqpt(q1d),
111 metric_normal(normal),
112 ne(NE),
113 metric(quality_metric),
114 B(b),
115 G(g),
116 J(J),
117 ir(integration_rule),
118 metric_coeff(mc)
119 { }
120
121 int Ndof() const { return ndof; }
122 int Nqpt() const { return nqpt; }
123
124 template <int MD1, int MQ1, typename METRIC, int T_D1D = 0, int T_Q1D = 0>
125 static void Mult(TMOPEnergyPA3D &ker)
126 {
127 const real_t metric_normal = ker.metric_normal;
128 const int NE = ker.ne, d1d = ker.ndof, q1d = ker.nqpt;
129 const int D1D = T_D1D ? T_D1D : d1d, Q1D = T_Q1D ? T_Q1D : q1d;
130
131 MFEM_VERIFY(T_D1D > 0 || D1D <= DeviceDofQuadLimits::Get().MAX_D1D, "");
132 MFEM_VERIFY(T_Q1D > 0 || Q1D <= DeviceDofQuadLimits::Get().MAX_D1D, "");
133
134 Array<real_t> mp;
135 if (auto metric = dynamic_cast<TMOP_Combo_QualityMetric *>(ker.metric))
136 {
137 metric->GetWeights(mp);
138 }
139
140 const auto *w = mp.Read();
141 const auto *b = ker.B.Read();
142 const auto *g = ker.G.Read();
143
144 const auto X = Reshape(ker.X.Read(), D1D, D1D, D1D, 3, NE);
145 const auto J = Reshape(ker.J.Read(), 3, 3, Q1D, Q1D, Q1D, NE);
146 const auto W = Reshape(ker.ir.GetWeights().Read(), Q1D, Q1D, Q1D);
147
148 const Vector &mc = ker.metric_coeff;
149 const bool const_m0 = mc.Size() == 1;
150 const auto MC = const_m0
151 ? Reshape(mc.Read(), 1, 1, 1, 1)
152 : Reshape(mc.Read(), Q1D, Q1D, Q1D, NE);
153
154 const bool use_detA = ker.use_detA;
155 auto E = Reshape(ker.E.Write(), Q1D, Q1D, Q1D, NE);
156 auto L = Reshape(ker.L.Write(), Q1D, Q1D, Q1D, NE);
157
158 mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e)
159 {
160 MFEM_SHARED real_t smem[MQ1][MQ1];
161 MFEM_SHARED real_t sB[MD1][MQ1], sG[MD1][MQ1];
162 kernels::internal::vd_regs3d_t<3, 3, MQ1> r0, r1;
163
164 kernels::internal::LoadMatrix(D1D, Q1D, b, sB);
165 kernels::internal::LoadMatrix(D1D, Q1D, g, sG);
166
167 kernels::internal::LoadDofs3d(e, D1D, X, r0);
168 kernels::internal::Grad3d(D1D, Q1D, smem, sB, sG, r0, r1);
169
170 for (int qz = 0; qz < Q1D; ++qz)
171 {
172 MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D)
173 {
174 MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D)
175 {
176 const real_t *Jtr = &J(0, 0, qx, qy, qz, e);
177 const real_t coeff = const_m0 ? MC(0, 0, 0, 0) : MC(qx, qy, qz, e);
178
179 // Jrt = Jtr^{-1}
180 real_t Jrt[9];
181 kernels::CalcInverse<3>(Jtr, Jrt);
182
183 // Jpr = X^t.DSh
184 const real_t Jpr[9] =
185 {
186 r1(0, 0, qz, qy, qx), r1(1, 0, qz, qy, qx), r1(2, 0, qz, qy, qx),
187 r1(0, 1, qz, qy, qx), r1(1, 1, qz, qy, qx), r1(2, 1, qz, qy, qx),
188 r1(0, 2, qz, qy, qx), r1(1, 2, qz, qy, qx), r1(2, 2, qz, qy, qx)
189 };
190
191 // Jpt = X^t.DS = (X^t.DSh).Jrt = Jpr.Jrt
192 real_t Jpt[9];
193 kernels::Mult(3, 3, 3, Jpr, Jrt, Jpt);
194
195 const real_t det = kernels::Det<3>(use_detA ? Jpr : Jtr);
196 const real_t weight = metric_normal * coeff * W(qx, qy, qz) * det;
197
198 const real_t EvalW = METRIC{}.EvalW(Jpt, w);
199
200 E(qx, qy, qz, e) = weight * EvalW;
201 L(qx, qy, qz, e) = weight;
202 }
203 }
204 }
205 });
206
207 ker.metric_energy = ker.E * ker.O;
208 MFEM_VERIFY(std::isfinite(ker.metric_energy), "Metric energy error");
209
210 ker.limiting_energy = ker.L * ker.O;
211 MFEM_VERIFY(std::isfinite(ker.limiting_energy), "Limiting energy error");
212 }
213
214 void GetEnergy(real_t &met_energy, real_t &lim_energy) const
215 {
216 met_energy = this->metric_energy;
217 lim_energy = this->limiting_energy;
218 }
219 };
220
221 } // namespace mfem