From e571fd7d18a444f5d00a61f36b6ffc6da7972d68 Mon Sep 17 00:00:00 2001
From: Kilian Weishaupt <kilian.weishaupt@iws.uni-stuttgart.de>
Date: Thu, 25 Jan 2018 16:27:24 +0100
Subject: [PATCH] [test][io] Add test for subgrid grid creator

---
 test/io/gridcreator/CMakeLists.txt            |  11 +-
 .../gridcreator/test_gridcreator_subgrid.cc   | 134 ++++++++++++++++++
 .../test_gridcreator_subgrid.input            |  12 ++
 test/references/subgrid-reference.vtu         |  84 +++++++++++
 4 files changed, 240 insertions(+), 1 deletion(-)
 create mode 100644 test/io/gridcreator/test_gridcreator_subgrid.cc
 create mode 100644 test/io/gridcreator/test_gridcreator_subgrid.input
 create mode 100644 test/references/subgrid-reference.vtu

diff --git a/test/io/gridcreator/CMakeLists.txt b/test/io/gridcreator/CMakeLists.txt
index 7873bece8a..5ae618be32 100644
--- a/test/io/gridcreator/CMakeLists.txt
+++ b/test/io/gridcreator/CMakeLists.txt
@@ -1,4 +1,4 @@
-dune_symlink_to_source_files(FILES grids test_gridcreator_gmsh.input test_gridcreator_cake.input)
+add_input_file_links()
 
 dune_add_test(NAME test_gridcreator_gmsh
               SOURCES test_gridcreator_gmsh.cc
@@ -19,3 +19,12 @@ dune_add_test(NAME test_gridcreator_cake
                        --command "${CMAKE_CURRENT_BINARY_DIR}/test_gridcreator_cake"
                        --files ${CMAKE_SOURCE_DIR}/test/references/cake-reference.vtu
                                ${CMAKE_CURRENT_BINARY_DIR}/cake-00000.vtu)
+
+dune_add_test(NAME test_gridcreator_subgrid
+              SOURCES test_gridcreator_subgrid.cc
+              CMAKE_GUARD dune-subgrid_FOUND
+              COMMAND ${CMAKE_SOURCE_DIR}/bin/testing/runtest.py
+              CMD_ARGS --script fuzzy
+                       --command "${CMAKE_CURRENT_BINARY_DIR}/test_gridcreator_subgrid"
+                       --files ${CMAKE_SOURCE_DIR}/test/references/subgrid-reference.vtu
+                               ${CMAKE_CURRENT_BINARY_DIR}/subgrid_three.vtu)
diff --git a/test/io/gridcreator/test_gridcreator_subgrid.cc b/test/io/gridcreator/test_gridcreator_subgrid.cc
new file mode 100644
index 0000000000..857ade51df
--- /dev/null
+++ b/test/io/gridcreator/test_gridcreator_subgrid.cc
@@ -0,0 +1,134 @@
+/*****************************************************************************
+ *   See the file COPYING for full copying permissions.                      *
+ *                                                                           *
+ *   This program is free software: you can redistribute it and/or modify    *
+ *   it under the terms of the GNU General Public License as published by    *
+ *   the Free Software Foundation, either version 2 of the License, or       *
+ *   (at your option) any later version.                                     *
+ *                                                                           *
+ *   This program is distributed in the hope that it will be useful,         *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
+ *   GNU General Public License for more details.                            *
+ *                                                                           *
+ *   You should have received a copy of the GNU General Public License       *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
+ *****************************************************************************/
+/*!
+ * \file
+ * \brief Test for the cake grid creator
+ */
+#include <config.h>
+#include <iostream>
+
+#include <dune/common/parallel/mpihelper.hh>
+#include <dune/common/fvector.hh>
+#include <dune/common/timer.hh>
+#include <dune/grid/io/file/vtk.hh>
+
+#include <dumux/common/parameters.hh>
+#include <dumux/io/gridcreator.hh>
+#include <dumux/io/subgridgridcreator.hh>
+#include <dumux/discretization/methods.hh>
+
+/*!
+ * \brief A method providing an () operator in order to select elements for a subgrid.
+ */
+template<class GlobalPosition>
+class CircleSelector
+{
+public:
+    CircleSelector(const GlobalPosition& center) : center_(center) {}
+
+    //! Select all elements within a circle around a center point.
+    int operator() (const auto& element) const
+    {
+        const auto x = element.geometry().center()[0];
+        const auto y = element.geometry().center()[1];
+        const double radius = 0.3;
+        return std::sqrt((x - center_[0])*(x - center_[0]) + (y - center_[1])*(y - center_[1])) < radius;
+    }
+private:
+    const GlobalPosition center_;
+};
+
+int main(int argc, char** argv) try
+{
+    using namespace Dumux;
+
+    // Initialize MPI, finalize is done automatically on exit.
+    Dune::MPIHelper::instance(argc, argv);
+
+    // First read parameters from input file.
+    Dumux::Parameters::init(argc, argv);
+
+    constexpr int dim = 2;
+    using GlobalPosition = Dune::FieldVector<double, dim>;
+
+    Dune::Timer timer;
+    using HostGrid = Dune::YaspGrid<dim, Dune::TensorProductCoordinates<double, dim> >;
+    using HostGridCreator = GridCreatorImpl<HostGrid, DiscretizationMethods::None>;
+    HostGridCreator::makeGrid();
+
+    // Calculate the bounding box of the host grid view.
+    GlobalPosition bBoxMin(std::numeric_limits<double>::max());
+    GlobalPosition bBoxMax(std::numeric_limits<double>::min());
+    for (const auto& vertex : vertices(HostGridCreator::grid().leafGridView()))
+    {
+        for (int i=0; i<dim; i++)
+        {
+            using std::min;
+            using std::max;
+            bBoxMin[i] = min(bBoxMin[i], vertex.geometry().corner(0)[i]);
+            bBoxMax[i] = max(bBoxMax[i], vertex.geometry().corner(0)[i]);
+        }
+    }
+
+    // Get the center of the hostgrid's domain.
+    const GlobalPosition center{bBoxMin[0]+0.5*bBoxMax[0], bBoxMin[1]+0.5*bBoxMax[1]};
+
+    // Select all elements right of the center.
+    auto elementSelectorOne = [&center](const auto& element)
+    {
+        return element.geometry().center()[0] > center[0];
+    };
+
+    // Select all elements left of the center.
+    auto elementSelectorTwo = [&center](const auto& element)
+    {
+        return element.geometry().center()[0] < center[0];
+    };
+
+    // Select all elements within a circle around the center.
+    // Instead of a lambda, we use a class providing an () operator.
+    // Of course, a lambda would be possible here, too.
+    CircleSelector<GlobalPosition> elementSelectorThree(center);
+
+    // Create three different subgrids from the same hostgrid.
+    auto subgridPtrOne = SubgridGridCreator<HostGrid>::makeGrid(HostGridCreator::grid(), elementSelectorOne, "SubGridOne");
+    auto subgridPtrTwo = SubgridGridCreator<HostGrid>::makeGrid(HostGridCreator::grid(), elementSelectorTwo, "SubGridTwo");
+    auto subgridPtrThree = SubgridGridCreator<HostGrid>::makeGrid(HostGridCreator::grid(), elementSelectorThree, "SubGridThree");
+
+    std::cout << "Constructing a host grid and three subgrids took "  << timer.elapsed() << " seconds.\n";
+
+    // Write out the host grid and the subgrids.
+    Dune::VTKWriter<HostGrid::LeafGridView> vtkWriter(HostGridCreator::grid().leafGridView());
+    vtkWriter.write("hostgrid");
+
+    return 0;
+}
+///////////////////////////////////////
+//////// Error handler ////////////////
+///////////////////////////////////////
+catch (Dumux::ParameterException &e) {
+    std::cerr << e << ". Abort!\n";
+    return 1;
+}
+catch (Dune::Exception &e) {
+    std::cerr << "Dune reported error: " << e << std::endl;
+    return 3;
+}
+catch (...) {
+    std::cerr << "Unknown exception thrown!\n";
+    return 4;
+}
diff --git a/test/io/gridcreator/test_gridcreator_subgrid.input b/test/io/gridcreator/test_gridcreator_subgrid.input
new file mode 100644
index 0000000000..f994441727
--- /dev/null
+++ b/test/io/gridcreator/test_gridcreator_subgrid.input
@@ -0,0 +1,12 @@
+SubGridOne.Problem.Name = one
+SubGridTwo.Problem.Name = two
+SubGridThree.Problem.Name = three
+
+[Grid]
+Positions0 = 0 1
+Positions1 = 0 1
+Cells0 = 1
+Cells1 = 1
+Refinement = 4
+
+WriteSubGridToVtk = true
diff --git a/test/references/subgrid-reference.vtu b/test/references/subgrid-reference.vtu
new file mode 100644
index 0000000000..9934b3fc1a
--- /dev/null
+++ b/test/references/subgrid-reference.vtu
@@ -0,0 +1,84 @@
+<?xml version="1.0"?>
+<VTKFile type="UnstructuredGrid" version="0.1" byte_order="LittleEndian">
+  <UnstructuredGrid>
+    <Piece NumberOfCells="76" NumberOfPoints="97">
+      <Points>
+        <DataArray type="Float32" Name="Coordinates" NumberOfComponents="3" format="ascii">
+          0.375 0.1875 0 0.4375 0.1875 0 0.375 0.25 0 0.4375 0.25 0
+          0.5 0.1875 0 0.5 0.25 0 0.5625 0.1875 0 0.5625 0.25 0
+          0.625 0.1875 0 0.625 0.25 0 0.3125 0.25 0 0.3125 0.3125 0
+          0.375 0.3125 0 0.4375 0.3125 0 0.5 0.3125 0 0.5625 0.3125 0
+          0.625 0.3125 0 0.6875 0.25 0 0.6875 0.3125 0 0.25 0.3125 0
+          0.25 0.375 0 0.3125 0.375 0 0.375 0.375 0 0.4375 0.375 0
+          0.5 0.375 0 0.5625 0.375 0 0.625 0.375 0 0.6875 0.375 0
+          0.75 0.3125 0 0.75 0.375 0 0.1875 0.375 0 0.1875 0.4375 0
+          0.25 0.4375 0 0.3125 0.4375 0 0.375 0.4375 0 0.4375 0.4375 0
+          0.5 0.4375 0 0.5625 0.4375 0 0.625 0.4375 0 0.6875 0.4375 0
+          0.75 0.4375 0 0.8125 0.375 0 0.8125 0.4375 0 0.1875 0.5 0
+          0.25 0.5 0 0.3125 0.5 0 0.375 0.5 0 0.4375 0.5 0
+          0.5 0.5 0 0.5625 0.5 0 0.625 0.5 0 0.6875 0.5 0
+          0.75 0.5 0 0.8125 0.5 0 0.1875 0.5625 0 0.25 0.5625 0
+          0.3125 0.5625 0 0.375 0.5625 0 0.4375 0.5625 0 0.5 0.5625 0
+          0.5625 0.5625 0 0.625 0.5625 0 0.6875 0.5625 0 0.75 0.5625 0
+          0.8125 0.5625 0 0.1875 0.625 0 0.25 0.625 0 0.3125 0.625 0
+          0.375 0.625 0 0.4375 0.625 0 0.5 0.625 0 0.5625 0.625 0
+          0.625 0.625 0 0.6875 0.625 0 0.75 0.625 0 0.8125 0.625 0
+          0.25 0.6875 0 0.3125 0.6875 0 0.375 0.6875 0 0.4375 0.6875 0
+          0.5 0.6875 0 0.5625 0.6875 0 0.625 0.6875 0 0.6875 0.6875 0
+          0.75 0.6875 0 0.3125 0.75 0 0.375 0.75 0 0.4375 0.75 0
+          0.5 0.75 0 0.5625 0.75 0 0.625 0.75 0 0.6875 0.75 0
+          0.375 0.8125 0 0.4375 0.8125 0 0.5 0.8125 0 0.5625 0.8125 0
+          0.625 0.8125 0
+        </DataArray>
+      </Points>
+      <Cells>
+        <DataArray type="Int32" Name="connectivity" NumberOfComponents="1" format="ascii">
+          0 1 3 2 1 4 5 3 4 6 7 5
+          6 8 9 7 10 2 12 11 2 3 13 12
+          3 5 14 13 5 7 15 14 7 9 16 15
+          9 17 18 16 19 11 21 20 11 12 22 21
+          12 13 23 22 13 14 24 23 14 15 25 24
+          15 16 26 25 16 18 27 26 18 28 29 27
+          30 20 32 31 20 21 33 32 21 22 34 33
+          22 23 35 34 23 24 36 35 24 25 37 36
+          25 26 38 37 26 27 39 38 27 29 40 39
+          29 41 42 40 31 32 44 43 32 33 45 44
+          33 34 46 45 34 35 47 46 35 36 48 47
+          36 37 49 48 37 38 50 49 38 39 51 50
+          39 40 52 51 40 42 53 52 43 44 55 54
+          44 45 56 55 45 46 57 56 46 47 58 57
+          47 48 59 58 48 49 60 59 49 50 61 60
+          50 51 62 61 51 52 63 62 52 53 64 63
+          54 55 66 65 55 56 67 66 56 57 68 67
+          57 58 69 68 58 59 70 69 59 60 71 70
+          60 61 72 71 61 62 73 72 62 63 74 73
+          63 64 75 74 66 67 77 76 67 68 78 77
+          68 69 79 78 69 70 80 79 70 71 81 80
+          71 72 82 81 72 73 83 82 73 74 84 83
+          77 78 86 85 78 79 87 86 79 80 88 87
+          80 81 89 88 81 82 90 89 82 83 91 90
+          86 87 93 92 87 88 94 93 88 89 95 94
+          89 90 96 95
+        </DataArray>
+        <DataArray type="Int32" Name="offsets" NumberOfComponents="1" format="ascii">
+          4 8 12 16 20 24 28 32 36 40 44 48
+          52 56 60 64 68 72 76 80 84 88 92 96
+          100 104 108 112 116 120 124 128 132 136 140 144
+          148 152 156 160 164 168 172 176 180 184 188 192
+          196 200 204 208 212 216 220 224 228 232 236 240
+          244 248 252 256 260 264 268 272 276 280 284 288
+          292 296 300 304
+        </DataArray>
+        <DataArray type="UInt8" Name="types" NumberOfComponents="1" format="ascii">
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9 9 9 9 9 9 9 9 9
+          9 9 9 9
+        </DataArray>
+      </Cells>
+    </Piece>
+  </UnstructuredGrid>
+</VTKFile>
-- 
GitLab