From c8b091c7010c891086f7a4bc2b9b148a108b599f Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Fri, 5 Jun 2020 16:46:02 +0200
Subject: [PATCH] ENH: reorganize files, minor cleanup

---
 README.md                                     |  2 +-
 {etc => doc}/README.md                        |  0
 src/Allwclean                                 |  2 +-
 src/Allwmake                                  |  2 +-
 src/petsc4Foam/Make/files                     |  2 +-
 .../{csrMatrix => }/solvers/petscSolver.C     | 49 +++-------
 .../{csrMatrix => }/solvers/petscSolver.H     |  6 +-
 src/petsc4Foam/utils/petscCacheManager.H      | 98 ++++++++++---------
 src/petsc4Foam/utils/petscControls.H          |  9 +-
 .../utils/petscLinearSolverContext.H          |  8 +-
 .../utils/petscLinearSolverContexts.H         | 34 +++----
 src/petsc4Foam/utils/petscUtils.C             |  9 +-
 src/petsc4Foam/utils/petscUtils.H             |  6 +-
 src/petsc4Foam/utils/petscWrappedVector.H     | 14 ++-
 14 files changed, 117 insertions(+), 124 deletions(-)
 rename {etc => doc}/README.md (100%)
 rename src/petsc4Foam/{csrMatrix => }/solvers/petscSolver.C (93%)
 rename src/petsc4Foam/{csrMatrix => }/solvers/petscSolver.H (98%)

diff --git a/README.md b/README.md
index 188f272..bab143f 100644
--- a/README.md
+++ b/README.md
@@ -56,5 +56,5 @@ with the same terms as OpenFOAM itself.
 
 [Hypre]: https://computing.llnl.gov/projects/hypre-scalable-linear-solvers-multigrid-methods
 [Petsc]: https://www.mcs.anl.gov/petsc/
-[Petsc-Installation]: etc/README.md
+[Petsc-Installation]: doc/README.md
 [Petsc-Ext-Installation]: https://www.mcs.anl.gov/petsc/documentation/installation.html
diff --git a/etc/README.md b/doc/README.md
similarity index 100%
rename from etc/README.md
rename to doc/README.md
diff --git a/src/Allwclean b/src/Allwclean
index 734938b..004019a 100755
--- a/src/Allwclean
+++ b/src/Allwclean
@@ -2,6 +2,6 @@
 cd "${0%/*}" || exit                            # Run from this directory
 #------------------------------------------------------------------------------
 
-petsc4Foam/Allwclean
+petsc4Foam/Allwclean $*
 
 #------------------------------------------------------------------------------
diff --git a/src/Allwmake b/src/Allwmake
index 56da38c..20d18c3 100755
--- a/src/Allwmake
+++ b/src/Allwmake
@@ -2,6 +2,6 @@
 cd "${0%/*}" || exit                            # Run from this directory
 #------------------------------------------------------------------------------
 
-petsc4Foam/Allwmake
+petsc4Foam/Allwmake $*
 
 #------------------------------------------------------------------------------
diff --git a/src/petsc4Foam/Make/files b/src/petsc4Foam/Make/files
index eee14d3..bd45c6f 100644
--- a/src/petsc4Foam/Make/files
+++ b/src/petsc4Foam/Make/files
@@ -1,4 +1,4 @@
-csrMatrix/solvers/petscSolver.C
+solvers/petscSolver.C
 utils/petscUtils.C
 utils/petscControls.C
 utils/petscLinearSolverContexts.C
diff --git a/src/petsc4Foam/csrMatrix/solvers/petscSolver.C b/src/petsc4Foam/solvers/petscSolver.C
similarity index 93%
rename from src/petsc4Foam/csrMatrix/solvers/petscSolver.C
rename to src/petsc4Foam/solvers/petscSolver.C
index c110205..0c0e09b 100644
--- a/src/petsc4Foam/csrMatrix/solvers/petscSolver.C
+++ b/src/petsc4Foam/solvers/petscSolver.C
@@ -38,6 +38,7 @@ License
 #include "petscLinearSolverContexts.H"
 #include "petscUtils.H"
 #include "petscWrappedVector.H"
+#include <cstring>
 
 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
 
@@ -125,7 +126,7 @@ Foam::solverPerformance Foam::petscSolver::solve
 
     const bool verbose
     (
-        petscDict_.lookupOrDefault("verbose", false)
+        petscDict_.getOrDefault("verbose", false)
     );
 
     dictionary petscDictOptions = petscDict_.subOrEmptyDict("options");
@@ -157,43 +158,31 @@ Foam::solverPerformance Foam::petscSolver::solve
 
     if (!ctx.initialized())
     {
-        if (debug)
-        {
-            Info<< "Initializing PETSc Linear Solver " << eqName_ << nl;
-        }
+        DebugInfo<< "Initializing PETSc Linear Solver " << eqName_ << nl;
 
         ctx.initialized() = true;
         needsCacheUpdate = false;
 
-        computeMatAllocation(Amat,
-                             lowNonZero,
-                             maxLowNonZeroPerRow);
-        buildMat(Amat,
-                 lowNonZero,
-                 maxLowNonZeroPerRow);
+        computeMatAllocation(Amat, lowNonZero, maxLowNonZeroPerRow);
+        buildMat(Amat, lowNonZero, maxLowNonZeroPerRow);
         buildKsp(Amat, ksp);
     }
 
     if (ctx.caching.needsMatrixUpdate() && needsCacheUpdate)
     {
-        buildMat(Amat,
-                 lowNonZero,
-                 maxLowNonZeroPerRow);
+        buildMat(Amat, lowNonZero, maxLowNonZeroPerRow);
         updateKsp(ksp, Amat, !ctx.caching.needsPrecondUpdate());
     }
 
     // Solver name from petsc
     KSPType ksptype;
     KSPGetType(ksp, &ksptype);
-    const word solverName
-    (
-        ksptype
-    );
+    const word solverName(ksptype);
 
     // Use built-in residual norm computation
     const bool usePetscResidualNorm
     (
-        petscDict_.lookupOrDefault("use_petsc_residual_norm", false)
+        petscDict_.getOrDefault("use_petsc_residual_norm", false)
     );
 
     // Setup class containing solver performance data
@@ -259,10 +248,7 @@ void Foam::petscSolver::updateKsp
     const bool reuse
 ) const
 {
-    if (debug)
-    {
-        Info<< "Cache-Hit: updating PETSc Amat " << eqName_ << nl;
-    }
+    DebugInfo<< "Cache-Hit: updating PETSc Amat " << eqName_ << nl;
 
     if (reuse)
     {
@@ -270,10 +256,7 @@ void Foam::petscSolver::updateKsp
     }
     else
     {
-        if (debug)
-        {
-            Info<< "Cache-Hit: updating PETSc Pmat " << eqName_ << nl;
-        }
+        DebugInfo<< "Cache-Hit: updating PETSc Pmat " << eqName_ << nl;
 
         KSPSetReusePreconditioner(ksp, PETSC_FALSE);
     }
@@ -304,7 +287,7 @@ void Foam::petscSolver::buildKsp
 
     const bool verbose
     (
-        petscDict_.lookupOrDefault("verbose", false)
+        petscDict_.getOrDefault("verbose", false)
     );
 
     // Solver name from petsc dictionary
@@ -388,7 +371,7 @@ void Foam::petscSolver::buildKsp
         maxIter_
     );
 
-    // Use the solution from the previous timestep if the solver is not preonly
+    // Use solution from the previous timestep if the solver is not 'preonly'
     if (solverName != "preonly")
     {
         KSPSetInitialGuessNonzero(ksp, PETSC_TRUE);
@@ -498,16 +481,16 @@ void Foam::petscSolver::computeMatAllocation
     (
         Amat,
         1,
-        nonZero_.data(),      // on-processor
-        nonZeroNeigh_.data(),  // off-processor
+        nonZero_.data(),        // on-processor
+        nonZeroNeigh_.data(),   // off-processor
         nonZero_.data(),
         nonZeroNeigh_.data()
     );
     MatMPISELLSetPreallocation
     (
         Amat,
-        0, nonZero_.data(),      // on-processor
-        0, nonZeroNeigh_.data()  // off-processor
+        0, nonZero_.data(),       // on-processor
+        0, nonZeroNeigh_.data()   // off-processor
     );
 
     // set the matrix options
diff --git a/src/petsc4Foam/csrMatrix/solvers/petscSolver.H b/src/petsc4Foam/solvers/petscSolver.H
similarity index 98%
rename from src/petsc4Foam/csrMatrix/solvers/petscSolver.H
rename to src/petsc4Foam/solvers/petscSolver.H
index 8f2e07a..a15f281 100644
--- a/src/petsc4Foam/csrMatrix/solvers/petscSolver.H
+++ b/src/petsc4Foam/solvers/petscSolver.H
@@ -58,10 +58,12 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef petscSolver_H
-#define petscSolver_H
+#ifndef petscFoamSolver_H
+#define petscFoamSolver_H
 
 #include "lduMatrix.H"
+
+// PETSc
 #include "petscmat.h"
 #include "petscksp.h"
 
diff --git a/src/petsc4Foam/utils/petscCacheManager.H b/src/petsc4Foam/utils/petscCacheManager.H
index d05fdc2..4a4b350 100644
--- a/src/petsc4Foam/utils/petscCacheManager.H
+++ b/src/petsc4Foam/utils/petscCacheManager.H
@@ -61,8 +61,8 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef petscCacheManager_H
-#define petscCacheManager_H
+#ifndef petscFoamCacheManager_H
+#define petscFoamCacheManager_H
 
 #include "Enum.H"
 #include "clockValue.H"
@@ -72,7 +72,6 @@ SourceFiles
 
 namespace Foam
 {
-
 namespace PetscUtils
 {
 
@@ -82,29 +81,52 @@ namespace PetscUtils
 
 struct Caching
 {
-    //- Supported caching types
-    enum cachingTypes
-    {
-        None,
-        Always,
-        Periodic,
-        Adaptive
-    };
+    // Public Data Types
+
+        //- Supported caching types
+        enum cachingTypes
+        {
+            None,
+            Always,
+            Periodic,
+            Adaptive
+        };
+
+
+        //- Names for selectable caching types
+        static const Enum<cachingTypes> cachingTypeNames_;
+
+
+    // Member Data
+
+        cachingTypes updateType_;
+
+        int updateFreq_;
+
+        //- Elapsed time (s) for current iteration
+        double timeIter;
+
+        //- Elapsed time (s) for first iteration
+        double time0;
+
+        //- Elapsed time (s) for second iteration
+        double time1;
 
+        //- Timer value
+        clockValue timer_;
 
-    //- Names for selectable caching types
-    static const Enum<cachingTypes> cachingTypeNames_;
 
+    // Constructors
 
-    //- Default construct
-    Caching()
-    :
-       updateType_{cachingTypes::None},
-       updateFreq_{1},
-       time{0},
-       time0{0},
-       time1{0}
-    {}
+        //- Default construct. No caching
+        Caching()
+        :
+           updateType_{cachingTypes::None},
+           updateFreq_{1},
+           timeIter{0},
+           time0{0},
+           time1{0}
+        {}
 
 
     void init(const word& key, const dictionary& dict)
@@ -124,24 +146,6 @@ struct Caching
             updateFreq_ = coeffsDict.getOrDefault<int>("frequency", 1);
         }
     }
-
-
-    // Member Data
-
-    cachingTypes updateType_;
-
-    int updateFreq_;
-
-    //- Elapsed time (s) for current iteration
-    double time;
-
-    //- Elapsed time (s) for first iteration
-    double time0;
-
-    //- Elapsed time (s) for second iteration
-    double time1;
-
-    clockValue timer_;
 };
 
 } // End namespace PetscUtils
@@ -210,15 +214,15 @@ public:
         if (precondCaching.updateType_ == PetscUtils::Caching::Adaptive)
         {
             // Elapsed timing interval (s)
-            precondCaching.time = precondCaching.timer_.elapsed();
+            precondCaching.timeIter = precondCaching.timer_.elapsed();
 
             if (iter == 0)
             {
-                precondCaching.time0 = precondCaching.time;
+                precondCaching.time0 = precondCaching.timeIter;
             }
             else if (iter == 1)
             {
-                precondCaching.time1 = precondCaching.time;
+                precondCaching.time1 = precondCaching.timeIter;
             }
         }
 
@@ -269,8 +273,12 @@ private:
             {
                 if (iter > 3) // we need at least three times
                 {
-                    double ratio0 = precondCaching.time0 / precondCaching.time;
-                    double ratio1 = precondCaching.time1 / precondCaching.time;
+                    const double ratio0 =
+                        precondCaching.time0 / precondCaching.timeIter;
+
+                    const double ratio1 =
+                        precondCaching.time1 / precondCaching.timeIter;
+
                     int nsteps =
                         min(1e5, ratio0 * (1. / mag(1. - ratio1 + 1e-6)));
 
diff --git a/src/petsc4Foam/utils/petscControls.H b/src/petsc4Foam/utils/petscControls.H
index 9ba47f3..ea0891d 100644
--- a/src/petsc4Foam/utils/petscControls.H
+++ b/src/petsc4Foam/utils/petscControls.H
@@ -36,8 +36,8 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef petscControls_H
-#define petscControls_H
+#ifndef petscFoamControls_H
+#define petscFoamControls_H
 
 #include "IOdictionary.H"
 #include "MeshObject.H"
@@ -50,7 +50,6 @@ namespace Foam
 // Forward Declarations
 class Time;
 
-
 /*---------------------------------------------------------------------------*\
                         Class petscControls Declaration
 \*---------------------------------------------------------------------------*/
@@ -68,7 +67,7 @@ class petscControls
 
 public:
 
-    // Declare name of the class and its debug switch
+    //- Runtime information
     TypeName("petscControlDict");
 
 
@@ -95,7 +94,7 @@ public:
         static void stop();
 
         //- Test if loaded
-        inline bool valid() const
+        bool valid() const
         {
             return loaded_;
         }
diff --git a/src/petsc4Foam/utils/petscLinearSolverContext.H b/src/petsc4Foam/utils/petscLinearSolverContext.H
index ed65c0b..75b1047 100644
--- a/src/petsc4Foam/utils/petscLinearSolverContext.H
+++ b/src/petsc4Foam/utils/petscLinearSolverContext.H
@@ -34,15 +34,17 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef petscLinearSolverContext_H
-#define petscLinearSolverContext_H
+#ifndef petscFoamLinearSolverContext_H
+#define petscFoamLinearSolverContext_H
 
 #include "solverPerformance.H"
 #include "petscCacheManager.H"
 
+// PETSc
 #include "petscvec.h"
 #include "petscmat.h"
 #include "petscksp.h"
+
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
@@ -82,7 +84,7 @@ public:
 
     // Constructors
 
-        //- Construct null
+        //- Default construct
         petscLinearSolverContext()
         :
             init_(false),
diff --git a/src/petsc4Foam/utils/petscLinearSolverContexts.H b/src/petsc4Foam/utils/petscLinearSolverContexts.H
index e9942dc..04bb86b 100644
--- a/src/petsc4Foam/utils/petscLinearSolverContexts.H
+++ b/src/petsc4Foam/utils/petscLinearSolverContexts.H
@@ -34,13 +34,13 @@ SourceFiles
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef petscLinearSolverContexts_H
-#define petscLinearSolverContexts_H
+#ifndef petscFoamLinearSolverContexts_H
+#define petscFoamLinearSolverContexts_H
 
 #include "fvMesh.H"
 #include "MeshObject.H"
-#include "petscLinearSolverContext.H"
 #include "HashPtrTable.H"
+#include "petscLinearSolverContext.H"
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
@@ -63,18 +63,19 @@ class petscLinearSolverContexts
     // Private Data
 
         //- Linear solver contexts
-        HashPtrTable<petscLinearSolverContext> contexts_;
+        mutable HashPtrTable<petscLinearSolverContext> contexts_;
 
 
 public:
 
+    //- Runtime information
     TypeName("petscLinearSolverContext");
 
 
     // Constructors
 
         //- Construct from mesh for given field
-        petscLinearSolverContexts(const fvMesh& mesh)
+        explicit petscLinearSolverContexts(const fvMesh& mesh)
         :
             MeshObject
             <
@@ -95,11 +96,7 @@ public:
         //- Linear solver contexts (mutable)
         HashPtrTable<petscLinearSolverContext>& contexts() const
         {
-            return
-                const_cast
-                <
-                   HashPtrTable<petscLinearSolverContext>&
-                >(contexts_);
+            return contexts_;
         }
 
 
@@ -110,7 +107,7 @@ public:
         }
 
 
-        //- Get existing or a new context
+        //- Get existing or a new context by name
         petscLinearSolverContext& getContext(const word& name) const
         {
             auto iter = contexts().find(name);
@@ -119,23 +116,20 @@ public:
 
             if (!iter.found())
             {
-                contexts().set(name, new petscLinearSolverContext());
+                contexts().set(name, new petscLinearSolverContext{});
                 iter = contexts().find(name);
             }
 
-            if (debug)
-            {
-                Info
-                    << "cache-" << (cached ? "hit" : "miss")
-                    << " PETSc linear solver context "
-                    << name << " mesh: " << mesh().name() << nl;
-            }
+            DebugInfo
+                << "cache-" << (cached ? "hit" : "miss")
+                << " PETSc linear solver context "
+                << name << " mesh: " << mesh().name() << nl;
 
             return *(*iter);
         }
 
 
-        //- Remove context
+        //- Remove context by name
         bool removeContext(const word& name) const
         {
             return contexts().erase(name);
diff --git a/src/petsc4Foam/utils/petscUtils.C b/src/petsc4Foam/utils/petscUtils.C
index ab8710b..670e73a 100644
--- a/src/petsc4Foam/utils/petscUtils.C
+++ b/src/petsc4Foam/utils/petscUtils.C
@@ -26,13 +26,12 @@ License
 
 \*---------------------------------------------------------------------------*/
 
-#include <algorithm>
-#include <cstring>
-
 #include "lduMatrix.H"
 #include "error.H"
 #include "petscUtils.H"
 #include "petscLinearSolverContext.H"
+#include <algorithm>
+
 // * * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * //
 
 Foam::scalar Foam::gAverage(Vec input)
@@ -294,7 +293,7 @@ void Foam::PetscUtils::setFlag
         Info<< key << ' ' << val << nl;
     }
 
-    PetscOptionsSetValue(NULL, key.c_str(), val.c_str());
+    PetscOptionsSetValue(nullptr, key.c_str(), val.c_str());
 }
 
 
@@ -315,7 +314,7 @@ void Foam::PetscUtils::setFlags
             Info<< key << ' ' << val << nl;
         }
 
-        PetscOptionsSetValue(NULL, key.c_str(), val.c_str());
+        PetscOptionsSetValue(nullptr, key.c_str(), val.c_str());
     }
 }
 
diff --git a/src/petsc4Foam/utils/petscUtils.H b/src/petsc4Foam/utils/petscUtils.H
index 02095bf..8ed3010 100644
--- a/src/petsc4Foam/utils/petscUtils.H
+++ b/src/petsc4Foam/utils/petscUtils.H
@@ -31,12 +31,12 @@ Description
     Utilities for OpenFOAM / PETSc interaction
 
 SourceFiles
-    PetscUtils.C
+    petscUtils.C
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef PetscUtils_H
-#define PetscUtils_H
+#ifndef petscFoamUtils_H
+#define petscFoamUtils_H
 
 // OpenFOAM
 #include "scalarField.H"
diff --git a/src/petsc4Foam/utils/petscWrappedVector.H b/src/petsc4Foam/utils/petscWrappedVector.H
index 75cc4c3..3189108 100644
--- a/src/petsc4Foam/utils/petscWrappedVector.H
+++ b/src/petsc4Foam/utils/petscWrappedVector.H
@@ -30,12 +30,11 @@ Description
     Wrap an OpenFOAM list as a PETSc vector
 
 SourceFiles
-    PetscWrappedVector.H
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef PetscWrappedVector_H
-#define PetscWrappedVector_H
+#ifndef petscFoamWrappedVector_H
+#define petscFoamWrappedVector_H
 
 // OpenFOAM
 #include "List.H"
@@ -63,6 +62,7 @@ class PetscWrappedVector
 
 
     // Private Member Functions
+
         //- Wrap an OpenFOAM list of values as a PETSc vector
         template<class T>
         void createImpl
@@ -72,7 +72,11 @@ class PetscWrappedVector
         )
         {
             MatCreateVecs(mat, &vec_, PETSC_NULL);
-            VecPlaceArray(vec_, reinterpret_cast<scalar*>(const_cast<T*>(list.cdata())));
+            VecPlaceArray
+            (
+                vec_,
+                reinterpret_cast<scalar*>(const_cast<T*>(list.cdata()))
+            );
         }
 
         //- Wrap an OpenFOAM list of values as a PETSc vector
@@ -100,6 +104,7 @@ class PetscWrappedVector
         //- No copy assignment
         void operator=(const PetscWrappedVector&) = delete;
 
+
 public:
 
     // Constructors
@@ -140,6 +145,7 @@ public:
             createImpl(list, comm);
         }
 
+
     //- Destructor
     ~PetscWrappedVector()
     {
-- 
GitLab