libMesh
Loading...
Searching...
No Matches
error_estimator.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19// Local include files
20#include "libmesh/libmesh_common.h"
21#include "libmesh/error_estimator.h"
22#include "libmesh/error_vector.h"
23#include "libmesh/equation_systems.h"
24#include "libmesh/parallel.h"
25#include "libmesh/enum_error_estimator_type.h"
26#include "libmesh/int_range.h"
27
28namespace libMesh
29{
30
31// ErrorEstimator functions
32void ErrorEstimator::reduce_error (std::vector<ErrorVectorReal> & error_per_cell,
33 const Parallel::Communicator & comm) const
34{
35 // This function must be run on all processors at once
36 // parallel_object_only();
37
38 // Each processor has now computed the error contributions
39 // for its local elements. We may need to sum the vector to
40 // recover the error for each element.
41
42 comm.sum(error_per_cell);
43}
44
45
46
48 ErrorVector & error_per_cell,
49 const std::map<const System *, SystemNorm> & error_norms,
50 const std::map<const System *, const NumericVector<Number> *> * solution_vectors,
51 bool estimate_parent_error)
52{
53 SystemNorm old_error_norm = this->error_norm;
54
55 // Sum the error values from each system
56 for (auto s : make_range(equation_systems.n_systems()))
57 {
58 ErrorVector system_error_per_cell;
59 const System & sys = equation_systems.get_system(s);
60 if (const auto it = error_norms.find(&sys);
61 it == error_norms.end())
62 this->error_norm = old_error_norm;
63 else
64 this->error_norm = it->second;
65
66 const NumericVector<Number> * solution_vector = nullptr;
67 if (solution_vectors &&
68 solution_vectors->find(&sys) != solution_vectors->end())
69 solution_vector = solution_vectors->find(&sys)->second;
70
71 this->estimate_error(sys, system_error_per_cell,
72 solution_vector, estimate_parent_error);
73
74 if (s)
75 {
76 libmesh_assert_equal_to (error_per_cell.size(), system_error_per_cell.size());
77 for (auto i : index_range(error_per_cell))
78 error_per_cell[i] += system_error_per_cell[i];
79 }
80 else
81 error_per_cell = system_error_per_cell;
82 }
83
84 // Restore our old state before returning
85 this->error_norm = old_error_norm;
86}
87
88
89
95 ErrorMap & errors_per_cell,
96 const std::map<const System *, const NumericVector<Number> *> * solution_vectors,
97 bool estimate_parent_error)
98{
99 SystemNorm old_error_norm = this->error_norm;
100
101 // Find the requested error values from each system
102 for (auto s : make_range(equation_systems.n_systems()))
103 {
104 const System & sys = equation_systems.get_system(s);
105
106 unsigned int n_vars = sys.n_vars();
107
108 for (unsigned int v = 0; v != n_vars; ++v)
109 {
110 // Only fill in ErrorVectors the user asks for
111 if (!errors_per_cell.count(std::make_pair(&sys, v)))
112 continue;
113
114 // Calculate error in only one variable
115 std::vector<Real> weights(n_vars, 0.0);
116 weights[v] = 1.0;
117 this->error_norm =
118 SystemNorm(std::vector<FEMNormType>(n_vars, old_error_norm.type(v)),
119 weights);
120
121 const NumericVector<Number> * solution_vector = nullptr;
122 if (solution_vectors &&
123 solution_vectors->find(&sys) != solution_vectors->end())
124 solution_vector = solution_vectors->find(&sys)->second;
125
126 this->estimate_error
127 (sys, *errors_per_cell[std::make_pair(&sys, v)],
128 solution_vector, estimate_parent_error);
129 }
130 }
131
132 // Restore our old state before returning
133 this->error_norm = old_error_norm;
134}
135
136} // namespace libMesh
unsigned int n_vars
This is the EquationSystems class.
unsigned int n_systems() const
const T_sys & get_system(std::string_view name) const
void reduce_error(std::vector< ErrorVectorReal > &error_per_cell, const Parallel::Communicator &comm) const
This method takes the local error contributions in error_per_cell from each processor and combines th...
SystemNorm error_norm
When estimating the error in a single system, the error_norm is used to control the scaling and norm ...
virtual void estimate_errors(const EquationSystems &equation_systems, ErrorVector &error_per_cell, const std::map< const System *, SystemNorm > &error_norms, const std::map< const System *, const NumericVector< Number > * > *solution_vectors=nullptr, bool estimate_parent_error=false)
This virtual function can be redefined in derived classes, but by default computes the sum of the err...
std::map< std::pair< const System *, unsigned int >, std::unique_ptr< ErrorVector > > ErrorMap
When calculating many error vectors at once, we need a data structure to hold them all.
virtual void estimate_error(const System &system, ErrorVector &error_per_cell, const NumericVector< Number > *solution_vector=nullptr, bool estimate_parent_error=false)=0
This pure virtual function must be redefined in derived classes to compute the error for each active ...
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
This class defines a norm/seminorm to be applied to a NumericVector which contains coefficients in a ...
Definition system_norm.h:50
FEMNormType type(unsigned int var) const
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
unsigned int n_vars() const
Definition system.C:2674
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176