276 *
for (
unsigned int i = 0; i <
values.size(); ++i)
286 *
const unsigned int )
const
294 *
return return_value;
301 * The corresponding right-hand side of the smooth function:
305 *
class SmoothRightHandSide :
public Function<dim>
308 * SmoothRightHandSide()
313 * std::vector<double> &
values,
314 *
const unsigned int )
const override;
321 * SmoothRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
322 * std::vector<double> &
values,
323 *
const unsigned int )
const
326 *
for (
unsigned int i = 0; i <
values.size(); ++i)
335 * The right-hand side that corresponds to the function
337 * assume that the diffusion coefficient @f$\nu = 1@f$:
341 *
class SingularRightHandSide :
public Function<dim>
344 * SingularRightHandSide()
349 * std::vector<double> &
values,
350 *
const unsigned int )
const override;
360 * SingularRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
361 * std::vector<double> &
values,
362 *
const unsigned int )
const
364 *
for (
unsigned int i = 0; i <
values.size(); ++i)
365 *
values[i] = -ref.laplacian(points[i]);
373 * <a name=
"Auxiliaryfunctions"></a>
375 * The following two auxiliary
functions are used to compute
376 * jump terms
for @f$u_h@f$ and @f$\nabla u_h@f$ on a face,
383 * std::vector<double> & jump)
385 *
const unsigned int n_q = fe_iv.n_quadrature_points;
386 * std::array<std::vector<double>, 2> face_values;
388 *
for (
unsigned int i = 0; i < 2; ++i)
390 * face_values[i].resize(n_q);
391 * fe_iv.get_fe_face_values(i).get_function_values(solution,
394 *
for (
unsigned int q = 0; q < n_q; ++q)
395 * jump[q] = face_values[0][q] - face_values[1][q];
405 *
const unsigned int n_q = fe_iv.n_quadrature_points;
406 * std::vector<Tensor<1, dim>> face_gradients[2];
407 * gradient_jump.resize(n_q);
408 *
for (
unsigned int i = 0; i < 2; ++i)
410 * face_gradients[i].resize(n_q);
411 * fe_iv.get_fe_face_values(i).get_function_gradients(solution,
412 * face_gradients[i]);
414 *
for (
unsigned int q = 0; q < n_q; ++q)
415 * gradient_jump[q] = face_gradients[0][q] - face_gradients[1][q];
420 * This function computes the penalty @f$\sigma@f$.
423 *
double get_penalty_factor(
const unsigned int fe_degree,
424 *
const double cell_extent_left,
425 *
const double cell_extent_right)
427 *
const unsigned int degree =
std::max(1U, fe_degree);
428 *
return degree * (degree + 1.) * 0.5 *
429 * (1. / cell_extent_left + 1. / cell_extent_right);
436 * <a name=
"TheCopyData"></a>
437 * <h3>The CopyData</h3>
439 * which is essentially the same as @ref step_12
"step-12". Note that the
440 *
"Scratch" object is not defined here because we use
442 * objects is extensively explained in the
WorkStream namespace documentation.
445 *
struct CopyDataFace
448 * std::vector<types::global_dof_index> joint_dof_indices;
449 * std::array<double, 2>
values;
450 * std::array<unsigned int, 2> cell_indices;
459 * std::vector<types::global_dof_index> local_dof_indices;
460 * std::vector<CopyDataFace> face_data;
465 *
template <
class Iterator>
466 *
void reinit(
const Iterator &cell,
const unsigned int dofs_per_cell)
468 *
cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
469 * cell_rhs.
reinit(dofs_per_cell);
470 * local_dof_indices.resize(dofs_per_cell);
471 * cell->get_dof_indices(local_dof_indices);
480 * <a name=
"TheSIPGLaplaceclass"></a>
481 * <h3>The SIPGLaplace
class</h3>
482 * After these preparations, we proceed with the main
class of this program,
483 * called `SIPGLaplace`. The overall structure of the
class is as in many
484 * of the other tutorial programs. Major differences will only come up in the
493 * SIPGLaplace(
const TestCase &test_case);
497 *
void setup_system();
498 *
void assemble_system();
500 *
void refine_grid();
501 *
void output_results(
const unsigned int cycle)
const;
503 *
void compute_errors();
504 *
void compute_error_estimate();
505 *
double compute_energy_norm_error();
508 *
const unsigned int degree;
510 *
const QGauss<dim - 1> face_quadrature;
512 *
const QGauss<dim - 1> face_quadrature_overintegration;
527 * The remainder of the
class's members are used for the following:
528 * - Vectors to store error estimator square and energy norm square per
530 * - Print convergence rate and errors on the screen.
531 * - The fiffusion coefficient @f$\nu@f$ is set to 1.
532 * - Members that store information about the test case to be computed.
535 * Vector<double> estimated_error_square_per_cell;
536 * Vector<double> energy_norm_square_per_cell;
538 * ConvergenceTable convergence_table;
540 * const double diffusion_coefficient = 1.;
542 * const TestCase test_case;
543 * std::unique_ptr<const Function<dim>> exact_solution;
544 * std::unique_ptr<const Function<dim>> rhs_function;
549 * The constructor here takes the test case as input and then
550 * determines the correct solution and right-hand side classes. The
551 * remaining member variables are initialized in the obvious way.
555 * SIPGLaplace<dim>::SIPGLaplace(const TestCase &test_case)
557 * , quadrature(degree + 1)
558 * , face_quadrature(degree + 1)
559 * , quadrature_overintegration(degree + 2)
560 * , face_quadrature_overintegration(degree + 2)
563 * , dof_handler(triangulation)
564 * , test_case(test_case)
566 * if (test_case == TestCase::convergence_rate)
568 * exact_solution = std::make_unique<const SmoothSolution<dim>>();
569 * rhs_function = std::make_unique<const SmoothRightHandSide<dim>>();
572 * else if (test_case == TestCase::l_singularity)
575 * std::make_unique<const Functions::LSingularityFunction>();
576 * rhs_function = std::make_unique<const SingularRightHandSide<dim>>();
579 * AssertThrow(false, ExcNotImplemented());
585 * void SIPGLaplace<dim>::setup_system()
587 * dof_handler.distribute_dofs(fe);
588 * DynamicSparsityPattern dsp(dof_handler.n_dofs());
589 * DoFTools::make_flux_sparsity_pattern(dof_handler, dsp);
590 * sparsity_pattern.copy_from(dsp);
592 * system_matrix.reinit(sparsity_pattern);
593 * solution.reinit(dof_handler.n_dofs());
594 * system_rhs.reinit(dof_handler.n_dofs());
602 * <a name="Theassemble_systemfunction"></a>
603 * <h3>The assemble_system function</h3>
604 * The assemble function here is similar to that in @ref step_12 "step-12" and @ref step_47 "step-47".
605 * Different from assembling by hand, we just need to focus
606 * on assembling on each cell, each boundary face, and each
607 * interior face. The loops over cells and faces are handled
608 * automatically by MeshWorker::mesh_loop().
612 * The function starts by defining a local (lambda) function that is
613 * used to integrate the cell terms:
617 * void SIPGLaplace<dim>::assemble_system()
619 * const auto cell_worker =
620 * [&](const auto &cell, auto &scratch_data, auto ©_data) {
621 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
622 * const unsigned int dofs_per_cell = fe_v.dofs_per_cell;
623 * copy_data.reinit(cell, dofs_per_cell);
625 * const auto & q_points = scratch_data.get_quadrature_points();
626 * const unsigned int n_q_points = q_points.size();
627 * const std::vector<double> &JxW = scratch_data.get_JxW_values();
629 * std::vector<double> rhs(n_q_points);
630 * rhs_function->value_list(q_points, rhs);
632 * for (unsigned int point = 0; point < n_q_points; ++point)
633 * for (unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
635 * for (unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
636 * copy_data.cell_matrix(i, j) +=
637 * diffusion_coefficient * // nu
638 * fe_v.shape_grad(i, point) * // grad v_h
639 * fe_v.shape_grad(j, point) * // grad u_h
642 * copy_data.cell_rhs(i) += fe_v.shape_value(i, point) * // v_h
650 * Next, we need a function that assembles face integrals on the boundary:
653 * const auto boundary_worker = [&](const auto & cell,
654 * const unsigned int &face_no,
655 * auto & scratch_data,
656 * auto & copy_data) {
657 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
659 * const auto & q_points = scratch_data.get_quadrature_points();
660 * const unsigned int n_q_points = q_points.size();
661 * const unsigned int dofs_per_cell = fe_fv.dofs_per_cell;
663 * const std::vector<double> & JxW = scratch_data.get_JxW_values();
664 * const std::vector<Tensor<1, dim>> &normals =
665 * scratch_data.get_normal_vectors();
667 * std::vector<double> g(n_q_points);
668 * exact_solution->value_list(q_points, g);
670 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
671 * const double penalty = get_penalty_factor(degree, extent1, extent1);
673 * for (unsigned int point = 0; point < n_q_points; ++point)
675 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
676 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
677 * copy_data.cell_matrix(i, j) +=
678 * (-diffusion_coefficient * // - nu
679 * fe_fv.shape_value(i, point) * // v_h
680 * (fe_fv.shape_grad(j, point) * // (grad u_h .
681 * normals[point]) // n)
683 * - diffusion_coefficient * // - nu
684 * (fe_fv.shape_grad(i, point) * // (grad v_h .
685 * normals[point]) * // n)
686 * fe_fv.shape_value(j, point) // u_h
688 * + diffusion_coefficient * penalty * // + nu sigma
689 * fe_fv.shape_value(i, point) * // v_h
690 * fe_fv.shape_value(j, point) // u_h
695 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
696 * copy_data.cell_rhs(i) +=
697 * (-diffusion_coefficient * // - nu
698 * (fe_fv.shape_grad(i, point) * // (grad v_h .
699 * normals[point]) * // n)
703 * + diffusion_coefficient * penalty * // + nu sigma
704 * fe_fv.shape_value(i, point) * g[point] // v_h g
713 * Finally, a function that assembles face integrals on interior
714 * faces. To reinitialize FEInterfaceValues, we need to pass
715 * cells, face and subface indices (for adaptive refinement) to
716 * the reinit() function of FEInterfaceValues:
719 * const auto face_worker = [&](const auto & cell,
720 * const unsigned int &f,
721 * const unsigned int &sf,
722 * const auto & ncell,
723 * const unsigned int &nf,
724 * const unsigned int &nsf,
725 * auto & scratch_data,
726 * auto & copy_data) {
727 * const FEInterfaceValues<dim> &fe_iv =
728 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
730 * const auto & q_points = fe_iv.get_quadrature_points();
731 * const unsigned int n_q_points = q_points.size();
733 * copy_data.face_data.emplace_back();
734 * CopyDataFace & copy_data_face = copy_data.face_data.back();
735 * const unsigned int n_dofs_face = fe_iv.n_current_interface_dofs();
736 * copy_data_face.joint_dof_indices = fe_iv.get_interface_dof_indices();
737 * copy_data_face.cell_matrix.reinit(n_dofs_face, n_dofs_face);
739 * const std::vector<double> & JxW = fe_iv.get_JxW_values();
740 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
742 * const double extent1 = cell->measure() / cell->face(f)->measure();
743 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
744 * const double penalty = get_penalty_factor(degree, extent1, extent2);
746 * for (unsigned int point = 0; point < n_q_points; ++point)
748 * for (unsigned int i = 0; i < n_dofs_face; ++i)
749 * for (unsigned int j = 0; j < n_dofs_face; ++j)
750 * copy_data_face.cell_matrix(i, j) +=
751 * (-diffusion_coefficient * // - nu
752 * fe_iv.jump(i, point) * // [v_h]
753 * (fe_iv.average_gradient(j, point) * // ({grad u_h} .
754 * normals[point]) // n)
756 * - diffusion_coefficient * // - nu
757 * (fe_iv.average_gradient(i, point) * // (grad v_h .
758 * normals[point]) * // n)
759 * fe_iv.jump(j, point) // [u_h]
761 * + diffusion_coefficient * penalty * // + nu sigma
762 * fe_iv.jump(i, point) * // [v_h]
763 * fe_iv.jump(j, point) // [u_h]
772 * The following lambda function will then copy data into the
773 * global matrix and right-hand side. Though there are no hanging
774 * node constraints in DG discretization, we define an empty
775 * AffineConstraints object that allows us to use the
776 * AffineConstraints::distribute_local_to_global() functionality.
779 * AffineConstraints<double> constraints;
780 * constraints.close();
781 * const auto copier = [&](const auto &c) {
782 * constraints.distribute_local_to_global(c.cell_matrix,
784 * c.local_dof_indices,
790 * Copy data from interior face assembly to the global matrix.
793 * for (auto &cdf : c.face_data)
795 * constraints.distribute_local_to_global(cdf.cell_matrix,
796 * cdf.joint_dof_indices,
804 * With the assembly functions defined, we can now create
805 * ScratchData and CopyData objects, and pass them together with
806 * the lambda functions above to MeshWorker::mesh_loop(). In
807 * addition, we need to specify that we want to assemble on
808 * interior faces exactly once.
811 * const UpdateFlags cell_flags = update_values | update_gradients |
812 * update_quadrature_points | update_JxW_values;
813 * const UpdateFlags face_flags = update_values | update_gradients |
814 * update_quadrature_points |
815 * update_normal_vectors | update_JxW_values;
817 * ScratchData scratch_data(
818 * mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
819 * CopyData copy_data;
821 * MeshWorker::mesh_loop(dof_handler.begin_active(),
827 * MeshWorker::assemble_own_cells |
828 * MeshWorker::assemble_boundary_faces |
829 * MeshWorker::assemble_own_interior_faces_once,
839 * <a name="Thesolveandoutput_resultsfunction"></a>
840 * <h3>The solve() and output_results() function</h3>
841 * The following two functions are entirely standard and without difficulty.
845 * void SIPGLaplace<dim>::solve()
847 * SparseDirectUMFPACK A_direct;
848 * A_direct.initialize(system_matrix);
849 * A_direct.vmult(solution, system_rhs);
855 * void SIPGLaplace<dim>::output_results(const unsigned int cycle) const
857 * const std::string filename = "sol_Q" + Utilities::int_to_string(degree, 1) +
858 * "-" + Utilities::int_to_string(cycle, 2) +
860 * std::ofstream output(filename);
862 * DataOut<dim> data_out;
863 * data_out.attach_dof_handler(dof_handler);
864 * data_out.add_data_vector(solution, "u", DataOut<dim>::type_dof_data);
865 * data_out.build_patches(mapping);
866 * data_out.write_vtu(output);
873 * <a name="Thecompute_error_estimatefunction"></a>
874 * <h3>The compute_error_estimate() function</h3>
875 * The assembly of the error estimator here is quite similar to
876 * that of the global matrix and right-had side and can be handled
877 * by the MeshWorker::mesh_loop() framework. To understand what
878 * each of the local (lambda) functions is doing, recall first that
879 * the local cell residual is defined as
880 * @f$h_K^2 \left\| f + \nu \Delta u_h \right\|_K^2@f$:
884 * void SIPGLaplace<dim>::compute_error_estimate()
886 * const auto cell_worker =
887 * [&](const auto &cell, auto &scratch_data, auto ©_data) {
888 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
890 * copy_data.cell_index = cell->active_cell_index();
892 * const auto & q_points = fe_v.get_quadrature_points();
893 * const unsigned int n_q_points = q_points.size();
894 * const std::vector<double> &JxW = fe_v.get_JxW_values();
896 * std::vector<Tensor<2, dim>> hessians(n_q_points);
897 * fe_v.get_function_hessians(solution, hessians);
899 * std::vector<double> rhs(n_q_points);
900 * rhs_function->value_list(q_points, rhs);
902 * const double hk = cell->diameter();
903 * double residual_norm_square = 0;
905 * for (unsigned int point = 0; point < n_q_points; ++point)
907 * const double residual =
908 * rhs[point] + diffusion_coefficient * trace(hessians[point]);
909 * residual_norm_square += residual * residual * JxW[point];
911 * copy_data.value = hk * hk * residual_norm_square;
916 * Next compute boundary terms @f$\sum_{f\in \partial K \cap \partial \Omega}
917 * \sigma \left\| [ u_h-g_D ] \right\|_f^2 @f$:
920 * const auto boundary_worker = [&](const auto & cell,
921 * const unsigned int &face_no,
922 * auto & scratch_data,
923 * auto & copy_data) {
924 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
926 * const auto & q_points = fe_fv.get_quadrature_points();
927 * const unsigned n_q_points = q_points.size();
929 * const std::vector<double> &JxW = fe_fv.get_JxW_values();
931 * std::vector<double> g(n_q_points);
932 * exact_solution->value_list(q_points, g);
934 * std::vector<double> sol_u(n_q_points);
935 * fe_fv.get_function_values(solution, sol_u);
937 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
938 * const double penalty = get_penalty_factor(degree, extent1, extent1);
940 * double difference_norm_square = 0.;
941 * for (unsigned int point = 0; point < q_points.size(); ++point)
943 * const double diff = (g[point] - sol_u[point]);
944 * difference_norm_square += diff * diff * JxW[point];
946 * copy_data.value += penalty * difference_norm_square;
951 * And finally interior face terms @f$\sum_{f\in \partial K}\lbrace \sigma
952 * \left\| [u_h] \right\|_f^2 + h_f \left\| [\nu \nabla u_h \cdot
953 * \mathbf n ] \right\|_f^2 \rbrace@f$:
956 * const auto face_worker = [&](const auto & cell,
957 * const unsigned int &f,
958 * const unsigned int &sf,
959 * const auto & ncell,
960 * const unsigned int &nf,
961 * const unsigned int &nsf,
962 * auto & scratch_data,
963 * auto & copy_data) {
964 * const FEInterfaceValues<dim> &fe_iv =
965 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
967 * copy_data.face_data.emplace_back();
968 * CopyDataFace ©_data_face = copy_data.face_data.back();
970 * copy_data_face.cell_indices[0] = cell->active_cell_index();
971 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
973 * const std::vector<double> & JxW = fe_iv.get_JxW_values();
974 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
976 * const auto & q_points = fe_iv.get_quadrature_points();
977 * const unsigned int n_q_points = q_points.size();
979 * std::vector<double> jump(n_q_points);
980 * get_function_jump(fe_iv, solution, jump);
982 * std::vector<Tensor<1, dim>> grad_jump(n_q_points);
983 * get_function_gradient_jump(fe_iv, solution, grad_jump);
985 * const double h = cell->face(f)->diameter();
987 * const double extent1 = cell->measure() / cell->face(f)->measure();
988 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
989 * const double penalty = get_penalty_factor(degree, extent1, extent2);
991 * double flux_jump_square = 0;
992 * double u_jump_square = 0;
993 * for (unsigned int point = 0; point < n_q_points; ++point)
995 * u_jump_square += jump[point] * jump[point] * JxW[point];
996 * const double flux_jump = grad_jump[point] * normals[point];
997 * flux_jump_square +=
998 * diffusion_coefficient * flux_jump * flux_jump * JxW[point];
1000 * copy_data_face.values[0] =
1001 * 0.5 * h * (flux_jump_square + penalty * u_jump_square);
1002 * copy_data_face.values[1] = copy_data_face.values[0];
1007 * Having computed local contributions for each cell, we still
1008 * need a way to copy these into the global vector that will hold
1009 * the error estimators for all cells:
1012 * const auto copier = [&](const auto ©_data) {
1013 * if (copy_data.cell_index != numbers::invalid_unsigned_int)
1014 * estimated_error_square_per_cell[copy_data.cell_index] +=
1016 * for (auto &cdf : copy_data.face_data)
1017 * for (unsigned int j = 0; j < 2; ++j)
1018 * estimated_error_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1023 * After all of this set-up, let's
do the actual work: We resize
1024 * the vector into which the results will be written, and then
1029 * estimated_error_square_per_cell.reinit(
triangulation.n_active_cells());
1037 * ScratchData scratch_data(
1038 * mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
1040 * CopyData copy_data;
1042 * dof_handler.
end(),
1057 * <a name=
"Thecompute_energy_norm_errorfunction"></a>
1058 * <h3>The compute_energy_norm_error() function</h3>
1059 * Next, we evaluate the accuracy in terms of the energy
norm.
1060 * This function is similar to the assembling of the error estimator above.
1061 * Here we compute the square of the energy
norm defined by
1063 * \|u \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla u \|_K^2 +
1064 * \sum_{f \in F_i} \sigma \| [ u ] \|_f^2 +
1065 * \sum_{f \in F_b} \sigma \|u\|_f^2.
1067 * Therefore the corresponding error is
1069 * \|u -u_h \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2
1070 * + \sum_{f \in F_i} \sigma \|[ u_h ] \|_f^2 + \sum_{f \in F_b}\sigma
1075 *
template <
int dim>
1076 *
double SIPGLaplace<dim>::compute_energy_norm_error()
1078 * energy_norm_square_per_cell.reinit(
triangulation.n_active_cells());
1082 * Assemble @f$\sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2 @f$.
1085 *
const auto cell_worker =
1086 * [&](
const auto &cell,
auto &scratch_data,
auto ©_data) {
1089 * copy_data.cell_index = cell->active_cell_index();
1091 *
const auto & q_points = fe_v.get_quadrature_points();
1092 *
const unsigned int n_q_points = q_points.size();
1093 *
const std::vector<double> &JxW = fe_v.get_JxW_values();
1095 * std::vector<Tensor<1, dim>> grad_u(n_q_points);
1096 * fe_v.get_function_gradients(solution, grad_u);
1098 * std::vector<Tensor<1, dim>> grad_exact(n_q_points);
1099 * exact_solution->gradient_list(q_points, grad_exact);
1101 *
double norm_square = 0;
1107 * copy_data.value = diffusion_coefficient * norm_square;
1112 * Assemble @f$\sum_{f \in F_b}\sigma \|u_h-g_D\|_f^2@f$.
1115 *
const auto boundary_worker = [&](
const auto & cell,
1116 *
const unsigned int &face_no,
1117 *
auto & scratch_data,
1118 *
auto & copy_data) {
1122 *
const unsigned n_q_points = q_points.size();
1126 * std::vector<double> g(n_q_points);
1127 * exact_solution->value_list(q_points, g);
1129 * std::vector<double> sol_u(n_q_points);
1132 *
const double extent1 = cell->measure() / cell->face(face_no)->measure();
1133 *
const double penalty = get_penalty_factor(degree, extent1, extent1);
1135 *
double difference_norm_square = 0.;
1138 *
const double diff = (g[
point] - sol_u[
point]);
1139 * difference_norm_square += diff * diff * JxW[
point];
1141 * copy_data.value += penalty * difference_norm_square;
1146 * Assemble @f$\sum_{f \in F_i} \sigma \| [ u_h ] \|_f^2@f$.
1149 *
const auto face_worker = [&](
const auto & cell,
1150 *
const unsigned int &f,
1151 *
const unsigned int &sf,
1152 *
const auto & ncell,
1153 *
const unsigned int &nf,
1154 *
const unsigned int &nsf,
1155 *
auto & scratch_data,
1156 *
auto & copy_data) {
1158 * scratch_data.
reinit(cell, f, sf, ncell, nf, nsf);
1160 * copy_data.face_data.emplace_back();
1161 * CopyDataFace ©_data_face = copy_data.face_data.back();
1163 * copy_data_face.cell_indices[0] = cell->active_cell_index();
1164 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
1169 *
const unsigned int n_q_points = q_points.size();
1171 * std::vector<double> jump(n_q_points);
1172 * get_function_jump(fe_iv, solution, jump);
1174 *
const double extent1 = cell->measure() / cell->face(f)->measure();
1175 *
const double extent2 = ncell->measure() / ncell->face(nf)->measure();
1176 *
const double penalty = get_penalty_factor(degree, extent1, extent2);
1178 *
double u_jump_square = 0;
1183 * copy_data_face.values[0] = 0.5 * penalty * u_jump_square;
1184 * copy_data_face.values[1] = copy_data_face.values[0];
1187 *
const auto copier = [&](
const auto ©_data) {
1189 * energy_norm_square_per_cell[copy_data.cell_index] += copy_data.value;
1190 *
for (
auto &cdf : copy_data.face_data)
1191 *
for (
unsigned int j = 0; j < 2; ++j)
1192 * energy_norm_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1200 *
const ScratchData scratch_data(mapping,
1202 * quadrature_overintegration,
1204 * face_quadrature_overintegration,
1207 * CopyData copy_data;
1209 * dof_handler.
end(),
1219 *
const double energy_error =
1220 *
std::sqrt(energy_norm_square_per_cell.l1_norm());
1221 *
return energy_error;
1229 * <a name=
"Therefine_gridfunction"></a>
1230 * <h3>The refine_grid() function</h3>
1233 * template <
int dim>
1234 *
void SIPGLaplace<dim>::refine_grid()
1236 *
const double refinement_fraction = 0.1;
1239 *
triangulation, estimated_error_square_per_cell, refinement_fraction, 0.);
1249 * <a name=
"Thecompute_errorsfunction"></a>
1250 * <h3>The compute_errors() function</h3>
1251 * We compute three errors in the @f$L_2@f$
norm, @f$H_1@f$ seminorm, and
1252 * the energy
norm, respectively. These are then printed to screen,
1253 * but also stored in a table that records how these errors decay
1254 * with mesh refinement and which can be output in
one step at the
1255 *
end of the program.
1258 * template <
int dim>
1259 *
void SIPGLaplace<dim>::compute_errors()
1261 *
double L2_error, H1_error, energy_error;
1268 * *(exact_solution.get()),
1269 * difference_per_cell,
1270 * quadrature_overintegration,
1274 * difference_per_cell,
1276 * convergence_table.add_value(
"L2", L2_error);
1284 * *(exact_solution.get()),
1285 * difference_per_cell,
1286 * quadrature_overintegration,
1290 * difference_per_cell,
1292 * convergence_table.add_value(
"H1", H1_error);
1296 * energy_error = compute_energy_norm_error();
1297 * convergence_table.add_value(
"Energy", energy_error);
1300 * std::cout <<
" Error in the L2 norm : " << L2_error << std::endl
1301 * <<
" Error in the H1 seminorm : " << H1_error << std::endl
1302 * <<
" Error in the energy norm : " << energy_error
1311 * <a name=
"Therunfunction"></a>
1312 * <h3>The
run() function</h3>
1315 * template <
int dim>
1316 *
void SIPGLaplace<dim>::
run()
1318 *
const unsigned int max_cycle =
1319 * (test_case == TestCase::convergence_rate ? 6 : 20);
1320 *
for (
unsigned int cycle = 0; cycle < max_cycle; ++cycle)
1322 * std::cout <<
"Cycle " << cycle << std::endl;
1324 *
switch (test_case)
1326 *
case TestCase::convergence_rate:
1341 *
case TestCase::l_singularity:
1361 * std::cout <<
" Number of active cells : "
1365 * std::cout <<
" Number of degrees of freedom : " << dof_handler.
n_dofs()
1368 * assemble_system();
1370 * output_results(cycle);
1372 * convergence_table.add_value(
"cycle", cycle);
1373 * convergence_table.add_value(
"cells",
triangulation.n_active_cells());
1374 * convergence_table.add_value(
"dofs", dof_handler.
n_dofs());
1378 *
if (test_case == TestCase::l_singularity)
1380 * compute_error_estimate();
1381 * std::cout <<
" Estimated error : "
1382 * <<
std::sqrt(estimated_error_square_per_cell.l1_norm())
1385 * convergence_table.add_value(
1387 *
std::sqrt(estimated_error_square_per_cell.l1_norm()));
1389 * std::cout << std::endl;
1394 * Having
run all of our computations, let us tell the convergence
1395 * table how to format its data and output it to screen:
1398 * convergence_table.set_precision(
"L2", 3);
1399 * convergence_table.set_precision(
"H1", 3);
1400 * convergence_table.set_precision(
"Energy", 3);
1402 * convergence_table.set_scientific(
"L2",
true);
1403 * convergence_table.set_scientific(
"H1",
true);
1404 * convergence_table.set_scientific(
"Energy",
true);
1406 *
if (test_case == TestCase::convergence_rate)
1408 * convergence_table.evaluate_convergence_rates(
1410 * convergence_table.evaluate_convergence_rates(
1413 *
if (test_case == TestCase::l_singularity)
1415 * convergence_table.set_precision(
"Estimator", 3);
1416 * convergence_table.set_scientific(
"Estimator",
true);
1419 * std::cout <<
"degree = " << degree << std::endl;
1420 * convergence_table.write_text(
1421 * std::cout, TableHandler::TextOutputFormat::org_mode_table);
1430 * <a name=
"Themainfunction"></a>
1431 * <h3>The main() function</h3>
1432 * The following <code>main</code> function is similar to previous examples as
1433 * well, and need not be commented on.
1440 *
using namespace dealii;
1441 *
using namespace Step74;
1443 *
const TestCase test_case = TestCase::l_singularity;
1445 * SIPGLaplace<2> problem(test_case);
1448 *
catch (std::exception &exc)
1450 * std::cerr << std::endl
1452 * <<
"----------------------------------------------------"
1454 * std::cerr <<
"Exception on processing: " << std::endl
1455 * << exc.what() << std::endl
1456 * <<
"Aborting!" << std::endl
1457 * <<
"----------------------------------------------------"
1463 * std::cerr << std::endl
1465 * <<
"----------------------------------------------------"
1467 * std::cerr <<
"Unknown exception!" << std::endl
1468 * <<
"Aborting!" << std::endl
1469 * <<
"----------------------------------------------------"
1477<a name=
"Results"></a><h1>Results</h1>
1480The output of
this program consist of the console output and
1481solutions in
vtu format.
1483In the
first test
case, when you
run the program, the screen output should look like the following:
1486 Number of active cells : 16
1487 Number of degrees of freedom : 256
1488 Error in the
L2 norm : 0.00193285
1489 Error in the H1 seminorm : 0.106087
1490 Error in the energy
norm : 0.150625
1493 Number of active cells : 64
1494 Number of degrees of freedom : 1024
1495 Error in the
L2 norm : 9.60497e-05
1496 Error in the H1 seminorm : 0.0089954
1497 Error in the energy
norm : 0.0113265
1505When
using the smooth
case with polynomial degree 3, the convergence
1506table will look like
this:
1507<table align=
"center" class=
"doxtable">
1519 <td align=
"center">0</td>
1520 <td align=
"right">16</td>
1521 <td align=
"right">256</td>
1522 <td align=
"center">1.933e-03</td>
1524 <td align=
"center">1.061e-01</td>
1526 <td align=
"center">1.506e-01</td>
1529 <td align=
"center">1</td>
1530 <td align=
"right">64</td>
1531 <td align=
"right">1024</td>
1532 <td align=
"center">9.605e-05</td>
1533 <td align=
"center">4.33</td>
1534 <td align=
"center">8.995e-03</td>
1535 <td align=
"center">3.56</td>
1536 <td align=
"center">1.133e-02</td>
1539 <td align=
"center">2</td>
1540 <td align=
"right">256</td>
1541 <td align=
"right">4096</td>
1542 <td align=
"center">5.606e-06</td>
1543 <td align=
"center">4.10</td>
1544 <td align=
"center">9.018e-04</td>
1545 <td align=
"center">3.32</td>
1546 <td align=
"center">9.736e-04</td>
1549 <td align=
"center">3</td>
1550 <td align=
"right">1024</td>
1551 <td align=
"right">16384</td>
1552 <td align=
"center">3.484e-07</td>
1553 <td align=
"center">4.01</td>
1554 <td align=
"center">1.071e-04</td>
1555 <td align=
"center">3.07</td>
1556 <td align=
"center">1.088e-04</td>
1559 <td align=
"center">4</td>
1560 <td align=
"right">4096</td>
1561 <td align=
"right">65536</td>
1562 <td align=
"center">2.179e-08</td>
1563 <td align=
"center">4.00</td>
1564 <td align=
"center">1.327e-05</td>
1565 <td align=
"center">3.01</td>
1566 <td align=
"center">1.331e-05</td>
1569 <td align=
"center">5</td>
1570 <td align=
"right">16384</td>
1571 <td align=
"right">262144</td>
1572 <td align=
"center">1.363e-09</td>
1573 <td align=
"center">4.00</td>
1574 <td align=
"center">1.656e-06</td>
1575 <td align=
"center">3.00</td>
1576 <td align=
"center">1.657e-06</td>
1580Theoretically,
for polynomial degree @f$p@f$, the order of convergence in @f$L_2@f$
1581norm and @f$H^1@f$ seminorm should be @f$p+1@f$ and @f$p@f$, respectively. Our numerical
1582results are in good agreement with theory.
1584In the
second test
case, when you
run the program, the screen output should look like the following:
1587 Number of active cells : 192
1588 Number of degrees of freedom : 3072
1589 Error in the
L2 norm : 0.000323585
1590 Error in the H1 seminorm : 0.0296202
1591 Error in the energy
norm : 0.0420478
1592 Estimated error : 0.136067
1595 Number of active cells : 249
1596 Number of degrees of freedom : 3984
1597 Error in the
L2 norm : 0.000114739
1598 Error in the H1 seminorm : 0.0186571
1599 Error in the energy
norm : 0.0264879
1600 Estimated error : 0.0857186
1608The following figure provides a
log-
log plot of the errors versus
1609the number of degrees of freedom
for this test
case on the
L-shaped
1610domain. In order to interpret it, let @f$n@f$ be the number of degrees of
1611freedom, then on uniformly refined meshes, @f$h@f$ is of order
1612@f$1/
\sqrt{n}@f$ in 2D. Combining the theoretical results in the previous
case,
1613we see that
if the solution is sufficiently smooth,
1614we can expect the error in the @f$L_2@f$
norm to be of order @f$O(n^{-\frac{p+1}{2}})@f$
1615and in @f$H^1@f$ seminorm to be @f$O(n^{-\frac{p}{2}})@f$. It is not a priori
1616clear that
one would get the same kind of behavior as a function of
1617@f$n@f$ on adaptively refined meshes like the ones we use
for this second
1618test
case, but
one can certainly hope. Indeed, from the figure, we see
1619that the SIPG with adaptive mesh refinement produces asymptotically
1620the kinds of hoped-
for results:
1622<img width=
"600px" src=
"https://www.dealii.org/images/steps/developer/step-74.log-log-plot.png" alt=
"">
1624In addition, we observe that the error estimator decreases
1625at almost the same rate as the errors in the energy
norm and @f$H^1@f$ seminorm,
1626and
one order lower than the @f$L_2@f$ error. This suggests
1627its ability to predict regions with large errors.
1629While
this tutorial is focused on the implementation, the @ref step_59
"step-59" tutorial program achieves an efficient
1630large-
scale solver in terms of computing time with
matrix-
free solution techniques.
1631Note that the @ref step_59
"step-59" tutorial does not work with meshes containing hanging nodes at
this moment,
1632because the multigrid interface matrices are not as easily determined,
1633but that is merely the lack of some interfaces in deal.II,
nothing fundamental.
1636<a name=
"PlainProg"></a>
1637<h1> The plain program</h1>
1638@include
"step-74.cc"
cell_iterator end() const
active_cell_iterator begin_active(const unsigned int level=0) const
types::global_dof_index n_dofs() const
const std::vector< double > & get_JxW_values() const
const std::vector< Point< spacedim > > & get_quadrature_points() const
void reinit(const CellIteratorType &cell, const unsigned int face_no, const unsigned int sub_face_no, const typename identity< CellIteratorType >::type &cell_neighbor, const unsigned int face_no_neighbor, const unsigned int sub_face_no_neighbor)
const std::vector< double > & get_JxW_values() const
const std::vector< Point< spacedim > > & get_quadrature_points() const
void get_function_values(const InputVector &fe_function, std::vector< typename InputVector::value_type > &values) const
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< RangeNumberType > &values, const unsigned int component=0) const
VectorizedArray< Number, width > log(const ::VectorizedArray< Number, width > &x)
VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &x)
@ 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.
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
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)
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
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.
static const types::blas_int one
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)
VectorType::value_type * end(VectorType &V)
void run(const Iterator &begin, const typename identity< Iterator >::type &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)
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 > 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