MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
qspace.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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 "qspace.hpp"
13#include "qfunction.hpp"
14#include "../general/forall.hpp"
15
16namespace mfem
17{
18
20 const IntegrationRule &ir)
21 : mesh(mesh_), order(ir.GetOrder())
22{
23 for (int g = 0; g < Geometry::NumGeom; g++)
24 {
25 int_rule[g] = NULL;
26 }
27 int_rule[geom] = &ir;
28}
29
31{
33 mesh.GetGeometries(dim, geoms);
34 for (Geometry::Type geom : geoms)
35 {
36 int_rule[geom] = &IntRules.Get(geom, order);
37 }
38}
39
40namespace
41{
42
43void ScaleByQuadratureWeights(Vector &weights, const IntegrationRule &ir)
44{
45 const int N = weights.Size();
46 const int n = ir.Size();
47 real_t *d_weights = weights.ReadWrite();
48 const real_t *d_w = ir.GetWeights().Read();
49
50 mfem::forall(N, [=] MFEM_HOST_DEVICE (int i)
51 {
52 d_weights[i] *= d_w[i%n];
53 });
54}
55
56} // anonymous namespace
57
59{
60 // First get the Jacobian determinants (without the quadrature weight
61 // contributions). We also store the pointer to the Vector object, so that
62 // we know when the cached weights are invalidated.
65
66 // Then scale by the quadrature weights.
67 const IntegrationRule &ir = GetIntRule(0);
68 ScaleByQuadratureWeights(weights, ir);
69}
70
72{
73 if (GetNE() == 0) { return weights; }
75 {
77 }
78 return weights;
79}
80
82{
83 QuadratureFunction qf(const_cast<QuadratureSpaceBase*>(this));
84 coeff.Project(qf);
85 return qf.Integrate();
86}
87
89 Vector &integrals) const
90{
91 const int vdim = coeff.GetVDim();
92 QuadratureFunction qf(const_cast<QuadratureSpaceBase*>(this), vdim);
93 coeff.Project(qf);
94 qf.Integrate(integrals);
95}
96
98{
99 const int num_elem = mesh.GetNE();
100 offsets.SetSize(num_elem + 1);
101 int offset = 0;
102 for (int i = 0; i < num_elem; i++)
103 {
104 offsets[i] = offset;
105 int geom = mesh.GetElementBaseGeometry(i);
106 MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule.");
107 offset += int_rule[geom]->GetNPoints();
108 }
109 offsets[num_elem] = size = offset;
110}
111
117
118QuadratureSpace::QuadratureSpace(Mesh *mesh_, std::istream &in)
119 : QuadratureSpaceBase(*mesh_)
120{
121 const char *msg = "invalid input stream";
122 std::string ident;
123
124 in >> ident; MFEM_VERIFY(ident == "QuadratureSpace", msg);
125 in >> ident; MFEM_VERIFY(ident == "Type:", msg);
126 in >> ident;
127 if (ident == "default_quadrature")
128 {
129 in >> ident; MFEM_VERIFY(ident == "Order:", msg);
130 in >> order;
131 }
132 else
133 {
134 MFEM_ABORT("unknown QuadratureSpace type: " << ident);
135 return;
136 }
137
138 Construct();
139}
140
142 : QuadratureSpaceBase(mesh_, mesh_.GetElementGeometry(0), ir)
143{
144 MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension()) == 1,
145 "Constructor not valid for mixed meshes");
147}
148
149void QuadratureSpace::Save(std::ostream &os) const
150{
151 os << "QuadratureSpace\n"
152 << "Type: default_quadrature\n"
153 << "Order: " << order << '\n';
154}
155
157{
159 // TODO: assumes only one integration rule. This should be fixed once
160 // Mesh::GetGeometricFactors acceps a QuadratureSpace instead of
161 // IntegrationRule.
162 const IntegrationRule &ir = GetIntRule(0);
163 auto *geom = mesh.GetGeometricFactors(ir, flags);
164 return geom->detJ;
165}
166
168 FaceType face_type_)
169 : QuadratureSpaceBase(mesh_, order_),
170 face_type(face_type_),
171 num_faces(mesh.GetNFbyType(face_type))
172{
173 Construct();
174}
175
177 FaceType face_type_)
178 : QuadratureSpaceBase(mesh_, mesh_.GetFaceGeometry(0), ir),
179 face_type(face_type_),
180 num_faces(mesh.GetNFbyType(face_type))
181{
182 MFEM_VERIFY(mesh.GetNumGeometries(mesh.Dimension() - 1) == 1,
183 "Constructor not valid for mixed meshes");
184 ConstructOffsets();
185}
186
187void FaceQuadratureSpace::ConstructOffsets()
188{
189 face_indices.SetSize(num_faces);
190 offsets.SetSize(num_faces + 1);
191 int offset = 0;
192 int f_idx = 0;
193 for (int i = 0; i < mesh.GetNumFacesWithGhost(); i++)
194 {
196 if (face.IsNonconformingCoarse() || !face.IsOfFaceType(face_type))
197 {
198 continue;
199 }
200 face_indices[f_idx] = i;
201 face_indices_inv[i] = f_idx;
202 offsets[f_idx] = offset;
204 MFEM_ASSERT(int_rule[geom] != NULL, "Missing integration rule");
205 offset += int_rule[geom]->GetNPoints();
206
207 f_idx++;
208 }
209 offsets[num_faces] = size = offset;
210}
211
212void FaceQuadratureSpace::Construct()
213{
215 ConstructOffsets();
216}
217
219{
220 const int f_idx = face_indices[idx];
222 {
223 const int dim = mesh.Dimension();
224 const IntegrationRule &ir = GetIntRule(idx);
225 const int q1d = (int)floor(pow(ir.GetNPoints(), 1.0/(dim-1)) + 0.5);
227 return ToLexOrdering(dim, face.element[0].local_face_id, q1d, iq);
228 }
229 else
230 {
231 return iq;
232 }
233}
234
236{
237 auto get_face_index = [this](const int idx)
238 {
239 const auto it = face_indices_inv.find(idx);
240 if (it == face_indices_inv.end()) { return -1; }
241 else { return it->second; }
242 };
243
244 switch (T.ElementType)
245 {
247 return get_face_index(T.ElementNo);
250 return get_face_index(mesh.GetBdrElementFaceIndex(T.ElementNo));
251 default:
252 MFEM_ABORT("Invalid element type.");
253 return -1;
254 }
255}
256
257void FaceQuadratureSpace::Save(std::ostream &os) const
258{
259 os << "FaceQuadratureSpace\n"
260 << "Type: default_quadrature\n"
261 << "Order: " << order << '\n';
262}
263
264const Vector &FaceQuadratureSpace::GetGeometricFactorWeights() const
265{
267 // TODO: assumes only one integration rule. This should be fixed once
268 // Mesh::GetFaceGeometricFactors acceps a QuadratureSpace instead of
269 // IntegrationRule.
270 const IntegrationRule &ir = GetIntRule(0);
271 auto *geom = mesh.GetFaceGeometricFactors(ir, flags, face_type);
272 return geom->detJ;
273}
274
275} // namespace mfem
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
Definition array.hpp:697
int Size() const
Return the logical size of the array.
Definition array.hpp:144
const T * Read(bool on_dev=true) const
Shortcut for mfem::Read(a.GetMemory(), a.Size(), on_dev).
Definition array.hpp:317
Base class Coefficients that optionally depend on space and time. These are used by the BilinearFormI...
virtual void Project(QuadratureFunction &qf)
Fill the QuadratureFunction qf by evaluating the coefficient at the quadrature points.
Vector detJ
Determinants of the Jacobians at all quadrature points.
Definition mesh.hpp:2883
int GetEntityIndex(const ElementTransformation &T) const override
Returns the index associated with the face described by T.
Definition qspace.cpp:235
Geometry::Type GetGeometry(int idx) const override
Returns the geometry type of face idx.
Definition qspace.hpp:204
void Save(std::ostream &out) const override
Write the FaceQuadratureSpace to the stream out.
Definition qspace.cpp:257
int GetPermutedIndex(int idx, int iq) const override
Returns the permuted index of the iq quadrature point in entity idx.
Definition qspace.cpp:218
FaceQuadratureSpace(Mesh &mesh_, int order_, FaceType face_type_)
Create a FaceQuadratureSpace based on the global rules from IntRules.
Definition qspace.cpp:167
Vector detJ
Determinants of the Jacobians at all quadrature points.
Definition mesh.hpp:2835
static const int NumGeom
Definition geom.hpp:42
static bool IsTensorProduct(Type geom)
Definition geom.hpp:108
Class for an integration rule - an Array of IntegrationPoint.
Definition intrules.hpp:100
int GetNPoints() const
Returns the number of the points in the integration rule.
Definition intrules.hpp:256
const Array< real_t > & GetWeights() const
Return the quadrature weights in a contiguous array.
Definition intrules.cpp:86
const IntegrationRule & Get(int GeomType, int Order)
Returns an integration rule for given GeomType and Order.
Mesh data type.
Definition mesh.hpp:56
void GetGeometries(int dim, Array< Geometry::Type > &el_geoms) const
Return all element geometries of the given dimension present in the mesh.
Definition mesh.cpp:6972
Geometry::Type GetFaceGeometry(int i) const
Return the Geometry::Type associated with face i.
Definition mesh.cpp:1452
int GetBdrElementFaceIndex(int be_idx) const
Return the local face (codimension-1) index for the given boundary element index.
Definition mesh.hpp:1518
const FaceGeometricFactors * GetFaceGeometricFactors(const IntegrationRule &ir, const int flags, FaceType type, MemoryType d_mt=MemoryType::DEFAULT)
Return the mesh geometric factors for the faces corresponding to the given integration rule.
Definition mesh.cpp:876
int GetNE() const
Returns number of elements.
Definition mesh.hpp:1226
int Dimension() const
Dimension of the reference space used within the elements.
Definition mesh.hpp:1160
FaceInformation GetFaceInformation(int f) const
Definition mesh.cpp:1169
int GetNumFacesWithGhost() const
Return the number of faces (3D), edges (2D) or vertices (1D) including ghost faces.
Definition mesh.cpp:6261
const GeometricFactors * GetGeometricFactors(const IntegrationRule &ir, const int flags, MemoryType d_mt=MemoryType::DEFAULT)
Return the mesh geometric factors corresponding to the given integration rule.
Definition mesh.cpp:856
int GetNumGeometries(int dim) const
Return the number of geometries of the given dimension present in the mesh.
Definition mesh.cpp:6961
Geometry::Type GetElementBaseGeometry(int i) const
Definition mesh.hpp:1385
long GetNodesSequence() const
Return the nodes update counter.
Definition mesh.hpp:2249
Represents values or vectors of values at quadrature points on a mesh.
Definition qfunction.hpp:24
real_t Integrate() const
Return the integral of the quadrature function (vdim = 1 only).
Abstract base class for QuadratureSpace and FaceQuadratureSpace.
Definition qspace.hpp:26
Vector weights
Integration weights.
Definition qspace.hpp:33
const Vector & GetWeights() const
Return the integration weights (including geometric factors).
Definition qspace.cpp:71
void ConstructWeights() const
Compute the integration weights.
Definition qspace.cpp:58
int GetNE() const
Return the number of entities.
Definition qspace.hpp:69
void ConstructIntRules(int dim)
Fill the int_rule array for each geometry type using order.
Definition qspace.cpp:30
const IntegrationRule * int_rule[Geometry::NumGeom]
The quadrature rules used for each geometry type.
Definition qspace.hpp:42
QuadratureSpaceBase(Mesh &mesh_, int order_=0)
Protected constructor. Used by derived classes.
Definition qspace.hpp:45
long nodes_sequence
Nodes counter for cache invalidation.
Definition qspace.hpp:34
int order
The order of integration rule.
Definition qspace.hpp:31
const IntegrationRule & GetIntRule(int idx) const
Return the IntegrationRule associated with entity idx.
Definition qspace.hpp:81
virtual const Vector & GetGeometricFactorWeights() const =0
Compute the det(J) (volume or faces, depending on the type).
Mesh & mesh
The underlying mesh.
Definition qspace.hpp:30
Array< int > offsets
Entity quadrature point offset array, of size num_entities + 1.
Definition qspace.hpp:40
int size
Total number of quadrature points.
Definition qspace.hpp:32
real_t Integrate(Coefficient &coeff) const
Return the integral of the scalar Coefficient coeff.
Definition qspace.cpp:81
const Vector & GetGeometricFactorWeights() const override
Compute the det(J) (volume or faces, depending on the type).
Definition qspace.cpp:156
void Save(std::ostream &out) const override
Write the QuadratureSpace to the stream out.
Definition qspace.cpp:149
QuadratureSpace(Mesh *mesh_, int order_)
Create a QuadratureSpace based on the global rules from IntRules.
Definition qspace.hpp:127
Base class for vector Coefficients that optionally depend on time and space.
int GetVDim()
Returns dimension of the vector.
virtual void Project(QuadratureFunction &qf)
Fill the QuadratureFunction qf by evaluating the coefficient at the quadrature points.
Vector data type.
Definition vector.hpp:80
virtual real_t * ReadWrite(bool on_dev=true)
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition vector.hpp:490
int Size() const
Returns the size of the vector.
Definition vector.hpp:218
int dim
Definition ex24.cpp:53
int ToLexOrdering(const int dim, const int face_id, const int size1d, const int index)
Convert a dof face index from Native ordering to lexicographic ordering for quads and hexes.
float real_t
Definition config.hpp:43
void forall(int N, lambda &&body)
Definition forall.hpp:754
FaceType
Definition mesh.hpp:47
IntegrationRules IntRules(0, Quadrature1D::GaussLegendre)
A global object with all integration rules (defined in intrules.cpp)
Definition intrules.hpp:486
This structure is used as a human readable output format that deciphers the information contained in ...
Definition mesh.hpp:1894
bool IsNonconformingCoarse() const
Return true if the face is a nonconforming coarse face.
Definition mesh.hpp:1972
bool IsOfFaceType(FaceType type) const
Return true if the face is of the same type as type.
Definition mesh.hpp:1941
struct mfem::Mesh::FaceInformation::@13 element[2]