| 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 TMOPEnergyPA2D |
| 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 |
TMOPEnergyPA2D(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 |
TMOPEnergyPA2D(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 |
TMOPEnergyPA2D(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(TMOPEnergyPA2D &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 m = dynamic_cast<TMOP_Combo_QualityMetric *>(ker.metric)) |
| 136 |
{ |
| 137 |
m->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, 2, NE); |
| 145 |
const auto J = Reshape(ker.J.Read(), 2, 2, Q1D, Q1D, NE); |
| 146 |
const auto W = Reshape(ker.ir.GetWeights().Read(), 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) |
| 152 |
: Reshape(mc.Read(), Q1D, Q1D, NE); |
| 153 |
|
| 154 |
const bool use_detA = ker.use_detA; |
| 155 |
auto E = Reshape(ker.E.Write(), Q1D, Q1D, NE); |
| 156 |
auto L = Reshape(ker.L.Write(), 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_regs2d_t<2, 2, MQ1> r0, r1; |
| 163 |
|
| 164 |
kernels::internal::LoadMatrix(D1D, Q1D, b, sB); |
| 165 |
kernels::internal::LoadMatrix(D1D, Q1D, g, sG); |
| 166 |
|
| 167 |
kernels::internal::LoadDofs2d(e, D1D, X, r0); |
| 168 |
kernels::internal::Grad2d(D1D, Q1D, smem, sB, sG, r0, r1); |
| 169 |
|
| 170 |
MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D) |
| 171 |
{ |
| 172 |
MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D) |
| 173 |
{ |
| 174 |
const real_t *Jtr = &J(0, 0, qx, qy, e); |
| 175 |
const real_t coeff = const_m0 ? MC(0, 0, 0) : MC(qx, qy, e); |
| 176 |
|
| 177 |
// Jrt = Jtr^{-1} |
| 178 |
real_t Jrt[4]; |
| 179 |
kernels::CalcInverse<2>(Jtr, Jrt); |
| 180 |
|
| 181 |
// Jpr = X^t.DSh |
| 182 |
const real_t Jpr[4] = |
| 183 |
{ |
| 184 |
r1[0][0][qy][qx], r1[1][0][qy][qx], |
| 185 |
r1[0][1][qy][qx], r1[1][1][qy][qx] |
| 186 |
}; |
| 187 |
|
| 188 |
// Jpt = X^T.DS = (X^T.DSh).Jrt = Jpr.Jrt |
| 189 |
real_t Jpt[4]; |
| 190 |
kernels::Mult(2, 2, 2, Jpr, Jrt, Jpt); |
| 191 |
|
| 192 |
const real_t det = kernels::Det<2>(use_detA ? Jpr : Jtr); |
| 193 |
const real_t weight = metric_normal * coeff * W(qx,qy) * det; |
| 194 |
|
| 195 |
const real_t EvalW = METRIC{}.EvalW(Jpt, w); |
| 196 |
|
| 197 |
E(qx, qy, e) = weight * EvalW; |
| 198 |
L(qx, qy, e) = weight; |
| 199 |
} |
| 200 |
} |
| 201 |
}); |
| 202 |
|
| 203 |
ker.metric_energy = ker.E * ker.O; |
| 204 |
MFEM_VERIFY(std::isfinite(ker.metric_energy), "Metric energy error"); |
| 205 |
|
| 206 |
ker.limiting_energy = ker.L * ker.O; |
| 207 |
MFEM_VERIFY(std::isfinite(ker.limiting_energy), "Limiting energy error"); |
| 208 |
} |
| 209 |
|
| 210 |
void GetEnergy(real_t &met_energy, real_t &lim_energy) const |
| 211 |
{ |
| 212 |
met_energy = this->metric_energy; |
| 213 |
lim_energy = this->limiting_energy; |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
} // namespace mfem |