747 *
class SaturationBoundaryValues :
public Function<dim>
750 * SaturationBoundaryValues()
755 *
const unsigned int component = 0)
const override;
762 * SaturationBoundaryValues<dim>::value(
const Point<dim> &p,
763 *
const unsigned int )
const
773 *
class SaturationInitialValues :
public Function<dim>
776 * SaturationInitialValues()
781 *
const unsigned int component = 0)
const override;
790 * SaturationInitialValues<dim>::value(
const Point<dim> & ,
791 *
const unsigned int )
const
798 *
void SaturationInitialValues<dim>::vector_value(
const Point<dim> &p,
801 *
for (
unsigned int c = 0; c < this->n_components; ++c)
802 *
values(c) = SaturationInitialValues<dim>::value(p, c);
809 * <a name=
"Permeabilitymodels"></a>
810 * <h3>Permeability models</h3>
814 * In
this tutorial, we still use the two permeability models previously
815 * used in @ref step_21
"step-21" so we again refrain from commenting in detail about them.
818 *
namespace SingleCurvingCrack
835 *
void KInverse<dim>::value_list(
const std::vector<
Point<dim>> &points,
840 *
for (
unsigned int p = 0; p < points.size(); ++p)
844 *
const double distance_to_flowline =
845 * std::fabs(points[p][1] - 0.5 - 0.1 *
std::sin(10 * points[p][0]));
847 *
const double permeability =
852 *
for (
unsigned int d = 0;
d < dim; ++
d)
853 * values[p][d][d] = 1. / permeability;
859 *
namespace RandomMedium
874 *
static std::vector<Point<dim>> centers;
880 * std::vector<Point<dim>> KInverse<dim>::centers = []() {
881 *
const unsigned int N =
882 * (dim == 2 ? 40 : (dim == 3 ? 100 : throw ExcNotImplemented()));
884 * std::vector<Point<dim>> centers_list(N);
885 *
for (
unsigned int i = 0; i < N; ++i)
886 *
for (
unsigned int d = 0;
d < dim; ++
d)
887 * centers_list[i][d] =
static_cast<double>(rand()) / RAND_MAX;
889 *
return centers_list;
895 *
void KInverse<dim>::value_list(
const std::vector<
Point<dim>> &points,
900 *
for (
unsigned int p = 0; p < points.size(); ++p)
904 *
double permeability = 0;
905 *
for (
unsigned int i = 0; i < centers.size(); ++i)
907 *
std::exp(-(points[p] - centers[i]).norm_square() / (0.05 * 0.05));
909 *
const double normalized_permeability =
912 *
for (
unsigned int d = 0;
d < dim; ++
d)
913 * values[p][d][d] = 1. / normalized_permeability;
922 * <a name=
"Physicalquantities"></a>
923 * <h3>Physical quantities</h3>
927 * The implementations of all the physical quantities such as total mobility
928 * @f$\lambda_t@f$ and fractional flow of water @f$F@f$ are taken from @ref step_21
"step-21" so
929 * again we don
't have do any comment about them. Compared to @ref step_21 "step-21" we
930 * have added checks that the saturation passed to these functions is in
931 * fact within the physically valid range. Furthermore, given that the
932 * wetting phase moves at speed @f$\mathbf u F'(S)@f$ it is clear that @f$F
'(S)@f$
933 * must be greater or equal to zero, so we assert that as well to make sure
934 * that our calculations to get at the formula for the derivative made
938 * double mobility_inverse(const double S, const double viscosity)
940 * return 1.0 / (1.0 / viscosity * S * S + (1 - S) * (1 - S));
944 * double fractional_flow(const double S, const double viscosity)
946 * Assert((S >= 0) && (S <= 1),
947 * ExcMessage("Saturation is outside its physically valid range."));
949 * return S * S / (S * S + viscosity * (1 - S) * (1 - S));
953 * double fractional_flow_derivative(const double S, const double viscosity)
955 * Assert((S >= 0) && (S <= 1),
956 * ExcMessage("Saturation is outside its physically valid range."));
958 * const double temp = (S * S + viscosity * (1 - S) * (1 - S));
960 * const double numerator =
961 * 2.0 * S * temp - S * S * (2.0 * S - 2.0 * viscosity * (1 - S));
962 * const double denominator = std::pow(temp, 2.0);
964 * const double F_prime = numerator / denominator;
966 * Assert(F_prime >= 0, ExcInternalError());
975 * <a name="Helperclassesforsolversandpreconditioners"></a>
976 * <h3>Helper classes for solvers and preconditioners</h3>
980 * In this first part we define a number of classes that we need in the
981 * construction of linear solvers and preconditioners. This part is
982 * essentially the same as that used in @ref step_31 "step-31". The only difference is that
983 * the original variable name stokes_matrix is replaced by another name
984 * darcy_matrix to match our problem.
987 * namespace LinearSolvers
989 * template <class MatrixType, class PreconditionerType>
990 * class InverseMatrix : public Subscriptor
993 * InverseMatrix(const MatrixType & m,
994 * const PreconditionerType &preconditioner);
997 * template <typename VectorType>
998 * void vmult(VectorType &dst, const VectorType &src) const;
1001 * const SmartPointer<const MatrixType> matrix;
1002 * const PreconditionerType & preconditioner;
1006 * template <class MatrixType, class PreconditionerType>
1007 * InverseMatrix<MatrixType, PreconditionerType>::InverseMatrix(
1008 * const MatrixType & m,
1009 * const PreconditionerType &preconditioner)
1011 * , preconditioner(preconditioner)
1016 * template <class MatrixType, class PreconditionerType>
1017 * template <typename VectorType>
1018 * void InverseMatrix<MatrixType, PreconditionerType>::vmult(
1020 * const VectorType &src) const
1022 * SolverControl solver_control(src.size(), 1e-7 * src.l2_norm());
1023 * SolverCG<VectorType> cg(solver_control);
1029 * cg.solve(*matrix, dst, src, preconditioner);
1031 * catch (std::exception &e)
1033 * Assert(false, ExcMessage(e.what()));
1037 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1038 * class BlockSchurPreconditioner : public Subscriptor
1041 * BlockSchurPreconditioner(
1042 * const TrilinosWrappers::BlockSparseMatrix &S,
1043 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1044 * PreconditionerTypeMp> &Mpinv,
1045 * const PreconditionerTypeA & Apreconditioner);
1047 * void vmult(TrilinosWrappers::MPI::BlockVector & dst,
1048 * const TrilinosWrappers::MPI::BlockVector &src) const;
1051 * const SmartPointer<const TrilinosWrappers::BlockSparseMatrix>
1053 * const SmartPointer<const InverseMatrix<TrilinosWrappers::SparseMatrix,
1054 * PreconditionerTypeMp>>
1056 * const PreconditionerTypeA &a_preconditioner;
1058 * mutable TrilinosWrappers::MPI::Vector tmp;
1063 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1064 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::
1065 * BlockSchurPreconditioner(
1066 * const TrilinosWrappers::BlockSparseMatrix &S,
1067 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1068 * PreconditionerTypeMp> &Mpinv,
1069 * const PreconditionerTypeA & Apreconditioner)
1070 * : darcy_matrix(&S)
1071 * , m_inverse(&Mpinv)
1072 * , a_preconditioner(Apreconditioner)
1073 * , tmp(complete_index_set(darcy_matrix->block(1, 1).m()))
1077 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1079 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::vmult(
1080 * TrilinosWrappers::MPI::BlockVector & dst,
1081 * const TrilinosWrappers::MPI::BlockVector &src) const
1083 * a_preconditioner.vmult(dst.block(0), src.block(0));
1084 * darcy_matrix->block(1, 0).residual(tmp, dst.block(0), src.block(1));
1086 * m_inverse->vmult(dst.block(1), tmp);
1088 * } // namespace LinearSolvers
1094 * <a name="TheTwoPhaseFlowProblemclass"></a>
1095 * <h3>The TwoPhaseFlowProblem class</h3>
1099 * The definition of the class that defines the top-level logic of solving
1100 * the time-dependent advection-dominated two-phase flow problem (or
1101 * Buckley-Leverett problem @cite Buckley1942) is mainly based on tutorial
1102 * programs @ref step_21 "step-21" and @ref step_33 "step-33", and in particular on @ref step_31 "step-31" where we have
1103 * used basically the same general structure as done here. As in @ref step_31 "step-31",
1104 * the key routines to look for in the implementation below are the
1105 * <code>run()</code> and <code>solve()</code> functions.
1109 * The main difference to @ref step_31 "step-31" is that, since adaptive operator splitting
1110 * is considered, we need a couple more member variables to hold the last
1111 * two computed Darcy (velocity/pressure) solutions in addition to the
1112 * current one (which is either computed directly, or extrapolated from the
1113 * previous two), and we need to remember the last two times we computed the
1114 * Darcy solution. We also need a helper function that figures out whether
1115 * we do indeed need to recompute the Darcy solution.
1119 * Unlike @ref step_31 "step-31", this step uses one more AffineConstraints object called
1120 * darcy_preconditioner_constraints. This constraint object is used only for
1121 * assembling the matrix for the Darcy preconditioner and includes hanging
1122 * node constraints as well as Dirichlet boundary value constraints for the
1123 * pressure variable. We need this because we are building a Laplace matrix
1124 * for the pressure as an approximation of the Schur complement) which is
1125 * only positive definite if boundary conditions are applied.
1129 * The collection of member functions and variables thus declared in this
1130 * class is then rather similar to those in @ref step_31 "step-31":
1133 * template <int dim>
1134 * class TwoPhaseFlowProblem
1137 * TwoPhaseFlowProblem(const unsigned int degree);
1141 * void setup_dofs();
1142 * void assemble_darcy_preconditioner();
1143 * void build_darcy_preconditioner();
1144 * void assemble_darcy_system();
1145 * void assemble_saturation_system();
1146 * void assemble_saturation_matrix();
1147 * void assemble_saturation_rhs();
1148 * void assemble_saturation_rhs_cell_term(
1149 * const FEValues<dim> & saturation_fe_values,
1150 * const FEValues<dim> & darcy_fe_values,
1151 * const double global_max_u_F_prime,
1152 * const double global_S_variation,
1153 * const std::vector<types::global_dof_index> &local_dof_indices);
1154 * void assemble_saturation_rhs_boundary_term(
1155 * const FEFaceValues<dim> & saturation_fe_face_values,
1156 * const FEFaceValues<dim> & darcy_fe_face_values,
1157 * const std::vector<types::global_dof_index> &local_dof_indices);
1159 * void refine_mesh(const unsigned int min_grid_level,
1160 * const unsigned int max_grid_level);
1161 * void output_results() const;
1165 * We follow with a number of helper functions that are used in a variety
1166 * of places throughout the program:
1169 * double get_max_u_F_prime() const;
1170 * std::pair<double, double> get_extrapolated_saturation_range() const;
1171 * bool determine_whether_to_solve_for_pressure_and_velocity() const;
1172 * void project_back_saturation();
1173 * double compute_viscosity(
1174 * const std::vector<double> & old_saturation,
1175 * const std::vector<double> & old_old_saturation,
1176 * const std::vector<Tensor<1, dim>> &old_saturation_grads,
1177 * const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
1178 * const std::vector<Vector<double>> &present_darcy_values,
1179 * const double global_max_u_F_prime,
1180 * const double global_S_variation,
1181 * const double cell_diameter) const;
1186 * This all is followed by the member variables, most of which are similar
1187 * to the ones in @ref step_31 "step-31", with the exception of the ones that pertain to
1188 * the macro time stepping for the velocity/pressure system:
1191 * Triangulation<dim> triangulation;
1192 * double global_Omega_diameter;
1194 * const unsigned int degree;
1196 * const unsigned int darcy_degree;
1197 * FESystem<dim> darcy_fe;
1198 * DoFHandler<dim> darcy_dof_handler;
1199 * AffineConstraints<double> darcy_constraints;
1201 * AffineConstraints<double> darcy_preconditioner_constraints;
1203 * TrilinosWrappers::BlockSparseMatrix darcy_matrix;
1204 * TrilinosWrappers::BlockSparseMatrix darcy_preconditioner_matrix;
1206 * TrilinosWrappers::MPI::BlockVector darcy_solution;
1207 * TrilinosWrappers::MPI::BlockVector darcy_rhs;
1209 * TrilinosWrappers::MPI::BlockVector last_computed_darcy_solution;
1210 * TrilinosWrappers::MPI::BlockVector second_last_computed_darcy_solution;
1213 * const unsigned int saturation_degree;
1214 * FE_Q<dim> saturation_fe;
1215 * DoFHandler<dim> saturation_dof_handler;
1216 * AffineConstraints<double> saturation_constraints;
1218 * TrilinosWrappers::SparseMatrix saturation_matrix;
1221 * TrilinosWrappers::MPI::Vector saturation_solution;
1222 * TrilinosWrappers::MPI::Vector old_saturation_solution;
1223 * TrilinosWrappers::MPI::Vector old_old_saturation_solution;
1224 * TrilinosWrappers::MPI::Vector saturation_rhs;
1226 * TrilinosWrappers::MPI::Vector
1227 * saturation_matching_last_computed_darcy_solution;
1229 * const double saturation_refinement_threshold;
1232 * const double end_time;
1234 * double current_macro_time_step;
1235 * double old_macro_time_step;
1238 * double old_time_step;
1239 * unsigned int timestep_number;
1241 * const double viscosity;
1242 * const double porosity;
1243 * const double AOS_threshold;
1245 * std::shared_ptr<TrilinosWrappers::PreconditionIC> top_left_preconditioner;
1246 * std::shared_ptr<TrilinosWrappers::PreconditionIC>
1247 * bottom_right_preconditioner;
1249 * bool rebuild_saturation_matrix;
1253 * At the very end we declare a variable that denotes the material
1254 * model. Compared to @ref step_21 "step-21", we do this here as a member variable since
1255 * we will want to use it in a variety of places and so having a central
1256 * place where such a variable is declared will make it simpler to replace
1257 * one class by another (e.g. replace RandomMedium::KInverse by
1258 * SingleCurvingCrack::KInverse).
1261 * const RandomMedium::KInverse<dim> k_inverse;
1268 * <a name="TwoPhaseFlowProblemdimTwoPhaseFlowProblem"></a>
1269 * <h3>TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem</h3>
1273 * The constructor of this class is an extension of the constructors in
1274 * @ref step_21 "step-21" and @ref step_31 "step-31". We need to add the various variables that concern
1275 * the saturation. As discussed in the introduction, we are going to use
1276 * @f$Q_2 \times Q_1@f$ (Taylor-Hood) elements again for the Darcy system, an
1277 * element combination that fulfills the Ladyzhenskaya-Babuska-Brezzi (LBB)
1278 * conditions [Brezzi and Fortin 1991, Chen 2005], and @f$Q_1@f$ elements for
1279 * the saturation. However, by using variables that store the polynomial
1280 * degree of the Darcy and temperature finite elements, it is easy to
1281 * consistently modify the degree of the elements as well as all quadrature
1282 * formulas used on them downstream. Moreover, we initialize the time
1283 * stepping variables related to operator splitting as well as the option
1284 * for matrix assembly and preconditioning:
1287 * template <int dim>
1288 * TwoPhaseFlowProblem<dim>::TwoPhaseFlowProblem(const unsigned int degree)
1289 * : triangulation(Triangulation<dim>::maximum_smoothing)
1290 * , global_Omega_diameter(std::numeric_limits<double>::quiet_NaN())
1292 * , darcy_degree(degree)
1293 * , darcy_fe(FE_Q<dim>(darcy_degree + 1), dim, FE_Q<dim>(darcy_degree), 1)
1294 * , darcy_dof_handler(triangulation)
1297 * saturation_degree(degree + 1)
1298 * , saturation_fe(saturation_degree)
1299 * , saturation_dof_handler(triangulation)
1302 * saturation_refinement_threshold(0.5)
1309 * current_macro_time_step(0)
1310 * , old_macro_time_step(0)
1314 * , old_time_step(0)
1315 * , timestep_number(0)
1318 * , AOS_threshold(3.0)
1321 * rebuild_saturation_matrix(true)
1328 * <a name="TwoPhaseFlowProblemdimsetup_dofs"></a>
1329 * <h3>TwoPhaseFlowProblem<dim>::setup_dofs</h3>
1333 * This is the function that sets up the DoFHandler objects we have here
1334 * (one for the Darcy part and one for the saturation part) as well as set
1335 * to the right sizes the various objects required for the linear algebra in
1336 * this program. Its basic operations are similar to what @ref step_31 "step-31" did.
1340 * The body of the function first enumerates all degrees of freedom for the
1341 * Darcy and saturation systems. For the Darcy part, degrees of freedom are
1342 * then sorted to ensure that velocities precede pressure DoFs so that we
1343 * can partition the Darcy matrix into a @f$2 \times 2@f$ matrix.
1347 * Then, we need to incorporate hanging node constraints and Dirichlet
1348 * boundary value constraints into darcy_preconditioner_constraints. The
1349 * boundary condition constraints are only set on the pressure component
1350 * since the Schur complement preconditioner that corresponds to the porous
1351 * media flow operator in non-mixed form, @f$-\nabla \cdot [\mathbf K
1352 * \lambda_t(S)]\nabla@f$, acts only on the pressure variable. Therefore, we
1353 * use a component_mask that filters out the velocity component, so that the
1354 * condensation is performed on pressure degrees of freedom only.
1358 * After having done so, we count the number of degrees of freedom in the
1359 * various blocks. This information is then used to create the sparsity
1360 * pattern for the Darcy and saturation system matrices as well as the
1361 * preconditioner matrix from which we build the Darcy preconditioner. As in
1362 * @ref step_31 "step-31", we choose to create the pattern using the blocked version of
1363 * DynamicSparsityPattern. So, for this, we follow the same way as @ref step_31 "step-31"
1364 * did and we don't have to repeat descriptions again
for the rest of the
1368 * template <int dim>
1369 *
void TwoPhaseFlowProblem<dim>::setup_dofs()
1371 * std::vector<unsigned int> darcy_block_component(dim + 1, 0);
1372 * darcy_block_component[dim] = 1;
1374 * darcy_dof_handler.distribute_dofs(darcy_fe);
1378 * darcy_constraints.clear();
1380 * darcy_constraints);
1381 * darcy_constraints.close();
1384 * saturation_dof_handler.distribute_dofs(saturation_fe);
1386 * saturation_constraints.clear();
1388 * saturation_constraints);
1389 * saturation_constraints.close();
1392 * darcy_preconditioner_constraints.clear();
1397 * darcy_preconditioner_constraints);
1399 * darcy_preconditioner_constraints,
1400 * darcy_fe.component_mask(
1403 * darcy_preconditioner_constraints.close();
1407 *
const std::vector<types::global_dof_index> darcy_dofs_per_block =
1409 * darcy_block_component);
1411 * n_p = darcy_dofs_per_block[1],
1412 * n_s = saturation_dof_handler.n_dofs();
1414 * std::cout <<
"Number of active cells: " <<
triangulation.n_active_cells()
1415 * <<
" (on " <<
triangulation.n_levels() <<
" levels)" << std::endl
1416 * <<
"Number of degrees of freedom: " << n_u + n_p + n_s <<
" ("
1417 * << n_u <<
'+' << n_p <<
'+' << n_s <<
')' << std::endl
1421 * darcy_matrix.clear();
1424 * darcy_dofs_per_block);
1427 *
for (
unsigned int c = 0; c < dim + 1; ++c)
1428 *
for (
unsigned int d = 0;
d < dim + 1; ++
d)
1429 *
if (!((c == dim) && (d == dim)))
1436 * darcy_dof_handler, coupling, dsp, darcy_constraints,
false);
1438 * darcy_matrix.reinit(dsp);
1442 * top_left_preconditioner.reset();
1443 * bottom_right_preconditioner.reset();
1444 * darcy_preconditioner_matrix.clear();
1447 * darcy_dofs_per_block);
1450 *
for (
unsigned int c = 0; c < dim + 1; ++c)
1451 *
for (
unsigned int d = 0;
d < dim + 1; ++
d)
1458 * darcy_dof_handler, coupling, dsp, darcy_constraints,
false);
1460 * darcy_preconditioner_matrix.reinit(dsp);
1465 * saturation_matrix.clear();
1471 * saturation_constraints,
1475 * saturation_matrix.reinit(dsp);
1481 * darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1483 * last_computed_darcy_solution.reinit(darcy_partitioning, MPI_COMM_WORLD);
1485 * second_last_computed_darcy_solution.reinit(darcy_partitioning,
1488 * darcy_rhs.reinit(darcy_partitioning, MPI_COMM_WORLD);
1491 * saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1492 * old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1493 * old_old_saturation_solution.reinit(saturation_partitioning, MPI_COMM_WORLD);
1495 * saturation_matching_last_computed_darcy_solution.reinit(
1496 * saturation_partitioning, MPI_COMM_WORLD);
1498 * saturation_rhs.reinit(saturation_partitioning, MPI_COMM_WORLD);
1505 * <a name=
"Assemblingmatricesandpreconditioners"></a>
1506 * <h3>Assembling matrices and preconditioners</h3>
1510 * The next few
functions are devoted to setting up the various system and
1511 * preconditioner matrices and right hand sides that we have to deal with in
1517 * <a name=
"TwoPhaseFlowProblemdimassemble_darcy_preconditioner"></a>
1518 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner</h4>
1522 * This function assembles the
matrix we use
for preconditioning the Darcy
1523 * system. What we need are a vector @ref GlossMassMatrix
"mass matrix" weighted by
1524 * @f$\left(\mathbf{
K} \lambda_t\right)^{-1}@f$ on the velocity components and a
1525 * mass
matrix weighted by @f$\left(\mathbf{
K} \lambda_t\right)@f$ on the
1526 * pressure component. We start by generating a quadrature
object of
1527 * appropriate order, the
FEValues object that can give values and gradients
1528 * at the quadrature points (together with quadrature weights). Next we
1529 * create data structures
for the cell
matrix and the relation between local
1530 * and global DoFs. The vectors phi_u and grad_phi_p are going to hold the
1532 * matrices, as was already done in @ref step_22
"step-22". Before we start the
loop over
1533 * all active cells, we have to specify which components are pressure and
1534 * which are velocity.
1538 * The creation of the local
matrix is rather simple. There are only a term
1539 * weighted by @f$\left(\mathbf{
K} \lambda_t\right)^{-1}@f$ (on the velocity)
1540 * and a Laplace
matrix weighted by @f$\left(\mathbf{
K} \lambda_t\right)@f$ to
1541 * be generated, so the creation of the local matrix is done in essentially
1542 * two lines. Since the material model functions at the top of
this file
1543 * only provide the inverses of the permeability and mobility, we have to
1544 * compute @f$\mathbf K@f$ and @f$\lambda_t@f$ by hand from the given values, once
1545 * per quadrature
point.
1549 * Once the local matrix is ready (loop over rows and columns in the local
1550 * matrix on each quadrature point), we get the local DoF indices and write
1551 * the local information into the global
matrix. We
do this by directly
1552 * applying the constraints (i.e. darcy_preconditioner_constraints) that
1553 * takes care of hanging node and zero Dirichlet boundary condition
1554 * constraints. By doing so, we don
't have to do that afterwards, and we
1557 * modify
matrix and vector entries and so are difficult to write
for the
1558 * Trilinos classes where we don
't immediately have access to individual
1562 * template <int dim>
1563 * void TwoPhaseFlowProblem<dim>::assemble_darcy_preconditioner()
1565 * std::cout << " Rebuilding darcy preconditioner..." << std::endl;
1567 * darcy_preconditioner_matrix = 0;
1569 * const QGauss<dim> quadrature_formula(darcy_degree + 2);
1570 * FEValues<dim> darcy_fe_values(darcy_fe,
1571 * quadrature_formula,
1572 * update_JxW_values | update_values |
1573 * update_gradients |
1574 * update_quadrature_points);
1575 * FEValues<dim> saturation_fe_values(saturation_fe,
1576 * quadrature_formula,
1579 * const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1580 * const unsigned int n_q_points = quadrature_formula.size();
1582 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1584 * std::vector<double> old_saturation_values(n_q_points);
1586 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1587 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1589 * std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1590 * std::vector<Tensor<1, dim>> grad_phi_p(dofs_per_cell);
1592 * const FEValuesExtractors::Vector velocities(0);
1593 * const FEValuesExtractors::Scalar pressure(dim);
1595 * auto cell = darcy_dof_handler.begin_active();
1596 * const auto endc = darcy_dof_handler.end();
1597 * auto saturation_cell = saturation_dof_handler.begin_active();
1599 * for (; cell != endc; ++cell, ++saturation_cell)
1601 * darcy_fe_values.reinit(cell);
1602 * saturation_fe_values.reinit(saturation_cell);
1606 * saturation_fe_values.get_function_values(old_saturation_solution,
1607 * old_saturation_values);
1609 * k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1610 * k_inverse_values);
1612 * for (unsigned int q = 0; q < n_q_points; ++q)
1614 * const double old_s = old_saturation_values[q];
1616 * const double inverse_mobility = mobility_inverse(old_s, viscosity);
1617 * const double mobility = 1.0 / inverse_mobility;
1618 * const Tensor<2, dim> permeability = invert(k_inverse_values[q]);
1620 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
1622 * phi_u[k] = darcy_fe_values[velocities].value(k, q);
1623 * grad_phi_p[k] = darcy_fe_values[pressure].gradient(k, q);
1626 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1627 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1629 * local_matrix(i, j) +=
1630 * (k_inverse_values[q] * inverse_mobility * phi_u[i] *
1632 * permeability * mobility * grad_phi_p[i] * grad_phi_p[j]) *
1633 * darcy_fe_values.JxW(q);
1637 * cell->get_dof_indices(local_dof_indices);
1638 * darcy_preconditioner_constraints.distribute_local_to_global(
1639 * local_matrix, local_dof_indices, darcy_preconditioner_matrix);
1647 * <a name="TwoPhaseFlowProblemdimbuild_darcy_preconditioner"></a>
1648 * <h4>TwoPhaseFlowProblem<dim>::build_darcy_preconditioner</h4>
1652 * After calling the above functions to assemble the preconditioner matrix,
1653 * this function generates the inner preconditioners that are going to be
1654 * used for the Schur complement block preconditioner. The preconditioners
1655 * need to be regenerated at every saturation time step since they depend on
1656 * the saturation @f$S@f$ that varies with time.
1660 * In here, we set up the preconditioner for the velocity-velocity matrix
1661 * @f$\mathbf{M}^{\mathbf{u}}@f$ and the Schur complement @f$\mathbf{S}@f$. As
1662 * explained in the introduction, we are going to use an IC preconditioner
1663 * based on the vector matrix @f$\mathbf{M}^{\mathbf{u}}@f$ and another based on
1664 * the scalar Laplace matrix @f$\tilde{\mathbf{S}}^p@f$ (which is spectrally
1665 * close to the Schur complement of the Darcy matrix). Usually, the
1666 * TrilinosWrappers::PreconditionIC class can be seen as a good black-box
1667 * preconditioner which does not need any special knowledge of the matrix
1668 * structure and/or the operator that's behind it.
1671 *
template <
int dim>
1672 *
void TwoPhaseFlowProblem<dim>::build_darcy_preconditioner()
1674 * assemble_darcy_preconditioner();
1676 * top_left_preconditioner =
1677 * std::make_shared<TrilinosWrappers::PreconditionIC>();
1678 * top_left_preconditioner->initialize(
1679 * darcy_preconditioner_matrix.block(0, 0));
1681 * bottom_right_preconditioner =
1682 * std::make_shared<TrilinosWrappers::PreconditionIC>();
1683 * bottom_right_preconditioner->initialize(
1684 * darcy_preconditioner_matrix.block(1, 1));
1691 * <a name=
"TwoPhaseFlowProblemdimassemble_darcy_system"></a>
1692 * <h4>TwoPhaseFlowProblem<dim>::assemble_darcy_system</h4>
1696 * This is the function that assembles the linear system
for the Darcy
1701 * Regarding the technical details of implementation, the procedures are
1702 * similar to those in @ref step_22
"step-22" and @ref step_31
"step-31". We reset
matrix and vector,
1703 * create a quadrature formula on the cells, and then create the respective
1708 * There is one thing that needs to be commented: since we have a separate
1709 * finite element and
DoFHandler for the saturation, we need to generate a
1710 *
second FEValues object for the proper evaluation of the saturation
1711 * solution. This isn
't too complicated to realize here: just use the
1712 * saturation structures and set an update flag for the basis function
1713 * values which we need for evaluation of the saturation solution. The only
1714 * important part to remember here is that the same quadrature formula is
1715 * used for both FEValues objects to ensure that we get matching information
1716 * when we loop over the quadrature points of the two objects.
1720 * The declarations proceed with some shortcuts for array sizes, the
1721 * creation of the local matrix, right hand side as well as the vector for
1722 * the indices of the local dofs compared to the global system.
1725 * template <int dim>
1726 * void TwoPhaseFlowProblem<dim>::assemble_darcy_system()
1731 * QGauss<dim> quadrature_formula(darcy_degree + 2);
1732 * QGauss<dim - 1> face_quadrature_formula(darcy_degree + 2);
1734 * FEValues<dim> darcy_fe_values(darcy_fe,
1735 * quadrature_formula,
1736 * update_values | update_gradients |
1737 * update_quadrature_points |
1738 * update_JxW_values);
1740 * FEValues<dim> saturation_fe_values(saturation_fe,
1741 * quadrature_formula,
1744 * FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
1745 * face_quadrature_formula,
1747 * update_normal_vectors |
1748 * update_quadrature_points |
1749 * update_JxW_values);
1751 * const unsigned int dofs_per_cell = darcy_fe.n_dofs_per_cell();
1753 * const unsigned int n_q_points = quadrature_formula.size();
1754 * const unsigned int n_face_q_points = face_quadrature_formula.size();
1756 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1757 * Vector<double> local_rhs(dofs_per_cell);
1759 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1761 * const Functions::ZeroFunction<dim> pressure_right_hand_side;
1762 * const PressureBoundaryValues<dim> pressure_boundary_values;
1764 * std::vector<double> pressure_rhs_values(n_q_points);
1765 * std::vector<double> boundary_values(n_face_q_points);
1766 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
1770 * Next we need a vector that will contain the values of the saturation
1771 * solution at the previous time level at the quadrature points to
1772 * assemble the saturation dependent coefficients in the Darcy equations.
1776 * The set of vectors we create next hold the evaluations of the basis
1777 * functions as well as their gradients that will be used for creating the
1778 * matrices. Putting these into their own arrays rather than asking the
1779 * FEValues object for this information each time it is needed is an
1780 * optimization to accelerate the assembly process, see @ref step_22 "step-22" for
1785 * The last two declarations are used to extract the individual blocks
1786 * (velocity, pressure, saturation) from the total FE system.
1789 * std::vector<double> old_saturation_values(n_q_points);
1791 * std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
1792 * std::vector<double> div_phi_u(dofs_per_cell);
1793 * std::vector<double> phi_p(dofs_per_cell);
1795 * const FEValuesExtractors::Vector velocities(0);
1796 * const FEValuesExtractors::Scalar pressure(dim);
1800 * Now start the loop over all cells in the problem. We are working on two
1801 * different DoFHandlers for this assembly routine, so we must have two
1802 * different cell iterators for the two objects in use. This might seem a
1803 * bit peculiar, but since both the Darcy system and the saturation system
1804 * use the same grid we can assume that the two iterators run in sync over
1805 * the cells of the two DoFHandler objects.
1809 * The first statements within the loop are again all very familiar, doing
1810 * the update of the finite element data as specified by the update flags,
1811 * zeroing out the local arrays and getting the values of the old solution
1812 * at the quadrature points. At this point we also have to get the values
1813 * of the saturation function of the previous time step at the quadrature
1814 * points. To this end, we can use the FEValues::get_function_values
1815 * (previously already used in @ref step_9 "step-9", @ref step_14 "step-14" and @ref step_15 "step-15"), a function
1816 * that takes a solution vector and returns a list of function values at
1817 * the quadrature points of the present cell. In fact, it returns the
1818 * complete vector-valued solution at each quadrature point, i.e. not only
1819 * the saturation but also the velocities and pressure.
1823 * Then we are ready to loop over the quadrature points on the cell to do
1824 * the integration. The formula for this follows in a straightforward way
1825 * from what has been discussed in the introduction.
1829 * Once this is done, we start the loop over the rows and columns of the
1830 * local matrix and feed the matrix with the relevant products.
1834 * The last step in the loop over all cells is to enter the local
1835 * contributions into the global matrix and vector structures to the
1836 * positions specified in local_dof_indices. Again, we let the
1837 * AffineConstraints class do the insertion of the cell matrix
1838 * elements to the global matrix, which already condenses the hanging node
1842 * auto cell = darcy_dof_handler.begin_active();
1843 * const auto endc = darcy_dof_handler.end();
1844 * auto saturation_cell = saturation_dof_handler.begin_active();
1846 * for (; cell != endc; ++cell, ++saturation_cell)
1848 * darcy_fe_values.reinit(cell);
1849 * saturation_fe_values.reinit(saturation_cell);
1854 * saturation_fe_values.get_function_values(old_saturation_solution,
1855 * old_saturation_values);
1857 * pressure_right_hand_side.value_list(
1858 * darcy_fe_values.get_quadrature_points(), pressure_rhs_values);
1859 * k_inverse.value_list(darcy_fe_values.get_quadrature_points(),
1860 * k_inverse_values);
1862 * for (unsigned int q = 0; q < n_q_points; ++q)
1864 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
1866 * phi_u[k] = darcy_fe_values[velocities].value(k, q);
1867 * div_phi_u[k] = darcy_fe_values[velocities].divergence(k, q);
1868 * phi_p[k] = darcy_fe_values[pressure].value(k, q);
1870 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1872 * const double old_s = old_saturation_values[q];
1873 * for (unsigned int j = 0; j <= i; ++j)
1875 * local_matrix(i, j) +=
1876 * (phi_u[i] * k_inverse_values[q] *
1877 * mobility_inverse(old_s, viscosity) * phi_u[j] -
1878 * div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
1879 * darcy_fe_values.JxW(q);
1883 * (-phi_p[i] * pressure_rhs_values[q]) * darcy_fe_values.JxW(q);
1887 * for (const auto &face : cell->face_iterators())
1888 * if (face->at_boundary())
1890 * darcy_fe_face_values.reinit(cell, face);
1892 * pressure_boundary_values.value_list(
1893 * darcy_fe_face_values.get_quadrature_points(), boundary_values);
1895 * for (unsigned int q = 0; q < n_face_q_points; ++q)
1896 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1898 * const Tensor<1, dim> phi_i_u =
1899 * darcy_fe_face_values[velocities].value(i, q);
1902 * -(phi_i_u * darcy_fe_face_values.normal_vector(q) *
1903 * boundary_values[q] * darcy_fe_face_values.JxW(q));
1907 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1908 * for (unsigned int j = i + 1; j < dofs_per_cell; ++j)
1909 * local_matrix(i, j) = local_matrix(j, i);
1911 * cell->get_dof_indices(local_dof_indices);
1913 * darcy_constraints.distribute_local_to_global(
1914 * local_matrix, local_rhs, local_dof_indices, darcy_matrix, darcy_rhs);
1922 * <a name="TwoPhaseFlowProblemdimassemble_saturation_system"></a>
1923 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_system</h4>
1927 * This function is to assemble the linear system for the saturation
1928 * transport equation. It calls, if necessary, two other member functions:
1929 * assemble_saturation_matrix() and assemble_saturation_rhs(). The former
1930 * function then assembles the saturation matrix that only needs to be
1931 * changed occasionally. On the other hand, the latter function that
1932 * assembles the right hand side must be called at every saturation time
1936 * template <int dim>
1937 * void TwoPhaseFlowProblem<dim>::assemble_saturation_system()
1939 * if (rebuild_saturation_matrix == true)
1941 * saturation_matrix = 0;
1942 * assemble_saturation_matrix();
1945 * saturation_rhs = 0;
1946 * assemble_saturation_rhs();
1954 * <a name="TwoPhaseFlowProblemdimassemble_saturation_matrix"></a>
1955 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_matrix</h4>
1959 * This function is easily understood since it only forms a simple mass
1960 * matrix for the left hand side of the saturation linear system by basis
1961 * functions phi_i_s and phi_j_s only. Finally, as usual, we enter the local
1962 * contribution into the global matrix by specifying the position in
1963 * local_dof_indices. This is done by letting the AffineConstraints class do
1964 * the insertion of the cell matrix elements to the global matrix, which
1965 * already condenses the hanging node constraints.
1968 * template <int dim>
1969 * void TwoPhaseFlowProblem<dim>::assemble_saturation_matrix()
1971 * QGauss<dim> quadrature_formula(saturation_degree + 2);
1973 * FEValues<dim> saturation_fe_values(saturation_fe,
1974 * quadrature_formula,
1975 * update_values | update_JxW_values);
1977 * const unsigned int dofs_per_cell = saturation_fe.n_dofs_per_cell();
1979 * const unsigned int n_q_points = quadrature_formula.size();
1981 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
1982 * Vector<double> local_rhs(dofs_per_cell);
1984 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
1986 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
1988 * saturation_fe_values.reinit(cell);
1992 * for (unsigned int q = 0; q < n_q_points; ++q)
1993 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1995 * const double phi_i_s = saturation_fe_values.shape_value(i, q);
1996 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1998 * const double phi_j_s = saturation_fe_values.shape_value(j, q);
1999 * local_matrix(i, j) +=
2000 * porosity * phi_i_s * phi_j_s * saturation_fe_values.JxW(q);
2003 * cell->get_dof_indices(local_dof_indices);
2005 * saturation_constraints.distribute_local_to_global(local_matrix,
2006 * local_dof_indices,
2007 * saturation_matrix);
2016 * <a name="TwoPhaseFlowProblemdimassemble_saturation_rhs"></a>
2017 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs</h4>
2021 * This function is to assemble the right hand side of the saturation
2022 * transport equation. Before going about it, we have to create two FEValues
2023 * objects for the Darcy and saturation systems respectively and, in
2024 * addition, two FEFaceValues objects for the two systems because we have a
2025 * boundary integral term in the weak form of saturation equation. For the
2026 * FEFaceValues object of the saturation system, we also require normal
2027 * vectors, which we request using the update_normal_vectors flag.
2031 * Next, before looping over all the cells, we have to compute some
2032 * parameters (e.g. global_u_infty, global_S_variation, and
2033 * global_Omega_diameter) that the artificial viscosity @f$\nu@f$ needs. This is
2034 * largely the same as was done in @ref step_31 "step-31", so you may see there for more
2039 * The real works starts with the loop over all the saturation and Darcy
2040 * cells to put the local contributions into the global vector. In this
2041 * loop, in order to simplify the implementation, we split some of the work
2042 * into two helper functions: assemble_saturation_rhs_cell_term and
2043 * assemble_saturation_rhs_boundary_term. We note that we insert cell or
2044 * boundary contributions into the global vector in the two functions rather
2045 * than in this present function.
2048 * template <int dim>
2049 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs()
2051 * QGauss<dim> quadrature_formula(saturation_degree + 2);
2052 * QGauss<dim - 1> face_quadrature_formula(saturation_degree + 2);
2054 * FEValues<dim> saturation_fe_values(saturation_fe,
2055 * quadrature_formula,
2056 * update_values | update_gradients |
2057 * update_quadrature_points |
2058 * update_JxW_values);
2059 * FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2060 * FEFaceValues<dim> saturation_fe_face_values(saturation_fe,
2061 * face_quadrature_formula,
2063 * update_normal_vectors |
2064 * update_quadrature_points |
2065 * update_JxW_values);
2066 * FEFaceValues<dim> darcy_fe_face_values(darcy_fe,
2067 * face_quadrature_formula,
2069 * FEFaceValues<dim> saturation_fe_face_values_neighbor(
2070 * saturation_fe, face_quadrature_formula, update_values);
2072 * const unsigned int dofs_per_cell =
2073 * saturation_dof_handler.get_fe().n_dofs_per_cell();
2074 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2076 * const double global_max_u_F_prime = get_max_u_F_prime();
2077 * const std::pair<double, double> global_S_range =
2078 * get_extrapolated_saturation_range();
2079 * const double global_S_variation =
2080 * global_S_range.second - global_S_range.first;
2082 * auto cell = saturation_dof_handler.begin_active();
2083 * const auto endc = saturation_dof_handler.end();
2084 * auto darcy_cell = darcy_dof_handler.begin_active();
2085 * for (; cell != endc; ++cell, ++darcy_cell)
2087 * saturation_fe_values.reinit(cell);
2088 * darcy_fe_values.reinit(darcy_cell);
2090 * cell->get_dof_indices(local_dof_indices);
2092 * assemble_saturation_rhs_cell_term(saturation_fe_values,
2094 * global_max_u_F_prime,
2095 * global_S_variation,
2096 * local_dof_indices);
2098 * for (const auto &face : cell->face_iterators())
2099 * if (face->at_boundary())
2101 * darcy_fe_face_values.reinit(darcy_cell, face);
2102 * saturation_fe_face_values.reinit(cell, face);
2103 * assemble_saturation_rhs_boundary_term(saturation_fe_face_values,
2104 * darcy_fe_face_values,
2105 * local_dof_indices);
2115 * <a name="TwoPhaseFlowProblemdimassemble_saturation_rhs_cell_term"></a>
2116 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term</h4>
2120 * This function takes care of integrating the cell terms of the right hand
2121 * side of the saturation equation, and then assembling it into the global
2122 * right hand side vector. Given the discussion in the introduction, the
2123 * form of these contributions is clear. The only tricky part is getting the
2124 * artificial viscosity and all that is necessary to compute it. The first
2125 * half of the function is devoted to this task.
2129 * The last part of the function is copying the local contributions into the
2130 * global vector with position specified in local_dof_indices.
2133 * template <int dim>
2134 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_cell_term(
2135 * const FEValues<dim> & saturation_fe_values,
2136 * const FEValues<dim> & darcy_fe_values,
2137 * const double global_max_u_F_prime,
2138 * const double global_S_variation,
2139 * const std::vector<types::global_dof_index> &local_dof_indices)
2141 * const unsigned int dofs_per_cell = saturation_fe_values.dofs_per_cell;
2142 * const unsigned int n_q_points = saturation_fe_values.n_quadrature_points;
2144 * std::vector<double> old_saturation_solution_values(n_q_points);
2145 * std::vector<double> old_old_saturation_solution_values(n_q_points);
2146 * std::vector<Tensor<1, dim>> old_grad_saturation_solution_values(n_q_points);
2147 * std::vector<Tensor<1, dim>> old_old_grad_saturation_solution_values(
2149 * std::vector<Vector<double>> present_darcy_solution_values(
2150 * n_q_points, Vector<double>(dim + 1));
2152 * saturation_fe_values.get_function_values(old_saturation_solution,
2153 * old_saturation_solution_values);
2154 * saturation_fe_values.get_function_values(
2155 * old_old_saturation_solution, old_old_saturation_solution_values);
2156 * saturation_fe_values.get_function_gradients(
2157 * old_saturation_solution, old_grad_saturation_solution_values);
2158 * saturation_fe_values.get_function_gradients(
2159 * old_old_saturation_solution, old_old_grad_saturation_solution_values);
2160 * darcy_fe_values.get_function_values(darcy_solution,
2161 * present_darcy_solution_values);
2164 * compute_viscosity(old_saturation_solution_values,
2165 * old_old_saturation_solution_values,
2166 * old_grad_saturation_solution_values,
2167 * old_old_grad_saturation_solution_values,
2168 * present_darcy_solution_values,
2169 * global_max_u_F_prime,
2170 * global_S_variation,
2171 * saturation_fe_values.get_cell()->diameter());
2173 * Vector<double> local_rhs(dofs_per_cell);
2175 * for (unsigned int q = 0; q < n_q_points; ++q)
2176 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2178 * const double old_s = old_saturation_solution_values[q];
2179 * Tensor<1, dim> present_u;
2180 * for (unsigned int d = 0; d < dim; ++d)
2181 * present_u[d] = present_darcy_solution_values[q](d);
2183 * const double phi_i_s = saturation_fe_values.shape_value(i, q);
2184 * const Tensor<1, dim> grad_phi_i_s =
2185 * saturation_fe_values.shape_grad(i, q);
2188 * (time_step * fractional_flow(old_s, viscosity) * present_u *
2190 * time_step * nu * old_grad_saturation_solution_values[q] *
2192 * porosity * old_s * phi_i_s) *
2193 * saturation_fe_values.JxW(q);
2196 * saturation_constraints.distribute_local_to_global(local_rhs,
2197 * local_dof_indices,
2205 * <a name="TwoPhaseFlowProblemdimassemble_saturation_rhs_boundary_term"></a>
2206 * <h4>TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term</h4>
2210 * The next function is responsible for the boundary integral terms in the
2211 * right hand side form of the saturation equation. For these, we have to
2212 * compute the upwinding flux on the global boundary faces, i.e. we impose
2213 * Dirichlet boundary conditions weakly only on inflow parts of the global
2214 * boundary. As before, this has been described in @ref step_21 "step-21" so we refrain
2215 * from giving more descriptions about that.
2218 * template <int dim>
2219 * void TwoPhaseFlowProblem<dim>::assemble_saturation_rhs_boundary_term(
2220 * const FEFaceValues<dim> & saturation_fe_face_values,
2221 * const FEFaceValues<dim> & darcy_fe_face_values,
2222 * const std::vector<types::global_dof_index> &local_dof_indices)
2224 * const unsigned int dofs_per_cell = saturation_fe_face_values.dofs_per_cell;
2225 * const unsigned int n_face_q_points =
2226 * saturation_fe_face_values.n_quadrature_points;
2228 * Vector<double> local_rhs(dofs_per_cell);
2230 * std::vector<double> old_saturation_solution_values_face(n_face_q_points);
2231 * std::vector<Vector<double>> present_darcy_solution_values_face(
2232 * n_face_q_points, Vector<double>(dim + 1));
2233 * std::vector<double> neighbor_saturation(n_face_q_points);
2235 * saturation_fe_face_values.get_function_values(
2236 * old_saturation_solution, old_saturation_solution_values_face);
2237 * darcy_fe_face_values.get_function_values(
2238 * darcy_solution, present_darcy_solution_values_face);
2240 * SaturationBoundaryValues<dim> saturation_boundary_values;
2241 * saturation_boundary_values.value_list(
2242 * saturation_fe_face_values.get_quadrature_points(), neighbor_saturation);
2244 * for (unsigned int q = 0; q < n_face_q_points; ++q)
2246 * Tensor<1, dim> present_u_face;
2247 * for (unsigned int d = 0; d < dim; ++d)
2248 * present_u_face[d] = present_darcy_solution_values_face[q](d);
2250 * const double normal_flux =
2251 * present_u_face * saturation_fe_face_values.normal_vector(q);
2253 * const bool is_outflow_q_point = (normal_flux >= 0);
2255 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2257 * time_step * normal_flux *
2258 * fractional_flow((is_outflow_q_point == true ?
2259 * old_saturation_solution_values_face[q] :
2260 * neighbor_saturation[q]),
2262 * saturation_fe_face_values.shape_value(i, q) *
2263 * saturation_fe_face_values.JxW(q);
2265 * saturation_constraints.distribute_local_to_global(local_rhs,
2266 * local_dof_indices,
2274 * <a name="TwoPhaseFlowProblemdimsolve"></a>
2275 * <h3>TwoPhaseFlowProblem<dim>::solve</h3>
2279 * This function implements the operator splitting algorithm, i.e. in each
2280 * time step it either re-computes the solution of the Darcy system or
2281 * extrapolates velocity/pressure from previous time steps, then determines
2282 * the size of the time step, and then updates the saturation variable. The
2283 * implementation largely follows similar code in @ref step_31 "step-31". It is, next to
2284 * the run() function, the central one in this program.
2288 * At the beginning of the function, we ask whether to solve the
2289 * pressure-velocity part by evaluating the a posteriori criterion (see the
2290 * following function). If necessary, we will solve the pressure-velocity
2291 * part using the GMRES solver with the Schur complement block
2292 * preconditioner as is described in the introduction.
2295 * template <int dim>
2296 * void TwoPhaseFlowProblem<dim>::solve()
2298 * const bool solve_for_pressure_and_velocity =
2299 * determine_whether_to_solve_for_pressure_and_velocity();
2301 * if (solve_for_pressure_and_velocity == true)
2303 * std::cout << " Solving Darcy (pressure-velocity) system..."
2306 * assemble_darcy_system();
2307 * build_darcy_preconditioner();
2310 * const LinearSolvers::InverseMatrix<TrilinosWrappers::SparseMatrix,
2311 * TrilinosWrappers::PreconditionIC>
2312 * mp_inverse(darcy_preconditioner_matrix.block(1, 1),
2313 * *bottom_right_preconditioner);
2315 * const LinearSolvers::BlockSchurPreconditioner<
2316 * TrilinosWrappers::PreconditionIC,
2317 * TrilinosWrappers::PreconditionIC>
2318 * preconditioner(darcy_matrix, mp_inverse, *top_left_preconditioner);
2320 * SolverControl solver_control(darcy_matrix.m(),
2321 * 1e-16 * darcy_rhs.l2_norm());
2323 * SolverGMRES<TrilinosWrappers::MPI::BlockVector> gmres(
2325 * SolverGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(
2328 * for (unsigned int i = 0; i < darcy_solution.size(); ++i)
2329 * if (darcy_constraints.is_constrained(i))
2330 * darcy_solution(i) = 0;
2332 * gmres.solve(darcy_matrix, darcy_solution, darcy_rhs, preconditioner);
2334 * darcy_constraints.distribute(darcy_solution);
2336 * std::cout << " ..." << solver_control.last_step()
2337 * << " GMRES iterations." << std::endl;
2341 * second_last_computed_darcy_solution = last_computed_darcy_solution;
2342 * last_computed_darcy_solution = darcy_solution;
2344 * saturation_matching_last_computed_darcy_solution =
2345 * saturation_solution;
2350 * On the other hand, if we have decided that we don't want to compute the
2351 * solution of the Darcy system
for the current time step, then we need to
2352 * simply
extrapolate the previous two Darcy solutions to the same time as
2353 * we would have computed the velocity/pressure at. We
do a simple linear
2354 * extrapolation, i.e. given the current length @f$dt@f$ of the macro time
2355 * step from the time when we last computed the Darcy solution to now
2356 * (given by <code>current_macro_time_step</code>), and @f$DT@f$ the length of
2357 * the last macro time step (given by <code>old_macro_time_step</code>),
2358 * then we get @f$u^\ast = u_p + dt \frac{u_p-u_{pp}}{DT} = (1+dt/DT)u_p -
2359 * dt/DT u_{pp}@f$, where @f$u_p@f$ and @f$u_{pp}@f$ are the last two computed Darcy
2360 * solutions. We can implement
this formula
using just two lines of code.
2364 * Note that the algorithm here only works
if we have at least two
2365 * previously computed Darcy solutions from which we can
extrapolate to
2366 * the current time, and
this is ensured by requiring re-computation of
2367 * the Darcy solution
for the
first 2 time steps.
2372 * darcy_solution = last_computed_darcy_solution;
2373 * darcy_solution.sadd(1 + current_macro_time_step / old_macro_time_step,
2374 * -current_macro_time_step / old_macro_time_step,
2375 * second_last_computed_darcy_solution);
2381 * With the so computed velocity vector, compute the optimal time step
2382 * based on the CFL criterion discussed in the introduction...
2386 * old_time_step = time_step;
2388 *
const double max_u_F_prime = get_max_u_F_prime();
2389 *
if (max_u_F_prime > 0)
2391 * saturation_degree / max_u_F_prime / 50;
2393 * time_step = end_time - time;
2400 * ...and then also update the length of the macro time steps we use
while
2401 * we
're dealing with time step sizes. In particular, this involves: (i)
2402 * If we have just recomputed the Darcy solution, then the length of the
2403 * previous macro time step is now fixed and the length of the current
2404 * macro time step is, up to now, simply the length of the current (micro)
2405 * time step. (ii) If we have not recomputed the Darcy solution, then the
2406 * length of the current macro time step has just grown by
2407 * <code>time_step</code>.
2410 * if (solve_for_pressure_and_velocity == true)
2412 * old_macro_time_step = current_macro_time_step;
2413 * current_macro_time_step = time_step;
2416 * current_macro_time_step += time_step;
2420 * The last step in this function is to recompute the saturation solution
2421 * based on the velocity field we've just obtained. This naturally happens
2422 * in every time step, and we don
't skip any of these computations. At the
2423 * end of computing the saturation, we project back into the allowed
2424 * interval @f$[0,1]@f$ to make sure our solution remains physical.
2428 * std::cout << " Solving saturation transport equation..." << std::endl;
2430 * assemble_saturation_system();
2432 * SolverControl solver_control(saturation_matrix.m(),
2433 * 1e-16 * saturation_rhs.l2_norm());
2434 * SolverCG<TrilinosWrappers::MPI::Vector> cg(solver_control);
2436 * TrilinosWrappers::PreconditionIC preconditioner;
2437 * preconditioner.initialize(saturation_matrix);
2439 * cg.solve(saturation_matrix,
2440 * saturation_solution,
2444 * saturation_constraints.distribute(saturation_solution);
2445 * project_back_saturation();
2447 * std::cout << " ..." << solver_control.last_step()
2448 * << " CG iterations." << std::endl;
2456 * <a name="TwoPhaseFlowProblemdimrefine_mesh"></a>
2457 * <h3>TwoPhaseFlowProblem<dim>::refine_mesh</h3>
2461 * The next function does the refinement and coarsening of the mesh. It does
2462 * its work in three blocks: (i) Compute refinement indicators by looking at
2463 * the gradient of a solution vector extrapolated linearly from the previous
2464 * two using the respective sizes of the time step (or taking the only
2465 * solution we have if this is the first time step). (ii) Flagging those
2466 * cells for refinement and coarsening where the gradient is larger or
2467 * smaller than a certain threshold, preserving minimal and maximal levels
2468 * of mesh refinement. (iii) Transferring the solution from the old to the
2469 * new mesh. None of this is particularly difficult.
2472 * template <int dim>
2473 * void TwoPhaseFlowProblem<dim>::refine_mesh(const unsigned int min_grid_level,
2474 * const unsigned int max_grid_level)
2476 * Vector<double> refinement_indicators(triangulation.n_active_cells());
2478 * const QMidpoint<dim> quadrature_formula;
2479 * FEValues<dim> fe_values(saturation_fe,
2480 * quadrature_formula,
2481 * update_gradients);
2482 * std::vector<Tensor<1, dim>> grad_saturation(1);
2484 * TrilinosWrappers::MPI::Vector extrapolated_saturation_solution(
2485 * saturation_solution);
2486 * if (timestep_number != 0)
2487 * extrapolated_saturation_solution.sadd((1. + time_step / old_time_step),
2488 * time_step / old_time_step,
2489 * old_saturation_solution);
2491 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2493 * const unsigned int cell_no = cell->active_cell_index();
2494 * fe_values.reinit(cell);
2495 * fe_values.get_function_gradients(extrapolated_saturation_solution,
2498 * refinement_indicators(cell_no) = grad_saturation[0].norm();
2503 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2505 * const unsigned int cell_no = cell->active_cell_index();
2506 * cell->clear_coarsen_flag();
2507 * cell->clear_refine_flag();
2509 * if ((static_cast<unsigned int>(cell->level()) < max_grid_level) &&
2510 * (std::fabs(refinement_indicators(cell_no)) >
2511 * saturation_refinement_threshold))
2512 * cell->set_refine_flag();
2513 * else if ((static_cast<unsigned int>(cell->level()) >
2514 * min_grid_level) &&
2515 * (std::fabs(refinement_indicators(cell_no)) <
2516 * 0.5 * saturation_refinement_threshold))
2517 * cell->set_coarsen_flag();
2521 * triangulation.prepare_coarsening_and_refinement();
2524 * std::vector<TrilinosWrappers::MPI::Vector> x_saturation(3);
2525 * x_saturation[0] = saturation_solution;
2526 * x_saturation[1] = old_saturation_solution;
2527 * x_saturation[2] = saturation_matching_last_computed_darcy_solution;
2529 * std::vector<TrilinosWrappers::MPI::BlockVector> x_darcy(2);
2530 * x_darcy[0] = last_computed_darcy_solution;
2531 * x_darcy[1] = second_last_computed_darcy_solution;
2533 * SolutionTransfer<dim, TrilinosWrappers::MPI::Vector> saturation_soltrans(
2534 * saturation_dof_handler);
2536 * SolutionTransfer<dim, TrilinosWrappers::MPI::BlockVector> darcy_soltrans(
2537 * darcy_dof_handler);
2540 * triangulation.prepare_coarsening_and_refinement();
2541 * saturation_soltrans.prepare_for_coarsening_and_refinement(x_saturation);
2543 * darcy_soltrans.prepare_for_coarsening_and_refinement(x_darcy);
2545 * triangulation.execute_coarsening_and_refinement();
2548 * std::vector<TrilinosWrappers::MPI::Vector> tmp_saturation(3);
2549 * tmp_saturation[0].reinit(saturation_solution);
2550 * tmp_saturation[1].reinit(saturation_solution);
2551 * tmp_saturation[2].reinit(saturation_solution);
2552 * saturation_soltrans.interpolate(x_saturation, tmp_saturation);
2554 * saturation_solution = tmp_saturation[0];
2555 * old_saturation_solution = tmp_saturation[1];
2556 * saturation_matching_last_computed_darcy_solution = tmp_saturation[2];
2558 * saturation_constraints.distribute(saturation_solution);
2559 * saturation_constraints.distribute(old_saturation_solution);
2560 * saturation_constraints.distribute(
2561 * saturation_matching_last_computed_darcy_solution);
2563 * std::vector<TrilinosWrappers::MPI::BlockVector> tmp_darcy(2);
2564 * tmp_darcy[0].reinit(darcy_solution);
2565 * tmp_darcy[1].reinit(darcy_solution);
2566 * darcy_soltrans.interpolate(x_darcy, tmp_darcy);
2568 * last_computed_darcy_solution = tmp_darcy[0];
2569 * second_last_computed_darcy_solution = tmp_darcy[1];
2571 * darcy_constraints.distribute(last_computed_darcy_solution);
2572 * darcy_constraints.distribute(second_last_computed_darcy_solution);
2574 * rebuild_saturation_matrix = true;
2583 * <a name="TwoPhaseFlowProblemdimoutput_results"></a>
2584 * <h3>TwoPhaseFlowProblem<dim>::output_results</h3>
2588 * This function generates graphical output. It is in essence a copy of the
2589 * implementation in @ref step_31 "step-31".
2592 * template <int dim>
2593 * void TwoPhaseFlowProblem<dim>::output_results() const
2595 * const FESystem<dim> joint_fe(darcy_fe, 1, saturation_fe, 1);
2596 * DoFHandler<dim> joint_dof_handler(triangulation);
2597 * joint_dof_handler.distribute_dofs(joint_fe);
2598 * Assert(joint_dof_handler.n_dofs() ==
2599 * darcy_dof_handler.n_dofs() + saturation_dof_handler.n_dofs(),
2600 * ExcInternalError());
2602 * Vector<double> joint_solution(joint_dof_handler.n_dofs());
2605 * std::vector<types::global_dof_index> local_joint_dof_indices(
2606 * joint_fe.n_dofs_per_cell());
2607 * std::vector<types::global_dof_index> local_darcy_dof_indices(
2608 * darcy_fe.n_dofs_per_cell());
2609 * std::vector<types::global_dof_index> local_saturation_dof_indices(
2610 * saturation_fe.n_dofs_per_cell());
2612 * auto joint_cell = joint_dof_handler.begin_active();
2613 * const auto joint_endc = joint_dof_handler.end();
2614 * auto darcy_cell = darcy_dof_handler.begin_active();
2615 * auto saturation_cell = saturation_dof_handler.begin_active();
2617 * for (; joint_cell != joint_endc;
2618 * ++joint_cell, ++darcy_cell, ++saturation_cell)
2620 * joint_cell->get_dof_indices(local_joint_dof_indices);
2621 * darcy_cell->get_dof_indices(local_darcy_dof_indices);
2622 * saturation_cell->get_dof_indices(local_saturation_dof_indices);
2624 * for (unsigned int i = 0; i < joint_fe.n_dofs_per_cell(); ++i)
2625 * if (joint_fe.system_to_base_index(i).first.first == 0)
2627 * Assert(joint_fe.system_to_base_index(i).second <
2628 * local_darcy_dof_indices.size(),
2629 * ExcInternalError());
2630 * joint_solution(local_joint_dof_indices[i]) = darcy_solution(
2631 * local_darcy_dof_indices[joint_fe.system_to_base_index(i)
2636 * Assert(joint_fe.system_to_base_index(i).first.first == 1,
2637 * ExcInternalError());
2638 * Assert(joint_fe.system_to_base_index(i).second <
2639 * local_darcy_dof_indices.size(),
2640 * ExcInternalError());
2641 * joint_solution(local_joint_dof_indices[i]) =
2642 * saturation_solution(
2643 * local_saturation_dof_indices
2644 * [joint_fe.system_to_base_index(i).second]);
2648 * std::vector<std::string> joint_solution_names(dim, "velocity");
2649 * joint_solution_names.emplace_back("pressure");
2650 * joint_solution_names.emplace_back("saturation");
2652 * std::vector<DataComponentInterpretation::DataComponentInterpretation>
2653 * data_component_interpretation(
2654 * dim, DataComponentInterpretation::component_is_part_of_vector);
2655 * data_component_interpretation.push_back(
2656 * DataComponentInterpretation::component_is_scalar);
2657 * data_component_interpretation.push_back(
2658 * DataComponentInterpretation::component_is_scalar);
2660 * DataOut<dim> data_out;
2662 * data_out.attach_dof_handler(joint_dof_handler);
2663 * data_out.add_data_vector(joint_solution,
2664 * joint_solution_names,
2665 * DataOut<dim>::type_dof_data,
2666 * data_component_interpretation);
2668 * data_out.build_patches();
2670 * std::string filename =
2671 * "solution-" + Utilities::int_to_string(timestep_number, 5) + ".vtu";
2672 * std::ofstream output(filename);
2673 * data_out.write_vtu(output);
2681 * <a name="Toolfunctions"></a>
2682 * <h3>Tool functions</h3>
2687 * <a name="TwoPhaseFlowProblemdimdetermine_whether_to_solve_for_pressure_and_velocity"></a>
2688 * <h4>TwoPhaseFlowProblem<dim>::determine_whether_to_solve_for_pressure_and_velocity</h4>
2692 * This function implements the a posteriori criterion for adaptive operator
2693 * splitting. The function is relatively straightforward given the way we
2694 * have implemented other functions above and given the formula for the
2695 * criterion derived in the paper.
2699 * If one decides that one wants the original IMPES method in which the
2700 * Darcy equation is solved in every time step, then this can be achieved by
2701 * setting the threshold value <code>AOS_threshold</code> (with a default of
2702 * @f$5.0@f$) to zero, thereby forcing the function to always return true.
2706 * Finally, note that the function returns true unconditionally for the
2707 * first two time steps to ensure that we have always solved the Darcy
2708 * system at least twice when skipping its solution, thereby allowing us to
2709 * extrapolate the velocity from the last two solutions in
2710 * <code>solve()</code>.
2713 * template <int dim>
2714 * bool TwoPhaseFlowProblem<
2715 * dim>::determine_whether_to_solve_for_pressure_and_velocity() const
2717 * if (timestep_number <= 2)
2720 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
2721 * const unsigned int n_q_points = quadrature_formula.size();
2723 * FEValues<dim> fe_values(saturation_fe,
2724 * quadrature_formula,
2725 * update_values | update_quadrature_points);
2727 * std::vector<double> old_saturation_after_solving_pressure(n_q_points);
2728 * std::vector<double> present_saturation(n_q_points);
2730 * std::vector<Tensor<2, dim>> k_inverse_values(n_q_points);
2732 * double max_global_aop_indicator = 0.0;
2734 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2736 * double max_local_mobility_reciprocal_difference = 0.0;
2737 * double max_local_permeability_inverse_l1_norm = 0.0;
2739 * fe_values.reinit(cell);
2740 * fe_values.get_function_values(
2741 * saturation_matching_last_computed_darcy_solution,
2742 * old_saturation_after_solving_pressure);
2743 * fe_values.get_function_values(saturation_solution, present_saturation);
2745 * k_inverse.value_list(fe_values.get_quadrature_points(),
2746 * k_inverse_values);
2748 * for (unsigned int q = 0; q < n_q_points; ++q)
2750 * const double mobility_reciprocal_difference = std::fabs(
2751 * mobility_inverse(present_saturation[q], viscosity) -
2752 * mobility_inverse(old_saturation_after_solving_pressure[q],
2755 * max_local_mobility_reciprocal_difference =
2756 * std::max(max_local_mobility_reciprocal_difference,
2757 * mobility_reciprocal_difference);
2759 * max_local_permeability_inverse_l1_norm =
2760 * std::max(max_local_permeability_inverse_l1_norm,
2761 * l1_norm(k_inverse_values[q]));
2764 * max_global_aop_indicator =
2765 * std::max(max_global_aop_indicator,
2766 * (max_local_mobility_reciprocal_difference *
2767 * max_local_permeability_inverse_l1_norm));
2770 * return (max_global_aop_indicator > AOS_threshold);
2778 * <a name="TwoPhaseFlowProblemdimproject_back_saturation"></a>
2779 * <h4>TwoPhaseFlowProblem<dim>::project_back_saturation</h4>
2783 * The next function simply makes sure that the saturation values always
2784 * remain within the physically reasonable range of @f$[0,1]@f$. While the
2785 * continuous equations guarantee that this is so, the discrete equations
2786 * don't. However,
if we allow the discrete solution to
escape this range we
2787 * get into trouble because terms like @f$F(S)@f$ and @f$F
'(S)@f$ will produce
2788 * unreasonable results (e.g. @f$F'(S)<0@f$
for @f$S<0@f$, which would imply that
2789 * the wetting fluid phase flows <i>against</i> the direction of the bulk
2790 * fluid velocity)). Consequently, at the end of each time step, we simply
2791 * project the saturation field back into the physically reasonable region.
2794 * template <int dim>
2795 *
void TwoPhaseFlowProblem<dim>::project_back_saturation()
2797 *
for (
unsigned int i = 0; i < saturation_solution.size(); ++i)
2798 *
if (saturation_solution(i) < 0.2)
2799 * saturation_solution(i) = 0.2;
2800 *
else if (saturation_solution(i) > 1)
2801 * saturation_solution(i) = 1;
2809 * <a name=
"TwoPhaseFlowProblemdimget_max_u_F_prime"></a>
2810 * <h4>TwoPhaseFlowProblem<dim>::get_max_u_F_prime</h4>
2814 * Another simpler helper function: Compute the maximum of the total
2815 * velocity times the derivative of the fraction flow function, i.e.,
2816 * compute @f$\|\mathbf{u}
F'(S)\|_{L_\infty(\Omega)}@f$. This term is used in
2817 * both the computation of the time step as well as in normalizing the
2818 * entropy-residual term in the artificial viscosity.
2821 * template <int dim>
2822 * double TwoPhaseFlowProblem<dim>::get_max_u_F_prime() const
2824 * const QGauss<dim> quadrature_formula(darcy_degree + 2);
2825 * const unsigned int n_q_points = quadrature_formula.size();
2827 * FEValues<dim> darcy_fe_values(darcy_fe, quadrature_formula, update_values);
2828 * FEValues<dim> saturation_fe_values(saturation_fe,
2829 * quadrature_formula,
2832 * std::vector<Vector<double>> darcy_solution_values(n_q_points,
2833 * Vector<double>(dim + 1));
2834 * std::vector<double> saturation_values(n_q_points);
2836 * double max_velocity_times_dF_dS = 0;
2838 * auto cell = darcy_dof_handler.begin_active();
2839 * const auto endc = darcy_dof_handler.end();
2840 * auto saturation_cell = saturation_dof_handler.begin_active();
2841 * for (; cell != endc; ++cell, ++saturation_cell)
2843 * darcy_fe_values.reinit(cell);
2844 * saturation_fe_values.reinit(saturation_cell);
2846 * darcy_fe_values.get_function_values(darcy_solution,
2847 * darcy_solution_values);
2848 * saturation_fe_values.get_function_values(old_saturation_solution,
2849 * saturation_values);
2851 * for (unsigned int q = 0; q < n_q_points; ++q)
2853 * Tensor<1, dim> velocity;
2854 * for (unsigned int i = 0; i < dim; ++i)
2855 * velocity[i] = darcy_solution_values[q](i);
2857 * const double dF_dS =
2858 * fractional_flow_derivative(saturation_values[q], viscosity);
2860 * max_velocity_times_dF_dS =
2861 * std::max(max_velocity_times_dF_dS, velocity.norm() * dF_dS);
2865 * return max_velocity_times_dF_dS;
2872 * <a name="TwoPhaseFlowProblemdimget_extrapolated_saturation_range"></a>
2873 * <h4>TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range</h4>
2877 * For computing the stabilization term, we need to know the range of the
2878 * saturation variable. Unlike in @ref step_31 "step-31", this range is trivially bounded
2879 * by the interval @f$[0,1]@f$ but we can do a bit better by looping over a
2880 * collection of quadrature points and seeing what the values are there. If
2881 * we can, i.e., if there are at least two timesteps around, we can even
2882 * take the values extrapolated to the next time step.
2886 * As before, the function is taken with minimal modifications from @ref step_31 "step-31".
2889 * template <int dim>
2890 * std::pair<double, double>
2891 * TwoPhaseFlowProblem<dim>::get_extrapolated_saturation_range() const
2893 * const QGauss<dim> quadrature_formula(saturation_degree + 2);
2894 * const unsigned int n_q_points = quadrature_formula.size();
2896 * FEValues<dim> fe_values(saturation_fe, quadrature_formula, update_values);
2897 * std::vector<double> old_saturation_values(n_q_points);
2898 * std::vector<double> old_old_saturation_values(n_q_points);
2900 * if (timestep_number != 0)
2902 * double min_saturation = std::numeric_limits<double>::max(),
2903 * max_saturation = -std::numeric_limits<double>::max();
2905 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2907 * fe_values.reinit(cell);
2908 * fe_values.get_function_values(old_saturation_solution,
2909 * old_saturation_values);
2910 * fe_values.get_function_values(old_old_saturation_solution,
2911 * old_old_saturation_values);
2913 * for (unsigned int q = 0; q < n_q_points; ++q)
2915 * const double saturation =
2916 * (1. + time_step / old_time_step) * old_saturation_values[q] -
2917 * time_step / old_time_step * old_old_saturation_values[q];
2919 * min_saturation = std::min(min_saturation, saturation);
2920 * max_saturation = std::max(max_saturation, saturation);
2924 * return std::make_pair(min_saturation, max_saturation);
2928 * double min_saturation = std::numeric_limits<double>::max(),
2929 * max_saturation = -std::numeric_limits<double>::max();
2931 * for (const auto &cell : saturation_dof_handler.active_cell_iterators())
2933 * fe_values.reinit(cell);
2934 * fe_values.get_function_values(old_saturation_solution,
2935 * old_saturation_values);
2937 * for (unsigned int q = 0; q < n_q_points; ++q)
2939 * const double saturation = old_saturation_values[q];
2941 * min_saturation = std::min(min_saturation, saturation);
2942 * max_saturation = std::max(max_saturation, saturation);
2946 * return std::make_pair(min_saturation, max_saturation);
2955 * <a name="TwoPhaseFlowProblemdimcompute_viscosity"></a>
2956 * <h4>TwoPhaseFlowProblem<dim>::compute_viscosity</h4>
2960 * The final tool function is used to compute the artificial viscosity on a
2961 * given cell. This isn't particularly complicated
if you have the formula
2962 *
for it in front of you, and looking at the implementation in @ref step_31
"step-31". The
2963 * major difference to that tutorial program is that the velocity here is
2964 * not simply @f$\mathbf u@f$ but @f$\mathbf u
F'(S)@f$ and some of the formulas
2965 * need to be adjusted accordingly.
2968 * template <int dim>
2969 * double TwoPhaseFlowProblem<dim>::compute_viscosity(
2970 * const std::vector<double> & old_saturation,
2971 * const std::vector<double> & old_old_saturation,
2972 * const std::vector<Tensor<1, dim>> &old_saturation_grads,
2973 * const std::vector<Tensor<1, dim>> &old_old_saturation_grads,
2974 * const std::vector<Vector<double>> &present_darcy_values,
2975 * const double global_max_u_F_prime,
2976 * const double global_S_variation,
2977 * const double cell_diameter) const
2979 * const double beta = .4 * dim;
2980 * const double alpha = 1;
2982 * if (global_max_u_F_prime == 0)
2983 * return 5e-3 * cell_diameter;
2985 * const unsigned int n_q_points = old_saturation.size();
2987 * double max_residual = 0;
2988 * double max_velocity_times_dF_dS = 0;
2990 * const bool use_dF_dS = true;
2992 * for (unsigned int q = 0; q < n_q_points; ++q)
2995 * for (unsigned int d = 0; d < dim; ++d)
2996 * u[d] = present_darcy_values[q](d);
2998 * const double dS_dt = porosity *
2999 * (old_saturation[q] - old_old_saturation[q]) /
3002 * const double dF_dS = fractional_flow_derivative(
3003 * (old_saturation[q] + old_old_saturation[q]) / 2.0, viscosity);
3005 * const double u_grad_S =
3006 * u * dF_dS * (old_saturation_grads[q] + old_old_saturation_grads[q]) /
3009 * const double residual =
3010 * std::abs((dS_dt + u_grad_S) *
3011 * std::pow((old_saturation[q] + old_old_saturation[q]) / 2,
3014 * max_residual = std::max(residual, max_residual);
3015 * max_velocity_times_dF_dS =
3016 * std::max(std::sqrt(u * u) * (use_dF_dS ? std::max(dF_dS, 1.) : 1),
3017 * max_velocity_times_dF_dS);
3020 * const double c_R = 1.0;
3021 * const double global_scaling = c_R * porosity *
3022 * (global_max_u_F_prime)*global_S_variation /
3023 * std::pow(global_Omega_diameter, alpha - 2.);
3026 * (max_velocity_times_dF_dS)*std::min(cell_diameter,
3027 * std::pow(cell_diameter, alpha) *
3036 * <a name="TwoPhaseFlowProblemdimrun"></a>
3037 * <h3>TwoPhaseFlowProblem<dim>::run</h3>
3041 * This function is, besides <code>solve()</code>, the primary function of
3042 * this program as it controls the time iteration as well as when the
3043 * solution is written into output files and when to do mesh refinement.
3047 * With the exception of the startup code that loops back to the beginning
3048 * of the function through the <code>goto start_time_iteration</code> label,
3049 * everything should be relatively straightforward. In any case, it mimics
3050 * the corresponding function in @ref step_31 "step-31".
3053 * template <int dim>
3054 * void TwoPhaseFlowProblem<dim>::run()
3056 * const unsigned int initial_refinement = (dim == 2 ? 5 : 2);
3057 * const unsigned int n_pre_refinement_steps = (dim == 2 ? 3 : 2);
3060 * GridGenerator::hyper_cube(triangulation, 0, 1);
3061 * triangulation.refine_global(initial_refinement);
3062 * global_Omega_diameter = GridTools::diameter(triangulation);
3066 * unsigned int pre_refinement_step = 0;
3068 * start_time_iteration:
3070 * VectorTools::project(saturation_dof_handler,
3071 * saturation_constraints,
3072 * QGauss<dim>(saturation_degree + 2),
3073 * SaturationInitialValues<dim>(),
3074 * old_saturation_solution);
3076 * time_step = old_time_step = 0;
3077 * current_macro_time_step = old_macro_time_step = 0;
3083 * std::cout << "Timestep " << timestep_number << ": t=" << time
3084 * << ", dt=" << time_step << std::endl;
3088 * std::cout << std::endl;
3090 * if (timestep_number % 200 == 0)
3093 * if (timestep_number % 25 == 0)
3094 * refine_mesh(initial_refinement,
3095 * initial_refinement + n_pre_refinement_steps);
3097 * if ((timestep_number == 0) &&
3098 * (pre_refinement_step < n_pre_refinement_steps))
3100 * ++pre_refinement_step;
3101 * goto start_time_iteration;
3104 * time += time_step;
3105 * ++timestep_number;
3107 * old_old_saturation_solution = old_saturation_solution;
3108 * old_saturation_solution = saturation_solution;
3110 * while (time <= end_time);
3112 * } // namespace Step43
3119 * <a name="Thecodemaincodefunction"></a>
3120 * <h3>The <code>main()</code> function</h3>
3124 * The main function looks almost the same as in all other programs. The need
3125 * to initialize the MPI subsystem for a program that uses Trilinos -- even
3126 * for programs that do not actually run in parallel -- is explained in
3127 * @ref step_31 "step-31".
3130 * int main(int argc, char *argv[])
3134 * using namespace dealii;
3135 * using namespace Step43;
3137 * Utilities::MPI::MPI_InitFinalize mpi_initialization(
3138 * argc, argv, numbers::invalid_unsigned_int);
3142 * This program can only be run in serial. Otherwise, throw an exception.
3145 * AssertThrow(Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD) == 1,
3147 * "This program can only be run in serial, use ./step-43"));
3149 * TwoPhaseFlowProblem<2> two_phase_flow_problem(1);
3150 * two_phase_flow_problem.run();
3152 * catch (std::exception &exc)
3154 * std::cerr << std::endl
3156 * << "----------------------------------------------------"
3158 * std::cerr << "Exception on processing: " << std::endl
3159 * << exc.what() << std::endl
3160 * << "Aborting!" << std::endl
3161 * << "----------------------------------------------------"
3168 * std::cerr << std::endl
3170 * << "----------------------------------------------------"
3172 * std::cerr << "Unknown exception!" << std::endl
3173 * << "Aborting!" << std::endl
3174 * << "----------------------------------------------------"
3182<a name="Results"></a><h1>Results</h1>
3186The output of this program is not really much different from that of
3187@ref step_21 "step-21": it solves the same problem, after all. Of more importance are
3188quantitative metrics such as the accuracy of the solution as well as
3189the time needed to compute it. These are documented in detail in the
3190two publications listed at the top of this page and we won't repeat
3193That said, no tutorial program is complete without a couple of good
3194pictures, so here is some output of a
run in 3
d:
3196<table align=
"center" class=
"tutorial" cellspacing=
"3" cellpadding=
"3">
3199 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.velocity.png" alt=
"">
3201 Velocity vectors of flow through the porous medium with
random
3202 permeability model. Streaming paths of high permeability and resulting
3203 high velocity are clearly visible.
3207 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.streamlines.png" alt=
"">
3209 Streamlines colored by the saturation along the streamline path. Blue
3210 streamlines indicate low saturations, i.e., the flow along these
3211 streamlines must be slow or
else more fluid would have been
3212 transported along them. On the other hand, green paths indicate high
3213 velocities since the fluid front has already reached further into the
3220 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.saturation.png" alt=
"">
3222 Streamlines with a
volume rendering of the saturation, showing how far
3223 the fluid front has advanced at
this time.
3227 <img src=
"https://www.dealii.org/images/steps/developer/step-43.3d.mesh.png" alt=
"">
3229 Surface of the mesh showing the adaptive refinement along the front.
3236<a name=
"extensions"></a>
3237<a name=
"Possibilitiesforextensions"></a><h3>Possibilities
for extensions</h3>
3240The primary objection one may have to
this program is that it is still too
3241slow: 3
d computations on reasonably fine meshes are simply too expensive to be
3242done routinely and with reasonably quick turn-around. This is similar to the
3243situation we were in when we wrote @ref step_31
"step-31", from which
this program has taken
3244much inspiration. The solution is similar as it was there as well: We need to
3245parallelize the program in a way similar to how we derived @ref step_32
"step-32" out of
3246@ref step_31
"step-31". In fact, all of the techniques used in @ref step_32
"step-32" would be transferable
3247to
this program as well, making the program
run on dozens or hundreds of
3248processors immediately.
3250A different direction is to make the program more relevant to many other
3251porous media applications. Specifically, one avenue is to go to the primary
3252user of porous media flow simulators, namely the oil industry. There,
3253applications in
this area are dominated by multiphase flow (i.e., more than
3254the two phases we have here), and the reactions they may have with each other
3255(or any other way phases may exchange mass, such as through dissolution in and
3256bubbling out of gas from the oil phase). Furthermore, the presence of gas
3257often leads to compressibility effects of the fluid. Jointly, these effects
3258are typically formulated in the widely-used
"black oil model". True reactions
3259between multiple phases also play a role in oil reservoir modeling when
3260considering controlled burns of oil in the reservoir to raise pressure and
3261temperature. These are much more complex problems, though, and left
for future
3264Finally, from a mathematical perspective, we have derived the
3265criterion
for re-computing the velocity/pressure solution at a given
3266time step under the assumption that we want to compare the solution we
3267would get at the current time step with that computed the last time we
3268actually solved
this system. However, in the program, whenever we did
3269not re-compute the solution, we didn
't just use the previously
3270computed solution but instead extrapolated from the previous two times
3271we solved the system. Consequently, the criterion was pessimistically
3272stated: what we should really compare is the solution we would get at
3273the current time step with the extrapolated one. Re-stating the
3274theorem in this regard is left as an exercise.
3276There are also other ways to extend the mathematical foundation of
3277this program; for example, one may say that it isn't the velocity we
3278care about, but in fact the saturation. Thus, one may ask whether the
3279criterion we use here to decide whether @f$\mathbf u@f$ needs to be
3280recomputed is appropriate; one may,
for example, suggest that it is
3281also important to decide whether (and by how much) a wrong velocity
3282field in fact affects the solution of the saturation equation. This
3283would then naturally lead to a sensitivity analysis.
3285From an algorithmic viewpoint, we have here used a criterion
for refinement
3286that is often used in engineering, namely by looking at the
gradient of
3287the solution. However,
if you inspect the solution, you will find that
3288it quickly leads to refinement almost everywhere, even in regions where it
3289is clearly not necessary: frequently used therefore does not need to imply
3290that it is a useful criterion to
begin with. On the other hand, replacing
3291this criterion by a different and better one should not be very difficult.
3293should certainly be applicable to the current problem as well.
3296<a name=
"PlainProg"></a>
3297<h1> The plain program</h1>
3298@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(ITERATOR begin, std_cxx20::type_identity_t< ITERATOR > 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())
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=ComponentMask())
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=AffineConstraints< number >(), const bool keep_constrained_dofs=true, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id)
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