From 3c14d4b02f10d7291a7498d7c144d0de089aca9d Mon Sep 17 00:00:00 2001
From: acrovato <170-acrovato@users.noreply.gitlab.uliege.be>
Date: Fri, 20 Mar 2020 22:20:23 +0100
Subject: [PATCH] Added iteration counter to tests, to increase robustness

---
 .gitlab-ci.yml            |  6 +++---
 flow/src/wNewton.cpp      | 12 ++++++------
 flow/src/wPicard.cpp      | 12 ++++++------
 flow/src/wSolver.h        |  1 +
 flow/tests/cylinder.py    |  2 ++
 flow/tests/cylinder2D5.py |  1 +
 flow/tests/cylinder3.py   |  1 +
 flow/tests/lift.py        |  1 +
 flow/tests/lift3.py       |  1 +
 flow/tests/meshDef.py     |  2 ++
 flow/tests/meshDef3.py    |  1 +
 flow/tests/nonlift.py     |  1 +
 12 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 96bc4057..a200fa2d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,7 +1,7 @@
 # gitlab-ci file for waves
 
 default:
-    image: rboman/waves
+    image: rboman/waves:2020.0
     before_script:
         - source /opt/intel/mkl/bin/mklvars.sh intel64
         - source /opt/intel/tbb/bin/tbbvars.sh intel64
@@ -35,7 +35,7 @@ build-py2-no-tlnos:
         - make -j 4
 
 build-py3:
-    image: rboman/waves-py3
+    image: rboman/waves-py3:2020.0
     stage: build
     script:
         - printenv | sort
@@ -71,7 +71,7 @@ ctest-py2:
         - build-py2
 
 ctest-py3:
-    image: rboman/waves-py3
+    image: rboman/waves-py3:2020.0
     stage: test
     script:
         - cd build
diff --git a/flow/src/wNewton.cpp b/flow/src/wNewton.cpp
index c4aace2b..477a43bc 100644
--- a/flow/src/wNewton.cpp
+++ b/flow/src/wNewton.cpp
@@ -97,7 +97,7 @@ bool Newton::run()
               << std::endl;
 
     // Initialize solver loop
-    int itC = 0;       // iteration counter
+    nIt = 0;           // iteration counter
     int avC = 0;       // adaptive viscosity counter
     bool rUpwd = true; // update best upwind element
 
@@ -127,7 +127,7 @@ bool Newton::run()
               << std::setw(15) << "Rel_Res[phi]"
               << std::setw(15) << "Abs_Res[phi]" << std::endl;
     std::cout << std::fixed << std::setprecision(5);
-    std::cout << std::setw(8) << itC
+    std::cout << std::setw(8) << nIt
               << std::setw(8) << 0
               << std::setw(8) << 1
               << std::setw(12) << "-"
@@ -173,7 +173,7 @@ bool Newton::run()
         bool solve_success = gmm::MUMPS_solve(J, deltaPhi, rPhi);
         if (!solve_success)
             throw std::runtime_error("Problem occured in gmm::MUMPS_solve!");
-        itC++;
+        nIt++;
 
         // Compute new solution and residual with linesearch
         fpe.update(phi, deltaPhi);
@@ -198,7 +198,7 @@ bool Newton::run()
 
         // Display residual
         std::cout << std::fixed << std::setprecision(5);
-        std::cout << std::setw(8) << itC
+        std::cout << std::setw(8) << nIt
                   << std::setw(8) << 1
                   << std::setw(8) << ls.fevalIt
                   << std::setw(12) << Cl
@@ -207,13 +207,13 @@ bool Newton::run()
                   << std::setw(15) << log10(absRes) << "\n";
 
         // Check convergence
-        if ((relRes < relTol || absRes < absTol) && itC != 0)
+        if ((relRes < relTol || absRes < absTol) && nIt != 0)
             break;
         else if (std::isnan(relRes))
             break;
         else
             continue;
-    } while (itC < maxIt);
+    } while (nIt < maxIt);
 
     // Compute field variables
     Solver::computeFlow();
diff --git a/flow/src/wPicard.cpp b/flow/src/wPicard.cpp
index c0fe61cf..97d56ea6 100644
--- a/flow/src/wPicard.cpp
+++ b/flow/src/wPicard.cpp
@@ -89,7 +89,7 @@ bool Picard::run()
               << std::endl;
 
     // Initialize solver loop
-    int it = 0;
+    nIt = 0;
     double resInit = 1.;
     double absRes = 1., relRes = 1.;
     std::vector<double> phiOld(phi.size()); // old solution (for relaxation)
@@ -117,7 +117,7 @@ bool Picard::run()
         std::vector<double> A_phi(pbl->msh->nodes.size());
         gmm::mult(A, phi, A_phi);
         gmm::add(A_phi, gmm::scaled(b, -1.0), rPhi);
-        if (it == 0)
+        if (nIt == 0)
             resInit = gmm::vect_norm2(rPhi);
         absRes = gmm::vect_norm2(rPhi);
         relRes = absRes / resInit;
@@ -127,18 +127,18 @@ bool Picard::run()
 
         // Display residual
         std::cout << std::fixed << std::setprecision(5);
-        std::cout << std::setw(8) << it
+        std::cout << std::setw(8) << nIt
                   << std::setw(12) << Cl
                   << std::setw(12) << Cd
                   << std::setw(15) << log10(relRes)
                   << std::setw(15) << log10(absRes) << "\n";
 
         // Check convergence
-        if ((relRes < relTol || absRes < absTol) && it != 0)
+        if ((relRes < relTol || absRes < absTol) && nIt != 0)
             break;
         else if (std::isnan(relRes))
             break;
-        it++;
+        nIt++;
 
         // Transfer old solution
         phiOld = phi;
@@ -152,7 +152,7 @@ bool Picard::run()
         // Relaxation
         for (size_t i = 0; i < phi.size(); ++i)
             phi[i] = (1 - relax) * phiOld[i] + relax * phi[i];
-    } while (it < maxIt);
+    } while (nIt < maxIt);
 
     // Terminate MUMPS
     mumps.finalize();
diff --git a/flow/src/wSolver.h b/flow/src/wSolver.h
index 5b184c8d..d7415b3a 100644
--- a/flow/src/wSolver.h
+++ b/flow/src/wSolver.h
@@ -43,6 +43,7 @@ public:
     int maxIt;     ///< max number of iterations
     bool verbose;  ///< display more info
 
+    int nIt;                  ///< number of iterations
     std::vector<double> phi;  ///< full potential
     std::vector<double> rPhi; ///< residual on potential
     std::vector<double> vPhi; ///< perturbation potential
diff --git a/flow/tests/cylinder.py b/flow/tests/cylinder.py
index 915fc8cf..29126407 100644
--- a/flow/tests/cylinder.py
+++ b/flow/tests/cylinder.py
@@ -158,9 +158,11 @@ def main():
     if not cnvrgd0 or not cnvrgd1:
         raise Exception(ccolors.ANSI_RED + 'Flow solver failed to converge!' + ccolors.ANSI_RESET)
     tests = CTests()
+    tests.add(CTest('Picard: iteration count', solver0.nIt, 12, 1, forceabs=True))
     tests.add(CTest('Picard: Neumann B.C. mean error', errNeu0, 0., 1e-2))
     tests.add(CTest('Picard: Dirichlet B.C. max error', errDir0, 0., 1e-8))
     tests.add(CTest('Picard: Wake/Kutta B.C. max error', errWak0, 0., 1e-2))
+    tests.add(CTest('Newton: iteration count', solver1.nIt, 3, 1, forceabs=True))
     tests.add(CTest('Newton: Neumann B.C. mean error', errNeu1, 0., 1e-2))
     tests.add(CTest('Newton: Dirichlet B.C. max error', errDir1, 0., 1e-8))
     tests.add(CTest('Newton: Wake/Kutta B.C. max error', errWak1, 0., 1e-2))
diff --git a/flow/tests/cylinder2D5.py b/flow/tests/cylinder2D5.py
index b105b65f..bc6c970c 100644
--- a/flow/tests/cylinder2D5.py
+++ b/flow/tests/cylinder2D5.py
@@ -148,6 +148,7 @@ def main():
     if (not cnvrgd):
         raise Exception(ccolors.ANSI_RED + 'Flow solver failed to converge!' + ccolors.ANSI_RESET)
     tests = CTests()
+    tests.add(CTest('iteration count', solver.nIt, 3, 1, forceabs=True))
     tests.add(CTest('Neumann B.C. mean error', errNeu, 0., 1e-2))
     tests.add(CTest('Dirichlet B.C. max error', errDir, 0., 1e-12))
     tests.add(CTest('Wake/Kutta B.C. max error', errWak, 0., 1e-1))
diff --git a/flow/tests/cylinder3.py b/flow/tests/cylinder3.py
index 14548427..c229e6f3 100644
--- a/flow/tests/cylinder3.py
+++ b/flow/tests/cylinder3.py
@@ -152,6 +152,7 @@ def main():
     if (not cnvrgd):
         raise Exception(ccolors.ANSI_RED + 'Flow solver failed to converge!' + ccolors.ANSI_RESET)
     tests = CTests()
+    tests.add(CTest('iteration count', solver.nIt, 4, 1, forceabs=True))
     tests.add(CTest('Neumann B.C. mean error', errNeu, 0., 1e-2))
     tests.add(CTest('Dirichlet B.C. max error', errDir, 0., 1e-12))
     tests.add(CTest('Wake/Kutta B.C. max error', errWak, 0., 1e-1))
diff --git a/flow/tests/lift.py b/flow/tests/lift.py
index 77313ead..8332d5ee 100644
--- a/flow/tests/lift.py
+++ b/flow/tests/lift.py
@@ -118,6 +118,7 @@ def main():
         tests.add(CTest('min(Cp)', min(Cp[:,3]), -1.03, 1e-1))
         tests.add(CTest('Cl', solver.Cl, 0.315, 5e-2))
     elif M_inf == 0.7 and alpha == 2*math.pi/180:
+        tests.add(CTest('iteration count', solver.nIt, 12, 3, forceabs=True))
         tests.add(CTest('min(Cp)', min(Cp[:,3]), -1.28, 5e-2))
         tests.add(CTest('Cl', solver.Cl, 0.388, 5e-2))
     else:
diff --git a/flow/tests/lift3.py b/flow/tests/lift3.py
index 30004839..774c77ad 100644
--- a/flow/tests/lift3.py
+++ b/flow/tests/lift3.py
@@ -111,6 +111,7 @@ def main():
     print(ccolors.ANSI_BLUE + 'PyTesting...' + ccolors.ANSI_RESET)
     tests = CTests()
     if alpha == 3*math.pi/180 and M_inf == 0.3 and spn == 1.0:
+        tests.add(CTest('iteration count', solver.nIt, 3, 1, forceabs=True))
         tests.add(CTest('CL', solver.Cl, 0.135, 5e-2))
         tests.add(CTest('CD', solver.Cd, 0.0062, 1e-2)) # Tranair (NF=0.0062, FF=0.0030), Panair 0.0035
     else:
diff --git a/flow/tests/meshDef.py b/flow/tests/meshDef.py
index 0d2040a1..82e3b312 100644
--- a/flow/tests/meshDef.py
+++ b/flow/tests/meshDef.py
@@ -188,6 +188,8 @@ def main():
     # check results
     print(ccolors.ANSI_BLUE + 'PyTesting...' + ccolors.ANSI_RESET)
     tests = CTests()
+    tests.add(CTest('solver: iteration count', solver.nIt, 5, 1, forceabs=True))
+    tests.add(CTest('solver_ref: iteration count', solver.nIt, 5, 1, forceabs=True))
     tests.add(CTest('min(Cp)', min(Cp[:,3]), min(Cp_ref[:,3]), 5e-2))
     tests.add(CTest('Cl', solver.Cl, solver_ref.Cl, 5e-2))
     tests.run()
diff --git a/flow/tests/meshDef3.py b/flow/tests/meshDef3.py
index 5a04160d..10cdedd3 100644
--- a/flow/tests/meshDef3.py
+++ b/flow/tests/meshDef3.py
@@ -153,6 +153,7 @@ def main():
     print(ccolors.ANSI_BLUE + 'PyTesting...' + ccolors.ANSI_RESET)
     tests = CTests()
     if alfa == 3*np.pi/180 and M_inf == 0.3 and spn == 1.0:
+        tests.add(CTest('iteration count', solver.nIt, 3, 1, forceabs=True))
         tests.add(CTest('CL', solver.Cl, 0.135, 5e-1))
         tests.add(CTest('CD', solver.Cd, 0.0062, 1e-2)) # Tranair (NF=0.0062, FF=0.0030), Panair 0.0035
     tests.run()
diff --git a/flow/tests/nonlift.py b/flow/tests/nonlift.py
index 6e6b2ae3..55d299db 100644
--- a/flow/tests/nonlift.py
+++ b/flow/tests/nonlift.py
@@ -115,6 +115,7 @@ def main():
     elif M_inf == 0.7:
         tests.add(CTest('min(Cp)', min(Cp[:,3]), -0.63, 5e-2))
     elif M_inf == 0.8:
+        tests.add(CTest('iteration count', solver.nIt, 16, 3, forceabs=True))
         tests.add(CTest('min(Cp)', min(Cp[:,3]), -0.89, 5e-2))
     else:
         raise Exception('Test not defined for this flow')
-- 
GitLab