MFEM  v4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
estimators.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_ERROR_ESTIMATORS
13 #define MFEM_ERROR_ESTIMATORS
14 
15 #include "../config/config.hpp"
16 #include "../linalg/vector.hpp"
17 #include "bilinearform.hpp"
18 #ifdef MFEM_USE_MPI
19 #include "pgridfunc.hpp"
20 #endif
21 
22 namespace mfem
23 {
24 
25 /** @brief Base class for all error estimators.
26  */
28 {
29 public:
31 };
32 
33 
34 /** @brief Base class for all element based error estimators.
35 
36  At a minimum, an ErrorEstimator must be able compute one non-negative real
37  (double) number for each element in the Mesh.
38  */
40 {
41 public:
42  /// Get a Vector with all element errors.
43  virtual const Vector &GetLocalErrors() = 0;
44 
45  /// Force recomputation of the estimates on the next call to GetLocalErrors.
46  virtual void Reset() = 0;
47 
48  virtual ~ErrorEstimator() { }
49 };
50 
51 
52 /** @brief The AnisotropicErrorEstimator class is the base class for all error
53  estimators that compute one non-negative real (double) number and an
54  anisotropic flag for every element in the Mesh.
55  */
57 {
58 public:
59  /** @brief Get an Array<int> with anisotropic flags for all mesh elements.
60  @return An empty array when anisotropic estimates are not available or
61  enabled. */
62  virtual const Array<int> &GetAnisotropicFlags() = 0;
63 };
64 
65 
66 /** @brief The ZienkiewiczZhuEstimator class implements the Zienkiewicz-Zhu
67  error estimation procedure.
68 
69  The required BilinearFormIntegrator must implement the methods
70  ComputeElementFlux() and ComputeFluxEnergy().
71  */
73 {
74 protected:
77  double total_error;
80  int flux_averaging; // see SetFluxAveraging()
81 
82  BilinearFormIntegrator *integ; ///< Not owned.
83  GridFunction *solution; ///< Not owned.
84 
85  FiniteElementSpace *flux_space; /**< @brief Ownership based on own_flux_fes.
86  Its Update() method is called automatically by this class when needed. */
87  bool own_flux_fes; ///< Ownership flag for flux_space.
88 
89  /// Check if the mesh of the solution was modified.
91  {
92  long mesh_sequence = solution->FESpace()->GetMesh()->GetSequence();
93  MFEM_ASSERT(mesh_sequence >= current_sequence, "");
94  return (mesh_sequence > current_sequence);
95  }
96 
97  /// Compute the element error estimates.
98  void ComputeEstimates();
99 
100 public:
101  /** @brief Construct a new ZienkiewiczZhuEstimator object.
102  @param integ This BilinearFormIntegrator must implement the methods
103  ComputeElementFlux() and ComputeFluxEnergy().
104  @param sol The solution field whose error is to be estimated.
105  @param flux_fes The ZienkiewiczZhuEstimator assumes ownership of this
106  FiniteElementSpace and will call its Update() method when
107  needed. */
109  FiniteElementSpace *flux_fes)
110  : current_sequence(-1),
111  total_error(),
112  anisotropic(false),
113  flux_averaging(0),
114  integ(&integ),
115  solution(&sol),
116  flux_space(flux_fes),
117  own_flux_fes(true)
118  { }
119 
120  /** @brief Construct a new ZienkiewiczZhuEstimator object.
121  @param integ This BilinearFormIntegrator must implement the methods
122  ComputeElementFlux() and ComputeFluxEnergy().
123  @param sol The solution field whose error is to be estimated.
124  @param flux_fes The ZienkiewiczZhuEstimator does NOT assume ownership of
125  this FiniteElementSpace; will call its Update() method
126  when needed. */
128  FiniteElementSpace &flux_fes)
129  : current_sequence(-1),
130  total_error(),
131  anisotropic(false),
132  flux_averaging(0),
133  integ(&integ),
134  solution(&sol),
135  flux_space(&flux_fes),
136  own_flux_fes(false)
137  { }
138 
139  /** @brief Enable/disable anisotropic estimates. To enable this option, the
140  BilinearFormIntegrator must support the 'd_energy' parameter in its
141  ComputeFluxEnergy() method. */
142  void SetAnisotropic(bool aniso = true) { anisotropic = aniso; }
143 
144  /** @brief Set the way the flux is averaged (smoothed) across elements.
145 
146  When @a fa is zero (default), averaging is performed globally. When @a fa
147  is non-zero, the flux averaging is performed locally for each mesh
148  attribute, i.e. the flux is not averaged across interfaces between
149  different mesh attributes. */
150  void SetFluxAveraging(int fa) { flux_averaging = fa; }
151 
152  /// Return the total error from the last error estimate.
153  double GetTotalError() const { return total_error; }
154 
155  /// Get a Vector with all element errors.
156  virtual const Vector &GetLocalErrors()
157  {
158  if (MeshIsModified()) { ComputeEstimates(); }
159  return error_estimates;
160  }
161 
162  /** @brief Get an Array<int> with anisotropic flags for all mesh elements.
163  Return an empty array when anisotropic estimates are not available or
164  enabled. */
166  {
167  if (MeshIsModified()) { ComputeEstimates(); }
168  return aniso_flags;
169  }
170 
171  /// Reset the error estimator.
172  virtual void Reset() { current_sequence = -1; }
173 
174  /** @brief Destroy a ZienkiewiczZhuEstimator object. Destroys, if owned, the
175  FiniteElementSpace, flux_space. */
177  {
178  if (own_flux_fes) { delete flux_space; }
179  }
180 };
181 
182 
183 #ifdef MFEM_USE_MPI
184 
185 /** @brief The L2ZienkiewiczZhuEstimator class implements the Zienkiewicz-Zhu
186  error estimation procedure where the flux averaging is replaced by a global
187  L2 projection (requiring a mass matrix solve).
188 
189  The required BilinearFormIntegrator must implement the methods
190  ComputeElementFlux() and ComputeFluxEnergy().
191 
192  Implemented for the parallel case only.
193  */
195 {
196 protected:
198  int local_norm_p; ///< Local L_p norm to use, default is 1.
200  double total_error;
201 
202  BilinearFormIntegrator *integ; ///< Not owned.
203  ParGridFunction *solution; ///< Not owned.
204 
205  ParFiniteElementSpace *flux_space; /**< @brief Ownership based on the flag
206  own_flux_fes. Its Update() method is called automatically by this class
207  when needed. */
208  ParFiniteElementSpace *smooth_flux_space; /**< @brief Ownership based on the
209  flag own_flux_fes. Its Update() method is called automatically by this
210  class when needed.*/
211  bool own_flux_fes; ///< Ownership flag for flux_space and smooth_flux_space.
212 
214  ParGridFunction &sol,
215  ParFiniteElementSpace *flux_fes,
216  ParFiniteElementSpace *smooth_flux_fes)
217  {
218  current_sequence = -1;
219  local_norm_p = 1;
220  total_error = 0.0;
221  this->integ = &integ;
222  solution = &sol;
223  flux_space = flux_fes;
224  smooth_flux_space = smooth_flux_fes;
225  }
226 
227  /// Check if the mesh of the solution was modified.
229  {
230  long mesh_sequence = solution->FESpace()->GetMesh()->GetSequence();
231  MFEM_ASSERT(mesh_sequence >= current_sequence, "");
232  return (mesh_sequence > current_sequence);
233  }
234 
235  /// Compute the element error estimates.
236  void ComputeEstimates();
237 
238 public:
239  /** @brief Construct a new L2ZienkiewiczZhuEstimator object.
240  @param integ This BilinearFormIntegrator must implement the methods
241  ComputeElementFlux() and ComputeFluxEnergy().
242  @param sol The solution field whose error is to be estimated.
243  @param flux_fes The L2ZienkiewiczZhuEstimator assumes ownership of this
244  FiniteElementSpace and will call its Update() method when
245  needed.
246  @param smooth_flux_fes
247  The L2ZienkiewiczZhuEstimator assumes ownership of this
248  FiniteElementSpace and will call its Update() method when
249  needed. */
251  ParGridFunction &sol,
252  ParFiniteElementSpace *flux_fes,
253  ParFiniteElementSpace *smooth_flux_fes)
254  { Init(integ, sol, flux_fes, smooth_flux_fes); own_flux_fes = true; }
255 
256  /** @brief Construct a new L2ZienkiewiczZhuEstimator object.
257  @param integ This BilinearFormIntegrator must implement the methods
258  ComputeElementFlux() and ComputeFluxEnergy().
259  @param sol The solution field whose error is to be estimated.
260  @param flux_fes The L2ZienkiewiczZhuEstimator does NOT assume ownership
261  of this FiniteElementSpace; will call its Update() method
262  when needed.
263  @param smooth_flux_fes
264  The L2ZienkiewiczZhuEstimator does NOT assume ownership
265  of this FiniteElementSpace; will call its Update() method
266  when needed. */
268  ParGridFunction &sol,
269  ParFiniteElementSpace &flux_fes,
270  ParFiniteElementSpace &smooth_flux_fes)
271  { Init(integ, sol, &flux_fes, &smooth_flux_fes); own_flux_fes = false; }
272 
273  /** @brief Set the exponent, p, of the Lp norm used for computing the local
274  element errors. Default value is 1. */
275  void SetLocalErrorNormP(int p) { local_norm_p = p; }
276 
277  /// Return the total error from the last error estimate.
278  double GetTotalError() const { return total_error; }
279 
280  /// Get a Vector with all element errors.
281  virtual const Vector &GetLocalErrors()
282  {
283  if (MeshIsModified()) { ComputeEstimates(); }
284  return error_estimates;
285  }
286 
287  /// Reset the error estimator.
288  virtual void Reset() { current_sequence = -1; }
289 
290  /** @brief Destroy a L2ZienkiewiczZhuEstimator object. Destroys, if owned,
291  the FiniteElementSpace, flux_space. */
293  {
294  if (own_flux_fes) { delete flux_space; delete smooth_flux_space; }
295  }
296 };
297 
298 #endif // MFEM_USE_MPI
299 
300 } // namespace mfem
301 
302 #endif // MFEM_ERROR_ESTIMATORS
virtual ~L2ZienkiewiczZhuEstimator()
Destroy a L2ZienkiewiczZhuEstimator object. Destroys, if owned, the FiniteElementSpace, flux_space.
Definition: estimators.hpp:292
void SetAnisotropic(bool aniso=true)
Enable/disable anisotropic estimates. To enable this option, the BilinearFormIntegrator must support ...
Definition: estimators.hpp:142
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:27
L2ZienkiewiczZhuEstimator(BilinearFormIntegrator &integ, ParGridFunction &sol, ParFiniteElementSpace *flux_fes, ParFiniteElementSpace *smooth_flux_fes)
Construct a new L2ZienkiewiczZhuEstimator object.
Definition: estimators.hpp:250
void ComputeEstimates()
Compute the element error estimates.
Definition: estimators.cpp:35
ParFiniteElementSpace * flux_space
Ownership based on the flag own_flux_fes. Its Update() method is called automatically by this class w...
Definition: estimators.hpp:205
bool MeshIsModified()
Check if the mesh of the solution was modified.
Definition: estimators.hpp:228
virtual const Vector & GetLocalErrors()
Get a Vector with all element errors.
Definition: estimators.hpp:281
void SetLocalErrorNormP(int p)
Set the exponent, p, of the Lp norm used for computing the local element errors. Default value is 1...
Definition: estimators.hpp:275
Abstract parallel finite element space.
Definition: pfespace.hpp:28
virtual void Reset()
Reset the error estimator.
Definition: estimators.hpp:288
The L2ZienkiewiczZhuEstimator class implements the Zienkiewicz-Zhu error estimation procedure where t...
Definition: estimators.hpp:194
virtual ~ErrorEstimator()
Definition: estimators.hpp:48
void ComputeEstimates()
Compute the element error estimates.
Definition: estimators.cpp:17
ParGridFunction * solution
Not owned.
Definition: estimators.hpp:203
void Init(BilinearFormIntegrator &integ, ParGridFunction &sol, ParFiniteElementSpace *flux_fes, ParFiniteElementSpace *smooth_flux_fes)
Definition: estimators.hpp:213
long GetSequence() const
Definition: mesh.hpp:1149
bool own_flux_fes
Ownership flag for flux_space and smooth_flux_space.
Definition: estimators.hpp:211
virtual const Vector & GetLocalErrors()=0
Get a Vector with all element errors.
Mesh * GetMesh() const
Returns the mesh.
Definition: fespace.hpp:272
Base class for all error estimators.
Definition: estimators.hpp:27
GridFunction * solution
Not owned.
Definition: estimators.hpp:83
double GetTotalError() const
Return the total error from the last error estimate.
Definition: estimators.hpp:153
int local_norm_p
Local L_p norm to use, default is 1.
Definition: estimators.hpp:198
void SetFluxAveraging(int fa)
Set the way the flux is averaged (smoothed) across elements.
Definition: estimators.hpp:150
Base class for all element based error estimators.
Definition: estimators.hpp:39
L2ZienkiewiczZhuEstimator(BilinearFormIntegrator &integ, ParGridFunction &sol, ParFiniteElementSpace &flux_fes, ParFiniteElementSpace &smooth_flux_fes)
Construct a new L2ZienkiewiczZhuEstimator object.
Definition: estimators.hpp:267
FiniteElementSpace * FESpace()
Definition: gridfunc.hpp:430
virtual const Array< int > & GetAnisotropicFlags()=0
Get an Array&lt;int&gt; with anisotropic flags for all mesh elements.
virtual void Reset()=0
Force recomputation of the estimates on the next call to GetLocalErrors.
Abstract base class BilinearFormIntegrator.
Definition: bilininteg.hpp:23
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:85
ParFiniteElementSpace * smooth_flux_space
Ownership based on the flag own_flux_fes. Its Update() method is called automatically by this class w...
Definition: estimators.hpp:208
double GetTotalError() const
Return the total error from the last error estimate.
Definition: estimators.hpp:278
virtual void Reset()
Reset the error estimator.
Definition: estimators.hpp:172
FiniteElementSpace * flux_space
Ownership based on own_flux_fes. Its Update() method is called automatically by this class when neede...
Definition: estimators.hpp:85
virtual const Array< int > & GetAnisotropicFlags()
Get an Array&lt;int&gt; with anisotropic flags for all mesh elements. Return an empty array when anisotropi...
Definition: estimators.hpp:165
The AnisotropicErrorEstimator class is the base class for all error estimators that compute one non-n...
Definition: estimators.hpp:56
BilinearFormIntegrator * integ
Not owned.
Definition: estimators.hpp:202
ZienkiewiczZhuEstimator(BilinearFormIntegrator &integ, GridFunction &sol, FiniteElementSpace *flux_fes)
Construct a new ZienkiewiczZhuEstimator object.
Definition: estimators.hpp:108
The ZienkiewiczZhuEstimator class implements the Zienkiewicz-Zhu error estimation procedure...
Definition: estimators.hpp:72
Vector data type.
Definition: vector.hpp:48
virtual const Vector & GetLocalErrors()
Get a Vector with all element errors.
Definition: estimators.hpp:156
bool MeshIsModified()
Check if the mesh of the solution was modified.
Definition: estimators.hpp:90
virtual ~ZienkiewiczZhuEstimator()
Destroy a ZienkiewiczZhuEstimator object. Destroys, if owned, the FiniteElementSpace, flux_space.
Definition: estimators.hpp:176
ZienkiewiczZhuEstimator(BilinearFormIntegrator &integ, GridFunction &sol, FiniteElementSpace &flux_fes)
Construct a new ZienkiewiczZhuEstimator object.
Definition: estimators.hpp:127
Class for parallel grid function.
Definition: pgridfunc.hpp:32
BilinearFormIntegrator * integ
Not owned.
Definition: estimators.hpp:82
bool own_flux_fes
Ownership flag for flux_space.
Definition: estimators.hpp:87