]> https://gitweb.dealii.org/ - dealii.git/commitdiff
step-41: C++ modernization. 8100/head
authorDavid Wells <drwells@email.unc.edu>
Sat, 11 May 2019 18:14:53 +0000 (14:14 -0400)
committerDavid Wells <drwells@email.unc.edu>
Sun, 12 May 2019 02:58:23 +0000 (22:58 -0400)
1. Avoid including <list> (we don't use it)
2. Inline function definitions
3. Use ranged-for loops

examples/step-41/step-41.cc

index f2e5ec11716be384d5e867182dc5085e1b2d7653..e1f5c1f97958a7412d9adb9996e55e97b57dcb12 100644 (file)
@@ -55,7 +55,6 @@
 
 #include <fstream>
 #include <iostream>
-#include <list>
 
 
 namespace Step41
@@ -126,19 +125,15 @@ namespace Step41
       : Function<dim>()
     {}
 
-    virtual double value(const Point<dim> & p,
-                         const unsigned int component = 0) const override;
-  };
-
-  template <int dim>
-  double RightHandSide<dim>::value(const Point<dim> &,
-                                   const unsigned int component) const
-  {
-    (void)component;
-    Assert(component == 0, ExcIndexRange(component, 0, 1));
+    virtual double value(const Point<dim> & /*p*/,
+                         const unsigned int component = 0) const override
+    {
+      (void)component;
+      AssertIndexRange(component, 1);
 
-    return -10;
-  }
+      return -10;
+    }
+  };
 
 
 
@@ -150,19 +145,15 @@ namespace Step41
       : Function<dim>()
     {}
 
-    virtual double value(const Point<dim> & p,
-                         const unsigned int component = 0) const override;
-  };
-
-  template <int dim>
-  double BoundaryValues<dim>::value(const Point<dim> &,
-                                    const unsigned int component) const
-  {
-    (void)component;
-    Assert(component == 0, ExcIndexRange(component, 0, 1));
+    virtual double value(const Point<dim> & /*p*/,
+                         const unsigned int component = 0) const override
+    {
+      (void)component;
+      AssertIndexRange(component, 1);
 
-    return 0;
-  }
+      return 0;
+    }
+  };
 
 
 
@@ -177,26 +168,22 @@ namespace Step41
     {}
 
     virtual double value(const Point<dim> & p,
-                         const unsigned int component = 0) const override;
+                         const unsigned int component = 0) const override
+    {
+      (void)component;
+      Assert(component == 0, ExcIndexRange(component, 0, 1));
+
+      if (p(0) < -0.5)
+        return -0.2;
+      else if (p(0) >= -0.5 && p(0) < 0.0)
+        return -0.4;
+      else if (p(0) >= 0.0 && p(0) < 0.5)
+        return -0.6;
+      else
+        return -0.8;
+    }
   };
 
-  template <int dim>
-  double Obstacle<dim>::value(const Point<dim> & p,
-                              const unsigned int component) const
-  {
-    (void)component;
-    Assert(component == 0, ExcIndexRange(component, 0, 1));
-
-    if (p(0) < -0.5)
-      return -0.2;
-    else if (p(0) >= -0.5 && p(0) < 0.0)
-      return -0.4;
-    else if (p(0) >= 0.0 && p(0) < 0.5)
-      return -0.6;
-    else
-      return -0.8;
-  }
-
 
 
   // @sect3{Implementation of the <code>ObstacleProblem</code> class}
@@ -309,11 +296,7 @@ namespace Step41
 
     std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
 
-    typename DoFHandler<dim>::active_cell_iterator cell =
-                                                     dof_handler.begin_active(),
-                                                   endc = dof_handler.end();
-
-    for (; cell != endc; ++cell)
+    for (const auto &cell : dof_handler.active_cell_iterators())
       {
         fe_values.reinit(cell);
         cell_matrix = 0;
@@ -385,11 +368,7 @@ namespace Step41
     FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
     std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
 
-    typename DoFHandler<dim>::active_cell_iterator cell =
-                                                     dof_handler.begin_active(),
-                                                   endc = dof_handler.end();
-
-    for (; cell != endc; ++cell)
+    for (const auto &cell : dof_handler.active_cell_iterators())
       {
         fe_values.reinit(cell);
         cell_matrix = 0;
@@ -475,10 +454,7 @@ namespace Step41
     const Obstacle<dim> obstacle;
     std::vector<bool>   dof_touched(dof_handler.n_dofs(), false);
 
-    typename DoFHandler<dim>::active_cell_iterator cell =
-                                                     dof_handler.begin_active(),
-                                                   endc = dof_handler.end();
-    for (; cell != endc; ++cell)
+    for (const auto &cell : dof_handler.active_cell_iterators())
       for (unsigned int v = 0; v < GeometryInfo<dim>::vertices_per_cell; ++v)
         {
           Assert(dof_handler.get_fe().dofs_per_cell ==
@@ -596,7 +572,7 @@ namespace Step41
 
     data_out.build_patches();
 
-    std::ofstream output_vtk(std::string("output_") +
+    std::ofstream output_vtk("output_" +
                              Utilities::int_to_string(iteration, 3) + ".vtk");
     data_out.write_vtk(output_vtk);
   }

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.