MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
qfunction.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, 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 #ifndef MFEM_QFUNCTION
13 #define MFEM_QFUNCTION
14 
15 #include "../config/config.hpp"
16 #include "qspace.hpp"
17 #include "gridfunc.hpp"
18 
19 namespace mfem
20 {
21 
22 /// Represents values or vectors of values at quadrature points on a mesh.
23 class QuadratureFunction : public Vector
24 {
25 protected:
26  QuadratureSpaceBase *qspace; ///< Associated QuadratureSpaceBase object.
27  bool own_qspace; ///< Does this own the associated QuadratureSpaceBase?
28  int vdim; ///< Vector dimension.
29 
30 public:
31  /// Default constructor, results in an empty vector.
32  QuadratureFunction() : qspace(nullptr), own_qspace(false), vdim(0) { }
33 
34  /// Create a QuadratureFunction based on the given QuadratureSpaceBase.
35  /** The QuadratureFunction does not assume ownership of the
36  QuadratureSpaceBase.
37  @note The Vector data is not initialized. */
38  QuadratureFunction(QuadratureSpaceBase &qspace_, int vdim_ = 1)
39  : Vector(vdim_*qspace_.GetSize()),
40  qspace(&qspace_), own_qspace(false), vdim(vdim_)
41  { }
42 
43  /// Create a QuadratureFunction based on the given QuadratureSpaceBase.
44  /** The QuadratureFunction does not assume ownership of the
45  QuadratureSpaceBase.
46  @warning @a qspace_ may not be NULL. */
47  QuadratureFunction(QuadratureSpaceBase *qspace_, int vdim_ = 1)
48  : QuadratureFunction(*qspace_, vdim_) { }
49 
50  /** @brief Copy constructor. The QuadratureSpace ownership flag, #own_qspace,
51  in the new object is set to false. */
53  : QuadratureFunction(*orig.qspace, orig.vdim)
54  {
55  Vector::operator=(orig);
56  }
57 
58  /// Read a QuadratureFunction from the stream @a in.
59  /** The QuadratureFunction assumes ownership of the read QuadratureSpace. */
60  QuadratureFunction(Mesh *mesh, std::istream &in);
61 
62  /// Get the vector dimension.
63  int GetVDim() const { return vdim; }
64 
65  /// Set the vector dimension, updating the size by calling Vector::SetSize().
66  void SetVDim(int vdim_)
67  { vdim = vdim_; SetSize(vdim*qspace->GetSize()); }
68 
69  /// Get the associated QuadratureSpaceBase object.
71 
72  /// Get the associated QuadratureSpaceBase object (const version).
73  const QuadratureSpaceBase *GetSpace() const { return qspace; }
74 
75  /// Change the QuadratureSpaceBase and optionally the vector dimension.
76  /** If the new QuadratureSpaceBase is different from the current one, the
77  QuadratureFunction will not assume ownership of the new space; otherwise,
78  the ownership flag remains the same.
79 
80  If the new vector dimension @a vdim_ < 0, the vector dimension remains
81  the same.
82 
83  The data size is updated by calling Vector::SetSize(). */
84  inline void SetSpace(QuadratureSpaceBase *qspace_, int vdim_ = -1);
85 
86  /** @brief Change the QuadratureSpaceBase, the data array, and optionally the
87  vector dimension. */
88  /** If the new QuadratureSpaceBase is different from the current one, the
89  QuadratureFunction will not assume ownership of the new space; otherwise,
90  the ownership flag remains the same.
91 
92  If the new vector dimension @a vdim_ < 0, the vector dimension remains
93  the same.
94 
95  The data array is replaced by calling Vector::NewDataAndSize(). */
96  inline void SetSpace(QuadratureSpaceBase *qspace_, double *qf_data,
97  int vdim_ = -1);
98 
99  /// Get the QuadratureSpaceBase ownership flag.
100  bool OwnsSpace() { return own_qspace; }
101 
102  /// Set the QuadratureSpaceBase ownership flag.
103  void SetOwnsSpace(bool own) { own_qspace = own; }
104 
105  /// Set this equal to a constant value.
106  QuadratureFunction &operator=(double value);
107 
108  /// Copy the data from @a v.
109  /** The size of @a v must be equal to the size of the associated
110  QuadratureSpaceBase #qspace times the QuadratureFunction vector
111  dimension i.e. QuadratureFunction::Size(). */
113 
114  /// Evaluate a grid function at each quadrature point.
115  void ProjectGridFunction(const GridFunction &gf);
116 
117  /// Return all values associated with mesh element @a idx in a Vector.
118  /** The result is stored in the Vector @a values as a reference to the
119  global values.
120 
121  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
122  `i`-th vector component at the `j`-th quadrature point.
123  */
124  inline void GetValues(int idx, Vector &values);
125 
126  /// Return all values associated with mesh element @a idx in a Vector.
127  /** The result is stored in the Vector @a values as a copy of the
128  global values.
129 
130  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
131  `i`-th vector component at the `j`-th quadrature point.
132  */
133  inline void GetValues(int idx, Vector &values) const;
134 
135  /// Return the quadrature function values at an integration point.
136  /** The result is stored in the Vector @a values as a reference to the
137  global values. */
138  inline void GetValues(int idx, const int ip_num, Vector &values);
139 
140  /// Return the quadrature function values at an integration point.
141  /** The result is stored in the Vector @a values as a copy to the
142  global values. */
143  inline void GetValues(int idx, const int ip_num, Vector &values) const;
144 
145  /// Return all values associated with mesh element @a idx in a DenseMatrix.
146  /** The result is stored in the DenseMatrix @a values as a reference to the
147  global values.
148 
149  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
150  `i`-th vector component at the `j`-th quadrature point.
151  */
152  inline void GetValues(int idx, DenseMatrix &values);
153 
154  /// Return all values associated with mesh element @a idx in a const DenseMatrix.
155  /** The result is stored in the DenseMatrix @a values as a copy of the
156  global values.
157 
158  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
159  `i`-th vector component at the `j`-th quadrature point.
160  */
161  inline void GetValues(int idx, DenseMatrix &values) const;
162 
163  /// Get the IntegrationRule associated with entity (element or face) @a idx.
164  const IntegrationRule &GetIntRule(int idx) const
165  { return GetSpace()->GetIntRule(idx); }
166 
167  /// Write the QuadratureFunction to the stream @a out.
168  void Save(std::ostream &out) const;
169 
170  /// @brief Write the QuadratureFunction to @a out in VTU (ParaView) format.
171  ///
172  /// The data will be uncompressed if @a compression_level is zero, or if the
173  /// format is VTKFormat::ASCII. Otherwise, zlib compression will be used for
174  /// binary data.
175  void SaveVTU(std::ostream &out, VTKFormat format=VTKFormat::ASCII,
176  int compression_level=0) const;
177 
178  /// @brief Save the QuadratureFunction to a VTU (ParaView) file.
179  ///
180  /// The extension ".vtu" will be appended to @a filename.
181  /// @sa SaveVTU(std::ostream &out, VTKFormat format=VTKFormat::ASCII,
182  /// int compression_level=0)
183  void SaveVTU(const std::string &filename, VTKFormat format=VTKFormat::ASCII,
184  int compression_level=0) const;
185 
187  {
188  if (own_qspace) { delete qspace; }
189  }
190 };
191 
192 // Inline methods
193 
195  int idx, Vector &values)
196 {
197  const int s_offset = qspace->offsets[idx];
198  const int sl_size = qspace->offsets[idx+1] - s_offset;
199  values.MakeRef(*this, vdim*s_offset, vdim*sl_size);
200 }
201 
203  int idx, Vector &values) const
204 {
205  const int s_offset = qspace->offsets[idx];
206  const int sl_size = qspace->offsets[idx+1] - s_offset;
207  values.SetSize(vdim*sl_size);
208  values.HostWrite();
209  const double *q = HostRead() + vdim*s_offset;
210  for (int i = 0; i<values.Size(); i++)
211  {
212  values(i) = *(q++);
213  }
214 }
215 
217  int idx, const int ip_num, Vector &values)
218 {
219  const int s_offset = qspace->offsets[idx] * vdim + ip_num * vdim;
220  values.MakeRef(*this, s_offset, vdim);
221 }
222 
224  int idx, const int ip_num, Vector &values) const
225 {
226  const int s_offset = qspace->offsets[idx] * vdim + ip_num * vdim;
227  values.SetSize(vdim);
228  values.HostWrite();
229  const double *q = HostRead() + s_offset;
230  for (int i = 0; i < values.Size(); i++)
231  {
232  values(i) = *(q++);
233  }
234 }
235 
237  int idx, DenseMatrix &values)
238 {
239  const int s_offset = qspace->offsets[idx];
240  const int sl_size = qspace->offsets[idx+1] - s_offset;
241  // Make the values matrix memory an alias of the quadrature function memory
242  Memory<double> &values_mem = values.GetMemory();
243  values_mem.Delete();
244  values_mem.MakeAlias(GetMemory(), vdim*s_offset, vdim*sl_size);
245  values.SetSize(vdim, sl_size);
246 }
247 
249  int idx, DenseMatrix &values) const
250 {
251  const int s_offset = qspace->offsets[idx];
252  const int sl_size = qspace->offsets[idx+1] - s_offset;
253  values.SetSize(vdim, sl_size);
254  values.HostWrite();
255  const double *q = HostRead() + vdim*s_offset;
256  for (int j = 0; j<sl_size; j++)
257  {
258  for (int i = 0; i<vdim; i++)
259  {
260  values(i,j) = *(q++);
261  }
262  }
263 }
264 
265 
267  int vdim_)
268 {
269  if (qspace_ != qspace)
270  {
271  if (own_qspace) { delete qspace; }
272  qspace = qspace_;
273  own_qspace = false;
274  }
275  vdim = (vdim_ < 0) ? vdim : vdim_;
277 }
278 
280  QuadratureSpaceBase *qspace_, double *qf_data, int vdim_)
281 {
282  if (qspace_ != qspace)
283  {
284  if (own_qspace) { delete qspace; }
285  qspace = qspace_;
286  own_qspace = false;
287  }
288  vdim = (vdim_ < 0) ? vdim : vdim_;
289  NewDataAndSize(qf_data, vdim*qspace->GetSize());
290 }
291 
292 
293 } // namespace mfem
294 
295 #endif
int GetVDim() const
Get the vector dimension.
Definition: qfunction.hpp:63
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:90
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:163
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
void ProjectGridFunction(const GridFunction &gf)
Evaluate a grid function at each quadrature point.
Definition: qfunction.cpp:56
double * HostWrite()
Shortcut for mfem::Write(GetMemory(), TotalSize(), false).
Definition: densemat.hpp:455
Memory< double > & GetMemory()
Definition: densemat.hpp:117
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:513
void Delete()
Delete the owned pointers and reset the Memory object.
QuadratureSpaceBase * qspace
Associated QuadratureSpaceBase object.
Definition: qfunction.hpp:26
bool own_qspace
Does this own the associated QuadratureSpaceBase?
Definition: qfunction.hpp:27
const QuadratureSpaceBase * GetSpace() const
Get the associated QuadratureSpaceBase object (const version).
Definition: qfunction.hpp:73
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
virtual double * HostWrite()
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:461
int Size() const
Returns the size of the vector.
Definition: vector.hpp:200
int vdim
Vector dimension.
Definition: qfunction.hpp:28
Data arrays will be written in ASCII format.
bool OwnsSpace()
Get the QuadratureSpaceBase ownership flag.
Definition: qfunction.hpp:100
Memory< double > & GetMemory()
Return a reference to the Memory object used by the Vector.
Definition: vector.hpp:235
Vector & operator=(const double *v)
Copy Size() entries from v.
Definition: vector.cpp:124
const IntegrationRule & GetIntRule(int idx) const
Get the IntegrationRule associated with entity (element or face) idx.
Definition: qfunction.hpp:164
void SetOwnsSpace(bool own)
Set the QuadratureSpaceBase ownership flag.
Definition: qfunction.hpp:103
QuadratureFunction(QuadratureSpaceBase &qspace_, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpaceBase.
Definition: qfunction.hpp:38
QuadratureFunction()
Default constructor, results in an empty vector.
Definition: qfunction.hpp:32
VTKFormat
Data array format for VTK and VTU files.
Definition: vtk.hpp:96
void SetVDim(int vdim_)
Set the vector dimension, updating the size by calling Vector::SetSize().
Definition: qfunction.hpp:66
virtual const double * HostRead() const
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: vector.hpp:453
QuadratureFunction & operator=(double value)
Set this equal to a constant value.
Definition: qfunction.cpp:19
void SaveVTU(std::ostream &out, VTKFormat format=VTKFormat::ASCII, int compression_level=0) const
Write the QuadratureFunction to out in VTU (ParaView) format.
Definition: qfunction.cpp:116
Array< int > offsets
Entity quadrature point offset array, of size num_entities + 1.
Definition: qspace.hpp:37
Abstract base class for QuadratureSpace and FaceQuadratureSpace.
Definition: qspace.hpp:24
const IntegrationRule & GetIntRule(int idx) const
Return the IntegrationRule associated with entity idx.
Definition: qspace.hpp:72
void GetValues(int idx, Vector &values)
Return all values associated with mesh element idx in a Vector.
Definition: qfunction.hpp:194
void MakeAlias(const Memory &base, int offset, int size)
Create a memory object that points inside the memory object base.
QuadratureSpaceBase * GetSpace()
Get the associated QuadratureSpaceBase object.
Definition: qfunction.hpp:70
void SetSpace(QuadratureSpaceBase *qspace_, int vdim_=-1)
Change the QuadratureSpaceBase and optionally the vector dimension.
Definition: qfunction.hpp:266
QuadratureFunction(const QuadratureFunction &orig)
Copy constructor. The QuadratureSpace ownership flag, own_qspace, in the new object is set to false...
Definition: qfunction.hpp:52
Vector data type.
Definition: vector.hpp:60
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:577
QuadratureFunction(QuadratureSpaceBase *qspace_, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpaceBase.
Definition: qfunction.hpp:47
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
Definition: densemat.hpp:105
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
int GetSize() const
Return the total number of quadrature points.
Definition: qspace.hpp:54
Represents values or vectors of values at quadrature points on a mesh.
Definition: qfunction.hpp:23
void Save(std::ostream &out) const
Write the QuadratureFunction to the stream out.
Definition: qfunction.cpp:47