MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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  int 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  int GetFaceValues(int i, int side, const IntegrationRule &ir, Vector &vals,
156  DenseMatrix &tr, int vdim = 1) const;
157 
159  DenseMatrix &vals) const;
160 
161  void GetVectorValues(int i, const IntegrationRule &ir,
162  DenseMatrix &vals, DenseMatrix &tr) const;
163 
164  int GetFaceVectorValues(int i, int side, const IntegrationRule &ir,
165  DenseMatrix &vals, DenseMatrix &tr) const;
166 
167  void GetValuesFrom(const GridFunction &orig_func);
168 
169  void GetBdrValuesFrom(const GridFunction &orig_func);
170 
171  void GetVectorFieldValues(int i, const IntegrationRule &ir,
172  DenseMatrix &vals,
173  DenseMatrix &tr, int comp = 0) const;
174 
175  /// For a vector grid function, makes sure that the ordering is byNODES.
176  void ReorderByNodes();
177 
178  /// Return the values as a vector on mesh vertices for dimension vdim.
179  void GetNodalValues(Vector &nval, int vdim = 1) const;
180 
181  void GetVectorFieldNodalValues(Vector &val, int comp) const;
182 
183  void ProjectVectorFieldOn(GridFunction &vec_field, int comp = 0);
184 
185  void GetDerivative(int comp, int der_comp, GridFunction &der);
186 
187  double GetDivergence(ElementTransformation &tr) const;
188 
189  void GetCurl(ElementTransformation &tr, Vector &curl) const;
190 
191  void GetGradient(ElementTransformation &tr, Vector &grad) const;
192 
194  DenseMatrix &grad) const;
195 
196  void GetGradients(const int elem, const IntegrationRule &ir,
197  DenseMatrix &grad) const
198  { GetGradients(*fes->GetElementTransformation(elem), ir, grad); }
199 
200  void GetVectorGradient(ElementTransformation &tr, DenseMatrix &grad) const;
201 
202  /** Compute \f$ (\int_{\Omega} (*this) \psi_i)/(\int_{\Omega} \psi_i) \f$,
203  where \f$ \psi_i \f$ are the basis functions for the FE space of avgs.
204  Both FE spaces should be scalar and on the same mesh. */
205  void GetElementAverages(GridFunction &avgs) const;
206 
207  /** Impose the given bounds on the function's DOFs while preserving its local
208  * integral (described in terms of the given weights) on the i'th element
209  * through SLBPQ optimization.
210  * Intended to be used for discontinuous FE functions. */
211  void ImposeBounds(int i, const Vector &weights,
212  const Vector &_lo, const Vector &_hi);
213  void ImposeBounds(int i, const Vector &weights,
214  double _min = 0.0, double _max = infinity());
215 
216  /** @brief Project the @a src GridFunction to @a this GridFunction, both of
217  which must be on the same mesh. */
218  /** The current implementation assumes that all elements use the same
219  projection matrix. */
220  void ProjectGridFunction(const GridFunction &src);
221 
222  virtual void ProjectCoefficient(Coefficient &coeff);
223 
224  // call fes -> BuildDofToArrays() before using this projection
225  void ProjectCoefficient(Coefficient &coeff, Array<int> &dofs, int vd = 0);
226 
228 
229  // call fes -> BuildDofToArrays() before using this projection
230  void ProjectCoefficient(VectorCoefficient &vcoeff, Array<int> &dofs);
231 
232  void ProjectCoefficient(Coefficient *coeff[]);
233 
234  /** @brief Project a discontinuous vector coefficient as a grid function on
235  a continuous finite element space. The values in shared dofs are
236  determined from the element with maximal attribute. */
237  virtual void ProjectDiscCoefficient(VectorCoefficient &coeff);
238 
240  /** @brief Projects a discontinuous coefficient so that the values in shared
241  vdofs are computed by taking an average of the possible values. */
242  virtual void ProjectDiscCoefficient(Coefficient &coeff, AvgType type);
243  /** @brief Projects a discontinuous _vector_ coefficient so that the values
244  in shared vdofs are computed by taking an average of the possible values.
245  */
246  virtual void ProjectDiscCoefficient(VectorCoefficient &coeff, AvgType type);
247 
248 protected:
249  /** @brief Accumulates (depending on @a type) the values of @a coeff at all
250  shared vdofs and counts in how many zones each vdof appears. */
251  void AccumulateAndCountZones(Coefficient &coeff, AvgType type,
252  Array<int> &zones_per_vdof);
253 
254  /** @brief Accumulates (depending on @a type) the values of @a vcoeff at all
255  shared vdofs and counts in how many zones each vdof appears. */
257  Array<int> &zones_per_vdof);
258 
260  VectorCoefficient *vcoeff, Array<int> &attr,
261  Array<int> &values_counter);
262 
264  Array<int> &bdr_attr,
265  Array<int> &values_counter);
266 
267  // Complete the computation of averages; called e.g. after
268  // AccumulateAndCountZones().
269  void ComputeMeans(AvgType type, Array<int> &zones_per_vdof);
270 
271 public:
272  /** @brief Project a Coefficient on the GridFunction, modifying only DOFs on
273  the boundary associated with the boundary attributes marked in the
274  @a attr array. */
276  {
277  Coefficient *coeff_p = &coeff;
278  ProjectBdrCoefficient(&coeff_p, attr);
279  }
280 
281  /** @brief Project a VectorCoefficient on the GridFunction, modifying only
282  DOFs on the boundary associated with the boundary attributes marked in
283  the @a attr array. */
284  virtual void ProjectBdrCoefficient(VectorCoefficient &vcoeff,
285  Array<int> &attr);
286 
287  /** @brief Project a set of Coefficient%s on the components of the
288  GridFunction, modifying only DOFs on the boundary associated with the
289  boundary attributed marked in the @a attr array. */
290  /** If a Coefficient pointer in the array @a coeff is NULL, that component
291  will not be touched. */
292  virtual void ProjectBdrCoefficient(Coefficient *coeff[], Array<int> &attr);
293 
294  /** Project the normal component of the given VectorCoefficient on
295  the boundary. Only boundary attributes that are marked in
296  'bdr_attr' are projected. Assumes RT-type VectorFE GridFunction. */
298  Array<int> &bdr_attr);
299 
300  /** @brief Project the tangential components of the given VectorCoefficient
301  on the boundary. Only boundary attributes that are marked in @a bdr_attr
302  are projected. Assumes ND-type VectorFE GridFunction. */
303  virtual void ProjectBdrCoefficientTangent(VectorCoefficient &vcoeff,
304  Array<int> &bdr_attr);
305 
306  virtual double ComputeL2Error(Coefficient &exsol,
307  const IntegrationRule *irs[] = NULL) const
308  { return ComputeLpError(2.0, exsol, NULL, irs); }
309 
310  virtual double ComputeL2Error(Coefficient *exsol[],
311  const IntegrationRule *irs[] = NULL) const;
312 
313  virtual double ComputeL2Error(VectorCoefficient &exsol,
314  const IntegrationRule *irs[] = NULL,
315  Array<int> *elems = NULL) const;
316 
317  virtual double ComputeH1Error(Coefficient *exsol, VectorCoefficient *exgrad,
318  Coefficient *ell_coef, double Nu,
319  int norm_type) const;
320 
321  virtual double ComputeMaxError(Coefficient &exsol,
322  const IntegrationRule *irs[] = NULL) const
323  {
324  return ComputeLpError(infinity(), exsol, NULL, irs);
325  }
326 
327  virtual double ComputeMaxError(Coefficient *exsol[],
328  const IntegrationRule *irs[] = NULL) const;
329 
330  virtual double ComputeMaxError(VectorCoefficient &exsol,
331  const IntegrationRule *irs[] = NULL) const
332  {
333  return ComputeLpError(infinity(), exsol, NULL, NULL, irs);
334  }
335 
336  virtual double ComputeL1Error(Coefficient &exsol,
337  const IntegrationRule *irs[] = NULL) const
338  { return ComputeLpError(1.0, exsol, NULL, irs); }
339 
340  virtual double ComputeW11Error(Coefficient *exsol, VectorCoefficient *exgrad,
341  int norm_type, Array<int> *elems = NULL,
342  const IntegrationRule *irs[] = NULL) const;
343 
344  virtual double ComputeL1Error(VectorCoefficient &exsol,
345  const IntegrationRule *irs[] = NULL) const
346  { return ComputeLpError(1.0, exsol, NULL, NULL, irs); }
347 
348  virtual double ComputeLpError(const double p, Coefficient &exsol,
349  Coefficient *weight = NULL,
350  const IntegrationRule *irs[] = NULL) const;
351 
352  /** Compute the Lp error in each element of the mesh and store the results in
353  the GridFunction @a error. The result should be an L2 GridFunction of
354  order zero using map type VALUE. */
355  virtual void ComputeElementLpErrors(const double p, Coefficient &exsol,
356  GridFunction &error,
357  Coefficient *weight = NULL,
358  const IntegrationRule *irs[] = NULL
359  ) const;
360 
361  virtual void ComputeElementL1Errors(Coefficient &exsol,
362  GridFunction &error,
363  const IntegrationRule *irs[] = NULL
364  ) const
365  { ComputeElementLpErrors(1.0, exsol, error, NULL, irs); }
366 
367  virtual void ComputeElementL2Errors(Coefficient &exsol,
368  GridFunction &error,
369  const IntegrationRule *irs[] = NULL
370  ) const
371  { ComputeElementLpErrors(2.0, exsol, error, NULL, irs); }
372 
374  GridFunction &error,
375  const IntegrationRule *irs[] = NULL
376  ) const
377  { ComputeElementLpErrors(infinity(), exsol, error, NULL, irs); }
378 
379  /** When given a vector weight, compute the pointwise (scalar) error as the
380  dot product of the vector error with the vector weight. Otherwise, the
381  scalar error is the l_2 norm of the vector error. */
382  virtual double ComputeLpError(const double p, VectorCoefficient &exsol,
383  Coefficient *weight = NULL,
384  VectorCoefficient *v_weight = NULL,
385  const IntegrationRule *irs[] = NULL) const;
386 
387  /** Compute the Lp error in each element of the mesh and store the results in
388  the GridFunction @ error. The result should be an L2 GridFunction of
389  order zero using map type VALUE. */
390  virtual void ComputeElementLpErrors(const double p, VectorCoefficient &exsol,
391  GridFunction &error,
392  Coefficient *weight = NULL,
393  VectorCoefficient *v_weight = NULL,
394  const IntegrationRule *irs[] = NULL
395  ) const;
396 
398  GridFunction &error,
399  const IntegrationRule *irs[] = NULL
400  ) const
401  { ComputeElementLpErrors(1.0, exsol, error, NULL, NULL, irs); }
402 
404  GridFunction &error,
405  const IntegrationRule *irs[] = NULL
406  ) const
407  { ComputeElementLpErrors(2.0, exsol, error, NULL, NULL, irs); }
408 
410  GridFunction &error,
411  const IntegrationRule *irs[] = NULL
412  ) const
413  { ComputeElementLpErrors(infinity(), exsol, error, NULL, NULL, irs); }
414 
415  virtual void ComputeFlux(BilinearFormIntegrator &blfi,
416  GridFunction &flux,
417  int wcoef = 1, int subdomain = -1);
418 
419  /// Redefine '=' for GridFunction = constant.
420  GridFunction &operator=(double value);
421 
422  /// Copy the data from @a v.
423  /** The size of @a v must be equal to the size of the associated
424  FiniteElementSpace #fes. */
425  GridFunction &operator=(const Vector &v);
426 
427  /// Transform by the Space UpdateMatrix (e.g., on Mesh change).
428  virtual void Update();
429 
431  const FiniteElementSpace *FESpace() const { return fes; }
432 
433  /// Associate a new FiniteElementSpace with the GridFunction.
434  /** The GridFunction is resized using the SetSize() method. */
435  virtual void SetSpace(FiniteElementSpace *f);
436 
437  /** @brief Make the GridFunction reference external data on a new
438  FiniteElementSpace. */
439  /** This method changes the FiniteElementSpace associated with the
440  GridFunction and sets the pointer @a v as external data in the
441  GridFunction. */
442  virtual void MakeRef(FiniteElementSpace *f, double *v);
443 
444  /** @brief Make the GridFunction reference external data on a new
445  FiniteElementSpace. */
446  /** This method changes the FiniteElementSpace associated with the
447  GridFunction and sets the data of the Vector @a v (plus the @a v_offset)
448  as external data in the GridFunction.
449  @note This version of the method will also perform bounds checks when
450  the build option MFEM_DEBUG is enabled. */
451  virtual void MakeRef(FiniteElementSpace *f, Vector &v, int v_offset);
452 
453  /** @brief Associate a new FiniteElementSpace and new true-dof data with the
454  GridFunction. */
455  /** - If the prolongation matrix of @a f is trivial (i.e. its method
456  FiniteElementSpace::GetProlongationMatrix() returns NULL), then the
457  method MakeRef() is called with the same arguments.
458  - Otherwise, the method SetSpace() is called with argument @a f.
459  - The internal true-dof vector is set to reference @a tv. */
460  void MakeTRef(FiniteElementSpace *f, double *tv);
461 
462  /** @brief Associate a new FiniteElementSpace and new true-dof data with the
463  GridFunction. */
464  /** - If the prolongation matrix of @a f is trivial (i.e. its method
465  FiniteElementSpace::GetProlongationMatrix() returns NULL), this method
466  calls MakeRef() with the same arguments.
467  - Otherwise, this method calls SetSpace() with argument @a f.
468  - The internal true-dof vector is set to reference the sub-vector of
469  @a tv starting at the offset @a tv_offset. */
470  void MakeTRef(FiniteElementSpace *f, Vector &tv, int tv_offset);
471 
472  /// Save the GridFunction to an output stream.
473  virtual void Save(std::ostream &out) const;
474 
475  /** Write the GridFunction in VTK format. Note that Mesh::PrintVTK must be
476  called first. The parameter ref > 0 must match the one used in
477  Mesh::PrintVTK. */
478  void SaveVTK(std::ostream &out, const std::string &field_name, int ref);
479 
480  void SaveSTL(std::ostream &out, int TimesToRefine = 1);
481 
482  /// Destroys grid function.
483  virtual ~GridFunction() { Destroy(); }
484 };
485 
486 
487 /** Overload operator<< for std::ostream and GridFunction; valid also for the
488  derived class ParGridFunction */
489 std::ostream &operator<<(std::ostream &out, const GridFunction &sol);
490 
491 
492 /** @brief Class representing a function through its values (scalar or vector)
493  at quadrature points. */
495 {
496 protected:
497  QuadratureSpace *qspace; ///< Associated QuadratureSpace
498  int vdim; ///< Vector dimension
499  bool own_qspace; ///< QuadratureSpace ownership flag
500 
501 public:
502  /// Create an empty QuadratureFunction.
503  /** The object can be initialized later using the SetSpace() methods. */
505  : qspace(NULL), vdim(0), own_qspace(false) { }
506 
507  /** @brief Copy constructor. The QuadratureSpace ownership flag, #own_qspace,
508  in the new object is set to false. */
510  : Vector(orig),
511  qspace(orig.qspace), vdim(orig.vdim), own_qspace(false) { }
512 
513  /// Create a QuadratureFunction based on the given QuadratureSpace.
514  /** The QuadratureFunction does not assume ownership of the QuadratureSpace.
515  @note The Vector data is not initialized. */
516  QuadratureFunction(QuadratureSpace *qspace_, int vdim_ = 1)
517  : Vector(vdim_*qspace_->GetSize()),
518  qspace(qspace_), vdim(vdim_), own_qspace(false) { }
519 
520  /** @brief Create a QuadratureFunction based on the given QuadratureSpace,
521  using the external data, @a qf_data. */
522  /** The QuadratureFunction does not assume ownership of neither the
523  QuadratureSpace nor the external data. */
524  QuadratureFunction(QuadratureSpace *qspace_, double *qf_data, int vdim_ = 1)
525  : Vector(qf_data, vdim_*qspace_->GetSize()),
526  qspace(qspace_), vdim(vdim_), own_qspace(false) { }
527 
528  /// Read a QuadratureFunction from the stream @a in.
529  /** The QuadratureFunction assumes ownership of the read QuadratureSpace. */
530  QuadratureFunction(Mesh *mesh, std::istream &in);
531 
532  virtual ~QuadratureFunction() { if (own_qspace) { delete qspace; } }
533 
534  /// Get the associated QuadratureSpace.
535  QuadratureSpace *GetSpace() const { return qspace; }
536 
537  /// Change the QuadratureSpace and optionally the vector dimension.
538  /** If the new QuadratureSpace is different from the current one, the
539  QuadratureFunction will not assume ownership of the new space; otherwise,
540  the ownership flag remains the same.
541 
542  If the new vector dimension @a vdim_ < 0, the vector dimension remains
543  the same.
544 
545  The data size is updated by calling Vector::SetSize(). */
546  inline void SetSpace(QuadratureSpace *qspace_, int vdim_ = -1);
547 
548  /** @brief Change the QuadratureSpace, the data array, and optionally the
549  vector dimension. */
550  /** If the new QuadratureSpace is different from the current one, the
551  QuadratureFunction will not assume ownership of the new space; otherwise,
552  the ownership flag remains the same.
553 
554  If the new vector dimension @a vdim_ < 0, the vector dimension remains
555  the same.
556 
557  The data array is replaced by calling Vector::NewDataAndSize(). */
558  inline void SetSpace(QuadratureSpace *qspace_, double *qf_data,
559  int vdim_ = -1);
560 
561  /// Get the vector dimension.
562  int GetVDim() const { return vdim; }
563 
564  /// Set the vector dimension, updating the size by calling Vector::SetSize().
565  void SetVDim(int vdim_)
566  { vdim = vdim_; SetSize(vdim*qspace->GetSize()); }
567 
568  /// Get the QuadratureSpace ownership flag.
569  bool OwnsSpace() { return own_qspace; }
570 
571  /// Set the QuadratureSpace ownership flag.
572  void SetOwnsSpace(bool own) { own_qspace = own; }
573 
574  /// Redefine '=' for QuadratureFunction = constant.
575  QuadratureFunction &operator=(double value);
576 
577  /// Copy the data from @a v.
578  /** The size of @a v must be equal to the size of the associated
579  QuadratureSpace #qspace. */
581 
582  /// Copy assignment. Only the data of the base class Vector is copied.
583  /** The QuadratureFunctions @a v and @a *this must have QuadratureSpaces with
584  the same size.
585 
586  @note Defining this method overwrites the implicitly defined copy
587  assignemnt operator. */
589 
590  /// Get the IntegrationRule associated with mesh element @a idx.
591  const IntegrationRule &GetElementIntRule(int idx) const
592  { return qspace->GetElementIntRule(idx); }
593 
594  /// Return all values associated with mesh element @a idx in a Vector.
595  /** The result is stored in the Vector @a values as a reference to the
596  global values.
597 
598  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
599  `i`-th vector component at the `j`-th quadrature point.
600  */
601  inline void GetElementValues(int idx, Vector &values);
602 
603  /// Return all values associated with mesh element @a idx in a Vector.
604  /** The result is stored in the Vector @a values as a copy of the
605  global values.
606 
607  Inside the Vector @a values, the index `i+vdim*j` corresponds to the
608  `i`-th vector component at the `j`-th quadrature point.
609  */
610  inline void GetElementValues(int idx, Vector &values) const;
611 
612  /// Return all values associated with mesh element @a idx in a DenseMatrix.
613  /** The result is stored in the DenseMatrix @a values as a reference to the
614  global values.
615 
616  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
617  `i`-th vector component at the `j`-th quadrature point.
618  */
619  inline void GetElementValues(int idx, DenseMatrix &values);
620 
621  /// Return all values associated with mesh element @a idx in a const DenseMatrix.
622  /** The result is stored in the DenseMatrix @a values as a copy of the
623  global values.
624 
625  Inside the DenseMatrix @a values, the `(i,j)` entry corresponds to the
626  `i`-th vector component at the `j`-th quadrature point.
627  */
628  inline void GetElementValues(int idx, DenseMatrix &values) const;
629 
630  /// Write the QuadratureFunction to the stream @a out.
631  void Save(std::ostream &out) const;
632 };
633 
634 /// Overload operator<< for std::ostream and QuadratureFunction.
635 std::ostream &operator<<(std::ostream &out, const QuadratureFunction &qf);
636 
637 
638 double ZZErrorEstimator(BilinearFormIntegrator &blfi,
639  GridFunction &u,
640  GridFunction &flux,
641  Vector &error_estimates,
642  Array<int> *aniso_flags = NULL,
643  int with_subdomains = 1);
644 
645 /// Compute the Lp distance between two grid functions on the given element.
646 double ComputeElementLpDistance(double p, int i,
647  GridFunction& gf1, GridFunction& gf2);
648 
649 
650 /// Class used for extruding scalar GridFunctions
652 {
653 private:
654  int n;
655  Mesh *mesh_in;
656  Coefficient &sol_in;
657 public:
659  : n(_n), mesh_in(m), sol_in(s) { }
660  virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip);
661  virtual ~ExtrudeCoefficient() { }
662 };
663 
664 /// Extrude a scalar 1D GridFunction, after extruding the mesh with Extrude1D.
665 GridFunction *Extrude1DGridFunction(Mesh *mesh, Mesh *mesh2d,
666  GridFunction *sol, const int ny);
667 
668 
669 // Inline methods
670 
671 inline void QuadratureFunction::SetSpace(QuadratureSpace *qspace_, int vdim_)
672 {
673  if (qspace_ != qspace)
674  {
675  if (own_qspace) { delete qspace; }
676  qspace = qspace_;
677  own_qspace = false;
678  }
679  vdim = (vdim_ < 0) ? vdim : vdim_;
681 }
682 
684  double *qf_data, int vdim_)
685 {
686  if (qspace_ != qspace)
687  {
688  if (own_qspace) { delete qspace; }
689  qspace = qspace_;
690  own_qspace = false;
691  }
692  vdim = (vdim_ < 0) ? vdim : vdim_;
693  NewDataAndSize(qf_data, vdim*qspace->GetSize());
694 }
695 
696 inline void QuadratureFunction::GetElementValues(int idx, Vector &values)
697 {
698  const int s_offset = qspace->element_offsets[idx];
699  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
700  values.NewDataAndSize(data + vdim*s_offset, vdim*sl_size);
701 }
702 
703 inline void QuadratureFunction::GetElementValues(int idx, Vector &values) const
704 {
705  const int s_offset = qspace->element_offsets[idx];
706  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
707  values.SetSize(vdim*sl_size);
708  const double *q = data + vdim*s_offset;
709  for (int i = 0; i<values.Size(); i++)
710  {
711  values(i) = *(q++);
712  }
713 }
714 
716 {
717  const int s_offset = qspace->element_offsets[idx];
718  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
719  values.Reset(data + vdim*s_offset, vdim, sl_size);
720 }
721 
723  DenseMatrix &values) const
724 {
725  const int s_offset = qspace->element_offsets[idx];
726  const int sl_size = qspace->element_offsets[idx+1] - s_offset;
727  values.SetSize(vdim, sl_size);
728  const double *q = data + vdim*s_offset;
729  for (int j = 0; j<sl_size; j++)
730  {
731  for (int i = 0; i<vdim; i++)
732  {
733  values(i,j) = *(q++);
734  }
735  }
736 }
737 
738 } // namespace mfem
739 
740 #endif
void SaveSTLTri(std::ostream &out, double p1[], double p2[], double p3[])
Definition: gridfunc.cpp:2729
int GetVDim() const
Get the vector dimension.
Definition: gridfunc.hpp:562
virtual ~GridFunction()
Destroys grid function.
Definition: gridfunc.hpp:483
std::ostream & operator<<(std::ostream &out, const Mesh &mesh)
Definition: mesh.cpp:9443
QuadratureFunction(QuadratureSpace *qspace_, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpace.
Definition: gridfunc.hpp:516
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:85
void GetElementAverages(GridFunction &avgs) const
Definition: gridfunc.cpp:1058
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:591
Memory< double > data
Definition: vector.hpp:52
Class used for extruding scalar GridFunctions.
Definition: gridfunc.hpp:651
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:306
void ProjectDeltaCoefficient(DeltaCoefficient &delta_coeff, double &integral)
Definition: gridfunc.cpp:1519
const IntegrationRule & GetElementIntRule(int idx) const
Get the IntegrationRule associated with mesh element idx.
Definition: fespace.hpp:660
double ZZErrorEstimator(BilinearFormIntegrator &blfi, GridFunction &u, GridFunction &flux, Vector &error_estimates, Array< int > *aniso_flags, int with_subdomains)
Definition: gridfunc.cpp:2898
void GetVectorValue(int i, const IntegrationPoint &ip, Vector &val) const
Definition: gridfunc.cpp:411
void SetSize(int s)
Resize the vector to size s.
Definition: vector.hpp:400
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:3048
void MakeOwner(FiniteElementCollection *_fec)
Make the GridFunction the owner of fec and fes.
Definition: gridfunc.hpp:112
virtual void ComputeFlux(BilinearFormIntegrator &blfi, GridFunction &flux, int wcoef=1, int subdomain=-1)
Definition: gridfunc.cpp:287
void ProjectDiscCoefficient(VectorCoefficient &coeff, Array< int > &dof_attr)
Definition: gridfunc.cpp:1713
bool own_qspace
QuadratureSpace ownership flag.
Definition: gridfunc.hpp:499
int GetSize() const
Return the total number of quadrature points.
Definition: fespace.hpp:657
int VectorDim() const
Definition: gridfunc.cpp:302
virtual ~QuadratureFunction()
Definition: gridfunc.hpp:532
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:150
void ImposeBounds(int i, const Vector &weights, const Vector &_lo, const Vector &_hi)
Definition: gridfunc.cpp:1132
virtual void ComputeElementL1Errors(Coefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:361
void AccumulateAndCountBdrTangentValues(VectorCoefficient &vcoeff, Array< int > &bdr_attr, Array< int > &values_counter)
Definition: gridfunc.cpp:1445
int vdim
Vector dimension.
Definition: gridfunc.hpp:498
void GetVectorGradientHat(ElementTransformation &T, DenseMatrix &gh) const
Definition: gridfunc.cpp:899
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:330
void SetSpace(QuadratureSpace *qspace_, int vdim_=-1)
Change the QuadratureSpace and optionally the vector dimension.
Definition: gridfunc.hpp:671
void ComputeMeans(AvgType type, Array< int > &zones_per_vdof)
Definition: gridfunc.cpp:1494
bool OwnsSpace()
Get the QuadratureSpace ownership flag.
Definition: gridfunc.hpp:569
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:355
QuadratureFunction(QuadratureSpace *qspace_, double *qf_data, int vdim_=1)
Create a QuadratureFunction based on the given QuadratureSpace, using the external data...
Definition: gridfunc.hpp:524
void GetVectorFieldNodalValues(Vector &val, int comp) const
Definition: gridfunc.cpp:768
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:2627
const FiniteElementSpace * FESpace() const
Definition: gridfunc.hpp:431
virtual double Eval(ElementTransformation &T, const IntegrationPoint &ip)
Evaluate the coefficient in the element described by T at the point ip.
Definition: gridfunc.cpp:3038
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:839
void ProjectBdrCoefficientNormal(VectorCoefficient &vcoeff, Array< int > &bdr_attr)
Definition: gridfunc.cpp:1804
void SetTrueVector()
Shortcut for calling GetTrueDofs() with GetTrueVector() as argument.
Definition: gridfunc.hpp:133
void SumFluxAndCount(BilinearFormIntegrator &blfi, GridFunction &flux, Array< int > &counts, int wcoef, int subdomain)
Definition: gridfunc.cpp:240
virtual double ComputeL1Error(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:336
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:196
void Reset(double *d, int h, int w)
Change the data array and the size of the DenseMatrix.
Definition: densemat.hpp:73
void GetVectorFieldValues(int i, const IntegrationRule &ir, DenseMatrix &vals, DenseMatrix &tr, int comp=0) const
Definition: gridfunc.cpp:699
void SetOwnsSpace(bool own)
Set the QuadratureSpace ownership flag.
Definition: gridfunc.hpp:572
virtual double ComputeH1Error(Coefficient *exsol, VectorCoefficient *exgrad, Coefficient *ell_coef, double Nu, int norm_type) const
Definition: gridfunc.cpp:1994
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:535
virtual double ComputeL1Error(VectorCoefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:344
QuadratureFunction()
Create an empty QuadratureFunction.
Definition: gridfunc.hpp:504
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:1216
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:565
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:445
virtual void ComputeElementMaxErrors(Coefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:373
FiniteElementCollection * OwnFEC()
Definition: gridfunc.hpp:114
virtual ~ExtrudeCoefficient()
Definition: gridfunc.hpp:661
virtual void ComputeElementL2Errors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:403
FiniteElementSpace * FESpace()
Definition: gridfunc.hpp:430
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:2305
QuadratureFunction & operator=(double value)
Redefine &#39;=&#39; for QuadratureFunction = constant.
Definition: gridfunc.cpp:2864
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:398
Abstract base class BilinearFormIntegrator.
Definition: bilininteg.hpp:23
virtual void ComputeElementL1Errors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:397
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:85
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:1088
QuadratureSpace * qspace
Associated QuadratureSpace.
Definition: gridfunc.hpp:497
virtual double ComputeMaxError(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:321
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:1046
double GetDivergence(ElementTransformation &tr) const
Definition: gridfunc.cpp:918
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:2981
int GetFaceVectorValues(int i, int side, const IntegrationRule &ir, DenseMatrix &vals, DenseMatrix &tr) const
Definition: gridfunc.cpp:582
Class for integration point with weight.
Definition: intrules.hpp:25
void SaveSTL(std::ostream &out, int TimesToRefine=1)
Definition: gridfunc.cpp:2749
int GetFaceValues(int i, int side, const IntegrationRule &ir, Vector &vals, DenseMatrix &tr, int vdim=1) const
Definition: gridfunc.cpp:478
virtual void ComputeElementLpErrors(const double p, Coefficient &exsol, GridFunction &error, Coefficient *weight=NULL, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.cpp:2370
void GetElementValues(int idx, Vector &values)
Return all values associated with mesh element idx in a Vector.
Definition: gridfunc.hpp:696
void GetGradients(ElementTransformation &tr, const IntegrationRule &ir, DenseMatrix &grad) const
Definition: gridfunc.cpp:1021
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:1875
void GetVectorValues(ElementTransformation &T, const IntegrationRule &ir, DenseMatrix &vals) const
Definition: gridfunc.cpp:528
QuadratureFunction(const QuadratureFunction &orig)
Copy constructor. The QuadratureSpace ownership flag, own_qspace, in the new object is set to false...
Definition: gridfunc.hpp:509
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:1582
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:741
virtual double ComputeW11Error(Coefficient *exsol, VectorCoefficient *exgrad, int norm_type, Array< int > *elems=NULL, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.cpp:2199
ExtrudeCoefficient(Mesh *m, Coefficient &s, int _n)
Definition: gridfunc.hpp:658
void GetCurl(ElementTransformation &tr, Vector &curl) const
Definition: gridfunc.cpp:952
Vector data type.
Definition: vector.hpp:48
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:628
void SetSize(int s)
Change the size of the DenseMatrix to s x s.
Definition: densemat.hpp:88
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:64
long GetSequence() const
Return update counter (see Mesh::sequence)
Definition: fespace.hpp:614
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:367
virtual void ComputeElementMaxErrors(VectorCoefficient &exsol, GridFunction &error, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:409
Class representing a function through its values (scalar or vector) at quadrature points...
Definition: gridfunc.hpp:494
void GetValuesFrom(const GridFunction &orig_func)
Definition: gridfunc.cpp:625
void GetBdrValuesFrom(const GridFunction &orig_func)
Definition: gridfunc.cpp:662
void AccumulateAndCountBdrValues(Coefficient *coeff[], VectorCoefficient *vcoeff, Array< int > &attr, Array< int > &values_counter)
Definition: gridfunc.cpp:1306
void Save(std::ostream &out) const
Write the QuadratureFunction to the stream out.
Definition: gridfunc.cpp:2882
void SaveVTK(std::ostream &out, const std::string &field_name, int ref)
Definition: gridfunc.cpp:2652
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:275
virtual double GetValue(int i, const IntegrationPoint &ip, int vdim=1) const
Definition: gridfunc.cpp:396
void GetGradient(ElementTransformation &tr, Vector &grad) const
Definition: gridfunc.cpp:1003
void ProjectVectorFieldOn(GridFunction &vec_field, int comp=0)
Definition: gridfunc.cpp:799