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-43.h
Go to the documentation of this file.
1) const
739 *   {
740 *   return 1 - p[0];
741 *   }
742 *  
743 *  
744 *   template <int dim>
745 *   class SaturationBoundaryValues : public Function<dim>
746 *   {
747 *   public:
748 *   SaturationBoundaryValues()
749 *   : Function<dim>(1)
750 *   {}
751 *  
752 *   virtual double value(const Point<dim> &p,
753 *   const unsigned int component = 0) const override;
754 *   };
755 *  
756 *  
757 *  
758 *   template <int dim>
759 *   double
760 *   SaturationBoundaryValues<dim>::value(const Point<dim> &p,
761 *   const unsigned int /*component*/) const
762 *   {
763 *   if (p[0] == 0)
764 *   return 1;
765 *   else
766 *   return 0;
767 *   }
768 *  
769 *  
770 *   template <int dim>
771 *   class SaturationInitialValues : public Function<dim>
772 *   {
773 *   public:
774 *   SaturationInitialValues()
775 *   : Function<dim>(1)
776 *   {}
777 *  
778 *   virtual double value(const Point<dim> &p,
779 *   const unsigned int component = 0) const override;
780 *  
781 *   virtual void vector_value(const Point<dim> &p,
782 *   Vector<double> &value) const override;
783 *   };
784 *  
785 *  
786 *   template <int dim>
787 *   double
788 *   SaturationInitialValues<dim>::value(const Point<dim> & /*p*/,
789 *   const unsigned int /*component*/) const
790 *   {
791 *   return 0.2;
792 *   }
793 *  
794 *  
795 *   template <int dim>
796 *   void SaturationInitialValues<dim>::vector_value(const Point<dim> &p,
797 *   Vector<double> &values) const
798 *   {
799 *   for (unsigned int c = 0; c < this->n_components; ++c)
800 *   values(c) = SaturationInitialValues<dim>::value(p, c);
801 *   }
802 *  
803 *  
804 * @endcode
805 *
806 *
807 * <a name="step_43-Permeabilitymodels"></a>
808 * <h3>Permeability models</h3>
809 *
810
811 *
812 * In this tutorial, we still use the two permeability models previously
813 * used in @ref step_21 "step-21" so we again refrain from commenting in detail about them.
814 *
815 * @code
816 *   namespace SingleCurvingCrack
817 *   {
818 *   template <int dim>
819 *   class KInverse : public TensorFunction<2, dim>
820 *   {
821 *   public:
822 *   KInverse()
824 *   {}
825 *  
826 *   virtual void
827 *   value_list(const std::vector<Point<dim>> &points,
828 *   std::vector<Tensor<2, dim>> &values) const override;
829 *   };
830 *  
831 *  
832 *   template <int dim>
833 *   void KInverse<dim>::value_list(const std::vector<Point<dim>> &points,
834 *   std::vector<Tensor<2, dim>> &values) const
835 *   {
836 *   AssertDimension(points.size(), values.size());
837 *  
838 *   for (unsigned int p = 0; p < points.size(); ++p)
839 *   {
840 *   values[p].clear();
841 *  
842 *   const double distance_to_flowline =
843 *   std::fabs(points[p][1] - 0.5 - 0.1 * std::sin(10 * points[p][0]));
844 *  
845 *   const double permeability =
846 *   std::max(std::exp(-(distance_to_flowline * distance_to_flowline) /
847 *   (0.1 * 0.1)),
848 *   0.01);
849 *  
850 *   for (unsigned int d = 0; d < dim; ++d)
851 *   values[p][d][d] = 1. / permeability;
852 *   }
853 *   }
854 *   } // namespace SingleCurvingCrack
855 *  
856 *  
857 *   namespace RandomMedium
858 *   {
859 *   template <int dim>
860 *   class KInverse : public TensorFunction<2, dim>
861 *   {
862 *   public:
863 *   KInverse()
865 *   {}
866 *  
867 *   virtual void
868 *   value_list(const std::vector<Point<dim>> &points,
869 *   std::vector<Tensor<2, dim>> &values) const override;
870 *  
871 *   private:
872 *   static std::vector<Point<dim>> centers;
873 *   };
874 *  
875 *  
876 *  
877 *   template <int dim>
878 *   std::vector<Point<dim>> KInverse<dim>::centers = []() {
879 *   const unsigned int N =
880 *   (dim == 2 ? 40 : (dim == 3 ? 100 : throw ExcNotImplemented()));
881 *  
882 *   std::vector<Point<dim>> centers_list(N);
883 *   for (unsigned int i = 0; i < N; ++i)
884 *   for (unsigned int d = 0; d < dim; ++d)
885 *   centers_list[i][d] = static_cast<double>(rand()) / RAND_MAX;
886 *  
887 *   return centers_list;
888 *   }();
889 *  
890 *  
891 *  
892 *   template <int dim>
893 *   void KInverse<dim>::value_list(const std::vector<Point<dim>> &points,
894 *   std::vector<Tensor<2, dim>> &values) const
895 *   {
896 *   AssertDimension(points.size(), values.size());
897 *  
898 *   for (unsigned int p = 0; p < points.size(); ++p)
899 *   {
900 *   values[p].clear();
901 *  
902 *   double permeability = 0;
903 *   for (unsigned int i = 0; i < centers.size(); ++i)
904 *   permeability +=
905 *   std::exp(-(points[p] - centers[i]).norm_square() / (0.05 * 0.05));
906 *  
907 *   const double normalized_permeability =
908 *   std::min(std::max(permeability, 0.01), 4.);
909 *  
910 *   for (unsigned int d = 0; d < dim; ++d)
911 *   values[p][d][d] = 1. / normalized_permeability;
912 *   }
913 *   }
914 *   } // namespace RandomMedium
915 *  
916 *  
917 * @endcode
918 *
919 *
920 * <a name="step_43-Physicalquantities"></a>
921 * <h3>Physical quantities</h3>
922 *
923
924 *
925 * The implementations of all the physical quantities such as total mobility
926 * @f$\lambda_t@f$ and fractional flow of water @f$F@f$ are taken from @ref step_21 "step-21" so
927 * again we don't have do any comment about them. Compared to @ref step_21 "step-21" we
928 * have added checks that the saturation passed to these functions is in
929 * fact within the physically valid range. Furthermore, given that the
930 * wetting phase moves at speed @f$\mathbf u F'(S)@f$ it is clear that @f$F'(S)@f$
931 * must be greater or equal to zero, so we assert that as well to make sure
932 * that our calculations to get at the formula for the derivative made
933 * sense.
934 *
935 * @code
936 *   double mobility_inverse(const double S, const double viscosity)
937 *   {
938 *   return 1.0 / (1.0 / viscosity * S * S + (1 - S) * (1 - S));
939 *   }
940 *  
941 *  
942 *   double fractional_flow(const double S, const double viscosity)
943 *   {
944 *   Assert((S >= 0) && (S <= 1),
945 *   ExcMessage("Saturation is outside its physically valid range."));
946 *  
947 *   return S * S / (S * S + viscosity * (1 - S) * (1 - S));
948 *   }
949 *  
950 *  
951 *   double fractional_flow_derivative(const double S, const double viscosity)
952 *   {
953 *   Assert((S >= 0) && (S <= 1),
954 *   ExcMessage("Saturation is outside its physically valid range."));
955 *  
956 *   const double temp = (S * S + viscosity * (1 - S) * (1 - S));
957 *  
958 *   const double numerator =
959 *   2.0 * S * temp - S * S * (2.0 * S - 2.0 * viscosity * (1 - S));
960 *   const double denominator = Utilities::fixed_power<2>(temp);
961 *  
962 *   const double F_prime = numerator / denominator;
963 *  
964 *   Assert(F_prime >= 0, ExcInternalError());
965 *  
966 *   return F_prime;
967 *   }
968 *  
969 *  
970 * @endcode
971 *
972 *
973 * <a name="step_43-Helperclassesforsolversandpreconditioners"></a>
974 * <h3>Helper classes for solvers and preconditioners</h3>
975 *
976
977 *
978 * In this first part we define a number of classes that we need in the
979 * construction of linear solvers and preconditioners. This part is
980 * essentially the same as that used in @ref step_31 "step-31". The only difference is that
981 * the original variable name stokes_matrix is replaced by another name
982 * darcy_matrix to match our problem.
983 *
984 * @code
985 *   namespace LinearSolvers
986 *   {
987 *   template <class MatrixType, class PreconditionerType>
988 *   class InverseMatrix : public Subscriptor
989 *   {
990 *   public:
991 *   InverseMatrix(const MatrixType &m,
992 *   const PreconditionerType &preconditioner);
993 *  
994 *  
995 *   template <typename VectorType>
996 *   void vmult(VectorType &dst, const VectorType &src) const;
997 *  
998 *   private:
999 *   const SmartPointer<const MatrixType> matrix;
1000 *   const PreconditionerType &preconditioner;
1001 *   };
1002 *  
1003 *  
1004 *   template <class MatrixType, class PreconditionerType>
1005 *   InverseMatrix<MatrixType, PreconditionerType>::InverseMatrix(
1006 *   const MatrixType &m,
1007 *   const PreconditionerType &preconditioner)
1008 *   : matrix(&m)
1009 *   , preconditioner(preconditioner)
1010 *   {}
1011 *  
1012 *  
1013 *  
1014 *   template <class MatrixType, class PreconditionerType>
1015 *   template <typename VectorType>
1016 *   void InverseMatrix<MatrixType, PreconditionerType>::vmult(
1017 *   VectorType &dst,
1018 *   const VectorType &src) const
1019 *   {
1020 *   SolverControl solver_control(src.size(), 1e-7 * src.l2_norm());
1021 *   SolverCG<VectorType> cg(solver_control);
1022 *  
1023 *   dst = 0;
1024 *  
1025 *   try
1026 *   {
1027 *   cg.solve(*matrix, dst, src, preconditioner);
1028 *   }
1029 *   catch (std::exception &e)
1030 *   {
1031 *   Assert(false, ExcMessage(e.what()));
1032 *   }
1033 *   }
1034 *  
1035 *   template <class PreconditionerTypeA, class PreconditionerTypeMp>
1036 *   class BlockSchurPreconditioner : public Subscriptor
1037 *   {
1038 *   public:
1039 *   BlockSchurPreconditioner(
1040 *   const TrilinosWrappers::BlockSparseMatrix &S,
1041 *   const InverseMatrix<TrilinosWrappers::SparseMatrix,
1042 *   PreconditionerTypeMp> &Mpinv,
1043 *   const PreconditionerTypeA &Apreconditioner);
1044 *  
1045 *   void vmult(TrilinosWrappers::MPI::BlockVector &dst,
1046 *   const TrilinosWrappers::MPI::BlockVector &src) const;
1047 *  
1048 *   private:
1049 *   const SmartPointer<const TrilinosWrappers::BlockSparseMatrix>
1050 *   darcy_matrix;
1051 *   const SmartPointer<const InverseMatrix<TrilinosWrappers::SparseMatrix,
1052 *   PreconditionerTypeMp>>
1053 *   m_inverse;
1054 *   const PreconditionerTypeA &a_preconditioner;
1055 *  
1056 *   mutable TrilinosWrappers::MPI::Vector tmp;
1057 *   };
1058 *  
1059 *  
1060 *  
1061 *   template <class PreconditionerTypeA, class PreconditionerTypeMp>
1062 *   BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::
1063 *   BlockSchurPreconditioner(
1064 *   const TrilinosWrappers::BlockSparseMatrix &S,
1065 *   const InverseMatrix<TrilinosWrappers::SparseMatrix,
1066 *   PreconditionerTypeMp> &Mpinv,
1067 *   const PreconditionerTypeA &Apreconditioner)
1068 *   : darcy_matrix(&S)
1069 *   , m_inverse(&Mpinv)
1070 *   , a_preconditioner(Apreconditioner)
1071 *   , tmp(complete_index_set(darcy_matrix->block(1, 1).m()))
1072 *   {}
1073 *  
1074 *  
1075 *   template <class PreconditionerTypeA, class PreconditionerTypeMp>
1076 *   void
1077 *   BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::vmult(
1078 *   TrilinosWrappers::MPI::BlockVector &dst,
1079 *   const TrilinosWrappers::MPI::BlockVector &src) const
1080 *   {
1081 *   a_preconditioner.vmult(dst.block(0), src.block(0));
1082 *   darcy_matrix->block(1, 0).residual(tmp, dst.block(0), src.block(1));
1083 *   tmp *= -1;
1084 *   m_inverse->vmult(dst.block(1), tmp);
1085 *   }
1086 *   } // namespace LinearSolvers
1087 *  
1088 *  
1089 * @endcode
1090 *
1091 *
1092 * <a name="step_43-TheTwoPhaseFlowProblemclass"></a>
1093 * <h3>The TwoPhaseFlowProblem class</h3>
1094 *
1095
1096 *
1097 * The definition of the class that defines the top-level logic of solving
1098 * the time-dependent advection-dominated two-phase flow problem (or
1099 * Buckley-Leverett problem @cite Buckley1942) is mainly based on tutorial
1100 * programs @ref step_21 "step-21" and @ref step_33 "step-33", and in particular on @ref step_31 "step-31" where we have
1101 * used basically the same general structure as done here. As in @ref step_31 "step-31",
1102 * the key routines to look for in the implementation below are the
1103 * <code>run()</code> and <code>solve()</code> functions.
1104 *
1105
1106 *
1107 * The main difference to @ref step_31 "step-31" is that, since adaptive operator splitting
1108 * is considered, we need a couple more member variables to hold the last
1109 * two computed Darcy (velocity/pressure) solutions in addition to the
1110 * current one (which is either computed directly, or extrapolated from the
1111 * previous two), and we need to remember the last two times we computed the
1112 * Darcy solution. We also need a helper function that figures out whether
1113 * we do indeed need to recompute the Darcy solution.
1114 *
1115
1116 *
1117 * Unlike @ref step_31 "step-31", this step uses one more AffineConstraints object called
1118 * darcy_preconditioner_constraints. This constraint object is used only for
1119 * assembling the matrix for the Darcy preconditioner and includes hanging
1120 * node constraints as well as Dirichlet boundary value constraints for the
1121 * pressure variable. We need this because we are building a Laplace matrix
1122 * for the pressure as an approximation of the Schur complement) which is
1123 * only positive definite if boundary conditions are applied.
1124 *
1125
1126 *
1127 * The collection of member functions and variables thus declared in this
1128 * class is then rather similar to those in @ref step_31 "step-31":
1129 *
1130 * @code
1131 *   template <int dim>
1132 *   class TwoPhaseFlowProblem
1133 *   {
1134 *   public:
1135 *   TwoPhaseFlowProblem(const unsigned int degree);
1136 *   void run();
1137 *  
1138 *   private:
1139 *   void setup_dofs();
1140 *   void assemble_darcy_preconditioner();
1141 *   void build_darcy_preconditioner();
1142 *   void assemble_darcy_system();
1143 *   void assemble_saturation_system();
1144 *   void assemble_saturation_matrix();
1145 *   void assemble_saturation_rhs();
1146 *   void assemble_saturation_rhs_cell_term(
1147 *   const FEValues<dim> &saturation_fe_values,
1148 *   const FEValues<dim> &darcy_fe_values,
1149 *   const double global_max_u_F_prime,
1150 *   const double global_S_variation,
1151 *   const std::vector<types::global_dof_index> &local_dof_indices);
1152 *   void assemble_saturation_rhs_boundary_term(
1153 *   const FEFaceValues<dim> &saturation_fe_face_values,
1154 *   const FEFaceValues<dim> &darcy_fe_face_values,
1155 *   const std::vector<types::global_dof_index> &local_dof_indices);
1156 *   void solve();
1157 *   void refine_mesh(const unsigned int min_grid_level,
1158 *   const unsigned int max_grid_level);
1159 *   void output_results() const;
1160 *  
1161 * @endcode
1162 *
1163 * We follow with a number of helper functions that are used in a variety
1164 * of places throughout the program:
1165 *
1166 * @code
1167 *   double get_max_u_F_prime() const;
1168 *   std::pair<double, double> get_extrapolated_saturation_range() const;
1169 *   bool determine_whether_to_solve_for_pressure_and_velocity() const;
1170 *   void project_back_saturation();
1171 *   double compute_viscosity(
1172 *   const std::vector<double> &old_saturation,
1173 *   const std::vector<double> &old_old_saturation,
1174 *   const std::vector<Tensor<1, dim>> &old_saturation_grads,
1175 *   const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
1176 *   const std::vector<Vector<double>> &present_darcy_values,
1177 *   const double global_max_u_F_prime,
1178 *   const double global_S_variation,
1179 *   const double cell_diameter) const;
1180 *  
1181 *  
1182 * @endcode
1183 *
1184 * This all is followed by the member variables, most of which are similar
1185 * to the ones in @ref step_31 "step-31", with the exception of the ones that pertain to
1186 * the macro time stepping for the velocity/pressure system:
1187 *
1188 * @code
1189 *   Triangulation<dim> triangulation;
1190 *   double global_Omega_diameter;
1191 *  
1192 *   const unsigned int degree;
1193 *  
1194 *   const unsigned int darcy_degree;
1195 *   const FESystem<dim> darcy_fe;
1196 *   DoFHandler<dim> darcy_dof_handler;
1197 *   AffineConstraints<double> darcy_constraints;
1198 *  
1199 *   AffineConstraints<double> darcy_preconditioner_constraints;
1200 *  
1201 *   TrilinosWrappers::BlockSparseMatrix darcy_matrix;
1202 *   TrilinosWrappers::BlockSparseMatrix darcy_preconditioner_matrix;
1203 *  
1204 *   TrilinosWrappers::MPI::BlockVector darcy_solution;
1205 *   TrilinosWrappers::MPI::BlockVector darcy_rhs;
1206 *  
1207 *   TrilinosWrappers::MPI::BlockVector last_computed_darcy_solution;
1208 *   TrilinosWrappers::MPI::BlockVector second_last_computed_darcy_solution;
1209 *  
1210 *  
1211 *   const unsigned int saturation_degree;
1212 *   const FE_Q<dim> saturation_fe;
1213 *   DoFHandler<dim> saturation_dof_handler;
1214 *   AffineConstraints<double> saturation_constraints;
1215 *  
1216 *   TrilinosWrappers::SparseMatrix saturation_matrix;
1217 *  
1218 *  
1219 *   TrilinosWrappers::MPI::Vector saturation_solution;
1220 *   TrilinosWrappers::MPI::Vector old_saturation_solution;
1221 *   TrilinosWrappers::MPI::Vector old_old_saturation_solution;
1222 *   TrilinosWrappers::MPI::Vector saturation_rhs;
1223 *  
1224 *   TrilinosWrappers::MPI::Vector
1225 *   saturation_matching_last_computed_darcy_solution;
1226 *  
1227 *   const double saturation_refinement_threshold;
1228 *  
1229 *   double time;
1230 *   const double end_time;
1231 *  
1232 *   double current_macro_time_step;
1233 *   double old_macro_time_step;
1234 *  
1235 *   double time_step;
1236 *   double old_time_step;
1237 *   unsigned int timestep_number;
1238 *  
1239 *   const double viscosity;
1240 *   const double porosity;
1241 *   const double AOS_threshold;
1242 *  
1243 *   std::shared_ptr<TrilinosWrappers::PreconditionIC> top_left_preconditioner;
1244 *   std::shared_ptr<TrilinosWrappers::PreconditionIC>
1245 *   bottom_right_preconditioner;
1246 *  
1247 *   bool rebuild_saturation_matrix;
1248 *  
1249 * @endcode
1250 *
1251 * At the very end we declare a variable that denotes the material
1252 * model. Compared to @ref step_21 "step-21", we do this here as a member variable since
1253 * we will want to use it in a variety of places and so having a central
1254 * place where such a variable is declared will make it simpler to replace
1255 * one class by another (e.g. replace RandomMedium::KInverse by
1256 * SingleCurvingCrack::KInverse).
1257 *
1258 * @code
1259 *   const RandomMedium::KInverse<dim> k_inverse;
1260 *   };
1261 *  
1262 *  
1263 * @endcode
1264 *
1265 *
1266 * <a name="step_43-TwoPhaseFlowProblemdimTwoPhaseFlowProblem"></a>
1267 * <h3>TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem</h3>
1268 *
1269
1270 *
1271 * The constructor of this class is an extension of the constructors in
1272 * @ref step_21 "step-21" and @ref step_31 "step-31". We need to add the various variables that concern
1273 * the saturation. As discussed in the introduction, we are going to use
1274 * @f$Q_2 \times Q_1@f$ (Taylor-Hood) elements again for the Darcy system, an
1275 * element combination that fulfills the Ladyzhenskaya-Babuska-Brezzi (LBB)
1276 * conditions [Brezzi and Fortin 1991, Chen 2005], and @f$Q_1@f$ elements for
1277 * the saturation. However, by using variables that store the polynomial
1278 * degree of the Darcy and temperature finite elements, it is easy to
1279 * consistently modify the degree of the elements as well as all quadrature
1280 * formulas used on them downstream. Moreover, we initialize the time
1281 * stepping variables related to operator splitting as well as the option
1282 * for matrix assembly and preconditioning:
1283 *
1284 * @code
1285 *   template <int dim>
1286 *   TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem(const unsigned int degree)
1287 *   : triangulation(Triangulation<dim>::maximum_smoothing)
1288 *   , global_Omega_diameter(std::numeric_limits<double>::quiet_NaN())
1289 *   , degree(degree)
1290 *   , darcy_degree(degree)
1291 *   , darcy_fe(FE_Q<dim>(darcy_degree + 1) ^ dim, FE_Q<dim>(darcy_degree))
1292 *   , darcy_dof_handler(triangulation)
1293 *   ,
1294 *  
1295 *   saturation_degree(degree + 1)
1296 *   , saturation_fe(saturation_degree)
1297 *   , saturation_dof_handler(triangulation)
1298 *   ,
1299 *  
1300 *   saturation_refinement_threshold(0.5)
1301 *   ,
1302 *  
1303 *   time(0)
1304 *   , end_time(10)
1305 *   ,
1306 *  
1307 *   current_macro_time_step(0)
1308 *   , old_macro_time_step(0)
1309 *   ,
1310 *  
1311 *   time_step(0)
1312 *   , old_time_step(0)
1313 *   , timestep_number(0)
1314 *   , viscosity(0.2)
1315 *   , porosity(1.0)
1316 *   , AOS_threshold(3.0)
1317 *   ,
1318 *  
1319 *   rebuild_saturation_matrix(true)
1320 *   {}
1321 *  
1322 *  
1323 * @endcode
1324 *
1325 *
1326 * <a name="step_43-TwoPhaseFlowProblemdimsetup_dofs"></a>
1327 * <h3>TwoPhaseFlowProblem<dim>::setup_dofs</h3>
1328 *
1329
1330 *
1331 * This is the function that sets up the DoFHandler objects we have here
1332 * (one for the Darcy part and one for the saturation part) as well as set
1333 * to the right sizes the various objects required for the linear algebra in
1334 * this program. Its basic operations are similar to what @ref step_31 "step-31" did.
1335 *
1336
1337 *
1338 * The body of the function first enumerates all degrees of freedom for the
1339 * Darcy and saturation systems. For the Darcy part, degrees of freedom are
1340 * then sorted to ensure that velocities precede pressure DoFs so that we
1341 * can partition the Darcy matrix into a @f$2 \times 2@f$ matrix.
1342 *
1343
1344 *
1345 * Then, we need to incorporate hanging node constraints and Dirichlet
1346 * boundary value constraints into darcy_preconditioner_constraints. The
1347 * boundary condition constraints are only set on the pressure component
1348 * since the Schur complement preconditioner that corresponds to the porous
1349 * media flow operator in non-mixed form, @f$-\nabla \cdot [\mathbf K
1350 * \lambda_t(S)]\nabla@f$, acts only on the pressure variable. Therefore, we
1351 * use a component_mask that filters out the velocity component, so that the
1352 * condensation is performed on pressure degrees of freedom only.
1353 *
1354
1355 *
1356 * After having done so, we count the number of degrees of freedom in the
1357 * various blocks. This information is then used to create the sparsity
1358 * pattern for the Darcy and saturation system matrices as well as the
1359 * preconditioner matrix from which we build the Darcy preconditioner. As in
1360 * @ref step_31 "step-31", we choose to create the pattern using the blocked version of
1361 * DynamicSparsityPattern. So, for this, we follow the same way as @ref step_31 "step-31"
1362 * did and we don't have to repeat descriptions again for the rest of the
1363 * member function.
1364 *
1365 * @code
1366 *   template <int dim>
1367 *   void TwoPhaseFlowProblem<dim>::setup_dofs()
1368 *   {
1369 *   std::vector<unsigned int> darcy_block_component(dim + 1, 0);
1370 *   darcy_block_component[dim] = 1;
1371 *   {
1372 *   darcy_dof_handler.distribute_dofs(darcy_fe);
1373 *   DoFRenumbering::Cuthill_McKee(darcy_dof_handler);
1374 *   DoFRenumbering::component_wise(darcy_dof_handler, darcy_block_component);
1375 *  
1376 *   darcy_constraints.clear();
1377 *   DoFTools::make_hanging_node_constraints(darcy_dof_handler,
1378 *   darcy_constraints);
1379 *   darcy_constraints.close();
1380 *   }
1381 *   {
1382 *   saturation_dof_handler.distribute_dofs(saturation_fe);
1383 *  
1384 *   saturation_constraints.clear();
1385 *   DoFTools::make_hanging_node_constraints(saturation_dof_handler,
1386 *   saturation_constraints);
1387 *   saturation_constraints.close();
1388 *   }
1389 *   {
1390 *   darcy_preconditioner_constraints.clear();
1391 *  
1392 *   const FEValuesExtractors::Scalar pressure(dim);
1393 *  
1394 *   DoFTools::make_hanging_node_constraints(darcy_dof_handler,
1395 *   darcy_preconditioner_constraints);
1396 *   DoFTools::make_zero_boundary_constraints(darcy_dof_handler,
1397 *   darcy_preconditioner_constraints,
1398 *   darcy_fe.component_mask(
1399 *   pressure));
1400 *  
1401 *   darcy_preconditioner_constraints.close();
1402 *   }
1403 *  
1404 *  
1405 *   const std::vector<types::global_dof_index> darcy_dofs_per_block =
1406 *   DoFTools::count_dofs_per_fe_block(darcy_dof_handler,
1407 *   darcy_block_component);
1408 *   const types::global_dof_index n_u = darcy_dofs_per_block[0],
1409 *   n_p = darcy_dofs_per_block[1],
1410 *   n_s = saturation_dof_handler.n_dofs();
1411 *  
1412 *   std::cout << "Number of active cells: " << triangulation.n_active_cells()
1413 *   << " (on " << triangulation.n_levels() << " levels)" << std::endl
1414 *   << "Number of degrees of freedom: " << n_u + n_p + n_s << " ("
1415 *   << n_u << '+' << n_p << '+' << n_s << ')' << std::endl
1416 *   << std::endl;
1417 *  
1418 *   {
1419 *   darcy_matrix.clear();
1420 *  
1421 *   BlockDynamicSparsityPattern dsp(darcy_dofs_per_block,
1422 *   darcy_dofs_per_block);
1423 *  
1424 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
1425 *   for (unsigned int c = 0; c < dim + 1; ++c)
1426 *   for (unsigned int d = 0; d < dim + 1; ++d)
1427 *   if (!((c == dim) && (d == dim)))
1428 *   coupling[c][d] = DoFTools::always;
1429 *   else
1430 *   coupling[c][d] = DoFTools::none;
1431 *  
1432 *  
1434 *   darcy_dof_handler, coupling, dsp, darcy_constraints, false);
1435 *  
1436 *   darcy_matrix.reinit(dsp);
1437 *   }
1438 *  
1439 *   {
1440 *   top_left_preconditioner.reset();
1441 *   bottom_right_preconditioner.reset();
1442 *   darcy_preconditioner_matrix.clear();
1443 *  
1444 *   BlockDynamicSparsityPattern dsp(darcy_dofs_per_block,
1445 *   darcy_dofs_per_block);
1446 *  
1447 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
1448 *   for (unsigned int c = 0; c < dim + 1; ++c)
1449 *   for (unsigned int d = 0; d < dim + 1; ++d)
1450 *   if (c == d)
1451 *   coupling[c][d] = DoFTools::always;
1452 *   else
1453 *   coupling[c][d] = DoFTools::none;
1454 *  
1456 *   darcy_dof_handler, coupling, dsp, darcy_constraints, false);
1457 *  
1458 *   darcy_preconditioner_matrix.reinit(dsp);
1459 *   }
1460 *  
1461 *  
1462 *   {
1463 *   saturation_matrix.clear();
1464 *  
1465 *   DynamicSparsityPattern dsp(n_s, n_s);
1466 *  
1467 *   DoFTools::make_sparsity_pattern(saturation_dof_handler,
1468 *   dsp,
1469 *   saturation_constraints,
1470 *   false);
1471 *  
1472 *  
1473 *   saturation_matrix.reinit(dsp);
1474 *   }
1475 *  
1476 *   const std::vector<IndexSet> darcy_partitioning = {complete_index_set(n_u),
1477 *   complete_index_set(n_p)};
1478 *  
1479 *   darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1480 *  
1481 *   last_computed_darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1482 *  
1483 *   second_last_computed_darcy_solution.reinit(darcy_partitioning,
1484 *   MPI_COMM_WORLD);
1485 *  
1486 *   darcy_rhs.reinit(darcy_partitioning, MPI_COMM_WORLD);
1487 *  
1488 *   const IndexSet saturation_partitioning = complete_index_set(n_s);
1489 *   saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1490 *   old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1491 *   old_old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1492 *  
1493 *   saturation_matching_last_computed_darcy_solution.reinit(
1494 *   saturation_partitioning, MPI_COMM_WORLD);
1495 *  
1496 *   saturation_rhs.reinit(saturation_partitioning, MPI_COMM_WORLD);
1497 *   }
1498 *  
1499 *  
1500 * @endcode
1501 *
1502 *
1503 * <a name="step_43-Assemblingmatricesandpreconditioners"></a>
1504 * <h3>Assembling matrices and preconditioners</h3>
1505 *
1506
1507 *
1508 * The next few functions are devoted to setting up the various system and
1509 * preconditioner matrices and right hand sides that we have to deal with in
1510 * this program.
1511 *
1512
1513 *
1514 *
1515 * <a name="step_43-TwoPhaseFlowProblemdimassemble_darcy_preconditioner"></a>
1516 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner</h4>
1517 *
1518
1519 *
1520 * This function assembles the matrix we use for preconditioning the Darcy
1521 * system. What we need are a vector @ref GlossMassMatrix "mass matrix" weighted by
1522 * @f$\left(\mathbf{K} \lambda_t\right)^{-1}@f$ on the velocity components and a
1523 * mass matrix weighted by @f$\left(\mathbf{K} \lambda_t\right)@f$ on the
1524 * pressure component. We start by generating a quadrature object of
1525 * appropriate order, the FEValues object that can give values and gradients
1526 * at the quadrature points (together with quadrature weights). Next we
1527 * create data structures for the cell matrix and the relation between local
1528 * and global DoFs. The vectors phi_u and grad_phi_p are going to hold the
1529 * values of the basis functions in order to faster build up the local
1530 * matrices, as was already done in @ref step_22 "step-22". Before we start the loop over
1531 * all active cells, we have to specify which components are pressure and
1532 * which are velocity.
1533 *
1534
1535 *
1536 * The creation of the local matrix is rather simple. There are only a term
1537 * weighted by @f$\left(\mathbf{K} \lambda_t\right)^{-1}@f$ (on the velocity)
1538 * and a Laplace matrix weighted by @f$\left(\mathbf{K} \lambda_t\right)@f$ to
1539 * be generated, so the creation of the local matrix is done in essentially
1540 * two lines. Since the material model functions at the top of this file
1541 * only provide the inverses of the permeability and mobility, we have to
1542 * compute @f$\mathbf K@f$ and @f$\lambda_t@f$ by hand from the given values, once
1543 * per quadrature point.
1544 *
1545
1546 *
1547 * Once the local matrix is ready (loop over rows and columns in the local
1548 * matrix on each quadrature point), we get the local DoF indices and write
1549 * the local information into the global matrix. We do this by directly
1550 * applying the constraints (i.e. darcy_preconditioner_constraints) that
1551 * takes care of hanging node and zero Dirichlet boundary condition
1552 * constraints. By doing so, we don't have to do that afterwards, and we
1553 * later don't have to use AffineConstraints::condense and
1554 * MatrixTools::apply_boundary_values, both functions that would need to
1555 * modify matrix and vector entries and so are difficult to write for the
1556 * Trilinos classes where we don't immediately have access to individual
1557 * memory locations.
1558 *
1559 * @code
1560 *   template <int dim>
1561 *   void TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner()
1562 *   {
1563 *   std::cout << " Rebuilding darcy preconditioner..." << std::endl;
1564 *  
1565 *   darcy_preconditioner_matrix = 0;
1566 *  
1567 *   const QGauss<dim> quadrature_formula(darcy_degree + 2);
1568 *   FEValues<dim> darcy_fe_values(darcy_fe,
1569 *   quadrature_formula,
1570 *   update_JxW_values | update_values |
1571 *   update_gradients |
1572 *   update_quadrature_points);
1573 *   FEValues<dim> saturation_fe_values(saturation_fe,
1574 *   quadrature_formula,
1575 *   update_values);
1576 *  
1577 *   const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1578 *   const unsigned int n_q_points = quadrature_formula.size();
1579 *  
1580 *   std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1581 *  
1582 *   std::vector<double> old_saturation_values(n_q_points);
1583 *  
1584 *   FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1585 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1586 *  
1587 *   std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1588 *   std::vector<Tensor<1, dim>> grad_phi_p(dofs_per_cell);
1589 *  
1590 *   const FEValuesExtractors::Vector velocities(0);
1591 *   const FEValuesExtractors::Scalar pressure(dim);
1592 *  
1593 *   auto cell = darcy_dof_handler.begin_active();
1594 *   const auto endc = darcy_dof_handler.end();
1595 *   auto saturation_cell = saturation_dof_handler.begin_active();
1596 *  
1597 *   for (; cell != endc; ++cell, ++saturation_cell)
1598 *   {
1599 *   darcy_fe_values.reinit(cell);
1600 *   saturation_fe_values.reinit(saturation_cell);
1601 *  
1602 *   local_matrix = 0;
1603 *  
1604 *   saturation_fe_values.get_function_values(old_saturation_solution,
1605 *   old_saturation_values);
1606 *  
1607 *   k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1608 *   k_inverse_values);
1609 *  
1610 *   for (unsigned int q = 0; q < n_q_points; ++q)
1611 *   {
1612 *   const double old_s = old_saturation_values[q];
1613 *  
1614 *   const double inverse_mobility = mobility_inverse(old_s, viscosity);
1615 *   const double mobility = 1.0 / inverse_mobility;
1616 *   const Tensor<2, dim> permeability = invert(k_inverse_values[q]);
1617 *  
1618 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
1619 *   {
1620 *   phi_u[k] = darcy_fe_values[velocities].value(k, q);
1621 *   grad_phi_p[k] = darcy_fe_values[pressure].gradient(k, q);
1622 *   }
1623 *  
1624 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1625 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1626 *   {
1627 *   local_matrix(i, j) +=
1628 *   (k_inverse_values[q] * inverse_mobility * phi_u[i] *
1629 *   phi_u[j] +
1630 *   permeability * mobility * grad_phi_p[i] * grad_phi_p[j]) *
1631 *   darcy_fe_values.JxW(q);
1632 *   }
1633 *   }
1634 *  
1635 *   cell->get_dof_indices(local_dof_indices);
1636 *   darcy_preconditioner_constraints.distribute_local_to_global(
1637 *   local_matrix, local_dof_indices, darcy_preconditioner_matrix);
1638 *   }
1639 *   }
1640 *  
1641 *  
1642 * @endcode
1643 *
1644 *
1645 * <a name="step_43-TwoPhaseFlowProblemdimbuild_darcy_preconditioner"></a>
1646 * <h4>TwoPhaseFlowProblem<dim>::build_darcy_preconditioner</h4>
1647 *
1648
1649 *
1650 * After calling the above functions to assemble the preconditioner matrix,
1651 * this function generates the inner preconditioners that are going to be
1652 * used for the Schur complement block preconditioner. The preconditioners
1653 * need to be regenerated at every saturation time step since they depend on
1654 * the saturation @f$S@f$ that varies with time.
1655 *
1656
1657 *
1658 * In here, we set up the preconditioner for the velocity-velocity matrix
1659 * @f$\mathbf{M}^{\mathbf{u}}@f$ and the Schur complement @f$\mathbf{S}@f$. As
1660 * explained in the introduction, we are going to use an IC preconditioner
1661 * based on the vector matrix @f$\mathbf{M}^{\mathbf{u}}@f$ and another based on
1662 * the scalar Laplace matrix @f$\tilde{\mathbf{S}}^p@f$ (which is spectrally
1663 * close to the Schur complement of the Darcy matrix). Usually, the
1664 * TrilinosWrappers::PreconditionIC class can be seen as a good black-box
1665 * preconditioner which does not need any special knowledge of the matrix
1666 * structure and/or the operator that's behind it.
1667 *
1668 * @code
1669 *   template <int dim>
1670 *   void TwoPhaseFlowProblem<dim>::build_darcy_preconditioner()
1671 *   {
1672 *   assemble_darcy_preconditioner();
1673 *  
1674 *   top_left_preconditioner =
1675 *   std::make_shared<TrilinosWrappers::PreconditionIC>();
1676 *   top_left_preconditioner->initialize(
1677 *   darcy_preconditioner_matrix.block(0, 0));
1678 *  
1679 *   bottom_right_preconditioner =
1680 *   std::make_shared<TrilinosWrappers::PreconditionIC>();
1681 *   bottom_right_preconditioner->initialize(
1682 *   darcy_preconditioner_matrix.block(1, 1));
1683 *   }
1684 *  
1685 *  
1686 * @endcode
1687 *
1688 *
1689 * <a name="step_43-TwoPhaseFlowProblemdimassemble_darcy_system"></a>
1690 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_system</h4>
1691 *
1692
1693 *
1694 * This is the function that assembles the linear system for the Darcy
1695 * system.
1696 *
1697
1698 *
1699 * Regarding the technical details of implementation, the procedures are
1700 * similar to those in @ref step_22 "step-22" and @ref step_31 "step-31". We reset matrix and vector,
1701 * create a quadrature formula on the cells, and then create the respective
1702 * FEValues object.
1703 *
1704
1705 *
1706 * There is one thing that needs to be commented: since we have a separate
1707 * finite element and DoFHandler for the saturation, we need to generate a
1708 * second FEValues object for the proper evaluation of the saturation
1709 * solution. This isn't too complicated to realize here: just use the
1710 * saturation structures and set an update flag for the basis function
1711 * values which we need for evaluation of the saturation solution. The only
1712 * important part to remember here is that the same quadrature formula is
1713 * used for both FEValues objects to ensure that we get matching information
1714 * when we loop over the quadrature points of the two objects.
1715 *
1716
1717 *
1718 * The declarations proceed with some shortcuts for array sizes, the
1719 * creation of the local matrix, right hand side as well as the vector for
1720 * the indices of the local dofs compared to the global system.
1721 *
1722 * @code
1723 *   template <int dim>
1724 *   void TwoPhaseFlowProblem<dim>::assemble_darcy_system()
1725 *   {
1726 *   darcy_matrix = 0;
1727 *   darcy_rhs = 0;
1728 *  
1729 *   const QGauss<dim> quadrature_formula(darcy_degree + 2);
1730 *   const QGauss<dim - 1> face_quadrature_formula(darcy_degree + 2);
1731 *  
1732 *   FEValues<dim> darcy_fe_values(darcy_fe,
1733 *   quadrature_formula,
1734 *   update_values | update_gradients |
1735 *   update_quadrature_points |
1736 *   update_JxW_values);
1737 *  
1738 *   FEValues<dim> saturation_fe_values(saturation_fe,
1739 *   quadrature_formula,
1740 *   update_values);
1741 *  
1742 *   FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
1743 *   face_quadrature_formula,
1744 *   update_values |
1745 *   update_normal_vectors |
1746 *   update_quadrature_points |
1747 *   update_JxW_values);
1748 *  
1749 *   const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1750 *  
1751 *   const unsigned int n_q_points = quadrature_formula.size();
1752 *   const unsigned int n_face_q_points = face_quadrature_formula.size();
1753 *  
1754 *   FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1755 *   Vector<double> local_rhs(dofs_per_cell);
1756 *  
1757 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1758 *  
1759 *   const Functions::ZeroFunction<dim> pressure_right_hand_side;
1760 *   const PressureBoundaryValues<dim> pressure_boundary_values;
1761 *  
1762 *   std::vector<double> pressure_rhs_values(n_q_points);
1763 *   std::vector<double> boundary_values(n_face_q_points);
1764 *   std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1765 *  
1766 * @endcode
1767 *
1768 * Next we need a vector that will contain the values of the saturation
1769 * solution at the previous time level at the quadrature points to
1770 * assemble the saturation dependent coefficients in the Darcy equations.
1771 *
1772
1773 *
1774 * The set of vectors we create next hold the evaluations of the basis
1775 * functions as well as their gradients that will be used for creating the
1776 * matrices. Putting these into their own arrays rather than asking the
1777 * FEValues object for this information each time it is needed is an
1778 * optimization to accelerate the assembly process, see @ref step_22 "step-22" for
1779 * details.
1780 *
1781
1782 *
1783 * The last two declarations are used to extract the individual blocks
1784 * (velocity, pressure, saturation) from the total FE system.
1785 *
1786 * @code
1787 *   std::vector<double> old_saturation_values(n_q_points);
1788 *  
1789 *   std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1790 *   std::vector<double> div_phi_u(dofs_per_cell);
1791 *   std::vector<double> phi_p(dofs_per_cell);
1792 *  
1793 *   const FEValuesExtractors::Vector velocities(0);
1794 *   const FEValuesExtractors::Scalar pressure(dim);
1795 *  
1796 * @endcode
1797 *
1798 * Now start the loop over all cells in the problem. We are working on two
1799 * different DoFHandlers for this assembly routine, so we must have two
1800 * different cell iterators for the two objects in use. This might seem a
1801 * bit peculiar, but since both the Darcy system and the saturation system
1802 * use the same grid we can assume that the two iterators run in sync over
1803 * the cells of the two DoFHandler objects.
1804 *
1805
1806 *
1807 * The first statements within the loop are again all very familiar, doing
1808 * the update of the finite element data as specified by the update flags,
1809 * zeroing out the local arrays and getting the values of the old solution
1810 * at the quadrature points. At this point we also have to get the values
1811 * of the saturation function of the previous time step at the quadrature
1812 * points. To this end, we can use the FEValues::get_function_values
1813 * (previously already used in @ref step_9 "step-9", @ref step_14 "step-14" and @ref step_15 "step-15"), a function
1814 * that takes a solution vector and returns a list of function values at
1815 * the quadrature points of the present cell. In fact, it returns the
1816 * complete vector-valued solution at each quadrature point, i.e. not only
1817 * the saturation but also the velocities and pressure.
1818 *
1819
1820 *
1821 * Then we are ready to loop over the quadrature points on the cell to do
1822 * the integration. The formula for this follows in a straightforward way
1823 * from what has been discussed in the introduction.
1824 *
1825
1826 *
1827 * Once this is done, we start the loop over the rows and columns of the
1828 * local matrix and feed the matrix with the relevant products.
1829 *
1830
1831 *
1832 * The last step in the loop over all cells is to enter the local
1833 * contributions into the global matrix and vector structures to the
1834 * positions specified in local_dof_indices. Again, we let the
1835 * AffineConstraints class do the insertion of the cell matrix
1836 * elements to the global matrix, which already condenses the hanging node
1837 * constraints.
1838 *
1839 * @code
1840 *   auto cell = darcy_dof_handler.begin_active();
1841 *   const auto endc = darcy_dof_handler.end();
1842 *   auto saturation_cell = saturation_dof_handler.begin_active();
1843 *  
1844 *   for (; cell != endc; ++cell, ++saturation_cell)
1845 *   {
1846 *   darcy_fe_values.reinit(cell);
1847 *   saturation_fe_values.reinit(saturation_cell);
1848 *  
1849 *   local_matrix = 0;
1850 *   local_rhs = 0;
1851 *  
1852 *   saturation_fe_values.get_function_values(old_saturation_solution,
1853 *   old_saturation_values);
1854 *  
1855 *   pressure_right_hand_side.value_list(
1856 *   darcy_fe_values.get_quadrature_points(), pressure_rhs_values);
1857 *   k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1858 *   k_inverse_values);
1859 *  
1860 *   for (unsigned int q = 0; q < n_q_points; ++q)
1861 *   {
1862 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
1863 *   {
1864 *   phi_u[k] = darcy_fe_values[velocities].value(k, q);
1865 *   div_phi_u[k] = darcy_fe_values[velocities].divergence(k, q);
1866 *   phi_p[k] = darcy_fe_values[pressure].value(k, q);
1867 *   }
1868 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1869 *   {
1870 *   const double old_s = old_saturation_values[q];
1871 *   for (unsigned int j = 0; j <= i; ++j)
1872 *   {
1873 *   local_matrix(i, j) +=
1874 *   (phi_u[i] * k_inverse_values[q] *
1875 *   mobility_inverse(old_s, viscosity) * phi_u[j] -
1876 *   div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
1877 *   darcy_fe_values.JxW(q);
1878 *   }
1879 *  
1880 *   local_rhs(i) +=
1881 *   (-phi_p[i] * pressure_rhs_values[q]) * darcy_fe_values.JxW(q);
1882 *   }
1883 *   }
1884 *  
1885 *   for (const auto &face : cell->face_iterators())
1886 *   if (face->at_boundary())
1887 *   {
1888 *   darcy_fe_face_values.reinit(cell, face);
1889 *  
1890 *   pressure_boundary_values.value_list(
1891 *   darcy_fe_face_values.get_quadrature_points(), boundary_values);
1892 *  
1893 *   for (unsigned int q = 0; q < n_face_q_points; ++q)
1894 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1895 *   {
1896 *   const Tensor<1, dim> phi_i_u =
1897 *   darcy_fe_face_values[velocities].value(i, q);
1898 *  
1899 *   local_rhs(i) +=
1900 *   -(phi_i_u * darcy_fe_face_values.normal_vector(q) *
1901 *   boundary_values[q] * darcy_fe_face_values.JxW(q));
1902 *   }
1903 *   }
1904 *  
1905 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1906 *   for (unsigned int j = i + 1; j < dofs_per_cell; ++j)
1907 *   local_matrix(i, j) = local_matrix(j, i);
1908 *  
1909 *   cell->get_dof_indices(local_dof_indices);
1910 *  
1911 *   darcy_constraints.distribute_local_to_global(
1912 *   local_matrix, local_rhs, local_dof_indices, darcy_matrix, darcy_rhs);
1913 *   }
1914 *   }
1915 *  
1916 *  
1917 * @endcode
1918 *
1919 *
1920 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_system"></a>
1921 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_system</h4>
1922 *
1923
1924 *
1925 * This function is to assemble the linear system for the saturation
1926 * transport equation. It calls, if necessary, two other member functions:
1927 * assemble_saturation_matrix() and assemble_saturation_rhs(). The former
1928 * function then assembles the saturation matrix that only needs to be
1929 * changed occasionally. On the other hand, the latter function that
1930 * assembles the right hand side must be called at every saturation time
1931 * step.
1932 *
1933 * @code
1934 *   template <int dim>
1935 *   void TwoPhaseFlowProblem<dim>::assemble_saturation_system()
1936 *   {
1937 *   if (rebuild_saturation_matrix == true)
1938 *   {
1939 *   saturation_matrix = 0;
1940 *   assemble_saturation_matrix();
1941 *   }
1942 *  
1943 *   saturation_rhs = 0;
1944 *   assemble_saturation_rhs();
1945 *   }
1946 *  
1947 *  
1948 *  
1949 * @endcode
1950 *
1951 *
1952 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_matrix"></a>
1953 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_matrix</h4>
1954 *
1955
1956 *
1957 * This function is easily understood since it only forms a simple mass
1958 * matrix for the left hand side of the saturation linear system by basis
1959 * functions phi_i_s and phi_j_s only. Finally, as usual, we enter the local
1960 * contribution into the global matrix by specifying the position in
1961 * local_dof_indices. This is done by letting the AffineConstraints class do
1962 * the insertion of the cell matrix elements to the global matrix, which
1963 * already condenses the hanging node constraints.
1964 *
1965 * @code
1966 *   template <int dim>
1967 *   void TwoPhaseFlowProblem<dim>::assemble_saturation_matrix()
1968 *   {
1969 *   const QGauss<dim> quadrature_formula(saturation_degree + 2);
1970 *  
1971 *   FEValues<dim> saturation_fe_values(saturation_fe,
1972 *   quadrature_formula,
1973 *   update_values | update_JxW_values);
1974 *  
1975 *   const unsigned int dofs_per_cell = saturation_fe.n_dofs_per_cell();
1976 *  
1977 *   const unsigned int n_q_points = quadrature_formula.size();
1978 *  
1979 *   FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1980 *   Vector<double> local_rhs(dofs_per_cell);
1981 *  
1982 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1983 *  
1984 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
1985 *   {
1986 *   saturation_fe_values.reinit(cell);
1987 *   local_matrix = 0;
1988 *   local_rhs = 0;
1989 *  
1990 *   for (unsigned int q = 0; q < n_q_points; ++q)
1991 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
1992 *   {
1993 *   const double phi_i_s = saturation_fe_values.shape_value(i, q);
1994 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
1995 *   {
1996 *   const double phi_j_s = saturation_fe_values.shape_value(j, q);
1997 *   local_matrix(i, j) +=
1998 *   porosity * phi_i_s * phi_j_s * saturation_fe_values.JxW(q);
1999 *   }
2000 *   }
2001 *   cell->get_dof_indices(local_dof_indices);
2002 *  
2003 *   saturation_constraints.distribute_local_to_global(local_matrix,
2004 *   local_dof_indices,
2005 *   saturation_matrix);
2006 *   }
2007 *   }
2008 *  
2009 *  
2010 *  
2011 * @endcode
2012 *
2013 *
2014 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs"></a>
2015 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs</h4>
2016 *
2017
2018 *
2019 * This function is to assemble the right hand side of the saturation
2020 * transport equation. Before going about it, we have to create two FEValues
2021 * objects for the Darcy and saturation systems respectively and, in
2022 * addition, two FEFaceValues objects for the two systems because we have a
2023 * boundary integral term in the weak form of saturation equation. For the
2024 * FEFaceValues object of the saturation system, we also require normal
2025 * vectors, which we request using the update_normal_vectors flag.
2026 *
2027
2028 *
2029 * Next, before looping over all the cells, we have to compute some
2030 * parameters (e.g. global_u_infty, global_S_variation, and
2031 * global_Omega_diameter) that the artificial viscosity @f$\nu@f$ needs. This is
2032 * largely the same as was done in @ref step_31 "step-31", so you may see there for more
2033 * information.
2034 *
2035
2036 *
2037 * The real works starts with the loop over all the saturation and Darcy
2038 * cells to put the local contributions into the global vector. In this
2039 * loop, in order to simplify the implementation, we split some of the work
2040 * into two helper functions: assemble_saturation_rhs_cell_term and
2041 * assemble_saturation_rhs_boundary_term. We note that we insert cell or
2042 * boundary contributions into the global vector in the two functions rather
2043 * than in this present function.
2044 *
2045 * @code
2046 *   template <int dim>
2047 *   void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs()
2048 *   {
2049 *   const QGauss<dim> quadrature_formula(saturation_degree + 2);
2050 *   const QGauss<dim - 1> face_quadrature_formula(saturation_degree + 2);
2051 *  
2052 *   FEValues<dim> saturation_fe_values(saturation_fe,
2053 *   quadrature_formula,
2054 *   update_values | update_gradients |
2055 *   update_quadrature_points |
2056 *   update_JxW_values);
2057 *   FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2058 *   FEFaceValues<dim> saturation_fe_face_values(saturation_fe,
2059 *   face_quadrature_formula,
2060 *   update_values |
2061 *   update_normal_vectors |
2062 *   update_quadrature_points |
2063 *   update_JxW_values);
2064 *   FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
2065 *   face_quadrature_formula,
2066 *   update_values);
2067 *   FEFaceValues<dim> saturation_fe_face_values_neighbor(
2068 *   saturation_fe, face_quadrature_formula, update_values);
2069 *  
2070 *   const unsigned int dofs_per_cell =
2071 *   saturation_dof_handler.get_fe().n_dofs_per_cell();
2072 *   std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2073 *  
2074 *   const double global_max_u_F_prime = get_max_u_F_prime();
2075 *   const std::pair<double, double> global_S_range =
2076 *   get_extrapolated_saturation_range();
2077 *   const double global_S_variation =
2078 *   global_S_range.second - global_S_range.first;
2079 *  
2080 *   auto cell = saturation_dof_handler.begin_active();
2081 *   const auto endc = saturation_dof_handler.end();
2082 *   auto darcy_cell = darcy_dof_handler.begin_active();
2083 *   for (; cell != endc; ++cell, ++darcy_cell)
2084 *   {
2085 *   saturation_fe_values.reinit(cell);
2086 *   darcy_fe_values.reinit(darcy_cell);
2087 *  
2088 *   cell->get_dof_indices(local_dof_indices);
2089 *  
2090 *   assemble_saturation_rhs_cell_term(saturation_fe_values,
2091 *   darcy_fe_values,
2092 *   global_max_u_F_prime,
2093 *   global_S_variation,
2094 *   local_dof_indices);
2095 *  
2096 *   for (const auto &face : cell->face_iterators())
2097 *   if (face->at_boundary())
2098 *   {
2099 *   darcy_fe_face_values.reinit(darcy_cell, face);
2100 *   saturation_fe_face_values.reinit(cell, face);
2101 *   assemble_saturation_rhs_boundary_term(saturation_fe_face_values,
2102 *   darcy_fe_face_values,
2103 *   local_dof_indices);
2104 *   }
2105 *   }
2106 *   }
2107 *  
2108 *  
2109 *  
2110 * @endcode
2111 *
2112 *
2113 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs_cell_term"></a>
2114 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term</h4>
2115 *
2116
2117 *
2118 * This function takes care of integrating the cell terms of the right hand
2119 * side of the saturation equation, and then assembling it into the global
2120 * right hand side vector. Given the discussion in the introduction, the
2121 * form of these contributions is clear. The only tricky part is getting the
2122 * artificial viscosity and all that is necessary to compute it. The first
2123 * half of the function is devoted to this task.
2124 *
2125
2126 *
2127 * The last part of the function is copying the local contributions into the
2128 * global vector with position specified in local_dof_indices.
2129 *
2130 * @code
2131 *   template <int dim>
2132 *   void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term(
2133 *   const FEValues<dim> &saturation_fe_values,
2134 *   const FEValues<dim> &darcy_fe_values,
2135 *   const double global_max_u_F_prime,
2136 *   const double global_S_variation,
2137 *   const std::vector<types::global_dof_index> &local_dof_indices)
2138 *   {
2139 *   const unsigned int dofs_per_cell = saturation_fe_values.dofs_per_cell;
2140 *   const unsigned int n_q_points = saturation_fe_values.n_quadrature_points;
2141 *  
2142 *   std::vector<double> old_saturation_solution_values(n_q_points);
2143 *   std::vector<double> old_old_saturation_solution_values(n_q_points);
2144 *   std::vector<Tensor<1, dim>> old_grad_saturation_solution_values(n_q_points);
2145 *   std::vector<Tensor<1, dim>> old_old_grad_saturation_solution_values(
2146 *   n_q_points);
2147 *   std::vector<Vector<double>> present_darcy_solution_values(
2148 *   n_q_points, Vector<double>(dim + 1));
2149 *  
2150 *   saturation_fe_values.get_function_values(old_saturation_solution,
2151 *   old_saturation_solution_values);
2152 *   saturation_fe_values.get_function_values(
2153 *   old_old_saturation_solution, old_old_saturation_solution_values);
2154 *   saturation_fe_values.get_function_gradients(
2155 *   old_saturation_solution, old_grad_saturation_solution_values);
2156 *   saturation_fe_values.get_function_gradients(
2157 *   old_old_saturation_solution, old_old_grad_saturation_solution_values);
2158 *   darcy_fe_values.get_function_values(darcy_solution,
2159 *   present_darcy_solution_values);
2160 *  
2161 *   const double nu =
2162 *   compute_viscosity(old_saturation_solution_values,
2163 *   old_old_saturation_solution_values,
2164 *   old_grad_saturation_solution_values,
2165 *   old_old_grad_saturation_solution_values,
2166 *   present_darcy_solution_values,
2167 *   global_max_u_F_prime,
2168 *   global_S_variation,
2169 *   saturation_fe_values.get_cell()->diameter());
2170 *  
2171 *   Vector<double> local_rhs(dofs_per_cell);
2172 *  
2173 *   for (unsigned int q = 0; q < n_q_points; ++q)
2174 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
2175 *   {
2176 *   const double old_s = old_saturation_solution_values[q];
2177 *   Tensor<1, dim> present_u;
2178 *   for (unsigned int d = 0; d < dim; ++d)
2179 *   present_u[d] = present_darcy_solution_values[q](d);
2180 *  
2181 *   const double phi_i_s = saturation_fe_values.shape_value(i, q);
2182 *   const Tensor<1, dim> grad_phi_i_s =
2183 *   saturation_fe_values.shape_grad(i, q);
2184 *  
2185 *   local_rhs(i) +=
2186 *   (time_step * fractional_flow(old_s, viscosity) * present_u *
2187 *   grad_phi_i_s -
2188 *   time_step * nu * old_grad_saturation_solution_values[q] *
2189 *   grad_phi_i_s +
2190 *   porosity * old_s * phi_i_s) *
2191 *   saturation_fe_values.JxW(q);
2192 *   }
2193 *  
2194 *   saturation_constraints.distribute_local_to_global(local_rhs,
2195 *   local_dof_indices,
2196 *   saturation_rhs);
2197 *   }
2198 *  
2199 *  
2200 * @endcode
2201 *
2202 *
2203 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs_boundary_term"></a>
2204 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term</h4>
2205 *
2206
2207 *
2208 * The next function is responsible for the boundary integral terms in the
2209 * right hand side form of the saturation equation. For these, we have to
2210 * compute the upwinding flux on the global boundary faces, i.e. we impose
2211 * Dirichlet boundary conditions weakly only on inflow parts of the global
2212 * boundary. As before, this has been described in @ref step_21 "step-21" so we refrain
2213 * from giving more descriptions about that.
2214 *
2215 * @code
2216 *   template <int dim>
2217 *   void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term(
2218 *   const FEFaceValues<dim> &saturation_fe_face_values,
2219 *   const FEFaceValues<dim> &darcy_fe_face_values,
2220 *   const std::vector<types::global_dof_index> &local_dof_indices)
2221 *   {
2222 *   const unsigned int dofs_per_cell = saturation_fe_face_values.dofs_per_cell;
2223 *   const unsigned int n_face_q_points =
2224 *   saturation_fe_face_values.n_quadrature_points;
2225 *  
2226 *   Vector<double> local_rhs(dofs_per_cell);
2227 *  
2228 *   std::vector<double> old_saturation_solution_values_face(n_face_q_points);
2229 *   std::vector<Vector<double>> present_darcy_solution_values_face(
2230 *   n_face_q_points, Vector<double>(dim + 1));
2231 *   std::vector<double> neighbor_saturation(n_face_q_points);
2232 *  
2233 *   saturation_fe_face_values.get_function_values(
2234 *   old_saturation_solution, old_saturation_solution_values_face);
2235 *   darcy_fe_face_values.get_function_values(
2236 *   darcy_solution, present_darcy_solution_values_face);
2237 *  
2238 *   SaturationBoundaryValues<dim> saturation_boundary_values;
2239 *   saturation_boundary_values.value_list(
2240 *   saturation_fe_face_values.get_quadrature_points(), neighbor_saturation);
2241 *  
2242 *   for (unsigned int q = 0; q < n_face_q_points; ++q)
2243 *   {
2244 *   Tensor<1, dim> present_u_face;
2245 *   for (unsigned int d = 0; d < dim; ++d)
2246 *   present_u_face[d] = present_darcy_solution_values_face[q](d);
2247 *  
2248 *   const double normal_flux =
2249 *   present_u_face * saturation_fe_face_values.normal_vector(q);
2250 *  
2251 *   const bool is_outflow_q_point = (normal_flux >= 0);
2252 *  
2253 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
2254 *   local_rhs(i) -=
2255 *   time_step * normal_flux *
2256 *   fractional_flow((is_outflow_q_point == true ?
2257 *   old_saturation_solution_values_face[q] :
2258 *   neighbor_saturation[q]),
2259 *   viscosity) *
2260 *   saturation_fe_face_values.shape_value(i, q) *
2261 *   saturation_fe_face_values.JxW(q);
2262 *   }
2263 *   saturation_constraints.distribute_local_to_global(local_rhs,
2264 *   local_dof_indices,
2265 *   saturation_rhs);
2266 *   }
2267 *  
2268 *  
2269 * @endcode
2270 *
2271 *
2272 * <a name="step_43-TwoPhaseFlowProblemdimsolve"></a>
2273 * <h3>TwoPhaseFlowProblem<dim>::solve</h3>
2274 *
2275
2276 *
2277 * This function implements the operator splitting algorithm, i.e. in each
2278 * time step it either re-computes the solution of the Darcy system or
2279 * extrapolates velocity/pressure from previous time steps, then determines
2280 * the size of the time step, and then updates the saturation variable. The
2281 * implementation largely follows similar code in @ref step_31 "step-31". It is, next to
2282 * the run() function, the central one in this program.
2283 *
2284
2285 *
2286 * At the beginning of the function, we ask whether to solve the
2287 * pressure-velocity part by evaluating the a posteriori criterion (see the
2288 * following function). If necessary, we will solve the pressure-velocity
2289 * part using the GMRES solver with the Schur complement block
2290 * preconditioner as is described in the introduction.
2291 *
2292 * @code
2293 *   template <int dim>
2294 *   void TwoPhaseFlowProblem<dim>::solve()
2295 *   {
2296 *   const bool solve_for_pressure_and_velocity =
2297 *   determine_whether_to_solve_for_pressure_and_velocity();
2298 *  
2299 *   if (solve_for_pressure_and_velocity == true)
2300 *   {
2301 *   std::cout << " Solving Darcy (pressure-velocity) system..."
2302 *   << std::endl;
2303 *  
2304 *   assemble_darcy_system();
2305 *   build_darcy_preconditioner();
2306 *  
2307 *   {
2308 *   const LinearSolvers::InverseMatrix<TrilinosWrappers::SparseMatrix,
2309 *   TrilinosWrappers::PreconditionIC>
2310 *   mp_inverse(darcy_preconditioner_matrix.block(1, 1),
2311 *   *bottom_right_preconditioner);
2312 *  
2313 *   const LinearSolvers::BlockSchurPreconditioner<
2314 *   TrilinosWrappers::PreconditionIC,
2315 *   TrilinosWrappers::PreconditionIC>
2316 *   preconditioner(darcy_matrix, mp_inverse, *top_left_preconditioner);
2317 *  
2318 *   SolverControl solver_control(darcy_matrix.m(),
2319 *   1e-16 * darcy_rhs.l2_norm());
2320 *  
2321 *   SolverGMRES<TrilinosWrappers::MPI::BlockVector> gmres(
2322 *   solver_control,
2323 *   SolverGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(
2324 *   100));
2325 *  
2326 *   for (unsigned int i = 0; i < darcy_solution.size(); ++i)
2327 *   if (darcy_constraints.is_constrained(i))
2328 *   darcy_solution(i) = 0;
2329 *  
2330 *   gmres.solve(darcy_matrix, darcy_solution, darcy_rhs, preconditioner);
2331 *  
2332 *   darcy_constraints.distribute(darcy_solution);
2333 *  
2334 *   std::cout << " ..." << solver_control.last_step()
2335 *   << " GMRES iterations." << std::endl;
2336 *   }
2337 *  
2338 *   {
2339 *   second_last_computed_darcy_solution = last_computed_darcy_solution;
2340 *   last_computed_darcy_solution = darcy_solution;
2341 *  
2342 *   saturation_matching_last_computed_darcy_solution =
2343 *   saturation_solution;
2344 *   }
2345 *   }
2346 * @endcode
2347 *
2348 * On the other hand, if we have decided that we don't want to compute the
2349 * solution of the Darcy system for the current time step, then we need to
2350 * simply extrapolate the previous two Darcy solutions to the same time as
2351 * we would have computed the velocity/pressure at. We do a simple linear
2352 * extrapolation, i.e. given the current length @f$dt@f$ of the macro time
2353 * step from the time when we last computed the Darcy solution to now
2354 * (given by <code>current_macro_time_step</code>), and @f$DT@f$ the length of
2355 * the last macro time step (given by <code>old_macro_time_step</code>),
2356 * then we get @f$u^\ast = u_p + dt \frac{u_p-u_{pp}}{DT} = (1+dt/DT)u_p -
2357 * dt/DT u_{pp}@f$, where @f$u_p@f$ and @f$u_{pp}@f$ are the last two computed Darcy
2358 * solutions. We can implement this formula using just two lines of code.
2359 *
2360
2361 *
2362 * Note that the algorithm here only works if we have at least two
2363 * previously computed Darcy solutions from which we can extrapolate to
2364 * the current time, and this is ensured by requiring re-computation of
2365 * the Darcy solution for the first 2 time steps.
2366 *
2367 * @code
2368 *   else
2369 *   {
2370 *   darcy_solution = last_computed_darcy_solution;
2371 *   darcy_solution.sadd(1 + current_macro_time_step / old_macro_time_step,
2372 *   -current_macro_time_step / old_macro_time_step,
2373 *   second_last_computed_darcy_solution);
2374 *   }
2375 *  
2376 *  
2377 * @endcode
2378 *
2379 * With the so computed velocity vector, compute the optimal time step
2380 * based on the CFL criterion discussed in the introduction...
2381 *
2382 * @code
2383 *   {
2384 *   old_time_step = time_step;
2385 *  
2386 *   const double max_u_F_prime = get_max_u_F_prime();
2387 *   if (max_u_F_prime > 0)
2388 *   time_step = porosity * GridTools::minimal_cell_diameter(triangulation) /
2389 *   saturation_degree / max_u_F_prime / 50;
2390 *   else
2391 *   time_step = end_time - time;
2392 *   }
2393 *  
2394 *  
2395 *  
2396 * @endcode
2397 *
2398 * ...and then also update the length of the macro time steps we use while
2399 * we're dealing with time step sizes. In particular, this involves: (i)
2400 * If we have just recomputed the Darcy solution, then the length of the
2401 * previous macro time step is now fixed and the length of the current
2402 * macro time step is, up to now, simply the length of the current (micro)
2403 * time step. (ii) If we have not recomputed the Darcy solution, then the
2404 * length of the current macro time step has just grown by
2405 * <code>time_step</code>.
2406 *
2407 * @code
2408 *   if (solve_for_pressure_and_velocity == true)
2409 *   {
2410 *   old_macro_time_step = current_macro_time_step;
2411 *   current_macro_time_step = time_step;
2412 *   }
2413 *   else
2414 *   current_macro_time_step += time_step;
2415 *  
2416 * @endcode
2417 *
2418 * The last step in this function is to recompute the saturation solution
2419 * based on the velocity field we've just obtained. This naturally happens
2420 * in every time step, and we don't skip any of these computations. At the
2421 * end of computing the saturation, we project back into the allowed
2422 * interval @f$[0,1]@f$ to make sure our solution remains physical.
2423 *
2424 * @code
2425 *   {
2426 *   std::cout << " Solving saturation transport equation..." << std::endl;
2427 *  
2428 *   assemble_saturation_system();
2429 *  
2430 *   SolverControl solver_control(saturation_matrix.m(),
2431 *   1e-16 * saturation_rhs.l2_norm());
2432 *   SolverCG<TrilinosWrappers::MPI::Vector> cg(solver_control);
2433 *  
2434 *   TrilinosWrappers::PreconditionIC preconditioner;
2435 *   preconditioner.initialize(saturation_matrix);
2436 *  
2437 *   cg.solve(saturation_matrix,
2438 *   saturation_solution,
2439 *   saturation_rhs,
2440 *   preconditioner);
2441 *  
2442 *   saturation_constraints.distribute(saturation_solution);
2443 *   project_back_saturation();
2444 *  
2445 *   std::cout << " ..." << solver_control.last_step()
2446 *   << " CG iterations." << std::endl;
2447 *   }
2448 *   }
2449 *  
2450 *  
2451 * @endcode
2452 *
2453 *
2454 * <a name="step_43-TwoPhaseFlowProblemdimrefine_mesh"></a>
2455 * <h3>TwoPhaseFlowProblem<dim>::refine_mesh</h3>
2456 *
2457
2458 *
2459 * The next function does the refinement and coarsening of the mesh. It does
2460 * its work in three blocks: (i) Compute refinement indicators by looking at
2461 * the gradient of a solution vector extrapolated linearly from the previous
2462 * two using the respective sizes of the time step (or taking the only
2463 * solution we have if this is the first time step). (ii) Flagging those
2464 * cells for refinement and coarsening where the gradient is larger or
2465 * smaller than a certain threshold, preserving minimal and maximal levels
2466 * of mesh refinement. (iii) Transferring the solution from the old to the
2467 * new mesh. None of this is particularly difficult.
2468 *
2469 * @code
2470 *   template <int dim>
2471 *   void TwoPhaseFlowProblem<dim>::refine_mesh(const unsigned int min_grid_level,
2472 *   const unsigned int max_grid_level)
2473 *   {
2474 *   Vector<double> refinement_indicators(triangulation.n_active_cells());
2475 *   {
2476 *   const QMidpoint<dim> quadrature_formula;
2477 *   FEValues<dim> fe_values(saturation_fe,
2478 *   quadrature_formula,
2479 *   update_gradients);
2480 *   std::vector<Tensor<1, dim>> grad_saturation(1);
2481 *  
2482 *   TrilinosWrappers::MPI::Vector extrapolated_saturation_solution(
2483 *   saturation_solution);
2484 *   if (timestep_number != 0)
2485 *   extrapolated_saturation_solution.sadd((1. + time_step / old_time_step),
2486 *   time_step / old_time_step,
2487 *   old_saturation_solution);
2488 *  
2489 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2490 *   {
2491 *   const unsigned int cell_no = cell->active_cell_index();
2492 *   fe_values.reinit(cell);
2493 *   fe_values.get_function_gradients(extrapolated_saturation_solution,
2494 *   grad_saturation);
2495 *  
2496 *   refinement_indicators(cell_no) = grad_saturation[0].norm();
2497 *   }
2498 *   }
2499 *  
2500 *   {
2501 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2502 *   {
2503 *   const unsigned int cell_no = cell->active_cell_index();
2504 *   cell->clear_coarsen_flag();
2505 *   cell->clear_refine_flag();
2506 *  
2507 *   if ((static_cast<unsigned int>(cell->level()) < max_grid_level) &&
2508 *   (std::fabs(refinement_indicators(cell_no)) >
2509 *   saturation_refinement_threshold))
2510 *   cell->set_refine_flag();
2511 *   else if ((static_cast<unsigned int>(cell->level()) >
2512 *   min_grid_level) &&
2513 *   (std::fabs(refinement_indicators(cell_no)) <
2514 *   0.5 * saturation_refinement_threshold))
2515 *   cell->set_coarsen_flag();
2516 *   }
2517 *   }
2518 *  
2519 *   triangulation.prepare_coarsening_and_refinement();
2520 *  
2521 *   {
2522 *   std::vector<TrilinosWrappers::MPI::Vector> x_saturation(3);
2523 *   x_saturation[0] = saturation_solution;
2524 *   x_saturation[1] = old_saturation_solution;
2525 *   x_saturation[2] = saturation_matching_last_computed_darcy_solution;
2526 *  
2527 *   std::vector<TrilinosWrappers::MPI::BlockVector> x_darcy(2);
2528 *   x_darcy[0] = last_computed_darcy_solution;
2529 *   x_darcy[1] = second_last_computed_darcy_solution;
2530 *  
2531 *   SolutionTransfer<dim, TrilinosWrappers::MPI::Vector> saturation_soltrans(
2532 *   saturation_dof_handler);
2533 *  
2534 *   SolutionTransfer<dim, TrilinosWrappers::MPI::BlockVector> darcy_soltrans(
2535 *   darcy_dof_handler);
2536 *  
2537 *  
2538 *   triangulation.prepare_coarsening_and_refinement();
2539 *   saturation_soltrans.prepare_for_coarsening_and_refinement(x_saturation);
2540 *  
2541 *   darcy_soltrans.prepare_for_coarsening_and_refinement(x_darcy);
2542 *  
2543 *   triangulation.execute_coarsening_and_refinement();
2544 *   setup_dofs();
2545 *  
2546 *   std::vector<TrilinosWrappers::MPI::Vector> tmp_saturation(3);
2547 *   tmp_saturation[0].reinit(saturation_solution);
2548 *   tmp_saturation[1].reinit(saturation_solution);
2549 *   tmp_saturation[2].reinit(saturation_solution);
2550 *   saturation_soltrans.interpolate(tmp_saturation);
2551 *  
2552 *   saturation_solution = tmp_saturation[0];
2553 *   old_saturation_solution = tmp_saturation[1];
2554 *   saturation_matching_last_computed_darcy_solution = tmp_saturation[2];
2555 *  
2556 *   saturation_constraints.distribute(saturation_solution);
2557 *   saturation_constraints.distribute(old_saturation_solution);
2558 *   saturation_constraints.distribute(
2559 *   saturation_matching_last_computed_darcy_solution);
2560 *  
2561 *   std::vector<TrilinosWrappers::MPI::BlockVector> tmp_darcy(2);
2562 *   tmp_darcy[0].reinit(darcy_solution);
2563 *   tmp_darcy[1].reinit(darcy_solution);
2564 *   darcy_soltrans.interpolate(tmp_darcy);
2565 *  
2566 *   last_computed_darcy_solution = tmp_darcy[0];
2567 *   second_last_computed_darcy_solution = tmp_darcy[1];
2568 *  
2569 *   darcy_constraints.distribute(last_computed_darcy_solution);
2570 *   darcy_constraints.distribute(second_last_computed_darcy_solution);
2571 *  
2572 *   rebuild_saturation_matrix = true;
2573 *   }
2574 *   }
2575 *  
2576 *  
2577 *  
2578 * @endcode
2579 *
2580 *
2581 * <a name="step_43-TwoPhaseFlowProblemdimoutput_results"></a>
2582 * <h3>TwoPhaseFlowProblem<dim>::output_results</h3>
2583 *
2584
2585 *
2586 * This function generates graphical output. It is in essence a copy of the
2587 * implementation in @ref step_31 "step-31".
2588 *
2589 * @code
2590 *   template <int dim>
2591 *   void TwoPhaseFlowProblem<dim>::output_results() const
2592 *   {
2593 *   const FESystem<dim> joint_fe(darcy_fe, 1, saturation_fe, 1);
2594 *   DoFHandler<dim> joint_dof_handler(triangulation);
2595 *   joint_dof_handler.distribute_dofs(joint_fe);
2596 *   Assert(joint_dof_handler.n_dofs() ==
2597 *   darcy_dof_handler.n_dofs() + saturation_dof_handler.n_dofs(),
2598 *   ExcInternalError());
2599 *  
2600 *   Vector<double> joint_solution(joint_dof_handler.n_dofs());
2601 *  
2602 *   {
2603 *   std::vector<types::global_dof_index> local_joint_dof_indices(
2604 *   joint_fe.n_dofs_per_cell());
2605 *   std::vector<types::global_dof_index> local_darcy_dof_indices(
2606 *   darcy_fe.n_dofs_per_cell());
2607 *   std::vector<types::global_dof_index> local_saturation_dof_indices(
2608 *   saturation_fe.n_dofs_per_cell());
2609 *  
2610 *   auto joint_cell = joint_dof_handler.begin_active();
2611 *   const auto joint_endc = joint_dof_handler.end();
2612 *   auto darcy_cell = darcy_dof_handler.begin_active();
2613 *   auto saturation_cell = saturation_dof_handler.begin_active();
2614 *  
2615 *   for (; joint_cell != joint_endc;
2616 *   ++joint_cell, ++darcy_cell, ++saturation_cell)
2617 *   {
2618 *   joint_cell->get_dof_indices(local_joint_dof_indices);
2619 *   darcy_cell->get_dof_indices(local_darcy_dof_indices);
2620 *   saturation_cell->get_dof_indices(local_saturation_dof_indices);
2621 *  
2622 *   for (unsigned int i = 0; i < joint_fe.n_dofs_per_cell(); ++i)
2623 *   if (joint_fe.system_to_base_index(i).first.first == 0)
2624 *   {
2625 *   Assert(joint_fe.system_to_base_index(i).second <
2626 *   local_darcy_dof_indices.size(),
2627 *   ExcInternalError());
2628 *   joint_solution(local_joint_dof_indices[i]) = darcy_solution(
2629 *   local_darcy_dof_indices[joint_fe.system_to_base_index(i)
2630 *   .second]);
2631 *   }
2632 *   else
2633 *   {
2634 *   Assert(joint_fe.system_to_base_index(i).first.first == 1,
2635 *   ExcInternalError());
2636 *   Assert(joint_fe.system_to_base_index(i).second <
2637 *   local_darcy_dof_indices.size(),
2638 *   ExcInternalError());
2639 *   joint_solution(local_joint_dof_indices[i]) =
2640 *   saturation_solution(
2641 *   local_saturation_dof_indices
2642 *   [joint_fe.system_to_base_index(i).second]);
2643 *   }
2644 *   }
2645 *   }
2646 *   std::vector<std::string> joint_solution_names(dim, "velocity");
2647 *   joint_solution_names.emplace_back("pressure");
2648 *   joint_solution_names.emplace_back("saturation");
2649 *  
2650 *   std::vector<DataComponentInterpretation::DataComponentInterpretation>
2651 *   data_component_interpretation(
2652 *   dim, DataComponentInterpretation::component_is_part_of_vector);
2653 *   data_component_interpretation.push_back(
2654 *   DataComponentInterpretation::component_is_scalar);
2655 *   data_component_interpretation.push_back(
2656 *   DataComponentInterpretation::component_is_scalar);
2657 *  
2658 *   DataOut<dim> data_out;
2659 *  
2660 *   data_out.attach_dof_handler(joint_dof_handler);
2661 *   data_out.add_data_vector(joint_solution,
2662 *   joint_solution_names,
2663 *   DataOut<dim>::type_dof_data,
2664 *   data_component_interpretation);
2665 *  
2666 *   data_out.build_patches();
2667 *  
2668 *   std::string filename =
2669 *   "solution-" + Utilities::int_to_string(timestep_number, 5) + ".vtu";
2670 *   std::ofstream output(filename);
2671 *   data_out.write_vtu(output);
2672 *   }
2673 *  
2674 *  
2675 *  
2676 * @endcode
2677 *
2678 *
2679 * <a name="step_43-Toolfunctions"></a>
2680 * <h3>Tool functions</h3>
2681 *
2682
2683 *
2684 *
2685 * <a name="step_43-TwoPhaseFlowProblemdimdetermine_whether_to_solve_for_pressure_and_velocity"></a>
2686 * <h4>TwoPhaseFlowProblem<dim>::determine_whether_to_solve_for_pressure_and_velocity</h4>
2687 *
2688
2689 *
2690 * This function implements the a posteriori criterion for adaptive operator
2691 * splitting. The function is relatively straightforward given the way we
2692 * have implemented other functions above and given the formula for the
2693 * criterion derived in the paper.
2694 *
2695
2696 *
2697 * If one decides that one wants the original IMPES method in which the
2698 * Darcy equation is solved in every time step, then this can be achieved by
2699 * setting the threshold value <code>AOS_threshold</code> (with a default of
2700 * @f$5.0@f$) to zero, thereby forcing the function to always return true.
2701 *
2702
2703 *
2704 * Finally, note that the function returns true unconditionally for the
2705 * first two time steps to ensure that we have always solved the Darcy
2706 * system at least twice when skipping its solution, thereby allowing us to
2707 * extrapolate the velocity from the last two solutions in
2708 * <code>solve()</code>.
2709 *
2710 * @code
2711 *   template <int dim>
2712 *   bool TwoPhaseFlowProblem<
2713 *   dim>::determine_whether_to_solve_for_pressure_and_velocity() const
2714 *   {
2715 *   if (timestep_number <= 2)
2716 *   return true;
2717 *  
2718 *   const QGauss<dim> quadrature_formula(saturation_degree + 2);
2719 *   const unsigned int n_q_points = quadrature_formula.size();
2720 *  
2721 *   FEValues<dim> fe_values(saturation_fe,
2722 *   quadrature_formula,
2723 *   update_values | update_quadrature_points);
2724 *  
2725 *   std::vector<double> old_saturation_after_solving_pressure(n_q_points);
2726 *   std::vector<double> present_saturation(n_q_points);
2727 *  
2728 *   std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
2729 *  
2730 *   double max_global_aop_indicator = 0.0;
2731 *  
2732 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2733 *   {
2734 *   double max_local_mobility_reciprocal_difference = 0.0;
2735 *   double max_local_permeability_inverse_l1_norm = 0.0;
2736 *  
2737 *   fe_values.reinit(cell);
2738 *   fe_values.get_function_values(
2739 *   saturation_matching_last_computed_darcy_solution,
2740 *   old_saturation_after_solving_pressure);
2741 *   fe_values.get_function_values(saturation_solution, present_saturation);
2742 *  
2743 *   k_inverse.value_list(fe_values.get_quadrature_points(),
2744 *   k_inverse_values);
2745 *  
2746 *   for (unsigned int q = 0; q < n_q_points; ++q)
2747 *   {
2748 *   const double mobility_reciprocal_difference = std::fabs(
2749 *   mobility_inverse(present_saturation[q], viscosity) -
2750 *   mobility_inverse(old_saturation_after_solving_pressure[q],
2751 *   viscosity));
2752 *  
2753 *   max_local_mobility_reciprocal_difference =
2754 *   std::max(max_local_mobility_reciprocal_difference,
2755 *   mobility_reciprocal_difference);
2756 *  
2757 *   max_local_permeability_inverse_l1_norm =
2758 *   std::max(max_local_permeability_inverse_l1_norm,
2759 *   l1_norm(k_inverse_values[q]));
2760 *   }
2761 *  
2762 *   max_global_aop_indicator =
2763 *   std::max(max_global_aop_indicator,
2764 *   (max_local_mobility_reciprocal_difference *
2765 *   max_local_permeability_inverse_l1_norm));
2766 *   }
2767 *  
2768 *   return (max_global_aop_indicator > AOS_threshold);
2769 *   }
2770 *  
2771 *  
2772 *  
2773 * @endcode
2774 *
2775 *
2776 * <a name="step_43-TwoPhaseFlowProblemdimproject_back_saturation"></a>
2777 * <h4>TwoPhaseFlowProblem<dim>::project_back_saturation</h4>
2778 *
2779
2780 *
2781 * The next function simply makes sure that the saturation values always
2782 * remain within the physically reasonable range of @f$[0,1]@f$. While the
2783 * continuous equations guarantee that this is so, the discrete equations
2784 * don't. However, if we allow the discrete solution to escape this range we
2785 * get into trouble because terms like @f$F(S)@f$ and @f$F'(S)@f$ will produce
2786 * unreasonable results (e.g. @f$F'(S)<0@f$ for @f$S<0@f$, which would imply that
2787 * the wetting fluid phase flows <i>against</i> the direction of the bulk
2788 * fluid velocity)). Consequently, at the end of each time step, we simply
2789 * project the saturation field back into the physically reasonable region.
2790 *
2791 * @code
2792 *   template <int dim>
2793 *   void TwoPhaseFlowProblem<dim>::project_back_saturation()
2794 *   {
2795 *   for (unsigned int i = 0; i < saturation_solution.size(); ++i)
2796 *   if (saturation_solution(i) < 0.2)
2797 *   saturation_solution(i) = 0.2;
2798 *   else if (saturation_solution(i) > 1)
2799 *   saturation_solution(i) = 1;
2800 *   }
2801 *  
2802 *  
2803 *  
2804 * @endcode
2805 *
2806 *
2807 * <a name="step_43-TwoPhaseFlowProblemdimget_max_u_F_prime"></a>
2808 * <h4>TwoPhaseFlowProblem<dim>::get_max_u_F_prime</h4>
2809 *
2810
2811 *
2812 * Another simpler helper function: Compute the maximum of the total
2813 * velocity times the derivative of the fraction flow function, i.e.,
2814 * compute @f$\|\mathbf{u} F'(S)\|_{L_\infty(\Omega)}@f$. This term is used in
2815 * both the computation of the time step as well as in normalizing the
2816 * entropy-residual term in the artificial viscosity.
2817 *
2818 * @code
2819 *   template <int dim>
2820 *   double TwoPhaseFlowProblem<dim>::get_max_u_F_prime() const
2821 *   {
2822 *   const QGauss<dim> quadrature_formula(darcy_degree + 2);
2823 *   const unsigned int n_q_points = quadrature_formula.size();
2824 *  
2825 *   FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2826 *   FEValues<dim> saturation_fe_values(saturation_fe,
2827 *   quadrature_formula,
2828 *   update_values);
2829 *  
2830 *   std::vector<Vector<double>> darcy_solution_values(n_q_points,
2831 *   Vector<double>(dim + 1));
2832 *   std::vector<double> saturation_values(n_q_points);
2833 *  
2834 *   double max_velocity_times_dF_dS = 0;
2835 *  
2836 *   auto cell = darcy_dof_handler.begin_active();
2837 *   const auto endc = darcy_dof_handler.end();
2838 *   auto saturation_cell = saturation_dof_handler.begin_active();
2839 *   for (; cell != endc; ++cell, ++saturation_cell)
2840 *   {
2841 *   darcy_fe_values.reinit(cell);
2842 *   saturation_fe_values.reinit(saturation_cell);
2843 *  
2844 *   darcy_fe_values.get_function_values(darcy_solution,
2845 *   darcy_solution_values);
2846 *   saturation_fe_values.get_function_values(old_saturation_solution,
2847 *   saturation_values);
2848 *  
2849 *   for (unsigned int q = 0; q < n_q_points; ++q)
2850 *   {
2851 *   Tensor<1, dim> velocity;
2852 *   for (unsigned int i = 0; i < dim; ++i)
2853 *   velocity[i] = darcy_solution_values[q](i);
2854 *  
2855 *   const double dF_dS =
2856 *   fractional_flow_derivative(saturation_values[q], viscosity);
2857 *  
2858 *   max_velocity_times_dF_dS =
2859 *   std::max(max_velocity_times_dF_dS, velocity.norm() * dF_dS);
2860 *   }
2861 *   }
2862 *  
2863 *   return max_velocity_times_dF_dS;
2864 *   }
2865 *  
2866 *  
2867 * @endcode
2868 *
2869 *
2870 * <a name="step_43-TwoPhaseFlowProblemdimget_extrapolated_saturation_range"></a>
2871 * <h4>TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range</h4>
2872 *
2873
2874 *
2875 * For computing the stabilization term, we need to know the range of the
2876 * saturation variable. Unlike in @ref step_31 "step-31", this range is trivially bounded
2877 * by the interval @f$[0,1]@f$ but we can do a bit better by looping over a
2878 * collection of quadrature points and seeing what the values are there. If
2879 * we can, i.e., if there are at least two timesteps around, we can even
2880 * take the values extrapolated to the next time step.
2881 *
2882
2883 *
2884 * As before, the function is taken with minimal modifications from @ref step_31 "step-31".
2885 *
2886 * @code
2887 *   template <int dim>
2888 *   std::pair<double, double>
2889 *   TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range() const
2890 *   {
2891 *   const QGauss<dim> quadrature_formula(saturation_degree + 2);
2892 *   const unsigned int n_q_points = quadrature_formula.size();
2893 *  
2894 *   FEValues<dim> fe_values(saturation_fe, quadrature_formula, update_values);
2895 *   std::vector<double> old_saturation_values(n_q_points);
2896 *   std::vector<double> old_old_saturation_values(n_q_points);
2897 *  
2898 *   if (timestep_number != 0)
2899 *   {
2900 *   double min_saturation = std::numeric_limits<double>::max(),
2901 *   max_saturation = -std::numeric_limits<double>::max();
2902 *  
2903 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2904 *   {
2905 *   fe_values.reinit(cell);
2906 *   fe_values.get_function_values(old_saturation_solution,
2907 *   old_saturation_values);
2908 *   fe_values.get_function_values(old_old_saturation_solution,
2909 *   old_old_saturation_values);
2910 *  
2911 *   for (unsigned int q = 0; q < n_q_points; ++q)
2912 *   {
2913 *   const double saturation =
2914 *   (1. + time_step / old_time_step) * old_saturation_values[q] -
2915 *   time_step / old_time_step * old_old_saturation_values[q];
2916 *  
2917 *   min_saturation = std::min(min_saturation, saturation);
2918 *   max_saturation = std::max(max_saturation, saturation);
2919 *   }
2920 *   }
2921 *  
2922 *   return std::make_pair(min_saturation, max_saturation);
2923 *   }
2924 *   else
2925 *   {
2926 *   double min_saturation = std::numeric_limits<double>::max(),
2927 *   max_saturation = -std::numeric_limits<double>::max();
2928 *  
2929 *   for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2930 *   {
2931 *   fe_values.reinit(cell);
2932 *   fe_values.get_function_values(old_saturation_solution,
2933 *   old_saturation_values);
2934 *  
2935 *   for (unsigned int q = 0; q < n_q_points; ++q)
2936 *   {
2937 *   const double saturation = old_saturation_values[q];
2938 *  
2939 *   min_saturation = std::min(min_saturation, saturation);
2940 *   max_saturation = std::max(max_saturation, saturation);
2941 *   }
2942 *   }
2943 *  
2944 *   return std::make_pair(min_saturation, max_saturation);
2945 *   }
2946 *   }
2947 *  
2948 *  
2949 *  
2950 * @endcode
2951 *
2952 *
2953 * <a name="step_43-TwoPhaseFlowProblemdimcompute_viscosity"></a>
2954 * <h4>TwoPhaseFlowProblem<dim>::compute_viscosity</h4>
2955 *
2956
2957 *
2958 * The final tool function is used to compute the artificial viscosity on a
2959 * given cell. This isn't particularly complicated if you have the formula
2960 * for it in front of you, and looking at the implementation in @ref step_31 "step-31". The
2961 * major difference to that tutorial program is that the velocity here is
2962 * not simply @f$\mathbf u@f$ but @f$\mathbf u F'(S)@f$ and some of the formulas
2963 * need to be adjusted accordingly.
2964 *
2965 * @code
2966 *   template <int dim>
2967 *   double TwoPhaseFlowProblem<dim>::compute_viscosity(
2968 *   const std::vector<double> &old_saturation,
2969 *   const std::vector<double> &old_old_saturation,
2970 *   const std::vector<Tensor<1, dim>> &old_saturation_grads,
2971 *   const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
2972 *   const std::vector<Vector<double>> &present_darcy_values,
2973 *   const double global_max_u_F_prime,
2974 *   const double global_S_variation,
2975 *   const double cell_diameter) const
2976 *   {
2977 *   const double beta = .4 * dim;
2978 *   const double alpha = 1;
2979 *  
2980 *   if (global_max_u_F_prime == 0)
2981 *   return 5e-3 * cell_diameter;
2982 *  
2983 *   const unsigned int n_q_points = old_saturation.size();
2984 *  
2985 *   double max_residual = 0;
2986 *   double max_velocity_times_dF_dS = 0;
2987 *  
2988 *   const bool use_dF_dS = true;
2989 *  
2990 *   for (unsigned int q = 0; q < n_q_points; ++q)
2991 *   {
2992 *   Tensor<1, dim> u;
2993 *   for (unsigned int d = 0; d < dim; ++d)
2994 *   u[d] = present_darcy_values[q](d);
2995 *  
2996 *   const double dS_dt = porosity *
2997 *   (old_saturation[q] - old_old_saturation[q]) /
2998 *   old_time_step;
2999 *  
3000 *   const double dF_dS = fractional_flow_derivative(
3001 *   (old_saturation[q] + old_old_saturation[q]) / 2.0, viscosity);
3002 *  
3003 *   const double u_grad_S =
3004 *   u * dF_dS * (old_saturation_grads[q] + old_old_saturation_grads[q]) /
3005 *   2.0;
3006 *  
3007 *   const double residual =
3008 *   std::abs((dS_dt + u_grad_S) *
3009 *   std::pow((old_saturation[q] + old_old_saturation[q]) / 2,
3010 *   alpha - 1.));
3011 *  
3012 *   max_residual = std::max(residual, max_residual);
3013 *   max_velocity_times_dF_dS =
3014 *   std::max(std::sqrt(u * u) * (use_dF_dS ? std::max(dF_dS, 1.) : 1),
3015 *   max_velocity_times_dF_dS);
3016 *   }
3017 *  
3018 *   const double c_R = 1.0;
3019 *   const double global_scaling = c_R * porosity *
3020 *   (global_max_u_F_prime)*global_S_variation /
3021 *   std::pow(global_Omega_diameter, alpha - 2.);
3022 *  
3023 *   return (beta *
3024 *   (max_velocity_times_dF_dS)*std::min(cell_diameter,
3025 *   std::pow(cell_diameter, alpha) *
3026 *   max_residual /
3027 *   global_scaling));
3028 *   }
3029 *  
3030 *  
3031 * @endcode
3032 *
3033 *
3034 * <a name="step_43-TwoPhaseFlowProblemdimrun"></a>
3035 * <h3>TwoPhaseFlowProblem<dim>::run</h3>
3036 *
3037
3038 *
3039 * This function is, besides <code>solve()</code>, the primary function of
3040 * this program as it controls the time iteration as well as when the
3041 * solution is written into output files and when to do mesh refinement.
3042 *
3043
3044 *
3045 * With the exception of the startup code that loops back to the beginning
3046 * of the function through the <code>goto start_time_iteration</code> label,
3047 * everything should be relatively straightforward. In any case, it mimics
3048 * the corresponding function in @ref step_31 "step-31".
3049 *
3050 * @code
3051 *   template <int dim>
3052 *   void TwoPhaseFlowProblem<dim>::run()
3053 *   {
3054 *   const unsigned int initial_refinement = (dim == 2 ? 5 : 2);
3055 *   const unsigned int n_pre_refinement_steps = (dim == 2 ? 3 : 2);
3056 *  
3057 *  
3058 *   GridGenerator::hyper_cube(triangulation, 0, 1);
3059 *   triangulation.refine_global(initial_refinement);
3060 *   global_Omega_diameter = GridTools::diameter(triangulation);
3061 *  
3062 *   setup_dofs();
3063 *  
3064 *   unsigned int pre_refinement_step = 0;
3065 *  
3066 *   start_time_iteration:
3067 *  
3068 *   VectorTools::project(saturation_dof_handler,
3069 *   saturation_constraints,
3070 *   QGauss<dim>(saturation_degree + 2),
3071 *   SaturationInitialValues<dim>(),
3072 *   old_saturation_solution);
3073 *  
3074 *   time_step = old_time_step = 0;
3075 *   current_macro_time_step = old_macro_time_step = 0;
3076 *  
3077 *   time = 0;
3078 *  
3079 *   do
3080 *   {
3081 *   std::cout << "Timestep " << timestep_number << ": t=" << time
3082 *   << ", dt=" << time_step << std::endl;
3083 *  
3084 *   solve();
3085 *  
3086 *   std::cout << std::endl;
3087 *  
3088 *   if (timestep_number % 200 == 0)
3089 *   output_results();
3090 *  
3091 *   if (timestep_number % 25 == 0)
3092 *   refine_mesh(initial_refinement,
3093 *   initial_refinement + n_pre_refinement_steps);
3094 *  
3095 *   if ((timestep_number == 0) &&
3096 *   (pre_refinement_step < n_pre_refinement_steps))
3097 *   {
3098 *   ++pre_refinement_step;
3099 *   goto start_time_iteration;
3100 *   }
3101 *  
3102 *   time += time_step;
3103 *   ++timestep_number;
3104 *  
3105 *   old_old_saturation_solution = old_saturation_solution;
3106 *   old_saturation_solution = saturation_solution;
3107 *   }
3108 *   while (time <= end_time);
3109 *   }
3110 *   } // namespace Step43
3111 *  
3112 *  
3113 *  
3114 * @endcode
3115 *
3116 *
3117 * <a name="step_43-Thecodemaincodefunction"></a>
3118 * <h3>The <code>main()</code> function</h3>
3119 *
3120
3121 *
3122 * The main function looks almost the same as in all other programs. The need
3123 * to initialize the MPI subsystem for a program that uses Trilinos -- even
3124 * for programs that do not actually run in parallel -- is explained in
3125 * @ref step_31 "step-31".
3126 *
3127 * @code
3128 *   int main(int argc, char *argv[])
3129 *   {
3130 *   try
3131 *   {
3132 *   using namespace dealii;
3133 *   using namespace Step43;
3134 *  
3135 *   Utilities::MPI::MPI_InitFinalize mpi_initialization(
3136 *   argc, argv, numbers::invalid_unsigned_int);
3137 *  
3138 * @endcode
3139 *
3140 * This program can only be run in serial. Otherwise, throw an exception.
3141 *
3142 * @code
3143 *   AssertThrow(Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD) == 1,
3144 *   ExcMessage(
3145 *   "This program can only be run in serial, use ./step-43"));
3146 *  
3147 *   TwoPhaseFlowProblem<2> two_phase_flow_problem(1);
3148 *   two_phase_flow_problem.run();
3149 *   }
3150 *   catch (std::exception &exc)
3151 *   {
3152 *   std::cerr << std::endl
3153 *   << std::endl
3154 *   << "----------------------------------------------------"
3155 *   << std::endl;
3156 *   std::cerr << "Exception on processing: " << std::endl
3157 *   << exc.what() << std::endl
3158 *   << "Aborting!" << std::endl
3159 *   << "----------------------------------------------------"
3160 *   << std::endl;
3161 *  
3162 *   return 1;
3163 *   }
3164 *   catch (...)
3165 *   {
3166 *   std::cerr << std::endl
3167 *   << std::endl
3168 *   << "----------------------------------------------------"
3169 *   << std::endl;
3170 *   std::cerr << "Unknown exception!" << std::endl
3171 *   << "Aborting!" << std::endl
3172 *   << "----------------------------------------------------"
3173 *   << std::endl;
3174 *   return 1;
3175 *   }
3176 *  
3177 *   return 0;
3178 *   }
3179 * @endcode
3180<a name="step_43-Results"></a><h1>Results</h1>
3181
3182
3183
3184The output of this program is not really much different from that of
3185@ref step_21 "step-21": it solves the same problem, after all. Of more importance are
3186quantitative metrics such as the accuracy of the solution as well as
3187the time needed to compute it. These are documented in detail in the
3188two publications listed at the top of this page and we won't repeat
3189them here.
3190
3191That said, no tutorial program is complete without a couple of good
3192pictures, so here is some output of a run in 3d:
3193
3194<table align="center" class="tutorial" cellspacing="3" cellpadding="3">
3195 <tr>
3196 <td align="center">
3197 <img src="https://www.dealii.org/images/steps/developer/step-43.3d.velocity.png" alt="">
3198 <p align="center">
3199 Velocity vectors of flow through the porous medium with random
3200 permeability model. Streaming paths of high permeability and resulting
3201 high velocity are clearly visible.
3202 </p>
3203 </td>
3204 <td align="center">
3205 <img src="https://www.dealii.org/images/steps/developer/step-43.3d.streamlines.png" alt="">
3206 <p align="center">
3207 Streamlines colored by the saturation along the streamline path. Blue
3208 streamlines indicate low saturations, i.e., the flow along these
3209 streamlines must be slow or else more fluid would have been
3210 transported along them. On the other hand, green paths indicate high
3211 velocities since the fluid front has already reached further into the
3212 domain.
3213 </p>
3214 </td>
3215 </tr>
3216 <tr>
3217 <td align="center">
3218 <img src="https://www.dealii.org/images/steps/developer/step-43.3d.saturation.png" alt="">
3219 <p align="center">
3220 Streamlines with a volume rendering of the saturation, showing how far
3221 the fluid front has advanced at this time.
3222 </p>
3223 </td>
3224 <td align="center">
3225 <img src="https://www.dealii.org/images/steps/developer/step-43.3d.mesh.png" alt="">
3226 <p align="center">
3227 Surface of the mesh showing the adaptive refinement along the front.
3228 </p>
3229 </td>
3230 </tr>
3231</table>
3232
3233
3234<a name="step-43-extensions"></a>
3235<a name="step_43-Possibilitiesforextensions"></a><h3>Possibilities for extensions</h3>
3236
3237
3238The primary objection one may have to this program is that it is still too
3239slow: 3d computations on reasonably fine meshes are simply too expensive to be
3240done routinely and with reasonably quick turn-around. This is similar to the
3241situation we were in when we wrote @ref step_31 "step-31", from which this program has taken
3242much inspiration. The solution is similar as it was there as well: We need to
3243parallelize the program in a way similar to how we derived @ref step_32 "step-32" out of
3244@ref step_31 "step-31". In fact, all of the techniques used in @ref step_32 "step-32" would be transferable
3245to this program as well, making the program run on dozens or hundreds of
3246processors immediately.
3247
3248A different direction is to make the program more relevant to many other
3249porous media applications. Specifically, one avenue is to go to the primary
3250user of porous media flow simulators, namely the oil industry. There,
3251applications in this area are dominated by multiphase flow (i.e., more than
3252the two phases we have here), and the reactions they may have with each other
3253(or any other way phases may exchange mass, such as through dissolution in and
3254bubbling out of gas from the oil phase). Furthermore, the presence of gas
3255often leads to compressibility effects of the fluid. Jointly, these effects
3256are typically formulated in the widely-used "black oil model". True reactions
3257between multiple phases also play a role in oil reservoir modeling when
3258considering controlled burns of oil in the reservoir to raise pressure and
3259temperature. These are much more complex problems, though, and left for future
3260projects.
3261
3262Finally, from a mathematical perspective, we have derived the
3263criterion for re-computing the velocity/pressure solution at a given
3264time step under the assumption that we want to compare the solution we
3265would get at the current time step with that computed the last time we
3266actually solved this system. However, in the program, whenever we did
3267not re-compute the solution, we didn't just use the previously
3268computed solution but instead extrapolated from the previous two times
3269we solved the system. Consequently, the criterion was pessimistically
3270stated: what we should really compare is the solution we would get at
3271the current time step with the extrapolated one. Re-stating the
3272theorem in this regard is left as an exercise.
3273
3274There are also other ways to extend the mathematical foundation of
3275this program; for example, one may say that it isn't the velocity we
3276care about, but in fact the saturation. Thus, one may ask whether the
3277criterion we use here to decide whether @f$\mathbf u@f$ needs to be
3278recomputed is appropriate; one may, for example, suggest that it is
3279also important to decide whether (and by how much) a wrong velocity
3280field in fact affects the solution of the saturation equation. This
3281would then naturally lead to a sensitivity analysis.
3282
3283From an algorithmic viewpoint, we have here used a criterion for refinement
3284that is often used in engineering, namely by looking at the gradient of
3285the solution. However, if you inspect the solution, you will find that
3286it quickly leads to refinement almost everywhere, even in regions where it
3287is clearly not necessary: frequently used therefore does not need to imply
3288that it is a useful criterion to begin with. On the other hand, replacing
3289this criterion by a different and better one should not be very difficult.
3290For example, the KellyErrorEstimator class used in many other programs
3291should certainly be applicable to the current problem as well.
3292 *
3293 *
3294<a name="step_43-PlainProg"></a>
3295<h1> The plain program</h1>
3296@include "step-43.cc"
3297*/
void condense(SparsityPattern &sparsity) const
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const
Definition point.h:111
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< value_type > &values) const
Point< 2 > second
Definition grid_out.cc:4624
Point< 2 > first
Definition grid_out.cc:4623
#define AssertDimension(dim1, dim2)
void loop(IteratorType begin, std_cxx20::type_identity_t< IteratorType > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, AssemblerType &assembler, const LoopControl &lctrl=LoopControl())
Definition loop.h:564
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)
void make_zero_boundary_constraints(const DoFHandler< dim, spacedim > &dof, const types::boundary_id boundary_id, AffineConstraints< number > &zero_boundary_constraints, const ComponentMask &component_mask={})
IndexSet complete_index_set(const IndexSet::size_type N)
Definition index_set.h:1204
CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt K
void component_wise(DoFHandler< dim, spacedim > &dof_handler, const std::vector< unsigned int > &target_component=std::vector< unsigned int >())
void Cuthill_McKee(DoFHandler< dim, spacedim > &dof_handler, const bool reversed_numbering=false, const bool use_constraints=false, const std::vector< types::global_dof_index > &starting_indices=std::vector< types::global_dof_index >())
void random(DoFHandler< dim, spacedim > &dof_handler)
std::vector< types::global_dof_index > count_dofs_per_fe_block(const DoFHandler< dim, spacedim > &dof, const std::vector< unsigned int > &target_block=std::vector< unsigned int >())
void extrapolate(const DoFHandler< dim, spacedim > &dof1, const InVector &z1, const DoFHandler< dim, spacedim > &dof2, OutVector &z2)
double minimal_cell_diameter(const Triangulation< dim, spacedim > &triangulation, const Mapping< dim, spacedim > &mapping=(ReferenceCells::get_hypercube< dim >() .template get_default_linear_mapping< dim, spacedim >()))
double volume(const Triangulation< dim, spacedim > &tria)
@ matrix
Contents is actually a matrix.
void apply_boundary_values(const std::map< types::global_dof_index, number > &boundary_values, SparseMatrix< number > &matrix, Vector< number > &solution, Vector< number > &right_hand_side, const bool eliminate_columns=true)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition utilities.cc:191
std::string escape(const std::string &input, const PatternBase::OutputStyle style)
Definition patterns.cc:52
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
Tensor< 2, dim, Number > F(const Tensor< 2, dim, Number > &Grad_u)
VectorType::value_type * begin(VectorType &V)
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)
::VectorizedArray< Number, width > exp(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation