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