| 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 "elasticity_operator.hpp" |
| 13 |
#include "elasticity_gradient_operator.hpp" |
| 14 |
|
| 15 |
namespace mfem |
| 16 |
{ |
| 17 |
|
| 18 |
ElasticityOperator::ElasticityOperator(ParMesh &mesh, const int order) |
| 19 |
: Operator(), mesh_(mesh), order_(order), dim_(mesh_.SpaceDimension()), |
| 20 |
vdim_(mesh_.SpaceDimension()), ne_(mesh_.GetNE()), h1_fec_(order_, dim_), |
| 21 |
h1_fes_(&mesh_, &h1_fec_, vdim_, Ordering::byNODES) |
| 22 |
{ |
| 23 |
this->height = h1_fes_.GetTrueVSize(); |
| 24 |
this->width = this->height; |
| 25 |
|
| 26 |
int global_tdof_size = h1_fes_.GlobalTrueVSize(); |
| 27 |
if (mesh.GetMyRank() == 0) |
| 28 |
{ |
| 29 |
out << "#dofs: " << global_tdof_size << std::endl; |
| 30 |
} |
| 31 |
|
| 32 |
h1_element_restriction_ = |
| 33 |
h1_fes_.GetElementRestriction(ElementDofOrdering::LEXICOGRAPHIC); |
| 34 |
h1_prolongation_ = h1_fes_.GetProlongationMatrix(); |
| 35 |
|
| 36 |
ir_ = const_cast<IntegrationRule *>( |
| 37 |
&IntRules.Get(mfem::Element::HEXAHEDRON, 2 * h1_fes_.GetOrder(0) + 1)); |
| 38 |
|
| 39 |
geometric_factors_ = h1_fes_.GetParMesh()->GetGeometricFactors( |
| 40 |
*ir_, GeometricFactors::JACOBIANS | GeometricFactors::DETERMINANTS); |
| 41 |
maps_ = &h1_fes_.GetTypicalFE()->GetDofToQuad(*ir_, DofToQuad::TENSOR); |
| 42 |
d1d_ = maps_->ndof; |
| 43 |
q1d_ = maps_->nqpt; |
| 44 |
|
| 45 |
dX_ess_.UseDevice(true); |
| 46 |
dX_ess_.SetSize(h1_fes_.GetTrueVSize()); |
| 47 |
|
| 48 |
X_el_.UseDevice(true); |
| 49 |
X_el_.SetSize(h1_element_restriction_->Height()); |
| 50 |
|
| 51 |
Y_el_.UseDevice(true); |
| 52 |
Y_el_.SetSize(h1_element_restriction_->Height()); |
| 53 |
|
| 54 |
cstate_el_.UseDevice(true); |
| 55 |
cstate_el_.SetSize(h1_element_restriction_->Height()); |
| 56 |
|
| 57 |
X_local_.UseDevice(true); |
| 58 |
X_local_.SetSize(h1_prolongation_->Height()); |
| 59 |
|
| 60 |
Y_local_.UseDevice(true); |
| 61 |
Y_local_.SetSize(h1_prolongation_->Height()); |
| 62 |
|
| 63 |
cstate_local_.UseDevice(true); |
| 64 |
cstate_local_.SetSize(h1_prolongation_->Height()); |
| 65 |
|
| 66 |
if (use_cache_) |
| 67 |
{ |
| 68 |
dsigma_cache_.SetSize(ne_ * q1d_ * q1d_ * q1d_ * dim_ * dim_ * dim_ * dim_); |
| 69 |
} |
| 70 |
|
| 71 |
gradient_ = new ElasticityGradientOperator(*this); |
| 72 |
} |
| 73 |
|
| 74 |
void ElasticityOperator::Mult(const Vector &X, Vector &Y) const |
| 75 |
{ |
| 76 |
ess_tdof_list_.Read(); |
| 77 |
|
| 78 |
// T-vector to L-vector |
| 79 |
h1_prolongation_->Mult(X, X_local_); |
| 80 |
// L-vector to E-vector |
| 81 |
h1_element_restriction_->Mult(X_local_, X_el_); |
| 82 |
|
| 83 |
// Reset output vector |
| 84 |
Y_el_ = 0.0; |
| 85 |
|
| 86 |
// Apply operator |
| 87 |
element_apply_kernel_wrapper(ne_, maps_->B, maps_->G, ir_->GetWeights(), |
| 88 |
geometric_factors_->J, geometric_factors_->detJ, |
| 89 |
X_el_, Y_el_); |
| 90 |
|
| 91 |
// E-vector to L-vector |
| 92 |
h1_element_restriction_->MultTranspose(Y_el_, Y_local_); |
| 93 |
// L-vector to T-vector |
| 94 |
h1_prolongation_->MultTranspose(Y_local_, Y); |
| 95 |
|
| 96 |
// Set the residual at Dirichlet dofs on the T-vector to zero |
| 97 |
Y.SetSubVector(ess_tdof_list_, 0.0); |
| 98 |
} |
| 99 |
|
| 100 |
Operator &ElasticityOperator::GetGradient(const Vector &x) const |
| 101 |
{ |
| 102 |
// invalidate cache |
| 103 |
recompute_cache_ = true; |
| 104 |
|
| 105 |
h1_prolongation_->Mult(x, cstate_local_); |
| 106 |
h1_element_restriction_->Mult(cstate_local_, cstate_el_); |
| 107 |
return *gradient_; |
| 108 |
} |
| 109 |
|
| 110 |
void ElasticityOperator::GradientMult(const Vector &dX, Vector &Y) const |
| 111 |
{ |
| 112 |
ess_tdof_list_.Read(); |
| 113 |
|
| 114 |
// Column elimination for essential dofs |
| 115 |
dX_ess_ = dX; |
| 116 |
dX_ess_.SetSubVector(ess_tdof_list_, 0.0); |
| 117 |
|
| 118 |
// T-vector to L-vector |
| 119 |
h1_prolongation_->Mult(dX_ess_, X_local_); |
| 120 |
// L-vector to E-vector |
| 121 |
h1_element_restriction_->Mult(X_local_, X_el_); |
| 122 |
|
| 123 |
// Reset output vector |
| 124 |
Y_el_ = 0.0; |
| 125 |
|
| 126 |
// Apply operator |
| 127 |
element_apply_gradient_kernel_wrapper( |
| 128 |
ne_, maps_->B, maps_->G, ir_->GetWeights(), geometric_factors_->J, |
| 129 |
geometric_factors_->detJ, X_el_, Y_el_, cstate_el_); |
| 130 |
|
| 131 |
// E-vector to L-vector |
| 132 |
h1_element_restriction_->MultTranspose(Y_el_, Y_local_); |
| 133 |
// L-vector to T-vector |
| 134 |
h1_prolongation_->MultTranspose(Y_local_, Y); |
| 135 |
|
| 136 |
// Re-assign the essential degrees of freedom on the final output vector. |
| 137 |
{ |
| 138 |
const auto d_dX = dX.Read(); |
| 139 |
auto d_Y = Y.ReadWrite(); |
| 140 |
const auto d_ess_tdof_list = ess_tdof_list_.Read(); |
| 141 |
mfem::forall(ess_tdof_list_.Size(), [=] MFEM_HOST_DEVICE (int i) |
| 142 |
{ |
| 143 |
d_Y[d_ess_tdof_list[i]] = d_dX[d_ess_tdof_list[i]]; |
| 144 |
}); |
| 145 |
} |
| 146 |
|
| 147 |
recompute_cache_ = false; |
| 148 |
} |
| 149 |
|
| 150 |
void ElasticityOperator::AssembleGradientDiagonal(Vector &Ke_diag, |
| 151 |
Vector &K_diag_local, |
| 152 |
Vector &K_diag) const |
| 153 |
{ |
| 154 |
Ke_diag.SetSize(d1d_ * d1d_ * d1d_ * dim_ * ne_ * dim_); |
| 155 |
K_diag_local.SetSize(h1_element_restriction_->Width() * dim_); |
| 156 |
K_diag.SetSize(h1_prolongation_->Width() * dim_); |
| 157 |
|
| 158 |
element_kernel_assemble_diagonal_wrapper( |
| 159 |
ne_, maps_->B, maps_->G, ir_->GetWeights(), geometric_factors_->J, |
| 160 |
geometric_factors_->detJ, cstate_el_, Ke_diag); |
| 161 |
|
| 162 |
// For each dimension, the H1 element restriction and H1 prolongation |
| 163 |
// transpose actions are applied separately. |
| 164 |
for (int i = 0; i < dim_; i++) |
| 165 |
{ |
| 166 |
// Scalar component E-size |
| 167 |
int sce_sz = d1d_ * d1d_ * d1d_ * dim_ * ne_; |
| 168 |
// Scalar component L-size |
| 169 |
int scl_sz = h1_element_restriction_->Width(); |
| 170 |
|
| 171 |
Vector vin_local, vout_local; |
| 172 |
vin_local.MakeRef(Ke_diag, i * sce_sz, sce_sz); |
| 173 |
vout_local.MakeRef(K_diag_local, i * scl_sz, scl_sz); |
| 174 |
h1_element_restriction_->MultTranspose(vin_local, vout_local); |
| 175 |
vout_local.GetMemory().SyncAlias(K_diag_local.GetMemory(), |
| 176 |
vout_local.Size()); |
| 177 |
|
| 178 |
// Scalar component T-size |
| 179 |
int sct_sz = h1_prolongation_->Width(); |
| 180 |
Vector vout; |
| 181 |
vout.MakeRef(K_diag, i * sct_sz, sct_sz); |
| 182 |
h1_prolongation_->MultTranspose(vout_local, vout); |
| 183 |
vout.GetMemory().SyncAlias(K_diag.GetMemory(), vout.Size()); |
| 184 |
} |
| 185 |
|
| 186 |
// Each essential dof row and column are set to zero with it's diagonal entry |
| 187 |
// set to 1, i.e. (Ke)_ii = 1.0. |
| 188 |
ess_tdof_list_.HostRead(); |
| 189 |
int num_submats = h1_fes_.GetTrueVSize() / h1_fes_.GetVDim(); |
| 190 |
auto K_diag_submats = Reshape(K_diag.HostWrite(), num_submats, dim_, dim_); |
| 191 |
for (int i = 0; i < ess_tdof_list_.Size(); i++) |
| 192 |
{ |
| 193 |
int ess_idx = ess_tdof_list_[i]; |
| 194 |
int submat = ess_idx % num_submats; |
| 195 |
int row = ess_idx / num_submats; |
| 196 |
for (int j = 0; j < dim_; j++) |
| 197 |
{ |
| 198 |
if (row == j) |
| 199 |
{ |
| 200 |
K_diag_submats(submat, row, j) = 1.0; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
K_diag_submats(submat, row, j) = 0.0; |
| 205 |
K_diag_submats(submat, j, row) = 0.0; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
ElasticityOperator::~ElasticityOperator() { delete gradient_; } |
| 212 |
|
| 213 |
} // namespace mfem |