https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MFEMConstraintUtils.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_MFEM_ENABLED
11
12#include "MFEMConstraintUtils.h"
13#include "MooseError.h"
15
16namespace
17{
18
24template <typename CoefficientType>
25void
26accumulateSubdomainProjection(mfem::ParGridFunction & gf,
27 CoefficientType & coef,
28 const mfem::Array<int> & subdomain_markers,
29 mfem::Vector & values,
30 mfem::Array<int> & counter)
31{
32 mfem::FiniteElementSpace & fes = *gf.FESpace();
33 values.SetSize(fes.GetVSize());
34 values = 0.0;
35 counter.SetSize(fes.GetVSize());
36 counter = 0;
37
38 mfem::Array<int> ldofs;
39 mfem::Vector vals;
40 mfem::DofTransformation doftrans;
41 for (const auto e : make_range(fes.GetNE()))
42 {
43 if (!subdomain_markers[fes.GetAttribute(e) - 1])
44 continue;
45 fes.GetElementVDofs(e, ldofs, doftrans);
46 vals.SetSize(ldofs.Size());
47 // The Project overload resolved here handles vector H1 (one scalar basis per
48 // component) and H(curl)/H(div) (tangential/normal edge/face moments) alike;
49 // the DofTransformation fixes ND/RT dof orientation.
50 fes.GetFE(e)->Project(coef, *fes.GetElementTransformation(e), vals);
51 doftrans.TransformPrimal(vals);
52 // AddElementVector undoes the ND/RT sign encoding carried in ldofs.
53 values.AddElementVector(ldofs, vals);
54 for (const auto ldof : ldofs)
55 counter[mfem::FiniteElementSpace::DecodeDof(ldof)]++;
56 }
57}
58
64void
65distributeSubdomainProjection(mfem::ParGridFunction & gf,
66 mfem::Vector & values,
67 mfem::Array<int> & counter,
68 const mfem::Array<int> & ldof_marker)
69{
70 // Sum the contributions and their counts onto the master of each group of ranks
71 // sharing a dof. Only the master ends up with the totals; on the other ranks
72 // values and counter keep their local partial results. The master forms the
73 // average and the final Bcast copies it to the rest of the group. Without this, a
74 // shared dof owned by a rank that holds no element of the subdomain would keep its
75 // unprojected value, and that is the value ParBilinearForm::FormLinearSystem
76 // eliminates against. This mirrors the idiom used by
77 // mfem::ParGridFunction::ProjectBdrCoefficientTangent.
78 mfem::GroupCommunicator & gcomm = gf.ParFESpace()->GroupComm();
79 gcomm.Reduce<int>(counter.HostReadWrite(), mfem::GroupCommunicator::Sum);
80 gcomm.Reduce<mfem::real_t>(values.HostReadWrite(), mfem::GroupCommunicator::Sum);
81
82 // Ensure host data is up-to-date before writing to it.
83 gf.HostReadWrite();
84
85 // Unmarked dofs on the subdomain boundary still received a contribution, but they
86 // are not constrained and must keep their values, which may come from an essential
87 // boundary condition. On a non-master rank a marked shared dof can have a local
88 // count of zero, so counter is checked to avoid dividing by it; the Bcast below
89 // replaces whatever is written there anyway.
90 for (const auto i : make_range(gf.Size()))
91 if (counter[i] && ldof_marker[i])
92 gf(i) = values(i) / counter[i];
93
94 gcomm.Bcast<mfem::real_t>(gf.HostReadWrite());
95}
96
98template <typename CoefficientType>
99void
100projectOnSubdomains(mfem::ParGridFunction & gf,
101 CoefficientType & coef,
102 const mfem::Array<int> & subdomain_markers,
103 const mfem::Array<int> & ldof_marker)
104{
105 mooseAssert(gf.FESpace()->GetVectorDim() == coef.GetVDim(),
106 "The coefficient must have one component per space component");
107
108 mfem::Vector values;
109 mfem::Array<int> counter;
110 accumulateSubdomainProjection(gf, coef, subdomain_markers, values, counter);
111 distributeSubdomainProjection(gf, values, counter, ldof_marker);
112}
113}
114
115namespace Moose::MFEM
116{
117
118void
119subdomainTDofs(mfem::ParFiniteElementSpace & pfes,
120 const mfem::Array<int> & subdomain_markers,
121 const bool include_boundary,
122 mfem::Array<int> & ldof_marker,
123 mfem::Array<int> & tdofs)
124{
125 mfem::ParMesh & pmesh = *pfes.GetParMesh();
126
127 // Local dof markers: ldof_marker is 1 if the dof touches a local element carrying
128 // one of the requested attributes, outside_marker if it touches one that does not.
129 // outside_marker is needed only to exclude the subdomain boundary. All requested
130 // attributes are marked in a single pass so multi-subdomain restrictions are not
131 // clobbered.
132 ldof_marker.SetSize(pfes.GetVSize());
133 ldof_marker = 0;
134 mfem::Array<int> outside_marker;
135 if (!include_boundary)
136 outside_marker.SetSize(pfes.GetVSize(), 0);
137 mfem::Array<int> ldofs;
138 for (const auto e : make_range(pmesh.GetNE()))
139 {
140 const bool inside = subdomain_markers[pmesh.GetAttribute(e) - 1];
141 if (!inside && include_boundary)
142 continue;
143 pfes.GetElementVDofs(e, ldofs);
144 auto & marker = inside ? ldof_marker : outside_marker;
145 for (const auto d : ldofs)
146 marker[mfem::FiniteElementSpace::DecodeDof(d)] = 1; // undo ND/RT sign encoding
147 }
148
149 // Make the marker consistent across processors sharing a dof (boolean-OR).
150 pfes.Synchronize(ldof_marker);
151
152 if (!include_boundary)
153 {
154 // The subdomain boundary consists of the dofs the subdomains share with other
155 // elements, marked above, and those on the exterior boundary of the mesh.
156 // Periodic faces and nonconforming master faces are not exterior, and neither
157 // are faces shared between ranks. GetFaceInformation only tells those apart once
158 // ParMesh::ExchangeFaceNbrData has run, so they are skipped explicitly.
159 std::vector<bool> shared_face(pmesh.GetNumFaces(), false);
160 for (const auto i : make_range(pmesh.GetNSharedFaces()))
161 shared_face[pmesh.GetSharedFace(i)] = true;
162 for (const auto f : make_range(pmesh.GetNumFaces()))
163 if (!shared_face[f] && pmesh.GetFaceInformation(f).IsBoundary())
164 {
165 pfes.GetFaceVDofs(f, ldofs);
166 for (const auto d : ldofs)
167 outside_marker[mfem::FiniteElementSpace::DecodeDof(d)] = 1;
168 }
169 // Synchronizing makes an interface coinciding with a processor boundary visible
170 // on the rank holding only the subdomain side of it.
171 pfes.Synchronize(outside_marker);
172 for (const auto i : make_range(ldof_marker.Size()))
173 ldof_marker[i] = ldof_marker[i] && !outside_marker[i];
174 }
175
176 // Convert the local dof marker to a true dof list exactly as GetEssentialTrueDofs does.
177 mfem::Array<int> tdof_marker;
178 pfes.GetRestrictionMatrix()->BooleanMult(ldof_marker, tdof_marker);
179 mfem::FiniteElementSpace::MarkerToList(tdof_marker, tdofs);
180}
181
182void
183projectCoefficientOnSubdomains(mfem::ParGridFunction & gf,
184 mfem::Coefficient & coef,
185 const mfem::Array<int> & subdomain_markers,
186 const mfem::Array<int> & ldof_marker)
187{
188 projectOnSubdomains(gf, coef, subdomain_markers, ldof_marker);
189}
190
191void
192projectCoefficientOnSubdomains(mfem::ParGridFunction & gf,
193 mfem::VectorCoefficient & coef,
194 const mfem::Array<int> & subdomain_markers,
195 const mfem::Array<int> & ldof_marker)
196{
197 projectOnSubdomains(gf, coef, subdomain_markers, ldof_marker);
198}
199
200void
201zeroTrueDofs(mfem::ParGridFunction & gf, const mfem::Array<int> & tdofs)
202{
203 // Go through the true vector so the local vector is left consistent across the
204 // ranks sharing a dof.
205 gf.SetTrueVector();
206 gf.GetTrueVector().SetSubVector(tdofs, 0.);
207 gf.SetFromTrueVector();
208}
209}
210
211#endif
std::array< Real, 2 > values
Definition MortarUtils.C:52
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
void projectCoefficientOnSubdomains(mfem::ParGridFunction &gf, mfem::Coefficient &coef, const mfem::Array< int > &subdomain_markers, const mfem::Array< int > &ldof_marker)
Overwrite the degrees of freedom of grid function gf marked in the local dof marker ldof_marker with ...
void subdomainTDofs(mfem::ParFiniteElementSpace &pfes, const mfem::Array< int > &subdomain_markers, bool include_boundary, mfem::Array< int > &ldof_marker, mfem::Array< int > &tdofs)
Degrees of freedom of pfes belonging to elements whose mesh attribute is set in the attribute marker ...
void zeroTrueDofs(mfem::ParGridFunction &gf, const mfem::Array< int > &tdofs)
Strongly set the true dofs listed in tdofs of grid function gf to zero, leaving its local vector cons...
IntRange< T > make_range(T beg, T end)