753 * std::ofstream out(output_name_base +
"-" +
763 * <a name=
"TheLaplacesolverclasses"></a>
764 * <h3>The Laplace solver classes</h3>
768 * Next are the actual solver classes. Again, we discuss only the
769 * differences to the previous program.
772 *
namespace LaplaceSolver
777 * <a name=
"TheLaplacesolverbaseclass"></a>
778 * <h4>The Laplace solver base
class</h4>
782 * This
class is almost unchanged, with the exception that it declares two
783 * more
functions: <code>output_solution</code> will be used to generate
784 * output files from the actual solutions computed by derived classes, and
785 * the <code>set_refinement_cycle</code>
function by which the testing
786 * framework sets the number of the refinement cycle to a local variable
787 * in
this class;
this number is later used to generate filenames
for the
796 *
virtual ~Base() =
default;
798 *
virtual void solve_problem() = 0;
799 *
virtual void postprocess(
800 *
const Evaluation::EvaluationBase<dim> &postprocessor)
const = 0;
801 *
virtual void refine_grid() = 0;
802 *
virtual unsigned int n_dofs()
const = 0;
804 *
virtual void set_refinement_cycle(
const unsigned int cycle);
806 *
virtual void output_solution()
const = 0;
811 *
unsigned int refinement_cycle;
824 *
void Base<dim>::set_refinement_cycle(
const unsigned int cycle)
826 * refinement_cycle = cycle;
833 * <a name=
"TheLaplaceSolverclass"></a>
834 * <h4>The Laplace
Solver class</h4>
838 * Likewise, the <code>
Solver</code>
class is entirely unchanged and will
839 * thus not be discussed.
843 *
class Solver :
public virtual Base<dim>
851 *
virtual ~
Solver()
override;
853 *
virtual void solve_problem()
override;
855 *
virtual void postprocess(
856 *
const Evaluation::EvaluationBase<dim> &postprocessor)
const override;
858 *
virtual unsigned int n_dofs()
const override;
871 *
struct LinearSystem
886 * The remainder of the
class is essentially a
copy of @ref step_13 "step-13"
887 * as well, including the data structures and
functions
888 * necessary to compute the linear system in
parallel using the
892 *
struct AssemblyScratchData
896 * AssemblyScratchData(
const AssemblyScratchData &scratch_data);
901 *
struct AssemblyCopyData
904 * std::vector<types::global_dof_index> local_dof_indices;
908 *
void assemble_linear_system(LinearSystem &linear_system);
910 *
void local_assemble_matrix(
912 * AssemblyScratchData & scratch_data,
913 * AssemblyCopyData & copy_data)
const;
916 *
void copy_local_to_global(
const AssemblyCopyData ©_data,
917 * LinearSystem & linear_system)
const;
930 * , quadrature(&quadrature)
931 * , face_quadrature(&face_quadrature)
933 * , boundary_values(&boundary_values)
940 * dof_handler.clear();
947 * dof_handler.distribute_dofs(*fe);
948 * solution.reinit(dof_handler.n_dofs());
950 * LinearSystem linear_system(dof_handler);
951 * assemble_linear_system(linear_system);
952 * linear_system.solve(solution);
958 *
const Evaluation::EvaluationBase<dim> &postprocessor)
const
960 * postprocessor(dof_handler, solution);
967 *
return dof_handler.n_dofs();
973 * The following few
functions and constructors are verbatim
974 * copies taken from @ref step_13
"step-13":
985 * AssemblyScratchData &scratch_data,
986 * AssemblyCopyData & copy_data) {
987 * this->local_assemble_matrix(cell, scratch_data, copy_data);
990 *
auto copier = [
this, &linear_system](
const AssemblyCopyData ©_data) {
991 * this->copy_local_to_global(copy_data, linear_system);
998 * AssemblyScratchData(*fe, *quadrature),
999 * AssemblyCopyData());
1000 * linear_system.hanging_node_constraints.condense(linear_system.matrix);
1002 * std::map<types::global_dof_index, double> boundary_value_map;
1006 * boundary_value_map);
1009 * linear_system.hanging_node_constraints.condense(linear_system.rhs);
1012 * linear_system.matrix,
1014 * linear_system.rhs);
1018 *
template <
int dim>
1026 *
template <
int dim>
1028 *
const AssemblyScratchData &scratch_data)
1029 * : fe_values(scratch_data.fe_values.
get_fe(),
1030 * scratch_data.fe_values.get_quadrature(),
1035 *
template <
int dim>
1038 * AssemblyScratchData & scratch_data,
1039 * AssemblyCopyData & copy_data)
const
1041 *
const unsigned int dofs_per_cell = fe->dofs_per_cell;
1042 *
const unsigned int n_q_points = quadrature->size();
1044 * copy_data.cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
1046 * copy_data.local_dof_indices.resize(dofs_per_cell);
1048 * scratch_data.fe_values.reinit(cell);
1050 *
for (
unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1051 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1052 *
for (
unsigned int j = 0; j < dofs_per_cell; ++j)
1053 * copy_data.cell_matrix(i, j) +=
1054 * (scratch_data.fe_values.shape_grad(i, q_point) *
1055 * scratch_data.fe_values.shape_grad(j, q_point) *
1056 * scratch_data.fe_values.JxW(q_point));
1058 * cell->get_dof_indices(copy_data.local_dof_indices);
1063 *
template <
int dim>
1065 * LinearSystem &linear_system)
const
1067 *
for (
unsigned int i = 0; i < copy_data.local_dof_indices.size(); ++i)
1068 *
for (
unsigned int j = 0; j < copy_data.local_dof_indices.size(); ++j)
1069 * linear_system.matrix.add(copy_data.local_dof_indices[i],
1070 * copy_data.local_dof_indices[j],
1071 * copy_data.cell_matrix(i, j));
1077 * Now
for the
functions that implement actions in the linear
1078 * system
class. First, the constructor initializes all data
1079 * elements to their correct sizes, and sets up a number of
1080 * additional data structures, such as constraints due to hanging
1081 * nodes. Since setting up the hanging nodes and finding out about
1082 * the
nonzero elements of the
matrix is independent, we
do that
1083 * in
parallel (
if the library was configured to use concurrency,
1084 * at least; otherwise, the actions are performed
1085 * sequentially). Note that we start only
one thread, and
do the
1086 *
second action in the main thread. Since only
one thread is
1087 * generated, we don
't use the <code>Threads::ThreadGroup</code>
1088 * class here, but rather use the one created thread object
1089 * directly to wait for this particular thread's exit. The
1090 * approach is generally the same as the
one we have used in
1091 * <code>Solver::assemble_linear_system()</code> above.
1095 * Note that taking the address of the
1097 * is a little tricky, since there are actually three
functions of
1098 *
this name,
one for each supported space dimension. Taking
1099 * addresses of overloaded
functions is somewhat complicated in
1100 *
C++, since the address-of operator <code>&</code> in that
case
1101 * returns a
set of values (the addresses of all
1102 *
functions with that name), and selecting the right
one is then
1103 * the next step. If the context dictates which
one to take (
for
1104 * example by assigning to a
function pointer of known type), then
1105 * the compiler can
do that by itself, but
if this set of pointers
1106 * shall be given as the argument to a
function that takes a
1107 *
template, the compiler could choose all without having a
1108 * preference
for one. We therefore have to make it clear to the
1109 * compiler which
one we would like to have;
for this, we could
1110 * use a cast, but
for more clarity, we assign it to a temporary
1111 * <code>mhnc_p</code> (
short for <code>pointer to
1113 *
using this pointer instead.
1116 * template <int dim>
1119 * hanging_node_constraints.clear();
1126 * Start a side task then
continue on the main thread
1139 * Wait
for the side task to be done before going further
1144 * hanging_node_constraints.close();
1145 * hanging_node_constraints.condense(dsp);
1146 * sparsity_pattern.copy_from(dsp);
1148 *
matrix.reinit(sparsity_pattern);
1149 * rhs.reinit(dof_handler.
n_dofs());
1154 *
template <
int dim>
1163 * cg.solve(
matrix, solution, rhs, preconditioner);
1165 * hanging_node_constraints.distribute(solution);
1173 * <a name=
"ThePrimalSolverclass"></a>
1174 * <h4>The PrimalSolver
class</h4>
1178 * The <code>PrimalSolver</code>
class is also mostly unchanged except for
1179 * implementing the <code>output_solution</code>
function. We keep the
1180 * <code>GlobalRefinement</code> and <code>RefinementKelly</code> classes
1181 * in
this program, and they can then rely on the
default implementation
1182 * of
this function which simply outputs the primal solution. The
class
1183 * implementing dual weighted error estimators will overload
this function
1184 * itself, to also output the dual solution.
1187 *
template <
int dim>
1188 *
class PrimalSolver :
public Solver<dim>
1198 *
virtual void output_solution()
const override;
1206 *
template <
int dim>
1219 * , rhs_function(&rhs_function)
1224 *
template <
int dim>
1225 *
void PrimalSolver<dim>::output_solution() const
1232 * std::ofstream out(
"solution-" +
std::to_string(this->refinement_cycle) +
1239 *
template <
int dim>
1240 *
void PrimalSolver<dim>::assemble_rhs(
Vector<double> &rhs)
const
1243 * *this->quadrature,
1247 *
const unsigned int dofs_per_cell = this->fe->dofs_per_cell;
1248 *
const unsigned int n_q_points = this->quadrature->size();
1251 * std::vector<double> rhs_values(n_q_points);
1252 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1258 * fe_values.reinit(cell);
1260 * rhs_function->value_list(fe_values.get_quadrature_points(),
1263 *
for (
unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1264 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1265 * cell_rhs(i) += (fe_values.shape_value(i, q_point) *
1266 * rhs_values[q_point] *
1267 * fe_values.JxW(q_point));
1269 * cell->get_dof_indices(local_dof_indices);
1270 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1271 * rhs(local_dof_indices[i]) += cell_rhs(i);
1279 * <a name=
"TheRefinementGlobalandRefinementKellyclasses"></a>
1280 * <h4>The RefinementGlobal and RefinementKelly classes</h4>
1284 * For the following two classes, the same applies as
for most of the
1285 * above: the
class is taken from the previous example as-is:
1288 * template <int dim>
1289 * class RefinementGlobal :
public PrimalSolver<dim>
1299 *
virtual void refine_grid()
override;
1304 *
template <
int dim>
1305 * RefinementGlobal<dim>::RefinementGlobal(
1312 * : Base<dim>(coarse_grid)
1313 * , PrimalSolver<dim>(coarse_grid,
1323 *
template <
int dim>
1324 *
void RefinementGlobal<dim>::refine_grid()
1331 *
template <
int dim>
1332 *
class RefinementKelly :
public PrimalSolver<dim>
1342 *
virtual void refine_grid()
override;
1347 *
template <
int dim>
1348 * RefinementKelly<dim>::RefinementKelly(
1355 * : Base<dim>(coarse_grid)
1356 * , PrimalSolver<dim>(coarse_grid,
1366 *
template <
int dim>
1367 *
void RefinementKelly<dim>::refine_grid()
1372 * this->dof_handler,
1376 * estimated_error_per_cell);
1378 * estimated_error_per_cell,
1389 * <a name=
"TheRefinementWeightedKellyclass"></a>
1390 * <h4>The RefinementWeightedKelly
class</h4>
1394 * This
class is a variant of the previous
one, in that it allows to
1395 * weight the refinement indicators we get from the library
's Kelly
1396 * indicator by some function. We include this class since the goal of
1397 * this example program is to demonstrate automatic refinement criteria
1398 * even for complex output quantities such as point values or stresses. If
1399 * we did not solve a dual problem and compute the weights thereof, we
1400 * would probably be tempted to give a hand-crafted weighting to the
1401 * indicators to account for the fact that we are going to evaluate these
1402 * quantities. This class accepts such a weighting function as argument to
1406 * template <int dim>
1407 * class RefinementWeightedKelly : public PrimalSolver<dim>
1410 * RefinementWeightedKelly(Triangulation<dim> & coarse_grid,
1411 * const FiniteElement<dim> & fe,
1412 * const Quadrature<dim> & quadrature,
1413 * const Quadrature<dim - 1> &face_quadrature,
1414 * const Function<dim> & rhs_function,
1415 * const Function<dim> & boundary_values,
1416 * const Function<dim> & weighting_function);
1418 * virtual void refine_grid() override;
1421 * const SmartPointer<const Function<dim>> weighting_function;
1426 * template <int dim>
1427 * RefinementWeightedKelly<dim>::RefinementWeightedKelly(
1428 * Triangulation<dim> & coarse_grid,
1429 * const FiniteElement<dim> & fe,
1430 * const Quadrature<dim> & quadrature,
1431 * const Quadrature<dim - 1> &face_quadrature,
1432 * const Function<dim> & rhs_function,
1433 * const Function<dim> & boundary_values,
1434 * const Function<dim> & weighting_function)
1435 * : Base<dim>(coarse_grid)
1436 * , PrimalSolver<dim>(coarse_grid,
1442 * , weighting_function(&weighting_function)
1449 * Now, here comes the main function, including the weighting:
1452 * template <int dim>
1453 * void RefinementWeightedKelly<dim>::refine_grid()
1457 * First compute some residual based error indicators for all cells by a
1458 * method already implemented in the library. What exactly we compute
1459 * here is described in more detail in the documentation of that class.
1462 * Vector<float> estimated_error_per_cell(
1463 * this->triangulation->n_active_cells());
1464 * std::map<types::boundary_id, const Function<dim> *> dummy_function_map;
1465 * KellyErrorEstimator<dim>::estimate(this->dof_handler,
1466 * *this->face_quadrature,
1467 * dummy_function_map,
1469 * estimated_error_per_cell);
1473 * Next weigh each entry in the vector of indicators by the value of the
1474 * function given to the constructor, evaluated at the cell center. We
1475 * need to write the result into the vector entry that corresponds to the
1476 * current cell, which we can obtain by asking the cell what its index
1477 * among all active cells is using CellAccessor::active_cell_index(). (In
1478 * reality, this index is zero for the first cell we handle in the loop,
1479 * one for the second cell, etc., and we could as well just keep track of
1480 * this index using an integer counter; but using
1481 * CellAccessor::active_cell_index() makes this more explicit.)
1484 * for (const auto &cell : this->dof_handler.active_cell_iterators())
1485 * estimated_error_per_cell(cell->active_cell_index()) *=
1486 * weighting_function->value(cell->center());
1488 * GridRefinement::refine_and_coarsen_fixed_number(*this->triangulation,
1489 * estimated_error_per_cell,
1492 * this->triangulation->execute_coarsening_and_refinement();
1495 * } // namespace LaplaceSolver
1501 * <a name="Equationdata"></a>
1502 * <h3>Equation data</h3>
1506 * In this example program, we work with the same data sets as in the
1507 * previous one, but as it may so happen that someone wants to run the
1508 * program with different boundary values and right hand side functions, or
1509 * on a different grid, we show a simple technique to do exactly that. For
1510 * more clarity, we furthermore pack everything that has to do with equation
1511 * data into a namespace of its own.
1515 * The underlying assumption is that this is a research program, and that
1516 * there we often have a number of test cases that consist of a domain, a
1517 * right hand side, boundary values, possibly a specified coefficient, and a
1518 * number of other parameters. They often vary all at the same time when
1519 * shifting from one example to another. To make handling such sets of
1520 * problem description parameters simple is the goal of the following.
1524 * Basically, the idea is this: let us have a structure for each set of
1525 * data, in which we pack everything that describes a test case: here, these
1526 * are two subclasses, one called <code>BoundaryValues</code> for the
1527 * boundary values of the exact solution, and one called
1528 * <code>RightHandSide</code>, and then a way to generate the coarse
1529 * grid. Since the solution of the previous example program looked like
1530 * curved ridges, we use this name here for the enclosing class. Note that
1531 * the names of the two inner classes have to be the same for all enclosing
1532 * test case classes, and also that we have attached the dimension template
1533 * argument to the enclosing class rather than to the inner ones, to make
1534 * further processing simpler. (From a language viewpoint, a namespace
1535 * would be better to encapsulate these inner classes, rather than a
1536 * structure. However, namespaces cannot be given as template arguments, so
1537 * we use a structure to allow a second object to select from within its
1538 * given argument. The enclosing structure, of course, has no member
1539 * variables apart from the classes it declares, and a static function to
1540 * generate the coarse mesh; it will in general never be instantiated.)
1544 * The idea is then the following (this is the right time to also take a
1545 * brief look at the code below): we can generate objects for boundary
1546 * values and right hand side by simply giving the name of the outer class
1547 * as a template argument to a class which we call here
1548 * <code>Data::SetUp</code>, and it then creates objects for the inner
1549 * classes. In this case, to get all that characterizes the curved ridge
1550 * solution, we would simply generate an instance of
1551 * <code>Data::SetUp@<Data::CurvedRidge@></code>, and everything we need to
1552 * know about the solution would be static member variables and functions of
1557 * This approach might seem like overkill in this case, but will become very
1558 * handy once a certain set up is not only characterized by Dirichlet
1559 * boundary values and a right hand side function, but in addition by
1560 * material properties, Neumann values, different boundary descriptors,
1561 * etc. In that case, the <code>SetUp</code> class might consist of a dozen
1562 * or more objects, and each descriptor class (like the
1563 * <code>CurvedRidges</code> class below) would have to provide them. Then,
1564 * you will be happy to be able to change from one set of data to another by
1565 * only changing the template argument to the <code>SetUp</code> class at
1566 * one place, rather than at many.
1570 * With this framework for different test cases, we are almost finished, but
1571 * one thing remains: by now we can select statically, by changing one
1572 * template argument, which data set to choose. In order to be able to do
1573 * that dynamically, i.e. at run time, we need a base class. This we provide
1574 * in the obvious way, see below, with virtual abstract functions. It forces
1575 * us to introduce a second template parameter <code>dim</code> which we
1576 * need for the base class (which could be avoided using some template
1577 * magic, but we omit that), but that's all.
1581 * Adding
new testcases is now simple, you don
't have to touch the framework
1582 * classes, only a structure like the <code>CurvedRidges</code> one is
1591 * <a name="TheSetUpBaseandSetUpclasses"></a>
1592 * <h4>The SetUpBase and SetUp classes</h4>
1596 * Based on the above description, the <code>SetUpBase</code> class then
1597 * looks as follows. To allow using the <code>SmartPointer</code> class
1598 * with this class, we derived from the <code>Subscriptor</code> class.
1601 * template <int dim>
1602 * struct SetUpBase : public Subscriptor
1604 * virtual const Function<dim> &get_boundary_values() const = 0;
1606 * virtual const Function<dim> &get_right_hand_side() const = 0;
1609 * create_coarse_grid(Triangulation<dim> &coarse_grid) const = 0;
1615 * And now for the derived class that takes the template argument as
1620 * Here we pack the data elements into private variables, and allow access
1621 * to them through the methods of the base class.
1624 * template <class Traits, int dim>
1625 * struct SetUp : public SetUpBase<dim>
1627 * virtual const Function<dim> &get_boundary_values() const override;
1629 * virtual const Function<dim> &get_right_hand_side() const override;
1633 * create_coarse_grid(Triangulation<dim> &coarse_grid) const override;
1636 * static const typename Traits::BoundaryValues boundary_values;
1637 * static const typename Traits::RightHandSide right_hand_side;
1642 * We have to provide definitions for the static member variables of the
1646 * template <class Traits, int dim>
1647 * const typename Traits::BoundaryValues SetUp<Traits, dim>::boundary_values;
1648 * template <class Traits, int dim>
1649 * const typename Traits::RightHandSide SetUp<Traits, dim>::right_hand_side;
1653 * And definitions of the member functions:
1656 * template <class Traits, int dim>
1657 * const Function<dim> &SetUp<Traits, dim>::get_boundary_values() const
1659 * return boundary_values;
1663 * template <class Traits, int dim>
1664 * const Function<dim> &SetUp<Traits, dim>::get_right_hand_side() const
1666 * return right_hand_side;
1670 * template <class Traits, int dim>
1671 * void SetUp<Traits, dim>::create_coarse_grid(
1672 * Triangulation<dim> &coarse_grid) const
1674 * Traits::create_coarse_grid(coarse_grid);
1681 * <a name="TheCurvedRidgesclass"></a>
1682 * <h4>The CurvedRidges class</h4>
1686 * The class that is used to describe the boundary values and right hand
1687 * side of the <code>curved ridge</code> problem already used in the
1688 * @ref step_13 "step-13" example program is then like so:
1691 * template <int dim>
1692 * struct CurvedRidges
1694 * class BoundaryValues : public Function<dim>
1697 * virtual double value(const Point<dim> & p,
1698 * const unsigned int component) const;
1702 * class RightHandSide : public Function<dim>
1705 * virtual double value(const Point<dim> & p,
1706 * const unsigned int component) const;
1709 * static void create_coarse_grid(Triangulation<dim> &coarse_grid);
1713 * template <int dim>
1714 * double CurvedRidges<dim>::BoundaryValues::value(
1715 * const Point<dim> &p,
1716 * const unsigned int /*component*/) const
1719 * for (unsigned int i = 1; i < dim; ++i)
1720 * q += std::sin(10 * p(i) + 5 * p(0) * p(0));
1721 * const double exponential = std::exp(q);
1722 * return exponential;
1727 * template <int dim>
1728 * double CurvedRidges<dim>::RightHandSide::value(
1729 * const Point<dim> &p,
1730 * const unsigned int /*component*/) const
1733 * for (unsigned int i = 1; i < dim; ++i)
1734 * q += std::sin(10 * p(i) + 5 * p(0) * p(0));
1735 * const double u = std::exp(q);
1736 * double t1 = 1, t2 = 0, t3 = 0;
1737 * for (unsigned int i = 1; i < dim; ++i)
1739 * t1 += std::cos(10 * p(i) + 5 * p(0) * p(0)) * 10 * p(0);
1740 * t2 += 10 * std::cos(10 * p(i) + 5 * p(0) * p(0)) -
1741 * 100 * std::sin(10 * p(i) + 5 * p(0) * p(0)) * p(0) * p(0);
1742 * t3 += 100 * std::cos(10 * p(i) + 5 * p(0) * p(0)) *
1743 * std::cos(10 * p(i) + 5 * p(0) * p(0)) -
1744 * 100 * std::sin(10 * p(i) + 5 * p(0) * p(0));
1748 * return -u * (t1 + t2 + t3);
1752 * template <int dim>
1753 * void CurvedRidges<dim>::create_coarse_grid(Triangulation<dim> &coarse_grid)
1755 * GridGenerator::hyper_cube(coarse_grid, -1, 1);
1756 * coarse_grid.refine_global(2);
1763 * <a name="TheExercise_2_3class"></a>
1764 * <h4>The Exercise_2_3 class</h4>
1768 * This example program was written while giving practical courses for a
1769 * lecture on adaptive finite element methods and duality based error
1770 * estimates. For these courses, we had one exercise, which required to
1771 * solve the Laplace equation with constant right hand side on a square
1772 * domain with a square hole in the center, and zero boundary
1773 * values. Since the implementation of the properties of this problem is
1774 * so particularly simple here, lets do it. As the number of the exercise
1775 * was 2.3, we take the liberty to retain this name for the class as well.
1778 * template <int dim>
1779 * struct Exercise_2_3
1783 * We need a class to denote the boundary values of the problem. In this
1784 * case, this is simple: it's the
zero function, so don
't even declare a
1785 * class, just an alias:
1788 * using BoundaryValues = Functions::ZeroFunction<dim>;
1792 * Second, a class that denotes the right hand side. Since they are
1793 * constant, just subclass the corresponding class of the library and be
1797 * class RightHandSide : public Functions::ConstantFunction<dim>
1801 * : Functions::ConstantFunction<dim>(1.)
1807 * Finally a function to generate the coarse grid. This is somewhat more
1808 * complicated here, see immediately below.
1811 * static void create_coarse_grid(Triangulation<dim> &coarse_grid);
1817 * As stated above, the grid for this example is the square [-1,1]^2 with
1818 * the square [-1/2,1/2]^2 as hole in it. We create the coarse grid as 4
1819 * times 4 cells with the middle four ones missing. To understand how
1820 * exactly the mesh is going to look, it may be simplest to just look
1821 * at the "Results" section of this tutorial program first. In general,
1822 * if you'd like to understand more about creating meshes either from
1823 * scratch by hand, as we
do here, or
using other techniques, you
1824 * should take a look at @ref step_49
"step-49".
1828 * Of course, the example has an extension to 3
d, but since
this function
1829 * cannot be written in a dimension independent way we choose not to
1830 * implement
this here, but rather only specialize the
template for
1831 * dim=2. If you compile the program
for 3
d, you
'll get a message from the
1832 * linker that this function is not implemented for 3d, and needs to be
1837 * For the creation of this geometry, the library has no predefined
1838 * method. In this case, the geometry is still simple enough to do the
1839 * creation by hand, rather than using a mesh generator.
1843 * void Exercise_2_3<2>::create_coarse_grid(Triangulation<2> &coarse_grid)
1847 * We first define the space dimension, to allow those parts of the
1848 * function that are actually dimension independent to use this
1849 * variable. That makes it simpler if you later take this as a starting
1850 * point to implement a 3d version of this mesh. The next step is then
1851 * to have a list of vertices. Here, they are 24 (5 times 5, with the
1852 * middle one omitted). It is probably best to draw a sketch here.
1855 * const unsigned int dim = 2;
1857 * const std::vector<Point<2>> vertices = {
1858 * {-1.0, -1.0}, {-0.5, -1.0}, {+0.0, -1.0}, {+0.5, -1.0}, {+1.0, -1.0},
1859 * {-1.0, -0.5}, {-0.5, -0.5}, {+0.0, -0.5}, {+0.5, -0.5}, {+1.0, -0.5},
1860 * {-1.0, +0.0}, {-0.5, +0.0}, {+0.5, +0.0}, {+1.0, +0.0},
1861 * {-1.0, +0.5}, {-0.5, +0.5}, {+0.0, +0.5}, {+0.5, +0.5}, {+1.0, +0.5},
1862 * {-1.0, +1.0}, {-0.5, +1.0}, {+0.0, +1.0}, {+0.5, +1.0}, {+1.0, +1.0}};
1866 * Next, we have to define the cells and the vertices they contain.
1869 * const std::vector<std::array<int, GeometryInfo<dim>::vertices_per_cell>>
1870 * cell_vertices = {{{0, 1, 5, 6}},
1876 * {{10, 11, 14, 15}},
1877 * {{12, 13, 17, 18}},
1878 * {{14, 15, 19, 20}},
1879 * {{15, 16, 20, 21}},
1880 * {{16, 17, 21, 22}},
1881 * {{17, 18, 22, 23}}};
1883 * const unsigned int n_cells = cell_vertices.size();
1887 * Again, we generate a C++ vector type from this, but this time by
1888 * looping over the cells (yes, this is boring). Additionally, we set
1889 * the material indicator to zero for all the cells:
1892 * std::vector<CellData<dim>> cells(n_cells, CellData<dim>());
1893 * for (unsigned int i = 0; i < n_cells; ++i)
1895 * for (unsigned int j = 0; j < GeometryInfo<dim>::vertices_per_cell;
1897 * cells[i].vertices[j] = cell_vertices[i][j];
1898 * cells[i].material_id = 0;
1903 * Finally pass all this information to the library to generate a
1904 * triangulation. The last parameter may be used to pass information
1905 * about non-zero boundary indicators at certain faces of the
1906 * triangulation to the library, but we don't want that here, so we give
1914 * And since we want that the evaluation
point (3/4,3/4) in this example
1918 * coarse_grid.refine_global(1);
1925 * <a name="Discussion"></a>
1926 * <h4>Discussion</h4>
1930 * As you have now read through this framework, you may be wondering why we
1931 * have not chosen to implement the classes implementing a certain setup
1932 * (like the <code>CurvedRidges</code> class) directly as classes derived
1933 * from <code>Data::SetUpBase</code>. Indeed, we could have done very well
1934 * so. The only reason is that then we would have to have member variables
1935 * for the solution and right hand side classes in the
1936 * <code>CurvedRidges</code> class, as well as member
functions overloading
1937 * the abstract
functions of the base class giving access to these member
1938 * variables. The <code>SetUp</code> class has the sole reason to relieve us
1939 * from the need to reiterate these member variables and
functions that
1940 * would be necessary in all such classes. In some way, the template
1941 * mechanism here only provides a way to have default implementations for a
1942 * number of
functions that depend on external quantities and can thus not
1943 * be provided using normal virtual
functions, at least not without the help
1948 * However, there might be good reasons to actually implement classes
1949 * derived from <code>Data::SetUpBase</code>, for example if the solution or
1950 * right hand side classes require constructors that take arguments, which
1951 * the <code>Data::SetUpBase</code> class cannot provide. In that case,
1952 * subclassing is a worthwhile strategy. Other possibilities for special
1953 * cases are to derive from <code>Data::SetUp@<SomeSetUp@></code> where
1954 * <code>SomeSetUp</code> denotes a class, or even to explicitly specialize
1955 * <code>Data::SetUp@<SomeSetUp@></code>. The latter allows to transparently
1956 * use the way the <code>SetUp</code> class is used for other
set-ups, but
1957 * with special actions taken for special arguments.
1961 *
A final observation favoring the approach taken here is the following: we
1962 * have found numerous times that when starting a
project, the number of
1963 * parameters (usually boundary values, right hand side, coarse grid, just
1964 * as here) was small, and the number of test cases was small as well. One
1965 * then starts out by handcoding them into a number of <code>switch</code>
1966 * statements. Over time, projects grow, and so does the number of test
1967 * cases. The number of <code>switch</code> statements grows with that, and
1968 * their length as well, and
one starts to find ways to consider impossible
1969 * examples where domains, boundary values, and right hand sides do not fit
1970 * together any more, and starts losing the overview over the whole
1971 * structure. Encapsulating everything belonging to a certain test case into
1972 * a structure of its own has proven worthwhile for this, as it keeps
1973 * everything that belongs to
one test case in
one place. Furthermore, it
1974 * allows to put these things all in
one or more files that are only devoted
1975 * to test cases and their data, without having to bring their actual
1976 * implementation into contact with the rest of the program.
1984 * <a name="Dualfunctionals"></a>
1985 * <h3>Dual functionals</h3>
1989 * As with the other components of the program, we put everything we need to
1990 * describe dual functionals into a namespace of its own, and define an
1991 * abstract base class that provides the interface the class solving the
1992 * dual problem needs for its work.
1996 * We will then implement two such classes, for the evaluation of a
point
1997 *
value and of the derivative of the solution at that
point. For these
1998 * functionals we already have the corresponding evaluation objects, so they
1999 * are complementary.
2002 * namespace DualFunctional
2007 * <a name=
"TheDualFunctionalBaseclass"></a>
2008 * <h4>The DualFunctionalBase
class</h4>
2012 * First start with the base
class for dual functionals. Since for linear
2013 * problems the characteristics of the dual problem play a role only in
2014 * the right hand side, we only need to provide
for a
function that
2015 * assembles the right hand side
for a given discretization:
2018 *
template <
int dim>
2030 * <a name=
"ThedualfunctionalPointValueEvaluationclass"></a>
2031 * <h4>The dual functional PointValueEvaluation
class</h4>
2035 * As a
first application, we consider the functional corresponding to the
2036 * evaluation of the solution
's value at a given point which again we
2037 * assume to be a vertex. Apart from the constructor that takes and stores
2038 * the evaluation point, this class consists only of the function that
2039 * implements assembling the right hand side.
2042 * template <int dim>
2043 * class PointValueEvaluation : public DualFunctionalBase<dim>
2046 * PointValueEvaluation(const Point<dim> &evaluation_point);
2048 * virtual void assemble_rhs(const DoFHandler<dim> &dof_handler,
2049 * Vector<double> & rhs) const override;
2052 * ExcEvaluationPointNotFound,
2054 * << "The evaluation point " << arg1
2055 * << " was not found among the vertices of the present grid.");
2058 * const Point<dim> evaluation_point;
2062 * template <int dim>
2063 * PointValueEvaluation<dim>::PointValueEvaluation(
2064 * const Point<dim> &evaluation_point)
2065 * : evaluation_point(evaluation_point)
2071 * As for doing the main purpose of the class, assembling the right hand
2072 * side, let us first consider what is necessary: The right hand side of
2073 * the dual problem is a vector of values J(phi_i), where J is the error
2074 * functional, and phi_i is the i-th shape function. Here, J is the
2075 * evaluation at the point x0, i.e. J(phi_i)=phi_i(x0).
2079 * Now, we have assumed that the evaluation point is a vertex. Thus, for
2080 * the usual finite elements we might be using in this program, we can
2081 * take for granted that at such a point exactly one shape function is
2082 * nonzero, and in particular has the value one. Thus, we set the right
2083 * hand side vector to all-zeros, then seek for the shape function
2084 * associated with that point and set the corresponding value of the right
2085 * hand side vector to one:
2088 * template <int dim>
2090 * PointValueEvaluation<dim>::assemble_rhs(const DoFHandler<dim> &dof_handler,
2091 * Vector<double> & rhs) const
2095 * So, first set everything to zeros...
2098 * rhs.reinit(dof_handler.n_dofs());
2102 * ...then loop over cells and find the evaluation point among the
2103 * vertices (or very close to a vertex, which may happen due to floating
2107 * for (const auto &cell : dof_handler.active_cell_iterators())
2108 * for (unsigned int vertex = 0;
2109 * vertex < GeometryInfo<dim>::vertices_per_cell;
2111 * if (cell->vertex(vertex).distance(evaluation_point) <
2112 * cell->diameter() * 1e-8)
2116 * Ok, found, so set corresponding entry, and leave function
2117 * since we are finished:
2120 * rhs(cell->vertex_dof_index(vertex, 0)) = 1;
2126 * Finally, a sanity check: if we somehow got here, then we must have
2127 * missed the evaluation point, so raise an exception unconditionally:
2130 * AssertThrow(false, ExcEvaluationPointNotFound(evaluation_point));
2137 * <a name="ThedualfunctionalPointXDerivativeEvaluationclass"></a>
2138 * <h4>The dual functional PointXDerivativeEvaluation class</h4>
2142 * As second application, we again consider the evaluation of the
2143 * x-derivative of the solution at one point. Again, the declaration of
2144 * the class, and the implementation of its constructor is not too
2148 * template <int dim>
2149 * class PointXDerivativeEvaluation : public DualFunctionalBase<dim>
2152 * PointXDerivativeEvaluation(const Point<dim> &evaluation_point);
2154 * virtual void assemble_rhs(const DoFHandler<dim> &dof_handler,
2155 * Vector<double> & rhs) const;
2158 * ExcEvaluationPointNotFound,
2160 * << "The evaluation point " << arg1
2161 * << " was not found among the vertices of the present grid.");
2164 * const Point<dim> evaluation_point;
2168 * template <int dim>
2169 * PointXDerivativeEvaluation<dim>::PointXDerivativeEvaluation(
2170 * const Point<dim> &evaluation_point)
2171 * : evaluation_point(evaluation_point)
2177 * What is interesting is the implementation of this functional: here,
2178 * J(phi_i)=d/dx phi_i(x0).
2182 * We could, as in the implementation of the respective evaluation object
2183 * take the average of the gradients of each shape function phi_i at this
2184 * evaluation point. However, we take a slightly different approach: we
2185 * simply take the average over all cells that surround this point. The
2186 * question which cells <code>surrounds</code> the evaluation point is
2187 * made dependent on the mesh width by including those cells for which the
2188 * distance of the cell's midpoint to the evaluation
point is less than
2189 * the cell
's diameter.
2193 * Taking the average of the gradient over the area/volume of these cells
2194 * leads to a dual solution which is very close to the one which would
2195 * result from the point evaluation of the gradient. It is simple to
2196 * justify theoretically that this does not change the method
2200 * template <int dim>
2201 * void PointXDerivativeEvaluation<dim>::assemble_rhs(
2202 * const DoFHandler<dim> &dof_handler,
2203 * Vector<double> & rhs) const
2207 * Again, first set all entries to zero:
2210 * rhs.reinit(dof_handler.n_dofs());
2214 * Initialize a <code>FEValues</code> object with a quadrature formula,
2215 * have abbreviations for the number of quadrature points and shape
2219 * QGauss<dim> quadrature(dof_handler.get_fe().degree + 1);
2220 * FEValues<dim> fe_values(dof_handler.get_fe(),
2222 * update_gradients | update_quadrature_points |
2223 * update_JxW_values);
2224 * const unsigned int n_q_points = fe_values.n_quadrature_points;
2225 * const unsigned int dofs_per_cell = dof_handler.get_fe().dofs_per_cell;
2229 * ...and have two objects that are used to store the global indices of
2230 * the degrees of freedom on a cell, and the values of the gradients of
2231 * the shape functions at the quadrature points:
2234 * Vector<double> cell_rhs(dofs_per_cell);
2235 * std::vector<unsigned int> local_dof_indices(dofs_per_cell);
2239 * Finally have a variable in which we will sum up the area/volume of
2240 * the cells over which we integrate, by integrating the unit functions
2244 * double total_volume = 0;
2248 * Then start the loop over all cells, and select those cells which are
2249 * close enough to the evaluation point:
2252 * for (const auto &cell : dof_handler.active_cell_iterators())
2253 * if (cell->center().distance(evaluation_point) <= cell->diameter())
2257 * If we have found such a cell, then initialize the
2258 * <code>FEValues</code> object and integrate the x-component of
2259 * the gradient of each shape function, as well as the unit
2260 * function for the total area/volume.
2263 * fe_values.reinit(cell);
2266 * for (unsigned int q = 0; q < n_q_points; ++q)
2268 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2270 * fe_values.shape_grad(i, q)[0] // (d/dx phi_i(x_q))
2271 * * fe_values.JxW(q); // * dx
2272 * total_volume += fe_values.JxW(q);
2277 * If we have the local contributions, distribute them to the
2281 * cell->get_dof_indices(local_dof_indices);
2282 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2283 * rhs(local_dof_indices[i]) += cell_rhs(i);
2288 * After we have looped over all cells, check whether we have found any
2289 * at all, by making sure that their volume is non-zero. If not, then
2290 * the results will be botched, as the right hand side should then still
2291 * be zero, so throw an exception:
2294 * AssertThrow(total_volume > 0,
2295 * ExcEvaluationPointNotFound(evaluation_point));
2299 * Finally, we have by now only integrated the gradients of the shape
2300 * functions, not taking their mean value. We fix this by dividing by
2301 * the measure of the volume over which we have integrated:
2304 * rhs /= total_volume;
2308 * } // namespace DualFunctional
2314 * <a name="ExtendingtheLaplaceSolvernamespace"></a>
2315 * <h3>Extending the LaplaceSolver namespace</h3>
2318 * namespace LaplaceSolver
2323 * <a name="TheDualSolverclass"></a>
2324 * <h4>The DualSolver class</h4>
2328 * In the same way as the <code>PrimalSolver</code> class above, we now
2329 * implement a <code>DualSolver</code>. It has all the same features, the
2330 * only difference is that it does not take a function object denoting a
2331 * right hand side object, but now takes a <code>DualFunctionalBase</code>
2332 * object that will assemble the right hand side vector of the dual
2333 * problem. The rest of the class is rather trivial.
2337 * Since both primal and dual solver will use the same triangulation, but
2338 * different discretizations, it now becomes clear why we have made the
2339 * <code>Base</code> class a virtual one: since the final class will be
2340 * derived from both <code>PrimalSolver</code> as well as
2341 * <code>DualSolver</code>, it would have two <code>Base</code> instances,
2342 * would we not have marked the inheritance as virtual. Since in many
2343 * applications the base class would store much more information than just
2344 * the triangulation which needs to be shared between primal and dual
2345 * solvers, we do not usually want to use two such base classes.
2348 * template <int dim>
2349 * class DualSolver : public Solver<dim>
2353 * Triangulation<dim> & triangulation,
2354 * const FiniteElement<dim> & fe,
2355 * const Quadrature<dim> & quadrature,
2356 * const Quadrature<dim - 1> & face_quadrature,
2357 * const DualFunctional::DualFunctionalBase<dim> &dual_functional);
2360 * const SmartPointer<const DualFunctional::DualFunctionalBase<dim>>
2362 * virtual void assemble_rhs(Vector<double> &rhs) const override;
2364 * static const Functions::ZeroFunction<dim> boundary_values;
2367 * template <int dim>
2368 * const Functions::ZeroFunction<dim> DualSolver<dim>::boundary_values;
2370 * template <int dim>
2371 * DualSolver<dim>::DualSolver(
2372 * Triangulation<dim> & triangulation,
2373 * const FiniteElement<dim> & fe,
2374 * const Quadrature<dim> & quadrature,
2375 * const Quadrature<dim - 1> & face_quadrature,
2376 * const DualFunctional::DualFunctionalBase<dim> &dual_functional)
2377 * : Base<dim>(triangulation)
2378 * , Solver<dim>(triangulation,
2383 * , dual_functional(&dual_functional)
2388 * template <int dim>
2389 * void DualSolver<dim>::assemble_rhs(Vector<double> &rhs) const
2391 * dual_functional->assemble_rhs(this->dof_handler, rhs);
2398 * <a name="TheWeightedResidualclass"></a>
2399 * <h4>The WeightedResidual class</h4>
2403 * Here finally comes the main class of this program, the one that
2404 * implements the dual weighted residual error estimator. It joins the
2405 * primal and dual solver classes to use them for the computation of
2406 * primal and dual solutions, and implements the error representation
2407 * formula for use as error estimate and mesh refinement.
2411 * The first few of the functions of this class are mostly overriders of
2412 * the respective functions of the base class:
2415 * template <int dim>
2416 * class WeightedResidual : public PrimalSolver<dim>, public DualSolver<dim>
2420 * Triangulation<dim> & coarse_grid,
2421 * const FiniteElement<dim> & primal_fe,
2422 * const FiniteElement<dim> & dual_fe,
2423 * const Quadrature<dim> & quadrature,
2424 * const Quadrature<dim - 1> & face_quadrature,
2425 * const Function<dim> & rhs_function,
2426 * const Function<dim> & boundary_values,
2427 * const DualFunctional::DualFunctionalBase<dim> &dual_functional);
2429 * virtual void solve_problem() override;
2431 * virtual void postprocess(
2432 * const Evaluation::EvaluationBase<dim> &postprocessor) const override;
2434 * virtual unsigned int n_dofs() const override;
2436 * virtual void refine_grid() override;
2438 * virtual void output_solution() const override;
2443 * In the private section, we have two functions that are used to call
2444 * the <code>solve_problem</code> functions of the primal and dual base
2445 * classes. These two functions will be called in parallel by the
2446 * <code>solve_problem</code> function of this class.
2449 * void solve_primal_problem();
2450 * void solve_dual_problem();
2453 * Then declare abbreviations for active cell iterators, to avoid that
2454 * we have to write this lengthy name over and over again:
2460 * using active_cell_iterator =
2461 * typename DoFHandler<dim>::active_cell_iterator;
2465 * Next, declare a data type that we will us to store the contribution
2466 * of faces to the error estimator. The idea is that we can compute the
2467 * face terms from each of the two cells to this face, as they are the
2468 * same when viewed from both sides. What we will do is to compute them
2469 * only once, based on some rules explained below which of the two
2470 * adjacent cells will be in charge to do so. We then store the
2471 * contribution of each face in a map mapping faces to their values, and
2472 * only collect the contributions for each cell by looping over the
2473 * cells a second time and grabbing the values from the map.
2477 * The data type of this map is declared here:
2480 * using FaceIntegrals =
2481 * typename std::map<typename DoFHandler<dim>::face_iterator, double>;
2485 * In the computation of the error estimates on cells and faces, we need
2486 * a number of helper objects, such as <code>FEValues</code> and
2487 * <code>FEFaceValues</code> functions, but also temporary objects
2488 * storing the values and gradients of primal and dual solutions, for
2489 * example. These fields are needed in the three functions that do the
2490 * integration on cells, and regular and irregular faces, respectively.
2494 * There are three reasonable ways to provide these fields: first, as
2495 * local variables in the function that needs them; second, as member
2496 * variables of this class; third, as arguments passed to that function.
2500 * These three alternatives all have drawbacks: the third that their
2501 * number is not negligible and would make calling these functions a
2502 * lengthy enterprise. The second has the drawback that it disallows
2503 * parallelization, since the threads that will compute the error
2504 * estimate have to have their own copies of these variables each, so
2505 * member variables of the enclosing class will not work. The first
2506 * approach, although straightforward, has a subtle but important
2507 * drawback: we will call these functions over and over again, many
2508 * thousands of times maybe; it now turns out that allocating
2509 * vectors and other objects that need memory from the heap is an
2510 * expensive business in terms of run-time, since memory allocation is
2511 * expensive when several threads are involved. It is thus
2512 * significantly better to allocate the memory only once, and recycle
2513 * the objects as often as possible.
2517 * What to do? Our answer is to use a variant of the third strategy.
2518 * In fact, this is exactly what the WorkStream concept is supposed to
2519 * do (we have already introduced it above, but see also @ref threads).
2520 * To avoid that we have to give these functions a dozen or so
2521 * arguments, we pack all these variables into two structures, one which
2522 * is used for the computations on cells, the other doing them on the
2523 * faces. Both are then joined into the WeightedResidualScratchData class
2524 * that will serve as the "scratch data" class of the WorkStream concept:
2529 * FEValues<dim> fe_values;
2530 * const SmartPointer<const Function<dim>> right_hand_side;
2532 * std::vector<double> cell_residual;
2533 * std::vector<double> rhs_values;
2534 * std::vector<double> dual_weights;
2535 * std::vector<double> cell_laplacians;
2536 * CellData(const FiniteElement<dim> &fe,
2537 * const Quadrature<dim> & quadrature,
2538 * const Function<dim> & right_hand_side);
2539 * CellData(const CellData &cell_data);
2544 * FEFaceValues<dim> fe_face_values_cell;
2545 * FEFaceValues<dim> fe_face_values_neighbor;
2546 * FESubfaceValues<dim> fe_subface_values_cell;
2548 * std::vector<double> jump_residual;
2549 * std::vector<double> dual_weights;
2550 * typename std::vector<Tensor<1, dim>> cell_grads;
2551 * typename std::vector<Tensor<1, dim>> neighbor_grads;
2552 * FaceData(const FiniteElement<dim> & fe,
2553 * const Quadrature<dim - 1> &face_quadrature);
2554 * FaceData(const FaceData &face_data);
2557 * struct WeightedResidualScratchData
2559 * WeightedResidualScratchData(
2560 * const FiniteElement<dim> & primal_fe,
2561 * const Quadrature<dim> & primal_quadrature,
2562 * const Quadrature<dim - 1> &primal_face_quadrature,
2563 * const Function<dim> & rhs_function,
2564 * const Vector<double> & primal_solution,
2565 * const Vector<double> & dual_weights);
2567 * WeightedResidualScratchData(
2568 * const WeightedResidualScratchData &scratch_data);
2570 * CellData cell_data;
2571 * FaceData face_data;
2572 * Vector<double> primal_solution;
2573 * Vector<double> dual_weights;
2579 * WorkStream::run generally wants both a scratch object and a copy
2580 * object. Here, for reasons similar to what we had in @ref step_9 "step-9" when
2581 * discussing the computation of an approximation of the gradient, we
2582 * don't actually need a
"copy data" structure. Since
WorkStream insists
2583 * on having
one of these, we just declare an empty structure that does
2584 * nothing other than being there.
2587 *
struct WeightedResidualCopyData
2594 * Regarding the evaluation of the error estimator, we have
one driver
2599 *
void estimate_error(
Vector<
float> &error_indicators) const;
2601 *
void estimate_on_one_cell(const active_cell_iterator & cell,
2602 * WeightedResidualScratchData &scratch_data,
2603 * WeightedResidualCopyData & copy_data,
2604 *
Vector<
float> & error_indicators,
2605 * FaceIntegrals &face_integrals) const;
2609 * Then we have
functions that do the actual integration of the error
2610 * representation formula. They will treat the terms on the cell
2611 * interiors, on those faces that have no hanging nodes, and on those
2612 * faces with hanging nodes, respectively:
2615 *
void integrate_over_cell(const active_cell_iterator &cell,
2616 * const
Vector<
double> & primal_solution,
2617 * const
Vector<
double> & dual_weights,
2619 *
Vector<
float> &error_indicators) const;
2621 *
void integrate_over_regular_face(const active_cell_iterator &cell,
2622 * const
unsigned int face_no,
2623 * const
Vector<
double> &primal_solution,
2624 * const
Vector<
double> &dual_weights,
2625 * FaceData & face_data,
2626 * FaceIntegrals &face_integrals) const;
2627 *
void integrate_over_irregular_face(const active_cell_iterator &cell,
2628 * const
unsigned int face_no,
2629 * const
Vector<
double> &primal_solution,
2630 * const
Vector<
double> &dual_weights,
2631 * FaceData & face_data,
2632 * FaceIntegrals &face_integrals) const;
2639 * In the implementation of this class, we
first have the constructors of
2640 * the <code>
CellData</code> and <code>FaceData</code> member classes, and
2641 * the <code>WeightedResidual</code> constructor. They only initialize
2642 * fields to their correct lengths, so we do not have to discuss them in
2646 * template <
int dim>
2650 * const
Function<dim> & right_hand_side)
2655 * , right_hand_side(&right_hand_side)
2657 * , rhs_values(quadrature.size())
2658 * , dual_weights(quadrature.size())
2659 * , cell_laplacians(quadrature.size())
2664 *
template <
int dim>
2665 * WeightedResidual<dim>::CellData::CellData(
const CellData &cell_data)
2666 * : fe_values(cell_data.fe_values.get_fe(),
2667 * cell_data.fe_values.get_quadrature(),
2670 * , right_hand_side(cell_data.right_hand_side)
2672 * , rhs_values(cell_data.rhs_values)
2673 * , dual_weights(cell_data.dual_weights)
2674 * , cell_laplacians(cell_data.cell_laplacians)
2679 *
template <
int dim>
2680 * WeightedResidual<dim>::FaceData::FaceData(
2683 * : fe_face_values_cell(fe,
2687 * , fe_face_values_neighbor(fe,
2693 *
const unsigned int n_face_q_points = face_quadrature.size();
2695 * jump_residual.resize(n_face_q_points);
2696 * dual_weights.resize(n_face_q_points);
2697 * cell_grads.resize(n_face_q_points);
2698 * neighbor_grads.resize(n_face_q_points);
2703 *
template <
int dim>
2704 * WeightedResidual<dim>::FaceData::FaceData(
const FaceData &face_data)
2705 * : fe_face_values_cell(face_data.fe_face_values_cell.get_fe(),
2706 * face_data.fe_face_values_cell.get_quadrature(),
2709 * , fe_face_values_neighbor(
2710 * face_data.fe_face_values_neighbor.get_fe(),
2711 * face_data.fe_face_values_neighbor.get_quadrature(),
2714 * , fe_subface_values_cell(
2715 * face_data.fe_subface_values_cell.get_fe(),
2716 * face_data.fe_subface_values_cell.get_quadrature(),
2718 * , jump_residual(face_data.jump_residual)
2719 * , dual_weights(face_data.dual_weights)
2720 * , cell_grads(face_data.cell_grads)
2721 * , neighbor_grads(face_data.neighbor_grads)
2726 *
template <
int dim>
2727 * WeightedResidual<dim>::WeightedResidualScratchData::
2728 * WeightedResidualScratchData(
2735 * : cell_data(primal_fe, primal_quadrature, rhs_function)
2736 * , face_data(primal_fe, primal_face_quadrature)
2737 * , primal_solution(primal_solution)
2738 * , dual_weights(dual_weights)
2741 *
template <
int dim>
2742 * WeightedResidual<dim>::WeightedResidualScratchData::
2743 * WeightedResidualScratchData(
2744 *
const WeightedResidualScratchData &scratch_data)
2745 * : cell_data(scratch_data.cell_data)
2746 * , face_data(scratch_data.face_data)
2747 * , primal_solution(scratch_data.primal_solution)
2748 * , dual_weights(scratch_data.dual_weights)
2753 *
template <
int dim>
2754 * WeightedResidual<dim>::WeightedResidual(
2762 *
const DualFunctional::DualFunctionalBase<dim> &dual_functional)
2763 * : Base<dim>(coarse_grid)
2764 * , PrimalSolver<dim>(coarse_grid,
2770 * , DualSolver<dim>(coarse_grid,
2780 * The next five
functions are boring, as they simply relay their work to
2781 * the base classes. The
first calls the primal and dual solvers in
2782 *
parallel,
while postprocessing the solution and retrieving the number
2783 * of degrees of freedom is done by the primal
class.
2786 *
template <
int dim>
2787 *
void WeightedResidual<dim>::solve_problem()
2798 *
template <
int dim>
2799 *
void WeightedResidual<dim>::solve_primal_problem()
2801 * PrimalSolver<dim>::solve_problem();
2804 *
template <
int dim>
2805 *
void WeightedResidual<dim>::solve_dual_problem()
2807 * DualSolver<dim>::solve_problem();
2811 *
template <
int dim>
2812 *
void WeightedResidual<dim>::postprocess(
2813 *
const Evaluation::EvaluationBase<dim> &postprocessor)
const
2815 * PrimalSolver<dim>::postprocess(postprocessor);
2819 *
template <
int dim>
2820 *
unsigned int WeightedResidual<dim>::n_dofs() const
2822 *
return PrimalSolver<dim>::n_dofs();
2829 * Now, it is becoming more interesting: the <code>refine_grid()</code>
2830 *
function asks the error estimator to compute the cell-wise error
2831 * indicators, then uses their absolute values
for mesh refinement.
2834 *
template <
int dim>
2835 *
void WeightedResidual<dim>::refine_grid()
2839 * First
call the
function that computes the cell-wise and global error:
2843 * estimate_error(error_indicators);
2847 * Then note that marking cells
for refinement or coarsening only works
2848 *
if all indicators are positive, to allow their comparison. Thus, drop
2849 * the signs on all these indicators:
2852 *
for (
float &error_indicator : error_indicators)
2853 * error_indicator =
std::fabs(error_indicator);
2857 * Finally, we can select between different strategies
for
2858 * refinement. The
default here is to
refine those cells with the
2859 * largest error indicators that make up
for a total of 80 per cent of
2860 * the error,
while we
coarsen those with the smallest indicators that
2861 * make up
for the bottom 2 per cent of the error.
2874 * Since we want to output both the primal and the dual solution, we
2875 * overload the <code>output_solution</code>
function. The only
2876 * interesting feature of
this function is that the primal and dual
2877 * solutions are defined on different finite element spaces, which is not
2878 * the format the <code>
DataOut</code>
class expects. Thus, we have to
2879 * transfer them to a common finite element space. Since we want the
2880 * solutions only to see them qualitatively, we contend ourselves with
2881 * interpolating the dual solution to the (smaller) primal space. For the
2882 * interpolation, there is a library
function, that takes a
2884 * constraints. The rest is standard.
2887 *
template <
int dim>
2888 *
void WeightedResidual<dim>::output_solution() const
2892 * primal_hanging_node_constraints);
2893 * primal_hanging_node_constraints.
close();
2894 *
Vector<double> dual_solution(PrimalSolver<dim>::dof_handler.n_dofs());
2896 * DualSolver<dim>::solution,
2897 * PrimalSolver<dim>::dof_handler,
2898 * primal_hanging_node_constraints,
2906 * Add the data vectors
for which we want output. Add them both, the
2908 * wish to write to output:
2911 * data_out.
add_data_vector(PrimalSolver<dim>::solution,
"primal_solution");
2916 * std::ofstream out(
"solution-" +
std::to_string(this->refinement_cycle) +
2925 * <a name=
"Estimatingerrors"></a>
2926 * <h3>Estimating errors</h3>
2931 * <a name=
"Errorestimationdriverfunctions"></a>
2932 * <h4>Error estimation driver
functions</h4>
2936 * As
for the actual computation of error estimates, let
's start with the
2937 * function that drives all this, i.e. calls those functions that actually
2938 * do the work, and finally collects the results.
2941 * template <int dim>
2943 * WeightedResidual<dim>::estimate_error(Vector<float> &error_indicators) const
2947 * The first task in computing the error is to set up vectors that
2948 * denote the primal solution, and the weights (z-z_h)=(z-I_hz), both in
2949 * the finite element space for which we have computed the dual
2950 * solution. For this, we have to interpolate the primal solution to the
2951 * dual finite element space, and to subtract the interpolation of the
2952 * computed dual solution to the primal finite element
2953 * space. Fortunately, the library provides functions for the
2954 * interpolation into larger or smaller finite element spaces, so this
2955 * is mostly obvious.
2959 * First, let's
do that
for the primal solution: it is cell-wise
2960 * interpolated into the finite element space in which we have solved
2961 * the dual problem: But, again as in the
2962 * <code>WeightedResidual::output_solution</code>
function we
first need
2964 * constraints, but
this time of the dual finite element space.
2969 * dual_hanging_node_constraints);
2970 * dual_hanging_node_constraints.close();
2971 *
Vector<double> primal_solution(DualSolver<dim>::dof_handler.n_dofs());
2973 * PrimalSolver<dim>::solution,
2974 * DualSolver<dim>::dof_handler,
2975 * dual_hanging_node_constraints,
2980 * Then
for computing the interpolation of the numerically approximated
2981 * dual solution z into the finite element space of the primal solution
2982 * and subtracting it from z: use the
2983 * <code>interpolate_difference</code>
function, that gives (z-I_hz) in
2984 * the element space of the dual solution.
2989 * primal_hanging_node_constraints);
2990 * primal_hanging_node_constraints.close();
2991 *
Vector<double> dual_weights(DualSolver<dim>::dof_handler.n_dofs());
2993 * dual_hanging_node_constraints,
2994 * DualSolver<dim>::solution,
2995 * PrimalSolver<dim>::dof_handler,
2996 * primal_hanging_node_constraints,
3001 * Note that
this could probably have been more efficient since those
3002 * constraints have been used previously when assembling
matrix and
3003 * right hand side
for the primal problem and writing out the dual
3004 * solution. We leave the optimization of the program in
this respect as
3009 * Having computed the dual weights we now proceed with computing the
3010 * cell and face residuals of the primal solution. First we
set up a map
3011 * between face iterators and their jump term contributions of faces to
3012 * the error estimator. The reason is that we compute the jump terms
3013 * only once, from
one side of the face, and want to collect them only
3014 * afterwards when looping over all cells a
second time.
3018 * We initialize
this map already with a
value of -1e20
for all faces,
3019 * since
this value will stand out in the results
if something should go
3020 * wrong and we fail to compute the
value for a face
for some
3021 * reason. Secondly,
this initialization already makes the std::map
3022 *
object allocate all objects it may possibly need. This is important
3023 * since we will write into
this structure from
parallel threads,
3024 * and doing so would not be thread-safe
if the map needed to allocate
3025 * memory and thereby reshape its data structures. In other words, the
3026 *
initial initialization relieves us from the necessity to synchronize
3027 * the threads through a mutex each time they write to (and modify the
3028 * structure of)
this map.
3031 * FaceIntegrals face_integrals;
3032 *
for (
const auto &cell :
3033 * DualSolver<dim>::dof_handler.active_cell_iterators())
3034 *
for (
const auto &face : cell->face_iterators())
3035 * face_integrals[face] = -1e20;
3037 *
auto worker = [
this,
3038 * &error_indicators,
3039 * &face_integrals](
const active_cell_iterator & cell,
3040 * WeightedResidualScratchData &scratch_data,
3041 * WeightedResidualCopyData & copy_data) {
3042 * this->estimate_on_one_cell(
3043 * cell, scratch_data, copy_data, error_indicators, face_integrals);
3046 *
auto do_nothing_copier =
3047 * std::function<void(const WeightedResidualCopyData &)>();
3052 * estimators for all cells in
parallel:
3056 * DualSolver<dim>::dof_handler.begin_active(),
3057 * DualSolver<dim>::dof_handler.
end(),
3059 * do_nothing_copier,
3060 * WeightedResidualScratchData(*DualSolver<dim>::fe,
3061 * *DualSolver<dim>::quadrature,
3062 * *DualSolver<dim>::face_quadrature,
3063 * *this->rhs_function,
3066 * WeightedResidualCopyData());
3070 * Once the error contributions are computed,
sum them up. For this,
3071 * note that the cell terms are already
set, and that only the edge
3072 * terms need to be collected. Thus,
loop over all cells and their
3073 * faces, make sure that the contributions of each of the faces are
3074 * there, and add them up. Only take minus
one half of the jump term,
3075 * since the other half will be taken by the neighboring cell.
3078 *
unsigned int present_cell = 0;
3079 * for (const auto &cell :
3080 * DualSolver<dim>::dof_handler.active_cell_iterators())
3082 *
for (
const auto &face : cell->face_iterators())
3084 *
Assert(face_integrals.find(face) != face_integrals.end(),
3086 * error_indicators(present_cell) -= 0.5 * face_integrals[face];
3090 * std::cout <<
" Estimated error="
3091 * << std::accumulate(error_indicators.begin(),
3092 * error_indicators.end(),
3101 * <a name=
"Estimatingonasinglecell"></a>
3102 * <h4>Estimating on a single cell</h4>
3106 * Next we have the
function that is called to estimate the error on a
3107 * single cell. The
function may be called multiple times
if the library was
3108 * configured to use multithreading. Here it goes:
3111 *
template <
int dim>
3112 *
void WeightedResidual<dim>::estimate_on_one_cell(
3113 *
const active_cell_iterator & cell,
3114 * WeightedResidualScratchData &scratch_data,
3115 * WeightedResidualCopyData & copy_data,
3117 * FaceIntegrals & face_integrals)
const
3121 * Because of
WorkStream, estimate_on_one_cell requires a CopyData
object
3122 * even
if it is no used. The next line silences a warning about
this
3130 * First task on each cell is to compute the cell residual
3131 * contributions of
this cell, and put them into the
3132 * <code>error_indicators</code> variable:
3135 * integrate_over_cell(cell,
3136 * scratch_data.primal_solution,
3137 * scratch_data.dual_weights,
3138 * scratch_data.cell_data,
3139 * error_indicators);
3143 * After computing the cell terms, turn to the face terms. For
this,
3144 *
loop over all faces of the present cell, and see whether
3145 * something needs to be computed on it:
3152 * First,
if this face is part of the boundary, then there is
3153 * nothing to
do. However, to make things easier when summing up
3154 * the contributions of the faces of cells, we enter
this face
3155 * into the list of faces with a
zero contribution to the error.
3158 *
if (cell->face(face_no)->at_boundary())
3160 * face_integrals[cell->face(face_no)] = 0;
3166 * Next, note that since we want to compute the jump terms on
3167 * each face only once although we access it twice (
if it is not
3168 * at the boundary), we have to define some rules who is
3169 * responsible
for computing on a face:
3173 * First,
if the neighboring cell is on the same
level as
this
3174 *
one, i.e. neither further refined not coarser, then the
one
3175 * with the lower index within
this level does the work. In
3176 * other words:
if the other
one has a lower index, then skip
3177 * work on
this face:
3180 *
if ((cell->neighbor(face_no)->has_children() ==
false) &&
3181 * (cell->neighbor(face_no)->level() == cell->level()) &&
3182 * (cell->neighbor(face_no)->index() < cell->index()))
3187 * Likewise, we
always work from the coarser cell
if this and
3188 * its neighbor differ in refinement. Thus,
if the neighboring
3189 * cell is less refined than the present
one, then
do nothing
3190 * since we integrate over the subfaces when we visit the coarse
3194 *
if (cell->at_boundary(face_no) ==
false)
3195 *
if (cell->neighbor(face_no)->level() < cell->level())
3201 * Now we know that we are in charge here, so actually compute
3202 * the face jump terms. If the face is a regular
one, i.e. the
3203 * other side
's cell is neither coarser not finer than this
3204 * cell, then call one function, and if the cell on the other
3205 * side is further refined, then use another function. Note that
3206 * the case that the cell on the other side is coarser cannot
3207 * happen since we have decided above that we handle this case
3208 * when we pass over that other cell.
3211 * if (cell->face(face_no)->has_children() == false)
3212 * integrate_over_regular_face(cell,
3214 * scratch_data.primal_solution,
3215 * scratch_data.dual_weights,
3216 * scratch_data.face_data,
3219 * integrate_over_irregular_face(cell,
3221 * scratch_data.primal_solution,
3222 * scratch_data.dual_weights,
3223 * scratch_data.face_data,
3232 * <a name="Computingcelltermerrorcontributions"></a>
3233 * <h4>Computing cell term error contributions</h4>
3237 * As for the actual computation of the error contributions, first turn to
3241 * template <int dim>
3242 * void WeightedResidual<dim>::integrate_over_cell(
3243 * const active_cell_iterator &cell,
3244 * const Vector<double> & primal_solution,
3245 * const Vector<double> & dual_weights,
3246 * CellData & cell_data,
3247 * Vector<float> & error_indicators) const
3251 * The tasks to be done are what appears natural from looking at the
3252 * error estimation formula: first get the right hand side and Laplacian
3253 * of the numerical solution at the quadrature points for the cell
3257 * cell_data.fe_values.reinit(cell);
3258 * cell_data.right_hand_side->value_list(
3259 * cell_data.fe_values.get_quadrature_points(), cell_data.rhs_values);
3260 * cell_data.fe_values.get_function_laplacians(primal_solution,
3261 * cell_data.cell_laplacians);
3265 * ...then get the dual weights...
3268 * cell_data.fe_values.get_function_values(dual_weights,
3269 * cell_data.dual_weights);
3273 * ...and finally build the sum over all quadrature points and store it
3274 * with the present cell:
3278 * for (unsigned int p = 0; p < cell_data.fe_values.n_quadrature_points; ++p)
3279 * sum += ((cell_data.rhs_values[p] + cell_data.cell_laplacians[p]) *
3280 * cell_data.dual_weights[p] * cell_data.fe_values.JxW(p));
3281 * error_indicators(cell->active_cell_index()) += sum;
3288 * <a name="Computingedgetermerrorcontributions1"></a>
3289 * <h4>Computing edge term error contributions -- 1</h4>
3293 * On the other hand, computation of the edge terms for the error estimate
3294 * is not so simple. First, we have to distinguish between faces with and
3295 * without hanging nodes. Because it is the simple case, we first consider
3296 * the case without hanging nodes on a face (let's
call this the `regular
'
3300 * template <int dim>
3301 * void WeightedResidual<dim>::integrate_over_regular_face(
3302 * const active_cell_iterator &cell,
3303 * const unsigned int face_no,
3304 * const Vector<double> & primal_solution,
3305 * const Vector<double> & dual_weights,
3306 * FaceData & face_data,
3307 * FaceIntegrals & face_integrals) const
3309 * const unsigned int n_q_points =
3310 * face_data.fe_face_values_cell.n_quadrature_points;
3314 * The first step is to get the values of the gradients at the
3315 * quadrature points of the finite element field on the present
3316 * cell. For this, initialize the <code>FEFaceValues</code> object
3317 * corresponding to this side of the face, and extract the gradients
3318 * using that object.
3321 * face_data.fe_face_values_cell.reinit(cell, face_no);
3322 * face_data.fe_face_values_cell.get_function_gradients(
3323 * primal_solution, face_data.cell_grads);
3327 * The second step is then to extract the gradients of the finite
3328 * element solution at the quadrature points on the other side of the
3329 * face, i.e. from the neighboring cell.
3333 * For this, do a sanity check before: make sure that the neighbor
3334 * actually exists (yes, we should not have come here if the neighbor
3335 * did not exist, but in complicated software there are bugs, so better
3336 * check this), and if this is not the case throw an error.
3339 * Assert(cell->neighbor(face_no).state() == IteratorState::valid,
3340 * ExcInternalError());
3343 * If we have that, then we need to find out with which face of the
3344 * neighboring cell we have to work, i.e. the <code>how-many'th</code> the
3345 * neighbor the present cell is of the cell behind the present face. For
3346 *
this, there is a
function, and we put the result into a variable with
3347 * the name <code>neighbor_neighbor</code>:
3350 *
const unsigned int neighbor_neighbor =
3351 * cell->neighbor_of_neighbor(face_no);
3354 * Then define an abbreviation
for the neighbor cell, initialize the
3356 * gradients on that cell:
3359 *
const active_cell_iterator neighbor = cell->neighbor(face_no);
3360 * face_data.fe_face_values_neighbor.reinit(neighbor, neighbor_neighbor);
3361 * face_data.fe_face_values_neighbor.get_function_gradients(
3362 * primal_solution, face_data.neighbor_grads);
3366 * Now that we have the gradients on
this and the neighboring cell,
3367 * compute the jump residual by multiplying the jump in the gradient
3368 * with the normal vector:
3371 *
for (
unsigned int p = 0; p < n_q_points; ++p)
3372 * face_data.jump_residual[p] =
3373 * ((face_data.cell_grads[p] - face_data.neighbor_grads[p]) *
3374 * face_data.fe_face_values_cell.normal_vector(p));
3378 * Next get the dual weights
for this face:
3381 * face_data.fe_face_values_cell.get_function_values(dual_weights,
3382 * face_data.dual_weights);
3386 * Finally, we have to compute the
sum over jump residuals, dual
3387 * weights, and quadrature weights, to get the result
for this face:
3390 *
double face_integral = 0;
3391 *
for (
unsigned int p = 0; p < n_q_points; ++p)
3393 * (face_data.jump_residual[p] * face_data.dual_weights[p] *
3394 * face_data.fe_face_values_cell.JxW(p));
3398 * Double check that the element already exists and that it was not
3399 * already written to...
3402 *
Assert(face_integrals.find(cell->face(face_no)) != face_integrals.end(),
3408 * ...then store computed
value at assigned location. Note that the
3409 * stored
value does not contain the factor 1/2 that appears in the
3410 * error representation. The reason is that the term actually does not
3412 * only appears
if we write it as a
sum over all cells and all faces of
3413 * each cell; we thus visit the same face twice. We take account of
this
3414 * by
using this factor -1/2 later, when we
sum up the contributions
for
3415 * each cell individually.
3418 * face_integrals[cell->face(face_no)] = face_integral;
3425 * <a name=
"Computingedgetermerrorcontributions2"></a>
3426 * <h4>Computing edge term error contributions -- 2</h4>
3430 * We are still missing the
case of faces with hanging nodes. This is what
3431 * is covered in
this function:
3434 *
template <
int dim>
3435 *
void WeightedResidual<dim>::integrate_over_irregular_face(
3436 *
const active_cell_iterator &cell,
3437 *
const unsigned int face_no,
3440 * FaceData & face_data,
3441 * FaceIntegrals & face_integrals)
const
3445 * First again two abbreviations, and some consistency checks whether
3446 * the
function is called only on faces
for which it is supposed to be
3450 *
const unsigned int n_q_points =
3451 * face_data.fe_face_values_cell.n_quadrature_points;
3455 * cell->neighbor(face_no);
3462 * Then find out which neighbor the present cell is of the adjacent
3463 * cell. Note that we will operate on the children of
this adjacent
3464 * cell, but that their orientation is the same as that of their mother,
3465 * i.e. the neighbor direction is the same.
3468 *
const unsigned int neighbor_neighbor =
3469 * cell->neighbor_of_neighbor(face_no);
3473 * Then simply
do everything we did in the previous
function for one
3474 * face
for all the sub-faces now:
3477 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
3482 * Start with some checks again: get an iterator pointing to the
3483 * cell behind the present subface and check whether its face is a
3484 * subface of the
one we are considering. If that were not the
case,
3485 * then there would be either a bug in the
3486 * <code>neighbor_neighbor</code>
function called above, or -- worse
3487 * -- some
function in the library did not keep to some underlying
3488 * assumptions about cells, their children, and their faces. In any
3489 *
case, even though
this assertion should not be triggered, it does
3490 * not harm to be cautious, and in optimized mode computations the
3491 * assertion will be removed anyway.
3494 *
const active_cell_iterator neighbor_child =
3495 * cell->neighbor_child_on_subface(face_no, subface_no);
3496 *
Assert(neighbor_child->face(neighbor_neighbor) ==
3497 * cell->face(face_no)->child(subface_no),
3502 * Now start the work by again getting the gradient of the solution
3503 *
first at
this side of the interface,
3506 * face_data.fe_subface_values_cell.reinit(cell, face_no, subface_no);
3507 * face_data.fe_subface_values_cell.get_function_gradients(
3508 * primal_solution, face_data.cell_grads);
3511 * then at the other side,
3514 * face_data.fe_face_values_neighbor.reinit(neighbor_child,
3515 * neighbor_neighbor);
3516 * face_data.fe_face_values_neighbor.get_function_gradients(
3517 * primal_solution, face_data.neighbor_grads);
3521 * and
finally building the jump residuals. Since we take the normal
3522 * vector from the other cell
this time, revert the
sign of the
3523 *
first term compared to the other
function:
3526 *
for (
unsigned int p = 0; p < n_q_points; ++p)
3527 * face_data.jump_residual[p] =
3528 * ((face_data.neighbor_grads[p] - face_data.cell_grads[p]) *
3529 * face_data.fe_face_values_neighbor.normal_vector(p));
3533 * Then get dual weights:
3536 * face_data.fe_face_values_neighbor.get_function_values(
3537 * dual_weights, face_data.dual_weights);
3541 * At last,
sum up the contribution of
this sub-face, and
set it in
3545 *
double face_integral = 0;
3546 *
for (
unsigned int p = 0; p < n_q_points; ++p)
3548 * (face_data.jump_residual[p] * face_data.dual_weights[p] *
3549 * face_data.fe_face_values_neighbor.JxW(p));
3550 * face_integrals[neighbor_child->face(neighbor_neighbor)] =
3556 * Once the contributions of all sub-faces are computed,
loop over all
3557 * sub-faces to collect and store them with the mother face
for simple
3558 * use when later collecting the error terms of cells. Again make safety
3559 * checks that the entries
for the sub-faces have been computed and
do
3564 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
3567 *
Assert(face_integrals.find(face->child(subface_no)) !=
3568 * face_integrals.end(),
3570 *
Assert(face_integrals[face->child(subface_no)] != -1e20,
3573 *
sum += face_integrals[face->child(subface_no)];
3577 * Finally store the
value with the parent face.
3580 * face_integrals[face] =
sum;
3589 * <a name=
"Asimulationframework"></a>
3590 * <h3>
A simulation framework</h3>
3594 * In the previous example program, we have had two
functions that were used
3595 * to drive the process of solving on subsequently finer grids. We extend
3596 *
this here to allow
for a number of parameters to be passed to these
3597 *
functions, and put all of that into framework
class.
3601 * You will have noted that
this program is built up of a number of small
3602 * parts (evaluation
functions, solver classes implementing various
3603 * refinement methods, different dual functionals, different problem and
3604 * data descriptions), which makes the program relatively simple to extend,
3605 * but also allows to solve a large number of different problems by
3606 * replacing
one part by another. We reflect
this flexibility by declaring a
3607 * structure in the following framework
class that holds a number of
3608 * parameters that may be
set to test various combinations of the parts of
3609 *
this program, and which can be used to test it at various problems and
3610 * discretizations in a simple way.
3613 *
template <
int dim>
3619 * First, we declare two abbreviations
for simple use of the respective
3623 *
using Evaluator = Evaluation::EvaluationBase<dim>;
3624 *
using EvaluatorList = std::list<Evaluator *>;
3629 * Then we have the structure which declares all the parameters that may
3630 * be
set. In the
default constructor of the structure, these values are
3631 * all
set to
default values,
for simple use.
3634 *
struct ProblemDescription
3638 * First allow
for the degrees of the piecewise polynomials by which the
3639 * primal and dual problems will be discretized. They
default to (bi-,
3640 * tri-)linear ansatz
functions for the primal, and (bi-, tri-)quadratic
3641 * ones for the dual problem. If a refinement criterion is chosen that
3642 * does not need the solution of a dual problem, the
value of the dual
3643 * finite element degree is of course ignored.
3646 *
unsigned int primal_fe_degree;
3647 *
unsigned int dual_fe_degree;
3651 * Then have an
object that describes the problem type, i.
e. right hand
3652 * side, domain, boundary values, etc. The pointer needed here defaults
3653 * to the Null pointer, i.
e. you will have to
set it in actual instances
3654 * of this
object to make it useful.
3657 * std::unique_ptr<const Data::SetUpBase<dim>> data;
3661 * Since we allow to use different refinement criteria (global
3662 * refinement, refinement by the Kelly error indicator, possibly with a
3663 * weight, and using the dual estimator), define a number of
enumeration
3664 * values, and subsequently a variable of that type. It will default to
3665 * <code>dual_weighted_error_estimator</code>.
3668 * enum RefinementCriterion
3670 * dual_weighted_error_estimator,
3671 * global_refinement,
3673 * weighted_kelly_indicator
3676 * RefinementCriterion refinement_criterion;
3680 * Next, an
object that describes the dual functional. It is only needed
3681 *
if the dual weighted residual refinement is chosen, and also defaults
3682 * to a Null pointer.
3685 * std::unique_ptr<const DualFunctional::DualFunctionalBase<dim>>
3690 * Then a list of evaluation objects. Its
default value is empty,
3691 * i.e. no evaluation objects.
3694 * EvaluatorList evaluator_list;
3698 * Next to last, a
function that is used as a weight to the
3699 * <code>RefinementWeightedKelly</code>
class. The
default value of
this
3700 * pointer is
zero, but you have to
set it to some other
value if you
3701 * want to use the <code>weighted_kelly_indicator</code> refinement
3705 * std::unique_ptr<const Function<dim>> kelly_weight;
3709 * Finally, we have a variable that denotes the maximum number of
3710 * degrees of freedom we allow
for the (primal) discretization. If it is
3711 * exceeded, we stop the process of solving and intermittent mesh
3712 * refinement. Its
default value is 20,000.
3715 *
unsigned int max_degrees_of_freedom;
3719 * Finally the
default constructor of
this class:
3722 * ProblemDescription();
3727 * The driver framework
class only has
one method which calls solver and
3728 * mesh refinement intermittently, and does some other small tasks in
3729 * between. Since it does not need data besides the parameters given to
3730 * it, we make it
static:
3733 *
static void run(
const ProblemDescription &descriptor);
3739 * As
for the implementation,
first the constructor of the parameter object,
3740 * setting all values to their defaults:
3743 *
template <
int dim>
3744 * Framework<dim>::ProblemDescription::ProblemDescription()
3745 * : primal_fe_degree(1)
3746 * , dual_fe_degree(2)
3747 * , refinement_criterion(dual_weighted_error_estimator)
3748 * , max_degrees_of_freedom(20000)
3755 * Then the
function which drives the whole process:
3758 *
template <
int dim>
3772 * then a
set of finite elements and appropriate quadrature formula:
3775 *
const FE_Q<dim> primal_fe(descriptor.primal_fe_degree);
3776 *
const FE_Q<dim> dual_fe(descriptor.dual_fe_degree);
3777 *
const QGauss<dim> quadrature(descriptor.dual_fe_degree + 1);
3778 *
const QGauss<dim - 1> face_quadrature(descriptor.dual_fe_degree + 1);
3782 * Next, select
one of the classes implementing different refinement
3786 * std::unique_ptr<LaplaceSolver::Base<dim>> solver;
3787 *
switch (descriptor.refinement_criterion)
3789 *
case ProblemDescription::dual_weighted_error_estimator:
3792 * std_cxx14::make_unique<LaplaceSolver::WeightedResidual<dim>>(
3798 * descriptor.data->get_right_hand_side(),
3799 * descriptor.data->get_boundary_values(),
3800 * *descriptor.dual_functional);
3804 *
case ProblemDescription::global_refinement:
3807 * std_cxx14::make_unique<LaplaceSolver::RefinementGlobal<dim>>(
3812 * descriptor.data->get_right_hand_side(),
3813 * descriptor.data->get_boundary_values());
3817 *
case ProblemDescription::kelly_indicator:
3820 * std_cxx14::make_unique<LaplaceSolver::RefinementKelly<dim>>(
3825 * descriptor.data->get_right_hand_side(),
3826 * descriptor.data->get_boundary_values());
3830 *
case ProblemDescription::weighted_kelly_indicator:
3832 * solver = std_cxx14::make_unique<
3833 * LaplaceSolver::RefinementWeightedKelly<dim>>(
3838 * descriptor.data->get_right_hand_side(),
3839 * descriptor.data->get_boundary_values(),
3840 * *descriptor.kelly_weight);
3850 * Now that all objects are in place,
run the main
loop. The stopping
3851 * criterion is implemented at the bottom of the
loop.
3855 * In the
loop,
first set the
new cycle number, then solve the problem,
3856 * output its solution(s),
apply the evaluation objects to it, then decide
3857 * whether we want to
refine the mesh further and solve again on
this
3858 * mesh, or jump out of the
loop.
3861 *
for (
unsigned int step = 0;
true; ++step)
3863 * std::cout <<
"Refinement cycle: " << step << std::endl;
3865 * solver->set_refinement_cycle(step);
3866 * solver->solve_problem();
3867 * solver->output_solution();
3869 * std::cout <<
" Number of degrees of freedom=" << solver->n_dofs()
3872 *
for (
const auto &evaluator : descriptor.evaluator_list)
3874 * evaluator->set_refinement_cycle(step);
3875 * solver->postprocess(*evaluator);
3879 *
if (solver->n_dofs() < descriptor.max_degrees_of_freedom)
3880 * solver->refine_grid();
3887 * Clean up the screen after the
loop has
run:
3890 * std::cout << std::endl;
3900 * <a name=
"Themainfunction"></a>
3901 * <h3>The main
function</h3>
3905 * Here
finally comes the main
function. It drives the whole process by
3906 * specifying a
set of parameters to be used
for the simulation (polynomial
3907 * degrees, evaluation and dual functionals, etc), and passes them packed into
3908 * a structure to the frame work
class above.
3915 *
using namespace Step14;
3919 * Describe the problem we want to solve here by passing a descriptor
3920 *
object to the
function doing the rest of the work:
3923 *
const unsigned int dim = 2;
3924 * Framework<dim>::ProblemDescription descriptor;
3928 * First
set the refinement criterion we wish to use:
3931 * descriptor.refinement_criterion =
3932 * Framework<dim>::ProblemDescription::dual_weighted_error_estimator;
3935 * Here, we could as well have used <code>global_refinement</code> or
3936 * <code>weighted_kelly_indicator</code>. Note that the information
3937 * given about dual finite elements, dual functional, etc is only
3938 * important
for the given choice of refinement criterion, and is
3939 * ignored otherwise.
3943 * Then
set the polynomial degrees of primal and dual problem. We choose
3944 * here bi-linear and bi-quadratic ones:
3947 * descriptor.primal_fe_degree = 1;
3948 * descriptor.dual_fe_degree = 2;
3952 * Then
set the description of the test
case, i.e. domain, boundary
3953 * values, and right hand side. These are prepackaged in classes. We
3954 * take here the description of <code>Exercise_2_3</code>, but you can
3955 * also use <code>CurvedRidges@<dim@></code>:
3959 * std_cxx14::make_unique<Data::SetUp<Data::Exercise_2_3<dim>, dim>>();
3963 * Next
set first a dual functional, then a list of evaluation
3964 * objects. We choose as
default the evaluation of the
value at an
3965 * evaluation
point, represented by the classes
3966 * <code>PointValueEvaluation</code> in the namespaces of evaluation and
3967 * dual functional classes. You can also
set the
3968 * <code>PointXDerivativeEvaluation</code> classes
for the x-derivative
3969 * instead of the
value at the evaluation
point.
3973 * Note that dual functional and evaluation objects should
3974 * match. However, you can give as many evaluation functionals as you
3975 * want, so you can have both
point value and derivative evaluated after
3976 * each step. One such additional evaluation is to output the grid in
3980 *
const Point<dim> evaluation_point(0.75, 0.75);
3981 * descriptor.dual_functional =
3982 * std_cxx14::make_unique<DualFunctional::PointValueEvaluation<dim>>(
3983 * evaluation_point);
3985 * Evaluation::PointValueEvaluation<dim> postprocessor1(evaluation_point);
3986 * Evaluation::GridOutput<dim> postprocessor2(
"grid");
3988 * descriptor.evaluator_list.push_back(&postprocessor1);
3989 * descriptor.evaluator_list.push_back(&postprocessor2);
3993 * Set the maximal number of degrees of freedom after which we want the
3994 * program to stop refining the mesh further:
3997 * descriptor.max_degrees_of_freedom = 20000;
4001 * Finally pass the descriptor
object to a
function that runs the entire
4010 * Catch exceptions to give information about things that failed:
4013 *
catch (std::exception &exc)
4015 * std::cerr << std::endl
4017 * <<
"----------------------------------------------------"
4019 * std::cerr <<
"Exception on processing: " << std::endl
4020 * << exc.what() << std::endl
4021 * <<
"Aborting!" << std::endl
4022 * <<
"----------------------------------------------------"
4028 * std::cerr << std::endl
4030 * <<
"----------------------------------------------------"
4032 * std::cerr <<
"Unknown exception!" << std::endl
4033 * <<
"Aborting!" << std::endl
4034 * <<
"----------------------------------------------------"
4042 <a name=
"Results"></a><h1>Results</h1>
4045 <a name=
"Pointvalues"></a><h3>
Point values</h3>
4049 This program offers a lot of possibilities to play around. We can thus
4050 only show a small part of all possible results that can be obtained
4051 with the help of
this program. However, you are encouraged to just
try
4052 it out, by changing the settings in the main program. Here, we start
4053 by simply letting it
run, unmodified:
4056 Number of degrees of freedom=72
4058 Estimated error=0.000702385
4060 Number of degrees of freedom=67
4062 Estimated error=0.000888953
4064 Number of degrees of freedom=130
4066 Estimated error=0.000454606
4068 Number of degrees of freedom=307
4070 Estimated error=0.000241254
4072 Number of degrees of freedom=718
4074 Estimated error=7.4912e-05
4076 Number of degrees of freedom=1665
4078 Estimated error=3.69111e-05
4080 Number of degrees of freedom=3975
4082 Estimated error=1.54218e-05
4084 Number of degrees of freedom=8934
4086 Estimated error=6.28359e-06
4088 Number of degrees of freedom=21799
4093 First let
's look what the program actually computed. On the seventh
4094 grid, primal and dual numerical solutions look like this (using a
4095 color scheme intended to evoke the snow-capped mountains of
4096 Colorado that the original author of this program now calls
4098 <table align="center">
4101 <img src="https://www.dealii.org/images/steps/developer/step-14.point-value.solution-7.9.2.png" alt="">
4104 <img src="https://www.dealii.org/images/steps/developer/step-14.point-value.solution-7-dual.9.2.png" alt="">
4108 Apparently, the region at the bottom left is so unimportant for the
4109 point value evaluation at the top right that the grid is left entirely
4110 unrefined there, even though the solution has singularities at the inner
4111 corner of that cell! Due
4112 to the symmetry in right hand side and domain, the solution should
4113 actually look like at the top right in all four corners, but the mesh
4114 refinement criterion involving the dual solution chose to refine them
4115 differently -- because we said that we really only care about a single
4116 function value somewhere at the top right.
4120 Here are some of the meshes that are produced in refinement cycles 0,
4121 2, 4 (top row), and 5, 7, and 8 (bottom row):
4123 <table width="80%" align="center">
4125 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-0.9.2.png" alt="" width="100%"></td>
4126 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-2.9.2.png" alt="" width="100%"></td>
4127 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-4.9.2.png" alt="" width="100%"></td>
4130 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-5.9.2.png" alt="" width="100%"></td>
4131 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-7.9.2.png" alt="" width="100%"></td>
4132 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.grid-8.9.2.png" alt="" width="100%"></td>
4136 Note the subtle interplay between resolving the corner singularities,
4137 and resolving around the point of evaluation. It will be rather
4138 difficult to generate such a mesh by hand, as this would involve to
4139 judge quantitatively how much which of the four corner singularities
4140 should be resolved, and to set the weight compared to the vicinity of
4141 the evaluation point.
4145 The program prints the point value and the estimated error in this
4146 quantity. From extrapolating it, we can guess that the exact value is
4147 somewhere close to 0.0334473, plus or minus 0.0000001 (note that we get
4148 almost 6 valid digits from only 22,000 (primal) degrees of
4149 freedom. This number cannot be obtained from the value of the
4150 functional alone, but I have used the assumption that the error
4151 estimator is mostly exact, and extrapolated the computed value plus
4152 the estimated error, to get an approximation of the true
4153 value. Computing with more degrees of freedom shows that this
4154 assumption is indeed valid.
4158 From the computed results, we can generate two graphs: one that shows
4159 the convergence of the error @f$J(u)-J(u_h)@f$ (taking the
4160 extrapolated value as correct) in the point value, and the value that
4161 we get by adding up computed value @f$J(u_h)@f$ and estimated
4162 error eta (if the error estimator @f$eta@f$ were exact, then the value
4163 @f$J(u_h)+\eta@f$ would equal the exact point value, and the error
4164 in this quantity would always be zero; however, since the error
4165 estimator is only a - good - approximation to the true error, we can
4166 by this only reduce the size of the error). In this graph, we also
4167 indicate the complexity @f${\cal O}(1/N)@f$ to show that mesh refinement
4168 acts optimal in this case. The second chart compares
4169 true and estimated error, and shows that the two are actually very
4170 close to each other, even for such a complicated quantity as the point
4174 <table width="80%" align="center">
4176 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.error.png" alt="" width="100%"></td>
4177 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-value.error-estimation.png" alt="" width="100%"></td>
4182 <a name="Comparingrefinementcriteria"></a><h3>Comparing refinement criteria</h3>
4186 Since we have accepted quite some effort when using the mesh
4187 refinement driven by the dual weighted error estimator (for solving
4188 the dual problem, and for evaluating the error representation), it is
4189 worth while asking whether that effort was successful. To this end, we
4190 first compare the achieved error levels for different mesh refinement
4191 criteria. To generate this data, simply change the value of the mesh
4192 refinement criterion variable in the main program. The results are
4193 thus (for the weight in the Kelly indicator, we have chosen the
4194 function @f$1/(r^2+0.1^2)@f$, where @f$r@f$
4195 is the distance to the evaluation point; it can be shown that this is
4196 the optimal weight if we neglect the effects of boundaries):
4198 <img src="https://www.dealii.org/images/steps/developer/step-14.point-value.error-comparison.png" alt="">
4202 Checking these numbers, we see that for global refinement, the error
4203 is proportional to @f$O(1/(sqrt(N) log(N)))@f$, and for the dual
4204 estimator @f$O(1/N)@f$. Generally speaking, we see that the dual
4205 weighted error estimator is better than the other refinement
4206 indicators, at least when compared with those that have a similarly
4207 regular behavior. The Kelly indicator produces smaller errors, but
4208 jumps about the picture rather irregularly, with the error also
4209 changing signs sometimes. Therefore, its behavior does not allow to
4210 extrapolate the results to larger values of N. Furthermore, if we
4211 trust the error estimates of the dual weighted error estimator, the
4212 results can be improved by adding the estimated error to the computed
4213 values. In terms of reliability, the weighted estimator is thus better
4214 than the Kelly indicator, although the latter sometimes produces
4219 <a name="Evaluationofpointstresses"></a><h3>Evaluation of point stresses</h3>
4223 Besides evaluating the values of the solution at a certain point, the
4224 program also offers the possibility to evaluate the x-derivatives at a
4225 certain point, and also to tailor mesh refinement for this. To let the
4226 program compute these quantities, simply replace the two occurrences of
4227 <code>PointValueEvaluation</code> in the main function by
4228 <code>PointXDerivativeEvaluation</code>, and let the program run:
4231 Number of degrees of freedom=72
4232 Point x-derivative=-0.0719397
4233 Estimated error=-0.0126173
4235 Number of degrees of freedom=61
4236 Point x-derivative=-0.0707956
4237 Estimated error=-0.00774316
4239 Number of degrees of freedom=131
4240 Point x-derivative=-0.0568671
4241 Estimated error=-0.00313426
4243 Number of degrees of freedom=247
4244 Point x-derivative=-0.053033
4245 Estimated error=-0.00136114
4247 Number of degrees of freedom=532
4248 Point x-derivative=-0.0526429
4249 Estimated error=-0.000558868
4251 Number of degrees of freedom=1267
4252 Point x-derivative=-0.0526955
4253 Estimated error=-0.000220116
4255 Number of degrees of freedom=2864
4256 Point x-derivative=-0.0527495
4257 Estimated error=-9.46731e-05
4259 Number of degrees of freedom=6409
4260 Point x-derivative=-0.052785
4261 Estimated error=-4.21543e-05
4263 Number of degrees of freedom=14183
4264 Point x-derivative=-0.0528028
4265 Estimated error=-2.04241e-05
4267 Number of degrees of freedom=29902
4268 Point x-derivative=-0.052814
4273 The solution looks roughly the same as before (the exact solution of
4274 course <em>is</em> the same, only the grid changed a little), but the
4275 dual solution is now different. A close-up around the point of
4276 evaluation shows this:
4277 <table align="center">
4280 <img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.solution-7-dual.png" alt="">
4283 <img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.solution-7-dual-close-up.png" alt="">
4286 This time, the grids in refinement cycles 0, 5, 6, 7, 8, and 9 look
4289 <table align="center" width="80%">
4291 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-0.9.2.png" alt="" width="100%"></td>
4292 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-5.9.2.png" alt="" width="100%"></td>
4293 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-6.9.2.png" alt="" width="100%"></td>
4296 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-7.9.2.png" alt="" width="100%"></td>
4297 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-8.9.2.png" alt="" width="100%"></td>
4298 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.grid-9.9.2.png" alt="" width="100%"></td>
4302 Note the asymmetry of the grids compared with those we obtained for
4303 the point evaluation. This is due to the fact that the domain and the primal
4304 solution may be symmetric about the diagonal, but the @f$x@f$-derivative is
4305 not, and the latter enters the refinement criterion.
4309 Then, it is interesting to compare actually computed values of the
4310 quantity of interest (i.e. the x-derivative of the solution at one
4311 point) with a reference value of -0.0528223... plus or minus
4312 0.0000005. We get this reference value by computing on finer grid after
4313 some more mesh refinements, with approximately 130,000 cells.
4314 Recall that if the error is @f$O(1/N)@f$ in the optimal case, then
4315 taking a mesh with ten times more cells gives us one additional digit
4320 In the left part of the following chart, you again see the convergence
4321 of the error towards this extrapolated value, while on the right you
4322 see a comparison of true and estimated error:
4324 <table width="80%" align="center">
4326 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.error.png" alt="" width="100%"></td>
4327 <td><img src="https://www.dealii.org/images/steps/developer/step-14.point-derivative.error-estimation.png" alt="" width="100%"></td>
4331 After an initial phase where the true error changes its sign, the
4332 estimated error matches it quite well, again. Also note the dramatic
4333 improvement in the error when using the estimated error to correct the
4334 computed value of @f$J(u_h)@f$.
4338 <a name="step13revisited"></a><h3>step-13 revisited</h3>
4342 If instead of the <code>Exercise_2_3</code> data set, we choose
4343 <code>CurvedRidges</code> in the main function, and choose @f$(0.5,0.5)@f$
4344 as the evaluation point, then we can redo the
4345 computations of the previous example program, to compare whether the
4346 results obtained with the help of the dual weighted error estimator
4347 are better than those we had previously.
4351 First, the meshes after 9 adaptive refinement cycles obtained with
4352 the point evaluation and derivative evaluation refinement
4353 criteria, respectively, look like this:
4355 <table width="80%" align="center">
4357 <td><img src="https://www.dealii.org/images/steps/developer/step-14.step-13.point-value.png" alt="" width="100%"></td>
4358 <td><img src="https://www.dealii.org/images/steps/developer/step-14.step-13.point-derivative.png" alt="" width="100%"></td>
4362 The features of the solution can still be seen in the mesh, but since the
4363 solution is smooth, the singularities of the dual solution entirely
4364 dominate the mesh refinement criterion, and lead to strongly
4365 concentrated meshes. The solution after the seventh refinement step looks
4368 <table width="40%" align="center">
4370 <td><img src="https://www.dealii.org/images/steps/developer/step-14.step-13.solution-7.9.2.png" alt="" width="100%"></td>
4374 Obviously, the solution is worse at some places, but the mesh
4375 refinement process should have taken care that these places are not
4376 important for computing the point value.
4381 The next point is to compare the new (duality based) mesh refinement
4382 criterion with the old ones. These are the results:
4384 <img src="https://www.dealii.org/images/steps/developer/step-14.step-13.error-comparison.png" alt="">
4388 The results are, well, somewhat mixed. First, the Kelly indicator
4389 disqualifies itself by its unsteady behavior, changing the sign of the
4390 error several times, and with increasing errors under mesh
4391 refinement. The dual weighted error estimator has a monotone decrease
4392 in the error, and is better than the weighted Kelly and global
4393 refinement, but the margin is not as large as expected. This is, here,
4394 due to the fact the global refinement can exploit the regular
4395 structure of the meshes around the point of evaluation, which leads to
4396 a better order of convergence for the point error. However, if we had
4397 a mesh that is not locally rectangular, for example because we had to
4398 approximate curved boundaries, or if the coefficients were not
4399 constant, then this advantage of globally refinement meshes would
4400 vanish, while the good performance of the duality based estimator
4406 <a name="Conclusionsandoutlook"></a><h3>Conclusions and outlook</h3>
4410 The results here are not too clearly indicating the superiority of the
4411 dual weighted error estimation approach for mesh refinement over other
4412 mesh refinement criteria, such as the Kelly indicator. This is due to
4413 the relative simplicity of the shown applications. If you are not
4414 convinced yet that this approach is indeed superior, you are invited
4415 to browse through the literature indicated in the introduction, where
4416 plenty of examples are provided where the dual weighted approach can
4417 reduce the necessary numerical work by orders of magnitude, making
4418 this the only way to compute certain quantities to reasonable
4423 Besides the objections you may raise against its use as a mesh
4424 refinement criterion, consider that accurate knowledge of the error in
4425 the quantity one might want to compute is of great use, since we can
4426 stop computations when we are satisfied with the accuracy. Using more
4427 traditional approaches, it is very difficult to get accurate estimates
4428 for arbitrary quantities, except for, maybe, the error in the energy
4429 norm, and we will then have no guarantee that the result we computed
4430 satisfies any requirements on its accuracy. Also, as was shown for the
4431 evaluation of point values and derivatives, the error estimate can be
4432 used to extrapolate the results, yielding much higher accuracy in the
4433 quantity we want to know.
4437 Leaving these mathematical considerations, we tried to write the
4438 program in a modular way, such that implementing another test case, or
4439 another evaluation and dual functional is simple. You are encouraged
4440 to take the program as a basis for your own experiments, and to play a
4449 <a name="PlainProg"></a>
4450 <h1> The plain program</h1>
4451 @include "step-14.cc"