From 296404d7e88c3fa328126a1e051c19043d677e75 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Wed, 11 Mar 2020 14:20:44 +0100
Subject: [PATCH] STYLE: use OpenFOAM clock routines

---
 src/petsc4Foam/utils/petscCacheManager.H | 41 +++++++++++++-----------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/src/petsc4Foam/utils/petscCacheManager.H b/src/petsc4Foam/utils/petscCacheManager.H
index 762f35b..d05fdc2 100644
--- a/src/petsc4Foam/utils/petscCacheManager.H
+++ b/src/petsc4Foam/utils/petscCacheManager.H
@@ -40,16 +40,16 @@ Description
         {
             matrixCaching
             {
-                update always;
+                update  always;
             }
 
             preconditionerCaching
             {
-                update periodic;
+                update  periodic;
 
                 periodicCoeffs
                 {
-                    frequency 3;
+                    frequency   3;
                 }
             }
         }
@@ -64,13 +64,10 @@ SourceFiles
 #ifndef petscCacheManager_H
 #define petscCacheManager_H
 
-#include "solverPerformance.H"
 #include "Enum.H"
+#include "clockValue.H"
 #include "dictionary.H"
 
-#include "petsctime.h"
-#include "petscmath.h"
-
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 namespace Foam
@@ -99,8 +96,7 @@ struct Caching
     static const Enum<cachingTypes> cachingTypeNames_;
 
 
-    // Constructors
-
+    //- Default construct
     Caching()
     :
        updateType_{cachingTypes::None},
@@ -136,10 +132,16 @@ struct Caching
 
     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;
-    PetscLogDouble ptime;
+
+    clockValue timer_;
 };
 
 } // End namespace PetscUtils
@@ -149,7 +151,6 @@ struct Caching
                      Class petscCacheManager Declaration
 \*---------------------------------------------------------------------------*/
 
-
 class petscCacheManager
 {
     // Private Data
@@ -199,7 +200,8 @@ public:
     {
         if (precondCaching.updateType_ == PetscUtils::Caching::Adaptive)
         {
-            PetscTime(&precondCaching.ptime);
+            // Begin timing interval
+            precondCaching.timer_.update();
         }
     }
 
@@ -207,10 +209,8 @@ public:
     {
         if (precondCaching.updateType_ == PetscUtils::Caching::Adaptive)
         {
-            PetscTimeSubtract(&precondCaching.ptime);
-
-            precondCaching.time =
-                fabs(static_cast<double>(precondCaching.ptime));
+            // Elapsed timing interval (s)
+            precondCaching.time = precondCaching.timer_.elapsed();
 
             if (iter == 0)
             {
@@ -253,7 +253,11 @@ private:
 
             case PetscUtils::Caching::Periodic:
             {
-                if ((iter % caching.updateFreq_) == 0)
+                if
+                (
+                    caching.updateFreq_ <= 1
+                 || (iter % caching.updateFreq_) == 0
+                )
                 {
                     iter = 0;
                     return true;
@@ -267,7 +271,8 @@ private:
                 {
                     double ratio0 = precondCaching.time0 / precondCaching.time;
                     double ratio1 = precondCaching.time1 / precondCaching.time;
-                    int nsteps = min( 1e5, int( ratio0 * ( 1. / fabs( 1. - ratio1 + 1e-6 ))));
+                    int nsteps =
+                        min(1e5, ratio0 * (1. / mag(1. - ratio1 + 1e-6)));
 
                     if (iter >= nsteps)
                     {
-- 
GitLab