libMesh::ErrorVector Class Reference

#include <error_vector.h>

Inheritance diagram for libMesh::ErrorVector:

Public Member Functions

 ErrorVector (dof_id_type i=0, MeshBase *mesh=libmesh_nullptr)
 
 ErrorVector (dof_id_type i, ErrorVectorReal val)
 
virtual ErrorVectorReal minimum () const libmesh_override
 
virtual Real mean () const libmesh_override
 
virtual Real median () libmesh_override
 
virtual Real median () const libmesh_override
 
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
 
virtual Real l2_norm () const
 
virtual ErrorVectorReal maximum () const
 
virtual Real stddev () const
 
virtual Real stddev (const Real known_mean) const
 
void normalize ()
 
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
 
void plot_histogram (const processor_id_type my_procid, const std::string &filename, unsigned int n_bins)
 

Protected Member Functions

bool is_active_elem (dof_id_type i) const
 

Protected Attributes

MeshBase_mesh
 

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 
)
inline

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
libMesh::ErrorVector::ErrorVector ( dof_id_type  i,
ErrorVectorReal  val 
)
inline

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 171 of file error_vector.C.

References is_active_elem().

Referenced by variance().

172 {
173  LOG_SCOPE ("cut_above()", "ErrorVector");
174 
175  const dof_id_type n = cast_int<dof_id_type>(this->size());
176 
177  std::vector<dof_id_type> cut_indices;
178  cut_indices.reserve(n/2); // Arbitrary
179 
180  for (dof_id_type i=0; i<n; i++)
181  if (this->is_active_elem(i))
182  {
183  if ((*this)[i] > cut)
184  {
185  cut_indices.push_back(i);
186  }
187  }
188 
189  return cut_indices;
190 }
bool is_active_elem(dof_id_type i) const
Definition: error_vector.C:194
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 147 of file error_vector.C.

References is_active_elem().

Referenced by variance().

148 {
149  LOG_SCOPE ("cut_below()", "ErrorVector");
150 
151  const dof_id_type n = cast_int<dof_id_type>(this->size());
152 
153  std::vector<dof_id_type> cut_indices;
154  cut_indices.reserve(n/2); // Arbitrary
155 
156  for (dof_id_type i=0; i<n; i++)
157  if (this->is_active_elem(i))
158  {
159  if ((*this)[i] < cut)
160  {
161  cut_indices.push_back(i);
162  }
163  }
164 
165  return cut_indices;
166 }
bool is_active_elem(dof_id_type i) const
Definition: error_vector.C:194
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

Computes and 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 194 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().

195 {
196  libmesh_assert_less (i, this->size());
197 
198  if (_mesh)
199  {
200  return _mesh->elem_ptr(i)->active();
201  }
202  else
203  return ((*this)[i] != 0.);
204 }
bool active() const
Definition: elem.h:1984
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 66 of file error_vector.C.

References is_active_elem(), and libMesh::Real.

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

67 {
68  LOG_SCOPE ("mean()", "ErrorVector");
69 
70  const dof_id_type n = cast_int<dof_id_type>(this->size());
71 
72  Real the_mean = 0;
73  dof_id_type nnz = 0;
74 
75  for (dof_id_type i=0; i<n; i++)
76  if (this->is_active_elem(i))
77  {
78  the_mean += ( static_cast<Real>((*this)[i]) - the_mean ) / (nnz + 1);
79 
80  nnz++;
81  }
82 
83  return the_mean;
84 }
bool is_active_elem(dof_id_type i) const
Definition: error_vector.C:194
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 89 of file error_vector.C.

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

Referenced by ErrorVector(), and median().

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

A const version of the median funtion. Requires twice the memory of original data set but does not change the original.

Reimplemented from libMesh::StatisticsVector< ErrorVectorReal >.

Definition at line 113 of file error_vector.C.

References median().

114 {
115  ErrorVector ev = (*this);
116 
117  return ev.median();
118 }
ErrorVector(dof_id_type i=0, MeshBase *mesh=libmesh_nullptr)
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 43 of file error_vector.C.

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

Referenced by ErrorVector().

44 {
45  LOG_SCOPE ("minimum()", "ErrorVector");
46 
47  const dof_id_type n = cast_int<dof_id_type>(this->size());
49 
50  for (dof_id_type i=0; i<n; i++)
51  {
52  // Only positive (or zero) values in the error vector
53  libmesh_assert_greater_equal ((*this)[i], 0.);
54  if (this->is_active_elem(i))
55  min = std::min (min, (*this)[i]);
56  }
57 
58  // ErrorVectors are for positive values
59  libmesh_assert_greater_equal (min, 0.);
60 
61  return min;
62 }
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
Definition: error_vector.C:194
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 207 of file error_vector.C.

References libMesh::MeshBase::active_local_elements_begin(), libMesh::MeshBase::active_local_elements_end(), 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::elements_begin(), libMesh::MeshBase::elements_end(), libMesh::err, libMesh::System::get_dof_map(), libMesh::DofObject::id(), 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::Elem::set_p_level(), libMesh::Elem::set_p_refinement_flag(), libMesh::System::solution, libMesh::ExodusII_IO::write(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::ExodusII_IO::write_element_data(), and libMesh::MeshOutput< MT >::write_equation_systems().

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

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

Computes the standard deviation of the data set, which is simply the square-root of the variance.

Definition at line 164 of file statistics.h.

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

165  { return std::sqrt(this->variance()); }
virtual Real libMesh::StatisticsVector< ErrorVectorReal >::stddev ( const Real  known_mean) const
inlinevirtualinherited

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 173 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().

174  { return std::sqrt(this->variance(known_mean)); }
virtual Real libMesh::ErrorVector::variance ( ) const
inlinevirtual

Computes 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 116 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().

117  { return this->variance(this->mean()); }
virtual Real variance() const libmesh_override
Definition: error_vector.h:116
virtual Real mean() const libmesh_override
Definition: error_vector.C:66
Real libMesh::ErrorVector::variance ( const Real  mean) const
virtual

Computes the variance of the data set ignoring inactive elements. where the mean is provided. 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 123 of file error_vector.C.

References is_active_elem(), and libMesh::Real.

124 {
125  const dof_id_type n = cast_int<dof_id_type>(this->size());
126 
127  LOG_SCOPE ("variance()", "ErrorVector");
128 
129  Real the_variance = 0;
130  dof_id_type nnz = 0;
131 
132  for (dof_id_type i=0; i<n; i++)
133  if (this->is_active_elem(i))
134  {
135  const Real delta = ( static_cast<Real>((*this)[i]) - mean_in );
136  the_variance += (delta * delta - the_variance) / (nnz + 1);
137 
138  nnz++;
139  }
140 
141  return the_variance;
142 }
bool is_active_elem(dof_id_type i) const
Definition: error_vector.C:194
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 164 of file error_vector.h.

Referenced by is_active_elem().


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