deal.II version GIT relicensing-1721-g8100761196 2024-08-31 12:30:00+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-63.h
Go to the documentation of this file.
1
518 *   ParameterHandler prm;
519 *  
520 *   prm.declare_entry("Epsilon",
521 *   "0.005",
522 *   Patterns::Double(0),
523 *   "Diffusion parameter");
524 *  
525 *   prm.declare_entry("Fe degree",
526 *   "1",
527 *   Patterns::Integer(1),
528 *   "Finite Element degree");
529 *   prm.declare_entry("Smoother type",
530 *   "block SOR",
531 *   Patterns::Selection("SOR|Jacobi|block SOR|block Jacobi"),
532 *   "Select smoother: SOR|Jacobi|block SOR|block Jacobi");
533 *   prm.declare_entry("Smoothing steps",
534 *   "2",
535 *   Patterns::Integer(1),
536 *   "Number of smoothing steps");
537 *   prm.declare_entry(
538 *   "DoF renumbering",
539 *   "downstream",
540 *   Patterns::Selection("none|downstream|upstream|random"),
541 *   "Select DoF renumbering: none|downstream|upstream|random");
542 *   prm.declare_entry("With streamline diffusion",
543 *   "true",
544 *   Patterns::Bool(),
545 *   "Enable streamline diffusion stabilization: true|false");
546 *   prm.declare_entry("Output",
547 *   "true",
548 *   Patterns::Bool(),
549 *   "Generate graphical output: true|false");
550 *  
551 *   /* ...and then try to read their values from the input file: */
552 *   if (prm_filename.empty())
553 *   {
554 *   prm.print_parameters(std::cout, ParameterHandler::PRM);
555 *   AssertThrow(
556 *   false, ExcMessage("Please pass a .prm file as the first argument!"));
557 *   }
558 *  
559 *   prm.parse_input(prm_filename);
560 *  
561 *   epsilon = prm.get_double("Epsilon");
562 *   fe_degree = prm.get_integer("Fe degree");
563 *   smoother_type = prm.get("Smoother type");
564 *   smoothing_steps = prm.get_integer("Smoothing steps");
565 *  
566 *   const std::string renumbering = prm.get("DoF renumbering");
567 *   if (renumbering == "none")
568 *   dof_renumbering = DoFRenumberingStrategy::none;
569 *   else if (renumbering == "downstream")
570 *   dof_renumbering = DoFRenumberingStrategy::downstream;
571 *   else if (renumbering == "upstream")
572 *   dof_renumbering = DoFRenumberingStrategy::upstream;
573 *   else if (renumbering == "random")
574 *   dof_renumbering = DoFRenumberingStrategy::random;
575 *   else
576 *   AssertThrow(false,
577 *   ExcMessage("The <DoF renumbering> parameter has "
578 *   "an invalid value."));
579 *  
580 *   with_streamline_diffusion = prm.get_bool("With streamline diffusion");
581 *   output = prm.get_bool("Output");
582 *   }
583 *  
584 *  
585 * @endcode
586 *
587 *
588 * <a name="step_63-Cellpermutations"></a>
589 * <h3>Cell permutations</h3>
590 *
591
592 *
593 * The ordering in which cells and degrees of freedom are traversed
594 * will play a role in the speed of convergence for multiplicative
595 * methods. Here we define functions which return a specific ordering
596 * of cells to be used by the block smoothers.
597 *
598
599 *
600 * For each type of cell ordering, we define a function for the
601 * active mesh and one for a level mesh (i.e., for the cells at one
602 * level of a multigrid hierarchy). While the only reordering
603 * necessary for solving the system will be on the level meshes, we
604 * include the active reordering for visualization purposes in
605 * output_results().
606 *
607
608 *
609 * For the two downstream ordering functions, we first create an
610 * array with all of the relevant cells that we then sort in
611 * downstream direction using a "comparator" object. The output of
612 * the functions is then simply an array of the indices of the cells
613 * in the just computed order.
614 *
615 * @code
616 *   template <int dim>
617 *   std::vector<unsigned int>
618 *   create_downstream_cell_ordering(const DoFHandler<dim> &dof_handler,
619 *   const Tensor<1, dim> direction,
620 *   const unsigned int level)
621 *   {
622 *   std::vector<typename DoFHandler<dim>::level_cell_iterator> ordered_cells;
623 *   ordered_cells.reserve(dof_handler.get_triangulation().n_cells(level));
624 *   for (const auto &cell : dof_handler.cell_iterators_on_level(level))
625 *   ordered_cells.push_back(cell);
626 *  
627 *   const DoFRenumbering::
628 *   CompareDownstream<typename DoFHandler<dim>::level_cell_iterator, dim>
629 *   comparator(direction);
630 *   std::sort(ordered_cells.begin(), ordered_cells.end(), comparator);
631 *  
632 *   std::vector<unsigned> ordered_indices;
633 *   ordered_indices.reserve(dof_handler.get_triangulation().n_cells(level));
634 *  
635 *   for (const auto &cell : ordered_cells)
636 *   ordered_indices.push_back(cell->index());
637 *  
638 *   return ordered_indices;
639 *   }
640 *  
641 *  
642 *  
643 *   template <int dim>
644 *   std::vector<unsigned int>
645 *   create_downstream_cell_ordering(const DoFHandler<dim> &dof_handler,
646 *   const Tensor<1, dim> direction)
647 *   {
648 *   std::vector<typename DoFHandler<dim>::active_cell_iterator> ordered_cells;
649 *   ordered_cells.reserve(dof_handler.get_triangulation().n_active_cells());
650 *   for (const auto &cell : dof_handler.active_cell_iterators())
651 *   ordered_cells.push_back(cell);
652 *  
653 *   const DoFRenumbering::
654 *   CompareDownstream<typename DoFHandler<dim>::active_cell_iterator, dim>
655 *   comparator(direction);
656 *   std::sort(ordered_cells.begin(), ordered_cells.end(), comparator);
657 *  
658 *   std::vector<unsigned int> ordered_indices;
659 *   ordered_indices.reserve(dof_handler.get_triangulation().n_active_cells());
660 *  
661 *   for (const auto &cell : ordered_cells)
662 *   ordered_indices.push_back(cell->index());
663 *  
664 *   return ordered_indices;
665 *   }
666 *  
667 *  
668 * @endcode
669 *
670 * The functions that produce a random ordering are similar in
671 * spirit in that they first put information about all cells into an
672 * array. But then, instead of sorting them, they shuffle the
673 * elements randomly using the facilities C++ offers to generate
674 * random numbers. The way this is done is by iterating over all
675 * elements of the array, drawing a random number for another
676 * element before that, and then exchanging these elements. The
677 * result is a random shuffle of the elements of the array.
678 *
679 * @code
680 *   template <int dim>
681 *   std::vector<unsigned int>
682 *   create_random_cell_ordering(const DoFHandler<dim> &dof_handler,
683 *   const unsigned int level)
684 *   {
685 *   std::vector<unsigned int> ordered_cells;
686 *   ordered_cells.reserve(dof_handler.get_triangulation().n_cells(level));
687 *   for (const auto &cell : dof_handler.cell_iterators_on_level(level))
688 *   ordered_cells.push_back(cell->index());
689 *  
690 *   std::mt19937 random_number_generator;
691 *   std::shuffle(ordered_cells.begin(),
692 *   ordered_cells.end(),
693 *   random_number_generator);
694 *  
695 *   return ordered_cells;
696 *   }
697 *  
698 *  
699 *  
700 *   template <int dim>
701 *   std::vector<unsigned int>
702 *   create_random_cell_ordering(const DoFHandler<dim> &dof_handler)
703 *   {
704 *   std::vector<unsigned int> ordered_cells;
705 *   ordered_cells.reserve(dof_handler.get_triangulation().n_active_cells());
706 *   for (const auto &cell : dof_handler.active_cell_iterators())
707 *   ordered_cells.push_back(cell->index());
708 *  
709 *   std::mt19937 random_number_generator;
710 *   std::shuffle(ordered_cells.begin(),
711 *   ordered_cells.end(),
712 *   random_number_generator);
713 *  
714 *   return ordered_cells;
715 *   }
716 *  
717 *  
718 * @endcode
719 *
720 *
721 * <a name="step_63-Righthandsideandboundaryvalues"></a>
722 * <h3>Right-hand side and boundary values</h3>
723 *
724
725 *
726 * The problem solved in this tutorial is an adaptation of Ex. 3.1.3 found
727 * on pg. 118 of <a
728 * href="https://global.oup.com/academic/product/finite-elements-and-fast-iterative-solvers-9780199678808">
729 * Finite Elements and Fast Iterative Solvers: with Applications in
730 * Incompressible Fluid Dynamics by Elman, Silvester, and Wathen</a>. The
731 * main difference being that we add a hole in the center of our domain with
732 * zero Dirichlet boundary conditions.
733 *
734
735 *
736 * For a complete description, we need classes that implement the
737 * zero right-hand side first (we could of course have just used
739 *
740 * @code
741 *   template <int dim>
742 *   class RightHandSide : public Function<dim>
743 *   {
744 *   public:
745 *   virtual double value(const Point<dim> &p,
746 *   const unsigned int component = 0) const override;
747 *  
748 *   virtual void value_list(const std::vector<Point<dim>> &points,
749 *   std::vector<double> &values,
750 *   const unsigned int component = 0) const override;
751 *   };
752 *  
753 *  
754 *  
755 *   template <int dim>
756 *   double RightHandSide<dim>::value(const Point<dim> &,
757 *   const unsigned int component) const
758 *   {
759 *   Assert(component == 0, ExcIndexRange(component, 0, 1));
760 *   (void)component;
761 *  
762 *   return 0.0;
763 *   }
764 *  
765 *  
766 *  
767 *   template <int dim>
768 *   void RightHandSide<dim>::value_list(const std::vector<Point<dim>> &points,
769 *   std::vector<double> &values,
770 *   const unsigned int component) const
771 *   {
772 *   AssertDimension(values.size(), points.size());
773 *  
774 *   for (unsigned int i = 0; i < points.size(); ++i)
775 *   values[i] = RightHandSide<dim>::value(points[i], component);
776 *   }
777 *  
778 *  
779 * @endcode
780 *
781 * We also have Dirichlet boundary conditions. On a connected portion of the
782 * outer, square boundary we set the value to 1, and we set the value to 0
783 * everywhere else (including the inner, circular boundary):
784 *
785 * @code
786 *   template <int dim>
787 *   class BoundaryValues : public Function<dim>
788 *   {
789 *   public:
790 *   virtual double value(const Point<dim> &p,
791 *   const unsigned int component = 0) const override;
792 *  
793 *   virtual void value_list(const std::vector<Point<dim>> &points,
794 *   std::vector<double> &values,
795 *   const unsigned int component = 0) const override;
796 *   };
797 *  
798 *  
799 *  
800 *   template <int dim>
801 *   double BoundaryValues<dim>::value(const Point<dim> &p,
802 *   const unsigned int component) const
803 *   {
804 *   Assert(component == 0, ExcIndexRange(component, 0, 1));
805 *   (void)component;
806 *  
807 * @endcode
808 *
809 * Set boundary to 1 if @f$x=1@f$, or if @f$x>0.5@f$ and @f$y=-1@f$.
810 *
811 * @code
812 *   if (std::fabs(p[0] - 1) < 1e-8 ||
813 *   (std::fabs(p[1] + 1) < 1e-8 && p[0] >= 0.5))
814 *   {
815 *   return 1.0;
816 *   }
817 *   else
818 *   {
819 *   return 0.0;
820 *   }
821 *   }
822 *  
823 *  
824 *  
825 *   template <int dim>
826 *   void BoundaryValues<dim>::value_list(const std::vector<Point<dim>> &points,
827 *   std::vector<double> &values,
828 *   const unsigned int component) const
829 *   {
830 *   AssertDimension(values.size(), points.size());
831 *  
832 *   for (unsigned int i = 0; i < points.size(); ++i)
833 *   values[i] = BoundaryValues<dim>::value(points[i], component);
834 *   }
835 *  
836 *  
837 *  
838 * @endcode
839 *
840 *
841 * <a name="step_63-Streamlinediffusionimplementation"></a>
842 * <h3>Streamline diffusion implementation</h3>
843 *
844
845 *
846 * The streamline diffusion method has a stabilization constant that
847 * we need to be able to compute. The choice of how this parameter
848 * is computed is taken from <a
849 * href="https://link.springer.com/chapter/10.1007/978-3-540-34288-5_27">On
850 * Discontinuity-Capturing Methods for Convection-Diffusion
851 * Equations by Volker John and Petr Knobloch</a>.
852 *
853 * @code
854 *   template <int dim>
855 *   double compute_stabilization_delta(const double hk,
856 *   const double eps,
857 *   const Tensor<1, dim> dir,
858 *   const double pk)
859 *   {
860 *   const double Peclet = dir.norm() * hk / (2.0 * eps * pk);
861 *   const double coth =
862 *   (1.0 + std::exp(-2.0 * Peclet)) / (1.0 - std::exp(-2.0 * Peclet));
863 *  
864 *   return hk / (2.0 * dir.norm() * pk) * (coth - 1.0 / Peclet);
865 *   }
866 *  
867 *  
868 * @endcode
869 *
870 *
871 * <a name="step_63-codeAdvectionProblemcodeclass"></a>
872 * <h3><code>AdvectionProblem</code> class</h3>
873 *
874
875 *
876 * This is the main class of the program, and should look very similar to
877 * @ref step_16 "step-16". The major difference is that, since we are defining our multigrid
878 * smoother at runtime, we choose to define a function `create_smoother()` and
879 * a class object `mg_smoother` which is a `std::unique_ptr` to a smoother
880 * that is derived from MGSmoother. Note that for smoothers derived from
881 * RelaxationBlock, we must include a `smoother_data` object for each level.
882 * This will contain information about the cell ordering and the method of
883 * inverting cell matrices.
884 *
885
886 *
887 *
888 * @code
889 *   template <int dim>
890 *   class AdvectionProblem
891 *   {
892 *   public:
893 *   AdvectionProblem(const Settings &settings);
894 *   void run();
895 *  
896 *   private:
897 *   void setup_system();
898 *  
899 *   template <class IteratorType>
900 *   void assemble_cell(const IteratorType &cell,
901 *   ScratchData<dim> &scratch_data,
902 *   CopyData &copy_data);
903 *   void assemble_system_and_multigrid();
904 *  
905 *   void setup_smoother();
906 *  
907 *   void solve();
908 *   void refine_grid();
909 *   void output_results(const unsigned int cycle) const;
910 *  
912 *   DoFHandler<dim> dof_handler;
913 *  
914 *   const FE_Q<dim> fe;
915 *   const MappingQ<dim> mapping;
916 *  
917 *   AffineConstraints<double> constraints;
918 *  
919 *   SparsityPattern sparsity_pattern;
920 *   SparseMatrix<double> system_matrix;
921 *  
922 *   Vector<double> solution;
923 *   Vector<double> system_rhs;
924 *  
925 *   MGLevelObject<SparsityPattern> mg_sparsity_patterns;
926 *   MGLevelObject<SparsityPattern> mg_interface_sparsity_patterns;
927 *  
928 *   MGLevelObject<SparseMatrix<double>> mg_matrices;
929 *   MGLevelObject<SparseMatrix<double>> mg_interface_in;
930 *   MGLevelObject<SparseMatrix<double>> mg_interface_out;
931 *  
932 *   mg::Matrix<Vector<double>> mg_matrix;
933 *   mg::Matrix<Vector<double>> mg_interface_matrix_in;
934 *   mg::Matrix<Vector<double>> mg_interface_matrix_out;
935 *  
936 *   std::unique_ptr<MGSmoother<Vector<double>>> mg_smoother;
937 *  
938 *   using SmootherType =
940 *   using SmootherAdditionalDataType = SmootherType::AdditionalData;
942 *  
943 *   MGConstrainedDoFs mg_constrained_dofs;
944 *  
945 *   Tensor<1, dim> advection_direction;
946 *  
947 *   const Settings settings;
948 *   };
949 *  
950 *  
951 *  
952 *   template <int dim>
953 *   AdvectionProblem<dim>::AdvectionProblem(const Settings &settings)
955 *   , dof_handler(triangulation)
956 *   , fe(settings.fe_degree)
957 *   , mapping(settings.fe_degree)
958 *   , settings(settings)
959 *   {
960 *   advection_direction[0] = -std::sin(numbers::PI / 6.0);
961 *   if (dim >= 2)
962 *   advection_direction[1] = std::cos(numbers::PI / 6.0);
963 *   if (dim >= 3)
964 *   AssertThrow(false, ExcNotImplemented());
965 *   }
966 *  
967 *  
968 * @endcode
969 *
970 *
971 * <a name="step_63-codeAdvectionProblemsetup_systemcode"></a>
972 * <h4><code>AdvectionProblem::setup_system()</code></h4>
973 *
974
975 *
976 * Here we first set up the DoFHandler, AffineConstraints, and
977 * SparsityPattern objects for both active and multigrid level meshes.
978 *
979
980 *
981 * We could renumber the active DoFs with the DoFRenumbering class,
982 * but the smoothers only act on multigrid levels and as such, this
983 * would not matter for the computations. Instead, we will renumber the
984 * DoFs on each multigrid level below.
985 *
986 * @code
987 *   template <int dim>
988 *   void AdvectionProblem<dim>::setup_system()
989 *   {
990 *   const unsigned int n_levels = triangulation.n_levels();
991 *  
992 *   dof_handler.distribute_dofs(fe);
993 *  
994 *   solution.reinit(dof_handler.n_dofs());
995 *   system_rhs.reinit(dof_handler.n_dofs());
996 *  
997 *   constraints.clear();
998 *   DoFTools::make_hanging_node_constraints(dof_handler, constraints);
999 *  
1001 *   mapping, dof_handler, 0, BoundaryValues<dim>(), constraints);
1003 *   mapping, dof_handler, 1, BoundaryValues<dim>(), constraints);
1004 *   constraints.close();
1005 *  
1006 *   DynamicSparsityPattern dsp(dof_handler.n_dofs());
1007 *   DoFTools::make_sparsity_pattern(dof_handler,
1008 *   dsp,
1009 *   constraints,
1010 *   /*keep_constrained_dofs = */ false);
1011 *  
1012 *   sparsity_pattern.copy_from(dsp);
1013 *   system_matrix.reinit(sparsity_pattern);
1014 *  
1015 *   dof_handler.distribute_mg_dofs();
1016 *  
1017 * @endcode
1018 *
1019 * Having enumerated the global degrees of freedom as well as (in
1020 * the last line above) the level degrees of freedom, let us
1021 * renumber the level degrees of freedom to get a better smoother
1022 * as explained in the introduction. The first block below
1023 * renumbers DoFs on each level in downstream or upstream
1024 * direction if needed. This is only necessary for point smoothers
1025 * (SOR and Jacobi) as the block smoothers operate on cells (see
1026 * `create_smoother()`). The blocks below then also implement
1027 * random numbering.
1028 *
1029 * @code
1030 *   if (settings.smoother_type == "SOR" || settings.smoother_type == "Jacobi")
1031 *   {
1032 *   if (settings.dof_renumbering ==
1033 *   Settings::DoFRenumberingStrategy::downstream ||
1034 *   settings.dof_renumbering ==
1035 *   Settings::DoFRenumberingStrategy::upstream)
1036 *   {
1037 *   const Tensor<1, dim> direction =
1038 *   (settings.dof_renumbering ==
1039 *   Settings::DoFRenumberingStrategy::upstream ?
1040 *   -1.0 :
1041 *   1.0) *
1042 *   advection_direction;
1043 *  
1044 *   for (unsigned int level = 0; level < n_levels; ++level)
1045 *   DoFRenumbering::downstream(dof_handler,
1046 *   level,
1047 *   direction,
1048 *   /*dof_wise_renumbering = */ true);
1049 *   }
1050 *   else if (settings.dof_renumbering ==
1051 *   Settings::DoFRenumberingStrategy::random)
1052 *   {
1053 *   for (unsigned int level = 0; level < n_levels; ++level)
1054 *   DoFRenumbering::random(dof_handler, level);
1055 *   }
1056 *   else
1058 *   }
1059 *  
1060 * @endcode
1061 *
1062 * The rest of the function just sets up data structures. The last
1063 * lines of the code below is unlike the other GMG tutorials, as
1064 * it sets up both the interface in and out matrices. We need this
1065 * since our problem is non-symmetric.
1066 *
1067 * @code
1068 *   mg_constrained_dofs.clear();
1069 *   mg_constrained_dofs.initialize(dof_handler);
1070 *  
1071 *   mg_constrained_dofs.make_zero_boundary_constraints(dof_handler, {0, 1});
1072 *  
1073 *   mg_matrices.resize(0, n_levels - 1);
1074 *   mg_matrices.clear_elements();
1075 *   mg_interface_in.resize(0, n_levels - 1);
1076 *   mg_interface_in.clear_elements();
1077 *   mg_interface_out.resize(0, n_levels - 1);
1078 *   mg_interface_out.clear_elements();
1079 *   mg_sparsity_patterns.resize(0, n_levels - 1);
1080 *   mg_interface_sparsity_patterns.resize(0, n_levels - 1);
1081 *  
1082 *   for (unsigned int level = 0; level < n_levels; ++level)
1083 *   {
1084 *   {
1085 *   DynamicSparsityPattern dsp(dof_handler.n_dofs(level),
1086 *   dof_handler.n_dofs(level));
1087 *   MGTools::make_sparsity_pattern(dof_handler, dsp, level);
1088 *   mg_sparsity_patterns[level].copy_from(dsp);
1089 *   mg_matrices[level].reinit(mg_sparsity_patterns[level]);
1090 *   }
1091 *   {
1092 *   DynamicSparsityPattern dsp(dof_handler.n_dofs(level),
1093 *   dof_handler.n_dofs(level));
1095 *   mg_constrained_dofs,
1096 *   dsp,
1097 *   level);
1098 *   mg_interface_sparsity_patterns[level].copy_from(dsp);
1099 *  
1100 *   mg_interface_in[level].reinit(mg_interface_sparsity_patterns[level]);
1101 *   mg_interface_out[level].reinit(mg_interface_sparsity_patterns[level]);
1102 *   }
1103 *   }
1104 *   }
1105 *  
1106 *  
1107 * @endcode
1108 *
1109 *
1110 * <a name="step_63-codeAdvectionProblemassemble_cellcode"></a>
1111 * <h4><code>AdvectionProblem::assemble_cell()</code></h4>
1112 *
1113
1114 *
1115 * Here we define the assembly of the linear system on each cell to
1116 * be used by the mesh_loop() function below. This one function
1117 * assembles the cell matrix for either an active or a level cell
1118 * (whatever it is passed as its first argument), and only assembles
1119 * a right-hand side if called with an active cell.
1120 *
1121
1122 *
1123 *
1124 * @code
1125 *   template <int dim>
1126 *   template <class IteratorType>
1127 *   void AdvectionProblem<dim>::assemble_cell(const IteratorType &cell,
1128 *   ScratchData<dim> &scratch_data,
1129 *   CopyData &copy_data)
1130 *   {
1131 *   copy_data.level = cell->level();
1132 *  
1133 *   const unsigned int dofs_per_cell =
1134 *   scratch_data.fe_values.get_fe().n_dofs_per_cell();
1135 *   copy_data.dofs_per_cell = dofs_per_cell;
1136 *   copy_data.cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
1137 *  
1138 *   const unsigned int n_q_points =
1139 *   scratch_data.fe_values.get_quadrature().size();
1140 *  
1141 *   if (cell->is_level_cell() == false)
1142 *   copy_data.cell_rhs.reinit(dofs_per_cell);
1143 *  
1144 *   copy_data.local_dof_indices.resize(dofs_per_cell);
1145 *   cell->get_active_or_mg_dof_indices(copy_data.local_dof_indices);
1146 *  
1147 *   scratch_data.fe_values.reinit(cell);
1148 *  
1149 *   RightHandSide<dim> right_hand_side;
1150 *   std::vector<double> rhs_values(n_q_points);
1151 *  
1152 *   right_hand_side.value_list(scratch_data.fe_values.get_quadrature_points(),
1153 *   rhs_values);
1154 *  
1155 * @endcode
1156 *
1157 * If we are using streamline diffusion we must add its contribution
1158 * to both the cell matrix and the cell right-hand side. If we are not
1159 * using streamline diffusion, setting @f$\delta=0@f$ negates this contribution
1160 * below and we are left with the standard, Galerkin finite element
1161 * assembly.
1162 *
1163 * @code
1164 *   const double delta = (settings.with_streamline_diffusion ?
1165 *   compute_stabilization_delta(cell->diameter(),
1166 *   settings.epsilon,
1167 *   advection_direction,
1168 *   settings.fe_degree) :
1169 *   0.0);
1170 *  
1171 *   for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1172 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1173 *   {
1174 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1175 *   {
1176 * @endcode
1177 *
1178 * The assembly of the local matrix has two parts. First
1179 * the Galerkin contribution:
1180 *
1181 * @code
1182 *   copy_data.cell_matrix(i, j) +=
1183 *   (settings.epsilon *
1184 *   scratch_data.fe_values.shape_grad(i, q_point) *
1185 *   scratch_data.fe_values.shape_grad(j, q_point) *
1186 *   scratch_data.fe_values.JxW(q_point)) +
1187 *   (scratch_data.fe_values.shape_value(i, q_point) *
1188 *   (advection_direction *
1189 *   scratch_data.fe_values.shape_grad(j, q_point)) *
1190 *   scratch_data.fe_values.JxW(q_point))
1191 * @endcode
1192 *
1193 * and then the streamline diffusion contribution:
1194 *
1195 * @code
1196 *   + delta *
1197 *   (advection_direction *
1198 *   scratch_data.fe_values.shape_grad(j, q_point)) *
1199 *   (advection_direction *
1200 *   scratch_data.fe_values.shape_grad(i, q_point)) *
1201 *   scratch_data.fe_values.JxW(q_point) -
1202 *   delta * settings.epsilon *
1203 *   trace(scratch_data.fe_values.shape_hessian(j, q_point)) *
1204 *   (advection_direction *
1205 *   scratch_data.fe_values.shape_grad(i, q_point)) *
1206 *   scratch_data.fe_values.JxW(q_point);
1207 *   }
1208 *   if (cell->is_level_cell() == false)
1209 *   {
1210 * @endcode
1211 *
1212 * The same applies to the right hand side. First the
1213 * Galerkin contribution:
1214 *
1215 * @code
1216 *   copy_data.cell_rhs(i) +=
1217 *   scratch_data.fe_values.shape_value(i, q_point) *
1218 *   rhs_values[q_point] * scratch_data.fe_values.JxW(q_point)
1219 * @endcode
1220 *
1221 * and then the streamline diffusion contribution:
1222 *
1223 * @code
1224 *   + delta * rhs_values[q_point] * advection_direction *
1225 *   scratch_data.fe_values.shape_grad(i, q_point) *
1226 *   scratch_data.fe_values.JxW(q_point);
1227 *   }
1228 *   }
1229 *   }
1230 *  
1231 *  
1232 * @endcode
1233 *
1234 *
1235 * <a name="step_63-codeAdvectionProblemassemble_system_and_multigridcode"></a>
1236 * <h4><code>AdvectionProblem::assemble_system_and_multigrid()</code></h4>
1237 *
1238
1239 *
1240 * Here we employ MeshWorker::mesh_loop() to go over cells and assemble the
1241 * system_matrix, system_rhs, and all mg_matrices for us.
1242 *
1243
1244 *
1245 *
1246 * @code
1247 *   template <int dim>
1248 *   void AdvectionProblem<dim>::assemble_system_and_multigrid()
1249 *   {
1250 *   const auto cell_worker_active =
1251 *   [&](const decltype(dof_handler.begin_active()) &cell,
1252 *   ScratchData<dim> &scratch_data,
1253 *   CopyData &copy_data) {
1254 *   this->assemble_cell(cell, scratch_data, copy_data);
1255 *   };
1256 *  
1257 *   const auto copier_active = [&](const CopyData &copy_data) {
1258 *   constraints.distribute_local_to_global(copy_data.cell_matrix,
1259 *   copy_data.cell_rhs,
1260 *   copy_data.local_dof_indices,
1261 *   system_matrix,
1262 *   system_rhs);
1263 *   };
1264 *  
1265 *  
1266 *   MeshWorker::mesh_loop(dof_handler.begin_active(),
1267 *   dof_handler.end(),
1268 *   cell_worker_active,
1269 *   copier_active,
1270 *   ScratchData<dim>(fe, fe.degree + 1),
1271 *   CopyData(),
1273 *  
1274 * @endcode
1275 *
1276 * Unlike the constraints for the active level, we choose to create
1277 * constraint objects for each multigrid level local to this function
1278 * since they are never needed elsewhere in the program.
1279 *
1280 * @code
1281 *   std::vector<AffineConstraints<double>> boundary_constraints(
1282 *   triangulation.n_global_levels());
1283 *   for (unsigned int level = 0; level < triangulation.n_global_levels();
1284 *   ++level)
1285 *   {
1286 *   boundary_constraints[level].reinit(
1287 *   dof_handler.locally_owned_mg_dofs(level),
1289 *  
1290 *   for (const types::global_dof_index dof_index :
1291 *   mg_constrained_dofs.get_refinement_edge_indices(level))
1292 *   boundary_constraints[level].constrain_dof_to_zero(dof_index);
1293 *   for (const types::global_dof_index dof_index :
1294 *   mg_constrained_dofs.get_boundary_indices(level))
1295 *   boundary_constraints[level].constrain_dof_to_zero(dof_index);
1296 *   boundary_constraints[level].close();
1297 *   }
1298 *  
1299 *   const auto cell_worker_mg =
1300 *   [&](const decltype(dof_handler.begin_mg()) &cell,
1301 *   ScratchData<dim> &scratch_data,
1302 *   CopyData &copy_data) {
1303 *   this->assemble_cell(cell, scratch_data, copy_data);
1304 *   };
1305 *  
1306 *   const auto copier_mg = [&](const CopyData &copy_data) {
1307 *   boundary_constraints[copy_data.level].distribute_local_to_global(
1308 *   copy_data.cell_matrix,
1309 *   copy_data.local_dof_indices,
1310 *   mg_matrices[copy_data.level]);
1311 *  
1312 * @endcode
1313 *
1314 * If @f$(i,j)@f$ is an `interface_out` dof pair, then @f$(j,i)@f$ is an
1315 * `interface_in` dof pair. Note: For `interface_in`, we load
1316 * the transpose of the interface entries, i.e., the entry for
1317 * dof pair @f$(j,i)@f$ is stored in `interface_in(i,j)`. This is an
1318 * optimization for the symmetric case which allows only one
1319 * matrix to be used when setting the edge_matrices in
1320 * solve(). Here, however, since our problem is non-symmetric,
1321 * we must store both `interface_in` and `interface_out`
1322 * matrices.
1323 *
1324 * @code
1325 *   for (unsigned int i = 0; i < copy_data.dofs_per_cell; ++i)
1326 *   for (unsigned int j = 0; j < copy_data.dofs_per_cell; ++j)
1327 *   if (mg_constrained_dofs.is_interface_matrix_entry(
1328 *   copy_data.level,
1329 *   copy_data.local_dof_indices[i],
1330 *   copy_data.local_dof_indices[j]))
1331 *   {
1332 *   mg_interface_out[copy_data.level].add(
1333 *   copy_data.local_dof_indices[i],
1334 *   copy_data.local_dof_indices[j],
1335 *   copy_data.cell_matrix(i, j));
1336 *   mg_interface_in[copy_data.level].add(
1337 *   copy_data.local_dof_indices[i],
1338 *   copy_data.local_dof_indices[j],
1339 *   copy_data.cell_matrix(j, i));
1340 *   }
1341 *   };
1342 *  
1343 *   MeshWorker::mesh_loop(dof_handler.begin_mg(),
1344 *   dof_handler.end_mg(),
1345 *   cell_worker_mg,
1346 *   copier_mg,
1347 *   ScratchData<dim>(fe, fe.degree + 1),
1348 *   CopyData(),
1350 *   }
1351 *  
1352 *  
1353 * @endcode
1354 *
1355 *
1356 * <a name="step_63-codeAdvectionProblemsetup_smoothercode"></a>
1357 * <h4><code>AdvectionProblem::setup_smoother()</code></h4>
1358 *
1359
1360 *
1361 * Next, we set up the smoother based on the settings in the `.prm` file. The
1362 * two options that are of significance is the number of pre- and
1363 * post-smoothing steps on each level of the multigrid v-cycle and the
1364 * relaxation parameter.
1365 *
1366
1367 *
1368 * Since multiplicative methods tend to be more powerful than additive method,
1369 * fewer smoothing steps are required to see convergence independent of mesh
1370 * size. The same holds for block smoothers over point smoothers. This is
1371 * reflected in the choice for the number of smoothing steps for each type of
1372 * smoother below.
1373 *
1374
1375 *
1376 * The relaxation parameter for point smoothers is chosen based on trial and
1377 * error, and reflects values necessary to keep the iteration counts in
1378 * the GMRES solve constant (or as close as possible) as we refine the mesh.
1379 * The two values given for both "Jacobi" and "SOR" in the `.prm` files are
1380 * for degree 1 and degree 3 finite elements. If the user wants to change to
1381 * another degree, they may need to adjust these numbers. For block smoothers,
1382 * this parameter has a more straightforward interpretation, namely that for
1383 * additive methods in 2d, a DoF can have a repeated contribution from up to 4
1384 * cells, therefore we must relax these methods by 0.25 to compensate. This is
1385 * not an issue for multiplicative methods as each cell's inverse application
1386 * carries new information to all its DoFs.
1387 *
1388
1389 *
1390 * Finally, as mentioned above, the point smoothers only operate on DoFs, and
1391 * the block smoothers on cells, so only the block smoothers need to be given
1392 * information regarding cell orderings. DoF ordering for point smoothers has
1393 * already been taken care of in `setup_system()`.
1394 *
1395
1396 *
1397 *
1398 * @code
1399 *   template <int dim>
1400 *   void AdvectionProblem<dim>::setup_smoother()
1401 *   {
1402 *   if (settings.smoother_type == "SOR")
1403 *   {
1404 *   using Smoother = PreconditionSOR<SparseMatrix<double>>;
1405 *  
1406 *   auto smoother =
1407 *   std::make_unique<MGSmootherPrecondition<SparseMatrix<double>,
1408 *   Smoother,
1409 *   Vector<double>>>();
1410 *   smoother->initialize(mg_matrices,
1411 *   Smoother::AdditionalData(fe.degree == 1 ? 1.0 :
1412 *   0.62));
1413 *   smoother->set_steps(settings.smoothing_steps);
1414 *   mg_smoother = std::move(smoother);
1415 *   }
1416 *   else if (settings.smoother_type == "Jacobi")
1417 *   {
1418 *   using Smoother = PreconditionJacobi<SparseMatrix<double>>;
1419 *   auto smoother =
1420 *   std::make_unique<MGSmootherPrecondition<SparseMatrix<double>,
1421 *   Smoother,
1422 *   Vector<double>>>();
1423 *   smoother->initialize(mg_matrices,
1424 *   Smoother::AdditionalData(fe.degree == 1 ? 0.6667 :
1425 *   0.47));
1426 *   smoother->set_steps(settings.smoothing_steps);
1427 *   mg_smoother = std::move(smoother);
1428 *   }
1429 *   else if (settings.smoother_type == "block SOR" ||
1430 *   settings.smoother_type == "block Jacobi")
1431 *   {
1432 *   smoother_data.resize(0, triangulation.n_levels() - 1);
1433 *  
1434 *   for (unsigned int level = 0; level < triangulation.n_levels(); ++level)
1435 *   {
1436 *   DoFTools::make_cell_patches(smoother_data[level].block_list,
1437 *   dof_handler,
1438 *   level);
1439 *  
1440 *   smoother_data[level].relaxation =
1441 *   (settings.smoother_type == "block SOR" ? 1.0 : 0.25);
1442 *   smoother_data[level].inversion = PreconditionBlockBase<double>::svd;
1443 *  
1444 *   std::vector<unsigned int> ordered_indices;
1445 *   switch (settings.dof_renumbering)
1446 *   {
1447 *   case Settings::DoFRenumberingStrategy::downstream:
1448 *   ordered_indices =
1449 *   create_downstream_cell_ordering(dof_handler,
1450 *   advection_direction,
1451 *   level);
1452 *   break;
1453 *  
1454 *   case Settings::DoFRenumberingStrategy::upstream:
1455 *   ordered_indices =
1456 *   create_downstream_cell_ordering(dof_handler,
1457 *   -1.0 * advection_direction,
1458 *   level);
1459 *   break;
1460 *  
1461 *   case Settings::DoFRenumberingStrategy::random:
1462 *   ordered_indices =
1463 *   create_random_cell_ordering(dof_handler, level);
1464 *   break;
1465 *  
1466 *   case Settings::DoFRenumberingStrategy::none:
1467 *   break;
1468 *  
1469 *   default:
1470 *   AssertThrow(false, ExcNotImplemented());
1471 *   break;
1472 *   }
1473 *  
1474 *   smoother_data[level].order =
1475 *   std::vector<std::vector<unsigned int>>(1, ordered_indices);
1476 *   }
1477 *  
1478 *   if (settings.smoother_type == "block SOR")
1479 *   {
1480 *   auto smoother = std::make_unique<MGSmootherPrecondition<
1481 *   SparseMatrix<double>,
1482 *   RelaxationBlockSOR<SparseMatrix<double>, double, Vector<double>>,
1483 *   Vector<double>>>();
1484 *   smoother->initialize(mg_matrices, smoother_data);
1485 *   smoother->set_steps(settings.smoothing_steps);
1486 *   mg_smoother = std::move(smoother);
1487 *   }
1488 *   else if (settings.smoother_type == "block Jacobi")
1489 *   {
1490 *   auto smoother = std::make_unique<
1491 *   MGSmootherPrecondition<SparseMatrix<double>,
1492 *   RelaxationBlockJacobi<SparseMatrix<double>,
1493 *   double,
1494 *   Vector<double>>,
1495 *   Vector<double>>>();
1496 *   smoother->initialize(mg_matrices, smoother_data);
1497 *   smoother->set_steps(settings.smoothing_steps);
1498 *   mg_smoother = std::move(smoother);
1499 *   }
1500 *   }
1501 *   else
1502 *   AssertThrow(false, ExcNotImplemented());
1503 *   }
1504 *  
1505 *  
1506 * @endcode
1507 *
1508 *
1509 * <a name="step_63-codeAdvectionProblemsolvecode"></a>
1510 * <h4><code>AdvectionProblem::solve()</code></h4>
1511 *
1512
1513 *
1514 * Before we can solve the system, we must first set up the multigrid
1515 * preconditioner. This requires the setup of the transfer between levels,
1516 * the coarse matrix solver, and the smoother. This setup follows almost
1517 * identically to @ref step_16 "step-16", the main difference being the various smoothers
1518 * defined above and the fact that we need different interface edge matrices
1519 * for in and out since our problem is non-symmetric. (In reality, for this
1520 * tutorial these interface matrices are empty since we are only using global
1521 * refinement, and thus have no refinement edges. However, we have still
1522 * included both here since if one made the simple switch to an adaptively
1523 * refined method, the program would still run correctly.)
1524 *
1525
1526 *
1527 * The last thing to note is that since our problem is non-symmetric, we must
1528 * use an appropriate Krylov subspace method. We choose here to
1529 * use GMRES since it offers the guarantee of residual reduction in each
1530 * iteration. The major disadvantage of GMRES is that, for each iteration,
1531 * the number of stored temporary vectors increases by one, and one also needs
1532 * to compute a scalar product with all previously stored vectors. This is
1533 * rather expensive. This requirement is relaxed by using the restarted GMRES
1534 * method which puts a cap on the number of vectors we are required to store
1535 * at any one time (here we restart after 50 temporary vectors, or 48
1536 * iterations). This then has the disadvantage that we lose information we
1537 * have gathered throughout the iteration and therefore we could see slower
1538 * convergence. As a consequence, where to restart is a question of balancing
1539 * memory consumption, CPU effort, and convergence speed.
1540 * However, the goal of this tutorial is to have very low
1541 * iteration counts by using a powerful GMG preconditioner, so we have picked
1542 * the restart length such that all of the results shown below converge prior
1543 * to restart happening, and thus we have a standard GMRES method. If the user
1544 * is interested, another suitable method offered in deal.II would be
1545 * BiCGStab.
1546 *
1547
1548 *
1549 *
1550 * @code
1551 *   template <int dim>
1552 *   void AdvectionProblem<dim>::solve()
1553 *   {
1554 *   const unsigned int max_iters = 200;
1555 *   const double solve_tolerance = 1e-8 * system_rhs.l2_norm();
1556 *   SolverControl solver_control(max_iters, solve_tolerance, true, true);
1557 *   solver_control.enable_history_data();
1558 *  
1559 *   using Transfer = MGTransferPrebuilt<Vector<double>>;
1560 *   Transfer mg_transfer(mg_constrained_dofs);
1561 *   mg_transfer.build(dof_handler);
1562 *  
1563 *   FullMatrix<double> coarse_matrix;
1564 *   coarse_matrix.copy_from(mg_matrices[0]);
1565 *   MGCoarseGridHouseholder<double, Vector<double>> coarse_grid_solver;
1566 *   coarse_grid_solver.initialize(coarse_matrix);
1567 *  
1568 *   setup_smoother();
1569 *  
1570 *   mg_matrix.initialize(mg_matrices);
1571 *   mg_interface_matrix_in.initialize(mg_interface_in);
1572 *   mg_interface_matrix_out.initialize(mg_interface_out);
1573 *  
1574 *   Multigrid<Vector<double>> mg(
1575 *   mg_matrix, coarse_grid_solver, mg_transfer, *mg_smoother, *mg_smoother);
1576 *   mg.set_edge_matrices(mg_interface_matrix_out, mg_interface_matrix_in);
1577 *  
1578 *   PreconditionMG<dim, Vector<double>, Transfer> preconditioner(dof_handler,
1579 *   mg,
1580 *   mg_transfer);
1581 *  
1582 *   std::cout << " Solving with GMRES to tol " << solve_tolerance << "..."
1583 *   << std::endl;
1584 *   SolverGMRES<Vector<double>> solver(
1585 *   solver_control, SolverGMRES<Vector<double>>::AdditionalData(50, true));
1586 *  
1587 *   Timer time;
1588 *   time.start();
1589 *   solver.solve(system_matrix, solution, system_rhs, preconditioner);
1590 *   time.stop();
1591 *  
1592 *   std::cout << " converged in " << solver_control.last_step()
1593 *   << " iterations"
1594 *   << " in " << time.last_wall_time() << " seconds " << std::endl;
1595 *  
1596 *   constraints.distribute(solution);
1597 *  
1598 *   mg_smoother.release();
1599 *   }
1600 *  
1601 *  
1602 * @endcode
1603 *
1604 *
1605 * <a name="step_63-codeAdvectionProblemoutput_resultscode"></a>
1606 * <h4><code>AdvectionProblem::output_results()</code></h4>
1607 *
1608
1609 *
1610 * The final function of interest generates graphical output.
1611 * Here we output the solution and cell ordering in a .vtu format.
1612 *
1613
1614 *
1615 * At the top of the function, we generate an index for each cell to
1616 * visualize the ordering used by the smoothers. Note that we do
1617 * this only for the active cells instead of the levels, where the
1618 * smoothers are actually used. For the point smoothers we renumber
1619 * DoFs instead of cells, so this is only an approximation of what
1620 * happens in reality. Finally, the random ordering is not the
1621 * random ordering we actually use (see `create_smoother()` for that).
1622 *
1623
1624 *
1625 * The (integer) ordering of cells is then copied into a (floating
1626 * point) vector for graphical output.
1627 *
1628 * @code
1629 *   template <int dim>
1630 *   void AdvectionProblem<dim>::output_results(const unsigned int cycle) const
1631 *   {
1632 *   const unsigned int n_active_cells = triangulation.n_active_cells();
1633 *   Vector<double> cell_indices(n_active_cells);
1634 *   {
1635 *   std::vector<unsigned int> ordered_indices;
1636 *   switch (settings.dof_renumbering)
1637 *   {
1638 *   case Settings::DoFRenumberingStrategy::downstream:
1639 *   ordered_indices =
1640 *   create_downstream_cell_ordering(dof_handler, advection_direction);
1641 *   break;
1642 *  
1643 *   case Settings::DoFRenumberingStrategy::upstream:
1644 *   ordered_indices =
1645 *   create_downstream_cell_ordering(dof_handler,
1646 *   -1.0 * advection_direction);
1647 *   break;
1648 *  
1649 *   case Settings::DoFRenumberingStrategy::random:
1650 *   ordered_indices = create_random_cell_ordering(dof_handler);
1651 *   break;
1652 *  
1653 *   case Settings::DoFRenumberingStrategy::none:
1654 *   ordered_indices.resize(n_active_cells);
1655 *   for (unsigned int i = 0; i < n_active_cells; ++i)
1656 *   ordered_indices[i] = i;
1657 *   break;
1658 *  
1659 *   default:
1660 *   AssertThrow(false, ExcNotImplemented());
1661 *   break;
1662 *   }
1663 *  
1664 *   for (unsigned int i = 0; i < n_active_cells; ++i)
1665 *   cell_indices(ordered_indices[i]) = static_cast<double>(i);
1666 *   }
1667 *  
1668 * @endcode
1669 *
1670 * The remainder of the function is then straightforward, given
1671 * previous tutorial programs:
1672 *
1673 * @code
1674 *   DataOut<dim> data_out;
1675 *   data_out.attach_dof_handler(dof_handler);
1676 *   data_out.add_data_vector(solution, "solution");
1677 *   data_out.add_data_vector(cell_indices, "cell_index");
1678 *   data_out.build_patches();
1679 *  
1680 *   const std::string filename =
1681 *   "solution-" + Utilities::int_to_string(cycle) + ".vtu";
1682 *   std::ofstream output(filename);
1683 *   data_out.write_vtu(output);
1684 *   }
1685 *  
1686 *  
1687 * @endcode
1688 *
1689 *
1690 * <a name="step_63-codeAdvectionProblemruncode"></a>
1691 * <h4><code>AdvectionProblem::run()</code></h4>
1692 *
1693
1694 *
1695 * As in most tutorials, this function creates/refines the mesh and calls
1696 * the various functions defined above to set up, assemble, solve, and output
1697 * the results.
1698 *
1699
1700 *
1701 * In cycle zero, we generate the mesh for the on the square
1702 * <code>[-1,1]^dim</code> with a hole of radius 3/10 units centered
1703 * at the origin. For objects with `manifold_id` equal to one
1704 * (namely, the faces adjacent to the hole), we assign a spherical
1705 * manifold.
1706 *
1707
1708 *
1709 *
1710 * @code
1711 *   template <int dim>
1712 *   void AdvectionProblem<dim>::run()
1713 *   {
1714 *   for (unsigned int cycle = 0; cycle < (settings.fe_degree == 1 ? 7u : 5u);
1715 *   ++cycle)
1716 *   {
1717 *   std::cout << " Cycle " << cycle << ':' << std::endl;
1718 *  
1719 *   if (cycle == 0)
1720 *   {
1721 *   GridGenerator::hyper_cube_with_cylindrical_hole(triangulation,
1722 *   0.3,
1723 *   1.0);
1724 *  
1725 *   const SphericalManifold<dim> manifold_description(Point<dim>(0, 0));
1726 *   triangulation.set_manifold(1, manifold_description);
1727 *   }
1728 *  
1729 *   triangulation.refine_global();
1730 *  
1731 *   setup_system();
1732 *  
1733 *   std::cout << " Number of active cells: "
1734 *   << triangulation.n_active_cells() << " ("
1735 *   << triangulation.n_levels() << " levels)" << std::endl;
1736 *   std::cout << " Number of degrees of freedom: "
1737 *   << dof_handler.n_dofs() << std::endl;
1738 *  
1739 *   assemble_system_and_multigrid();
1740 *  
1741 *   solve();
1742 *  
1743 *   if (settings.output)
1744 *   output_results(cycle);
1745 *  
1746 *   std::cout << std::endl;
1747 *   }
1748 *   }
1749 *   } // namespace Step63
1750 *  
1751 *  
1752 * @endcode
1753 *
1754 *
1755 * <a name="step_63-Thecodemaincodefunction"></a>
1756 * <h3>The <code>main</code> function</h3>
1757 *
1758
1759 *
1760 * Finally, the main function is like most tutorials. The only
1761 * interesting bit is that we require the user to pass a `.prm` file
1762 * as a sole command line argument. If no parameter file is given, the
1763 * program will output the contents of a sample parameter file with
1764 * all default values to the screen that the user can then copy and
1765 * paste into their own `.prm` file.
1766 *
1767
1768 *
1769 *
1770 * @code
1771 *   int main(int argc, char *argv[])
1772 *   {
1773 *   try
1774 *   {
1775 *   Step63::Settings settings;
1776 *   settings.get_parameters((argc > 1) ? (argv[1]) : "");
1777 *  
1778 *   Step63::AdvectionProblem<2> advection_problem_2d(settings);
1779 *   advection_problem_2d.run();
1780 *   }
1781 *   catch (std::exception &exc)
1782 *   {
1783 *   std::cerr << std::endl
1784 *   << std::endl
1785 *   << "----------------------------------------------------"
1786 *   << std::endl;
1787 *   std::cerr << "Exception on processing: " << std::endl
1788 *   << exc.what() << std::endl
1789 *   << "Aborting!" << std::endl
1790 *   << "----------------------------------------------------"
1791 *   << std::endl;
1792 *   return 1;
1793 *   }
1794 *   catch (...)
1795 *   {
1796 *   std::cerr << std::endl
1797 *   << std::endl
1798 *   << "----------------------------------------------------"
1799 *   << std::endl;
1800 *   std::cerr << "Unknown exception!" << std::endl
1801 *   << "Aborting!" << std::endl
1802 *   << "----------------------------------------------------"
1803 *   << std::endl;
1804 *   return 1;
1805 *   }
1806 *  
1807 *   return 0;
1808 *   }
1809 * @endcode
1810<a name="step_63-Results"></a><h1>Results</h1>
1811
1812
1813<a name="step_63-GMRESIterationNumbers"></a><h3> GMRES Iteration Numbers </h3>
1814
1815
1816The major advantage for GMG is that it is an @f$\mathcal{O}(n)@f$ method,
1817that is, the complexity of the problem increases linearly with the
1818problem size. To show then that the linear solver presented in this
1819tutorial is in fact @f$\mathcal{O}(n)@f$, all one needs to do is show that
1820the iteration counts for the GMRES solve stay roughly constant as we
1821refine the mesh.
1822
1823Each of the following tables gives the GMRES iteration counts to reduce the
1824initial residual by a factor of @f$10^8@f$. We selected a sufficient number of smoothing steps
1825(based on the method) to get iteration numbers independent of mesh size. As
1826can be seen from the tables below, the method is indeed @f$\mathcal{O}(n)@f$.
1827
1828<a name="step_63-DoFCellRenumbering"></a><h4> DoF/Cell Renumbering </h4>
1829
1830
1831The point-wise smoothers ("Jacobi" and "SOR") get applied in the order the
1832DoFs are numbered on each level. We can influence this using the
1833DoFRenumbering namespace. The block smoothers are applied based on the
1834ordering we set in `setup_smoother()`. We can visualize this numbering. The
1835following pictures show the cell numbering of the active cells in downstream,
1836random, and upstream numbering (left to right):
1837
1838<img src="https://www.dealii.org/images/steps/developer/step-63-cell-order.png" alt="">
1839
1840Let us start with the additive smoothers. The following table shows
1841the number of iterations necessary to obtain convergence from GMRES:
1842
1843<table align="center" class="doxtable">
1844<tr>
1845 <th></th>
1846 <th></th>
1847 <th colspan="1">@f$Q_1@f$</th>
1848 <th colspan="7">Smoother (smoothing steps)</th>
1849</tr>
1850<tr>
1851 <th></th>
1852 <th></th>
1853 <th></th>
1854 <th colspan="3">Jacobi (6)</th>
1855 <th></th>
1856 <th colspan="3">Block Jacobi (3)</th>
1857</tr>
1858<tr>
1859 <th></th>
1860 <th></th>
1861 <th></th>
1862 <th colspan="3">Renumbering Strategy</th>
1863 <th></th>
1864 <th colspan="3">Renumbering Strategy</th>
1865</tr>
1866<tr>
1867 <th>Cells</th>
1868 <th></th>
1869 <th>DoFs</th>
1870 <th>Downstream</th>
1871 <th>Random</th>
1872 <th>Upstream</th>
1873 <th></th>
1874 <th>Downstream</th>
1875 <th>Random</th>
1876 <th>Upstream</th>
1877</tr>
1878<tr>
1879 <th>32</th>
1880 <th></th>
1881 <th>48</th>
1882 <td>3</th>
1883 <td>3</th>
1884 <td>3</th>
1885 <th></th>
1886 <td>3</th>
1887 <td>3</th>
1888 <td>3</th>
1889</tr>
1890<tr>
1891 <th>128</th>
1892 <th></th>
1893 <th>160</th>
1894 <td>6</th>
1895 <td>6</th>
1896 <td>6</th>
1897 <th></th>
1898 <td>6</th>
1899 <td>6</th>
1900 <td>6</th>
1901</tr>
1902<tr>
1903 <th>512</th>
1904 <th></th>
1905 <th>576</th>
1906 <td>11</th>
1907 <td>11</th>
1908 <td>11</th>
1909 <th></th>
1910 <td>9</th>
1911 <td>9</th>
1912 <td>9</th>
1913</tr>
1914<tr>
1915 <th>2048</th>
1916 <th></th>
1917 <th>2176</th>
1918 <td>15</th>
1919 <td>15</th>
1920 <td>15</th>
1921 <th></th>
1922 <td>13</th>
1923 <td>13</th>
1924 <td>13</th>
1925</tr>
1926<tr>
1927 <th>8192</th>
1928 <th></th>
1929 <th>8448</th>
1930 <td>18</th>
1931 <td>18</th>
1932 <td>18</th>
1933 <th></th>
1934 <td>15</th>
1935 <td>15</th>
1936 <td>15</th>
1937</tr>
1938<tr>
1939 <th>32768</th>
1940 <th></th>
1941 <th>33280</th>
1942 <td>20</th>
1943 <td>20</th>
1944 <td>20</th>
1945 <th></th>
1946 <td>16</th>
1947 <td>16</th>
1948 <td>16</th>
1949</tr>
1950<tr>
1951 <th>131072</th>
1952 <th></th>
1953 <th>132096</th>
1954 <td>20</th>
1955 <td>20</th>
1956 <td>20</th>
1957 <th></th>
1958 <td>16</th>
1959 <td>16</th>
1960 <td>16</th>
1961</tr>
1962</table>
1963
1964We see that renumbering the
1965DoFs/cells has no effect on convergence speed. This is because these
1966smoothers compute operations on each DoF (point-smoother) or cell
1967(block-smoother) independently and add up the results. Since we can
1968define these smoothers as an application of a sum of matrices, and
1969matrix addition is commutative, the order at which we sum the
1970different components will not affect the end result.
1971
1972On the other hand, the situation is different for multiplicative smoothers:
1973
1974<table align="center" class="doxtable">
1975<tr>
1976 <th></th>
1977 <th></th>
1978 <th colspan="1">@f$Q_1@f$</th>
1979 <th colspan="7">Smoother (smoothing steps)</th>
1980</tr>
1981<tr>
1982 <th></th>
1983 <th></th>
1984 <th></th>
1985 <th colspan="3">SOR (3)</th>
1986 <th></th>
1987 <th colspan="3">Block SOR (1)</th>
1988</tr>
1989<tr>
1990 <th></th>
1991 <th></th>
1992 <th></th>
1993 <th colspan="3">Renumbering Strategy</th>
1994 <th></th>
1995 <th colspan="3">Renumbering Strategy</th>
1996</tr>
1997<tr>
1998 <th>Cells</th>
1999 <th></th>
2000 <th>DoFs</th>
2001 <th>Downstream</th>
2002 <th>Random</th>
2003 <th>Upstream</th>
2004 <th></th>
2005 <th>Downstream</th>
2006 <th>Random</th>
2007 <th>Upstream</th>
2008</tr>
2009<tr>
2010 <th>32</th>
2011 <th></th>
2012 <th>48</th>
2013 <td>2</th>
2014 <td>2</th>
2015 <td>3</th>
2016 <th></th>
2017 <td>2</th>
2018 <td>2</th>
2019 <td>3</th>
2020</tr>
2021<tr>
2022 <th>128</th>
2023 <th></th>
2024 <th>160</th>
2025 <td>5</th>
2026 <td>5</th>
2027 <td>7</th>
2028 <th></th>
2029 <td>5</th>
2030 <td>5</th>
2031 <td>7</th>
2032</tr>
2033<tr>
2034 <th>512</th>
2035 <th></th>
2036 <th>576</th>
2037 <td>7</th>
2038 <td>9</th>
2039 <td>11</th>
2040 <th></th>
2041 <td>7</th>
2042 <td>7</th>
2043 <td>12</th>
2044</tr>
2045<tr>
2046 <th>2048</th>
2047 <th></th>
2048 <th>2176</th>
2049 <td>10</th>
2050 <td>12</th>
2051 <td>15</th>
2052 <th></th>
2053 <td>8</th>
2054 <td>10</th>
2055 <td>17</th>
2056</tr>
2057<tr>
2058 <th>8192</th>
2059 <th></th>
2060 <th>8448</th>
2061 <td>11</th>
2062 <td>15</th>
2063 <td>19</th>
2064 <th></th>
2065 <td>10</th>
2066 <td>11</th>
2067 <td>20</th>
2068</tr>
2069<tr>
2070 <th>32768</th>
2071 <th></th>
2072 <th>33280</th>
2073 <td>12</th>
2074 <td>16</th>
2075 <td>20</th>
2076 <th></th>
2077 <td>10</th>
2078 <td>12</th>
2079 <td>21</th>
2080</tr>
2081<tr>
2082 <th>131072</th>
2083 <th></th>
2084 <th>132096</th>
2085 <td>12</th>
2086 <td>16</th>
2087 <td>19</th>
2088 <th></th>
2089 <td>11</th>
2090 <td>12</th>
2091 <td>21</th>
2092</tr>
2093</table>
2094
2095Here, we can speed up
2096convergence by renumbering the DoFs/cells in the advection direction,
2097and similarly, we can slow down convergence if we do the renumbering
2098in the opposite direction. This is because advection-dominated
2099problems have a directional flow of information (in the advection
2100direction) which, given the right renumbering of DoFs/cells,
2101multiplicative methods are able to capture.
2102
2103This feature of multiplicative methods is, however, dependent on the
2104value of @f$\varepsilon@f$. As we increase @f$\varepsilon@f$ and the problem
2105becomes more diffusion-dominated, we have a more uniform propagation
2106of information over the mesh and there is a diminished advantage for
2107renumbering in the advection direction. On the opposite end, in the
2108extreme case of @f$\varepsilon=0@f$ (advection-only), we have a 1st-order
2109PDE and multiplicative methods with the right renumbering become
2110effective solvers: A correct downstream numbering may lead to methods
2111that require only a single iteration because information can be
2112propagated from the inflow boundary downstream, with no information
2113transport in the opposite direction. (Note, however, that in the case
2114of @f$\varepsilon=0@f$, special care must be taken for the boundary
2115conditions in this case).
2116
2117
2118<a name="step_63-Pointvsblocksmoothers"></a><h4> %Point vs. block smoothers </h4>
2119
2120
2121We will limit the results to runs using the downstream
2122renumbering. Here is a cross comparison of all four smoothers for both
2123@f$Q_1@f$ and @f$Q_3@f$ elements:
2124
2125<table align="center" class="doxtable">
2126<tr>
2127 <th></th>
2128 <td></th>
2129 <th colspan="1">@f$Q_1@f$</th>
2130 <th colspan="4">Smoother (smoothing steps)</th>
2131 <th></th>
2132 <th colspan="1">@f$Q_3@f$</th>
2133 <th colspan="4">Smoother (smoothing steps)</th>
2134</tr>
2135<tr>
2136 <th colspan="1">Cells</th>
2137 <td></th>
2138 <th colspan="1">DoFs</th>
2139 <th colspan="1">Jacobi (6)</th>
2140 <th colspan="1">Block Jacobi (3)</th>
2141 <th colspan="1">SOR (3)</th>
2142 <th colspan="1">Block SOR (1)</th>
2143 <th></th>
2144 <th colspan="1">DoFs</th>
2145 <th colspan="1">Jacobi (6)</th>
2146 <th colspan="1">Block Jacobi (3)</th>
2147 <th colspan="1">SOR (3)</th>
2148 <th colspan="1">Block SOR (1)</th>
2149</tr>
2150<tr>
2151 <th>32</th>
2152 <td></th>
2153 <th>48</th>
2154 <td>3</th>
2155 <td>3</th>
2156 <td>2</th>
2157 <td>2</th>
2158 <td></th>
2159 <th>336</th>
2160 <td>15</th>
2161 <td>14</th>
2162 <td>15</th>
2163 <td>6</th>
2164</tr>
2165<tr>
2166 <th>128</th>
2167 <td></th>
2168 <th>160</th>
2169 <td>6</th>
2170 <td>6</th>
2171 <td>5</th>
2172 <td>5</th>
2173 <td></th>
2174 <th>1248</th>
2175 <td>23</th>
2176 <td>18</th>
2177 <td>21</th>
2178 <td>9</th>
2179</tr>
2180<tr>
2181 <th>512</th>
2182 <td></th>
2183 <th>576</th>
2184 <td>11</th>
2185 <td>9</th>
2186 <td>7</th>
2187 <td>7</th>
2188 <td></th>
2189 <th>4800</th>
2190 <td>29</th>
2191 <td>21</th>
2192 <td>28</th>
2193 <td>9</th>
2194</tr>
2195<tr>
2196 <th>2048</th>
2197 <td></th>
2198 <th>2176</th>
2199 <td>15</th>
2200 <td>13</th>
2201 <td>10</th>
2202 <td>8</th>
2203 <td></th>
2204 <th>18816</th>
2205 <td>33</th>
2206 <td>22</th>
2207 <td>32</th>
2208 <td>9</th>
2209</tr>
2210<tr>
2211 <th>8192</th>
2212 <td></th>
2213 <th>8448</th>
2214 <td>18</th>
2215 <td>15</th>
2216 <td>11</th>
2217 <td>10</th>
2218 <td></th>
2219 <th>74496</th>
2220 <td>35</th>
2221 <td>22</th>
2222 <td>34</th>
2223 <td>10</th>
2224</tr>
2225<tr>
2226 <th>32768</th>
2227 <td></th>
2228 <th>33280</th>
2229 <td>20</th>
2230 <td>16</th>
2231 <td>12</th>
2232 <td>10</th>
2233 <td></th>
2234</tr>
2235<tr>
2236 <th>131072</th>
2237 <td></th>
2238 <th>132096</th>
2239 <td>20</th>
2240 <td>16</th>
2241 <td>12</th>
2242 <td>11</th>
2243 <td></th>
2244</tr>
2245</table>
2246
2247We see that for @f$Q_1@f$, both multiplicative smoothers require a smaller
2248combination of smoothing steps and iteration counts than either
2249additive smoother. However, when we increase the degree to a @f$Q_3@f$
2250element, there is a clear advantage for the block smoothers in terms
2251of the number of smoothing steps and iterations required to
2252solve. Specifically, the block SOR smoother gives constant iteration
2253counts over the degree, and the block Jacobi smoother only sees about
2254a 38% increase in iterations compared to 75% and 183% for Jacobi and
2255SOR respectively.
2256
2257<a name="step_63-Cost"></a><h3> Cost </h3>
2258
2259
2260Iteration counts do not tell the full story in the optimality of a one
2261smoother over another. Obviously we must examine the cost of an
2262iteration. Block smoothers here are at a disadvantage as they are
2263having to construct and invert a cell matrix for each cell. Here is a
2264comparison of solve times for a @f$Q_3@f$ element with 74,496 DoFs:
2265
2266<table align="center" class="doxtable">
2267<tr>
2268 <th colspan="1">@f$Q_3@f$</th>
2269 <th colspan="4">Smoother (smoothing steps)</th>
2270</tr>
2271<tr>
2272 <th colspan="1">DoFs</th>
2273 <th colspan="1">Jacobi (6)</th>
2274 <th colspan="1">Block Jacobi (3)</th>
2275 <th colspan="1">SOR (3)</th>
2276 <th colspan="1">Block SOR (1)</th>
2277</tr>
2278<tr>
2279 <th>74496</th>
2280 <td>0.68s</th>
2281 <td>5.82s</th>
2282 <td>1.18s</th>
2283 <td>1.02s</th>
2284</tr>
2285</table>
2286
2287The smoother that requires the most iterations (Jacobi) actually takes
2288the shortest time (roughly 2/3 the time of the next fastest
2289method). This is because all that is required to apply a Jacobi
2290smoothing step is multiplication by a diagonal matrix which is very
2291cheap. On the other hand, while SOR requires over 3x more iterations
2292(each with 3x more smoothing steps) than block SOR, the times are
2293roughly equivalent, implying that a smoothing step of block SOR is
2294roughly 9x slower than a smoothing step of SOR. Lastly, block Jacobi
2295is almost 6x more expensive than block SOR, which intuitively makes
2296sense from the fact that 1 step of each method has the same cost
2297(inverting the cell matrices and either adding or multiply them
2298together), and block Jacobi has 3 times the number of smoothing steps per
2299iteration with 2 times the iterations.
2300
2301
2302<a name="step_63-Additionalpoints"></a><h3> Additional points </h3>
2303
2304
2305There are a few more important points to mention:
2306
2307<ol>
2308<li> For a mesh distributed in parallel, multiplicative methods cannot
2309be executed over the entire domain. This is because they operate one
2310cell at a time, and downstream cells can only be handled once upstream
2311cells have already been done. This is fine on a single processor: The
2312processor just goes through the list of cells one after the
2313other. However, in parallel, it would imply that some processors are
2314idle because upstream processors have not finished doing the work on
2315cells upstream from the ones owned by the current processor. Once the
2316upstream processors are done, the downstream ones can start, but by
2317that time the upstream processors have no work left. In other words,
2318most of the time during these smoother steps, most processors are in
2319fact idle. This is not how one obtains good parallel scalability!
2320
2321One can use a hybrid method where
2322a multiplicative smoother is applied on each subdomain, but as you
2323increase the number of subdomains, the method approaches the behavior
2324of an additive method. This is a major disadvantage to these methods.
2325</li>
2326
2327<li> Current research into block smoothers suggest that soon we will be
2328able to compute the inverse of the cell matrices much cheaper than
2329what is currently being done inside deal.II. This research is based on
2330the fast diagonalization method (dating back to the 1960s) and has
2331been used in the spectral community for around 20 years (see, e.g., <a
2332href="https://doi.org/10.1007/s10915-004-4787-3"> Hybrid
2333Multigrid/Schwarz Algorithms for the Spectral Element Method by Lottes
2334and Fischer</a>). There are currently efforts to generalize these
2335methods to DG and make them more robust. Also, it seems that one
2336should be able to take advantage of matrix-free implementations and
2337the fact that, in the interior of the domain, cell matrices tend to
2338look very similar, allowing fewer matrix inverse computations.
2339</li>
2340</ol>
2341
2342Combining 1. and 2. gives a good reason for expecting that a method
2343like block Jacobi could become very powerful in the future, even
2344though currently for these examples it is quite slow.
2345
2346
2347<a name="step_63-Possibilitiesforextensions"></a><h3> Possibilities for extensions </h3>
2348
2349
2350<a name="step_63-ConstantiterationsforQsub5sub"></a><h4> Constant iterations for Q<sub>5</sub> </h4>
2351
2352
2353Change the number of smoothing steps and the smoother relaxation
2354parameter (set in <code>Smoother::AdditionalData()</code> inside
2355<code>create_smoother()</code>, only necessary for point smoothers) so
2356that we maintain a constant number of iterations for a @f$Q_5@f$ element.
2357
2358<a name="step_63-Effectivenessofrenumberingforchangingepsilon"></a><h4> Effectiveness of renumbering for changing epsilon </h4>
2359
2360
2361Increase/decrease the parameter "Epsilon" in the `.prm` files of the
2362multiplicative methods and observe for which values renumbering no
2363longer influences convergence speed.
2364
2365<a name="step_63-Meshadaptivity"></a><h4> Mesh adaptivity </h4>
2366
2367
2368The code is set up to work correctly with an adaptively refined mesh (the
2369interface matrices are created and set). Devise a suitable refinement
2370criterium or try the KellyErrorEstimator class.
2371 *
2372 *
2373<a name="step_63-PlainProg"></a>
2374<h1> The plain program</h1>
2375@include "step-63.cc"
2376*/
Definition fe_q.h:554
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< RangeNumberType > &values, const unsigned int component=0) const
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const
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
Point< 3 > center
DerivativeForm< 1, spacedim, dim, Number > transpose(const DerivativeForm< 1, dim, spacedim, Number > &DF)
Point< 2 > first
Definition grid_out.cc:4623
unsigned int level
Definition grid_out.cc:4626
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrow(cond, exc)
void mesh_loop(const CellIteratorType &begin, const CellIteratorType &end, const CellWorkerFunctionType &cell_worker, const CopierType &copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const AssembleFlags flags=assemble_own_cells, const BoundaryWorkerFunctionType &boundary_worker=BoundaryWorkerFunctionType(), const FaceWorkerFunctionType &face_worker=FaceWorkerFunctionType(), const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
Definition mesh_loop.h:281
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)
#define DEAL_II_NOT_IMPLEMENTED()
Expression coth(const Expression &x)
void downstream(DoFHandler< dim, spacedim > &dof_handler, const Tensor< 1, spacedim > &direction, const bool dof_wise_renumbering=false)
void random(DoFHandler< dim, spacedim > &dof_handler)
IndexSet extract_locally_relevant_level_dofs(const DoFHandler< dim, spacedim > &dof_handler, const unsigned int level)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
@ matrix
Contents is actually a matrix.
@ symmetric
Matrix is symmetric.
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:1013
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
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition utilities.cc:191
SymmetricTensor< 2, dim, Number > C(const Tensor< 2, dim, Number > &F)
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)
SymmetricTensor< 2, dim, Number > epsilon(const Tensor< 2, dim, Number > &Grad_u)
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)
int(&) functions(const void *v1, const void *v2)
static constexpr double PI
Definition numbers.h:254
STL namespace.
::VectorizedArray< Number, width > exp(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
DEAL_II_HOST constexpr Number trace(const SymmetricTensor< 2, dim2, Number > &)