Loading [MathJax]/extensions/TeX/AMSsymbols.js
 Reference documentation for deal.II version 9.3.3
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
step-31.h
Go to the documentation of this file.
1,
1270 * const unsigned int /*component*/ = 0) const override
1271 * {
1272 * return 0;
1273 * }
1274 *
1275 * virtual void vector_value(const Point<dim> &p,
1276 * Vector<double> & value) const override
1277 * {
1278 * for (unsigned int c = 0; c < this->n_components; ++c)
1279 * value(c) = TemperatureInitialValues<dim>::value(p, c);
1280 * }
1281 * };
1282 *
1283 *
1284 *
1285 * template <int dim>
1286 * class TemperatureRightHandSide : public Function<dim>
1287 * {
1288 * public:
1289 * TemperatureRightHandSide()
1290 * : Function<dim>(1)
1291 * {}
1292 *
1293 * virtual double value(const Point<dim> & p,
1294 * const unsigned int component = 0) const override
1295 * {
1296 * (void)component;
1297 * Assert(component == 0,
1298 * ExcMessage("Invalid operation for a scalar function."));
1299 *
1300 * Assert((dim == 2) || (dim == 3), ExcNotImplemented());
1301 *
1302 * static const Point<dim> source_centers[3] = {
1303 * (dim == 2 ? Point<dim>(.3, .1) : Point<dim>(.3, .5, .1)),
1304 * (dim == 2 ? Point<dim>(.45, .1) : Point<dim>(.45, .5, .1)),
1305 * (dim == 2 ? Point<dim>(.75, .1) : Point<dim>(.75, .5, .1))};
1306 * static const double source_radius = (dim == 2 ? 1. / 32 : 1. / 8);
1307 *
1308 * return ((source_centers[0].distance(p) < source_radius) ||
1309 * (source_centers[1].distance(p) < source_radius) ||
1310 * (source_centers[2].distance(p) < source_radius) ?
1311 * 1 :
1312 * 0);
1313 * }
1314 *
1315 * virtual void vector_value(const Point<dim> &p,
1316 * Vector<double> & value) const override
1317 * {
1318 * for (unsigned int c = 0; c < this->n_components; ++c)
1319 * value(c) = TemperatureRightHandSide<dim>::value(p, c);
1320 * }
1321 * };
1322 * } // namespace EquationData
1323 *
1324 *
1325 *
1326 * @endcode
1327 *
1328 *
1329 * <a name="Linearsolversandpreconditioners"></a>
1330 * <h3>Linear solvers and preconditioners</h3>
1331 *
1332
1333 *
1334 * This section introduces some objects that are used for the solution of
1335 * the linear equations of the Stokes system that we need to solve in each
1336 * time step. Many of the ideas used here are the same as in @ref step_20 "step-20", where
1337 * Schur complement based preconditioners and solvers have been introduced,
1338 * with the actual interface taken from @ref step_22 "step-22" (in particular the
1339 * discussion in the "Results" section of @ref step_22 "step-22", in which we introduce
1340 * alternatives to the direct Schur complement approach). Note, however,
1341 * that here we don't use the Schur complement to solve the Stokes
1342 * equations, though an approximate Schur complement (the mass matrix on the
1343 * pressure space) appears in the preconditioner.
1344 *
1345 * @code
1346 * namespace LinearSolvers
1347 * {
1348 * @endcode
1349 *
1350 *
1351 * <a name="ThecodeInverseMatrixcodeclasstemplate"></a>
1352 * <h4>The <code>InverseMatrix</code> class template</h4>
1353 *
1354
1355 *
1356 * This class is an interface to calculate the action of an "inverted"
1357 * matrix on a vector (using the <code>vmult</code> operation) in the same
1358 * way as the corresponding class in @ref step_22 "step-22": when the product of an
1359 * object of this class is requested, we solve a linear equation system
1360 * with that matrix using the CG method, accelerated by a preconditioner
1361 * of (templated) class <code>PreconditionerType</code>.
1362 *
1363
1364 *
1365 * In a minor deviation from the implementation of the same class in
1366 * @ref step_22 "step-22", we make the <code>vmult</code> function take any
1367 * kind of vector type (it will yield compiler errors, however, if the
1368 * matrix does not allow a matrix-vector product with this kind of
1369 * vector).
1370 *
1371
1372 *
1373 * Secondly, we catch any exceptions that the solver may have thrown. The
1374 * reason is as follows: When debugging a program like this one
1375 * occasionally makes a mistake of passing an indefinite or nonsymmetric
1376 * matrix or preconditioner to the current class. The solver will, in that
1377 * case, not converge and throw a run-time exception. If not caught here
1378 * it will propagate up the call stack and may end up in
1379 * <code>main()</code> where we output an error message that will say that
1380 * the CG solver failed. The question then becomes: Which CG solver? The
1381 * one that inverted the mass matrix? The one that inverted the top left
1382 * block with the Laplace operator? Or a CG solver in one of the several
1383 * other nested places where we use linear solvers in the current code? No
1384 * indication about this is present in a run-time exception because it
1385 * doesn't store the stack of calls through which we got to the place
1386 * where the exception was generated.
1387 *
1388
1389 *
1390 * So rather than letting the exception propagate freely up to
1391 * <code>main()</code> we realize that there is little that an outer
1392 * function can do if the inner solver fails and rather convert the
1393 * run-time exception into an assertion that fails and triggers a call to
1394 * <code>abort()</code>, allowing us to trace back in a debugger how we
1395 * got to the current place.
1396 *
1397 * @code
1398 * template <class MatrixType, class PreconditionerType>
1399 * class InverseMatrix : public Subscriptor
1400 * {
1401 * public:
1402 * InverseMatrix(const MatrixType & m,
1403 * const PreconditionerType &preconditioner);
1404 *
1405 *
1406 * template <typename VectorType>
1407 * void vmult(VectorType &dst, const VectorType &src) const;
1408 *
1409 * private:
1411 * const PreconditionerType & preconditioner;
1412 * };
1413 *
1414 *
1415 * template <class MatrixType, class PreconditionerType>
1416 * InverseMatrix<MatrixType, PreconditionerType>::InverseMatrix(
1417 * const MatrixType & m,
1418 * const PreconditionerType &preconditioner)
1419 * : matrix(&m)
1420 * , preconditioner(preconditioner)
1421 * {}
1422 *
1423 *
1424 *
1425 * template <class MatrixType, class PreconditionerType>
1426 * template <typename VectorType>
1427 * void InverseMatrix<MatrixType, PreconditionerType>::vmult(
1428 * VectorType & dst,
1429 * const VectorType &src) const
1430 * {
1431 * SolverControl solver_control(src.size(), 1e-7 * src.l2_norm());
1432 * SolverCG<VectorType> cg(solver_control);
1433 *
1434 * dst = 0;
1435 *
1436 * try
1437 * {
1438 * cg.solve(*matrix, dst, src, preconditioner);
1439 * }
1440 * catch (std::exception &e)
1441 * {
1442 * Assert(false, ExcMessage(e.what()));
1443 * }
1444 * }
1445 *
1446 * @endcode
1447 *
1448 *
1449 * <a name="Schurcomplementpreconditioner"></a>
1450 * <h4>Schur complement preconditioner</h4>
1451 *
1452
1453 *
1454 * This is the implementation of the Schur complement preconditioner as
1455 * described in detail in the introduction. As opposed to @ref step_20 "step-20" and
1456 * @ref step_22 "step-22", we solve the block system all-at-once using GMRES, and use the
1457 * Schur complement of the block structured matrix to build a good
1458 * preconditioner instead.
1459 *
1460
1461 *
1462 * Let's have a look at the ideal preconditioner matrix
1463 * @f$P=\left(\begin{array}{cc} A & 0 \\ B & -S \end{array}\right)@f$
1464 * described in the introduction. If we apply this matrix in the solution
1465 * of a linear system, convergence of an iterative GMRES solver will be
1466 * governed by the matrix @f{eqnarray*} P^{-1}\left(\begin{array}{cc} A &
1467 * B^T \\ B & 0 \end{array}\right) = \left(\begin{array}{cc} I & A^{-1}
1468 * B^T \\ 0 & I \end{array}\right), @f} which indeed is very simple. A
1469 * GMRES solver based on exact matrices would converge in one iteration,
1470 * since all eigenvalues are equal (any Krylov method takes at most as
1471 * many iterations as there are distinct eigenvalues). Such a
1472 * preconditioner for the blocked Stokes system has been proposed by
1473 * Silvester and Wathen ("Fast iterative solution of stabilised Stokes
1474 * systems part II. Using general block preconditioners", SIAM
1475 * J. Numer. Anal., 31 (1994), pp. 1352-1367).
1476 *
1477
1478 *
1479 * Replacing @f$P@f$ by @f$\tilde{P}@f$ keeps that spirit alive: the product
1480 * @f$P^{-1} A@f$ will still be close to a matrix with eigenvalues 1 with a
1481 * distribution that does not depend on the problem size. This lets us
1482 * hope to be able to get a number of GMRES iterations that is
1483 * problem-size independent.
1484 *
1485
1486 *
1487 * The deal.II users who have already gone through the @ref step_20 "step-20" and @ref step_22 "step-22"
1488 * tutorials can certainly imagine how we're going to implement this. We
1489 * replace the exact inverse matrices in @f$P^{-1}@f$ by some approximate
1490 * inverses built from the InverseMatrix class, and the inverse Schur
1491 * complement will be approximated by the pressure mass matrix @f$M_p@f$
1492 * (weighted by @f$\eta^{-1}@f$ as mentioned in the introduction). As pointed
1493 * out in the results section of @ref step_22 "step-22", we can replace the exact inverse
1494 * of @f$A@f$ by just the application of a preconditioner, in this case
1495 * on a vector Laplace matrix as was explained in the introduction. This
1496 * does increase the number of (outer) GMRES iterations, but is still
1497 * significantly cheaper than an exact inverse, which would require
1498 * between 20 and 35 CG iterations for <em>each</em> outer solver step
1499 * (using the AMG preconditioner).
1500 *
1501
1502 *
1503 * Having the above explanations in mind, we define a preconditioner class
1504 * with a <code>vmult</code> functionality, which is all we need for the
1505 * interaction with the usual solver functions further below in the
1506 * program code.
1507 *
1508
1509 *
1510 * First the declarations. These are similar to the definition of the
1511 * Schur complement in @ref step_20 "step-20", with the difference that we need some more
1512 * preconditioners in the constructor and that the matrices we use here
1513 * are built upon Trilinos:
1514 *
1515 * @code
1516 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1517 * class BlockSchurPreconditioner : public Subscriptor
1518 * {
1519 * public:
1520 * BlockSchurPreconditioner(
1522 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1523 * PreconditionerTypeMp> &Mpinv,
1524 * const PreconditionerTypeA & Apreconditioner);
1525 *
1526 * void vmult(TrilinosWrappers::MPI::BlockVector & dst,
1527 * const TrilinosWrappers::MPI::BlockVector &src) const;
1528 *
1529 * private:
1531 * stokes_matrix;
1532 * const SmartPointer<const InverseMatrix<TrilinosWrappers::SparseMatrix,
1533 * PreconditionerTypeMp>>
1534 * m_inverse;
1535 * const PreconditionerTypeA &a_preconditioner;
1536 *
1537 * mutable TrilinosWrappers::MPI::Vector tmp;
1538 * };
1539 *
1540 *
1541 *
1542 * @endcode
1543 *
1544 * When using a TrilinosWrappers::MPI::Vector or a
1545 * TrilinosWrappers::MPI::BlockVector, the Vector is initialized using an
1546 * IndexSet. IndexSet is used not only to resize the
1547 * TrilinosWrappers::MPI::Vector but it also associates an index in the
1548 * TrilinosWrappers::MPI::Vector with a degree of freedom (see @ref step_40 "step-40" for
1549 * a more detailed explanation). The function complete_index_set() creates
1550 * an IndexSet where every valid index is part of the set. Note that this
1551 * program can only be run sequentially and will throw an exception if used
1552 * in parallel.
1553 *
1554 * @code
1555 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1556 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::
1557 * BlockSchurPreconditioner(
1559 * const InverseMatrix<TrilinosWrappers::SparseMatrix,
1560 * PreconditionerTypeMp> &Mpinv,
1561 * const PreconditionerTypeA & Apreconditioner)
1562 * : stokes_matrix(&S)
1563 * , m_inverse(&Mpinv)
1564 * , a_preconditioner(Apreconditioner)
1565 * , tmp(complete_index_set(stokes_matrix->block(1, 1).m()))
1566 * {}
1567 *
1568 *
1569 * @endcode
1570 *
1571 * Next is the <code>vmult</code> function. We implement the action of
1572 * @f$P^{-1}@f$ as described above in three successive steps. In formulas, we
1573 * want to compute @f$Y=P^{-1}X@f$ where @f$X,Y@f$ are both vectors with two block
1574 * components.
1575 *
1576
1577 *
1578 * The first step multiplies the velocity part of the vector by a
1579 * preconditioner of the matrix @f$A@f$, i.e., we compute @f$Y_0={\tilde
1580 * A}^{-1}X_0@f$. The resulting velocity vector is then multiplied by @f$B@f$
1581 * and subtracted from the pressure, i.e., we want to compute @f$X_1-BY_0@f$.
1582 * This second step only acts on the pressure vector and is accomplished
1583 * by the residual function of our matrix classes, except that the sign is
1584 * wrong. Consequently, we change the sign in the temporary pressure
1585 * vector and finally multiply by the inverse pressure mass matrix to get
1586 * the final pressure vector, completing our work on the Stokes
1587 * preconditioner:
1588 *
1589 * @code
1590 * template <class PreconditionerTypeA, class PreconditionerTypeMp>
1591 * void
1592 * BlockSchurPreconditioner<PreconditionerTypeA, PreconditionerTypeMp>::vmult(
1594 * const TrilinosWrappers::MPI::BlockVector &src) const
1595 * {
1596 * a_preconditioner.vmult(dst.block(0), src.block(0));
1597 * stokes_matrix->block(1, 0).residual(tmp, dst.block(0), src.block(1));
1598 * tmp *= -1;
1599 * m_inverse->vmult(dst.block(1), tmp);
1600 * }
1601 * } // namespace LinearSolvers
1602 *
1603 *
1604 *
1605 * @endcode
1606 *
1607 *
1608 * <a name="ThecodeBoussinesqFlowProblemcodeclasstemplate"></a>
1609 * <h3>The <code>BoussinesqFlowProblem</code> class template</h3>
1610 *
1611
1612 *
1613 * The definition of the class that defines the top-level logic of solving
1614 * the time-dependent Boussinesq problem is mainly based on the @ref step_22 "step-22"
1615 * tutorial program. The main differences are that now we also have to solve
1616 * for the temperature equation, which forces us to have a second DoFHandler
1617 * object for the temperature variable as well as matrices, right hand
1618 * sides, and solution vectors for the current and previous time steps. As
1619 * mentioned in the introduction, all linear algebra objects are going to
1620 * use wrappers of the corresponding Trilinos functionality.
1621 *
1622
1623 *
1624 * The member functions of this class are reminiscent of @ref step_21 "step-21", where we
1625 * also used a staggered scheme that first solve the flow equations (here
1626 * the Stokes equations, in @ref step_21 "step-21" Darcy flow) and then update the advected
1627 * quantity (here the temperature, there the saturation). The functions that
1628 * are new are mainly concerned with determining the time step, as well as
1629 * the proper size of the artificial viscosity stabilization.
1630 *
1631
1632 *
1633 * The last three variables indicate whether the various matrices or
1634 * preconditioners need to be rebuilt the next time the corresponding build
1635 * functions are called. This allows us to move the corresponding
1636 * <code>if</code> into the respective function and thereby keeping our main
1637 * <code>run()</code> function clean and easy to read.
1638 *
1639 * @code
1640 * template <int dim>
1641 * class BoussinesqFlowProblem
1642 * {
1643 * public:
1644 * BoussinesqFlowProblem();
1645 * void run();
1646 *
1647 * private:
1648 * void setup_dofs();
1649 * void assemble_stokes_preconditioner();
1650 * void build_stokes_preconditioner();
1651 * void assemble_stokes_system();
1652 * void assemble_temperature_system(const double maximal_velocity);
1653 * void assemble_temperature_matrix();
1654 * double get_maximal_velocity() const;
1655 * std::pair<double, double> get_extrapolated_temperature_range() const;
1656 * void solve();
1657 * void output_results() const;
1658 * void refine_mesh(const unsigned int max_grid_level);
1659 *
1660 * double compute_viscosity(
1661 * const std::vector<double> & old_temperature,
1662 * const std::vector<double> & old_old_temperature,
1663 * const std::vector<Tensor<1, dim>> &old_temperature_grads,
1664 * const std::vector<Tensor<1, dim>> &old_old_temperature_grads,
1665 * const std::vector<double> & old_temperature_laplacians,
1666 * const std::vector<double> & old_old_temperature_laplacians,
1667 * const std::vector<Tensor<1, dim>> &old_velocity_values,
1668 * const std::vector<Tensor<1, dim>> &old_old_velocity_values,
1669 * const std::vector<double> & gamma_values,
1670 * const double global_u_infty,
1671 * const double global_T_variation,
1672 * const double cell_diameter) const;
1673 *
1674 *
1676 * double global_Omega_diameter;
1677 *
1678 * const unsigned int stokes_degree;
1679 * FESystem<dim> stokes_fe;
1680 * DoFHandler<dim> stokes_dof_handler;
1681 * AffineConstraints<double> stokes_constraints;
1682 *
1683 * std::vector<IndexSet> stokes_partitioning;
1685 * TrilinosWrappers::BlockSparseMatrix stokes_preconditioner_matrix;
1686 *
1687 * TrilinosWrappers::MPI::BlockVector stokes_solution;
1688 * TrilinosWrappers::MPI::BlockVector old_stokes_solution;
1690 *
1691 *
1692 * const unsigned int temperature_degree;
1693 * FE_Q<dim> temperature_fe;
1694 * DoFHandler<dim> temperature_dof_handler;
1695 * AffineConstraints<double> temperature_constraints;
1696 *
1697 * TrilinosWrappers::SparseMatrix temperature_mass_matrix;
1698 * TrilinosWrappers::SparseMatrix temperature_stiffness_matrix;
1699 * TrilinosWrappers::SparseMatrix temperature_matrix;
1700 *
1701 * TrilinosWrappers::MPI::Vector temperature_solution;
1702 * TrilinosWrappers::MPI::Vector old_temperature_solution;
1703 * TrilinosWrappers::MPI::Vector old_old_temperature_solution;
1704 * TrilinosWrappers::MPI::Vector temperature_rhs;
1705 *
1706 *
1707 * double time_step;
1708 * double old_time_step;
1709 * unsigned int timestep_number;
1710 *
1711 * std::shared_ptr<TrilinosWrappers::PreconditionAMG> Amg_preconditioner;
1712 * std::shared_ptr<TrilinosWrappers::PreconditionIC> Mp_preconditioner;
1713 *
1714 * bool rebuild_stokes_matrix;
1715 * bool rebuild_temperature_matrices;
1716 * bool rebuild_stokes_preconditioner;
1717 * };
1718 *
1719 *
1720 * @endcode
1721 *
1722 *
1723 * <a name="BoussinesqFlowProblemclassimplementation"></a>
1724 * <h3>BoussinesqFlowProblem class implementation</h3>
1725 *
1726
1727 *
1728 *
1729 * <a name="BoussinesqFlowProblemBoussinesqFlowProblem"></a>
1730 * <h4>BoussinesqFlowProblem::BoussinesqFlowProblem</h4>
1731 *
1732
1733 *
1734 * The constructor of this class is an extension of the constructor in
1735 * @ref step_22 "step-22". We need to add the various variables that concern the
1736 * temperature. As discussed in the introduction, we are going to use
1737 * @f$Q_2\times Q_1@f$ (Taylor-Hood) elements again for the Stokes part, and
1738 * @f$Q_2@f$ elements for the temperature. However, by using variables that
1739 * store the polynomial degree of the Stokes and temperature finite
1740 * elements, it is easy to consistently modify the degree of the elements as
1741 * well as all quadrature formulas used on them downstream. Moreover, we
1742 * initialize the time stepping as well as the options for matrix assembly
1743 * and preconditioning:
1744 *
1745 * @code
1746 * template <int dim>
1747 * BoussinesqFlowProblem<dim>::BoussinesqFlowProblem()
1749 * , global_Omega_diameter(std::numeric_limits<double>::quiet_NaN())
1750 * , stokes_degree(1)
1751 * , stokes_fe(FE_Q<dim>(stokes_degree + 1), dim, FE_Q<dim>(stokes_degree), 1)
1752 * , stokes_dof_handler(triangulation)
1753 * ,
1754 *
1755 * temperature_degree(2)
1756 * , temperature_fe(temperature_degree)
1757 * , temperature_dof_handler(triangulation)
1758 * ,
1759 *
1760 * time_step(0)
1761 * , old_time_step(0)
1762 * , timestep_number(0)
1763 * , rebuild_stokes_matrix(true)
1764 * , rebuild_temperature_matrices(true)
1765 * , rebuild_stokes_preconditioner(true)
1766 * {}
1767 *
1768 *
1769 *
1770 * @endcode
1771 *
1772 *
1773 * <a name="BoussinesqFlowProblemget_maximal_velocity"></a>
1774 * <h4>BoussinesqFlowProblem::get_maximal_velocity</h4>
1775 *
1776
1777 *
1778 * Starting the real functionality of this class is a helper function that
1779 * determines the maximum (@f$L_\infty@f$) velocity in the domain (at the
1780 * quadrature points, in fact). How it works should be relatively obvious to
1781 * all who have gotten to this point of the tutorial. Note that since we are
1782 * only interested in the velocity, rather than using
1783 * <code>stokes_fe_values.get_function_values</code> to get the values of
1784 * the entire Stokes solution (velocities and pressures) we use
1785 * <code>stokes_fe_values[velocities].get_function_values</code> to extract
1786 * only the velocities part. This has the additional benefit that we get it
1787 * as a Tensor<1,dim>, rather than some components in a Vector<double>,
1788 * allowing us to process it right away using the <code>norm()</code>
1789 * function to get the magnitude of the velocity.
1790 *
1791
1792 *
1793 * The only point worth thinking about a bit is how to choose the quadrature
1794 * points we use here. Since the goal of this function is to find the
1795 * maximal velocity over a domain by looking at quadrature points on each
1796 * cell. So we should ask how we should best choose these quadrature points
1797 * on each cell. To this end, recall that if we had a single @f$Q_1@f$ field
1798 * (rather than the vector-valued field of higher order) then the maximum
1799 * would be attained at a vertex of the mesh. In other words, we should use
1800 * the QTrapezoid class that has quadrature points only at the vertices of
1801 * cells.
1802 *
1803
1804 *
1805 * For higher order shape functions, the situation is more complicated: the
1806 * maxima and minima may be attained at points between the support points of
1807 * shape functions (for the usual @f$Q_p@f$ elements the support points are the
1808 * equidistant Lagrange interpolation points); furthermore, since we are
1809 * looking for the maximum magnitude of a vector-valued quantity, we can
1810 * even less say with certainty where the set of potential maximal points
1811 * are. Nevertheless, intuitively if not provably, the Lagrange
1812 * interpolation points appear to be a better choice than the Gauss points.
1813 *
1814
1815 *
1816 * There are now different methods to produce a quadrature formula with
1817 * quadrature points equal to the interpolation points of the finite
1818 * element. One option would be to use the
1819 * FiniteElement::get_unit_support_points() function, reduce the output to a
1820 * unique set of points to avoid duplicate function evaluations, and create
1821 * a Quadrature object using these points. Another option, chosen here, is
1822 * to use the QTrapezoid class and combine it with the QIterated class that
1823 * repeats the QTrapezoid formula on a number of sub-cells in each coordinate
1824 * direction. To cover all support points, we need to iterate it
1825 * <code>stokes_degree+1</code> times since this is the polynomial degree of
1826 * the Stokes element in use:
1827 *
1828 * @code
1829 * template <int dim>
1830 * double BoussinesqFlowProblem<dim>::get_maximal_velocity() const
1831 * {
1832 * const QIterated<dim> quadrature_formula(QTrapezoid<1>(), stokes_degree + 1);
1833 * const unsigned int n_q_points = quadrature_formula.size();
1834 *
1835 * FEValues<dim> fe_values(stokes_fe, quadrature_formula, update_values);
1836 * std::vector<Tensor<1, dim>> velocity_values(n_q_points);
1837 * double max_velocity = 0;
1838 *
1839 * const FEValuesExtractors::Vector velocities(0);
1840 *
1841 * for (const auto &cell : stokes_dof_handler.active_cell_iterators())
1842 * {
1843 * fe_values.reinit(cell);
1844 * fe_values[velocities].get_function_values(stokes_solution,
1845 * velocity_values);
1846 *
1847 * for (unsigned int q = 0; q < n_q_points; ++q)
1848 * max_velocity = std::max(max_velocity, velocity_values[q].norm());
1849 * }
1850 *
1851 * return max_velocity;
1852 * }
1853 *
1854 *
1855 *
1856 * @endcode
1857 *
1858 *
1859 * <a name="BoussinesqFlowProblemget_extrapolated_temperature_range"></a>
1860 * <h4>BoussinesqFlowProblem::get_extrapolated_temperature_range</h4>
1861 *
1862
1863 *
1864 * Next a function that determines the minimum and maximum temperature at
1865 * quadrature points inside @f$\Omega@f$ when extrapolated from the two previous
1866 * time steps to the current one. We need this information in the
1867 * computation of the artificial viscosity parameter @f$\nu@f$ as discussed in
1868 * the introduction.
1869 *
1870
1871 *
1872 * The formula for the extrapolated temperature is
1873 * @f$\left(1+\frac{k_n}{k_{n-1}} \right)T^{n-1} + \frac{k_n}{k_{n-1}}
1874 * T^{n-2}@f$. The way to compute it is to loop over all quadrature points and
1875 * update the maximum and minimum value if the current value is
1876 * bigger/smaller than the previous one. We initialize the variables that
1877 * store the max and min before the loop over all quadrature points by the
1878 * smallest and the largest number representable as a double. Then we know
1879 * for a fact that it is larger/smaller than the minimum/maximum and that
1880 * the loop over all quadrature points is ultimately going to update the
1881 * initial value with the correct one.
1882 *
1883
1884 *
1885 * The only other complication worth mentioning here is that in the first
1886 * time step, @f$T^{k-2}@f$ is not yet available of course. In that case, we can
1887 * only use @f$T^{k-1}@f$ which we have from the initial temperature. As
1888 * quadrature points, we use the same choice as in the previous function
1889 * though with the difference that now the number of repetitions is
1890 * determined by the polynomial degree of the temperature field.
1891 *
1892 * @code
1893 * template <int dim>
1894 * std::pair<double, double>
1895 * BoussinesqFlowProblem<dim>::get_extrapolated_temperature_range() const
1896 * {
1897 * const QIterated<dim> quadrature_formula(QTrapezoid<1>(),
1898 * temperature_degree);
1899 * const unsigned int n_q_points = quadrature_formula.size();
1900 *
1901 * FEValues<dim> fe_values(temperature_fe, quadrature_formula, update_values);
1902 * std::vector<double> old_temperature_values(n_q_points);
1903 * std::vector<double> old_old_temperature_values(n_q_points);
1904 *
1905 * if (timestep_number != 0)
1906 * {
1907 * double min_temperature = std::numeric_limits<double>::max(),
1908 * max_temperature = -std::numeric_limits<double>::max();
1909 *
1910 * for (const auto &cell : temperature_dof_handler.active_cell_iterators())
1911 * {
1912 * fe_values.reinit(cell);
1913 * fe_values.get_function_values(old_temperature_solution,
1914 * old_temperature_values);
1915 * fe_values.get_function_values(old_old_temperature_solution,
1916 * old_old_temperature_values);
1917 *
1918 * for (unsigned int q = 0; q < n_q_points; ++q)
1919 * {
1920 * const double temperature =
1921 * (1. + time_step / old_time_step) * old_temperature_values[q] -
1922 * time_step / old_time_step * old_old_temperature_values[q];
1923 *
1924 * min_temperature = std::min(min_temperature, temperature);
1925 * max_temperature = std::max(max_temperature, temperature);
1926 * }
1927 * }
1928 *
1929 * return std::make_pair(min_temperature, max_temperature);
1930 * }
1931 * else
1932 * {
1933 * double min_temperature = std::numeric_limits<double>::max(),
1934 * max_temperature = -std::numeric_limits<double>::max();
1935 *
1936 * for (const auto &cell : temperature_dof_handler.active_cell_iterators())
1937 * {
1938 * fe_values.reinit(cell);
1939 * fe_values.get_function_values(old_temperature_solution,
1940 * old_temperature_values);
1941 *
1942 * for (unsigned int q = 0; q < n_q_points; ++q)
1943 * {
1944 * const double temperature = old_temperature_values[q];
1945 *
1946 * min_temperature = std::min(min_temperature, temperature);
1947 * max_temperature = std::max(max_temperature, temperature);
1948 * }
1949 * }
1950 *
1951 * return std::make_pair(min_temperature, max_temperature);
1952 * }
1953 * }
1954 *
1955 *
1956 *
1957 * @endcode
1958 *
1959 *
1960 * <a name="BoussinesqFlowProblemcompute_viscosity"></a>
1961 * <h4>BoussinesqFlowProblem::compute_viscosity</h4>
1962 *
1963
1964 *
1965 * The last of the tool functions computes the artificial viscosity
1966 * parameter @f$\nu|_K@f$ on a cell @f$K@f$ as a function of the extrapolated
1967 * temperature, its gradient and Hessian (second derivatives), the velocity,
1968 * the right hand side @f$\gamma@f$ all on the quadrature points of the current
1969 * cell, and various other parameters as described in detail in the
1970 * introduction.
1971 *
1972
1973 *
1974 * There are some universal constants worth mentioning here. First, we need
1975 * to fix @f$\beta@f$; we choose @f$\beta=0.017\cdot dim@f$, a choice discussed in
1976 * detail in the results section of this tutorial program. The second is the
1977 * exponent @f$\alpha@f$; @f$\alpha=1@f$ appears to work fine for the current
1978 * program, even though some additional benefit might be expected from
1979 * choosing @f$\alpha = 2@f$. Finally, there is one thing that requires special
1980 * casing: In the first time step, the velocity equals zero, and the formula
1981 * for @f$\nu|_K@f$ is not defined. In that case, we return @f$\nu|_K=5\cdot 10^3
1982 * \cdot h_K@f$, a choice admittedly more motivated by heuristics than
1983 * anything else (it is in the same order of magnitude, however, as the
1984 * value returned for most cells on the second time step).
1985 *
1986
1987 *
1988 * The rest of the function should be mostly obvious based on the material
1989 * discussed in the introduction:
1990 *
1991 * @code
1992 * template <int dim>
1993 * double BoussinesqFlowProblem<dim>::compute_viscosity(
1994 * const std::vector<double> & old_temperature,
1995 * const std::vector<double> & old_old_temperature,
1996 * const std::vector<Tensor<1, dim>> &old_temperature_grads,
1997 * const std::vector<Tensor<1, dim>> &old_old_temperature_grads,
1998 * const std::vector<double> & old_temperature_laplacians,
1999 * const std::vector<double> & old_old_temperature_laplacians,
2000 * const std::vector<Tensor<1, dim>> &old_velocity_values,
2001 * const std::vector<Tensor<1, dim>> &old_old_velocity_values,
2002 * const std::vector<double> & gamma_values,
2003 * const double global_u_infty,
2004 * const double global_T_variation,
2005 * const double cell_diameter) const
2006 * {
2007 * constexpr double beta = 0.017 * dim;
2008 * constexpr double alpha = 1.0;
2009 *
2010 * if (global_u_infty == 0)
2011 * return 5e-3 * cell_diameter;
2012 *
2013 * const unsigned int n_q_points = old_temperature.size();
2014 *
2015 * double max_residual = 0;
2016 * double max_velocity = 0;
2017 *
2018 * for (unsigned int q = 0; q < n_q_points; ++q)
2019 * {
2020 * const Tensor<1, dim> u =
2021 * (old_velocity_values[q] + old_old_velocity_values[q]) / 2;
2022 *
2023 * const double dT_dt =
2024 * (old_temperature[q] - old_old_temperature[q]) / old_time_step;
2025 * const double u_grad_T =
2026 * u * (old_temperature_grads[q] + old_old_temperature_grads[q]) / 2;
2027 *
2028 * const double kappa_Delta_T =
2029 * EquationData::kappa *
2030 * (old_temperature_laplacians[q] + old_old_temperature_laplacians[q]) /
2031 * 2;
2032 *
2033 * const double residual =
2034 * std::abs((dT_dt + u_grad_T - kappa_Delta_T - gamma_values[q]) *
2035 * std::pow((old_temperature[q] + old_old_temperature[q]) / 2,
2036 * alpha - 1.));
2037 *
2038 * max_residual = std::max(residual, max_residual);
2039 * max_velocity = std::max(std::sqrt(u * u), max_velocity);
2040 * }
2041 *
2042 * const double c_R = std::pow(2., (4. - 2 * alpha) / dim);
2043 * const double global_scaling = c_R * global_u_infty * global_T_variation *
2044 * std::pow(global_Omega_diameter, alpha - 2.);
2045 *
2046 * return (
2047 * beta * max_velocity *
2048 * std::min(cell_diameter,
2049 * std::pow(cell_diameter, alpha) * max_residual / global_scaling));
2050 * }
2051 *
2052 *
2053 *
2054 * @endcode
2055 *
2056 *
2057 * <a name="BoussinesqFlowProblemsetup_dofs"></a>
2058 * <h4>BoussinesqFlowProblem::setup_dofs</h4>
2059 *
2060
2061 *
2062 * This is the function that sets up the DoFHandler objects we have here
2063 * (one for the Stokes part and one for the temperature part) as well as set
2064 * to the right sizes the various objects required for the linear algebra in
2065 * this program. Its basic operations are similar to what we do in @ref step_22 "step-22".
2066 *
2067
2068 *
2069 * The body of the function first enumerates all degrees of freedom for the
2070 * Stokes and temperature systems. For the Stokes part, degrees of freedom
2071 * are then sorted to ensure that velocities precede pressure DoFs so that
2072 * we can partition the Stokes matrix into a @f$2\times 2@f$ matrix. As a
2073 * difference to @ref step_22 "step-22", we do not perform any additional DoF
2074 * renumbering. In that program, it paid off since our solver was heavily
2075 * dependent on ILU's, whereas we use AMG here which is not sensitive to the
2076 * DoF numbering. The IC preconditioner for the inversion of the pressure
2077 * mass matrix would of course take advantage of a Cuthill-McKee like
2078 * renumbering, but its costs are low compared to the velocity portion, so
2079 * the additional work does not pay off.
2080 *
2081
2082 *
2083 * We then proceed with the generation of the hanging node constraints that
2084 * arise from adaptive grid refinement for both DoFHandler objects. For the
2085 * velocity, we impose no-flux boundary conditions @f$\mathbf{u}\cdot
2086 * \mathbf{n}=0@f$ by adding constraints to the object that already stores the
2087 * hanging node constraints matrix. The second parameter in the function
2088 * describes the first of the velocity components in the total dof vector,
2089 * which is zero here. The variable <code>no_normal_flux_boundaries</code>
2090 * denotes the boundary indicators for which to set the no flux boundary
2091 * conditions; here, this is boundary indicator zero.
2092 *
2093
2094 *
2095 * After having done so, we count the number of degrees of freedom in the
2096 * various blocks:
2097 *
2098 * @code
2099 * template <int dim>
2100 * void BoussinesqFlowProblem<dim>::setup_dofs()
2101 * {
2102 * std::vector<unsigned int> stokes_sub_blocks(dim + 1, 0);
2103 * stokes_sub_blocks[dim] = 1;
2104 *
2105 * {
2106 * stokes_dof_handler.distribute_dofs(stokes_fe);
2107 * DoFRenumbering::component_wise(stokes_dof_handler, stokes_sub_blocks);
2108 *
2109 * stokes_constraints.clear();
2110 * DoFTools::make_hanging_node_constraints(stokes_dof_handler,
2111 * stokes_constraints);
2112 * std::set<types::boundary_id> no_normal_flux_boundaries;
2113 * no_normal_flux_boundaries.insert(0);
2114 * VectorTools::compute_no_normal_flux_constraints(stokes_dof_handler,
2115 * 0,
2116 * no_normal_flux_boundaries,
2117 * stokes_constraints);
2118 * stokes_constraints.close();
2119 * }
2120 * {
2121 * temperature_dof_handler.distribute_dofs(temperature_fe);
2122 *
2123 * temperature_constraints.clear();
2124 * DoFTools::make_hanging_node_constraints(temperature_dof_handler,
2125 * temperature_constraints);
2126 * temperature_constraints.close();
2127 * }
2128 *
2129 * const std::vector<types::global_dof_index> stokes_dofs_per_block =
2130 * DoFTools::count_dofs_per_fe_block(stokes_dof_handler, stokes_sub_blocks);
2131 *
2132 * const unsigned int n_u = stokes_dofs_per_block[0],
2133 * n_p = stokes_dofs_per_block[1],
2134 * n_T = temperature_dof_handler.n_dofs();
2135 *
2136 * std::cout << "Number of active cells: " << triangulation.n_active_cells()
2137 * << " (on " << triangulation.n_levels() << " levels)" << std::endl
2138 * << "Number of degrees of freedom: " << n_u + n_p + n_T << " ("
2139 * << n_u << '+' << n_p << '+' << n_T << ')' << std::endl
2140 * << std::endl;
2141 *
2142 * @endcode
2143 *
2144 * The next step is to create the sparsity pattern for the Stokes and
2145 * temperature system matrices as well as the preconditioner matrix from
2146 * which we build the Stokes preconditioner. As in @ref step_22 "step-22", we choose to
2147 * create the pattern by
2148 * using the blocked version of DynamicSparsityPattern.
2149 *
2150
2151 *
2152 * So, we first release the memory stored in the matrices, then set up an
2153 * object of type BlockDynamicSparsityPattern consisting of
2154 * @f$2\times 2@f$ blocks (for the Stokes system matrix and preconditioner) or
2155 * DynamicSparsityPattern (for the temperature part). We then
2156 * fill these objects with the nonzero pattern, taking into account that
2157 * for the Stokes system matrix, there are no entries in the
2158 * pressure-pressure block (but all velocity vector components couple with
2159 * each other and with the pressure). Similarly, in the Stokes
2160 * preconditioner matrix, only the diagonal blocks are nonzero, since we
2161 * use the vector Laplacian as discussed in the introduction. This
2162 * operator only couples each vector component of the Laplacian with
2163 * itself, but not with the other vector components. (Application of the
2164 * constraints resulting from the no-flux boundary conditions will couple
2165 * vector components at the boundary again, however.)
2166 *
2167
2168 *
2169 * When generating the sparsity pattern, we directly apply the constraints
2170 * from hanging nodes and no-flux boundary conditions. This approach was
2171 * already used in @ref step_27 "step-27", but is different from the one in early
2172 * tutorial programs where we first built the original sparsity pattern
2173 * and only then added the entries resulting from constraints. The reason
2174 * for doing so is that later during assembly we are going to distribute
2175 * the constraints immediately when transferring local to global
2176 * dofs. Consequently, there will be no data written at positions of
2177 * constrained degrees of freedom, so we can let the
2178 * DoFTools::make_sparsity_pattern function omit these entries by setting
2179 * the last Boolean flag to <code>false</code>. Once the sparsity pattern
2180 * is ready, we can use it to initialize the Trilinos matrices. Since the
2181 * Trilinos matrices store the sparsity pattern internally, there is no
2182 * need to keep the sparsity pattern around after the initialization of
2183 * the matrix.
2184 *
2185 * @code
2186 * stokes_partitioning.resize(2);
2187 * stokes_partitioning[0] = complete_index_set(n_u);
2188 * stokes_partitioning[1] = complete_index_set(n_p);
2189 * {
2190 * stokes_matrix.clear();
2191 *
2192 * BlockDynamicSparsityPattern dsp(2, 2);
2193 *
2194 * dsp.block(0, 0).reinit(n_u, n_u);
2195 * dsp.block(0, 1).reinit(n_u, n_p);
2196 * dsp.block(1, 0).reinit(n_p, n_u);
2197 * dsp.block(1, 1).reinit(n_p, n_p);
2198 *
2199 * dsp.collect_sizes();
2200 *
2201 * Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
2202 *
2203 * for (unsigned int c = 0; c < dim + 1; ++c)
2204 * for (unsigned int d = 0; d < dim + 1; ++d)
2205 * if (!((c == dim) && (d == dim)))
2206 * coupling[c][d] = DoFTools::always;
2207 * else
2208 * coupling[c][d] = DoFTools::none;
2209 *
2210 * DoFTools::make_sparsity_pattern(
2211 * stokes_dof_handler, coupling, dsp, stokes_constraints, false);
2212 *
2213 * stokes_matrix.reinit(dsp);
2214 * }
2215 *
2216 * {
2217 * Amg_preconditioner.reset();
2218 * Mp_preconditioner.reset();
2219 * stokes_preconditioner_matrix.clear();
2220 *
2221 * BlockDynamicSparsityPattern dsp(2, 2);
2222 *
2223 * dsp.block(0, 0).reinit(n_u, n_u);
2224 * dsp.block(0, 1).reinit(n_u, n_p);
2225 * dsp.block(1, 0).reinit(n_p, n_u);
2226 * dsp.block(1, 1).reinit(n_p, n_p);
2227 *
2228 * dsp.collect_sizes();
2229 *
2230 * Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
2231 * for (unsigned int c = 0; c < dim + 1; ++c)
2232 * for (unsigned int d = 0; d < dim + 1; ++d)
2233 * if (c == d)
2234 * coupling[c][d] = DoFTools::always;
2235 * else
2236 * coupling[c][d] = DoFTools::none;
2237 *
2238 * DoFTools::make_sparsity_pattern(
2239 * stokes_dof_handler, coupling, dsp, stokes_constraints, false);
2240 *
2241 * stokes_preconditioner_matrix.reinit(dsp);
2242 * }
2243 *
2244 * @endcode
2245 *
2246 * The creation of the temperature matrix (or, rather, matrices, since we
2247 * provide a temperature mass matrix and a temperature stiffness matrix,
2248 * that will be added together for time discretization) follows the
2249 * generation of the Stokes matrix &ndash; except that it is much easier
2250 * here since we do not need to take care of any blocks or coupling
2251 * between components. Note how we initialize the three temperature
2252 * matrices: We only use the sparsity pattern for reinitialization of the
2253 * first matrix, whereas we use the previously generated matrix for the
2254 * two remaining reinits. The reason for doing so is that reinitialization
2255 * from an already generated matrix allows Trilinos to reuse the sparsity
2256 * pattern instead of generating a new one for each copy. This saves both
2257 * some time and memory.
2258 *
2259 * @code
2260 * {
2261 * temperature_mass_matrix.clear();
2262 * temperature_stiffness_matrix.clear();
2263 * temperature_matrix.clear();
2264 *
2265 * DynamicSparsityPattern dsp(n_T, n_T);
2266 * DoFTools::make_sparsity_pattern(temperature_dof_handler,
2267 * dsp,
2268 * temperature_constraints,
2269 * false);
2270 *
2271 * temperature_matrix.reinit(dsp);
2272 * temperature_mass_matrix.reinit(temperature_matrix);
2273 * temperature_stiffness_matrix.reinit(temperature_matrix);
2274 * }
2275 *
2276 * @endcode
2277 *
2278 * Lastly, we set the vectors for the Stokes solutions @f$\mathbf u^{n-1}@f$
2279 * and @f$\mathbf u^{n-2}@f$, as well as for the temperatures @f$T^{n}@f$,
2280 * @f$T^{n-1}@f$ and @f$T^{n-2}@f$ (required for time stepping) and all the system
2281 * right hand sides to their correct sizes and block structure:
2282 *
2283 * @code
2284 * IndexSet temperature_partitioning = complete_index_set(n_T);
2285 * stokes_solution.reinit(stokes_partitioning, MPI_COMM_WORLD);
2286 * old_stokes_solution.reinit(stokes_partitioning, MPI_COMM_WORLD);
2287 * stokes_rhs.reinit(stokes_partitioning, MPI_COMM_WORLD);
2288 *
2289 * temperature_solution.reinit(temperature_partitioning, MPI_COMM_WORLD);
2290 * old_temperature_solution.reinit(temperature_partitioning, MPI_COMM_WORLD);
2291 * old_old_temperature_solution.reinit(temperature_partitioning,
2292 * MPI_COMM_WORLD);
2293 *
2294 * temperature_rhs.reinit(temperature_partitioning, MPI_COMM_WORLD);
2295 * }
2296 *
2297 *
2298 *
2299 * @endcode
2300 *
2301 *
2302 * <a name="BoussinesqFlowProblemassemble_stokes_preconditioner"></a>
2303 * <h4>BoussinesqFlowProblem::assemble_stokes_preconditioner</h4>
2304 *
2305
2306 *
2307 * This function assembles the matrix we use for preconditioning the Stokes
2308 * system. What we need are a vector Laplace matrix on the velocity
2309 * components and a mass matrix weighted by @f$\eta^{-1}@f$ on the pressure
2310 * component. We start by generating a quadrature object of appropriate
2311 * order, the FEValues object that can give values and gradients at the
2312 * quadrature points (together with quadrature weights). Next we create data
2313 * structures for the cell matrix and the relation between local and global
2314 * DoFs. The vectors <code>grad_phi_u</code> and <code>phi_p</code> are
2315 * going to hold the values of the basis functions in order to faster build
2316 * up the local matrices, as was already done in @ref step_22 "step-22". Before we start
2317 * the loop over all active cells, we have to specify which components are
2318 * pressure and which are velocity.
2319 *
2320 * @code
2321 * template <int dim>
2322 * void BoussinesqFlowProblem<dim>::assemble_stokes_preconditioner()
2323 * {
2324 * stokes_preconditioner_matrix = 0;
2325 *
2326 * const QGauss<dim> quadrature_formula(stokes_degree + 2);
2327 * FEValues<dim> stokes_fe_values(stokes_fe,
2328 * quadrature_formula,
2329 * update_JxW_values | update_values |
2330 * update_gradients);
2331 *
2332 * const unsigned int dofs_per_cell = stokes_fe.n_dofs_per_cell();
2333 * const unsigned int n_q_points = quadrature_formula.size();
2334 *
2335 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
2336 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2337 *
2338 * std::vector<Tensor<2, dim>> grad_phi_u(dofs_per_cell);
2339 * std::vector<double> phi_p(dofs_per_cell);
2340 *
2341 * const FEValuesExtractors::Vector velocities(0);
2342 * const FEValuesExtractors::Scalar pressure(dim);
2343 *
2344 * for (const auto &cell : stokes_dof_handler.active_cell_iterators())
2345 * {
2346 * stokes_fe_values.reinit(cell);
2347 * local_matrix = 0;
2348 *
2349 * @endcode
2350 *
2351 * The creation of the local matrix is rather simple. There are only a
2352 * Laplace term (on the velocity) and a mass matrix weighted by
2353 * @f$\eta^{-1}@f$ to be generated, so the creation of the local matrix is
2354 * done in two lines. Once the local matrix is ready (loop over rows
2355 * and columns in the local matrix on each quadrature point), we get
2356 * the local DoF indices and write the local information into the
2357 * global matrix. We do this as in @ref step_27 "step-27", i.e., we directly apply the
2358 * constraints from hanging nodes locally. By doing so, we don't have
2359 * to do that afterwards, and we don't also write into entries of the
2360 * matrix that will actually be set to zero again later when
2361 * eliminating constraints.
2362 *
2363 * @code
2364 * for (unsigned int q = 0; q < n_q_points; ++q)
2365 * {
2366 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
2367 * {
2368 * grad_phi_u[k] = stokes_fe_values[velocities].gradient(k, q);
2369 * phi_p[k] = stokes_fe_values[pressure].value(k, q);
2370 * }
2371 *
2372 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2373 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
2374 * local_matrix(i, j) +=
2375 * (EquationData::eta *
2376 * scalar_product(grad_phi_u[i], grad_phi_u[j]) +
2377 * (1. / EquationData::eta) * phi_p[i] * phi_p[j]) *
2378 * stokes_fe_values.JxW(q);
2379 * }
2380 *
2381 * cell->get_dof_indices(local_dof_indices);
2382 * stokes_constraints.distribute_local_to_global(
2383 * local_matrix, local_dof_indices, stokes_preconditioner_matrix);
2384 * }
2385 * }
2386 *
2387 *
2388 *
2389 * @endcode
2390 *
2391 *
2392 * <a name="BoussinesqFlowProblembuild_stokes_preconditioner"></a>
2393 * <h4>BoussinesqFlowProblem::build_stokes_preconditioner</h4>
2394 *
2395
2396 *
2397 * This function generates the inner preconditioners that are going to be
2398 * used for the Schur complement block preconditioner. Since the
2399 * preconditioners need only to be regenerated when the matrices change,
2400 * this function does not have to do anything in case the matrices have not
2401 * changed (i.e., the flag <code>rebuild_stokes_preconditioner</code> has
2402 * the value <code>false</code>). Otherwise its first task is to call
2403 * <code>assemble_stokes_preconditioner</code> to generate the
2404 * preconditioner matrices.
2405 *
2406
2407 *
2408 * Next, we set up the preconditioner for the velocity-velocity matrix
2409 * @f$A@f$. As explained in the introduction, we are going to use an AMG
2410 * preconditioner based on a vector Laplace matrix @f$\hat{A}@f$ (which is
2411 * spectrally close to the Stokes matrix @f$A@f$). Usually, the
2412 * TrilinosWrappers::PreconditionAMG class can be seen as a good black-box
2413 * preconditioner which does not need any special knowledge. In this case,
2414 * however, we have to be careful: since we build an AMG for a vector
2415 * problem, we have to tell the preconditioner setup which dofs belong to
2416 * which vector component. We do this using the function
2417 * DoFTools::extract_constant_modes, a function that generates a set of
2418 * <code>dim</code> vectors, where each one has ones in the respective
2419 * component of the vector problem and zeros elsewhere. Hence, these are the
2420 * constant modes on each component, which explains the name of the
2421 * variable.
2422 *
2423 * @code
2424 * template <int dim>
2425 * void BoussinesqFlowProblem<dim>::build_stokes_preconditioner()
2426 * {
2427 * if (rebuild_stokes_preconditioner == false)
2428 * return;
2429 *
2430 * std::cout << " Rebuilding Stokes preconditioner..." << std::flush;
2431 *
2432 * assemble_stokes_preconditioner();
2433 *
2434 * Amg_preconditioner = std::make_shared<TrilinosWrappers::PreconditionAMG>();
2435 *
2436 * std::vector<std::vector<bool>> constant_modes;
2437 * FEValuesExtractors::Vector velocity_components(0);
2438 * DoFTools::extract_constant_modes(stokes_dof_handler,
2439 * stokes_fe.component_mask(
2440 * velocity_components),
2441 * constant_modes);
2442 * TrilinosWrappers::PreconditionAMG::AdditionalData amg_data;
2443 * amg_data.constant_modes = constant_modes;
2444 *
2445 * @endcode
2446 *
2447 * Next, we set some more options of the AMG preconditioner. In
2448 * particular, we need to tell the AMG setup that we use quadratic basis
2449 * functions for the velocity matrix (this implies more nonzero elements
2450 * in the matrix, so that a more robust algorithm needs to be chosen
2451 * internally). Moreover, we want to be able to control how the coarsening
2452 * structure is build up. The way the Trilinos smoothed aggregation AMG
2453 * does this is to look which matrix entries are of similar size as the
2454 * diagonal entry in order to algebraically build a coarse-grid
2455 * structure. By setting the parameter <code>aggregation_threshold</code>
2456 * to 0.02, we specify that all entries that are more than two percent of
2457 * size of some diagonal pivots in that row should form one coarse grid
2458 * point. This parameter is rather ad hoc, and some fine-tuning of it can
2459 * influence the performance of the preconditioner. As a rule of thumb,
2460 * larger values of <code>aggregation_threshold</code> will decrease the
2461 * number of iterations, but increase the costs per iteration. A look at
2462 * the Trilinos documentation will provide more information on these
2463 * parameters. With this data set, we then initialize the preconditioner
2464 * with the matrix we want it to apply to.
2465 *
2466
2467 *
2468 * Finally, we also initialize the preconditioner for the inversion of the
2469 * pressure mass matrix. This matrix is symmetric and well-behaved, so we
2470 * can chose a simple preconditioner. We stick with an incomplete Cholesky
2471 * (IC) factorization preconditioner, which is designed for symmetric
2472 * matrices. We could have also chosen an SSOR preconditioner with
2473 * relaxation factor around 1.2, but IC is cheaper for our example. We
2474 * wrap the preconditioners into a <code>std::shared_ptr</code>
2475 * pointer, which makes it easier to recreate the preconditioner next time
2476 * around since we do not have to care about destroying the previously
2477 * used object.
2478 *
2479 * @code
2480 * amg_data.elliptic = true;
2481 * amg_data.higher_order_elements = true;
2482 * amg_data.smoother_sweeps = 2;
2483 * amg_data.aggregation_threshold = 0.02;
2484 * Amg_preconditioner->initialize(stokes_preconditioner_matrix.block(0, 0),
2485 * amg_data);
2486 *
2487 * Mp_preconditioner = std::make_shared<TrilinosWrappers::PreconditionIC>();
2488 * Mp_preconditioner->initialize(stokes_preconditioner_matrix.block(1, 1));
2489 *
2490 * std::cout << std::endl;
2491 *
2492 * rebuild_stokes_preconditioner = false;
2493 * }
2494 *
2495 *
2496 *
2497 * @endcode
2498 *
2499 *
2500 * <a name="BoussinesqFlowProblemassemble_stokes_system"></a>
2501 * <h4>BoussinesqFlowProblem::assemble_stokes_system</h4>
2502 *
2503
2504 *
2505 * The time lag scheme we use for advancing the coupled Stokes-temperature
2506 * system forces us to split up the assembly (and the solution of linear
2507 * systems) into two step. The first one is to create the Stokes system
2508 * matrix and right hand side, and the second is to create matrix and right
2509 * hand sides for the temperature dofs, which depends on the result of the
2510 * linear system for the velocity.
2511 *
2512
2513 *
2514 * This function is called at the beginning of each time step. In the first
2515 * time step or if the mesh has changed, indicated by the
2516 * <code>rebuild_stokes_matrix</code>, we need to assemble the Stokes
2517 * matrix; on the other hand, if the mesh hasn't changed and the matrix is
2518 * already available, this is not necessary and all we need to do is
2519 * assemble the right hand side vector which changes in each time step.
2520 *
2521
2522 *
2523 * Regarding the technical details of implementation, not much has changed
2524 * from @ref step_22 "step-22". We reset matrix and vector, create a quadrature formula on
2525 * the cells, and then create the respective FEValues object. For the update
2526 * flags, we require basis function derivatives only in case of a full
2527 * assembly, since they are not needed for the right hand side; as always,
2528 * choosing the minimal set of flags depending on what is currently needed
2529 * makes the call to FEValues::reinit further down in the program more
2530 * efficient.
2531 *
2532
2533 *
2534 * There is one thing that needs to be commented &ndash; since we have a
2535 * separate finite element and DoFHandler for the temperature, we need to
2536 * generate a second FEValues object for the proper evaluation of the
2537 * temperature solution. This isn't too complicated to realize here: just
2538 * use the temperature structures and set an update flag for the basis
2539 * function values which we need for evaluation of the temperature
2540 * solution. The only important part to remember here is that the same
2541 * quadrature formula is used for both FEValues objects to ensure that we
2542 * get matching information when we loop over the quadrature points of the
2543 * two objects.
2544 *
2545
2546 *
2547 * The declarations proceed with some shortcuts for array sizes, the
2548 * creation of the local matrix and right hand side as well as the vector
2549 * for the indices of the local dofs compared to the global system.
2550 *
2551 * @code
2552 * template <int dim>
2553 * void BoussinesqFlowProblem<dim>::assemble_stokes_system()
2554 * {
2555 * std::cout << " Assembling..." << std::flush;
2556 *
2557 * if (rebuild_stokes_matrix == true)
2558 * stokes_matrix = 0;
2559 *
2560 * stokes_rhs = 0;
2561 *
2562 * const QGauss<dim> quadrature_formula(stokes_degree + 2);
2563 * FEValues<dim> stokes_fe_values(
2564 * stokes_fe,
2565 * quadrature_formula,
2566 * update_values | update_quadrature_points | update_JxW_values |
2567 * (rebuild_stokes_matrix == true ? update_gradients : UpdateFlags(0)));
2568 *
2569 * FEValues<dim> temperature_fe_values(temperature_fe,
2570 * quadrature_formula,
2571 * update_values);
2572 *
2573 * const unsigned int dofs_per_cell = stokes_fe.n_dofs_per_cell();
2574 * const unsigned int n_q_points = quadrature_formula.size();
2575 *
2576 * FullMatrix<double> local_matrix(dofs_per_cell, dofs_per_cell);
2577 * Vector<double> local_rhs(dofs_per_cell);
2578 *
2579 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2580 *
2581 * @endcode
2582 *
2583 * Next we need a vector that will contain the values of the temperature
2584 * solution at the previous time level at the quadrature points to
2585 * assemble the source term in the right hand side of the momentum
2586 * equation. Let's call this vector <code>old_solution_values</code>.
2587 *
2588
2589 *
2590 * The set of vectors we create next hold the evaluations of the basis
2591 * functions as well as their gradients and symmetrized gradients that
2592 * will be used for creating the matrices. Putting these into their own
2593 * arrays rather than asking the FEValues object for this information each
2594 * time it is needed is an optimization to accelerate the assembly
2595 * process, see @ref step_22 "step-22" for details.
2596 *
2597
2598 *
2599 * The last two declarations are used to extract the individual blocks
2600 * (velocity, pressure, temperature) from the total FE system.
2601 *
2602 * @code
2603 * std::vector<double> old_temperature_values(n_q_points);
2604 *
2605 * std::vector<Tensor<1, dim>> phi_u(dofs_per_cell);
2606 * std::vector<SymmetricTensor<2, dim>> grads_phi_u(dofs_per_cell);
2607 * std::vector<double> div_phi_u(dofs_per_cell);
2608 * std::vector<double> phi_p(dofs_per_cell);
2609 *
2610 * const FEValuesExtractors::Vector velocities(0);
2611 * const FEValuesExtractors::Scalar pressure(dim);
2612 *
2613 * @endcode
2614 *
2615 * Now start the loop over all cells in the problem. We are working on two
2616 * different DoFHandlers for this assembly routine, so we must have two
2617 * different cell iterators for the two objects in use. This might seem a
2618 * bit peculiar, since both the Stokes system and the temperature system
2619 * use the same grid, but that's the only way to keep degrees of freedom
2620 * in sync. The first statements within the loop are again all very
2621 * familiar, doing the update of the finite element data as specified by
2622 * the update flags, zeroing out the local arrays and getting the values
2623 * of the old solution at the quadrature points. Then we are ready to loop
2624 * over the quadrature points on the cell.
2625 *
2626 * @code
2627 * auto cell = stokes_dof_handler.begin_active();
2628 * const auto endc = stokes_dof_handler.end();
2629 * auto temperature_cell = temperature_dof_handler.begin_active();
2630 *
2631 * for (; cell != endc; ++cell, ++temperature_cell)
2632 * {
2633 * stokes_fe_values.reinit(cell);
2634 * temperature_fe_values.reinit(temperature_cell);
2635 *
2636 * local_matrix = 0;
2637 * local_rhs = 0;
2638 *
2639 * temperature_fe_values.get_function_values(old_temperature_solution,
2640 * old_temperature_values);
2641 *
2642 * for (unsigned int q = 0; q < n_q_points; ++q)
2643 * {
2644 * const double old_temperature = old_temperature_values[q];
2645 *
2646 * @endcode
2647 *
2648 * Next we extract the values and gradients of basis functions
2649 * relevant to the terms in the inner products. As shown in
2650 * @ref step_22 "step-22" this helps accelerate assembly.
2651 *
2652
2653 *
2654 * Once this is done, we start the loop over the rows and columns
2655 * of the local matrix and feed the matrix with the relevant
2656 * products. The right hand side is filled with the forcing term
2657 * driven by temperature in direction of gravity (which is
2658 * vertical in our example). Note that the right hand side term
2659 * is always generated, whereas the matrix contributions are only
2660 * updated when it is requested by the
2661 * <code>rebuild_matrices</code> flag.
2662 *
2663 * @code
2664 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
2665 * {
2666 * phi_u[k] = stokes_fe_values[velocities].value(k, q);
2667 * if (rebuild_stokes_matrix)
2668 * {
2669 * grads_phi_u[k] =
2670 * stokes_fe_values[velocities].symmetric_gradient(k, q);
2671 * div_phi_u[k] =
2672 * stokes_fe_values[velocities].divergence(k, q);
2673 * phi_p[k] = stokes_fe_values[pressure].value(k, q);
2674 * }
2675 * }
2676 *
2677 * if (rebuild_stokes_matrix)
2678 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2679 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
2680 * local_matrix(i, j) +=
2681 * (EquationData::eta * 2 * (grads_phi_u[i] * grads_phi_u[j]) -
2682 * div_phi_u[i] * phi_p[j] - phi_p[i] * div_phi_u[j]) *
2683 * stokes_fe_values.JxW(q);
2684 *
2685 * const Point<dim> gravity =
2686 * -((dim == 2) ? (Point<dim>(0, 1)) : (Point<dim>(0, 0, 1)));
2687 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2688 * local_rhs(i) += (-EquationData::density * EquationData::beta *
2689 * gravity * phi_u[i] * old_temperature) *
2690 * stokes_fe_values.JxW(q);
2691 * }
2692 *
2693 * @endcode
2694 *
2695 * The last step in the loop over all cells is to enter the local
2696 * contributions into the global matrix and vector structures to the
2697 * positions specified in <code>local_dof_indices</code>. Again, we
2698 * let the AffineConstraints class do the insertion of the cell
2699 * matrix elements to the global matrix, which already condenses the
2700 * hanging node constraints.
2701 *
2702 * @code
2703 * cell->get_dof_indices(local_dof_indices);
2704 *
2705 * if (rebuild_stokes_matrix == true)
2706 * stokes_constraints.distribute_local_to_global(local_matrix,
2707 * local_rhs,
2708 * local_dof_indices,
2709 * stokes_matrix,
2710 * stokes_rhs);
2711 * else
2712 * stokes_constraints.distribute_local_to_global(local_rhs,
2713 * local_dof_indices,
2714 * stokes_rhs);
2715 * }
2716 *
2717 * rebuild_stokes_matrix = false;
2718 *
2719 * std::cout << std::endl;
2720 * }
2721 *
2722 *
2723 *
2724 * @endcode
2725 *
2726 *
2727 * <a name="BoussinesqFlowProblemassemble_temperature_matrix"></a>
2728 * <h4>BoussinesqFlowProblem::assemble_temperature_matrix</h4>
2729 *
2730
2731 *
2732 * This function assembles the matrix in the temperature equation. The
2733 * temperature matrix consists of two parts, a mass matrix and the time step
2734 * size times a stiffness matrix given by a Laplace term times the amount of
2735 * diffusion. Since the matrix depends on the time step size (which varies
2736 * from one step to another), the temperature matrix needs to be updated
2737 * every time step. We could simply regenerate the matrices in every time
2738 * step, but this is not really efficient since mass and Laplace matrix do
2739 * only change when we change the mesh. Hence, we do this more efficiently
2740 * by generating two separate matrices in this function, one for the mass
2741 * matrix and one for the stiffness (diffusion) matrix. We will then sum up
2742 * the matrix plus the stiffness matrix times the time step size once we
2743 * know the actual time step.
2744 *
2745
2746 *
2747 * So the details for this first step are very simple. In case we need to
2748 * rebuild the matrix (i.e., the mesh has changed), we zero the data
2749 * structures, get a quadrature formula and a FEValues object, and create
2750 * local matrices, local dof indices and evaluation structures for the basis
2751 * functions.
2752 *
2753 * @code
2754 * template <int dim>
2755 * void BoussinesqFlowProblem<dim>::assemble_temperature_matrix()
2756 * {
2757 * if (rebuild_temperature_matrices == false)
2758 * return;
2759 *
2760 * temperature_mass_matrix = 0;
2761 * temperature_stiffness_matrix = 0;
2762 *
2763 * QGauss<dim> quadrature_formula(temperature_degree + 2);
2764 * FEValues<dim> temperature_fe_values(temperature_fe,
2765 * quadrature_formula,
2766 * update_values | update_gradients |
2767 * update_JxW_values);
2768 *
2769 * const unsigned int dofs_per_cell = temperature_fe.n_dofs_per_cell();
2770 * const unsigned int n_q_points = quadrature_formula.size();
2771 *
2772 * FullMatrix<double> local_mass_matrix(dofs_per_cell, dofs_per_cell);
2773 * FullMatrix<double> local_stiffness_matrix(dofs_per_cell, dofs_per_cell);
2774 *
2775 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2776 *
2777 * std::vector<double> phi_T(dofs_per_cell);
2778 * std::vector<Tensor<1, dim>> grad_phi_T(dofs_per_cell);
2779 *
2780 * @endcode
2781 *
2782 * Now, let's start the loop over all cells in the triangulation. We need
2783 * to zero out the local matrices, update the finite element evaluations,
2784 * and then loop over the rows and columns of the matrices on each
2785 * quadrature point, where we then create the mass matrix and the
2786 * stiffness matrix (Laplace terms times the diffusion
2787 * <code>EquationData::kappa</code>. Finally, we let the constraints
2788 * object insert these values into the global matrix, and directly
2789 * condense the constraints into the matrix.
2790 *
2791 * @code
2792 * for (const auto &cell : temperature_dof_handler.active_cell_iterators())
2793 * {
2794 * local_mass_matrix = 0;
2795 * local_stiffness_matrix = 0;
2796 *
2797 * temperature_fe_values.reinit(cell);
2798 *
2799 * for (unsigned int q = 0; q < n_q_points; ++q)
2800 * {
2801 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
2802 * {
2803 * grad_phi_T[k] = temperature_fe_values.shape_grad(k, q);
2804 * phi_T[k] = temperature_fe_values.shape_value(k, q);
2805 * }
2806 *
2807 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
2808 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
2809 * {
2810 * local_mass_matrix(i, j) +=
2811 * (phi_T[i] * phi_T[j] * temperature_fe_values.JxW(q));
2812 * local_stiffness_matrix(i, j) +=
2813 * (EquationData::kappa * grad_phi_T[i] * grad_phi_T[j] *
2814 * temperature_fe_values.JxW(q));
2815 * }
2816 * }
2817 *
2818 * cell->get_dof_indices(local_dof_indices);
2819 *
2820 * temperature_constraints.distribute_local_to_global(
2821 * local_mass_matrix, local_dof_indices, temperature_mass_matrix);
2822 * temperature_constraints.distribute_local_to_global(
2823 * local_stiffness_matrix,
2824 * local_dof_indices,
2825 * temperature_stiffness_matrix);
2826 * }
2827 *
2828 * rebuild_temperature_matrices = false;
2829 * }
2830 *
2831 *
2832 *
2833 * @endcode
2834 *
2835 *
2836 * <a name="BoussinesqFlowProblemassemble_temperature_system"></a>
2837 * <h4>BoussinesqFlowProblem::assemble_temperature_system</h4>
2838 *
2839
2840 *
2841 * This function does the second part of the assembly work on the
2842 * temperature matrix, the actual addition of pressure mass and stiffness
2843 * matrix (where the time step size comes into play), as well as the
2844 * creation of the velocity-dependent right hand side. The declarations for
2845 * the right hand side assembly in this function are pretty much the same as
2846 * the ones used in the other assembly routines, except that we restrict
2847 * ourselves to vectors this time. We are going to calculate residuals on
2848 * the temperature system, which means that we have to evaluate second
2849 * derivatives, specified by the update flag <code>update_hessians</code>.
2850 *
2851
2852 *
2853 * The temperature equation is coupled to the Stokes system by means of the
2854 * fluid velocity. These two parts of the solution are associated with
2855 * different DoFHandlers, so we again need to create a second FEValues
2856 * object for the evaluation of the velocity at the quadrature points.
2857 *
2858 * @code
2859 * template <int dim>
2860 * void BoussinesqFlowProblem<dim>::assemble_temperature_system(
2861 * const double maximal_velocity)
2862 * {
2863 * const bool use_bdf2_scheme = (timestep_number != 0);
2864 *
2865 * if (use_bdf2_scheme == true)
2866 * {
2867 * temperature_matrix.copy_from(temperature_mass_matrix);
2868 * temperature_matrix *=
2869 * (2 * time_step + old_time_step) / (time_step + old_time_step);
2870 * temperature_matrix.add(time_step, temperature_stiffness_matrix);
2871 * }
2872 * else
2873 * {
2874 * temperature_matrix.copy_from(temperature_mass_matrix);
2875 * temperature_matrix.add(time_step, temperature_stiffness_matrix);
2876 * }
2877 *
2878 * temperature_rhs = 0;
2879 *
2880 * const QGauss<dim> quadrature_formula(temperature_degree + 2);
2881 * FEValues<dim> temperature_fe_values(temperature_fe,
2882 * quadrature_formula,
2884 * update_hessians |
2887 * FEValues<dim> stokes_fe_values(stokes_fe,
2888 * quadrature_formula,
2889 * update_values);
2890 *
2891 * const unsigned int dofs_per_cell = temperature_fe.n_dofs_per_cell();
2892 * const unsigned int n_q_points = quadrature_formula.size();
2893 *
2894 * Vector<double> local_rhs(dofs_per_cell);
2895 *
2896 * std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
2897 *
2898 * @endcode
2899 *
2900 * Next comes the declaration of vectors to hold the old and older
2901 * solution values (as a notation for time levels @f$n-1@f$ and
2902 * @f$n-2@f$, respectively) and gradients at quadrature points of the
2903 * current cell. We also declare an object to hold the temperature right
2904 * hand side values (<code>gamma_values</code>), and we again use
2905 * shortcuts for the temperature basis functions. Eventually, we need to
2906 * find the temperature extrema and the diameter of the computational
2907 * domain which will be used for the definition of the stabilization
2908 * parameter (we got the maximal velocity as an input to this function).
2909 *
2910 * @code
2911 * std::vector<Tensor<1, dim>> old_velocity_values(n_q_points);
2912 * std::vector<Tensor<1, dim>> old_old_velocity_values(n_q_points);
2913 * std::vector<double> old_temperature_values(n_q_points);
2914 * std::vector<double> old_old_temperature_values(n_q_points);
2915 * std::vector<Tensor<1, dim>> old_temperature_grads(n_q_points);
2916 * std::vector<Tensor<1, dim>> old_old_temperature_grads(n_q_points);
2917 * std::vector<double> old_temperature_laplacians(n_q_points);
2918 * std::vector<double> old_old_temperature_laplacians(n_q_points);
2919 *
2920 * EquationData::TemperatureRightHandSide<dim> temperature_right_hand_side;
2921 * std::vector<double> gamma_values(n_q_points);
2922 *
2923 * std::vector<double> phi_T(dofs_per_cell);
2924 * std::vector<Tensor<1, dim>> grad_phi_T(dofs_per_cell);
2925 *
2926 * const std::pair<double, double> global_T_range =
2927 * get_extrapolated_temperature_range();
2928 *
2929 * const FEValuesExtractors::Vector velocities(0);
2930 *
2931 * @endcode
2932 *
2933 * Now, let's start the loop over all cells in the triangulation. Again,
2934 * we need two cell iterators that walk in parallel through the cells of
2935 * the two involved DoFHandler objects for the Stokes and temperature
2936 * part. Within the loop, we first set the local rhs to zero, and then get
2937 * the values and derivatives of the old solution functions at the
2938 * quadrature points, since they are going to be needed for the definition
2939 * of the stabilization parameters and as coefficients in the equation,
2940 * respectively. Note that since the temperature has its own DoFHandler
2941 * and FEValues object we get the entire solution at the quadrature point
2942 * (which is the scalar temperature field only anyway) whereas for the
2943 * Stokes part we restrict ourselves to extracting the velocity part (and
2944 * ignoring the pressure part) by using
2945 * <code>stokes_fe_values[velocities].get_function_values</code>.
2946 *
2947 * @code
2948 * auto cell = temperature_dof_handler.begin_active();
2949 * const auto endc = temperature_dof_handler.end();
2950 * auto stokes_cell = stokes_dof_handler.begin_active();
2951 *
2952 * for (; cell != endc; ++cell, ++stokes_cell)
2953 * {
2954 * local_rhs = 0;
2955 *
2956 * temperature_fe_values.reinit(cell);
2957 * stokes_fe_values.reinit(stokes_cell);
2958 *
2959 * temperature_fe_values.get_function_values(old_temperature_solution,
2960 * old_temperature_values);
2961 * temperature_fe_values.get_function_values(old_old_temperature_solution,
2962 * old_old_temperature_values);
2963 *
2964 * temperature_fe_values.get_function_gradients(old_temperature_solution,
2965 * old_temperature_grads);
2966 * temperature_fe_values.get_function_gradients(
2967 * old_old_temperature_solution, old_old_temperature_grads);
2968 *
2969 * temperature_fe_values.get_function_laplacians(
2970 * old_temperature_solution, old_temperature_laplacians);
2971 * temperature_fe_values.get_function_laplacians(
2972 * old_old_temperature_solution, old_old_temperature_laplacians);
2973 *
2974 * temperature_right_hand_side.value_list(
2975 * temperature_fe_values.get_quadrature_points(), gamma_values);
2976 *
2977 * stokes_fe_values[velocities].get_function_values(stokes_solution,
2978 * old_velocity_values);
2979 * stokes_fe_values[velocities].get_function_values(
2980 * old_stokes_solution, old_old_velocity_values);
2981 *
2982 * @endcode
2983 *
2984 * Next, we calculate the artificial viscosity for stabilization
2985 * according to the discussion in the introduction using the dedicated
2986 * function. With that at hand, we can get into the loop over
2987 * quadrature points and local rhs vector components. The terms here
2988 * are quite lengthy, but their definition follows the time-discrete
2989 * system developed in the introduction of this program. The BDF-2
2990 * scheme needs one more term from the old time step (and involves
2991 * more complicated factors) than the backward Euler scheme that is
2992 * used for the first time step. When all this is done, we distribute
2993 * the local vector into the global one (including hanging node
2994 * constraints).
2995 *
2996 * @code
2997 * const double nu =
2998 * compute_viscosity(old_temperature_values,
2999 * old_old_temperature_values,
3000 * old_temperature_grads,
3001 * old_old_temperature_grads,
3002 * old_temperature_laplacians,
3003 * old_old_temperature_laplacians,
3004 * old_velocity_values,
3005 * old_old_velocity_values,
3006 * gamma_values,
3007 * maximal_velocity,
3008 * global_T_range.second - global_T_range.first,
3009 * cell->diameter());
3010 *
3011 * for (unsigned int q = 0; q < n_q_points; ++q)
3012 * {
3013 * for (unsigned int k = 0; k < dofs_per_cell; ++k)
3014 * {
3015 * grad_phi_T[k] = temperature_fe_values.shape_grad(k, q);
3016 * phi_T[k] = temperature_fe_values.shape_value(k, q);
3017 * }
3018 *
3019 * const double T_term_for_rhs =
3020 * (use_bdf2_scheme ?
3021 * (old_temperature_values[q] * (1 + time_step / old_time_step) -
3022 * old_old_temperature_values[q] * (time_step * time_step) /
3023 * (old_time_step * (time_step + old_time_step))) :
3024 * old_temperature_values[q]);
3025 *
3026 * const Tensor<1, dim> ext_grad_T =
3027 * (use_bdf2_scheme ?
3028 * (old_temperature_grads[q] * (1 + time_step / old_time_step) -
3029 * old_old_temperature_grads[q] * time_step / old_time_step) :
3030 * old_temperature_grads[q]);
3031 *
3032 * const Tensor<1, dim> extrapolated_u =
3033 * (use_bdf2_scheme ?
3034 * (old_velocity_values[q] * (1 + time_step / old_time_step) -
3035 * old_old_velocity_values[q] * time_step / old_time_step) :
3036 * old_velocity_values[q]);
3037 *
3038 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
3039 * local_rhs(i) +=
3040 * (T_term_for_rhs * phi_T[i] -
3041 * time_step * extrapolated_u * ext_grad_T * phi_T[i] -
3042 * time_step * nu * ext_grad_T * grad_phi_T[i] +
3043 * time_step * gamma_values[q] * phi_T[i]) *
3044 * temperature_fe_values.JxW(q);
3045 * }
3046 *
3047 * cell->get_dof_indices(local_dof_indices);
3048 * temperature_constraints.distribute_local_to_global(local_rhs,
3049 * local_dof_indices,
3050 * temperature_rhs);
3051 * }
3052 * }
3053 *
3054 *
3055 *
3056 * @endcode
3057 *
3058 *
3059 * <a name="BoussinesqFlowProblemsolve"></a>
3060 * <h4>BoussinesqFlowProblem::solve</h4>
3061 *
3062
3063 *
3064 * This function solves the linear systems of equations. Following the
3065 * introduction, we start with the Stokes system, where we need to generate
3066 * our block Schur preconditioner. Since all the relevant actions are
3067 * implemented in the class <code>BlockSchurPreconditioner</code>, all we
3068 * have to do is to initialize the class appropriately. What we need to pass
3069 * down is an <code>InverseMatrix</code> object for the pressure mass
3070 * matrix, which we set up using the respective class together with the IC
3071 * preconditioner we already generated, and the AMG preconditioner for the
3072 * velocity-velocity matrix. Note that both <code>Mp_preconditioner</code>
3073 * and <code>Amg_preconditioner</code> are only pointers, so we use
3074 * <code>*</code> to pass down the actual preconditioner objects.
3075 *
3076
3077 *
3078 * Once the preconditioner is ready, we create a GMRES solver for the block
3079 * system. Since we are working with Trilinos data structures, we have to
3080 * set the respective template argument in the solver. GMRES needs to
3081 * internally store temporary vectors for each iteration (see the discussion
3082 * in the results section of @ref step_22 "step-22") &ndash; the more vectors it can use,
3083 * the better it will generally perform. To keep memory demands in check, we
3084 * set the number of vectors to 100. This means that up to 100 solver
3085 * iterations, every temporary vector can be stored. If the solver needs to
3086 * iterate more often to get the specified tolerance, it will work on a
3087 * reduced set of vectors by restarting at every 100 iterations.
3088 *
3089
3090 *
3091 * With this all set up, we solve the system and distribute the constraints
3092 * in the Stokes system, i.e., hanging nodes and no-flux boundary condition,
3093 * in order to have the appropriate solution values even at constrained
3094 * dofs. Finally, we write the number of iterations to the screen.
3095 *
3096 * @code
3097 * template <int dim>
3098 * void BoussinesqFlowProblem<dim>::solve()
3099 * {
3100 * std::cout << " Solving..." << std::endl;
3101 *
3102 * {
3103 * const LinearSolvers::InverseMatrix<TrilinosWrappers::SparseMatrix,
3104 * TrilinosWrappers::PreconditionIC>
3105 * mp_inverse(stokes_preconditioner_matrix.block(1, 1),
3106 * *Mp_preconditioner);
3107 *
3108 * const LinearSolvers::BlockSchurPreconditioner<
3109 * TrilinosWrappers::PreconditionAMG,
3110 * TrilinosWrappers::PreconditionIC>
3111 * preconditioner(stokes_matrix, mp_inverse, *Amg_preconditioner);
3112 *
3113 * SolverControl solver_control(stokes_matrix.m(),
3114 * 1e-6 * stokes_rhs.l2_norm());
3115 *
3116 * SolverGMRES<TrilinosWrappers::MPI::BlockVector> gmres(
3117 * solver_control,
3118 * SolverGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(100));
3119 *
3120 * for (unsigned int i = 0; i < stokes_solution.size(); ++i)
3121 * if (stokes_constraints.is_constrained(i))
3122 * stokes_solution(i) = 0;
3123 *
3124 * gmres.solve(stokes_matrix, stokes_solution, stokes_rhs, preconditioner);
3125 *
3126 * stokes_constraints.distribute(stokes_solution);
3127 *
3128 * std::cout << " " << solver_control.last_step()
3129 * << " GMRES iterations for Stokes subsystem." << std::endl;
3130 * }
3131 *
3132 * @endcode
3133 *
3134 * Once we know the Stokes solution, we can determine the new time step
3135 * from the maximal velocity. We have to do this to satisfy the CFL
3136 * condition since convection terms are treated explicitly in the
3137 * temperature equation, as discussed in the introduction. The exact form
3138 * of the formula used here for the time step is discussed in the results
3139 * section of this program.
3140 *
3141
3142 *
3143 * There is a snatch here. The formula contains a division by the maximum
3144 * value of the velocity. However, at the start of the computation, we
3145 * have a constant temperature field (we start with a constant
3146 * temperature, and it will be nonconstant only after the first time step
3147 * during which the source acts). Constant temperature means that no
3148 * buoyancy acts, and so the velocity is zero. Dividing by it will not
3149 * likely lead to anything good.
3150 *
3151
3152 *
3153 * To avoid the resulting infinite time step, we ask whether the maximal
3154 * velocity is very small (in particular smaller than the values we
3155 * encounter during any of the following time steps) and if so rather than
3156 * dividing by zero we just divide by a small value, resulting in a large
3157 * but finite time step.
3158 *
3159 * @code
3160 * old_time_step = time_step;
3161 * const double maximal_velocity = get_maximal_velocity();
3162 *
3163 * if (maximal_velocity >= 0.01)
3164 * time_step = 1. / (1.7 * dim * std::sqrt(1. * dim)) / temperature_degree *
3165 * GridTools::minimal_cell_diameter(triangulation) /
3166 * maximal_velocity;
3167 * else
3168 * time_step = 1. / (1.7 * dim * std::sqrt(1. * dim)) / temperature_degree *
3169 * GridTools::minimal_cell_diameter(triangulation) / .01;
3170 *
3171 * std::cout << " "
3172 * << "Time step: " << time_step << std::endl;
3173 *
3174 * temperature_solution = old_temperature_solution;
3175 *
3176 * @endcode
3177 *
3178 * Next we set up the temperature system and the right hand side using the
3179 * function <code>assemble_temperature_system()</code>. Knowing the
3180 * matrix and right hand side of the temperature equation, we set up a
3181 * preconditioner and a solver. The temperature matrix is a mass matrix
3182 * (with eigenvalues around one) plus a Laplace matrix (with eigenvalues
3183 * between zero and @f$ch^{-2}@f$) times a small number proportional to the
3184 * time step @f$k_n@f$. Hence, the resulting symmetric and positive definite
3185 * matrix has eigenvalues in the range @f$[1,1+k_nh^{-2}]@f$ (up to
3186 * constants). This matrix is only moderately ill conditioned even for
3187 * small mesh sizes and we get a reasonably good preconditioner by simple
3188 * means, for example with an incomplete Cholesky decomposition
3189 * preconditioner (IC) as we also use for preconditioning the pressure
3190 * mass matrix solver. As a solver, we choose the conjugate gradient
3191 * method CG. As before, we tell the solver to use Trilinos vectors via
3192 * the template argument <code>TrilinosWrappers::MPI::Vector</code>.
3193 * Finally, we solve, distribute the hanging node constraints and write out
3194 * the number of iterations.
3195 *
3196 * @code
3197 * assemble_temperature_system(maximal_velocity);
3198 * {
3199 * SolverControl solver_control(temperature_matrix.m(),
3200 * 1e-8 * temperature_rhs.l2_norm());
3201 * SolverCG<TrilinosWrappers::MPI::Vector> cg(solver_control);
3202 *
3203 * TrilinosWrappers::PreconditionIC preconditioner;
3204 * preconditioner.initialize(temperature_matrix);
3205 *
3206 * cg.solve(temperature_matrix,
3207 * temperature_solution,
3208 * temperature_rhs,
3209 * preconditioner);
3210 *
3211 * temperature_constraints.distribute(temperature_solution);
3212 *
3213 * std::cout << " " << solver_control.last_step()
3214 * << " CG iterations for temperature." << std::endl;
3215 *
3216 * @endcode
3217 *
3218 * At the end of this function, we step through the vector and read out
3219 * the maximum and minimum temperature value, which we also want to
3220 * output. This will come in handy when determining the correct constant
3221 * in the choice of time step as discuss in the results section of this
3222 * program.
3223 *
3224 * @code
3225 * double min_temperature = temperature_solution(0),
3226 * max_temperature = temperature_solution(0);
3227 * for (unsigned int i = 0; i < temperature_solution.size(); ++i)
3228 * {
3229 * min_temperature =
3230 * std::min<double>(min_temperature, temperature_solution(i));
3231 * max_temperature =
3232 * std::max<double>(max_temperature, temperature_solution(i));
3233 * }
3234 *
3235 * std::cout << " Temperature range: " << min_temperature << ' '
3236 * << max_temperature << std::endl;
3237 * }
3238 * }
3239 *
3240 *
3241 *
3242 * @endcode
3243 *
3244 *
3245 * <a name="BoussinesqFlowProblemoutput_results"></a>
3246 * <h4>BoussinesqFlowProblem::output_results</h4>
3247 *
3248
3249 *
3250 * This function writes the solution to a VTK output file for visualization,
3251 * which is done every tenth time step. This is usually quite a simple task,
3252 * since the deal.II library provides functions that do almost all the job
3253 * for us. There is one new function compared to previous examples: We want
3254 * to visualize both the Stokes solution and the temperature as one data
3255 * set, but we have done all the calculations based on two different
3256 * DoFHandler objects. Luckily, the DataOut class is prepared to deal with
3257 * it. All we have to do is to not attach one single DoFHandler at the
3258 * beginning and then use that for all added vector, but specify the
3259 * DoFHandler to each vector separately. The rest is done as in @ref step_22 "step-22". We
3260 * create solution names (that are going to appear in the visualization
3261 * program for the individual components). The first <code>dim</code>
3262 * components are the vector velocity, and then we have pressure for the
3263 * Stokes part, whereas temperature is scalar. This information is read out
3264 * using the DataComponentInterpretation helper class. Next, we actually
3265 * attach the data vectors with their DoFHandler objects, build patches
3266 * according to the degree of freedom, which are (sub-) elements that
3267 * describe the data for visualization programs. Finally, we open a file
3268 * (that includes the time step number) and write the vtk data into it.
3269 *
3270 * @code
3271 * template <int dim>
3272 * void BoussinesqFlowProblem<dim>::output_results() const
3273 * {
3274 * if (timestep_number % 10 != 0)
3275 * return;
3276 *
3277 * std::vector<std::string> stokes_names(dim, "velocity");
3278 * stokes_names.emplace_back("p");
3279 * std::vector<DataComponentInterpretation::DataComponentInterpretation>
3280 * stokes_component_interpretation(
3281 * dim + 1, DataComponentInterpretation::component_is_scalar);
3282 * for (unsigned int i = 0; i < dim; ++i)
3283 * stokes_component_interpretation[i] =
3284 * DataComponentInterpretation::component_is_part_of_vector;
3285 *
3286 * DataOut<dim> data_out;
3287 * data_out.add_data_vector(stokes_dof_handler,
3288 * stokes_solution,
3289 * stokes_names,
3290 * stokes_component_interpretation);
3291 * data_out.add_data_vector(temperature_dof_handler,
3292 * temperature_solution,
3293 * "T");
3294 * data_out.build_patches(std::min(stokes_degree, temperature_degree));
3295 *
3296 * std::ofstream output("solution-" +
3297 * Utilities::int_to_string(timestep_number, 4) + ".vtk");
3298 * data_out.write_vtk(output);
3299 * }
3300 *
3301 *
3302 *
3303 * @endcode
3304 *
3305 *
3306 * <a name="BoussinesqFlowProblemrefine_mesh"></a>
3307 * <h4>BoussinesqFlowProblem::refine_mesh</h4>
3308 *
3309
3310 *
3311 * This function takes care of the adaptive mesh refinement. The three tasks
3312 * this function performs is to first find out which cells to
3313 * refine/coarsen, then to actually do the refinement and eventually
3314 * transfer the solution vectors between the two different grids. The first
3315 * task is simply achieved by using the well-established Kelly error
3316 * estimator on the temperature (it is the temperature we're mainly
3317 * interested in for this program, and we need to be accurate in regions of
3318 * high temperature gradients, also to not have too much numerical
3319 * diffusion). The second task is to actually do the remeshing. That
3320 * involves only basic functions as well, such as the
3321 * <code>refine_and_coarsen_fixed_fraction</code> that refines those cells
3322 * with the largest estimated error that together make up 80 per cent of the
3323 * error, and coarsens those cells with the smallest error that make up for
3324 * a combined 10 per cent of the error.
3325 *
3326
3327 *
3328 * If implemented like this, we would get a program that will not make much
3329 * progress: Remember that we expect temperature fields that are nearly
3330 * discontinuous (the diffusivity @f$\kappa@f$ is very small after all) and
3331 * consequently we can expect that a freely adapted mesh will refine further
3332 * and further into the areas of large gradients. This decrease in mesh size
3333 * will then be accompanied by a decrease in time step, requiring an
3334 * exceedingly large number of time steps to solve to a given final time. It
3335 * will also lead to meshes that are much better at resolving
3336 * discontinuities after several mesh refinement cycles than in the
3337 * beginning.
3338 *
3339
3340 *
3341 * In particular to prevent the decrease in time step size and the
3342 * correspondingly large number of time steps, we limit the maximal
3343 * refinement depth of the mesh. To this end, after the refinement indicator
3344 * has been applied to the cells, we simply loop over all cells on the
3345 * finest level and unselect them from refinement if they would result in
3346 * too high a mesh level.
3347 *
3348 * @code
3349 * template <int dim>
3350 * void
3351 * BoussinesqFlowProblem<dim>::refine_mesh(const unsigned int max_grid_level)
3352 * {
3353 * Vector<float> estimated_error_per_cell(triangulation.n_active_cells());
3354 *
3355 * KellyErrorEstimator<dim>::estimate(temperature_dof_handler,
3356 * QGauss<dim - 1>(temperature_degree + 1),
3357 * {},
3358 * temperature_solution,
3359 * estimated_error_per_cell);
3360 *
3362 * estimated_error_per_cell,
3363 * 0.8,
3364 * 0.1);
3365 * if (triangulation.n_levels() > max_grid_level)
3366 * for (auto &cell :
3367 * triangulation.active_cell_iterators_on_level(max_grid_level))
3368 * cell->clear_refine_flag();
3369 *
3370 * @endcode
3371 *
3372 * As part of mesh refinement we need to transfer the solution vectors
3373 * from the old mesh to the new one. To this end we use the
3374 * SolutionTransfer class and we have to prepare the solution vectors that
3375 * should be transferred to the new grid (we will lose the old grid once
3376 * we have done the refinement so the transfer has to happen concurrently
3377 * with refinement). What we definitely need are the current and the old
3378 * temperature (BDF-2 time stepping requires two old solutions). Since the
3379 * SolutionTransfer objects only support to transfer one object per dof
3380 * handler, we need to collect the two temperature solutions in one data
3381 * structure. Moreover, we choose to transfer the Stokes solution, too,
3382 * since we need the velocity at two previous time steps, of which only
3383 * one is calculated on the fly.
3384 *
3385
3386 *
3387 * Consequently, we initialize two SolutionTransfer objects for the Stokes
3388 * and temperature DoFHandler objects, by attaching them to the old dof
3389 * handlers. With this at place, we can prepare the triangulation and the
3390 * data vectors for refinement (in this order).
3391 *
3392 * @code
3393 * std::vector<TrilinosWrappers::MPI::Vector> x_temperature(2);
3394 * x_temperature[0] = temperature_solution;
3395 * x_temperature[1] = old_temperature_solution;
3396 * TrilinosWrappers::MPI::BlockVector x_stokes = stokes_solution;
3397 *
3399 * temperature_dof_handler);
3401 * stokes_dof_handler);
3402 *
3403 * triangulation.prepare_coarsening_and_refinement();
3404 * temperature_trans.prepare_for_coarsening_and_refinement(x_temperature);
3405 * stokes_trans.prepare_for_coarsening_and_refinement(x_stokes);
3406 *
3407 * @endcode
3408 *
3409 * Now everything is ready, so do the refinement and recreate the dof
3410 * structure on the new grid, and initialize the matrix structures and the
3411 * new vectors in the <code>setup_dofs</code> function. Next, we actually
3412 * perform the interpolation of the solutions between the grids. We create
3413 * another copy of temporary vectors for temperature (now corresponding to
3414 * the new grid), and let the interpolate function do the job. Then, the
3415 * resulting array of vectors is written into the respective vector member
3416 * variables.
3417 *
3418
3419 *
3420 * Remember that the set of constraints will be updated for the new
3421 * triangulation in the setup_dofs() call.
3422 *
3423 * @code
3424 * triangulation.execute_coarsening_and_refinement();
3425 * setup_dofs();
3426 *
3427 * std::vector<TrilinosWrappers::MPI::Vector> tmp(2);
3428 * tmp[0].reinit(temperature_solution);
3429 * tmp[1].reinit(temperature_solution);
3430 * temperature_trans.interpolate(x_temperature, tmp);
3431 *
3432 * temperature_solution = tmp[0];
3433 * old_temperature_solution = tmp[1];
3434 *
3435 * @endcode
3436 *
3437 * After the solution has been transferred we then enforce the constraints
3438 * on the transferred solution.
3439 *
3440 * @code
3441 * temperature_constraints.distribute(temperature_solution);
3442 * temperature_constraints.distribute(old_temperature_solution);
3443 *
3444 * @endcode
3445 *
3446 * For the Stokes vector, everything is just the same &ndash; except that
3447 * we do not need another temporary vector since we just interpolate a
3448 * single vector. In the end, we have to tell the program that the matrices
3449 * and preconditioners need to be regenerated, since the mesh has changed.
3450 *
3451 * @code
3452 * stokes_trans.interpolate(x_stokes, stokes_solution);
3453 *
3454 * stokes_constraints.distribute(stokes_solution);
3455 *
3456 * rebuild_stokes_matrix = true;
3457 * rebuild_temperature_matrices = true;
3458 * rebuild_stokes_preconditioner = true;
3459 * }
3460 *
3461 *
3462 *
3463 * @endcode
3464 *
3465 *
3466 * <a name="BoussinesqFlowProblemrun"></a>
3467 * <h4>BoussinesqFlowProblem::run</h4>
3468 *
3469
3470 *
3471 * This function performs all the essential steps in the Boussinesq
3472 * program. It starts by setting up a grid (depending on the spatial
3473 * dimension, we choose some different level of initial refinement and
3474 * additional adaptive refinement steps, and then create a cube in
3475 * <code>dim</code> dimensions and set up the dofs for the first time. Since
3476 * we want to start the time stepping already with an adaptively refined
3477 * grid, we perform some pre-refinement steps, consisting of all assembly,
3478 * solution and refinement, but without actually advancing in time. Rather,
3479 * we use the vilified <code>goto</code> statement to jump out of the time
3480 * loop right after mesh refinement to start all over again on the new mesh
3481 * beginning at the <code>start_time_iteration</code> label. (The use of the
3482 * <code>goto</code> is discussed in @ref step_26 "step-26".)
3483 *
3484
3485 *
3486 * Before we start, we project the initial values to the grid and obtain the
3487 * first data for the <code>old_temperature_solution</code> vector. Then, we
3488 * initialize time step number and time step and start the time loop.
3489 *
3490 * @code
3491 * template <int dim>
3492 * void BoussinesqFlowProblem<dim>::run()
3493 * {
3494 * const unsigned int initial_refinement = (dim == 2 ? 4 : 2);
3495 * const unsigned int n_pre_refinement_steps = (dim == 2 ? 4 : 3);
3496 *
3497 *
3499 * global_Omega_diameter = GridTools::diameter(triangulation);
3500 *
3501 * triangulation.refine_global(initial_refinement);
3502 *
3503 * setup_dofs();
3504 *
3505 * unsigned int pre_refinement_step = 0;
3506 *
3507 * start_time_iteration:
3508 *
3509 * VectorTools::project(temperature_dof_handler,
3510 * temperature_constraints,
3511 * QGauss<dim>(temperature_degree + 2),
3512 * EquationData::TemperatureInitialValues<dim>(),
3513 * old_temperature_solution);
3514 *
3515 * timestep_number = 0;
3516 * time_step = old_time_step = 0;
3517 *
3518 * double time = 0;
3519 *
3520 * do
3521 * {
3522 * std::cout << "Timestep " << timestep_number << ": t=" << time
3523 * << std::endl;
3524 *
3525 * @endcode
3526 *
3527 * The first steps in the time loop are all obvious &ndash; we
3528 * assemble the Stokes system, the preconditioner, the temperature
3529 * matrix (matrices and preconditioner do actually only change in case
3530 * we've remeshed before), and then do the solve. Before going on with
3531 * the next time step, we have to check whether we should first finish
3532 * the pre-refinement steps or if we should remesh (every fifth time
3533 * step), refining up to a level that is consistent with initial
3534 * refinement and pre-refinement steps. Last in the loop is to advance
3535 * the solutions, i.e., to copy the solutions to the next "older" time
3536 * level.
3537 *
3538 * @code
3539 * assemble_stokes_system();
3540 * build_stokes_preconditioner();
3541 * assemble_temperature_matrix();
3542 *
3543 * solve();
3544 *
3545 * output_results();
3546 *
3547 * std::cout << std::endl;
3548 *
3549 * if ((timestep_number == 0) &&
3550 * (pre_refinement_step < n_pre_refinement_steps))
3551 * {
3552 * refine_mesh(initial_refinement + n_pre_refinement_steps);
3553 * ++pre_refinement_step;
3554 * goto start_time_iteration;
3555 * }
3556 * else if ((timestep_number > 0) && (timestep_number % 5 == 0))
3557 * refine_mesh(initial_refinement + n_pre_refinement_steps);
3558 *
3559 * time += time_step;
3560 * ++timestep_number;
3561 *
3562 * old_stokes_solution = stokes_solution;
3563 * old_old_temperature_solution = old_temperature_solution;
3564 * old_temperature_solution = temperature_solution;
3565 * }
3566 * @endcode
3567 *
3568 * Do all the above until we arrive at time 100.
3569 *
3570 * @code
3571 * while (time <= 100);
3572 * }
3573 * } // namespace Step31
3574 *
3575 *
3576 *
3577 * @endcode
3578 *
3579 *
3580 * <a name="Thecodemaincodefunction"></a>
3581 * <h3>The <code>main</code> function</h3>
3582 *
3583
3584 *
3585 * The main function looks almost the same as in all other programs.
3586 *
3587
3588 *
3589 * There is one difference we have to be careful about. This program uses
3590 * Trilinos and, typically, Trilinos is configured so that it can run in
3591 * %parallel using MPI. This doesn't mean that it <i>has</i> to run in
3592 * %parallel, and in fact this program (unlike @ref step_32 "step-32") makes no attempt at
3593 * all to do anything in %parallel using MPI. Nevertheless, Trilinos wants the
3594 * MPI system to be initialized. We do that be creating an object of type
3595 * Utilities::MPI::MPI_InitFinalize that initializes MPI (if available) using
3596 * the arguments given to main() (i.e., <code>argc</code> and
3597 * <code>argv</code>) and de-initializes it again when the object goes out of
3598 * scope.
3599 *
3600 * @code
3601 * int main(int argc, char *argv[])
3602 * {
3603 * try
3604 * {
3605 * using namespace dealii;
3606 * using namespace Step31;
3607 *
3608 * Utilities::MPI::MPI_InitFinalize mpi_initialization(
3609 * argc, argv, numbers::invalid_unsigned_int);
3610 *
3611 * @endcode
3612 *
3613 * This program can only be run in serial. Otherwise, throw an exception.
3614 *
3615 * @code
3616 * AssertThrow(Utilities::MPI::n_mpi_processes(MPI_COMM_WORLD) == 1,
3617 * ExcMessage(
3618 * "This program can only be run in serial, use ./step-31"));
3619 *
3620 * BoussinesqFlowProblem<2> flow_problem;
3621 * flow_problem.run();
3622 * }
3623 * catch (std::exception &exc)
3624 * {
3625 * std::cerr << std::endl
3626 * << std::endl
3627 * << "----------------------------------------------------"
3628 * << std::endl;
3629 * std::cerr << "Exception on processing: " << std::endl
3630 * << exc.what() << std::endl
3631 * << "Aborting!" << std::endl
3632 * << "----------------------------------------------------"
3633 * << std::endl;
3634 *
3635 * return 1;
3636 * }
3637 * catch (...)
3638 * {
3639 * std::cerr << std::endl
3640 * << std::endl
3641 * << "----------------------------------------------------"
3642 * << std::endl;
3643 * std::cerr << "Unknown exception!" << std::endl
3644 * << "Aborting!" << std::endl
3645 * << "----------------------------------------------------"
3646 * << std::endl;
3647 * return 1;
3648 * }
3649 *
3650 * return 0;
3651 * }
3652 * @endcode
3653<a name="Results"></a><h1>Results</h1>
3654
3655
3656<a name="Resultsin2d"></a><h3> Results in 2d </h3>
3657
3658
3659When you run the program in 2d, the output will look something like
3660this:
3661<code>
3662<pre>
3663Number of active cells: 256 (on 5 levels)
3664Number of degrees of freedom: 3556 (2178+289+1089)
3665
3666Timestep 0: t=0
3667 Assembling...
3668 Rebuilding Stokes preconditioner...
3669 Solving...
3670 0 GMRES iterations for Stokes subsystem.
3671 Time step: 0.919118
3672 9 CG iterations for temperature.
3673 Temperature range: -0.16687 1.30011
3674
3675Number of active cells: 280 (on 6 levels)
3676Number of degrees of freedom: 4062 (2490+327+1245)
3677
3678Timestep 0: t=0
3679 Assembling...
3680 Rebuilding Stokes preconditioner...
3681 Solving...
3682 0 GMRES iterations for Stokes subsystem.
3683 Time step: 0.459559
3684 9 CG iterations for temperature.
3685 Temperature range: -0.0982971 0.598503
3686
3687Number of active cells: 520 (on 7 levels)
3688Number of degrees of freedom: 7432 (4562+589+2281)
3689
3690Timestep 0: t=0
3691 Assembling...
3692 Rebuilding Stokes preconditioner...
3693 Solving...
3694 0 GMRES iterations for Stokes subsystem.
3695 Time step: 0.229779
3696 9 CG iterations for temperature.
3697 Temperature range: -0.0551098 0.294493
3698
3699Number of active cells: 1072 (on 8 levels)
3700Number of degrees of freedom: 15294 (9398+1197+4699)
3701
3702Timestep 0: t=0
3703 Assembling...
3704 Rebuilding Stokes preconditioner...
3705 Solving...
3706 0 GMRES iterations for Stokes subsystem.
3707 Time step: 0.11489
3708 9 CG iterations for temperature.
3709 Temperature range: -0.0273524 0.156861
3710
3711Number of active cells: 2116 (on 9 levels)
3712Number of degrees of freedom: 30114 (18518+2337+9259)
3713
3714Timestep 0: t=0
3715 Assembling...
3716 Rebuilding Stokes preconditioner...
3717 Solving...
3718 0 GMRES iterations for Stokes subsystem.
3719 Time step: 0.0574449
3720 9 CG iterations for temperature.
3721 Temperature range: -0.014993 0.0738328
3722
3723Timestep 1: t=0.0574449
3724 Assembling...
3725 Solving...
3726 56 GMRES iterations for Stokes subsystem.
3727 Time step: 0.0574449
3728 9 CG iterations for temperature.
3729 Temperature range: -0.0273934 0.14488
3730
3731...
3732</pre>
3733</code>
3734
3735In the beginning we refine the mesh several times adaptively and
3736always return to time step zero to restart on the newly refined
3737mesh. Only then do we start the actual time iteration.
3738
3739The program runs for a while. The temperature field for time steps 0,
3740500, 1000, 1500, 2000, 3000, 4000, and 5000 looks like this (note that
3741the color scale used for the temperature is not always the same):
3742
3743<table align="center" class="doxtable">
3744 <tr>
3745 <td>
3746 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.00.png" alt="">
3747 </td>
3748 <td>
3749 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.01.png" alt="">
3750 </td>
3751 <td>
3752 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.02.png" alt="">
3753 </td>
3754 <td>
3755 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.03.png" alt="">
3756 </td>
3757 </tr>
3758 <tr>
3759 <td>
3760 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.04.png" alt="">
3761 </td>
3762 <td>
3763 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.05.png" alt="">
3764 </td>
3765 <td>
3766 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.06.png" alt="">
3767 </td>
3768 <td>
3769 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.solution.07.png" alt="">
3770 </td>
3771 </tr>
3772</table>
3773
3774The visualizations shown here were generated using a version of the example
3775which did not enforce the constraints after transferring the mesh.
3776
3777As can be seen, we have three heat sources that heat fluid and
3778therefore produce a buoyancy effect that lets hots pockets of fluid
3779rise up and swirl around. By a chimney effect, the three streams are
3780pressed together by fluid that comes from the outside and wants to
3781join the updraft party. Note that because the fluid is initially at
3782rest, those parts of the fluid that were initially over the sources
3783receive a longer heating time than that fluid that is later dragged
3784over the source by the fully developed flow field. It is therefore
3785hotter, a fact that can be seen in the red tips of the three
3786plumes. Note also the relatively fine features of the flow field, a
3787result of the sophisticated transport stabilization of the temperature
3788equation we have chosen.
3789
3790In addition to the pictures above, the following ones show the
3791adaptive mesh and the flow field at the same time steps:
3792
3793<table align="center" class="doxtable">
3794 <tr>
3795 <td>
3796 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.00.png" alt="">
3797 </td>
3798 <td>
3799 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.01.png" alt="">
3800 </td>
3801 <td>
3802 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.02.png" alt="">
3803 </td>
3804 <td>
3805 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.03.png" alt="">
3806 </td>
3807 </tr>
3808 <tr>
3809 <td>
3810 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.04.png" alt="">
3811 </td>
3812 <td>
3813 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.05.png" alt="">
3814 </td>
3815 <td>
3816 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.06.png" alt="">
3817 </td>
3818 <td>
3819 <img src="https://www.dealii.org/images/steps/developer/step-31.2d.grid.07.png" alt="">
3820 </td>
3821 </tr>
3822</table>
3823
3824
3825<a name="Resultsin3d"></a><h3> Results in 3d </h3>
3826
3827
3828The same thing can of course be done in 3d by changing the template
3829parameter to the BoussinesqFlowProblem object in <code>main()</code>
3830from 2 to 3, so that the output now looks like follows:
3831
3832<code>
3833<pre>
3834Number of active cells: 64 (on 3 levels)
3835Number of degrees of freedom: 3041 (2187+125+729)
3836
3837Timestep 0: t=0
3838 Assembling...
3839 Rebuilding Stokes preconditioner...
3840 Solving...
3841 0 GMRES iterations for Stokes subsystem.
3842 Time step: 2.45098
3843 9 CG iterations for temperature.
3844 Temperature range: -0.675683 4.94725
3845
3846Number of active cells: 288 (on 4 levels)
3847Number of degrees of freedom: 12379 (8943+455+2981)
3848
3849Timestep 0: t=0
3850 Assembling...
3851 Rebuilding Stokes preconditioner...
3852 Solving...
3853 0 GMRES iterations for Stokes subsystem.
3854 Time step: 1.22549
3855 9 CG iterations for temperature.
3856 Temperature range: -0.527701 2.25764
3857
3858Number of active cells: 1296 (on 5 levels)
3859Number of degrees of freedom: 51497 (37305+1757+12435)
3860
3861Timestep 0: t=0
3862 Assembling...
3863 Rebuilding Stokes preconditioner...
3864 Solving...
3865 0 GMRES iterations for Stokes subsystem.
3866 Time step: 0.612745
3867 10 CG iterations for temperature.
3868 Temperature range: -0.496942 0.847395
3869
3870Number of active cells: 5048 (on 6 levels)
3871Number of degrees of freedom: 192425 (139569+6333+46523)
3872
3873Timestep 0: t=0
3874 Assembling...
3875 Rebuilding Stokes preconditioner...
3876 Solving...
3877 0 GMRES iterations for Stokes subsystem.
3878 Time step: 0.306373
3879 10 CG iterations for temperature.
3880 Temperature range: -0.267683 0.497739
3881
3882Timestep 1: t=0.306373
3883 Assembling...
3884 Solving...
3885 27 GMRES iterations for Stokes subsystem.
3886 Time step: 0.306373
3887 10 CG iterations for temperature.
3888 Temperature range: -0.461787 0.958679
3889
3890...
3891</pre>
3892</code>
3893
3894Visualizing the temperature isocontours at time steps 0,
389550, 100, 150, 200, 300, 400, 500, 600, 700, and 800 yields the
3896following plots:
3897
3898<table align="center" class="doxtable">
3899 <tr>
3900 <td>
3901 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.00.png" alt="">
3902 </td>
3903 <td>
3904 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.01.png" alt="">
3905 </td>
3906 <td>
3907 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.02.png" alt="">
3908 </td>
3909 <td>
3910 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.03.png" alt="">
3911 </td>
3912 </tr>
3913 <tr>
3914 <td>
3915 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.04.png" alt="">
3916 </td>
3917 <td>
3918 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.05.png" alt="">
3919 </td>
3920 <td>
3921 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.06.png" alt="">
3922 </td>
3923 <td>
3924 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.07.png" alt="">
3925 </td>
3926 </tr>
3927 <tr>
3928 <td>
3929 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.08.png" alt="">
3930 </td>
3931 <td>
3932 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.09.png" alt="">
3933 </td>
3934 <td>
3935 <img src="https://www.dealii.org/images/steps/developer/step-31.3d.solution.10.png" alt="">
3936 </td>
3937 <td>
3938 </td>
3939 </tr>
3940</table>
3941
3942That the first picture looks like three hedgehogs stems from the fact that our
3943scheme essentially projects the source times the first time step size onto the
3944mesh to obtain the temperature field in the first time step. Since the source
3945function is discontinuous, we need to expect over- and undershoots from this
3946project. This is in fact what happens (it's easier to check this in 2d) and
3947leads to the crumpled appearance of the isosurfaces. The visualizations shown
3948here were generated using a version of the example which did not enforce the
3949constraints after transferring the mesh.
3950
3951
3952
3953<a name="Numericalexperimentstodetermineoptimalparameters"></a><h3> Numerical experiments to determine optimal parameters </h3>
3954
3955
3956The program as is has three parameters that we don't have much of a
3957theoretical handle on how to choose in an optimal way. These are:
3958<ul>
3959 <li>The time step must satisfy a CFL condition
3960 @f$k\le \min_K \frac{c_kh_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$. Here, @f$c_k@f$ is
3961 dimensionless, but what is the right value?
3962 <li>In the computation of the artificial viscosity,
3963@f{eqnarray*}
3964 \nu_\alpha(T)|_K
3965 =
3966 \beta
3967 \|\mathbf{u}\|_{L^\infty(K)}
3968 \min\left\{
3969 h_K,
3970 h_K^\alpha
3971 \frac{\|R_\alpha(T)\|_{L^\infty(K)}}{c(\mathbf{u},T)}
3972 \right\},
3973@f}
3974 with @f$c(\mathbf{u},T) =
3975 c_R\ \|\mathbf{u}\|_{L^\infty(\Omega)} \ \mathrm{var}(T)
3976 \ |\mathrm{diam}(\Omega)|^{\alpha-2}@f$.
3977 Here, the choice of the dimensionless %numbers @f$\beta,c_R@f$ is of
3978 interest.
3979</ul>
3980In all of these cases, we will have to expect that the correct choice of each
3981value depends on that of the others, and most likely also on the space
3982dimension and polynomial degree of the finite element used for the
3983temperature. Below we'll discuss a few numerical experiments to choose
3984constants @f$c_k@f$ and @f$\beta@f$.
3985
3986Below, we will not discuss the choice of @f$c_R@f$. In the program, we set
3987it to @f$c_R=2^{\frac{4-2\alpha}{d}}@f$. The reason for this value is a
3988bit complicated and has more to do with the history of the program
3989than reasoning: while the correct formula for the global scaling
3990parameter @f$c(\mathbf{u},T)@f$ is shown above, the program (including the
3991version shipped with deal.II 6.2) initially had a bug in that we
3992computed
3993@f$c(\mathbf{u},T) =
3994 \|\mathbf{u}\|_{L^\infty(\Omega)} \ \mathrm{var}(T)
3995 \ \frac{1}{|\mathrm{diam}(\Omega)|^{\alpha-2}}@f$ instead, where
3996we had set the scaling parameter to one. Since we only computed on the
3997unit square/cube where @f$\mathrm{diam}(\Omega)=2^{1/d}@f$, this was
3998entirely equivalent to using the correct formula with
3999@f$c_R=\left(2^{1/d}\right)^{4-2\alpha}=2^{\frac{4-2\alpha}{d}}@f$. Since
4000this value for @f$c_R@f$ appears to work just fine for the current
4001program, we corrected the formula in the program and set @f$c_R@f$ to a
4002value that reproduces exactly the results we had before. We will,
4003however, revisit this issue again in @ref step_32 "step-32".
4004
4005Now, however, back to the discussion of what values of @f$c_k@f$ and
4006@f$\beta@f$ to choose:
4007
4008
4009<a name="Choosingicsubksubiandbeta"></a><h4> Choosing <i>c<sub>k</sub></i> and beta </h4>
4010
4011
4012These two constants are definitely linked in some way. The reason is easy to
4013see: In the case of a pure advection problem,
4014@f$\frac{\partial T}{\partial t} + \mathbf{u}\cdot\nabla T = \gamma@f$, any
4015explicit scheme has to satisfy a CFL condition of the form
4016@f$k\le \min_K \frac{c_k^a h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$. On the other hand,
4017for a pure diffusion problem,
4018@f$\frac{\partial T}{\partial t} + \nu \Delta T = \gamma@f$,
4019explicit schemes need to satisfy a condition
4020@f$k\le \min_K \frac{c_k^d h_K^2}{\nu}@f$. So given the form of @f$\nu@f$ above, an
4021advection diffusion problem like the one we have to solve here will result in
4022a condition of the form
4023@f$
4024k\le \min_K \min \left\{
4025 \frac{c_k^a h_K}{\|\mathbf{u}\|_{L^\infty(K)}},
4026 \frac{c_k^d h_K^2}{\beta \|\mathbf{u}\|_{L^\infty(K)} h_K}\right\}
4027 =
4028 \min_K \left( \min \left\{
4029 c_k^a,
4030 \frac{c_k^d}{\beta}\right\}
4031 \frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}} \right)
4032@f$.
4033It follows that we have to face the fact that we might want to choose @f$\beta@f$
4034larger to improve the stability of the numerical scheme (by increasing the
4035amount of artificial diffusion), but we have to pay a price in the form of
4036smaller, and consequently more time steps. In practice, one would therefore
4037like to choose @f$\beta@f$ as small as possible to keep the transport problem
4038sufficiently stabilized while at the same time trying to choose the time step
4039as large as possible to reduce the overall amount of work.
4040
4041The find the right balance, the only way is to do a few computational
4042experiments. Here's what we did: We modified the program slightly to allow
4043less mesh refinement (so we don't always have to wait that long) and to choose
4044@f$
4045 \nu(T)|_K
4046 =
4047 \beta
4048 \|\mathbf{u}\|_{L^\infty(K)} h_K
4049@f$ to eliminate the effect of the constant @f$c_R@f$ (we know that
4050solutions are stable by using this version of @f$\nu(T)@f$ as an artificial
4051viscosity, but that we can improve things -- i.e. make the solution
4052sharper -- by using the more complicated formula for this artificial
4053viscosity). We then run the program
4054for different values @f$c_k,\beta@f$ and observe maximal and minimal temperatures
4055in the domain. What we expect to see is this: If we choose the time step too
4056big (i.e. choose a @f$c_k@f$ bigger than theoretically allowed) then we will get
4057exponential growth of the temperature. If we choose @f$\beta@f$ too small, then
4058the transport stabilization becomes insufficient and the solution will show
4059significant oscillations but not exponential growth.
4060
4061
4062<a name="ResultsforQsub1subelements"></a><h5>Results for Q<sub>1</sub> elements</h5>
4063
4064
4065Here is what we get for
4066@f$\beta=0.01, \beta=0.1@f$, and @f$\beta=0.5@f$, different choices of @f$c_k@f$, and
4067bilinear elements (<code>temperature_degree=1</code>) in 2d:
4068
4069<table align="center" class="doxtable">
4070 <tr>
4071 <td>
4072 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q1.beta=0.01.png" alt="">
4073 </td>
4074 <td>
4075 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q1.beta=0.03.png" alt="">
4076 </td>
4077 </tr>
4078 <tr>
4079 <td>
4080 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q1.beta=0.1.png" alt="">
4081 </td>
4082 <td>
4083 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q1.beta=0.5.png" alt="">
4084 </td>
4085 </tr>
4086</table>
4087
4088The way to interpret these graphs goes like this: for @f$\beta=0.01@f$ and
4089@f$c_k=\frac 12,\frac 14@f$, we see exponential growth or at least large
4090variations, but if we choose
4091@f$k=\frac 18\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$
4092or smaller, then the scheme is
4093stable though a bit wobbly. For more artificial diffusion, we can choose
4094@f$k=\frac 14\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$
4095or smaller for @f$\beta=0.03@f$,
4096@f$k=\frac 13\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$
4097or smaller for @f$\beta=0.1@f$, and again need
4098@f$k=\frac 1{15}\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$
4099for @f$\beta=0.5@f$ (this time because much diffusion requires a small time
4100step).
4101
4102So how to choose? If we were simply interested in a large time step, then we
4103would go with @f$\beta=0.1@f$ and
4104@f$k=\frac 13\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$.
4105On the other hand, we're also interested in accuracy and here it may be of
4106interest to actually investigate what these curves show. To this end note that
4107we start with a zero temperature and that our sources are positive &mdash; so
4108we would intuitively expect that the temperature can never drop below
4109zero. But it does, a consequence of Gibb's phenomenon when using continuous
4110elements to approximate a discontinuous solution. We can therefore see that
4111choosing @f$\beta@f$ too small is bad: too little artificial diffusion leads to
4112over- and undershoots that aren't diffused away. On the other hand, for large
4113@f$\beta@f$, the minimum temperature drops below zero at the beginning but then
4114quickly diffuses back to zero.
4115
4116On the other hand, let's also look at the maximum temperature. Watching the
4117movie of the solution, we see that initially the fluid is at rest. The source
4118keeps heating the same volume of fluid whose temperature increases linearly at
4119the beginning until its buoyancy is able to move it upwards. The hottest part
4120of the fluid is therefore transported away from the solution and fluid taking
4121its place is heated for only a short time before being moved out of the source
4122region, therefore remaining cooler than the initial bubble. If @f$\kappa=0@f$
4123(in the program it is nonzero but very small) then the hottest part of the
4124fluid should be advected along with the flow with its temperature
4125constant. That's what we can see in the graphs with the smallest @f$\beta@f$: Once
4126the maximum temperature is reached, it hardly changes any more. On the other
4127hand, the larger the artificial diffusion, the more the hot spot is
4128diffused. Note that for this criterion, the time step size does not play a
4129significant role.
4130
4131So to sum up, likely the best choice would appear to be @f$\beta=0.03@f$
4132and @f$k=\frac 14\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$. The curve is
4133a bit wobbly, but overall pictures looks pretty reasonable with the
4134exception of some over and undershoots close to the start time due to
4135Gibb's phenomenon.
4136
4137
4138<a name="ResultsforQsub2subelements"></a><h5>Results for Q<sub>2</sub> elements</h5>
4139
4140
4141One can repeat the same sequence of experiments for higher order
4142elements as well. Here are the graphs for bi-quadratic shape functions
4143(<code>temperature_degree=2</code>) for the temperature, while we
4144retain the @f$Q_2/Q_1@f$ stable Taylor-Hood element for the Stokes system:
4145
4146<table align="center" class="doxtable">
4147 <tr>
4148 <td>
4149 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q2.beta=0.01.png" alt="">
4150 </td>
4151 <td>
4152 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q2.beta=0.03.png" alt="">
4153 </td>
4154 </tr>
4155 <tr>
4156 <td>
4157 <img src="https://www.dealii.org/images/steps/developer/step-31.timestep.q2.beta=0.1.png" alt="">
4158 </td>
4159 </tr>
4160</table>
4161
4162Again, small values of @f$\beta@f$ lead to less diffusion but we have to
4163choose the time step very small to keep things under control. Too
4164large values of @f$\beta@f$ make for more diffusion, but again require
4165small time steps. The best value would appear to be @f$\beta=0.03@f$, as
4166for the @f$Q_1@f$ element, and then we have to choose
4167@f$k=\frac 18\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$ &mdash; exactly
4168half the size for the @f$Q_1@f$ element, a fact that may not be surprising
4169if we state the CFL condition as the requirement that the time step be
4170small enough so that the distance transport advects in each time step
4171is no longer than one <i>grid point</i> away (which for @f$Q_1@f$ elements
4172is @f$h_K@f$, but for @f$Q_2@f$ elements is @f$h_K/2@f$). It turns out that @f$\beta@f$
4173needs to be slightly larger for obtaining stable results also late in
4174the simulation at times larger than 60, so we actually choose it as
4175@f$\beta = 0.034@f$ in the code.
4176
4177
4178<a name="Resultsfor3d"></a><h5>Results for 3d</h5>
4179
4180
4181One can repeat these experiments in 3d and find the optimal time step
4182for each value of @f$\beta@f$ and find the best value of @f$\beta@f$. What one
4183finds is that for the same @f$\beta@f$ already used in 2d, the time steps
4184needs to be a bit smaller, by around a factor of 1.2 or so. This is
4185easily explained: the time step restriction is
4186@f$k=\min_K \frac{ch_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$ where @f$h_K@f$ is
4187the <i>diameter</i> of the cell. However, what is really needed is the
4188distance between mesh points, which is @f$\frac{h_K}{\sqrt{d}}@f$. So a
4189more appropriate form would be
4190@f$k=\min_K \frac{ch_K}{\|\mathbf{u}\|_{L^\infty(K)}\sqrt{d}}@f$.
4191
4192The second find is that one needs to choose @f$\beta@f$ slightly bigger
4193(about @f$\beta=0.05@f$ or so). This then again reduces the time step we
4194can take.
4195
4196
4197
4198
4199<a name="Conclusions"></a><h5>Conclusions</h5>
4200
4201
4202Concluding, from the simple computations above, @f$\beta=0.034@f$ appears to be a
4203good choice for the stabilization parameter in 2d, and @f$\beta=0.05@f$ in 3d. In
4204a dimension independent way, we can model this as @f$\beta=0.017d@f$. If one does
4205longer computations (several thousand time steps) on finer meshes, one
4206realizes that the time step size is not quite small enough and that for
4207stability one will have to reduce the above values a bit more (by about a
4208factor of @f$\frac 78@f$).
4209
4210As a consequence, a formula that reconciles 2d, 3d, and variable polynomial
4211degree and takes all factors in account reads as follows:
4212@f{eqnarray*}
4213 k =
4214 \frac 1{2 \cdot 1.7} \frac 1{\sqrt{d}}
4215 \frac 2d
4216 \frac 1{q_T}
4217 \frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}
4218 =
4219 \frac 1{1.7 d\sqrt{d}}
4220 \frac 1{q_T}
4221 \frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}.
4222@f}
4223In the first form (in the center of the equation), @f$\frac
42241{2 \cdot 1.7}@f$ is a universal constant, @f$\frac 1{\sqrt{d}}@f$
4225is the factor that accounts for the difference between cell diameter
4226and grid point separation,
4227@f$\frac 2d@f$ accounts for the increase in @f$\beta@f$ with space dimension,
4228@f$\frac 1{q_T}@f$ accounts for the distance between grid points for
4229higher order elements, and @f$\frac{h_K}{\|\mathbf{u}\|_{L^\infty(K)}}@f$
4230for the local speed of transport relative to the cell size. This is
4231the formula that we use in the program.
4232
4233As for the question of whether to use @f$Q_1@f$ or @f$Q_2@f$ elements for the
4234temperature, the following considerations may be useful: First,
4235solving the temperature equation is hardly a factor in the overall
4236scheme since almost the entire compute time goes into solving the
4237Stokes system in each time step. Higher order elements for the
4238temperature equation are therefore not a significant drawback. On the
4239other hand, if one compares the size of the over- and undershoots the
4240solution produces due to the discontinuous source description, one
4241notices that for the choice of @f$\beta@f$ and @f$k@f$ as above, the @f$Q_1@f$
4242solution dips down to around @f$-0.47@f$, whereas the @f$Q_2@f$ solution only
4243goes to @f$-0.13@f$ (remember that the exact solution should never become
4244negative at all. This means that the @f$Q_2@f$ solution is significantly
4245more accurate; the program therefore uses these higher order elements,
4246despite the penalty we pay in terms of smaller time steps.
4247
4248
4249<a name="Possibilitiesforextensions"></a><h3> Possibilities for extensions </h3>
4250
4251
4252There are various ways to extend the current program. Of particular interest
4253is, of course, to make it faster and/or increase the resolution of the
4254program, in particular in 3d. This is the topic of the @ref step_32 "step-32"
4255tutorial program which will implement strategies to solve this problem in
4256%parallel on a cluster. It is also the basis of the much larger open
4257source code ASPECT (see https://aspect.geodynamics.org/ ) that can solve realistic
4258problems and that constitutes the further development of @ref step_32 "step-32".
4259
4260Another direction would be to make the fluid flow more realistic. The program
4261was initially written to simulate various cases simulating the convection of
4262material in the earth's mantle, i.e. the zone between the outer earth core and
4263the solid earth crust: there, material is heated from below and cooled from
4264above, leading to thermal convection. The physics of this fluid are much more
4265complicated than shown in this program, however: The viscosity of mantle
4266material is strongly dependent on the temperature, i.e. @f$\eta=\eta(T)@f$, with
4267the dependency frequently modeled as a viscosity that is reduced exponentially
4268with rising temperature. Secondly, much of the dynamics of the mantle is
4269determined by chemical reactions, primarily phase changes of the various
4270crystals that make up the mantle; the buoyancy term on the right hand side of
4271the Stokes equations then depends not only on the temperature, but also on the
4272chemical composition at a given location which is advected by the flow field
4273but also changes as a function of pressure and temperature. We will
4274investigate some of these effects in later tutorial programs as well.
4275 *
4276 *
4277<a name="PlainProg"></a>
4278<h1> The plain program</h1>
4279@include "step-31.cc"
4280*/
void reinit(const TriaIterator< DoFCellAccessor< dim, spacedim, level_dof_access > > &cell)
Definition: fe_q.h:549
const std::vector< Point< dim > > & get_unit_support_points() const
const unsigned int n_components
Definition: function.h:164
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
IndexSet complete_index_set(const IndexSet::size_type N)
Definition: index_set.h:1013
static void estimate(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const Quadrature< dim - 1 > &quadrature, const std::map< types::boundary_id, const Function< spacedim, typename InputVector::value_type > * > &neumann_bc, const InputVector &solution, Vector< float > &error, const ComponentMask &component_mask=ComponentMask(), const Function< spacedim > *coefficients=nullptr, const unsigned int n_threads=numbers::invalid_unsigned_int, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id, const types::material_id material_id=numbers::invalid_material_id, const Strategy strategy=cell_diameter_over_24)
Definition: point.h:111
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
constexpr Number trace(const SymmetricTensor< 2, dim, Number > &d)
Definition: vector.h:110
VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &x, const ::VectorizedArray< Number, width > &y)
VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &x, const ::VectorizedArray< Number, width > &y)
float depth
Point< 3 > vertices[4]
@ update_hessians
Second derivatives of shape functions.
@ update_values
Shape function values.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
Point< 2 > second
Definition: grid_out.cc:4588
Point< 2 > first
Definition: grid_out.cc:4587
unsigned int level
Definition: grid_out.cc:4590
__global__ void set(Number *val, const Number s, const size_type N)
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
Definition: exceptions.h:1575
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:439
const Event initial
Definition: event.cc:65
void approximate(SynchronousIterators< std::tuple< typename DoFHandler< dim, spacedim >::active_cell_iterator, Vector< float >::iterator > > const &cell, const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof_handler, const InputVector &solution, const unsigned int component)
Expression sign(const Expression &x)
void downstream(DoFHandler< dim, spacedim > &dof_handler, const Tensor< 1, spacedim > &direction, const bool dof_wise_renumbering=false)
void interpolate(const DoFHandler< dim, spacedim > &dof1, const InVector &u1, const DoFHandler< dim, spacedim > &dof2, OutVector &u2)
void hyper_cube(Triangulation< dim, spacedim > &tria, const double left=0., const double right=1., const bool colorize=false)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void refine_and_coarsen_fixed_fraction(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double top_fraction, const double bottom_fraction, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max(), const VectorTools::NormType norm_type=VectorTools::NormType::L1_norm)
void scale(const double scaling_factor, Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:2042
double diameter(const Triangulation< dim, spacedim > &tria)
Definition: grid_tools.cc:81
@ valid
Iterator points to a valid object.
static const char L
static const types::blas_int zero
@ matrix
Contents is actually a matrix.
static const char A
static const char T
static const types::blas_int one
SparseMatrix< double > SparseMatrix
BlockVector< double > BlockVector
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition: divergence.h:472
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void partition(const SparsityPattern &sparsity_pattern, const unsigned int n_partitions, std::vector< unsigned int > &partition_indices, const Partitioner partitioner=Partitioner::metis)
constexpr ReturnType< rank, T >::value_type & extract(T &t, const ArrayType &indices)
void call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
VectorType::value_type * end(VectorType &V)
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:117
void project(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const AffineConstraints< typename VectorType::value_type > &constraints, const Quadrature< dim > &quadrature, const Function< spacedim, typename VectorType::value_type > &function, VectorType &vec, const bool enforce_zero_boundary=false, const Quadrature< dim - 1 > &q_boundary=(dim > 1 ? QGauss< dim - 1 >(2) :Quadrature< dim - 1 >(0)), const bool project_to_boundary_first=false)
void run(const Iterator &begin, const typename identity< Iterator >::type &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
Definition: work_stream.h:472
void abort(const ExceptionBase &exc) noexcept
Definition: exceptions.cc:449
long double gamma(const unsigned int n)
void copy(const T *begin, const T *end, U *dest)
int(&) functions(const void *v1, const void *v2)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
Definition: loop.h:71
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
Definition: matrix_block.h:618
static const unsigned int invalid_unsigned_int
Definition: types.h:196
STL namespace.
::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 > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation