From 771e9b6ccdc4a01303f07ebcbc72e4428fa472e2 Mon Sep 17 00:00:00 2001
From: Max Kahnt <max.kahnt@fu-berlin.de>
Date: Wed, 26 Apr 2017 16:21:00 +0200
Subject: [PATCH] Add GridConstructionTest.

---
 dune/fufem/test/CMakeLists.txt          |   1 +
 dune/fufem/test/gridconstructiontest.cc | 164 ++++++++++++++++++++++++
 2 files changed, 165 insertions(+)
 create mode 100644 dune/fufem/test/gridconstructiontest.cc

diff --git a/dune/fufem/test/CMakeLists.txt b/dune/fufem/test/CMakeLists.txt
index 26ca6151..d5ac0085 100644
--- a/dune/fufem/test/CMakeLists.txt
+++ b/dune/fufem/test/CMakeLists.txt
@@ -13,6 +13,7 @@ set(GRID_BASED_TESTS
   functionspacebasistest
   generalizedlaplaceassemblertest
   gradientassemblertest
+  gridconstructiontest
   gridfunctiontest
   gridfunctionadaptortest
   h1functionalassemblertest
diff --git a/dune/fufem/test/gridconstructiontest.cc b/dune/fufem/test/gridconstructiontest.cc
new file mode 100644
index 00000000..b2f17a3f
--- /dev/null
+++ b/dune/fufem/test/gridconstructiontest.cc
@@ -0,0 +1,164 @@
+#include <config.h>
+#include <iostream>
+
+#include <dune/common/parametertree.hh>
+#include <dune/common/parallel/mpihelper.hh>
+#include <dune/fufem/utilities/gridconstruction.hh>
+
+#include <dune/grid/onedgrid.hh>
+
+#if HAVE_UG
+#include <dune/grid/uggrid.hh>
+#endif // HAVE_UG
+
+#if HAVE_DUNE_ALUGRID
+#include <dune/alugrid/grid.hh>
+template <size_t dim>
+using SN_ALUGrid = Dune::ALUGrid<dim, dim, Dune::ALUGridElementType::simplex,
+                                 Dune::ALUGridRefinementType::nonconforming>;
+#endif // HAVE_DUNE_ALUGRID
+
+struct GridConstructionTest {
+
+  template <class Grid>
+  bool test1D() const {
+    using C = Dune::GridConstruction<Grid, 1>;
+    return test([this]() {
+      Grid* interval = C::createGrid(getIntervalConfig());
+      delete (interval);
+    });
+  }
+
+  template <class Grid>
+  bool test2D() const {
+    using C = Dune::GridConstruction<Grid, 2>;
+    return test([this]() {
+      Grid* rectangle = C::createGrid(getRectangleConfig());
+      delete (rectangle);
+      Grid* circle = C::createGrid(getCircleConfig());
+      delete (circle);
+    });
+  }
+
+  template <class Grid>
+  bool test3D() const {
+    using C = Dune::GridConstruction<Grid, 3>;
+    return test([this]() {
+      Grid* tubeSegment = C::createGrid(getTubeSegmentConfig());
+      delete (tubeSegment);
+      Grid* sphere = C::createGrid(getSphereConfig());
+      delete (sphere);
+      Grid* cuboid = C::createGrid(getCuboidConfig());
+      delete (cuboid);
+    });
+  }
+
+private:
+  template <class T>
+  bool test(T&& t) const {
+    try {
+      t();
+    } catch (const Dune::Exception& e) {
+      std::cout << "Exception caught:" << e << std::endl;
+      return false;
+    } catch (...) {
+      std::cout << "Unkonwn exception caught." << std::endl;
+      return false;
+    }
+    return true; // no exception was caught
+  }
+
+  using Config = Dune::ParameterTree;
+
+  Config getIntervalConfig() const {
+    Config config;
+    config["type"] = "interval";
+    config["lowerCorner"] = "0.0";
+    config["upperCorner"] = "1.0";
+    config["elements"] = "3";
+    return config;
+  }
+
+  Config getRectangleConfig() const {
+    Config config;
+    config["type"] = "rectangle";
+    config["lowerCorner"] = "0.0 0.0";
+    config["upperCorner"] = "1.0 1.0";
+    config["elements"] = "3 3";
+    config["tetrahedral"] = "1";
+    return config;
+  }
+
+  Config getCircleConfig() const {
+    Config config;
+    config["type"] = "circle";
+    config["center"] = "0.0 0.0";
+    config["radius"] = "1.0";
+    return config;
+  }
+
+  Config getTubeSegmentConfig() const {
+    Config config;
+    config["type"] = "tubeSegment";
+    config["center"] = "0.0 0.0 0.0";
+    config["thickness"] = "0.2";
+    config["length"] = "2.0";
+    config["innerRadius"] = "1.0";
+    config["fromAngle"] = "0.0";
+    config["toAngle"] = "10.0";
+    config["nElementRing"] = "10";
+    config["nElementLength"] = "5";
+    config["closeTube"] = "0";
+    config["axis"] = "0";
+    config["tetrahedra"] = "1";
+    config["parameterizedBoundary"] = "0";
+    return config;
+  }
+
+  Config getSphereConfig() const {
+    Config config;
+    config["type"] = "sphere";
+    config["center"] = "0.0 0.0 0.0";
+    config["radius"] = "1.0";
+    return config;
+  }
+
+  Config getCuboidConfig() const {
+    Config config;
+    config["type"] = "cuboid";
+    config["lowerCorner"] = "0.0 0.0 0.0";
+    config["upperCorner"] = "1.0 1.0 1.0";
+    config["elements"] = "4 5 4";
+    config["tetrahedral"] = "1";
+    return config;
+  }
+};
+
+int main(int argc, char** argv) {
+  bool passed = true;
+  Dune::MPIHelper::instance(argc, argv);
+
+  std::cout << "This is GridConstructionTest v0.01" << std::endl;
+  GridConstructionTest test;
+
+  std::cout << "Testing OneD grid construction." << std::endl;
+  passed = passed && test.test1D<Dune::OneDGrid>();
+
+#if HAVE_UG
+  std::cout << "Testing UG grid construction." << std::endl;
+  passed = passed && test.test2D<Dune::UGGrid<2>>();
+  passed = passed && test.test3D<Dune::UGGrid<3>>();
+#else  // HAVE_UG
+  std::cout << "No UG Grid available." << std::endl;
+#endif // HAVE_UG
+
+#if HAVE_DUNE_ALUGRID
+  std::cout << "Testing ALU grid construction." << std::endl;
+  passed = passed && test.test2D<SN_ALUGrid<2>>();
+//  passed = passed && test.test3D<SN_ALUGrid<3>>(); // makeSphere not compatible
+#else // HAVE_DUNE_ALUGRID
+  std::cout << "No ALU Grid available." << std::endl;
+#endif // HAVE_DUNE_ALUGRID
+
+  return passed ? 0 : 1;
+}
-- 
GitLab