MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
gridfunc.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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_GRIDFUNC
13 #define MFEM_GRIDFUNC
14 
15 #include "../config/config.hpp"
16 #include "fespace.hpp"
17 #include "coefficient.hpp"
18 #include "bilininteg.hpp"
19 #include <limits>
20 #include <ostream>
21 #include <string>
22 
23 namespace mfem
24 {
25 
26 /// Class for grid function - Vector with associated FE space.
27 class GridFunction : public Vector
28 {
29 protected:
30  /// FE space on which the grid function lives. Owned if #fec is not NULL.
32 
33  /** @brief Used when the grid function is read from a file. It can also be
34  set explicitly, see MakeOwner().
35 
36  If not NULL, this pointer is owned by the GridFunction. */
38 
39  long sequence; // see FiniteElementSpace::sequence, Mesh::sequence
40 
41  /** Optional, internal true-dof vector: if the FiniteElementSpace #fes has a
42  non-trivial (i.e. not NULL) prolongation operator, this Vector may hold
43  associated true-dof values - either owned or external. */
45 
46  void SaveSTLTri(std::ostream &out, double p1[], double p2[], double p3[]);
47 
49 
50  // Project the delta coefficient without scaling and return the (local)
51  // integral of the projection.
52  void ProjectDeltaCoefficient(DeltaCoefficient &delta_coeff,
53  double &integral);
54 
55  // Sum fluxes to vertices and count element contributions
57  GridFunction &flux,
58  Array<int>& counts,
59  bool wcoef,
60  int subdomain);
61 
62  /** Project a discontinuous vector coefficient in a continuous space and
63  return in dof_attr the maximal attribute of the elements containing each
64  degree of freedom. */
65  void ProjectDiscCoefficient(VectorCoefficient &coeff, Array<int> &dof_attr);
66 
67  void Destroy();
68 
69 public:
70 
71  GridFunction() { fes = NULL; fec = NULL; sequence = 0; UseDevice(true); }
72 
73  /// Copy constructor. The internal true-dof vector #t_vec is not copied.
75  : Vector(orig), fes(orig.fes), fec(NULL), sequence(orig.sequence)
76  { UseDevice(true); }
77 
78  /// Construct a GridFunction associated with the FiniteElementSpace @a *f.
79  GridFunction(FiniteElementSpace *f) : Vector(f->GetVSize())
80  { fes = f; fec = NULL; sequence = f->GetSequence(); UseDevice(true); }
81 
82  /// Construct a GridFunction using previously allocated array @a data.
83  /** The GridFunction does not assume ownership of @a data which is assumed to
84  be of size at least `f->GetVSize()`. Similar to the Vector constructor
85  for externally allocated array, the pointer @a data can be NULL. The data
86  array can be replaced later using the method SetData().
87  */
89  : Vector(data, f->GetVSize())
90  { fes = f; fec = NULL; sequence = f->GetSequence(); UseDevice(true); }
91 
92  /// Construct a GridFunction on the given Mesh, using the data from @a input.
93  /** The content of @a input should be in the format created by the method
94  Save(). The reconstructed FiniteElementSpace and FiniteElementCollection
95  are owned by the GridFunction. */
96  GridFunction(Mesh *m, std::istream &input);
97 
98  GridFunction(Mesh *m, GridFunction *gf_array[], int num_pieces);
99 
100  /// Copy assignment. Only the data of the base class Vector is copied.
101  /** It is assumed that this object and @a rhs use FiniteElementSpace%s that
102  have the same size.
103 
104  @note Defining this method overwrites the implicitly defined copy
105  assignemnt operator. */
107  { return operator=((const Vector &)rhs); }
108 
109  /// Make the GridFunction the owner of #fec and #fes.
110  /** If the new FiniteElementCollection, @a _fec, is NULL, ownership of #fec
111  and #fes is taken away. */
112  void MakeOwner(FiniteElementCollection *_fec) { fec = _fec; }
113 
115 
116  int VectorDim() const;
117 
118  /// Read only access to the (optional) internal true-dof Vector.
119  /** Note that the returned Vector may be empty, if not previously allocated
120  or set. */
121  const Vector &GetTrueVector() const { return t_vec; }
122  /// Read and write access to the (optional) internal true-dof Vector.
123  /** Note that the returned Vector may be empty, if not previously allocated
124  or set. */
125  Vector &GetTrueVector() { return t_vec; }
126 
127  /// @brief Extract the true-dofs from the GridFunction. If all dofs are true,
128  /// then `tv` will be set to point to the data of `*this`.
129  /** @warning This method breaks const-ness when all dofs are true. */
130  void GetTrueDofs(Vector &tv) const;
131 
132  /// Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
134 
135  /// Set the GridFunction from the given true-dof vector.
136  virtual void SetFromTrueDofs(const Vector &tv);
137 
138  /// Shortcut for calling SetFromTrueDofs() with GetTrueVector() as argument.
140 
141  /// Returns the values in the vertices of i'th element for dimension vdim.
142  void GetNodalValues(int i, Array<double> &nval, int vdim = 1) const;
143 
144  virtual double GetValue(int i, const IntegrationPoint &ip,
145  int vdim = 1) const;
146 
147  void GetVectorValue(int i, const IntegrationPoint &ip, Vector &val) const;
148 
149  void GetValues(int i, const IntegrationRule &ir, Vector &vals,
150  int vdim = 1) const;
151 
152  void GetValues(int i, const IntegrationRule &ir, Vector &vals,
153  DenseMatrix &tr, int vdim = 1) const;
154 
155  void GetLaplacians(int i, const IntegrationRule &ir, Vector &laps,
156  int vdim = 1) const;
157 
158  void GetLaplacians(int i, const IntegrationRule &ir, Vector &laps,
159  DenseMatrix &tr, int vdim = 1) const;
160 
161  void GetHessians(int i, const IntegrationRule &ir, DenseMatrix &hess,
162  int vdim = 1) const;
163 
164  void GetHessians(int i, const IntegrationRule &ir, DenseMatrix &hess,
165  DenseMatrix &tr, int vdim = 1) const;
166 
167  int GetFaceValues(int i, int side, const IntegrationRule &ir, Vector &vals,
168  DenseMatrix &tr, int vdim = 1) const;
169 
171  DenseMatrix &vals) const;
172 
173  void GetVectorValues(int i, const IntegrationRule &ir,
174  DenseMatrix &vals, DenseMatrix &tr) const;
175 
176  int GetFaceVectorValues(int i, int side, const IntegrationRule &ir,
177  DenseMatrix &vals, DenseMatrix &tr) const;
178 
179  void GetValuesFrom(const GridFunction &orig_func);
180 
181  void GetBdrValuesFrom(const GridFunction &orig_func);
182 
183  void GetVectorFieldValues(int i, const IntegrationRule &ir,
184  DenseMatrix &vals,
185  DenseMatrix &tr, int comp = 0) const;
186 
187  /// For a vector grid function, makes sure that the ordering is byNODES.
188  void ReorderByNodes();
189 
190  /// Return the values as a vector on mesh vertices for dimension vdim.
191  void GetNodalValues(Vector &nval, int vdim = 1) const;
192 
193  void GetVectorFieldNodalValues(Vector &val, int comp) const;
194 
195  void ProjectVectorFieldOn(GridFunction &vec_field, int comp = 0);
196 
197  void GetDerivative(int comp, int der_comp, GridFunction &der);
198 
199  double GetDivergence(ElementTransformation &tr) const;
200 
201  void GetCurl(ElementTransformation &tr, Vector &curl) const;
202 
203  void GetGradient(ElementTransformation &tr, Vector &grad) const;
204 
206  DenseMatrix &grad) const;
207 
208  void GetGradients(const int elem, const IntegrationRule &ir,
209  DenseMatrix &grad) const
210  { GetGradients(*fes->GetElementTransformation(elem), ir, grad); }
211 
212  void GetVectorGradient(ElementTransformation &tr, DenseMatrix &grad) const;
213 
214  /** Compute \f$ (\int_{\Omega} (*this) \psi_i)/(\int_{\Omega} \psi_i) \f$,
215  where \f$ \psi_i \f$ are the basis functions for the FE space of avgs.
216  Both FE spaces should be scalar and on the same mesh. */
217  void GetElementAverages(GridFunction &avgs) const;
218 
219  /** Impose the given bounds on the function's DOFs while preserving its local
220  * integral (described in terms of the given weights) on the i'th element
221  * through SLBPQ optimization.
222  * Intended to be used for discontinuous FE functions. */
223  void ImposeBounds(int i, const Vector &weights,
224  const Vector &_lo, const Vector &_hi);
225  void ImposeBounds(int i, const Vector &weights,
226  double _min = 0.0, double _max = infinity());
227 
228  /** @brief Project the @a src GridFunction to @a this GridFunction, both of
229  which must be on the same mesh. */
230  /** The current implementation assumes that all elements use the same
231  projection matrix. */
232  void ProjectGridFunction(const GridFunction &src);
233 
234  virtual void ProjectCoefficient(Coefficient &coeff);
235 
236  // call fes -> BuildDofToArrays() before using this projection
237  void ProjectCoefficient(Coefficient &coeff, Array<int> &dofs, int vd = 0);
238 
240 
241  // call fes -> BuildDofToArrays() before using this projection
242  void ProjectCoefficient(VectorCoefficient &vcoeff, Array<int> &dofs);
243 
244  void ProjectCoefficient(Coefficient *coeff[]);
245 
246  /** @brief Project a discontinuous vector coefficient as a grid function on
247  a continuous finite element space. The values in shared dofs are
248  determined from the element with maximal attribute. */
249  virtual void ProjectDiscCoefficient(VectorCoefficient &coeff);
250 
252  /** @brief Projects a discontinuous coefficient so that the values in shared
253  vdofs are computed by taking an average of the possible values. */
254  virtual void ProjectDiscCoefficient(Coefficient &coeff, AvgType type);
255  /** @brief Projects a discontinuous _vector_ coefficient so that the values
256  in shared vdofs are computed by taking an average of the possible values.
257  */
258  virtual void ProjectDiscCoefficient(VectorCoefficient &coeff, AvgType type);
259 
260 protected:
261  /** @brief Accumulates (depending on @a type) the values of @a coeff at all
262  shared vdofs and counts in how many zones each vdof appears. */
263  void AccumulateAndCountZones(Coefficient &coeff, AvgType type,
264  Array<int> &zones_per_vdof);
265 
266  /** @brief Accumulates (depending on @a type) the values of @a vcoeff at all
267  shared vdofs and counts in how many zones each vdof appears. */
269  Array<int> &zones_per_vdof);
270 
272  VectorCoefficient *vcoeff, Array<int> &attr,
273  Array<int> &values_counter);
274 
276  Array<int> &bdr_attr,
277  Array<int> &values_counter);
278 
279  // Complete the computation of averages; called e.g. after
280  // AccumulateAndCountZones().
281  void ComputeMeans(AvgType type, Array<int> &zones_per_vdof);
282 
283 public:
284  /** @brief Project a Coefficient on the GridFunction, modifying only DOFs on
285  the boundary associated with the boundary attributes marked in the
286  @a attr array. */
288  {
289  Coefficient *coeff_p = &coeff;
290  ProjectBdrCoefficient(&coeff_p, attr);
291  }
292 
293  /** @brief Project a VectorCoefficient on the GridFunction, modifying only
294  DOFs on the boundary associated with the boundary attributes marked in
295  the @a attr array. */
296  virtual void ProjectBdrCoefficient(VectorCoefficient &vcoeff,
297  Array<int> &attr);
298 
299  /** @brief Project a set of Coefficient%s on the components of the
300  GridFunction, modifying only DOFs on the boundary associated with the
301  boundary attributed marked in the @a attr array. */
302  /** If a Coefficient pointer in the array @a coeff is NULL, that component
303  will not be touched. */
304  virtual void ProjectBdrCoefficient(Coefficient *coeff[], Array<int> &attr);
305 
306  /** Project the normal component of the given VectorCoefficient on
307  the boundary. Only boundary attributes that are marked in
308  'bdr_attr' are projected. Assumes RT-type VectorFE GridFunction. */
310  Array<int> &bdr_attr);
311 
312  /** @brief Project the tangential components of the given VectorCoefficient
313  on the boundary. Only boundary attributes that are marked in @a bdr_attr
314  are projected. Assumes ND-type VectorFE GridFunction. */
315  virtual void ProjectBdrCoefficientTangent(VectorCoefficient &vcoeff,
316  Array<int> &bdr_attr);
317 
318  virtual double ComputeL2Error(Coefficient &exsol,
319  const IntegrationRule *irs[] = NULL) const
320  { return ComputeLpError(2.0, exsol, NULL, irs); }
321 
322  virtual double ComputeL2Error(Coefficient *exsol[],
323  const IntegrationRule *irs[] = NULL) const;
324 
325  virtual double ComputeL2Error(VectorCoefficient &exsol,
326  const IntegrationRule *irs[] = NULL,
327  Array<int> *elems = NULL) const;
328 
329  virtual double ComputeH1Error(Coefficient *exsol, VectorCoefficient *exgrad,
330  Coefficient *ell_coef, double Nu,
331  int norm_type) const;
332 
333  virtual double ComputeMaxError(Coefficient &exsol,
334  const IntegrationRule *irs[] = NULL) const
335  {
336  return ComputeLpError(infinity(), exsol, NULL, irs);
337  }
338 
339  virtual double ComputeMaxError(Coefficient *exsol[],
340  const IntegrationRule *irs[] = NULL) const;
341 
342  virtual double ComputeMaxError(VectorCoefficient &exsol,
343  const IntegrationRule *irs[] = NULL) const
344  {
345  return ComputeLpError(infinity(), exsol, NULL, NULL, irs);
346  }
347 
348  virtual double ComputeL1Error(Coefficient &exsol,
349  const IntegrationRule *irs[] = NULL) const
350  { return ComputeLpError(1.0, exsol, NULL, irs); }
351 
352  virtual double ComputeW11Error(Coefficient *exsol, VectorCoefficient *exgrad,
353  int norm_type, Array<int> *elems = NULL,
354  const IntegrationRule *irs[] = NULL) const;
355 
356  virtual double ComputeL1Error(VectorCoefficient &exsol,
357  const IntegrationRule *irs[] = NULL) const
358  { return ComputeLpError(1.0, exsol, NULL, NULL, irs); }
359 
360  virtual double ComputeLpError(const double p, Coefficient &exsol,
361  Coefficient *weight = NULL,
362  const IntegrationRule *irs[] = NULL) const;
363 
364  /** Compute the Lp error in each element of the mesh and store the results in
365  the GridFunction @a error. The result should be an L2 GridFunction of
366  order zero using map type VALUE. */
367  virtual void ComputeElementLpErrors(const double p, Coefficient &exsol,
368  GridFunction &error,
369  Coefficient *weight = NULL,
370  const IntegrationRule *irs[] = NULL
371  ) const;
372 
373  virtual void ComputeElementL1Errors(Coefficient &exsol,
374  GridFunction &error,
375  const IntegrationRule *irs[] = NULL
376  ) const
377  { ComputeElementLpErrors(1.0, exsol, error, NULL, irs); }
378 
379  virtual void ComputeElementL2Errors(Coefficient &exsol,
380  GridFunction &error,
381  const IntegrationRule *irs[] = NULL
382  ) const
383  { ComputeElementLpErrors(2.0, exsol, error, NULL, irs); }
384 
386  GridFunction &error,
387  const IntegrationRule *irs[] = NULL
388  ) const
389  { ComputeElementLpErrors(infinity(), exsol, error, NULL, irs); }
390 
391  /** When given a vector weight, compute the pointwise (scalar) error as the
392  dot product of the vector error with the vector weight. Otherwise, the
393  scalar error is the l_2 norm of the vector error. */
394  virtual double ComputeLpError(const double p, VectorCoefficient &exsol,
395  Coefficient *weight = NULL,
396  VectorCoefficient *v_weight = NULL,
397  const IntegrationRule *irs[] = NULL) const;
398 
399  /** Compute the Lp error in each element of the mesh and store the results in
400  the GridFunction @ error. The result should be an L2 GridFunction of
401  order zero using map type VALUE. */
402  virtual void ComputeElementLpErrors(const double p, VectorCoefficient &exsol,
403  GridFunction &error,
404  Coefficient *weight = NULL,
405  VectorCoefficient *v_weight = NULL,
406  const IntegrationRule *irs[] = NULL
407  ) const;
408 
410  GridFunction &error,
411  const IntegrationRule *irs[] = NULL
412  ) const
413  { ComputeElementLpErrors(1.0, exsol, error, NULL, NULL, irs); }
414 
416  GridFunction &error,
417  const IntegrationRule *irs[] = NULL
418  ) const
419  { ComputeElementLpErrors(2.0, exsol, error, NULL, NULL, irs); }
420 
422  GridFunction &error,
423  const IntegrationRule *irs[] = NULL
424  ) const
425  { ComputeElementLpErrors(infinity(), exsol, error, NULL, NULL, irs); }
426 
427  virtual void ComputeFlux(BilinearFormIntegrator &blfi,
428  GridFunction &flux,
429  bool wcoef = true, int subdomain = -1);
430 
431  /// Redefine '=' for GridFunction = constant.
432  GridFunction &operator=(double value);
433 
434  /// Copy the data from @a v.
435  /** The size of @a v must be equal to the size of the associated
436  FiniteElementSpace #fes. */
437  GridFunction &operator=(const Vector &v);
438 
439  /// Transform by the Space UpdateMatrix (e.g., on Mesh change).
440  virtual void Update();
441 
443  const FiniteElementSpace *FESpace() const { return fes; }
444 
445  /// Associate a new FiniteElementSpace with the GridFunction.
446  /** The GridFunction is resized using the SetSize() method. */
447  virtual void SetSpace(FiniteElementSpace *f);
448 
449  using Vector::MakeRef;
450 
451  /** @brief Make the GridFunction reference external data on a new
452  FiniteElementSpace. */
453  /** This method changes the FiniteElementSpace associated with the
454  GridFunction and sets the pointer @a v as external data in the
455  GridFunction. */
456  virtual void MakeRef(FiniteElementSpace *f, double *v);
457 
458  /** @brief Make the GridFunction reference external data on a new
459  FiniteElementSpace. */
460  /** This method changes the FiniteElementSpace associated with the
461  GridFunction and sets the data of the Vector @a v (plus the @a v_offset)
462  as external data in the GridFunction.
463  @note This version of the method will also perform bounds checks when
464  the build option MFEM_DEBUG is enabled. */
465  virtual void MakeRef(FiniteElementSpace *f, Vector &v, int v_offset);
466 
467  /** @brief Associate a new FiniteElementSpace and new true-dof data with the
468  GridFunction. */
469  /** - If the prolongation matrix of @a f is trivial (i.e. its method
470  FiniteElementSpace::GetProlongationMatrix() returns NULL), then the
471  method MakeRef() is called with the same arguments.
472  - Otherwise, the method SetSpace() is called with argument @a f.
473  - The internal true-dof vector is set to reference @a tv. */
474  void MakeTRef(FiniteElementSpace *f, double *tv);
475 
476  /** @brief Associate a new FiniteElementSpace and new true-dof data with the
477  GridFunction. */
478  /** - If the prolongation matrix of @a f is trivial (i.e. its method
479  FiniteElementSpace::GetProlongationMatrix() returns NULL), this method
480  calls MakeRef() with the same arguments.
481  - Otherwise, this method calls SetSpace() with argument @a f.
482  - The internal true-dof vector is set to reference the sub-vector of
483  @a tv starting at the offset @a tv_offset. */
484  void MakeTRef(FiniteElementSpace *f, Vector &tv, int tv_offset);
485 
486  /// Save the GridFunction to an output stream.
487  virtual void Save(std::ostream &out) const;
488 
489  /** Write the GridFunction in VTK format. Note that Mesh::PrintVTK must be
490  called first. The parameter ref > 0 must match the one used in
491  Mesh::PrintVTK. */
492  void SaveVTK(std::ostream &out, const std::string &field_name, int ref);
493 
494  void SaveSTL(std::ostream &out, int TimesToRefine = 1);
495 
496  /// Destroys grid function.
497  virtual ~GridFunction() { Destroy(); }
498 };
499 
500 
501 /** Overload operator<< for std::ostream and GridFunction; valid also for the
502  derived class ParGridFunction */
503 std::ostream &operator<<(std::ostream &out, const GridFunction &sol);
504 
505 
506 /** @brief Class representing a function through its values (scalar or vector)
507  at quadrature points. */
509 {
510 protected:
511  QuadratureSpace *qspace; ///< Associated QuadratureSpace
512  int vdim; ///< Vector dimension
513  bool own_qspace; ///< QuadratureSpace ownership flag
514 
515 public:
516  /// Create an empty QuadratureFunction.
517  /** The object can be initialized later using the SetSpace() methods. */
519  : qspace(NULL), vdim(0), own_qspace(false) { }
520 
521  /** @brief Copy constructor. The QuadratureSpace ownership flag, #own_qspace,
522  in the new object is set to false. */
524  : Vector(orig),
525  qspace(orig.qspace), vdim(orig.vdim), own_qspace(false) { }
526 
527  /// Create a QuadratureFunction based on the given QuadratureSpace.
528  /** The QuadratureFunction does not assume ownership of the QuadratureSpace.
529  @note The Vector data is not initialized. */
530  QuadratureFunction(QuadratureSpace *qspace_, int vdim_ = 1)
531  : Vector(vdim_*qspace_->GetSize()),
532  qspace(qspace_), vdim(vdim_), own_qspace(false) { }
533 
534  /** @brief Create a QuadratureFunction based on the given QuadratureSpace,
535  using the external data, @a qf_data. */
536  /** The QuadratureFunction does not assume ownership of neither the
537  QuadratureSpace nor the external data. */
538  QuadratureFunction(QuadratureSpace *qspace_, double *qf_data, int vdim_ = 1)
539  : Vector(qf_data, vdim_*qspace_->GetSize()),
540  qspace(qspace_), vdim(vdim_), own_qspace(false) { }
541 
542  /// Read a QuadratureFunction from the stream @a in.
543  /** The QuadratureFunction assumes ownership of the read QuadratureSpace. */
544  QuadratureFunction(Mesh *mesh, std::istream &in);
545 
546  virtual ~QuadratureFunction() { if (own_qspace) { delete qspace; } }
547 
548  /// Get the associated QuadratureSpace.
549  QuadratureSpace *GetSpace() const { return qspace; }
550 
551  /// Change the QuadratureSpace and optionally the vector dimension.
552  /** If the new QuadratureSpace is different from the current one, the
553  QuadratureFunction will not assume ownership of the new space; otherwise,
554  the ownership flag remains the same.
555 
556  If the new vector dimension @a vdim_ < 0, the vector dimension remains
557  the same.
558 
559  The data size is updated by calling Vector::SetSize(). */
560  inline void SetSpace(QuadratureSpace *qspace_, int vdim_ = -1);
561 
562  /** @brief Change the QuadratureSpace, the data array, and optionally the
563  vector dimension. */
564  /** If the new QuadratureSpace is different from the current one, the
565  QuadratureFunction will not assume ownership of the new space; otherwise,
566  the ownership flag remains the same.
567 
568  If the new vector dimension @a vdim_ < 0, the vector dimension remains
569  the same.
570 
571  The data array is replaced by calling Vector::NewDataAndSize(). */
572  inline void SetSpace(QuadratureSpace *qspace_, double *qf_data,
573  int vdim_ = -1);
574 
575  /// Get the vector dimension.
576  int GetVDim() const { return vdim; }
577 
578  /// Set the vector dimension, updating the size by calling Vector::SetSize().
579  void SetVDim(int vdim_)
580  { vdim = vdim_; SetSize(vdim*qspace->GetSize()); }
581 
582  /// Get the QuadratureSpace ownership flag.
583  bool OwnsSpace() { return own_qspace; }
584 
585  /// Set the QuadratureSpace ownership flag.
586  void SetOwnsSpace(bool own) { own_qspace = own; }
587 
588  /// Redefine '=' for QuadratureFunction = constant.
589  QuadratureFunction &operator=(double value);
590 
591  /// Copy the data from @a v.
592  /** The size of @a v must be equal to the size of the associated
593  QuadratureSpace #qspace. */
595 
596  /// Copy assignment. Only the data of the base class Vector is copied.
597  /** The QuadratureFunctions @a v and @a *this must have QuadratureSpaces with
598  the same size.
599 
600  @note Defining this method overwrites the implicitly defined copy
601  assignemnt operator. */
603 
604  /// Get the IntegrationRule associated with mesh element @a idx.
605  const IntegrationRule &GetElementIntRule(int idx) const
606  { return qspace->GetElementIntRule(idx); }
607 
608  /// Return all values associated with mesh element @a idx in a Vector.
609  /** The result is stored in the Vector @a values as a reference to the
610  global values.
611 
612  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
613  `i`-th vector component at the `j`-th quadrature point.
614  */
615  inline void GetElementValues(int idx, Vector &values);
616 
617  /// Return all values associated with mesh element @a idx in a Vector.
618  /** The result is stored in the Vector @a values as a copy of the
619  global values.
620 
621  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
622  `i`-th vector component at the `j`-th quadrature point.
623  */
624  inline void GetElementValues(int idx, Vector &values) const;
625 
626  /// Return all values associated with mesh element @a idx in a DenseMatrix.
627  /** The result is stored in the DenseMatrix @a values as a reference to the
628  global values.
629 
630  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
631  `i`-th vector component at the `j`-th quadrature point.
632  */
633  inline void GetElementValues(int idx, DenseMatrix &values);
634 
635  /// Return all values associated with mesh element @a idx in a const DenseMatrix.
636  /** The result is stored in the DenseMatrix @a values as a copy of the
637  global values.
638 
639  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
640  `i`-th vector component at the `j`-th quadrature point.
641  */
642  inline void GetElementValues(int idx, DenseMatrix &values) const;
643 
644  /// Write the QuadratureFunction to the stream @a out.
645  void Save(std::ostream &out) const;
646 };
647 
648 /// Overload operator<< for std::ostream and QuadratureFunction.
649 std::ostream &operator<<(std::ostream &out, const QuadratureFunction &qf);
650 
651 
652 double ZZErrorEstimator(BilinearFormIntegrator &blfi,
653  GridFunction &u,
654  GridFunction &flux,
655  Vector &error_estimates,
656  Array<int> *aniso_flags = NULL,
657  int with_subdomains = 1,
658  bool with_coeff = false);
659 
660 /// Compute the Lp distance between two grid functions on the given element.
661 double ComputeElementLpDistance(double p, int i,
662  GridFunction& gf1, GridFunction& gf2);
663 
664 
665 /// Class used for extruding scalar GridFunctions
667 {
668 private:
669  int n;
670  Mesh *mesh_in;
671  Coefficient &sol_in;
672 public:
674  : n(_n), mesh_in(m), sol_in(s) { }
675  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip);
676  virtual ~ExtrudeCoefficient() { }
677 };
678 
679 /// Extrude a scalar 1D GridFunction, after extruding the mesh with Extrude1D.
680 GridFunction *Extrude1DGridFunction(Mesh *mesh, Mesh *mesh2d,
681  GridFunction *sol, const int ny);
682 
683 
684 // Inline methods
685 
686 inline void QuadratureFunction::SetSpace(QuadratureSpace *qspace_, int vdim_)
687 {
688  if (qspace_ != qspace)
689  {
690  if (own_qspace) { delete qspace; }
691  qspace = qspace_;
692  own_qspace = false;
693  }
694  vdim = (vdim_ < 0) ? vdim : vdim_;
696 }
697 
699  double *qf_data, int vdim_)
700 {
701  if (qspace_ != qspace)
702  {
703  if (own_qspace) { delete qspace; }
704  qspace = qspace_;
705  own_qspace = false;
706  }
707  vdim = (vdim_ < 0) ? vdim : vdim_;
708  NewDataAndSize(qf_data, vdim*qspace->GetSize());
709 }
710 
711 inline void QuadratureFunction::GetElementValues(int idx, Vector &values)
712 {
713  const int s_offset = qspace->element_offsets[idx];
714  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
715  values.NewDataAndSize(data + vdim*s_offset, vdim*sl_size);
716 }
717 
718 inline void QuadratureFunction::GetElementValues(int idx, Vector &values) const
719 {
720  const int s_offset = qspace->element_offsets[idx];
721  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
722  values.SetSize(vdim*sl_size);
723  const double *q = data + vdim*s_offset;
724  for (int i = 0; i<values.Size(); i++)
725  {
726  values(i) = *(q++);
727  }
728 }
729 
731 {
732  const int s_offset = qspace->element_offsets[idx];
733  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
734  values.Reset(data + vdim*s_offset, vdim, sl_size);
735 }
736 
738  DenseMatrix &values) const
739 {
740  const int s_offset = qspace->element_offsets[idx];
741  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
742  values.SetSize(vdim, sl_size);
743  const double *q = data + vdim*s_offset;
744  for (int j = 0; j<sl_size; j++)
745  {
746  for (int i = 0; i<vdim; i++)
747  {
748  values(i,j) = *(q++);
749  }
750  }
751 }
752 
753 } // namespace mfem
754 
755 #endif
void SaveSTLTri(std::ostream &out, double p1[], double p2[], double p3[])
Definition: gridfunc.cpp:2833
int GetVDim() const
Get the vector dimension.
Definition: gridfunc.hpp:576
virtual ~GridFunction()
Destroys grid function.
Definition: gridfunc.hpp:497
std::ostream & operator<<(std::ostream &out, const Mesh &mesh)
Definition: mesh.cpp:10127
QuadratureFunction(QuadratureSpace *qspace_, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpace.
Definition: gridfunc.hpp:530
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:90
void GetElementAverages(GridFunction &avgs) const
Definition: gridfunc.cpp:1150
void NewDataAndSize(double *d, int s)
Set the Vector data and size, deleting the old data, if owned.
Definition: vector.hpp:131
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:27
const IntegrationRule & GetElementIntRule(int idx) const
Get the IntegrationRule associated with mesh element idx.
Definition: gridfunc.hpp:605
Memory< double > data
Definition: vector.hpp:52
Class used for extruding scalar GridFunctions.
Definition: gridfunc.hpp:666
void SumFluxAndCount(BilinearFormIntegrator &blfi, GridFunction &flux, Array< int > &counts, bool wcoef, int subdomain)
Definition: gridfunc.cpp:240
void SetFromTrueVector()
Shortcut for calling SetFromTrueDofs() with GetTrueVector() as argument.
Definition: gridfunc.hpp:139
virtual double ComputeL2Error(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:318
void ProjectDeltaCoefficient(DeltaCoefficient &delta_coeff, double &integral)
Definition: gridfunc.cpp:1622
const IntegrationRule & GetElementIntRule(int idx) const
Get the IntegrationRule associated with mesh element idx.
Definition: fespace.hpp:703
void GetVectorValue(int i, const IntegrationPoint &ip, Vector &val) const
Definition: gridfunc.cpp:408
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:407
GridFunction * Extrude1DGridFunction(Mesh *mesh, Mesh *mesh2d, GridFunction *sol, const int ny)
Extrude a scalar 1D GridFunction, after extruding the mesh with Extrude1D.
Definition: gridfunc.cpp:3152
void MakeOwner(FiniteElementCollection *_fec)
Make the GridFunction the owner of fec and fes.
Definition: gridfunc.hpp:112
void ProjectDiscCoefficient(VectorCoefficient &coeff, Array< int > &dof_attr)
Definition: gridfunc.cpp:1816
bool own_qspace
QuadratureSpace ownership flag.
Definition: gridfunc.hpp:513
int GetSize() const
Return the total number of quadrature points.
Definition: fespace.hpp:700
int VectorDim() const
Definition: gridfunc.cpp:302
virtual ~QuadratureFunction()
Definition: gridfunc.hpp:546
Data type dense matrix using column-major storage.
Definition: densemat.hpp:23
Delta function coefficient.
int Size() const
Returns the size of the vector.
Definition: vector.hpp:157
void ImposeBounds(int i, const Vector &weights, const Vector &_lo, const Vector &_hi)
Definition: gridfunc.cpp:1224
virtual void ComputeElementL1Errors(Coefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:373
void AccumulateAndCountBdrTangentValues(VectorCoefficient &vcoeff, Array< int > &bdr_attr, Array< int > &values_counter)
Definition: gridfunc.cpp:1546
int vdim
Vector dimension.
Definition: gridfunc.hpp:512
void GetVectorGradientHat(ElementTransformation &T, DenseMatrix &gh) const
Definition: gridfunc.cpp:991
Vector & GetTrueVector()
Read and write access to the (optional) internal true-dof Vector.
Definition: gridfunc.hpp:125
virtual double ComputeMaxError(VectorCoefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:342
void SetSpace(QuadratureSpace *qspace_, int vdim_=-1)
Change the QuadratureSpace and optionally the vector dimension.
Definition: gridfunc.hpp:686
void ComputeMeans(AvgType type, Array< int > &zones_per_vdof)
Definition: gridfunc.cpp:1597
bool OwnsSpace()
Get the QuadratureSpace ownership flag.
Definition: gridfunc.hpp:583
void GetNodalValues(int i, Array< double > &nval, int vdim=1) const
Returns the values in the vertices of i&#39;th element for dimension vdim.
Definition: gridfunc.cpp:352
QuadratureFunction(QuadratureSpace *qspace_, double *qf_data, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpace, using the external data...
Definition: gridfunc.hpp:538
void GetVectorFieldNodalValues(Vector &val, int comp) const
Definition: gridfunc.cpp:860
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:2731
const FiniteElementSpace * FESpace() const
Definition: gridfunc.hpp:443
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Evaluate the coefficient in the element described by T at the point ip.
Definition: gridfunc.cpp:3142
GridFunction(FiniteElementSpace *f, double *data)
Construct a GridFunction using previously allocated array data.
Definition: gridfunc.hpp:88
void GetDerivative(int comp, int der_comp, GridFunction &der)
Definition: gridfunc.cpp:931
void ProjectBdrCoefficientNormal(VectorCoefficient &vcoeff, Array< int > &bdr_attr)
Definition: gridfunc.cpp:1908
void SetTrueVector()
Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
Definition: gridfunc.hpp:133
void GetHessians(int i, const IntegrationRule &ir, DenseMatrix &hess, int vdim=1) const
Definition: gridfunc.cpp:514
virtual double ComputeL1Error(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:348
bool UseDevice() const
Return the device flag of the Memory object used by the Vector.
Definition: vector.hpp:89
void GetGradients(const int elem, const IntegrationRule &ir, DenseMatrix &grad) const
Definition: gridfunc.hpp:208
void Reset(double *d, int h, int w)
Change the data array and the size of the DenseMatrix.
Definition: densemat.hpp:75
void GetVectorFieldValues(int i, const IntegrationRule &ir, DenseMatrix &vals, DenseMatrix &tr, int comp=0) const
Definition: gridfunc.cpp:791
void SetOwnsSpace(bool own)
Set the QuadratureSpace ownership flag.
Definition: gridfunc.hpp:586
virtual double ComputeH1Error(Coefficient *exsol, VectorCoefficient *exgrad, Coefficient *ell_coef, double Nu, int norm_type) const
Definition: gridfunc.cpp:2098
GridFunction(const GridFunction &orig)
Copy constructor. The internal true-dof vector t_vec is not copied.
Definition: gridfunc.hpp:74
void MakeTRef(FiniteElementSpace *f, double *tv)
Associate a new FiniteElementSpace and new true-dof data with the GridFunction.
Definition: gridfunc.cpp:207
QuadratureSpace * GetSpace() const
Get the associated QuadratureSpace.
Definition: gridfunc.hpp:549
virtual double ComputeL1Error(VectorCoefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:356
QuadratureFunction()
Create an empty QuadratureFunction.
Definition: gridfunc.hpp:518
void AccumulateAndCountZones(Coefficient &coeff, AvgType type, Array< int > &zones_per_vdof)
Accumulates (depending on type) the values of coeff at all shared vdofs and counts in how many zones ...
Definition: gridfunc.cpp:1308
virtual void MakeRef(FiniteElementSpace *f, double *v)
Make the GridFunction reference external data on a new FiniteElementSpace.
Definition: gridfunc.cpp:188
void SetVDim(int vdim_)
Set the vector dimension, updating the size by calling Vector::SetSize().
Definition: gridfunc.hpp:579
void GetLaplacians(int i, const IntegrationRule &ir, Vector &laps, int vdim=1) const
Definition: gridfunc.cpp:475
void GetTrueDofs(Vector &tv) const
Extract the true-dofs from the GridFunction. If all dofs are true, then tv will be set to point to th...
Definition: gridfunc.cpp:323
void GetValues(int i, const IntegrationRule &ir, Vector &vals, int vdim=1) const
Definition: gridfunc.cpp:442
virtual void ComputeElementMaxErrors(Coefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:385
FiniteElementCollection * OwnFEC()
Definition: gridfunc.hpp:114
virtual ~ExtrudeCoefficient()
Definition: gridfunc.hpp:676
virtual void ComputeElementL2Errors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:415
FiniteElementSpace * FESpace()
Definition: gridfunc.hpp:442
GridFunction & operator=(const GridFunction &rhs)
Copy assignment. Only the data of the base class Vector is copied.
Definition: gridfunc.hpp:106
virtual double ComputeLpError(const double p, Coefficient &exsol, Coefficient *weight=NULL, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.cpp:2409
QuadratureFunction & operator=(double value)
Redefine &#39;=&#39; for QuadratureFunction = constant.
Definition: gridfunc.cpp:2968
const Vector & GetTrueVector() const
Read only access to the (optional) internal true-dof Vector.
Definition: gridfunc.hpp:121
ElementTransformation * GetElementTransformation(int i) const
Returns ElementTransformation for the i-th element.
Definition: fespace.hpp:441
Abstract base class BilinearFormIntegrator.
Definition: bilininteg.hpp:24
virtual void ComputeElementL1Errors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:409
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:87
Base class Coefficient that may optionally depend on time.
Definition: coefficient.hpp:31
FiniteElementSpace * fes
FE space on which the grid function lives. Owned if fec is not NULL.
Definition: gridfunc.hpp:31
void ProjectGridFunction(const GridFunction &src)
Project the src GridFunction to this GridFunction, both of which must be on the same mesh...
Definition: gridfunc.cpp:1180
QuadratureSpace * qspace
Associated QuadratureSpace.
Definition: gridfunc.hpp:511
virtual double ComputeMaxError(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:333
FiniteElementCollection * fec
Used when the grid function is read from a file. It can also be set explicitly, see MakeOwner()...
Definition: gridfunc.hpp:37
void GetVectorGradient(ElementTransformation &tr, DenseMatrix &grad) const
Definition: gridfunc.cpp:1138
double GetDivergence(ElementTransformation &tr) const
Definition: gridfunc.cpp:1010
virtual void Update()
Transform by the Space UpdateMatrix (e.g., on Mesh change).
Definition: gridfunc.cpp:152
double ComputeElementLpDistance(double p, int i, GridFunction &gf1, GridFunction &gf2)
Compute the Lp distance between two grid functions on the given element.
Definition: gridfunc.cpp:3085
int GetFaceVectorValues(int i, int side, const IntegrationRule &ir, DenseMatrix &vals, DenseMatrix &tr) const
Definition: gridfunc.cpp:674
double ZZErrorEstimator(BilinearFormIntegrator &blfi, GridFunction &u, GridFunction &flux, Vector &error_estimates, Array< int > *aniso_flags, int with_subdomains, bool with_coeff)
Definition: gridfunc.cpp:3002
Class for integration point with weight.
Definition: intrules.hpp:25
void SaveSTL(std::ostream &out, int TimesToRefine=1)
Definition: gridfunc.cpp:2853
int GetFaceValues(int i, int side, const IntegrationRule &ir, Vector &vals, DenseMatrix &tr, int vdim=1) const
Definition: gridfunc.cpp:570
virtual void ComputeElementLpErrors(const double p, Coefficient &exsol, GridFunction &error, Coefficient *weight=NULL, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.cpp:2474
void GetElementValues(int idx, Vector &values)
Return all values associated with mesh element idx in a Vector.
Definition: gridfunc.hpp:711
void GetGradients(ElementTransformation &tr, const IntegrationRule &ir, DenseMatrix &grad) const
Definition: gridfunc.cpp:1113
virtual void ProjectBdrCoefficientTangent(VectorCoefficient &vcoeff, Array< int > &bdr_attr)
Project the tangential components of the given VectorCoefficient on the boundary. Only boundary attri...
Definition: gridfunc.cpp:1979
void GetVectorValues(ElementTransformation &T, const IntegrationRule &ir, DenseMatrix &vals) const
Definition: gridfunc.cpp:620
QuadratureFunction(const QuadratureFunction &orig)
Copy constructor. The QuadratureSpace ownership flag, own_qspace, in the new object is set to false...
Definition: gridfunc.hpp:523
double infinity()
Define a shortcut for std::numeric_limits&lt;double&gt;::infinity()
Definition: vector.hpp:42
virtual void ProjectCoefficient(Coefficient &coeff)
Definition: gridfunc.cpp:1685
virtual void SetSpace(FiniteElementSpace *f)
Associate a new FiniteElementSpace with the GridFunction.
Definition: gridfunc.cpp:180
void ReorderByNodes()
For a vector grid function, makes sure that the ordering is byNODES.
Definition: gridfunc.cpp:833
virtual double ComputeW11Error(Coefficient *exsol, VectorCoefficient *exgrad, int norm_type, Array< int > *elems=NULL, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.cpp:2303
ExtrudeCoefficient(Mesh *m, Coefficient &s, int _n)
Definition: gridfunc.hpp:673
void GetCurl(ElementTransformation &tr, Vector &curl) const
Definition: gridfunc.cpp:1044
Vector data type.
Definition: vector.hpp:48
void MakeRef(Vector &base, int offset, int size)
Reset the Vector to be a reference to a sub-vector of base.
Definition: vector.hpp:465
virtual void ComputeFlux(BilinearFormIntegrator &blfi, GridFunction &flux, bool wcoef=true, int subdomain=-1)
Definition: gridfunc.cpp:287
GridFunction(FiniteElementSpace *f)
Construct a GridFunction associated with the FiniteElementSpace *f.
Definition: gridfunc.hpp:79
Class representing the storage layout of a QuadratureFunction.
Definition: fespace.hpp:671
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
Definition: densemat.hpp:90
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
long GetSequence() const
Return update counter (see Mesh::sequence)
Definition: fespace.hpp:657
virtual void SetFromTrueDofs(const Vector &tv)
Set the GridFunction from the given true-dof vector.
Definition: gridfunc.cpp:338
virtual void ComputeElementL2Errors(Coefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:379
virtual void ComputeElementMaxErrors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:421
Class representing a function through its values (scalar or vector) at quadrature points...
Definition: gridfunc.hpp:508
void GetValuesFrom(const GridFunction &orig_func)
Definition: gridfunc.cpp:717
void GetBdrValuesFrom(const GridFunction &orig_func)
Definition: gridfunc.cpp:754
void AccumulateAndCountBdrValues(Coefficient *coeff[], VectorCoefficient *vcoeff, Array< int > &attr, Array< int > &values_counter)
Definition: gridfunc.cpp:1404
void Save(std::ostream &out) const
Write the QuadratureFunction to the stream out.
Definition: gridfunc.cpp:2986
void SaveVTK(std::ostream &out, const std::string &field_name, int ref)
Definition: gridfunc.cpp:2756
void ProjectBdrCoefficient(Coefficient &coeff, Array< int > &attr)
Project a Coefficient on the GridFunction, modifying only DOFs on the boundary associated with the bo...
Definition: gridfunc.hpp:287
virtual double GetValue(int i, const IntegrationPoint &ip, int vdim=1) const
Definition: gridfunc.cpp:393
void GetGradient(ElementTransformation &tr, Vector &grad) const
Definition: gridfunc.cpp:1095
void ProjectVectorFieldOn(GridFunction &vec_field, int comp=0)
Definition: gridfunc.cpp:891