Loading [MathJax]/extensions/TeX/newcommand.js
 Reference documentation for deal.II version 9.3.3
\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}} \newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=} \newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]} \newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
step-9.h
Go to the documentation of this file.
1false);
1079 * sparsity_pattern.copy_from(dsp);
1080 *
1081 * system_matrix.reinit(sparsity_pattern);
1082 *
1083 * solution.reinit(dof_handler.n_dofs());
1084 * system_rhs.reinit(dof_handler.n_dofs());
1085 * }
1086 *
1087 *
1088 *
1089 * @endcode
1090 *
1091 * In the following function, the matrix and right hand side are
1092 * assembled. As stated in the documentation of the main class above, it
1093 * does not do this itself, but rather delegates to the function following
1094 * next, utilizing the WorkStream concept discussed in @ref threads .
1095 *
1096
1097 *
1098 * If you have looked through the @ref threads module, you will have
1099 * seen that assembling in parallel does not take an incredible
1100 * amount of extra code as long as you diligently describe what the
1101 * scratch and copy data objects are, and if you define suitable
1102 * functions for the local assembly and the copy operation from local
1103 * contributions to global objects. This done, the following will do
1104 * all the heavy lifting to get these operations done on multiple
1105 * threads on as many cores as you have in your system:
1106 *
1107 * @code
1108 * template <int dim>
1109 * void AdvectionProblem<dim>::assemble_system()
1110 * {
1111 * WorkStream::run(dof_handler.begin_active(),
1112 * dof_handler.end(),
1113 * *this,
1114 * &AdvectionProblem::local_assemble_system,
1115 * &AdvectionProblem::copy_local_to_global,
1116 * AssemblyScratchData(fe),
1117 * AssemblyCopyData());
1118 * }
1119 *
1120 *
1121 *
1122 * @endcode
1123 *
1124 * As already mentioned above, we need to have scratch objects for
1125 * the parallel computation of local contributions. These objects
1126 * contain FEValues and FEFaceValues objects (as well as some arrays), and so
1127 * we will need to have constructors and copy constructors that allow us to
1128 * create them. For the cell terms we need the values
1129 * and gradients of the shape functions, the quadrature points in
1130 * order to determine the source density and the advection field at
1131 * a given point, and the weights of the quadrature points times the
1132 * determinant of the Jacobian at these points. In contrast, for the
1133 * boundary integrals, we don't need the gradients, but rather the
1134 * normal vectors to the cells. This determines which update flags
1135 * we will have to pass to the constructors of the members of the
1136 * class:
1137 *
1138 * @code
1139 * template <int dim>
1140 * AdvectionProblem<dim>::AssemblyScratchData::AssemblyScratchData(
1141 * const FiniteElement<dim> &fe)
1142 * : fe_values(fe,
1143 * QGauss<dim>(fe.degree + 1),
1146 * , fe_face_values(fe,
1147 * QGauss<dim - 1>(fe.degree + 1),
1150 * , rhs_values(fe_values.get_quadrature().size())
1151 * , advection_directions(fe_values.get_quadrature().size())
1152 * , face_boundary_values(fe_face_values.get_quadrature().size())
1153 * , face_advection_directions(fe_face_values.get_quadrature().size())
1154 * {}
1155 *
1156 *
1157 *
1158 * template <int dim>
1159 * AdvectionProblem<dim>::AssemblyScratchData::AssemblyScratchData(
1160 * const AssemblyScratchData &scratch_data)
1161 * : fe_values(scratch_data.fe_values.get_fe(),
1162 * scratch_data.fe_values.get_quadrature(),
1165 * , fe_face_values(scratch_data.fe_face_values.get_fe(),
1166 * scratch_data.fe_face_values.get_quadrature(),
1169 * , rhs_values(scratch_data.rhs_values.size())
1170 * , advection_directions(scratch_data.advection_directions.size())
1171 * , face_boundary_values(scratch_data.face_boundary_values.size())
1172 * , face_advection_directions(scratch_data.face_advection_directions.size())
1173 * {}
1174 *
1175 *
1176 *
1177 * @endcode
1178 *
1179 * Now, this is the function that does the actual work. It is not very
1180 * different from the <code>assemble_system</code> functions of previous
1181 * example programs, so we will again only comment on the differences. The
1182 * mathematical stuff closely follows what we have said in the introduction.
1183 *
1184
1185 *
1186 * There are a number of points worth mentioning here, though. The
1187 * first one is that we have moved the FEValues and FEFaceValues
1188 * objects into the ScratchData object. We have done so because the
1189 * alternative would have been to simply create one every time we
1190 * get into this function -- i.e., on every cell. It now turns out
1191 * that the FEValues classes were written with the explicit goal of
1192 * moving everything that remains the same from cell to cell into
1193 * the construction of the object, and only do as little work as
1194 * possible in FEValues::reinit() whenever we move to a new
1195 * cell. What this means is that it would be very expensive to
1196 * create a new object of this kind in this function as we would
1197 * have to do it for every cell -- exactly the thing we wanted to
1198 * avoid with the FEValues class. Instead, what we do is create it
1199 * only once (or a small number of times) in the scratch objects and
1200 * then re-use it as often as we can.
1201 *
1202
1203 *
1204 * This begs the question of whether there are other objects we
1205 * create in this function whose creation is expensive compared to
1206 * its use. Indeed, at the top of the function, we declare all sorts
1207 * of objects. The <code>AdvectionField</code>,
1208 * <code>RightHandSide</code> and <code>BoundaryValues</code> do not
1209 * cost much to create, so there is no harm here. However,
1210 * allocating memory in creating the <code>rhs_values</code> and
1211 * similar variables below typically costs a significant amount of
1212 * time, compared to just accessing the (temporary) values we store
1213 * in them. Consequently, these would be candidates for moving into
1214 * the <code>AssemblyScratchData</code> class. We will leave this as
1215 * an exercise.
1216 *
1217 * @code
1218 * template <int dim>
1219 * void AdvectionProblem<dim>::local_assemble_system(
1220 * const typename DoFHandler<dim>::active_cell_iterator &cell,
1221 * AssemblyScratchData & scratch_data,
1222 * AssemblyCopyData & copy_data)
1223 * {
1224 * @endcode
1225 *
1226 * We define some abbreviations to avoid unnecessarily long lines:
1227 *
1228 * @code
1229 * const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
1230 * const unsigned int n_q_points =
1231 * scratch_data.fe_values.get_quadrature().size();
1232 * const unsigned int n_face_q_points =
1233 * scratch_data.fe_face_values.get_quadrature().size();
1234 *
1235 * @endcode
1236 *
1237 * We declare cell matrix and cell right hand side...
1238 *
1239 * @code
1240 * copy_data.cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
1241 * copy_data.cell_rhs.reinit(dofs_per_cell);
1242 *
1243 * @endcode
1244 *
1245 * ... an array to hold the global indices of the degrees of freedom of
1246 * the cell on which we are presently working...
1247 *
1248 * @code
1249 * copy_data.local_dof_indices.resize(dofs_per_cell);
1250 *
1251 * @endcode
1252 *
1253 * ... then initialize the <code>FEValues</code> object...
1254 *
1255 * @code
1256 * scratch_data.fe_values.reinit(cell);
1257 *
1258 * @endcode
1259 *
1260 * ... obtain the values of right hand side and advection directions
1261 * at the quadrature points...
1262 *
1263 * @code
1264 * scratch_data.advection_field.value_list(
1265 * scratch_data.fe_values.get_quadrature_points(),
1266 * scratch_data.advection_directions);
1267 * scratch_data.right_hand_side.value_list(
1268 * scratch_data.fe_values.get_quadrature_points(), scratch_data.rhs_values);
1269 *
1270 * @endcode
1271 *
1272 * ... set the value of the streamline diffusion parameter as
1273 * described in the introduction...
1274 *
1275 * @code
1276 * const double delta = 0.1 * cell->diameter();
1277 *
1278 * @endcode
1279 *
1280 * ... and assemble the local contributions to the system matrix and
1281 * right hand side as also discussed above:
1282 *
1283 * @code
1284 * for (unsigned int q_point = 0; q_point < n_q_points; ++q_point)
1285 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1286 * {
1287 * @endcode
1288 *
1289 * Alias the AssemblyScratchData object to keep the lines from
1290 * getting too long:
1291 *
1292 * @code
1293 * const auto &sd = scratch_data;
1294 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1295 * copy_data.cell_matrix(i, j) +=
1296 * ((sd.fe_values.shape_value(i, q_point) + // (phi_i +
1297 * delta * (sd.advection_directions[q_point] * // delta beta
1298 * sd.fe_values.shape_grad(i, q_point))) * // grad phi_i)
1299 * sd.advection_directions[q_point] * // beta
1300 * sd.fe_values.shape_grad(j, q_point)) * // grad phi_j
1301 * sd.fe_values.JxW(q_point); // dx
1302 *
1303 * copy_data.cell_rhs(i) +=
1304 * (sd.fe_values.shape_value(i, q_point) + // (phi_i +
1305 * delta * (sd.advection_directions[q_point] * // delta beta
1306 * sd.fe_values.shape_grad(i, q_point))) * // grad phi_i)
1307 * sd.rhs_values[q_point] * // f
1308 * sd.fe_values.JxW(q_point); // dx
1309 * }
1310 *
1311 * @endcode
1312 *
1313 * Besides the cell terms which we have built up now, the bilinear
1314 * form of the present problem also contains terms on the boundary of
1315 * the domain. Therefore, we have to check whether any of the faces of
1316 * this cell are on the boundary of the domain, and if so assemble the
1317 * contributions of this face as well. Of course, the bilinear form
1318 * only contains contributions from the <code>inflow</code> part of
1319 * the boundary, but to find out whether a certain part of a face of
1320 * the present cell is part of the inflow boundary, we have to have
1321 * information on the exact location of the quadrature points and on
1322 * the direction of flow at this point; we obtain this information
1323 * using the FEFaceValues object and only decide within the main loop
1324 * whether a quadrature point is on the inflow boundary.
1325 *
1326 * @code
1327 * for (const auto &face : cell->face_iterators())
1328 * if (face->at_boundary())
1329 * {
1330 * @endcode
1331 *
1332 * Ok, this face of the present cell is on the boundary of the
1333 * domain. Just as for the usual FEValues object which we have
1334 * used in previous examples and also above, we have to
1335 * reinitialize the FEFaceValues object for the present face:
1336 *
1337 * @code
1338 * scratch_data.fe_face_values.reinit(cell, face);
1339 *
1340 * @endcode
1341 *
1342 * For the quadrature points at hand, we ask for the values of
1343 * the inflow function and for the direction of flow:
1344 *
1345 * @code
1346 * scratch_data.boundary_values.value_list(
1347 * scratch_data.fe_face_values.get_quadrature_points(),
1348 * scratch_data.face_boundary_values);
1349 * scratch_data.advection_field.value_list(
1350 * scratch_data.fe_face_values.get_quadrature_points(),
1351 * scratch_data.face_advection_directions);
1352 *
1353 * @endcode
1354 *
1355 * Now loop over all quadrature points and see whether this face is on
1356 * the inflow or outflow part of the boundary. The normal
1357 * vector points out of the cell: since the face is at
1358 * the boundary, the normal vector points out of the domain,
1359 * so if the advection direction points into the domain, its
1360 * scalar product with the normal vector must be negative (to see why
1361 * this is true, consider the scalar product definition that uses a
1362 * cosine):
1363 *
1364 * @code
1365 * for (unsigned int q_point = 0; q_point < n_face_q_points; ++q_point)
1366 * if (scratch_data.fe_face_values.normal_vector(q_point) *
1367 * scratch_data.face_advection_directions[q_point] <
1368 * 0.)
1369 * @endcode
1370 *
1371 * If the face is part of the inflow boundary, then compute the
1372 * contributions of this face to the global matrix and right
1373 * hand side, using the values obtained from the
1374 * FEFaceValues object and the formulae discussed in the
1375 * introduction:
1376 *
1377 * @code
1378 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1379 * {
1380 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1381 * copy_data.cell_matrix(i, j) -=
1382 * (scratch_data.face_advection_directions[q_point] *
1383 * scratch_data.fe_face_values.normal_vector(q_point) *
1384 * scratch_data.fe_face_values.shape_value(i, q_point) *
1385 * scratch_data.fe_face_values.shape_value(j, q_point) *
1386 * scratch_data.fe_face_values.JxW(q_point));
1387 *
1388 * copy_data.cell_rhs(i) -=
1389 * (scratch_data.face_advection_directions[q_point] *
1390 * scratch_data.fe_face_values.normal_vector(q_point) *
1391 * scratch_data.face_boundary_values[q_point] *
1392 * scratch_data.fe_face_values.shape_value(i, q_point) *
1393 * scratch_data.fe_face_values.JxW(q_point));
1394 * }
1395 * }
1396 *
1397 * @endcode
1398 *
1399 * The final piece of information the copy routine needs is the global
1400 * indices of the degrees of freedom on this cell, so we end by writing
1401 * them to the local array:
1402 *
1403 * @code
1404 * cell->get_dof_indices(copy_data.local_dof_indices);
1405 * }
1406 *
1407 *
1408 *
1409 * @endcode
1410 *
1411 * The second function we needed to write was the one that copies
1412 * the local contributions the previous function computed (and
1413 * put into the AssemblyCopyData object) into the global matrix and right
1414 * hand side vector objects. This is essentially what we always had
1415 * as the last block of code when assembling something on every
1416 * cell. The following should therefore be pretty obvious:
1417 *
1418 * @code
1419 * template <int dim>
1420 * void
1421 * AdvectionProblem<dim>::copy_local_to_global(const AssemblyCopyData &copy_data)
1422 * {
1423 * hanging_node_constraints.distribute_local_to_global(
1424 * copy_data.cell_matrix,
1425 * copy_data.cell_rhs,
1426 * copy_data.local_dof_indices,
1427 * system_matrix,
1428 * system_rhs);
1429 * }
1430 *
1431 * @endcode
1432 *
1433 * Here comes the linear solver routine. As the system is no longer
1434 * symmetric positive definite as in all the previous examples, we cannot
1435 * use the Conjugate Gradient method anymore. Rather, we use a solver that
1436 * is more general and does not rely on any special properties of the
1437 * matrix: the GMRES method. GMRES, like the conjugate gradient method,
1438 * requires a decent preconditioner: we use a Jacobi preconditioner here,
1439 * which works well enough for this problem.
1440 *
1441 * @code
1442 * template <int dim>
1443 * void AdvectionProblem<dim>::solve()
1444 * {
1445 * SolverControl solver_control(std::max<std::size_t>(1000,
1446 * system_rhs.size() / 10),
1447 * 1e-10 * system_rhs.l2_norm());
1448 * SolverGMRES<Vector<double>> solver(solver_control);
1450 * preconditioner.initialize(system_matrix, 1.0);
1451 * solver.solve(system_matrix, solution, system_rhs, preconditioner);
1452 *
1453 * Vector<double> residual(dof_handler.n_dofs());
1454 *
1455 * system_matrix.vmult(residual, solution);
1456 * residual -= system_rhs;
1457 * std::cout << " Iterations required for convergence: "
1458 * << solver_control.last_step() << '\n'
1459 * << " Max norm of residual: "
1460 * << residual.linfty_norm() << '\n';
1461 *
1462 * hanging_node_constraints.distribute(solution);
1463 * }
1464 *
1465 * @endcode
1466 *
1467 * The following function refines the grid according to the quantity
1468 * described in the introduction. The respective computations are made in
1469 * the class <code>GradientEstimation</code>.
1470 *
1471 * @code
1472 * template <int dim>
1473 * void AdvectionProblem<dim>::refine_grid()
1474 * {
1475 * Vector<float> estimated_error_per_cell(triangulation.n_active_cells());
1476 *
1477 * GradientEstimation::estimate(dof_handler,
1478 * solution,
1479 * estimated_error_per_cell);
1480 *
1482 * estimated_error_per_cell,
1483 * 0.3,
1484 * 0.03);
1485 *
1486 * triangulation.execute_coarsening_and_refinement();
1487 * }
1488 *
1489 * @endcode
1490 *
1491 * This function is similar to the one in step 6, but since we use a higher
1492 * degree finite element we save the solution in a different
1493 * way. Visualization programs like VisIt and Paraview typically only
1494 * understand data that is associated with nodes: they cannot plot
1495 * fifth-degree basis functions, which results in a very inaccurate picture
1496 * of the solution we computed. To get around this we save multiple
1497 * <em>patches</em> per cell: in 2D we save 64 bilinear `cells' to the VTU
1498 * file for each cell, and in 3D we save 512. The end result is that the
1499 * visualization program will use a piecewise linear interpolation of the
1500 * cubic basis functions: this captures the solution detail and, with most
1501 * screen resolutions, looks smooth. We save the grid in a separate step
1502 * with no extra patches so that we have a visual representation of the cell
1503 * faces.
1504 *
1505
1506 *
1507 * Version 9.1 of deal.II gained the ability to write higher degree
1508 * polynomials (i.e., write piecewise bicubic visualization data for our
1509 * piecewise bicubic solution) VTK and VTU output: however, not all recent
1510 * versions of ParaView and VisIt (as of 2018) can read this format, so we
1511 * use the older, more general (but less efficient) approach here.
1512 *
1513 * @code
1514 * template <int dim>
1515 * void AdvectionProblem<dim>::output_results(const unsigned int cycle) const
1516 * {
1517 * {
1518 * GridOut grid_out;
1519 * std::ofstream output("grid-" + std::to_string(cycle) + ".vtu");
1520 * grid_out.write_vtu(triangulation, output);
1521 * }
1522 *
1523 * {
1524 * DataOut<dim> data_out;
1525 * data_out.attach_dof_handler(dof_handler);
1526 * data_out.add_data_vector(solution, "solution");
1527 * data_out.build_patches(8);
1528 *
1529 * @endcode
1530 *
1531 * VTU output can be expensive, both to compute and to write to
1532 * disk. Here we ask ZLib, a compression library, to compress the data
1533 * in a way that maximizes throughput.
1534 *
1535 * @code
1536 * DataOutBase::VtkFlags vtk_flags;
1537 * vtk_flags.compression_level =
1538 * DataOutBase::VtkFlags::ZlibCompressionLevel::best_speed;
1539 * data_out.set_flags(vtk_flags);
1540 *
1541 * std::ofstream output("solution-" + std::to_string(cycle) + ".vtu");
1542 * data_out.write_vtu(output);
1543 * }
1544 * }
1545 *
1546 *
1547 * @endcode
1548 *
1549 * ... as is the main loop (setup -- solve -- refine), aside from the number
1550 * of cycles and the initial grid:
1551 *
1552 * @code
1553 * template <int dim>
1554 * void AdvectionProblem<dim>::run()
1555 * {
1556 * for (unsigned int cycle = 0; cycle < 10; ++cycle)
1557 * {
1558 * std::cout << "Cycle " << cycle << ':' << std::endl;
1559 *
1560 * if (cycle == 0)
1561 * {
1562 * GridGenerator::hyper_cube(triangulation, -1, 1);
1563 * triangulation.refine_global(3);
1564 * }
1565 * else
1566 * {
1567 * refine_grid();
1568 * }
1569 *
1570 *
1571 * std::cout << " Number of active cells: "
1572 * << triangulation.n_active_cells() << std::endl;
1573 *
1574 * setup_system();
1575 *
1576 * std::cout << " Number of degrees of freedom: "
1577 * << dof_handler.n_dofs() << std::endl;
1578 *
1579 * assemble_system();
1580 * solve();
1581 * output_results(cycle);
1582 * }
1583 * }
1584 *
1585 *
1586 *
1587 * @endcode
1588 *
1589 *
1590 * <a name="GradientEstimationclassimplementation"></a>
1591 * <h3>GradientEstimation class implementation</h3>
1592 *
1593
1594 *
1595 * Now for the implementation of the <code>GradientEstimation</code> class.
1596 * Let us start by defining constructors for the
1597 * <code>EstimateScratchData</code> class used by the
1598 * <code>estimate_cell()</code> function:
1599 *
1600 * @code
1601 * template <int dim>
1602 * GradientEstimation::EstimateScratchData<dim>::EstimateScratchData(
1603 * const FiniteElement<dim> &fe,
1604 * const Vector<double> & solution,
1605 * Vector<float> & error_per_cell)
1606 * : fe_midpoint_value(fe,
1607 * QMidpoint<dim>(),
1608 * update_values | update_quadrature_points)
1609 * , solution(solution)
1610 * , error_per_cell(error_per_cell)
1611 * , cell_midpoint_value(1)
1612 * , neighbor_midpoint_value(1)
1613 * {
1614 * @endcode
1615 *
1616 * We allocate a vector to hold iterators to all active neighbors of
1617 * a cell. We reserve the maximal number of active neighbors in order to
1618 * avoid later reallocations. Note how this maximal number of active
1619 * neighbors is computed here.
1620 *
1621 * @code
1622 * active_neighbors.reserve(GeometryInfo<dim>::faces_per_cell *
1623 * GeometryInfo<dim>::max_children_per_face);
1624 * }
1625 *
1626 *
1627 * template <int dim>
1628 * GradientEstimation::EstimateScratchData<dim>::EstimateScratchData(
1629 * const EstimateScratchData &scratch_data)
1630 * : fe_midpoint_value(scratch_data.fe_midpoint_value.get_fe(),
1631 * scratch_data.fe_midpoint_value.get_quadrature(),
1632 * update_values | update_quadrature_points)
1633 * , solution(scratch_data.solution)
1634 * , error_per_cell(scratch_data.error_per_cell)
1635 * , cell_midpoint_value(1)
1636 * , neighbor_midpoint_value(1)
1637 * {}
1638 *
1639 *
1640 * @endcode
1641 *
1642 * Next comes the implementation of the <code>GradientEstimation</code>
1643 * class. The first function does not much except for delegating work to the
1644 * other function, but there is a bit of setup at the top.
1645 *
1646
1647 *
1648 * Before starting with the work, we check that the vector into
1649 * which the results are written has the right size. Programming
1650 * mistakes in which one forgets to size arguments correctly at the
1651 * calling site are quite common. Because the resulting damage from
1652 * not catching such errors is often subtle (e.g., corruption of
1653 * data somewhere in memory, or non-reproducible results), it is
1654 * well worth the effort to check for such things.
1655 *
1656 * @code
1657 * template <int dim>
1658 * void GradientEstimation::estimate(const DoFHandler<dim> &dof_handler,
1659 * const Vector<double> & solution,
1660 * Vector<float> & error_per_cell)
1661 * {
1662 * Assert(
1663 * error_per_cell.size() == dof_handler.get_triangulation().n_active_cells(),
1664 * ExcInvalidVectorLength(error_per_cell.size(),
1665 * dof_handler.get_triangulation().n_active_cells()));
1666 *
1667 * WorkStream::run(dof_handler.begin_active(),
1668 * dof_handler.end(),
1669 * &GradientEstimation::template estimate_cell<dim>,
1670 * std::function<void(const EstimateCopyData &)>(),
1671 * EstimateScratchData<dim>(dof_handler.get_fe(),
1672 * solution,
1673 * error_per_cell),
1674 * EstimateCopyData());
1675 * }
1676 *
1677 *
1678 * @endcode
1679 *
1680 * Here comes the function that estimates the local error by computing the
1681 * finite difference approximation of the gradient. The function first
1682 * computes the list of active neighbors of the present cell and then
1683 * computes the quantities described in the introduction for each of
1684 * the neighbors. The reason for this order is that it is not a one-liner
1685 * to find a given neighbor with locally refined meshes. In principle, an
1686 * optimized implementation would find neighbors and the quantities
1687 * depending on them in one step, rather than first building a list of
1688 * neighbors and in a second step their contributions but we will gladly
1689 * leave this as an exercise. As discussed before, the worker function
1690 * passed to WorkStream::run works on "scratch" objects that keep all
1691 * temporary objects. This way, we do not need to create and initialize
1692 * objects that are expensive to initialize within the function that does
1693 * the work every time it is called for a given cell. Such an argument is
1694 * passed as the second argument. The third argument would be a "copy-data"
1695 * object (see @ref threads for more information) but we do not actually use
1696 * any of these here. Since WorkStream::run() insists on passing three
1697 * arguments, we declare this function with three arguments, but simply
1698 * ignore the last one.
1699 *
1700
1701 *
1702 * (This is unsatisfactory from an aesthetic perspective. It can be avoided
1703 * by using an anonymous (lambda) function. If you allow, let us here show
1704 * how. First, assume that we had declared this function to only take two
1705 * arguments by omitting the unused last one. Now, WorkStream::run still
1706 * wants to call this function with three arguments, so we need to find a
1707 * way to "forget" the third argument in the call. Simply passing
1708 * WorkStream::run the pointer to the function as we do above will not do
1709 * this -- the compiler will complain that a function declared to have two
1710 * arguments is called with three arguments. However, we can do this by
1711 * passing the following as the third argument to WorkStream::run():
1712 * <div class=CodeFragmentInTutorialComment>
1713 * @code
1714 * [](const typename DoFHandler<dim>::active_cell_iterator &cell,
1715 * EstimateScratchData<dim> & scratch_data,
1716 * EstimateCopyData &)
1717 * {
1718 * GradientEstimation::estimate_cell<dim>(cell, scratch_data);
1719 * }
1720 * @endcode
1721 * </div>
1722 * This is not much better than the solution implemented below: either the
1723 * routine itself must take three arguments or it must be wrapped by
1724 * something that takes three arguments. We don't use this since adding the
1725 * unused argument at the beginning is simpler.
1726 *
1727
1728 *
1729 * Now for the details:
1730 *
1731 * @code
1732 * template <int dim>
1733 * void GradientEstimation::estimate_cell(
1734 * const typename DoFHandler<dim>::active_cell_iterator &cell,
1735 * EstimateScratchData<dim> & scratch_data,
1736 * const EstimateCopyData &)
1737 * {
1738 * @endcode
1739 *
1740 * We need space for the tensor <code>Y</code>, which is the sum of
1741 * outer products of the y-vectors.
1742 *
1743 * @code
1744 * Tensor<2, dim> Y;
1745 *
1746 * @endcode
1747 *
1748 * First initialize the <code>FEValues</code> object, as well as the
1749 * <code>Y</code> tensor:
1750 *
1751 * @code
1752 * scratch_data.fe_midpoint_value.reinit(cell);
1753 *
1754 * @endcode
1755 *
1756 * Now, before we go on, we first compute a list of all active neighbors
1757 * of the present cell. We do so by first looping over all faces and see
1758 * whether the neighbor there is active, which would be the case if it
1759 * is on the same level as the present cell or one level coarser (note
1760 * that a neighbor can only be once coarser than the present cell, as
1761 * we only allow a maximal difference of one refinement over a face in
1762 * deal.II). Alternatively, the neighbor could be on the same level
1763 * and be further refined; then we have to find which of its children
1764 * are next to the present cell and select these (note that if a child
1765 * of a neighbor of an active cell that is next to this active cell,
1766 * needs necessarily be active itself, due to the one-refinement rule
1767 * cited above).
1768 *
1769
1770 *
1771 * Things are slightly different in one space dimension, as there the
1772 * one-refinement rule does not exist: neighboring active cells may
1773 * differ in as many refinement levels as they like. In this case, the
1774 * computation becomes a little more difficult, but we will explain
1775 * this below.
1776 *
1777
1778 *
1779 * Before starting the loop over all neighbors of the present cell, we
1780 * have to clear the array storing the iterators to the active
1781 * neighbors, of course.
1782 *
1783 * @code
1784 * scratch_data.active_neighbors.clear();
1785 * for (const auto face_n : cell->face_indices())
1786 * if (!cell->at_boundary(face_n))
1787 * {
1788 * @endcode
1789 *
1790 * First define an abbreviation for the iterator to the face and
1791 * the neighbor
1792 *
1793 * @code
1794 * const auto face = cell->face(face_n);
1795 * const auto neighbor = cell->neighbor(face_n);
1796 *
1797 * @endcode
1798 *
1799 * Then check whether the neighbor is active. If it is, then it
1800 * is on the same level or one level coarser (if we are not in
1801 * 1D), and we are interested in it in any case.
1802 *
1803 * @code
1804 * if (neighbor->is_active())
1805 * scratch_data.active_neighbors.push_back(neighbor);
1806 * else
1807 * {
1808 * @endcode
1809 *
1810 * If the neighbor is not active, then check its children.
1811 *
1812 * @code
1813 * if (dim == 1)
1814 * {
1815 * @endcode
1816 *
1817 * To find the child of the neighbor which bounds to the
1818 * present cell, successively go to its right child if
1819 * we are left of the present cell (n==0), or go to the
1820 * left child if we are on the right (n==1), until we
1821 * find an active cell.
1822 *
1823 * @code
1824 * auto neighbor_child = neighbor;
1825 * while (neighbor_child->has_children())
1826 * neighbor_child = neighbor_child->child(face_n == 0 ? 1 : 0);
1827 *
1828 * @endcode
1829 *
1830 * As this used some non-trivial geometrical intuition,
1831 * we might want to check whether we did it right,
1832 * i.e., check whether the neighbor of the cell we found
1833 * is indeed the cell we are presently working
1834 * on. Checks like this are often useful and have
1835 * frequently uncovered errors both in algorithms like
1836 * the line above (where it is simple to involuntarily
1837 * exchange <code>n==1</code> for <code>n==0</code> or
1838 * the like) and in the library (the assumptions
1839 * underlying the algorithm above could either be wrong,
1840 * wrongly documented, or are violated due to an error
1841 * in the library). One could in principle remove such
1842 * checks after the program works for some time, but it
1843 * might be a good things to leave it in anyway to check
1844 * for changes in the library or in the algorithm above.
1845 *
1846
1847 *
1848 * Note that if this check fails, then this is certainly
1849 * an error that is irrecoverable and probably qualifies
1850 * as an internal error. We therefore use a predefined
1851 * exception class to throw here.
1852 *
1853 * @code
1854 * Assert(neighbor_child->neighbor(face_n == 0 ? 1 : 0) == cell,
1855 * ExcInternalError());
1856 *
1857 * @endcode
1858 *
1859 * If the check succeeded, we push the active neighbor
1860 * we just found to the stack we keep:
1861 *
1862 * @code
1863 * scratch_data.active_neighbors.push_back(neighbor_child);
1864 * }
1865 * else
1866 * @endcode
1867 *
1868 * If we are not in 1d, we collect all neighbor children
1869 * `behind' the subfaces of the current face and move on:
1870 *
1871 * @code
1872 * for (unsigned int subface_n = 0; subface_n < face->n_children();
1873 * ++subface_n)
1874 * scratch_data.active_neighbors.push_back(
1875 * cell->neighbor_child_on_subface(face_n, subface_n));
1876 * }
1877 * }
1878 *
1879 * @endcode
1880 *
1881 * OK, now that we have all the neighbors, lets start the computation
1882 * on each of them. First we do some preliminaries: find out about the
1883 * center of the present cell and the solution at this point. The
1884 * latter is obtained as a vector of function values at the quadrature
1885 * points, of which there are only one, of course. Likewise, the
1886 * position of the center is the position of the first (and only)
1887 * quadrature point in real space.
1888 *
1889 * @code
1890 * const Point<dim> this_center =
1891 * scratch_data.fe_midpoint_value.quadrature_point(0);
1892 *
1893 * scratch_data.fe_midpoint_value.get_function_values(
1894 * scratch_data.solution, scratch_data.cell_midpoint_value);
1895 *
1896 * @endcode
1897 *
1898 * Now loop over all active neighbors and collect the data we
1899 * need.
1900 *
1901 * @code
1902 * Tensor<1, dim> projected_gradient;
1903 * for (const auto &neighbor : scratch_data.active_neighbors)
1904 * {
1905 * @endcode
1906 *
1907 * Then get the center of the neighbor cell and the value of the
1908 * finite element function at that point. Note that for this
1909 * information we have to reinitialize the <code>FEValues</code>
1910 * object for the neighbor cell.
1911 *
1912 * @code
1913 * scratch_data.fe_midpoint_value.reinit(neighbor);
1914 * const Point<dim> neighbor_center =
1915 * scratch_data.fe_midpoint_value.quadrature_point(0);
1916 *
1917 * scratch_data.fe_midpoint_value.get_function_values(
1918 * scratch_data.solution, scratch_data.neighbor_midpoint_value);
1919 *
1920 * @endcode
1921 *
1922 * Compute the vector <code>y</code> connecting the centers of the
1923 * two cells. Note that as opposed to the introduction, we denote
1924 * by <code>y</code> the normalized difference vector, as this is
1925 * the quantity used everywhere in the computations.
1926 *
1927 * @code
1928 * Tensor<1, dim> y = neighbor_center - this_center;
1929 * const double distance = y.norm();
1930 * y /= distance;
1931 *
1932 * @endcode
1933 *
1934 * Then add up the contribution of this cell to the Y matrix...
1935 *
1936 * @code
1937 * for (unsigned int i = 0; i < dim; ++i)
1938 * for (unsigned int j = 0; j < dim; ++j)
1939 * Y[i][j] += y[i] * y[j];
1940 *
1941 * @endcode
1942 *
1943 * ... and update the sum of difference quotients:
1944 *
1945 * @code
1946 * projected_gradient += (scratch_data.neighbor_midpoint_value[0] -
1947 * scratch_data.cell_midpoint_value[0]) /
1948 * distance * y;
1949 * }
1950 *
1951 * @endcode
1952 *
1953 * If now, after collecting all the information from the neighbors, we
1954 * can determine an approximation of the gradient for the present
1955 * cell, then we need to have passed over vectors <code>y</code> which
1956 * span the whole space, otherwise we would not have all components of
1957 * the gradient. This is indicated by the invertibility of the matrix.
1958 *
1959
1960 *
1961 * If the matrix is not invertible, then the present
1962 * cell had an insufficient number of active neighbors. In contrast to
1963 * all previous cases (where we raised exceptions) this is, however,
1964 * not a programming error: it is a runtime error that can happen in
1965 * optimized mode even if it ran well in debug mode, so it is
1966 * reasonable to try to catch this error also in optimized mode. For
1967 * this case, there is the <code>AssertThrow</code> macro: it checks
1968 * the condition like the <code>Assert</code> macro, but not only in
1969 * debug mode; it then outputs an error message, but instead of
1970 * aborting the program as in the case of the <code>Assert</code>
1971 * macro, the exception is thrown using the <code>throw</code> command
1972 * of C++. This way, one has the possibility to catch this error and
1973 * take reasonable counter actions. One such measure would be to
1974 * refine the grid globally, as the case of insufficient directions
1975 * can not occur if every cell of the initial grid has been refined at
1976 * least once.
1977 *
1978 * @code
1979 * AssertThrow(determinant(Y) != 0, ExcInsufficientDirections());
1980 *
1981 * @endcode
1982 *
1983 * If, on the other hand, the matrix is invertible, then invert it,
1984 * multiply the other quantity with it, and compute the estimated error
1985 * using this quantity and the correct powers of the mesh width:
1986 *
1987 * @code
1988 * const Tensor<2, dim> Y_inverse = invert(Y);
1989 *
1990 * const Tensor<1, dim> gradient = Y_inverse * projected_gradient;
1991 *
1992 * @endcode
1993 *
1994 * The last part of this function is the one where we write into
1995 * the element of the output vector what we have just
1996 * computed. The address of this vector has been stored in the
1997 * scratch data object, and all we have to do is know how to get
1998 * at the correct element inside this vector -- but we can ask the
1999 * cell we're on the how-manyth active cell it is for this:
2000 *
2001 * @code
2002 * scratch_data.error_per_cell(cell->active_cell_index()) =
2003 * (std::pow(cell->diameter(), 1 + 1.0 * dim / 2) * gradient.norm());
2004 * }
2005 * } // namespace Step9
2006 *
2007 *
2008 * @endcode
2009 *
2010 *
2011 * <a name="Mainfunction"></a>
2012 * <h3>Main function</h3>
2013 *
2014
2015 *
2016 * The <code>main</code> function is similar to the previous examples. The
2017 * primary difference is that we use MultithreadInfo to set the maximum
2018 * number of threads (see the documentation module @ref threads
2019 * "Parallel computing with multiple processors accessing shared memory"
2020 * for more information). The number of threads used is the minimum of the
2021 * environment variable DEAL_II_NUM_THREADS and the parameter of
2022 * <code>set_thread_limit</code>. If no value is given to
2023 * <code>set_thread_limit</code>, the default value from the Intel Threading
2024 * Building Blocks (TBB) library is used. If the call to
2025 * <code>set_thread_limit</code> is omitted, the number of threads will be
2026 * chosen by TBB independently of DEAL_II_NUM_THREADS.
2027 *
2028 * @code
2029 * int main()
2030 * {
2031 * using namespace dealii;
2032 * try
2033 * {
2035 *
2036 * Step9::AdvectionProblem<2> advection_problem_2d;
2037 * advection_problem_2d.run();
2038 * }
2039 * catch (std::exception &exc)
2040 * {
2041 * std::cerr << std::endl
2042 * << std::endl
2043 * << "----------------------------------------------------"
2044 * << std::endl;
2045 * std::cerr << "Exception on processing: " << std::endl
2046 * << exc.what() << std::endl
2047 * << "Aborting!" << std::endl
2048 * << "----------------------------------------------------"
2049 * << std::endl;
2050 * return 1;
2051 * }
2052 * catch (...)
2053 * {
2054 * std::cerr << std::endl
2055 * << std::endl
2056 * << "----------------------------------------------------"
2057 * << std::endl;
2058 * std::cerr << "Unknown exception!" << std::endl
2059 * << "Aborting!" << std::endl
2060 * << "----------------------------------------------------"
2061 * << std::endl;
2062 * return 1;
2063 * }
2064 *
2065 * return 0;
2066 * }
2067 * @endcode
2068<a name="Results"></a><h1>Results</h1>
2069
2070
2071
2072The results of this program are not particularly spectacular. They
2073consist of the console output, some grid files, and the solution on
2074each of these grids. First for the console output:
2075@code
2076Cycle 0:
2077 Number of active cells: 64
2078 Number of degrees of freedom: 1681
2079 Iterations required for convergence: 298
2080 Max norm of residual: 3.60316e-12
2081Cycle 1:
2082 Number of active cells: 124
2083 Number of degrees of freedom: 3537
2084 Iterations required for convergence: 415
2085 Max norm of residual: 3.70682e-12
2086Cycle 2:
2087 Number of active cells: 247
2088 Number of degrees of freedom: 6734
2089 Iterations required for convergence: 543
2090 Max norm of residual: 7.19716e-13
2091Cycle 3:
2092 Number of active cells: 502
2093 Number of degrees of freedom: 14105
2094 Iterations required for convergence: 666
2095 Max norm of residual: 3.45628e-13
2096Cycle 4:
2097 Number of active cells: 1003
2098 Number of degrees of freedom: 27462
2099 Iterations required for convergence: 1064
2100 Max norm of residual: 1.86495e-13
2101Cycle 5:
2102 Number of active cells: 1993
2103 Number of degrees of freedom: 55044
2104 Iterations required for convergence: 1251
2105 Max norm of residual: 1.28765e-13
2106Cycle 6:
2107 Number of active cells: 3985
2108 Number of degrees of freedom: 108492
2109 Iterations required for convergence: 2035
2110 Max norm of residual: 6.78085e-14
2111Cycle 7:
2112 Number of active cells: 7747
2113 Number of degrees of freedom: 210612
2114 Iterations required for convergence: 2187
2115 Max norm of residual: 2.61457e-14
2116Cycle 8:
2117 Number of active cells: 15067
2118 Number of degrees of freedom: 406907
2119 Iterations required for convergence: 3079
2120 Max norm of residual: 2.9932e-14
2121Cycle 9:
2122 Number of active cells: 29341
2123 Number of degrees of freedom: 780591
2124 Iterations required for convergence: 3913
2125 Max norm of residual: 8.15689e-15
2126@endcode
2127
2128Quite a number of cells are used on the finest level to resolve the features of
2129the solution. Here are the fourth and tenth grids:
2130<div class="twocolumn" style="width: 80%">
2131 <div>
2132 <img src="https://www.dealii.org/images/steps/developer/step-9-grid-3.png"
2133 alt="Fourth grid in the refinement cycle, showing some adaptivity to features."
2134 width="400" height="400">
2135 </div>
2136 <div>
2137 <img src="https://www.dealii.org/images/steps/developer/step-9-grid-9.png"
2138 alt="Tenth grid in the refinement cycle, showing that the waves are fully captured."
2139 width="400" height="400">
2140 </div>
2141</div>
2142and the fourth and tenth solutions:
2143<div class="twocolumn" style="width: 80%">
2144 <div>
2145 <img src="https://www.dealii.org/images/steps/developer/step-9-solution-3.png"
2146 alt="Fourth solution, showing that we resolve most features but some
2147 are sill unresolved and appear blury."
2148 width="400" height="400">
2149 </div>
2150 <div>
2151 <img src="https://www.dealii.org/images/steps/developer/step-9-solution-9.png"
2152 alt="Tenth solution, showing a fully resolved flow."
2153 width="400" height="400">
2154 </div>
2155</div>
2156and both the grid and solution zoomed in:
2157<div class="twocolumn" style="width: 80%">
2158 <div>
2159 <img src="https://www.dealii.org/images/steps/developer/step-9-solution-3-zoom.png"
2160 alt="Detail of the fourth solution, showing that we resolve most
2161 features but some are sill unresolved and appear blury. In particular,
2162 the larger cells need to be refined."
2163 width="400" height="400">
2164 </div>
2165 <div>
2166 <img src="https://www.dealii.org/images/steps/developer/step-9-solution-9-zoom.png"
2167 alt="Detail of the tenth solution, showing that we needed a lot more
2168 cells than were present in the fourth solution."
2169 width="400" height="400">
2170 </div>
2171</div>
2172
2173The solution is created by that part that is transported along the wiggly
2174advection field from the left and lower boundaries to the top right, and the
2175part that is created by the source in the lower left corner, and the results of
2176which are also transported along. The grid shown above is well-adapted to
2177resolve these features. The comparison between plots shows that, even though we
2178are using a high-order approximation, we still need adaptive mesh refinement to
2179fully resolve the wiggles.
2180 *
2181 *
2182<a name="PlainProg"></a>
2183<h1> The plain program</h1>
2184@include "step-9.cc"
2185*/
void reinit(const TriaIterator< DoFCellAccessor< dim, spacedim, level_dof_access > > &cell, const unsigned int face_no)
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)
SymmetricTensor< rank, dim, Number > sum(const SymmetricTensor< rank, dim, Number > &local, const MPI_Comm &mpi_communicator)
constexpr Number determinant(const SymmetricTensor< 2, dim, Number > &t)
Definition: vector.h:110
@ 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.
Point< 2 > second
Definition: grid_out.cc:4588
Point< 2 > first
Definition: grid_out.cc:4587
unsigned int level
Definition: grid_out.cc:4590
__global__ void set(Number *val, const Number s, const size_type N)
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcInternalError()
typename ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:438
void loop(ITERATOR begin, typename identity< ITERATOR >::type end, DOFINFO &dinfo, INFOBOX &info, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &cell_worker, const std::function< void(DOFINFO &, typename INFOBOX::CellInfo &)> &boundary_worker, const std::function< void(DOFINFO &, DOFINFO &, typename INFOBOX::CellInfo &, typename INFOBOX::CellInfo &)> &face_worker, ASSEMBLER &assembler, const LoopControl &lctrl=LoopControl())
Definition: loop.h:439
void initialize(const MatrixType &A, const AdditionalData &parameters=AdditionalData())
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.
static const types::blas_int one
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition: divergence.h:472
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
Definition: utilities.cc:188
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
void call(const std::function< RT()> &function, internal::return_value< RT > &ret_val)
VectorType::value_type * end(VectorType &V)
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)
Definition: work_stream.h:1252
void copy(const T *begin, const T *end, U *dest)
int(&) functions(const void *v1, const void *v2)
void assemble(const MeshWorker::DoFInfoBox< dim, DOFINFO > &dinfo, A *assembler)
Definition: loop.h:71
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation