612 *
class BoundaryValues :
public Function<dim>
616 * std::vector<double> &values,
617 *
const unsigned int = 0) const override
621 *
for (
unsigned int i = 0; i <
values.size(); ++i)
623 *
if (points[i][0] < 0.5)
638 * The flow field is chosen to be a quarter circle with counterclockwise
639 * flow direction and with the origin as midpoint
for the right half of the
640 * domain with
positive @f$x@f$
values, whereas the flow simply goes to the left
641 * in the left part of the domain at a velocity that matches the one coming
642 * in from the right. In the circular part the magnitude of the flow
643 * velocity is proportional to the distance from the origin. This is a
644 * difference to @ref step_12
"step-12", where the magnitude was 1 everywhere. the
new
645 * definition leads to a linear variation of @f$\beta@f$ along each given face
646 * of a cell. On the other hand, the solution @f$u(x,y)@f$ is exactly the same
650 *
void value_list(
const std::vector<
Point<dim>> &points,
655 *
for (
unsigned int i = 0; i < points.size(); ++i)
657 *
if (points[i][0] > 0)
659 *
values[i][0] = -points[i][1];
660 *
values[i][1] = points[i][0];
665 *
values[i][0] = -points[i][1];
676 * <a name=
"step_30-ClassDGTransportEquation"></a>
677 * <h3>Class: DGTransportEquation</h3>
681 * This declaration of
this class is utterly unaffected by our current
686 *
class DGTransportEquation
689 * DGTransportEquation();
707 *
const Beta<dim> beta_function;
708 *
const RHS<dim> rhs_function;
709 *
const BoundaryValues<dim> boundary_function;
716 * Likewise, the constructor of the
class as well as the
functions
717 * assembling the terms corresponding to cell interiors and boundary faces
718 * are unchanged from before. The function that assembles face terms between
719 * cells also did not change because all it does is operate on two objects
721 * and
FESubfaceValues). Where these objects come from, i.e. how they are
722 * initialized, is of no concern to
this function: it simply assumes that
723 * the quadrature points on faces or subfaces represented by the two objects
724 * correspond to the same points in physical space.
728 * DGTransportEquation<dim>::DGTransportEquation()
731 * , boundary_function()
737 *
void DGTransportEquation<dim>::assemble_cell_term(
744 * std::vector<Point<dim>> beta(fe_v.n_quadrature_points);
745 * std::vector<double> rhs(fe_v.n_quadrature_points);
747 * beta_function.value_list(fe_v.get_quadrature_points(), beta);
748 * rhs_function.value_list(fe_v.get_quadrature_points(), rhs);
750 *
for (
unsigned int point = 0;
point < fe_v.n_quadrature_points; ++
point)
751 *
for (
unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
753 *
for (
unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
754 * ui_vi_matrix(i, j) -= beta[
point] * fe_v.shape_grad(i, point) *
755 * fe_v.shape_value(j, point) * JxW[
point];
758 * rhs[
point] * fe_v.shape_value(i, point) * JxW[
point];
764 *
void DGTransportEquation<dim>::assemble_boundary_term(
769 *
const std::vector<double> &JxW = fe_v.get_JxW_values();
770 *
const std::vector<Tensor<1, dim>> &normals = fe_v.get_normal_vectors();
772 * std::vector<Point<dim>> beta(fe_v.n_quadrature_points);
773 * std::vector<double> g(fe_v.n_quadrature_points);
775 * beta_function.value_list(fe_v.get_quadrature_points(), beta);
776 * boundary_function.value_list(fe_v.get_quadrature_points(), g);
778 *
for (
unsigned int point = 0;
point < fe_v.n_quadrature_points; ++
point)
780 *
const double beta_n = beta[
point] * normals[
point];
782 *
for (
unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
783 *
for (
unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
784 * ui_vi_matrix(i, j) += beta_n * fe_v.shape_value(j, point) *
785 * fe_v.shape_value(i, point) * JxW[
point];
787 *
for (
unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
789 * beta_n * g[
point] * fe_v.shape_value(i, point) * JxW[
point];
795 *
void DGTransportEquation<dim>::assemble_face_term(
803 *
const std::vector<double> &JxW = fe_v.get_JxW_values();
804 *
const std::vector<Tensor<1, dim>> &normals = fe_v.get_normal_vectors();
806 * std::vector<Point<dim>> beta(fe_v.n_quadrature_points);
808 * beta_function.value_list(fe_v.get_quadrature_points(), beta);
810 *
for (
unsigned int point = 0;
point < fe_v.n_quadrature_points; ++
point)
812 *
const double beta_n = beta[
point] * normals[
point];
815 *
for (
unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
816 *
for (
unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
817 * ui_vi_matrix(i, j) += beta_n * fe_v.shape_value(j, point) *
818 * fe_v.shape_value(i, point) * JxW[
point];
820 *
for (
unsigned int k = 0; k < fe_v_neighbor.dofs_per_cell; ++k)
821 *
for (
unsigned int j = 0; j < fe_v.dofs_per_cell; ++j)
822 * ui_ve_matrix(k, j) -= beta_n * fe_v.shape_value(j, point) *
823 * fe_v_neighbor.shape_value(k, point) *
828 *
for (
unsigned int i = 0; i < fe_v.dofs_per_cell; ++i)
829 *
for (
unsigned int l = 0;
l < fe_v_neighbor.dofs_per_cell; ++
l)
830 * ue_vi_matrix(i, l) += beta_n *
831 * fe_v_neighbor.shape_value(l, point) *
832 * fe_v.shape_value(i, point) * JxW[
point];
834 *
for (
unsigned int k = 0; k < fe_v_neighbor.dofs_per_cell; ++k)
835 *
for (
unsigned int l = 0;
l < fe_v_neighbor.dofs_per_cell; ++
l)
836 * ue_ve_matrix(k, l) -=
837 * beta_n * fe_v_neighbor.shape_value(l, point) *
838 * fe_v_neighbor.shape_value(k, point) * JxW[
point];
847 * <a name=
"step_30-ClassDGMethod"></a>
848 * <h3>Class: DGMethod</h3>
852 * This declaration is much like that of @ref step_12
"step-12". However, we introduce a
853 *
new routine (set_anisotropic_flags) and modify another one (refine_grid).
860 * DGMethod(
const bool anisotropic);
865 *
void setup_system();
866 *
void assemble_system();
868 *
void refine_grid();
869 *
void set_anisotropic_flags();
870 *
void output_results(
const unsigned int cycle)
const;
876 * Again we want to use DG elements of degree 1 (but
this is only
877 * specified in the constructor). If you want to use a DG method of a
878 * different degree replace 1 in the constructor by the
new degree.
881 *
const unsigned int degree;
889 * This is
new, the threshold
value used in the evaluation of the
890 * anisotropic jump indicator explained in the introduction. Its
value is
891 * set to 3.0 in the constructor, but it can easily be changed to a
892 * different
value greater than 1.
895 *
const double anisotropic_threshold_ratio;
898 * This is a
bool flag indicating whether anisotropic refinement shall be
899 * used or not. It is set by the constructor, which takes an argument of
903 *
const bool anisotropic;
906 *
const QGauss<dim - 1> face_quadrature;
911 *
const DGTransportEquation<dim> dg;
916 * DGMethod<dim>::DGMethod(
const bool anisotropic)
920 * Change here
for DG methods of different degrees.
926 * , anisotropic_threshold_ratio(3.)
927 * , anisotropic(anisotropic)
930 * As beta is a linear function, we can choose the degree of the
931 * quadrature
for which the resulting integration is correct. Thus, we
932 * choose to use <code>degree+1</code> Gauss points, which enables us to
933 * integrate exactly polynomials of degree <code>2*degree+1</code>, enough
934 *
for all the integrals we will perform in
this program.
937 * , quadrature(degree + 1)
938 * , face_quadrature(degree + 1)
945 *
void DGMethod<dim>::setup_system()
947 * dof_handler.distribute_dofs(fe);
948 * sparsity_pattern.
reinit(dof_handler.n_dofs(),
949 * dof_handler.n_dofs(),
953 * fe.n_dofs_per_cell());
957 * sparsity_pattern.compress();
959 * system_matrix.reinit(sparsity_pattern);
961 * solution2.reinit(dof_handler.n_dofs());
962 * right_hand_side.reinit(dof_handler.n_dofs());
969 * <a name=
"step_30-Functionassemble_system"></a>
970 * <h4>
Function: assemble_system</h4>
974 * We proceed with the <code>assemble_system</code> function that implements
975 * the DG discretization. This function does the same thing as the
976 * <code>assemble_system</code> function from @ref step_12
"step-12" (but without
977 *
MeshWorker). The four cases considered
for the neighbor-relations of a
978 * cell are the same as the isotropic
case, namely a) cell is at the
979 * boundary,
b) there are finer neighboring cells, c) the neighbor is
980 * neither coarser nor finer and
d) the neighbor is coarser. However, the
981 * way in which we decide upon which
case we have are modified in the way
982 * described in the introduction.
986 *
void DGMethod<dim>::assemble_system()
988 *
const unsigned int dofs_per_cell = dof_handler.get_fe().n_dofs_per_cell();
989 * std::vector<types::global_dof_index> dofs(dofs_per_cell);
990 * std::vector<types::global_dof_index> dofs_neighbor(dofs_per_cell);
1002 *
FEValues<dim> fe_v(mapping, fe, quadrature, update_flags);
1006 * face_update_flags);
1010 * face_update_flags);
1014 * neighbor_face_update_flags);
1025 *
for (
const auto &cell : dof_handler.active_cell_iterators())
1030 * fe_v.reinit(cell);
1032 * dg.assemble_cell_term(fe_v, ui_vi_matrix, cell_vector);
1034 * cell->get_dof_indices(dofs);
1036 *
for (
const auto face_no : cell->face_indices())
1038 * const auto face = cell->face(face_no);
1042 * Case (a): The face is at the boundary.
1045 * if (face->at_boundary())
1047 * fe_v_face.
reinit(cell, face_no);
1049 * dg.assemble_boundary_term(fe_v_face, ui_vi_matrix, cell_vector);
1054 * ExcInternalError());
1055 *
const auto neighbor = cell->neighbor(face_no);
1059 * Case (b): This is an
internal face and the neighbor
1060 * is refined (which we can test by asking whether the
1061 * face of the current cell has children). In this
1062 * case, we will need to integrate over the
1063 *
"sub-faces", i.
e., the children of the face of the
1068 * (There is a slightly confusing corner case: If we
1069 * are in 1
d -- where admittedly the current program
1070 * and its demonstration of anisotropic refinement is
1071 * not particularly relevant -- then the faces between
1072 * cells are
always the same: they are just
1073 *
vertices. In other words, in 1
d, we do not want to
1074 * treat faces between cells of different
level
1075 * differently. The condition `face->has_children()`
1076 * we
check here ensures this: in 1
d, this function
1077 *
always returns `false`, and consequently in 1
d we
1078 * will not ever go into this `if` branch. But we will
1079 * have to come back to this corner case below in case
1083 * if (face->has_children())
1087 * We need to know, which of the neighbors faces points in
1088 * the direction of our cell. Using the @p
1089 * neighbor_face_no function we get this information for
1090 * both coarser and non-coarser neighbors.
1093 * const unsigned
int neighbor2 =
1094 * cell->neighbor_face_no(face_no);
1098 * Now we
loop over all subfaces, i.e. the children and
1099 * possibly grandchildren of the current face.
1102 *
for (
unsigned int subface_no = 0;
1103 * subface_no < face->n_active_descendants();
1108 * To get the cell behind the current subface we can
1109 * use the @p neighbor_child_on_subface function. it
1110 * takes care of all the complicated situations of
1111 * anisotropic refinement and non-standard faces.
1114 *
const auto neighbor_child =
1115 * cell->neighbor_child_on_subface(face_no, subface_no);
1116 *
Assert(!neighbor_child->has_children(),
1117 * ExcInternalError());
1121 * The remaining part of
this case is unchanged.
1128 * fe_v_subface.reinit(cell, face_no, subface_no);
1129 * fe_v_face_neighbor.reinit(neighbor_child, neighbor2);
1131 * dg.assemble_face_term(fe_v_subface,
1132 * fe_v_face_neighbor,
1138 * neighbor_child->get_dof_indices(dofs_neighbor);
1140 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1141 *
for (
unsigned int j = 0; j < dofs_per_cell; ++j)
1143 * system_matrix.add(dofs[i],
1145 * ue_vi_matrix(i, j));
1146 * system_matrix.add(dofs_neighbor[i],
1148 * ui_ve_matrix(i, j));
1149 * system_matrix.add(dofs_neighbor[i],
1151 * ue_ve_matrix(i, j));
1159 * Case (c). We get here
if this is an
internal
1160 * face and
if the neighbor is not further refined
1161 * (or, as mentioned above, we are in 1
d in which
1162 *
case we get here
for every
internal face). We
1163 * then need to decide whether we want to
1164 * integrate over the current face. If the
1165 * neighbor is in fact coarser, then we ignore the
1166 * face and instead handle it when we visit the
1167 * neighboring cell and look at the current face
1168 * (except in 1d, where as mentioned above
this is
1172 *
if (dim > 1 && cell->neighbor_is_coarser(face_no))
1177 * On the other hand,
if the neighbor is more
1178 * refined, then we have already handled the face
1179 * in
case (b) above (except in 1d). So
for 2
d and
1180 * 3
d, we just have to decide whether we want to
1181 * handle a face between cells at the same
level
1182 * from the current side or from the neighboring
1183 * side. We
do this by introducing a tie-breaker:
1184 * We
'll just take the cell with the smaller index
1185 * (within the current refinement level). In 1d,
1186 * we take either the coarser cell, or if they are
1187 * on the same level, the one with the smaller
1188 * index within that level. This leads to a
1189 * complicated condition that, hopefully, makes
1190 * sense given the description above:
1193 * if (((dim > 1) && (cell->index() < neighbor->index())) ||
1194 * ((dim == 1) && ((cell->level() < neighbor->level()) ||
1195 * ((cell->level() == neighbor->level()) &&
1196 * (cell->index() < neighbor->index())))))
1200 * Here we know, that the neighbor is not coarser so we
1201 * can use the usual @p neighbor_of_neighbor
1202 * function. However, we could also use the more
1203 * general @p neighbor_face_no function.
1206 * const unsigned int neighbor2 =
1207 * cell->neighbor_of_neighbor(face_no);
1213 * fe_v_face.reinit(cell, face_no);
1214 * fe_v_face_neighbor.reinit(neighbor, neighbor2);
1216 * dg.assemble_face_term(fe_v_face,
1217 * fe_v_face_neighbor,
1223 * neighbor->get_dof_indices(dofs_neighbor);
1225 * for (unsigned int i = 0; i < dofs_per_cell; ++i)
1226 * for (unsigned int j = 0; j < dofs_per_cell; ++j)
1228 * system_matrix.add(dofs[i],
1230 * ue_vi_matrix(i, j));
1231 * system_matrix.add(dofs_neighbor[i],
1233 * ui_ve_matrix(i, j));
1234 * system_matrix.add(dofs_neighbor[i],
1236 * ue_ve_matrix(i, j));
1242 * We do not need to consider a case (d), as those
1243 * faces are treated 'from the other side within
1251 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1252 *
for (
unsigned int j = 0; j < dofs_per_cell; ++j)
1253 * system_matrix.add(dofs[i], dofs[j], ui_vi_matrix(i, j));
1255 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
1256 * right_hand_side(dofs[i]) += cell_vector(i);
1264 * <a name=
"step_30-Solver"></a>
1269 * For
this simple problem we use the simple Richardson iteration again. The
1270 * solver is completely unaffected by our anisotropic changes.
1273 *
template <
int dim>
1281 * preconditioner.
initialize(system_matrix, fe.n_dofs_per_cell());
1283 * solver.solve(system_matrix, solution, right_hand_side, preconditioner);
1290 * <a name=
"step_30-Refinement"></a>
1291 * <h3>Refinement</h3>
1295 * We
refine the grid according to the same simple refinement criterion used
1296 * in @ref step_12
"step-12", namely an approximation to the
gradient of the solution.
1299 *
template <
int dim>
1300 *
void DGMethod<dim>::refine_grid()
1312 * gradient_indicator);
1316 * and
scale it to obtain an error indicator.
1319 *
for (
const auto &cell :
triangulation.active_cell_iterators())
1320 * gradient_indicator[cell->active_cell_index()] *=
1324 * Then we use
this indicator to flag the 30 percent of the cells with
1325 * highest error indicator to be refined.
1329 * gradient_indicator,
1334 * Now the refinement flags are set
for those cells with a large error
1335 * indicator. If
nothing is done to change
this, those cells will be
1336 * refined isotropically. If the @p anisotropic flag given to
this
1337 * function is set, we now call the set_anisotropic_flags() function,
1338 * which uses the jump indicator to reset some of the refinement flags to
1339 * anisotropic refinement.
1343 * set_anisotropic_flags();
1346 * Now execute the refinement considering anisotropic as well as isotropic
1355 * Once an error indicator has been evaluated and the cells with largest
1356 * error are flagged for refinement we want to loop over the flagged cells
1357 * again to decide whether they need isotropic refinement or whether
1358 * anisotropic refinement is more appropriate. This is the anisotropic jump
1359 * indicator explained in the introduction.
1362 * template <
int dim>
1363 *
void DGMethod<dim>::set_anisotropic_flags()
1367 * We want to evaluate the jump over faces of the flagged cells, so we
1368 * need some objects to evaluate
values of the solution on faces.
1377 * face_update_flags);
1381 * face_update_flags);
1389 * Now we need to
loop over all active cells.
1392 *
for (
const auto &cell : dof_handler.active_cell_iterators())
1395 * We only need to consider cells which are flagged for refinement.
1398 * if (cell->refine_flag_set())
1400 *
std::array<double, dim> jump_in_coordinate_direction;
1401 * std::array<double, dim> face_area_in_coordinate_direction;
1403 *
for (
const auto face_no : cell->face_indices())
1405 * const auto face = cell->face(face_no);
1407 *
if (!face->at_boundary())
1409 *
Assert(cell->neighbor(face_no).state() ==
1411 * ExcInternalError());
1412 *
const auto neighbor = cell->neighbor(face_no);
1414 * std::vector<double> u(fe_v_face.n_quadrature_points);
1415 * std::vector<double> u_neighbor(fe_v_face.n_quadrature_points);
1419 * The four cases of different neighbor relations seen in
1420 * the assembly routines are repeated much in the same way
1424 *
if (face->has_children())
1428 * The neighbor is refined. First we store the
1429 * information, which of the neighbor
's faces points in
1430 * the direction of our current cell. This property is
1431 * inherited to the children.
1434 * unsigned int neighbor2 = cell->neighbor_face_no(face_no);
1437 * Now we loop over all subfaces,
1440 * for (unsigned int subface_no = 0;
1441 * subface_no < face->n_active_descendants();
1446 * get an iterator pointing to the cell behind the
1447 * present subface...
1450 * const auto neighbor_child =
1451 * cell->neighbor_child_on_subface(face_no,
1453 * Assert(!neighbor_child->has_children(),
1454 * ExcInternalError());
1457 * ... and reinit the respective FEFaceValues and
1458 * FESubFaceValues objects.
1461 * fe_v_subface.reinit(cell, face_no, subface_no);
1462 * fe_v_face_neighbor.reinit(neighbor_child, neighbor2);
1465 * We obtain the function values
1468 * fe_v_subface.get_function_values(solution2, u);
1469 * fe_v_face_neighbor.get_function_values(solution2,
1473 * as well as the quadrature weights, multiplied by
1474 * the Jacobian determinant.
1477 * const std::vector<double> &JxW =
1478 * fe_v_subface.get_JxW_values();
1481 * Now we loop over all quadrature points
1484 * for (unsigned int x = 0;
1485 * x < fe_v_subface.n_quadrature_points;
1490 * and integrate the absolute value of the jump
1491 * of the solution, i.e. the absolute value of
1492 * the difference between the function value
1493 * seen from the current cell and the
1494 * neighboring cell, respectively. We know, that
1495 * the first two faces are orthogonal to the
1496 * first coordinate direction on the unit cell,
1497 * the second two faces are orthogonal to the
1498 * second coordinate direction and so on, so we
1499 * accumulate these values into vectors with
1500 * <code>dim</code> components.
1503 * jump_in_coordinate_direction[face_no / 2] +=
1504 * std::abs(u[x] - u_neighbor[x]) * JxW[x];
1507 * We also sum up the scaled weights to obtain
1508 * the measure of the face.
1511 * face_area_in_coordinate_direction[face_no / 2] +=
1518 * if (!cell->neighbor_is_coarser(face_no))
1522 * Our current cell and the neighbor have the same
1523 * refinement along the face under
1524 * consideration. Apart from that, we do much the
1525 * same as with one of the subcells in the above
1529 * unsigned int neighbor2 =
1530 * cell->neighbor_of_neighbor(face_no);
1532 * fe_v_face.reinit(cell, face_no);
1533 * fe_v_face_neighbor.reinit(neighbor, neighbor2);
1535 * fe_v_face.get_function_values(solution2, u);
1536 * fe_v_face_neighbor.get_function_values(solution2,
1539 * const std::vector<double> &JxW =
1540 * fe_v_face.get_JxW_values();
1542 * for (unsigned int x = 0;
1543 * x < fe_v_face.n_quadrature_points;
1546 * jump_in_coordinate_direction[face_no / 2] +=
1547 * std::abs(u[x] - u_neighbor[x]) * JxW[x];
1548 * face_area_in_coordinate_direction[face_no / 2] +=
1552 * else // i.e. neighbor is coarser than cell
1556 * Now the neighbor is actually coarser. This case
1557 * is new, in that it did not occur in the assembly
1558 * routine. Here, we have to consider it, but this
1559 * is not overly complicated. We simply use the @p
1560 * neighbor_of_coarser_neighbor function, which
1561 * again takes care of anisotropic refinement and
1562 * non-standard face orientation by itself.
1565 * std::pair<unsigned int, unsigned int>
1566 * neighbor_face_subface =
1567 * cell->neighbor_of_coarser_neighbor(face_no);
1568 * Assert(neighbor_face_subface.first < cell->n_faces(),
1569 * ExcInternalError());
1570 * Assert(neighbor_face_subface.second <
1571 * neighbor->face(neighbor_face_subface.first)
1572 * ->n_active_descendants(),
1573 * ExcInternalError());
1574 * Assert(neighbor->neighbor_child_on_subface(
1575 * neighbor_face_subface.first,
1576 * neighbor_face_subface.second) == cell,
1577 * ExcInternalError());
1579 * fe_v_face.reinit(cell, face_no);
1580 * fe_v_subface.reinit(neighbor,
1581 * neighbor_face_subface.first,
1582 * neighbor_face_subface.second);
1584 * fe_v_face.get_function_values(solution2, u);
1585 * fe_v_subface.get_function_values(solution2,
1588 * const std::vector<double> &JxW =
1589 * fe_v_face.get_JxW_values();
1591 * for (unsigned int x = 0;
1592 * x < fe_v_face.n_quadrature_points;
1595 * jump_in_coordinate_direction[face_no / 2] +=
1596 * std::abs(u[x] - u_neighbor[x]) * JxW[x];
1597 * face_area_in_coordinate_direction[face_no / 2] +=
1606 * Now we analyze the size of the mean jumps, which we get dividing
1607 * the jumps by the measure of the respective faces.
1610 * std::array<double, dim> average_jumps;
1611 * double sum_of_average_jumps = 0.;
1612 * for (unsigned int i = 0; i < dim; ++i)
1614 * average_jumps[i] = jump_in_coordinate_direction[i] /
1615 * face_area_in_coordinate_direction[i];
1616 * sum_of_average_jumps += average_jumps[i];
1621 * Now we loop over the <code>dim</code> coordinate directions of
1622 * the unit cell and compare the average jump over the faces
1623 * orthogonal to that direction with the average jumps over faces
1624 * orthogonal to the remaining direction(s). If the first is larger
1625 * than the latter by a given factor, we refine only along hat
1626 * axis. Otherwise we leave the refinement flag unchanged, resulting
1627 * in isotropic refinement.
1630 * for (unsigned int i = 0; i < dim; ++i)
1631 * if (average_jumps[i] > anisotropic_threshold_ratio *
1632 * (sum_of_average_jumps - average_jumps[i]))
1633 * cell->set_refine_flag(RefinementCase<dim>::cut_axis(i));
1640 * <a name="step_30-TheRest"></a>
1645 * The remaining part of the program very much follows the scheme of
1646 * previous tutorial programs. We output the mesh in VTU format (just
1647 * as we did in @ref step_1 "step-1", for example), and the visualization output
1648 * in VTU format as we almost always do.
1651 * template <int dim>
1652 * void DGMethod<dim>::output_results(const unsigned int cycle) const
1654 * std::string refine_type;
1656 * refine_type = ".aniso";
1658 * refine_type = ".iso";
1661 * const std::string filename =
1662 * "grid-" + std::to_string(cycle) + refine_type + ".svg";
1663 * std::cout << " Writing grid to <" << filename << ">..." << std::endl;
1664 * std::ofstream svg_output(filename);
1667 * grid_out.write_svg(triangulation, svg_output);
1671 * const std::string filename =
1672 * "sol-" + std::to_string(cycle) + refine_type + ".vtu";
1673 * std::cout << " Writing solution to <" << filename << ">..."
1675 * std::ofstream gnuplot_output(filename);
1677 * DataOut<dim> data_out;
1678 * data_out.attach_dof_handler(dof_handler);
1679 * data_out.add_data_vector(solution2, "u");
1681 * data_out.build_patches(degree);
1683 * data_out.write_vtu(gnuplot_output);
1689 * template <int dim>
1690 * void DGMethod<dim>::run()
1692 * for (unsigned int cycle = 0; cycle < 6; ++cycle)
1694 * std::cout << "Cycle " << cycle << ':
' << std::endl;
1700 * Create the rectangular domain.
1703 * Point<dim> p1, p2;
1705 * for (unsigned int i = 0; i < dim; ++i)
1709 * Adjust the number of cells in different directions to obtain
1710 * completely isotropic cells for the original mesh.
1713 * std::vector<unsigned int> repetitions(dim, 1);
1714 * repetitions[0] = 2;
1715 * GridGenerator::subdivided_hyper_rectangle(triangulation,
1720 * triangulation.refine_global(5 - dim);
1726 * std::cout << " Number of active cells: "
1727 * << triangulation.n_active_cells() << std::endl;
1731 * std::cout << " Number of degrees of freedom: " << dof_handler.n_dofs()
1734 * Timer assemble_timer;
1735 * assemble_system();
1736 * std::cout << " Time of assemble_system: " << assemble_timer.cpu_time()
1740 * output_results(cycle);
1742 * std::cout << std::endl;
1745 * } // namespace Step30
1753 * using namespace Step30;
1757 * If you want to run the program in 3d, simply change the following
1758 * line to <code>const unsigned int dim = 3;</code>.
1761 * const unsigned int dim = 2;
1766 * First, we perform a run with isotropic refinement.
1769 * std::cout << "Performing a " << dim
1770 * << "D run with isotropic refinement..." << std::endl
1771 * << "------------------------------------------------"
1773 * DGMethod<dim> dgmethod_iso(false);
1774 * dgmethod_iso.run();
1780 * Now we do a second run, this time with anisotropic refinement.
1783 * std::cout << std::endl
1784 * << "Performing a " << dim
1785 * << "D run with anisotropic refinement..." << std::endl
1786 * << "--------------------------------------------------"
1788 * DGMethod<dim> dgmethod_aniso(true);
1789 * dgmethod_aniso.run();
1792 * catch (std::exception &exc)
1794 * std::cerr << std::endl
1796 * << "----------------------------------------------------"
1798 * std::cerr << "Exception on processing: " << std::endl
1799 * << exc.what() << std::endl
1800 * << "Aborting!" << std::endl
1801 * << "----------------------------------------------------"
1807 * std::cerr << std::endl
1809 * << "----------------------------------------------------"
1811 * std::cerr << "Unknown exception!" << std::endl
1812 * << "Aborting!" << std::endl
1813 * << "----------------------------------------------------"
1821<a name="step_30-Results"></a><h1>Results</h1>
1825The output of this program consist of the console output, the SVG
1826files containing the grids, and the solutions given in VTU format.
1828Performing a 2D run with isotropic refinement...
1829------------------------------------------------
1831 Number of active cells: 128
1832 Number of degrees of freedom: 512
1833 Time of assemble_system: 0.092049
1834 Writing grid to <grid-0.iso.svg>...
1835 Writing solution to <sol-0.iso.vtu>...
1838 Number of active cells: 239
1839 Number of degrees of freedom: 956
1840 Time of assemble_system: 0.109519
1841 Writing grid to <grid-1.iso.svg>...
1842 Writing solution to <sol-1.iso.vtu>...
1845 Number of active cells: 491
1846 Number of degrees of freedom: 1964
1847 Time of assemble_system: 0.08303
1848 Writing grid to <grid-2.iso.svg>...
1849 Writing solution to <sol-2.iso.vtu>...
1852 Number of active cells: 1031
1853 Number of degrees of freedom: 4124
1854 Time of assemble_system: 0.278987
1855 Writing grid to <grid-3.iso.svg>...
1856 Writing solution to <sol-3.iso.vtu>...
1859 Number of active cells: 2027
1860 Number of degrees of freedom: 8108
1861 Time of assemble_system: 0.305869
1862 Writing grid to <grid-4.iso.svg>...
1863 Writing solution to <sol-4.iso.vtu>...
1866 Number of active cells: 4019
1867 Number of degrees of freedom: 16076
1868 Time of assemble_system: 0.47616
1869 Writing grid to <grid-5.iso.svg>...
1870 Writing solution to <sol-5.iso.vtu>...
1873Performing a 2D run with anisotropic refinement...
1874--------------------------------------------------
1876 Number of active cells: 128
1877 Number of degrees of freedom: 512
1878 Time of assemble_system: 0.052866
1879 Writing grid to <grid-0.aniso.svg>...
1880 Writing solution to <sol-0.aniso.vtu>...
1883 Number of active cells: 171
1884 Number of degrees of freedom: 684
1885 Time of assemble_system: 0.050917
1886 Writing grid to <grid-1.aniso.svg>...
1887 Writing solution to <sol-1.aniso.vtu>...
1890 Number of active cells: 255
1891 Number of degrees of freedom: 1020
1892 Time of assemble_system: 0.064132
1893 Writing grid to <grid-2.aniso.svg>...
1894 Writing solution to <sol-2.aniso.vtu>...
1897 Number of active cells: 394
1898 Number of degrees of freedom: 1576
1899 Time of assemble_system: 0.119849
1900 Writing grid to <grid-3.aniso.svg>...
1901 Writing solution to <sol-3.aniso.vtu>...
1904 Number of active cells: 648
1905 Number of degrees of freedom: 2592
1906 Time of assemble_system: 0.218244
1907 Writing grid to <grid-4.aniso.svg>...
1908 Writing solution to <sol-4.aniso.vtu>...
1911 Number of active cells: 1030
1912 Number of degrees of freedom: 4120
1913 Time of assemble_system: 0.128121
1914 Writing grid to <grid-5.aniso.svg>...
1915 Writing solution to <sol-5.aniso.vtu>...
1918This text output shows the reduction in the number of cells which results from
1919the successive application of anisotropic refinement. After the last refinement
1920step the savings have accumulated so much that almost four times as many cells
1921and thus degrees of freedom are needed in the isotropic case. The time needed for assembly
1922scales with a similar factor.
1924The first interesting part is of course to see how the meshes look like.
1925On the left are the isotropically refined ones, on the right the
1926anisotropic ones (colors indicate the refinement level of cells):
1928<table width="80%" align="center">
1931 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-0.iso.9.2.png" alt="">
1934 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-0.aniso.9.2.png" alt="">
1939 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-1.iso.9.2.png" alt="">
1942 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-1.aniso.9.2.png" alt="">
1947 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-2.iso.9.2.png" alt="">
1950 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-2.aniso.9.2.png" alt="">
1955 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-3.iso.9.2.png" alt="">
1958 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-3.aniso.9.2.png" alt="">
1963 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-4.iso.9.2.png" alt="">
1966 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-4.aniso.9.2.png" alt="">
1971 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-5.iso.9.2.png" alt="">
1974 <img src="https://www.dealii.org/images/steps/developer/step-30.grid-5.aniso.9.2.png" alt="">
1980The other interesting thing is, of course, to see the solution on these
1981two sequences of meshes. Here they are, on the refinement cycles 1 and 4,
1982clearly showing that the solution is indeed composed of <i>discontinuous</i> piecewise
1985<table width="60%" align="center">
1988 <img src="https://www.dealii.org/images/steps/developer/step-30.sol-1.iso.9.2.png" alt="">
1991 <img src="https://www.dealii.org/images/steps/developer/step-30.sol-1.aniso.9.2.png" alt="">
1996 <img src="https://www.dealii.org/images/steps/developer/step-30.sol-4.iso.9.2.png" alt="">
1999 <img src="https://www.dealii.org/images/steps/developer/step-30.sol-4.aniso.9.2.png" alt="">
2004We see, that the solution on the anisotropically refined mesh is very similar to
2005the solution obtained on the isotropically refined mesh. Thus the anisotropic
2006indicator seems to effectively select the appropriate cells for anisotropic
2009The pictures also explain why the mesh is refined as it is.
2010In the whole left part of the domain refinement is only performed along the
2011@f$y@f$-axis of cells. In the right part of the domain the refinement is dominated by
2012isotropic refinement, as the anisotropic feature of the solution - the jump from
2013one to zero - is not well aligned with the mesh where the advection direction
2014takes a turn. However, at the bottom and closest (to the observer) parts of the
2015quarter circle this jumps again becomes more and more aligned
2016with the mesh and the refinement algorithm reacts by creating anisotropic cells
2017of increasing aspect ratio.
2019It might seem that the necessary alignment of anisotropic features and the
2020coarse mesh can decrease performance significantly for real world
2021problems. That is not wrong in general: If one were, for example, to apply
2022anisotropic refinement to problems in which shocks appear (e.g., the
2023equations solved in @ref step_69 "step-69"), then it many cases the shock is not aligned
2024with the mesh and anisotropic refinement will help little unless one also
2025introduces techniques to move the mesh in alignment with the shocks.
2026On the other hand, many steep features of solutions are due to boundary layers.
2027In those cases, the mesh is already aligned with the anisotropic features
2028because it is of course aligned with the boundary itself, and anisotropic
2029refinement will almost always increase the efficiency of computations on
2030adapted grids for these cases.
2033<a name="step_30-PlainProg"></a>
2034<h1> The plain program</h1>
2035@include "step-30.cc"
const std::vector< double > & get_JxW_values() const
virtual void value_list(const std::vector< Point< dim > > &points, std::vector< RangeNumberType > &values, const unsigned int component=0) const
void initialize(const MatrixType &A, const AdditionalData parameters)
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
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())
void make_flux_sparsity_pattern(const DoFHandler< dim, spacedim > &dof_handler, SparsityPatternBase &sparsity_pattern)
@ 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 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)
void approximate_gradient(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const InputVector &solution, Vector< float > &derivative_norm, const unsigned int component=0)
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void refine_and_coarsen_fixed_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())
@ valid
Iterator points to a valid object.
Point< spacedim > point(const gp_Pnt &p, const double tolerance=1e-10)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
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)
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)
bool check(const ConstraintKinds kind_in, const unsigned int dim)
int(&) functions(const void *v1, const void *v2)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation