From c61466b6e524955e46595531f24166d21544e893 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 31 May 2024 12:42:52 +0100
Subject: [PATCH] ENH: polyPatch cached areaFraction setter with uniform
 fraction

STYLE: polyPatch cached areaFraction as std::unique_ptr

- more consistent with other demand-driven data.
  Getter now returns tmp field instead of const reference.
---
 .../polyPatches/polyPatch/polyPatch.C         | 60 ++++++++++---------
 .../polyPatches/polyPatch/polyPatch.H         | 19 +++---
 ...ureForceBaffleVelocityFvPatchVectorField.C | 41 +++----------
 .../wallDistAddressing/wallDistAddressing.C   |  2 +-
 .../basic/InteractionLists/InteractionLists.C |  2 +-
 .../cyclicAMIPolyPatch/cyclicAMIPolyPatch.C   | 12 ++--
 .../algorithms/MeshWave/FaceCellWave.C        |  2 +-
 src/meshTools/cellDist/cellDistFuncs.C        |  4 +-
 .../cellDist/patchWave/patchDataWave.C        |  2 +-
 src/meshTools/cellDist/patchWave/patchWave.C  |  2 +-
 10 files changed, 63 insertions(+), 83 deletions(-)

diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.C b/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.C
index bc132c24abe..db304f4b2ec 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.C
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.C
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2016 OpenFOAM Foundation
-    Copyright (C) 2018-2023 OpenCFD Ltd.
+    Copyright (C) 2018-2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -95,9 +95,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(start),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {
     if (constraintType(patchType))
     {
@@ -124,9 +122,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(start),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {}
 
 
@@ -151,9 +147,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(dict.get<label>("startFace")),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {
     if (constraintType(patchType))
     {
@@ -180,9 +174,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(pp.start()),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {}
 
 
@@ -207,9 +199,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(newStart),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {}
 
 
@@ -234,9 +224,7 @@ Foam::polyPatch::polyPatch
         bm.mesh().points()
     ),
     start_(newStart),
-    boundaryMesh_(bm),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(bm)
 {}
 
 
@@ -245,9 +233,7 @@ Foam::polyPatch::polyPatch(const polyPatch& p)
     patchIdentifier(p),
     primitivePatch(p),
     start_(p.start_),
-    boundaryMesh_(p.boundaryMesh_),
-    faceCellsPtr_(nullptr),
-    mePtr_(nullptr)
+    boundaryMesh_(p.boundaryMesh_)
 {}
 
 
@@ -362,24 +348,41 @@ Foam::tmp<Foam::scalarField> Foam::polyPatch::areaFraction
 
     forAll(*this, facei)
     {
-        const face& curFace = this->operator[](facei);
-        fraction[facei] =
-            mag(faceAreas[facei])/(curFace.mag(points) + ROOTVSMALL);
+        const face& f = this->operator[](facei);
+        fraction[facei] = faceAreas[facei].mag()/(f.mag(points) + ROOTVSMALL);
     }
 
     return tfraction;
 }
 
 
-const Foam::tmp<Foam::scalarField>& Foam::polyPatch::areaFraction() const
+Foam::tmp<Foam::scalarField> Foam::polyPatch::areaFraction() const
 {
-    return areaFraction_;
+    if (areaFractionPtr_)
+    {
+        return tmp<scalarField>(*areaFractionPtr_);
+    }
+    return nullptr;
+}
+
+
+void Foam::polyPatch::areaFraction(const scalar fraction)
+{
+    areaFractionPtr_ = std::make_unique<scalarField>(size(), fraction);
 }
 
 
 void Foam::polyPatch::areaFraction(const tmp<scalarField>& fraction)
 {
-    areaFraction_ = fraction;
+    if (fraction)
+    {
+        // Steal or clone
+        areaFractionPtr_.reset(fraction.ptr());
+    }
+    else
+    {
+        areaFractionPtr_.reset(nullptr);
+    }
 }
 
 
@@ -427,6 +430,7 @@ void Foam::polyPatch::clearAddressing()
     primitivePatch::clearPatchMeshAddr();
     faceCellsPtr_.reset(nullptr);
     mePtr_.reset(nullptr);
+    areaFractionPtr_.reset(nullptr);
 }
 
 
diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.H b/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.H
index 49d2786bc07..02f9901e20f 100644
--- a/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.H
+++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/polyPatch/polyPatch.H
@@ -6,7 +6,7 @@
      \\/     M anipulation  |
 -------------------------------------------------------------------------------
     Copyright (C) 2011-2015 OpenFOAM Foundation
-    Copyright (C) 2015-2023 OpenCFD Ltd.
+    Copyright (C) 2015-2024 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -89,7 +89,7 @@ class polyPatch
         mutable std::unique_ptr<labelList> mePtr_;
 
         //- Cached area fraction
-        tmp<scalarField> areaFraction_;
+        std::unique_ptr<scalarField> areaFractionPtr_;
 
 
 protected:
@@ -445,15 +445,18 @@ public:
             tmp<vectorField> faceCellCentres() const;
 
             //- Calculate the area fraction as the ratio of the stored face
-            //  area and the area given by the face points.
+            //- area and the area given by the face points.
             tmp<scalarField> areaFraction(const pointField& points) const;
 
-            //- Return the cached area fraction. Usually only set for the
-            //- non-overlap patches on ACMI.
-            const tmp<scalarField>& areaFraction() const;
+            //- Return the cached area fraction.
+            //- Usually only set for the non-overlap patches on ACMI.
+            tmp<scalarField> areaFraction() const;
 
-            //- Set cached area fraction
-            void areaFraction(const tmp<scalarField>&);
+            //- Set uniform cached area fraction
+            void areaFraction(const scalar fraction);
+
+            //- Set cached area fraction (non-uniform)
+            void areaFraction(const tmp<scalarField>& fraction);
 
 
         // Addressing into mesh
diff --git a/src/finiteVolume/fields/fvPatchFields/derived/activePressureForceBaffleVelocity/activePressureForceBaffleVelocityFvPatchVectorField.C b/src/finiteVolume/fields/fvPatchFields/derived/activePressureForceBaffleVelocity/activePressureForceBaffleVelocityFvPatchVectorField.C
index cb80bf364a9..f2b719f5454 100644
--- a/src/finiteVolume/fields/fvPatchFields/derived/activePressureForceBaffleVelocity/activePressureForceBaffleVelocityFvPatchVectorField.C
+++ b/src/finiteVolume/fields/fvPatchFields/derived/activePressureForceBaffleVelocity/activePressureForceBaffleVelocityFvPatchVectorField.C
@@ -317,16 +317,10 @@ void Foam::activePressureForceBaffleVelocityFvPatchVectorField::updateCoeffs()
             Info<< "Open fraction = " << openFraction_ << endl;
         }
 
-        scalar areaFraction = 0.0;
-
-        if (opening_)
-        {
-            areaFraction = openFraction_;
-        }
-        else
-        {
-            areaFraction = 1 - openFraction_;
-        }
+        const scalar areaFraction =
+        (
+            opening_ ? openFraction_ : (1 - openFraction_)
+        );
 
         if (patch().boundaryMesh().mesh().moving())
         {
@@ -352,40 +346,19 @@ void Foam::activePressureForceBaffleVelocityFvPatchVectorField::updateCoeffs()
         }
         const_cast<scalarField&>(patch().magSf()) = mag(patch().Sf());
         // Cache fraction
-        const_cast<polyPatch&>(patch().patch()).areaFraction
-        (
-            tmp<scalarField>::New
-            (
-                patch().patch().size(),
-                (1-areaFraction)
-            )
-        );
+        const_cast<polyPatch&>(patch().patch()).areaFraction(1-areaFraction);
 
 
         // Update owner side of cyclic
         const_cast<vectorField&>(cyclicPatch.Sf()) = areaFraction*initCyclicSf_;
         const_cast<scalarField&>(cyclicPatch.magSf()) = mag(cyclicPatch.Sf());
-        const_cast<polyPatch&>(cyclicPatch.patch()).areaFraction
-        (
-            tmp<scalarField>::New
-            (
-                cyclicPatch.patch().size(),
-                areaFraction
-            )
-        );
+        const_cast<polyPatch&>(cyclicPatch.patch()).areaFraction(areaFraction);
 
 
         // Update neighbour side of cyclic
         const_cast<vectorField&>(nbrPatch.Sf()) = areaFraction*nbrCyclicSf_;
         const_cast<scalarField&>(nbrPatch.magSf()) = mag(nbrPatch.Sf());
-        const_cast<polyPatch&>(cyclicPatch.patch()).areaFraction
-        (
-            tmp<scalarField>::New
-            (
-                nbrPatch.patch().size(),
-                areaFraction
-            )
-        );
+        const_cast<polyPatch&>(nbrPatch.patch()).areaFraction(areaFraction);
 
         curTimeIndex_ = this->db().time().timeIndex();
     }
diff --git a/src/finiteVolume/fvMesh/wallDist/wallDistAddressing/wallDistAddressing.C b/src/finiteVolume/fvMesh/wallDist/wallDistAddressing/wallDistAddressing.C
index 67a20853af9..dad43a7d9fb 100644
--- a/src/finiteVolume/fvMesh/wallDist/wallDistAddressing/wallDistAddressing.C
+++ b/src/finiteVolume/fvMesh/wallDist/wallDistAddressing/wallDistAddressing.C
@@ -154,7 +154,7 @@ void Foam::wallDistAddressing::correct(volScalarField& y)
     for (const label patchi : patchIDs_)
     {
         const auto& fc = C.boundaryField()[patchi];
-        const auto& areaFraction = fc.patch().patch().areaFraction();
+        const auto areaFraction(fc.patch().patch().areaFraction());
 
         forAll(fc, patchFacei)
         {
diff --git a/src/lagrangian/basic/InteractionLists/InteractionLists.C b/src/lagrangian/basic/InteractionLists/InteractionLists.C
index edae174aa47..6d8b65e8394 100644
--- a/src/lagrangian/basic/InteractionLists/InteractionLists.C
+++ b/src/lagrangian/basic/InteractionLists/InteractionLists.C
@@ -299,7 +299,7 @@ void Foam::InteractionLists<ParticleType>::buildInteractionLists()
     {
         if (isA<wallPolyPatch>(patch))
         {
-            const auto& areaFraction = patch.areaFraction();
+            const auto areaFraction(patch.areaFraction());
 
             forAll(patch, facei)
             {
diff --git a/src/meshTools/AMIInterpolation/patches/cyclicAMI/cyclicAMIPolyPatch/cyclicAMIPolyPatch.C b/src/meshTools/AMIInterpolation/patches/cyclicAMI/cyclicAMIPolyPatch/cyclicAMIPolyPatch.C
index 266dc991d77..62a18bbc54a 100644
--- a/src/meshTools/AMIInterpolation/patches/cyclicAMI/cyclicAMIPolyPatch/cyclicAMIPolyPatch.C
+++ b/src/meshTools/AMIInterpolation/patches/cyclicAMI/cyclicAMIPolyPatch/cyclicAMIPolyPatch.C
@@ -558,12 +558,12 @@ void Foam::cyclicAMIPolyPatch::movePoints
     polyPatch::movePoints
      -> primitivePatch::movePoints
         -> primitivePatch::clearGeom:
-    deleteDemandDrivenData(localPointsPtr_);
-    deleteDemandDrivenData(faceCentresPtr_);
-    deleteDemandDrivenData(faceAreasPtr_);
-    deleteDemandDrivenData(magFaceAreasPtr_);
-    deleteDemandDrivenData(faceNormalsPtr_);
-    deleteDemandDrivenData(pointNormalsPtr_);
+    localPointsPtr_.reset(nullptr);
+    faceCentresPtr_.reset(nullptr);
+    faceAreasPtr_.reset(nullptr);
+    magFaceAreasPtr_.reset(nullptr);
+    faceNormalsPtr_.reset(nullptr);
+    pointNormalsPtr_.reset(nullptr);
 */
 }
 
diff --git a/src/meshTools/algorithms/MeshWave/FaceCellWave.C b/src/meshTools/algorithms/MeshWave/FaceCellWave.C
index b96792829bb..6123f5c6bcf 100644
--- a/src/meshTools/algorithms/MeshWave/FaceCellWave.C
+++ b/src/meshTools/algorithms/MeshWave/FaceCellWave.C
@@ -822,7 +822,7 @@ void Foam::FaceCellWave<Type, TrackingData>::handleAMICyclicPatches()
 
             // Merge into global storage
 
-            const auto& areaFraction = patch.areaFraction();
+            const auto areaFraction(patch.areaFraction());
 
             forAll(receiveInfo, i)
             {
diff --git a/src/meshTools/cellDist/cellDistFuncs.C b/src/meshTools/cellDist/cellDistFuncs.C
index 1b064dc391c..86cfdd15880 100644
--- a/src/meshTools/cellDist/cellDistFuncs.C
+++ b/src/meshTools/cellDist/cellDistFuncs.C
@@ -249,7 +249,7 @@ void Foam::cellDistFuncs::correctBoundaryFaceCells
         if (patchIDs.found(patchi))
         {
             const polyPatch& patch = pbm[patchi];
-            const auto& areaFraction = patch.areaFraction();
+            const auto areaFraction(patch.areaFraction());
 
             // Check cells with face on wall
             forAll(patch, patchFacei)
@@ -306,7 +306,7 @@ void Foam::cellDistFuncs::correctBoundaryPointCells
 
             bitSet isWallPoint(meshPoints.size(), true);
             {
-                const auto& areaFraction = patch.areaFraction();
+                const auto areaFraction(patch.areaFraction());
 
                 // Check cells with face on wall
                 forAll(patch, patchFacei)
diff --git a/src/meshTools/cellDist/patchWave/patchDataWave.C b/src/meshTools/cellDist/patchWave/patchDataWave.C
index 293018e8d91..9f943225c3e 100644
--- a/src/meshTools/cellDist/patchWave/patchDataWave.C
+++ b/src/meshTools/cellDist/patchWave/patchDataWave.C
@@ -53,7 +53,7 @@ void Foam::patchDataWave<TransferType, TrackingData>::setChangedFaces
         {
             const polyPatch& patch = mesh.boundaryMesh()[patchi];
 
-            const auto& areaFraction = patch.areaFraction();
+            const auto areaFraction(patch.areaFraction());
 
             const auto faceCentres(patch.faceCentres());
 
diff --git a/src/meshTools/cellDist/patchWave/patchWave.C b/src/meshTools/cellDist/patchWave/patchWave.C
index e8d583fb238..deeb062d407 100644
--- a/src/meshTools/cellDist/patchWave/patchWave.C
+++ b/src/meshTools/cellDist/patchWave/patchWave.C
@@ -48,7 +48,7 @@ void Foam::patchWave::setChangedFaces
         {
             const polyPatch& patch = mesh.boundaryMesh()[patchi];
 
-            const auto& areaFraction = patch.areaFraction();
+            const auto areaFraction(patch.areaFraction());
 
             const auto faceCentres(patch.faceCentres());
 
-- 
GitLab