From c8795df6c25f07c39d66d4f7fdf5816bef7bc085 Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Mon, 8 Jun 2020 23:05:42 +0200
Subject: [PATCH] Use STL algorithms instead of raw for-loops.

---
 GeoLib/Polygon.cpp                            | 25 ++++++------
 MathLib/LinAlg/Eigen/EigenVector.h            |  5 +--
 MathLib/LinAlg/Lis/LisMatrix.h                |  3 +-
 MeshGeoToolsLib/BoundaryElementsSearcher.cpp  | 39 ++++++++++++-------
 MeshGeoToolsLib/MeshNodeSearcher.cpp          | 33 +++++++++-------
 NumLib/DOF/ComputeSparsityPattern.cpp         | 12 +++---
 NumLib/DOF/LocalToGlobalIndexMap.cpp          | 10 ++---
 NumLib/DOF/MeshComponentMap.cpp               |  6 +--
 .../LocalLinearLeastSquaresExtrapolator.cpp   |  7 ++--
 ProcessLib/HydroMechanics/HydroMechanicsFEM.h |  6 +--
 ProcessLib/LIE/Common/LevelSetFunction.cpp    | 13 +++----
 ProcessLib/LIE/Common/MeshUtils.cpp           | 28 ++++++-------
 .../SmallDeformationProcess.cpp               | 13 ++++---
 ProcessLib/Output/IntegrationPointWriter.cpp  | 21 +++++-----
 ProcessLib/Process.cpp                        |  8 ++--
 ProcessLib/ProcessVariable.cpp                | 12 +++---
 .../SmallDeformation/SmallDeformationFEM.h    |  7 ++--
 .../SmallDeformationNonlocalFEM.h             | 19 ++++-----
 ProcessLib/TES/TESLocalAssembler-impl.h       |  5 +--
 ProcessLib/VectorMatrixAssembler.cpp          | 18 ++++-----
 Tests/MeshGeoToolsLib/TestGeoMapper.cpp       |  7 ++--
 Tests/MeshLib/TestMeshGenerator.cpp           | 17 ++++----
 22 files changed, 157 insertions(+), 157 deletions(-)

diff --git a/GeoLib/Polygon.cpp b/GeoLib/Polygon.cpp
index b02580b6d2a..e9be778dc81 100644
--- a/GeoLib/Polygon.cpp
+++ b/GeoLib/Polygon.cpp
@@ -230,20 +230,17 @@ bool Polygon::isPartOfPolylineInPolygon(const Polyline& ply) const
         }
     }
 
-    GeoLib::Point s;
-    for (auto polygon_seg : *this)
-    {
-        if (std::any_of(ply.begin(), ply.end(),
-                        [&polygon_seg, &s](auto const& polyline_seg) {
-                            return GeoLib::lineSegmentIntersect(polyline_seg,
-                                                                polygon_seg, s);
-                        }))
-        {
-            return true;
-        }
-    }
-
-    return false;
+    auto polygon_segment_intersects_line = [&](auto const& polygon_seg) {
+        GeoLib::Point s;
+        return std::any_of(ply.begin(), ply.end(),
+                           [&polygon_seg, &s](auto const& polyline_seg) {
+                               return GeoLib::lineSegmentIntersect(
+                                   polyline_seg, polygon_seg, s);
+                           });
+    };
+
+    return any_of(std::cbegin(*this), std::cend(*this),
+                  polygon_segment_intersects_line);
 }
 
 bool Polygon::getNextIntersectionPointPolygonLine(
diff --git a/MathLib/LinAlg/Eigen/EigenVector.h b/MathLib/LinAlg/Eigen/EigenVector.h
index a8a1380328c..af7fc4bcd84 100644
--- a/MathLib/LinAlg/Eigen/EigenVector.h
+++ b/MathLib/LinAlg/Eigen/EigenVector.h
@@ -71,9 +71,8 @@ public:
         std::vector<double> local_x;
         local_x.reserve(indices.size());
 
-        for (auto i : indices) {
-            local_x.emplace_back(_vec[i]);
-        }
+        transform(cbegin(indices), cend(indices), back_inserter(local_x),
+                  [&](auto const i) { return _vec[i]; });
 
         return local_x;
     }
diff --git a/MathLib/LinAlg/Lis/LisMatrix.h b/MathLib/LinAlg/Lis/LisMatrix.h
index 163a9b714fd..e4bfbdafbdd 100644
--- a/MathLib/LinAlg/Lis/LisMatrix.h
+++ b/MathLib/LinAlg/Lis/LisMatrix.h
@@ -194,7 +194,8 @@ void operator()(LisMatrix &matrix, SPARSITY_PATTERN const& sparsity_pattern)
     row_sizes.reserve(n_rows);
 
     // LIS needs 1 more entry, otherewise it starts reallocating arrays.
-    for (auto i : sparsity_pattern) row_sizes.push_back(i+1);
+    transform(cbegin(sparsity_pattern), cend(sparsity_pattern),
+              back_inserter(row_sizes), [](auto const i) { return i + 1; });
 
     int ierr = lis_matrix_malloc(matrix._AA, 0, row_sizes.data());
     checkLisError(ierr);
diff --git a/MeshGeoToolsLib/BoundaryElementsSearcher.cpp b/MeshGeoToolsLib/BoundaryElementsSearcher.cpp
index 1616406cb75..05daeeee3cd 100644
--- a/MeshGeoToolsLib/BoundaryElementsSearcher.cpp
+++ b/MeshGeoToolsLib/BoundaryElementsSearcher.cpp
@@ -74,12 +74,15 @@ BoundaryElementsSearcher::getBoundaryElementsAtPoint(
     GeoLib::Point const& point, bool const multiple_nodes_allowed)
 {
     // look for already saved points and return if found.
-    for (auto const& boundaryElements : _boundary_elements_at_point)
+    if (auto const it = find_if(cbegin(_boundary_elements_at_point),
+                                cend(_boundary_elements_at_point),
+                                [&](auto const& boundary_elements) {
+                                    return boundary_elements->getPoint() ==
+                                           point;
+                                });
+        it != cend(_boundary_elements_at_point))
     {
-        if (boundaryElements->getPoint() == point)
-        {
-            return boundaryElements->getBoundaryElements();
-        }
+        return (*it)->getBoundaryElements();
     }
 
     // create new boundary elements at points.
@@ -93,12 +96,15 @@ BoundaryElementsSearcher::getBoundaryElementsAlongPolyline(
     GeoLib::Polyline const& polyline)
 {
     // look for already saved polylines and return if found.
-    for (auto const& boundary_elements : _boundary_elements_along_polylines)
+    if (auto const it = find_if(cbegin(_boundary_elements_along_polylines),
+                                cend(_boundary_elements_along_polylines),
+                                [&](auto const& boundary_elements) {
+                                    return &boundary_elements->getPolyline() ==
+                                           &polyline;
+                                });
+        it != cend(_boundary_elements_along_polylines))
     {
-        if (&boundary_elements->getPolyline() == &polyline)
-        {
-            return boundary_elements->getBoundaryElements();
-        }
+        return (*it)->getBoundaryElements();
     }
 
     // create new boundary elements at points.
@@ -112,12 +118,15 @@ BoundaryElementsSearcher::getBoundaryElementsOnSurface(
     GeoLib::Surface const& surface)
 {
     // look for already saved surfaces and return if found.
-    for (auto const& boundary_elements : _boundary_elements_along_surfaces)
+    if (auto const it = find_if(cbegin(_boundary_elements_along_surfaces),
+                                cend(_boundary_elements_along_surfaces),
+                                [&](auto const& boundary_elements) {
+                                    return &boundary_elements->getSurface() ==
+                                           &surface;
+                                });
+        it != cend(_boundary_elements_along_surfaces))
     {
-        if (&boundary_elements->getSurface() == &surface)
-        {
-            return boundary_elements->getBoundaryElements();
-        }
+        return (*it)->getBoundaryElements();
     }
 
     _boundary_elements_along_surfaces.push_back(
diff --git a/MeshGeoToolsLib/MeshNodeSearcher.cpp b/MeshGeoToolsLib/MeshNodeSearcher.cpp
index 8b60d0da99d..1e8feca4bf0 100644
--- a/MeshGeoToolsLib/MeshNodeSearcher.cpp
+++ b/MeshGeoToolsLib/MeshNodeSearcher.cpp
@@ -152,12 +152,13 @@ std::vector<std::size_t> const& MeshNodeSearcher::getMeshNodeIDsAlongSurface(
 MeshNodesOnPoint& MeshNodeSearcher::getMeshNodesOnPoint(
     GeoLib::Point const& pnt) const
 {
-    for (auto const& mesh_nodes_on_point : _mesh_nodes_on_points)
+    if (auto const it = find_if(
+            cbegin(_mesh_nodes_on_points),
+            cend(_mesh_nodes_on_points),
+            [&](auto const& nodes) { return &(nodes->getPoint()) == &pnt; });
+        it != cend(_mesh_nodes_on_points))
     {
-        if (&(mesh_nodes_on_point->getPoint()) == &pnt)
-        {
-            return *mesh_nodes_on_point;
-        }
+        return **it;
     }
 
     _mesh_nodes_on_points.push_back(
@@ -172,12 +173,13 @@ MeshNodesOnPoint& MeshNodeSearcher::getMeshNodesOnPoint(
 MeshNodesAlongPolyline& MeshNodeSearcher::getMeshNodesAlongPolyline(
     GeoLib::Polyline const& ply) const
 {
-    for (auto const& mesh_nodes_along_polyline : _mesh_nodes_along_polylines)
+    if (auto const it = find_if(
+            cbegin(_mesh_nodes_along_polylines),
+            cend(_mesh_nodes_along_polylines),
+            [&](auto const& nodes) { return &(nodes->getPolyline()) == &ply; });
+        it != cend(_mesh_nodes_along_polylines))
     {
-        if (&(mesh_nodes_along_polyline->getPolyline()) == &ply)
-        {
-            return *mesh_nodes_along_polyline;
-        }
+        return **it;
     }
 
     // compute nodes (and supporting points) along polyline
@@ -190,12 +192,13 @@ MeshNodesAlongPolyline& MeshNodeSearcher::getMeshNodesAlongPolyline(
 MeshNodesAlongSurface& MeshNodeSearcher::getMeshNodesAlongSurface(
     GeoLib::Surface const& sfc) const
 {
-    for (auto const& mesh_nodes_along_surface : _mesh_nodes_along_surfaces)
+    if (auto const it = find_if(
+            cbegin(_mesh_nodes_along_surfaces),
+            cend(_mesh_nodes_along_surfaces),
+            [&](auto const& nodes) { return &(nodes->getSurface()) == &sfc; });
+        it != cend(_mesh_nodes_along_surfaces))
     {
-        if (&(mesh_nodes_along_surface->getSurface()) == &sfc)
-        {
-            return *mesh_nodes_along_surface;
-        }
+        return **it;
     }
 
     // compute nodes (and supporting points) on surface
diff --git a/NumLib/DOF/ComputeSparsityPattern.cpp b/NumLib/DOF/ComputeSparsityPattern.cpp
index 38430a643e7..fe67cc7f30f 100644
--- a/NumLib/DOF/ComputeSparsityPattern.cpp
+++ b/NumLib/DOF/ComputeSparsityPattern.cpp
@@ -10,6 +10,8 @@
 
 #include "ComputeSparsityPattern.h"
 
+#include <numeric>
+
 #include "LocalToGlobalIndexMap.h"
 #include "MeshLib/NodeAdjacencyTable.h"
 
@@ -54,11 +56,11 @@ GlobalSparsityPattern computeSparsityPatternNonPETSc(
     // Map adjacent mesh nodes to "adjacent global indices".
     for (std::size_t n = 0; n < mesh.getNumberOfNodes(); ++n)
     {
-        unsigned n_connected_dof = 0;
-        for (auto an : node_adjacency_table.getAdjacentNodes(n))
-        {
-            n_connected_dof += global_idcs[an].size();
-        }
+        auto const& an = node_adjacency_table.getAdjacentNodes(n);
+        auto const n_connected_dof = std::accumulate(
+            cbegin(an), cend(an), 0, [&](auto const result, auto const i) {
+                return result + global_idcs[i].size();
+            });
         for (auto global_index : global_idcs[n])
         {
             sparsity_pattern[global_index] = n_connected_dof;
diff --git a/NumLib/DOF/LocalToGlobalIndexMap.cpp b/NumLib/DOF/LocalToGlobalIndexMap.cpp
index 83fa3a9f5ec..dffd9c1efe1 100644
--- a/NumLib/DOF/LocalToGlobalIndexMap.cpp
+++ b/NumLib/DOF/LocalToGlobalIndexMap.cpp
@@ -245,11 +245,11 @@ LocalToGlobalIndexMap* LocalToGlobalIndexMap::deriveBoundaryConstrainedMap(
     // Create a subset of the current mesh component map.
     std::vector<int> global_component_ids;
 
-    for (auto component_id : component_ids)
-    {
-        global_component_ids.push_back(
-            getGlobalComponent(variable_id, component_id));
-    }
+    transform(cbegin(component_ids), cend(component_ids),
+              back_inserter(global_component_ids),
+              [&](auto const component_id) {
+                  return getGlobalComponent(variable_id, component_id);
+              });
 
     auto mesh_component_map = _mesh_component_map.getSubset(
         _mesh_subsets, new_mesh_subset, global_component_ids);
diff --git a/NumLib/DOF/MeshComponentMap.cpp b/NumLib/DOF/MeshComponentMap.cpp
index 886d31c9c29..1c3dd6fd358 100644
--- a/NumLib/DOF/MeshComponentMap.cpp
+++ b/NumLib/DOF/MeshComponentMap.cpp
@@ -304,10 +304,8 @@ std::vector<GlobalIndexType> MeshComponentMap::getGlobalIndicesByComponent(
 
     std::vector<GlobalIndexType> global_indices;
     global_indices.reserve(pairs.size());
-    for (const auto& pair : pairs)
-    {
-        global_indices.push_back(pair.second);
-    }
+    transform(cbegin(pairs), cend(pairs), back_inserter(global_indices),
+              [&](const auto& pair) { return pair.second; });
 
     return global_indices;
 }
diff --git a/NumLib/Extrapolation/LocalLinearLeastSquaresExtrapolator.cpp b/NumLib/Extrapolation/LocalLinearLeastSquaresExtrapolator.cpp
index 46096293f72..0de85308805 100644
--- a/NumLib/Extrapolation/LocalLinearLeastSquaresExtrapolator.cpp
+++ b/NumLib/Extrapolation/LocalLinearLeastSquaresExtrapolator.cpp
@@ -259,10 +259,9 @@ void LocalLinearLeastSquaresExtrapolator::extrapolateElement(
         // _nodal_values is ordered location-wise
         for (unsigned comp = 0; comp < num_components; ++comp)
         {
-            for (auto i : global_indices)
-            {
-                indices.push_back(num_components * i + comp);
-            }
+            transform(cbegin(global_indices), cend(global_indices),
+                      back_inserter(indices),
+                      [&](auto const i) { return num_components * i + comp; });
         }
 
         // Nodal_values are passed as a raw pointer, because PETScVector and
diff --git a/ProcessLib/HydroMechanics/HydroMechanicsFEM.h b/ProcessLib/HydroMechanics/HydroMechanicsFEM.h
index a9396825546..8ecade58f5e 100644
--- a/ProcessLib/HydroMechanics/HydroMechanicsFEM.h
+++ b/ProcessLib/HydroMechanics/HydroMechanicsFEM.h
@@ -364,10 +364,8 @@ private:
         cache.clear();
         cache.reserve(_ip_data.size());
 
-        for (auto const& ip_data : _ip_data)
-        {
-            cache.push_back(ip_data.eps[component]);
-        }
+        transform(cbegin(_ip_data), cend(_ip_data), back_inserter(cache),
+                  [&](auto const& ip_data) { return ip_data.eps[component]; });
 
         return cache;
     }
diff --git a/ProcessLib/LIE/Common/LevelSetFunction.cpp b/ProcessLib/LIE/Common/LevelSetFunction.cpp
index 4ec36791d4e..f6bca05d13f 100644
--- a/ProcessLib/LIE/Common/LevelSetFunction.cpp
+++ b/ProcessLib/LIE/Common/LevelSetFunction.cpp
@@ -10,9 +10,9 @@
 #include "LevelSetFunction.h"
 
 #include <boost/math/special_functions/sign.hpp>
+#include <numeric>
 
 #include "BaseLib/Algorithm.h"
-
 #include "BranchProperty.h"
 #include "FractureProperty.h"
 #include "JunctionProperty.h"
@@ -61,12 +61,11 @@ std::vector<double> uGlobalEnrichments(
     for (std::size_t i = 0; i < frac_props.size(); i++)
     {
         auto const* frac = frac_props[i];
-        double enrich = levelsets[i];
-        for (const auto& j : frac->branches_slave)
-        {
-            enrich *= Heaviside(levelsetBranch(j, x));
-        }
-        enrichments[i] = enrich;
+        enrichments[i] = std::accumulate(
+            cbegin(frac->branches_slave), cend(frac->branches_slave),
+            levelsets[i], [&](double const& enrich, auto const& branch) {
+                return enrich * Heaviside(levelsetBranch(branch, x));
+            });
     }
 
     // junctions
diff --git a/ProcessLib/LIE/Common/MeshUtils.cpp b/ProcessLib/LIE/Common/MeshUtils.cpp
index 88919f8d72c..6da22f62f3d 100644
--- a/ProcessLib/LIE/Common/MeshUtils.cpp
+++ b/ProcessLib/LIE/Common/MeshUtils.cpp
@@ -48,14 +48,11 @@ public:
             return false;
         }
 
-        unsigned n_connected_fracture_elements = 0;
-        for (MeshLib::Element const* e : node.getElements())
-        {
-            if (e->getDimension() == _fracture_element_dim)
-            {
-                n_connected_fracture_elements++;
-            }
-        }
+        auto const n_connected_fracture_elements =
+            count_if(cbegin(node.getElements()), cend(node.getElements()),
+                     [&](auto* const e) {
+                         return e->getDimension() == _fracture_element_dim;
+                     });
         assert(n_connected_fracture_elements > 0);
 
         return (n_connected_fracture_elements == 1);
@@ -88,10 +85,8 @@ void findFracutreIntersections(
     std::vector<std::size_t> all_fracture_nodes;
     for (auto& vec : vec_fracture_nodes)
     {
-        for (auto* node : vec)
-        {
-            all_fracture_nodes.push_back(node->getID());
-        }
+        transform(cbegin(vec), cend(vec), back_inserter(all_fracture_nodes),
+                  [](auto* const n) { return n->getID(); });
     }
 
     // create a table of a node id and connected material IDs
@@ -248,10 +243,11 @@ void getFractureMatrixDataInMesh(
     {
         OGS_FATAL("Could not access MaterialIDs property from mesh.");
     }
-    for (MeshLib::Element* e : all_fracture_elements)
-    {
-        vec_fracture_mat_IDs.push_back((*material_ids)[e->getID()]);
-    }
+    transform(
+        cbegin(all_fracture_elements), cend(all_fracture_elements),
+        back_inserter(vec_fracture_mat_IDs),
+        [&material_ids](auto const* e) { return (*material_ids)[e->getID()]; });
+
     BaseLib::makeVectorUnique(vec_fracture_mat_IDs);
     DBUG("-> found {:d} fracture material groups", vec_fracture_mat_IDs.size());
 
diff --git a/ProcessLib/LIE/SmallDeformation/SmallDeformationProcess.cpp b/ProcessLib/LIE/SmallDeformation/SmallDeformationProcess.cpp
index ebf1b472e11..0f020082d33 100644
--- a/ProcessLib/LIE/SmallDeformation/SmallDeformationProcess.cpp
+++ b/ProcessLib/LIE/SmallDeformation/SmallDeformationProcess.cpp
@@ -120,11 +120,14 @@ SmallDeformationProcess<DisplacementDim>::SmallDeformationProcess(
     }
 
     // set junctions
-    for (auto& vec_junction_nodeID_matID : vec_junction_nodeID_matIDs)
-    {
-        _vec_junction_nodes.push_back(const_cast<MeshLib::Node*>(
-            _mesh.getNode(vec_junction_nodeID_matID.first)));
-    }
+    transform(cbegin(vec_junction_nodeID_matIDs),
+              cend(vec_junction_nodeID_matIDs),
+              back_inserter(_vec_junction_nodes),
+              [&](auto& vec_junction_nodeID_matID) {
+                  return const_cast<MeshLib::Node*>(
+                      _mesh.getNode(vec_junction_nodeID_matID.first));
+              });
+
     for (std::size_t i = 0; i < vec_junction_nodeID_matIDs.size(); i++)
     {
         auto const& material_ids = vec_junction_nodeID_matIDs[i].second;
diff --git a/ProcessLib/Output/IntegrationPointWriter.cpp b/ProcessLib/Output/IntegrationPointWriter.cpp
index 96c96c7176a..e5803bcdf68 100644
--- a/ProcessLib/Output/IntegrationPointWriter.cpp
+++ b/ProcessLib/Output/IntegrationPointWriter.cpp
@@ -74,12 +74,14 @@ static void addIntegrationPointMetaData(
 static ProcessLib::IntegrationPointMetaData extractIntegrationPointMetaData(
     json const& meta_data, std::string const& name)
 {
-    for (auto const& md : meta_data["integration_point_arrays"])
+    auto const& ip_meta_data = meta_data["integration_point_arrays"];
+    if (auto const it =
+            find_if(cbegin(ip_meta_data), cend(ip_meta_data),
+                    [&name](auto const& md) { return md["name"] == name; });
+        it != cend(ip_meta_data))
     {
-        if (md["name"] == name)
-        {
-            return {name, md["number_of_components"], md["integration_order"]};
-        }
+        return {name, (*it)["number_of_components"],
+                (*it)["integration_order"]};
     }
     OGS_FATAL("No integration point meta data with name '{:s}' found.", name);
 }
@@ -92,10 +94,11 @@ void addIntegrationPointWriter(
         integration_point_writer)
 {
     std::vector<IntegrationPointMetaData> meta_data;
-    for (auto const& ip_writer : integration_point_writer)
-    {
-        meta_data.push_back(addIntegrationPointData(mesh, *ip_writer));
-    }
+    meta_data.reserve(size(integration_point_writer));
+    transform(cbegin(integration_point_writer), cend(integration_point_writer),
+              back_inserter(meta_data), [&](auto const& ip_writer) {
+                  return addIntegrationPointData(mesh, *ip_writer);
+              });
     if (!meta_data.empty())
     {
         addIntegrationPointMetaData(mesh, meta_data);
diff --git a/ProcessLib/Process.cpp b/ProcessLib/Process.cpp
index 3fbc016c17b..f36a8fdf002 100644
--- a/ProcessLib/Process.cpp
+++ b/ProcessLib/Process.cpp
@@ -266,10 +266,10 @@ void Process::constructMonolithicProcessDofTable()
     }
 
     // Create a vector of the number of variable components
-    for (ProcessVariable const& pv : _process_variables[0])
-    {
-        vec_var_n_components.push_back(pv.getNumberOfComponents());
-    }
+    transform(
+        cbegin(_process_variables[0]), cend(_process_variables[0]),
+        back_inserter(vec_var_n_components),
+        [](ProcessVariable const& pv) { return pv.getNumberOfComponents(); });
 
     _local_to_global_index_map =
         std::make_unique<NumLib::LocalToGlobalIndexMap>(
diff --git a/ProcessLib/ProcessVariable.cpp b/ProcessLib/ProcessVariable.cpp
index fa50813da21..210368af54a 100644
--- a/ProcessLib/ProcessVariable.cpp
+++ b/ProcessLib/ProcessVariable.cpp
@@ -329,12 +329,12 @@ std::vector<std::unique_ptr<SourceTerm>> ProcessVariable::createSourceTerms(
 {
     std::vector<std::unique_ptr<SourceTerm>> source_terms;
 
-    for (auto& config : _source_term_configs)
-    {
-        source_terms.emplace_back(createSourceTerm(
-            config, dof_table, config.mesh, variable_id, integration_order,
-            _shapefunction_order, parameters));
-    }
+    transform(cbegin(_source_term_configs), cend(_source_term_configs),
+              back_inserter(source_terms), [&](auto& config) {
+                  return createSourceTerm(config, dof_table, config.mesh,
+                                          variable_id, integration_order,
+                                          _shapefunction_order, parameters);
+              });
 
     return source_terms;
 }
diff --git a/ProcessLib/SmallDeformation/SmallDeformationFEM.h b/ProcessLib/SmallDeformation/SmallDeformationFEM.h
index 0668fea1905..fcc28dc600a 100644
--- a/ProcessLib/SmallDeformation/SmallDeformationFEM.h
+++ b/ProcessLib/SmallDeformation/SmallDeformationFEM.h
@@ -376,10 +376,9 @@ public:
         cache.clear();
         cache.reserve(_ip_data.size());
 
-        for (auto const& ip_data : _ip_data)
-        {
-            cache.push_back(ip_data.free_energy_density);
-        }
+        transform(
+            cbegin(_ip_data), cend(_ip_data), back_inserter(cache),
+            [](auto const& ip_data) { return ip_data.free_energy_density; });
 
         return cache;
     }
diff --git a/ProcessLib/SmallDeformationNonlocal/SmallDeformationNonlocalFEM.h b/ProcessLib/SmallDeformationNonlocal/SmallDeformationNonlocalFEM.h
index 8f0a069d4e6..525053841db 100644
--- a/ProcessLib/SmallDeformationNonlocal/SmallDeformationNonlocalFEM.h
+++ b/ProcessLib/SmallDeformationNonlocal/SmallDeformationNonlocalFEM.h
@@ -618,10 +618,9 @@ public:
         cache.clear();
         cache.reserve(_ip_data.size());
 
-        for (auto const& ip_data : _ip_data)
-        {
-            cache.push_back(ip_data.free_energy_density);
-        }
+        transform(
+            cbegin(_ip_data), cend(_ip_data), back_inserter(cache),
+            [](auto const& ip_data) { return ip_data.free_energy_density; });
 
         return cache;
     }
@@ -635,10 +634,8 @@ public:
         cache.clear();
         cache.reserve(_ip_data.size());
 
-        for (auto const& ip_data : _ip_data)
-        {
-            cache.push_back(*ip_data.eps_p_V);
-        }
+        transform(cbegin(_ip_data), cend(_ip_data), back_inserter(cache),
+                  [](auto const& ip_data) { return *ip_data.eps_p_V; });
 
         return cache;
     }
@@ -652,10 +649,8 @@ public:
         cache.clear();
         cache.reserve(_ip_data.size());
 
-        for (auto const& ip_data : _ip_data)
-        {
-            cache.push_back(*ip_data.eps_p_D_xx);
-        }
+        transform(cbegin(_ip_data), cend(_ip_data), back_inserter(cache),
+                  [](auto const& ip_data) { return *ip_data.eps_p_D_xx; });
 
         return cache;
     }
diff --git a/ProcessLib/TES/TESLocalAssembler-impl.h b/ProcessLib/TES/TESLocalAssembler-impl.h
index 15514661970..2e53aa07e20 100644
--- a/ProcessLib/TES/TESLocalAssembler-impl.h
+++ b/ProcessLib/TES/TESLocalAssembler-impl.h
@@ -225,9 +225,8 @@ TESLocalAssembler<ShapeFunction_, IntegrationMethod_, GlobalDim>::
     cache.clear();
     cache.reserve(rho_SR.size());
 
-    for (auto const rho : rho_SR) {
-        cache.push_back(rho / rho_SR_dry - 1.0);
-    }
+    transform(cbegin(rho_SR), cend(rho_SR), back_inserter(cache),
+              [&](auto const rho) { return rho / rho_SR_dry - 1.0; });
 
     return cache;
 }
diff --git a/ProcessLib/VectorMatrixAssembler.cpp b/ProcessLib/VectorMatrixAssembler.cpp
index e988c305cd0..063a1e6538f 100644
--- a/ProcessLib/VectorMatrixAssembler.cpp
+++ b/ProcessLib/VectorMatrixAssembler.cpp
@@ -50,11 +50,10 @@ void VectorMatrixAssembler::assemble(
 {
     std::vector<std::vector<GlobalIndexType>> indices_of_processes;
     indices_of_processes.reserve(dof_tables.size());
-    for (auto dof_table : dof_tables)
-    {
-        indices_of_processes.emplace_back(
-            NumLib::getIndices(mesh_item_id, dof_table.get()));
-    }
+    transform(cbegin(dof_tables), cend(dof_tables),
+              back_inserter(indices_of_processes), [&](auto const& dof_table) {
+                  return NumLib::getIndices(mesh_item_id, dof_table);
+              });
 
     auto const& indices = indices_of_processes[process_id];
     _local_M_data.clear();
@@ -118,11 +117,10 @@ void VectorMatrixAssembler::assembleWithJacobian(
 {
     std::vector<std::vector<GlobalIndexType>> indices_of_processes;
     indices_of_processes.reserve(dof_tables.size());
-    for (auto dof_table : dof_tables)
-    {
-        indices_of_processes.emplace_back(
-            NumLib::getIndices(mesh_item_id, dof_table.get()));
-    }
+    transform(cbegin(dof_tables), cend(dof_tables),
+              back_inserter(indices_of_processes), [&](auto const& dof_table) {
+                  return NumLib::getIndices(mesh_item_id, dof_table);
+              });
 
     auto const& indices = indices_of_processes[process_id];
     auto const local_xdot = xdot.get(indices);
diff --git a/Tests/MeshGeoToolsLib/TestGeoMapper.cpp b/Tests/MeshGeoToolsLib/TestGeoMapper.cpp
index d5b01661714..6450cd7b4ab 100644
--- a/Tests/MeshGeoToolsLib/TestGeoMapper.cpp
+++ b/Tests/MeshGeoToolsLib/TestGeoMapper.cpp
@@ -53,9 +53,10 @@ TEST_F(MeshGeoToolsLibGeoMapper, PointsOnSurfaceMesh)
         GeoLib::GEOObjects geo_obj;
         std::string geo_name("TestGeoMapperPoints");
         auto points = std::make_unique<std::vector<GeoLib::Point*>>();
-        for (auto & p : pnts) {
-            points->push_back(new GeoLib::Point(p));
-        }
+        points->reserve(size(pnts));
+        transform(cbegin(pnts), cend(pnts), back_inserter(*points),
+                  [](auto& p) { return new GeoLib::Point(p); });
+
         geo_obj.addPointVec(std::move(points), geo_name);
         MeshGeoToolsLib::GeoMapper geo_mapper(geo_obj, geo_name);
 
diff --git a/Tests/MeshLib/TestMeshGenerator.cpp b/Tests/MeshLib/TestMeshGenerator.cpp
index 5c8001450cf..2e66b40866b 100644
--- a/Tests/MeshLib/TestMeshGenerator.cpp
+++ b/Tests/MeshLib/TestMeshGenerator.cpp
@@ -7,10 +7,9 @@
  *              http://www.opengeosys.org/project/license
  */
 
-#include <memory>
 #include <cmath>
-
-#include "gtest/gtest.h"
+#include <memory>
+#include <numeric>
 
 #include "MathLib/MathTools.h"
 #include "MeshLib/Elements/Element.h"
@@ -18,6 +17,7 @@
 #include "MeshLib/Mesh.h"
 #include "MeshLib/MeshGenerators/MeshGenerator.h"
 #include "MeshLib/Node.h"
+#include "gtest/gtest.h"
 
 using namespace MeshLib;
 
@@ -181,11 +181,12 @@ TEST(MeshLib, MeshGeneratorRegularPyramid)
     ASSERT_DOUBLE_EQ(L, node_n1[2]);
 
     // check if the domain volume equals the volume of the elements
-    long double element_volumes = 0.0;
-    for (auto const element : msh->getElements())
-    {
-        element_volumes += element->computeVolume();
-    }
+    long double const element_volumes =
+        std::accumulate(cbegin(msh->getElements()), cend(msh->getElements()),
+                        0., [](double const volume, auto* const element) {
+                            return volume + element->computeVolume();
+                        });
+
     EXPECT_NEAR(L*L*L, element_volumes, 1e-10);
 
     // test node order of the elements
-- 
GitLab