Loading [MathJax]/extensions/TeX/AMSsymbols.js
 Reference documentation for deal.II version 9.3.3
\(\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\}}\)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
step-21.h
Go to the documentation of this file.
1,
811 * const unsigned int /*component*/ = 0) const override
812 * {
813 * return 0;
814 * }
815 * };
816 *
817 *
818 *
819 * @endcode
820 *
821 *
822 * <a name="Pressureboundaryvalues"></a>
823 * <h4>Pressure boundary values</h4>
824 *
825
826 *
827 * The next are pressure boundary values. As mentioned in the introduction,
828 * we choose a linear pressure field:
829 *
830 * @code
831 * template <int dim>
832 * class PressureBoundaryValues : public Function<dim>
833 * {
834 * public:
835 * PressureBoundaryValues()
836 * : Function<dim>(1)
837 * {}
838 *
839 * virtual double value(const Point<dim> &p,
840 * const unsigned int /*component*/ = 0) const override
841 * {
842 * return 1 - p[0];
843 * }
844 * };
845 *
846 *
847 *
848 * @endcode
849 *
850 *
851 * <a name="Saturationboundaryvalues"></a>
852 * <h4>Saturation boundary values</h4>
853 *
854
855 *
856 * Then we also need boundary values on the inflow portions of the
857 * boundary. The question whether something is an inflow part is decided
858 * when assembling the right hand side, we only have to provide a functional
859 * description of the boundary values. This is as explained in the
860 * introduction:
861 *
862 * @code
863 * template <int dim>
864 * class SaturationBoundaryValues : public Function<dim>
865 * {
866 * public:
867 * SaturationBoundaryValues()
868 * : Function<dim>(1)
869 * {}
870 *
871 * virtual double value(const Point<dim> &p,
872 * const unsigned int /*component*/ = 0) const override
873 * {
874 * if (p[0] == 0)
875 * return 1;
876 * else
877 * return 0;
878 * }
879 * };
880 *
881 *
882 *
883 * @endcode
884 *
885 *
886 * <a name="Initialdata"></a>
887 * <h4>Initial data</h4>
888 *
889
890 *
891 * Finally, we need initial data. In reality, we only need initial data for
892 * the saturation, but we are lazy, so we will later, before the first time
893 * step, simply interpolate the entire solution for the previous time step
894 * from a function that contains all vector components.
895 *
896
897 *
898 * We therefore simply create a function that returns zero in all
899 * components. We do that by simply forward every function to the
900 * Functions::ZeroFunction class. Why not use that right away in the places of
901 * this program where we presently use the <code>InitialValues</code> class?
902 * Because this way it is simpler to later go back and choose a different
903 * function for initial values.
904 *
905 * @code
906 * template <int dim>
907 * class InitialValues : public Function<dim>
908 * {
909 * public:
910 * InitialValues()
911 * : Function<dim>(dim + 2)
912 * {}
913 *
914 * virtual double value(const Point<dim> & p,
915 * const unsigned int component = 0) const override
916 * {
917 * return Functions::ZeroFunction<dim>(dim + 2).value(p, component);
918 * }
919 *
920 * virtual void vector_value(const Point<dim> &p,
921 * Vector<double> & values) const override
922 * {
924 * }
925 * };
926 *
927 *
928 *
929 * @endcode
930 *
931 *
932 * <a name="Theinversepermeabilitytensor"></a>
933 * <h3>The inverse permeability tensor</h3>
934 *
935
936 *
937 * As announced in the introduction, we implement two different permeability
938 * tensor fields. Each of them we put into a namespace of its own, so that
939 * it will be easy later to replace use of one by the other in the code.
940 *
941
942 *
943 *
944 * <a name="Singlecurvingcrackpermeability"></a>
945 * <h4>Single curving crack permeability</h4>
946 *
947
948 *
949 * The first function for the permeability was the one that models a single
950 * curving crack. It was already used at the end of @ref step_20 "step-20", and its
951 * functional form is given in the introduction of the present tutorial
952 * program. As in some previous programs, we have to declare a (seemingly
953 * unnecessary) default constructor of the KInverse class to avoid warnings
954 * from some compilers:
955 *
956 * @code
957 * namespace SingleCurvingCrack
958 * {
959 * template <int dim>
960 * class KInverse : public TensorFunction<2, dim>
961 * {
962 * public:
963 * KInverse()
965 * {}
966 *
967 * virtual void
968 * value_list(const std::vector<Point<dim>> &points,
969 * std::vector<Tensor<2, dim>> & values) const override
970 * {
971 * Assert(points.size() == values.size(),
972 * ExcDimensionMismatch(points.size(), values.size()));
973 *
974 * for (unsigned int p = 0; p < points.size(); ++p)
975 * {
976 * values[p].clear();
977 *
978 * const double distance_to_flowline =
979 * std::fabs(points[p][1] - 0.5 - 0.1 * std::sin(10 * points[p][0]));
980 *
981 * const double permeability =
982 * std::max(std::exp(-(distance_to_flowline * distance_to_flowline) /
983 * (0.1 * 0.1)),
984 * 0.01);
985 *
986 * for (unsigned int d = 0; d < dim; ++d)
987 * values[p][d][d] = 1. / permeability;
988 * }
989 * }
990 * };
991 * } // namespace SingleCurvingCrack
992 *
993 *
994 * @endcode
995 *
996 *
997 * <a name="Randommediumpermeability"></a>
998 * <h4>Random medium permeability</h4>
999 *
1000
1001 *
1002 * This function does as announced in the introduction, i.e. it creates an
1003 * overlay of exponentials at random places. There is one thing worth
1004 * considering for this class. The issue centers around the problem that the
1005 * class creates the centers of the exponentials using a random function. If
1006 * we therefore created the centers each time we create an object of the
1007 * present type, we would get a different list of centers each time. That's
1008 * not what we expect from classes of this type: they should reliably
1009 * represent the same function.
1010 *
1011
1012 *
1013 * The solution to this problem is to make the list of centers a static
1014 * member variable of this class, i.e. there exists exactly one such
1015 * variable for the entire program, rather than for each object of this
1016 * type. That's exactly what we are going to do.
1017 *
1018
1019 *
1020 * The next problem, however, is that we need a way to initialize this
1021 * variable. Since this variable is initialized at the beginning of the
1022 * program, we can't use a regular member function for that since there may
1023 * not be an object of this type around at the time. The C++ standard
1024 * therefore says that only non-member and static member functions can be
1025 * used to initialize a static variable. We use the latter possibility by
1026 * defining a function <code>get_centers</code> that computes the list of
1027 * center points when called.
1028 *
1029
1030 *
1031 * Note that this class works just fine in both 2d and 3d, with the only
1032 * difference being that we use more points in 3d: by experimenting we find
1033 * that we need more exponentials in 3d than in 2d (we have more ground to
1034 * cover, after all, if we want to keep the distance between centers roughly
1035 * equal), so we choose 40 in 2d and 100 in 3d. For any other dimension, the
1036 * function does presently not know what to do so simply throws an exception
1037 * indicating exactly this.
1038 *
1039 * @code
1040 * namespace RandomMedium
1041 * {
1042 * template <int dim>
1043 * class KInverse : public TensorFunction<2, dim>
1044 * {
1045 * public:
1046 * KInverse()
1047 * : TensorFunction<2, dim>()
1048 * {}
1049 *
1050 * virtual void
1051 * value_list(const std::vector<Point<dim>> &points,
1052 * std::vector<Tensor<2, dim>> & values) const override
1053 * {
1054 * Assert(points.size() == values.size(),
1055 * ExcDimensionMismatch(points.size(), values.size()));
1056 *
1057 * for (unsigned int p = 0; p < points.size(); ++p)
1058 * {
1059 * values[p].clear();
1060 *
1061 * double permeability = 0;
1062 * for (unsigned int i = 0; i < centers.size(); ++i)
1063 * permeability += std::exp(-(points[p] - centers[i]).norm_square() /
1064 * (0.05 * 0.05));
1065 *
1066 * const double normalized_permeability =
1067 * std::min(std::max(permeability, 0.01), 4.);
1068 *
1069 * for (unsigned int d = 0; d < dim; ++d)
1070 * values[p][d][d] = 1. / normalized_permeability;
1071 * }
1072 * }
1073 *
1074 * private:
1075 * static std::vector<Point<dim>> centers;
1076 *
1077 * static std::vector<Point<dim>> get_centers()
1078 * {
1079 * const unsigned int N =
1080 * (dim == 2 ? 40 : (dim == 3 ? 100 : throw ExcNotImplemented()));
1081 *
1082 * std::vector<Point<dim>> centers_list(N);
1083 * for (unsigned int i = 0; i < N; ++i)
1084 * for (unsigned int d = 0; d < dim; ++d)
1085 * centers_list[i][d] = static_cast<double>(rand()) / RAND_MAX;
1086 *
1087 * return centers_list;
1088 * }
1089 * };
1090 *
1091 *
1092 *
1093 * template <int dim>
1094 * std::vector<Point<dim>>
1095 * KInverse<dim>::centers = KInverse<dim>::get_centers();
1096 * } // namespace RandomMedium
1097 *
1098 *
1099 *
1100 * @endcode
1101 *
1102 *
1103 * <a name="Theinversemobilityandsaturationfunctions"></a>
1104 * <h3>The inverse mobility and saturation functions</h3>
1105 *
1106
1107 *
1108 * There are two more pieces of data that we need to describe, namely the
1109 * inverse mobility function and the saturation curve. Their form is also
1110 * given in the introduction:
1111 *
1112 * @code
1113 * double mobility_inverse(const double S, const double viscosity)
1114 * {
1115 * return 1.0 / (1.0 / viscosity * S * S + (1 - S) * (1 - S));
1116 * }
1117 *
1118 * double fractional_flow(const double S, const double viscosity)
1119 * {
1120 * return S * S / (S * S + viscosity * (1 - S) * (1 - S));
1121 * }
1122 *
1123 *
1124 *
1125 * @endcode
1126 *
1127 *
1128 * <a name="Linearsolversandpreconditioners"></a>
1129 * <h3>Linear solvers and preconditioners</h3>
1130 *
1131
1132 *
1133 * The linear solvers we use are also completely analogous to the ones used
1134 * in @ref step_20 "step-20". The following classes are therefore copied verbatim from
1135 * there. Note that the classes here are not only copied from
1136 * @ref step_20 "step-20", but also duplicate classes in deal.II. In a future version of this
1137 * example, they should be replaced by an efficient method, though. There is a
1138 * single change: if the size of a linear system is small, i.e. when the mesh
1139 * is very coarse, then it is sometimes not sufficient to set a maximum of
1140 * <code>src.size()</code> CG iterations before the solver in the
1141 * <code>vmult()</code> function converges. (This is, of course, a result of
1142 * numerical round-off, since we know that on paper, the CG method converges
1143 * in at most <code>src.size()</code> steps.) As a consequence, we set the
1144 * maximum number of iterations equal to the maximum of the size of the linear
1145 * system and 200.
1146 *
1147 * @code
1148 * template <class MatrixType>
1149 * class InverseMatrix : public Subscriptor
1150 * {
1151 * public:
1152 * InverseMatrix(const MatrixType &m)
1153 * : matrix(&m)
1154 * {}
1155 *
1156 * void vmult(Vector<double> &dst, const Vector<double> &src) const
1157 * {
1158 * SolverControl solver_control(std::max<unsigned int>(src.size(), 200),
1159 * 1e-8 * src.l2_norm());
1160 * SolverCG<Vector<double>> cg(solver_control);
1161 *
1162 * dst = 0;
1163 *
1164 * cg.solve(*matrix, dst, src, PreconditionIdentity());
1165 * }
1166 *
1167 * private:
1168 * const SmartPointer<const MatrixType> matrix;
1169 * };
1170 *
1171 *
1172 *
1173 * class SchurComplement : public Subscriptor
1174 * {
1175 * public:
1176 * SchurComplement(const BlockSparseMatrix<double> & A,
1177 * const InverseMatrix<SparseMatrix<double>> &Minv)
1178 * : system_matrix(&A)
1179 * , m_inverse(&Minv)
1180 * , tmp1(A.block(0, 0).m())
1181 * , tmp2(A.block(0, 0).m())
1182 * {}
1183 *
1184 * void vmult(Vector<double> &dst, const Vector<double> &src) const
1185 * {
1186 * system_matrix->block(0, 1).vmult(tmp1, src);
1187 * m_inverse->vmult(tmp2, tmp1);
1188 * system_matrix->block(1, 0).vmult(dst, tmp2);
1189 * }
1190 *
1191 * private:
1192 * const SmartPointer<const BlockSparseMatrix<double>> system_matrix;
1193 * const SmartPointer<const InverseMatrix<SparseMatrix<double>>> m_inverse;
1194 *
1195 * mutable Vector<double> tmp1, tmp2;
1196 * };
1197 *
1198 *
1199 *
1200 * class ApproximateSchurComplement : public Subscriptor
1201 * {
1202 * public:
1203 * ApproximateSchurComplement(const BlockSparseMatrix<double> &A)
1204 * : system_matrix(&A)
1205 * , tmp1(A.block(0, 0).m())
1206 * , tmp2(A.block(0, 0).m())
1207 * {}
1208 *
1209 * void vmult(Vector<double> &dst, const Vector<double> &src) const
1210 * {
1211 * system_matrix->block(0, 1).vmult(tmp1, src);
1212 * system_matrix->block(0, 0).precondition_Jacobi(tmp2, tmp1);
1213 * system_matrix->block(1, 0).vmult(dst, tmp2);
1214 * }
1215 *
1216 * private:
1217 * const SmartPointer<const BlockSparseMatrix<double>> system_matrix;
1218 *
1219 * mutable Vector<double> tmp1, tmp2;
1220 * };
1221 *
1222 *
1223 *
1224 * @endcode
1225 *
1226 *
1227 * <a name="codeTwoPhaseFlowProblemcodeclassimplementation"></a>
1228 * <h3><code>TwoPhaseFlowProblem</code> class implementation</h3>
1229 *
1230
1231 *
1232 * Here now the implementation of the main class. Much of it is actually
1233 * copied from @ref step_20 "step-20", so we won't comment on it in much detail. You should
1234 * try to get familiar with that program first, then most of what is
1235 * happening here should be mostly clear.
1236 *
1237
1238 *
1239 *
1240 * <a name="TwoPhaseFlowProblemTwoPhaseFlowProblem"></a>
1241 * <h4>TwoPhaseFlowProblem::TwoPhaseFlowProblem</h4>
1242 *
1243
1244 *
1245 * First for the constructor. We use @f$RT_k \times DQ_k \times DQ_k@f$
1246 * spaces. For initializing the DiscreteTime object, we don't set the time
1247 * step size in the constructor because we don't have its value yet.
1248 * The time step size is initially set to zero, but it will be computed
1249 * before it is needed to increment time, as described in a subsection of
1250 * the introduction. The time object internally prevents itself from being
1251 * incremented when @f$dt = 0@f$, forcing us to set a non-zero desired size for
1252 * @f$dt@f$ before advancing time.
1253 *
1254 * @code
1255 * template <int dim>
1256 * TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem(const unsigned int degree)
1257 * : degree(degree)
1258 * , fe(FE_RaviartThomas<dim>(degree),
1259 * 1,
1260 * FE_DGQ<dim>(degree),
1261 * 1,
1262 * FE_DGQ<dim>(degree),
1263 * 1)
1264 * , dof_handler(triangulation)
1265 * , n_refinement_steps(5)
1266 * , time(/*start time*/ 0., /*end time*/ 1.)
1267 * , viscosity(0.2)
1268 * {}
1269 *
1270 *
1271 *
1272 * @endcode
1273 *
1274 *
1275 * <a name="TwoPhaseFlowProblemmake_grid_and_dofs"></a>
1276 * <h4>TwoPhaseFlowProblem::make_grid_and_dofs</h4>
1277 *
1278
1279 *
1280 * This next function starts out with well-known functions calls that create
1281 * and refine a mesh, and then associate degrees of freedom with it. It does
1282 * all the same things as in @ref step_20 "step-20", just now for three components instead
1283 * of two.
1284 *
1285 * @code
1286 * template <int dim>
1287 * void TwoPhaseFlowProblem<dim>::make_grid_and_dofs()
1288 * {
1290 * triangulation.refine_global(n_refinement_steps);
1291 *
1292 * dof_handler.distribute_dofs(fe);
1293 * DoFRenumbering::component_wise(dof_handler);
1294 *
1295 * const std::vector<types::global_dof_index> dofs_per_component =
1297 * const unsigned int n_u = dofs_per_component[0],
1298 * n_p = dofs_per_component[dim],
1299 * n_s = dofs_per_component[dim + 1];
1300 *
1301 * std::cout << "Number of active cells: " << triangulation.n_active_cells()
1302 * << std::endl
1303 * << "Number of degrees of freedom: " << dof_handler.n_dofs()
1304 * << " (" << n_u << '+' << n_p << '+' << n_s << ')' << std::endl
1305 * << std::endl;
1306 *
1307 * const unsigned int n_couplings = dof_handler.max_couplings_between_dofs();
1308 *
1309 * sparsity_pattern.reinit(3, 3);
1310 * sparsity_pattern.block(0, 0).reinit(n_u, n_u, n_couplings);
1311 * sparsity_pattern.block(1, 0).reinit(n_p, n_u, n_couplings);
1312 * sparsity_pattern.block(2, 0).reinit(n_s, n_u, n_couplings);
1313 * sparsity_pattern.block(0, 1).reinit(n_u, n_p, n_couplings);
1314 * sparsity_pattern.block(1, 1).reinit(n_p, n_p, n_couplings);
1315 * sparsity_pattern.block(2, 1).reinit(n_s, n_p, n_couplings);
1316 * sparsity_pattern.block(0, 2).reinit(n_u, n_s, n_couplings);
1317 * sparsity_pattern.block(1, 2).reinit(n_p, n_s, n_couplings);
1318 * sparsity_pattern.block(2, 2).reinit(n_s, n_s, n_couplings);
1319 *
1320 * sparsity_pattern.collect_sizes();
1321 *
1322 * DoFTools::make_sparsity_pattern(dof_handler, sparsity_pattern);
1323 * sparsity_pattern.compress();
1324 *
1325 *
1326 * system_matrix.reinit(sparsity_pattern);
1327 *
1328 *
1329 * solution.reinit(3);
1330 * solution.block(0).reinit(n_u);
1331 * solution.block(1).reinit(n_p);
1332 * solution.block(2).reinit(n_s);
1333 * solution.collect_sizes();
1334 *
1335 * old_solution.reinit(3);
1336 * old_solution.block(0).reinit(n_u);
1337 * old_solution.block(1).reinit(n_p);
1338 * old_solution.block(2).reinit(n_s);
1339 * old_solution.collect_sizes();
1340 *
1341 * system_rhs.reinit(3);
1342 * system_rhs.block(0).reinit(n_u);
1343 * system_rhs.block(1).reinit(n_p);
1344 * system_rhs.block(2).reinit(n_s);
1345 * system_rhs.collect_sizes();
1346 * }
1347 *
1348 *
1349 * @endcode
1350 *
1351 *
1352 * <a name="TwoPhaseFlowProblemassemble_system"></a>
1353 * <h4>TwoPhaseFlowProblem::assemble_system</h4>
1354 *
1355
1356 *
1357 * This is the function that assembles the linear system, or at least
1358 * everything except the (1,3) block that depends on the still-unknown
1359 * velocity computed during this time step (we deal with this in
1360 * <code>assemble_rhs_S</code>). Much of it is again as in @ref step_20 "step-20", but we
1361 * have to deal with some nonlinearity this time. However, the top of the
1362 * function is pretty much as usual (note that we set matrix and right hand
1363 * side to zero at the beginning &mdash; something we didn't have to do for
1364 * stationary problems since there we use each matrix object only once and
1365 * it is empty at the beginning anyway).
1366 *
1367
1368 *
1369 * Note that in its present form, the function uses the permeability
1370 * implemented in the RandomMedium::KInverse class. Switching to the single
1371 * curved crack permeability function is as simple as just changing the
1372 * namespace name.
1373 *
1374 * @code
1375 * template <int dim>
1376 * void TwoPhaseFlowProblem<dim>::assemble_system()
1377 * {
1378 * system_matrix = 0;
1379 * system_rhs = 0;
1380 *
1381 * QGauss<dim> quadrature_formula(degree + 2);
1382 * QGauss<dim - 1> face_quadrature_formula(degree + 2);
1383 *
1384 * FEValues<dim> fe_values(fe,
1385 * quadrature_formula,
1388 * FEFaceValues<dim> fe_face_values(fe,
1389 * face_quadrature_formula,
1393 *
1394 * const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1395 *
1396 * const unsigned int n_q_points = quadrature_formula.size();
1397 * const unsigned int n_face_q_points = face_quadrature_formula.size();
1398 *
1399 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1400 * Vector<double> local_rhs(dofs_per_cell);
1401 *
1402 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1403 *
1404 * const PressureRightHandSide<dim> pressure_right_hand_side;
1405 * const PressureBoundaryValues<dim> pressure_boundary_values;
1406 * const RandomMedium::KInverse<dim> k_inverse;
1407 *
1408 * std::vector<double> pressure_rhs_values(n_q_points);
1409 * std::vector<double> boundary_values(n_face_q_points);
1410 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1411 *
1412 * std::vector<Vector<double>> old_solution_values(n_q_points,
1413 * Vector<double>(dim + 2));
1414 * std::vector<std::vector<Tensor<1, dim>>> old_solution_grads(
1415 * n_q_points, std::vector<Tensor<1, dim>>(dim + 2));
1416 *
1417 * const FEValuesExtractors::Vector velocities(0);
1418 * const FEValuesExtractors::Scalar pressure(dim);
1419 * const FEValuesExtractors::Scalar saturation(dim + 1);
1420 *
1421 * for (const auto &cell : dof_handler.active_cell_iterators())
1422 * {
1423 * fe_values.reinit(cell);
1424 * local_matrix = 0;
1425 * local_rhs = 0;
1426 *
1427 * @endcode
1428 *
1429 * Here's the first significant difference: We have to get the values
1430 * of the saturation function of the previous time step at the
1431 * quadrature points. To this end, we can use the
1432 * FEValues::get_function_values (previously already used in @ref step_9 "step-9",
1433 * @ref step_14 "step-14" and @ref step_15 "step-15"), a function that takes a solution vector and
1434 * returns a list of function values at the quadrature points of the
1435 * present cell. In fact, it returns the complete vector-valued
1436 * solution at each quadrature point, i.e. not only the saturation but
1437 * also the velocities and pressure:
1438 *
1439 * @code
1440 * fe_values.get_function_values(old_solution, old_solution_values);
1441 *
1442 * @endcode
1443 *
1444 * Then we also have to get the values of the pressure right hand side
1445 * and of the inverse permeability tensor at the quadrature points:
1446 *
1447 * @code
1448 * pressure_right_hand_side.value_list(fe_values.get_quadrature_points(),
1449 * pressure_rhs_values);
1450 * k_inverse.value_list(fe_values.get_quadrature_points(),
1451 * k_inverse_values);
1452 *
1453 * @endcode
1454 *
1455 * With all this, we can now loop over all the quadrature points and
1456 * shape functions on this cell and assemble those parts of the matrix
1457 * and right hand side that we deal with in this function. The
1458 * individual terms in the contributions should be self-explanatory
1459 * given the explicit form of the bilinear form stated in the
1460 * introduction:
1461 *
1462 * @code
1463 * for (unsigned int q = 0; q < n_q_points; ++q)
1464 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1465 * {
1466 * const double old_s = old_solution_values[q](dim + 1);
1467 *
1468 * const Tensor<1, dim> phi_i_u = fe_values[velocities].value(i, q);
1469 * const double div_phi_i_u = fe_values[velocities].divergence(i, q);
1470 * const double phi_i_p = fe_values[pressure].value(i, q);
1471 * const double phi_i_s = fe_values[saturation].value(i, q);
1472 *
1473 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1474 * {
1475 * const Tensor<1, dim> phi_j_u =
1476 * fe_values[velocities].value(j, q);
1477 * const double div_phi_j_u =
1478 * fe_values[velocities].divergence(j, q);
1479 * const double phi_j_p = fe_values[pressure].value(j, q);
1480 * const double phi_j_s = fe_values[saturation].value(j, q);
1481 *
1482 * local_matrix(i, j) +=
1483 * (phi_i_u * k_inverse_values[q] *
1484 * mobility_inverse(old_s, viscosity) * phi_j_u -
1485 * div_phi_i_u * phi_j_p - phi_i_p * div_phi_j_u +
1486 * phi_i_s * phi_j_s) *
1487 * fe_values.JxW(q);
1488 * }
1489 *
1490 * local_rhs(i) +=
1491 * (-phi_i_p * pressure_rhs_values[q]) * fe_values.JxW(q);
1492 * }
1493 *
1494 *
1495 * @endcode
1496 *
1497 * Next, we also have to deal with the pressure boundary values. This,
1498 * again is as in @ref step_20 "step-20":
1499 *
1500 * @code
1501 * for (const auto &face : cell->face_iterators())
1502 * if (face->at_boundary())
1503 * {
1504 * fe_face_values.reinit(cell, face);
1505 *
1506 * pressure_boundary_values.value_list(
1507 * fe_face_values.get_quadrature_points(), boundary_values);
1508 *
1509 * for (unsigned int q = 0; q < n_face_q_points; ++q)
1510 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1511 * {
1512 * const Tensor<1, dim> phi_i_u =
1513 * fe_face_values[velocities].value(i, q);
1514 *
1515 * local_rhs(i) +=
1516 * -(phi_i_u * fe_face_values.normal_vector(q) *
1517 * boundary_values[q] * fe_face_values.JxW(q));
1518 * }
1519 * }
1520 *
1521 * @endcode
1522 *
1523 * The final step in the loop over all cells is to transfer local
1524 * contributions into the global matrix and right hand side vector:
1525 *
1526 * @code
1527 * cell->get_dof_indices(local_dof_indices);
1528 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1529 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1530 * system_matrix.add(local_dof_indices[i],
1531 * local_dof_indices[j],
1532 * local_matrix(i, j));
1533 *
1534 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1535 * system_rhs(local_dof_indices[i]) += local_rhs(i);
1536 * }
1537 * }
1538 *
1539 *
1540 * @endcode
1541 *
1542 * So much for assembly of matrix and right hand side. Note that we do not
1543 * have to interpolate and apply boundary values since they have all been
1544 * taken care of in the weak form already.
1545 *
1546
1547 *
1548 *
1549
1550 *
1551 *
1552 * <a name="TwoPhaseFlowProblemassemble_rhs_S"></a>
1553 * <h4>TwoPhaseFlowProblem::assemble_rhs_S</h4>
1554 *
1555
1556 *
1557 * As explained in the introduction, we can only evaluate the right hand
1558 * side of the saturation equation once the velocity has been computed. We
1559 * therefore have this separate function to this end.
1560 *
1561 * @code
1562 * template <int dim>
1563 * void TwoPhaseFlowProblem<dim>::assemble_rhs_S()
1564 * {
1565 * QGauss<dim> quadrature_formula(degree + 2);
1566 * QGauss<dim - 1> face_quadrature_formula(degree + 2);
1567 * FEValues<dim> fe_values(fe,
1568 * quadrature_formula,
1569 * update_values | update_gradients |
1570 * update_quadrature_points | update_JxW_values);
1571 * FEFaceValues<dim> fe_face_values(fe,
1572 * face_quadrature_formula,
1573 * update_values | update_normal_vectors |
1574 * update_quadrature_points |
1575 * update_JxW_values);
1576 * FEFaceValues<dim> fe_face_values_neighbor(fe,
1577 * face_quadrature_formula,
1578 * update_values);
1579 *
1580 * const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1581 * const unsigned int n_q_points = quadrature_formula.size();
1582 * const unsigned int n_face_q_points = face_quadrature_formula.size();
1583 *
1584 * Vector<double> local_rhs(dofs_per_cell);
1585 *
1586 * std::vector<Vector<double>> old_solution_values(n_q_points,
1587 * Vector<double>(dim + 2));
1588 * std::vector<Vector<double>> old_solution_values_face(n_face_q_points,
1589 * Vector<double>(dim +
1590 * 2));
1591 * std::vector<Vector<double>> old_solution_values_face_neighbor(
1592 * n_face_q_points, Vector<double>(dim + 2));
1593 * std::vector<Vector<double>> present_solution_values(n_q_points,
1594 * Vector<double>(dim +
1595 * 2));
1596 * std::vector<Vector<double>> present_solution_values_face(
1597 * n_face_q_points, Vector<double>(dim + 2));
1598 *
1599 * std::vector<double> neighbor_saturation(n_face_q_points);
1600 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1601 *
1602 * SaturationBoundaryValues<dim> saturation_boundary_values;
1603 *
1604 * const FEValuesExtractors::Scalar saturation(dim + 1);
1605 *
1606 * for (const auto &cell : dof_handler.active_cell_iterators())
1607 * {
1608 * local_rhs = 0;
1609 * fe_values.reinit(cell);
1610 *
1611 * fe_values.get_function_values(old_solution, old_solution_values);
1612 * fe_values.get_function_values(solution, present_solution_values);
1613 *
1614 * @endcode
1615 *
1616 * First for the cell terms. These are, following the formulas in the
1617 * introduction, @f$(S^n,\sigma)-(F(S^n) \mathbf{v}^{n+1},\nabla
1618 * \sigma)@f$, where @f$\sigma@f$ is the saturation component of the test
1619 * function:
1620 *
1621 * @code
1622 * for (unsigned int q = 0; q < n_q_points; ++q)
1623 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1624 * {
1625 * const double old_s = old_solution_values[q](dim + 1);
1626 * Tensor<1, dim> present_u;
1627 * for (unsigned int d = 0; d < dim; ++d)
1628 * present_u[d] = present_solution_values[q](d);
1629 *
1630 * const double phi_i_s = fe_values[saturation].value(i, q);
1631 * const Tensor<1, dim> grad_phi_i_s =
1632 * fe_values[saturation].gradient(i, q);
1633 *
1634 * local_rhs(i) +=
1635 * (time.get_next_step_size() * fractional_flow(old_s, viscosity) *
1636 * present_u * grad_phi_i_s +
1637 * old_s * phi_i_s) *
1638 * fe_values.JxW(q);
1639 * }
1640 *
1641 * @endcode
1642 *
1643 * Secondly, we have to deal with the flux parts on the face
1644 * boundaries. This was a bit more involved because we first have to
1645 * determine which are the influx and outflux parts of the cell
1646 * boundary. If we have an influx boundary, we need to evaluate the
1647 * saturation on the other side of the face (or the boundary values,
1648 * if we are at the boundary of the domain).
1649 *
1650
1651 *
1652 * All this is a bit tricky, but has been explained in some detail
1653 * already in @ref step_9 "step-9". Take a look there how this is supposed to work!
1654 *
1655 * @code
1656 * for (const auto face_no : cell->face_indices())
1657 * {
1658 * fe_face_values.reinit(cell, face_no);
1659 *
1660 * fe_face_values.get_function_values(old_solution,
1661 * old_solution_values_face);
1662 * fe_face_values.get_function_values(solution,
1663 * present_solution_values_face);
1664 *
1665 * if (cell->at_boundary(face_no))
1666 * saturation_boundary_values.value_list(
1667 * fe_face_values.get_quadrature_points(), neighbor_saturation);
1668 * else
1669 * {
1670 * const auto neighbor = cell->neighbor(face_no);
1671 * const unsigned int neighbor_face =
1672 * cell->neighbor_of_neighbor(face_no);
1673 *
1674 * fe_face_values_neighbor.reinit(neighbor, neighbor_face);
1675 *
1676 * fe_face_values_neighbor.get_function_values(
1677 * old_solution, old_solution_values_face_neighbor);
1678 *
1679 * for (unsigned int q = 0; q < n_face_q_points; ++q)
1680 * neighbor_saturation[q] =
1681 * old_solution_values_face_neighbor[q](dim + 1);
1682 * }
1683 *
1684 *
1685 * for (unsigned int q = 0; q < n_face_q_points; ++q)
1686 * {
1687 * Tensor<1, dim> present_u_face;
1688 * for (unsigned int d = 0; d < dim; ++d)
1689 * present_u_face[d] = present_solution_values_face[q](d);
1690 *
1691 * const double normal_flux =
1692 * present_u_face * fe_face_values.normal_vector(q);
1693 *
1694 * const bool is_outflow_q_point = (normal_flux >= 0);
1695 *
1696 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1697 * local_rhs(i) -=
1698 * time.get_next_step_size() * normal_flux *
1699 * fractional_flow((is_outflow_q_point == true ?
1700 * old_solution_values_face[q](dim + 1) :
1701 * neighbor_saturation[q]),
1702 * viscosity) *
1703 * fe_face_values[saturation].value(i, q) *
1704 * fe_face_values.JxW(q);
1705 * }
1706 * }
1707 *
1708 * cell->get_dof_indices(local_dof_indices);
1709 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1710 * system_rhs(local_dof_indices[i]) += local_rhs(i);
1711 * }
1712 * }
1713 *
1714 *
1715 *
1716 * @endcode
1717 *
1718 *
1719 * <a name="TwoPhaseFlowProblemsolve"></a>
1720 * <h4>TwoPhaseFlowProblem::solve</h4>
1721 *
1722
1723 *
1724 * After all these preparations, we finally solve the linear system for
1725 * velocity and pressure in the same way as in @ref step_20 "step-20". After that, we have
1726 * to deal with the saturation equation (see below):
1727 *
1728 * @code
1729 * template <int dim>
1730 * void TwoPhaseFlowProblem<dim>::solve()
1731 * {
1732 * const InverseMatrix<SparseMatrix<double>> m_inverse(
1733 * system_matrix.block(0, 0));
1734 * Vector<double> tmp(solution.block(0).size());
1735 * Vector<double> schur_rhs(solution.block(1).size());
1736 * Vector<double> tmp2(solution.block(2).size());
1737 *
1738 *
1739 * @endcode
1740 *
1741 * First the pressure, using the pressure Schur complement of the first
1742 * two equations:
1743 *
1744 * @code
1745 * {
1746 * m_inverse.vmult(tmp, system_rhs.block(0));
1747 * system_matrix.block(1, 0).vmult(schur_rhs, tmp);
1748 * schur_rhs -= system_rhs.block(1);
1749 *
1750 *
1751 * SchurComplement schur_complement(system_matrix, m_inverse);
1752 *
1753 * ApproximateSchurComplement approximate_schur_complement(system_matrix);
1754 *
1755 * InverseMatrix<ApproximateSchurComplement> preconditioner(
1756 * approximate_schur_complement);
1757 *
1758 *
1759 * SolverControl solver_control(solution.block(1).size(),
1760 * 1e-12 * schur_rhs.l2_norm());
1761 * SolverCG<Vector<double>> cg(solver_control);
1762 *
1763 * cg.solve(schur_complement, solution.block(1), schur_rhs, preconditioner);
1764 *
1765 * std::cout << " " << solver_control.last_step()
1766 * << " CG Schur complement iterations for pressure." << std::endl;
1767 * }
1768 *
1769 * @endcode
1770 *
1771 * Now the velocity:
1772 *
1773 * @code
1774 * {
1775 * system_matrix.block(0, 1).vmult(tmp, solution.block(1));
1776 * tmp *= -1;
1777 * tmp += system_rhs.block(0);
1778 *
1779 * m_inverse.vmult(solution.block(0), tmp);
1780 * }
1781 *
1782 * @endcode
1783 *
1784 * Finally, we have to take care of the saturation equation. The first
1785 * business we have here is to determine the time step using the formula
1786 * in the introduction. Knowing the shape of our domain and that we
1787 * created the mesh by regular subdivision of cells, we can compute the
1788 * diameter of each of our cells quite easily (in fact we use the linear
1789 * extensions in coordinate directions of the cells, not the
1790 * diameter). Note that we will learn a more general way to do this in
1791 * @ref step_24 "step-24", where we use the GridTools::minimal_cell_diameter function.
1792 *
1793
1794 *
1795 * The maximal velocity we compute using a helper function to compute the
1796 * maximal velocity defined below, and with all this we can evaluate our
1797 * new time step length. We use the method
1798 * DiscreteTime::set_desired_next_time_step() to suggest the new
1799 * calculated value of the time step to the DiscreteTime object. In most
1800 * cases, the time object uses the exact provided value to increment time.
1801 * It some case, the step size may be modified further by the time object.
1802 * For example, if the calculated time increment overshoots the end time,
1803 * it is truncated accordingly.
1804 *
1805 * @code
1806 * time.set_desired_next_step_size(std::pow(0.5, double(n_refinement_steps)) /
1807 * get_maximal_velocity());
1808 *
1809 * @endcode
1810 *
1811 * The next step is to assemble the right hand side, and then to pass
1812 * everything on for solution. At the end, we project back saturations
1813 * onto the physically reasonable range:
1814 *
1815 * @code
1816 * assemble_rhs_S();
1817 * {
1818 * SolverControl solver_control(system_matrix.block(2, 2).m(),
1819 * 1e-8 * system_rhs.block(2).l2_norm());
1820 * SolverCG<Vector<double>> cg(solver_control);
1821 * cg.solve(system_matrix.block(2, 2),
1822 * solution.block(2),
1823 * system_rhs.block(2),
1824 * PreconditionIdentity());
1825 *
1826 * project_back_saturation();
1827 *
1828 * std::cout << " " << solver_control.last_step()
1829 * << " CG iterations for saturation." << std::endl;
1830 * }
1831 *
1832 *
1833 * old_solution = solution;
1834 * }
1835 *
1836 *
1837 * @endcode
1838 *
1839 *
1840 * <a name="TwoPhaseFlowProblemoutput_results"></a>
1841 * <h4>TwoPhaseFlowProblem::output_results</h4>
1842 *
1843
1844 *
1845 * There is nothing surprising here. Since the program will do a lot of time
1846 * steps, we create an output file only every fifth time step and skip all
1847 * other time steps at the top of the file already.
1848 *
1849
1850 *
1851 * When creating file names for output close to the bottom of the function,
1852 * we convert the number of the time step to a string representation that
1853 * is padded by leading zeros to four digits. We do this because this way
1854 * all output file names have the same length, and consequently sort well
1855 * when creating a directory listing.
1856 *
1857 * @code
1858 * template <int dim>
1859 * void TwoPhaseFlowProblem<dim>::output_results() const
1860 * {
1861 * if (time.get_step_number() % 5 != 0)
1862 * return;
1863 *
1864 * std::vector<std::string> solution_names;
1865 * switch (dim)
1866 * {
1867 * case 2:
1868 * solution_names = {"u", "v", "p", "S"};
1869 * break;
1870 *
1871 * case 3:
1872 * solution_names = {"u", "v", "w", "p", "S"};
1873 * break;
1874 *
1875 * default:
1876 * Assert(false, ExcNotImplemented());
1877 * }
1878 *
1879 * DataOut<dim> data_out;
1880 *
1881 * data_out.attach_dof_handler(dof_handler);
1882 * data_out.add_data_vector(solution, solution_names);
1883 *
1884 * data_out.build_patches(degree + 1);
1885 *
1886 * std::ofstream output("solution-" +
1887 * Utilities::int_to_string(time.get_step_number(), 4) +
1888 * ".vtk");
1889 * data_out.write_vtk(output);
1890 * }
1891 *
1892 *
1893 *
1894 * @endcode
1895 *
1896 *
1897 * <a name="TwoPhaseFlowProblemproject_back_saturation"></a>
1898 * <h4>TwoPhaseFlowProblem::project_back_saturation</h4>
1899 *
1900
1901 *
1902 * In this function, we simply run over all saturation degrees of freedom
1903 * and make sure that if they should have left the physically reasonable
1904 * range, that they be reset to the interval @f$[0,1]@f$. To do this, we only
1905 * have to loop over all saturation components of the solution vector; these
1906 * are stored in the block 2 (block 0 are the velocities, block 1 are the
1907 * pressures).
1908 *
1909
1910 *
1911 * It may be instructive to note that this function almost never triggers
1912 * when the time step is chosen as mentioned in the introduction. However,
1913 * if we choose the timestep only slightly larger, we get plenty of values
1914 * outside the proper range. Strictly speaking, the function is therefore
1915 * unnecessary if we choose the time step small enough. In a sense, the
1916 * function is therefore only a safety device to avoid situations where our
1917 * entire solution becomes unphysical because individual degrees of freedom
1918 * have become unphysical a few time steps earlier.
1919 *
1920 * @code
1921 * template <int dim>
1922 * void TwoPhaseFlowProblem<dim>::project_back_saturation()
1923 * {
1924 * for (unsigned int i = 0; i < solution.block(2).size(); ++i)
1925 * if (solution.block(2)(i) < 0)
1926 * solution.block(2)(i) = 0;
1927 * else if (solution.block(2)(i) > 1)
1928 * solution.block(2)(i) = 1;
1929 * }
1930 *
1931 *
1932 * @endcode
1933 *
1934 *
1935 * <a name="TwoPhaseFlowProblemget_maximal_velocity"></a>
1936 * <h4>TwoPhaseFlowProblem::get_maximal_velocity</h4>
1937 *
1938
1939 *
1940 * The following function is used in determining the maximal allowable time
1941 * step. What it does is to loop over all quadrature points in the domain
1942 * and find what the maximal magnitude of the velocity is.
1943 *
1944 * @code
1945 * template <int dim>
1946 * double TwoPhaseFlowProblem<dim>::get_maximal_velocity() const
1947 * {
1948 * QGauss<dim> quadrature_formula(degree + 2);
1949 * const unsigned int n_q_points = quadrature_formula.size();
1950 *
1951 * FEValues<dim> fe_values(fe, quadrature_formula, update_values);
1952 * std::vector<Vector<double>> solution_values(n_q_points,
1953 * Vector<double>(dim + 2));
1954 * double max_velocity = 0;
1955 *
1956 * for (const auto &cell : dof_handler.active_cell_iterators())
1957 * {
1958 * fe_values.reinit(cell);
1959 * fe_values.get_function_values(solution, solution_values);
1960 *
1961 * for (unsigned int q = 0; q < n_q_points; ++q)
1962 * {
1963 * Tensor<1, dim> velocity;
1964 * for (unsigned int i = 0; i < dim; ++i)
1965 * velocity[i] = solution_values[q](i);
1966 *
1967 * max_velocity = std::max(max_velocity, velocity.norm());
1968 * }
1969 * }
1970 *
1971 * return max_velocity;
1972 * }
1973 *
1974 *
1975 * @endcode
1976 *
1977 *
1978 * <a name="TwoPhaseFlowProblemrun"></a>
1979 * <h4>TwoPhaseFlowProblem::run</h4>
1980 *
1981
1982 *
1983 * This is the final function of our main class. Its brevity speaks for
1984 * itself. There are only two points worth noting: First, the function
1985 * projects the initial values onto the finite element space at the
1986 * beginning; the VectorTools::project function doing this requires an
1987 * argument indicating the hanging node constraints. We have none in this
1988 * program (we compute on a uniformly refined mesh), but the function
1989 * requires the argument anyway, of course. So we have to create a
1990 * constraint object. In its original state, constraint objects are
1991 * unsorted, and have to be sorted (using the AffineConstraints::close
1992 * function) before they can be used. This is what we do here, and which is
1993 * why we can't simply call the VectorTools::project function with an
1994 * anonymous temporary object <code>AffineConstraints<double>()</code> as the
1995 * second argument.
1996 *
1997
1998 *
1999 * The second point worth mentioning is that we only compute the length of
2000 * the present time step in the middle of solving the linear system
2001 * corresponding to each time step. We can therefore output the present
2002 * time of a time step only at the end of the time step.
2003 * We increment time by calling the method DiscreteTime::advance_time()
2004 * inside the loop. Since we are reporting the time and dt after we
2005 * increment it, we have to call the method
2007 * DiscreteTime::get_next_step_size(). After many steps, when the simulation
2008 * reaches the end time, the last dt is chosen by the DiscreteTime class in
2009 * such a way that the last step finishes exactly at the end time.
2010 *
2011 * @code
2012 * template <int dim>
2013 * void TwoPhaseFlowProblem<dim>::run()
2014 * {
2015 * make_grid_and_dofs();
2016 *
2017 * {
2018 * AffineConstraints<double> constraints;
2019 * constraints.close();
2020 *
2021 * VectorTools::project(dof_handler,
2022 * constraints,
2023 * QGauss<dim>(degree + 2),
2024 * InitialValues<dim>(),
2025 * old_solution);
2026 * }
2027 *
2028 * do
2029 * {
2030 * std::cout << "Timestep " << time.get_step_number() + 1 << std::endl;
2031 *
2032 * assemble_system();
2033 *
2034 * solve();
2035 *
2036 * output_results();
2037 *
2038 * time.advance_time();
2039 * std::cout << " Now at t=" << time.get_current_time()
2040 * << ", dt=" << time.get_previous_step_size() << '.'
2041 * << std::endl
2042 * << std::endl;
2043 * }
2044 * while (time.is_at_end() == false);
2045 * }
2046 * } // namespace Step21
2047 *
2048 *
2049 * @endcode
2050 *
2051 *
2052 * <a name="Thecodemaincodefunction"></a>
2053 * <h3>The <code>main</code> function</h3>
2054 *
2055
2056 *
2057 * That's it. In the main function, we pass the degree of the finite element
2058 * space to the constructor of the TwoPhaseFlowProblem object. Here, we use
2059 * zero-th degree elements, i.e. @f$RT_0\times DQ_0 \times DQ_0@f$. The rest is as
2060 * in all the other programs.
2061 *
2062 * @code
2063 * int main()
2064 * {
2065 * try
2066 * {
2067 * using namespace Step21;
2068 *
2069 * TwoPhaseFlowProblem<2> two_phase_flow_problem(0);
2070 * two_phase_flow_problem.run();
2071 * }
2072 * catch (std::exception &exc)
2073 * {
2074 * std::cerr << std::endl
2075 * << std::endl
2076 * << "----------------------------------------------------"
2077 * << std::endl;
2078 * std::cerr << "Exception on processing: " << std::endl
2079 * << exc.what() << std::endl
2080 * << "Aborting!" << std::endl
2081 * << "----------------------------------------------------"
2082 * << std::endl;
2083 *
2084 * return 1;
2085 * }
2086 * catch (...)
2087 * {
2088 * std::cerr << std::endl
2089 * << std::endl
2090 * << "----------------------------------------------------"
2091 * << std::endl;
2092 * std::cerr << "Unknown exception!" << std::endl
2093 * << "Aborting!" << std::endl
2094 * << "----------------------------------------------------"
2095 * << std::endl;
2096 * return 1;
2097 * }
2098 *
2099 * return 0;
2100 * }
2101 * @endcode
2102<a name="Results"></a><h1>Results</h1>
2103
2104
2105The code as presented here does not actually compute the results
2106found on the web page. The reason is, that even on a decent
2107computer it runs more than a day. If you want to reproduce these
2108results, modify the end time of the DiscreteTime object to `250` within the
2109constructor of TwoPhaseFlowProblem.
2110
2111If we run the program, we get the following kind of output:
2112@code
2113Number of active cells: 1024
2114Number of degrees of freedom: 4160 (2112+1024+1024)
2115
2116Timestep 1
2117 22 CG Schur complement iterations for pressure.
2118 1 CG iterations for saturation.
2119 Now at t=0.0326742, dt=0.0326742.
2120
2121Timestep 2
2122 17 CG Schur complement iterations for pressure.
2123 1 CG iterations for saturation.
2124 Now at t=0.0653816, dt=0.0327074.
2125
2126Timestep 3
2127 17 CG Schur complement iterations for pressure.
2128 1 CG iterations for saturation.
2129 Now at t=0.0980651, dt=0.0326836.
2130
2131...
2132@endcode
2133As we can see, the time step is pretty much constant right from the start,
2134which indicates that the velocities in the domain are not strongly dependent
2135on changes in saturation, although they certainly are through the factor
2136@f$\lambda(S)@f$ in the pressure equation.
2137
2138Our second observation is that the number of CG iterations needed to solve the
2139pressure Schur complement equation drops from 22 to 17 between the first and
2140the second time step (in fact, it remains around 17 for the rest of the
2141computations). The reason is actually simple: Before we solve for the pressure
2142during a time step, we don't reset the <code>solution</code> variable to
2143zero. The pressure (and the other variables) therefore have the previous time
2144step's values at the time we get into the CG solver. Since the velocities and
2145pressures don't change very much as computations progress, the previous time
2146step's pressure is actually a good initial guess for this time step's
2147pressure. Consequently, the number of iterations we need once we have computed
2148the pressure once is significantly reduced.
2149
2150The final observation concerns the number of iterations needed to solve for
2151the saturation, i.e. one. This shouldn't surprise us too much: the matrix we
2152have to solve with is the mass matrix. However, this is the mass matrix for
2153the @f$DGQ_0@f$ element of piecewise constants where no element couples with the
2154degrees of freedom on neighboring cells. The matrix is therefore a diagonal
2155one, and it is clear that we should be able to invert this matrix in a single
2156CG iteration.
2157
2158
2159With all this, here are a few movies that show how the saturation progresses
2160over time. First, this is for the single crack model, as implemented in the
2161<code>SingleCurvingCrack::KInverse</code> class:
2162
2163<img src="https://www.dealii.org/images/steps/developer/step-21.centerline.gif" alt="">
2164
2165As can be seen, the water rich fluid snakes its way mostly along the
2166high-permeability zone in the middle of the domain, whereas the rest of the
2167domain is mostly impermeable. This and the next movie are generated using
2168<code>n_refinement_steps=7</code>, leading to a @f$128\times 128@f$ mesh with some
216916,000 cells and about 66,000 unknowns in total.
2170
2171
2172The second movie shows the saturation for the random medium model of class
2173<code>RandomMedium::KInverse</code>, where we have randomly distributed
2174centers of high permeability and fluid hops from one of these zones to
2175the next:
2176
2177<img src="https://www.dealii.org/images/steps/developer/step-21.random2d.gif" alt="">
2178
2179
2180Finally, here is the same situation in three space dimensions, on a mesh with
2181<code>n_refinement_steps=5</code>, which produces a mesh of some 32,000 cells
2182and 167,000 degrees of freedom:
2183
2184<img src="https://www.dealii.org/images/steps/developer/step-21.random3d.gif" alt="">
2185
2186To repeat these computations, all you have to do is to change the line
2187@code
2188 TwoPhaseFlowProblem<2> two_phase_flow_problem(0);
2189@endcode
2190in the main function to
2191@code
2192 TwoPhaseFlowProblem<3> two_phase_flow_problem(0);
2193@endcode
2194The visualization uses a cloud technique, where the saturation is indicated by
2195colored but transparent clouds for each cell. This way, one can also see
2196somewhat what happens deep inside the domain. A different way of visualizing
2197would have been to show isosurfaces of the saturation evolving over
2198time. There are techniques to plot isosurfaces transparently, so that one can
2199see several of them at the same time like the layers of an onion.
2200
2201So why don't we show such isosurfaces? The problem lies in the way isosurfaces
2202are computed: they require that the field to be visualized is continuous, so
2203that the isosurfaces can be generated by following contours at least across a
2204single cell. However, our saturation field is piecewise constant and
2205discontinuous. If we wanted to plot an isosurface for a saturation @f$S=0.5@f$,
2206chances would be that there is no single point in the domain where that
2207saturation is actually attained. If we had to define isosurfaces in that
2208context at all, we would have to take the interfaces between cells, where one
2209of the two adjacent cells has a saturation greater than and the other cell a
2210saturation less than 0.5. However, it appears that most visualization programs
2211are not equipped to do this kind of transformation.
2212
2213
2214<a name="extensions"></a>
2215<a name="Possibilitiesforextensions"></a><h3>Possibilities for extensions</h3>
2216
2217
2218There are a number of areas where this program can be improved. Three of them
2219are listed below. All of them are, in fact, addressed in a tutorial program
2220that forms the continuation of the current one: @ref step_43 "step-43".
2221
2222
2223<a name="Solvers"></a><h4>Solvers</h4>
2224
2225
2226At present, the program is not particularly fast: the 2d random medium
2227computation took about a day for the 1,000 or so time steps. The corresponding
22283d computation took almost two days for 800 time steps. The reason why it
2229isn't faster than this is twofold. First, we rebuild the entire matrix in
2230every time step, although some parts such as the @f$B@f$, @f$B^T@f$, and @f$M^S@f$ blocks
2231never change.
2232
2233Second, we could do a lot better with the solver and
2234preconditioners. Presently, we solve the Schur complement @f$B^TM^u(S)^{-1}B@f$
2235with a CG method, using @f$[B^T (\textrm{diag}(M^u(S)))^{-1} B]^{-1}@f$ as a
2236preconditioner. Applying this preconditioner is expensive, since it involves
2237solving a linear system each time. This may have been appropriate for @ref
2238step_20 "step-20", where we have to solve the entire problem only
2239once. However, here we have to solve it hundreds of times, and in such cases
2240it is worth considering a preconditioner that is more expensive to set up the
2241first time, but cheaper to apply later on.
2242
2243One possibility would be to realize that the matrix we use as preconditioner,
2244@f$B^T (\textrm{diag}(M^u(S)))^{-1} B@f$ is still sparse, and symmetric on top of
2245that. If one looks at the flow field evolve over time, we also see that while
2246@f$S@f$ changes significantly over time, the pressure hardly does and consequently
2247@f$B^T (\textrm{diag}(M^u(S)))^{-1} B \approx B^T (\textrm{diag}(M^u(S^0)))^{-1}
2248B@f$. In other words, the matrix for the first time step should be a good
2249preconditioner also for all later time steps. With a bit of
2250back-and-forthing, it isn't hard to actually get a representation of it as a
2251SparseMatrix object. We could then hand it off to the SparseMIC class to form
2252a sparse incomplete Cholesky decomposition. To form this decomposition is
2253expensive, but we have to do it only once in the first time step, and can then
2254use it as a cheap preconditioner in the future. We could do better even by
2255using the SparseDirectUMFPACK class that produces not only an incomplete, but
2256a complete decomposition of the matrix, which should yield an even better
2257preconditioner.
2258
2259Finally, why use the approximation @f$B^T (\textrm{diag}(M^u(S)))^{-1} B@f$ to
2260precondition @f$B^T M^u(S)^{-1} B@f$? The latter matrix, after all, is the mixed
2261form of the Laplace operator on the pressure space, for which we use linear
2262elements. We could therefore build a separate matrix @f$A^p@f$ on the side that
2263directly corresponds to the non-mixed formulation of the Laplacian, for
2264example using the bilinear form @f$(\mathbf{K}\lambda(S^n) \nabla
2265\varphi_i,\nabla\varphi_j)@f$. We could then form an incomplete or complete
2266decomposition of this non-mixed matrix and use it as a preconditioner of the
2267mixed form.
2268
2269Using such techniques, it can reasonably be expected that the solution process
2270will be faster by at least an order of magnitude.
2271
2272
2273<a name="Timestepping"></a><h4>Time stepping</h4>
2274
2275
2276In the introduction we have identified the time step restriction
2277@f[
2278 \triangle t_{n+1} \le \frac h{|\mathbf{u}^{n+1}(\mathbf{x})|}
2279@f]
2280that has to hold globally, i.e. for all @f$\mathbf x@f$. After discretization, we
2281satisfy it by choosing
2282@f[
2283 \triangle t_{n+1} = \frac {\min_K h_K}{\max_{\mathbf{x}}|\mathbf{u}^{n+1}(\mathbf{x})|}.
2284@f]
2285
2286This restriction on the time step is somewhat annoying: the finer we make the
2287mesh the smaller the time step; in other words, we get punished twice: each
2288time step is more expensive to solve and we have to do more time steps.
2289
2290This is particularly annoying since the majority of the additional work is
2291spent solving the implicit part of the equations, i.e. the pressure-velocity
2292system, whereas it is the hyperbolic transport equation for the saturation
2293that imposes the time step restriction.
2294
2295To avoid this bottleneck, people have invented a number of approaches. For
2296example, they may only re-compute the pressure-velocity field every few time
2297steps (or, if you want, use different time step sizes for the
2298pressure/velocity and saturation equations). This keeps the time step
2299restriction on the cheap explicit part while it makes the solution of the
2300implicit part less frequent. Experiments in this direction are
2301certainly worthwhile; one starting point for such an approach is the paper by
2302Zhangxin Chen, Guanren Huan and Baoyan Li: <i>An improved IMPES method for
2303two-phase flow in porous media</i>, Transport in Porous Media, 54 (2004),
2304pp. 361&mdash;376. There are certainly many other papers on this topic as well, but
2305this one happened to land on our desk a while back.
2306
2307
2308
2309<a name="Adaptivity"></a><h4>Adaptivity</h4>
2310
2311
2312Adaptivity would also clearly help. Looking at the movies, one clearly sees
2313that most of the action is confined to a relatively small part of the domain
2314(this particularly obvious for the saturation, but also holds for the
2315velocities and pressures). Adaptivity can therefore be expected to keep the
2316necessary number of degrees of freedom low, or alternatively increase the
2317accuracy.
2318
2319On the other hand, adaptivity for time dependent problems is not a trivial
2320thing: we would have to change the mesh every few time steps, and we would
2321have to transport our present solution to the next mesh every time we change
2322it (something that the SolutionTransfer class can help with). These are not
2323insurmountable obstacles, but they do require some additional coding and more
2324than we felt comfortable was worth packing into this tutorial program.
2325 *
2326 *
2327<a name="PlainProg"></a>
2328<h1> The plain program</h1>
2329@include "step-21.cc"
2330*/
double get_previous_step_size() const
void advance_time()
Definition: fe_dgq.h:111
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
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const override
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &return_value) const override
Definition: point.h:111
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< value_type > &values) const
Definition: vector.h:110
@ update_values
Shape function values.
@ update_normal_vectors
Normal vectors.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
Point< 2 > second
Definition: grid_out.cc:4588
Point< 2 > first
Definition: grid_out.cc:4587
__global__ void set(Number *val, const Number s, const size_type N)
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void loop(ITERATOR begin, typename identity< ITERATOR >::type 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, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:439
void make_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternType &sparsity_pattern, const AffineConstraints< number > &constraints=AffineConstraints< number >(), const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
const Event initial
Definition: event.cc:65
Expression fabs(const Expression &x)
void component_wise(DoFHandler< dim, spacedim > &dof_handler, const std::vector< unsigned int > &target_component=std::vector< unsigned int >())
void random(DoFHandler< dim, spacedim > &dof_handler)
std::vector< types::global_dof_index > count_dofs_per_fe_component(const DoFHandler< dim, spacedim > &dof_handler, const bool vector_valued_once=false, const std::vector< unsigned int > &target_component={})
Definition: dof_tools.cc:1943
void interpolate(const DoFHandler< dim, spacedim > &dof1, const InVector &u1, const DoFHandler< dim, spacedim > &dof2, OutVector &u2)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
static const types::blas_int zero
@ matrix
Contents is actually a matrix.
static const char T
static const types::blas_int one
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
VectorType::value_type * end(VectorType &V)
void project(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const AffineConstraints< typename VectorType::value_type > &constraints, const Quadrature< dim > &quadrature, const Function< spacedim, typename VectorType::value_type > &function, VectorType &vec, const bool enforce_zero_boundary=false, const Quadrature< dim - 1 > &q_boundary=(dim > 1 ? QGauss< dim - 1 >(2) :Quadrature< dim - 1 >(0)), const bool project_to_boundary_first=false)
void run(const Iterator &begin, const typename identity< Iterator >::type &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
Definition: work_stream.h:472
int(&) functions(const void *v1, const void *v2)
::VectorizedArray< Number, width > exp(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