From cb5235ab53b0ec0c1422cda7747d33ec8a0d8853 Mon Sep 17 00:00:00 2001
From: Samuel Burbulla <samuel.burbulla@mathematik.uni-stuttgart.de>
Date: Tue, 2 Jul 2019 15:35:11 +0200
Subject: [PATCH] [test] Add a unit test for boundary segments.

---
 doc/grids/gmsh/line1d2dbseg.geo             | 12 +++
 doc/grids/gmsh/line1d2dbseg.msh             | 19 +++++
 dune/foamgrid/test/CMakeLists.txt           |  1 +
 dune/foamgrid/test/boundary-segment-test.cc | 90 +++++++++++++++++++++
 4 files changed, 122 insertions(+)
 create mode 100644 doc/grids/gmsh/line1d2dbseg.geo
 create mode 100644 doc/grids/gmsh/line1d2dbseg.msh
 create mode 100644 dune/foamgrid/test/boundary-segment-test.cc

diff --git a/doc/grids/gmsh/line1d2dbseg.geo b/doc/grids/gmsh/line1d2dbseg.geo
new file mode 100644
index 0000000..d8fd97d
--- /dev/null
+++ b/doc/grids/gmsh/line1d2dbseg.geo
@@ -0,0 +1,12 @@
+Point(1) = {0, 0, 0, 1.0};
+Point(2) = {1, 1, 0, 1.0};
+Point(3) = {-1, 0, 0, 1.0};
+Point(4) = {-2, 0, 0, 1.0};
+Point(5) = {-3, 1, 0, 1.0};
+Line(1) = {3, 1};
+Line(2) = {1, 2};
+Line(3) = {3, 4};
+Line(4) = {4, 5};
+
+Physical Line(1) = {1:4};
+Physical Point(42) = {2};
diff --git a/doc/grids/gmsh/line1d2dbseg.msh b/doc/grids/gmsh/line1d2dbseg.msh
new file mode 100644
index 0000000..7c2d67a
--- /dev/null
+++ b/doc/grids/gmsh/line1d2dbseg.msh
@@ -0,0 +1,19 @@
+$MeshFormat
+2.2 0 8
+$EndMeshFormat
+$Nodes
+5
+1 0 0 0
+2 1 1 0
+3 -1 0 0
+4 -2 0 0
+5 -3 1 0
+$EndNodes
+$Elements
+5
+1 15 2 42 2 2
+2 1 2 1 1 3 1
+3 1 2 1 2 1 2
+4 1 2 1 3 3 4
+5 1 2 1 4 4 5
+$EndElements
diff --git a/dune/foamgrid/test/CMakeLists.txt b/dune/foamgrid/test/CMakeLists.txt
index 2bdeacc..195ad43 100644
--- a/dune/foamgrid/test/CMakeLists.txt
+++ b/dune/foamgrid/test/CMakeLists.txt
@@ -11,6 +11,7 @@ else()
   dune_add_test(SOURCES local-refine-test.cc)
 endif()
 
+dune_add_test(SOURCES boundary-segment-test.cc)
 dune_add_test(SOURCES global-refine-test.cc)
 dune_add_test(SOURCES growth-test-1d.cc)
 dune_add_test(SOURCES growth-test-2d.cc)
diff --git a/dune/foamgrid/test/boundary-segment-test.cc b/dune/foamgrid/test/boundary-segment-test.cc
new file mode 100644
index 0000000..c286e52
--- /dev/null
+++ b/dune/foamgrid/test/boundary-segment-test.cc
@@ -0,0 +1,90 @@
+#include <config.h>
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+#include <dune/common/parallel/mpihelper.hh>
+#include <dune/common/exceptions.hh>
+#include <dune/grid/io/file/gmshreader.hh>
+#include <dune/grid/test/gridcheck.hh>
+#include <dune/grid/test/checkintersectionit.hh>
+#include <dune/foamgrid/foamgrid.hh>
+
+template<class Grid>
+void checkBoundarySegments(const Grid& grid,
+                           const Dune::GridFactory<Grid>& factory,
+                           const std::vector<int>& boundaryMarkers,
+                           int boundaryIntersectionsExpected,
+                           int insertedBoundariesExpected,
+                           int numIntersectionsExpected,
+                           int boundaryIdExpected = 42)
+{
+  std::cout << "  Checking boundary segment indices" << std::endl;
+
+  int numIntersections = 0;
+  int boundaryIntersections = 0;
+  int insertedBoundaries = 0;
+
+  for ( const auto& element : elements(grid.leafGridView()) )
+  {
+    for ( const auto& intersection : intersections(grid.leafGridView(), element) )
+    {
+      numIntersections++;
+      if ( intersection.boundary() )
+      {
+        boundaryIntersections++;
+        if ( factory.wasInserted( intersection ) )
+        {
+          insertedBoundaries++;
+          const int bndsegIdx = intersection.boundarySegmentIndex();
+          const int bndId = boundaryMarkers[bndsegIdx];
+          if (bndId != boundaryIdExpected)
+            DUNE_THROW(Dune::GridError, "Wrong boundary ID. Expected " << boundaryIdExpected << " got " << bndId);
+        }
+      }
+    }
+  }
+
+  // sanity checks
+  if (boundaryIntersections != boundaryIntersectionsExpected)
+      DUNE_THROW(Dune::GridError, "Wrong number of boundary intersections. Expected " << boundaryIntersectionsExpected << " got " << boundaryIntersections);
+  if (insertedBoundaries != insertedBoundariesExpected)
+      DUNE_THROW(Dune::GridError, "Wrong number of inserted boundaries. Expected " << insertedBoundariesExpected << " got " << insertedBoundaries);
+  if (numIntersections != numIntersectionsExpected)
+      DUNE_THROW(Dune::GridError, "Wrong number of intersections. Expected " << numIntersectionsExpected << " got " << numIntersections);
+}
+
+int main (int argc, char *argv[]) try
+{
+    using namespace Dune;
+
+    Dune::MPIHelper::instance(argc, argv);
+
+    // paths to gmsh test files
+    const std::string dune_foamgrid_path = std::string(DUNE_FOAMGRID_EXAMPLE_GRIDS_PATH) + "gmsh/";
+
+    {
+        std::cout << "\n################################################\n";
+        std::cout << "Checking FoamGrid<1, 2> (1d in 2d grid)\n";
+        std::cout << "################################################\n\n";
+
+        std::cout << "  Creating grid" << std::endl;
+        using Grid = FoamGrid<1, 2>;
+        GridFactory<Grid> factory;
+
+        std::vector<int> boundaryMarkers, elementMarkers;
+        GmshReader<Grid>::read(factory, dune_foamgrid_path + "line1d2dbseg.msh", boundaryMarkers, elementMarkers, /*verbose*/ true, /*insertBoundarySegments*/ true);
+        auto grid = std::shared_ptr<Grid>(factory.createGrid());
+
+        // check the boundary segments
+        checkBoundarySegments(*grid, factory,boundaryMarkers,  2, 1, 8);
+    }
+}
+// //////////////////////////////////
+//   Error handler
+// /////////////////////////////////
+catch (const Dune::Exception& e) {
+    std::cout << e << std::endl;
+    return 1;
+}
-- 
GitLab