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