From 40315b21c073b6b20806143b1f26ded8919e3d3b Mon Sep 17 00:00:00 2001
From: graham <g.macpherson@opencfd.co.uk>
Date: Fri, 3 Dec 2010 22:15:47 +0000
Subject: [PATCH] ENH: Allow a weightField volScalarField to be specified in
 decomposeParDict.

Weights the cells in the decomposition.
---
 .../decomposePar/decomposeParDict             |  9 +--
 .../decomposePar/domainDecomposition.C        |  5 +-
 .../decomposePar/domainDecomposition.H        |  5 +-
 .../domainDecompositionDistribute.C           | 79 +++++++++++++++++--
 4 files changed, 82 insertions(+), 16 deletions(-)

diff --git a/applications/utilities/parallelProcessing/decomposePar/decomposeParDict b/applications/utilities/parallelProcessing/decomposePar/decomposeParDict
index 81b087456d2..afa8e1e21de 100644
--- a/applications/utilities/parallelProcessing/decomposePar/decomposeParDict
+++ b/applications/utilities/parallelProcessing/decomposePar/decomposeParDict
@@ -19,15 +19,17 @@ FoamFile
 
 numberOfSubdomains  8;
 
-
 //- Keep owner and neighbour on same processor for faces in zones:
 // preserveFaceZones (heater solid1 solid3);
 
-
 //- Keep owner and neighbour on same processor for faces in patches:
 //  (makes sense only for cyclic patches)
 //preservePatches (cyclic_half0 cyclic_half1);
 
+//- Use the volScalarField named here as a weight for each cell in the
+//  decomposition.  For example, use a particle population field to decompose
+//  for a balanced number of particles in a lagrangian simulation.
+// weightField dsmcRhoNMean;
 
 method          scotch;
 // method          hierarchical;
@@ -59,11 +61,8 @@ multiLevelCoeffs
     }
 }
 
-
 // Desired output
 
-
-
 simpleCoeffs
 {
     n           (2 1 1);
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.C b/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.C
index 785cf8e975b..d69c2c4dfd0 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.C
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.C
@@ -24,7 +24,6 @@ License
 \*---------------------------------------------------------------------------*/
 
 #include "domainDecomposition.H"
-#include "Time.H"
 #include "dictionary.H"
 #include "labelIOList.H"
 #include "processorPolyPatch.H"
@@ -341,10 +340,10 @@ bool Foam::domainDecomposition::writeDecomposition()
         const labelList& curProcessorPatchStarts =
             procProcessorPatchStartIndex_[procI];
 
-        const labelListList& curSubPatchIDs = 
+        const labelListList& curSubPatchIDs =
             procProcessorPatchSubPatchIDs_[procI];
 
-        const labelListList& curSubStarts = 
+        const labelListList& curSubStarts =
             procProcessorPatchSubPatchStarts_[procI];
 
         const polyPatchList& meshPatches = boundaryMesh();
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.H b/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.H
index d916d11ab1e..74ffcbfaf75 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.H
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecomposition.H
@@ -41,6 +41,9 @@ SourceFiles
 #include "SLList.H"
 #include "PtrList.H"
 #include "point.H"
+#include "Time.H"
+#include "volFields.H"
+
 
 namespace Foam
 {
@@ -80,7 +83,7 @@ class domainDecomposition
         // original face. In order to do this properly, all face
         // indices will be incremented by 1 and the decremented as
         // necessary to avoid the problem of face number zero having no
-        // sign.  
+        // sign.
         List<DynamicList<label> > procFaceAddressing_;
 
         //- Labels of cells for each processor
diff --git a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDistribute.C b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDistribute.C
index 6544ab769f3..2c0067df2fa 100644
--- a/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDistribute.C
+++ b/applications/utilities/parallelProcessing/decomposePar/domainDecompositionDistribute.C
@@ -114,7 +114,35 @@ void Foam::domainDecomposition::distributeCells()
 
     if (sameProcFaces.empty())
     {
-        cellToProc_ = decomposePtr().decompose(*this, cellCentres());
+        if (decompositionDict_.found("weightField"))
+        {
+            word weightName = decompositionDict_.lookup("weightField");
+
+            volScalarField weights
+            (
+                IOobject
+                (
+                    weightName,
+                    time().timeName(),
+                    *this,
+                    IOobject::MUST_READ,
+                    IOobject::NO_WRITE
+                ),
+                *this
+            );
+
+            cellToProc_ = decomposePtr().decompose
+            (
+                *this,
+                cellCentres(),
+                weights.internalField()
+            );
+        }
+        else
+        {
+            cellToProc_ = decomposePtr().decompose(*this, cellCentres());
+        }
+
     }
     else
     {
@@ -173,12 +201,49 @@ void Foam::domainDecomposition::distributeCells()
 
         // Do decomposition on agglomeration
         // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-        cellToProc_ = decomposePtr().decompose
-        (
-            *this,
-            globalRegion,
-            regionCentres
-        );
+        if (decompositionDict_.found("weightField"))
+        {
+            scalarField regionWeights(globalRegion.nRegions(), 0);
+
+            word weightName = decompositionDict_.lookup("weightField");
+
+            volScalarField weights
+            (
+                IOobject
+                (
+                    weightName,
+                    time().timeName(),
+                    *this,
+                    IOobject::MUST_READ,
+                    IOobject::NO_WRITE
+                ),
+                *this
+            );
+
+            forAll(globalRegion, cellI)
+            {
+                label regionI = globalRegion[cellI];
+
+                regionWeights[regionI] += weights.internalField()[cellI];
+            }
+
+            cellToProc_ = decomposePtr().decompose
+            (
+                *this,
+                globalRegion,
+                regionCentres,
+                regionWeights
+            );
+        }
+        else
+        {
+            cellToProc_ = decomposePtr().decompose
+            (
+                *this,
+                globalRegion,
+                regionCentres
+            );
+        }
     }
 
     Info<< "\nFinished decomposition in "
-- 
GitLab