745 *
class SaturationBoundaryValues :
public Function<dim>
748 * SaturationBoundaryValues()
753 *
const unsigned int component = 0)
const override;
760 * SaturationBoundaryValues<dim>::value(
const Point<dim> &p,
761 *
const unsigned int )
const
771 *
class SaturationInitialValues :
public Function<dim>
774 * SaturationInitialValues()
779 *
const unsigned int component = 0)
const override;
788 * SaturationInitialValues<dim>::value(
const Point<dim> & ,
789 *
const unsigned int )
const
796 *
void SaturationInitialValues<dim>::vector_value(
const Point<dim> &p,
799 *
for (
unsigned int c = 0; c < this->n_components; ++c)
800 *
values(c) = SaturationInitialValues<dim>::value(p, c);
807 * <a name=
"step_43-Permeabilitymodels"></a>
808 * <h3>Permeability models</h3>
812 * In
this tutorial, we still use the two permeability models previously
813 * used in @ref step_21
"step-21" so we again refrain from commenting in detail about them.
816 *
namespace SingleCurvingCrack
833 *
void KInverse<dim>::value_list(
const std::vector<
Point<dim>> &points,
838 *
for (
unsigned int p = 0; p < points.size(); ++p)
842 *
const double distance_to_flowline =
843 * std::fabs(points[p][1] - 0.5 - 0.1 *
std::sin(10 * points[p][0]));
845 *
const double permeability =
850 *
for (
unsigned int d = 0;
d < dim; ++
d)
851 * values[p][d][d] = 1. / permeability;
857 *
namespace RandomMedium
872 *
static std::vector<Point<dim>> centers;
878 * std::vector<Point<dim>> KInverse<dim>::centers = []() {
879 *
const unsigned int N =
880 * (dim == 2 ? 40 : (dim == 3 ? 100 : throw ExcNotImplemented()));
882 * std::vector<Point<dim>> centers_list(N);
883 *
for (
unsigned int i = 0; i < N; ++i)
884 *
for (
unsigned int d = 0;
d < dim; ++
d)
885 * centers_list[i][d] =
static_cast<double>(rand()) / RAND_MAX;
887 *
return centers_list;
893 *
void KInverse<dim>::value_list(
const std::vector<
Point<dim>> &points,
898 *
for (
unsigned int p = 0; p < points.size(); ++p)
902 *
double permeability = 0;
903 *
for (
unsigned int i = 0; i < centers.size(); ++i)
905 *
std::exp(-(points[p] - centers[i]).norm_square() / (0.05 * 0.05));
907 *
const double normalized_permeability =
910 *
for (
unsigned int d = 0;
d < dim; ++
d)
911 * values[p][d][d] = 1. / normalized_permeability;
920 * <a name=
"step_43-Physicalquantities"></a>
921 * <h3>Physical quantities</h3>
925 * The implementations of all the physical quantities such as total mobility
926 * @f$\lambda_t@f$ and fractional flow of water @f$F@f$ are taken from @ref step_21
"step-21" so
927 * again we don
't have do any comment about them. Compared to @ref step_21 "step-21" we
928 * have added checks that the saturation passed to these functions is in
929 * fact within the physically valid range. Furthermore, given that the
930 * wetting phase moves at speed @f$\mathbf u F'(S)@f$ it is clear that @f$F
'(S)@f$
931 * must be greater or equal to zero, so we assert that as well to make sure
932 * that our calculations to get at the formula for the derivative made
936 * double mobility_inverse(const double S, const double viscosity)
938 * return 1.0 / (1.0 / viscosity * S * S + (1 - S) * (1 - S));
942 * double fractional_flow(const double S, const double viscosity)
944 * Assert((S >= 0) && (S <= 1),
945 * ExcMessage("Saturation is outside its physically valid range."));
947 * return S * S / (S * S + viscosity * (1 - S) * (1 - S));
951 * double fractional_flow_derivative(const double S, const double viscosity)
953 * Assert((S >= 0) && (S <= 1),
954 * ExcMessage("Saturation is outside its physically valid range."));
956 * const double temp = (S * S + viscosity * (1 - S) * (1 - S));
958 * const double numerator =
959 * 2.0 * S * temp - S * S * (2.0 * S - 2.0 * viscosity * (1 - S));
960 * const double denominator = Utilities::fixed_power<2>(temp);
962 * const double F_prime = numerator / denominator;
964 * Assert(F_prime >= 0, ExcInternalError());
973 * <a name="step_43-Helperclassesforsolversandpreconditioners"></a>
974 * <h3>Helper classes for solvers and preconditioners</h3>
978 * In this first part we define a number of classes that we need in the
979 * construction of linear solvers and preconditioners. This part is
980 * essentially the same as that used in @ref step_31 "step-31". The only difference is that
981 * the original variable name stokes_matrix is replaced by another name
982 * darcy_matrix to match our problem.
985 * namespace LinearSolvers
987 * template <class MatrixType, class PreconditionerType>
988 * class InverseMatrix : public Subscriptor
991 * InverseMatrix(const MatrixType &m,
992 * const PreconditionerType &preconditioner);
995 * template <typename VectorType>
996 * void vmult(VectorType &dst, const VectorType &src) const;
999 * const SmartPointer<const MatrixType> matrix;
1000 * const PreconditionerType &preconditioner;
1004 * template <class MatrixType, class PreconditionerType>
1005 * InverseMatrix<MatrixType, PreconditionerType>::InverseMatrix(
1006 * const MatrixType &m,
1007 * const PreconditionerType &preconditioner)
1009 * , preconditioner(preconditioner)
1014 * template <class MatrixType, class PreconditionerType>
1015 * template <typename VectorType>
1016 * void InverseMatrix<MatrixType, PreconditionerType>::vmult(
1018 * const VectorType &src) const
1020 * SolverControl solver_control(src.size(), 1e-7 * src.l2_norm());
1021 * SolverCG<VectorType> cg(solver_control);
1027 * cg.solve(*matrix, dst, src, preconditioner);
1029 * catch (std::exception &e)
1031 * Assert(false, ExcMessage(e.what()));
1035 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1036 * class BlockSchurPreconditioner : public Subscriptor
1039 * BlockSchurPreconditioner(
1040 * const TrilinosWrappers::BlockSparseMatrix &S,
1041 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1042 * PreconditionerTypeMp> &Mpinv,
1043 * const PreconditionerTypeA &Apreconditioner);
1045 * void vmult(TrilinosWrappers::MPI::BlockVector &dst,
1046 * const TrilinosWrappers::MPI::BlockVector &src) const;
1049 * const SmartPointer<const TrilinosWrappers::BlockSparseMatrix>
1051 * const SmartPointer<const InverseMatrix<TrilinosWrappers::SparseMatrix,
1052 * PreconditionerTypeMp>>
1054 * const PreconditionerTypeA &a_preconditioner;
1056 * mutable TrilinosWrappers::MPI::Vector tmp;
1061 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1062 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::
1063 * BlockSchurPreconditioner(
1064 * const TrilinosWrappers::BlockSparseMatrix &S,
1065 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1066 * PreconditionerTypeMp> &Mpinv,
1067 * const PreconditionerTypeA &Apreconditioner)
1068 * : darcy_matrix(&S)
1069 * , m_inverse(&Mpinv)
1070 * , a_preconditioner(Apreconditioner)
1071 * , tmp(complete_index_set(darcy_matrix->block(1, 1).m()))
1075 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1077 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::vmult(
1078 * TrilinosWrappers::MPI::BlockVector &dst,
1079 * const TrilinosWrappers::MPI::BlockVector &src) const
1081 * a_preconditioner.vmult(dst.block(0), src.block(0));
1082 * darcy_matrix->block(1, 0).residual(tmp, dst.block(0), src.block(1));
1084 * m_inverse->vmult(dst.block(1), tmp);
1086 * } // namespace LinearSolvers
1092 * <a name="step_43-TheTwoPhaseFlowProblemclass"></a>
1093 * <h3>The TwoPhaseFlowProblem class</h3>
1097 * The definition of the class that defines the top-level logic of solving
1098 * the time-dependent advection-dominated two-phase flow problem (or
1099 * Buckley-Leverett problem @cite Buckley1942) is mainly based on tutorial
1100 * programs @ref step_21 "step-21" and @ref step_33 "step-33", and in particular on @ref step_31 "step-31" where we have
1101 * used basically the same general structure as done here. As in @ref step_31 "step-31",
1102 * the key routines to look for in the implementation below are the
1103 * <code>run()</code> and <code>solve()</code> functions.
1107 * The main difference to @ref step_31 "step-31" is that, since adaptive operator splitting
1108 * is considered, we need a couple more member variables to hold the last
1109 * two computed Darcy (velocity/pressure) solutions in addition to the
1110 * current one (which is either computed directly, or extrapolated from the
1111 * previous two), and we need to remember the last two times we computed the
1112 * Darcy solution. We also need a helper function that figures out whether
1113 * we do indeed need to recompute the Darcy solution.
1117 * Unlike @ref step_31 "step-31", this step uses one more AffineConstraints object called
1118 * darcy_preconditioner_constraints. This constraint object is used only for
1119 * assembling the matrix for the Darcy preconditioner and includes hanging
1120 * node constraints as well as Dirichlet boundary value constraints for the
1121 * pressure variable. We need this because we are building a Laplace matrix
1122 * for the pressure as an approximation of the Schur complement) which is
1123 * only positive definite if boundary conditions are applied.
1127 * The collection of member functions and variables thus declared in this
1128 * class is then rather similar to those in @ref step_31 "step-31":
1131 * template <int dim>
1132 * class TwoPhaseFlowProblem
1135 * TwoPhaseFlowProblem(const unsigned int degree);
1139 * void setup_dofs();
1140 * void assemble_darcy_preconditioner();
1141 * void build_darcy_preconditioner();
1142 * void assemble_darcy_system();
1143 * void assemble_saturation_system();
1144 * void assemble_saturation_matrix();
1145 * void assemble_saturation_rhs();
1146 * void assemble_saturation_rhs_cell_term(
1147 * const FEValues<dim> &saturation_fe_values,
1148 * const FEValues<dim> &darcy_fe_values,
1149 * const double global_max_u_F_prime,
1150 * const double global_S_variation,
1151 * const std::vector<types::global_dof_index> &local_dof_indices);
1152 * void assemble_saturation_rhs_boundary_term(
1153 * const FEFaceValues<dim> &saturation_fe_face_values,
1154 * const FEFaceValues<dim> &darcy_fe_face_values,
1155 * const std::vector<types::global_dof_index> &local_dof_indices);
1157 * void refine_mesh(const unsigned int min_grid_level,
1158 * const unsigned int max_grid_level);
1159 * void output_results() const;
1163 * We follow with a number of helper functions that are used in a variety
1164 * of places throughout the program:
1167 * double get_max_u_F_prime() const;
1168 * std::pair<double, double> get_extrapolated_saturation_range() const;
1169 * bool determine_whether_to_solve_for_pressure_and_velocity() const;
1170 * void project_back_saturation();
1171 * double compute_viscosity(
1172 * const std::vector<double> &old_saturation,
1173 * const std::vector<double> &old_old_saturation,
1174 * const std::vector<Tensor<1, dim>> &old_saturation_grads,
1175 * const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
1176 * const std::vector<Vector<double>> &present_darcy_values,
1177 * const double global_max_u_F_prime,
1178 * const double global_S_variation,
1179 * const double cell_diameter) const;
1184 * This all is followed by the member variables, most of which are similar
1185 * to the ones in @ref step_31 "step-31", with the exception of the ones that pertain to
1186 * the macro time stepping for the velocity/pressure system:
1189 * Triangulation<dim> triangulation;
1190 * double global_Omega_diameter;
1192 * const unsigned int degree;
1194 * const unsigned int darcy_degree;
1195 * const FESystem<dim> darcy_fe;
1196 * DoFHandler<dim> darcy_dof_handler;
1197 * AffineConstraints<double> darcy_constraints;
1199 * AffineConstraints<double> darcy_preconditioner_constraints;
1201 * TrilinosWrappers::BlockSparseMatrix darcy_matrix;
1202 * TrilinosWrappers::BlockSparseMatrix darcy_preconditioner_matrix;
1204 * TrilinosWrappers::MPI::BlockVector darcy_solution;
1205 * TrilinosWrappers::MPI::BlockVector darcy_rhs;
1207 * TrilinosWrappers::MPI::BlockVector last_computed_darcy_solution;
1208 * TrilinosWrappers::MPI::BlockVector second_last_computed_darcy_solution;
1211 * const unsigned int saturation_degree;
1212 * const FE_Q<dim> saturation_fe;
1213 * DoFHandler<dim> saturation_dof_handler;
1214 * AffineConstraints<double> saturation_constraints;
1216 * TrilinosWrappers::SparseMatrix saturation_matrix;
1219 * TrilinosWrappers::MPI::Vector saturation_solution;
1220 * TrilinosWrappers::MPI::Vector old_saturation_solution;
1221 * TrilinosWrappers::MPI::Vector old_old_saturation_solution;
1222 * TrilinosWrappers::MPI::Vector saturation_rhs;
1224 * TrilinosWrappers::MPI::Vector
1225 * saturation_matching_last_computed_darcy_solution;
1227 * const double saturation_refinement_threshold;
1230 * const double end_time;
1232 * double current_macro_time_step;
1233 * double old_macro_time_step;
1236 * double old_time_step;
1237 * unsigned int timestep_number;
1239 * const double viscosity;
1240 * const double porosity;
1241 * const double AOS_threshold;
1243 * std::shared_ptr<TrilinosWrappers::PreconditionIC> top_left_preconditioner;
1244 * std::shared_ptr<TrilinosWrappers::PreconditionIC>
1245 * bottom_right_preconditioner;
1247 * bool rebuild_saturation_matrix;
1251 * At the very end we declare a variable that denotes the material
1252 * model. Compared to @ref step_21 "step-21", we do this here as a member variable since
1253 * we will want to use it in a variety of places and so having a central
1254 * place where such a variable is declared will make it simpler to replace
1255 * one class by another (e.g. replace RandomMedium::KInverse by
1256 * SingleCurvingCrack::KInverse).
1259 * const RandomMedium::KInverse<dim> k_inverse;
1266 * <a name="step_43-TwoPhaseFlowProblemdimTwoPhaseFlowProblem"></a>
1267 * <h3>TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem</h3>
1271 * The constructor of this class is an extension of the constructors in
1272 * @ref step_21 "step-21" and @ref step_31 "step-31". We need to add the various variables that concern
1273 * the saturation. As discussed in the introduction, we are going to use
1274 * @f$Q_2 \times Q_1@f$ (Taylor-Hood) elements again for the Darcy system, an
1275 * element combination that fulfills the Ladyzhenskaya-Babuska-Brezzi (LBB)
1276 * conditions [Brezzi and Fortin 1991, Chen 2005], and @f$Q_1@f$ elements for
1277 * the saturation. However, by using variables that store the polynomial
1278 * degree of the Darcy and temperature finite elements, it is easy to
1279 * consistently modify the degree of the elements as well as all quadrature
1280 * formulas used on them downstream. Moreover, we initialize the time
1281 * stepping variables related to operator splitting as well as the option
1282 * for matrix assembly and preconditioning:
1285 * template <int dim>
1286 * TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem(const unsigned int degree)
1287 * : triangulation(Triangulation<dim>::maximum_smoothing)
1288 * , global_Omega_diameter(std::numeric_limits<double>::quiet_NaN())
1290 * , darcy_degree(degree)
1291 * , darcy_fe(FE_Q<dim>(darcy_degree + 1) ^ dim, FE_Q<dim>(darcy_degree))
1292 * , darcy_dof_handler(triangulation)
1295 * saturation_degree(degree + 1)
1296 * , saturation_fe(saturation_degree)
1297 * , saturation_dof_handler(triangulation)
1300 * saturation_refinement_threshold(0.5)
1307 * current_macro_time_step(0)
1308 * , old_macro_time_step(0)
1312 * , old_time_step(0)
1313 * , timestep_number(0)
1316 * , AOS_threshold(3.0)
1319 * rebuild_saturation_matrix(true)
1326 * <a name="step_43-TwoPhaseFlowProblemdimsetup_dofs"></a>
1327 * <h3>TwoPhaseFlowProblem<dim>::setup_dofs</h3>
1331 * This is the function that sets up the DoFHandler objects we have here
1332 * (one for the Darcy part and one for the saturation part) as well as set
1333 * to the right sizes the various objects required for the linear algebra in
1334 * this program. Its basic operations are similar to what @ref step_31 "step-31" did.
1338 * The body of the function first enumerates all degrees of freedom for the
1339 * Darcy and saturation systems. For the Darcy part, degrees of freedom are
1340 * then sorted to ensure that velocities precede pressure DoFs so that we
1341 * can partition the Darcy matrix into a @f$2 \times 2@f$ matrix.
1345 * Then, we need to incorporate hanging node constraints and Dirichlet
1346 * boundary value constraints into darcy_preconditioner_constraints. The
1347 * boundary condition constraints are only set on the pressure component
1348 * since the Schur complement preconditioner that corresponds to the porous
1349 * media flow operator in non-mixed form, @f$-\nabla \cdot [\mathbf K
1350 * \lambda_t(S)]\nabla@f$, acts only on the pressure variable. Therefore, we
1351 * use a component_mask that filters out the velocity component, so that the
1352 * condensation is performed on pressure degrees of freedom only.
1356 * After having done so, we count the number of degrees of freedom in the
1357 * various blocks. This information is then used to create the sparsity
1358 * pattern for the Darcy and saturation system matrices as well as the
1359 * preconditioner matrix from which we build the Darcy preconditioner. As in
1360 * @ref step_31 "step-31", we choose to create the pattern using the blocked version of
1361 * DynamicSparsityPattern. So, for this, we follow the same way as @ref step_31 "step-31"
1362 * did and we don't have to repeat descriptions again
for the rest of the
1366 * template <int dim>
1367 *
void TwoPhaseFlowProblem<dim>::setup_dofs()
1369 * std::vector<unsigned int> darcy_block_component(dim + 1, 0);
1370 * darcy_block_component[dim] = 1;
1372 * darcy_dof_handler.distribute_dofs(darcy_fe);
1376 * darcy_constraints.clear();
1378 * darcy_constraints);
1379 * darcy_constraints.close();
1382 * saturation_dof_handler.distribute_dofs(saturation_fe);
1384 * saturation_constraints.clear();
1386 * saturation_constraints);
1387 * saturation_constraints.close();
1390 * darcy_preconditioner_constraints.clear();
1395 * darcy_preconditioner_constraints);
1397 * darcy_preconditioner_constraints,
1398 * darcy_fe.component_mask(
1401 * darcy_preconditioner_constraints.close();
1405 *
const std::vector<types::global_dof_index> darcy_dofs_per_block =
1407 * darcy_block_component);
1409 * n_p = darcy_dofs_per_block[1],
1410 * n_s = saturation_dof_handler.n_dofs();
1412 * std::cout <<
"Number of active cells: " <<
triangulation.n_active_cells()
1413 * <<
" (on " <<
triangulation.n_levels() <<
" levels)" << std::endl
1414 * <<
"Number of degrees of freedom: " << n_u + n_p + n_s <<
" ("
1415 * << n_u <<
'+' << n_p <<
'+' << n_s <<
')' << std::endl
1419 * darcy_matrix.clear();
1422 * darcy_dofs_per_block);
1425 *
for (
unsigned int c = 0; c < dim + 1; ++c)
1426 *
for (
unsigned int d = 0;
d < dim + 1; ++
d)
1427 *
if (!((c == dim) && (d == dim)))
1434 * darcy_dof_handler, coupling, dsp, darcy_constraints,
false);
1436 * darcy_matrix.reinit(dsp);
1440 * top_left_preconditioner.reset();
1441 * bottom_right_preconditioner.reset();
1442 * darcy_preconditioner_matrix.clear();
1445 * darcy_dofs_per_block);
1448 *
for (
unsigned int c = 0; c < dim + 1; ++c)
1449 *
for (
unsigned int d = 0;
d < dim + 1; ++
d)
1456 * darcy_dof_handler, coupling, dsp, darcy_constraints,
false);
1458 * darcy_preconditioner_matrix.reinit(dsp);
1463 * saturation_matrix.clear();
1469 * saturation_constraints,
1473 * saturation_matrix.reinit(dsp);
1479 * darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1481 * last_computed_darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1483 * second_last_computed_darcy_solution.reinit(darcy_partitioning,
1486 * darcy_rhs.reinit(darcy_partitioning, MPI_COMM_WORLD);
1489 * saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1490 * old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1491 * old_old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1493 * saturation_matching_last_computed_darcy_solution.reinit(
1494 * saturation_partitioning, MPI_COMM_WORLD);
1496 * saturation_rhs.reinit(saturation_partitioning, MPI_COMM_WORLD);
1503 * <a name=
"step_43-Assemblingmatricesandpreconditioners"></a>
1504 * <h3>Assembling matrices and preconditioners</h3>
1508 * The next few
functions are devoted to setting up the various system and
1509 * preconditioner matrices and right hand sides that we have to deal with in
1515 * <a name=
"step_43-TwoPhaseFlowProblemdimassemble_darcy_preconditioner"></a>
1516 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner</h4>
1520 * This function assembles the
matrix we use
for preconditioning the Darcy
1521 * system. What we need are a vector @ref GlossMassMatrix
"mass matrix" weighted by
1522 * @f$\left(\mathbf{
K} \lambda_t\right)^{-1}@f$ on the velocity components and a
1523 * mass
matrix weighted by @f$\left(\mathbf{
K} \lambda_t\right)@f$ on the
1524 * pressure component. We start by generating a quadrature
object of
1525 * appropriate order, the
FEValues object that can give values and gradients
1526 * at the quadrature points (together with quadrature weights). Next we
1527 * create data structures
for the cell
matrix and the relation between local
1528 * and global DoFs. The vectors phi_u and grad_phi_p are going to hold the
1530 * matrices, as was already done in @ref step_22
"step-22". Before we start the
loop over
1531 * all active cells, we have to specify which components are pressure and
1532 * which are velocity.
1536 * The creation of the local
matrix is rather simple. There are only a term
1537 * weighted by @f$\left(\mathbf{
K} \lambda_t\right)^{-1}@f$ (on the velocity)
1538 * and a Laplace
matrix weighted by @f$\left(\mathbf{
K} \lambda_t\right)@f$ to
1539 * be generated, so the creation of the local matrix is done in essentially
1540 * two lines. Since the material model functions at the top of
this file
1541 * only provide the inverses of the permeability and mobility, we have to
1542 * compute @f$\mathbf K@f$ and @f$\lambda_t@f$ by hand from the given values, once
1543 * per quadrature
point.
1547 * Once the local matrix is ready (loop over rows and columns in the local
1548 * matrix on each quadrature point), we get the local DoF indices and write
1549 * the local information into the global
matrix. We
do this by directly
1550 * applying the constraints (i.e. darcy_preconditioner_constraints) that
1551 * takes care of hanging node and zero Dirichlet boundary condition
1552 * constraints. By doing so, we don
't have to do that afterwards, and we
1555 * modify
matrix and vector entries and so are difficult to write
for the
1556 * Trilinos classes where we don
't immediately have access to individual
1560 * template <int dim>
1561 * void TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner()
1563 * std::cout << " Rebuilding darcy preconditioner..." << std::endl;
1565 * darcy_preconditioner_matrix = 0;
1567 * const QGauss<dim> quadrature_formula(darcy_degree + 2);
1568 * FEValues<dim> darcy_fe_values(darcy_fe,
1569 * quadrature_formula,
1570 * update_JxW_values | update_values |
1571 * update_gradients |
1572 * update_quadrature_points);
1573 * FEValues<dim> saturation_fe_values(saturation_fe,
1574 * quadrature_formula,
1577 * const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1578 * const unsigned int n_q_points = quadrature_formula.size();
1580 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1582 * std::vector<double> old_saturation_values(n_q_points);
1584 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1585 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1587 * std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1588 * std::vector<Tensor<1, dim>> grad_phi_p(dofs_per_cell);
1590 * const FEValuesExtractors::Vector velocities(0);
1591 * const FEValuesExtractors::Scalar pressure(dim);
1593 * auto cell = darcy_dof_handler.begin_active();
1594 * const auto endc = darcy_dof_handler.end();
1595 * auto saturation_cell = saturation_dof_handler.begin_active();
1597 * for (; cell != endc; ++cell, ++saturation_cell)
1599 * darcy_fe_values.reinit(cell);
1600 * saturation_fe_values.reinit(saturation_cell);
1604 * saturation_fe_values.get_function_values(old_saturation_solution,
1605 * old_saturation_values);
1607 * k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1608 * k_inverse_values);
1610 * for (unsigned int q = 0; q < n_q_points; ++q)
1612 * const double old_s = old_saturation_values[q];
1614 * const double inverse_mobility = mobility_inverse(old_s, viscosity);
1615 * const double mobility = 1.0 / inverse_mobility;
1616 * const Tensor<2, dim> permeability = invert(k_inverse_values[q]);
1618 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
1620 * phi_u[k] = darcy_fe_values[velocities].value(k, q);
1621 * grad_phi_p[k] = darcy_fe_values[pressure].gradient(k, q);
1624 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1625 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1627 * local_matrix(i, j) +=
1628 * (k_inverse_values[q] * inverse_mobility * phi_u[i] *
1630 * permeability * mobility * grad_phi_p[i] * grad_phi_p[j]) *
1631 * darcy_fe_values.JxW(q);
1635 * cell->get_dof_indices(local_dof_indices);
1636 * darcy_preconditioner_constraints.distribute_local_to_global(
1637 * local_matrix, local_dof_indices, darcy_preconditioner_matrix);
1645 * <a name="step_43-TwoPhaseFlowProblemdimbuild_darcy_preconditioner"></a>
1646 * <h4>TwoPhaseFlowProblem<dim>::build_darcy_preconditioner</h4>
1650 * After calling the above functions to assemble the preconditioner matrix,
1651 * this function generates the inner preconditioners that are going to be
1652 * used for the Schur complement block preconditioner. The preconditioners
1653 * need to be regenerated at every saturation time step since they depend on
1654 * the saturation @f$S@f$ that varies with time.
1658 * In here, we set up the preconditioner for the velocity-velocity matrix
1659 * @f$\mathbf{M}^{\mathbf{u}}@f$ and the Schur complement @f$\mathbf{S}@f$. As
1660 * explained in the introduction, we are going to use an IC preconditioner
1661 * based on the vector matrix @f$\mathbf{M}^{\mathbf{u}}@f$ and another based on
1662 * the scalar Laplace matrix @f$\tilde{\mathbf{S}}^p@f$ (which is spectrally
1663 * close to the Schur complement of the Darcy matrix). Usually, the
1664 * TrilinosWrappers::PreconditionIC class can be seen as a good black-box
1665 * preconditioner which does not need any special knowledge of the matrix
1666 * structure and/or the operator that's behind it.
1669 *
template <
int dim>
1670 *
void TwoPhaseFlowProblem<dim>::build_darcy_preconditioner()
1672 * assemble_darcy_preconditioner();
1674 * top_left_preconditioner =
1675 * std::make_shared<TrilinosWrappers::PreconditionIC>();
1676 * top_left_preconditioner->initialize(
1677 * darcy_preconditioner_matrix.block(0, 0));
1679 * bottom_right_preconditioner =
1680 * std::make_shared<TrilinosWrappers::PreconditionIC>();
1681 * bottom_right_preconditioner->initialize(
1682 * darcy_preconditioner_matrix.block(1, 1));
1689 * <a name=
"step_43-TwoPhaseFlowProblemdimassemble_darcy_system"></a>
1690 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_system</h4>
1694 * This is the function that assembles the linear system
for the Darcy
1699 * Regarding the technical details of implementation, the procedures are
1700 * similar to those in @ref step_22
"step-22" and @ref step_31
"step-31". We reset
matrix and vector,
1701 * create a quadrature formula on the cells, and then create the respective
1706 * There is one thing that needs to be commented: since we have a separate
1707 * finite element and
DoFHandler for the saturation, we need to generate a
1708 *
second FEValues object for the proper evaluation of the saturation
1709 * solution. This isn
't too complicated to realize here: just use the
1710 * saturation structures and set an update flag for the basis function
1711 * values which we need for evaluation of the saturation solution. The only
1712 * important part to remember here is that the same quadrature formula is
1713 * used for both FEValues objects to ensure that we get matching information
1714 * when we loop over the quadrature points of the two objects.
1718 * The declarations proceed with some shortcuts for array sizes, the
1719 * creation of the local matrix, right hand side as well as the vector for
1720 * the indices of the local dofs compared to the global system.
1723 * template <int dim>
1724 * void TwoPhaseFlowProblem<dim>::assemble_darcy_system()
1729 * const QGauss<dim> quadrature_formula(darcy_degree + 2);
1730 * const QGauss<dim - 1> face_quadrature_formula(darcy_degree + 2);
1732 * FEValues<dim> darcy_fe_values(darcy_fe,
1733 * quadrature_formula,
1734 * update_values | update_gradients |
1735 * update_quadrature_points |
1736 * update_JxW_values);
1738 * FEValues<dim> saturation_fe_values(saturation_fe,
1739 * quadrature_formula,
1742 * FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
1743 * face_quadrature_formula,
1745 * update_normal_vectors |
1746 * update_quadrature_points |
1747 * update_JxW_values);
1749 * const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1751 * const unsigned int n_q_points = quadrature_formula.size();
1752 * const unsigned int n_face_q_points = face_quadrature_formula.size();
1754 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1755 * Vector<double> local_rhs(dofs_per_cell);
1757 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1759 * const Functions::ZeroFunction<dim> pressure_right_hand_side;
1760 * const PressureBoundaryValues<dim> pressure_boundary_values;
1762 * std::vector<double> pressure_rhs_values(n_q_points);
1763 * std::vector<double> boundary_values(n_face_q_points);
1764 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1768 * Next we need a vector that will contain the values of the saturation
1769 * solution at the previous time level at the quadrature points to
1770 * assemble the saturation dependent coefficients in the Darcy equations.
1774 * The set of vectors we create next hold the evaluations of the basis
1775 * functions as well as their gradients that will be used for creating the
1776 * matrices. Putting these into their own arrays rather than asking the
1777 * FEValues object for this information each time it is needed is an
1778 * optimization to accelerate the assembly process, see @ref step_22 "step-22" for
1783 * The last two declarations are used to extract the individual blocks
1784 * (velocity, pressure, saturation) from the total FE system.
1787 * std::vector<double> old_saturation_values(n_q_points);
1789 * std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1790 * std::vector<double> div_phi_u(dofs_per_cell);
1791 * std::vector<double> phi_p(dofs_per_cell);
1793 * const FEValuesExtractors::Vector velocities(0);
1794 * const FEValuesExtractors::Scalar pressure(dim);
1798 * Now start the loop over all cells in the problem. We are working on two
1799 * different DoFHandlers for this assembly routine, so we must have two
1800 * different cell iterators for the two objects in use. This might seem a
1801 * bit peculiar, but since both the Darcy system and the saturation system
1802 * use the same grid we can assume that the two iterators run in sync over
1803 * the cells of the two DoFHandler objects.
1807 * The first statements within the loop are again all very familiar, doing
1808 * the update of the finite element data as specified by the update flags,
1809 * zeroing out the local arrays and getting the values of the old solution
1810 * at the quadrature points. At this point we also have to get the values
1811 * of the saturation function of the previous time step at the quadrature
1812 * points. To this end, we can use the FEValues::get_function_values
1813 * (previously already used in @ref step_9 "step-9", @ref step_14 "step-14" and @ref step_15 "step-15"), a function
1814 * that takes a solution vector and returns a list of function values at
1815 * the quadrature points of the present cell. In fact, it returns the
1816 * complete vector-valued solution at each quadrature point, i.e. not only
1817 * the saturation but also the velocities and pressure.
1821 * Then we are ready to loop over the quadrature points on the cell to do
1822 * the integration. The formula for this follows in a straightforward way
1823 * from what has been discussed in the introduction.
1827 * Once this is done, we start the loop over the rows and columns of the
1828 * local matrix and feed the matrix with the relevant products.
1832 * The last step in the loop over all cells is to enter the local
1833 * contributions into the global matrix and vector structures to the
1834 * positions specified in local_dof_indices. Again, we let the
1835 * AffineConstraints class do the insertion of the cell matrix
1836 * elements to the global matrix, which already condenses the hanging node
1840 * auto cell = darcy_dof_handler.begin_active();
1841 * const auto endc = darcy_dof_handler.end();
1842 * auto saturation_cell = saturation_dof_handler.begin_active();
1844 * for (; cell != endc; ++cell, ++saturation_cell)
1846 * darcy_fe_values.reinit(cell);
1847 * saturation_fe_values.reinit(saturation_cell);
1852 * saturation_fe_values.get_function_values(old_saturation_solution,
1853 * old_saturation_values);
1855 * pressure_right_hand_side.value_list(
1856 * darcy_fe_values.get_quadrature_points(), pressure_rhs_values);
1857 * k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1858 * k_inverse_values);
1860 * for (unsigned int q = 0; q < n_q_points; ++q)
1862 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
1864 * phi_u[k] = darcy_fe_values[velocities].value(k, q);
1865 * div_phi_u[k] = darcy_fe_values[velocities].divergence(k, q);
1866 * phi_p[k] = darcy_fe_values[pressure].value(k, q);
1868 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1870 * const double old_s = old_saturation_values[q];
1871 * for (unsigned int j = 0; j <= i; ++j)
1873 * local_matrix(i, j) +=
1874 * (phi_u[i] * k_inverse_values[q] *
1875 * mobility_inverse(old_s, viscosity) * phi_u[j] -
1876 * div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
1877 * darcy_fe_values.JxW(q);
1881 * (-phi_p[i] * pressure_rhs_values[q]) * darcy_fe_values.JxW(q);
1885 * for (const auto &face : cell->face_iterators())
1886 * if (face->at_boundary())
1888 * darcy_fe_face_values.reinit(cell, face);
1890 * pressure_boundary_values.value_list(
1891 * darcy_fe_face_values.get_quadrature_points(), boundary_values);
1893 * for (unsigned int q = 0; q < n_face_q_points; ++q)
1894 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1896 * const Tensor<1, dim> phi_i_u =
1897 * darcy_fe_face_values[velocities].value(i, q);
1900 * -(phi_i_u * darcy_fe_face_values.normal_vector(q) *
1901 * boundary_values[q] * darcy_fe_face_values.JxW(q));
1905 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1906 * for (unsigned int j = i + 1; j < dofs_per_cell; ++j)
1907 * local_matrix(i, j) = local_matrix(j, i);
1909 * cell->get_dof_indices(local_dof_indices);
1911 * darcy_constraints.distribute_local_to_global(
1912 * local_matrix, local_rhs, local_dof_indices, darcy_matrix, darcy_rhs);
1920 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_system"></a>
1921 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_system</h4>
1925 * This function is to assemble the linear system for the saturation
1926 * transport equation. It calls, if necessary, two other member functions:
1927 * assemble_saturation_matrix() and assemble_saturation_rhs(). The former
1928 * function then assembles the saturation matrix that only needs to be
1929 * changed occasionally. On the other hand, the latter function that
1930 * assembles the right hand side must be called at every saturation time
1934 * template <int dim>
1935 * void TwoPhaseFlowProblem<dim>::assemble_saturation_system()
1937 * if (rebuild_saturation_matrix == true)
1939 * saturation_matrix = 0;
1940 * assemble_saturation_matrix();
1943 * saturation_rhs = 0;
1944 * assemble_saturation_rhs();
1952 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_matrix"></a>
1953 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_matrix</h4>
1957 * This function is easily understood since it only forms a simple mass
1958 * matrix for the left hand side of the saturation linear system by basis
1959 * functions phi_i_s and phi_j_s only. Finally, as usual, we enter the local
1960 * contribution into the global matrix by specifying the position in
1961 * local_dof_indices. This is done by letting the AffineConstraints class do
1962 * the insertion of the cell matrix elements to the global matrix, which
1963 * already condenses the hanging node constraints.
1966 * template <int dim>
1967 * void TwoPhaseFlowProblem<dim>::assemble_saturation_matrix()
1969 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
1971 * FEValues<dim> saturation_fe_values(saturation_fe,
1972 * quadrature_formula,
1973 * update_values | update_JxW_values);
1975 * const unsigned int dofs_per_cell = saturation_fe.n_dofs_per_cell();
1977 * const unsigned int n_q_points = quadrature_formula.size();
1979 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1980 * Vector<double> local_rhs(dofs_per_cell);
1982 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1984 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
1986 * saturation_fe_values.reinit(cell);
1990 * for (unsigned int q = 0; q < n_q_points; ++q)
1991 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1993 * const double phi_i_s = saturation_fe_values.shape_value(i, q);
1994 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1996 * const double phi_j_s = saturation_fe_values.shape_value(j, q);
1997 * local_matrix(i, j) +=
1998 * porosity * phi_i_s * phi_j_s * saturation_fe_values.JxW(q);
2001 * cell->get_dof_indices(local_dof_indices);
2003 * saturation_constraints.distribute_local_to_global(local_matrix,
2004 * local_dof_indices,
2005 * saturation_matrix);
2014 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs"></a>
2015 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs</h4>
2019 * This function is to assemble the right hand side of the saturation
2020 * transport equation. Before going about it, we have to create two FEValues
2021 * objects for the Darcy and saturation systems respectively and, in
2022 * addition, two FEFaceValues objects for the two systems because we have a
2023 * boundary integral term in the weak form of saturation equation. For the
2024 * FEFaceValues object of the saturation system, we also require normal
2025 * vectors, which we request using the update_normal_vectors flag.
2029 * Next, before looping over all the cells, we have to compute some
2030 * parameters (e.g. global_u_infty, global_S_variation, and
2031 * global_Omega_diameter) that the artificial viscosity @f$\nu@f$ needs. This is
2032 * largely the same as was done in @ref step_31 "step-31", so you may see there for more
2037 * The real works starts with the loop over all the saturation and Darcy
2038 * cells to put the local contributions into the global vector. In this
2039 * loop, in order to simplify the implementation, we split some of the work
2040 * into two helper functions: assemble_saturation_rhs_cell_term and
2041 * assemble_saturation_rhs_boundary_term. We note that we insert cell or
2042 * boundary contributions into the global vector in the two functions rather
2043 * than in this present function.
2046 * template <int dim>
2047 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs()
2049 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
2050 * const QGauss<dim - 1> face_quadrature_formula(saturation_degree + 2);
2052 * FEValues<dim> saturation_fe_values(saturation_fe,
2053 * quadrature_formula,
2054 * update_values | update_gradients |
2055 * update_quadrature_points |
2056 * update_JxW_values);
2057 * FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2058 * FEFaceValues<dim> saturation_fe_face_values(saturation_fe,
2059 * face_quadrature_formula,
2061 * update_normal_vectors |
2062 * update_quadrature_points |
2063 * update_JxW_values);
2064 * FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
2065 * face_quadrature_formula,
2067 * FEFaceValues<dim> saturation_fe_face_values_neighbor(
2068 * saturation_fe, face_quadrature_formula, update_values);
2070 * const unsigned int dofs_per_cell =
2071 * saturation_dof_handler.get_fe().n_dofs_per_cell();
2072 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2074 * const double global_max_u_F_prime = get_max_u_F_prime();
2075 * const std::pair<double, double> global_S_range =
2076 * get_extrapolated_saturation_range();
2077 * const double global_S_variation =
2078 * global_S_range.second - global_S_range.first;
2080 * auto cell = saturation_dof_handler.begin_active();
2081 * const auto endc = saturation_dof_handler.end();
2082 * auto darcy_cell = darcy_dof_handler.begin_active();
2083 * for (; cell != endc; ++cell, ++darcy_cell)
2085 * saturation_fe_values.reinit(cell);
2086 * darcy_fe_values.reinit(darcy_cell);
2088 * cell->get_dof_indices(local_dof_indices);
2090 * assemble_saturation_rhs_cell_term(saturation_fe_values,
2092 * global_max_u_F_prime,
2093 * global_S_variation,
2094 * local_dof_indices);
2096 * for (const auto &face : cell->face_iterators())
2097 * if (face->at_boundary())
2099 * darcy_fe_face_values.reinit(darcy_cell, face);
2100 * saturation_fe_face_values.reinit(cell, face);
2101 * assemble_saturation_rhs_boundary_term(saturation_fe_face_values,
2102 * darcy_fe_face_values,
2103 * local_dof_indices);
2113 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs_cell_term"></a>
2114 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term</h4>
2118 * This function takes care of integrating the cell terms of the right hand
2119 * side of the saturation equation, and then assembling it into the global
2120 * right hand side vector. Given the discussion in the introduction, the
2121 * form of these contributions is clear. The only tricky part is getting the
2122 * artificial viscosity and all that is necessary to compute it. The first
2123 * half of the function is devoted to this task.
2127 * The last part of the function is copying the local contributions into the
2128 * global vector with position specified in local_dof_indices.
2131 * template <int dim>
2132 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term(
2133 * const FEValues<dim> &saturation_fe_values,
2134 * const FEValues<dim> &darcy_fe_values,
2135 * const double global_max_u_F_prime,
2136 * const double global_S_variation,
2137 * const std::vector<types::global_dof_index> &local_dof_indices)
2139 * const unsigned int dofs_per_cell = saturation_fe_values.dofs_per_cell;
2140 * const unsigned int n_q_points = saturation_fe_values.n_quadrature_points;
2142 * std::vector<double> old_saturation_solution_values(n_q_points);
2143 * std::vector<double> old_old_saturation_solution_values(n_q_points);
2144 * std::vector<Tensor<1, dim>> old_grad_saturation_solution_values(n_q_points);
2145 * std::vector<Tensor<1, dim>> old_old_grad_saturation_solution_values(
2147 * std::vector<Vector<double>> present_darcy_solution_values(
2148 * n_q_points, Vector<double>(dim + 1));
2150 * saturation_fe_values.get_function_values(old_saturation_solution,
2151 * old_saturation_solution_values);
2152 * saturation_fe_values.get_function_values(
2153 * old_old_saturation_solution, old_old_saturation_solution_values);
2154 * saturation_fe_values.get_function_gradients(
2155 * old_saturation_solution, old_grad_saturation_solution_values);
2156 * saturation_fe_values.get_function_gradients(
2157 * old_old_saturation_solution, old_old_grad_saturation_solution_values);
2158 * darcy_fe_values.get_function_values(darcy_solution,
2159 * present_darcy_solution_values);
2162 * compute_viscosity(old_saturation_solution_values,
2163 * old_old_saturation_solution_values,
2164 * old_grad_saturation_solution_values,
2165 * old_old_grad_saturation_solution_values,
2166 * present_darcy_solution_values,
2167 * global_max_u_F_prime,
2168 * global_S_variation,
2169 * saturation_fe_values.get_cell()->diameter());
2171 * Vector<double> local_rhs(dofs_per_cell);
2173 * for (unsigned int q = 0; q < n_q_points; ++q)
2174 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2176 * const double old_s = old_saturation_solution_values[q];
2177 * Tensor<1, dim> present_u;
2178 * for (unsigned int d = 0; d < dim; ++d)
2179 * present_u[d] = present_darcy_solution_values[q](d);
2181 * const double phi_i_s = saturation_fe_values.shape_value(i, q);
2182 * const Tensor<1, dim> grad_phi_i_s =
2183 * saturation_fe_values.shape_grad(i, q);
2186 * (time_step * fractional_flow(old_s, viscosity) * present_u *
2188 * time_step * nu * old_grad_saturation_solution_values[q] *
2190 * porosity * old_s * phi_i_s) *
2191 * saturation_fe_values.JxW(q);
2194 * saturation_constraints.distribute_local_to_global(local_rhs,
2195 * local_dof_indices,
2203 * <a name="step_43-TwoPhaseFlowProblemdimassemble_saturation_rhs_boundary_term"></a>
2204 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term</h4>
2208 * The next function is responsible for the boundary integral terms in the
2209 * right hand side form of the saturation equation. For these, we have to
2210 * compute the upwinding flux on the global boundary faces, i.e. we impose
2211 * Dirichlet boundary conditions weakly only on inflow parts of the global
2212 * boundary. As before, this has been described in @ref step_21 "step-21" so we refrain
2213 * from giving more descriptions about that.
2216 * template <int dim>
2217 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term(
2218 * const FEFaceValues<dim> &saturation_fe_face_values,
2219 * const FEFaceValues<dim> &darcy_fe_face_values,
2220 * const std::vector<types::global_dof_index> &local_dof_indices)
2222 * const unsigned int dofs_per_cell = saturation_fe_face_values.dofs_per_cell;
2223 * const unsigned int n_face_q_points =
2224 * saturation_fe_face_values.n_quadrature_points;
2226 * Vector<double> local_rhs(dofs_per_cell);
2228 * std::vector<double> old_saturation_solution_values_face(n_face_q_points);
2229 * std::vector<Vector<double>> present_darcy_solution_values_face(
2230 * n_face_q_points, Vector<double>(dim + 1));
2231 * std::vector<double> neighbor_saturation(n_face_q_points);
2233 * saturation_fe_face_values.get_function_values(
2234 * old_saturation_solution, old_saturation_solution_values_face);
2235 * darcy_fe_face_values.get_function_values(
2236 * darcy_solution, present_darcy_solution_values_face);
2238 * SaturationBoundaryValues<dim> saturation_boundary_values;
2239 * saturation_boundary_values.value_list(
2240 * saturation_fe_face_values.get_quadrature_points(), neighbor_saturation);
2242 * for (unsigned int q = 0; q < n_face_q_points; ++q)
2244 * Tensor<1, dim> present_u_face;
2245 * for (unsigned int d = 0; d < dim; ++d)
2246 * present_u_face[d] = present_darcy_solution_values_face[q](d);
2248 * const double normal_flux =
2249 * present_u_face * saturation_fe_face_values.normal_vector(q);
2251 * const bool is_outflow_q_point = (normal_flux >= 0);
2253 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2255 * time_step * normal_flux *
2256 * fractional_flow((is_outflow_q_point == true ?
2257 * old_saturation_solution_values_face[q] :
2258 * neighbor_saturation[q]),
2260 * saturation_fe_face_values.shape_value(i, q) *
2261 * saturation_fe_face_values.JxW(q);
2263 * saturation_constraints.distribute_local_to_global(local_rhs,
2264 * local_dof_indices,
2272 * <a name="step_43-TwoPhaseFlowProblemdimsolve"></a>
2273 * <h3>TwoPhaseFlowProblem<dim>::solve</h3>
2277 * This function implements the operator splitting algorithm, i.e. in each
2278 * time step it either re-computes the solution of the Darcy system or
2279 * extrapolates velocity/pressure from previous time steps, then determines
2280 * the size of the time step, and then updates the saturation variable. The
2281 * implementation largely follows similar code in @ref step_31 "step-31". It is, next to
2282 * the run() function, the central one in this program.
2286 * At the beginning of the function, we ask whether to solve the
2287 * pressure-velocity part by evaluating the a posteriori criterion (see the
2288 * following function). If necessary, we will solve the pressure-velocity
2289 * part using the GMRES solver with the Schur complement block
2290 * preconditioner as is described in the introduction.
2293 * template <int dim>
2294 * void TwoPhaseFlowProblem<dim>::solve()
2296 * const bool solve_for_pressure_and_velocity =
2297 * determine_whether_to_solve_for_pressure_and_velocity();
2299 * if (solve_for_pressure_and_velocity == true)
2301 * std::cout << " Solving Darcy (pressure-velocity) system..."
2304 * assemble_darcy_system();
2305 * build_darcy_preconditioner();
2308 * const LinearSolvers::InverseMatrix<TrilinosWrappers::SparseMatrix,
2309 * TrilinosWrappers::PreconditionIC>
2310 * mp_inverse(darcy_preconditioner_matrix.block(1, 1),
2311 * *bottom_right_preconditioner);
2313 * const LinearSolvers::BlockSchurPreconditioner<
2314 * TrilinosWrappers::PreconditionIC,
2315 * TrilinosWrappers::PreconditionIC>
2316 * preconditioner(darcy_matrix, mp_inverse, *top_left_preconditioner);
2318 * SolverControl solver_control(darcy_matrix.m(),
2319 * 1e-16 * darcy_rhs.l2_norm());
2321 * SolverGMRES<TrilinosWrappers::MPI::BlockVector> gmres(
2323 * SolverGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(
2326 * for (unsigned int i = 0; i < darcy_solution.size(); ++i)
2327 * if (darcy_constraints.is_constrained(i))
2328 * darcy_solution(i) = 0;
2330 * gmres.solve(darcy_matrix, darcy_solution, darcy_rhs, preconditioner);
2332 * darcy_constraints.distribute(darcy_solution);
2334 * std::cout << " ..." << solver_control.last_step()
2335 * << " GMRES iterations." << std::endl;
2339 * second_last_computed_darcy_solution = last_computed_darcy_solution;
2340 * last_computed_darcy_solution = darcy_solution;
2342 * saturation_matching_last_computed_darcy_solution =
2343 * saturation_solution;
2348 * On the other hand, if we have decided that we don't want to compute the
2349 * solution of the Darcy system
for the current time step, then we need to
2350 * simply
extrapolate the previous two Darcy solutions to the same time as
2351 * we would have computed the velocity/pressure at. We
do a simple linear
2352 * extrapolation, i.e. given the current length @f$dt@f$ of the macro time
2353 * step from the time when we last computed the Darcy solution to now
2354 * (given by <code>current_macro_time_step</code>), and @f$DT@f$ the length of
2355 * the last macro time step (given by <code>old_macro_time_step</code>),
2356 * then we get @f$u^\ast = u_p + dt \frac{u_p-u_{pp}}{DT} = (1+dt/DT)u_p -
2357 * dt/DT u_{pp}@f$, where @f$u_p@f$ and @f$u_{pp}@f$ are the last two computed Darcy
2358 * solutions. We can implement
this formula
using just two lines of code.
2362 * Note that the algorithm here only works
if we have at least two
2363 * previously computed Darcy solutions from which we can
extrapolate to
2364 * the current time, and
this is ensured by requiring re-computation of
2365 * the Darcy solution
for the
first 2 time steps.
2370 * darcy_solution = last_computed_darcy_solution;
2371 * darcy_solution.sadd(1 + current_macro_time_step / old_macro_time_step,
2372 * -current_macro_time_step / old_macro_time_step,
2373 * second_last_computed_darcy_solution);
2379 * With the so computed velocity vector, compute the optimal time step
2380 * based on the CFL criterion discussed in the introduction...
2384 * old_time_step = time_step;
2386 *
const double max_u_F_prime = get_max_u_F_prime();
2387 *
if (max_u_F_prime > 0)
2389 * saturation_degree / max_u_F_prime / 50;
2391 * time_step = end_time - time;
2398 * ...and then also update the length of the macro time steps we use
while
2399 * we
're dealing with time step sizes. In particular, this involves: (i)
2400 * If we have just recomputed the Darcy solution, then the length of the
2401 * previous macro time step is now fixed and the length of the current
2402 * macro time step is, up to now, simply the length of the current (micro)
2403 * time step. (ii) If we have not recomputed the Darcy solution, then the
2404 * length of the current macro time step has just grown by
2405 * <code>time_step</code>.
2408 * if (solve_for_pressure_and_velocity == true)
2410 * old_macro_time_step = current_macro_time_step;
2411 * current_macro_time_step = time_step;
2414 * current_macro_time_step += time_step;
2418 * The last step in this function is to recompute the saturation solution
2419 * based on the velocity field we've just obtained. This naturally happens
2420 * in every time step, and we don
't skip any of these computations. At the
2421 * end of computing the saturation, we project back into the allowed
2422 * interval @f$[0,1]@f$ to make sure our solution remains physical.
2426 * std::cout << " Solving saturation transport equation..." << std::endl;
2428 * assemble_saturation_system();
2430 * SolverControl solver_control(saturation_matrix.m(),
2431 * 1e-16 * saturation_rhs.l2_norm());
2432 * SolverCG<TrilinosWrappers::MPI::Vector> cg(solver_control);
2434 * TrilinosWrappers::PreconditionIC preconditioner;
2435 * preconditioner.initialize(saturation_matrix);
2437 * cg.solve(saturation_matrix,
2438 * saturation_solution,
2442 * saturation_constraints.distribute(saturation_solution);
2443 * project_back_saturation();
2445 * std::cout << " ..." << solver_control.last_step()
2446 * << " CG iterations." << std::endl;
2454 * <a name="step_43-TwoPhaseFlowProblemdimrefine_mesh"></a>
2455 * <h3>TwoPhaseFlowProblem<dim>::refine_mesh</h3>
2459 * The next function does the refinement and coarsening of the mesh. It does
2460 * its work in three blocks: (i) Compute refinement indicators by looking at
2461 * the gradient of a solution vector extrapolated linearly from the previous
2462 * two using the respective sizes of the time step (or taking the only
2463 * solution we have if this is the first time step). (ii) Flagging those
2464 * cells for refinement and coarsening where the gradient is larger or
2465 * smaller than a certain threshold, preserving minimal and maximal levels
2466 * of mesh refinement. (iii) Transferring the solution from the old to the
2467 * new mesh. None of this is particularly difficult.
2470 * template <int dim>
2471 * void TwoPhaseFlowProblem<dim>::refine_mesh(const unsigned int min_grid_level,
2472 * const unsigned int max_grid_level)
2474 * Vector<double> refinement_indicators(triangulation.n_active_cells());
2476 * const QMidpoint<dim> quadrature_formula;
2477 * FEValues<dim> fe_values(saturation_fe,
2478 * quadrature_formula,
2479 * update_gradients);
2480 * std::vector<Tensor<1, dim>> grad_saturation(1);
2482 * TrilinosWrappers::MPI::Vector extrapolated_saturation_solution(
2483 * saturation_solution);
2484 * if (timestep_number != 0)
2485 * extrapolated_saturation_solution.sadd((1. + time_step / old_time_step),
2486 * time_step / old_time_step,
2487 * old_saturation_solution);
2489 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2491 * const unsigned int cell_no = cell->active_cell_index();
2492 * fe_values.reinit(cell);
2493 * fe_values.get_function_gradients(extrapolated_saturation_solution,
2496 * refinement_indicators(cell_no) = grad_saturation[0].norm();
2501 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2503 * const unsigned int cell_no = cell->active_cell_index();
2504 * cell->clear_coarsen_flag();
2505 * cell->clear_refine_flag();
2507 * if ((static_cast<unsigned int>(cell->level()) < max_grid_level) &&
2508 * (std::fabs(refinement_indicators(cell_no)) >
2509 * saturation_refinement_threshold))
2510 * cell->set_refine_flag();
2511 * else if ((static_cast<unsigned int>(cell->level()) >
2512 * min_grid_level) &&
2513 * (std::fabs(refinement_indicators(cell_no)) <
2514 * 0.5 * saturation_refinement_threshold))
2515 * cell->set_coarsen_flag();
2519 * triangulation.prepare_coarsening_and_refinement();
2522 * std::vector<TrilinosWrappers::MPI::Vector> x_saturation(3);
2523 * x_saturation[0] = saturation_solution;
2524 * x_saturation[1] = old_saturation_solution;
2525 * x_saturation[2] = saturation_matching_last_computed_darcy_solution;
2527 * std::vector<TrilinosWrappers::MPI::BlockVector> x_darcy(2);
2528 * x_darcy[0] = last_computed_darcy_solution;
2529 * x_darcy[1] = second_last_computed_darcy_solution;
2531 * SolutionTransfer<dim, TrilinosWrappers::MPI::Vector> saturation_soltrans(
2532 * saturation_dof_handler);
2534 * SolutionTransfer<dim, TrilinosWrappers::MPI::BlockVector> darcy_soltrans(
2535 * darcy_dof_handler);
2538 * triangulation.prepare_coarsening_and_refinement();
2539 * saturation_soltrans.prepare_for_coarsening_and_refinement(x_saturation);
2541 * darcy_soltrans.prepare_for_coarsening_and_refinement(x_darcy);
2543 * triangulation.execute_coarsening_and_refinement();
2546 * std::vector<TrilinosWrappers::MPI::Vector> tmp_saturation(3);
2547 * tmp_saturation[0].reinit(saturation_solution);
2548 * tmp_saturation[1].reinit(saturation_solution);
2549 * tmp_saturation[2].reinit(saturation_solution);
2550 * saturation_soltrans.interpolate(tmp_saturation);
2552 * saturation_solution = tmp_saturation[0];
2553 * old_saturation_solution = tmp_saturation[1];
2554 * saturation_matching_last_computed_darcy_solution = tmp_saturation[2];
2556 * saturation_constraints.distribute(saturation_solution);
2557 * saturation_constraints.distribute(old_saturation_solution);
2558 * saturation_constraints.distribute(
2559 * saturation_matching_last_computed_darcy_solution);
2561 * std::vector<TrilinosWrappers::MPI::BlockVector> tmp_darcy(2);
2562 * tmp_darcy[0].reinit(darcy_solution);
2563 * tmp_darcy[1].reinit(darcy_solution);
2564 * darcy_soltrans.interpolate(tmp_darcy);
2566 * last_computed_darcy_solution = tmp_darcy[0];
2567 * second_last_computed_darcy_solution = tmp_darcy[1];
2569 * darcy_constraints.distribute(last_computed_darcy_solution);
2570 * darcy_constraints.distribute(second_last_computed_darcy_solution);
2572 * rebuild_saturation_matrix = true;
2581 * <a name="step_43-TwoPhaseFlowProblemdimoutput_results"></a>
2582 * <h3>TwoPhaseFlowProblem<dim>::output_results</h3>
2586 * This function generates graphical output. It is in essence a copy of the
2587 * implementation in @ref step_31 "step-31".
2590 * template <int dim>
2591 * void TwoPhaseFlowProblem<dim>::output_results() const
2593 * const FESystem<dim> joint_fe(darcy_fe, 1, saturation_fe, 1);
2594 * DoFHandler<dim> joint_dof_handler(triangulation);
2595 * joint_dof_handler.distribute_dofs(joint_fe);
2596 * Assert(joint_dof_handler.n_dofs() ==
2597 * darcy_dof_handler.n_dofs() + saturation_dof_handler.n_dofs(),
2598 * ExcInternalError());
2600 * Vector<double> joint_solution(joint_dof_handler.n_dofs());
2603 * std::vector<types::global_dof_index> local_joint_dof_indices(
2604 * joint_fe.n_dofs_per_cell());
2605 * std::vector<types::global_dof_index> local_darcy_dof_indices(
2606 * darcy_fe.n_dofs_per_cell());
2607 * std::vector<types::global_dof_index> local_saturation_dof_indices(
2608 * saturation_fe.n_dofs_per_cell());
2610 * auto joint_cell = joint_dof_handler.begin_active();
2611 * const auto joint_endc = joint_dof_handler.end();
2612 * auto darcy_cell = darcy_dof_handler.begin_active();
2613 * auto saturation_cell = saturation_dof_handler.begin_active();
2615 * for (; joint_cell != joint_endc;
2616 * ++joint_cell, ++darcy_cell, ++saturation_cell)
2618 * joint_cell->get_dof_indices(local_joint_dof_indices);
2619 * darcy_cell->get_dof_indices(local_darcy_dof_indices);
2620 * saturation_cell->get_dof_indices(local_saturation_dof_indices);
2622 * for (unsigned int i = 0; i < joint_fe.n_dofs_per_cell(); ++i)
2623 * if (joint_fe.system_to_base_index(i).first.first == 0)
2625 * Assert(joint_fe.system_to_base_index(i).second <
2626 * local_darcy_dof_indices.size(),
2627 * ExcInternalError());
2628 * joint_solution(local_joint_dof_indices[i]) = darcy_solution(
2629 * local_darcy_dof_indices[joint_fe.system_to_base_index(i)
2634 * Assert(joint_fe.system_to_base_index(i).first.first == 1,
2635 * ExcInternalError());
2636 * Assert(joint_fe.system_to_base_index(i).second <
2637 * local_darcy_dof_indices.size(),
2638 * ExcInternalError());
2639 * joint_solution(local_joint_dof_indices[i]) =
2640 * saturation_solution(
2641 * local_saturation_dof_indices
2642 * [joint_fe.system_to_base_index(i).second]);
2646 * std::vector<std::string> joint_solution_names(dim, "velocity");
2647 * joint_solution_names.emplace_back("pressure");
2648 * joint_solution_names.emplace_back("saturation");
2650 * std::vector<DataComponentInterpretation::DataComponentInterpretation>
2651 * data_component_interpretation(
2652 * dim, DataComponentInterpretation::component_is_part_of_vector);
2653 * data_component_interpretation.push_back(
2654 * DataComponentInterpretation::component_is_scalar);
2655 * data_component_interpretation.push_back(
2656 * DataComponentInterpretation::component_is_scalar);
2658 * DataOut<dim> data_out;
2660 * data_out.attach_dof_handler(joint_dof_handler);
2661 * data_out.add_data_vector(joint_solution,
2662 * joint_solution_names,
2663 * DataOut<dim>::type_dof_data,
2664 * data_component_interpretation);
2666 * data_out.build_patches();
2668 * std::string filename =
2669 * "solution-" + Utilities::int_to_string(timestep_number, 5) + ".vtu";
2670 * std::ofstream output(filename);
2671 * data_out.write_vtu(output);
2679 * <a name="step_43-Toolfunctions"></a>
2680 * <h3>Tool functions</h3>
2685 * <a name="step_43-TwoPhaseFlowProblemdimdetermine_whether_to_solve_for_pressure_and_velocity"></a>
2686 * <h4>TwoPhaseFlowProblem<dim>::determine_whether_to_solve_for_pressure_and_velocity</h4>
2690 * This function implements the a posteriori criterion for adaptive operator
2691 * splitting. The function is relatively straightforward given the way we
2692 * have implemented other functions above and given the formula for the
2693 * criterion derived in the paper.
2697 * If one decides that one wants the original IMPES method in which the
2698 * Darcy equation is solved in every time step, then this can be achieved by
2699 * setting the threshold value <code>AOS_threshold</code> (with a default of
2700 * @f$5.0@f$) to zero, thereby forcing the function to always return true.
2704 * Finally, note that the function returns true unconditionally for the
2705 * first two time steps to ensure that we have always solved the Darcy
2706 * system at least twice when skipping its solution, thereby allowing us to
2707 * extrapolate the velocity from the last two solutions in
2708 * <code>solve()</code>.
2711 * template <int dim>
2712 * bool TwoPhaseFlowProblem<
2713 * dim>::determine_whether_to_solve_for_pressure_and_velocity() const
2715 * if (timestep_number <= 2)
2718 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
2719 * const unsigned int n_q_points = quadrature_formula.size();
2721 * FEValues<dim> fe_values(saturation_fe,
2722 * quadrature_formula,
2723 * update_values | update_quadrature_points);
2725 * std::vector<double> old_saturation_after_solving_pressure(n_q_points);
2726 * std::vector<double> present_saturation(n_q_points);
2728 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
2730 * double max_global_aop_indicator = 0.0;
2732 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2734 * double max_local_mobility_reciprocal_difference = 0.0;
2735 * double max_local_permeability_inverse_l1_norm = 0.0;
2737 * fe_values.reinit(cell);
2738 * fe_values.get_function_values(
2739 * saturation_matching_last_computed_darcy_solution,
2740 * old_saturation_after_solving_pressure);
2741 * fe_values.get_function_values(saturation_solution, present_saturation);
2743 * k_inverse.value_list(fe_values.get_quadrature_points(),
2744 * k_inverse_values);
2746 * for (unsigned int q = 0; q < n_q_points; ++q)
2748 * const double mobility_reciprocal_difference = std::fabs(
2749 * mobility_inverse(present_saturation[q], viscosity) -
2750 * mobility_inverse(old_saturation_after_solving_pressure[q],
2753 * max_local_mobility_reciprocal_difference =
2754 * std::max(max_local_mobility_reciprocal_difference,
2755 * mobility_reciprocal_difference);
2757 * max_local_permeability_inverse_l1_norm =
2758 * std::max(max_local_permeability_inverse_l1_norm,
2759 * l1_norm(k_inverse_values[q]));
2762 * max_global_aop_indicator =
2763 * std::max(max_global_aop_indicator,
2764 * (max_local_mobility_reciprocal_difference *
2765 * max_local_permeability_inverse_l1_norm));
2768 * return (max_global_aop_indicator > AOS_threshold);
2776 * <a name="step_43-TwoPhaseFlowProblemdimproject_back_saturation"></a>
2777 * <h4>TwoPhaseFlowProblem<dim>::project_back_saturation</h4>
2781 * The next function simply makes sure that the saturation values always
2782 * remain within the physically reasonable range of @f$[0,1]@f$. While the
2783 * continuous equations guarantee that this is so, the discrete equations
2784 * don't. However,
if we allow the discrete solution to
escape this range we
2785 * get into trouble because terms like @f$F(S)@f$ and @f$F
'(S)@f$ will produce
2786 * unreasonable results (e.g. @f$F'(S)<0@f$
for @f$S<0@f$, which would imply that
2787 * the wetting fluid phase flows <i>against</i> the direction of the bulk
2788 * fluid velocity)). Consequently, at the end of each time step, we simply
2789 * project the saturation field back into the physically reasonable region.
2792 * template <int dim>
2793 *
void TwoPhaseFlowProblem<dim>::project_back_saturation()
2795 *
for (
unsigned int i = 0; i < saturation_solution.size(); ++i)
2796 *
if (saturation_solution(i) < 0.2)
2797 * saturation_solution(i) = 0.2;
2798 *
else if (saturation_solution(i) > 1)
2799 * saturation_solution(i) = 1;
2807 * <a name=
"step_43-TwoPhaseFlowProblemdimget_max_u_F_prime"></a>
2808 * <h4>TwoPhaseFlowProblem<dim>::get_max_u_F_prime</h4>
2812 * Another simpler helper function: Compute the maximum of the total
2813 * velocity times the derivative of the fraction flow function, i.e.,
2814 * compute @f$\|\mathbf{u}
F'(S)\|_{L_\infty(\Omega)}@f$. This term is used in
2815 * both the computation of the time step as well as in normalizing the
2816 * entropy-residual term in the artificial viscosity.
2819 * template <int dim>
2820 * double TwoPhaseFlowProblem<dim>::get_max_u_F_prime() const
2822 * const QGauss<dim> quadrature_formula(darcy_degree + 2);
2823 * const unsigned int n_q_points = quadrature_formula.size();
2825 * FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2826 * FEValues<dim> saturation_fe_values(saturation_fe,
2827 * quadrature_formula,
2830 * std::vector<Vector<double>> darcy_solution_values(n_q_points,
2831 * Vector<double>(dim + 1));
2832 * std::vector<double> saturation_values(n_q_points);
2834 * double max_velocity_times_dF_dS = 0;
2836 * auto cell = darcy_dof_handler.begin_active();
2837 * const auto endc = darcy_dof_handler.end();
2838 * auto saturation_cell = saturation_dof_handler.begin_active();
2839 * for (; cell != endc; ++cell, ++saturation_cell)
2841 * darcy_fe_values.reinit(cell);
2842 * saturation_fe_values.reinit(saturation_cell);
2844 * darcy_fe_values.get_function_values(darcy_solution,
2845 * darcy_solution_values);
2846 * saturation_fe_values.get_function_values(old_saturation_solution,
2847 * saturation_values);
2849 * for (unsigned int q = 0; q < n_q_points; ++q)
2851 * Tensor<1, dim> velocity;
2852 * for (unsigned int i = 0; i < dim; ++i)
2853 * velocity[i] = darcy_solution_values[q](i);
2855 * const double dF_dS =
2856 * fractional_flow_derivative(saturation_values[q], viscosity);
2858 * max_velocity_times_dF_dS =
2859 * std::max(max_velocity_times_dF_dS, velocity.norm() * dF_dS);
2863 * return max_velocity_times_dF_dS;
2870 * <a name="step_43-TwoPhaseFlowProblemdimget_extrapolated_saturation_range"></a>
2871 * <h4>TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range</h4>
2875 * For computing the stabilization term, we need to know the range of the
2876 * saturation variable. Unlike in @ref step_31 "step-31", this range is trivially bounded
2877 * by the interval @f$[0,1]@f$ but we can do a bit better by looping over a
2878 * collection of quadrature points and seeing what the values are there. If
2879 * we can, i.e., if there are at least two timesteps around, we can even
2880 * take the values extrapolated to the next time step.
2884 * As before, the function is taken with minimal modifications from @ref step_31 "step-31".
2887 * template <int dim>
2888 * std::pair<double, double>
2889 * TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range() const
2891 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
2892 * const unsigned int n_q_points = quadrature_formula.size();
2894 * FEValues<dim> fe_values(saturation_fe, quadrature_formula, update_values);
2895 * std::vector<double> old_saturation_values(n_q_points);
2896 * std::vector<double> old_old_saturation_values(n_q_points);
2898 * if (timestep_number != 0)
2900 * double min_saturation = std::numeric_limits<double>::max(),
2901 * max_saturation = -std::numeric_limits<double>::max();
2903 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2905 * fe_values.reinit(cell);
2906 * fe_values.get_function_values(old_saturation_solution,
2907 * old_saturation_values);
2908 * fe_values.get_function_values(old_old_saturation_solution,
2909 * old_old_saturation_values);
2911 * for (unsigned int q = 0; q < n_q_points; ++q)
2913 * const double saturation =
2914 * (1. + time_step / old_time_step) * old_saturation_values[q] -
2915 * time_step / old_time_step * old_old_saturation_values[q];
2917 * min_saturation = std::min(min_saturation, saturation);
2918 * max_saturation = std::max(max_saturation, saturation);
2922 * return std::make_pair(min_saturation, max_saturation);
2926 * double min_saturation = std::numeric_limits<double>::max(),
2927 * max_saturation = -std::numeric_limits<double>::max();
2929 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2931 * fe_values.reinit(cell);
2932 * fe_values.get_function_values(old_saturation_solution,
2933 * old_saturation_values);
2935 * for (unsigned int q = 0; q < n_q_points; ++q)
2937 * const double saturation = old_saturation_values[q];
2939 * min_saturation = std::min(min_saturation, saturation);
2940 * max_saturation = std::max(max_saturation, saturation);
2944 * return std::make_pair(min_saturation, max_saturation);
2953 * <a name="step_43-TwoPhaseFlowProblemdimcompute_viscosity"></a>
2954 * <h4>TwoPhaseFlowProblem<dim>::compute_viscosity</h4>
2958 * The final tool function is used to compute the artificial viscosity on a
2959 * given cell. This isn't particularly complicated
if you have the formula
2960 *
for it in front of you, and looking at the implementation in @ref step_31
"step-31". The
2961 * major difference to that tutorial program is that the velocity here is
2962 * not simply @f$\mathbf u@f$ but @f$\mathbf u
F'(S)@f$ and some of the formulas
2963 * need to be adjusted accordingly.
2966 * template <int dim>
2967 * double TwoPhaseFlowProblem<dim>::compute_viscosity(
2968 * const std::vector<double> &old_saturation,
2969 * const std::vector<double> &old_old_saturation,
2970 * const std::vector<Tensor<1, dim>> &old_saturation_grads,
2971 * const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
2972 * const std::vector<Vector<double>> &present_darcy_values,
2973 * const double global_max_u_F_prime,
2974 * const double global_S_variation,
2975 * const double cell_diameter) const
2977 * const double beta = .4 * dim;
2978 * const double alpha = 1;
2980 * if (global_max_u_F_prime == 0)
2981 * return 5e-3 * cell_diameter;
2983 * const unsigned int n_q_points = old_saturation.size();
2985 * double max_residual = 0;
2986 * double max_velocity_times_dF_dS = 0;
2988 * const bool use_dF_dS = true;
2990 * for (unsigned int q = 0; q < n_q_points; ++q)
2993 * for (unsigned int d = 0; d < dim; ++d)
2994 * u[d] = present_darcy_values[q](d);
2996 * const double dS_dt = porosity *
2997 * (old_saturation[q] - old_old_saturation[q]) /
3000 * const double dF_dS = fractional_flow_derivative(
3001 * (old_saturation[q] + old_old_saturation[q]) / 2.0, viscosity);
3003 * const double u_grad_S =
3004 * u * dF_dS * (old_saturation_grads[q] + old_old_saturation_grads[q]) /
3007 * const double residual =
3008 * std::abs((dS_dt + u_grad_S) *
3009 * std::pow((old_saturation[q] + old_old_saturation[q]) / 2,
3012 * max_residual = std::max(residual, max_residual);
3013 * max_velocity_times_dF_dS =
3014 * std::max(std::sqrt(u * u) * (use_dF_dS ? std::max(dF_dS, 1.) : 1),
3015 * max_velocity_times_dF_dS);
3018 * const double c_R = 1.0;
3019 * const double global_scaling = c_R * porosity *
3020 * (global_max_u_F_prime)*global_S_variation /
3021 * std::pow(global_Omega_diameter, alpha - 2.);
3024 * (max_velocity_times_dF_dS)*std::min(cell_diameter,
3025 * std::pow(cell_diameter, alpha) *
3034 * <a name="step_43-TwoPhaseFlowProblemdimrun"></a>
3035 * <h3>TwoPhaseFlowProblem<dim>::run</h3>
3039 * This function is, besides <code>solve()</code>, the primary function of
3040 * this program as it controls the time iteration as well as when the
3041 * solution is written into output files and when to do mesh refinement.
3045 * With the exception of the startup code that loops back to the beginning
3046 * of the function through the <code>goto start_time_iteration</code> label,
3047 * everything should be relatively straightforward. In any case, it mimics
3048 * the corresponding function in @ref step_31 "step-31".
3051 * template <int dim>
3052 * void TwoPhaseFlowProblem<dim>::run()
3054 * const unsigned int initial_refinement = (dim == 2 ? 5 : 2);
3055 * const unsigned int n_pre_refinement_steps = (dim == 2 ? 3 : 2);
3058 * GridGenerator::hyper_cube(triangulation, 0, 1);
3059 * triangulation.refine_global(initial_refinement);
3060 * global_Omega_diameter = GridTools::diameter(triangulation);
3064 * unsigned int pre_refinement_step = 0;
3066 * start_time_iteration:
3068 * VectorTools::project(saturation_dof_handler,
3069 * saturation_constraints,
3070 * QGauss<dim>(saturation_degree + 2),
3071 * SaturationInitialValues<dim>(),
3072 * old_saturation_solution);
3074 * time_step = old_time_step = 0;
3075 * current_macro_time_step = old_macro_time_step = 0;
3081 * std::cout << "Timestep " << timestep_number << ": t=" << time
3082 * << ", dt=" << time_step << std::endl;
3086 * std::cout << std::endl;
3088 * if (timestep_number % 200 == 0)
3091 * if (timestep_number % 25 == 0)
3092 * refine_mesh(initial_refinement,
3093 * initial_refinement + n_pre_refinement_steps);
3095 * if ((timestep_number == 0) &&
3096 * (pre_refinement_step < n_pre_refinement_steps))
3098 * ++pre_refinement_step;
3099 * goto start_time_iteration;
3102 * time += time_step;
3103 * ++timestep_number;
3105 * old_old_saturation_solution = old_saturation_solution;
3106 * old_saturation_solution = saturation_solution;
3108 * while (time <= end_time);
3110 * } // namespace Step43
3117 * <a name="step_43-Thecodemaincodefunction"></a>
3118 * <h3>The <code>main()</code> function</h3>
3122 * The main function looks almost the same as in all other programs. The need
3123 * to initialize the MPI subsystem for a program that uses Trilinos -- even
3124 * for programs that do not actually run in parallel -- is explained in
3125 * @ref step_31 "step-31".
3128 * int main(int argc, char *argv[])
3132 * using namespace dealii;
3133 * using namespace Step43;
3135 * Utilities::MPI::MPI_InitFinalize mpi_initialization(
3136 * argc, argv, numbers::invalid_unsigned_int);
3140 * This program can only be run in serial. Otherwise, throw an exception.
3143 * AssertThrow(Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD) == 1,
3145 * "This program can only be run in serial, use ./step-43"));
3147 * TwoPhaseFlowProblem<2> two_phase_flow_problem(1);
3148 * two_phase_flow_problem.run();
3150 * catch (std::exception &exc)
3152 * std::cerr << std::endl
3154 * << "----------------------------------------------------"
3156 * std::cerr << "Exception on processing: " << std::endl
3157 * << exc.what() << std::endl
3158 * << "Aborting!" << std::endl
3159 * << "----------------------------------------------------"
3166 * std::cerr << std::endl
3168 * << "----------------------------------------------------"
3170 * std::cerr << "Unknown exception!" << std::endl
3171 * << "Aborting!" << std::endl
3172 * << "----------------------------------------------------"
3180<a name="step_43-Results"></a><h1>Results</h1>
3184The output of this program is not really much different from that of
3185@ref step_21 "step-21": it solves the same problem, after all. Of more importance are
3186quantitative metrics such as the accuracy of the solution as well as
3187the time needed to compute it. These are documented in detail in the
3188two publications listed at the top of this page and we won't repeat
3191That said, no tutorial program is complete without a couple of good
3192pictures, so here is some output of a
run in 3
d:
3194<table align=
"center" class=
"tutorial" cellspacing=
"3" cellpadding=
"3">
3197 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.velocity.png" alt=
"">
3199 Velocity vectors of flow through the porous medium with
random
3200 permeability model. Streaming paths of high permeability and resulting
3201 high velocity are clearly visible.
3205 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.streamlines.png" alt=
"">
3207 Streamlines colored by the saturation along the streamline path. Blue
3208 streamlines indicate low saturations, i.e., the flow along these
3209 streamlines must be slow or
else more fluid would have been
3210 transported along them. On the other hand, green paths indicate high
3211 velocities since the fluid front has already reached further into the
3218 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.saturation.png" alt=
"">
3220 Streamlines with a
volume rendering of the saturation, showing how far
3221 the fluid front has advanced at
this time.
3225 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.mesh.png" alt=
"">
3227 Surface of the mesh showing the adaptive refinement along the front.
3234<a name=
"step-43-extensions"></a>
3235<a name=
"step_43-Possibilitiesforextensions"></a><h3>Possibilities
for extensions</h3>
3238The primary objection one may have to
this program is that it is still too
3239slow: 3
d computations on reasonably fine meshes are simply too expensive to be
3240done routinely and with reasonably quick turn-around. This is similar to the
3241situation we were in when we wrote @ref step_31
"step-31", from which
this program has taken
3242much inspiration. The solution is similar as it was there as well: We need to
3243parallelize the program in a way similar to how we derived @ref step_32
"step-32" out of
3244@ref step_31
"step-31". In fact, all of the techniques used in @ref step_32
"step-32" would be transferable
3245to
this program as well, making the program
run on dozens or hundreds of
3246processors immediately.
3248A different direction is to make the program more relevant to many other
3249porous media applications. Specifically, one avenue is to go to the primary
3250user of porous media flow simulators, namely the oil industry. There,
3251applications in
this area are dominated by multiphase flow (i.e., more than
3252the two phases we have here), and the reactions they may have with each other
3253(or any other way phases may exchange mass, such as through dissolution in and
3254bubbling out of gas from the oil phase). Furthermore, the presence of gas
3255often leads to compressibility effects of the fluid. Jointly, these effects
3256are typically formulated in the widely-used
"black oil model". True reactions
3257between multiple phases also play a role in oil reservoir modeling when
3258considering controlled burns of oil in the reservoir to raise pressure and
3259temperature. These are much more complex problems, though, and left
for future
3262Finally, from a mathematical perspective, we have derived the
3263criterion
for re-computing the velocity/pressure solution at a given
3264time step under the assumption that we want to compare the solution we
3265would get at the current time step with that computed the last time we
3266actually solved
this system. However, in the program, whenever we did
3267not re-compute the solution, we didn
't just use the previously
3268computed solution but instead extrapolated from the previous two times
3269we solved the system. Consequently, the criterion was pessimistically
3270stated: what we should really compare is the solution we would get at
3271the current time step with the extrapolated one. Re-stating the
3272theorem in this regard is left as an exercise.
3274There are also other ways to extend the mathematical foundation of
3275this program; for example, one may say that it isn't the velocity we
3276care about, but in fact the saturation. Thus, one may ask whether the
3277criterion we use here to decide whether @f$\mathbf u@f$ needs to be
3278recomputed is appropriate; one may,
for example, suggest that it is
3279also important to decide whether (and by how much) a wrong velocity
3280field in fact affects the solution of the saturation equation. This
3281would then naturally lead to a sensitivity analysis.
3283From an algorithmic viewpoint, we have here used a criterion
for refinement
3284that is often used in engineering, namely by looking at the
gradient of
3285the solution. However,
if you inspect the solution, you will find that
3286it quickly leads to refinement almost everywhere, even in regions where it
3287is clearly not necessary: frequently used therefore does not need to imply
3288that it is a useful criterion to
begin with. On the other hand, replacing
3289this criterion by a different and better one should not be very difficult.
3291should certainly be applicable to the current problem as well.
3294<a name=
"step_43-PlainProg"></a>
3295<h1> The plain program</h1>
3296@include
"step-43.cc"
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
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< value_type > &values) const
#define AssertDimension(dim1, dim2)
void loop(IteratorType begin, std_cxx20::type_identity_t< IteratorType > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, AssemblerType &assembler, const LoopControl &lctrl=LoopControl())
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)
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)
@ matrix
Contents is actually a matrix.
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
std::string escape(const std::string &input, const PatternBase::OutputStyle style)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
Tensor< 2, dim, Number > F(const Tensor< 2, dim, Number > &Grad_u)
VectorType::value_type * begin(VectorType &V)
void run(const Iterator &begin, const std_cxx20::type_identity_t< Iterator > &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
int(&) functions(const void *v1, const void *v2)
::VectorizedArray< Number, width > exp(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation