| 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 "../pa.hpp" |
| 13 |
#include "../../tmop.hpp" |
| 14 |
#include "../../kernels.hpp" |
| 15 |
#include "../../gridfunc.hpp" // IWYU pragma: keep |
| 16 |
#include "../../../general/forall.hpp" |
| 17 |
#include "../../../linalg/kernels.hpp" |
| 18 |
|
| 19 |
namespace mfem |
| 20 |
{ |
| 21 |
|
| 22 |
template <int T_Q1D = 0> |
| 23 |
void TMOP_TcIdealShapeUnitSize_2D(const int NE, |
| 24 |
const ConstDeviceMatrix &W, |
| 25 |
DeviceTensor<5> &J, |
| 26 |
const int q1d) |
| 27 |
{ |
| 28 |
const int Q1D = T_Q1D ? T_Q1D : q1d; |
| 29 |
|
| 30 |
mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e) |
| 31 |
{ |
| 32 |
constexpr int DIM = 2; |
| 33 |
const int Q1D = T_Q1D ? T_Q1D : q1d; |
| 34 |
MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D) |
| 35 |
{ |
| 36 |
MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D) |
| 37 |
{ |
| 38 |
kernels::Set(DIM, DIM, 1.0, &W(0, 0), &J(0, 0, qx, qy, e)); |
| 39 |
} |
| 40 |
} |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
template <int MD1, int MQ1, int T_D1D = 0, int T_Q1D = 0> |
| 45 |
void TMOP_TcIdealShapeGivenSize_2D(const int NE, |
| 46 |
const real_t detW, |
| 47 |
const real_t *b, |
| 48 |
const real_t *g, |
| 49 |
const ConstDeviceMatrix &W, |
| 50 |
const DeviceTensor<4, const real_t> &X, |
| 51 |
DeviceTensor<5> &J, |
| 52 |
const int d1d, |
| 53 |
const int q1d) |
| 54 |
{ |
| 55 |
const int D1D = T_D1D ? T_D1D : d1d; |
| 56 |
const int Q1D = T_Q1D ? T_Q1D : q1d; |
| 57 |
|
| 58 |
mfem::forall_2D(NE, Q1D, Q1D, [=] MFEM_HOST_DEVICE(int e) |
| 59 |
{ |
| 60 |
MFEM_SHARED real_t smem[MQ1][MQ1]; |
| 61 |
MFEM_SHARED real_t sB[MD1][MQ1], sG[MD1][MQ1]; |
| 62 |
kernels::internal::vd_regs2d_t<2, 2, MQ1> r0, r1; |
| 63 |
|
| 64 |
kernels::internal::LoadMatrix(D1D, Q1D, b, sB); |
| 65 |
kernels::internal::LoadMatrix(D1D, Q1D, g, sG); |
| 66 |
|
| 67 |
kernels::internal::LoadDofs2d(e, D1D, X, r0); |
| 68 |
kernels::internal::Grad2d(D1D, Q1D, smem, sB, sG, r0, r1); |
| 69 |
|
| 70 |
MFEM_FOREACH_THREAD_DIRECT(qy, y, Q1D) |
| 71 |
{ |
| 72 |
MFEM_FOREACH_THREAD_DIRECT(qx, x, Q1D) |
| 73 |
{ |
| 74 |
const real_t *Wid = &W(0, 0); |
| 75 |
const real_t Jtr[4] = |
| 76 |
{ |
| 77 |
r1[0][0][qy][qx], r1[1][0][qy][qx], |
| 78 |
r1[0][1][qy][qx], r1[1][1][qy][qx] |
| 79 |
}; |
| 80 |
const real_t detJ = kernels::Det<2>(Jtr); |
| 81 |
const real_t alpha = std::pow(detJ / detW, 1. / 2); |
| 82 |
kernels::Set(2, 2, alpha, Wid, &J(0, 0, qx, qy, e)); |
| 83 |
} |
| 84 |
} |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
MFEM_TMOP_REGISTER_KERNELS_1(TMOPTcIdealShapeUnitSize2D, |
| 89 |
TMOP_TcIdealShapeUnitSize_2D); |
| 90 |
MFEM_TMOP_ADD_SPECIALIZED_KERNELS_1(TMOPTcIdealShapeUnitSize2D); |
| 91 |
|
| 92 |
MFEM_TMOP_MDQ_REGISTER(TMOPTcIdealShapeGivenSize2D, |
| 93 |
TMOP_TcIdealShapeGivenSize_2D); |
| 94 |
MFEM_TMOP_MDQ_SPECIALIZE(TMOPTcIdealShapeGivenSize2D); |
| 95 |
|
| 96 |
template <> |
| 97 |
bool TargetConstructor::ComputeAllElementTargets<2>( |
| 98 |
const FiniteElementSpace &fes, |
| 99 |
const IntegrationRule &ir, |
| 100 |
const Vector &, |
| 101 |
DenseTensor &Jtr) const |
| 102 |
{ |
| 103 |
MFEM_ASSERT(target_type == IDEAL_SHAPE_UNIT_SIZE || nodes != nullptr, ""); |
| 104 |
const Mesh *mesh = fes.GetMesh(); |
| 105 |
const int NE = mesh->GetNE(); |
| 106 |
// Quick return for empty processors: |
| 107 |
if (NE == 0) { return true; } |
| 108 |
const int dim = mesh->Dimension(); |
| 109 |
MFEM_VERIFY(mesh->GetNumGeometries(dim) <= 1, |
| 110 |
"mixed meshes are not supported"); |
| 111 |
MFEM_VERIFY(!fes.IsVariableOrder(), "variable orders are not supported"); |
| 112 |
const FiniteElement &fe = *fes.GetFE(0); |
| 113 |
MFEM_VERIFY(fe.GetGeomType() == Geometry::SQUARE, ""); |
| 114 |
|
| 115 |
if (current_W_type != Geometry::SQUARE) |
| 116 |
{ |
| 117 |
current_W_type = Geometry::SQUARE; |
| 118 |
current_W = Geometries.GetGeomToPerfGeomJac(current_W_type); |
| 119 |
} |
| 120 |
current_W.HostRead(); // Needed for det |
| 121 |
const real_t detW = current_W.Det(); |
| 122 |
const DofToQuad::Mode mode = DofToQuad::TENSOR; |
| 123 |
const DofToQuad &maps = fe.GetDofToQuad(ir, mode); |
| 124 |
const int d = maps.ndof, q = maps.nqpt; |
| 125 |
|
| 126 |
MFEM_VERIFY(d <= DeviceDofQuadLimits::Get().MAX_D1D, ""); |
| 127 |
MFEM_VERIFY(q <= DeviceDofQuadLimits::Get().MAX_Q1D, ""); |
| 128 |
|
| 129 |
const auto W = Reshape(current_W.Read(), 2, 2); |
| 130 |
const auto *b = maps.B.Read(), *g = maps.G.Read(); |
| 131 |
auto J = Reshape(Jtr.Write(), 2, 2, q, q, NE); |
| 132 |
|
| 133 |
switch (target_type) |
| 134 |
{ |
| 135 |
case IDEAL_SHAPE_UNIT_SIZE: // Jtr(i) = Wideal; |
| 136 |
{ |
| 137 |
TMOPTcIdealShapeUnitSize2D::Run(q, NE, W, J, q); |
| 138 |
return true; |
| 139 |
} |
| 140 |
case IDEAL_SHAPE_EQUAL_SIZE: return false; |
| 141 |
case IDEAL_SHAPE_GIVEN_SIZE: |
| 142 |
{ |
| 143 |
MFEM_VERIFY(GetNodes(), "No target-matrix nodes!"); |
| 144 |
const ElementDofOrdering ordering = ElementDofOrdering::LEXICOGRAPHIC; |
| 145 |
const Operator *R = fes.GetElementRestriction(ordering); |
| 146 |
Vector x(R->Height(), Device::GetDeviceMemoryType()); |
| 147 |
x.UseDevice(true); |
| 148 |
R->Mult(*GetNodes(), x); |
| 149 |
MFEM_ASSERT(nodes->FESpace()->GetVDim() == 2, ""); |
| 150 |
const auto X = Reshape(x.Read(), d, d, 2, NE); |
| 151 |
|
| 152 |
TMOPTcIdealShapeGivenSize2D::Run(d, q, NE, detW, b, g, W, X, J, d, q); |
| 153 |
return true; |
| 154 |
} |
| 155 |
case GIVEN_SHAPE_AND_SIZE: return false; |
| 156 |
default: return false; |
| 157 |
} |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
} // namespace mfem |