From daf219efb76c1987f0d712943e16b5948453f3e8 Mon Sep 17 00:00:00 2001
From: Tobias Meisel <tobias.meisel@ufz.de>
Date: Wed, 7 Oct 2020 13:46:57 +0200
Subject: [PATCH] [MeL] Write MeshToFile is now able to write xdmf

---
 MeshLib/IO/VtkIO/writeXdmf.cpp         | 56 ------------------------
 MeshLib/IO/XDMF/writeXdmf.cpp          | 59 ++++++++++++++++++++++++++
 MeshLib/IO/{VtkIO => XDMF}/writeXdmf.h | 14 +++---
 MeshLib/IO/writeMeshToFile.cpp         | 16 +++++--
 MeshLib/IO/writeMeshToFile.h           |  2 +-
 ProcessLib/Output/ProcessOutput.cpp    |  1 +
 6 files changed, 80 insertions(+), 68 deletions(-)
 delete mode 100644 MeshLib/IO/VtkIO/writeXdmf.cpp
 create mode 100644 MeshLib/IO/XDMF/writeXdmf.cpp
 rename MeshLib/IO/{VtkIO => XDMF}/writeXdmf.h (75%)

diff --git a/MeshLib/IO/VtkIO/writeXdmf.cpp b/MeshLib/IO/VtkIO/writeXdmf.cpp
deleted file mode 100644
index 8c106443ea1..00000000000
--- a/MeshLib/IO/VtkIO/writeXdmf.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-/**
- * \file
- * \author Tobias Meisel
- * \date   2020-10-06
- * \brief  Implementation of WriteXdmf function.
- *
- * \copyright
- * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org)
- *            Distributed under a Modified BSD License.
- *              See accompanying file LICENSE.txt or
- *              http://www.opengeosys.org/project/license
- */
-
-#include "writeXdmf.h"
-
-#include <vtkNew.h>
-#include <vtkSmartPointer.h>
-#include <vtkXdmf3Writer.h>
-
-#include "MeshLib/Vtk/VtkMappedMeshSource.h"
-
-namespace MeshLib
-{
-namespace IO
-{
-bool writeXdmf3(const MeshLib::Mesh& mesh, std::string const &file_name)
-{
-        #ifdef USE_PETSC
-            int rank;
-            MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
-            int mpi_size;
-            MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
-            vtkSmartPointer<vtkPXdmf3Writer> writer = vtkSmartPointer<vtkPXdmf3Writer>::New(); // open file handle
-
-        #else
-            vtkSmartPointer<vtkXdmf3Writer> writer = vtkSmartPointer<vtkXdmf3Writer>::New(); // open file handle
-        #endif
-
-            writer->SetFileName(file_name.c_str());
-            vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
-            vtkSource->SetMesh(&mesh);
-            vtkSource->Update();
-            writer->SetInputData(vtkSource->GetOutput());
-
-        #ifdef USE_PETSC
-            writer->SetGhostLevel(1);
-            writer->SetNumberOfPieces(num_partitions);
-            writer->SetStartPiece(rank);
-            writer->SetEndPiece(rank);
-        #endif
-            writer->Write();
-                // close file handle
-        return 0;
-}
-} //end namespace IO
-} //end namespace MeshLib
\ No newline at end of file
diff --git a/MeshLib/IO/XDMF/writeXdmf.cpp b/MeshLib/IO/XDMF/writeXdmf.cpp
new file mode 100644
index 00000000000..f7f1ba601ae
--- /dev/null
+++ b/MeshLib/IO/XDMF/writeXdmf.cpp
@@ -0,0 +1,59 @@
+/**
+ * \file
+ * \author Tobias Meisel
+ * \date   2020-10-06
+ * \brief  Implementation of WriteXdmf3 function.
+ *
+ * \copyright
+ * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org)
+ *            Distributed under a Modified BSD License.
+ *              See accompanying file LICENSE.txt or
+ *              http://www.opengeosys.org/project/license
+ */
+
+#include "writeXdmf.h"
+
+#include <vtkNew.h>
+#include <vtkPXdmf3Writer.h>
+#include <vtkSmartPointer.h>
+#include <vtkXdmf3Writer.h>
+
+#include "MeshLib/Vtk/VtkMappedMeshSource.h"
+
+namespace MeshLib
+{
+namespace IO
+{
+bool writeXdmf3(const MeshLib::Mesh& mesh, std::string const& file_name)
+{
+#ifdef USE_PETSC
+    int rank;
+    MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
+    int mpi_size;
+    MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
+    vtkSmartPointer<vtkPXdmf3Writer> writer =
+        vtkSmartPointer<vtkPXdmf3Writer>::New();  // open file handle
+
+#else
+    vtkSmartPointer<vtkXdmf3Writer> writer =
+        vtkSmartPointer<vtkXdmf3Writer>::New();  // open file handle
+#endif
+
+    writer->SetFileName(file_name.c_str());
+    vtkNew<MeshLib::VtkMappedMeshSource> vtkSource;
+    vtkSource->SetMesh(&mesh);
+    vtkSource->Update();
+    writer->SetInputData(vtkSource->GetOutput());
+
+#ifdef USE_PETSC
+    writer->SetGhostLevel(1);
+    writer->SetNumberOfPieces(num_partitions);
+    writer->SetStartPiece(rank);
+    writer->SetEndPiece(rank);
+#endif
+    writer->Write();
+    // close file handle
+    return 0;
+}
+}  // end namespace IO
+}  // end namespace MeshLib
\ No newline at end of file
diff --git a/MeshLib/IO/VtkIO/writeXdmf.h b/MeshLib/IO/XDMF/writeXdmf.h
similarity index 75%
rename from MeshLib/IO/VtkIO/writeXdmf.h
rename to MeshLib/IO/XDMF/writeXdmf.h
index 71c6f5f5766..b723cde6807 100644
--- a/MeshLib/IO/VtkIO/writeXdmf.h
+++ b/MeshLib/IO/XDMF/writeXdmf.h
@@ -15,20 +15,18 @@
 #pragma once
 
 #include <string>
-#include "MeshLib/Mesh.h"
-
-namespace MeshLib {
 
+namespace MeshLib
+{
+class Mesh;
 
 namespace IO
 {
-
 /// Writes mesh to XDMF file.
 /// \return True on success, false on error
-bool writeXdmf3(const MeshLib::Mesh& mesh, std::string const &file_name);
 /// \param mesh           Mesh holds all data to be written.
 /// \param file_name      File name.
-/// \return True on success, false on error
+bool writeXdmf3(MeshLib::Mesh const& mesh, std::string const& file_name);
 
-} // end namespace IO
-} // end namespace MeshLib
\ No newline at end of file
+}  // end namespace IO
+}  // end namespace MeshLib
\ No newline at end of file
diff --git a/MeshLib/IO/writeMeshToFile.cpp b/MeshLib/IO/writeMeshToFile.cpp
index debc78c3bd7..256680b6a6d 100644
--- a/MeshLib/IO/writeMeshToFile.cpp
+++ b/MeshLib/IO/writeMeshToFile.cpp
@@ -10,14 +10,14 @@
 #include "writeMeshToFile.h"
 
 #include "BaseLib/Logging.h"
-
 #include "BaseLib/FileTools.h"
 #include "BaseLib/StringTools.h"
 
 #include "MeshLib/Mesh.h"
-
 #include "MeshLib/IO/Legacy/MeshIO.h"
 #include "MeshLib/IO/VtkIO/VtuInterface.h"
+#include "MeshLib/IO/XDMF/writeXdmf.h"
+
 
 namespace MeshLib
 {
@@ -44,9 +44,19 @@ int writeMeshToFile(const MeshLib::Mesh &mesh, const std::string &file_name)
         }
         return 0;
     }
-
+    if (BaseLib::hasFileExtension(".xdmf", file_name))
+    {
+        if (auto const result = writeXdmf3(mesh, file_name); !result)
+        {
+            ERR("writeMeshToFile(): Could not write mesh to '{:s}'.",
+                file_name);
+            return -1;
+        }
+        return 0;
+    }
     ERR("writeMeshToFile(): Unknown mesh file format in file {:s}.", file_name);
     return -1;
+
 }
 
 } // end namespace IO
diff --git a/MeshLib/IO/writeMeshToFile.h b/MeshLib/IO/writeMeshToFile.h
index 223a2a0d31f..d2751c7a667 100644
--- a/MeshLib/IO/writeMeshToFile.h
+++ b/MeshLib/IO/writeMeshToFile.h
@@ -18,4 +18,4 @@ namespace IO
 {
 int writeMeshToFile(const MeshLib::Mesh &mesh, const std::string &file_name);
 }
-}
+}
\ No newline at end of file
diff --git a/ProcessLib/Output/ProcessOutput.cpp b/ProcessLib/Output/ProcessOutput.cpp
index ae067ab9f93..f6b0f1495ce 100644
--- a/ProcessLib/Output/ProcessOutput.cpp
+++ b/ProcessLib/Output/ProcessOutput.cpp
@@ -20,6 +20,7 @@
 #include "IntegrationPointWriter.h"
 #include "MathLib/LinAlg/LinAlg.h"
 #include "MeshLib/IO/VtkIO/VtuInterface.h"
+#include "MeshLib/IO/XDMF/writeXdmf.h"
 #include "NumLib/DOF/LocalToGlobalIndexMap.h"
 
 /// Copies the ogs_version string containing the release number and the git
-- 
GitLab