277 *
for (
unsigned int i = 0; i <
values.size(); ++i)
286 * SmoothSolution<dim>::gradient(
const Point<dim> &point,
287 *
const unsigned int )
const
295 *
return return_value;
302 * The corresponding right-hand side of the smooth function:
306 *
class SmoothRightHandSide :
public Function<dim>
309 * SmoothRightHandSide()
314 * std::vector<double> &values,
315 *
const unsigned int )
const override;
322 * SmoothRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
323 * std::vector<double> &values,
324 *
const unsigned int )
const
327 *
for (
unsigned int i = 0; i <
values.size(); ++i)
328 * values[i] = 8. * PI * PI *
std::sin(2. * PI * points[i][0]) *
336 * The right-hand side that corresponds to the function
338 * assume that the diffusion coefficient @f$\nu = 1@f$:
342 *
class SingularRightHandSide :
public Function<dim>
345 * SingularRightHandSide()
350 * std::vector<double> &values,
351 *
const unsigned int )
const override;
361 * SingularRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
362 * std::vector<double> &values,
363 *
const unsigned int )
const
365 *
for (
unsigned int i = 0; i <
values.size(); ++i)
374 * <a name=
"step_74-Auxiliaryfunctions"></a>
376 * This function computes the penalty @f$\sigma@f$.
379 *
double get_penalty_factor(
const unsigned int fe_degree,
380 *
const double cell_extent_left,
381 *
const double cell_extent_right)
383 *
const unsigned int degree =
std::max(1U, fe_degree);
384 *
return degree * (degree + 1.) * 0.5 *
385 * (1. / cell_extent_left + 1. / cell_extent_right);
392 * <a name=
"step_74-TheCopyData"></a>
393 * <h3>The CopyData</h3>
395 * which is essentially the same as @ref step_12
"step-12". Note that the
396 *
"Scratch" object is not defined here because we use
398 * objects is extensively explained in the
WorkStream namespace documentation.
401 *
struct CopyDataFace
404 * std::vector<types::global_dof_index> joint_dof_indices;
405 * std::array<double, 2>
values;
406 * std::array<unsigned int, 2> cell_indices;
415 * std::vector<types::global_dof_index> local_dof_indices;
416 * std::vector<CopyDataFace> face_data;
421 *
template <
class Iterator>
422 *
void reinit(
const Iterator &cell,
const unsigned int dofs_per_cell)
424 *
cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
425 * cell_rhs.reinit(dofs_per_cell);
426 * local_dof_indices.resize(dofs_per_cell);
427 * cell->get_dof_indices(local_dof_indices);
436 * <a name=
"step_74-TheSIPGLaplaceclass"></a>
437 * <h3>The SIPGLaplace
class</h3>
438 * After these preparations, we proceed with the main
class of this program,
439 * called `SIPGLaplace`. The overall structure of the
class is as in many
440 * of the other tutorial programs. Major differences will only come up in the
449 * SIPGLaplace(
const TestCase &test_case);
453 *
void setup_system();
454 *
void assemble_system();
456 *
void refine_grid();
457 *
void output_results(
const unsigned int cycle)
const;
459 *
void compute_errors();
460 *
void compute_error_estimate();
461 *
double compute_energy_norm_error();
464 *
const unsigned int degree;
466 *
const QGauss<dim - 1> face_quadrature;
468 *
const QGauss<dim - 1> face_quadrature_overintegration;
483 * The remainder of the
class's members are used for the following:
484 * - Vectors to store error estimator square and energy norm square per
486 * - Print convergence rate and errors on the screen.
487 * - The fiffusion coefficient @f$\nu@f$ is set to 1.
488 * - Members that store information about the test case to be computed.
491 * Vector<double> estimated_error_square_per_cell;
492 * Vector<double> energy_norm_square_per_cell;
494 * ConvergenceTable convergence_table;
496 * const double diffusion_coefficient = 1.;
498 * const TestCase test_case;
499 * std::unique_ptr<const Function<dim>> exact_solution;
500 * std::unique_ptr<const Function<dim>> rhs_function;
505 * The constructor here takes the test case as input and then
506 * determines the correct solution and right-hand side classes. The
507 * remaining member variables are initialized in the obvious way.
511 * SIPGLaplace<dim>::SIPGLaplace(const TestCase &test_case)
513 * , quadrature(degree + 1)
514 * , face_quadrature(degree + 1)
515 * , quadrature_overintegration(degree + 2)
516 * , face_quadrature_overintegration(degree + 2)
519 * , dof_handler(triangulation)
520 * , test_case(test_case)
522 * if (test_case == TestCase::convergence_rate)
524 * exact_solution = std::make_unique<const SmoothSolution<dim>>();
525 * rhs_function = std::make_unique<const SmoothRightHandSide<dim>>();
528 * else if (test_case == TestCase::l_singularity)
531 * std::make_unique<const Functions::LSingularityFunction>();
532 * rhs_function = std::make_unique<const SingularRightHandSide<dim>>();
535 * AssertThrow(false, ExcNotImplemented());
541 * void SIPGLaplace<dim>::setup_system()
543 * dof_handler.distribute_dofs(fe);
544 * DynamicSparsityPattern dsp(dof_handler.n_dofs());
545 * DoFTools::make_flux_sparsity_pattern(dof_handler, dsp);
546 * sparsity_pattern.copy_from(dsp);
548 * system_matrix.reinit(sparsity_pattern);
549 * solution.reinit(dof_handler.n_dofs());
550 * system_rhs.reinit(dof_handler.n_dofs());
558 * <a name="step_74-Theassemble_systemfunction"></a>
559 * <h3>The assemble_system function</h3>
560 * The assemble function here is similar to that in @ref step_12 "step-12" and @ref step_47 "step-47".
561 * Different from assembling by hand, we just need to focus
562 * on assembling on each cell, each boundary face, and each
563 * interior face. The loops over cells and faces are handled
564 * automatically by MeshWorker::mesh_loop().
568 * The function starts by defining a local (lambda) function that is
569 * used to integrate the cell terms:
573 * void SIPGLaplace<dim>::assemble_system()
575 * const auto cell_worker =
576 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
577 * ScratchData &scratch_data,
578 * CopyData ©_data) {
579 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
580 * const unsigned int dofs_per_cell = fe_v.dofs_per_cell;
581 * copy_data.reinit(cell, dofs_per_cell);
583 * const std::vector<Point<dim>> &q_points =
584 * scratch_data.get_quadrature_points();
585 * const unsigned int n_q_points = q_points.size();
586 * const std::vector<double> &JxW = scratch_data.get_JxW_values();
588 * std::vector<double> rhs(n_q_points);
589 * rhs_function->value_list(q_points, rhs);
591 * for (unsigned int point = 0; point < n_q_points; ++point)
592 * for (unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
594 * for (unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
595 * copy_data.cell_matrix(i, j) +=
596 * diffusion_coefficient * // nu
597 * fe_v.shape_grad(i, point) * // grad v_h
598 * fe_v.shape_grad(j, point) * // grad u_h
601 * copy_data.cell_rhs(i) += fe_v.shape_value(i, point) * // v_h
609 * Next, we need a function that assembles face integrals on the boundary:
612 * const auto boundary_worker =
613 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
614 * const unsigned int &face_no,
615 * ScratchData &scratch_data,
616 * CopyData ©_data) {
617 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
619 * const std::vector<Point<dim>> &q_points =
620 * scratch_data.get_quadrature_points();
621 * const unsigned int n_q_points = q_points.size();
622 * const unsigned int dofs_per_cell = fe_fv.dofs_per_cell;
624 * const std::vector<double> &JxW = scratch_data.get_JxW_values();
625 * const std::vector<Tensor<1, dim>> &normals =
626 * scratch_data.get_normal_vectors();
628 * std::vector<double> g(n_q_points);
629 * exact_solution->value_list(q_points, g);
631 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
632 * const double penalty = get_penalty_factor(degree, extent1, extent1);
634 * for (unsigned int point = 0; point < n_q_points; ++point)
636 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
637 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
638 * copy_data.cell_matrix(i, j) +=
639 * (-diffusion_coefficient * // - nu
640 * fe_fv.shape_value(i, point) * // v_h
641 * (fe_fv.shape_grad(j, point) * // (grad u_h .
642 * normals[point]) // n)
644 * - diffusion_coefficient * // - nu
645 * (fe_fv.shape_grad(i, point) * // (grad v_h .
646 * normals[point]) * // n)
647 * fe_fv.shape_value(j, point) // u_h
649 * + diffusion_coefficient * penalty * // + nu sigma
650 * fe_fv.shape_value(i, point) * // v_h
651 * fe_fv.shape_value(j, point) // u_h
656 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
657 * copy_data.cell_rhs(i) +=
658 * (-diffusion_coefficient * // - nu
659 * (fe_fv.shape_grad(i, point) * // (grad v_h .
660 * normals[point]) * // n)
664 * + diffusion_coefficient * penalty * // + nu sigma
665 * fe_fv.shape_value(i, point) * g[point] // v_h g
674 * Finally, a function that assembles face integrals on interior
675 * faces. To reinitialize FEInterfaceValues, we need to pass
676 * cells, face and subface indices (for adaptive refinement) to
677 * the reinit() function of FEInterfaceValues:
680 * const auto face_worker =
681 * [&](const typename DoFHandler<dim>::cell_iterator &cell,
682 * const unsigned int &f,
683 * const unsigned int &sf,
684 * const typename DoFHandler<dim>::cell_iterator &ncell,
685 * const unsigned int &nf,
686 * const unsigned int &nsf,
687 * ScratchData &scratch_data,
688 * CopyData ©_data) {
689 * const FEInterfaceValues<dim> &fe_iv =
690 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
692 * copy_data.face_data.emplace_back();
693 * CopyDataFace ©_data_face = copy_data.face_data.back();
694 * const unsigned int n_dofs_face = fe_iv.n_current_interface_dofs();
695 * copy_data_face.joint_dof_indices = fe_iv.get_interface_dof_indices();
696 * copy_data_face.cell_matrix.reinit(n_dofs_face, n_dofs_face);
698 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
699 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
701 * const double extent1 = cell->measure() / cell->face(f)->measure();
702 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
703 * const double penalty = get_penalty_factor(degree, extent1, extent2);
705 * for (const unsigned int point : fe_iv.quadrature_point_indices())
707 * for (const unsigned int i : fe_iv.dof_indices())
708 * for (const unsigned int j : fe_iv.dof_indices())
709 * copy_data_face.cell_matrix(i, j) +=
710 * (-diffusion_coefficient * // - nu
711 * fe_iv.jump_in_shape_values(i, point) * // [v_h]
712 * (fe_iv.average_of_shape_gradients(j,
713 * point) * // ({grad u_h} .
714 * normals[point]) // n)
716 * - diffusion_coefficient * // - nu
717 * (fe_iv.average_of_shape_gradients(i,
718 * point) * // (grad v_h .
719 * normals[point]) * // n)
720 * fe_iv.jump_in_shape_values(j, point) // [u_h]
722 * + diffusion_coefficient * penalty * // + nu sigma
723 * fe_iv.jump_in_shape_values(i, point) * // [v_h]
724 * fe_iv.jump_in_shape_values(j, point) // [u_h]
733 * The following lambda function will then copy data into the
734 * global matrix and right-hand side. Though there are no hanging
735 * node constraints in DG discretization, we define an empty
736 * AffineConstraints object that allows us to use the
737 * AffineConstraints::distribute_local_to_global() functionality.
740 * AffineConstraints<double> constraints;
741 * constraints.close();
742 * const auto copier = [&](const CopyData &c) {
743 * constraints.distribute_local_to_global(c.cell_matrix,
745 * c.local_dof_indices,
751 * Copy data from interior face assembly to the global matrix.
754 * for (const CopyDataFace &cdf : c.face_data)
756 * constraints.distribute_local_to_global(cdf.cell_matrix,
757 * cdf.joint_dof_indices,
765 * With the assembly functions defined, we can now create
766 * ScratchData and CopyData objects, and pass them together with
767 * the lambda functions above to MeshWorker::mesh_loop(). In
768 * addition, we need to specify that we want to assemble on
769 * interior faces exactly once.
772 * const UpdateFlags cell_flags = update_values | update_gradients |
773 * update_quadrature_points | update_JxW_values;
774 * const UpdateFlags face_flags = update_values | update_gradients |
775 * update_quadrature_points |
776 * update_normal_vectors | update_JxW_values;
778 * ScratchData scratch_data(
779 * mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
780 * CopyData copy_data;
782 * MeshWorker::mesh_loop(dof_handler.begin_active(),
788 * MeshWorker::assemble_own_cells |
789 * MeshWorker::assemble_boundary_faces |
790 * MeshWorker::assemble_own_interior_faces_once,
800 * <a name="step_74-Thesolveandoutput_resultsfunction"></a>
801 * <h3>The solve() and output_results() function</h3>
802 * The following two functions are entirely standard and without difficulty.
806 * void SIPGLaplace<dim>::solve()
808 * SparseDirectUMFPACK A_direct;
809 * A_direct.initialize(system_matrix);
810 * A_direct.vmult(solution, system_rhs);
816 * void SIPGLaplace<dim>::output_results(const unsigned int cycle) const
818 * const std::string filename = "sol_Q" + Utilities::int_to_string(degree, 1) +
819 * "-" + Utilities::int_to_string(cycle, 2) +
821 * std::ofstream output(filename);
823 * DataOut<dim> data_out;
824 * data_out.attach_dof_handler(dof_handler);
825 * data_out.add_data_vector(solution, "u", DataOut<dim>::type_dof_data);
826 * data_out.build_patches(mapping);
827 * data_out.write_vtu(output);
834 * <a name="step_74-Thecompute_error_estimatefunction"></a>
835 * <h3>The compute_error_estimate() function</h3>
836 * The assembly of the error estimator here is quite similar to
837 * that of the global matrix and right-had side and can be handled
838 * by the MeshWorker::mesh_loop() framework. To understand what
839 * each of the local (lambda) functions is doing, recall first that
840 * the local cell residual is defined as
841 * @f$h_K^2 \left\| f + \nu \Delta u_h \right\|_K^2@f$:
845 * void SIPGLaplace<dim>::compute_error_estimate()
847 * const auto cell_worker =
848 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
849 * ScratchData &scratch_data,
850 * CopyData ©_data) {
851 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
853 * copy_data.cell_index = cell->active_cell_index();
855 * const std::vector<Point<dim>> &q_points = fe_v.get_quadrature_points();
856 * const unsigned int n_q_points = q_points.size();
857 * const std::vector<double> &JxW = fe_v.get_JxW_values();
859 * std::vector<Tensor<2, dim>> hessians(n_q_points);
860 * fe_v.get_function_hessians(solution, hessians);
862 * std::vector<double> rhs(n_q_points);
863 * rhs_function->value_list(q_points, rhs);
865 * const double hk = cell->diameter();
866 * double residual_norm_square = 0;
868 * for (unsigned int point = 0; point < n_q_points; ++point)
870 * const double residual =
871 * rhs[point] + diffusion_coefficient * trace(hessians[point]);
872 * residual_norm_square += residual * residual * JxW[point];
874 * copy_data.value = hk * hk * residual_norm_square;
879 * Next compute boundary terms @f$\sum_{f\in \partial K \cap \partial \Omega}
880 * \sigma \left\| [ u_h-g_D ] \right\|_f^2 @f$:
883 * const auto boundary_worker =
884 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
885 * const unsigned int &face_no,
886 * ScratchData &scratch_data,
887 * CopyData ©_data) {
888 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
890 * const std::vector<Point<dim>> &q_points = fe_fv.get_quadrature_points();
891 * const unsigned n_q_points = q_points.size();
893 * const std::vector<double> &JxW = fe_fv.get_JxW_values();
895 * std::vector<double> g(n_q_points);
896 * exact_solution->value_list(q_points, g);
898 * std::vector<double> sol_u(n_q_points);
899 * fe_fv.get_function_values(solution, sol_u);
901 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
902 * const double penalty = get_penalty_factor(degree, extent1, extent1);
904 * double difference_norm_square = 0.;
905 * for (unsigned int point = 0; point < q_points.size(); ++point)
907 * const double diff = (g[point] - sol_u[point]);
908 * difference_norm_square += diff * diff * JxW[point];
910 * copy_data.value += penalty * difference_norm_square;
915 * And finally interior face terms @f$\sum_{f\in \partial K}\lbrace \sigma
916 * \left\| [u_h] \right\|_f^2 + h_f \left\| [\nu \nabla u_h \cdot
917 * \mathbf n ] \right\|_f^2 \rbrace@f$:
920 * const auto face_worker =
921 * [&](const typename DoFHandler<dim>::cell_iterator &cell,
922 * const unsigned int &f,
923 * const unsigned int &sf,
924 * const typename DoFHandler<dim>::cell_iterator &ncell,
925 * const unsigned int &nf,
926 * const unsigned int &nsf,
927 * ScratchData &scratch_data,
928 * CopyData ©_data) {
929 * const FEInterfaceValues<dim> &fe_iv =
930 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
932 * copy_data.face_data.emplace_back();
933 * CopyDataFace ©_data_face = copy_data.face_data.back();
935 * copy_data_face.cell_indices[0] = cell->active_cell_index();
936 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
938 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
939 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
941 * const std::vector<Point<dim>> &q_points = fe_iv.get_quadrature_points();
942 * const unsigned int n_q_points = q_points.size();
944 * std::vector<double> jump(n_q_points);
945 * fe_iv.get_jump_in_function_values(solution, jump);
947 * std::vector<Tensor<1, dim>> grad_jump(n_q_points);
948 * fe_iv.get_jump_in_function_gradients(solution, grad_jump);
950 * const double h = cell->face(f)->diameter();
952 * const double extent1 = cell->measure() / cell->face(f)->measure();
953 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
954 * const double penalty = get_penalty_factor(degree, extent1, extent2);
956 * double flux_jump_square = 0;
957 * double u_jump_square = 0;
958 * for (unsigned int point = 0; point < n_q_points; ++point)
960 * u_jump_square += jump[point] * jump[point] * JxW[point];
961 * const double flux_jump = grad_jump[point] * normals[point];
962 * flux_jump_square +=
963 * diffusion_coefficient * flux_jump * flux_jump * JxW[point];
965 * copy_data_face.values[0] =
966 * 0.5 * h * (flux_jump_square + penalty * u_jump_square);
967 * copy_data_face.values[1] = copy_data_face.values[0];
972 * Having computed local contributions for each cell, we still
973 * need a way to copy these into the global vector that will hold
974 * the error estimators for all cells:
977 * const auto copier = [&](const CopyData ©_data) {
978 * if (copy_data.cell_index != numbers::invalid_unsigned_int)
979 * estimated_error_square_per_cell[copy_data.cell_index] +=
981 * for (const CopyDataFace &cdf : copy_data.face_data)
982 * for (unsigned int j = 0; j < 2; ++j)
983 * estimated_error_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
988 * After all of this set-up, let's
do the actual work: We resize
989 * the vector into which the results will be written, and then
1002 * ScratchData scratch_data(
1003 * mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
1005 * CopyData copy_data;
1007 * dof_handler.end(),
1022 * <a name=
"step_74-Thecompute_energy_norm_errorfunction"></a>
1023 * <h3>The compute_energy_norm_error() function</h3>
1024 * Next, we evaluate the accuracy in terms of the energy norm.
1025 * This function is similar to the assembling of the error estimator above.
1026 * Here we compute the square of the energy norm defined by
1028 * \|u \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla u \|_K^2 +
1029 * \sum_{f \in F_i} \sigma \| [ u ] \|_f^2 +
1030 * \sum_{f \in F_b} \sigma \|u\|_f^2.
1032 * Therefore the corresponding error is
1034 * \|u -u_h \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2
1035 * + \sum_{f \in F_i} \sigma \|[ u_h ] \|_f^2 + \sum_{f \in F_b}\sigma
1040 *
template <
int dim>
1041 *
double SIPGLaplace<dim>::compute_energy_norm_error()
1043 * energy_norm_square_per_cell.reinit(
triangulation.n_active_cells());
1047 * Assemble @f$\sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2 @f$.
1050 *
const auto cell_worker =
1052 * ScratchData &scratch_data,
1053 * CopyData ©_data) {
1056 * copy_data.cell_index = cell->active_cell_index();
1058 *
const std::vector<Point<dim>> &q_points = fe_v.get_quadrature_points();
1059 *
const unsigned int n_q_points = q_points.size();
1060 *
const std::vector<double> &JxW = fe_v.get_JxW_values();
1062 * std::vector<Tensor<1, dim>> grad_u(n_q_points);
1063 * fe_v.get_function_gradients(solution, grad_u);
1065 * std::vector<Tensor<1, dim>> grad_exact(n_q_points);
1066 * exact_solution->gradient_list(q_points, grad_exact);
1068 *
double norm_square = 0;
1069 *
for (
unsigned int point = 0;
point < n_q_points; ++
point)
1072 * (grad_u[point] - grad_exact[point]).norm_square() * JxW[
point];
1074 * copy_data.value = diffusion_coefficient * norm_square;
1079 * Assemble @f$\sum_{f \in F_b}\sigma \|u_h-g_D\|_f^2@f$.
1082 *
const auto boundary_worker =
1084 *
const unsigned int &face_no,
1085 * ScratchData &scratch_data,
1086 * CopyData ©_data) {
1090 *
const unsigned n_q_points = q_points.size();
1092 *
const std::vector<double> &JxW = fe_fv.get_JxW_values();
1094 * std::vector<double> g(n_q_points);
1095 * exact_solution->value_list(q_points, g);
1097 * std::vector<double> sol_u(n_q_points);
1098 * fe_fv.get_function_values(solution, sol_u);
1100 *
const double extent1 = cell->measure() / cell->face(face_no)->measure();
1101 *
const double penalty = get_penalty_factor(degree, extent1, extent1);
1103 *
double difference_norm_square = 0.;
1104 *
for (
unsigned int point = 0;
point < q_points.size(); ++
point)
1106 *
const double diff = (g[
point] - sol_u[
point]);
1107 * difference_norm_square += diff * diff * JxW[
point];
1109 * copy_data.value += penalty * difference_norm_square;
1114 * Assemble @f$\sum_{f \in F_i} \sigma \| [ u_h ] \|_f^2@f$.
1117 *
const auto face_worker =
1119 *
const unsigned int &f,
1120 *
const unsigned int &sf,
1122 *
const unsigned int &nf,
1123 *
const unsigned int &nsf,
1124 * ScratchData &scratch_data,
1125 * CopyData ©_data) {
1127 * scratch_data.
reinit(cell, f, sf, ncell, nf, nsf);
1129 * copy_data.face_data.emplace_back();
1130 * CopyDataFace ©_data_face = copy_data.face_data.back();
1132 * copy_data_face.cell_indices[0] = cell->active_cell_index();
1133 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
1135 *
const std::vector<double> &JxW = fe_iv.get_JxW_values();
1137 *
const std::vector<Point<dim>> &q_points = fe_iv.get_quadrature_points();
1138 *
const unsigned int n_q_points = q_points.size();
1140 * std::vector<double> jump(n_q_points);
1141 * fe_iv.get_jump_in_function_values(solution, jump);
1143 *
const double extent1 = cell->measure() / cell->face(f)->measure();
1144 *
const double extent2 = ncell->measure() / ncell->face(nf)->measure();
1145 *
const double penalty = get_penalty_factor(degree, extent1, extent2);
1147 *
double u_jump_square = 0;
1148 *
for (
unsigned int point = 0;
point < n_q_points; ++
point)
1152 * copy_data_face.values[0] = 0.5 * penalty * u_jump_square;
1153 * copy_data_face.values[1] = copy_data_face.values[0];
1156 *
const auto copier = [&](
const CopyData ©_data) {
1158 * energy_norm_square_per_cell[copy_data.cell_index] += copy_data.value;
1159 *
for (
const CopyDataFace &cdf : copy_data.face_data)
1160 * for (unsigned
int j = 0; j < 2; ++j)
1161 * energy_norm_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1169 *
const ScratchData scratch_data(mapping,
1171 * quadrature_overintegration,
1173 * face_quadrature_overintegration,
1176 * CopyData copy_data;
1178 * dof_handler.end(),
1188 *
const double energy_error =
1189 *
std::sqrt(energy_norm_square_per_cell.l1_norm());
1190 *
return energy_error;
1198 * <a name=
"step_74-Therefine_gridfunction"></a>
1199 * <h3>The refine_grid() function</h3>
1202 * template <
int dim>
1203 *
void SIPGLaplace<dim>::refine_grid()
1205 *
const double refinement_fraction = 0.1;
1208 *
triangulation, estimated_error_square_per_cell, refinement_fraction, 0.);
1218 * <a name=
"step_74-Thecompute_errorsfunction"></a>
1219 * <h3>The compute_errors() function</h3>
1220 * We compute three errors in the @f$L_2@f$ norm, @f$H_1@f$ seminorm, and
1221 * the energy norm, respectively. These are then printed to screen,
1222 * but also stored in a table that records how these errors decay
1223 * with mesh refinement and which can be output in one step at the
1224 * end of the program.
1227 * template <
int dim>
1228 *
void SIPGLaplace<dim>::compute_errors()
1230 *
double L2_error, H1_error, energy_error;
1237 * *(exact_solution.get()),
1238 * difference_per_cell,
1239 * quadrature_overintegration,
1243 * difference_per_cell,
1245 * convergence_table.add_value(
"L2", L2_error);
1253 * *(exact_solution.get()),
1254 * difference_per_cell,
1255 * quadrature_overintegration,
1259 * difference_per_cell,
1261 * convergence_table.add_value(
"H1", H1_error);
1265 * energy_error = compute_energy_norm_error();
1266 * convergence_table.add_value(
"Energy", energy_error);
1269 * std::cout <<
" Error in the L2 norm : " << L2_error << std::endl
1270 * <<
" Error in the H1 seminorm : " << H1_error << std::endl
1271 * <<
" Error in the energy norm : " << energy_error
1280 * <a name=
"step_74-Therunfunction"></a>
1281 * <h3>The
run() function</h3>
1284 * template <
int dim>
1285 *
void SIPGLaplace<dim>::run()
1287 *
const unsigned int max_cycle =
1288 * (test_case == TestCase::convergence_rate ? 6 : 20);
1289 *
for (
unsigned int cycle = 0; cycle < max_cycle; ++cycle)
1291 * std::cout <<
"Cycle " << cycle << std::endl;
1293 *
switch (test_case)
1295 *
case TestCase::convergence_rate:
1310 *
case TestCase::l_singularity:
1330 * std::cout <<
" Number of active cells : "
1334 * std::cout <<
" Number of degrees of freedom : " << dof_handler.n_dofs()
1337 * assemble_system();
1339 * output_results(cycle);
1341 * convergence_table.add_value(
"cycle", cycle);
1342 * convergence_table.add_value(
"cells",
triangulation.n_active_cells());
1343 * convergence_table.add_value(
"dofs", dof_handler.n_dofs());
1347 *
if (test_case == TestCase::l_singularity)
1349 * compute_error_estimate();
1350 * std::cout <<
" Estimated error : "
1351 * <<
std::sqrt(estimated_error_square_per_cell.l1_norm())
1354 * convergence_table.add_value(
1356 *
std::sqrt(estimated_error_square_per_cell.l1_norm()));
1358 * std::cout << std::endl;
1363 * Having
run all of our computations, let us tell the convergence
1364 * table how to format its
data and output it to screen:
1367 * convergence_table.set_precision(
"L2", 3);
1368 * convergence_table.set_precision(
"H1", 3);
1369 * convergence_table.set_precision(
"Energy", 3);
1371 * convergence_table.set_scientific(
"L2",
true);
1372 * convergence_table.set_scientific(
"H1",
true);
1373 * convergence_table.set_scientific(
"Energy",
true);
1375 *
if (test_case == TestCase::convergence_rate)
1377 * convergence_table.evaluate_convergence_rates(
1379 * convergence_table.evaluate_convergence_rates(
1382 *
if (test_case == TestCase::l_singularity)
1384 * convergence_table.set_precision(
"Estimator", 3);
1385 * convergence_table.set_scientific(
"Estimator",
true);
1388 * std::cout <<
"degree = " << degree << std::endl;
1389 * convergence_table.write_text(
1399 * <a name=
"step_74-Themainfunction"></a>
1400 * <h3>The main() function</h3>
1401 * The following <code>main</code> function is similar to previous examples as
1402 * well, and need not be commented on.
1409 *
using namespace dealii;
1410 *
using namespace Step74;
1412 *
const TestCase test_case = TestCase::l_singularity;
1414 * SIPGLaplace<2> problem(test_case);
1417 *
catch (std::exception &exc)
1419 * std::cerr << std::endl
1421 * <<
"----------------------------------------------------"
1423 * std::cerr <<
"Exception on processing: " << std::endl
1424 * << exc.what() << std::endl
1425 * <<
"Aborting!" << std::endl
1426 * <<
"----------------------------------------------------"
1432 * std::cerr << std::endl
1434 * <<
"----------------------------------------------------"
1436 * std::cerr <<
"Unknown exception!" << std::endl
1437 * <<
"Aborting!" << std::endl
1438 * <<
"----------------------------------------------------"
1446<a name=
"step_74-Results"></a><h1>Results</h1>
1449The output of
this program consist of the console output and
1450solutions in
vtu format.
1452In the
first test
case, when you
run the program, the screen output should look like the following:
1455 Number of active cells : 16
1456 Number of degrees of freedom : 256
1457 Error in the
L2 norm : 0.00193285
1458 Error in the H1 seminorm : 0.106087
1459 Error in the energy
norm : 0.150625
1462 Number of active cells : 64
1463 Number of degrees of freedom : 1024
1464 Error in the
L2 norm : 9.60497e-05
1465 Error in the H1 seminorm : 0.0089954
1466 Error in the energy
norm : 0.0113265
1474When
using the smooth
case with polynomial degree 3, the convergence
1475table will look like
this:
1476<table align=
"center" class=
"doxtable">
1488 <td align=
"center">0</td>
1489 <td align=
"right">16</td>
1490 <td align=
"right">256</td>
1491 <td align=
"center">1.933e-03</td>
1493 <td align=
"center">1.061e-01</td>
1495 <td align=
"center">1.506e-01</td>
1498 <td align=
"center">1</td>
1499 <td align=
"right">64</td>
1500 <td align=
"right">1024</td>
1501 <td align=
"center">9.605e-05</td>
1502 <td align=
"center">4.33</td>
1503 <td align=
"center">8.995e-03</td>
1504 <td align=
"center">3.56</td>
1505 <td align=
"center">1.133e-02</td>
1508 <td align=
"center">2</td>
1509 <td align=
"right">256</td>
1510 <td align=
"right">4096</td>
1511 <td align=
"center">5.606e-06</td>
1512 <td align=
"center">4.10</td>
1513 <td align=
"center">9.018e-04</td>
1514 <td align=
"center">3.32</td>
1515 <td align=
"center">9.736e-04</td>
1518 <td align=
"center">3</td>
1519 <td align=
"right">1024</td>
1520 <td align=
"right">16384</td>
1521 <td align=
"center">3.484e-07</td>
1522 <td align=
"center">4.01</td>
1523 <td align=
"center">1.071e-04</td>
1524 <td align=
"center">3.07</td>
1525 <td align=
"center">1.088e-04</td>
1528 <td align=
"center">4</td>
1529 <td align=
"right">4096</td>
1530 <td align=
"right">65536</td>
1531 <td align=
"center">2.179e-08</td>
1532 <td align=
"center">4.00</td>
1533 <td align=
"center">1.327e-05</td>
1534 <td align=
"center">3.01</td>
1535 <td align=
"center">1.331e-05</td>
1538 <td align=
"center">5</td>
1539 <td align=
"right">16384</td>
1540 <td align=
"right">262144</td>
1541 <td align=
"center">1.363e-09</td>
1542 <td align=
"center">4.00</td>
1543 <td align=
"center">1.656e-06</td>
1544 <td align=
"center">3.00</td>
1545 <td align=
"center">1.657e-06</td>
1549Theoretically,
for polynomial degree @f$p@f$, the order of convergence in @f$L_2@f$
1550norm and @f$H^1@f$ seminorm should be @f$p+1@f$ and @f$p@f$, respectively. Our numerical
1551results are in good agreement with theory.
1553In the
second test
case, when you
run the program, the screen output should look like the following:
1556 Number of active cells : 192
1557 Number of degrees of freedom : 3072
1558 Error in the
L2 norm : 0.000323585
1559 Error in the H1 seminorm : 0.0296202
1560 Error in the energy
norm : 0.0420478
1561 Estimated error : 0.136067
1564 Number of active cells : 249
1565 Number of degrees of freedom : 3984
1566 Error in the
L2 norm : 0.000114739
1567 Error in the H1 seminorm : 0.0186571
1568 Error in the energy
norm : 0.0264879
1569 Estimated error : 0.0857186
1577The following figure provides a
log-
log plot of the errors versus
1578the number of degrees of freedom
for this test
case on the L-shaped
1579domain. In order to interpret it, let @f$n@f$ be the number of degrees of
1580freedom, then on uniformly refined meshes, @f$h@f$ is of order
1581@f$1/\
sqrt{n}@f$ in 2D. Combining the theoretical results in the previous
case,
1582we see that
if the solution is sufficiently smooth,
1583we can expect the error in the @f$L_2@f$
norm to be of order @f$O(n^{-\frac{p+1}{2}})@f$
1584and in @f$H^1@f$ seminorm to be @f$O(n^{-\frac{p}{2}})@f$. It is not a priori
1585clear that one would get the same kind of behavior as a function of
1586@f$n@f$ on adaptively refined meshes like the ones we use
for this second
1587test
case, but one can certainly hope. Indeed, from the figure, we see
1588that the SIPG with adaptive mesh refinement produces asymptotically
1589the kinds of hoped-
for results:
1591<img width=
"600px" src=
"https://www.dealii.org/images/steps/developer/step-74.log-log-plot.png" alt=
"">
1593In addition, we observe that the error estimator decreases
1594at almost the same rate as the errors in the energy
norm and @f$H^1@f$ seminorm,
1595and one order lower than the @f$L_2@f$ error. This suggests
1596its ability to predict regions with large errors.
1598While this tutorial is focused on the implementation, the @ref step_59
"step-59" tutorial program achieves an efficient
1599large-
scale solver in terms of computing time with
matrix-free solution techniques.
1600Note that the @ref step_59
"step-59" tutorial does not work with meshes containing hanging nodes at this moment,
1601because the multigrid interface matrices are not as easily determined,
1602but that is merely the lack of some interfaces in deal.II,
nothing fundamental.
1605<a name=
"step_74-PlainProg"></a>
1606<h1> The plain program</h1>
1607@include
"step-74.cc"
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< Point< spacedim > > & get_quadrature_points() const
void reinit(const TriaIterator< DoFCellAccessor< dim, spacedim, level_dof_access > > &cell)
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< RangeNumberType > &values, const unsigned int component=0) const
virtual double laplacian(const Point< 2 > &p, const unsigned int component=0) const override
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
typename ActiveSelector::cell_iterator cell_iterator
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_hessians
Second derivatives of shape functions.
@ 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.
#define DEAL_II_NOT_IMPLEMENTED()
std::vector< index_type > data
void hyper_L(Triangulation< dim > &tria, const double left=-1., const double right=1., const bool colorize=false)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
void refine_and_coarsen_fixed_number(Triangulation< dim, spacedim > &triangulation, const Vector< Number > &criteria, const double top_fraction_of_cells, const double bottom_fraction_of_cells, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
@ matrix
Contents is actually a matrix.
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)
void L2(Vector< number > &result, const FEValuesBase< dim > &fe, const std::vector< double > &input, const double factor=1.)
@ assemble_boundary_faces
@ assemble_own_interior_faces_once
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
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)
unsigned int n_cells(const internal::TriangulationImplementation::NumberCache< 1 > &c)
int(&) functions(const void *v1, const void *v2)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
static constexpr double PI
static const unsigned int invalid_unsigned_int
::VectorizedArray< Number, width > log(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation