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