From cd8bc891f0ea0651fada48f19ffa51765d0299a1 Mon Sep 17 00:00:00 2001
From: Kutalmis Bercin <kutalmis.bercin@esi-group.com>
Date: Tue, 27 Feb 2024 13:40:36 +0000
Subject: [PATCH] ENH: fvMatrix, faMatrix:  replace raw pointers with
 unique_ptr (#3107)

---
 .../reactingMultiphaseEulerFoam/pUf/pEqn.H    |  5 +-
 src/finiteArea/faMatrices/faMatrix/faMatrix.C | 52 ++++++++----------
 src/finiteArea/faMatrices/faMatrix/faMatrix.H | 11 +++-
 .../gaussFaLaplacianScheme.C                  |  6 +-
 .../gaussLaplacianScheme.C                    |  2 +-
 .../gaussLaplacianSchemes.C                   |  6 +-
 .../relaxedNonOrthoGaussLaplacianScheme.C     |  2 +-
 .../relaxedNonOrthoGaussLaplacianSchemes.C    |  2 +-
 .../fvMatrices/fvMatrix/fvMatrix.C            | 55 +++++++++----------
 .../fvMatrices/fvMatrix/fvMatrix.H            | 11 +++-
 10 files changed, 76 insertions(+), 76 deletions(-)

diff --git a/applications/solvers/multiphase/reactingMultiphaseEulerFoam/pUf/pEqn.H b/applications/solvers/multiphase/reactingMultiphaseEulerFoam/pUf/pEqn.H
index 08d97031b04..b66aabffce6 100644
--- a/applications/solvers/multiphase/reactingMultiphaseEulerFoam/pUf/pEqn.H
+++ b/applications/solvers/multiphase/reactingMultiphaseEulerFoam/pUf/pEqn.H
@@ -272,10 +272,7 @@ while (pimple.correct())
                     ).ptr()
                 );
 
-                deleteDemandDrivenData
-                (
-                    pEqnComps[phasei].faceFluxCorrectionPtr()
-                );
+                pEqnComps[phasei].faceFluxCorrectionPtr(nullptr);
 
                 pEqnComps[phasei].relax();
             }
diff --git a/src/finiteArea/faMatrices/faMatrix/faMatrix.C b/src/finiteArea/faMatrices/faMatrix/faMatrix.C
index 9db099316f2..ac90e710128 100644
--- a/src/finiteArea/faMatrices/faMatrix/faMatrix.C
+++ b/src/finiteArea/faMatrices/faMatrix/faMatrix.C
@@ -190,8 +190,7 @@ Foam::faMatrix<Type>::faMatrix
     dimensions_(ds),
     source_(psi.size(), Zero),
     internalCoeffs_(psi.mesh().boundary().size()),
-    boundaryCoeffs_(psi.mesh().boundary().size()),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(psi.mesh().boundary().size())
 {
     DebugInFunction
         << "constructing faMatrix<Type> for field " << psi_.name()
@@ -231,19 +230,17 @@ Foam::faMatrix<Type>::faMatrix(const faMatrix<Type>& fam)
     dimensions_(fam.dimensions_),
     source_(fam.source_),
     internalCoeffs_(fam.internalCoeffs_),
-    boundaryCoeffs_(fam.boundaryCoeffs_),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(fam.boundaryCoeffs_)
 {
     DebugInFunction
         << "Copying faMatrix<Type> for field " << psi_.name() << endl;
 
     if (fam.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, faePatchField, edgeMesh>
-            (
-                *(fam.faceFluxCorrectionPtr_)
-            );
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            *(fam.faceFluxCorrectionPtr_)
+        );
     }
 }
 
@@ -256,8 +253,7 @@ Foam::faMatrix<Type>::faMatrix(const tmp<faMatrix<Type>>& tmat)
     dimensions_(tmat().dimensions_),
     source_(tmat.constCast().source_, tmat.movable()),
     internalCoeffs_(tmat.constCast().internalCoeffs_, tmat.movable()),
-    boundaryCoeffs_(tmat.constCast().boundaryCoeffs_, tmat.movable()),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(tmat.constCast().boundaryCoeffs_, tmat.movable())
 {
     DebugInFunction
         << "Copy/Move faMatrix<Type> for field " << psi_.name() << endl;
@@ -266,16 +262,15 @@ Foam::faMatrix<Type>::faMatrix(const tmp<faMatrix<Type>>& tmat)
     {
         if (tmat.movable())
         {
-            faceFluxCorrectionPtr_ = tmat().faceFluxCorrectionPtr_;
-            tmat().faceFluxCorrectionPtr_ = nullptr;
+            faceFluxCorrectionPtr_ =
+                std::move(tmat.constCast().faceFluxCorrectionPtr_);
         }
-        else
+        else if (tmat().faceFluxCorrectionPtr_)
         {
-            faceFluxCorrectionPtr_ =
-                new GeometricField<Type, faePatchField, edgeMesh>
-                (
-                    *(tmat().faceFluxCorrectionPtr_)
-                );
+            faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+            (
+                *(tmat().faceFluxCorrectionPtr_)
+            );
         }
     }
 
@@ -290,8 +285,6 @@ Foam::faMatrix<Type>::~faMatrix()
 {
     DebugInFunction
         << "Destroying faMatrix<Type> for field " << psi_.name() << endl;
-
-    deleteDemandDrivenData(faceFluxCorrectionPtr_);
 }
 
 
@@ -788,9 +781,10 @@ void Foam::faMatrix<Type>::operator=(const faMatrix<Type>& famv)
     }
     else if (famv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, faePatchField, edgeMesh>
-            (*famv.faceFluxCorrectionPtr_);
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            *famv.faceFluxCorrectionPtr_
+        );
     }
 }
 
@@ -835,8 +829,7 @@ void Foam::faMatrix<Type>::operator+=(const faMatrix<Type>& famv)
     }
     else if (famv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ = new
-        GeometricField<Type, faePatchField, edgeMesh>
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
         (
             *famv.faceFluxCorrectionPtr_
         );
@@ -869,9 +862,10 @@ void Foam::faMatrix<Type>::operator-=(const faMatrix<Type>& famv)
     }
     else if (famv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, faePatchField, edgeMesh>
-            (-*famv.faceFluxCorrectionPtr_);
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            -*famv.faceFluxCorrectionPtr_
+        );
     }
 }
 
diff --git a/src/finiteArea/faMatrices/faMatrix/faMatrix.H b/src/finiteArea/faMatrices/faMatrix/faMatrix.H
index dd56ad68dbf..0b4681537c0 100644
--- a/src/finiteArea/faMatrices/faMatrix/faMatrix.H
+++ b/src/finiteArea/faMatrices/faMatrix/faMatrix.H
@@ -151,7 +151,7 @@ private:
         FieldField<Field, Type> boundaryCoeffs_;
 
         //- Face flux field for non-orthogonal correction
-        mutable faceFluxFieldType* faceFluxCorrectionPtr_;
+        mutable std::unique_ptr<faceFluxFieldType> faceFluxCorrectionPtr_;
 
 
 protected:
@@ -353,8 +353,7 @@ public:
             }
 
             //- Declare return type of the faceFluxCorrectionPtr() function
-            typedef GeometricField<Type, faePatchField, edgeMesh>
-                *faceFluxFieldPtrType;
+            typedef std::unique_ptr<faceFluxFieldType> faceFluxFieldPtrType;
 
             //- Return pointer to face-flux non-orthogonal correction field
             faceFluxFieldPtrType& faceFluxCorrectionPtr()
@@ -362,6 +361,12 @@ public:
                 return faceFluxCorrectionPtr_;
             }
 
+            //- Set pointer to face-flux non-orthogonal correction field
+            void faceFluxCorrectionPtr(faceFluxFieldType* flux)
+            {
+                faceFluxCorrectionPtr_.reset(flux);
+            }
+
             //- True if face-flux non-orthogonal correction field exists
             bool hasFaceFluxCorrection() const noexcept
             {
diff --git a/src/finiteArea/finiteArea/laplacianSchemes/gaussFaLaplacianScheme/gaussFaLaplacianScheme.C b/src/finiteArea/finiteArea/laplacianSchemes/gaussFaLaplacianScheme/gaussFaLaplacianScheme.C
index 46e7d4bb4f3..b5236b2369e 100644
--- a/src/finiteArea/finiteArea/laplacianSchemes/gaussFaLaplacianScheme/gaussFaLaplacianScheme.C
+++ b/src/finiteArea/finiteArea/laplacianSchemes/gaussFaLaplacianScheme/gaussFaLaplacianScheme.C
@@ -82,8 +82,10 @@ gaussLaplacianScheme<Type>::famLaplacian
     {
         if (this->mesh().fluxRequired(vf.name()))
         {
-            fam.faceFluxCorrectionPtr() = new
-            GeometricField<Type, faePatchField, edgeMesh>
+            fam.faceFluxCorrectionPtr() = std::make_unique
+            <
+                GeometricField<Type, faePatchField, edgeMesh>
+            >
             (
                 gammaMagSf*this->tlnGradScheme_().correction(vf)
             );
diff --git a/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianScheme.C b/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianScheme.C
index 20f20e80e1b..27f420f72ee 100644
--- a/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianScheme.C
+++ b/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianScheme.C
@@ -194,7 +194,7 @@ gaussLaplacianScheme<Type, GType>::fvmLaplacian
 
     if (mesh.fluxRequired(vf.name()))
     {
-        fvm.faceFluxCorrectionPtr() = tfaceFluxCorrection.ptr();
+        fvm.faceFluxCorrectionPtr(tfaceFluxCorrection.ptr());
     }
 
     return tfvm;
diff --git a/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianSchemes.C b/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianSchemes.C
index 1201bad1fe2..7d19f5b2656 100644
--- a/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianSchemes.C
+++ b/src/finiteVolume/finiteVolume/laplacianSchemes/gaussLaplacianScheme/gaussLaplacianSchemes.C
@@ -61,8 +61,10 @@ Foam::fv::gaussLaplacianScheme<Foam::Type, Foam::scalar>::fvmLaplacian         \
     {                                                                          \
         if (mesh.fluxRequired(vf.name()))                                      \
         {                                                                      \
-            fvm.faceFluxCorrectionPtr() = new                                  \
-            GeometricField<Type, fvsPatchField, surfaceMesh>                   \
+            fvm.faceFluxCorrectionPtr() = std::make_unique                     \
+            <                                                                  \
+                GeometricField<Type, fvsPatchField, surfaceMesh>               \
+            >                                                                  \
             (                                                                  \
                 gammaMagSf*this->tsnGradScheme_().correction(vf)               \
             );                                                                 \
diff --git a/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianScheme.C b/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianScheme.C
index ef0efdb299f..44e554ad400 100644
--- a/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianScheme.C
+++ b/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianScheme.C
@@ -222,7 +222,7 @@ relaxedNonOrthoGaussLaplacianScheme<Type, GType>::fvmLaplacian
 
     if (mesh.fluxRequired(vf.name()))
     {
-        fvm.faceFluxCorrectionPtr() = trelaxedCorrection.ptr();
+        fvm.faceFluxCorrectionPtr(trelaxedCorrection.ptr());
     }
 
     return tfvm;
diff --git a/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianSchemes.C b/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianSchemes.C
index ce32fd20fbb..662e909dd50 100644
--- a/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianSchemes.C
+++ b/src/finiteVolume/finiteVolume/laplacianSchemes/relaxedNonOrthoGaussLaplacianScheme/relaxedNonOrthoGaussLaplacianSchemes.C
@@ -98,7 +98,7 @@ fvmLaplacian                                                                   \
                                                                                \
         if (mesh.fluxRequired(vf.name()))                                      \
         {                                                                      \
-            fvm.faceFluxCorrectionPtr() = trelaxedCorrection.ptr();            \
+            fvm.faceFluxCorrectionPtr(trelaxedCorrection.ptr());               \
         }                                                                      \
     }                                                                          \
                                                                                \
diff --git a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C
index bf2ab42635a..254436784f3 100644
--- a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C
+++ b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.C
@@ -369,8 +369,7 @@ Foam::fvMatrix<Type>::fvMatrix
     dimensions_(ds),
     source_(psi.size(), Zero),
     internalCoeffs_(psi.mesh().boundary().size()),
-    boundaryCoeffs_(psi.mesh().boundary().size()),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(psi.mesh().boundary().size())
 {
     DebugInFunction
         << "Constructing fvMatrix<Type> for field " << psi_.name() << endl;
@@ -410,19 +409,17 @@ Foam::fvMatrix<Type>::fvMatrix(const fvMatrix<Type>& fvm)
     dimensions_(fvm.dimensions_),
     source_(fvm.source_),
     internalCoeffs_(fvm.internalCoeffs_),
-    boundaryCoeffs_(fvm.boundaryCoeffs_),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(fvm.boundaryCoeffs_)
 {
     DebugInFunction
         << "Copying fvMatrix<Type> for field " << psi_.name() << endl;
 
     if (fvm.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, fvsPatchField, surfaceMesh>
-            (
-                *(fvm.faceFluxCorrectionPtr_)
-            );
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            *(fvm.faceFluxCorrectionPtr_)
+        );
     }
 }
 
@@ -438,8 +435,7 @@ Foam::fvMatrix<Type>::fvMatrix(const tmp<fvMatrix<Type>>& tmat)
     dimensions_(tmat().dimensions_),
     source_(tmat.constCast().source_, tmat.movable()),
     internalCoeffs_(tmat.constCast().internalCoeffs_, tmat.movable()),
-    boundaryCoeffs_(tmat.constCast().boundaryCoeffs_, tmat.movable()),
-    faceFluxCorrectionPtr_(nullptr)
+    boundaryCoeffs_(tmat.constCast().boundaryCoeffs_, tmat.movable())
 {
     DebugInFunction
         << "Copy/move fvMatrix<Type> for field " << psi_.name() << endl;
@@ -448,16 +444,15 @@ Foam::fvMatrix<Type>::fvMatrix(const tmp<fvMatrix<Type>>& tmat)
     {
         if (tmat.movable())
         {
-            faceFluxCorrectionPtr_ = tmat().faceFluxCorrectionPtr_;
-            tmat().faceFluxCorrectionPtr_ = nullptr;
+            faceFluxCorrectionPtr_ =
+                std::move(tmat.constCast().faceFluxCorrectionPtr_);
         }
-        else
+        else if (tmat().faceFluxCorrectionPtr_)
         {
-            faceFluxCorrectionPtr_ =
-                new GeometricField<Type, fvsPatchField, surfaceMesh>
-                (
-                    *(tmat().faceFluxCorrectionPtr_)
-                );
+            faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+            (
+                *(tmat().faceFluxCorrectionPtr_)
+            );
         }
     }
 
@@ -473,7 +468,6 @@ Foam::fvMatrix<Type>::~fvMatrix()
     DebugInFunction
         << "Destroying fvMatrix<Type> for field " << psi_.name() << endl;
 
-    deleteDemandDrivenData(faceFluxCorrectionPtr_);
     subMatrices_.clear();
 }
 
@@ -1577,9 +1571,10 @@ void Foam::fvMatrix<Type>::operator=(const fvMatrix<Type>& fvmv)
     }
     else if (fvmv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, fvsPatchField, surfaceMesh>
-        (*fvmv.faceFluxCorrectionPtr_);
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            *fvmv.faceFluxCorrectionPtr_
+        );
     }
 
     useImplicit_ = fvmv.useImplicit_;
@@ -1631,8 +1626,7 @@ void Foam::fvMatrix<Type>::operator+=(const fvMatrix<Type>& fvmv)
     }
     else if (fvmv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ = new
-        GeometricField<Type, fvsPatchField, surfaceMesh>
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
         (
             *fvmv.faceFluxCorrectionPtr_
         );
@@ -1669,9 +1663,10 @@ void Foam::fvMatrix<Type>::operator-=(const fvMatrix<Type>& fvmv)
     }
     else if (fvmv.faceFluxCorrectionPtr_)
     {
-        faceFluxCorrectionPtr_ =
-            new GeometricField<Type, fvsPatchField, surfaceMesh>
-        (-*fvmv.faceFluxCorrectionPtr_);
+        faceFluxCorrectionPtr_ = std::make_unique<faceFluxFieldType>
+        (
+            -*fvmv.faceFluxCorrectionPtr_
+        );
     }
 }
 
@@ -2002,7 +1997,7 @@ Foam::tmp<Foam::fvMatrix<Type>> Foam::correction
 
     // Delete the faceFluxCorrection from the correction matrix
     // as it does not have a clear meaning or purpose
-    deleteDemandDrivenData(tAcorr.ref().faceFluxCorrectionPtr());
+    tAcorr.ref().faceFluxCorrectionPtr(nullptr);
 
     return tAcorr;
 }
@@ -2018,7 +2013,7 @@ Foam::tmp<Foam::fvMatrix<Type>> Foam::correction
 
     // Delete the faceFluxCorrection from the correction matrix
     // as it does not have a clear meaning or purpose
-    deleteDemandDrivenData(tAcorr.ref().faceFluxCorrectionPtr());
+    tAcorr.ref().faceFluxCorrectionPtr(nullptr);
 
     return tAcorr;
 }
diff --git a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.H b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.H
index c00236f7c9f..78f18d22c3e 100644
--- a/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.H
+++ b/src/finiteVolume/fvMatrices/fvMatrix/fvMatrix.H
@@ -172,7 +172,7 @@ private:
         FieldField<Field, Type> boundaryCoeffs_;
 
         //- Face flux field for non-orthogonal correction
-        mutable faceFluxFieldType* faceFluxCorrectionPtr_;
+        mutable std::unique_ptr<faceFluxFieldType> faceFluxCorrectionPtr_;
 
 
 protected:
@@ -494,8 +494,7 @@ public:
             }
 
             //- Declare return type of the faceFluxCorrectionPtr() function
-            typedef GeometricField<Type, fvsPatchField, surfaceMesh>
-                *faceFluxFieldPtrType;
+            typedef std::unique_ptr<faceFluxFieldType> faceFluxFieldPtrType;
 
             //- Return pointer to face-flux non-orthogonal correction field
             faceFluxFieldPtrType& faceFluxCorrectionPtr()
@@ -503,6 +502,12 @@ public:
                 return faceFluxCorrectionPtr_;
             }
 
+            //- Set pointer to face-flux non-orthogonal correction field
+            void faceFluxCorrectionPtr(faceFluxFieldType* flux)
+            {
+                faceFluxCorrectionPtr_.reset(flux);
+            }
+
             //- True if face-flux non-orthogonal correction field exists
             bool hasFaceFluxCorrection() const noexcept
             {
-- 
GitLab