deal.II version GIT relicensing-6809-ge913b9bb34 2026-09-25 17:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
step-50.h
Go to the documentation of this file.
1,
482 *   const unsigned int /*component*/ = 0) const override
483 *   {
484 *   return 1.0;
485 *   }
486 *  
487 *  
488 *   template <typename number>
490 *   value(const Point<dim, VectorizedArray<number>> & /*p*/,
491 *   const unsigned int /*component*/ = 0) const
492 *   {
493 *   return VectorizedArray<number>(1.0);
494 *   }
495 *   };
496 *  
497 *  
498 * @endcode
499 *
500 * This next class represents the diffusion coefficient. We use a variable
501 * coefficient which is 100.0 at any point where at least one coordinate is
502 * less than -0.5, and 1.0 at all other points. As above, a separate value()
503 * returning a VectorizedArray is used for the matrix-free code. An @p
504 * average() function computes the arithmetic average for a set of points.
505 *
506 * @code
507 *   template <int dim>
508 *   class Coefficient : public Function<dim>
509 *   {
510 *   public:
511 *   virtual double value(const Point<dim> &p,
512 *   const unsigned int /*component*/ = 0) const override;
513 *  
514 *   template <typename number>
516 *   const unsigned int /*component*/ = 0) const;
517 *  
518 *   template <typename number>
519 *   number average_value(const std::vector<Point<dim, number>> &points) const;
520 *  
521 * @endcode
522 *
523 * When using a coefficient in the MatrixFree framework, we also
524 * need a function that creates a Table of coefficient values for a
525 * set of cells provided by the MatrixFree operator argument here.
526 *
527 * @code
528 *   template <typename number>
529 *   std::shared_ptr<Table<2, VectorizedArray<number>>> make_coefficient_table(
530 *   const MatrixFree<dim, number, VectorizedArray<number>> &mf_storage) const;
531 *   };
532 *  
533 *  
534 *  
535 *   template <int dim>
536 *   double Coefficient<dim>::value(const Point<dim> &p, const unsigned int) const
537 *   {
538 *   for (int d = 0; d < dim; ++d)
539 *   {
540 *   if (p[d] < -0.5)
541 *   return 100.0;
542 *   }
543 *   return 1.0;
544 *   }
545 *  
546 *  
547 *  
548 *   template <int dim>
549 *   template <typename number>
551 *   Coefficient<dim>::value(const Point<dim, VectorizedArray<number>> &p,
552 *   const unsigned int) const
553 *   {
555 *   for (unsigned int i = 0; i < VectorizedArray<number>::size(); ++i)
556 *   {
557 *   for (int d = 0; d < dim; ++d)
558 *   if (p[d][i] < -0.5)
559 *   {
560 *   return_value[i] = 100.0;
561 *   break;
562 *   }
563 *   }
564 *  
565 *   return return_value;
566 *   }
567 *  
568 *  
569 *  
570 *   template <int dim>
571 *   template <typename number>
572 *   number Coefficient<dim>::average_value(
573 *   const std::vector<Point<dim, number>> &points) const
574 *   {
575 *   number average(0);
576 *   for (unsigned int i = 0; i < points.size(); ++i)
577 *   average += value(points[i]);
578 *   average /= points.size();
579 *  
580 *   return average;
581 *   }
582 *  
583 *  
584 *  
585 *   template <int dim>
586 *   template <typename number>
587 *   std::shared_ptr<Table<2, VectorizedArray<number>>>
588 *   Coefficient<dim>::make_coefficient_table(
589 *   const MatrixFree<dim, number, VectorizedArray<number>> &mf_storage) const
590 *   {
591 *   auto coefficient_table =
592 *   std::make_shared<Table<2, VectorizedArray<number>>>();
593 *  
594 *   FEEvaluation<dim, -1, 0, 1, number> fe_eval(mf_storage);
595 *  
596 *   const unsigned int n_cells = mf_storage.n_cell_batches();
597 *  
598 *   coefficient_table->reinit(n_cells, 1);
599 *  
600 *   for (unsigned int cell = 0; cell < n_cells; ++cell)
601 *   {
602 *   fe_eval.reinit(cell);
603 *  
604 *   VectorizedArray<number> average_value = 0.;
605 *   for (const unsigned int q : fe_eval.quadrature_point_indices())
606 *   average_value += value(fe_eval.quadrature_point(q));
607 *   average_value /= fe_eval.n_q_points;
608 *  
609 *   (*coefficient_table)(cell, 0) = average_value;
610 *   }
611 *  
612 *   return coefficient_table;
613 *   }
614 *  
615 *  
616 *  
617 * @endcode
618 *
619 *
620 * <a name="step_50-Runtimeparameters"></a>
621 * <h3>Run time parameters</h3>
622 *
623
624 *
625 * We will use ParameterHandler to pass in parameters at runtime. The
626 * structure @p Settings parses and stores these parameters to be queried
627 * throughout the program.
628 *
629 * @code
630 *   struct Settings
631 *   {
632 *   bool try_parse(const std::string &prm_filename);
633 *  
634 *   enum SolverType
635 *   {
636 *   gmg_mb,
637 *   gmg_mf,
638 *   amg
639 *   };
640 *  
641 *   SolverType solver;
642 *  
643 *   int dimension;
644 *   double smoother_dampen;
645 *   unsigned int smoother_steps;
646 *   unsigned int n_steps;
647 *   bool output;
648 *   };
649 *  
650 *  
651 *  
652 *   bool Settings::try_parse(const std::string &prm_filename)
653 *   {
654 *   ParameterHandler prm;
655 *   prm.declare_entry("dim",
656 *   "2",
658 *   "The problem dimension.");
659 *   prm.declare_entry("n_steps",
660 *   "10",
662 *   "Number of adaptive refinement steps.");
663 *   prm.declare_entry("smoother dampen",
664 *   "1.0",
665 *   Patterns::Double(0.0),
666 *   "Dampen factor for the smoother.");
667 *   prm.declare_entry("smoother steps",
668 *   "1",
670 *   "Number of smoother steps.");
671 *   prm.declare_entry("solver",
672 *   "MF",
673 *   Patterns::Selection("MF|MB|AMG"),
674 *   "Switch between matrix-free GMG, "
675 *   "matrix-based GMG, and AMG.");
676 *   prm.declare_entry("output",
677 *   "false",
678 *   Patterns::Bool(),
679 *   "Output graphical results.");
680 *  
681 *   if (prm_filename.empty())
682 *   {
683 *   std::cout
684 *   << "**** Error: No input file provided!\n"
685 *   << "**** Error: Call this program as './step-50 input.prm\n"
686 *   << '\n'
687 *   << "**** You may want to use one of the input files in this\n"
688 *   << "**** directory, or use the following default values\n"
689 *   << "**** to create an input file:\n";
690 *   if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
691 *   prm.print_parameters(std::cout, ParameterHandler::PRM);
692 *   return false;
693 *   }
694 *  
695 *   try
696 *   {
697 *   prm.parse_input(prm_filename);
698 *   }
699 *   catch (std::exception &e)
700 *   {
701 *   if (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0)
702 *   std::cerr << e.what() << std::endl;
703 *   return false;
704 *   }
705 *  
706 *   if (prm.get("solver") == "MF")
707 *   this->solver = gmg_mf;
708 *   else if (prm.get("solver") == "MB")
709 *   this->solver = gmg_mb;
710 *   else if (prm.get("solver") == "AMG")
711 *   this->solver = amg;
712 *   else
713 *   AssertThrow(false, ExcNotImplemented());
714 *  
715 *   this->dimension = prm.get_integer("dim");
716 *   this->n_steps = prm.get_integer("n_steps");
717 *   this->smoother_dampen = prm.get_double("smoother dampen");
718 *   this->smoother_steps = prm.get_integer("smoother steps");
719 *   this->output = prm.get_bool("output");
720 *  
721 *   return true;
722 *   }
723 *  
724 *  
725 *  
726 * @endcode
727 *
728 *
729 * <a name="step_50-LaplaceProblemclass"></a>
730 * <h3>LaplaceProblem class</h3>
731 *
732
733 *
734 * This is the main class of the program. It looks very similar to
735 * @ref step_16 "step-16", @ref step_37 "step-37", and @ref step_40 "step-40". For the MatrixFree setup, we use the
736 * MatrixFreeOperators::LaplaceOperator class which defines `local_apply()`,
737 * `compute_diagonal()`, and `set_coefficient()` functions internally. Note
738 * that the polynomial degree is a template parameter of this class. This is
739 * necessary for the matrix-free code.
740 *
741 * @code
742 *   template <int dim, int degree>
743 *   class LaplaceProblem
744 *   {
745 *   public:
746 *   LaplaceProblem(const Settings &settings);
747 *   void run();
748 *  
749 *   private:
750 * @endcode
751 *
752 * We will use the following types throughout the program. First the
753 * matrix-based types, after that the matrix-free classes. For the
754 * matrix-free implementation, we use @p float for the level operators.
755 *
756 * @code
757 *   using MatrixType = LA::MPI::SparseMatrix;
758 *   using VectorType = LA::MPI::Vector;
759 *   using PreconditionAMG = LA::MPI::PreconditionAMG;
760 *  
761 *   using MatrixFreeLevelMatrix = MatrixFreeOperators::LaplaceOperator<
762 *   dim,
763 *   degree,
764 *   degree + 1,
765 *   1,
767 *   using MatrixFreeActiveMatrix = MatrixFreeOperators::LaplaceOperator<
768 *   dim,
769 *   degree,
770 *   degree + 1,
771 *   1,
773 *  
774 *   using MatrixFreeLevelVector = LinearAlgebra::distributed::Vector<float>;
775 *   using MatrixFreeActiveVector = LinearAlgebra::distributed::Vector<double>;
776 *  
777 *   void setup_system();
778 *   void setup_multigrid();
779 *   void assemble_system();
780 *   void assemble_multigrid();
781 *   void assemble_rhs();
782 *   void solve();
783 *   void estimate();
784 *   void refine_grid();
785 *   void output_results(const unsigned int cycle);
786 *  
787 *   Settings settings;
788 *  
789 *   MPI_Comm mpi_communicator;
790 *   ConditionalOStream pcout;
791 *  
793 *   const MappingQ1<dim> mapping;
794 *   const FE_Q<dim> fe;
795 *  
796 *   DoFHandler<dim> dof_handler;
797 *  
798 *   IndexSet locally_owned_dofs;
799 *   IndexSet locally_relevant_dofs;
800 *   AffineConstraints<double> constraints;
801 *  
802 *   MatrixType system_matrix;
803 *   MatrixFreeActiveMatrix mf_system_matrix;
804 *   VectorType solution;
805 *   VectorType right_hand_side;
806 *   Vector<double> estimated_error_square_per_cell;
807 *  
808 *   MGLevelObject<MatrixType> mg_matrix;
809 *   MGLevelObject<MatrixType> mg_interface_in;
810 *   MGConstrainedDoFs mg_constrained_dofs;
811 *  
813 *  
814 *   TimerOutput computing_timer;
815 *   };
816 *  
817 *  
818 * @endcode
819 *
820 * The only interesting part about the constructor is that we construct the
821 * multigrid hierarchy unless we use AMG. For that, we need to parse the
822 * run time parameters before this constructor completes.
823 *
824 * @code
825 *   template <int dim, int degree>
826 *   LaplaceProblem<dim, degree>::LaplaceProblem(const Settings &settings)
827 *   : settings(settings)
828 *   , mpi_communicator(MPI_COMM_WORLD)
829 *   , pcout(std::cout,
830 *   (Utilities::MPI::this_mpi_process(mpi_communicator) == 0))
831 *   , triangulation(
832 *   mpi_communicator,
834 *   (settings.solver == Settings::amg) ?
836 *   parallel::distributed::Triangulation<
838 *   , mapping()
839 *   , fe(degree)
840 *   , dof_handler(triangulation)
841 *   , computing_timer(pcout, TimerOutput::never, TimerOutput::wall_times)
842 *   {
843 *   GridGenerator::hyper_L(triangulation, -1., 1., /*colorize*/ false);
844 *   triangulation.refine_global(1);
845 *   }
846 *  
847 *  
848 *  
849 * @endcode
850 *
851 *
852 * <a name="step_50-LaplaceProblemsetup_system"></a>
853 * <h4>LaplaceProblem::setup_system()</h4>
854 *
855
856 *
857 * Unlike @ref step_16 "step-16" and @ref step_37 "step-37", we split the set up into two parts,
858 * setup_system() and setup_multigrid(). Here is the typical setup_system()
859 * function for the active mesh found in most tutorials. For matrix-free, the
860 * active mesh set up is similar to @ref step_37 "step-37"; for matrix-based (GMG and AMG
861 * solvers), the setup is similar to @ref step_40 "step-40".
862 *
863 * @code
864 *   template <int dim, int degree>
865 *   void LaplaceProblem<dim, degree>::setup_system()
866 *   {
867 *   TimerOutput::Scope timing(computing_timer, "Setup");
868 *  
869 *   dof_handler.distribute_dofs(fe);
870 *  
871 *   locally_relevant_dofs =
873 *   locally_owned_dofs = dof_handler.locally_owned_dofs();
874 *  
875 *   solution.reinit(locally_owned_dofs, mpi_communicator);
876 *   right_hand_side.reinit(locally_owned_dofs, mpi_communicator);
877 *   constraints.reinit(locally_owned_dofs, locally_relevant_dofs);
878 *   DoFTools::make_hanging_node_constraints(dof_handler, constraints);
879 *  
881 *   mapping, dof_handler, 0, Functions::ZeroFunction<dim>(), constraints);
882 *   constraints.close();
883 *  
884 *   switch (settings.solver)
885 *   {
886 *   case Settings::gmg_mf:
887 *   {
888 *   typename MatrixFree<dim, double>::AdditionalData additional_data;
889 *   additional_data.tasks_parallel_scheme =
891 *   additional_data.mapping_update_flags =
893 *   std::shared_ptr<MatrixFree<dim, double>> mf_storage =
894 *   std::make_shared<MatrixFree<dim, double>>();
895 *   mf_storage->reinit(mapping,
896 *   dof_handler,
897 *   constraints,
898 *   QGauss<1>(degree + 1),
899 *   additional_data);
900 *  
901 *   mf_system_matrix.initialize(mf_storage);
902 *  
903 *   const Coefficient<dim> coefficient;
904 *   mf_system_matrix.set_coefficient(
905 *   coefficient.make_coefficient_table(*mf_storage));
906 *  
907 *   break;
908 *   }
909 *  
910 *   case Settings::gmg_mb:
911 *   case Settings::amg:
912 *   {
913 *   DynamicSparsityPattern dsp(locally_relevant_dofs);
914 *   DoFTools::make_sparsity_pattern(dof_handler, dsp, constraints);
915 *  
917 *   locally_owned_dofs,
918 *   mpi_communicator,
919 *   locally_relevant_dofs);
920 *  
921 *   system_matrix.reinit(locally_owned_dofs,
922 *   locally_owned_dofs,
923 *   dsp,
924 *   mpi_communicator);
925 *  
926 *   break;
927 *   }
928 *  
929 *   default:
931 *   }
932 *   }
933 *  
934 * @endcode
935 *
936 *
937 * <a name="step_50-LaplaceProblemsetup_multigrid"></a>
938 * <h4>LaplaceProblem::setup_multigrid()</h4>
939 *
940
941 *
942 * This function does the multilevel setup for both matrix-free and
943 * matrix-based GMG. The matrix-free setup is similar to that of @ref step_37 "step-37", and
944 * the matrix-based is similar to @ref step_16 "step-16", except we must use appropriate
945 * distributed sparsity patterns.
946 *
947
948 *
949 * The function is not called for the AMG approach, but to err on the
950 * safe side, the main `switch` statement of this function
951 * nevertheless makes sure that the function only operates on known
952 * multigrid settings by throwing an assertion if the function were
953 * called for anything other than the two geometric multigrid methods.
954 *
955 * @code
956 *   template <int dim, int degree>
957 *   void LaplaceProblem<dim, degree>::setup_multigrid()
958 *   {
959 *   TimerOutput::Scope timing(computing_timer, "Setup multigrid");
960 *  
961 *   dof_handler.distribute_mg_dofs();
962 *  
963 *   mg_constrained_dofs.clear();
964 *   mg_constrained_dofs.initialize(dof_handler);
965 *  
966 *   const std::set<types::boundary_id> boundary_ids = {types::boundary_id(0)};
967 *   mg_constrained_dofs.make_zero_boundary_constraints(dof_handler,
968 *   boundary_ids);
969 *  
970 *   const unsigned int n_levels = triangulation.n_global_levels();
971 *  
972 *   switch (settings.solver)
973 *   {
974 *   case Settings::gmg_mf:
975 *   {
976 *   mf_mg_matrix.resize(0, n_levels - 1);
977 *  
978 *   for (unsigned int level = 0; level < n_levels; ++level)
979 *   {
980 *   AffineConstraints<double> level_constraints(
981 *   dof_handler.locally_owned_mg_dofs(level),
983 *   level));
984 *   for (const types::global_dof_index dof_index :
985 *   mg_constrained_dofs.get_boundary_indices(level))
986 *   level_constraints.constrain_dof_to_zero(dof_index);
987 *   level_constraints.close();
988 *  
989 *   typename MatrixFree<dim, float>::AdditionalData additional_data;
990 *   additional_data.tasks_parallel_scheme =
992 *   additional_data.mapping_update_flags =
995 *   additional_data.mg_level = level;
996 *   std::shared_ptr<MatrixFree<dim, float>> mf_storage_level(
997 *   new MatrixFree<dim, float>());
998 *   mf_storage_level->reinit(mapping,
999 *   dof_handler,
1000 *   level_constraints,
1001 *   QGauss<1>(degree + 1),
1002 *   additional_data);
1003 *  
1004 *   mf_mg_matrix[level].initialize(mf_storage_level,
1005 *   mg_constrained_dofs,
1006 *   level);
1007 *  
1008 *   const Coefficient<dim> coefficient;
1009 *   mf_mg_matrix[level].set_coefficient(
1010 *   coefficient.make_coefficient_table(*mf_storage_level));
1011 *  
1012 *   mf_mg_matrix[level].compute_diagonal();
1013 *   }
1014 *  
1015 *   break;
1016 *   }
1017 *  
1018 *   case Settings::gmg_mb:
1019 *   {
1020 *   mg_matrix.resize(0, n_levels - 1);
1021 *   mg_matrix.clear_elements();
1022 *   mg_interface_in.resize(0, n_levels - 1);
1023 *   mg_interface_in.clear_elements();
1024 *  
1025 *   for (unsigned int level = 0; level < n_levels; ++level)
1026 *   {
1027 *   const IndexSet dof_set =
1029 *   level);
1030 *  
1031 *   {
1032 *   DynamicSparsityPattern dsp(dof_set);
1033 *   MGTools::make_sparsity_pattern(dof_handler, dsp, level);
1034 *   dsp.compress();
1036 *   dsp,
1037 *   dof_handler.locally_owned_mg_dofs(level),
1038 *   mpi_communicator,
1039 *   dof_set);
1040 *  
1041 *   mg_matrix[level].reinit(
1042 *   dof_handler.locally_owned_mg_dofs(level),
1043 *   dof_handler.locally_owned_mg_dofs(level),
1044 *   dsp,
1045 *   mpi_communicator);
1046 *   }
1047 *  
1048 *   {
1049 *   DynamicSparsityPattern dsp(dof_set);
1051 *   mg_constrained_dofs,
1052 *   dsp,
1053 *   level);
1054 *   dsp.compress();
1056 *   dsp,
1057 *   dof_handler.locally_owned_mg_dofs(level),
1058 *   mpi_communicator,
1059 *   dof_set);
1060 *  
1061 *   mg_interface_in[level].reinit(
1062 *   dof_handler.locally_owned_mg_dofs(level),
1063 *   dof_handler.locally_owned_mg_dofs(level),
1064 *   dsp,
1065 *   mpi_communicator);
1066 *   }
1067 *   }
1068 *   break;
1069 *   }
1070 *  
1071 *   default:
1073 *   }
1074 *   }
1075 *  
1076 *  
1077 * @endcode
1078 *
1079 *
1080 * <a name="step_50-LaplaceProblemassemble_system"></a>
1081 * <h4>LaplaceProblem::assemble_system()</h4>
1082 *
1083
1084 *
1085 * The assembly is split into three parts: `assemble_system()`,
1086 * `assemble_multigrid()`, and `assemble_rhs()`. The
1087 * `assemble_system()` function here assembles and stores the (global)
1088 * system matrix and the right-hand side for the matrix-based
1089 * methods. It is similar to the assembly in @ref step_40 "step-40".
1090 *
1091
1092 *
1093 * Note that the matrix-free method does not execute this function as it does
1094 * not need to assemble a matrix, and it will instead assemble the right-hand
1095 * side in assemble_rhs().
1096 *
1097 * @code
1098 *   template <int dim, int degree>
1099 *   void LaplaceProblem<dim, degree>::assemble_system()
1100 *   {
1101 *   TimerOutput::Scope timing(computing_timer, "Assemble");
1102 *  
1103 *   const QGauss<dim> quadrature_formula(degree + 1);
1104 *  
1105 *   FEValues<dim> fe_values(fe,
1106 *   quadrature_formula,
1109 *  
1110 *   const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1111 *   const unsigned int n_q_points = quadrature_formula.size();
1112 *  
1113 *   FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
1114 *   Vector<double> cell_rhs(dofs_per_cell);
1115 *  
1116 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1117 *  
1118 *   const Coefficient<dim> coefficient;
1119 *   RightHandSide<dim> rhs;
1120 *   std::vector<double> rhs_values(n_q_points);
1121 *  
1122 *   for (const auto &cell : dof_handler.active_cell_iterators())
1123 *   if (cell->is_locally_owned())
1124 *   {
1125 *   cell_matrix = 0;
1126 *   cell_rhs = 0;
1127 *  
1128 *   fe_values.reinit(cell);
1129 *  
1130 *   const double coefficient_value =
1131 *   coefficient.average_value(fe_values.get_quadrature_points());
1132 *   rhs.value_list(fe_values.get_quadrature_points(), rhs_values);
1133 *  
1134 *   for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1135 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1136 *   {
1137 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1138 *   cell_matrix(i, j) +=
1139 *   coefficient_value * // epsilon(x)
1140 *   fe_values.shape_grad(i, q_point) * // * grad phi_i(x)
1141 *   fe_values.shape_grad(j, q_point) * // * grad phi_j(x)
1142 *   fe_values.JxW(q_point); // * dx
1143 *  
1144 *   cell_rhs(i) +=
1145 *   fe_values.shape_value(i, q_point) * // grad phi_i(x)
1146 *   rhs_values[q_point] * // * f(x)
1147 *   fe_values.JxW(q_point); // * dx
1148 *   }
1149 *  
1150 *   cell->get_dof_indices(local_dof_indices);
1151 *   constraints.distribute_local_to_global(cell_matrix,
1152 *   cell_rhs,
1153 *   local_dof_indices,
1154 *   system_matrix,
1155 *   right_hand_side);
1156 *   }
1157 *  
1158 *   system_matrix.compress(VectorOperation::add);
1159 *   right_hand_side.compress(VectorOperation::add);
1160 *   }
1161 *  
1162 *  
1163 * @endcode
1164 *
1165 *
1166 * <a name="step_50-LaplaceProblemassemble_multigrid"></a>
1167 * <h4>LaplaceProblem::assemble_multigrid()</h4>
1168 *
1169
1170 *
1171 * The following function assembles and stores the multilevel matrices for the
1172 * matrix-based GMG method. This function is similar to the one found in
1173 * @ref step_16 "step-16", only here it works for distributed meshes. This difference amounts
1174 * to adding a condition that we only assemble on locally owned level cells
1175 * and a call to compress() for each matrix that is built.
1176 *
1177 * @code
1178 *   template <int dim, int degree>
1179 *   void LaplaceProblem<dim, degree>::assemble_multigrid()
1180 *   {
1181 *   TimerOutput::Scope timing(computing_timer, "Assemble multigrid");
1182 *  
1183 *   const QGauss<dim> quadrature_formula(degree + 1);
1184 *  
1185 *   FEValues<dim> fe_values(fe,
1186 *   quadrature_formula,
1189 *  
1190 *   const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1191 *   const unsigned int n_q_points = quadrature_formula.size();
1192 *  
1193 *   FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
1194 *  
1195 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1196 *  
1197 *   const Coefficient<dim> coefficient;
1198 *  
1199 *   std::vector<AffineConstraints<double>> boundary_constraints(
1200 *   triangulation.n_global_levels());
1201 *   for (unsigned int level = 0; level < triangulation.n_global_levels();
1202 *   ++level)
1203 *   {
1204 *   boundary_constraints[level].reinit(
1205 *   dof_handler.locally_owned_mg_dofs(level),
1207 *  
1208 *   for (const types::global_dof_index dof_index :
1209 *   mg_constrained_dofs.get_refinement_edge_indices(level))
1210 *   boundary_constraints[level].constrain_dof_to_zero(dof_index);
1211 *   for (const types::global_dof_index dof_index :
1212 *   mg_constrained_dofs.get_boundary_indices(level))
1213 *   boundary_constraints[level].constrain_dof_to_zero(dof_index);
1214 *   boundary_constraints[level].close();
1215 *   }
1216 *  
1217 *   for (const auto &cell : dof_handler.cell_iterators())
1218 *   if (cell->level_subdomain_id() == triangulation.locally_owned_subdomain())
1219 *   {
1220 *   cell_matrix = 0;
1221 *   fe_values.reinit(cell);
1222 *  
1223 *   const double coefficient_value =
1224 *   coefficient.average_value(fe_values.get_quadrature_points());
1225 *  
1226 *   for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1227 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1228 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1229 *   cell_matrix(i, j) +=
1230 *   coefficient_value * fe_values.shape_grad(i, q_point) *
1231 *   fe_values.shape_grad(j, q_point) * fe_values.JxW(q_point);
1232 *  
1233 *   cell->get_mg_dof_indices(local_dof_indices);
1234 *  
1235 *   boundary_constraints[cell->level()].distribute_local_to_global(
1236 *   cell_matrix, local_dof_indices, mg_matrix[cell->level()]);
1237 *  
1238 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1239 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1240 *   if (mg_constrained_dofs.is_interface_matrix_entry(
1241 *   cell->level(), local_dof_indices[i], local_dof_indices[j]))
1242 *   mg_interface_in[cell->level()].add(local_dof_indices[i],
1243 *   local_dof_indices[j],
1244 *   cell_matrix(i, j));
1245 *   }
1246 *  
1247 *   for (unsigned int i = 0; i < triangulation.n_global_levels(); ++i)
1248 *   {
1249 *   mg_matrix[i].compress(VectorOperation::add);
1250 *   mg_interface_in[i].compress(VectorOperation::add);
1251 *   }
1252 *   }
1253 *  
1254 *  
1255 *  
1256 * @endcode
1257 *
1258 *
1259 * <a name="step_50-LaplaceProblemassemble_rhs"></a>
1260 * <h4>LaplaceProblem::assemble_rhs()</h4>
1261 *
1262
1263 *
1264 * The final function in this triptych assembles the right-hand side
1265 * vector for the matrix-free method -- because in the matrix-free
1266 * framework, we don't have to assemble the matrix and can get away
1267 * with only assembling the right hand side. We could do this by extracting
1268 * the code from the `assemble_system()` function above that deals with the
1269 * right hand side, but we decide instead to go all in on the matrix-free
1270 * approach and do the assembly using that way as well.
1271 *
1272
1273 *
1274 * The result is a function that is similar
1275 * to the one found in the "Use FEEvaluation::read_dof_values_plain()
1276 * to avoid resolving constraints" subsection in the "Possibilities
1277 * for extensions" section of @ref step_37 "step-37".
1278 *
1279
1280 *
1281 * The reason for this function is that the MatrixFree operators do not take
1282 * into account non-homogeneous Dirichlet constraints, instead treating all
1283 * Dirichlet constraints as homogeneous. To account for this, the right-hand
1284 * side here is assembled as the residual @f$r_0 = f-Au_0@f$, where @f$u_0@f$ is a
1285 * zero vector except in the Dirichlet values. Then when solving, we have that
1286 * the solution is @f$u = u_0 + A^{-1}r_0@f$. This can be seen as a Newton
1287 * iteration on a linear system with initial guess @f$u_0@f$. The CG solve in the
1288 * `solve()` function below computes @f$A^{-1}r_0@f$ and the call to
1289 * `constraints.distribute()` (which directly follows) adds the @f$u_0@f$.
1290 *
1291
1292 *
1293 * Obviously, since we are considering a problem with zero Dirichlet boundary,
1294 * we could have taken a similar approach to @ref step_37 "step-37" `assemble_rhs()`, but
1295 * this additional work allows us to change the problem declaration if we so
1296 * choose.
1297 *
1298
1299 *
1300 * This function has two parts in the integration loop: applying the negative
1301 * of matrix @f$A@f$ to @f$u_0@f$ by submitting the negative of the gradient, and
1302 * adding the right-hand side contribution by submitting the value @f$f@f$. We
1303 * must be sure to use `read_dof_values_plain()` for evaluating @f$u_0@f$ as
1304 * `read_dof_values()` would set all Dirichlet values to zero.
1305 *
1306
1307 *
1308 * Finally, the system_rhs vector is of type LA::MPI::Vector, but the
1309 * MatrixFree class only work for
1310 * LinearAlgebra::distributed::Vector. Therefore we must
1311 * compute the right-hand side using MatrixFree functionality and then
1312 * use the functions in the `ChangeVectorType` namespace to copy it to
1313 * the correct type.
1314 *
1315 * @code
1316 *   template <int dim, int degree>
1317 *   void LaplaceProblem<dim, degree>::assemble_rhs()
1318 *   {
1319 *   TimerOutput::Scope timing(computing_timer, "Assemble right-hand side");
1320 *  
1321 *   MatrixFreeActiveVector solution_copy;
1322 *   MatrixFreeActiveVector right_hand_side_copy;
1323 *   mf_system_matrix.initialize_dof_vector(solution_copy);
1324 *   mf_system_matrix.initialize_dof_vector(right_hand_side_copy);
1325 *  
1326 *   solution_copy = 0.;
1327 *   constraints.distribute(solution_copy);
1328 *   solution_copy.update_ghost_values();
1329 *   right_hand_side_copy = 0;
1330 *   const Table<2, VectorizedArray<double>> &coefficient =
1331 *   *(mf_system_matrix.get_coefficient());
1332 *  
1333 *   RightHandSide<dim> right_hand_side_function;
1334 *  
1335 *   FEEvaluation<dim, degree, degree + 1, 1, double> phi(
1336 *   *mf_system_matrix.get_matrix_free());
1337 *  
1338 *   for (unsigned int cell = 0;
1339 *   cell < mf_system_matrix.get_matrix_free()->n_cell_batches();
1340 *   ++cell)
1341 *   {
1342 *   phi.reinit(cell);
1343 *   phi.read_dof_values_plain(solution_copy);
1344 *   phi.evaluate(EvaluationFlags::gradients);
1345 *  
1346 *   for (const unsigned int q : phi.quadrature_point_indices())
1347 *   {
1348 *   phi.submit_gradient(-1.0 *
1349 *   (coefficient(cell, 0) * phi.get_gradient(q)),
1350 *   q);
1351 *   phi.submit_value(
1352 *   right_hand_side_function.value(phi.quadrature_point(q)), q);
1353 *   }
1354 *  
1355 *   phi.integrate_scatter(EvaluationFlags::values |
1356 *   EvaluationFlags::gradients,
1357 *   right_hand_side_copy);
1358 *   }
1359 *  
1360 *   right_hand_side_copy.compress(VectorOperation::add);
1361 *  
1362 *   ChangeVectorTypes::copy(right_hand_side, right_hand_side_copy);
1363 *   }
1364 *  
1365 *  
1366 *  
1367 * @endcode
1368 *
1369 *
1370 * <a name="step_50-LaplaceProblemsolve"></a>
1371 * <h4>LaplaceProblem::solve()</h4>
1372 *
1373
1374 *
1375 * Here we set up the multigrid preconditioner, test the timing of a single
1376 * V-cycle, and solve the linear system. Unsurprisingly, this is one of the
1377 * places where the three methods differ the most.
1378 *
1379 * @code
1380 *   template <int dim, int degree>
1381 *   void LaplaceProblem<dim, degree>::solve()
1382 *   {
1383 *   TimerOutput::Scope timing(computing_timer, "Solve");
1384 *  
1385 *   SolverControl solver_control(1000, 1.e-10 * right_hand_side.l2_norm());
1386 *   solver_control.enable_history_data();
1387 *  
1388 *   solution = 0.;
1389 *  
1390 * @endcode
1391 *
1392 * The solver for the matrix-free GMG method is similar to @ref step_37 "step-37", apart
1393 * from adding some interface matrices in complete analogy to @ref step_16 "step-16".
1394 *
1395 * @code
1396 *   switch (settings.solver)
1397 *   {
1398 *   case Settings::gmg_mf:
1399 *   {
1400 *   computing_timer.enter_subsection("Solve: Preconditioner setup");
1401 *  
1402 *   MGTransferMatrixFree<dim, float> mg_transfer(mg_constrained_dofs);
1403 *   mg_transfer.build(dof_handler);
1404 *  
1405 *   SolverControl coarse_solver_control(1000, 1e-12, false, false);
1406 *   SolverCG<MatrixFreeLevelVector> coarse_solver(
1407 *   coarse_solver_control);
1408 *   PreconditionIdentity identity;
1409 *   MGCoarseGridIterativeSolver<MatrixFreeLevelVector,
1410 *   SolverCG<MatrixFreeLevelVector>,
1411 *   MatrixFreeLevelMatrix,
1412 *   PreconditionIdentity>
1413 *   coarse_grid_solver(coarse_solver, mf_mg_matrix[0], identity);
1414 *  
1415 *   using Smoother = PreconditionJacobi<MatrixFreeLevelMatrix>;
1416 *   MGSmootherPrecondition<MatrixFreeLevelMatrix,
1417 *   Smoother,
1418 *   MatrixFreeLevelVector>
1419 *   smoother;
1420 *   smoother.initialize(mf_mg_matrix,
1421 *   typename Smoother::AdditionalData(
1422 *   settings.smoother_dampen));
1423 *   smoother.set_steps(settings.smoother_steps);
1424 *  
1425 *   mg::Matrix<MatrixFreeLevelVector> mg_m(mf_mg_matrix);
1426 *  
1427 *   MGLevelObject<
1428 *   MatrixFreeOperators::MGInterfaceOperator<MatrixFreeLevelMatrix>>
1429 *   mg_interface_matrices;
1430 *   mg_interface_matrices.resize(0,
1431 *   triangulation.n_global_levels() - 1);
1432 *   for (unsigned int level = 0;
1433 *   level < triangulation.n_global_levels();
1434 *   ++level)
1435 *   mg_interface_matrices[level].initialize(mf_mg_matrix[level]);
1436 *   mg::Matrix<MatrixFreeLevelVector> mg_interface(
1437 *   mg_interface_matrices);
1438 *  
1439 *   Multigrid<MatrixFreeLevelVector> mg(
1440 *   mg_m, coarse_grid_solver, mg_transfer, smoother, smoother);
1441 *   mg.set_edge_matrices(mg_interface, mg_interface);
1442 *  
1443 *   PreconditionMG<dim,
1444 *   MatrixFreeLevelVector,
1445 *   MGTransferMatrixFree<dim, float>>
1446 *   preconditioner(dof_handler, mg, mg_transfer);
1447 *  
1448 * @endcode
1449 *
1450 * Copy the solution vector and right-hand side from LA::MPI::Vector
1451 * to LinearAlgebra::distributed::Vector so that we can solve.
1452 *
1453 * @code
1454 *   MatrixFreeActiveVector solution_copy;
1455 *   MatrixFreeActiveVector right_hand_side_copy;
1456 *   mf_system_matrix.initialize_dof_vector(solution_copy);
1457 *   mf_system_matrix.initialize_dof_vector(right_hand_side_copy);
1458 *  
1459 *   ChangeVectorTypes::copy(solution_copy, solution);
1460 *   ChangeVectorTypes::copy(right_hand_side_copy, right_hand_side);
1461 *   computing_timer.leave_subsection("Solve: Preconditioner setup");
1462 *  
1463 * @endcode
1464 *
1465 * Timing for 1 V-cycle.
1466 *
1467 * @code
1468 *   {
1469 *   TimerOutput::Scope timing(computing_timer,
1470 *   "Solve: 1 multigrid V-cycle");
1471 *   preconditioner.vmult(solution_copy, right_hand_side_copy);
1472 *   }
1473 *   solution_copy = 0.;
1474 *  
1475 * @endcode
1476 *
1477 * Solve the linear system, update the ghost values of the solution,
1478 * copy back to LA::MPI::Vector and distribute constraints.
1479 *
1480 * @code
1481 *   {
1482 *   SolverCG<MatrixFreeActiveVector> solver(solver_control);
1483 *  
1484 *   TimerOutput::Scope timing(computing_timer, "Solve: CG");
1485 *   solver.solve(mf_system_matrix,
1486 *   solution_copy,
1487 *   right_hand_side_copy,
1488 *   preconditioner);
1489 *   }
1490 *  
1491 *   solution_copy.update_ghost_values();
1492 *   ChangeVectorTypes::copy(solution, solution_copy);
1493 *   constraints.distribute(solution);
1494 *  
1495 *   break;
1496 *   }
1497 *  
1498 * @endcode
1499 *
1500 * Solver for the matrix-based GMG method, similar to @ref step_16 "step-16", only
1501 * using a Jacobi smoother instead of a SOR smoother (which is not
1502 * implemented in parallel).
1503 *
1504 * @code
1505 *   case Settings::gmg_mb:
1506 *   {
1507 *   computing_timer.enter_subsection("Solve: Preconditioner setup");
1508 *  
1509 *   MGTransferPrebuilt<VectorType> mg_transfer(mg_constrained_dofs);
1510 *   mg_transfer.build(dof_handler);
1511 *  
1512 *   SolverControl coarse_solver_control(1000, 1e-12, false, false);
1513 *   SolverCG<VectorType> coarse_solver(coarse_solver_control);
1514 *   PreconditionIdentity identity;
1515 *   MGCoarseGridIterativeSolver<VectorType,
1516 *   SolverCG<VectorType>,
1517 *   MatrixType,
1518 *   PreconditionIdentity>
1519 *   coarse_grid_solver(coarse_solver, mg_matrix[0], identity);
1520 *  
1521 *   using Smoother = LA::MPI::PreconditionJacobi;
1522 *   MGSmootherPrecondition<MatrixType, Smoother, VectorType> smoother;
1523 *  
1524 *   #ifdef USE_PETSC_LA
1525 *   smoother.initialize(mg_matrix);
1526 *   Assert(
1527 *   settings.smoother_dampen == 1.0,
1528 *   ExcNotImplemented(
1529 *   "PETSc's PreconditionJacobi has no support for a damping parameter."));
1530 *   #else
1531 *   smoother.initialize(mg_matrix, settings.smoother_dampen);
1532 *   #endif
1533 *  
1534 *   smoother.set_steps(settings.smoother_steps);
1535 *  
1536 *   mg::Matrix<VectorType> mg_m(mg_matrix);
1537 *   mg::Matrix<VectorType> mg_in(mg_interface_in);
1538 *   mg::Matrix<VectorType> mg_out(mg_interface_in);
1539 *  
1540 *   Multigrid<VectorType> mg(
1541 *   mg_m, coarse_grid_solver, mg_transfer, smoother, smoother);
1542 *   mg.set_edge_matrices(mg_out, mg_in);
1543 *  
1544 *  
1545 *   PreconditionMG<dim, VectorType, MGTransferPrebuilt<VectorType>>
1546 *   preconditioner(dof_handler, mg, mg_transfer);
1547 *  
1548 *   computing_timer.leave_subsection("Solve: Preconditioner setup");
1549 *  
1550 * @endcode
1551 *
1552 * Timing for 1 V-cycle.
1553 *
1554 * @code
1555 *   {
1556 *   TimerOutput::Scope timing(computing_timer,
1557 *   "Solve: 1 multigrid V-cycle");
1558 *   preconditioner.vmult(solution, right_hand_side);
1559 *   }
1560 *   solution = 0.;
1561 *  
1562 * @endcode
1563 *
1564 * Solve the linear system and distribute constraints.
1565 *
1566 * @code
1567 *   {
1568 *   SolverCG<VectorType> solver(solver_control);
1569 *  
1570 *   TimerOutput::Scope timing(computing_timer, "Solve: CG");
1571 *   solver.solve(system_matrix,
1572 *   solution,
1573 *   right_hand_side,
1574 *   preconditioner);
1575 *   }
1576 *  
1577 *   constraints.distribute(solution);
1578 *  
1579 *   break;
1580 *   }
1581 *  
1582 * @endcode
1583 *
1584 * Solver for the AMG method, similar to @ref step_40 "step-40".
1585 *
1586 * @code
1587 *   case Settings::amg:
1588 *   {
1589 *   computing_timer.enter_subsection("Solve: Preconditioner setup");
1590 *  
1591 *   PreconditionAMG preconditioner;
1592 *   PreconditionAMG::AdditionalData Amg_data;
1593 *  
1594 *   #ifdef USE_PETSC_LA
1595 *   Amg_data.symmetric_operator = true;
1596 *   #else
1597 *   Amg_data.elliptic = true;
1598 *   Amg_data.smoother_type = "Jacobi";
1599 *   # ifdef DEAL_II_TRILINOS_WITH_EPETRA
1600 *   Amg_data.higher_order_elements = true;
1601 *   # endif
1602 *   Amg_data.smoother_sweeps = settings.smoother_steps;
1603 *   Amg_data.aggregation_threshold = 0.02;
1604 *   #endif
1605 *  
1606 *   Amg_data.output_details = false;
1607 *  
1608 *   preconditioner.initialize(system_matrix, Amg_data);
1609 *   computing_timer.leave_subsection("Solve: Preconditioner setup");
1610 *  
1611 * @endcode
1612 *
1613 * Timing for 1 V-cycle.
1614 *
1615 * @code
1616 *   {
1617 *   TimerOutput::Scope timing(computing_timer,
1618 *   "Solve: 1 multigrid V-cycle");
1619 *   preconditioner.vmult(solution, right_hand_side);
1620 *   }
1621 *   solution = 0.;
1622 *  
1623 * @endcode
1624 *
1625 * Solve the linear system and distribute constraints.
1626 *
1627 * @code
1628 *   {
1629 *   SolverCG<VectorType> solver(solver_control);
1630 *  
1631 *   TimerOutput::Scope timing(computing_timer, "Solve: CG");
1632 *   solver.solve(system_matrix,
1633 *   solution,
1634 *   right_hand_side,
1635 *   preconditioner);
1636 *   }
1637 *   constraints.distribute(solution);
1638 *  
1639 *   break;
1640 *   }
1641 *  
1642 *   default:
1643 *   DEAL_II_ASSERT_UNREACHABLE();
1644 *   }
1645 *  
1646 *   pcout << " Number of CG iterations: " << solver_control.last_step()
1647 *   << std::endl;
1648 *   }
1649 *  
1650 *  
1651 * @endcode
1652 *
1653 *
1654 * <a name="step_50-Theerrorestimator"></a>
1655 * <h3>The error estimator</h3>
1656 *
1657
1658 *
1659 * We use the FEInterfaceValues class to assemble an error estimator to decide
1660 * which cells to refine. See the exact definition of the cell and face
1661 * integrals in the introduction. To use the method, we define Scratch and
1662 * Copy objects for the MeshWorker::mesh_loop() with much of the following
1663 * code being in essence as was set up in @ref step_12 "step-12" already (or at least similar
1664 * in spirit).
1665 *
1666 * @code
1667 *   template <int dim>
1668 *   struct ScratchData
1669 *   {
1670 *   ScratchData(const Mapping<dim> &mapping,
1671 *   const FiniteElement<dim> &fe,
1672 *   const unsigned int quadrature_degree,
1673 *   const UpdateFlags update_flags,
1674 *   const UpdateFlags interface_update_flags)
1675 *   : fe_values(mapping, fe, QGauss<dim>(quadrature_degree), update_flags)
1676 *   , fe_interface_values(mapping,
1677 *   fe,
1678 *   QGauss<dim - 1>(quadrature_degree),
1679 *   interface_update_flags)
1680 *   {}
1681 *  
1682 *  
1683 *   ScratchData(const ScratchData<dim> &scratch_data)
1684 *   : fe_values(scratch_data.fe_values.get_mapping(),
1685 *   scratch_data.fe_values.get_fe(),
1686 *   scratch_data.fe_values.get_quadrature(),
1687 *   scratch_data.fe_values.get_update_flags())
1688 *   , fe_interface_values(scratch_data.fe_values.get_mapping(),
1689 *   scratch_data.fe_values.get_fe(),
1690 *   scratch_data.fe_interface_values.get_quadrature(),
1691 *   scratch_data.fe_interface_values.get_update_flags())
1692 *   {}
1693 *  
1694 *   FEValues<dim> fe_values;
1695 *   FEInterfaceValues<dim> fe_interface_values;
1696 *   };
1697 *  
1698 *  
1699 *  
1700 *   struct CopyData
1701 *   {
1702 *   CopyData()
1703 *   : cell_index(numbers::invalid_unsigned_int)
1704 *   , value(0.)
1705 *   {}
1706 *  
1707 *   struct FaceData
1708 *   {
1709 *   unsigned int cell_indices[2];
1710 *   double values[2];
1711 *   };
1712 *  
1713 *   unsigned int cell_index;
1714 *   double value;
1715 *   std::vector<FaceData> face_data;
1716 *   };
1717 *  
1718 *  
1719 *   template <int dim, int degree>
1720 *   void LaplaceProblem<dim, degree>::estimate()
1721 *   {
1722 *   TimerOutput::Scope timing(computing_timer, "Estimate");
1723 *  
1724 *   VectorType temp_solution;
1725 *   temp_solution.reinit(locally_owned_dofs,
1726 *   locally_relevant_dofs,
1727 *   mpi_communicator);
1728 *   temp_solution = solution;
1729 *  
1730 *   const Coefficient<dim> coefficient;
1731 *  
1732 *   estimated_error_square_per_cell.reinit(triangulation.n_active_cells());
1733 *  
1734 *   using Iterator = typename DoFHandler<dim>::active_cell_iterator;
1735 *  
1736 * @endcode
1737 *
1738 * Assembler for cell residual @f$h^2 \| f + \epsilon \triangle u \|_K^2@f$
1739 *
1740 * @code
1741 *   auto cell_worker = [&](const Iterator &cell,
1742 *   ScratchData<dim> &scratch_data,
1743 *   CopyData &copy_data) {
1744 *   FEValues<dim> &fe_values = scratch_data.fe_values;
1745 *   fe_values.reinit(cell);
1746 *  
1747 *   RightHandSide<dim> rhs;
1748 *   const double rhs_value = rhs.value(cell->center());
1749 *  
1750 *   const double nu = coefficient.value(cell->center());
1751 *  
1752 *   std::vector<Tensor<2, dim>> hessians(fe_values.n_quadrature_points);
1753 *   fe_values.get_function_hessians(temp_solution, hessians);
1754 *  
1755 *   copy_data.cell_index = cell->active_cell_index();
1756 *  
1757 *   double residual_norm_square = 0.;
1758 *   for (unsigned k = 0; k < fe_values.n_quadrature_points; ++k)
1759 *   {
1760 *   const double residual = (rhs_value + nu * trace(hessians[k]));
1761 *   residual_norm_square += residual * residual * fe_values.JxW(k);
1762 *   }
1763 *  
1764 *   copy_data.value =
1765 *   cell->diameter() * cell->diameter() * residual_norm_square;
1766 *   };
1767 *  
1768 * @endcode
1769 *
1770 * Assembler for face term @f$\sum_F h_F \| \jump{\epsilon \nabla u \cdot n}
1771 * \|_F^2@f$
1772 *
1773 * @code
1774 *   auto face_worker = [&](const Iterator &cell,
1775 *   const unsigned int &f,
1776 *   const unsigned int &sf,
1777 *   const Iterator &ncell,
1778 *   const unsigned int &nf,
1779 *   const unsigned int &nsf,
1780 *   ScratchData<dim> &scratch_data,
1781 *   CopyData &copy_data) {
1782 *   FEInterfaceValues<dim> &fe_interface_values =
1783 *   scratch_data.fe_interface_values;
1784 *   fe_interface_values.reinit(cell, f, sf, ncell, nf, nsf);
1785 *  
1786 *   copy_data.face_data.emplace_back();
1787 *   CopyData::FaceData &copy_data_face = copy_data.face_data.back();
1788 *  
1789 *   copy_data_face.cell_indices[0] = cell->active_cell_index();
1790 *   copy_data_face.cell_indices[1] = ncell->active_cell_index();
1791 *  
1792 *   const double coeff1 = coefficient.value(cell->center());
1793 *   const double coeff2 = coefficient.value(ncell->center());
1794 *  
1795 *   std::vector<Tensor<1, dim>> grad_u[2];
1796 *  
1797 *   for (unsigned int i = 0; i < 2; ++i)
1798 *   {
1799 *   grad_u[i].resize(fe_interface_values.n_quadrature_points);
1800 *   fe_interface_values.get_fe_face_values(i).get_function_gradients(
1801 *   temp_solution, grad_u[i]);
1802 *   }
1803 *  
1804 *   double jump_norm_square = 0.;
1805 *  
1806 *   for (unsigned int qpoint = 0;
1807 *   qpoint < fe_interface_values.n_quadrature_points;
1808 *   ++qpoint)
1809 *   {
1810 *   const double jump = coeff1 * grad_u[0][qpoint] *
1811 *   fe_interface_values.normal_vector(qpoint) -
1812 *   coeff2 * grad_u[1][qpoint] *
1813 *   fe_interface_values.normal_vector(qpoint);
1814 *  
1815 *   jump_norm_square += jump * jump * fe_interface_values.JxW(qpoint);
1816 *   }
1817 *  
1818 *   const double h = cell->face(f)->measure();
1819 *   copy_data_face.values[0] = 0.5 * h * jump_norm_square;
1820 *   copy_data_face.values[1] = copy_data_face.values[0];
1821 *   };
1822 *  
1823 *   auto copier = [&](const CopyData &copy_data) {
1824 *   if (copy_data.cell_index != numbers::invalid_unsigned_int)
1825 *   estimated_error_square_per_cell[copy_data.cell_index] +=
1826 *   copy_data.value;
1827 *  
1828 *   for (const auto &cdf : copy_data.face_data)
1829 *   for (unsigned int j = 0; j < 2; ++j)
1830 *   estimated_error_square_per_cell[cdf.cell_indices[j]] += cdf.values[j];
1831 *   };
1832 *  
1833 *   const unsigned int n_gauss_points = degree + 1;
1834 *   ScratchData<dim> scratch_data(mapping,
1835 *   fe,
1836 *   n_gauss_points,
1837 *   update_hessians | update_quadrature_points |
1838 *   update_JxW_values,
1839 *   update_values | update_gradients |
1840 *   update_JxW_values | update_normal_vectors);
1841 *   CopyData copy_data;
1842 *  
1843 * @endcode
1844 *
1845 * We need to assemble each interior face once but we need to make sure that
1846 * both processes assemble the face term between a locally owned and a ghost
1847 * cell. This is achieved by setting the
1848 * MeshWorker::assemble_ghost_faces_both flag. We need to do this, because
1849 * we do not communicate the error estimator contributions here.
1850 *
1851 * @code
1852 *   MeshWorker::mesh_loop(dof_handler.begin_active(),
1853 *   dof_handler.end(),
1854 *   cell_worker,
1855 *   copier,
1856 *   scratch_data,
1857 *   copy_data,
1858 *   MeshWorker::assemble_own_cells |
1859 *   MeshWorker::assemble_ghost_faces_both |
1860 *   MeshWorker::assemble_own_interior_faces_once,
1861 *   /*boundary_worker=*/nullptr,
1862 *   face_worker);
1863 *  
1864 *   const double global_error_estimate =
1865 *   std::sqrt(Utilities::MPI::sum(estimated_error_square_per_cell.l1_norm(),
1866 *   mpi_communicator));
1867 *   pcout << " Global error estimate: " << global_error_estimate
1868 *   << std::endl;
1869 *   }
1870 *  
1871 *  
1872 * @endcode
1873 *
1874 *
1875 * <a name="step_50-LaplaceProblemrefine_grid"></a>
1876 * <h4>LaplaceProblem::refine_grid()</h4>
1877 *
1878
1879 *
1880 * We use the cell-wise estimator stored in the vector @p estimate_vector and
1881 * refine a fixed number of cells (chosen here to roughly double the number of
1882 * DoFs in each step).
1883 *
1884 * @code
1885 *   template <int dim, int degree>
1886 *   void LaplaceProblem<dim, degree>::refine_grid()
1887 *   {
1888 *   TimerOutput::Scope timing(computing_timer, "Refine grid");
1889 *  
1890 *   const double refinement_fraction = 1. / (std::pow(2.0, dim) - 1.);
1891 *   parallel::distributed::GridRefinement::refine_and_coarsen_fixed_number(
1892 *   triangulation, estimated_error_square_per_cell, refinement_fraction, 0.0);
1893 *  
1894 *   triangulation.execute_coarsening_and_refinement();
1895 *   }
1896 *  
1897 *  
1898 * @endcode
1899 *
1900 *
1901 * <a name="step_50-LaplaceProblemoutput_results"></a>
1902 * <h4>LaplaceProblem::output_results()</h4>
1903 *
1904
1905 *
1906 * The output_results() function is similar to the ones found in many of the
1907 * tutorials (see @ref step_40 "step-40" for example).
1908 *
1909 * @code
1910 *   template <int dim, int degree>
1911 *   void LaplaceProblem<dim, degree>::output_results(const unsigned int cycle)
1912 *   {
1913 *   TimerOutput::Scope timing(computing_timer, "Output results");
1914 *  
1915 *   VectorType temp_solution;
1916 *   temp_solution.reinit(locally_owned_dofs,
1917 *   locally_relevant_dofs,
1918 *   mpi_communicator);
1919 *   temp_solution = solution;
1920 *  
1921 *   DataOut<dim> data_out;
1922 *   data_out.attach_dof_handler(dof_handler);
1923 *   data_out.add_data_vector(temp_solution, "solution");
1924 *  
1925 *   Vector<float> subdomain(triangulation.n_active_cells());
1926 *   for (unsigned int i = 0; i < subdomain.size(); ++i)
1927 *   subdomain(i) = triangulation.locally_owned_subdomain();
1928 *   data_out.add_data_vector(subdomain, "subdomain");
1929 *  
1930 *   Vector<float> level(triangulation.n_active_cells());
1931 *   for (const auto &cell : triangulation.active_cell_iterators())
1932 *   level(cell->active_cell_index()) = cell->level();
1933 *   data_out.add_data_vector(level, "level");
1934 *  
1935 *   if (estimated_error_square_per_cell.size() > 0)
1936 *   data_out.add_data_vector(estimated_error_square_per_cell,
1937 *   "estimated_error_square_per_cell");
1938 *  
1939 *   data_out.build_patches();
1940 *  
1941 *   const std::string pvtu_filename = data_out.write_vtu_with_pvtu_record(
1942 *   "", "solution", cycle, mpi_communicator, 2 /*n_digits*/, 1 /*n_groups*/);
1943 *  
1944 *   pcout << " Wrote " << pvtu_filename << std::endl;
1945 *   }
1946 *  
1947 *  
1948 * @endcode
1949 *
1950 *
1951 * <a name="step_50-LaplaceProblemrun"></a>
1952 * <h4>LaplaceProblem::run()</h4>
1953 *
1954
1955 *
1956 * As in most tutorials, this function calls the various functions defined
1957 * above to set up, assemble, solve, and output the results.
1958 *
1959 * @code
1960 *   template <int dim, int degree>
1961 *   void LaplaceProblem<dim, degree>::run()
1962 *   {
1963 *   for (unsigned int cycle = 0; cycle < settings.n_steps; ++cycle)
1964 *   {
1965 *   pcout << "Cycle " << cycle << ':' << std::endl;
1966 *   if (cycle > 0)
1967 *   refine_grid();
1968 *  
1969 *   pcout << " Number of active cells: "
1970 *   << triangulation.n_global_active_cells();
1971 *  
1972 * @endcode
1973 *
1974 * We only output level cell data for the GMG methods (same with DoF
1975 * data below). Note that the partition efficiency is irrelevant for AMG
1976 * since the level hierarchy is not distributed or used during the
1977 * computation.
1978 *
1979 * @code
1980 *   if (settings.solver == Settings::gmg_mf ||
1981 *   settings.solver == Settings::gmg_mb)
1982 *   pcout << " (" << triangulation.n_global_levels() << " global levels)"
1983 *   << std::endl
1984 *   << " Partition efficiency: "
1985 *   << 1.0 / MGTools::workload_imbalance(triangulation);
1986 *   pcout << std::endl;
1987 *  
1988 *   setup_system();
1989 *  
1990 * @endcode
1991 *
1992 * Only set up the multilevel hierarchy for GMG.
1993 *
1994 * @code
1995 *   if (settings.solver == Settings::gmg_mf ||
1996 *   settings.solver == Settings::gmg_mb)
1997 *   setup_multigrid();
1998 *  
1999 *   pcout << " Number of degrees of freedom: " << dof_handler.n_dofs();
2000 *   if (settings.solver == Settings::gmg_mf ||
2001 *   settings.solver == Settings::gmg_mb)
2002 *   {
2003 *   pcout << " (by level: ";
2004 *   for (unsigned int level = 0;
2005 *   level < triangulation.n_global_levels();
2006 *   ++level)
2007 *   pcout << dof_handler.n_dofs(level)
2008 *   << (level == triangulation.n_global_levels() - 1 ? ")" :
2009 *   ", ");
2010 *   }
2011 *   pcout << std::endl;
2012 *  
2013 * @endcode
2014 *
2015 * For the matrix-free method, we only assemble the right-hand side.
2016 * For both matrix-based methods, we assemble both active matrix and
2017 * right-hand side, and only assemble the multigrid matrices for
2018 * matrix-based GMG.
2019 *
2020 * @code
2021 *   if (settings.solver == Settings::gmg_mf)
2022 *   assemble_rhs();
2023 *   else /*gmg_mb or amg*/
2024 *   {
2025 *   assemble_system();
2026 *   if (settings.solver == Settings::gmg_mb)
2027 *   assemble_multigrid();
2028 *   }
2029 *  
2030 *   solve();
2031 *   estimate();
2032 *  
2033 *   if (settings.output)
2034 *   output_results(cycle);
2035 *  
2036 *   computing_timer.print_summary();
2037 *   computing_timer.reset();
2038 *   }
2039 *   }
2040 *   } // namespace Step50
2041 *  
2042 * @endcode
2043 *
2044 *
2045 * <a name="step_50-Themainfunction"></a>
2046 * <h3>The main() function</h3>
2047 *
2048
2049 *
2050 * This is a similar main function to @ref step_40 "step-40", with the exception that
2051 * we require the user to pass a .prm file as a sole command line
2052 * argument (see @ref step_29 "step-29" and the documentation of the ParameterHandler
2053 * class for a complete discussion of parameter files).
2054 *
2055 * @code
2056 *   int main(int argc, char *argv[])
2057 *   {
2058 *   using namespace dealii;
2059 *   using namespace Step50;
2060 *   Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
2061 *  
2062 *   Settings settings;
2063 *   if (!settings.try_parse((argc > 1) ? (argv[1]) : ""))
2064 *   return 0;
2065 *  
2066 *   try
2067 *   {
2068 *   constexpr unsigned int fe_degree = 2;
2069 *  
2070 *   switch (settings.dimension)
2071 *   {
2072 *   case 2:
2073 *   {
2074 *   LaplaceProblem<2, fe_degree> test(settings);
2075 *   test.run();
2076 *  
2077 *   break;
2078 *   }
2079 *  
2080 *   case 3:
2081 *   {
2082 *   LaplaceProblem<3, fe_degree> test(settings);
2083 *   test.run();
2084 *  
2085 *   break;
2086 *   }
2087 *  
2088 *   default:
2089 *   DEAL_II_NOT_IMPLEMENTED();
2090 *   }
2091 *   }
2092 *   catch (std::exception &exc)
2093 *   {
2094 *   std::cerr << std::endl
2095 *   << std::endl
2096 *   << "----------------------------------------------------"
2097 *   << std::endl;
2098 *   std::cerr << "Exception on processing: " << std::endl
2099 *   << exc.what() << std::endl
2100 *   << "Aborting!" << std::endl
2101 *   << "----------------------------------------------------"
2102 *   << std::endl;
2103 *   MPI_Abort(MPI_COMM_WORLD, 1);
2104 *   return 1;
2105 *   }
2106 *   catch (...)
2107 *   {
2108 *   std::cerr << std::endl
2109 *   << std::endl
2110 *   << "----------------------------------------------------"
2111 *   << std::endl;
2112 *   std::cerr << "Unknown exception!" << std::endl
2113 *   << "Aborting!" << std::endl
2114 *   << "----------------------------------------------------"
2115 *   << std::endl;
2116 *   MPI_Abort(MPI_COMM_WORLD, 2);
2117 *   return 1;
2118 *   }
2119 *  
2120 *   return 0;
2121 *   }
2122 * @endcode
2123<a name="step_50-Results"></a><h1>Results</h1>
2124
2125
2126When you run the program using the following command
2127@code
2128mpirun -np 16 ./step-50 gmg_mf_2d.prm
2129@endcode
2130the screen output should look like the following:
2131@code
2132Cycle 0:
2133 Number of active cells: 12 (2 global levels)
2134 Partition efficiency: 0.1875
2135 Number of degrees of freedom: 65 (by level: 21, 65)
2136 Number of CG iterations: 10
2137 Global error estimate: 0.355373
2138 Wrote solution_00.pvtu
2139
2140
2141+---------------------------------------------+------------+------------+
2142| Total wallclock time elapsed since start | 0.0163s | |
2143| | | |
2144| Section | no. calls | wall time | % of total |
2145+---------------------------------+-----------+------------+------------+
2146| Assemble right-hand side | 1 | 0.000374s | 2.3% |
2147| Estimate | 1 | 0.000724s | 4.4% |
2148| Output results | 1 | 0.00277s | 17% |
2149| Setup | 1 | 0.00225s | 14% |
2150| Setup multigrid | 1 | 0.00181s | 11% |
2151| Solve | 1 | 0.00364s | 22% |
2152| Solve: 1 multigrid V-cycle | 1 | 0.000354s | 2.2% |
2153| Solve: CG | 1 | 0.00151s | 9.3% |
2154| Solve: Preconditioner setup | 1 | 0.00125s | 7.7% |
2155+---------------------------------+-----------+------------+------------+
2156
2157Cycle 1:
2158 Number of active cells: 24 (3 global levels)
2159 Partition efficiency: 0.276786
2160 Number of degrees of freedom: 139 (by level: 21, 65, 99)
2161 Number of CG iterations: 10
2162 Global error estimate: 0.216726
2163 Wrote solution_01.pvtu
2164
2165
2166+---------------------------------------------+------------+------------+
2167| Total wallclock time elapsed since start | 0.0169s | |
2168| | | |
2169| Section | no. calls | wall time | % of total |
2170+---------------------------------+-----------+------------+------------+
2171| Assemble right-hand side | 1 | 0.000309s | 1.8% |
2172| Estimate | 1 | 0.00156s | 9.2% |
2173| Output results | 1 | 0.00222s | 13% |
2174| Refine grid | 1 | 0.00278s | 16% |
2175| Setup | 1 | 0.00196s | 12% |
2176| Setup multigrid | 1 | 0.0023s | 14% |
2177| Solve | 1 | 0.00565s | 33% |
2178| Solve: 1 multigrid V-cycle | 1 | 0.000349s | 2.1% |
2179| Solve: CG | 1 | 0.00285s | 17% |
2180| Solve: Preconditioner setup | 1 | 0.00195s | 12% |
2181+---------------------------------+-----------+------------+------------+
2182
2183Cycle 2:
2184 Number of active cells: 51 (4 global levels)
2185 Partition efficiency: 0.41875
2186 Number of degrees of freedom: 245 (by level: 21, 65, 225, 25)
2187 Number of CG iterations: 11
2188 Global error estimate: 0.112098
2189 Wrote solution_02.pvtu
2190
2191
2192+---------------------------------------------+------------+------------+
2193| Total wallclock time elapsed since start | 0.0183s | |
2194| | | |
2195| Section | no. calls | wall time | % of total |
2196+---------------------------------+-----------+------------+------------+
2197| Assemble right-hand side | 1 | 0.000274s | 1.5% |
2198| Estimate | 1 | 0.00127s | 6.9% |
2199| Output results | 1 | 0.00227s | 12% |
2200| Refine grid | 1 | 0.0024s | 13% |
2201| Setup | 1 | 0.00191s | 10% |
2202| Setup multigrid | 1 | 0.00295s | 16% |
2203| Solve | 1 | 0.00702s | 38% |
2204| Solve: 1 multigrid V-cycle | 1 | 0.000398s | 2.2% |
2205| Solve: CG | 1 | 0.00376s | 21% |
2206| Solve: Preconditioner setup | 1 | 0.00238s | 13% |
2207+---------------------------------+-----------+------------+------------+
2208.
2209.
2210.
2211@endcode
2212Here, the timing of the `solve()` function is split up in 3 parts: setting
2213up the multigrid preconditioner, execution of a single multigrid V-cycle, and
2214the CG solver. The V-cycle that is timed is unnecessary for the overall solve
2215and only meant to give an insight at the different costs for AMG and GMG.
2216Also it should be noted that when using the AMG solver, "Workload imbalance"
2217is not included in the output since the hierarchy of coarse meshes is not
2218required.
2219
2220All results in this section are gathered on Intel Xeon Platinum 8280 (Cascade
2221Lake) nodes which have 56 cores and 192GB per node and support AVX-512 instructions,
2222allowing for vectorization over 8 doubles (vectorization used only in the matrix-free
2223computations). The code is compiled using gcc 7.1.0 with intel-mpi 17.0.3. Trilinos
222412.10.1 is used for the matrix-based GMG/AMG computations.
2225
2226We can then gather a variety of information by calling the program
2227with the input files that are provided in the directory in which
2228@ref step_50 "step-50" is located. Using these, and adjusting the number of mesh
2229refinement steps, we can produce information about how well the
2230program scales.
2231
2232The following table gives weak scaling timings for this program on up to 256M DoFs
2233and 7,168 processors. (Recall that weak scaling keeps the number of
2234degrees of freedom per processor constant while increasing the number of
2235processors; i.e., it considers larger and larger problems.)
2236Here, @f$\mathbb{E}@f$ is the partition efficiency from the
2237 introduction (also equal to 1.0/workload imbalance), "Setup" is a combination
2238of setup, setup multigrid, assemble, and assemble multigrid from the timing blocks,
2239and "Prec" is the preconditioner setup. Ideally all times would stay constant
2240over each problem size for the individual solvers, but since the partition
2241efficiency decreases from 0.371 to 0.161 from largest to smallest problem size,
2242we expect to see an approximately @f$0.371/0.161=2.3@f$ times increase in timings
2243for GMG. This is, in fact, pretty close to what we really get:
2244
2245<table align="center" class="doxtable">
2246<tr>
2247 <th colspan="4"></th>
2248 <th></th>
2249 <th colspan="4">MF-GMG</th>
2250 <th></th>
2251 <th colspan="4">MB-GMG</th>
2252 <th></th>
2253 <th colspan="4">AMG</th>
2254</tr>
2255<tr>
2256 <th align="right">Procs</th>
2257 <th align="right">Cycle</th>
2258 <th align="right">DoFs</th>
2259 <th align="right">@f$\mathbb{E}@f$</th>
2260 <th></th>
2261 <th align="right">Setup</th>
2262 <th align="right">Prec</th>
2263 <th align="right">Solve</th>
2264 <th align="right">Total</th>
2265 <th></th>
2266 <th align="right">Setup</th>
2267 <th align="right">Prec</th>
2268 <th align="right">Solve</th>
2269 <th align="right">Total</th>
2270 <th></th>
2271 <th align="right">Setup</th>
2272 <th align="right">Prec</th>
2273 <th align="right">Solve</th>
2274 <th align="right">Total</th>
2275</tr>
2276<tr>
2277 <td align="right">112</th>
2278 <td align="right">13</th>
2279 <td align="right">4M</th>
2280 <td align="right">0.37</th>
2281 <td></td>
2282 <td align="right">0.742</th>
2283 <td align="right">0.393</th>
2284 <td align="right">0.200</th>
2285 <td align="right">1.335</th>
2286 <td></td>
2287 <td align="right">1.714</th>
2288 <td align="right">2.934</th>
2289 <td align="right">0.716</th>
2290 <td align="right">5.364</th>
2291 <td></td>
2292 <td align="right">1.544</th>
2293 <td align="right">0.456</th>
2294 <td align="right">1.150</th>
2295 <td align="right">3.150</th>
2296</tr>
2297<tr>
2298 <td align="right">448</th>
2299 <td align="right">15</th>
2300 <td align="right">16M</th>
2301 <td align="right">0.29</th>
2302 <td></td>
2303 <td align="right">0.884</th>
2304 <td align="right">0.535</th>
2305 <td align="right">0.253</th>
2306 <td align="right">1.672</th>
2307 <td></td>
2308 <td align="right">1.927</th>
2309 <td align="right">3.776</th>
2310 <td align="right">1.190</th>
2311 <td align="right">6.893</th>
2312 <td></td>
2313 <td align="right">1.544</th>
2314 <td align="right">0.456</th>
2315 <td align="right">1.150</th>
2316 <td align="right">3.150</th>
2317</tr>
2318<tr>
2319 <td align="right">1,792</th>
2320 <td align="right">17</th>
2321 <td align="right">65M</th>
2322 <td align="right">0.22</th>
2323 <td></td>
2324 <td align="right">1.122</th>
2325 <td align="right">0.686</th>
2326 <td align="right">0.309</th>
2327 <td align="right">2.117</th>
2328 <td></td>
2329 <td align="right">2.171</th>
2330 <td align="right">4.862</th>
2331 <td align="right">1.660</th>
2332 <td align="right">8.693</th>
2333 <td></td>
2334 <td align="right">1.654</th>
2335 <td align="right">0.546</th>
2336 <td align="right">1.460</th>
2337 <td align="right">3.660</th>
2338</tr>
2339<tr>
2340 <td align="right">7,168</th>
2341 <td align="right">19</th>
2342 <td align="right">256M</th>
2343 <td align="right">0.16</th>
2344 <td></td>
2345 <td align="right">1.214</th>
2346 <td align="right">0.893</th>
2347 <td align="right">0.521</th>
2348 <td align="right">2.628</th>
2349 <td></td>
2350 <td align="right">2.386</th>
2351 <td align="right">7.260</th>
2352 <td align="right">2.560</th>
2353 <td align="right">12.206</th>
2354 <td></td>
2355 <td align="right">1.844</th>
2356 <td align="right">1.010</th>
2357 <td align="right">1.890</th>
2358 <td align="right">4.744</th>
2359</tr>
2360</table>
2361
2362On the other hand, the algebraic multigrid in the last set of columns
2363is relatively unaffected by the increasing imbalance of the mesh
2364hierarchy (because it doesn't use the mesh hierarchy) and the growth
2365in time is rather driven by other factors that are well documented in
2366the literature (most notably that the algorithmic complexity of
2367some parts of algebraic multigrid methods appears to be @f${\cal O}(N
2368\log N)@f$ instead of @f${\cal O}(N)@f$ for geometric multigrid).
2369
2370The upshort of the table above is that the matrix-free geometric multigrid
2371method appears to be the fastest approach to solving this equation if
2372not by a huge margin. Matrix-based methods, on the other hand, are
2373consistently the worst.
2374
2375The following figure provides strong scaling results for each method, i.e.,
2376we solve the same problem on more and more processors. Specifically,
2377we consider the problems after 16 mesh refinement cycles
2378(32M DoFs) and 19 cycles (256M DoFs), on between 56 to 28,672 processors:
2379
2380<img width="600px" src="https://dealii.org/images/steps/developer/step-50-strong-scaling.png" alt="">
2381
2382While the matrix-based GMG solver and AMG scale similarly and have a
2383similar time to solution (at least as long as there is a substantial
2384number of unknowns per processor -- say, several 10,000), the
2385matrix-free GMG solver scales much better and solves the finer problem
2386in roughly the same time as the AMG solver for the coarser mesh with
2387only an eighth of the number of processors. Conversely, it can solve the
2388same problem on the same number of processors in about one eighth the
2389time.
2390
2391
2392<a name="step_50-Possibilitiesforextensions"></a><h3> Possibilities for extensions </h3>
2393
2394
2395<a name="step_50-Testingconvergenceandhigherorderelements"></a><h4> Testing convergence and higher order elements </h4>
2396
2397
2398The finite element degree is currently hard-coded as 2, see the template
2399arguments of the main class. It is easy to change. To test, it would be
2400interesting to switch to a test problem with a reference solution. This way,
2401you can compare error rates.
2402
2403<a name="step_50-Coarsesolver"></a><h4> Coarse solver </h4>
2404
2405
2406A more interesting example would involve a more complicated coarse mesh (see
2407@ref step_49 "step-49" for inspiration). The issue in that case is that the coarsest
2408level of the mesh hierarchy is actually quite large, and one would
2409have to think about ways to solve the coarse level problem
2410efficiently. (This is not an issue for algebraic multigrid methods
2411because they would just continue to build coarser and coarser levels
2412of the matrix, regardless of their geometric origin.)
2413
2414In the program here, we simply solve the coarse level problem with a
2415Conjugate Gradient method without any preconditioner. That is acceptable
2416if the coarse problem is really small -- for example, if the coarse
2417mesh had a single cell, then the coarse mesh problems has a @f$9\times 9@f$
2418matrix in 2d, and a @f$27\times 27@f$ matrix in 3d; for the coarse mesh we
2419use on the @f$L@f$-shaped domain of the current program, these sizes are
2420@f$21\times 21@f$ in 2d and @f$117\times 117@f$ in 3d. But if the coarse mesh
2421consists of hundreds or thousands of cells, this approach will no
2422longer work and might start to dominate the overall run-time of each V-cycle.
2423A common approach is then to solve the coarse mesh problem using an
2424algebraic multigrid preconditioner; this would then, however, require
2425assembling the coarse matrix (even for the matrix-free version) as
2426input to the AMG implementation.
2427 *
2428 *
2429<a name="step_50-PlainProg"></a>
2430<h1> The plain program</h1>
2431@include "step-50.cc"
2432*/
*  *  for(const auto &cell :triangulation.active_cell_iterators())
*  *  int main(int argc, char **argv)
*const unsigned int n_steps
*  x_component_mask set(0, true)
*  *  *  struct InterferenceTaperTransform *  
Definition fe_q.h:552
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
Definition point.h:111
#define DEAL_II_NOT_IMPLEMENTED()
unsigned int level
Definition grid_out.cc:4642
#define AssertThrow(cond, exc)
void make_hanging_node_constraints(const DoFHandler< dim, spacedim > &dof_handler, AffineConstraints< number > &constraints)
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity_pattern, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
@ update_values
Shape function values.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
std::vector< value_type > split(const typename ::Triangulation< dim, spacedim >::cell_iterator &parent, const value_type parent_value)
IndexSet extract_locally_relevant_dofs(const DoFHandler< dim, spacedim > &dof_handler)
IndexSet extract_locally_relevant_level_dofs(const DoFHandler< dim, spacedim > &dof_handler, const unsigned int level)
void hyper_L(Triangulation< dim > &tria, const double left=-1., const double right=1., const bool colorize=false)
void scale(const double scaling_factor, Triangulation< dim, spacedim > &triangulation)
@ matrix
Contents is actually a matrix.
constexpr char V
constexpr char A
constexpr types::blas_int one
PETScWrappers::PreconditionBoomerAMG PreconditionAMG
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
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.)
Definition advection.h:72
void make_interface_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, const MGConstrainedDoFs &mg_constrained_dofs, SparsityPatternBase &sparsity, const unsigned int level)
Definition mg_tools.cc:1003
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity, const unsigned int level, const AffineConstraints< number > &constraints={}, const bool keep_constrained_dofs=true)
Definition mg_tools.cc:575
void compute_diagonal(const MatrixFree< dim, Number, VectorizedArrayType > &matrix_free, VectorType &diagonal_global, const std::function< void(FEEvaluation< dim, fe_degree, n_q_points_1d, n_components, Number, VectorizedArrayType > &)> &cell_operation, const unsigned int dof_handler_index=0, const unsigned int quadrature_index=0, const unsigned int first_selected_component=0, const unsigned int first_vector_component=0)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition utilities.cc:210
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
*  *  if(update_pressure &update_flags) *  compute_pressure(constitutive_request
*  *  *  RotationFunction< dim, Number >::RotationFunction Number(dim)
void distribute_sparsity_pattern(DynamicSparsityPattern &dsp, const IndexSet &locally_owned_rows, const MPI_Comm mpi_comm, const IndexSet &locally_relevant_rows)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
std::string compress(const std::string &input)
Definition utilities.cc:381
void interpolate_boundary_values(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const std::map< types::boundary_id, const Function< spacedim, number > * > &function_map, std::map< types::global_dof_index, number > &boundary_values, const ComponentMask &component_mask={})
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)
Definition tria.cc:15808
int(&) functions(const void *v1, const void *v2)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
Definition loop.h:68
Definition types.h:30
unsigned int boundary_id
Definition types.h:159
TasksParallelScheme tasks_parallel_scheme