279 *
for (
unsigned int i = 0; i <
values.size(); ++i)
288 *
SmoothSolution<dim>::gradient(
const Point<dim> &point,
289 *
const unsigned int )
const
297 *
return return_value;
304 * The corresponding right-hand side of the smooth function:
308 *
class SmoothRightHandSide :
public Function<dim>
311 *
SmoothRightHandSide()
316 *
std::vector<double> &values,
317 *
const unsigned int )
const override;
324 *
SmoothRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
325 *
std::vector<double> &values,
326 *
const unsigned int )
const
329 *
for (
unsigned int i = 0; i <
values.size(); ++i)
330 *
values[i] = 8. * PI * PI *
std::sin(2. * PI * points[i][0]) *
338 * The right-hand side that corresponds to the function
340 * assume that the diffusion coefficient @f$\nu = 1@f$:
344 *
class SingularRightHandSide :
public Function<dim>
347 *
SingularRightHandSide()
352 *
std::vector<double> &values,
353 *
const unsigned int )
const override;
363 *
SingularRightHandSide<dim>::value_list(
const std::vector<
Point<dim>> &points,
364 *
std::vector<double> &values,
365 *
const unsigned int )
const
367 *
for (
unsigned int i = 0; i <
values.size(); ++i)
376 * <a name=
"step_74-Auxiliaryfunctions"></a>
378 * This function computes the penalty @f$\sigma@f$.
381 *
double get_penalty_factor(
const unsigned int fe_degree,
382 *
const double cell_extent_left,
383 *
const double cell_extent_right)
385 *
const unsigned int degree =
std::max(1U, fe_degree);
386 *
return degree * (degree + 1.) * 0.5 *
387 *
(1. / cell_extent_left + 1. / cell_extent_right);
394 * <a name=
"step_74-TheCopyData"></a>
395 * <h3>The CopyData</h3>
397 * which is essentially the same as @ref step_12
"step-12". Note that the
398 *
"Scratch" object is not defined here because we use
400 * objects is extensively explained in the
WorkStream namespace documentation.
403 *
struct CopyDataFace
406 *
std::vector<types::global_dof_index> joint_dof_indices;
407 *
std::array<double, 2>
values;
408 *
std::array<unsigned int, 2> cell_indices;
417 *
std::vector<types::global_dof_index> local_dof_indices;
418 *
std::vector<CopyDataFace> face_data;
423 *
template <
class Iterator>
424 *
void reinit(
const Iterator &cell,
const unsigned int dofs_per_cell)
426 *
cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
427 *
cell_rhs.reinit(dofs_per_cell);
428 *
local_dof_indices.resize(dofs_per_cell);
429 *
cell->get_dof_indices(local_dof_indices);
438 * <a name=
"step_74-TheSIPGLaplaceclass"></a>
439 * <h3>The SIPGLaplace
class</h3>
440 * After these preparations, we proceed with the
main class of this program,
441 * called `SIPGLaplace`. The overall structure of the
class is as in many
442 * of the other tutorial programs. Major differences will only come up in the
451 *
SIPGLaplace(
const TestCase &test_case);
455 *
void setup_system();
456 *
void assemble_system();
458 *
void refine_grid();
459 *
void output_results(
const unsigned int cycle)
const;
461 *
void compute_errors();
462 *
void compute_error_estimate();
463 *
double compute_energy_norm_error();
466 *
const unsigned int degree;
468 *
const QGauss<dim - 1> face_quadrature;
470 *
const QGauss<dim - 1> face_quadrature_overintegration;
485 * The remainder of the
class's members are used for the following:
486 * - Vectors to store error estimator square and energy norm square per
488 * - Print convergence rate and errors on the screen.
489 * - The fiffusion coefficient @f$\nu@f$ is set to 1.
490 * - Members that store information about the test case to be computed.
493 * Vector<double> estimated_error_square_per_cell;
494 * Vector<double> energy_norm_square_per_cell;
496 * ConvergenceTable convergence_table;
498 * const double diffusion_coefficient = 1.;
500 * const TestCase test_case;
501 * std::unique_ptr<const Function<dim>> exact_solution;
502 * std::unique_ptr<const Function<dim>> rhs_function;
507 * The constructor here takes the test case as input and then
508 * determines the correct solution and right-hand side classes. The
509 * remaining member variables are initialized in the obvious way.
513 * SIPGLaplace<dim>::SIPGLaplace(const TestCase &test_case)
515 * , quadrature(degree + 1)
516 * , face_quadrature(degree + 1)
517 * , quadrature_overintegration(degree + 2)
518 * , face_quadrature_overintegration(degree + 2)
521 * , dof_handler(triangulation)
522 * , test_case(test_case)
524 * if (test_case == TestCase::convergence_rate)
526 * exact_solution = std::make_unique<const SmoothSolution<dim>>();
527 * rhs_function = std::make_unique<const SmoothRightHandSide<dim>>();
530 * else if (test_case == TestCase::l_singularity)
533 * std::make_unique<const Functions::LSingularityFunction>();
534 * rhs_function = std::make_unique<const SingularRightHandSide<dim>>();
537 * AssertThrow(false, ExcNotImplemented());
543 * void SIPGLaplace<dim>::setup_system()
545 * dof_handler.distribute_dofs(fe);
546 * DynamicSparsityPattern dsp(dof_handler.n_dofs());
547 * DoFTools::make_flux_sparsity_pattern(dof_handler, dsp);
548 * sparsity_pattern.copy_from(dsp);
550 * system_matrix.reinit(sparsity_pattern);
551 * solution.reinit(dof_handler.n_dofs());
552 * system_rhs.reinit(dof_handler.n_dofs());
560 * <a name="step_74-Theassemble_systemfunction"></a>
561 * <h3>The assemble_system function</h3>
562 * The assemble function here is similar to that in @ref step_12 "step-12" and @ref step_47 "step-47".
563 * Different from assembling by hand, we just need to focus
564 * on assembling on each cell, each boundary face, and each
565 * interior face. The loops over cells and faces are handled
566 * automatically by MeshWorker::mesh_loop().
570 * The function starts by defining a local (lambda) function that is
571 * used to integrate the cell terms:
575 * void SIPGLaplace<dim>::assemble_system()
577 * const auto cell_worker =
578 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
579 * ScratchData &scratch_data,
580 * CopyData ©_data) {
581 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
582 * const unsigned int dofs_per_cell = fe_v.dofs_per_cell;
583 * copy_data.reinit(cell, dofs_per_cell);
585 * const std::vector<Point<dim>> &q_points =
586 * scratch_data.get_quadrature_points();
587 * const unsigned int n_q_points = q_points.size();
588 * const std::vector<double> &JxW = scratch_data.get_JxW_values();
590 * std::vector<double> rhs(n_q_points);
591 * rhs_function->value_list(q_points, rhs);
593 * for (unsigned int point = 0; point < n_q_points; ++point)
594 * for (unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
596 * for (unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
597 * copy_data.cell_matrix(i, j) +=
598 * diffusion_coefficient * // nu
599 * fe_v.shape_grad(i, point) * // grad v_h
600 * fe_v.shape_grad(j, point) * // grad u_h
603 * copy_data.cell_rhs(i) += fe_v.shape_value(i, point) * // v_h
611 * Next, we need a function that assembles face integrals on the boundary:
614 * const auto boundary_worker =
615 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
616 * const unsigned int &face_no,
617 * ScratchData &scratch_data,
618 * CopyData ©_data) {
619 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
621 * const std::vector<Point<dim>> &q_points =
622 * scratch_data.get_quadrature_points();
623 * const unsigned int n_q_points = q_points.size();
624 * const unsigned int dofs_per_cell = fe_fv.dofs_per_cell;
626 * const std::vector<double> &JxW = scratch_data.get_JxW_values();
627 * const std::vector<Tensor<1, dim>> &normals =
628 * scratch_data.get_normal_vectors();
630 * std::vector<double> g(n_q_points);
631 * exact_solution->value_list(q_points, g);
633 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
634 * const double penalty = get_penalty_factor(degree, extent1, extent1);
636 * for (unsigned int point = 0; point < n_q_points; ++point)
638 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
639 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
640 * copy_data.cell_matrix(i, j) +=
641 * (-diffusion_coefficient * // - nu
642 * fe_fv.shape_value(i, point) * // v_h
643 * (fe_fv.shape_grad(j, point) * // (grad u_h .
644 * normals[point]) // n)
646 * - diffusion_coefficient * // - nu
647 * (fe_fv.shape_grad(i, point) * // (grad v_h .
648 * normals[point]) * // n)
649 * fe_fv.shape_value(j, point) // u_h
651 * + diffusion_coefficient * penalty * // + nu sigma
652 * fe_fv.shape_value(i, point) * // v_h
653 * fe_fv.shape_value(j, point) // u_h
658 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
659 * copy_data.cell_rhs(i) +=
660 * (-diffusion_coefficient * // - nu
661 * (fe_fv.shape_grad(i, point) * // (grad v_h .
662 * normals[point]) * // n)
666 * + diffusion_coefficient * penalty * // + nu sigma
667 * fe_fv.shape_value(i, point) * g[point] // v_h g
676 * Finally, a function that assembles face integrals on interior
677 * faces. To reinitialize FEInterfaceValues, we need to pass
678 * cells, face and subface indices (for adaptive refinement) to
679 * the reinit() function of FEInterfaceValues:
682 * const auto face_worker =
683 * [&](const typename DoFHandler<dim>::cell_iterator &cell,
684 * const unsigned int &f,
685 * const unsigned int &sf,
686 * const typename DoFHandler<dim>::cell_iterator &ncell,
687 * const unsigned int &nf,
688 * const unsigned int &nsf,
689 * ScratchData &scratch_data,
690 * CopyData ©_data) {
691 * const FEInterfaceValues<dim> &fe_iv =
692 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
694 * copy_data.face_data.emplace_back();
695 * CopyDataFace ©_data_face = copy_data.face_data.back();
696 * const unsigned int n_dofs_face = fe_iv.n_current_interface_dofs();
697 * copy_data_face.joint_dof_indices = fe_iv.get_interface_dof_indices();
698 * copy_data_face.cell_matrix.reinit(n_dofs_face, n_dofs_face);
700 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
701 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
703 * const double extent1 = cell->measure() / cell->face(f)->measure();
704 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
705 * const double penalty = get_penalty_factor(degree, extent1, extent2);
707 * for (const unsigned int point : fe_iv.quadrature_point_indices())
709 * for (const unsigned int i : fe_iv.dof_indices())
710 * for (const unsigned int j : fe_iv.dof_indices())
711 * copy_data_face.cell_matrix(i, j) +=
712 * (-diffusion_coefficient * // - nu
713 * fe_iv.jump_in_shape_values(i, point) * // [v_h]
714 * (fe_iv.average_of_shape_gradients(j,
715 * point) * // ({grad u_h} .
716 * normals[point]) // n)
718 * - diffusion_coefficient * // - nu
719 * (fe_iv.average_of_shape_gradients(i,
720 * point) * // (grad v_h .
721 * normals[point]) * // n)
722 * fe_iv.jump_in_shape_values(j, point) // [u_h]
724 * + diffusion_coefficient * penalty * // + nu sigma
725 * fe_iv.jump_in_shape_values(i, point) * // [v_h]
726 * fe_iv.jump_in_shape_values(j, point) // [u_h]
735 * The following lambda function will then copy data into the
736 * global matrix and right-hand side. Though there are no hanging
737 * node constraints in DG discretization, we define an empty
738 * AffineConstraints object that allows us to use the
739 * AffineConstraints::distribute_local_to_global() functionality.
742 * AffineConstraints<double> constraints;
743 * constraints.close();
744 * const auto copier = [&](const CopyData &c) {
745 * constraints.distribute_local_to_global(c.cell_matrix,
747 * c.local_dof_indices,
753 * Copy data from interior face assembly to the global matrix.
756 * for (const CopyDataFace &cdf : c.face_data)
758 * constraints.distribute_local_to_global(cdf.cell_matrix,
759 * cdf.joint_dof_indices,
767 * With the assembly functions defined, we can now create
768 * ScratchData and CopyData objects, and pass them together with
769 * the lambda functions above to MeshWorker::mesh_loop(). In
770 * addition, we need to specify that we want to assemble on
771 * interior faces exactly once.
774 * const UpdateFlags cell_flags = update_values | update_gradients |
775 * update_quadrature_points | update_JxW_values;
776 * const UpdateFlags face_flags = update_values | update_gradients |
777 * update_quadrature_points |
778 * update_normal_vectors | update_JxW_values;
780 * ScratchData scratch_data(
781 * mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
782 * CopyData copy_data;
784 * MeshWorker::mesh_loop(dof_handler.begin_active(),
790 * MeshWorker::assemble_own_cells |
791 * MeshWorker::assemble_boundary_faces |
792 * MeshWorker::assemble_own_interior_faces_once,
802 * <a name="step_74-Thesolveandoutput_resultsfunction"></a>
803 * <h3>The solve() and output_results() function</h3>
804 * The following two functions are entirely standard and without difficulty.
808 * void SIPGLaplace<dim>::solve()
810 * SparseDirectUMFPACK A_direct;
811 * A_direct.initialize(system_matrix);
812 * A_direct.vmult(solution, system_rhs);
818 * void SIPGLaplace<dim>::output_results(const unsigned int cycle) const
820 * const std::string filename = "sol_Q" + Utilities::int_to_string(degree, 1) +
821 * "-" + Utilities::int_to_string(cycle, 2) +
823 * std::ofstream output(filename);
825 * DataOut<dim> data_out;
826 * data_out.attach_dof_handler(dof_handler);
827 * data_out.add_data_vector(solution, "u", DataOut<dim>::type_dof_data);
828 * data_out.build_patches(mapping);
829 * data_out.write_vtu(output);
836 * <a name="step_74-Thecompute_error_estimatefunction"></a>
837 * <h3>The compute_error_estimate() function</h3>
838 * The assembly of the error estimator here is quite similar to
839 * that of the global matrix and right-had side and can be handled
840 * by the MeshWorker::mesh_loop() framework. To understand what
841 * each of the local (lambda) functions is doing, recall first that
842 * the local cell residual is defined as
843 * @f$h_K^2 \left\| f + \nu \Delta u_h \right\|_K^2@f$:
847 * void SIPGLaplace<dim>::compute_error_estimate()
849 * const auto cell_worker =
850 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
851 * ScratchData &scratch_data,
852 * CopyData ©_data) {
853 * const FEValues<dim> &fe_v = scratch_data.reinit(cell);
855 * copy_data.cell_index = cell->active_cell_index();
857 * const std::vector<Point<dim>> &q_points = fe_v.get_quadrature_points();
858 * const unsigned int n_q_points = q_points.size();
859 * const std::vector<double> &JxW = fe_v.get_JxW_values();
861 * std::vector<Tensor<2, dim>> hessians(n_q_points);
862 * fe_v.get_function_hessians(solution, hessians);
864 * std::vector<double> rhs(n_q_points);
865 * rhs_function->value_list(q_points, rhs);
867 * const double hk = cell->diameter();
868 * double residual_norm_square = 0;
870 * for (unsigned int point = 0; point < n_q_points; ++point)
872 * const double residual =
873 * rhs[point] + diffusion_coefficient * trace(hessians[point]);
874 * residual_norm_square += residual * residual * JxW[point];
876 * copy_data.value = hk * hk * residual_norm_square;
881 * Next compute boundary terms @f$\sum_{f\in \partial K \cap \partial \Omega}
882 * \sigma \left\| [ u_h-g_D ] \right\|_f^2 @f$:
885 * const auto boundary_worker =
886 * [&](const typename DoFHandler<dim>::active_cell_iterator &cell,
887 * const unsigned int &face_no,
888 * ScratchData &scratch_data,
889 * CopyData ©_data) {
890 * const FEFaceValuesBase<dim> &fe_fv = scratch_data.reinit(cell, face_no);
892 * const std::vector<Point<dim>> &q_points = fe_fv.get_quadrature_points();
893 * const unsigned n_q_points = q_points.size();
895 * const std::vector<double> &JxW = fe_fv.get_JxW_values();
897 * std::vector<double> g(n_q_points);
898 * exact_solution->value_list(q_points, g);
900 * std::vector<double> sol_u(n_q_points);
901 * fe_fv.get_function_values(solution, sol_u);
903 * const double extent1 = cell->measure() / cell->face(face_no)->measure();
904 * const double penalty = get_penalty_factor(degree, extent1, extent1);
906 * double difference_norm_square = 0.;
907 * for (unsigned int point = 0; point < q_points.size(); ++point)
909 * const double diff = (g[point] - sol_u[point]);
910 * difference_norm_square += diff * diff * JxW[point];
912 * copy_data.value += penalty * difference_norm_square;
917 * And finally interior face terms @f$\sum_{f\in \partial K}\lbrace \sigma
918 * \left\| [u_h] \right\|_f^2 + h_f \left\| [\nu \nabla u_h \cdot
919 * \mathbf n ] \right\|_f^2 \rbrace@f$:
922 * const auto face_worker =
923 * [&](const typename DoFHandler<dim>::cell_iterator &cell,
924 * const unsigned int &f,
925 * const unsigned int &sf,
926 * const typename DoFHandler<dim>::cell_iterator &ncell,
927 * const unsigned int &nf,
928 * const unsigned int &nsf,
929 * ScratchData &scratch_data,
930 * CopyData ©_data) {
931 * const FEInterfaceValues<dim> &fe_iv =
932 * scratch_data.reinit(cell, f, sf, ncell, nf, nsf);
934 * copy_data.face_data.emplace_back();
935 * CopyDataFace ©_data_face = copy_data.face_data.back();
937 * copy_data_face.cell_indices[0] = cell->active_cell_index();
938 * copy_data_face.cell_indices[1] = ncell->active_cell_index();
940 * const std::vector<double> &JxW = fe_iv.get_JxW_values();
941 * const std::vector<Tensor<1, dim>> &normals = fe_iv.get_normal_vectors();
943 * const std::vector<Point<dim>> &q_points = fe_iv.get_quadrature_points();
944 * const unsigned int n_q_points = q_points.size();
946 * std::vector<double> jump(n_q_points);
947 * fe_iv.get_jump_in_function_values(solution, jump);
949 * std::vector<Tensor<1, dim>> grad_jump(n_q_points);
950 * fe_iv.get_jump_in_function_gradients(solution, grad_jump);
952 * const double h = cell->face(f)->diameter();
954 * const double extent1 = cell->measure() / cell->face(f)->measure();
955 * const double extent2 = ncell->measure() / ncell->face(nf)->measure();
956 * const double penalty = get_penalty_factor(degree, extent1, extent2);
958 * double flux_jump_square = 0;
959 * double u_jump_square = 0;
960 * for (unsigned int point = 0; point < n_q_points; ++point)
962 * u_jump_square += jump[point] * jump[point] * JxW[point];
963 * const double flux_jump = grad_jump[point] * normals[point];
964 * flux_jump_square +=
965 * diffusion_coefficient * flux_jump * flux_jump * JxW[point];
967 * copy_data_face.values[0] =
968 * 0.5 * h * (flux_jump_square + penalty * u_jump_square);
969 * copy_data_face.values[1] = copy_data_face.values[0];
974 * Having computed local contributions for each cell, we still
975 * need a way to copy these into the global vector that will hold
976 * the error estimators for all cells:
979 * const auto copier = [&](const CopyData ©_data) {
980 * if (copy_data.cell_index != numbers::invalid_unsigned_int)
981 * estimated_error_square_per_cell[copy_data.cell_index] +=
983 * for (const CopyDataFace &cdf : copy_data.face_data)
984 * for (unsigned int j = 0; j < 2; ++j)
985 * estimated_error_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
990 * After all of this set-up, let's
do the actual work: We resize
991 * the vector into which the results will be written, and then
996 *
estimated_error_square_per_cell.
reinit(triangulation.n_active_cells());
1004 *
ScratchData scratch_data(
1005 *
mapping, fe, quadrature, cell_flags, face_quadrature, face_flags);
1007 *
CopyData copy_data;
1009 *
dof_handler.end(),
1024 * <a name=
"step_74-Thecompute_energy_norm_errorfunction"></a>
1025 * <h3>The compute_energy_norm_error() function</h3>
1026 * Next, we evaluate the accuracy in terms of the energy norm.
1027 * This function is similar to the assembling of the error estimator above.
1028 * Here we compute the square of the energy norm defined by
1030 * \|u \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla u \|_K^2 +
1031 * \sum_{f \in F_i} \sigma \| [ u ] \|_f^2 +
1032 * \sum_{f \in F_b} \sigma \|u\|_f^2.
1034 * Therefore the corresponding error is
1036 * \|u -u_h \|_{1,h}^2 = \sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2
1037 * + \sum_{f \in F_i} \sigma \|[ u_h ] \|_f^2 + \sum_{f \in F_b}\sigma
1042 *
template <
int dim>
1043 *
double SIPGLaplace<dim>::compute_energy_norm_error()
1045 *
energy_norm_square_per_cell.reinit(triangulation.n_active_cells());
1049 * Assemble @f$\sum_{K \in \Gamma_h} \nu\|\nabla (u_h - u) \|_K^2 @f$.
1052 *
const auto cell_worker =
1054 *
ScratchData &scratch_data,
1055 *
CopyData ©_data) {
1058 *
copy_data.cell_index = cell->active_cell_index();
1060 *
const std::vector<Point<dim>> &q_points = fe_v.get_quadrature_points();
1061 *
const unsigned int n_q_points = q_points.size();
1062 *
const std::vector<double> &JxW = fe_v.get_JxW_values();
1064 *
std::vector<Tensor<1, dim>> grad_u(n_q_points);
1065 *
fe_v.get_function_gradients(solution, grad_u);
1067 *
std::vector<Tensor<1, dim>> grad_exact(n_q_points);
1068 *
exact_solution->gradient_list(q_points, grad_exact);
1070 *
double norm_square = 0;
1071 *
for (
unsigned int point = 0;
point < n_q_points; ++
point)
1074 *
(grad_u[point] - grad_exact[point]).norm_square() * JxW[
point];
1076 *
copy_data.value = diffusion_coefficient * norm_square;
1081 * Assemble @f$\sum_{f \in F_b}\sigma \|u_h-g_D\|_f^2@f$.
1084 *
const auto boundary_worker =
1086 *
const unsigned int &face_no,
1087 *
ScratchData &scratch_data,
1088 *
CopyData ©_data) {
1092 *
const unsigned n_q_points = q_points.size();
1094 *
const std::vector<double> &JxW = fe_fv.get_JxW_values();
1096 *
std::vector<double> g(n_q_points);
1097 *
exact_solution->value_list(q_points, g);
1099 *
std::vector<double> sol_u(n_q_points);
1100 *
fe_fv.get_function_values(solution, sol_u);
1102 *
const double extent1 = cell->measure() / cell->face(face_no)->measure();
1103 *
const double penalty = get_penalty_factor(degree, extent1, extent1);
1105 *
double difference_norm_square = 0.;
1106 *
for (
unsigned int point = 0;
point < q_points.size(); ++
point)
1108 *
const double diff = (g[
point] - sol_u[
point]);
1109 *
difference_norm_square += diff * diff * JxW[
point];
1111 *
copy_data.value += penalty * difference_norm_square;
1116 * Assemble @f$\sum_{f \in F_i} \sigma \| [ u_h ] \|_f^2@f$.
1119 *
const auto face_worker =
1121 *
const unsigned int &f,
1122 *
const unsigned int &sf,
1124 *
const unsigned int &nf,
1125 *
const unsigned int &nsf,
1126 *
ScratchData &scratch_data,
1127 *
CopyData ©_data) {
1129 *
scratch_data.
reinit(cell, f, sf, ncell, nf, nsf);
1131 *
copy_data.face_data.emplace_back();
1132 *
CopyDataFace ©_data_face = copy_data.face_data.back();
1134 *
copy_data_face.cell_indices[0] = cell->active_cell_index();
1135 *
copy_data_face.cell_indices[1] = ncell->active_cell_index();
1137 *
const std::vector<double> &JxW = fe_iv.get_JxW_values();
1139 *
const std::vector<Point<dim>> &q_points = fe_iv.get_quadrature_points();
1140 *
const unsigned int n_q_points = q_points.size();
1142 *
std::vector<double> jump(n_q_points);
1143 *
fe_iv.get_jump_in_function_values(solution, jump);
1145 *
const double extent1 = cell->measure() / cell->face(f)->measure();
1146 *
const double extent2 = ncell->measure() / ncell->face(nf)->measure();
1147 *
const double penalty = get_penalty_factor(degree, extent1, extent2);
1149 *
double u_jump_square = 0;
1150 *
for (
unsigned int point = 0;
point < n_q_points; ++
point)
1154 *
copy_data_face.values[0] = 0.5 * penalty * u_jump_square;
1155 *
copy_data_face.values[1] = copy_data_face.values[0];
1158 *
const auto copier = [&](
const CopyData ©_data) {
1160 *
energy_norm_square_per_cell[copy_data.cell_index] += copy_data.value;
1161 *
for (
const CopyDataFace &cdf : copy_data.face_data)
1162 *
for (unsigned
int j = 0; j < 2; ++j)
1163 *
energy_norm_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1171 *
const ScratchData scratch_data(mapping,
1173 *
quadrature_overintegration,
1175 *
face_quadrature_overintegration,
1178 *
CopyData copy_data;
1180 *
dof_handler.end(),
1190 *
const double energy_error =
1191 *
std::sqrt(energy_norm_square_per_cell.l1_norm());
1192 *
return energy_error;
1200 * <a name=
"step_74-Therefine_gridfunction"></a>
1201 * <h3>The refine_grid() function</h3>
1204 *
template <
int dim>
1205 *
void SIPGLaplace<dim>::refine_grid()
1207 *
const double refinement_fraction = 0.1;
1210 *
triangulation, estimated_error_square_per_cell, refinement_fraction, 0.);
1212 *
triangulation.execute_coarsening_and_refinement();
1220 * <a name=
"step_74-Thecompute_errorsfunction"></a>
1221 * <h3>The compute_errors() function</h3>
1222 * We compute three errors in the @f$L_2@f$ norm, @f$H_1@f$ seminorm, and
1223 * the energy norm, respectively. These are then printed to screen,
1224 * but also stored in a table that records how these errors decay
1225 * with mesh refinement and which can be output in one step at the
1226 *
end of the program.
1229 *
template <
int dim>
1230 *
void SIPGLaplace<dim>::compute_errors()
1232 *
double L2_error, H1_error, energy_error;
1235 *
Vector<float> difference_per_cell(triangulation.n_active_cells());
1239 *
*(exact_solution.get()),
1240 *
difference_per_cell,
1241 *
quadrature_overintegration,
1245 *
difference_per_cell,
1247 *
convergence_table.add_value(
"L2", L2_error);
1251 *
Vector<float> difference_per_cell(triangulation.n_active_cells());
1255 *
*(exact_solution.get()),
1256 *
difference_per_cell,
1257 *
quadrature_overintegration,
1261 *
difference_per_cell,
1263 *
convergence_table.add_value(
"H1", H1_error);
1267 *
energy_error = compute_energy_norm_error();
1268 *
convergence_table.add_value(
"Energy", energy_error);
1271 *
std::cout <<
" Error in the L2 norm : " << L2_error << std::endl
1272 *
<<
" Error in the H1 seminorm : " << H1_error << std::endl
1273 *
<<
" Error in the energy norm : " << energy_error
1282 * <a name=
"step_74-Therunfunction"></a>
1283 * <h3>The
run() function</h3>
1286 *
template <
int dim>
1287 *
void SIPGLaplace<dim>::run()
1289 *
const unsigned int max_cycle =
1290 *
(test_case == TestCase::convergence_rate ? 6 : 20);
1291 *
for (
unsigned int cycle = 0; cycle < max_cycle; ++cycle)
1293 *
std::cout <<
"Cycle " << cycle << std::endl;
1295 *
switch (test_case)
1297 *
case TestCase::convergence_rate:
1303 *
triangulation.refine_global(2);
1307 *
triangulation.refine_global(1);
1312 *
case TestCase::l_singularity:
1317 *
triangulation.refine_global(3);
1332 *
std::cout <<
" Number of active cells : "
1333 *
<< triangulation.n_active_cells() << std::endl;
1336 *
std::cout <<
" Number of degrees of freedom : " << dof_handler.n_dofs()
1339 *
assemble_system();
1341 *
output_results(cycle);
1343 *
convergence_table.add_value(
"cycle", cycle);
1344 *
convergence_table.add_value(
"cells", triangulation.n_active_cells());
1345 *
convergence_table.add_value(
"dofs", dof_handler.n_dofs());
1349 *
if (test_case == TestCase::l_singularity)
1351 *
compute_error_estimate();
1352 *
std::cout <<
" Estimated error : "
1353 *
<<
std::sqrt(estimated_error_square_per_cell.l1_norm())
1356 *
convergence_table.add_value(
1358 *
std::sqrt(estimated_error_square_per_cell.l1_norm()));
1360 *
std::cout << std::endl;
1365 * Having
run all of our computations, let us tell the convergence
1366 * table how to format its
data and output it to screen:
1369 *
convergence_table.set_precision(
"L2", 3);
1370 *
convergence_table.set_precision(
"H1", 3);
1371 *
convergence_table.set_precision(
"Energy", 3);
1373 *
convergence_table.set_scientific(
"L2",
true);
1374 *
convergence_table.set_scientific(
"H1",
true);
1375 *
convergence_table.set_scientific(
"Energy",
true);
1377 *
if (test_case == TestCase::convergence_rate)
1379 *
convergence_table.evaluate_convergence_rates(
1381 *
convergence_table.evaluate_convergence_rates(
1384 *
if (test_case == TestCase::l_singularity)
1386 *
convergence_table.set_precision(
"Estimator", 3);
1387 *
convergence_table.set_scientific(
"Estimator",
true);
1390 *
std::cout <<
"degree = " << degree << std::endl;
1391 *
convergence_table.write_text(
1401 * <a name=
"step_74-Themainfunction"></a>
1402 * <h3>The
main() function</h3>
1403 * The following <code>
main</code> function is similar to previous examples as
1404 * well, and need not be commented on.
1411 *
using namespace dealii;
1412 *
using namespace Step74;
1414 *
const TestCase test_case = TestCase::l_singularity;
1416 *
SIPGLaplace<2> problem(test_case);
1419 *
catch (std::exception &exc)
1421 *
std::cerr << std::endl
1423 *
<<
"----------------------------------------------------"
1425 *
std::cerr <<
"Exception on processing: " << std::endl
1426 *
<< exc.what() << std::endl
1427 *
<<
"Aborting!" << std::endl
1428 *
<<
"----------------------------------------------------"
1434 *
std::cerr << std::endl
1436 *
<<
"----------------------------------------------------"
1438 *
std::cerr <<
"Unknown exception!" << std::endl
1439 *
<<
"Aborting!" << std::endl
1440 *
<<
"----------------------------------------------------"
1448<a name=
"step_74-Results"></a><h1>Results</h1>
1451The output of
this program consist of the console output and
1452solutions in
vtu format.
1454In the
first test
case, when you
run the program, the screen output should look like the following:
1457 Number of active cells : 16
1458 Number of degrees of freedom : 256
1459 Error in the
L2 norm : 0.00193285
1460 Error in the H1 seminorm : 0.106087
1461 Error in the energy
norm : 0.150625
1464 Number of active cells : 64
1465 Number of degrees of freedom : 1024
1466 Error in the
L2 norm : 9.60497e-05
1467 Error in the H1 seminorm : 0.0089954
1468 Error in the energy
norm : 0.0113265
1476When
using the smooth
case with polynomial degree 3, the convergence
1477table will look like
this:
1478<table align=
"center" class=
"doxtable">
1490 <td align=
"center">0</td>
1491 <td align=
"right">16</td>
1492 <td align=
"right">256</td>
1493 <td align=
"center">1.933e-03</td>
1495 <td align=
"center">1.061e-01</td>
1497 <td align=
"center">1.506e-01</td>
1500 <td align=
"center">1</td>
1501 <td align=
"right">64</td>
1502 <td align=
"right">1024</td>
1503 <td align=
"center">9.605e-05</td>
1504 <td align=
"center">4.33</td>
1505 <td align=
"center">8.995e-03</td>
1506 <td align=
"center">3.56</td>
1507 <td align=
"center">1.133e-02</td>
1510 <td align=
"center">2</td>
1511 <td align=
"right">256</td>
1512 <td align=
"right">4096</td>
1513 <td align=
"center">5.606e-06</td>
1514 <td align=
"center">4.10</td>
1515 <td align=
"center">9.018e-04</td>
1516 <td align=
"center">3.32</td>
1517 <td align=
"center">9.736e-04</td>
1520 <td align=
"center">3</td>
1521 <td align=
"right">1024</td>
1522 <td align=
"right">16384</td>
1523 <td align=
"center">3.484e-07</td>
1524 <td align=
"center">4.01</td>
1525 <td align=
"center">1.071e-04</td>
1526 <td align=
"center">3.07</td>
1527 <td align=
"center">1.088e-04</td>
1530 <td align=
"center">4</td>
1531 <td align=
"right">4096</td>
1532 <td align=
"right">65536</td>
1533 <td align=
"center">2.179e-08</td>
1534 <td align=
"center">4.00</td>
1535 <td align=
"center">1.327e-05</td>
1536 <td align=
"center">3.01</td>
1537 <td align=
"center">1.331e-05</td>
1540 <td align=
"center">5</td>
1541 <td align=
"right">16384</td>
1542 <td align=
"right">262144</td>
1543 <td align=
"center">1.363e-09</td>
1544 <td align=
"center">4.00</td>
1545 <td align=
"center">1.656e-06</td>
1546 <td align=
"center">3.00</td>
1547 <td align=
"center">1.657e-06</td>
1551Theoretically,
for polynomial degree @f$p@f$, the order of convergence in @f$L_2@f$
1552norm and @f$H^1@f$ seminorm should be @f$p+1@f$ and @f$p@f$, respectively. Our numerical
1553results are in good agreement with theory.
1555In the
second test
case, when you
run the program, the screen output should look like the following:
1558 Number of active cells : 192
1559 Number of degrees of freedom : 3072
1560 Error in the
L2 norm : 0.000323585
1561 Error in the H1 seminorm : 0.0296202
1562 Error in the energy
norm : 0.0420478
1563 Estimated error : 0.136067
1566 Number of active cells : 249
1567 Number of degrees of freedom : 3984
1568 Error in the
L2 norm : 0.000114739
1569 Error in the H1 seminorm : 0.0186571
1570 Error in the energy
norm : 0.0264879
1571 Estimated error : 0.0857186
1579The following figure provides a
log-
log plot of the errors versus
1580the number of degrees of freedom
for this test
case on the
L-shaped
1581domain. In order to interpret it, let @f$n@f$ be the number of degrees of
1582freedom, then on uniformly refined meshes, @f$h@f$ is of order
1583@f$1/\
sqrt{n}@f$ in 2D. Combining the theoretical results in the previous
case,
1584we see that
if the solution is sufficiently smooth,
1585we can expect the error in the @f$L_2@f$
norm to be of order @f$O(n^{-\frac{p+1}{2}})@f$
1586and in @f$H^1@f$ seminorm to be @f$O(n^{-\frac{p}{2}})@f$. It is not a priori
1587clear that one would get the same kind of behavior as a function of
1588@f$n@f$ on adaptively refined meshes like the ones we use
for this second
1589test
case, but one can certainly hope. Indeed, from the figure, we see
1590that the SIPG with adaptive mesh refinement produces asymptotically
1591the kinds of hoped-
for results:
1593<img width=
"600px" src=
"https://dealii.org/images/steps/developer/step-74.log-log-plot.png" alt=
"">
1595In addition, we observe that the error estimator decreases
1596at almost the same rate as the errors in the energy
norm and @f$H^1@f$ seminorm,
1597and
one order lower than the @f$L_2@f$ error. This suggests
1598its ability to predict regions with large errors.
1600While this tutorial is focused on the implementation, the @ref step_59
"step-59" tutorial program achieves an efficient
1601large-
scale solver in terms of computing time with
matrix-free solution techniques.
1602Note that the @ref step_59
"step-59" tutorial does not work with meshes containing hanging nodes at this moment,
1603because the multigrid interface matrices are not as easily determined,
1604but that is merely the lack of some interfaces in deal.II,
nothing fundamental.
1607<a name=
"step_74-PlainProg"></a>
1608<h1> The plain program</h1>
1609@include
"step-74.cc"
* * for(const auto &cell :triangulation.active_cell_iterators())
* * int main(int argc, char **argv)
* * * struct InterferenceTaperTransform *
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)
#define DEAL_II_NOT_IMPLEMENTED()
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.
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.
constexpr 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)
* * * RotationFunction< dim, Number >::RotationFunction Number(dim)
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)
constexpr 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 > &)