1085 *
sparsity_pattern.copy_from(
dsp);
1087 *
system_matrix.
reinit(sparsity_pattern);
1089 *
solution.
reinit(dof_handler.n_dofs());
1118 *
dof_handler.
end(),
1149 *
QGauss<dim>(fe.degree + 1),
1152 *
, fe_face_values(fe,
1153 *
QGauss<dim - 1>(fe.degree + 1),
1167 *
: fe_values(scratch_data.fe_values.get_fe(),
1168 *
scratch_data.fe_values.get_quadrature(),
1171 *
, fe_face_values(scratch_data.fe_face_values.get_fe(),
1172 *
scratch_data.fe_face_values.get_quadrature(),
1235 *
const unsigned
int dofs_per_cell = fe.n_dofs_per_cell();
1236 *
const unsigned int n_q_points =
1237 *
scratch_data.fe_values.get_quadrature().
size();
1239 *
scratch_data.fe_face_values.get_quadrature().
size();
1246 *
copy_data.cell_matrix.
reinit(dofs_per_cell, dofs_per_cell);
1247 *
copy_data.cell_rhs.
reinit(dofs_per_cell);
1255 *
copy_data.local_dof_indices.resize(dofs_per_cell);
1262 *
scratch_data.fe_values.
reinit(cell);
1267 * at
the quadrature points...
1270 *
scratch_data.advection_field.value_list(
1271 *
scratch_data.fe_values.get_quadrature_points(),
1272 *
scratch_data.advection_directions);
1273 *
scratch_data.right_hand_side.value_list(
1274 *
scratch_data.fe_values.get_quadrature_points(), scratch_data.rhs_values);
1282 *
const double delta = 0.1 * cell->diameter();
1291 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1299 *
const auto &
sd = scratch_data;
1300 *
for (
unsigned int j = 0;
j < dofs_per_cell; ++
j)
1301 *
copy_data.cell_matrix(i,
j) +=
1302 *
((
sd.fe_values.shape_value(i,
q_point) +
1303 *
delta * (
sd.advection_directions[
q_point] *
1304 *
sd.fe_values.shape_grad(i,
q_point))) *
1309 *
copy_data.cell_rhs(i) +=
1310 *
(
sd.fe_values.shape_value(i,
q_point) +
1311 *
delta * (
sd.advection_directions[
q_point] *
1312 *
sd.fe_values.shape_grad(i,
q_point))) *
1333 *
for (
const auto &face : cell->face_iterators())
1334 *
if (face->at_boundary())
1344 *
scratch_data.fe_face_values.
reinit(cell, face);
1352 *
scratch_data.boundary_values.value_list(
1353 *
scratch_data.fe_face_values.get_quadrature_points(),
1354 *
scratch_data.face_boundary_values);
1355 *
scratch_data.advection_field.value_list(
1356 *
scratch_data.fe_face_values.get_quadrature_points(),
1357 *
scratch_data.face_advection_directions);
1372 *
if (scratch_data.fe_face_values.normal_vector(
q_point) *
1373 *
scratch_data.face_advection_directions[
q_point] <
1384 *
for (unsigned
int i = 0; i < dofs_per_cell; ++i)
1386 *
for (
unsigned int j = 0;
j < dofs_per_cell; ++
j)
1387 *
copy_data.cell_matrix(i,
j) -=
1388 *
(scratch_data.face_advection_directions[
q_point] *
1389 *
scratch_data.fe_face_values.normal_vector(
q_point) *
1390 *
scratch_data.fe_face_values.shape_value(i,
q_point) *
1391 *
scratch_data.fe_face_values.shape_value(
j,
q_point) *
1392 *
scratch_data.fe_face_values.JxW(
q_point));
1394 *
copy_data.cell_rhs(i) -=
1395 *
(scratch_data.face_advection_directions[
q_point] *
1396 *
scratch_data.fe_face_values.normal_vector(
q_point) *
1397 *
scratch_data.face_boundary_values[
q_point] *
1398 *
scratch_data.fe_face_values.shape_value(i,
q_point) *
1399 *
scratch_data.fe_face_values.JxW(
q_point));
1410 *
cell->get_dof_indices(copy_data.local_dof_indices);
1425 *
template <
int dim>
1430 *
copy_data.cell_matrix,
1431 *
copy_data.cell_rhs,
1432 *
copy_data.local_dof_indices,
1444 *
requires a
decent preconditioner:
we use a Jacobi preconditioner
here,
1448 *
template <
int dim>
1456 *
preconditioner.initialize(system_matrix, 1.0);
1457 *
solver.solve(system_matrix, solution,
system_rhs, preconditioner);
1461 *
system_matrix.vmult(residual, solution);
1463 *
std::cout <<
" Iterations required for convergence: "
1464 *
<< solver_control.last_step() <<
'\n'
1465 *
<<
" Max norm of residual: "
1466 *
<< residual.linfty_norm() <<
'\n';
1478 *
template <
int dim>
1483 *
GradientEstimation::estimate(dof_handler,
1504 * file for each cell, and in 3d we save 512. The end result is that the
1505 * visualization program will use a piecewise linear interpolation of the
1506 * cubic basis functions: this captures the solution detail and, with most
1507 * screen resolutions, looks smooth. We save the grid in a separate step
1508 * with no extra patches so that we have a visual representation of the cell
1513 * Version 9.1 of deal.II gained the ability to write higher degree
1514 * polynomials (i.e., write piecewise bicubic visualization data for our
1515 * piecewise bicubic solution) VTK and VTU output: however, not all recent
1516 * versions of ParaView and VisIt (as of 2018) can read this format, so we
1517 * use the older, more general (but less efficient) approach here.
1520 * template <int dim>
1521 * void AdvectionProblem<dim>::output_results(const unsigned int cycle) const
1525 * std::ofstream output("grid-" + std::to_string(cycle) + ".vtu");
1526 * grid_out.write_vtu(triangulation, output);
1530 * DataOut<dim> data_out;
1531 * data_out.attach_dof_handler(dof_handler);
1532 * data_out.add_data_vector(solution, "solution");
1533 * data_out.build_patches(8);
1537 * VTU output can be expensive, both to compute and to write to
1538 * disk. Here we ask ZLib, a compression library, to compress the data
1539 * in a way that maximizes throughput.
1542 * DataOutBase::VtkFlags vtk_flags;
1543 * vtk_flags.compression_level = DataOutBase::CompressionLevel::best_speed;
1544 * data_out.set_flags(vtk_flags);
1546 * std::ofstream output("solution-" + std::to_string(cycle) + ".vtu");
1547 * data_out.write_vtu(output);
1554 * ... as is the main loop (setup -- solve -- refine), aside from the number
1555 * of cycles and the initial grid:
1558 * template <int dim>
1559 * void AdvectionProblem<dim>::run()
1561 * for (unsigned int cycle = 0; cycle < 10; ++cycle)
1563 * std::cout << "Cycle " << cycle << ':
' << std::endl;
1567 * GridGenerator::hyper_cube(triangulation, -1, 1);
1568 * triangulation.refine_global(3);
1576 * std::cout << " Number of active cells: "
1577 * << triangulation.n_active_cells() << std::endl;
1581 * std::cout << " Number of degrees of freedom: "
1582 * << dof_handler.n_dofs() << std::endl;
1584 * assemble_system();
1586 * output_results(cycle);
1595 * <a name="GradientEstimationclassimplementation"></a>
1596 * <h3>GradientEstimation class implementation</h3>
1600 * Now for the implementation of the <code>GradientEstimation</code> class.
1601 * Let us start by defining constructors for the
1602 * <code>EstimateScratchData</code> class used by the
1603 * <code>estimate_cell()</code> function:
1606 * template <int dim>
1607 * GradientEstimation::EstimateScratchData<dim>::EstimateScratchData(
1608 * const FiniteElement<dim> &fe,
1609 * const Vector<double> & solution,
1610 * Vector<float> & error_per_cell)
1611 * : fe_midpoint_value(fe,
1613 * update_values | update_quadrature_points)
1614 * , solution(solution)
1615 * , error_per_cell(error_per_cell)
1616 * , cell_midpoint_value(1)
1617 * , neighbor_midpoint_value(1)
1621 * We allocate a vector to hold iterators to all active neighbors of
1622 * a cell. We reserve the maximal number of active neighbors in order to
1623 * avoid later reallocations. Note how this maximal number of active
1624 * neighbors is computed here.
1627 * active_neighbors.reserve(GeometryInfo<dim>::faces_per_cell *
1628 * GeometryInfo<dim>::max_children_per_face);
1632 * template <int dim>
1633 * GradientEstimation::EstimateScratchData<dim>::EstimateScratchData(
1634 * const EstimateScratchData &scratch_data)
1635 * : fe_midpoint_value(scratch_data.fe_midpoint_value.get_fe(),
1636 * scratch_data.fe_midpoint_value.get_quadrature(),
1637 * update_values | update_quadrature_points)
1638 * , solution(scratch_data.solution)
1639 * , error_per_cell(scratch_data.error_per_cell)
1640 * , cell_midpoint_value(1)
1641 * , neighbor_midpoint_value(1)
1647 * Next comes the implementation of the <code>GradientEstimation</code>
1648 * class. The first function does not much except for delegating work to the
1649 * other function, but there is a bit of setup at the top.
1653 * Before starting with the work, we check that the vector into
1654 * which the results are written has the right size. Programming
1655 * mistakes in which one forgets to size arguments correctly at the
1656 * calling site are quite common. Because the resulting damage from
1657 * not catching such errors is often subtle (e.g., corruption of
1658 * data somewhere in memory, or non-reproducible results), it is
1659 * well worth the effort to check for such things.
1662 * template <int dim>
1663 * void GradientEstimation::estimate(const DoFHandler<dim> &dof_handler,
1664 * const Vector<double> & solution,
1665 * Vector<float> & error_per_cell)
1668 * error_per_cell.size() == dof_handler.get_triangulation().n_active_cells(),
1669 * ExcInvalidVectorLength(error_per_cell.size(),
1670 * dof_handler.get_triangulation().n_active_cells()));
1672 * WorkStream::run(dof_handler.begin_active(),
1673 * dof_handler.end(),
1674 * &GradientEstimation::template estimate_cell<dim>,
1675 * std::function<void(const EstimateCopyData &)>(),
1676 * EstimateScratchData<dim>(dof_handler.get_fe(),
1679 * EstimateCopyData());
1685 * Here comes the function that estimates the local error by computing the
1686 * finite difference approximation of the gradient. The function first
1687 * computes the list of active neighbors of the present cell and then
1688 * computes the quantities described in the introduction for each of
1689 * the neighbors. The reason for this order is that it is not a one-liner
1690 * to find a given neighbor with locally refined meshes. In principle, an
1691 * optimized implementation would find neighbors and the quantities
1692 * depending on them in one step, rather than first building a list of
1693 * neighbors and in a second step their contributions but we will gladly
1694 * leave this as an exercise. As discussed before, the worker function
1695 * passed to WorkStream::run works on "scratch" objects that keep all
1696 * temporary objects. This way, we do not need to create and initialize
1697 * objects that are expensive to initialize within the function that does
1698 * the work every time it is called for a given cell. Such an argument is
1699 * passed as the second argument. The third argument would be a "copy-data"
1700 * object (see @ref threads for more information) but we do not actually use
1701 * any of these here. Since WorkStream::run() insists on passing three
1702 * arguments, we declare this function with three arguments, but simply
1703 * ignore the last one.
1707 * (This is unsatisfactory from an aesthetic perspective. It can be avoided
1708 * by using an anonymous (lambda) function. If you allow, let us here show
1709 * how. First, assume that we had declared this function to only take two
1710 * arguments by omitting the unused last one. Now, WorkStream::run still
1711 * wants to call this function with three arguments, so we need to find a
1712 * way to "forget" the third argument in the call. Simply passing
1713 * WorkStream::run the pointer to the function as we do above will not do
1714 * this -- the compiler will complain that a function declared to have two
1715 * arguments is called with three arguments. However, we can do this by
1716 * passing the following as the third argument to WorkStream::run():
1717 * <div class=CodeFragmentInTutorialComment>
1719 * [](const typename DoFHandler<dim>::active_cell_iterator &cell,
1720 * EstimateScratchData<dim> & scratch_data,
1721 * EstimateCopyData &)
1723 * GradientEstimation::estimate_cell<dim>(cell, scratch_data);
1727 * This is not much better than the solution implemented below: either the
1728 * routine itself must take three arguments or it must be wrapped by
1737 *
template <
int dim>
1757 *
scratch_data.fe_midpoint_value.
reinit(cell);
1789 *
scratch_data.active_neighbors.clear();
1790 *
for (
const auto face_n : cell->face_indices())
1800 *
const auto neighbor = cell->neighbor(
face_n);
1809 *
if (neighbor->is_active())
1810 *
scratch_data.active_neighbors.push_back(neighbor);
1874 * `
behind' the subfaces of the current face and move on:
1877 * for (unsigned int subface_n = 0; subface_n < face->n_children();
1879 * scratch_data.active_neighbors.push_back(
1880 * cell->neighbor_child_on_subface(face_n, subface_n));
1886 * OK, now that we have all the neighbors, lets start the computation
1887 * on each of them. First we do some preliminaries: find out about the
1888 * center of the present cell and the solution at this point. The
1889 * latter is obtained as a vector of function values at the quadrature
1890 * points, of which there are only one, of course. Likewise, the
1891 * position of the center is the position of the first (and only)
1892 * quadrature point in real space.
1895 * const Point<dim> this_center =
1896 * scratch_data.fe_midpoint_value.quadrature_point(0);
1898 * scratch_data.fe_midpoint_value.get_function_values(
1899 * scratch_data.solution, scratch_data.cell_midpoint_value);
1903 * Now loop over all active neighbors and collect the data we
1907 * Tensor<1, dim> projected_gradient;
1908 * for (const auto &neighbor : scratch_data.active_neighbors)
1912 * Then get the center of the neighbor cell and the value of the
1913 * finite element function at that point. Note that for this
1914 * information we have to reinitialize the <code>FEValues</code>
1915 * object for the neighbor cell.
1918 * scratch_data.fe_midpoint_value.reinit(neighbor);
1919 * const Point<dim> neighbor_center =
1920 * scratch_data.fe_midpoint_value.quadrature_point(0);
1922 * scratch_data.fe_midpoint_value.get_function_values(
1923 * scratch_data.solution, scratch_data.neighbor_midpoint_value);
1927 * Compute the vector <code>y</code> connecting the centers of the
1928 * two cells. Note that as opposed to the introduction, we denote
1929 * by <code>y</code> the normalized difference vector, as this is
1930 * the quantity used everywhere in the computations.
1933 * Tensor<1, dim> y = neighbor_center - this_center;
1934 * const double distance = y.norm();
1939 * Then add up the contribution of this cell to the Y matrix...
1942 * for (unsigned int i = 0; i < dim; ++i)
1943 * for (unsigned int j = 0; j < dim; ++j)
1944 * Y[i][j] += y[i] * y[j];
1948 * ... and update the sum of difference quotients:
1951 * projected_gradient += (scratch_data.neighbor_midpoint_value[0] -
1952 * scratch_data.cell_midpoint_value[0]) /
1958 * If now, after collecting all the information from the neighbors, we
1959 * can determine an approximation of the gradient for the present
1960 * cell, then we need to have passed over vectors <code>y</code> which
1961 * span the whole space, otherwise we would not have all components of
1962 * the gradient. This is indicated by the invertibility of the matrix.
1966 * If the matrix is not invertible, then the present
1967 * cell had an insufficient number of active neighbors. In contrast to
1968 * all previous cases (where we raised exceptions) this is, however,
1969 * not a programming error: it is a runtime error that can happen in
1970 * optimized mode even if it ran well in debug mode, so it is
1971 * reasonable to try to catch this error also in optimized mode. For
1972 * this case, there is the <code>AssertThrow</code> macro: it checks
1973 * the condition like the <code>Assert</code> macro, but not only in
1974 * debug mode; it then outputs an error message, but instead of
1975 * aborting the program as in the case of the <code>Assert</code>
1976 * macro, the exception is thrown using the <code>throw</code> command
1977 * of C++. This way, one has the possibility to catch this error and
1978 * take reasonable counter actions. One such measure would be to
1979 * refine the grid globally, as the case of insufficient directions
1980 * can not occur if every cell of the initial grid has been refined at
1984 * AssertThrow(determinant(Y) != 0, ExcInsufficientDirections());
1988 * If, on the other hand, the matrix is invertible, then invert it,
1989 * multiply the other quantity with it, and compute the estimated error
1990 * using this quantity and the correct powers of the mesh width:
1993 * const Tensor<2, dim> Y_inverse = invert(Y);
1995 * const Tensor<1, dim> gradient = Y_inverse * projected_gradient;
1999 * The last part of this function is the one where we write into
2000 * the element of the output vector what we have just
2001 * computed. The address of this vector has been stored in the
2002 * scratch data object, and all we have to do is know how to get
2003 * at the correct element inside this vector -- but we can ask the
2007 *
scratch_data.error_per_cell(cell->active_cell_index()) =
2016 * <a name=
"Mainfunction"></a>
2024 *
"Parallel computing with multiple processors accessing shared memory"
2036 *
using namespace dealii;
2044 *
catch (std::exception &exc)
2046 *
std::cerr << std::endl
2048 *
<<
"----------------------------------------------------"
2050 *
std::cerr <<
"Exception on processing: " << std::endl
2051 *
<< exc.what() << std::endl
2052 *
<<
"Aborting!" << std::endl
2053 *
<<
"----------------------------------------------------"
2059 *
std::cerr << std::endl
2061 *
<<
"----------------------------------------------------"
2063 *
std::cerr <<
"Unknown exception!" << std::endl
2064 *
<<
"Aborting!" << std::endl
2065 *
<<
"----------------------------------------------------"
2135<
div class=
"twocolumn" style=
"width: 80%">
2137 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-grid-3.png"
2138 alt=
"Fourth grid in the refinement cycle, showing some adaptivity to features."
2139 width=
"400" height=
"400">
2142 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-grid-9.png"
2143 alt=
"Tenth grid in the refinement cycle, showing that the waves are fully captured."
2144 width=
"400" height=
"400">
2148<
div class=
"twocolumn" style=
"width: 80%">
2150 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-solution-3.png"
2151 alt=
"Fourth solution, showing that we resolve most features but some
2152 are sill unresolved and appear blury."
2153 width=
"400" height=
"400">
2156 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-solution-9.png"
2157 alt=
"Tenth solution, showing a fully resolved flow."
2158 width=
"400" height=
"400">
2162<
div class=
"twocolumn" style=
"width: 80%">
2164 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-solution-3-zoom.png"
2165 alt=
"Detail of the fourth solution, showing that we resolve most
2166 features but some are sill unresolved and appear blury. In particular,
2167 the larger cells need to be refined."
2168 width=
"400" height=
"400">
2171 <
img src=
"https://www.dealii.org/images/steps/developer/step-9-solution-9-zoom.png"
2172 alt=
"Detail of the tenth solution, showing that we needed a lot more
2173 cells than were present in the fourth solution."
2174 width=
"400" height=
"400">
2187<a name=
"PlainProg"></a>
value_type * data() const noexcept
void reinit(value_type *starting_element, const std::size_t n_elements)
void reinit(const TriaIterator< DoFCellAccessor< dim, spacedim, level_dof_access > > &cell)
static void set_thread_limit(const unsigned int max_threads=numbers::invalid_unsigned_int)
__global__ void set(Number *val, const Number s, const size_type N)
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
typename ActiveSelector::active_cell_iterator active_cell_iterator
void loop(ITERATOR begin, std_cxx20::type_identity_t< ITERATOR > end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
@ update_values
Shape function values.
@ update_normal_vectors
Normal vectors.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
void refine_and_coarsen_fixed_number(Triangulation< dim, spacedim > &triangulation, const Vector< Number > &criteria, const double top_fraction_of_cells, const double bottom_fraction_of_cells, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
@ matrix
Contents is actually a matrix.
@ symmetric
Matrix is symmetric.
@ general
No special properties.
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
T sum(const T &t, const MPI_Comm mpi_communicator)
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)
bool check(const ConstraintKinds kind_in, const unsigned int dim)
void copy(const T *begin, const T *end, U *dest)
int(&) functions(const void *v1, const void *v2)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
DEAL_II_HOST constexpr Number determinant(const SymmetricTensor< 2, dim, Number > &)