libMesh
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
libMesh::ErrorVector Class Reference

The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite element mesh. More...

#include <error_vector.h>

Inheritance diagram for libMesh::ErrorVector:
[legend]

Public Member Functions

 ErrorVector (dof_id_type i=0, MeshBase *mesh=libmesh_nullptr)
 ErrorVector constructor; sets initial length to i. More...
 
 ErrorVector (dof_id_type i, ErrorVectorReal val)
 ErrorVector constructor; sets initial length to i and initial values to val. More...
 
virtual ErrorVectorReal minimum () const libmesh_override
 
virtual Real mean () const libmesh_override
 
virtual Real median () libmesh_override
 
virtual Real median () const libmesh_override
 A const version of the median function. More...
 
virtual Real variance () const libmesh_override
 
virtual Real variance (const Real mean) const libmesh_override
 
virtual std::vector< dof_id_typecut_below (Real cut) const libmesh_override
 
virtual std::vector< dof_id_typecut_above (Real cut) const libmesh_override
 
void plot_error (const std::string &filename, const MeshBase &mesh) const
 Plots a data file, of a type determined by looking at the file extension in filename, of the error values on the active elements of mesh. More...
 
virtual Real l2_norm () const
 
virtual ErrorVectorReal maximum () const
 
virtual Real stddev () const
 
virtual Real stddev (const Real known_mean) const
 
void normalize ()
 Divides all entries by the largest entry and stores the result. More...
 
virtual void histogram (std::vector< dof_id_type > &bin_members, unsigned int n_bins=10)
 
virtual void histogram (std::vector< dof_id_type > &bin_members, unsigned int n_bins=10) const
 A const version of the histogram function. More...
 
void plot_histogram (const processor_id_type my_procid, const std::string &filename, unsigned int n_bins)
 Generates a Matlab/Octave style file which can be used to make a plot of the histogram having the desired number of bins. More...
 

Protected Member Functions

bool is_active_elem (dof_id_type i) const
 Utility function to decide whether element i is active. More...
 

Protected Attributes

MeshBase_mesh
 Pointer to the mesh, which may be used to decide which elements are active. More...
 

Detailed Description

The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite element mesh.

In general, when computing the error on a mesh only the active elements are considered, but the ErrorVector is sized according to the total number of elements in the mesh. The ErrorVector is thus padded with zeros for all the inactive elements, and this must be taken into account when calculating the statistics. Since the error is a positive quantity this class assumes it contains positive data (i.e. min_val >= 0.).

Author
Benjamin S. Kirk
Date
2003

Definition at line 50 of file error_vector.h.

Constructor & Destructor Documentation

libMesh::ErrorVector::ErrorVector ( dof_id_type  i = 0,
MeshBase mesh = libmesh_nullptr 
)

ErrorVector constructor; sets initial length to i.

If mesh is not null, MeshBase::elem() and Elem::is_active() will be used to distinguish active and inactive elements. If mesh is null, ErrorVector will assume that all 0.0 error values correspond to inactive elements and all non-zero error values correspond to active elements.

Definition at line 62 of file error_vector.h.

62  :
63  StatisticsVector<ErrorVectorReal> (i),
64  _mesh(mesh)
65  {}
MeshBase * _mesh
Pointer to the mesh, which may be used to decide which elements are active.
Definition: error_vector.h:163
MeshBase & mesh
libMesh::ErrorVector::ErrorVector ( dof_id_type  i,
ErrorVectorReal  val 
)

ErrorVector constructor; sets initial length to i and initial values to val.

If mesh is not null, MeshBase::elem() and Elem::is_active() will be used to distinguish active and inactive elements. If mesh is null, ErrorVector will assume that all 0.0 error values correspond to inactive elements and all non-zero error values correspond to active elements.

Definition at line 75 of file error_vector.h.

References libMesh::ErrorVectorReal, mean(), median(), minimum(), and libMesh::Real.

75  :
76  StatisticsVector<ErrorVectorReal> (i,val) {}

Member Function Documentation

std::vector< dof_id_type > libMesh::ErrorVector::cut_above ( Real  cut) const
virtual
Returns
A vector of dof_id_types which correspond to the indices of every member of the data set above the cutoff value cut ignoring inactive elements.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 172 of file error_vector.C.

References is_active_elem().

Referenced by variance().

173 {
174  LOG_SCOPE ("cut_above()", "ErrorVector");
175 
176  const dof_id_type n = cast_int<dof_id_type>(this->size());
177 
178  std::vector<dof_id_type> cut_indices;
179  cut_indices.reserve(n/2); // Arbitrary
180 
181  for (dof_id_type i=0; i<n; i++)
182  if (this->is_active_elem(i))
183  {
184  if ((*this)[i] > cut)
185  {
186  cut_indices.push_back(i);
187  }
188  }
189 
190  return cut_indices;
191 }
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
uint8_t dof_id_type
Definition: id_types.h:64
std::vector< dof_id_type > libMesh::ErrorVector::cut_below ( Real  cut) const
virtual
Returns
A vector of dof_id_types which correspond to the indices of every member of the data set below the cutoff value cut ignoring inactive elements.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 148 of file error_vector.C.

References is_active_elem().

Referenced by variance().

149 {
150  LOG_SCOPE ("cut_below()", "ErrorVector");
151 
152  const dof_id_type n = cast_int<dof_id_type>(this->size());
153 
154  std::vector<dof_id_type> cut_indices;
155  cut_indices.reserve(n/2); // Arbitrary
156 
157  for (dof_id_type i=0; i<n; i++)
158  if (this->is_active_elem(i))
159  {
160  if ((*this)[i] < cut)
161  {
162  cut_indices.push_back(i);
163  }
164  }
165 
166  return cut_indices;
167 }
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
uint8_t dof_id_type
Definition: id_types.h:64
virtual void libMesh::StatisticsVector< ErrorVectorReal >::histogram ( std::vector< dof_id_type > &  bin_members,
unsigned int  n_bins = 10 
)
virtualinherited
Returns
A histogram with n_bins bins for the data set.

For simplicity, the bins are assumed to be of uniform size. Upon return, the bin_members vector will contain unsigned integers which give the number of members in each bin. WARNING: This non-const function sorts the vector, changing its order. Source: GNU Scientific Library.

virtual void libMesh::StatisticsVector< ErrorVectorReal >::histogram ( std::vector< dof_id_type > &  bin_members,
unsigned int  n_bins = 10 
) const
virtualinherited

A const version of the histogram function.

bool libMesh::ErrorVector::is_active_elem ( dof_id_type  i) const
protected

Utility function to decide whether element i is active.

Definition at line 195 of file error_vector.C.

References _mesh, libMesh::Elem::active(), and libMesh::MeshBase::elem_ptr().

Referenced by cut_above(), cut_below(), mean(), median(), minimum(), and variance().

196 {
197  libmesh_assert_less (i, this->size());
198 
199  if (_mesh)
200  {
201  return _mesh->elem_ptr(i)->active();
202  }
203  else
204  return ((*this)[i] != 0.);
205 }
bool active() const
Definition: elem.h:2257
MeshBase * _mesh
Pointer to the mesh, which may be used to decide which elements are active.
Definition: error_vector.h:163
virtual const Elem * elem_ptr(const dof_id_type i) const =0
virtual Real libMesh::StatisticsVector< ErrorVectorReal >::l2_norm ( ) const
virtualinherited
Returns
The l2 norm of the data set.
virtual ErrorVectorReal libMesh::StatisticsVector< ErrorVectorReal >::maximum ( ) const
virtualinherited
Returns
The maximum value in the data set.
Real libMesh::ErrorVector::mean ( ) const
virtual
Returns
The mean value of the data set. Ignores zero values.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 67 of file error_vector.C.

References is_active_elem(), and libMesh::Real.

Referenced by ErrorVector(), libMesh::MeshRefinement::flag_elements_by_mean_stddev(), main(), and variance().

68 {
69  LOG_SCOPE ("mean()", "ErrorVector");
70 
71  const dof_id_type n = cast_int<dof_id_type>(this->size());
72 
73  Real the_mean = 0;
74  dof_id_type nnz = 0;
75 
76  for (dof_id_type i=0; i<n; i++)
77  if (this->is_active_elem(i))
78  {
79  the_mean += ( static_cast<Real>((*this)[i]) - the_mean ) / (nnz + 1);
80 
81  nnz++;
82  }
83 
84  return the_mean;
85 }
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t dof_id_type
Definition: id_types.h:64
Real libMesh::ErrorVector::median ( )
virtual
Returns
The median (e.g. the middle) value of the data set, ignoring inactive elements.

This function modifies the original data by sorting, so it can't be called on const objects. Source: GNU Scientific Library

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 90 of file error_vector.C.

References is_active_elem(), and libMesh::StatisticsVector< T >::median().

Referenced by ErrorVector(), and median().

91 {
92  const dof_id_type n = cast_int<dof_id_type>(this->size());
93 
94  if (n == 0)
95  return 0.;
96 
97 
98  // Build a StatisticsVector<ErrorVectorReal> containing
99  // only our active entries and take its mean
100  StatisticsVector<ErrorVectorReal> sv;
101 
102  sv.reserve (n);
103 
104  for (dof_id_type i=0; i<n; i++)
105  if (this->is_active_elem(i))
106  sv.push_back((*this)[i]);
107 
108  return sv.median();
109 }
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
uint8_t dof_id_type
Definition: id_types.h:64
Real libMesh::ErrorVector::median ( ) const
virtual

A const version of the median function.

Requires twice the memory of original data set but does not change the original.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 114 of file error_vector.C.

References median().

115 {
116  ErrorVector ev = (*this);
117 
118  return ev.median();
119 }
ErrorVector(dof_id_type i=0, MeshBase *mesh=libmesh_nullptr)
ErrorVector constructor; sets initial length to i.
Definition: error_vector.h:62
ErrorVectorReal libMesh::ErrorVector::minimum ( ) const
virtual
Returns
The minimum nonzero value in the data set.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 44 of file error_vector.C.

References libMesh::ErrorVectorReal, is_active_elem(), std::max(), and std::min().

Referenced by ErrorVector().

45 {
46  LOG_SCOPE ("minimum()", "ErrorVector");
47 
48  const dof_id_type n = cast_int<dof_id_type>(this->size());
50 
51  for (dof_id_type i=0; i<n; i++)
52  {
53  // Only positive (or zero) values in the error vector
54  libmesh_assert_greater_equal ((*this)[i], 0.);
55  if (this->is_active_elem(i))
56  min = std::min (min, (*this)[i]);
57  }
58 
59  // ErrorVectors are for positive values
60  libmesh_assert_greater_equal (min, 0.);
61 
62  return min;
63 }
long double max(long double a, double b)
DIE A HORRIBLE DEATH HERE typedef float ErrorVectorReal
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
long double min(long double a, double b)
uint8_t dof_id_type
Definition: id_types.h:64
void libMesh::StatisticsVector< ErrorVectorReal >::normalize ( )
inherited

Divides all entries by the largest entry and stores the result.

void libMesh::ErrorVector::plot_error ( const std::string &  filename,
const MeshBase mesh 
) const

Plots a data file, of a type determined by looking at the file extension in filename, of the error values on the active elements of mesh.

Definition at line 208 of file error_vector.C.

References libMesh::MeshBase::active_local_element_ptr_range(), libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::MeshBase::all_first_order(), libMesh::MeshBase::allow_renumbering(), libMesh::MeshBase::clone(), libMesh::CONSTANT, libMesh::Elem::DO_NOTHING, libMesh::DofMap::dof_indices(), libMesh::MeshBase::element_ptr_range(), libMesh::ENCODE, libMesh::err, libMesh::System::get_dof_map(), libMesh::EquationSystems::init(), libMesh::MeshBase::max_elem_id(), libMesh::MeshBase::max_node_id(), mesh, libMesh::MONOMIAL, libMesh::MeshBase::n_elem(), libMesh::MeshBase::n_nodes(), libMesh::MeshBase::renumber_nodes_and_elements(), libMesh::System::solution, libMesh::WRITE, libMesh::ExodusII_IO::write(), libMesh::XdrIO::write(), libMesh::EquationSystems::write(), libMesh::EquationSystems::WRITE_ADDITIONAL_DATA, libMesh::EquationSystems::WRITE_DATA, libMesh::GMVIO::write_discontinuous_gmv(), libMesh::ExodusII_IO::write_element_data(), and libMesh::MeshOutput< MT >::write_equation_systems().

Referenced by libMesh::AdjointResidualErrorEstimator::estimate_error(), main(), and variance().

210 {
211  UniquePtr<MeshBase> meshptr = oldmesh.clone();
212  MeshBase & mesh = *meshptr;
213 
214  // The all_first_order routine will prepare_for_use(), which would
215  // break our ordering if elements get changed.
216  mesh.allow_renumbering(false);
217  mesh.all_first_order();
218 
219 #ifdef LIBMESH_ENABLE_AMR
220  // We don't want p elevation when plotting a single constant value
221  // per element
222  for (auto & elem : mesh.element_ptr_range())
223  {
224  elem->set_p_refinement_flag(Elem::DO_NOTHING);
225  elem->set_p_level(0);
226  }
227 #endif // LIBMESH_ENABLE_AMR
228 
229  EquationSystems temp_es (mesh);
230  ExplicitSystem & error_system
231  = temp_es.add_system<ExplicitSystem> ("Error");
232  error_system.add_variable("error", CONSTANT, MONOMIAL);
233  temp_es.init();
234 
235  const DofMap & error_dof_map = error_system.get_dof_map();
236  std::vector<dof_id_type> dof_indices;
237 
238  for (const auto & elem : mesh.active_local_element_ptr_range())
239  {
240  error_dof_map.dof_indices(elem, dof_indices);
241 
242  const dof_id_type elem_id = elem->id();
243 
244  //0 for the monomial basis
245  const dof_id_type solution_index = dof_indices[0];
246 
247  // libMesh::out << "elem_number=" << elem_number << std::endl;
248  libmesh_assert_less (elem_id, (*this).size());
249 
250  // We may have zero error values in special circumstances
251  // libmesh_assert_greater ((*this)[elem_id], 0.);
252  error_system.solution->set(solution_index, (*this)[elem_id]);
253  }
254 
255  error_system.solution->close();
256 
257  // We may have to renumber if the original numbering was not
258  // contiguous. Since this is just a temporary mesh, that's probably
259  // fine.
260  if (mesh.max_elem_id() != mesh.n_elem() ||
261  mesh.max_node_id() != mesh.n_nodes())
262  {
263  mesh.allow_renumbering(true);
264  mesh.renumber_nodes_and_elements();
265  }
266 
267  if (filename.rfind(".gmv") < filename.size())
268  {
269  GMVIO(mesh).write_discontinuous_gmv(filename,
270  temp_es, false);
271  }
272  else if (filename.rfind(".plt") < filename.size())
273  {
274  TecplotIO (mesh).write_equation_systems
275  (filename, temp_es);
276  }
277 #ifdef LIBMESH_HAVE_EXODUS_API
278  else if ((filename.rfind(".exo") < filename.size()) ||
279  (filename.rfind(".e") < filename.size()))
280  {
281  ExodusII_IO io(mesh);
282  io.write(filename);
283  io.write_element_data(temp_es);
284  }
285 #endif
286  else if (filename.rfind(".xda") < filename.size())
287  {
288  XdrIO(mesh).write("mesh-"+filename);
289  temp_es.write("soln-"+filename,WRITE,
292  }
293  else if (filename.rfind(".xdr") < filename.size())
294  {
295  XdrIO(mesh,true).write("mesh-"+filename);
296  temp_es.write("soln-"+filename,ENCODE,
299  }
300  else
301  {
302  libmesh_here();
303  libMesh::err << "Warning: ErrorVector::plot_error currently only"
304  << " supports .gmv, .plt, .xdr/.xda, and .exo/.e (if enabled) output;" << std::endl;
305  libMesh::err << "Could not recognize filename: " << filename
306  << std::endl;
307  }
308 }
OStreamProxy err
MeshBase & mesh
uint8_t dof_id_type
Definition: id_types.h:64
void libMesh::StatisticsVector< ErrorVectorReal >::plot_histogram ( const processor_id_type  my_procid,
const std::string &  filename,
unsigned int  n_bins 
)
inherited

Generates a Matlab/Octave style file which can be used to make a plot of the histogram having the desired number of bins.

Uses the histogram(...) function in this class WARNING: The histogram(...) function is non-const, and changes the order of the vector.

virtual Real libMesh::StatisticsVector< ErrorVectorReal >::stddev ( ) const
virtualinherited
Returns
The standard deviation of the data set, which is simply the square-root of the variance.

Definition at line 154 of file statistics.h.

References libMesh::StatisticsVector< T >::variance().

155  { return std::sqrt(this->variance()); }
virtual Real libMesh::StatisticsVector< ErrorVectorReal >::stddev ( const Real  known_mean) const
virtualinherited
Returns
Computes the standard deviation of the data set, which is simply the square-root of the variance.

This method can be used for efficiency when the mean has already been computed.

Definition at line 164 of file statistics.h.

References libMesh::StatisticsVector< T >::cut_above(), libMesh::StatisticsVector< T >::cut_below(), libMesh::StatisticsVector< T >::histogram(), libMesh::StatisticsVector< T >::normalize(), libMesh::StatisticsVector< T >::plot_histogram(), libMesh::Real, and libMesh::StatisticsVector< T >::variance().

165  { return std::sqrt(this->variance(known_mean)); }
virtual Real libMesh::ErrorVector::variance ( ) const
virtual
Returns
The variance of the data set ignoring inactive elements.

Uses a recurrence relation to prevent data overflow for large sums.

Note
The variance is equal to the standard deviation squared. The variance is normalized by N in this case. Source: GNU Scientific Library.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 115 of file error_vector.h.

References cut_above(), cut_below(), is_active_elem(), mean(), mesh, plot_error(), and libMesh::Real.

Referenced by libMesh::MeshRefinement::flag_elements_by_mean_stddev(), and main().

116  { return this->variance(this->mean()); }
virtual Real variance() const libmesh_override
Definition: error_vector.h:115
virtual Real mean() const libmesh_override
Definition: error_vector.C:67
Real libMesh::ErrorVector::variance ( const Real  mean) const
virtual
Returns
The variance of the data set ignoring inactive elements and given the mean.

This is useful for efficiency when you have already calculated the mean. Uses a recurrence relation to prevent data overflow for large sums.

Note
The variance is equal to the standard deviation squared. Source: GNU Scientific Library.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 124 of file error_vector.C.

References is_active_elem(), and libMesh::Real.

125 {
126  const dof_id_type n = cast_int<dof_id_type>(this->size());
127 
128  LOG_SCOPE ("variance()", "ErrorVector");
129 
130  Real the_variance = 0;
131  dof_id_type nnz = 0;
132 
133  for (dof_id_type i=0; i<n; i++)
134  if (this->is_active_elem(i))
135  {
136  const Real delta = ( static_cast<Real>((*this)[i]) - mean_in );
137  the_variance += (delta * delta - the_variance) / (nnz + 1);
138 
139  nnz++;
140  }
141 
142  return the_variance;
143 }
bool is_active_elem(dof_id_type i) const
Utility function to decide whether element i is active.
Definition: error_vector.C:195
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t dof_id_type
Definition: id_types.h:64

Member Data Documentation

MeshBase* libMesh::ErrorVector::_mesh
protected

Pointer to the mesh, which may be used to decide which elements are active.

Definition at line 163 of file error_vector.h.

Referenced by is_active_elem().


The documentation for this class was generated from the following files: