Reference documentation for deal.II version 9.6.0
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
step-32.h
Go to the documentation of this file.
1
1508 *   constexpr double kappa = 1e-6; /* m^2 / s */
1509 *   constexpr double reference_density = 3300; /* kg / m^3 */
1510 *   constexpr double reference_temperature = 293; /* K */
1511 *   constexpr double expansion_coefficient = 2e-5; /* 1/K */
1512 *   constexpr double specific_heat = 1250; /* J / K / kg */
1513 *   constexpr double radiogenic_heating = 7.4e-12; /* W / kg */
1514 *  
1515 *  
1516 *   constexpr double R0 = 6371000. - 2890000.; /* m */
1517 *   constexpr double R1 = 6371000. - 35000.; /* m */
1518 *  
1519 *   constexpr double T0 = 4000 + 273; /* K */
1520 *   constexpr double T1 = 700 + 273; /* K */
1521 *  
1522 *  
1523 * @endcode
1524 *
1525 * The next set of definitions are for functions that encode the density
1526 * as a function of temperature, the gravity vector, and the initial
1527 * values for the temperature. Again, all of these (along with the values
1528 * they compute) are discussed in the introduction:
1529 *
1530 * @code
1531 *   double density(const double temperature)
1532 *   {
1533 *   return (
1534 *   reference_density *
1535 *   (1 - expansion_coefficient * (temperature - reference_temperature)));
1536 *   }
1537 *  
1538 *  
1539 *   template <int dim>
1540 *   Tensor<1, dim> gravity_vector(const Point<dim> &p)
1541 *   {
1542 *   const double r = p.norm();
1543 *   return -(1.245e-6 * r + 7.714e13 / r / r) * p / r;
1544 *   }
1545 *  
1546 *  
1547 *  
1548 *   template <int dim>
1549 *   class TemperatureInitialValues : public Function<dim>
1550 *   {
1551 *   public:
1552 *   TemperatureInitialValues()
1553 *   : Function<dim>(1)
1554 *   {}
1555 *  
1556 *   virtual double value(const Point<dim> &p,
1557 *   const unsigned int component = 0) const override;
1558 *  
1559 *   virtual void vector_value(const Point<dim> &p,
1560 *   Vector<double> &value) const override;
1561 *   };
1562 *  
1563 *  
1564 *  
1565 *   template <int dim>
1566 *   double TemperatureInitialValues<dim>::value(const Point<dim> &p,
1567 *   const unsigned int) const
1568 *   {
1569 *   const double r = p.norm();
1570 *   const double h = R1 - R0;
1571 *  
1572 *   const double s = (r - R0) / h;
1573 *   const double q =
1574 *   (dim == 3) ? std::max(0.0, cos(numbers::PI * abs(p[2] / R1))) : 1.0;
1575 *   const double phi = std::atan2(p[0], p[1]);
1576 *   const double tau = s + 0.2 * s * (1 - s) * std::sin(6 * phi) * q;
1577 *  
1578 *   return T0 * (1.0 - tau) + T1 * tau;
1579 *   }
1580 *  
1581 *  
1582 *   template <int dim>
1583 *   void
1584 *   TemperatureInitialValues<dim>::vector_value(const Point<dim> &p,
1585 *   Vector<double> &values) const
1586 *   {
1587 *   for (unsigned int c = 0; c < this->n_components; ++c)
1588 *   values(c) = TemperatureInitialValues<dim>::value(p, c);
1589 *   }
1590 *  
1591 *  
1592 * @endcode
1593 *
1594 * As mentioned in the introduction we need to rescale the pressure to
1595 * avoid the relative ill-conditioning of the momentum and mass
1596 * conservation equations. The scaling factor is @f$\frac{\eta}{L}@f$ where
1597 * @f$L@f$ was a typical length scale. By experimenting it turns out that a
1598 * good length scale is the diameter of plumes, which is around 10 km:
1599 *
1600 * @code
1601 *   constexpr double pressure_scaling = eta / 10000;
1602 *  
1603 * @endcode
1604 *
1605 * The final number in this namespace is a constant that denotes the
1606 * number of seconds per (average, tropical) year. We use this only when
1607 * generating screen output: internally, all computations of this program
1608 * happen in SI units (kilogram, meter, seconds) but writing geological
1609 * times in seconds yields numbers that one can't relate to reality, and
1610 * so we convert to years using the factor defined here:
1611 *
1612 * @code
1613 *   const double year_in_seconds = 60 * 60 * 24 * 365.2425;
1614 *  
1615 *   } // namespace EquationData
1616 *  
1617 *  
1618 *  
1619 * @endcode
1620 *
1621 *
1622 * <a name="step_32-PreconditioningtheStokessystem"></a>
1623 * <h3>Preconditioning the Stokes system</h3>
1624 *
1625
1626 *
1627 * This namespace implements the preconditioner. As discussed in the
1628 * introduction, this preconditioner differs in a number of key portions
1629 * from the one used in @ref step_31 "step-31". Specifically, it is a right preconditioner,
1630 * implementing the matrix
1631 * @f{align*}{
1632 * \left(\begin{array}{cc}A^{-1} & -A^{-1}B^TS^{-1}
1633 * \\0 & S^{-1}
1634 * \end{array}\right)
1635 * @f}
1636 * where the two inverse matrix operations
1637 * are approximated by linear solvers or, if the right flag is given to the
1638 * constructor of this class, by a single AMG V-cycle for the velocity
1639 * block. The three code blocks of the <code>vmult</code> function implement
1640 * the multiplications with the three blocks of this preconditioner matrix
1641 * and should be self explanatory if you have read through @ref step_31 "step-31" or the
1642 * discussion of composing solvers in @ref step_20 "step-20".
1643 *
1644 * @code
1645 *   namespace LinearSolvers
1646 *   {
1647 *   template <class PreconditionerTypeA, class PreconditionerTypeMp>
1648 *   class BlockSchurPreconditioner : public Subscriptor
1649 *   {
1650 *   public:
1651 *   BlockSchurPreconditioner(const TrilinosWrappers::BlockSparseMatrix &S,
1652 *   const TrilinosWrappers::BlockSparseMatrix &Spre,
1653 *   const PreconditionerTypeMp &Mppreconditioner,
1654 *   const PreconditionerTypeA &Apreconditioner,
1655 *   const bool do_solve_A)
1656 *   : stokes_matrix(&S)
1657 *   , stokes_preconditioner_matrix(&Spre)
1658 *   , mp_preconditioner(Mppreconditioner)
1659 *   , a_preconditioner(Apreconditioner)
1660 *   , do_solve_A(do_solve_A)
1661 *   {}
1662 *  
1663 *   void vmult(TrilinosWrappers::MPI::BlockVector &dst,
1664 *   const TrilinosWrappers::MPI::BlockVector &src) const
1665 *   {
1666 *   TrilinosWrappers::MPI::Vector utmp(src.block(0));
1667 *  
1668 *   {
1669 *   SolverControl solver_control(5000, 1e-6 * src.block(1).l2_norm());
1670 *  
1671 *   SolverCG<TrilinosWrappers::MPI::Vector> solver(solver_control);
1672 *  
1673 *   solver.solve(stokes_preconditioner_matrix->block(1, 1),
1674 *   dst.block(1),
1675 *   src.block(1),
1676 *   mp_preconditioner);
1677 *  
1678 *   dst.block(1) *= -1.0;
1679 *   }
1680 *  
1681 *   {
1682 *   stokes_matrix->block(0, 1).vmult(utmp, dst.block(1));
1683 *   utmp *= -1.0;
1684 *   utmp.add(src.block(0));
1685 *   }
1686 *  
1687 *   if (do_solve_A == true)
1688 *   {
1689 *   SolverControl solver_control(5000, utmp.l2_norm() * 1e-2);
1690 *   TrilinosWrappers::SolverCG solver(solver_control);
1691 *   solver.solve(stokes_matrix->block(0, 0),
1692 *   dst.block(0),
1693 *   utmp,
1694 *   a_preconditioner);
1695 *   }
1696 *   else
1697 *   a_preconditioner.vmult(dst.block(0), utmp);
1698 *   }
1699 *  
1700 *   private:
1701 *   const SmartPointer<const TrilinosWrappers::BlockSparseMatrix>
1702 *   stokes_matrix;
1703 *   const SmartPointer<const TrilinosWrappers::BlockSparseMatrix>
1704 *   stokes_preconditioner_matrix;
1705 *   const PreconditionerTypeMp &mp_preconditioner;
1706 *   const PreconditionerTypeA &a_preconditioner;
1707 *   const bool do_solve_A;
1708 *   };
1709 *   } // namespace LinearSolvers
1710 *  
1711 *  
1712 *  
1713 * @endcode
1714 *
1715 *
1716 * <a name="step_32-Definitionofassemblydatastructures"></a>
1717 * <h3>Definition of assembly data structures</h3>
1718 *
1719
1720 *
1721 * As described in the introduction, we will use the WorkStream mechanism
1722 * discussed in the @ref threads topic to parallelize operations among the
1723 * processors of a single machine. The WorkStream class requires that data
1724 * is passed around in two kinds of data structures, one for scratch data
1725 * and one to pass data from the assembly function to the function that
1726 * copies local contributions into global objects.
1727 *
1728
1729 *
1730 * The following namespace (and the two sub-namespaces) contains a
1731 * collection of data structures that serve this purpose, one pair for each
1732 * of the four operations discussed in the introduction that we will want to
1733 * parallelize. Each assembly routine gets two sets of data: a Scratch array
1734 * that collects all the classes and arrays that are used for the
1735 * calculation of the cell contribution, and a CopyData array that keeps
1736 * local matrices and vectors which will be written into the global
1737 * matrix. Whereas CopyData is a container for the final data that is
1738 * written into the global matrices and vector (and, thus, absolutely
1739 * necessary), the Scratch arrays are merely there for performance reasons
1740 * &mdash; it would be much more expensive to set up a FEValues object on
1741 * each cell, than creating it only once and updating some derivative data.
1742 *
1743
1744 *
1745 * @ref step_31 "step-31" had four assembly routines: One for the preconditioner matrix of
1746 * the Stokes system, one for the Stokes matrix and right hand side, one for
1747 * the temperature matrices and one for the right hand side of the
1748 * temperature equation. We here organize the scratch arrays and CopyData
1749 * objects for each of those four assembly components using a
1750 * <code>struct</code> environment (since we consider these as temporary
1751 * objects we pass around, rather than classes that implement functionality
1752 * of their own, though this is a more subjective point of view to
1753 * distinguish between <code>struct</code>s and <code>class</code>es).
1754 *
1755
1756 *
1757 * Regarding the Scratch objects, each struct is equipped with a constructor
1758 * that creates an @ref FEValues object using the @ref FiniteElement,
1759 * Quadrature, @ref Mapping (which describes the interpolation of curved
1760 * boundaries), and @ref UpdateFlags instances. Moreover, we manually
1761 * implement a copy constructor (since the FEValues class is not copyable by
1762 * itself), and provide some additional vector fields that are used to hold
1763 * intermediate data during the computation of local contributions.
1764 *
1765
1766 *
1767 * Let us start with the scratch arrays and, specifically, the one used for
1768 * assembly of the Stokes preconditioner:
1769 *
1770 * @code
1771 *   namespace Assembly
1772 *   {
1773 *   namespace Scratch
1774 *   {
1775 *   template <int dim>
1776 *   struct StokesPreconditioner
1777 *   {
1778 *   StokesPreconditioner(const FiniteElement<dim> &stokes_fe,
1779 *   const Quadrature<dim> &stokes_quadrature,
1780 *   const Mapping<dim> &mapping,
1781 *   const UpdateFlags update_flags);
1782 *  
1783 *   StokesPreconditioner(const StokesPreconditioner &data);
1784 *  
1785 *  
1786 *   FEValues<dim> stokes_fe_values;
1787 *  
1788 *   std::vector<Tensor<2, dim>> grad_phi_u;
1789 *   std::vector<double> phi_p;
1790 *   };
1791 *  
1792 *   template <int dim>
1793 *   StokesPreconditioner<dim>::StokesPreconditioner(
1794 *   const FiniteElement<dim> &stokes_fe,
1795 *   const Quadrature<dim> &stokes_quadrature,
1796 *   const Mapping<dim> &mapping,
1797 *   const UpdateFlags update_flags)
1798 *   : stokes_fe_values(mapping, stokes_fe, stokes_quadrature, update_flags)
1799 *   , grad_phi_u(stokes_fe.n_dofs_per_cell())
1800 *   , phi_p(stokes_fe.n_dofs_per_cell())
1801 *   {}
1802 *  
1803 *  
1804 *  
1805 *   template <int dim>
1806 *   StokesPreconditioner<dim>::StokesPreconditioner(
1807 *   const StokesPreconditioner &scratch)
1808 *   : stokes_fe_values(scratch.stokes_fe_values.get_mapping(),
1809 *   scratch.stokes_fe_values.get_fe(),
1810 *   scratch.stokes_fe_values.get_quadrature(),
1811 *   scratch.stokes_fe_values.get_update_flags())
1812 *   , grad_phi_u(scratch.grad_phi_u)
1813 *   , phi_p(scratch.phi_p)
1814 *   {}
1815 *  
1816 *  
1817 *  
1818 * @endcode
1819 *
1820 * The next one is the scratch object used for the assembly of the full
1821 * Stokes system. Observe that we derive the StokesSystem scratch class
1822 * from the StokesPreconditioner class above. We do this because all the
1823 * objects that are necessary for the assembly of the preconditioner are
1824 * also needed for the actual matrix system and right hand side, plus
1825 * some extra data. This makes the program more compact. Note also that
1826 * the assembly of the Stokes system and the temperature right hand side
1827 * further down requires data from temperature and velocity,
1828 * respectively, so we actually need two FEValues objects for those two
1829 * cases.
1830 *
1831 * @code
1832 *   template <int dim>
1833 *   struct StokesSystem : public StokesPreconditioner<dim>
1834 *   {
1835 *   StokesSystem(const FiniteElement<dim> &stokes_fe,
1836 *   const Mapping<dim> &mapping,
1837 *   const Quadrature<dim> &stokes_quadrature,
1838 *   const UpdateFlags stokes_update_flags,
1839 *   const FiniteElement<dim> &temperature_fe,
1840 *   const UpdateFlags temperature_update_flags);
1841 *  
1842 *   StokesSystem(const StokesSystem<dim> &data);
1843 *  
1844 *  
1845 *   FEValues<dim> temperature_fe_values;
1846 *  
1847 *   std::vector<Tensor<1, dim>> phi_u;
1848 *   std::vector<SymmetricTensor<2, dim>> grads_phi_u;
1849 *   std::vector<double> div_phi_u;
1850 *  
1851 *   std::vector<double> old_temperature_values;
1852 *   };
1853 *  
1854 *  
1855 *   template <int dim>
1856 *   StokesSystem<dim>::StokesSystem(
1857 *   const FiniteElement<dim> &stokes_fe,
1858 *   const Mapping<dim> &mapping,
1859 *   const Quadrature<dim> &stokes_quadrature,
1860 *   const UpdateFlags stokes_update_flags,
1861 *   const FiniteElement<dim> &temperature_fe,
1862 *   const UpdateFlags temperature_update_flags)
1863 *   : StokesPreconditioner<dim>(stokes_fe,
1864 *   stokes_quadrature,
1865 *   mapping,
1866 *   stokes_update_flags)
1867 *   , temperature_fe_values(mapping,
1868 *   temperature_fe,
1869 *   stokes_quadrature,
1870 *   temperature_update_flags)
1871 *   , phi_u(stokes_fe.n_dofs_per_cell())
1872 *   , grads_phi_u(stokes_fe.n_dofs_per_cell())
1873 *   , div_phi_u(stokes_fe.n_dofs_per_cell())
1874 *   , old_temperature_values(stokes_quadrature.size())
1875 *   {}
1876 *  
1877 *  
1878 *   template <int dim>
1879 *   StokesSystem<dim>::StokesSystem(const StokesSystem<dim> &scratch)
1880 *   : StokesPreconditioner<dim>(scratch)
1881 *   , temperature_fe_values(
1882 *   scratch.temperature_fe_values.get_mapping(),
1883 *   scratch.temperature_fe_values.get_fe(),
1884 *   scratch.temperature_fe_values.get_quadrature(),
1885 *   scratch.temperature_fe_values.get_update_flags())
1886 *   , phi_u(scratch.phi_u)
1887 *   , grads_phi_u(scratch.grads_phi_u)
1888 *   , div_phi_u(scratch.div_phi_u)
1889 *   , old_temperature_values(scratch.old_temperature_values)
1890 *   {}
1891 *  
1892 *  
1893 * @endcode
1894 *
1895 * After defining the objects used in the assembly of the Stokes system,
1896 * we do the same for the assembly of the matrices necessary for the
1897 * temperature system. The general structure is very similar:
1898 *
1899 * @code
1900 *   template <int dim>
1901 *   struct TemperatureMatrix
1902 *   {
1903 *   TemperatureMatrix(const FiniteElement<dim> &temperature_fe,
1904 *   const Mapping<dim> &mapping,
1905 *   const Quadrature<dim> &temperature_quadrature);
1906 *  
1907 *   TemperatureMatrix(const TemperatureMatrix &data);
1908 *  
1909 *  
1910 *   FEValues<dim> temperature_fe_values;
1911 *  
1912 *   std::vector<double> phi_T;
1913 *   std::vector<Tensor<1, dim>> grad_phi_T;
1914 *   };
1915 *  
1916 *  
1917 *   template <int dim>
1918 *   TemperatureMatrix<dim>::TemperatureMatrix(
1919 *   const FiniteElement<dim> &temperature_fe,
1920 *   const Mapping<dim> &mapping,
1921 *   const Quadrature<dim> &temperature_quadrature)
1922 *   : temperature_fe_values(mapping,
1923 *   temperature_fe,
1924 *   temperature_quadrature,
1925 *   update_values | update_gradients |
1926 *   update_JxW_values)
1927 *   , phi_T(temperature_fe.n_dofs_per_cell())
1928 *   , grad_phi_T(temperature_fe.n_dofs_per_cell())
1929 *   {}
1930 *  
1931 *  
1932 *   template <int dim>
1933 *   TemperatureMatrix<dim>::TemperatureMatrix(
1934 *   const TemperatureMatrix &scratch)
1935 *   : temperature_fe_values(
1936 *   scratch.temperature_fe_values.get_mapping(),
1937 *   scratch.temperature_fe_values.get_fe(),
1938 *   scratch.temperature_fe_values.get_quadrature(),
1939 *   scratch.temperature_fe_values.get_update_flags())
1940 *   , phi_T(scratch.phi_T)
1941 *   , grad_phi_T(scratch.grad_phi_T)
1942 *   {}
1943 *  
1944 *  
1945 * @endcode
1946 *
1947 * The final scratch object is used in the assembly of the right hand
1948 * side of the temperature system. This object is significantly larger
1949 * than the ones above because a lot more quantities enter the
1950 * computation of the right hand side of the temperature equation. In
1951 * particular, the temperature values and gradients of the previous two
1952 * time steps need to be evaluated at the quadrature points, as well as
1953 * the velocities and the strain rates (i.e. the symmetric gradients of
1954 * the velocity) that enter the right hand side as friction heating
1955 * terms. Despite the number of terms, the following should be rather
1956 * self explanatory:
1957 *
1958 * @code
1959 *   template <int dim>
1960 *   struct TemperatureRHS
1961 *   {
1962 *   TemperatureRHS(const FiniteElement<dim> &temperature_fe,
1963 *   const FiniteElement<dim> &stokes_fe,
1964 *   const Mapping<dim> &mapping,
1965 *   const Quadrature<dim> &quadrature);
1966 *  
1967 *   TemperatureRHS(const TemperatureRHS &data);
1968 *  
1969 *  
1970 *   FEValues<dim> temperature_fe_values;
1971 *   FEValues<dim> stokes_fe_values;
1972 *  
1973 *   std::vector<double> phi_T;
1974 *   std::vector<Tensor<1, dim>> grad_phi_T;
1975 *  
1976 *   std::vector<Tensor<1, dim>> old_velocity_values;
1977 *   std::vector<Tensor<1, dim>> old_old_velocity_values;
1978 *  
1979 *   std::vector<SymmetricTensor<2, dim>> old_strain_rates;
1980 *   std::vector<SymmetricTensor<2, dim>> old_old_strain_rates;
1981 *  
1982 *   std::vector<double> old_temperature_values;
1983 *   std::vector<double> old_old_temperature_values;
1984 *   std::vector<Tensor<1, dim>> old_temperature_grads;
1985 *   std::vector<Tensor<1, dim>> old_old_temperature_grads;
1986 *   std::vector<double> old_temperature_laplacians;
1987 *   std::vector<double> old_old_temperature_laplacians;
1988 *   };
1989 *  
1990 *  
1991 *   template <int dim>
1992 *   TemperatureRHS<dim>::TemperatureRHS(
1993 *   const FiniteElement<dim> &temperature_fe,
1994 *   const FiniteElement<dim> &stokes_fe,
1995 *   const Mapping<dim> &mapping,
1996 *   const Quadrature<dim> &quadrature)
1997 *   : temperature_fe_values(mapping,
1998 *   temperature_fe,
1999 *   quadrature,
2000 *   update_values | update_gradients |
2001 *   update_hessians | update_quadrature_points |
2002 *   update_JxW_values)
2003 *   , stokes_fe_values(mapping,
2004 *   stokes_fe,
2005 *   quadrature,
2006 *   update_values | update_gradients)
2007 *   , phi_T(temperature_fe.n_dofs_per_cell())
2008 *   , grad_phi_T(temperature_fe.n_dofs_per_cell())
2009 *   ,
2010 *  
2011 *   old_velocity_values(quadrature.size())
2012 *   , old_old_velocity_values(quadrature.size())
2013 *   , old_strain_rates(quadrature.size())
2014 *   , old_old_strain_rates(quadrature.size())
2015 *   ,
2016 *  
2017 *   old_temperature_values(quadrature.size())
2018 *   , old_old_temperature_values(quadrature.size())
2019 *   , old_temperature_grads(quadrature.size())
2020 *   , old_old_temperature_grads(quadrature.size())
2021 *   , old_temperature_laplacians(quadrature.size())
2022 *   , old_old_temperature_laplacians(quadrature.size())
2023 *   {}
2024 *  
2025 *  
2026 *   template <int dim>
2027 *   TemperatureRHS<dim>::TemperatureRHS(const TemperatureRHS &scratch)
2028 *   : temperature_fe_values(
2029 *   scratch.temperature_fe_values.get_mapping(),
2030 *   scratch.temperature_fe_values.get_fe(),
2031 *   scratch.temperature_fe_values.get_quadrature(),
2032 *   scratch.temperature_fe_values.get_update_flags())
2033 *   , stokes_fe_values(scratch.stokes_fe_values.get_mapping(),
2034 *   scratch.stokes_fe_values.get_fe(),
2035 *   scratch.stokes_fe_values.get_quadrature(),
2036 *   scratch.stokes_fe_values.get_update_flags())
2037 *   , phi_T(scratch.phi_T)
2038 *   , grad_phi_T(scratch.grad_phi_T)
2039 *   ,
2040 *  
2041 *   old_velocity_values(scratch.old_velocity_values)
2042 *   , old_old_velocity_values(scratch.old_old_velocity_values)
2043 *   , old_strain_rates(scratch.old_strain_rates)
2044 *   , old_old_strain_rates(scratch.old_old_strain_rates)
2045 *   ,
2046 *  
2047 *   old_temperature_values(scratch.old_temperature_values)
2048 *   , old_old_temperature_values(scratch.old_old_temperature_values)
2049 *   , old_temperature_grads(scratch.old_temperature_grads)
2050 *   , old_old_temperature_grads(scratch.old_old_temperature_grads)
2051 *   , old_temperature_laplacians(scratch.old_temperature_laplacians)
2052 *   , old_old_temperature_laplacians(scratch.old_old_temperature_laplacians)
2053 *   {}
2054 *   } // namespace Scratch
2055 *  
2056 *  
2057 * @endcode
2058 *
2059 * The CopyData objects are even simpler than the Scratch objects as all
2060 * they have to do is to store the results of local computations until
2061 * they can be copied into the global matrix or vector objects. These
2062 * structures therefore only need to provide a constructor, a copy
2063 * operation, and some arrays for local matrix, local vectors and the
2064 * relation between local and global degrees of freedom (a.k.a.
2065 * <code>local_dof_indices</code>). Again, we have one such structure for
2066 * each of the four operations we will parallelize using the WorkStream
2067 * class:
2068 *
2069 * @code
2070 *   namespace CopyData
2071 *   {
2072 *   template <int dim>
2073 *   struct StokesPreconditioner
2074 *   {
2075 *   StokesPreconditioner(const FiniteElement<dim> &stokes_fe);
2076 *   StokesPreconditioner(const StokesPreconditioner &data);
2077 *   StokesPreconditioner &operator=(const StokesPreconditioner &) = default;
2078 *  
2079 *   FullMatrix<double> local_matrix;
2080 *   std::vector<types::global_dof_index> local_dof_indices;
2081 *   };
2082 *  
2083 *   template <int dim>
2084 *   StokesPreconditioner<dim>::StokesPreconditioner(
2085 *   const FiniteElement<dim> &stokes_fe)
2086 *   : local_matrix(stokes_fe.n_dofs_per_cell(), stokes_fe.n_dofs_per_cell())
2087 *   , local_dof_indices(stokes_fe.n_dofs_per_cell())
2088 *   {}
2089 *  
2090 *   template <int dim>
2091 *   StokesPreconditioner<dim>::StokesPreconditioner(
2092 *   const StokesPreconditioner &data)
2093 *   : local_matrix(data.local_matrix)
2094 *   , local_dof_indices(data.local_dof_indices)
2095 *   {}
2096 *  
2097 *  
2098 *  
2099 *   template <int dim>
2100 *   struct StokesSystem : public StokesPreconditioner<dim>
2101 *   {
2102 *   StokesSystem(const FiniteElement<dim> &stokes_fe);
2103 *  
2104 *   Vector<double> local_rhs;
2105 *   };
2106 *  
2107 *   template <int dim>
2108 *   StokesSystem<dim>::StokesSystem(const FiniteElement<dim> &stokes_fe)
2109 *   : StokesPreconditioner<dim>(stokes_fe)
2110 *   , local_rhs(stokes_fe.n_dofs_per_cell())
2111 *   {}
2112 *  
2113 *  
2114 *  
2115 *   template <int dim>
2116 *   struct TemperatureMatrix
2117 *   {
2118 *   TemperatureMatrix(const FiniteElement<dim> &temperature_fe);
2119 *  
2120 *   FullMatrix<double> local_mass_matrix;
2121 *   FullMatrix<double> local_stiffness_matrix;
2122 *   std::vector<types::global_dof_index> local_dof_indices;
2123 *   };
2124 *  
2125 *   template <int dim>
2126 *   TemperatureMatrix<dim>::TemperatureMatrix(
2127 *   const FiniteElement<dim> &temperature_fe)
2128 *   : local_mass_matrix(temperature_fe.n_dofs_per_cell(),
2129 *   temperature_fe.n_dofs_per_cell())
2130 *   , local_stiffness_matrix(temperature_fe.n_dofs_per_cell(),
2131 *   temperature_fe.n_dofs_per_cell())
2132 *   , local_dof_indices(temperature_fe.n_dofs_per_cell())
2133 *   {}
2134 *  
2135 *  
2136 *  
2137 *   template <int dim>
2138 *   struct TemperatureRHS
2139 *   {
2140 *   TemperatureRHS(const FiniteElement<dim> &temperature_fe);
2141 *  
2142 *   Vector<double> local_rhs;
2143 *   std::vector<types::global_dof_index> local_dof_indices;
2144 *   FullMatrix<double> matrix_for_bc;
2145 *   };
2146 *  
2147 *   template <int dim>
2148 *   TemperatureRHS<dim>::TemperatureRHS(
2149 *   const FiniteElement<dim> &temperature_fe)
2150 *   : local_rhs(temperature_fe.n_dofs_per_cell())
2151 *   , local_dof_indices(temperature_fe.n_dofs_per_cell())
2152 *   , matrix_for_bc(temperature_fe.n_dofs_per_cell(),
2153 *   temperature_fe.n_dofs_per_cell())
2154 *   {}
2155 *   } // namespace CopyData
2156 *   } // namespace Assembly
2157 *  
2158 *  
2159 *  
2160 * @endcode
2161 *
2162 *
2163 * <a name="step_32-ThecodeBoussinesqFlowProblemcodeclasstemplate"></a>
2164 * <h3>The <code>BoussinesqFlowProblem</code> class template</h3>
2165 *
2166
2167 *
2168 * This is the declaration of the main class. It is very similar to @ref step_31 "step-31"
2169 * but there are a number differences we will comment on below.
2170 *
2171
2172 *
2173 * The top of the class is essentially the same as in @ref step_31 "step-31", listing the
2174 * public methods and a set of private functions that do the heavy
2175 * lifting. Compared to @ref step_31 "step-31" there are only two additions to this
2176 * section: the function <code>get_cfl_number()</code> that computes the
2177 * maximum CFL number over all cells which we then compute the global time
2178 * step from, and the function <code>get_entropy_variation()</code> that is
2179 * used in the computation of the entropy stabilization. It is akin to the
2180 * <code>get_extrapolated_temperature_range()</code> we have used in @ref step_31 "step-31"
2181 * for this purpose, but works on the entropy instead of the temperature
2182 * instead.
2183 *
2184 * @code
2185 *   template <int dim>
2186 *   class BoussinesqFlowProblem
2187 *   {
2188 *   public:
2189 *   struct Parameters;
2190 *   BoussinesqFlowProblem(Parameters &parameters);
2191 *   void run();
2192 *  
2193 *   private:
2194 *   void setup_dofs();
2195 *   void assemble_stokes_preconditioner();
2196 *   void build_stokes_preconditioner();
2197 *   void assemble_stokes_system();
2198 *   void assemble_temperature_matrix();
2199 *   void assemble_temperature_system(const double maximal_velocity);
2200 *   double get_maximal_velocity() const;
2201 *   double get_cfl_number() const;
2202 *   double get_entropy_variation(const double average_temperature) const;
2203 *   std::pair<double, double> get_extrapolated_temperature_range() const;
2204 *   void solve();
2205 *   void output_results();
2206 *   void refine_mesh(const unsigned int max_grid_level);
2207 *  
2208 *   double compute_viscosity(
2209 *   const std::vector<double> &old_temperature,
2210 *   const std::vector<double> &old_old_temperature,
2211 *   const std::vector<Tensor<1, dim>> &old_temperature_grads,
2212 *   const std::vector<Tensor<1, dim>> &old_old_temperature_grads,
2213 *   const std::vector<double> &old_temperature_laplacians,
2214 *   const std::vector<double> &old_old_temperature_laplacians,
2215 *   const std::vector<Tensor<1, dim>> &old_velocity_values,
2216 *   const std::vector<Tensor<1, dim>> &old_old_velocity_values,
2217 *   const std::vector<SymmetricTensor<2, dim>> &old_strain_rates,
2218 *   const std::vector<SymmetricTensor<2, dim>> &old_old_strain_rates,
2219 *   const double global_u_infty,
2220 *   const double global_T_variation,
2221 *   const double average_temperature,
2222 *   const double global_entropy_variation,
2223 *   const double cell_diameter) const;
2224 *  
2225 *   public:
2226 * @endcode
2227 *
2228 * The first significant new component is the definition of a struct for
2229 * the parameters according to the discussion in the introduction. This
2230 * structure is initialized by reading from a parameter file during
2231 * construction of this object.
2232 *
2233 * @code
2234 *   struct Parameters
2235 *   {
2236 *   Parameters(const std::string &parameter_filename);
2237 *  
2238 *   static void declare_parameters(ParameterHandler &prm);
2239 *   void parse_parameters(ParameterHandler &prm);
2240 *  
2241 *   double end_time;
2242 *  
2243 *   unsigned int initial_global_refinement;
2244 *   unsigned int initial_adaptive_refinement;
2245 *  
2246 *   bool generate_graphical_output;
2247 *   unsigned int graphical_output_interval;
2248 *  
2249 *   unsigned int adaptive_refinement_interval;
2250 *  
2251 *   double stabilization_alpha;
2252 *   double stabilization_c_R;
2253 *   double stabilization_beta;
2254 *  
2255 *   unsigned int stokes_velocity_degree;
2256 *   bool use_locally_conservative_discretization;
2257 *  
2258 *   unsigned int temperature_degree;
2259 *   };
2260 *  
2261 *   private:
2262 *   Parameters &parameters;
2263 *  
2264 * @endcode
2265 *
2266 * The <code>pcout</code> (for <i>%parallel <code>std::cout</code></i>)
2267 * object is used to simplify writing output: each MPI process can use
2268 * this to generate output as usual, but since each of these processes
2269 * will (hopefully) produce the same output it will just be replicated
2270 * many times over; with the ConditionalOStream class, only the output
2271 * generated by one MPI process will actually be printed to screen,
2272 * whereas the output by all the other threads will simply be forgotten.
2273 *
2274 * @code
2275 *   ConditionalOStream pcout;
2276 *  
2277 * @endcode
2278 *
2279 * The following member variables will then again be similar to those in
2280 * @ref step_31 "step-31" (and to other tutorial programs). As mentioned in the
2281 * introduction, we fully distribute computations, so we will have to use
2282 * the parallel::distributed::Triangulation class (see @ref step_40 "step-40") but the
2283 * remainder of these variables is rather standard with two exceptions:
2284 *
2285
2286 *
2287 * - The <code>mapping</code> variable is used to denote a higher-order
2288 * polynomial mapping. As mentioned in the introduction, we use this
2289 * mapping when forming integrals through quadrature for all cells.
2290 *
2291
2292 *
2293 * - In a bit of naming confusion, you will notice below that some of the
2294 * variables from namespace TrilinosWrappers are taken from namespace
2295 * TrilinosWrappers::MPI (such as the right hand side vectors) whereas
2296 * others are not (such as the various matrices). This is due to legacy
2297 * reasons. We will frequently have to query velocities
2298 * and temperatures at arbitrary quadrature points; consequently, rather
2299 * than importing ghost information of a vector whenever we need access
2300 * to degrees of freedom that are relevant locally but owned by another
2301 * processor, we solve linear systems in %parallel but then immediately
2302 * initialize a vector including ghost entries of the solution for further
2303 * processing. The various <code>*_solution</code> vectors are therefore
2304 * filled immediately after solving their respective linear system in
2305 * %parallel and will always contain values for all
2306 * @ref GlossLocallyRelevantDof "locally relevant degrees of freedom";
2307 * the fully distributed vectors that we obtain from the solution process
2308 * and that only ever contain the
2309 * @ref GlossLocallyOwnedDof "locally owned degrees of freedom" are
2310 * destroyed immediately after the solution process and after we have
2311 * copied the relevant values into the member variable vectors.
2312 *
2313 * @code
2314 *   parallel::distributed::Triangulation<dim> triangulation;
2315 *   double global_Omega_diameter;
2316 *  
2317 *   const MappingQ<dim> mapping;
2318 *  
2319 *   const FESystem<dim> stokes_fe;
2320 *   DoFHandler<dim> stokes_dof_handler;
2321 *   AffineConstraints<double> stokes_constraints;
2322 *  
2323 *   TrilinosWrappers::BlockSparseMatrix stokes_matrix;
2324 *   TrilinosWrappers::BlockSparseMatrix stokes_preconditioner_matrix;
2325 *  
2326 *   TrilinosWrappers::MPI::BlockVector stokes_solution;
2327 *   TrilinosWrappers::MPI::BlockVector old_stokes_solution;
2328 *   TrilinosWrappers::MPI::BlockVector stokes_rhs;
2329 *  
2330 *  
2331 *   const FE_Q<dim> temperature_fe;
2332 *   DoFHandler<dim> temperature_dof_handler;
2333 *   AffineConstraints<double> temperature_constraints;
2334 *  
2335 *   TrilinosWrappers::SparseMatrix temperature_mass_matrix;
2336 *   TrilinosWrappers::SparseMatrix temperature_stiffness_matrix;
2337 *   TrilinosWrappers::SparseMatrix temperature_matrix;
2338 *  
2339 *   TrilinosWrappers::MPI::Vector temperature_solution;
2340 *   TrilinosWrappers::MPI::Vector old_temperature_solution;
2341 *   TrilinosWrappers::MPI::Vector old_old_temperature_solution;
2342 *   TrilinosWrappers::MPI::Vector temperature_rhs;
2343 *  
2344 *  
2345 *   double time_step;
2346 *   double old_time_step;
2347 *   unsigned int timestep_number;
2348 *  
2349 *   std::shared_ptr<TrilinosWrappers::PreconditionAMG> Amg_preconditioner;
2350 *   std::shared_ptr<TrilinosWrappers::PreconditionJacobi> Mp_preconditioner;
2351 *   std::shared_ptr<TrilinosWrappers::PreconditionJacobi> T_preconditioner;
2352 *  
2353 *   bool rebuild_stokes_matrix;
2354 *   bool rebuild_stokes_preconditioner;
2355 *   bool rebuild_temperature_matrices;
2356 *   bool rebuild_temperature_preconditioner;
2357 *  
2358 * @endcode
2359 *
2360 * The next member variable, <code>computing_timer</code> is used to
2361 * conveniently account for compute time spent in certain "sections" of
2362 * the code that are repeatedly entered. For example, we will enter (and
2363 * leave) sections for Stokes matrix assembly and would like to accumulate
2364 * the run time spent in this section over all time steps. Every so many
2365 * time steps as well as at the end of the program (through the destructor
2366 * of the TimerOutput class) we will then produce a nice summary of the
2367 * times spent in the different sections into which we categorize the
2368 * run-time of this program.
2369 *
2370 * @code
2371 *   TimerOutput computing_timer;
2372 *  
2373 * @endcode
2374 *
2375 * After these member variables we have a number of auxiliary functions
2376 * that have been broken out of the ones listed above. Specifically, there
2377 * are first three functions that we call from <code>setup_dofs</code> and
2378 * then the ones that do the assembling of linear systems:
2379 *
2380 * @code
2381 *   void setup_stokes_matrix(
2382 *   const std::vector<IndexSet> &stokes_partitioning,
2383 *   const std::vector<IndexSet> &stokes_relevant_partitioning);
2384 *   void setup_stokes_preconditioner(
2385 *   const std::vector<IndexSet> &stokes_partitioning,
2386 *   const std::vector<IndexSet> &stokes_relevant_partitioning);
2387 *   void setup_temperature_matrices(
2388 *   const IndexSet &temperature_partitioning,
2389 *   const IndexSet &temperature_relevant_partitioning);
2390 *  
2391 *  
2392 * @endcode
2393 *
2394 * Following the @ref MTWorkStream "task-based parallelization" paradigm,
2395 * we split all the assembly routines into two parts: a first part that
2396 * can do all the calculations on a certain cell without taking care of
2397 * other threads, and a second part (which is writing the local data into
2398 * the global matrices and vectors) which can be entered by only one
2399 * thread at a time. In order to implement that, we provide functions for
2400 * each of those two steps for all the four assembly routines that we use
2401 * in this program. The following eight functions do exactly this:
2402 *
2403 * @code
2404 *   void local_assemble_stokes_preconditioner(
2405 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
2406 *   Assembly::Scratch::StokesPreconditioner<dim> &scratch,
2407 *   Assembly::CopyData::StokesPreconditioner<dim> &data);
2408 *  
2409 *   void copy_local_to_global_stokes_preconditioner(
2410 *   const Assembly::CopyData::StokesPreconditioner<dim> &data);
2411 *  
2412 *  
2413 *   void local_assemble_stokes_system(
2414 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
2415 *   Assembly::Scratch::StokesSystem<dim> &scratch,
2416 *   Assembly::CopyData::StokesSystem<dim> &data);
2417 *  
2418 *   void copy_local_to_global_stokes_system(
2419 *   const Assembly::CopyData::StokesSystem<dim> &data);
2420 *  
2421 *  
2422 *   void local_assemble_temperature_matrix(
2423 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
2424 *   Assembly::Scratch::TemperatureMatrix<dim> &scratch,
2425 *   Assembly::CopyData::TemperatureMatrix<dim> &data);
2426 *  
2427 *   void copy_local_to_global_temperature_matrix(
2428 *   const Assembly::CopyData::TemperatureMatrix<dim> &data);
2429 *  
2430 *  
2431 *  
2432 *   void local_assemble_temperature_rhs(
2433 *   const std::pair<double, double> global_T_range,
2434 *   const double global_max_velocity,
2435 *   const double global_entropy_variation,
2436 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
2437 *   Assembly::Scratch::TemperatureRHS<dim> &scratch,
2438 *   Assembly::CopyData::TemperatureRHS<dim> &data);
2439 *  
2440 *   void copy_local_to_global_temperature_rhs(
2441 *   const Assembly::CopyData::TemperatureRHS<dim> &data);
2442 *  
2443 * @endcode
2444 *
2445 * Finally, we forward declare a member class that we will define later on
2446 * and that will be used to compute a number of quantities from our
2447 * solution vectors that we'd like to put into the output files for
2448 * visualization.
2449 *
2450 * @code
2451 *   class Postprocessor;
2452 *   };
2453 *  
2454 *  
2455 * @endcode
2456 *
2457 *
2458 * <a name="step_32-BoussinesqFlowProblemclassimplementation"></a>
2459 * <h3>BoussinesqFlowProblem class implementation</h3>
2460 *
2461
2462 *
2463 *
2464 * <a name="step_32-BoussinesqFlowProblemParameters"></a>
2465 * <h4>BoussinesqFlowProblem::Parameters</h4>
2466 *
2467
2468 *
2469 * Here comes the definition of the parameters for the Stokes problem. We
2470 * allow to set the end time for the simulation, the level of refinements
2471 * (both global and adaptive, which in the sum specify what maximum level
2472 * the cells are allowed to have), and the interval between refinements in
2473 * the time stepping.
2474 *
2475
2476 *
2477 * Then, we let the user specify constants for the stabilization parameters
2478 * (as discussed in the introduction), the polynomial degree for the Stokes
2479 * velocity space, whether to use the locally conservative discretization
2480 * based on FE_DGP elements for the pressure or not (FE_Q elements for
2481 * pressure), and the polynomial degree for the temperature interpolation.
2482 *
2483
2484 *
2485 * The constructor checks for a valid input file (if not, a file with
2486 * default parameters for the quantities is written), and eventually parses
2487 * the parameters.
2488 *
2489 * @code
2490 *   template <int dim>
2491 *   BoussinesqFlowProblem<dim>::Parameters::Parameters(
2492 *   const std::string &parameter_filename)
2493 *   : end_time(1e8)
2494 *   , initial_global_refinement(2)
2495 *   , initial_adaptive_refinement(2)
2496 *   , adaptive_refinement_interval(10)
2497 *   , stabilization_alpha(2)
2498 *   , stabilization_c_R(0.11)
2499 *   , stabilization_beta(0.078)
2500 *   , stokes_velocity_degree(2)
2501 *   , use_locally_conservative_discretization(true)
2502 *   , temperature_degree(2)
2503 *   {
2504 *   ParameterHandler prm;
2505 *   BoussinesqFlowProblem<dim>::Parameters::declare_parameters(prm);
2506 *  
2507 *   std::ifstream parameter_file(parameter_filename);
2508 *  
2509 *   if (!parameter_file)
2510 *   {
2511 *   parameter_file.close();
2512 *  
2513 *   std::ofstream parameter_out(parameter_filename);
2514 *   prm.print_parameters(parameter_out, ParameterHandler::Text);
2515 *  
2516 *   AssertThrow(
2517 *   false,
2518 *   ExcMessage(
2519 *   "Input parameter file <" + parameter_filename +
2520 *   "> not found. Creating a template file of the same name."));
2521 *   }
2522 *  
2523 *   prm.parse_input(parameter_file);
2524 *   parse_parameters(prm);
2525 *   }
2526 *  
2527 *  
2528 *  
2529 * @endcode
2530 *
2531 * Next we have a function that declares the parameters that we expect in
2532 * the input file, together with their data types, default values and a
2533 * description:
2534 *
2535 * @code
2536 *   template <int dim>
2537 *   void BoussinesqFlowProblem<dim>::Parameters::declare_parameters(
2538 *   ParameterHandler &prm)
2539 *   {
2540 *   prm.declare_entry("End time",
2541 *   "1e8",
2542 *   Patterns::Double(0),
2543 *   "The end time of the simulation in years.");
2544 *   prm.declare_entry("Initial global refinement",
2545 *   "2",
2546 *   Patterns::Integer(0),
2547 *   "The number of global refinement steps performed on "
2548 *   "the initial coarse mesh, before the problem is first "
2549 *   "solved there.");
2550 *   prm.declare_entry("Initial adaptive refinement",
2551 *   "2",
2552 *   Patterns::Integer(0),
2553 *   "The number of adaptive refinement steps performed after "
2554 *   "initial global refinement.");
2555 *   prm.declare_entry("Time steps between mesh refinement",
2556 *   "10",
2557 *   Patterns::Integer(1),
2558 *   "The number of time steps after which the mesh is to be "
2559 *   "adapted based on computed error indicators.");
2560 *   prm.declare_entry("Generate graphical output",
2561 *   "false",
2562 *   Patterns::Bool(),
2563 *   "Whether graphical output is to be generated or not. "
2564 *   "You may not want to get graphical output if the number "
2565 *   "of processors is large.");
2566 *   prm.declare_entry("Time steps between graphical output",
2567 *   "50",
2568 *   Patterns::Integer(1),
2569 *   "The number of time steps between each generation of "
2570 *   "graphical output files.");
2571 *  
2572 *   prm.enter_subsection("Stabilization parameters");
2573 *   {
2574 *   prm.declare_entry("alpha",
2575 *   "2",
2576 *   Patterns::Double(1, 2),
2577 *   "The exponent in the entropy viscosity stabilization.");
2578 *   prm.declare_entry("c_R",
2579 *   "0.11",
2580 *   Patterns::Double(0),
2581 *   "The c_R factor in the entropy viscosity "
2582 *   "stabilization.");
2583 *   prm.declare_entry("beta",
2584 *   "0.078",
2585 *   Patterns::Double(0),
2586 *   "The beta factor in the artificial viscosity "
2587 *   "stabilization. An appropriate value for 2d is 0.052 "
2588 *   "and 0.078 for 3d.");
2589 *   }
2590 *   prm.leave_subsection();
2591 *  
2592 *   prm.enter_subsection("Discretization");
2593 *   {
2594 *   prm.declare_entry(
2595 *   "Stokes velocity polynomial degree",
2596 *   "2",
2597 *   Patterns::Integer(1),
2598 *   "The polynomial degree to use for the velocity variables "
2599 *   "in the Stokes system.");
2600 *   prm.declare_entry(
2601 *   "Temperature polynomial degree",
2602 *   "2",
2603 *   Patterns::Integer(1),
2604 *   "The polynomial degree to use for the temperature variable.");
2605 *   prm.declare_entry(
2606 *   "Use locally conservative discretization",
2607 *   "true",
2608 *   Patterns::Bool(),
2609 *   "Whether to use a Stokes discretization that is locally "
2610 *   "conservative at the expense of a larger number of degrees "
2611 *   "of freedom, or to go with a cheaper discretization "
2612 *   "that does not locally conserve mass (although it is "
2613 *   "globally conservative.");
2614 *   }
2615 *   prm.leave_subsection();
2616 *   }
2617 *  
2618 *  
2619 *  
2620 * @endcode
2621 *
2622 * And then we need a function that reads the contents of the
2623 * ParameterHandler object we get by reading the input file and puts the
2624 * results into variables that store the values of the parameters we have
2625 * previously declared:
2626 *
2627 * @code
2628 *   template <int dim>
2629 *   void BoussinesqFlowProblem<dim>::Parameters::parse_parameters(
2630 *   ParameterHandler &prm)
2631 *   {
2632 *   end_time = prm.get_double("End time");
2633 *   initial_global_refinement = prm.get_integer("Initial global refinement");
2634 *   initial_adaptive_refinement =
2635 *   prm.get_integer("Initial adaptive refinement");
2636 *  
2637 *   adaptive_refinement_interval =
2638 *   prm.get_integer("Time steps between mesh refinement");
2639 *  
2640 *   generate_graphical_output = prm.get_bool("Generate graphical output");
2641 *   graphical_output_interval =
2642 *   prm.get_integer("Time steps between graphical output");
2643 *  
2644 *   prm.enter_subsection("Stabilization parameters");
2645 *   {
2646 *   stabilization_alpha = prm.get_double("alpha");
2647 *   stabilization_c_R = prm.get_double("c_R");
2648 *   stabilization_beta = prm.get_double("beta");
2649 *   }
2650 *   prm.leave_subsection();
2651 *  
2652 *   prm.enter_subsection("Discretization");
2653 *   {
2654 *   stokes_velocity_degree =
2655 *   prm.get_integer("Stokes velocity polynomial degree");
2656 *   temperature_degree = prm.get_integer("Temperature polynomial degree");
2657 *   use_locally_conservative_discretization =
2658 *   prm.get_bool("Use locally conservative discretization");
2659 *   }
2660 *   prm.leave_subsection();
2661 *   }
2662 *  
2663 *  
2664 *  
2665 * @endcode
2666 *
2667 *
2668 * <a name="step_32-BoussinesqFlowProblemBoussinesqFlowProblem"></a>
2669 * <h4>BoussinesqFlowProblem::BoussinesqFlowProblem</h4>
2670 *
2671
2672 *
2673 * The constructor of the problem is very similar to the constructor in
2674 * @ref step_31 "step-31". What is different is the %parallel communication: Trilinos uses
2675 * a message passing interface (MPI) for data distribution. When entering
2676 * the BoussinesqFlowProblem class, we have to decide how the parallelization
2677 * is to be done. We choose a rather simple strategy and let all processors
2678 * that are running the program work together, specified by the communicator
2679 * <code>MPI_COMM_WORLD</code>. Next, we create the output stream (as we
2680 * already did in @ref step_18 "step-18") that only generates output on the first MPI
2681 * process and is completely forgetful on all others. The implementation of
2682 * this idea is to check the process number when <code>pcout</code> gets a
2683 * true argument, and it uses the <code>std::cout</code> stream for
2684 * output. If we are one processor five, for instance, then we will give a
2685 * <code>false</code> argument to <code>pcout</code>, which means that the
2686 * output of that processor will not be printed. With the exception of the
2687 * mapping object (for which we use polynomials of degree 4) all but the
2688 * final member variable are exactly the same as in @ref step_31 "step-31".
2689 *
2690
2691 *
2692 * This final object, the TimerOutput object, is then told to restrict
2693 * output to the <code>pcout</code> stream (processor 0), and then we
2694 * specify that we want to get a summary table at the end of the program
2695 * which shows us wallclock times (as opposed to CPU times). We will
2696 * manually also request intermediate summaries every so many time steps in
2697 * the <code>run()</code> function below.
2698 *
2699 * @code
2700 *   template <int dim>
2701 *   BoussinesqFlowProblem<dim>::BoussinesqFlowProblem(Parameters &parameters_)
2702 *   : parameters(parameters_)
2703 *   , pcout(std::cout, (Utilities::MPI::this_mpi_process(MPI_COMM_WORLD) == 0))
2704 *   ,
2705 *  
2706 *   triangulation(MPI_COMM_WORLD,
2710 *   ,
2711 *  
2712 *   global_Omega_diameter(0.)
2713 *   ,
2714 *  
2715 *   mapping(4)
2716 *   ,
2717 *  
2718 *   stokes_fe(FE_Q<dim>(parameters.stokes_velocity_degree) ^ dim,
2719 *   (parameters.use_locally_conservative_discretization ?
2720 *   static_cast<const FiniteElement<dim> &>(
2721 *   FE_DGP<dim>(parameters.stokes_velocity_degree - 1)) :
2722 *   static_cast<const FiniteElement<dim> &>(
2723 *   FE_Q<dim>(parameters.stokes_velocity_degree - 1))))
2724 *   ,
2725 *  
2726 *   stokes_dof_handler(triangulation)
2727 *   ,
2728 *  
2729 *   temperature_fe(parameters.temperature_degree)
2730 *   , temperature_dof_handler(triangulation)
2731 *   ,
2732 *  
2733 *   time_step(0)
2734 *   , old_time_step(0)
2735 *   , timestep_number(0)
2736 *   , rebuild_stokes_matrix(true)
2737 *   , rebuild_stokes_preconditioner(true)
2738 *   , rebuild_temperature_matrices(true)
2739 *   , rebuild_temperature_preconditioner(true)
2740 *   ,
2741 *  
2742 *   computing_timer(MPI_COMM_WORLD,
2743 *   pcout,
2744 *   TimerOutput::summary,
2745 *   TimerOutput::wall_times)
2746 *   {}
2747 *  
2748 *  
2749 *  
2750 * @endcode
2751 *
2752 *
2753 * <a name="step_32-TheBoussinesqFlowProblemhelperfunctions"></a>
2754 * <h4>The BoussinesqFlowProblem helper functions</h4>
2755 *
2756 * <a name="step_32-BoussinesqFlowProblemget_maximal_velocity"></a>
2757 * <h5>BoussinesqFlowProblem::get_maximal_velocity</h5>
2758 *
2759
2760 *
2761 * Except for two small details, the function to compute the global maximum
2762 * of the velocity is the same as in @ref step_31 "step-31". The first detail is actually
2763 * common to all functions that implement loops over all cells in the
2764 * triangulation: When operating in %parallel, each processor can only work
2765 * on a chunk of cells since each processor only has a certain part of the
2766 * entire triangulation. This chunk of cells that we want to work on is
2767 * identified via a so-called <code>subdomain_id</code>, as we also did in
2768 * @ref step_18 "step-18". All we need to change is hence to perform the cell-related
2769 * operations only on cells that are owned by the current process (as
2770 * opposed to ghost or artificial cells), i.e. for which the subdomain id
2771 * equals the number of the process ID. Since this is a commonly used
2772 * operation, there is a shortcut for this operation: we can ask whether the
2773 * cell is owned by the current processor using
2774 * <code>cell-@>is_locally_owned()</code>.
2775 *
2776
2777 *
2778 * The second difference is the way we calculate the maximum value. Before,
2779 * we could simply have a <code>double</code> variable that we checked
2780 * against on each quadrature point for each cell. Now, we have to be a bit
2781 * more careful since each processor only operates on a subset of
2782 * cells. What we do is to first let each processor calculate the maximum
2783 * among its cells, and then do a global communication operation
2784 * <code>Utilities::MPI::max</code> that computes the maximum value among
2785 * all the maximum values of the individual processors. MPI provides such a
2786 * call, but it's even simpler to use the respective function in namespace
2787 * Utilities::MPI using the MPI communicator object since that will do the
2788 * right thing even if we work without MPI and on a single machine only. The
2789 * call to <code>Utilities::MPI::max</code> needs two arguments, namely the
2790 * local maximum (input) and the MPI communicator, which is MPI_COMM_WORLD
2791 * in this example.
2792 *
2793 * @code
2794 *   template <int dim>
2795 *   double BoussinesqFlowProblem<dim>::get_maximal_velocity() const
2796 *   {
2797 *   const QIterated<dim> quadrature_formula(QTrapezoid<1>(),
2798 *   parameters.stokes_velocity_degree);
2799 *   const unsigned int n_q_points = quadrature_formula.size();
2800 *  
2801 *   FEValues<dim> fe_values(mapping,
2802 *   stokes_fe,
2803 *   quadrature_formula,
2804 *   update_values);
2805 *   std::vector<Tensor<1, dim>> velocity_values(n_q_points);
2806 *  
2807 *   const FEValuesExtractors::Vector velocities(0);
2808 *  
2809 *   double max_local_velocity = 0;
2810 *  
2811 *   for (const auto &cell : stokes_dof_handler.active_cell_iterators())
2812 *   if (cell->is_locally_owned())
2813 *   {
2814 *   fe_values.reinit(cell);
2815 *   fe_values[velocities].get_function_values(stokes_solution,
2816 *   velocity_values);
2817 *  
2818 *   for (unsigned int q = 0; q < n_q_points; ++q)
2819 *   max_local_velocity =
2820 *   std::max(max_local_velocity, velocity_values[q].norm());
2821 *   }
2822 *  
2823 *   return Utilities::MPI::max(max_local_velocity, MPI_COMM_WORLD);
2824 *   }
2825 *  
2826 *  
2827 * @endcode
2828 *
2829 *
2830 * <a name="step_32-BoussinesqFlowProblemget_cfl_number"></a>
2831 * <h5>BoussinesqFlowProblem::get_cfl_number</h5>
2832 *
2833
2834 *
2835 * The next function does something similar, but we now compute the CFL
2836 * number, i.e., maximal velocity on a cell divided by the cell
2837 * diameter. This number is necessary to determine the time step size, as we
2838 * use a semi-explicit time stepping scheme for the temperature equation
2839 * (see @ref step_31 "step-31" for a discussion). We compute it in the same way as above:
2840 * Compute the local maximum over all locally owned cells, then exchange it
2841 * via MPI to find the global maximum.
2842 *
2843 * @code
2844 *   template <int dim>
2845 *   double BoussinesqFlowProblem<dim>::get_cfl_number() const
2846 *   {
2847 *   const QIterated<dim> quadrature_formula(QTrapezoid<1>(),
2848 *   parameters.stokes_velocity_degree);
2849 *   const unsigned int n_q_points = quadrature_formula.size();
2850 *  
2851 *   FEValues<dim> fe_values(mapping,
2852 *   stokes_fe,
2853 *   quadrature_formula,
2854 *   update_values);
2855 *   std::vector<Tensor<1, dim>> velocity_values(n_q_points);
2856 *  
2857 *   const FEValuesExtractors::Vector velocities(0);
2858 *  
2859 *   double max_local_cfl = 0;
2860 *  
2861 *   for (const auto &cell : stokes_dof_handler.active_cell_iterators())
2862 *   if (cell->is_locally_owned())
2863 *   {
2864 *   fe_values.reinit(cell);
2865 *   fe_values[velocities].get_function_values(stokes_solution,
2866 *   velocity_values);
2867 *  
2868 *   double max_local_velocity = 1e-10;
2869 *   for (unsigned int q = 0; q < n_q_points; ++q)
2870 *   max_local_velocity =
2871 *   std::max(max_local_velocity, velocity_values[q].norm());
2872 *   max_local_cfl =
2873 *   std::max(max_local_cfl, max_local_velocity / cell->diameter());
2874 *   }
2875 *  
2876 *   return Utilities::MPI::max(max_local_cfl, MPI_COMM_WORLD);
2877 *   }
2878 *  
2879 *  
2880 * @endcode
2881 *
2882 *
2883 * <a name="step_32-BoussinesqFlowProblemget_entropy_variation"></a>
2884 * <h5>BoussinesqFlowProblem::get_entropy_variation</h5>
2885 *
2886
2887 *
2888 * Next comes the computation of the global entropy variation
2889 * @f$\|E(T)-\bar{E}(T)\|_\infty@f$ where the entropy @f$E@f$ is defined as
2890 * discussed in the introduction. This is needed for the evaluation of the
2891 * stabilization in the temperature equation as explained in the
2892 * introduction. The entropy variation is actually only needed if we use
2893 * @f$\alpha=2@f$ as a power in the residual computation. The infinity norm is
2894 * computed by the maxima over quadrature points, as usual in discrete
2895 * computations.
2896 *
2897
2898 *
2899 * In order to compute this quantity, we first have to find the
2900 * space-average @f$\bar{E}(T)@f$ and then evaluate the maximum. However, that
2901 * means that we would need to perform two loops. We can avoid the overhead
2902 * by noting that @f$\|E(T)-\bar{E}(T)\|_\infty =
2903 * \max\big(E_{\textrm{max}}(T)-\bar{E}(T),
2904 * \bar{E}(T)-E_{\textrm{min}}(T)\big)@f$, i.e., the maximum out of the
2905 * deviation from the average entropy in positive and negative
2906 * directions. The four quantities we need for the latter formula (maximum
2907 * entropy, minimum entropy, average entropy, area) can all be evaluated in
2908 * the same loop over all cells, so we choose this simpler variant.
2909 *
2910 * @code
2911 *   template <int dim>
2912 *   double BoussinesqFlowProblem<dim>::get_entropy_variation(
2913 *   const double average_temperature) const
2914 *   {
2915 *   if (parameters.stabilization_alpha != 2)
2916 *   return 1.;
2917 *  
2918 *   const QGauss<dim> quadrature_formula(parameters.temperature_degree + 1);
2919 *   const unsigned int n_q_points = quadrature_formula.size();
2920 *  
2921 *   FEValues<dim> fe_values(temperature_fe,
2922 *   quadrature_formula,
2923 *   update_values | update_JxW_values);
2924 *   std::vector<double> old_temperature_values(n_q_points);
2925 *   std::vector<double> old_old_temperature_values(n_q_points);
2926 *  
2927 * @endcode
2928 *
2929 * In the two functions above we computed the maximum of numbers that were
2930 * all non-negative, so we knew that zero was certainly a lower bound. On
2931 * the other hand, here we need to find the maximum deviation from the
2932 * average value, i.e., we will need to know the maximal and minimal
2933 * values of the entropy for which we don't a priori know the sign.
2934 *
2935
2936 *
2937 * To compute it, we can therefore start with the largest and smallest
2938 * possible values we can store in a double precision number: The minimum
2939 * is initialized with a bigger and the maximum with a smaller number than
2940 * any one that is going to appear. We are then guaranteed that these
2941 * numbers will be overwritten in the loop on the first cell or, if this
2942 * processor does not own any cells, in the communication step at the
2943 * latest. The following loop then computes the minimum and maximum local
2944 * entropy as well as keeps track of the area/volume of the part of the
2945 * domain we locally own and the integral over the entropy on it:
2946 *
2947 * @code
2948 *   double min_entropy = std::numeric_limits<double>::max(),
2949 *   max_entropy = -std::numeric_limits<double>::max(), area = 0,
2950 *   entropy_integrated = 0;
2951 *  
2952 *   for (const auto &cell : temperature_dof_handler.active_cell_iterators())
2953 *   if (cell->is_locally_owned())
2954 *   {
2955 *   fe_values.reinit(cell);
2956 *   fe_values.get_function_values(old_temperature_solution,
2957 *   old_temperature_values);
2958 *   fe_values.get_function_values(old_old_temperature_solution,
2959 *   old_old_temperature_values);
2960 *   for (unsigned int q = 0; q < n_q_points; ++q)
2961 *   {
2962 *   const double T =
2963 *   (old_temperature_values[q] + old_old_temperature_values[q]) / 2;
2964 *   const double entropy =
2965 *   ((T - average_temperature) * (T - average_temperature));
2966 *  
2967 *   min_entropy = std::min(min_entropy, entropy);
2968 *   max_entropy = std::max(max_entropy, entropy);
2969 *   area += fe_values.JxW(q);
2970 *   entropy_integrated += fe_values.JxW(q) * entropy;
2971 *   }
2972 *   }
2973 *  
2974 * @endcode
2975 *
2976 * Now we only need to exchange data between processors: we need to sum
2977 * the two integrals (<code>area</code>, <code>entropy_integrated</code>),
2978 * and get the extrema for maximum and minimum. We could do this through
2979 * four different data exchanges, but we can it with two:
2980 * Utilities::MPI::sum also exists in a variant that takes an array of
2981 * values that are all to be summed up. And we can also utilize the
2982 * Utilities::MPI::max function by realizing that forming the minimum over
2983 * the minimal entropies equals forming the negative of the maximum over
2984 * the negative of the minimal entropies; this maximum can then be
2985 * combined with forming the maximum over the maximal entropies.
2986 *
2987 * @code
2988 *   const double local_sums[2] = {entropy_integrated, area},
2989 *   local_maxima[2] = {-min_entropy, max_entropy};
2990 *   double global_sums[2], global_maxima[2];
2991 *  
2992 *   Utilities::MPI::sum(local_sums, MPI_COMM_WORLD, global_sums);
2993 *   Utilities::MPI::max(local_maxima, MPI_COMM_WORLD, global_maxima);
2994 *  
2995 * @endcode
2996 *
2997 * Having computed everything this way, we can then compute the average
2998 * entropy and find the @f$L^\infty@f$ norm by taking the larger of the
2999 * deviation of the maximum or minimum from the average:
3000 *
3001 * @code
3002 *   const double average_entropy = global_sums[0] / global_sums[1];
3003 *   const double entropy_diff = std::max(global_maxima[1] - average_entropy,
3004 *   average_entropy - (-global_maxima[0]));
3005 *   return entropy_diff;
3006 *   }
3007 *  
3008 *  
3009 *  
3010 * @endcode
3011 *
3012 *
3013 * <a name="step_32-BoussinesqFlowProblemget_extrapolated_temperature_range"></a>
3014 * <h5>BoussinesqFlowProblem::get_extrapolated_temperature_range</h5>
3015 *
3016
3017 *
3018 * The next function computes the minimal and maximal value of the
3019 * extrapolated temperature over the entire domain. Again, this is only a
3020 * slightly modified version of the respective function in @ref step_31 "step-31". As in
3021 * the function above, we collect local minima and maxima and then compute
3022 * the global extrema using the same trick as above.
3023 *
3024
3025 *
3026 * As already discussed in @ref step_31 "step-31", the function needs to distinguish
3027 * between the first and all following time steps because it uses a higher
3028 * order temperature extrapolation scheme when at least two previous time
3029 * steps are available.
3030 *
3031 * @code
3032 *   template <int dim>
3033 *   std::pair<double, double>
3034 *   BoussinesqFlowProblem<dim>::get_extrapolated_temperature_range() const
3035 *   {
3036 *   const QIterated<dim> quadrature_formula(QTrapezoid<1>(),
3037 *   parameters.temperature_degree);
3038 *   const unsigned int n_q_points = quadrature_formula.size();
3039 *  
3040 *   FEValues<dim> fe_values(mapping,
3041 *   temperature_fe,
3042 *   quadrature_formula,
3043 *   update_values);
3044 *   std::vector<double> old_temperature_values(n_q_points);
3045 *   std::vector<double> old_old_temperature_values(n_q_points);
3046 *  
3047 *   double min_local_temperature = std::numeric_limits<double>::max(),
3048 *   max_local_temperature = -std::numeric_limits<double>::max();
3049 *  
3050 *   if (timestep_number != 0)
3051 *   {
3052 *   for (const auto &cell : temperature_dof_handler.active_cell_iterators())
3053 *   if (cell->is_locally_owned())
3054 *   {
3055 *   fe_values.reinit(cell);
3056 *   fe_values.get_function_values(old_temperature_solution,
3057 *   old_temperature_values);
3058 *   fe_values.get_function_values(old_old_temperature_solution,
3059 *   old_old_temperature_values);
3060 *  
3061 *   for (unsigned int q = 0; q < n_q_points; ++q)
3062 *   {
3063 *   const double temperature =
3064 *   (1. + time_step / old_time_step) *
3065 *   old_temperature_values[q] -
3066 *   time_step / old_time_step * old_old_temperature_values[q];
3067 *  
3068 *   min_local_temperature =
3069 *   std::min(min_local_temperature, temperature);
3070 *   max_local_temperature =
3071 *   std::max(max_local_temperature, temperature);
3072 *   }
3073 *   }
3074 *   }
3075 *   else
3076 *   {
3077 *   for (const auto &cell : temperature_dof_handler.active_cell_iterators())
3078 *   if (cell->is_locally_owned())
3079 *   {
3080 *   fe_values.reinit(cell);
3081 *   fe_values.get_function_values(old_temperature_solution,
3082 *   old_temperature_values);
3083 *  
3084 *   for (unsigned int q = 0; q < n_q_points; ++q)
3085 *   {
3086 *   const double temperature = old_temperature_values[q];
3087 *  
3088 *   min_local_temperature =
3089 *   std::min(min_local_temperature, temperature);
3090 *   max_local_temperature =
3091 *   std::max(max_local_temperature, temperature);
3092 *   }
3093 *   }
3094 *   }
3095 *  
3096 *   double local_extrema[2] = {-min_local_temperature, max_local_temperature};
3097 *   double global_extrema[2];
3098 *   Utilities::MPI::max(local_extrema, MPI_COMM_WORLD, global_extrema);
3099 *  
3100 *   return std::make_pair(-global_extrema[0], global_extrema[1]);
3101 *   }
3102 *  
3103 *  
3104 * @endcode
3105 *
3106 *
3107 * <a name="step_32-BoussinesqFlowProblemcompute_viscosity"></a>
3108 * <h5>BoussinesqFlowProblem::compute_viscosity</h5>
3109 *
3110
3111 *
3112 * The function that calculates the viscosity is purely local and so needs
3113 * no communication at all. It is mostly the same as in @ref step_31 "step-31" but with an
3114 * updated formulation of the viscosity if @f$\alpha=2@f$ is chosen:
3115 *
3116 * @code
3117 *   template <int dim>
3118 *   double BoussinesqFlowProblem<dim>::compute_viscosity(
3119 *   const std::vector<double> &old_temperature,
3120 *   const std::vector<double> &old_old_temperature,
3121 *   const std::vector<Tensor<1, dim>> &old_temperature_grads,
3122 *   const std::vector<Tensor<1, dim>> &old_old_temperature_grads,
3123 *   const std::vector<double> &old_temperature_laplacians,
3124 *   const std::vector<double> &old_old_temperature_laplacians,
3125 *   const std::vector<Tensor<1, dim>> &old_velocity_values,
3126 *   const std::vector<Tensor<1, dim>> &old_old_velocity_values,
3127 *   const std::vector<SymmetricTensor<2, dim>> &old_strain_rates,
3128 *   const std::vector<SymmetricTensor<2, dim>> &old_old_strain_rates,
3129 *   const double global_u_infty,
3130 *   const double global_T_variation,
3131 *   const double average_temperature,
3132 *   const double global_entropy_variation,
3133 *   const double cell_diameter) const
3134 *   {
3135 *   if (global_u_infty == 0)
3136 *   return 5e-3 * cell_diameter;
3137 *  
3138 *   const unsigned int n_q_points = old_temperature.size();
3139 *  
3140 *   double max_residual = 0;
3141 *   double max_velocity = 0;
3142 *  
3143 *   for (unsigned int q = 0; q < n_q_points; ++q)
3144 *   {
3145 *   const Tensor<1, dim> u =
3146 *   (old_velocity_values[q] + old_old_velocity_values[q]) / 2;
3147 *  
3148 *   const SymmetricTensor<2, dim> strain_rate =
3149 *   (old_strain_rates[q] + old_old_strain_rates[q]) / 2;
3150 *  
3151 *   const double T = (old_temperature[q] + old_old_temperature[q]) / 2;
3152 *   const double dT_dt =
3153 *   (old_temperature[q] - old_old_temperature[q]) / old_time_step;
3154 *   const double u_grad_T =
3155 *   u * (old_temperature_grads[q] + old_old_temperature_grads[q]) / 2;
3156 *  
3157 *   const double kappa_Delta_T =
3158 *   EquationData::kappa *
3159 *   (old_temperature_laplacians[q] + old_old_temperature_laplacians[q]) /
3160 *   2;
3161 *   const double gamma =
3162 *   ((EquationData::radiogenic_heating * EquationData::density(T) +
3163 *   2 * EquationData::eta * strain_rate * strain_rate) /
3164 *   (EquationData::density(T) * EquationData::specific_heat));
3165 *  
3166 *   double residual = std::abs(dT_dt + u_grad_T - kappa_Delta_T - gamma);
3167 *   if (parameters.stabilization_alpha == 2)
3168 *   residual *= std::abs(T - average_temperature);
3169 *  
3170 *   max_residual = std::max(residual, max_residual);
3171 *   max_velocity = std::max(std::sqrt(u * u), max_velocity);
3172 *   }
3173 *  
3174 *   const double max_viscosity =
3175 *   (parameters.stabilization_beta * max_velocity * cell_diameter);
3176 *   if (timestep_number == 0)
3177 *   return max_viscosity;
3178 *   else
3179 *   {
3180 *   Assert(old_time_step > 0, ExcInternalError());
3181 *  
3182 *   double entropy_viscosity;
3183 *   if (parameters.stabilization_alpha == 2)
3184 *   entropy_viscosity =
3185 *   (parameters.stabilization_c_R * cell_diameter * cell_diameter *
3186 *   max_residual / global_entropy_variation);
3187 *   else
3188 *   entropy_viscosity =
3189 *   (parameters.stabilization_c_R * cell_diameter *
3190 *   global_Omega_diameter * max_velocity * max_residual /
3191 *   (global_u_infty * global_T_variation));
3192 *  
3193 *   return std::min(max_viscosity, entropy_viscosity);
3194 *   }
3195 *   }
3196 *  
3197 *  
3198 *  
3199 * @endcode
3200 *
3201 *
3202 * <a name="step_32-TheBoussinesqFlowProblemsetupfunctions"></a>
3203 * <h4>The BoussinesqFlowProblem setup functions</h4>
3204 *
3205
3206 *
3207 * The following three functions set up the Stokes matrix, the matrix used
3208 * for the Stokes preconditioner, and the temperature matrix. The code is
3209 * mostly the same as in @ref step_31 "step-31", but it has been broken out into three
3210 * functions of their own for simplicity.
3211 *
3212
3213 *
3214 * The main functional difference between the code here and that in @ref step_31 "step-31"
3215 * is that the matrices we want to set up are distributed across multiple
3216 * processors. Since we still want to build up the sparsity pattern first
3217 * for efficiency reasons, we could continue to build the <i>entire</i>
3218 * sparsity pattern as a BlockDynamicSparsityPattern, as we did in
3219 * @ref step_31 "step-31". However, that would be inefficient: every processor would build
3220 * the same sparsity pattern, but only initialize a small part of the matrix
3221 * using it. It also violates the principle that every processor should only
3222 * work on those cells it owns (and, if necessary the layer of ghost cells
3223 * around it).
3224 *
3225
3226 *
3227 * Rather, we use an object of type TrilinosWrappers::BlockSparsityPattern,
3228 * which is (obviously) a wrapper around a sparsity pattern object provided
3229 * by Trilinos. The advantage is that the Trilinos sparsity pattern class
3230 * can communicate across multiple processors: if this processor fills in
3231 * all the nonzero entries that result from the cells it owns, and every
3232 * other processor does so as well, then at the end after some MPI
3233 * communication initiated by the <code>compress()</code> call, we will have
3234 * the globally assembled sparsity pattern available with which the global
3235 * matrix can be initialized.
3236 *
3237
3238 *
3239 * There is one important aspect when initializing Trilinos sparsity
3240 * patterns in parallel: In addition to specifying the locally owned rows
3241 * and columns of the matrices via the @p stokes_partitioning index set, we
3242 * also supply information about all the rows we are possibly going to write
3243 * into when assembling on a certain processor. The set of locally relevant
3244 * rows contains all such rows (possibly also a few unnecessary ones, but it
3245 * is difficult to find the exact row indices before actually getting
3246 * indices on all cells and resolving constraints). This additional
3247 * information allows to exactly determine the structure for the
3248 * off-processor data found during assembly. While Trilinos matrices are
3249 * able to collect this information on the fly as well (when initializing
3250 * them from some other reinit method), it is less efficient and leads to
3251 * problems when assembling matrices with multiple threads. In this program,
3252 * we pessimistically assume that only one processor at a time can write
3253 * into the matrix while assembly (whereas the computation is parallel),
3254 * which is fine for Trilinos matrices. In practice, one can do better by
3255 * hinting WorkStream at cells that do not share vertices, allowing for
3256 * parallelism among those cells (see the graph coloring algorithms and
3257 * WorkStream with colored iterators argument). However, that only works
3258 * when only one MPI processor is present because Trilinos' internal data
3259 * structures for accumulating off-processor data on the fly are not thread
3260 * safe. With the initialization presented here, there is no such problem
3261 * and one could safely introduce graph coloring for this algorithm.
3262 *
3263
3264 *
3265 * The only other change we need to make is to tell the
3266 * DoFTools::make_sparsity_pattern() function that it is only supposed to
3267 * work on a subset of cells, namely the ones whose
3268 * <code>subdomain_id</code> equals the number of the current processor, and
3269 * to ignore all other cells.
3270 *
3271
3272 *
3273 * This strategy is replicated across all three of the following functions.
3274 *
3275
3276 *
3277 * Note that Trilinos matrices store the information contained in the
3278 * sparsity patterns, so we can safely release the <code>sp</code> variable
3279 * once the matrix has been given the sparsity structure.
3280 *
3281 * @code
3282 *   template <int dim>
3283 *   void BoussinesqFlowProblem<dim>::setup_stokes_matrix(
3284 *   const std::vector<IndexSet> &stokes_partitioning,
3285 *   const std::vector<IndexSet> &stokes_relevant_partitioning)
3286 *   {
3287 *   stokes_matrix.clear();
3288 *  
3289 *   TrilinosWrappers::BlockSparsityPattern sp(stokes_partitioning,
3290 *   stokes_partitioning,
3291 *   stokes_relevant_partitioning,
3292 *   MPI_COMM_WORLD);
3293 *  
3294 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
3295 *   for (unsigned int c = 0; c < dim + 1; ++c)
3296 *   for (unsigned int d = 0; d < dim + 1; ++d)
3297 *   if (!((c == dim) && (d == dim)))
3298 *   coupling[c][d] = DoFTools::always;
3299 *   else
3300 *   coupling[c][d] = DoFTools::none;
3301 *  
3302 *   DoFTools::make_sparsity_pattern(stokes_dof_handler,
3303 *   coupling,
3304 *   sp,
3305 *   stokes_constraints,
3306 *   false,
3307 *   Utilities::MPI::this_mpi_process(
3308 *   MPI_COMM_WORLD));
3309 *   sp.compress();
3310 *  
3311 *   stokes_matrix.reinit(sp);
3312 *   }
3313 *  
3314 *  
3315 *  
3316 *   template <int dim>
3317 *   void BoussinesqFlowProblem<dim>::setup_stokes_preconditioner(
3318 *   const std::vector<IndexSet> &stokes_partitioning,
3319 *   const std::vector<IndexSet> &stokes_relevant_partitioning)
3320 *   {
3321 *   Amg_preconditioner.reset();
3322 *   Mp_preconditioner.reset();
3323 *  
3324 *   stokes_preconditioner_matrix.clear();
3325 *  
3326 *   TrilinosWrappers::BlockSparsityPattern sp(stokes_partitioning,
3327 *   stokes_partitioning,
3328 *   stokes_relevant_partitioning,
3329 *   MPI_COMM_WORLD);
3330 *  
3331 *   Table<2, DoFTools::Coupling> coupling(dim + 1, dim + 1);
3332 *   for (unsigned int c = 0; c < dim + 1; ++c)
3333 *   for (unsigned int d = 0; d < dim + 1; ++d)
3334 *   if (c == d)
3335 *   coupling[c][d] = DoFTools::always;
3336 *   else
3337 *   coupling[c][d] = DoFTools::none;
3338 *  
3339 *   DoFTools::make_sparsity_pattern(stokes_dof_handler,
3340 *   coupling,
3341 *   sp,
3342 *   stokes_constraints,
3343 *   false,
3344 *   Utilities::MPI::this_mpi_process(
3345 *   MPI_COMM_WORLD));
3346 *   sp.compress();
3347 *  
3348 *   stokes_preconditioner_matrix.reinit(sp);
3349 *   }
3350 *  
3351 *  
3352 *   template <int dim>
3353 *   void BoussinesqFlowProblem<dim>::setup_temperature_matrices(
3354 *   const IndexSet &temperature_partitioner,
3355 *   const IndexSet &temperature_relevant_partitioner)
3356 *   {
3357 *   T_preconditioner.reset();
3358 *   temperature_mass_matrix.clear();
3359 *   temperature_stiffness_matrix.clear();
3360 *   temperature_matrix.clear();
3361 *  
3362 *   TrilinosWrappers::SparsityPattern sp(temperature_partitioner,
3363 *   temperature_partitioner,
3364 *   temperature_relevant_partitioner,
3365 *   MPI_COMM_WORLD);
3366 *   DoFTools::make_sparsity_pattern(temperature_dof_handler,
3367 *   sp,
3368 *   temperature_constraints,
3369 *   false,
3370 *   Utilities::MPI::this_mpi_process(
3371 *   MPI_COMM_WORLD));
3372 *   sp.compress();
3373 *  
3374 *   temperature_matrix.reinit(sp);
3375 *   temperature_mass_matrix.reinit(sp);
3376 *   temperature_stiffness_matrix.reinit(sp);
3377 *   }
3378 *  
3379 *  
3380 *  
3381 * @endcode
3382 *
3383 * The remainder of the setup function (after splitting out the three
3384 * functions above) mostly has to deal with the things we need to do for
3385 * parallelization across processors. Because setting all of this up is a
3386 * significant compute time expense of the program, we put everything we do
3387 * here into a timer group so that we can get summary information about the
3388 * fraction of time spent in this part of the program at its end.
3389 *
3390
3391 *
3392 * At the top as usual we enumerate degrees of freedom and sort them by
3393 * component/block, followed by writing their numbers to the screen from
3394 * processor zero. The DoFHandler::distributed_dofs() function, when applied
3395 * to a parallel::distributed::Triangulation object, sorts degrees of
3396 * freedom in such a way that all degrees of freedom associated with
3397 * subdomain zero come before all those associated with subdomain one,
3398 * etc. For the Stokes part, this entails, however, that velocities and
3399 * pressures become intermixed, but this is trivially solved by sorting
3400 * again by blocks; it is worth noting that this latter operation leaves the
3401 * relative ordering of all velocities and pressures alone, i.e. within the
3402 * velocity block we will still have all those associated with subdomain
3403 * zero before all velocities associated with subdomain one, etc. This is
3404 * important since we store each of the blocks of this matrix distributed
3405 * across all processors and want this to be done in such a way that each
3406 * processor stores that part of the matrix that is roughly equal to the
3407 * degrees of freedom located on those cells that it will actually work on.
3408 *
3409
3410 *
3411 * When printing the numbers of degrees of freedom, note that these numbers
3412 * are going to be large if we use many processors. Consequently, we let the
3413 * stream put a comma separator in between every three digits. The state of
3414 * the stream, using the locale, is saved from before to after this
3415 * operation. While slightly opaque, the code works because the default
3416 * locale (which we get using the constructor call
3417 * <code>std::locale("")</code>) implies printing numbers with a comma
3418 * separator for every third digit (i.e., thousands, millions, billions).
3419 *
3420
3421 *
3422 * In this function as well as many below, we measure how much time
3423 * we spend here and collect that in a section called "Setup dof
3424 * systems" across function invocations. This is done using an
3425 * TimerOutput::Scope object that gets a timer going in the section
3426 * with above name of the `computing_timer` object upon construction
3427 * of the local variable; the timer is stopped again when the
3428 * destructor of the `timing_section` variable is called. This, of
3429 * course, happens either at the end of the function, or if we leave
3430 * the function through a `return` statement or when an exception is
3431 * thrown somewhere -- in other words, whenever we leave this
3432 * function in any way. The use of such "scope" objects therefore
3433 * makes sure that we do not have to manually add code that tells
3434 * the timer to stop at every location where this function may be
3435 * left.
3436 *
3437 * @code
3438 *   template <int dim>
3439 *   void BoussinesqFlowProblem<dim>::setup_dofs()
3440 *   {
3441 *   TimerOutput::Scope timing_section(computing_timer, "Setup dof systems");
3442 *  
3443 *   stokes_dof_handler.distribute_dofs(stokes_fe);
3444 *  
3445 *   std::vector<unsigned int> stokes_sub_blocks(dim + 1, 0);
3446 *   stokes_sub_blocks[dim] = 1;
3447 *   DoFRenumbering::component_wise(stokes_dof_handler, stokes_sub_blocks);
3448 *  
3449 *   temperature_dof_handler.distribute_dofs(temperature_fe);
3450 *  
3451 *   const std::vector<types::global_dof_index> stokes_dofs_per_block =
3452 *   DoFTools::count_dofs_per_fe_block(stokes_dof_handler, stokes_sub_blocks);
3453 *  
3454 *   const types::global_dof_index n_u = stokes_dofs_per_block[0],
3455 *   n_p = stokes_dofs_per_block[1],
3456 *   n_T = temperature_dof_handler.n_dofs();
3457 *  
3458 *   std::locale s = pcout.get_stream().getloc();
3459 *   pcout.get_stream().imbue(std::locale(""));
3460 *   pcout << "Number of active cells: " << triangulation.n_global_active_cells()
3461 *   << " (on " << triangulation.n_levels() << " levels)" << std::endl
3462 *   << "Number of degrees of freedom: " << n_u + n_p + n_T << " (" << n_u
3463 *   << '+' << n_p << '+' << n_T << ')' << std::endl
3464 *   << std::endl;
3465 *   pcout.get_stream().imbue(s);
3466 *  
3467 *  
3468 * @endcode
3469 *
3470 * After this, we have to set up the various partitioners (of type
3471 * <code>IndexSet</code>, see the introduction) that describe which parts
3472 * of each matrix or vector will be stored where, then call the functions
3473 * that actually set up the matrices, and at the end also resize the
3474 * various vectors we keep around in this program.
3475 *
3476
3477 *
3478 *
3479 * @code
3480 *   const IndexSet &stokes_locally_owned_index_set =
3481 *   stokes_dof_handler.locally_owned_dofs();
3482 *   const IndexSet stokes_locally_relevant_set =
3483 *   DoFTools::extract_locally_relevant_dofs(stokes_dof_handler);
3484 *  
3485 *   std::vector<IndexSet> stokes_partitioning;
3486 *   stokes_partitioning.push_back(
3487 *   stokes_locally_owned_index_set.get_view(0, n_u));
3488 *   stokes_partitioning.push_back(
3489 *   stokes_locally_owned_index_set.get_view(n_u, n_u + n_p));
3490 *  
3491 *   std::vector<IndexSet> stokes_relevant_partitioning;
3492 *   stokes_relevant_partitioning.push_back(
3493 *   stokes_locally_relevant_set.get_view(0, n_u));
3494 *   stokes_relevant_partitioning.push_back(
3495 *   stokes_locally_relevant_set.get_view(n_u, n_u + n_p));
3496 *  
3497 *   const IndexSet temperature_partitioning =
3498 *   temperature_dof_handler.locally_owned_dofs();
3499 *   const IndexSet temperature_relevant_partitioning =
3500 *   DoFTools::extract_locally_relevant_dofs(temperature_dof_handler);
3501 *  
3502 * @endcode
3503 *
3504 * Following this, we can compute constraints for the solution vectors,
3505 * including hanging node constraints and homogeneous and inhomogeneous
3506 * boundary values for the Stokes and temperature fields. Note that as for
3507 * everything else, the constraint objects can not hold <i>all</i>
3508 * constraints on every processor. Rather, each processor needs to store
3509 * only those that are actually necessary for correctness given that it
3510 * only assembles linear systems on cells it owns. As discussed in the
3511 * @ref distributed_paper "this paper", the set of constraints we need to
3512 * know about is exactly the set of constraints on all locally relevant
3513 * degrees of freedom, so this is what we use to initialize the constraint
3514 * objects.
3515 *
3516 * @code
3517 *   {
3518 *   stokes_constraints.clear();
3519 *   stokes_constraints.reinit(stokes_locally_owned_index_set,
3520 *   stokes_locally_relevant_set);
3521 *  
3522 *   DoFTools::make_hanging_node_constraints(stokes_dof_handler,
3523 *   stokes_constraints);
3524 *  
3525 *   const FEValuesExtractors::Vector velocity_components(0);
3526 *   VectorTools::interpolate_boundary_values(
3527 *   stokes_dof_handler,
3528 *   0,
3529 *   Functions::ZeroFunction<dim>(dim + 1),
3530 *   stokes_constraints,
3531 *   stokes_fe.component_mask(velocity_components));
3532 *  
3533 *   std::set<types::boundary_id> no_normal_flux_boundaries;
3534 *   no_normal_flux_boundaries.insert(1);
3535 *   VectorTools::compute_no_normal_flux_constraints(stokes_dof_handler,
3536 *   0,
3537 *   no_normal_flux_boundaries,
3538 *   stokes_constraints,
3539 *   mapping);
3540 *   stokes_constraints.close();
3541 *   }
3542 *   {
3543 *   temperature_constraints.clear();
3544 *   temperature_constraints.reinit(temperature_partitioning,
3545 *   temperature_relevant_partitioning);
3546 *  
3547 *   DoFTools::make_hanging_node_constraints(temperature_dof_handler,
3548 *   temperature_constraints);
3549 *   VectorTools::interpolate_boundary_values(
3550 *   temperature_dof_handler,
3551 *   0,
3552 *   EquationData::TemperatureInitialValues<dim>(),
3553 *   temperature_constraints);
3554 *   VectorTools::interpolate_boundary_values(
3555 *   temperature_dof_handler,
3556 *   1,
3557 *   EquationData::TemperatureInitialValues<dim>(),
3558 *   temperature_constraints);
3559 *   temperature_constraints.close();
3560 *   }
3561 *  
3562 * @endcode
3563 *
3564 * All this done, we can then initialize the various matrix and vector
3565 * objects to their proper sizes. At the end, we also record that all
3566 * matrices and preconditioners have to be re-computed at the beginning of
3567 * the next time step. Note how we initialize the vectors for the Stokes
3568 * and temperature right hand sides: These are writable vectors (last
3569 * boolean argument set to @p true) that have the correct one-to-one
3570 * partitioning of locally owned elements but are still given the relevant
3571 * partitioning for means of figuring out the vector entries that are
3572 * going to be set right away. As for matrices, this allows for writing
3573 * local contributions into the vector with multiple threads (always
3574 * assuming that the same vector entry is not accessed by multiple threads
3575 * at the same time). The other vectors only allow for read access of
3576 * individual elements, including ghosts, but are not suitable for
3577 * solvers.
3578 *
3579 * @code
3580 *   setup_stokes_matrix(stokes_partitioning, stokes_relevant_partitioning);
3581 *   setup_stokes_preconditioner(stokes_partitioning,
3582 *   stokes_relevant_partitioning);
3583 *   setup_temperature_matrices(temperature_partitioning,
3584 *   temperature_relevant_partitioning);
3585 *  
3586 *   stokes_rhs.reinit(stokes_partitioning,
3587 *   stokes_relevant_partitioning,
3588 *   MPI_COMM_WORLD,
3589 *   true);
3590 *   stokes_solution.reinit(stokes_relevant_partitioning, MPI_COMM_WORLD);
3591 *   old_stokes_solution.reinit(stokes_solution);
3592 *  
3593 *   temperature_rhs.reinit(temperature_partitioning,
3594 *   temperature_relevant_partitioning,
3595 *   MPI_COMM_WORLD,
3596 *   true);
3597 *   temperature_solution.reinit(temperature_relevant_partitioning,
3598 *   MPI_COMM_WORLD);
3599 *   old_temperature_solution.reinit(temperature_solution);
3600 *   old_old_temperature_solution.reinit(temperature_solution);
3601 *  
3602 *   rebuild_stokes_matrix = true;
3603 *   rebuild_stokes_preconditioner = true;
3604 *   rebuild_temperature_matrices = true;
3605 *   rebuild_temperature_preconditioner = true;
3606 *   }
3607 *  
3608 *  
3609 *  
3610 * @endcode
3611 *
3612 *
3613 * <a name="step_32-TheBoussinesqFlowProblemassemblyfunctions"></a>
3614 * <h4>The BoussinesqFlowProblem assembly functions</h4>
3615 *
3616
3617 *
3618 * Following the discussion in the introduction and in the @ref threads
3619 * topic, we split the assembly functions into different parts:
3620 *
3621
3622 *
3623 * <ul> <li> The local calculations of matrices and right hand sides, given
3624 * a certain cell as input (these functions are named
3625 * <code>local_assemble_*</code> below). The resulting function is, in other
3626 * words, essentially the body of the loop over all cells in @ref step_31 "step-31". Note,
3627 * however, that these functions store the result from the local
3628 * calculations in variables of classes from the CopyData namespace.
3629 *
3630
3631 *
3632 * <li>These objects are then given to the second step which writes the
3633 * local data into the global data structures (these functions are named
3634 * <code>copy_local_to_global_*</code> below). These functions are pretty
3635 * trivial.
3636 *
3637
3638 *
3639 * <li>These two subfunctions are then used in the respective assembly
3640 * routine (called <code>assemble_*</code> below), where a WorkStream object
3641 * is set up and runs over all the cells that belong to the processor's
3642 * subdomain. </ul>
3643 *
3644
3645 *
3646 *
3647 * <a name="step_32-Stokespreconditionerassembly"></a>
3648 * <h5>Stokes preconditioner assembly</h5>
3649 *
3650
3651 *
3652 * Let us start with the functions that builds the Stokes
3653 * preconditioner. The first two of these are pretty trivial, given the
3654 * discussion above. Note in particular that the main point in using the
3655 * scratch data object is that we want to avoid allocating any objects on
3656 * the free space each time we visit a new cell. As a consequence, the
3657 * assembly function below only has automatic local variables, and
3658 * everything else is accessed through the scratch data object, which is
3659 * allocated only once before we start the loop over all cells:
3660 *
3661 * @code
3662 *   template <int dim>
3663 *   void BoussinesqFlowProblem<dim>::local_assemble_stokes_preconditioner(
3664 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
3665 *   Assembly::Scratch::StokesPreconditioner<dim> &scratch,
3666 *   Assembly::CopyData::StokesPreconditioner<dim> &data)
3667 *   {
3668 *   const unsigned int dofs_per_cell = stokes_fe.n_dofs_per_cell();
3669 *   const unsigned int n_q_points =
3670 *   scratch.stokes_fe_values.n_quadrature_points;
3671 *  
3672 *   const FEValuesExtractors::Vector velocities(0);
3673 *   const FEValuesExtractors::Scalar pressure(dim);
3674 *  
3675 *   scratch.stokes_fe_values.reinit(cell);
3676 *   cell->get_dof_indices(data.local_dof_indices);
3677 *  
3678 *   data.local_matrix = 0;
3679 *  
3680 *   for (unsigned int q = 0; q < n_q_points; ++q)
3681 *   {
3682 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
3683 *   {
3684 *   scratch.grad_phi_u[k] =
3685 *   scratch.stokes_fe_values[velocities].gradient(k, q);
3686 *   scratch.phi_p[k] = scratch.stokes_fe_values[pressure].value(k, q);
3687 *   }
3688 *  
3689 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
3690 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
3691 *   data.local_matrix(i, j) +=
3692 *   (EquationData::eta *
3693 *   scalar_product(scratch.grad_phi_u[i], scratch.grad_phi_u[j]) +
3694 *   (1. / EquationData::eta) * EquationData::pressure_scaling *
3695 *   EquationData::pressure_scaling *
3696 *   (scratch.phi_p[i] * scratch.phi_p[j])) *
3697 *   scratch.stokes_fe_values.JxW(q);
3698 *   }
3699 *   }
3700 *  
3701 *  
3702 *  
3703 *   template <int dim>
3704 *   void BoussinesqFlowProblem<dim>::copy_local_to_global_stokes_preconditioner(
3705 *   const Assembly::CopyData::StokesPreconditioner<dim> &data)
3706 *   {
3707 *   stokes_constraints.distribute_local_to_global(data.local_matrix,
3708 *   data.local_dof_indices,
3709 *   stokes_preconditioner_matrix);
3710 *   }
3711 *  
3712 *  
3713 * @endcode
3714 *
3715 * Now for the function that actually puts things together, using the
3716 * WorkStream functions. WorkStream::run needs a start and end iterator to
3717 * enumerate the cells it is supposed to work on. Typically, one would use
3718 * DoFHandler::begin_active() and DoFHandler::end() for that but here we
3719 * actually only want the subset of cells that in fact are owned by the
3720 * current processor. This is where the FilteredIterator class comes into
3721 * play: you give it a range of cells and it provides an iterator that only
3722 * iterates over that subset of cells that satisfy a certain predicate (a
3723 * predicate is a function of one argument that either returns true or
3724 * false). The predicate we use here is IteratorFilters::LocallyOwnedCell,
3725 * i.e., it returns true exactly if the cell is owned by the current
3726 * processor. The resulting iterator range is then exactly what we need.
3727 *
3728
3729 *
3730 * With this obstacle out of the way, we call the WorkStream::run
3731 * function with this set of cells, scratch and copy objects, and
3732 * with pointers to two functions: the local assembly and
3733 * copy-local-to-global function. These functions need to have very
3734 * specific signatures: three arguments in the first and one
3735 * argument in the latter case (see the documentation of the
3736 * WorkStream::run function for the meaning of these arguments).
3737 * Note how we use a lambda functions to
3738 * create a function object that satisfies this requirement. It uses
3739 * function arguments for the local assembly function that specify
3740 * cell, scratch data, and copy data, as well as function argument
3741 * for the copy function that expects the
3742 * data to be written into the global matrix (also see the discussion in
3743 * @ref step_13 "step-13"'s <code>assemble_linear_system()</code> function). On the other
3744 * hand, the implicit zeroth argument of member functions (namely
3745 * the <code>this</code> pointer of the object on which that member
3746 * function is to operate on) is <i>bound</i> to the
3747 * <code>this</code> pointer of the current function and is captured. The
3748 * WorkStream::run function, as a consequence, does not need to know
3749 * anything about the object these functions work on.
3750 *
3751
3752 *
3753 * When the WorkStream is executed, it will create several local assembly
3754 * routines of the first kind for several cells and let some available
3755 * processors work on them. The function that needs to be synchronized,
3756 * i.e., the write operation into the global matrix, however, is executed by
3757 * only one thread at a time in the prescribed order. Of course, this only
3758 * holds for the parallelization on a single MPI process. Different MPI
3759 * processes will have their own WorkStream objects and do that work
3760 * completely independently (and in different memory spaces). In a
3761 * distributed calculation, some data will accumulate at degrees of freedom
3762 * that are not owned by the respective processor. It would be inefficient
3763 * to send data around every time we encounter such a dof. What happens
3764 * instead is that the Trilinos sparse matrix will keep that data and send
3765 * it to the owner at the end of assembly, by calling the
3766 * <code>compress()</code> command.
3767 *
3768 * @code
3769 *   template <int dim>
3770 *   void BoussinesqFlowProblem<dim>::assemble_stokes_preconditioner()
3771 *   {
3772 *   stokes_preconditioner_matrix = 0;
3773 *  
3774 *   const QGauss<dim> quadrature_formula(parameters.stokes_velocity_degree + 1);
3775 *  
3776 *   using CellFilter =
3778 *  
3779 *   auto worker =
3780 *   [this](const typename DoFHandler<dim>::active_cell_iterator &cell,
3781 *   Assembly::Scratch::StokesPreconditioner<dim> &scratch,
3782 *   Assembly::CopyData::StokesPreconditioner<dim> &data) {
3783 *   this->local_assemble_stokes_preconditioner(cell, scratch, data);
3784 *   };
3785 *  
3786 *   auto copier =
3787 *   [this](const Assembly::CopyData::StokesPreconditioner<dim> &data) {
3788 *   this->copy_local_to_global_stokes_preconditioner(data);
3789 *   };
3790 *  
3792 *   stokes_dof_handler.begin_active()),
3793 *   CellFilter(IteratorFilters::LocallyOwnedCell(),
3794 *   stokes_dof_handler.end()),
3795 *   worker,
3796 *   copier,
3797 *   Assembly::Scratch::StokesPreconditioner<dim>(
3798 *   stokes_fe,
3799 *   quadrature_formula,
3800 *   mapping,
3802 *   Assembly::CopyData::StokesPreconditioner<dim>(stokes_fe));
3803 *  
3804 *   stokes_preconditioner_matrix.compress(VectorOperation::add);
3805 *   }
3806 *  
3807 *  
3808 *  
3809 * @endcode
3810 *
3811 * The final function in this block initiates assembly of the Stokes
3812 * preconditioner matrix and then in fact builds the Stokes
3813 * preconditioner. It is mostly the same as in the serial case. The only
3814 * difference to @ref step_31 "step-31" is that we use a Jacobi preconditioner for the
3815 * pressure mass matrix instead of IC, as discussed in the introduction.
3816 *
3817 * @code
3818 *   template <int dim>
3819 *   void BoussinesqFlowProblem<dim>::build_stokes_preconditioner()
3820 *   {
3821 *   if (rebuild_stokes_preconditioner == false)
3822 *   return;
3823 *  
3824 *   TimerOutput::Scope timer_section(computing_timer,
3825 *   " Build Stokes preconditioner");
3826 *   pcout << " Rebuilding Stokes preconditioner..." << std::flush;
3827 *  
3828 *   assemble_stokes_preconditioner();
3829 *  
3830 *   std::vector<std::vector<bool>> constant_modes;
3831 *   const FEValuesExtractors::Vector velocity_components(0);
3832 *   DoFTools::extract_constant_modes(stokes_dof_handler,
3833 *   stokes_fe.component_mask(
3834 *   velocity_components),
3835 *   constant_modes);
3836 *  
3837 *   Mp_preconditioner =
3838 *   std::make_shared<TrilinosWrappers::PreconditionJacobi>();
3839 *   Amg_preconditioner = std::make_shared<TrilinosWrappers::PreconditionAMG>();
3840 *  
3842 *   Amg_data.constant_modes = constant_modes;
3843 *   Amg_data.elliptic = true;
3844 *   Amg_data.higher_order_elements = true;
3845 *   Amg_data.smoother_sweeps = 2;
3846 *   Amg_data.aggregation_threshold = 0.02;
3847 *  
3848 *   Mp_preconditioner->initialize(stokes_preconditioner_matrix.block(1, 1));
3849 *   Amg_preconditioner->initialize(stokes_preconditioner_matrix.block(0, 0),
3850 *   Amg_data);
3851 *  
3852 *   rebuild_stokes_preconditioner = false;
3853 *  
3854 *   pcout << std::endl;
3855 *   }
3856 *  
3857 *  
3858 * @endcode
3859 *
3860 *
3861 * <a name="step_32-Stokessystemassembly"></a>
3862 * <h5>Stokes system assembly</h5>
3863 *
3864
3865 *
3866 * The next three functions implement the assembly of the Stokes system,
3867 * again split up into a part performing local calculations, one for writing
3868 * the local data into the global matrix and vector, and one for actually
3869 * running the loop over all cells with the help of the WorkStream
3870 * class. Note that the assembly of the Stokes matrix needs only to be done
3871 * in case we have changed the mesh. Otherwise, just the
3872 * (temperature-dependent) right hand side needs to be calculated
3873 * here. Since we are working with distributed matrices and vectors, we have
3874 * to call the respective <code>compress()</code> functions in the end of
3875 * the assembly in order to send non-local data to the owner process.
3876 *
3877 * @code
3878 *   template <int dim>
3879 *   void BoussinesqFlowProblem<dim>::local_assemble_stokes_system(
3880 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
3881 *   Assembly::Scratch::StokesSystem<dim> &scratch,
3882 *   Assembly::CopyData::StokesSystem<dim> &data)
3883 *   {
3884 *   const unsigned int dofs_per_cell =
3885 *   scratch.stokes_fe_values.get_fe().n_dofs_per_cell();
3886 *   const unsigned int n_q_points =
3887 *   scratch.stokes_fe_values.n_quadrature_points;
3888 *  
3889 *   const FEValuesExtractors::Vector velocities(0);
3890 *   const FEValuesExtractors::Scalar pressure(dim);
3891 *  
3892 *   scratch.stokes_fe_values.reinit(cell);
3893 *  
3894 *   const typename DoFHandler<dim>::active_cell_iterator temperature_cell =
3895 *   cell->as_dof_handler_iterator(temperature_dof_handler);
3896 *   scratch.temperature_fe_values.reinit(temperature_cell);
3897 *  
3898 *   if (rebuild_stokes_matrix)
3899 *   data.local_matrix = 0;
3900 *   data.local_rhs = 0;
3901 *  
3902 *   scratch.temperature_fe_values.get_function_values(
3903 *   old_temperature_solution, scratch.old_temperature_values);
3904 *  
3905 *   for (unsigned int q = 0; q < n_q_points; ++q)
3906 *   {
3907 *   const double old_temperature = scratch.old_temperature_values[q];
3908 *  
3909 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
3910 *   {
3911 *   scratch.phi_u[k] = scratch.stokes_fe_values[velocities].value(k, q);
3912 *   if (rebuild_stokes_matrix)
3913 *   {
3914 *   scratch.grads_phi_u[k] =
3915 *   scratch.stokes_fe_values[velocities].symmetric_gradient(k, q);
3916 *   scratch.div_phi_u[k] =
3917 *   scratch.stokes_fe_values[velocities].divergence(k, q);
3918 *   scratch.phi_p[k] =
3919 *   scratch.stokes_fe_values[pressure].value(k, q);
3920 *   }
3921 *   }
3922 *  
3923 *   if (rebuild_stokes_matrix == true)
3924 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
3925 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
3926 *   data.local_matrix(i, j) +=
3927 *   (EquationData::eta * 2 *
3928 *   (scratch.grads_phi_u[i] * scratch.grads_phi_u[j]) -
3929 *   (EquationData::pressure_scaling * scratch.div_phi_u[i] *
3930 *   scratch.phi_p[j]) -
3931 *   (EquationData::pressure_scaling * scratch.phi_p[i] *
3932 *   scratch.div_phi_u[j])) *
3933 *   scratch.stokes_fe_values.JxW(q);
3934 *  
3935 *   const Tensor<1, dim> gravity = EquationData::gravity_vector(
3936 *   scratch.stokes_fe_values.quadrature_point(q));
3937 *  
3938 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
3939 *   data.local_rhs(i) += (EquationData::density(old_temperature) *
3940 *   gravity * scratch.phi_u[i]) *
3941 *   scratch.stokes_fe_values.JxW(q);
3942 *   }
3943 *  
3944 *   cell->get_dof_indices(data.local_dof_indices);
3945 *   }
3946 *  
3947 *  
3948 *  
3949 *   template <int dim>
3950 *   void BoussinesqFlowProblem<dim>::copy_local_to_global_stokes_system(
3951 *   const Assembly::CopyData::StokesSystem<dim> &data)
3952 *   {
3953 *   if (rebuild_stokes_matrix == true)
3954 *   stokes_constraints.distribute_local_to_global(data.local_matrix,
3955 *   data.local_rhs,
3956 *   data.local_dof_indices,
3957 *   stokes_matrix,
3958 *   stokes_rhs);
3959 *   else
3960 *   stokes_constraints.distribute_local_to_global(data.local_rhs,
3961 *   data.local_dof_indices,
3962 *   stokes_rhs);
3963 *   }
3964 *  
3965 *  
3966 *  
3967 *   template <int dim>
3968 *   void BoussinesqFlowProblem<dim>::assemble_stokes_system()
3969 *   {
3970 *   TimerOutput::Scope timer_section(computing_timer,
3971 *   " Assemble Stokes system");
3972 *  
3973 *   if (rebuild_stokes_matrix == true)
3974 *   stokes_matrix = 0;
3975 *  
3976 *   stokes_rhs = 0;
3977 *  
3978 *   const QGauss<dim> quadrature_formula(parameters.stokes_velocity_degree + 1);
3979 *  
3980 *   using CellFilter =
3982 *  
3983 *   WorkStream::run(
3984 *   CellFilter(IteratorFilters::LocallyOwnedCell(),
3985 *   stokes_dof_handler.begin_active()),
3986 *   CellFilter(IteratorFilters::LocallyOwnedCell(), stokes_dof_handler.end()),
3987 *   [this](const typename DoFHandler<dim>::active_cell_iterator &cell,
3988 *   Assembly::Scratch::StokesSystem<dim> &scratch,
3989 *   Assembly::CopyData::StokesSystem<dim> &data) {
3990 *   this->local_assemble_stokes_system(cell, scratch, data);
3991 *   },
3992 *   [this](const Assembly::CopyData::StokesSystem<dim> &data) {
3993 *   this->copy_local_to_global_stokes_system(data);
3994 *   },
3995 *   Assembly::Scratch::StokesSystem<dim>(
3996 *   stokes_fe,
3997 *   mapping,
3998 *   quadrature_formula,
4000 *   (rebuild_stokes_matrix == true ? update_gradients : UpdateFlags(0))),
4001 *   temperature_fe,
4002 *   update_values),
4003 *   Assembly::CopyData::StokesSystem<dim>(stokes_fe));
4004 *  
4005 *   if (rebuild_stokes_matrix == true)
4006 *   stokes_matrix.compress(VectorOperation::add);
4007 *   stokes_rhs.compress(VectorOperation::add);
4008 *  
4009 *   rebuild_stokes_matrix = false;
4010 *  
4011 *   pcout << std::endl;
4012 *   }
4013 *  
4014 *  
4015 * @endcode
4016 *
4017 *
4018 * <a name="step_32-Temperaturematrixassembly"></a>
4019 * <h5>Temperature matrix assembly</h5>
4020 *
4021
4022 *
4023 * The task to be performed by the next three functions is to calculate a
4024 * mass matrix and a Laplace matrix on the temperature system. These will be
4025 * combined in order to yield the semi-implicit time stepping matrix that
4026 * consists of the mass matrix plus a time step-dependent weight factor
4027 * times the Laplace matrix. This function is again essentially the body of
4028 * the loop over all cells from @ref step_31 "step-31".
4029 *
4030
4031 *
4032 * The two following functions perform similar services as the ones above.
4033 *
4034 * @code
4035 *   template <int dim>
4036 *   void BoussinesqFlowProblem<dim>::local_assemble_temperature_matrix(
4037 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
4038 *   Assembly::Scratch::TemperatureMatrix<dim> &scratch,
4039 *   Assembly::CopyData::TemperatureMatrix<dim> &data)
4040 *   {
4041 *   const unsigned int dofs_per_cell =
4042 *   scratch.temperature_fe_values.get_fe().n_dofs_per_cell();
4043 *   const unsigned int n_q_points =
4044 *   scratch.temperature_fe_values.n_quadrature_points;
4045 *  
4046 *   scratch.temperature_fe_values.reinit(cell);
4047 *   cell->get_dof_indices(data.local_dof_indices);
4048 *  
4049 *   data.local_mass_matrix = 0;
4050 *   data.local_stiffness_matrix = 0;
4051 *  
4052 *   for (unsigned int q = 0; q < n_q_points; ++q)
4053 *   {
4054 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
4055 *   {
4056 *   scratch.grad_phi_T[k] =
4057 *   scratch.temperature_fe_values.shape_grad(k, q);
4058 *   scratch.phi_T[k] = scratch.temperature_fe_values.shape_value(k, q);
4059 *   }
4060 *  
4061 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
4062 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
4063 *   {
4064 *   data.local_mass_matrix(i, j) +=
4065 *   (scratch.phi_T[i] * scratch.phi_T[j] *
4066 *   scratch.temperature_fe_values.JxW(q));
4067 *   data.local_stiffness_matrix(i, j) +=
4068 *   (EquationData::kappa * scratch.grad_phi_T[i] *
4069 *   scratch.grad_phi_T[j] * scratch.temperature_fe_values.JxW(q));
4070 *   }
4071 *   }
4072 *   }
4073 *  
4074 *  
4075 *  
4076 *   template <int dim>
4077 *   void BoussinesqFlowProblem<dim>::copy_local_to_global_temperature_matrix(
4078 *   const Assembly::CopyData::TemperatureMatrix<dim> &data)
4079 *   {
4080 *   temperature_constraints.distribute_local_to_global(data.local_mass_matrix,
4081 *   data.local_dof_indices,
4082 *   temperature_mass_matrix);
4083 *   temperature_constraints.distribute_local_to_global(
4084 *   data.local_stiffness_matrix,
4085 *   data.local_dof_indices,
4086 *   temperature_stiffness_matrix);
4087 *   }
4088 *  
4089 *  
4090 *   template <int dim>
4091 *   void BoussinesqFlowProblem<dim>::assemble_temperature_matrix()
4092 *   {
4093 *   if (rebuild_temperature_matrices == false)
4094 *   return;
4095 *  
4096 *   TimerOutput::Scope timer_section(computing_timer,
4097 *   " Assemble temperature matrices");
4098 *   temperature_mass_matrix = 0;
4099 *   temperature_stiffness_matrix = 0;
4100 *  
4101 *   const QGauss<dim> quadrature_formula(parameters.temperature_degree + 2);
4102 *  
4103 *   using CellFilter =
4105 *  
4106 *   WorkStream::run(
4107 *   CellFilter(IteratorFilters::LocallyOwnedCell(),
4108 *   temperature_dof_handler.begin_active()),
4109 *   CellFilter(IteratorFilters::LocallyOwnedCell(),
4110 *   temperature_dof_handler.end()),
4111 *   [this](const typename DoFHandler<dim>::active_cell_iterator &cell,
4112 *   Assembly::Scratch::TemperatureMatrix<dim> &scratch,
4113 *   Assembly::CopyData::TemperatureMatrix<dim> &data) {
4114 *   this->local_assemble_temperature_matrix(cell, scratch, data);
4115 *   },
4116 *   [this](const Assembly::CopyData::TemperatureMatrix<dim> &data) {
4117 *   this->copy_local_to_global_temperature_matrix(data);
4118 *   },
4119 *   Assembly::Scratch::TemperatureMatrix<dim>(temperature_fe,
4120 *   mapping,
4121 *   quadrature_formula),
4122 *   Assembly::CopyData::TemperatureMatrix<dim>(temperature_fe));
4123 *  
4124 *   temperature_mass_matrix.compress(VectorOperation::add);
4125 *   temperature_stiffness_matrix.compress(VectorOperation::add);
4126 *  
4127 *   rebuild_temperature_matrices = false;
4128 *   rebuild_temperature_preconditioner = true;
4129 *   }
4130 *  
4131 *  
4132 * @endcode
4133 *
4134 *
4135 * <a name="step_32-Temperaturerighthandsideassembly"></a>
4136 * <h5>Temperature right hand side assembly</h5>
4137 *
4138
4139 *
4140 * This is the last assembly function. It calculates the right hand side of
4141 * the temperature system, which includes the convection and the
4142 * stabilization terms. It includes a lot of evaluations of old solutions at
4143 * the quadrature points (which are necessary for calculating the artificial
4144 * viscosity of stabilization), but is otherwise similar to the other
4145 * assembly functions. Notice, once again, how we resolve the dilemma of
4146 * having inhomogeneous boundary conditions, by just making a right hand
4147 * side at this point (compare the comments for the <code>project()</code>
4148 * function above): We create some matrix columns with exactly the values
4149 * that would be entered for the temperature @ref GlossStiffnessMatrix "stiffness matrix", in case we
4150 * have inhomogeneously constrained dofs. That will account for the correct
4151 * balance of the right hand side vector with the matrix system of
4152 * temperature.
4153 *
4154 * @code
4155 *   template <int dim>
4156 *   void BoussinesqFlowProblem<dim>::local_assemble_temperature_rhs(
4157 *   const std::pair<double, double> global_T_range,
4158 *   const double global_max_velocity,
4159 *   const double global_entropy_variation,
4160 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
4161 *   Assembly::Scratch::TemperatureRHS<dim> &scratch,
4162 *   Assembly::CopyData::TemperatureRHS<dim> &data)
4163 *   {
4164 *   const bool use_bdf2_scheme = (timestep_number != 0);
4165 *  
4166 *   const unsigned int dofs_per_cell =
4167 *   scratch.temperature_fe_values.get_fe().n_dofs_per_cell();
4168 *   const unsigned int n_q_points =
4169 *   scratch.temperature_fe_values.n_quadrature_points;
4170 *  
4171 *   const FEValuesExtractors::Vector velocities(0);
4172 *  
4173 *   data.local_rhs = 0;
4174 *   data.matrix_for_bc = 0;
4175 *   cell->get_dof_indices(data.local_dof_indices);
4176 *  
4177 *   scratch.temperature_fe_values.reinit(cell);
4178 *  
4179 *   typename DoFHandler<dim>::active_cell_iterator stokes_cell =
4180 *   cell->as_dof_handler_iterator(stokes_dof_handler);
4181 *   scratch.stokes_fe_values.reinit(stokes_cell);
4182 *  
4183 *   scratch.temperature_fe_values.get_function_values(
4184 *   old_temperature_solution, scratch.old_temperature_values);
4185 *   scratch.temperature_fe_values.get_function_values(
4186 *   old_old_temperature_solution, scratch.old_old_temperature_values);
4187 *  
4188 *   scratch.temperature_fe_values.get_function_gradients(
4189 *   old_temperature_solution, scratch.old_temperature_grads);
4190 *   scratch.temperature_fe_values.get_function_gradients(
4191 *   old_old_temperature_solution, scratch.old_old_temperature_grads);
4192 *  
4193 *   scratch.temperature_fe_values.get_function_laplacians(
4194 *   old_temperature_solution, scratch.old_temperature_laplacians);
4195 *   scratch.temperature_fe_values.get_function_laplacians(
4196 *   old_old_temperature_solution, scratch.old_old_temperature_laplacians);
4197 *  
4198 *   scratch.stokes_fe_values[velocities].get_function_values(
4199 *   stokes_solution, scratch.old_velocity_values);
4200 *   scratch.stokes_fe_values[velocities].get_function_values(
4201 *   old_stokes_solution, scratch.old_old_velocity_values);
4202 *   scratch.stokes_fe_values[velocities].get_function_symmetric_gradients(
4203 *   stokes_solution, scratch.old_strain_rates);
4204 *   scratch.stokes_fe_values[velocities].get_function_symmetric_gradients(
4205 *   old_stokes_solution, scratch.old_old_strain_rates);
4206 *  
4207 *   const double nu =
4208 *   compute_viscosity(scratch.old_temperature_values,
4209 *   scratch.old_old_temperature_values,
4210 *   scratch.old_temperature_grads,
4211 *   scratch.old_old_temperature_grads,
4212 *   scratch.old_temperature_laplacians,
4213 *   scratch.old_old_temperature_laplacians,
4214 *   scratch.old_velocity_values,
4215 *   scratch.old_old_velocity_values,
4216 *   scratch.old_strain_rates,
4217 *   scratch.old_old_strain_rates,
4218 *   global_max_velocity,
4219 *   global_T_range.second - global_T_range.first,
4220 *   0.5 * (global_T_range.second + global_T_range.first),
4221 *   global_entropy_variation,
4222 *   cell->diameter());
4223 *  
4224 *   for (unsigned int q = 0; q < n_q_points; ++q)
4225 *   {
4226 *   for (unsigned int k = 0; k < dofs_per_cell; ++k)
4227 *   {
4228 *   scratch.phi_T[k] = scratch.temperature_fe_values.shape_value(k, q);
4229 *   scratch.grad_phi_T[k] =
4230 *   scratch.temperature_fe_values.shape_grad(k, q);
4231 *   }
4232 *  
4233 *  
4234 *   const double T_term_for_rhs =
4235 *   (use_bdf2_scheme ?
4236 *   (scratch.old_temperature_values[q] *
4237 *   (1 + time_step / old_time_step) -
4238 *   scratch.old_old_temperature_values[q] * (time_step * time_step) /
4239 *   (old_time_step * (time_step + old_time_step))) :
4240 *   scratch.old_temperature_values[q]);
4241 *  
4242 *   const double ext_T =
4243 *   (use_bdf2_scheme ? (scratch.old_temperature_values[q] *
4244 *   (1 + time_step / old_time_step) -
4245 *   scratch.old_old_temperature_values[q] *
4246 *   time_step / old_time_step) :
4247 *   scratch.old_temperature_values[q]);
4248 *  
4249 *   const Tensor<1, dim> ext_grad_T =
4250 *   (use_bdf2_scheme ? (scratch.old_temperature_grads[q] *
4251 *   (1 + time_step / old_time_step) -
4252 *   scratch.old_old_temperature_grads[q] * time_step /
4253 *   old_time_step) :
4254 *   scratch.old_temperature_grads[q]);
4255 *  
4256 *   const Tensor<1, dim> extrapolated_u =
4257 *   (use_bdf2_scheme ?
4258 *   (scratch.old_velocity_values[q] * (1 + time_step / old_time_step) -
4259 *   scratch.old_old_velocity_values[q] * time_step / old_time_step) :
4260 *   scratch.old_velocity_values[q]);
4261 *  
4262 *   const SymmetricTensor<2, dim> extrapolated_strain_rate =
4263 *   (use_bdf2_scheme ?
4264 *   (scratch.old_strain_rates[q] * (1 + time_step / old_time_step) -
4265 *   scratch.old_old_strain_rates[q] * time_step / old_time_step) :
4266 *   scratch.old_strain_rates[q]);
4267 *  
4268 *   const double gamma =
4269 *   ((EquationData::radiogenic_heating * EquationData::density(ext_T) +
4270 *   2 * EquationData::eta * extrapolated_strain_rate *
4271 *   extrapolated_strain_rate) /
4272 *   (EquationData::density(ext_T) * EquationData::specific_heat));
4273 *  
4274 *   for (unsigned int i = 0; i < dofs_per_cell; ++i)
4275 *   {
4276 *   data.local_rhs(i) +=
4277 *   (T_term_for_rhs * scratch.phi_T[i] -
4278 *   time_step * extrapolated_u * ext_grad_T * scratch.phi_T[i] -
4279 *   time_step * nu * ext_grad_T * scratch.grad_phi_T[i] +
4280 *   time_step * gamma * scratch.phi_T[i]) *
4281 *   scratch.temperature_fe_values.JxW(q);
4282 *  
4283 *   if (temperature_constraints.is_inhomogeneously_constrained(
4284 *   data.local_dof_indices[i]))
4285 *   {
4286 *   for (unsigned int j = 0; j < dofs_per_cell; ++j)
4287 *   data.matrix_for_bc(j, i) +=
4288 *   (scratch.phi_T[i] * scratch.phi_T[j] *
4289 *   (use_bdf2_scheme ? ((2 * time_step + old_time_step) /
4290 *   (time_step + old_time_step)) :
4291 *   1.) +
4292 *   scratch.grad_phi_T[i] * scratch.grad_phi_T[j] *
4293 *   EquationData::kappa * time_step) *
4294 *   scratch.temperature_fe_values.JxW(q);
4295 *   }
4296 *   }
4297 *   }
4298 *   }
4299 *  
4300 *  
4301 *   template <int dim>
4302 *   void BoussinesqFlowProblem<dim>::copy_local_to_global_temperature_rhs(
4303 *   const Assembly::CopyData::TemperatureRHS<dim> &data)
4304 *   {
4305 *   temperature_constraints.distribute_local_to_global(data.local_rhs,
4306 *   data.local_dof_indices,
4307 *   temperature_rhs,
4308 *   data.matrix_for_bc);
4309 *   }
4310 *  
4311 *  
4312 *  
4313 * @endcode
4314 *
4315 * In the function that runs the WorkStream for actually calculating the
4316 * right hand side, we also generate the final matrix. As mentioned above,
4317 * it is a sum of the mass matrix and the Laplace matrix, times some time
4318 * step-dependent weight. This weight is specified by the BDF-2 time
4319 * integration scheme, see the introduction in @ref step_31 "step-31". What is new in this
4320 * tutorial program (in addition to the use of MPI parallelization and the
4321 * WorkStream class), is that we now precompute the temperature
4322 * preconditioner as well. The reason is that the setup of the Jacobi
4323 * preconditioner takes a noticeable time compared to the solver because we
4324 * usually only need between 10 and 20 iterations for solving the
4325 * temperature system (this might sound strange, as Jacobi really only
4326 * consists of a diagonal, but in Trilinos it is derived from more general
4327 * framework for point relaxation preconditioners which is a bit
4328 * inefficient). Hence, it is more efficient to precompute the
4329 * preconditioner, even though the matrix entries may slightly change
4330 * because the time step might change. This is not too big a problem because
4331 * we remesh every few time steps (and regenerate the preconditioner then).
4332 *
4333 * @code
4334 *   template <int dim>
4335 *   void BoussinesqFlowProblem<dim>::assemble_temperature_system(
4336 *   const double maximal_velocity)
4337 *   {
4338 *   const bool use_bdf2_scheme = (timestep_number != 0);
4339 *  
4340 *   if (use_bdf2_scheme == true)
4341 *   {
4342 *   temperature_matrix.copy_from(temperature_mass_matrix);
4343 *   temperature_matrix *=
4344 *   (2 * time_step + old_time_step) / (time_step + old_time_step);
4345 *   temperature_matrix.add(time_step, temperature_stiffness_matrix);
4346 *   }
4347 *   else
4348 *   {
4349 *   temperature_matrix.copy_from(temperature_mass_matrix);
4350 *   temperature_matrix.add(time_step, temperature_stiffness_matrix);
4351 *   }
4352 *  
4353 *   if (rebuild_temperature_preconditioner == true)
4354 *   {
4355 *   T_preconditioner =
4356 *   std::make_shared<TrilinosWrappers::PreconditionJacobi>();
4357 *   T_preconditioner->initialize(temperature_matrix);
4358 *   rebuild_temperature_preconditioner = false;
4359 *   }
4360 *  
4361 * @endcode
4362 *
4363 * The next part is computing the right hand side vectors. To do so, we
4364 * first compute the average temperature @f$T_m@f$ that we use for evaluating
4365 * the artificial viscosity stabilization through the residual @f$E(T) =
4366 * (T-T_m)^2@f$. We do this by defining the midpoint between maximum and
4367 * minimum temperature as average temperature in the definition of the
4368 * entropy viscosity. An alternative would be to use the integral average,
4369 * but the results are not very sensitive to this choice. The rest then
4370 * only requires calling WorkStream::run again, binding the arguments to
4371 * the <code>local_assemble_temperature_rhs</code> function that are the
4372 * same in every call to the correct values:
4373 *
4374 * @code
4375 *   temperature_rhs = 0;
4376 *  
4377 *   const QGauss<dim> quadrature_formula(parameters.temperature_degree + 2);
4378 *   const std::pair<double, double> global_T_range =
4379 *   get_extrapolated_temperature_range();
4380 *  
4381 *   const double average_temperature =
4382 *   0.5 * (global_T_range.first + global_T_range.second);
4383 *   const double global_entropy_variation =
4384 *   get_entropy_variation(average_temperature);
4385 *  
4386 *   using CellFilter =
4388 *  
4389 *   auto worker =
4390 *   [this, global_T_range, maximal_velocity, global_entropy_variation](
4391 *   const typename DoFHandler<dim>::active_cell_iterator &cell,
4392 *   Assembly::Scratch::TemperatureRHS<dim> &scratch,
4393 *   Assembly::CopyData::TemperatureRHS<dim> &data) {
4394 *   this->local_assemble_temperature_rhs(global_T_range,
4395 *   maximal_velocity,
4396 *   global_entropy_variation,
4397 *   cell,
4398 *   scratch,
4399 *   data);
4400 *   };
4401 *  
4402 *   auto copier = [this](const Assembly::CopyData::TemperatureRHS<dim> &data) {
4403 *   this->copy_local_to_global_temperature_rhs(data);
4404 *   };
4405 *  
4407 *   temperature_dof_handler.begin_active()),
4408 *   CellFilter(IteratorFilters::LocallyOwnedCell(),
4409 *   temperature_dof_handler.end()),
4410 *   worker,
4411 *   copier,
4412 *   Assembly::Scratch::TemperatureRHS<dim>(
4413 *   temperature_fe, stokes_fe, mapping, quadrature_formula),
4414 *   Assembly::CopyData::TemperatureRHS<dim>(temperature_fe));
4415 *  
4416 *   temperature_rhs.compress(VectorOperation::add);
4417 *   }
4418 *  
4419 *  
4420 *  
4421 * @endcode
4422 *
4423 *
4424 * <a name="step_32-BoussinesqFlowProblemsolve"></a>
4425 * <h4>BoussinesqFlowProblem::solve</h4>
4426 *
4427
4428 *
4429 * This function solves the linear systems in each time step of the
4430 * Boussinesq problem. First, we work on the Stokes system and then on the
4431 * temperature system. In essence, it does the same things as the respective
4432 * function in @ref step_31 "step-31". However, there are a few changes here.
4433 *
4434
4435 *
4436 * The first change is related to the way we store our solution: we keep the
4437 * vectors with locally owned degrees of freedom plus ghost nodes on each
4438 * MPI node. When we enter a solver which is supposed to perform
4439 * matrix-vector products with a distributed matrix, this is not the
4440 * appropriate form, though. There, we will want to have the solution vector
4441 * to be distributed in the same way as the matrix, i.e. without any
4442 * ghosts. So what we do first is to generate a distributed vector called
4443 * <code>distributed_stokes_solution</code> and put only the locally owned
4444 * dofs into that, which is neatly done by the <code>operator=</code> of the
4445 * Trilinos vector.
4446 *
4447
4448 *
4449 * Next, we scale the pressure solution (or rather, the initial guess) for
4450 * the solver so that it matches with the length scales in the matrices, as
4451 * discussed in the introduction. We also immediately scale the pressure
4452 * solution back to the correct units after the solution is completed. We
4453 * also need to set the pressure values at hanging nodes to zero. This we
4454 * also did in @ref step_31 "step-31" in order not to disturb the Schur complement by some
4455 * vector entries that actually are irrelevant during the solve stage. As a
4456 * difference to @ref step_31 "step-31", here we do it only for the locally owned pressure
4457 * dofs. After solving for the Stokes solution, each processor copies the
4458 * distributed solution back into the solution vector that also includes
4459 * ghost elements.
4460 *
4461
4462 *
4463 * The third and most obvious change is that we have two variants for the
4464 * Stokes solver: A fast solver that sometimes breaks down, and a robust
4465 * solver that is slower. This is what we already discussed in the
4466 * introduction. Here is how we realize it: First, we perform 30 iterations
4467 * with the fast solver based on the simple preconditioner based on the AMG
4468 * V-cycle instead of an approximate solve (this is indicated by the
4469 * <code>false</code> argument to the
4470 * <code>LinearSolvers::BlockSchurPreconditioner</code> object). If we
4471 * converge, everything is fine. If we do not converge, the solver control
4472 * object will throw an exception SolverControl::NoConvergence. Usually,
4473 * this would abort the program because we don't catch them in our usual
4474 * <code>solve()</code> functions. This is certainly not what we want to
4475 * happen here. Rather, we want to switch to the strong solver and continue
4476 * the solution process with whatever vector we got so far. Hence, we catch
4477 * the exception with the C++ try/catch mechanism. We then simply go through
4478 * the same solver sequence again in the <code>catch</code> clause, this
4479 * time passing the @p true flag to the preconditioner for the strong
4480 * solver, signaling an approximate CG solve.
4481 *
4482 * @code
4483 *   template <int dim>
4484 *   void BoussinesqFlowProblem<dim>::solve()
4485 *   {
4486 *   {
4487 *   TimerOutput::Scope timer_section(computing_timer,
4488 *   " Solve Stokes system");
4489 *  
4490 *   pcout << " Solving Stokes system... " << std::flush;
4491 *  
4492 *   TrilinosWrappers::MPI::BlockVector distributed_stokes_solution(
4493 *   stokes_rhs);
4494 *   distributed_stokes_solution = stokes_solution;
4495 *  
4496 *   distributed_stokes_solution.block(1) /= EquationData::pressure_scaling;
4497 *  
4498 *   const unsigned int
4499 *   start = (distributed_stokes_solution.block(0).size() +
4500 *   distributed_stokes_solution.block(1).local_range().first),
4501 *   end = (distributed_stokes_solution.block(0).size() +
4502 *   distributed_stokes_solution.block(1).local_range().second);
4503 *   for (unsigned int i = start; i < end; ++i)
4504 *   if (stokes_constraints.is_constrained(i))
4505 *   distributed_stokes_solution(i) = 0;
4506 *  
4507 *  
4508 *   PrimitiveVectorMemory<TrilinosWrappers::MPI::BlockVector> mem;
4509 *  
4510 *   unsigned int n_iterations = 0;
4511 *   const double solver_tolerance = 1e-8 * stokes_rhs.l2_norm();
4512 *   SolverControl solver_control(30, solver_tolerance);
4513 *  
4514 *   try
4515 *   {
4516 *   const LinearSolvers::BlockSchurPreconditioner<
4517 *   TrilinosWrappers::PreconditionAMG,
4518 *   TrilinosWrappers::PreconditionJacobi>
4519 *   preconditioner(stokes_matrix,
4520 *   stokes_preconditioner_matrix,
4521 *   *Mp_preconditioner,
4522 *   *Amg_preconditioner,
4523 *   false);
4524 *  
4525 *   SolverFGMRES<TrilinosWrappers::MPI::BlockVector> solver(
4526 *   solver_control,
4527 *   mem,
4528 *   SolverFGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(
4529 *   30));
4530 *   solver.solve(stokes_matrix,
4531 *   distributed_stokes_solution,
4532 *   stokes_rhs,
4533 *   preconditioner);
4534 *  
4535 *   n_iterations = solver_control.last_step();
4536 *   }
4537 *  
4538 *   catch (SolverControl::NoConvergence &)
4539 *   {
4540 *   const LinearSolvers::BlockSchurPreconditioner<
4541 *   TrilinosWrappers::PreconditionAMG,
4542 *   TrilinosWrappers::PreconditionJacobi>
4543 *   preconditioner(stokes_matrix,
4544 *   stokes_preconditioner_matrix,
4545 *   *Mp_preconditioner,
4546 *   *Amg_preconditioner,
4547 *   true);
4548 *  
4549 *   SolverControl solver_control_refined(stokes_matrix.m(),
4550 *   solver_tolerance);
4551 *   SolverFGMRES<TrilinosWrappers::MPI::BlockVector> solver(
4552 *   solver_control_refined,
4553 *   mem,
4554 *   SolverFGMRES<TrilinosWrappers::MPI::BlockVector>::AdditionalData(
4555 *   50));
4556 *   solver.solve(stokes_matrix,
4557 *   distributed_stokes_solution,
4558 *   stokes_rhs,
4559 *   preconditioner);
4560 *  
4561 *   n_iterations =
4562 *   (solver_control.last_step() + solver_control_refined.last_step());
4563 *   }
4564 *  
4565 *  
4566 *   stokes_constraints.distribute(distributed_stokes_solution);
4567 *  
4568 *   distributed_stokes_solution.block(1) *= EquationData::pressure_scaling;
4569 *  
4570 *   stokes_solution = distributed_stokes_solution;
4571 *   pcout << n_iterations << " iterations." << std::endl;
4572 *   }
4573 *  
4574 *  
4575 * @endcode
4576 *
4577 * Now let's turn to the temperature part: First, we compute the time step
4578 * size. We found that we need smaller time steps for 3d than for 2d for
4579 * the shell geometry. This is because the cells are more distorted in
4580 * that case (it is the smallest edge length that determines the CFL
4581 * number). Instead of computing the time step from maximum velocity and
4582 * minimal mesh size as in @ref step_31 "step-31", we compute local CFL numbers, i.e., on
4583 * each cell we compute the maximum velocity times the mesh size, and
4584 * compute the maximum of them. Hence, we need to choose the factor in
4585 * front of the time step slightly smaller. (We later re-considered this
4586 * approach towards time stepping. If you're curious about this, you may
4587 * want to read the time stepping section in @cite HDGB17 .)
4588 *
4589
4590 *
4591 * After temperature right hand side assembly, we solve the linear
4592 * system for temperature (with fully distributed vectors without
4593 * ghost elements and using the solution from the last timestep as
4594 * our initial guess for the iterative solver), apply constraints,
4595 * and copy the vector back to one with ghosts.
4596 *
4597
4598 *
4599 * In the end, we extract the temperature range similarly to @ref step_31 "step-31" to
4600 * produce some output (for example in order to help us choose the
4601 * stabilization constants, as discussed in the introduction). The only
4602 * difference is that we need to exchange maxima over all processors.
4603 *
4604 * @code
4605 *   {
4606 *   TimerOutput::Scope timer_section(computing_timer,
4607 *   " Assemble temperature rhs");
4608 *  
4609 *   old_time_step = time_step;
4610 *  
4611 *   const double scaling = (dim == 3 ? 0.25 : 1.0);
4612 *   time_step = (scaling / (2.1 * dim * std::sqrt(1. * dim)) /
4613 *   (parameters.temperature_degree * get_cfl_number()));
4614 *  
4615 *   const double maximal_velocity = get_maximal_velocity();
4616 *   pcout << " Maximal velocity: "
4617 *   << maximal_velocity * EquationData::year_in_seconds * 100
4618 *   << " cm/year" << std::endl;
4619 *   pcout << " "
4620 *   << "Time step: " << time_step / EquationData::year_in_seconds
4621 *   << " years" << std::endl;
4622 *  
4623 *   assemble_temperature_system(maximal_velocity);
4624 *   }
4625 *  
4626 *   {
4627 *   TimerOutput::Scope timer_section(computing_timer,
4628 *   " Solve temperature system");
4629 *  
4630 *   SolverControl solver_control(temperature_matrix.m(),
4631 *   1e-12 * temperature_rhs.l2_norm());
4632 *   SolverCG<TrilinosWrappers::MPI::Vector> cg(solver_control);
4633 *  
4634 *   TrilinosWrappers::MPI::Vector distributed_temperature_solution(
4635 *   temperature_rhs);
4636 *   distributed_temperature_solution = old_temperature_solution;
4637 *  
4638 *   cg.solve(temperature_matrix,
4639 *   distributed_temperature_solution,
4640 *   temperature_rhs,
4641 *   *T_preconditioner);
4642 *  
4643 *   temperature_constraints.distribute(distributed_temperature_solution);
4644 *   temperature_solution = distributed_temperature_solution;
4645 *  
4646 *   pcout << " " << solver_control.last_step()
4647 *   << " CG iterations for temperature" << std::endl;
4648 *  
4649 *   double temperature[2] = {std::numeric_limits<double>::max(),
4650 *   -std::numeric_limits<double>::max()};
4651 *   double global_temperature[2];
4652 *  
4653 *   for (unsigned int i =
4654 *   distributed_temperature_solution.local_range().first;
4655 *   i < distributed_temperature_solution.local_range().second;
4656 *   ++i)
4657 *   {
4658 *   temperature[0] =
4659 *   std::min<double>(temperature[0],
4660 *   distributed_temperature_solution(i));
4661 *   temperature[1] =
4662 *   std::max<double>(temperature[1],
4663 *   distributed_temperature_solution(i));
4664 *   }
4665 *  
4666 *   temperature[0] *= -1.0;
4667 *   Utilities::MPI::max(temperature, MPI_COMM_WORLD, global_temperature);
4668 *   global_temperature[0] *= -1.0;
4669 *  
4670 *   pcout << " Temperature range: " << global_temperature[0] << ' '
4671 *   << global_temperature[1] << std::endl;
4672 *   }
4673 *   }
4674 *  
4675 *  
4676 * @endcode
4677 *
4678 *
4679 * <a name="step_32-BoussinesqFlowProblemoutput_results"></a>
4680 * <h4>BoussinesqFlowProblem::output_results</h4>
4681 *
4682
4683 *
4684 * Next comes the function that generates the output. The quantities to
4685 * output could be introduced manually like we did in @ref step_31 "step-31". An
4686 * alternative is to hand this task over to a class PostProcessor that
4687 * inherits from the class DataPostprocessor, which can be attached to
4688 * DataOut. This allows us to output derived quantities from the solution,
4689 * like the friction heating included in this example. It overloads the
4690 * virtual function DataPostprocessor::evaluate_vector_field(),
4691 * which is then internally called from DataOut::build_patches(). We have to
4692 * give it values of the numerical solution, its derivatives, normals to the
4693 * cell, the actual evaluation points and any additional quantities. This
4694 * follows the same procedure as discussed in @ref step_29 "step-29" and other programs.
4695 *
4696 * @code
4697 *   template <int dim>
4698 *   class BoussinesqFlowProblem<dim>::Postprocessor
4699 *   : public DataPostprocessor<dim>
4700 *   {
4701 *   public:
4702 *   Postprocessor(const unsigned int partition, const double minimal_pressure);
4703 *  
4704 *   virtual void evaluate_vector_field(
4705 *   const DataPostprocessorInputs::Vector<dim> &inputs,
4706 *   std::vector<Vector<double>> &computed_quantities) const override;
4707 *  
4708 *   virtual std::vector<std::string> get_names() const override;
4709 *  
4710 *   virtual std::vector<
4711 *   DataComponentInterpretation::DataComponentInterpretation>
4712 *   get_data_component_interpretation() const override;
4713 *  
4714 *   virtual UpdateFlags get_needed_update_flags() const override;
4715 *  
4716 *   private:
4717 *   const unsigned int partition;
4718 *   const double minimal_pressure;
4719 *   };
4720 *  
4721 *  
4722 *   template <int dim>
4723 *   BoussinesqFlowProblem<dim>::Postprocessor::Postprocessor(
4724 *   const unsigned int partition,
4725 *   const double minimal_pressure)
4726 *   : partition(partition)
4727 *   , minimal_pressure(minimal_pressure)
4728 *   {}
4729 *  
4730 *  
4731 * @endcode
4732 *
4733 * Here we define the names for the variables we want to output. These are
4734 * the actual solution values for velocity, pressure, and temperature, as
4735 * well as the friction heating and to each cell the number of the processor
4736 * that owns it. This allows us to visualize the partitioning of the domain
4737 * among the processors. Except for the velocity, which is vector-valued,
4738 * all other quantities are scalar.
4739 *
4740 * @code
4741 *   template <int dim>
4742 *   std::vector<std::string>
4743 *   BoussinesqFlowProblem<dim>::Postprocessor::get_names() const
4744 *   {
4745 *   std::vector<std::string> solution_names(dim, "velocity");
4746 *   solution_names.emplace_back("p");
4747 *   solution_names.emplace_back("T");
4748 *   solution_names.emplace_back("friction_heating");
4749 *   solution_names.emplace_back("partition");
4750 *  
4751 *   return solution_names;
4752 *   }
4753 *  
4754 *  
4755 *   template <int dim>
4756 *   std::vector<DataComponentInterpretation::DataComponentInterpretation>
4757 *   BoussinesqFlowProblem<dim>::Postprocessor::get_data_component_interpretation()
4758 *   const
4759 *   {
4760 *   std::vector<DataComponentInterpretation::DataComponentInterpretation>
4761 *   interpretation(dim,
4762 *   DataComponentInterpretation::component_is_part_of_vector);
4763 *  
4764 *   interpretation.push_back(DataComponentInterpretation::component_is_scalar);
4765 *   interpretation.push_back(DataComponentInterpretation::component_is_scalar);
4766 *   interpretation.push_back(DataComponentInterpretation::component_is_scalar);
4767 *   interpretation.push_back(DataComponentInterpretation::component_is_scalar);
4768 *  
4769 *   return interpretation;
4770 *   }
4771 *  
4772 *  
4773 *   template <int dim>
4774 *   UpdateFlags
4775 *   BoussinesqFlowProblem<dim>::Postprocessor::get_needed_update_flags() const
4776 *   {
4777 *   return update_values | update_gradients | update_quadrature_points;
4778 *   }
4779 *  
4780 *  
4781 * @endcode
4782 *
4783 * Now we implement the function that computes the derived quantities. As we
4784 * also did for the output, we rescale the velocity from its SI units to
4785 * something more readable, namely cm/year. Next, the pressure is scaled to
4786 * be between 0 and the maximum pressure. This makes it more easily
4787 * comparable -- in essence making all pressure variables positive or
4788 * zero. Temperature is taken as is, and the friction heating is computed as
4789 * @f$2 \eta \varepsilon(\mathbf{u}) \cdot \varepsilon(\mathbf{u})@f$.
4790 *
4791
4792 *
4793 * The quantities we output here are more for illustration, rather than for
4794 * actual scientific value. We come back to this briefly in the results
4795 * section of this program and explain what one may in fact be interested in.
4796 *
4797 * @code
4798 *   template <int dim>
4799 *   void BoussinesqFlowProblem<dim>::Postprocessor::evaluate_vector_field(
4800 *   const DataPostprocessorInputs::Vector<dim> &inputs,
4801 *   std::vector<Vector<double>> &computed_quantities) const
4802 *   {
4803 *   const unsigned int n_evaluation_points = inputs.solution_values.size();
4804 *   Assert(inputs.solution_gradients.size() == n_evaluation_points,
4805 *   ExcInternalError());
4806 *   Assert(computed_quantities.size() == n_evaluation_points,
4807 *   ExcInternalError());
4808 *   Assert(inputs.solution_values[0].size() == dim + 2, ExcInternalError());
4809 *  
4810 *   for (unsigned int p = 0; p < n_evaluation_points; ++p)
4811 *   {
4812 *   for (unsigned int d = 0; d < dim; ++d)
4813 *   computed_quantities[p](d) = (inputs.solution_values[p](d) *
4814 *   EquationData::year_in_seconds * 100);
4815 *  
4816 *   const double pressure =
4817 *   (inputs.solution_values[p](dim) - minimal_pressure);
4818 *   computed_quantities[p](dim) = pressure;
4819 *  
4820 *   const double temperature = inputs.solution_values[p](dim + 1);
4821 *   computed_quantities[p](dim + 1) = temperature;
4822 *  
4823 *   Tensor<2, dim> grad_u;
4824 *   for (unsigned int d = 0; d < dim; ++d)
4825 *   grad_u[d] = inputs.solution_gradients[p][d];
4826 *   const SymmetricTensor<2, dim> strain_rate = symmetrize(grad_u);
4827 *   computed_quantities[p](dim + 2) =
4828 *   2 * EquationData::eta * strain_rate * strain_rate;
4829 *  
4830 *   computed_quantities[p](dim + 3) = partition;
4831 *   }
4832 *   }
4833 *  
4834 *  
4835 * @endcode
4836 *
4837 * The <code>output_results()</code> function has a similar task to the one
4838 * in @ref step_31 "step-31". However, here we are going to demonstrate a different
4839 * technique on how to merge output from different DoFHandler objects. The
4840 * way we're going to achieve this recombination is to create a joint
4841 * DoFHandler that collects both components, the Stokes solution and the
4842 * temperature solution. This can be nicely done by combining the finite
4843 * elements from the two systems to form one FESystem, and let this
4844 * collective system define a new DoFHandler object. To be sure that
4845 * everything was done correctly, we perform a sanity check that ensures
4846 * that we got all the dofs from both Stokes and temperature even in the
4847 * combined system. We then combine the data vectors. Unfortunately, there
4848 * is no straight-forward relation that tells us how to sort Stokes and
4849 * temperature vector into the joint vector. The way we can get around this
4850 * trouble is to rely on the information collected in the FESystem. For each
4851 * dof on a cell, the joint finite element knows to which equation component
4852 * (velocity component, pressure, or temperature) it belongs – that's the
4853 * information we need! So we step through all cells (with iterators into
4854 * all three DoFHandlers moving in sync), and for each joint cell dof, we
4855 * read out that component using the FiniteElement::system_to_base_index
4856 * function (see there for a description of what the various parts of its
4857 * return value contain). We also need to keep track whether we're on a
4858 * Stokes dof or a temperature dof, which is contained in
4859 * joint_fe.system_to_base_index(i).first.first. Eventually, the dof_indices
4860 * data structures on either of the three systems tell us how the relation
4861 * between global vector and local dofs looks like on the present cell,
4862 * which concludes this tedious work. We make sure that each processor only
4863 * works on the subdomain it owns locally (and not on ghost or artificial
4864 * cells) when building the joint solution vector. The same will then have
4865 * to be done in DataOut::build_patches(), but that function does so
4866 * automatically.
4867 *
4868
4869 *
4870 * What we end up with is a set of patches that we can write using the
4871 * functions in DataOutBase in a variety of output formats. Here, we then
4872 * have to pay attention that what each processor writes is really only its
4873 * own part of the domain, i.e. we will want to write each processor's
4874 * contribution into a separate file. This we do by adding an additional
4875 * number to the filename when we write the solution. This is not really
4876 * new, we did it similarly in @ref step_40 "step-40". Note that we write in the compressed
4877 * format @p .vtu instead of plain vtk files, which saves quite some
4878 * storage.
4879 *
4880
4881 *
4882 * All the rest of the work is done in the PostProcessor class.
4883 *
4884 * @code
4885 *   template <int dim>
4886 *   void BoussinesqFlowProblem<dim>::output_results()
4887 *   {
4888 *   TimerOutput::Scope timer_section(computing_timer, "Postprocessing");
4889 *  
4890 *   const FESystem<dim> joint_fe(stokes_fe, 1, temperature_fe, 1);
4891 *  
4892 *   DoFHandler<dim> joint_dof_handler(triangulation);
4893 *   joint_dof_handler.distribute_dofs(joint_fe);
4894 *   Assert(joint_dof_handler.n_dofs() ==
4895 *   stokes_dof_handler.n_dofs() + temperature_dof_handler.n_dofs(),
4896 *   ExcInternalError());
4897 *  
4898 *   TrilinosWrappers::MPI::Vector joint_solution;
4899 *   joint_solution.reinit(joint_dof_handler.locally_owned_dofs(),
4900 *   MPI_COMM_WORLD);
4901 *  
4902 *   {
4903 *   std::vector<types::global_dof_index> local_joint_dof_indices(
4904 *   joint_fe.n_dofs_per_cell());
4905 *   std::vector<types::global_dof_index> local_stokes_dof_indices(
4906 *   stokes_fe.n_dofs_per_cell());
4907 *   std::vector<types::global_dof_index> local_temperature_dof_indices(
4908 *   temperature_fe.n_dofs_per_cell());
4909 *  
4910 *   typename DoFHandler<dim>::active_cell_iterator
4911 *   joint_cell = joint_dof_handler.begin_active(),
4912 *   joint_endc = joint_dof_handler.end(),
4913 *   stokes_cell = stokes_dof_handler.begin_active(),
4914 *   temperature_cell = temperature_dof_handler.begin_active();
4915 *   for (; joint_cell != joint_endc;
4916 *   ++joint_cell, ++stokes_cell, ++temperature_cell)
4917 *   if (joint_cell->is_locally_owned())
4918 *   {
4919 *   joint_cell->get_dof_indices(local_joint_dof_indices);
4920 *   stokes_cell->get_dof_indices(local_stokes_dof_indices);
4921 *   temperature_cell->get_dof_indices(local_temperature_dof_indices);
4922 *  
4923 *   for (unsigned int i = 0; i < joint_fe.n_dofs_per_cell(); ++i)
4924 *   if (joint_fe.system_to_base_index(i).first.first == 0)
4925 *   {
4926 *   Assert(joint_fe.system_to_base_index(i).second <
4927 *   local_stokes_dof_indices.size(),
4928 *   ExcInternalError());
4929 *  
4930 *   joint_solution(local_joint_dof_indices[i]) = stokes_solution(
4931 *   local_stokes_dof_indices[joint_fe.system_to_base_index(i)
4932 *   .second]);
4933 *   }
4934 *   else
4935 *   {
4936 *   Assert(joint_fe.system_to_base_index(i).first.first == 1,
4937 *   ExcInternalError());
4938 *   Assert(joint_fe.system_to_base_index(i).second <
4939 *   local_temperature_dof_indices.size(),
4940 *   ExcInternalError());
4941 *   joint_solution(local_joint_dof_indices[i]) =
4942 *   temperature_solution(
4943 *   local_temperature_dof_indices
4944 *   [joint_fe.system_to_base_index(i).second]);
4945 *   }
4946 *   }
4947 *   }
4948 *  
4949 *   joint_solution.compress(VectorOperation::insert);
4950 *  
4951 *   const IndexSet locally_relevant_joint_dofs =
4952 *   DoFTools::extract_locally_relevant_dofs(joint_dof_handler);
4953 *   TrilinosWrappers::MPI::Vector locally_relevant_joint_solution;
4954 *   locally_relevant_joint_solution.reinit(locally_relevant_joint_dofs,
4955 *   MPI_COMM_WORLD);
4956 *   locally_relevant_joint_solution = joint_solution;
4957 *  
4958 *   Postprocessor postprocessor(Utilities::MPI::this_mpi_process(
4959 *   MPI_COMM_WORLD),
4960 *   stokes_solution.block(1).min());
4961 *  
4962 *   DataOut<dim> data_out;
4963 *   data_out.attach_dof_handler(joint_dof_handler);
4964 *   data_out.add_data_vector(locally_relevant_joint_solution, postprocessor);
4965 *   data_out.build_patches();
4966 *  
4967 *   static int out_index = 0;
4968 *   data_out.write_vtu_with_pvtu_record(
4969 *   "./", "solution", out_index, MPI_COMM_WORLD, 5);
4970 *  
4971 *   ++out_index;
4972 *   }
4973 *  
4974 *  
4975 *  
4976 * @endcode
4977 *
4978 *
4979 * <a name="step_32-BoussinesqFlowProblemrefine_mesh"></a>
4980 * <h4>BoussinesqFlowProblem::refine_mesh</h4>
4981 *
4982
4983 *
4984 * This function isn't really new either. Since the <code>setup_dofs</code>
4985 * function that we call in the middle has its own timer section, we split
4986 * timing this function into two sections. It will also allow us to easily
4987 * identify which of the two is more expensive.
4988 *
4989
4990 *
4991 * One thing of note, however, is that we only want to compute error
4992 * indicators on the locally owned subdomain. In order to achieve this, we
4993 * pass one additional argument to the KellyErrorEstimator::estimate
4994 * function. Note that the vector for error estimates is resized to the
4995 * number of active cells present on the current process, which is less than
4996 * the total number of active cells on all processors (but more than the
4997 * number of locally owned active cells); each processor only has a few
4998 * coarse cells around the locally owned ones, as also explained in @ref step_40 "step-40".
4999 *
5000
5001 *
5002 * The local error estimates are then handed to a %parallel version of
5004 * also @ref step_40 "step-40") which looks at the errors and finds the cells that need
5005 * refinement by comparing the error values across processors. As in
5006 * @ref step_31 "step-31", we want to limit the maximum grid level. So in case some cells
5007 * have been marked that are already at the finest level, we simply clear
5008 * the refine flags.
5009 *
5010 * @code
5011 *   template <int dim>
5012 *   void
5013 *   BoussinesqFlowProblem<dim>::refine_mesh(const unsigned int max_grid_level)
5014 *   {
5016 *   temperature_trans(temperature_dof_handler);
5019 *   stokes_trans(stokes_dof_handler);
5020 *  
5021 *   {
5022 *   TimerOutput::Scope timer_section(computing_timer,
5023 *   "Refine mesh structure, part 1");
5024 *  
5025 *   Vector<float> estimated_error_per_cell(triangulation.n_active_cells());
5026 *  
5028 *   temperature_dof_handler,
5029 *   QGauss<dim - 1>(parameters.temperature_degree + 1),
5030 *   std::map<types::boundary_id, const Function<dim> *>(),
5031 *   temperature_solution,
5032 *   estimated_error_per_cell,
5033 *   ComponentMask(),
5034 *   nullptr,
5035 *   0,
5037 *  
5039 *   triangulation, estimated_error_per_cell, 0.3, 0.1);
5040 *  
5041 *   if (triangulation.n_levels() > max_grid_level)
5042 *   for (typename Triangulation<dim>::active_cell_iterator cell =
5043 *   triangulation.begin_active(max_grid_level);
5044 *   cell != triangulation.end();
5045 *   ++cell)
5046 *   cell->clear_refine_flag();
5047 *  
5048 * @endcode
5049 *
5050 * With all flags marked as necessary, we can then tell the
5051 * parallel::distributed::SolutionTransfer objects to get ready to
5052 * transfer data from one mesh to the next, which they will do when
5053 * notified by
5054 * Triangulation as part of the @p execute_coarsening_and_refinement() call.
5055 * The syntax is similar to the non-%parallel solution transfer (with the
5056 * exception that here a pointer to the vector entries is enough). The
5057 * remainder of the function further down below is then concerned with
5058 * setting up the data structures again after mesh refinement and
5059 * restoring the solution vectors on the new mesh.
5060 *
5061 * @code
5062 *   const std::vector<const TrilinosWrappers::MPI::Vector *> x_temperature = {
5063 *   &temperature_solution, &old_temperature_solution};
5064 *   const std::vector<const TrilinosWrappers::MPI::BlockVector *> x_stokes = {
5065 *   &stokes_solution, &old_stokes_solution};
5066 *  
5068 *  
5069 *   temperature_trans.prepare_for_coarsening_and_refinement(x_temperature);
5070 *   stokes_trans.prepare_for_coarsening_and_refinement(x_stokes);
5071 *  
5073 *   }
5074 *  
5075 *   setup_dofs();
5076 *  
5077 *   {
5078 *   TimerOutput::Scope timer_section(computing_timer,
5079 *   "Refine mesh structure, part 2");
5080 *  
5081 *   {
5082 *   TrilinosWrappers::MPI::Vector distributed_temp1(temperature_rhs);
5083 *   TrilinosWrappers::MPI::Vector distributed_temp2(temperature_rhs);
5084 *  
5085 *   std::vector<TrilinosWrappers::MPI::Vector *> tmp = {&distributed_temp1,
5086 *   &distributed_temp2};
5087 *   temperature_trans.interpolate(tmp);
5088 *  
5089 * @endcode
5090 *
5091 * enforce constraints to make the interpolated solution conforming on
5092 * the new mesh:
5093 *
5094 * @code
5095 *   temperature_constraints.distribute(distributed_temp1);
5096 *   temperature_constraints.distribute(distributed_temp2);
5097 *  
5098 *   temperature_solution = distributed_temp1;
5099 *   old_temperature_solution = distributed_temp2;
5100 *   }
5101 *  
5102 *   {
5103 *   TrilinosWrappers::MPI::BlockVector distributed_stokes(stokes_rhs);
5104 *   TrilinosWrappers::MPI::BlockVector old_distributed_stokes(stokes_rhs);
5105 *  
5106 *   std::vector<TrilinosWrappers::MPI::BlockVector *> stokes_tmp = {
5107 *   &distributed_stokes, &old_distributed_stokes};
5108 *  
5109 *   stokes_trans.interpolate(stokes_tmp);
5110 *  
5111 * @endcode
5112 *
5113 * enforce constraints to make the interpolated solution conforming on
5114 * the new mesh:
5115 *
5116 * @code
5117 *   stokes_constraints.distribute(distributed_stokes);
5118 *   stokes_constraints.distribute(old_distributed_stokes);
5119 *  
5120 *   stokes_solution = distributed_stokes;
5121 *   old_stokes_solution = old_distributed_stokes;
5122 *   }
5123 *   }
5124 *   }
5125 *  
5126 *  
5127 *  
5128 * @endcode
5129 *
5130 *
5131 * <a name="step_32-BoussinesqFlowProblemrun"></a>
5132 * <h4>BoussinesqFlowProblem::run</h4>
5133 *
5134
5135 *
5136 * This is the final and controlling function in this class. It, in fact,
5137 * runs the entire rest of the program and is, once more, very similar to
5138 * @ref step_31 "step-31". The only substantial difference is that we use a different mesh
5139 * now (a GridGenerator::hyper_shell instead of a simple cube geometry).
5140 *
5141 * @code
5142 *   template <int dim>
5143 *   void BoussinesqFlowProblem<dim>::run()
5144 *   {
5146 *   Point<dim>(),
5147 *   EquationData::R0,
5148 *   EquationData::R1,
5149 *   (dim == 3) ? 96 : 12,
5150 *   true);
5151 *  
5152 *   global_Omega_diameter = GridTools::diameter(triangulation);
5153 *  
5154 *   triangulation.refine_global(parameters.initial_global_refinement);
5155 *  
5156 *   setup_dofs();
5157 *  
5158 *   unsigned int pre_refinement_step = 0;
5159 *  
5160 *   start_time_iteration:
5161 *  
5162 *   {
5163 *   TrilinosWrappers::MPI::Vector solution(
5164 *   temperature_dof_handler.locally_owned_dofs());
5165 * @endcode
5166 *
5167 * VectorTools::project supports parallel vector classes with most
5168 * standard finite elements via deal.II's own native MatrixFree framework:
5169 * since we use standard Lagrange elements of moderate order this function
5170 * works well here.
5171 *
5172 * @code
5173 *   VectorTools::project(temperature_dof_handler,
5174 *   temperature_constraints,
5175 *   QGauss<dim>(parameters.temperature_degree + 2),
5176 *   EquationData::TemperatureInitialValues<dim>(),
5177 *   solution);
5178 * @endcode
5179 *
5180 * Having so computed the current temperature field, let us set the member
5181 * variable that holds the temperature nodes. Strictly speaking, we really
5182 * only need to set <code>old_temperature_solution</code> since the first
5183 * thing we will do is to compute the Stokes solution that only requires
5184 * the previous time step's temperature field. That said, nothing good can
5185 * come from not initializing the other vectors as well (especially since
5186 * it's a relatively cheap operation and we only have to do it once at the
5187 * beginning of the program) if we ever want to extend our numerical
5188 * method or physical model, and so we initialize
5189 * <code>old_temperature_solution</code> and
5190 * <code>old_old_temperature_solution</code> as well. The assignment makes
5191 * sure that the vectors on the left hand side (which where initialized to
5192 * contain ghost elements as well) also get the correct ghost elements. In
5193 * other words, the assignment here requires communication between
5194 * processors:
5195 *
5196 * @code
5197 *   temperature_solution = solution;
5198 *   old_temperature_solution = solution;
5199 *   old_old_temperature_solution = solution;
5200 *   }
5201 *  
5202 *   timestep_number = 0;
5203 *   time_step = old_time_step = 0;
5204 *  
5205 *   double time = 0;
5206 *  
5207 *   do
5208 *   {
5209 *   pcout << "Timestep " << timestep_number
5210 *   << ": t=" << time / EquationData::year_in_seconds << " years"
5211 *   << std::endl;
5212 *  
5213 *   assemble_stokes_system();
5214 *   build_stokes_preconditioner();
5215 *   assemble_temperature_matrix();
5216 *  
5217 *   solve();
5218 *  
5219 *   pcout << std::endl;
5220 *  
5221 *   if ((timestep_number == 0) &&
5222 *   (pre_refinement_step < parameters.initial_adaptive_refinement))
5223 *   {
5224 *   refine_mesh(parameters.initial_global_refinement +
5225 *   parameters.initial_adaptive_refinement);
5226 *   ++pre_refinement_step;
5227 *   goto start_time_iteration;
5228 *   }
5229 *   else if ((timestep_number > 0) &&
5230 *   (timestep_number % parameters.adaptive_refinement_interval ==
5231 *   0))
5232 *   refine_mesh(parameters.initial_global_refinement +
5233 *   parameters.initial_adaptive_refinement);
5234 *  
5235 *   if ((parameters.generate_graphical_output == true) &&
5236 *   (timestep_number % parameters.graphical_output_interval == 0))
5237 *   output_results();
5238 *  
5239 * @endcode
5240 *
5241 * In order to speed up linear solvers, we extrapolate the solutions
5242 * from the old time levels to the new one. This gives a very good
5243 * initial guess, cutting the number of iterations needed in solvers
5244 * by more than one half. We do not need to extrapolate in the last
5245 * iteration, so if we reached the final time, we stop here.
5246 *
5247
5248 *
5249 * As the last thing during a time step (before actually bumping up
5250 * the number of the time step), we check whether the current time
5251 * step number is divisible by 100, and if so we let the computing
5252 * timer print a summary of CPU times spent so far.
5253 *
5254 * @code
5255 *   if (time > parameters.end_time * EquationData::year_in_seconds)
5256 *   break;
5257 *  
5258 *   TrilinosWrappers::MPI::BlockVector old_old_stokes_solution;
5259 *   old_old_stokes_solution = old_stokes_solution;
5260 *   old_stokes_solution = stokes_solution;
5261 *   old_old_temperature_solution = old_temperature_solution;
5262 *   old_temperature_solution = temperature_solution;
5263 *   if (old_time_step > 0)
5264 *   {
5265 * @endcode
5266 *
5267 * Trilinos sadd does not like ghost vectors even as input. Copy
5268 * into distributed vectors for now:
5269 *
5270 * @code
5271 *   {
5272 *   TrilinosWrappers::MPI::BlockVector distr_solution(stokes_rhs);
5273 *   distr_solution = stokes_solution;
5274 *   TrilinosWrappers::MPI::BlockVector distr_old_solution(stokes_rhs);
5275 *   distr_old_solution = old_old_stokes_solution;
5276 *   distr_solution.sadd(1. + time_step / old_time_step,
5277 *   -time_step / old_time_step,
5278 *   distr_old_solution);
5279 *   stokes_solution = distr_solution;
5280 *   }
5281 *   {
5282 *   TrilinosWrappers::MPI::Vector distr_solution(temperature_rhs);
5283 *   distr_solution = temperature_solution;
5284 *   TrilinosWrappers::MPI::Vector distr_old_solution(temperature_rhs);
5285 *   distr_old_solution = old_old_temperature_solution;
5286 *   distr_solution.sadd(1. + time_step / old_time_step,
5287 *   -time_step / old_time_step,
5288 *   distr_old_solution);
5289 *   temperature_solution = distr_solution;
5290 *   }
5291 *   }
5292 *  
5293 *   if ((timestep_number > 0) && (timestep_number % 100 == 0))
5294 *   computing_timer.print_summary();
5295 *  
5296 *   time += time_step;
5297 *   ++timestep_number;
5298 *   }
5299 *   while (true);
5300 *  
5301 * @endcode
5302 *
5303 * If we are generating graphical output, do so also for the last time
5304 * step unless we had just done so before we left the do-while loop
5305 *
5306 * @code
5307 *   if ((parameters.generate_graphical_output == true) &&
5308 *   !((timestep_number - 1) % parameters.graphical_output_interval == 0))
5309 *   output_results();
5310 *   }
5311 *   } // namespace Step32
5312 *  
5313 *  
5314 *  
5315 * @endcode
5316 *
5317 *
5318 * <a name="step_32-Thecodemaincodefunction"></a>
5319 * <h3>The <code>main</code> function</h3>
5320 *
5321
5322 *
5323 * The main function is short as usual and very similar to the one in
5324 * @ref step_31 "step-31". Since we use a parameter file which is specified as an argument in
5325 * the command line, we have to read it in here and pass it on to the
5326 * Parameters class for parsing. If no filename is given in the command line,
5327 * we simply use the <code>step-32.prm</code> file which is distributed
5328 * together with the program.
5329 *
5330
5331 *
5332 * Because 3d computations are simply very slow unless you throw a lot of
5333 * processors at them, the program defaults to 2d. You can get the 3d version
5334 * by changing the constant dimension below to 3.
5335 *
5336 * @code
5337 *   int main(int argc, char *argv[])
5338 *   {
5339 *   try
5340 *   {
5341 *   using namespace Step32;
5342 *   using namespace dealii;
5343 *  
5344 *   Utilities::MPI::MPI_InitFinalize mpi_initialization(
5345 *   argc, argv, numbers::invalid_unsigned_int);
5346 *  
5347 *   std::string parameter_filename;
5348 *   if (argc >= 2)
5349 *   parameter_filename = argv[1];
5350 *   else
5351 *   parameter_filename = "step-32.prm";
5352 *  
5353 *   const int dim = 2;
5354 *   BoussinesqFlowProblem<dim>::Parameters parameters(parameter_filename);
5355 *   BoussinesqFlowProblem<dim> flow_problem(parameters);
5356 *   flow_problem.run();
5357 *   }
5358 *   catch (std::exception &exc)
5359 *   {
5360 *   std::cerr << std::endl
5361 *   << std::endl
5362 *   << "----------------------------------------------------"
5363 *   << std::endl;
5364 *   std::cerr << "Exception on processing: " << std::endl
5365 *   << exc.what() << std::endl
5366 *   << "Aborting!" << std::endl
5367 *   << "----------------------------------------------------"
5368 *   << std::endl;
5369 *  
5370 *   return 1;
5371 *   }
5372 *   catch (...)
5373 *   {
5374 *   std::cerr << std::endl
5375 *   << std::endl
5376 *   << "----------------------------------------------------"
5377 *   << std::endl;
5378 *   std::cerr << "Unknown exception!" << std::endl
5379 *   << "Aborting!" << std::endl
5380 *   << "----------------------------------------------------"
5381 *   << std::endl;
5382 *   return 1;
5383 *   }
5384 *  
5385 *   return 0;
5386 *   }
5387 * @endcode
5388<a name="step_32-Results"></a><h1>Results</h1>
5389
5390
5391When run, the program simulates convection in 3d in much the same way
5392as @ref step_31 "step-31" did, though with an entirely different testcase.
5393
5394
5395<a name="step_32-Comparisonofresultswithstep31"></a><h3>Comparison of results with step-31</h3>
5396
5397
5398Before we go to this testcase, however, let us show a few results from a
5399slightly earlier version of this program that was solving exactly the
5400testcase we used in @ref step_31 "step-31", just that we now solve it in parallel and with
5401much higher resolution. We show these results mainly for comparison.
5402
5403Here are two images that show this higher resolution if we choose a 3d
5404computation in <code>main()</code> and if we set
5405<code>initial_refinement=3</code> and
5406<code>n_pre_refinement_steps=4</code>. At the time steps shown, the
5407meshes had around 72,000 and 236,000 cells, for a total of 2,680,000
5408and 8,250,000 degrees of freedom, respectively, more than an order of
5409magnitude more than we had available in @ref step_31 "step-31":
5410
5411<table align="center" class="doxtable">
5412 <tr>
5413 <td>
5414 <img src="https://www.dealii.org/images/steps/developer/step-32.3d.cube.0.png" alt="">
5415 </td>
5416 </tr>
5417 <tr>
5418 <td>
5419 <img src="https://www.dealii.org/images/steps/developer/step-32.3d.cube.1.png" alt="">
5420 </td>
5421 </tr>
5422</table>
5423
5424The computation was done on a subset of 50 processors of the Brazos
5425cluster at Texas A&amp;M University.
5426
5427
5428<a name="step_32-Resultsfora2dcircularshelltestcase"></a><h3>Results for a 2d circular shell testcase</h3>
5429
5430
5431Next, we will run @ref step_32 "step-32" with the parameter file in the directory with one
5432change: we increase the final time to 1e9. Here we are using 16 processors. The
5433command to launch is (note that @ref step_32 "step-32".prm is the default):
5434
5435<code>
5436<pre>
5437\$ mpirun -np 16 ./step-32
5438</pre>
5439</code>
5440
5441Note that running a job on a cluster typically requires going through a job
5442scheduler, which we won't discuss here. The output will look roughly like
5443this:
5444
5445<code>
5446<pre>
5447\$ mpirun -np 16 ./step-32
5448Number of active cells: 12,288 (on 6 levels)
5449Number of degrees of freedom: 186,624 (99,840+36,864+49,920)
5450
5451Timestep 0: t=0 years
5452
5453 Rebuilding Stokes preconditioner...
5454 Solving Stokes system... 41 iterations.
5455 Maximal velocity: 60.4935 cm/year
5456 Time step: 18166.9 years
5457 17 CG iterations for temperature
5458 Temperature range: 973 4273.16
5459
5460Number of active cells: 15,921 (on 7 levels)
5461Number of degrees of freedom: 252,723 (136,640+47,763+68,320)
5462
5463Timestep 0: t=0 years
5464
5465 Rebuilding Stokes preconditioner...
5466 Solving Stokes system... 50 iterations.
5467 Maximal velocity: 60.3223 cm/year
5468 Time step: 10557.6 years
5469 19 CG iterations for temperature
5470 Temperature range: 973 4273.16
5471
5472Number of active cells: 19,926 (on 8 levels)
5473Number of degrees of freedom: 321,246 (174,312+59,778+87,156)
5474
5475Timestep 0: t=0 years
5476
5477 Rebuilding Stokes preconditioner...
5478 Solving Stokes system... 50 iterations.
5479 Maximal velocity: 57.8396 cm/year
5480 Time step: 5453.78 years
5481 18 CG iterations for temperature
5482 Temperature range: 973 4273.16
5483
5484Timestep 1: t=5453.78 years
5485
5486 Solving Stokes system... 49 iterations.
5487 Maximal velocity: 59.0231 cm/year
5488 Time step: 5345.86 years
5489 18 CG iterations for temperature
5490 Temperature range: 973 4273.16
5491
5492Timestep 2: t=10799.6 years
5493
5494 Solving Stokes system... 24 iterations.
5495 Maximal velocity: 60.2139 cm/year
5496 Time step: 5241.51 years
5497 17 CG iterations for temperature
5498 Temperature range: 973 4273.16
5499
5500[...]
5501
5502Timestep 100: t=272151 years
5503
5504 Solving Stokes system... 21 iterations.
5505 Maximal velocity: 161.546 cm/year
5506 Time step: 1672.96 years
5507 17 CG iterations for temperature
5508 Temperature range: 973 4282.57
5509
5510Number of active cells: 56,085 (on 8 levels)
5511Number of degrees of freedom: 903,408 (490,102+168,255+245,051)
5512
5513
5514
5515+---------------------------------------------+------------+------------+
5516| Total wallclock time elapsed since start | 115s | |
5517| | | |
5518| Section | no. calls | wall time | % of total |
5519+---------------------------------+-----------+------------+------------+
5520| Assemble Stokes system | 103 | 2.82s | 2.5% |
5521| Assemble temperature matrices | 12 | 0.452s | 0.39% |
5522| Assemble temperature rhs | 103 | 11.5s | 10% |
5523| Build Stokes preconditioner | 12 | 2.09s | 1.8% |
5524| Solve Stokes system | 103 | 90.4s | 79% |
5525| Solve temperature system | 103 | 1.53s | 1.3% |
5526| Postprocessing | 3 | 0.532s | 0.46% |
5527| Refine mesh structure, part 1 | 12 | 0.93s | 0.81% |
5528| Refine mesh structure, part 2 | 12 | 0.384s | 0.33% |
5529| Setup dof systems | 13 | 2.96s | 2.6% |
5530+---------------------------------+-----------+------------+------------+
5531
5532[...]
5533
5534+---------------------------------------------+------------+------------+
5535| Total wallclock time elapsed since start | 9.14e+04s | |
5536| | | |
5537| Section | no. calls | wall time | % of total |
5538+---------------------------------+-----------+------------+------------+
5539| Assemble Stokes system | 47045 | 2.05e+03s | 2.2% |
5540| Assemble temperature matrices | 4707 | 310s | 0.34% |
5541| Assemble temperature rhs | 47045 | 8.7e+03s | 9.5% |
5542| Build Stokes preconditioner | 4707 | 1.48e+03s | 1.6% |
5543| Solve Stokes system | 47045 | 7.34e+04s | 80% |
5544| Solve temperature system | 47045 | 1.46e+03s | 1.6% |
5545| Postprocessing | 1883 | 222s | 0.24% |
5546| Refine mesh structure, part 1 | 4706 | 641s | 0.7% |
5547| Refine mesh structure, part 2 | 4706 | 259s | 0.28% |
5548| Setup dof systems | 4707 | 1.86e+03s | 2% |
5549+---------------------------------+-----------+------------+------------+
5550</pre>
5551</code>
5552
5553The simulation terminates when the time reaches the 1 billion years
5554selected in the input file. You can extrapolate from this how long a
5555simulation would take for a different final time (the time step size
5556ultimately settles on somewhere around 20,000 years, so computing for
5557two billion years will take 100,000 time steps, give or take 20%). As
5558can be seen here, we spend most of the compute time in assembling
5559linear systems and &mdash; above all &mdash; in solving Stokes
5560systems.
5561
5562
5563To demonstrate the output we show the output from every 1250th time step here:
5564<table>
5565 <tr>
5566 <td>
5567 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-000.png" alt="">
5568 </td>
5569 <td>
5570 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-050.png" alt="">
5571 </td>
5572 <td>
5573 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-100.png" alt="">
5574 </td>
5575 </tr>
5576 <tr>
5577 <td>
5578 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-150.png" alt="">
5579 </td>
5580 <td>
5581 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-200.png" alt="">
5582 </td>
5583 <td>
5584 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-250.png" alt="">
5585 </td>
5586 </tr>
5587 <tr>
5588 <td>
5589 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-300.png" alt="">
5590 </td>
5591 <td>
5592 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-350.png" alt="">
5593 </td>
5594 <td>
5595 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-400.png" alt="">
5596 </td>
5597 </tr>
5598 <tr>
5599 <td>
5600 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-450.png" alt="">
5601 </td>
5602 <td>
5603 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-500.png" alt="">
5604 </td>
5605 <td>
5606 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-550.png" alt="">
5607 </td>
5608 </tr>
5609 <tr>
5610 <td>
5611 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-time-600.png" alt="">
5612 </td>
5613 <td>
5614 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-cells.png" alt="">
5615 </td>
5616 <td>
5617 <img src="https://www.dealii.org/images/steps/developer/step-32-2d-partition.png" alt="">
5618 </td>
5619 </tr>
5620</table>
5621
5622The last two images show the grid as well as the partitioning of the mesh for
5623the same computation with 16 subdomains and 16 processors. The full dynamics of
5624this simulation are really only visible by looking at an animation, for example
5625the one <a
5626href="https://www.dealii.org/images/steps/developer/step-32-2d-temperature.webm">shown
5627on this site</a>. This image is well worth watching due to its artistic quality
5628and entrancing depiction of the evolution of the magma plumes.
5629
5630If you watch the movie, you'll see that the convection pattern goes
5631through several stages: First, it gets rid of the instable temperature
5632layering with the hot material overlain by the dense cold
5633material. After this great driver is removed and we have a sort of
5634stable situation, a few blobs start to separate from the hot boundary
5635layer at the inner ring and rise up, with a few cold fingers also
5636dropping down from the outer boundary layer. During this phase, the solution
5637remains mostly symmetric, reflecting the 12-fold symmetry of the
5638original mesh. In a final phase, the fluid enters vigorous chaotic
5639stirring in which all symmetries are lost. This is a pattern that then
5640continues to dominate flow.
5641
5642These different phases can also be identified if we look at the
5643maximal velocity as a function of time in the simulation:
5644
5645<img src="https://www.dealii.org/images/steps/developer/step-32.2d.t_vs_vmax.png" alt="">
5646
5647Here, the velocity (shown in centimeters per year) becomes very large,
5648to the order of several meters per year) at the beginning when the
5649temperature layering is instable. It then calms down to relatively
5650small values before picking up again in the chaotic stirring
5651regime. There, it remains in the range of 10-40 centimeters per year,
5652quite within the physically expected region.
5653
5654
5655<a name="step_32-Resultsfora3dsphericalshelltestcase"></a><h3>Results for a 3d spherical shell testcase</h3>
5656
5657
56583d computations are very expensive computationally. Furthermore, as
5659seen above, interesting behavior only starts after quite a long time
5660requiring more CPU hours than is available on a typical
5661cluster. Consequently, rather than showing a complete simulation here,
5662let us simply show a couple of pictures we have obtained using the
5663successor to this program, called <i>ASPECT</i> (short for <i>Advanced
5664%Solver for Problems in Earth's ConvecTion</i>), that is being
5665developed independently of deal.II and that already incorporates some
5666of the extensions discussed below. The following two pictures show
5667isocontours of the temperature and the partition of the domain (along
5668with the mesh) onto 512 processors:
5669
5670<p align="center">
5671<img src="https://www.dealii.org/images/steps/developer/step-32.3d-sphere.solution.png" alt="">
5672
5673<img src="https://www.dealii.org/images/steps/developer/step-32.3d-sphere.partition.png" alt="">
5674</p>
5675
5676
5677<a name="step-32-extensions"></a>
5678<a name="step_32-Possibilitiesforextensions"></a><h3>Possibilities for extensions</h3>
5679
5680
5681There are many directions in which this program could be extended. As
5682mentioned at the end of the introduction, most of these are under active
5683development in the <i>ASPECT</i> (short for <i>Advanced %Solver for Problems
5684in Earth's ConvecTion</i>) code at the time this tutorial program is being
5685finished. Specifically, the following are certainly topics that one should
5686address to make the program more useful:
5687
5688<ul>
5689 <li> <b>Adiabatic heating/cooling:</b>
5690 The temperature field we get in our simulations after a while
5691 is mostly constant with boundary layers at the inner and outer
5692 boundary, and streamers of cold and hot material mixing
5693 everything. Yet, this doesn't match our expectation that things
5694 closer to the earth core should be hotter than closer to the
5695 surface. The reason is that the energy equation we have used does
5696 not include a term that describes adiabatic cooling and heating:
5697 rock, like gas, heats up as you compress it. Consequently, material
5698 that rises up cools adiabatically, and cold material that sinks down
5699 heats adiabatically. The correct temperature equation would
5700 therefore look somewhat like this:
5701 @f{eqnarray*}{
5702 \frac{D T}{Dt}
5703 -
5704 \nabla \cdot \kappa \nabla T &=& \gamma + \tau\frac{Dp}{Dt},
5705 @f}
5706 or, expanding the advected derivative @f$\frac{D}{Dt} =
5707 \frac{\partial}{\partial t} + \mathbf u \cdot \nabla@f$:
5708 @f{eqnarray*}{
5709 \frac{\partial T}{\partial t}
5710 +
5711 {\mathbf u} \cdot \nabla T
5712 -
5713 \nabla \cdot \kappa \nabla T &=& \gamma +
5714 \tau\left\{\frac{\partial
5715 p}{\partial t} + \mathbf u \cdot \nabla p \right\}.
5716 @f}
5717 In other words, as pressure increases in a rock volume
5718 (@f$\frac{Dp}{Dt}>0@f$) we get an additional heat source, and vice
5719 versa.
5720
5721 The time derivative of the pressure is a bit awkward to
5722 implement. If necessary, one could approximate using the fact
5723 outlined in the introduction that the pressure can be decomposed
5724 into a dynamic component due to temperature differences and the
5725 resulting flow, and a static component that results solely from the
5726 static pressure of the overlying rock. Since the latter is much
5727 bigger, one may approximate @f$p\approx p_{\text{static}}=-\rho_{\text{ref}}
5728 [1+\beta T_{\text{ref}}] \varphi@f$, and consequently
5729 @f$\frac{Dp}{Dt} \approx \left\{- \mathbf u \cdot \nabla \rho_{\text{ref}}
5730 [1+\beta T_{\text{ref}}]\varphi\right\} = \rho_{\text{ref}}
5731 [1+\beta T_{\text{ref}}] \mathbf u \cdot \mathbf g@f$.
5732 In other words, if the fluid is moving in the direction of gravity
5733 (downward) it will be compressed and because in that case @f$\mathbf u
5734 \cdot \mathbf g > 0@f$ we get a positive heat source. Conversely, the
5735 fluid will cool down if it moves against the direction of gravity.
5736
5737<li> <b>Compressibility:</b>
5738 As already hinted at in the temperature model above,
5739 mantle rocks are not incompressible. Rather, given the enormous pressures in
5740 the earth mantle (at the core-mantle boundary, the pressure is approximately
5741 140 GPa, equivalent to 1,400,000 times atmospheric pressure), rock actually
5742 does compress to something around 1.5 times the density it would have
5743 at surface pressure. Modeling this presents any number of
5744 difficulties. Primarily, the mass conservation equation is no longer
5745 @f$\textrm{div}\;\mathbf u=0@f$ but should read
5746 @f$\textrm{div}(\rho\mathbf u)=0@f$ where the density @f$\rho@f$ is now no longer
5747 spatially constant but depends on temperature and pressure. A consequence is
5748 that the model is now no longer linear; a linearized version of the Stokes
5749 equation is also no longer symmetric requiring us to rethink preconditioners
5750 and, possibly, even the discretization. We won't go into detail here as to
5751 how this can be resolved.
5752
5753<li> <b>Nonlinear material models:</b> As already hinted at in various places,
5754 material parameters such as the density, the viscosity, and the various
5755 thermal parameters are not constant throughout the earth mantle. Rather,
5756 they nonlinearly depend on the pressure and temperature, and in the case of
5757 the viscosity on the strain rate @f$\varepsilon(\mathbf u)@f$. For complicated
5758 models, the only way to solve such models accurately may be to actually
5759 iterate this dependence out in each time step, rather than simply freezing
5760 coefficients at values extrapolated from the previous time step(s).
5761
5762<li> <b>Checkpoint/restart:</b> Running this program in 2d on a number of
5763 processors allows solving realistic models in a day or two. However, in 3d,
5764 compute times are so large that one runs into two typical problems: (i) On
5765 most compute clusters, the queuing system limits run times for individual
5766 jobs are to 2 or 3 days; (ii) losing the results of a computation due to
5767 hardware failures, misconfigurations, or power outages is a shame when
5768 running on hundreds of processors for a couple of days. Both of these
5769 problems can be addressed by periodically saving the state of the program
5770 and, if necessary, restarting the program at this point. This technique is
5771 commonly called <i>checkpoint/restart</i> and it requires that the entire
5772 state of the program is written to a permanent storage location (e.g. a hard
5773 drive). Given the complexity of the data structures of this program, this is
5774 not entirely trivial (it may also involve writing gigabytes or more of
5775 data), but it can be made easier by realizing that one can save the state
5776 between two time steps where it essentially only consists of the mesh and
5777 solution vectors; during restart one would then first re-enumerate degrees
5778 of freedom in the same way as done before and then re-assemble
5779 matrices. Nevertheless, given the distributed nature of the data structures
5780 involved here, saving and restoring the state of a program is not
5781 trivial. An additional complexity is introduced by the fact that one may
5782 want to change the number of processors between runs, for example because
5783 one may wish to continue computing on a mesh that is finer than the one used
5784 to precompute a starting temperature field at an intermediate time.
5785
5786<li> <b>Predictive postprocessing:</b> The point of computations like this is
5787 not simply to solve the equations. Rather, it is typically the exploration
5788 of different physical models and their comparison with things that we can
5789 measure at the earth surface, in order to find which models are realistic
5790 and which are contradicted by reality. To this end, we need to compute
5791 quantities from our solution vectors that are related to what we can
5792 observe. Among these are, for example, heatfluxes at the surface of the
5793 earth, as well as seismic velocities throughout the mantle as these affect
5794 earthquake waves that are recorded by seismographs.
5795
5796<li> <b>Better refinement criteria:</b> As can be seen above for the
57973d case, the mesh in 3d is primarily refined along the inner
5798boundary. This is because the boundary layer there is stronger than
5799any other transition in the domain, leading us to refine there almost
5800exclusively and basically not at all following the plumes. One
5801certainly needs better refinement criteria to track the parts of the
5802solution we are really interested in better than the criterion used
5803here, namely the KellyErrorEstimator applied to the temperature, is
5804able to.
5805</ul>
5806
5807
5808There are many other ways to extend the current program. However, rather than
5809discussing them here, let us point to the much larger open
5810source code ASPECT (see https://aspect.geodynamics.org/ ) that constitutes the
5811further development of @ref step_32 "step-32" and that already includes many such possible
5812extensions.
5813 *
5814 *
5815<a name="step_32-PlainProg"></a>
5816<h1> The plain program</h1>
5817@include "step-32.cc"
5818*/
virtual void build_patches(const unsigned int n_subdivisions=0)
Definition data_out.cc:1062
void reinit(const Triangulation< dim, spacedim > &tria)
active_cell_iterator begin_active(const unsigned int level=0) const
Definition fe_q.h:554
std::pair< std::pair< unsigned int, unsigned int >, unsigned int > system_to_base_index(const unsigned int index) const
virtual RangeNumberType value(const Point< dim > &p, const unsigned int component=0) const
virtual void vector_value(const Point< dim > &p, Vector< RangeNumberType > &values) const
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, Number > * > &neumann_bc, const ReadVector< Number > &solution, Vector< float > &error, const ComponentMask &component_mask={}, 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)
double get_double(const std::string &entry_name) const
Definition point.h:111
numbers::NumberTraits< Number >::real_type norm() const
unsigned int n_active_cells() const
void refine_global(const unsigned int times=1)
unsigned int n_levels() const
cell_iterator end() const
active_cell_iterator begin_active(const unsigned int level=0) const
types::subdomain_id locally_owned_subdomain() const override
Definition tria_base.cc:345
virtual void execute_coarsening_and_refinement() override
Definition tria.cc:3320
virtual bool prepare_coarsening_and_refinement() override
Definition tria.cc:2805
Point< 3 > vertices[4]
Point< 2 > second
Definition grid_out.cc:4624
Point< 2 > first
Definition grid_out.cc:4623
unsigned int level
Definition grid_out.cc:4626
__global__ void set(Number *val, const Number s, const size_type N)
#define Assert(cond, exc)
#define AssertThrow(cond, exc)
typename ActiveSelector::active_cell_iterator active_cell_iterator
void loop(IteratorType begin, std_cxx20::type_identity_t< IteratorType > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(std_cxx20::type_identity_t< DOFINFO > &, std_cxx20::type_identity_t< DOFINFO > &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, AssemblerType &assembler, const LoopControl &lctrl=LoopControl())
Definition loop.h:564
UpdateFlags
@ update_values
Shape function values.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
std::vector< value_type > split(const typename ::Triangulation< dim, spacedim >::cell_iterator &parent, const value_type parent_value)
const Event initial
Definition event.cc:64
const Event remesh
Definition event.cc:65
void approximate(const SynchronousIterators< std::tuple< typename DoFHandler< dim, spacedim >::active_cell_iterator, Vector< float >::iterator > > &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 extract_constant_modes(const DoFHandler< dim, spacedim > &dof_handler, const ComponentMask &component_mask, std::vector< std::vector< bool > > &constant_modes)
void extrapolate(const DoFHandler< dim, spacedim > &dof1, const InVector &z1, const DoFHandler< dim, spacedim > &dof2, OutVector &z2)
void hyper_shell(Triangulation< dim, spacedim > &tria, const Point< spacedim > &center, const double inner_radius, const double outer_radius, const unsigned int n_cells=0, 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 scale(const double scaling_factor, Triangulation< dim, spacedim > &triangulation)
double volume(const Triangulation< dim, spacedim > &tria)
double diameter(const Triangulation< dim, spacedim > &tria)
@ valid
Iterator points to a valid object.
@ matrix
Contents is actually a matrix.
@ symmetric
Matrix is symmetric.
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition divergence.h:471
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition utilities.cc:191
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
VectorType::value_type * end(VectorType &V)
std::vector< unsigned int > serial(const std::vector< unsigned int > &targets, const std::function< RequestType(const unsigned int)> &create_request, const std::function< AnswerType(const unsigned int, const RequestType &)> &answer_request, const std::function< void(const unsigned int, const AnswerType &)> &process_answer, const MPI_Comm comm)
T sum(const T &t, const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:107
std::string compress(const std::string &input)
Definition utilities.cc:389
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 >()), const bool project_to_boundary_first=false)
void run(const Iterator &begin, const std_cxx20::type_identity_t< Iterator > &end, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length, const unsigned int chunk_size)
void run(const std::vector< std::vector< Iterator > > &colored_iterators, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
void abort(const ExceptionBase &exc) noexcept
bool check(const ConstraintKinds kind_in, const unsigned int dim)
long double gamma(const unsigned int n)
int(& functions)(const void *v1, const void *v2)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
static constexpr double PI
Definition numbers.h:259
void refine_and_coarsen_fixed_fraction(::Triangulation< dim, spacedim > &tria, const ::Vector< Number > &criteria, const double top_fraction_of_error, const double bottom_fraction_of_error, const VectorTools::NormType norm_type=VectorTools::L1_norm)
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 > cos(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sin(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
Definition types.h:32
unsigned int subdomain_id
Definition types.h:43
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation