void make_grid_and_dofs ();
void assemble_system ();
void solve ();
+ void compute_errors () const;
void output_results () const;
Triangulation<dim> triangulation;
};
+template <int dim>
+class ExactSolution : public Function<dim>
+{
+ public:
+ ExactSolution () : Function<dim>(dim+1) {};
+
+ virtual double value (const Point<dim> &p,
+ const unsigned int component = 0) const;
+};
+
// We wanted the right hand side
}
+template <int dim>
+double ExactSolution<dim>::value (const Point<dim> &p,
+ const unsigned int component) const
+{
+ double return_value = 1;
+ for (unsigned int i=0; i<dim; ++i)
+ return_value *= std::sin(deal_II_numbers::PI*p(i));
+
+ return return_value;
+}
+
+
// The boundary values were to be
// chosen to be x*x+y*y in 2D, and
// x*x+y*y+z*z in 3D. This happens to
void LaplaceProblem<dim>::make_grid_and_dofs ()
{
GridGenerator::hyper_cube (triangulation, 0, 1);
- triangulation.refine_global (4);
+ triangulation.refine_global (5);
std::cout << " Number of active cells: "
<< triangulation.n_active_cells()
+template <int dim>
+void LaplaceProblem<dim>::compute_errors () const
+{
+ Vector<double> tmp (triangulation.n_active_cells());
+ ExactSolution<dim> exact_solution;
+ {
+ const ComponentSelectFunction<dim> mask (dim, 1., dim+1);
+ VectorTools::integrate_difference (dof_handler, solution, exact_solution,
+ tmp, QGauss<dim>(degree+1),
+ VectorTools::L2_norm,
+ &mask);
+ }
+ const double p_l2_error = tmp.l2_norm();
+
+ double u_l2_error = 0;
+ for (unsigned int d=0; d<dim; ++d)
+ {
+ const ComponentSelectFunction<dim> mask(d, 1., dim+1);
+ VectorTools::integrate_difference (dof_handler, solution, exact_solution,
+ tmp, QGauss<dim>(degree+1),
+ VectorTools::L2_norm,
+ &mask);
+ u_l2_error = std::sqrt (u_l2_error*u_l2_error +
+ tmp.l2_norm() * tmp.l2_norm());
+ }
+
+ double u_h1_error = 0;
+ for (unsigned int d=0; d<dim; ++d)
+ {
+ const ComponentSelectFunction<dim> mask(d, 1., dim+1);
+ VectorTools::integrate_difference (dof_handler, solution, exact_solution,
+ tmp, QGauss<dim>(degree+1),
+ VectorTools::H1_seminorm,
+ &mask);
+ u_h1_error = std::sqrt (u_h1_error*u_h1_error +
+ tmp.l2_norm() * tmp.l2_norm());
+ }
+
+
+ std::cout << "Errors: ||e_p||_L2 = " << p_l2_error
+ << ", ||e_u||_L2 = " << u_l2_error
+ << ", |e_u|_H1 = " << u_h1_error
+ << std::endl;
+}
+
+
+
// This function also does what the
// respective one did in the previous
// example. No changes here for
make_grid_and_dofs();
assemble_system ();
solve ();
+ compute_errors ();
output_results ();
}