From 1cabcf6c1228c706821900de1fd077c2fca0909f Mon Sep 17 00:00:00 2001
From: mattijs <mattijs@hunt.opencfd.co.uk>
Date: Tue, 30 Jun 2009 22:17:03 +0100
Subject: [PATCH] createBaffles with faceZone

---
 .../createBaffles/createBaffles.C             | 336 ++++++++++--------
 .../filter/Allrun                             |  23 +-
 2 files changed, 192 insertions(+), 167 deletions(-)

diff --git a/applications/utilities/mesh/manipulation/createBaffles/createBaffles.C b/applications/utilities/mesh/manipulation/createBaffles/createBaffles.C
index 1fb8cc64efe..88af3a1f291 100644
--- a/applications/utilities/mesh/manipulation/createBaffles/createBaffles.C
+++ b/applications/utilities/mesh/manipulation/createBaffles/createBaffles.C
@@ -43,16 +43,89 @@ Description
 #include "ReadFields.H"
 #include "volFields.H"
 #include "surfaceFields.H"
+#include "ZoneIDs.H"
 
 using namespace Foam;
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
+void modifyOrAddFace
+(
+    polyTopoChange& meshMod,
+    const face& f,
+    const label faceI,
+    const label own,
+    const bool flipFaceFlux,
+    const label newPatchI,
+    const label zoneID,
+    const bool zoneFlip,
+
+    PackedBoolList& modifiedFace
+)
+{
+    if (!modifiedFace[faceI])
+    {
+        // First usage of face. Modify.
+        meshMod.setAction
+        (
+            polyModifyFace
+            (
+                f,                          // modified face
+                faceI,                      // label of face
+                own,                        // owner
+                -1,                         // neighbour
+                flipFaceFlux,               // face flip
+                newPatchI,                  // patch for face
+                false,                      // remove from zone
+                zoneID,                     // zone for face
+                zoneFlip                    // face flip in zone
+            )
+        );
+        modifiedFace[faceI] = 1;
+    }
+    else
+    {
+        // Second or more usage of face. Add.
+        meshMod.setAction
+        (
+            polyAddFace
+            (
+                f,                          // modified face
+                own,                        // owner
+                -1,                         // neighbour
+                -1,                         // master point
+                -1,                         // master edge
+                faceI,                      // master face
+                flipFaceFlux,               // face flip
+                newPatchI,                  // patch for face
+                zoneID,                     // zone for face
+                zoneFlip                    // face flip in zone
+            )
+        );
+    }
+}
+
+
+label findPatchID(const polyMesh& mesh, const word& name)
+{
+    label patchI = mesh.boundaryMesh().findPatchID(name);
+
+    if (patchI == -1)
+    {
+        FatalErrorIn("findPatchID(const polyMesh&, const word&)")
+            << "Cannot find patch " << name << endl
+            << "Valid patches are " << mesh.boundaryMesh().names()
+            << exit(FatalError);
+    }
+    return patchI;
+}
+
+
 // Main program:
 
 int main(int argc, char *argv[])
 {
-    argList::validArgs.append("set");
+    argList::validArgs.append("faceZone");
     argList::validArgs.append("patch");
     argList::validOptions.insert("additionalPatches", "(patch2 .. patchN)");
     argList::validOptions.insert("overwrite", "");
@@ -67,30 +140,28 @@ int main(int argc, char *argv[])
     const faceZoneMesh& faceZones = mesh.faceZones();
 
     // Faces to baffle
-    word setName(args.additionalArgs()[0]);
-    Info<< "Reading faceSet from " << setName << nl << endl;
-    faceSet facesToSplit(mesh, setName);
-    // Make sure set is synchronised across couples
-    facesToSplit.sync(mesh);
-    Info<< "Read " << returnReduce(facesToSplit.size(), sumOp<label>())
-        << " faces from " << setName << nl << endl;
+    faceZoneID zoneID(args.additionalArgs()[0], faceZones);
+
+    Info<< "Converting faces on zone " << zoneID.name()
+        << " into baffles." << nl << endl;
+
+    const faceZone& fZone = faceZones[zoneID.index()];
+
+    Info<< "Found " << returnReduce(fZone.size(), sumOp<label>())
+        << " faces on zone " << zoneID.name() << nl << endl;
 
+    // Make sure patches and zoneFaces are synchronised across couples
+    patches.checkParallelSync(true);
+    fZone.checkParallelSync(true);
 
     // Patches to put baffles into
     DynamicList<label> newPatches(1);
 
     word patchName(args.additionalArgs()[1]);
-    newPatches.append(patches.findPatchID(patchName));
-    Pout<< "Using patch " << patchName
+    newPatches.append(findPatchID(mesh, patchName));
+    Info<< "Using patch " << patchName
         << " at index " << newPatches[0] << endl;
 
-    if (newPatches[0] == -1)
-    {
-        FatalErrorIn(args.executable())
-            << "Cannot find patch " << patchName << endl
-            << "Valid patches are " << patches.names() << exit(FatalError);
-    }
-
 
     // Additional patches
     if (args.optionFound("additionalPatches"))
@@ -103,19 +174,9 @@ int main(int argc, char *argv[])
         newPatches.reserve(patchNames.size() + 1);
         forAll(patchNames, i)
         {
-            label patchI = patches.findPatchID(patchNames[i]);
+            newPatches.append(findPatchID(mesh, patchNames[i]));
             Info<< "Using additional patch " << patchNames[i]
-                << " at index " << patchI << endl;
-
-            if (patchI == -1)
-            {
-                FatalErrorIn(args.executable())
-                    << "Cannot find patch " << patchNames[i] << endl
-                    << "Valid patches are " << patches.names()
-                    << exit(FatalError);
-            }
-
-            newPatches.append(patchI);
+                << " at index " << newPatches[newPatches.size()-1] << endl;
         }
     }
 
@@ -166,111 +227,112 @@ int main(int argc, char *argv[])
     polyTopoChange meshMod(mesh);
 
 
-    // Do the actual changes
-    // Note order in which faces are modified/added is so they end up correctly
-    // for cyclic patches (original faces first and then reversed faces)
-    // since otherwise it will have trouble matching baffles.
+    // Do the actual changes. Note:
+    // - loop in incrementing face order (not necessary if faceZone ordered).
+    //   Preserves any existing ordering on patch faces.
+    // - two passes, do non-flip faces first and flip faces second. This
+    //   guarantees that when e.g. creating a cyclic all faces from one
+    //   side come first and faces from the other side next.
 
-    label nBaffled = 0;
+    PackedBoolList modifiedFace(mesh.nFaces());
 
     forAll(newPatches, i)
     {
         label newPatchI = newPatches[i];
 
+        // Pass 1. Do selected side of zone
+        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
         for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
         {
-            if (facesToSplit.found(faceI))
-            {
-                const face& f = mesh.faces()[faceI];
-                label zoneID = faceZones.whichZone(faceI);
-                bool zoneFlip = false;
-                if (zoneID >= 0)
-                {
-                    const faceZone& fZone = faceZones[zoneID];
-                    zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
-                }
+            label zoneFaceI = fZone.whichFace(faceI);
 
-                if (i == 0)
+            if (zoneFaceI != -1)
+            {
+                if (!fZone.flipMap()[zoneFaceI])
                 {
-                    // First usage of face. Modify.
-                    meshMod.setAction
+                    // Use owner side of face
+                    modifyOrAddFace
                     (
-                        polyModifyFace
-                        (
-                            f,                          // modified face
-                            faceI,                      // label of face
-                            mesh.faceOwner()[faceI],    // owner
-                            -1,                         // neighbour
-                            false,                      // face flip
-                            newPatchI,                  // patch for face
-                            false,                      // remove from zone
-                            zoneID,                     // zone for face
-                            zoneFlip                    // face flip in zone
-                        )
+                        meshMod,
+                        mesh.faces()[faceI],    // modified face
+                        faceI,                  // label of face
+                        mesh.faceOwner()[faceI],// owner
+                        false,                  // face flip
+                        newPatchI,              // patch for face
+                        zoneID.index(),         // zone for face
+                        false,                  // face flip in zone
+                        modifiedFace            // modify or add status
                     );
                 }
                 else
                 {
-                    // Second or more usage of face. Add.
-                    meshMod.setAction
+                    // Use neighbour side of face
+                    modifyOrAddFace
                     (
-                        polyAddFace
-                        (
-                            f,                          // modified face
-                            mesh.faceOwner()[faceI],    // owner
-                            -1,                         // neighbour
-                            -1,                         // master point
-                            -1,                         // master edge
-                            faceI,                      // master face
-                            false,                      // face flip
-                            newPatchI,                  // patch for face
-                            zoneID,                     // zone for face
-                            zoneFlip                    // face flip in zone
-                        )
+                        meshMod,
+                        mesh.faces()[faceI].reverseFace(),  // modified face
+                        faceI,                      // label of face
+                        mesh.faceNeighbour()[faceI],// owner
+                        true,                       // face flip
+                        newPatchI,                  // patch for face
+                        zoneID.index(),             // zone for face
+                        true,                       // face flip in zone
+                        modifiedFace                // modify or add status
                     );
                 }
-
-                nBaffled++;
             }
         }
 
-        // Add the reversed face.
+
+        // Pass 2. Do other side of zone
+        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
         for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
         {
-            if (facesToSplit.found(faceI))
+            label zoneFaceI = fZone.whichFace(faceI);
+
+            if (zoneFaceI != -1)
             {
-                const face& f = mesh.faces()[faceI];
-                label zoneID = faceZones.whichZone(faceI);
-                bool zoneFlip = false;
-                if (zoneID >= 0)
+                if (!fZone.flipMap()[zoneFaceI])
                 {
-                    const faceZone& fZone = faceZones[zoneID];
-                    zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
+                    // Use neighbour side of face
+                    modifyOrAddFace
+                    (
+                        meshMod,
+                        mesh.faces()[faceI].reverseFace(),  // modified face
+                        faceI,                              // label of face
+                        mesh.faceNeighbour()[faceI],        // owner
+                        true,                               // face flip
+                        newPatchI,                          // patch for face
+                        zoneID.index(),                     // zone for face
+                        true,                               // face flip in zone
+                        modifiedFace                        // modify or add
+                    );
                 }
-                label nei = mesh.faceNeighbour()[faceI];
-
-                meshMod.setAction
-                (
-                    polyAddFace
+                else
+                {
+                    // Use owner side of face
+                    modifyOrAddFace
                     (
-                        f.reverseFace(),            // modified face
-                        nei,                        // owner
-                        -1,                         // neighbour
-                        -1,                         // masterPointID
-                        -1,                         // masterEdgeID
-                        faceI,                      // masterFaceID,
-                        true,                       // face flip
-                        newPatchI,                  // patch for face
-                        zoneID,                     // zone for face
-                        zoneFlip                    // face flip in zone
-                    )
-                );
-
-                nBaffled++;
+                        meshMod,
+                        mesh.faces()[faceI],    // modified face
+                        faceI,                  // label of face
+                        mesh.faceOwner()[faceI],// owner
+                        false,                  // face flip
+                        newPatchI,              // patch for face
+                        zoneID.index(),         // zone for face
+                        false,                  // face flip in zone
+                        modifiedFace            // modify or add status
+                    );
+                }
             }
         }
 
+
         // Modify any boundary faces
+        // ~~~~~~~~~~~~~~~~~~~~~~~~~
+
         forAll(patches, patchI)
         {
             const polyPatch& pp = patches[patchI];
@@ -286,58 +348,22 @@ int main(int argc, char *argv[])
                 {
                     label faceI = pp.start()+i;
 
-                    if (facesToSplit.found(faceI))
+                    label zoneFaceI = fZone.whichFace(faceI);
+
+                    if (zoneFaceI != -1)
                     {
-                        const face& f = mesh.faces()[faceI];
-                        label zoneID = faceZones.whichZone(faceI);
-                        bool zoneFlip = false;
-                        if (zoneID >= 0)
-                        {
-                            const faceZone& fZone = faceZones[zoneID];
-                            zoneFlip = fZone.flipMap()[fZone.whichFace(faceI)];
-                        }
-
-                        if (i == 0)
-                        {
-                            // First usage of face. Modify.
-                            meshMod.setAction
-                            (
-                                polyModifyFace
-                                (
-                                    f,                      // modified face
-                                    faceI,                  // label of face
-                                    mesh.faceOwner()[faceI],// owner
-                                    -1,                     // neighbour
-                                    false,                  // face flip
-                                    newPatchI,              // patch for face
-                                    false,                  // remove from zone
-                                    zoneID,                 // zone for face
-                                    zoneFlip                // face flip in zone
-                                )
-                            );
-                        }
-                        else
-                        {
-                            // Second or more usage of face. Add.
-                            meshMod.setAction
-                            (
-                                polyAddFace
-                                (
-                                    f,                      // modified face
-                                    mesh.faceOwner()[faceI],// owner
-                                    -1,                     // neighbour
-                                    -1,                     // master point
-                                    -1,                     // master edge
-                                    faceI,                  // master face
-                                    false,                  // face flip
-                                    newPatchI,              // patch for face
-                                    zoneID,                 // zone for face
-                                    zoneFlip                // face flip in zone
-                                )
-                            );
-                        }
-
-                        nBaffled++;
+                        modifyOrAddFace
+                        (
+                            meshMod,
+                            mesh.faces()[faceI],        // modified face
+                            faceI,                      // label of face
+                            mesh.faceOwner()[faceI],    // owner
+                            false,                      // face flip
+                            newPatchI,                  // patch for face
+                            zoneID.index(),             // zone for face
+                            fZone.flipMap()[zoneFaceI], // face flip in zone
+                            modifiedFace                // modify or add status
+                        );
                     }
                 }
             }
@@ -345,7 +371,7 @@ int main(int argc, char *argv[])
     }
 
 
-    Info<< "Converted " << returnReduce(nBaffled, sumOp<label>())
+    Info<< "Converted " << returnReduce(modifiedFace.count(), sumOp<label>())
         << " faces into boundary faces on patch " << patchName << nl << endl;
 
     if (!overwrite)
diff --git a/tutorials/lagrangian/porousExplicitSourceReactingParcelFoam/filter/Allrun b/tutorials/lagrangian/porousExplicitSourceReactingParcelFoam/filter/Allrun
index 68d74e3f344..8378f1f44bd 100755
--- a/tutorials/lagrangian/porousExplicitSourceReactingParcelFoam/filter/Allrun
+++ b/tutorials/lagrangian/porousExplicitSourceReactingParcelFoam/filter/Allrun
@@ -1,24 +1,23 @@
 #!/bin/sh
 
+# Source tutorial run functions
+. $WM_PROJECT_DIR/bin/tools/RunFunctions
+
+application="porousExplicitSourceReactingParcelFoam"
+
 # create mesh
-blockMesh >& log.blockMesh
+runApplication blockMesh
 
-# create sets - 1
+# create sets
 setSet -batch system/sets.setSet >& log.setSet1
 
+# convert sets to zones
+setsToZones -noFlipMap >& log.setsToZones
+
 # create the first cyclic - lhs of porous zone
 createBaffles cycLeft cycLeft -overwrite >& log.createBaffles1
 
-# create sets - 2 (ordering changed after createBaffles)
-setSet -batch system/sets.setSet >& log.setSet2
-
 # create the second cyclic - rhs of porous zone
 createBaffles cycRight cycRight -overwrite >& log.createBaffles2
 
-# create sets - 3 (ordering changed after createBaffles)
-setSet -batch system/sets.setSet >& log.setSet3
-
-# convert sets to zones
-setsToZones -noFlipMap >& log.setsToZones
-
-
+runApplication $application
-- 
GitLab