Debian Codegraph @

fem/tmop/assemble/grad3.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 #pragma once
12
13 #include "../../tmop.hpp"
14 #include "../../kernels.hpp"
15 #include "../../../general/forall.hpp"
16 #include "../../../linalg/kernels.hpp"
17
18 namespace mfem
19 {
20
21 class TMOPAssembleGradPA3D
22 {
23 const TMOP_Integrator *ti; // not owned
24 const Vector &x;
25
26 public:
27 TMOPAssembleGradPA3D(const TMOP_Integrator *ti, const Vector &x): ti(ti),
28 x(x) {}
29
30 int Ndof() const { return ti->PA.maps->ndof; }
31 int Nqpt() const { return ti->PA.maps->nqpt; }
32
33 template <int MD1, int MQ1, typename METRIC, int T_D1D = 0, int T_Q1D = 0>
34 static void Mult(TMOPAssembleGradPA3D &ker)
35 {
36 const TMOP_Integrator *ti = ker.ti;
37 const real_t metric_normal = ti->metric_normal;
38 const int NE = ti->PA.ne, d1d = ker.Ndof(), q1d = ti->PA.maps->nqpt;
39 const int D1D = T_D1D ? T_D1D : d1d, Q1D = T_Q1D ? T_Q1D : q1d;
40 MFEM_VERIFY(D1D <= DeviceDofQuadLimits::Get().MAX_D1D, "");
41 MFEM_VERIFY(Q1D <= DeviceDofQuadLimits::Get().MAX_Q1D, "");
42
43 Array<real_t> mp;
44 if (auto m = dynamic_cast<TMOP_Combo_QualityMetric *>(ti->metric))
45 {
46 m->GetWeights(mp);
47 }
48 const real_t *w = mp.Read();
49
50 const auto *b = ti->PA.maps->B.Read(), *g = ti->PA.maps->G.Read();
51 const auto X = Reshape(ker.x.Read(), D1D, D1D, D1D, 3, NE);
52 const auto W = Reshape(ti->PA.ir->GetWeights().Read(), Q1D, Q1D, Q1D);
53 const auto J = Reshape(ti->PA.Jtr.Read(), 3, 3, Q1D, Q1D, Q1D, NE);
54 auto H = Reshape(ti->PA.H.Write(), 3, 3, 3, 3, Q1D, Q1D, Q1D, NE);
55
56 const Vector &mc = ti->PA.MC;
57 const bool const_m0 = mc.Size() == 1;
58 const auto MC = const_m0
59 ? Reshape(mc.Read(), 1, 1, 1, 1)
60 : Reshape(mc.Read(), Q1D, Q1D, Q1D, NE);
61
62 mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e)
63 {
64 MFEM_SHARED real_t smem[MQ1][MQ1];
65 MFEM_SHARED real_t sB[MD1][MQ1], sG[MD1][MQ1];
66 kernels::internal::vd_regs3d_t<3, 3, MQ1> r0, r1;
67
68 kernels::internal::LoadMatrix(D1D, Q1D, b, sB);
69 kernels::internal::LoadMatrix(D1D, Q1D, g, sG);
70
71 kernels::internal::LoadDofs3d(e, D1D, X, r0);
72 kernels::internal::Grad3d(D1D, Q1D, smem, sB, sG, r0, r1);
73
74 for (int qz = 0; qz < Q1D; ++qz)
75 {
76 MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D)
77 {
78 MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D)
79 {
80 const real_t *Jtr = &J(0, 0, qx, qy, qz, e);
81 const real_t detJtr = kernels::Det<3>(Jtr);
82 const real_t m_coef = const_m0 ?
83 MC(0, 0, 0, 0) :
84 MC(qx, qy, qz, e);
85 const real_t weight = metric_normal * m_coef * W(qx, qy, qz) * detJtr;
86
87 // Jrt = Jtr^{-1}
88 real_t Jrt[9];
89 kernels::CalcInverse<3>(Jtr, Jrt);
90
91 // Jpr = X^T.DSh
92 real_t Jpr[9] =
93 {
94 r1(0, 0, qz, qy, qx), r1(1, 0, qz, qy, qx), r1(2, 0, qz, qy, qx),
95 r1(0, 1, qz, qy, qx), r1(1, 1, qz, qy, qx), r1(2, 1, qz, qy, qx),
96 r1(0, 2, qz, qy, qx), r1(1, 2, qz, qy, qx), r1(2, 2, qz, qy, qx)
97 };
98
99 // Jpt = X^T . DS = (X^T.DSh) . Jrt = Jpr . Jrt
100 real_t Jpt[9];
101 kernels::Mult(3, 3, 3, Jpr, Jrt, Jpt);
102
103 METRIC{}.AssembleH(qx, qy, qz, e, weight, Jrt, Jpr, Jpt, w, H);
104 }
105 }
106 }
107 });
108 }
109 };
110
111 } // namespace mfem