From e1c71927734cfacbcc7f12c664fb7f651847b20f Mon Sep 17 00:00:00 2001
From: Wenqing Wang <wenqing.wang@ufz.de>
Date: Mon, 9 May 2016 17:05:56 +0200
Subject: [PATCH] [PMesh] Added a mesh partitioning tool as a utility

---
 Applications/Utils/CMakeLists.txt             |  1 +
 .../Utils/PartitionMesh/CMakeLists.txt        | 23 ++++++
 .../Utils/PartitionMesh/MeshPartitioning.cpp  | 51 +++++++++++++
 .../Utils/PartitionMesh/MeshPartitioning.h    | 58 +++++++++++++++
 .../Utils/PartitionMesh/PartitionMesh.cpp     | 72 +++++++++++++++++++
 5 files changed, 205 insertions(+)
 create mode 100644 Applications/Utils/PartitionMesh/CMakeLists.txt
 create mode 100644 Applications/Utils/PartitionMesh/MeshPartitioning.cpp
 create mode 100644 Applications/Utils/PartitionMesh/MeshPartitioning.h
 create mode 100644 Applications/Utils/PartitionMesh/PartitionMesh.cpp

diff --git a/Applications/Utils/CMakeLists.txt b/Applications/Utils/CMakeLists.txt
index 8c39fbc8274..b59b53186da 100644
--- a/Applications/Utils/CMakeLists.txt
+++ b/Applications/Utils/CMakeLists.txt
@@ -3,6 +3,7 @@ add_subdirectory(GeoTools)
 add_subdirectory(MeshGeoTools)
 add_subdirectory(MeshEdit)
 add_subdirectory(ModelPreparation)
+add_subdirectory(PartitionMesh)
 add_subdirectory(SimpleMeshCreation)
 
 if(OGS_BUILD_GUI)
diff --git a/Applications/Utils/PartitionMesh/CMakeLists.txt b/Applications/Utils/PartitionMesh/CMakeLists.txt
new file mode 100644
index 00000000000..0c85505cfd2
--- /dev/null
+++ b/Applications/Utils/PartitionMesh/CMakeLists.txt
@@ -0,0 +1,23 @@
+
+include_directories(
+	${CMAKE_SOURCE_DIR}/Utils/PartitionMesh
+	${CMAKE_SOURCE_DIR}/BaseLib
+	${CMAKE_SOURCE_DIR}/FileIO
+	${CMAKE_SOURCE_DIR}/MeshLib
+)
+
+add_executable(partmesh PartitionMesh.cpp MeshPartitioning.h MeshPartitioning.cpp)
+set_target_properties(partmesh PROPERTIES FOLDER Utilities)
+target_link_libraries(partmesh FileIO)
+ADD_VTK_DEPENDENCY(partmesh)
+
+####################
+### Installation ###
+####################
+install(TARGETS partmesh RUNTIME DESTINATION bin COMPONENT ogs_partmesh)
+
+cpack_add_component(ogs_partmesh
+	DISPLAY_NAME "Mesh partitioning tool"
+	DESCRIPTION "Mesh partitioning tool."
+	GROUP Utilities
+)
diff --git a/Applications/Utils/PartitionMesh/MeshPartitioning.cpp b/Applications/Utils/PartitionMesh/MeshPartitioning.cpp
new file mode 100644
index 00000000000..ead10df8c64
--- /dev/null
+++ b/Applications/Utils/PartitionMesh/MeshPartitioning.cpp
@@ -0,0 +1,51 @@
+/*!
+  \file MeshPartitioning.cpp
+  \date   2016.05
+
+  \brief  Define the members of class MeshPartitioning
+
+  \copyright
+  Copyright (c) 2012-2016, 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 "MeshPartitioning.h"
+
+#include <fstream>
+
+#include "MeshLib/Elements/Element.h"
+
+namespace MeshLib
+{
+void MeshPartitioning :: write2METIS(const std::string& file_name)
+{
+    std::ofstream os(file_name.data(), std::ios::trunc);
+    os << _elements.size() <<" \n";
+    for (const auto elem : _elements)
+    {
+        for (unsigned j=0; j<elem->getNNodes(); j++)
+        {
+            os << elem->getNodeIndex(j) + 1 <<" ";
+        }
+        os << std::endl;
+    }
+}
+
+void MeshPartitioning :: partitionByNode()
+{
+
+}
+
+void MeshPartitioning :: writeNodePartitionedMeshASCII()
+{
+}
+
+void MeshPartitioning :: writeNodePartitionedMeshBinary()
+{
+}
+
+}   // namespace MeshLib
+
diff --git a/Applications/Utils/PartitionMesh/MeshPartitioning.h b/Applications/Utils/PartitionMesh/MeshPartitioning.h
new file mode 100644
index 00000000000..ae1270479be
--- /dev/null
+++ b/Applications/Utils/PartitionMesh/MeshPartitioning.h
@@ -0,0 +1,58 @@
+/*!
+  \file MeshPartitioning.h
+  \date   2016.05
+
+  \brief  Declare a class to perform mesh partitioning
+
+  \copyright
+  Copyright (c) 2012-2016, OpenGeoSys Community (http://www.opengeosys.org)
+             Distributed under a Modified BSD License.
+               See accompanying file LICENSE.txt or
+               http://www.opengeosys.org/project/license
+
+*/
+
+#ifndef MESH_PARTITIONING_H_
+#define MESH_PARTITIONING_H_
+
+#include <vector>
+#include <string>
+
+#include "MeshLib/Mesh.h"
+
+namespace MeshLib
+{
+
+/// A subdomain mesh.
+class MeshPartitioning : public Mesh
+{
+    public:
+        /// Copy constructor
+        MeshPartitioning(const MeshLib::Mesh &mesh) : Mesh(mesh)
+        {}
+
+        /// Write mesh to METIS input file
+        void write2METIS(const std::string& file_name);
+
+        /// Partition by element.
+        void partitionByElement()
+        {
+            /* to be decided */
+        }
+
+        /// Partition by node.
+        void partitionByNode();
+
+        /// Write the partitioned mesh into ASCII files.
+        void writeNodePartitionedMeshASCII();
+
+        /// Write the partitioned mesh into binary files.
+        void writeNodePartitionedMeshBinary();
+
+    private:
+
+};
+
+}   // namespace MeshLib
+
+#endif // MESH_PARTITIONING_H_
diff --git a/Applications/Utils/PartitionMesh/PartitionMesh.cpp b/Applications/Utils/PartitionMesh/PartitionMesh.cpp
new file mode 100644
index 00000000000..b277abddb8e
--- /dev/null
+++ b/Applications/Utils/PartitionMesh/PartitionMesh.cpp
@@ -0,0 +1,72 @@
+/*!
+  \file PartitionMesh.cpp
+  \date   2016.05
+
+  \brief  A tool for mesh partitioning.
+
+  \copyright
+  Copyright (c) 2012-2016, 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 "tclap/CmdLine.h"
+
+#include "logog/include/logog.hpp"
+
+#include "LogogSimpleFormatter.h"
+#include "BaseLib/FileTools.h"
+
+#include "FileIO/VtkIO/VtuInterface.h"
+#include "Legacy/MeshIO.h"
+
+#include "MeshPartitioning.h"
+
+int main (int argc, char* argv[])
+{
+    LOGOG_INITIALIZE();
+    logog::Cout* logog_cout (new logog::Cout);
+    BaseLib::LogogSimpleFormatter *custom_format (new BaseLib::LogogSimpleFormatter);
+    logog_cout->SetFormatter(*custom_format);
+
+    std::string m_str = "Partition a mesh for parallel computing."
+                        "The tasks of this tool are in twofold:\n"
+                        "1. Convert mesh file to the input file of the partitioning tool,\n"
+                        "2. Partition a mesh using the partitioning tool,\n"
+                        "\tcreate the mesh data of each partition,\n"
+                        "\trenumber the node indicies of each partition,\n"
+                        "\tand output the results for parallel computing.";
+
+    TCLAP::CmdLine cmd(m_str, ' ', "0.1");
+    TCLAP::ValueArg<std::string> mesh_in("i", "mesh-input-file",
+                                         "the name of the file containing the input mesh", true,
+                                         "", "file name of input mesh");
+
+    TCLAP::SwitchArg ogs2metis_flag("1", "ogs2metis",
+                                    "Indicator to convert the ogs mesh file to METIS input file", cmd, false);
+
+    cmd.parse(argc, argv);
+
+    const std::string ifile_name = mesh_in.getValue();
+
+    MeshLib::MeshPartitioning* mesh = dynamic_cast<MeshLib::MeshPartitioning*>
+                                      (FileIO::VtuInterface::readVTUFile(ifile_name));
+    INFO("Mesh read: %d nodes, %d elements.", mesh->getNNodes(), mesh->getNElements());
+
+    if ( ogs2metis_flag.getValue() )
+    {
+        mesh->write2METIS(BaseLib::dropFileExtension(ifile_name) + ".mesh");
+    }
+    else
+    {
+
+    }
+
+    delete custom_format;
+    delete logog_cout;
+    LOGOG_SHUTDOWN();
+
+    return 0;
+}
-- 
GitLab