From 2b05a8d15183788c419cf8fa95764b02574f5c2c Mon Sep 17 00:00:00 2001
From: Stephane Del Pino <stephane.delpino44@gmail.com>
Date: Sun, 2 Feb 2025 22:38:55 +0100
Subject: [PATCH] Add cell_owner and node_owner (mesh->item_value) functions

This is useful to check mesh partition/load balance
---
 src/language/modules/MeshModule.cpp | 40 +++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/language/modules/MeshModule.cpp b/src/language/modules/MeshModule.cpp
index c060da50d..9b698b9e0 100644
--- a/src/language/modules/MeshModule.cpp
+++ b/src/language/modules/MeshModule.cpp
@@ -308,6 +308,46 @@ MeshModule::MeshModule()
                                               return DualMeshManager::instance().getMedianDualMesh(mesh_v);
                                             }
 
+                                            ));
+
+  this->_addBuiltinFunction("cell_owner", std::function(
+
+                                            [](const std::shared_ptr<const MeshVariant>& mesh_v)
+                                              -> std::shared_ptr<const ItemValueVariant> {
+                                              return std::visit(
+                                                [&](auto&& mesh) {
+                                                  const auto& connectivity = mesh->connectivity();
+                                                  auto cell_owner          = connectivity.cellOwner();
+                                                  CellValue<long int> cell_owner_long{connectivity};
+                                                  parallel_for(
+                                                    connectivity.numberOfCells(), PUGS_LAMBDA(const CellId cell_id) {
+                                                      cell_owner_long[cell_id] = cell_owner[cell_id];
+                                                    });
+                                                  return std::make_shared<const ItemValueVariant>(cell_owner_long);
+                                                },
+                                                mesh_v->variant());
+                                            }
+
+                                            ));
+
+  this->_addBuiltinFunction("node_owner", std::function(
+
+                                            [](const std::shared_ptr<const MeshVariant>& mesh_v)
+                                              -> std::shared_ptr<const ItemValueVariant> {
+                                              return std::visit(
+                                                [&](auto&& mesh) {
+                                                  const auto& connectivity = mesh->connectivity();
+                                                  auto node_owner          = connectivity.nodeOwner();
+                                                  NodeValue<long int> node_owner_long{connectivity};
+                                                  parallel_for(
+                                                    connectivity.numberOfNodes(), PUGS_LAMBDA(const NodeId node_id) {
+                                                      node_owner_long[node_id] = node_owner[node_id];
+                                                    });
+                                                  return std::make_shared<const ItemValueVariant>(node_owner_long);
+                                                },
+                                                mesh_v->variant());
+                                            }
+
                                             ));
 }
 
-- 
GitLab