From 7be0eca6cc6d06345ac74b28a5ea0658ec0fffb5 Mon Sep 17 00:00:00 2001
From: Anna Wellmann <a.wellmann@tu-braunschweig.de>
Date: Tue, 21 Sep 2021 12:12:17 +0200
Subject: [PATCH] Add method to reorder send and recvIndices

Find all send and recv indices which also are interface cells fine to coarse, coarse (iCellFCC).
Move them to the front of the vector of send and recv indices. Return their number as well.
---
 .../grid/GridBuilder/GridBuilder.h            |  4 ++
 .../grid/GridBuilder/LevelGridBuilder.cpp     | 51 +++++++++++++++++++
 .../grid/GridBuilder/LevelGridBuilder.h       |  5 ++
 .../VirtualFluids_GPU/Parameter/Parameter.h   |  7 +++
 4 files changed, 67 insertions(+)

diff --git a/src/gpu/GridGenerator/grid/GridBuilder/GridBuilder.h b/src/gpu/GridGenerator/grid/GridBuilder/GridBuilder.h
index 55dd90545..9548cdac7 100644
--- a/src/gpu/GridGenerator/grid/GridBuilder/GridBuilder.h
+++ b/src/gpu/GridGenerator/grid/GridBuilder/GridBuilder.h
@@ -86,6 +86,10 @@ public:
     virtual uint getNumberOfReceiveIndices( int direction, uint level ) = 0;
     virtual void getSendIndices( int* sendIndices, int direction, int level ) = 0;
     virtual void getReceiveIndices( int* sendIndices, int direction, int level ) = 0;
+    virtual void reorderSendRecvIndexForCommAfterFtoC(int *sendIndices, int *recvIndices,
+                                                      int &numberOfSendNeighborsAfterFtoC,
+                                                      int &numberOfRecvNeighborsAfterFtoC, uint *iCellFCCBorder,
+                                                      uint sizeOfICellFCCBorder, int direction, int level) = 0;
 
     virtual uint getNumberOfFluidNodes(unsigned int level) const = 0;
     virtual void getFluidNodeIndices(uint *fluidNodeIndices, const int level) const = 0;
diff --git a/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.cpp b/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.cpp
index e4b7861fc..d9182fc54 100644
--- a/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.cpp
+++ b/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.cpp
@@ -266,6 +266,57 @@ GRIDGENERATOR_EXPORT void LevelGridBuilder::getReceiveIndices(int * receiveIndic
     }
 }
 
+GRIDGENERATOR_EXPORT void LevelGridBuilder::reorderSendRecvIndexForCommAfterFtoC(int *sendIndices, int *recvIndices,
+                                                                                 int &numberOfSendNeighborsAfterFtoC,
+                                                                                 int &numberOfRecvNeighborsAfterFtoC,
+                                                                                 uint* iCellFCCBorder, 
+                                                                                 uint sizeOfICellFCCBorder,
+                                                                                 int direction, int level)
+{
+    uint numberOfIndices = getNumberOfSendIndices(direction, level);
+    int sparseIndexSend;
+    bool isInICellFCCBorder;
+    std::vector<int> sendIndicesAfterFtoC;
+    std::vector<int> sendIndicesOther;
+    std::vector<int> recvIndicesAfterFtoC;
+    std::vector<int> recvIndicesOther;
+
+    for (uint i = 0; i < numberOfIndices; i++) {
+        sparseIndexSend = sendIndices[i];
+
+        // check if sparse index is in ICellFCC border
+        isInICellFCCBorder = false;
+        for (uint j = 0; j < sizeOfICellFCCBorder; j++) {
+            if (iCellFCCBorder[j] == sparseIndexSend) {
+                isInICellFCCBorder = true;
+                break;
+            }
+        }
+
+        // add index to corresponding vector
+        if (isInICellFCCBorder) {
+            sendIndicesAfterFtoC.push_back(sparseIndexSend);
+            recvIndicesAfterFtoC.push_back(sparseIndexSend);
+        } else {
+            sendIndicesOther.push_back(recvIndices[i]);
+            recvIndicesOther.push_back(recvIndices[i]);
+        }
+    }
+
+    numberOfSendNeighborsAfterFtoC = sendIndicesAfterFtoC.size();
+    numberOfRecvNeighborsAfterFtoC = recvIndicesAfterFtoC.size();
+
+    // copy new vectors back to sendIndices and receive indices arrays
+    for (uint i = 0; i < numberOfSendNeighborsAfterFtoC; i++) {
+        sendIndices[i] = sendIndicesAfterFtoC[i];
+        recvIndices[i] = recvIndicesAfterFtoC[i];
+    }
+    for (uint i = 0; i < sendIndicesOther.size(); i++) {
+        sendIndices[i + numberOfSendNeighborsAfterFtoC] = sendIndicesOther[i];
+        recvIndices[i + numberOfRecvNeighborsAfterFtoC] = recvIndicesOther[i];
+    }
+}
+
 uint LevelGridBuilder::getNumberOfNodes(unsigned int level) const
 {
     return grids[level]->getSparseSize();
diff --git a/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.h b/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.h
index a3cab9c6e..fdaa6e25d 100644
--- a/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.h
+++ b/src/gpu/GridGenerator/grid/GridBuilder/LevelGridBuilder.h
@@ -149,6 +149,11 @@ public:
     GRIDGENERATOR_EXPORT uint getNumberOfReceiveIndices( int direction, uint level ) override;
     GRIDGENERATOR_EXPORT void getSendIndices( int* sendIndices, int direction, int level ) override;
     GRIDGENERATOR_EXPORT void getReceiveIndices( int* sendIndices, int direction, int level ) override;
+    GRIDGENERATOR_EXPORT void reorderSendRecvIndexForCommAfterFtoC(int *sendIndices, int *recvIndices,
+                                                                   int &numberOfSendNeighborsAfterFtoC,
+                                                                   int &numberOfRecvNeighborsAfterFtoC,
+                                                                   uint *iCellFCCBorder, uint sizeOfICellFCCBorder,
+                                                                   int direction, int level) override;
 
 };
 
diff --git a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
index 09926c8cb..c09a50a83 100644
--- a/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
+++ b/src/gpu/VirtualFluids_GPU/Parameter/Parameter.h
@@ -293,6 +293,13 @@ struct LBMSimulationParameter
     std::vector<ProcessNeighbor27> recvProcessNeighborX;
     std::vector<ProcessNeighbor27> recvProcessNeighborY;
     std::vector<ProcessNeighbor27> recvProcessNeighborZ;
+
+    std::vector<int> numberOfSendProcessNeighborsAfterFtoCX;
+    std::vector<int> numberOfSendProcessNeighborsAfterFtoCY;
+    std::vector<int> numberOfSendProcessNeighborsAfterFtoCZ;
+    std::vector<int> numberOfRecvProcessNeighborsAfterFtoCX;
+    std::vector<int> numberOfRecvProcessNeighborsAfterFtoCY;
+    std::vector<int> numberOfRecvProcessNeighborsAfterFtoCZ;
     ///////////////////////////////////////////////////////
     // 3D domain decomposition convection diffusion
     std::vector<ProcessNeighbor27> sendProcessNeighborADX;
-- 
GitLab