215 * #ifndef INCLUDE_DG_UPWIND_H_
216 * #define INCLUDE_DG_UPWIND_H_
219 * #include <deal.II/base/function.h>
220 * #include <deal.II/base/quadrature_lib.h>
222 * #include <deal.II/dofs/dof_handler.h>
223 * #include <deal.II/dofs/dof_tools.h>
225 * #include <deal.II/fe/fe_dgq.h>
226 * #include <deal.II/fe/fe_q.h>
227 * #include <deal.II/fe/fe_values.h>
228 * #include <deal.II/fe/mapping_q1.h>
230 * #include <deal.II/grid/grid_generator.h>
231 * #include <deal.II/grid/grid_out.h>
232 * #include <deal.II/grid/grid_refinement.h>
233 * #include <deal.II/grid/tria.h>
235 * #include <deal.II/lac/dynamic_sparsity_pattern.h>
236 * #include <deal.II/lac/full_matrix.h>
237 * #include <deal.II/lac/sparse_matrix.h>
238 * #include <deal.II/lac/vector.h>
240 * #include <deal.II/numerics/data_out.h>
241 * #include <deal.II/numerics/vector_tools.h>
248 * #include <deal.II/fe/fe_interface_values.h>
254 * #include <deal.II/lac/precondition_block.h>
255 * #include <deal.II/lac/solver_richardson.h>
256 * #include <deal.II/lac/sparse_direct.h>
259 * We are going to use
gradients as refinement indicator.
262 * #include <deal.II/numerics/derivative_approximation.h>
268 * #include <deal.II/base/convergence_table.h>
270 * #include <deal.II/meshworker/
mesh_loop.h>
274 * To enable parameter handling
277 * #include <deal.II/base/function_parser.h>
278 * #include <deal.II/base/parameter_acceptor.h>
279 * #include <deal.II/base/parameter_handler.h>
280 * #include <deal.II/base/parsed_convergence_table.h>
281 * #include <deal.II/base/symbolic_function.h>
283 * #include <deal.II/meshworker/copy_data.h>
284 * #include <deal.II/meshworker/
mesh_loop.h>
285 * #include <deal.II/meshworker/scratch_data.h>
288 * #include <iostream>
293 * This is a
struct used only for throwing an exception when theta parameter is
299 * std::string message;
300 * theta_exc(std::string &&s)
301 * : message{std::move(s)} {};
305 *
return message.c_str();
314 * AdvectionReaction();
316 * initialize_params(
const std::string &filename);
323 * parse_string(
const std::string ¶meters);
333 * output_results(
const unsigned int cycle)
const;
337 * compute_energy_norm();
339 * compute_local_projection_and_estimate();
346 * Furthermore we want to use DG elements.
349 * std::unique_ptr<FE_DGQ<dim>> fe;
362 * So far we declared the usual objects. Hereafter we declare
371 *
unsigned int fe_degree = 1;
375 * and then we define
default values that will be parsed from the following
379 * std::string exact_solution_expression =
380 *
"tanh(100*(x+y-0.5))";
381 * std::string rhs_expression =
382 *
"-200*tanh(100*x + 100*y - 50.0)^2 + tanh(100*x + 100*y - 50.0) + 200";
383 * std::string advection_coefficient_expression =
"1.0";
384 * std::string boundary_conditions_expression =
"tanh(100*x + 100*y - 50.0)";
385 * std::string refinement =
"residual";
386 * std::string output_filename =
"DG_advection_reaction_estimator";
387 * std::map<std::string, double> constants;
390 *
bool use_direct_solver =
true;
391 *
unsigned int n_refinement_cycles = 8;
392 *
unsigned int n_global_refinements = 3;
393 *
double theta = 0.5;
400<a name=
"ann-main.cc"></a>
401<h1>Annotated version of main.cc</h1>
417 * #include
"include/DG_advection_reaction.h"
420 * main(
int argc,
char **argv)
424 * std::string par_name =
"";
427 * par_name = argv[1];
431 * par_name =
"parameters.prm";
434 * AdvectionReaction<2> problem;
435 * problem.initialize_params(par_name);
438 *
catch (std::exception &exc)
440 * std::cerr << std::endl
442 * <<
"----------------------------------------------------"
444 * std::cerr <<
"Exception on processing: " << std::endl
445 * << exc.what() << std::endl
446 * <<
"Aborting!" << std::endl
447 * <<
"----------------------------------------------------"
451 *
catch (
const theta_exc &theta_range)
453 * std::cerr << std::endl
455 * <<
"----------------------------------------------------"
457 * std::cerr <<
"Exception on processing: " << std::endl
458 * << theta_range.what() << std::endl
459 * <<
"Aborting!" << std::endl
460 * <<
"----------------------------------------------------"
466 * std::cerr << std::endl
468 * <<
"----------------------------------------------------"
470 * std::cerr <<
"Unknown exception!" << std::endl
471 * <<
"Aborting!" << std::endl
472 * <<
"----------------------------------------------------"
482<a name=
"ann-source/DG_advection_reaction.cc"></a>
483<h1>Annotated version of source/DG_advection_reaction.cc</h1>
499 * #include
"../include/DG_advection_reaction.h"
503 * Compute and returns the wind field
b
510 *
Assert(dim > 1, ExcNotImplemented());
513 * wind_field[0] = 1.0;
514 * wind_field[1] = 1.0;
522 * <a name=
"source/DG_advection_reaction.cc-TheScratchDataandCopyDataclasses"></a>
523 * <h3>The ScratchData and CopyData classes</h3>
527 * The following objects are the scratch and
copy objects we use in the call
529 * that works similar to
FEValues or FEFacesValues, except that it acts on
530 * an
interface between two cells and allows us to
assemble the interface
531 * terms in our weak form.
548 * : fe_values(mapping, fe, quadrature, update_flags)
549 * , fe_interface_values(mapping, fe, quadrature_face, interface_update_flags)
552 * ScratchData(
const ScratchData<dim> &scratch_data)
553 * : fe_values(scratch_data.fe_values.get_mapping(),
554 * scratch_data.fe_values.get_fe(),
555 * scratch_data.fe_values.get_quadrature(),
556 * scratch_data.fe_values.get_update_flags())
557 * , fe_interface_values(scratch_data.fe_interface_values.get_mapping(),
558 * scratch_data.fe_interface_values.get_fe(),
559 * scratch_data.fe_interface_values.get_quadrature(),
560 * scratch_data.fe_interface_values.get_update_flags())
569 *
struct CopyDataFace
572 * std::vector<types::global_dof_index> joint_dof_indices;
573 * std::array<double, 2> values;
574 * std::array<unsigned int, 2> cell_indices;
583 * std::vector<types::global_dof_index> local_dof_indices;
584 * std::vector<CopyDataFace> face_data;
587 *
double value_estimator;
593 *
template <
class Iterator>
595 *
reinit(
const Iterator &cell,
unsigned int dofs_per_cell)
597 *
cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
598 * cell_mass_matrix.reinit(dofs_per_cell, dofs_per_cell);
600 * cell_rhs.reinit(dofs_per_cell);
601 * cell_mass_rhs.reinit(dofs_per_cell);
603 * local_dof_indices.resize(dofs_per_cell);
604 * cell->get_dof_indices(local_dof_indices);
613 * <a name=
"source/DG_advection_reaction.cc-Auxiliaryfunction"></a>
614 * <h3>Auxiliary function</h3>
615 * This auxiliary function is taken from @ref step_74
"step-74" and it
's used to
616 * compute the jump of the finite element function @f$u_h@f$ on a face.
621 * get_function_jump(const FEInterfaceValues<dim> &fe_iv,
622 * const Vector<double> &solution,
623 * std::vector<double> &jump)
625 * const unsigned int n_q = fe_iv.n_quadrature_points;
626 * std::array<std::vector<double>, 2> face_values;
628 * for (unsigned int i = 0; i < 2; ++i)
630 * face_values[i].resize(n_q);
631 * fe_iv.get_fe_face_values(i).get_function_values(solution, face_values[i]);
633 * for (unsigned int q = 0; q < n_q; ++q)
634 * jump[q] = face_values[0][q] - face_values[1][q];
640 * AdvectionReaction<dim>::AdvectionReaction()
642 * , dof_handler(triangulation)
644 * Assert(dim > 1, ExcMessage("Not implemented in 1D."));
645 * add_parameter("Finite element degree", fe_degree);
646 * add_parameter("Problem constants", constants);
647 * add_parameter("Output filename", output_filename);
648 * add_parameter("Use direct solver", use_direct_solver);
649 * add_parameter("Number of refinement cycles", n_refinement_cycles);
650 * add_parameter("Number of global refinement", n_global_refinements);
651 * add_parameter("Refinement", refinement);
652 * add_parameter("Exact solution expression", exact_solution_expression);
653 * add_parameter("Boundary conditions expression",
654 * boundary_conditions_expression);
655 * add_parameter("Theta", theta);
656 * add_parameter("Advection coefficient expression",
657 * advection_coefficient_expression);
658 * add_parameter("Right hand side expression", rhs_expression);
660 * this->prm.enter_subsection("Error table");
661 * error_table.add_parameters(this->prm);
662 * this->prm.leave_subsection();
669 * AdvectionReaction<dim>::initialize_params(const std::string &filename)
671 * ParameterAcceptor::initialize(filename,
672 * "last_used_parameters.prm",
673 * ParameterHandler::Short);
674 * if (theta < 0.0 || theta > 10.0 || std::abs(theta) < 1e-12)
677 * theta_exc("Theta parameter is not in a suitable range: see paper by "
678 * "Brezzi, Marini, Suli for an extended discussion"));
686 * AdvectionReaction<dim>::parse_string(const std::string ¶meters)
688 * ParameterAcceptor::prm.parse_input_from_string(parameters);
689 * ParameterAcceptor::parse_all_parameters();
696 * AdvectionReaction<dim>::setup_system()
700 * first need to distribute the DoFs.
705 * fe = std::make_unique<FE_DGQ<dim>>(fe_degree);
706 * const auto vars = dim == 2 ? "x,y" : "x,y,z";
707 * exact_solution.initialize(vars, exact_solution_expression, constants);
708 * rhs.initialize(vars, rhs_expression, constants);
709 * advection_coeff.initialize(vars,
710 * advection_coefficient_expression,
712 * boundary_conditions.initialize(vars,
713 * boundary_conditions_expression,
716 * dof_handler.distribute_dofs(*fe);
720 * To build the sparsity pattern for DG discretizations, we can call the
721 * function analogue to DoFTools::make_sparsity_pattern, which is called
722 * DoFTools::make_flux_sparsity_pattern:
725 * DynamicSparsityPattern dsp(dof_handler.n_dofs());
726 * DoFTools::make_flux_sparsity_pattern(dof_handler,
727 * dsp); // DG sparsity pattern generator
728 * sparsity_pattern.copy_from(dsp);
732 * Finally, we set up the structure of all components of the linear system.
735 * system_matrix.reinit(sparsity_pattern);
736 * solution.reinit(dof_handler.n_dofs());
737 * right_hand_side.reinit(dof_handler.n_dofs());
744 * in the call to MeshWorker::mesh_loop() we only need to specify what should
746 * each cell, each boundary face, and each interior face. These three tasks
747 * are handled by the lambda functions inside the function below.
755 * AdvectionReaction<dim>::assemble_system()
757 * using Iterator = typename DoFHandler<dim>::active_cell_iterator;
759 * const QGauss<dim> quadrature = fe->tensor_degree() + 1;
760 * const QGauss<dim - 1> quadrature_face = fe->tensor_degree() + 1;
764 * This is the function that will be executed for each cell.
767 * const auto cell_worker = [&](const Iterator &cell,
768 * ScratchData<dim> &scratch_data,
769 * CopyData ©_data) {
770 * FEValues<dim> fe_values_continuous(*fe,
772 * update_values | update_gradients |
773 * update_quadrature_points |
774 * update_JxW_values);
776 * const unsigned int n_dofs =
777 * scratch_data.fe_values.get_fe().n_dofs_per_cell();
778 * copy_data.reinit(cell, n_dofs);
779 * scratch_data.fe_values.reinit(cell);
781 * const auto &q_points = scratch_data.fe_values.get_quadrature_points();
783 * const FEValues<dim> &fe_v = scratch_data.fe_values;
784 * const std::vector<double> &JxW = fe_v.get_JxW_values();
786 * for (unsigned int point = 0; point < fe_v.n_quadrature_points; ++point)
788 * auto beta_q = beta(q_points[point]);
789 * for (unsigned int i = 0; i < n_dofs; ++i)
791 * for (unsigned int j = 0; j < n_dofs; ++j)
793 * copy_data.cell_matrix(i, j) +=
795 * * fe_v.shape_grad(i, point) // \nabla \phi_i
796 * * fe_v.shape_value(j, point) // \phi_j
797 * + advection_coeff.value(q_points[point]) * // gamma
798 * fe_v.shape_value(i, point) // phi_i
799 * * fe_v.shape_value(j, point) // phi_j
803 * copy_data.cell_rhs(i) += rhs.value(q_points[point]) // f(x_q)
804 * * fe_v.shape_value(i, point) // phi_i(x_q)
805 * * JxW[point]; // dx
812 * This is the function called for boundary faces and consists of a normal
813 * integration using FEFaceValues. New is the logic to decide if the term
814 * goes into the system matrix (outflow) or the right-hand side (inflow).
817 * const auto boundary_worker = [&](const Iterator &cell,
818 * const unsigned int &face_no,
819 * ScratchData<dim> &scratch_data,
820 * CopyData ©_data) {
821 * scratch_data.fe_interface_values.reinit(cell, face_no);
822 * const FEFaceValuesBase<dim> &fe_face =
823 * scratch_data.fe_interface_values.get_fe_face_values(0);
825 * const auto &q_points = fe_face.get_quadrature_points();
827 * const unsigned int n_facet_dofs = fe_face.get_fe().n_dofs_per_cell();
828 * const std::vector<double> &JxW = fe_face.get_JxW_values();
829 * const std::vector<Tensor<1, dim>> &normals = fe_face.get_normal_vectors();
831 * std::vector<double> g(q_points.size());
832 * exact_solution.value_list(q_points, g);
834 * for (unsigned int point = 0; point < q_points.size(); ++point)
836 * const double beta_dot_n = beta(q_points[point]) * normals[point];
838 * if (beta_dot_n > 0)
840 * for (unsigned int i = 0; i < n_facet_dofs; ++i)
841 * for (unsigned int j = 0; j < n_facet_dofs; ++j)
842 * copy_data.cell_matrix(i, j) +=
843 * fe_face.shape_value(i,
845 * * fe_face.shape_value(j, point) // \phi_j
846 * * beta_dot_n // \beta . n
847 * * JxW[point]; // dx
850 * for (unsigned int i = 0; i < n_facet_dofs; ++i)
851 * copy_data.cell_rhs(i) += -fe_face.shape_value(i, point) // \phi_i
853 * * beta_dot_n // \beta . n
854 * * JxW[point]; // dx
860 * This is the function called on interior faces. The arguments specify
861 * cells, face and subface indices (for adaptive refinement). We just pass
862 * them along to the reinit() function of FEInterfaceValues.
865 * const auto face_worker = [&](const Iterator &cell,
866 * const unsigned int &f,
867 * const unsigned int &sf,
868 * const Iterator &ncell,
869 * const unsigned int &nf,
870 * const unsigned int &nsf,
871 * ScratchData<dim> &scratch_data,
872 * CopyData ©_data) {
873 * FEInterfaceValues<dim> &fe_iv = scratch_data.fe_interface_values;
874 * fe_iv.reinit(cell, f, sf, ncell, nf, nsf);
875 * const auto &q_points = fe_iv.get_quadrature_points();
877 * copy_data.face_data.emplace_back();
878 * CopyDataFace ©_data_face = copy_data.face_data.back();
880 * const unsigned int n_dofs = fe_iv.n_current_interface_dofs();
881 * copy_data_face.joint_dof_indices = fe_iv.get_interface_dof_indices();
883 * copy_data_face.cell_matrix.reinit(n_dofs, n_dofs);
885 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
886 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
888 * for (unsigned int qpoint = 0; qpoint < q_points.size(); ++qpoint)
890 * const double beta_dot_n = beta(q_points[qpoint]) * normals[qpoint];
891 * for (unsigned int i = 0; i < n_dofs; ++i)
893 * for (unsigned int j = 0; j < n_dofs; ++j)
895 * copy_data_face.cell_matrix(i, j) +=
896 * (beta(q_points[qpoint]) * normals[qpoint] *
897 * fe_iv.average_of_shape_values(j, qpoint) *
898 * fe_iv.jump_in_shape_values(i, qpoint) +
899 * theta * std::abs(beta_dot_n) *
900 * fe_iv.jump_in_shape_values(j, qpoint) *
901 * fe_iv.jump_in_shape_values(i, qpoint)) *
910 * The following lambda function will handle copying the data from the
911 * cell and face assembly into the global matrix and right-hand side.
915 * While we would not need an AffineConstraints object, because there are
916 * no hanging node constraints in DG discretizations, we use an empty
917 * object here as this allows us to use its `copy_local_to_global`
921 * const AffineConstraints<double> constraints;
923 * const auto copier = [&](const CopyData &c) {
924 * constraints.distribute_local_to_global(c.cell_matrix,
926 * c.local_dof_indices,
930 * for (auto &cdf : c.face_data)
932 * constraints.distribute_local_to_global(cdf.cell_matrix,
933 * cdf.joint_dof_indices,
938 * ScratchData<dim> scratch_data(mapping, *fe, quadrature, quadrature_face);
939 * CopyData copy_data;
943 * Here, we finally handle the assembly. We pass in ScratchData and
944 * CopyData objects, the lambda functions from above, an specify that we
945 * want to assemble interior faces once.
948 * MeshWorker::mesh_loop(dof_handler.begin_active(),
954 * MeshWorker::assemble_own_cells |
955 * MeshWorker::assemble_boundary_faces |
956 * MeshWorker::assemble_own_interior_faces_once,
965 * AdvectionReaction<dim>::solve()
967 * if (use_direct_solver)
969 * SparseDirectUMFPACK system_matrix_inverse;
970 * system_matrix_inverse.initialize(system_matrix);
971 * system_matrix_inverse.vmult(solution, right_hand_side);
977 * Here we have a classic iterative solver, as done in many tutorials:
980 * SolverControl solver_control(1000, 1e-15);
981 * SolverRichardson<Vector<double>> solver(solver_control);
982 * PreconditionBlockSSOR<SparseMatrix<double>> preconditioner;
983 * preconditioner.initialize(system_matrix, fe->n_dofs_per_cell());
984 * solver.solve(system_matrix, solution, right_hand_side, preconditioner);
985 * std::cout << " Solver converged in " << solver_control.last_step()
986 * << " iterations." << std::endl;
995 * <a name="source/DG_advection_reaction.cc-Meshrefinement"></a>
996 * <h3>Mesh refinement</h3>
997 * We refine the grid according the proposed estimator or with an approximation
998 * to the gradient of the solution. The first option is the default one (you can
999 * see it in the header file)
1002 * template <int dim>
1004 * AdvectionReaction<dim>::refine_grid()
1006 * if (refinement == "residual")
1010 * If the `refinement` string is `"residual"`, then we first compute the
1014 * compute_local_projection_and_estimate();
1017 * We then set the refinement fraction and as usual we execute the
1021 * const double refinement_fraction = 0.6;
1022 * GridRefinement::refine_and_coarsen_fixed_fraction(
1023 * triangulation, error_indicator_per_cell, refinement_fraction, 0.0);
1024 * triangulation.execute_coarsening_and_refinement();
1026 * else if (refinement == "gradient")
1028 * Vector<float> gradient_indicator(triangulation.n_active_cells());
1032 * Now the approximate gradients are computed
1035 * DerivativeApproximation::approximate_gradient(mapping,
1038 * gradient_indicator);
1042 * and they are cell-wise scaled by the factor @f$h^{1+d/2}@f$
1045 * unsigned int cell_no = 0;
1046 * for (const auto &cell : dof_handler.active_cell_iterators())
1047 * gradient_indicator(cell_no++) *=
1048 * std::pow(cell->diameter(), 1 + 1.0 * dim / 2);
1052 * Finally they serve as refinement indicator.
1055 * GridRefinement::refine_and_coarsen_fixed_fraction(triangulation,
1056 * gradient_indicator,
1060 * triangulation.execute_coarsening_and_refinement();
1061 * std::cout << gradient_indicator.l2_norm() << '\n
';
1063 * else if (refinement == "global")
1065 * triangulation.refine_global(
1066 * 1); // just for testing on uniformly refined meshes
1070 * Assert(false, ExcInternalError());
1078 * The output of this program consists of a vtk file of the adaptively
1079 * refined grids and the numerical solutions.
1082 * template <int dim>
1084 * AdvectionReaction<dim>::output_results(const unsigned int cycle) const
1086 * const std::string filename = "solution-" + std::to_string(cycle) + ".vtk";
1087 * std::cout << " Writing solution to <" << filename << ">" << std::endl;
1088 * std::ofstream output(filename);
1090 * DataOut<dim> data_out;
1091 * data_out.attach_dof_handler(dof_handler);
1092 * data_out.add_data_vector(solution, "u", DataOut<dim>::type_dof_data);
1093 * data_out.build_patches(mapping);
1094 * data_out.write_vtk(output);
1097 * template <int dim>
1099 * AdvectionReaction<dim>::compute_error()
1101 * error_table.error_from_exact(
1105 * exact_solution); // be careful: a FD approximation of the gradient is used
1108 * to compute the H^1 norm if Solution<dim> doesn't
1109 * implements the Gradient function
1119 * <a name=
"source/DG_advection_reaction.cc-Computetheenergynorm"></a>
1120 * <h3>Compute the energy
norm</h3>
1121 * The energy
norm is defined as @f$ |||\cdot ||| = \Bigl(||\cdot||_{0,\Omega}^2 +
1122 * \sum_{F \in \mathbb{
F}}||c_F^{\frac{1}{2}}[\cdot] ||_{0,
F}^2
1123 * \Bigr)^{\frac{1}{2}}@f$ Notice that in the current
case we have @f$c_f = \frac{|
b
1124 * \cdot n|}{2}@f$ Like in the assembly, all the contributions are handled
1125 * separately by
using ScratchData and CopyData objects.
1128 *
template <
int dim>
1130 * AdvectionReaction<dim>::compute_energy_norm()
1138 * We start off by adding cell contributions
1141 *
const auto cell_worker = [&](
const Iterator &cell,
1142 * ScratchData<dim> &scratch_data,
1143 * CopyData ©_data) {
1144 *
const unsigned int n_dofs =
1145 * scratch_data.fe_values.get_fe().n_dofs_per_cell();
1146 * copy_data.reinit(cell, n_dofs);
1147 * scratch_data.fe_values.reinit(cell);
1149 * copy_data.cell_index = cell->active_cell_index();
1151 *
const auto &q_points = scratch_data.fe_values.get_quadrature_points();
1155 *
double error_square_norm{0.0};
1156 * std::vector<double> sol_u(fe_v.n_quadrature_points);
1157 * fe_v.get_function_values(solution, sol_u);
1159 *
for (
unsigned int point = 0;
point < fe_v.n_quadrature_points; ++
point)
1161 *
const double diff =
1162 * (sol_u[point] - exact_solution.value(q_points[point]));
1163 * error_square_norm += diff * diff * JxW[
point];
1165 * copy_data.value = error_square_norm;
1170 * Here we add contributions coming from the
internal faces
1173 *
const auto face_worker = [&](
const Iterator &cell,
1174 *
const unsigned int &f,
1175 *
const unsigned int &sf,
1176 *
const Iterator &ncell,
1177 *
const unsigned int &nf,
1178 *
const unsigned int &nsf,
1179 * ScratchData<dim> &scratch_data,
1180 * CopyData ©_data) {
1182 * fe_iv.
reinit(cell, f, sf, ncell, nf, nsf);
1184 * copy_data.face_data.emplace_back();
1185 * CopyDataFace ©_data_face = copy_data.face_data.back();
1186 * copy_data_face.cell_indices[0] = cell->active_cell_index();
1187 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
1189 *
const auto &q_points = fe_iv.get_quadrature_points();
1190 *
const unsigned n_q_points = q_points.size();
1191 *
const std::vector<double> &JxW = fe_iv.get_JxW_values();
1192 * std::vector<double> g(n_q_points);
1194 * std::vector<double> jump(n_q_points);
1195 * get_function_jump(fe_iv, solution, jump);
1197 *
const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
1199 *
double error_jump_square{0.0};
1200 *
for (
unsigned int point = 0;
point < n_q_points; ++
point)
1202 *
const double beta_dot_n =
1203 * theta *
std::abs(beta(q_points[point]) * normals[point]);
1204 * error_jump_square +=
1208 * copy_data.value = error_jump_square;
1213 * Finally, we add the boundary contributions
1216 *
const auto boundary_worker = [&](
const Iterator &cell,
1217 *
const unsigned int &face_no,
1218 * ScratchData<dim> &scratch_data,
1219 * CopyData ©_data) {
1220 * scratch_data.fe_interface_values.reinit(cell, face_no);
1222 * scratch_data.fe_interface_values.get_fe_face_values(0);
1224 *
const unsigned n_q_points = q_points.size();
1225 *
const std::vector<double> &JxW = fe_fv.get_JxW_values();
1227 * std::vector<double> g(n_q_points);
1229 * std::vector<double> sol_u(n_q_points);
1230 * fe_fv.get_function_values(solution, sol_u);
1232 *
const std::vector<Tensor<1, dim>> &normals = fe_fv.get_normal_vectors();
1234 *
double difference_norm_square = 0.;
1235 *
for (
unsigned int point = 0;
point < q_points.size(); ++
point)
1237 *
const double beta_dot_n =
1238 * theta *
std::abs(beta(q_points[point]) * normals[point]);
1239 *
const double diff =
1240 * (boundary_conditions.value(q_points[point]) - sol_u[point]);
1241 * difference_norm_square += beta_dot_n * diff * diff * JxW[
point];
1243 * copy_data.value = difference_norm_square;
1246 *
const auto copier = [&](
const auto ©_data) {
1249 * energy_norm_square_per_cell[copy_data.cell_index] += copy_data.value;
1251 *
for (
auto &cdf : copy_data.face_data)
1252 * for (unsigned
int j = 0; j < 2; ++j)
1253 * energy_norm_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1256 * ScratchData<dim> scratch_data(mapping,
1259 *
QGauss<dim - 1>{fe->tensor_degree() + 1});
1261 * CopyData copy_data;
1264 * dof_handler.end(),
1275 *
const double energy_error =
std::sqrt(energy_norm_square_per_cell.l1_norm());
1276 *
return energy_error;
1284 * <a name=
"source/DG_advection_reaction.cc-Computingtheestimator"></a>
1285 * <h3>Computing the estimator</h3>
1286 * In the estimator, we have to compute the term @f$||f- c u_h - \Pi(f- c
1287 * u_h)||_{T}^{2}@f$ over a
generic cell @f$T@f$. To achieve
this, we
first need to
1288 * compute the projection involving the finite element function @f$u_h@f$. Using the
1289 * definition of orthogonal projection, we
're required to solve cellwise
1290 * @f$(v_h,f-c u_h)_T = (v_h,\Pi)_T \qquad \forall v_h \in V_h@f$ for @f$\Pi@f$, which
1291 * means that we have to build a mass-matrix on each cell. Once we have the
1292 * projection, which is a finite element function, we can add its contribution
1293 * in the <code>cell_worker</code> lambda. As done in @ref step_74 "step-74", the square of the
1294 * error indicator is computed.
1300 * template <int dim>
1302 * AdvectionReaction<dim>::compute_local_projection_and_estimate()
1306 * Compute the term @f$||f-c u_h - \Pi(f- cu_h)||_T^2@f$
1309 * using Iterator = typename DoFHandler<dim>::active_cell_iterator;
1310 * error_indicator_per_cell.reinit(triangulation.n_active_cells());
1312 * const auto cell_worker = [&](const Iterator &cell,
1313 * ScratchData<dim> &scratch_data,
1314 * CopyData ©_data) {
1315 * const unsigned int n_dofs =
1316 * scratch_data.fe_values.get_fe().n_dofs_per_cell();
1318 * copy_data.reinit(cell, n_dofs);
1319 * scratch_data.fe_values.reinit(cell);
1320 * copy_data.cell_index = cell->active_cell_index();
1322 * const auto &q_points = scratch_data.fe_values.get_quadrature_points();
1323 * const unsigned n_q_points = q_points.size();
1325 * const FEValues<dim> &fe_v = scratch_data.fe_values;
1326 * const std::vector<double> &JxW = fe_v.get_JxW_values();
1328 * std::vector<double> sol_u_at_quadrature_points(fe_v.n_quadrature_points);
1329 * fe_v.get_function_values(solution, sol_u_at_quadrature_points);
1333 * Compute local L^2 projection of @f$f- c u_h@f$ over the local finite element
1337 * for (unsigned int point = 0; point < n_q_points; ++point)
1339 * for (unsigned int i = 0; i < n_dofs; ++i)
1341 * for (unsigned int j = 0; j < n_dofs; ++j)
1343 * copy_data.cell_mass_matrix(i, j) +=
1344 * fe_v.shape_value(i, point) * // phi_i(x_q)
1345 * fe_v.shape_value(j, point) * // phi_j(x_q)
1346 * JxW[point]; // dx(x_q)
1348 * copy_data.cell_mass_rhs(i) +=
1349 * (rhs.value(q_points[point]) * // f(x_q)
1350 * fe_v.shape_value(i, point) // phi_i(x_q)
1351 * - advection_coeff.value(q_points[point]) *
1352 * fe_v.shape_value(i, point) * // c*phi_i(x_q)
1353 * sol_u_at_quadrature_points[point]) * // u_h(x_q)
1358 * FullMatrix<double> inverse(fe_v.n_quadrature_points,
1359 * fe_v.n_quadrature_points);
1360 * inverse.invert(copy_data.cell_mass_matrix);
1361 * Vector<double> proj(fe_v.n_quadrature_points); // projection of (f-c*U_h) on
1364 * the local fe_space
1367 * inverse.vmult(proj, copy_data.cell_mass_rhs); // M^{-1}*rhs = proj
1369 * double square_norm_over_cell = 0.0;
1370 * for (unsigned int point = 0; point < n_q_points; ++point)
1372 * const double diff = rhs.value(q_points[point]) -
1373 * sol_u_at_quadrature_points[point] - proj[point];
1374 * square_norm_over_cell += diff * diff * JxW[point];
1376 * copy_data.value_estimator = square_norm_over_cell;
1381 * Finally we have the boundary term with @f$||\beta (g-u_h^+)||^2@f$
1384 * const auto boundary_worker = [&](const Iterator &cell,
1385 * const unsigned int &face_no,
1386 * ScratchData<dim> &scratch_data,
1387 * CopyData ©_data) {
1388 * scratch_data.fe_interface_values.reinit(cell, face_no);
1389 * const FEFaceValuesBase<dim> &fe_fv =
1390 * scratch_data.fe_interface_values.get_fe_face_values(0);
1391 * const auto &q_points = fe_fv.get_quadrature_points();
1392 * const unsigned n_q_points = q_points.size();
1393 * const std::vector<double> &JxW = fe_fv.get_JxW_values();
1395 * std::vector<double> g(n_q_points);
1396 * exact_solution.value_list(q_points, g);
1398 * std::vector<double> sol_u(n_q_points);
1399 * fe_fv.get_function_values(solution, sol_u);
1401 * const std::vector<Tensor<1, dim>> &normals = fe_fv.get_normal_vectors();
1403 * double square_norm_over_bdary_face = 0.;
1404 * for (unsigned int point = 0; point < q_points.size(); ++point)
1406 * const double beta_dot_n = beta(q_points[point]) * normals[point];
1408 * if (beta_dot_n < 0) //\partial_{-T} \cap \partial_{- \Omega}
1410 * const double diff =
1411 * std::abs(beta_dot_n) * (g[point] - sol_u[point]);
1412 * square_norm_over_bdary_face += diff * diff * JxW[point];
1415 * copy_data.value_estimator += square_norm_over_bdary_face;
1420 * Then compute the interior face terms with @f$|| \sqrt{b \cdot n}[u_h]||^2@f$
1423 * const auto face_worker = [&](const Iterator &cell,
1424 * const unsigned int &f,
1425 * const unsigned int &sf,
1426 * const Iterator &ncell,
1427 * const unsigned int &nf,
1428 * const unsigned int &nsf,
1429 * ScratchData<dim> &scratch_data,
1430 * CopyData ©_data) {
1431 * FEInterfaceValues<dim> &fe_iv = scratch_data.fe_interface_values;
1432 * fe_iv.reinit(cell, f, sf, ncell, nf, nsf);
1434 * copy_data.face_data.emplace_back();
1435 * CopyDataFace ©_data_face = copy_data.face_data.back();
1436 * copy_data_face.cell_indices[0] = cell->active_cell_index();
1437 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
1439 * const auto &q_points = fe_iv.get_quadrature_points();
1440 * const unsigned n_q_points = q_points.size();
1442 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
1443 * std::vector<double> g(n_q_points);
1445 * std::vector<double> jump(n_q_points);
1446 * get_function_jump(fe_iv, solution, jump);
1448 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
1450 * double error_jump_square{0.0};
1451 * for (unsigned int point = 0; point < n_q_points; ++point)
1453 * const double beta_dot_n = beta(q_points[point]) * normals[point];
1454 * if (beta_dot_n < 0)
1456 * error_jump_square +=
1457 * std::abs(beta_dot_n) * jump[point] * jump[point] * JxW[point];
1461 * copy_data_face.values[0] = error_jump_square;
1462 * copy_data_face.values[1] = copy_data_face.values[0];
1465 * ScratchData<dim> scratch_data(mapping,
1467 * QGauss<dim>{fe->tensor_degree() + 1},
1468 * QGauss<dim - 1>{fe->tensor_degree() + 1});
1470 * const auto copier = [&](const auto ©_data) {
1471 * if (copy_data.cell_index != numbers::invalid_unsigned_int)
1473 * error_indicator_per_cell[copy_data.cell_index] +=
1474 * copy_data.value_estimator;
1476 * for (auto &cdf : copy_data.face_data)
1478 * for (unsigned int j = 0; j < 2; ++j)
1480 * error_indicator_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1487 * Here, we finally handle the assembly of the Mass matrix (M)_{ij} = (\phi_j,
1488 * \phi_i)_T. We pass in ScratchData and CopyData objects
1491 * CopyData copy_data;
1492 * MeshWorker::mesh_loop(dof_handler.begin_active(),
1493 * dof_handler.end(),
1498 * MeshWorker::assemble_own_cells |
1499 * MeshWorker::assemble_boundary_faces |
1500 * MeshWorker::assemble_own_interior_faces_once,
1509 * Usual <code>run</code> function, which runs over several refinement cycles
1512 * template <int dim>
1514 * AdvectionReaction<dim>::run()
1516 * std::vector<double> energy_errors;
1517 * std::vector<int> dofs_hist;
1518 * for (unsigned int cycle = 0; cycle < n_refinement_cycles; ++cycle)
1520 * std::cout << "Cycle " << cycle << std::endl;
1524 * GridGenerator::hyper_cube(triangulation);
1525 * triangulation.refine_global(n_global_refinements);
1531 * std::cout << " Number of active cells: "
1532 * << triangulation.n_active_cells() << std::endl;
1533 * std::cout << " Number of degrees of freedom: " << dof_handler.n_dofs()
1537 * assemble_system();
1540 * output_results(cycle);
1542 * energy_errors.emplace_back(compute_energy_norm());
1543 * dofs_hist.emplace_back(triangulation.n_active_cells());
1545 * error_table.output_table(std::cout);
1547 * for (unsigned int i = 0; i < n_refinement_cycles; ++i)
1548 * std::cout << "Cycle " << i << "\t" << energy_errors[i] << std::endl;
1552 * Explicit instantiation
1555 * template class AdvectionReaction<1>;
1556 * template class AdvectionReaction<2>;
1557 * template class AdvectionReaction<3>;
void reinit(const CellIteratorType &cell, const unsigned int face_no, const unsigned int sub_face_no, const CellNeighborIteratorType &cell_neighbor, const unsigned int face_no_neighbor, const unsigned int sub_face_no_neighbor, const unsigned int q_index=numbers::invalid_unsigned_int, const unsigned int mapping_index=numbers::invalid_unsigned_int, const unsigned int fe_index=numbers::invalid_unsigned_int, const unsigned int fe_index_neighbor=numbers::invalid_unsigned_int)
const std::vector< double > & get_JxW_values() const
const std::vector< Point< spacedim > > & get_quadrature_points() const
unsigned int depth_console(const unsigned int n)
Abstract base class for mapping classes.
The ParsedConvergenceTable class.
unsigned int n_active_cells() const
#define Assert(cond, exc)
typename ActiveSelector::active_cell_iterator active_cell_iterator
void mesh_loop(const CellIteratorType &begin, const CellIteratorType &end, const CellWorkerFunctionType &cell_worker, const CopierType &copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const AssembleFlags flags=assemble_own_cells, const BoundaryWorkerFunctionType &boundary_worker=BoundaryWorkerFunctionType(), const FaceWorkerFunctionType &face_worker=FaceWorkerFunctionType(), const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
@ update_values
Shape function values.
@ update_normal_vectors
Normal vectors.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
void cell_matrix(FullMatrix< double > &M, const FEValuesBase< dim > &fe, const FEValuesBase< dim > &fetest, const ArrayView< const std::vector< double > > &velocity, const double factor=1.)
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
@ assemble_boundary_faces
@ assemble_own_interior_faces_once
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
Tensor< 2, dim, Number > F(const Tensor< 2, dim, Number > &Grad_u)
void run(const Iterator &begin, const std_cxx20::type_identity_t< Iterator > &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
void copy(const T *begin, const T *end, U *dest)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
static const unsigned int invalid_unsigned_int
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation