From 35538d74453230e9e62c152334008cc1b6e4769e Mon Sep 17 00:00:00 2001
From: Timo Koch <timo.koch@iws.uni-stuttgart.de>
Date: Wed, 27 Mar 2019 18:05:54 +0100
Subject: [PATCH] [io][gridcreator] Support reading from untructured vtk grid
 files (sequential)

---
 CHANGELOG.md                 |  3 +++
 dumux/io/grid/gridmanager.hh | 30 +++++++++++++++++++++++++-----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index defe4fcbcb..7e34a7e9d2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,9 @@ Differences Between DuMuX 3.1 and DuMuX 3.0
 - __Multidomain__: It is now possible to use the facet coupling module together with the mpfa-o scheme in the bulk domain.
 - Added a `StaggeredNewtonConvergenceWriter` for the staggered grid discretization scheme
 - The box scheme works now on grids with prism / wedge elements in 3D
+- The GridManager now support reading unstructured grids and data from vtu/vtp files (ASCII, XML format) sequential
+  This means for UGGrid and FoamGrid you can now specify a grid in such a format in the input file
+  `Grid.File = mygrid.vtu` / `Grid.File = mygrid.vtp` and the associated data is available in the grid data object
 
 ### Immediate interface changes not allowing/requiring a deprecation period
 
diff --git a/dumux/io/grid/gridmanager.hh b/dumux/io/grid/gridmanager.hh
index 4b0d287f53..0f46329e45 100644
--- a/dumux/io/grid/gridmanager.hh
+++ b/dumux/io/grid/gridmanager.hh
@@ -75,6 +75,7 @@
 
 #include <dumux/common/parameters.hh>
 #include <dumux/discretization/method.hh>
+#include <dumux/io/vtk/vtkreader.hh>
 
 #include "griddata.hh"
 
@@ -142,7 +143,7 @@ public:
 
     std::shared_ptr<GridData> getGridData() const
     {
-        if (!enableDgfGridPointer_ && !enableGmshDomainMarkers_)
+        if (!gridData_)
             DUNE_THROW(Dune::IOError, "No grid data available");
 
         return gridData_;
@@ -191,23 +192,28 @@ protected:
     }
 
     /*!
-     * \brief Makes a grid from a file. We currently support *.dgf (Dune Grid Format) and *.msh (Gmsh mesh format).
+     * \brief Makes a grid from a file. We currently support
+     *     - dgf (Dune Grid Format)
+     *     - msh (Gmsh mesh format)
+     *     - vtp/vtu (VTK file formats)
      */
     void makeGridFromFile(const std::string& fileName,
                           const std::string& modelParamGroup)
     {
         // We found a file in the input file...does it have a supported extension?
         const std::string extension = getFileExtension(fileName);
-        if (extension != "dgf" && extension != "msh")
-            DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " only supports DGF (*.dgf) and Gmsh (*.msh) grid files but the specified filename has extension: *."<< extension);
+        if (extension != "dgf" && extension != "msh" && extension != "vtu" && extension != "vtp")
+            DUNE_THROW(Dune::IOError, "Grid type " << Dune::className<Grid>() << " doesn't support grid files with extension: *."<< extension);
 
-        // make the grid
+        // Dune Grid Format (DGF) files
         if (extension == "dgf")
         {
             enableDgfGridPointer_ = true;
             dgfGridPtr() = Dune::GridPtr<Grid>(fileName.c_str(), Dune::MPIHelper::getCommunicator());
             gridData_ = std::make_shared<GridData>(dgfGridPtr_);
         }
+
+        // Gmsh mesh format
         else if (extension == "msh")
         {
             // get some optional parameters
@@ -234,6 +240,20 @@ protected:
                 gridPtr() = std::shared_ptr<Grid>(gridFactory->createGrid());
             }
         }
+
+        // VTK file formats for unstructured grids
+        else if (extension == "vtu" || extension == "vtp")
+        {
+            if (Dune::MPIHelper::getCollectiveCommunication().size() > 1)
+                DUNE_THROW(Dune::NotImplemented, "Reading grids in parallel from VTK file formats is currently not supported!");
+
+            VTKReader vtkReader(fileName);
+            VTKReader::Data cellData, pointData;
+            auto gridFactory = std::make_unique<Dune::GridFactory<Grid>>();
+            const bool verbose = getParamFromGroup<bool>(modelParamGroup, "Grid.Verbosity", false);
+            gridPtr() = vtkReader.readGrid(*gridFactory, cellData, pointData, verbose);
+            gridData_ = std::make_shared<GridData>(gridPtr_, std::move(gridFactory), std::move(cellData), std::move(pointData));
+        }
     }
 
     /*!
-- 
GitLab