From a3a8516f5f21b929eda6ebe3071a98d92e751d6e Mon Sep 17 00:00:00 2001
From: Anthony Royer <anthony.royer@uliege.be>
Date: Tue, 6 Apr 2021 23:25:52 +0200
Subject: [PATCH] Remove 'numParts' in 'PartitionUsingThisSplit'

---
 Geo/GModel.cpp             |  2 +-
 Mesh/meshPartition.cpp     | 15 ++++++++++++---
 Mesh/meshPartition.h       |  2 +-
 Plugin/SimplePartition.cpp |  3 +--
 4 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/Geo/GModel.cpp b/Geo/GModel.cpp
index acbc93610d..8a82c3fdba 100644
--- a/Geo/GModel.cpp
+++ b/Geo/GModel.cpp
@@ -2066,7 +2066,7 @@ int GModel::partitionMesh(
     if(elementPartition.empty())
       return PartitionMesh(this, numPart);
     else
-      return PartitionUsingThisSplit(this, numPart, elementPartition);
+      return PartitionUsingThisSplit(this, elementPartition);
   }
   return 1;
 #else
diff --git a/Mesh/meshPartition.cpp b/Mesh/meshPartition.cpp
index d68c8be891..152879bd99 100644
--- a/Mesh/meshPartition.cpp
+++ b/Mesh/meshPartition.cpp
@@ -15,6 +15,7 @@
 #include <cstdlib>
 #include <map>
 #include <unordered_map>
+#include <unordered_set>
 #include "GmshConfig.h"
 #include "GmshMessage.h"
 #include "GModel.h"
@@ -2512,11 +2513,19 @@ int UnpartitionMesh(GModel *model)
 
 // Create the partition according to the element split given by elmToPartition
 // Returns: 0 = success, 1 = no elements found.
-int PartitionUsingThisSplit(GModel *model, int numPart,
+int PartitionUsingThisSplit(GModel *model,
                             std::vector<std::pair<MElement *, int> > &elmToPart)
 {
   Graph graph(model);
   if(makeGraph(model, graph, -1)) return 1;
+  
+  int numPart = 0;
+  std::unordered_set< int > partTags;
+  for(std::size_t i = 0; i < elmToPart.size(); i++) {
+    partTags.insert(elmToPart[i].second);
+  }
+  numPart = partTags.size();
+  
   graph.createDualGraph(false);
   graph.nparts(numPart);
 
@@ -2600,7 +2609,7 @@ int ConvertOldPartitioningToNewOne(GModel *model)
     }
   }
 
-  return PartitionUsingThisSplit(model, partitions.size(), elmToPartition);
+  return PartitionUsingThisSplit(model, elmToPartition);
 }
 
 #else
@@ -2616,7 +2625,7 @@ int UnpartitionMesh(GModel *model) { return 0; }
 int ConvertOldPartitioningToNewOne(GModel *model) { return 0; }
 
 int PartitionUsingThisSplit(
-  GModel *model, std::size_t npart,
+  GModel *model,
   std::vector<std::pair<MElement *, int> > &elmToPartition)
 {
   Msg::Error("Gmsh must be compiled with METIS support to partition meshes");
diff --git a/Mesh/meshPartition.h b/Mesh/meshPartition.h
index bf1fc354e4..22a0a27cda 100644
--- a/Mesh/meshPartition.h
+++ b/Mesh/meshPartition.h
@@ -13,7 +13,7 @@ class GFace;
 int PartitionMesh(GModel *model, int numPart);
 int UnpartitionMesh(GModel *model);
 
-int PartitionUsingThisSplit(GModel *model, int numPart,
+int PartitionUsingThisSplit(GModel *model,
   std::vector<std::pair<MElement *, int> > &elmToPartition);
 int ConvertOldPartitioningToNewOne(GModel *model);
 
diff --git a/Plugin/SimplePartition.cpp b/Plugin/SimplePartition.cpp
index 7868244477..976a0f3596 100644
--- a/Plugin/SimplePartition.cpp
+++ b/Plugin/SimplePartition.cpp
@@ -171,8 +171,7 @@ void GMSH_SimplePartitionPlugin::run()
 
   opt_mesh_partition_create_topology(0, GMSH_SET | GMSH_GUI, createTopology);
 
-  int ier = PartitionUsingThisSplit(m, numSlicesX * numSlicesY * numSlicesZ,
-                                    elmToPartition);
+  int ier = PartitionUsingThisSplit(m, elmToPartition);
   if(!ier) {
     opt_mesh_color_carousel(0, GMSH_SET | GMSH_GUI, 3.);
     CTX::instance()->mesh.changed = ENT_ALL;
-- 
GitLab