From 6da145263ba67c32ad3c6b8af64e83f3a9b2913e Mon Sep 17 00:00:00 2001
From: acrovato <a.crovato@uliege.be>
Date: Wed, 27 Apr 2022 14:55:45 +0200
Subject: [PATCH] Add verbosity level

---
 tbox/src/wLinesearch.cpp | 26 +++++++++++++-------------
 tbox/src/wMshDeform.cpp  | 30 ++++++++++++++++++------------
 tbox/src/wMshDeform.h    |  3 ++-
 3 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/tbox/src/wLinesearch.cpp b/tbox/src/wLinesearch.cpp
index 3bf8e33..8254d77 100644
--- a/tbox/src/wLinesearch.cpp
+++ b/tbox/src/wLinesearch.cpp
@@ -111,7 +111,7 @@ double FleuryLS::run()
     double phit = fct.eval(h);
     fevalIt = 2;
 
-    if (verbose > 2)
+    if (verbose > 3)
     {
         std::cout << "Starting Backeting \n";
         std::cout << "\t" << std::setprecision(16) << "phi1(" << 0 << ") = " << phi1 << '\n';
@@ -128,14 +128,14 @@ double FleuryLS::run()
         {
             if (h > hU)
             {
-                if (verbose > 2)
+                if (verbose > 3)
                 {
                     std::cout << "Positive slope detected (in ascending bracketing #1)!\n";
                 }
 
                 if (revAllowed)
                 {
-                    if (verbose > 2)
+                    if (verbose > 3)
                     {
                         std::cout << "Reverse Direction of Line Search (in ascending bracketing #1)!"
                                   << "\n";
@@ -146,7 +146,7 @@ double FleuryLS::run()
                 }
                 else
                 {
-                    if (verbose > 2)
+                    if (verbose > 3)
                     {
                         std::cout << "Error in ascending bracketing #1:\n"
                                   << std::setprecision(16) << "a1=" << 0 << " a2=" << h << " a3 =" << 2.0 * h << '\n'
@@ -164,7 +164,7 @@ double FleuryLS::run()
             phit = fct.eval(2 * h);
             fevalIt++;
 
-            if (verbose > 2)
+            if (verbose > 3)
             {
                 std::cout << "Ascending bracketing iteration # " << nB << ": \n";
                 std::cout << "\t"
@@ -185,14 +185,14 @@ double FleuryLS::run()
         {
             if (h < hL)
             {
-                if (verbose > 2)
+                if (verbose > 3)
                 {
                     std::cout << "Positive slope detected (in descending bracketing #2)!\n";
                 }
 
                 if (revAllowed)
                 {
-                    if (verbose > 2)
+                    if (verbose > 3)
                     {
                         std::cout << "Reverse Direction of Line Search (in descending bracketing #2) !"
                                   << "\n";
@@ -203,7 +203,7 @@ double FleuryLS::run()
                 }
                 else
                 {
-                    if (verbose > 2)
+                    if (verbose > 3)
                     {
                         std::cout << "Error in descending bracketing #2:\n"
                                   << std::setprecision(16) << "a1=" << 0 << " a2=" << h << " a3 =" << 2.0 * h << '\n'
@@ -220,7 +220,7 @@ double FleuryLS::run()
             phi3 = phit;
             phit = fct.eval(h / 2);
             fevalIt++;
-            if (verbose > 2)
+            if (verbose > 3)
             {
                 std::cout << "Descending bracketing iteration # " << nB << ": \n";
                 std::cout << "\t"
@@ -241,7 +241,7 @@ double FleuryLS::run()
     double a3 = 2 * h;
     double a4 = h * (4 * phi2 - 3 * phi1 - phi3) / (4 * phi2 - 2 * phi1 - 2 * phi3);
 
-    if (verbose > 2)
+    if (verbose > 3)
     {
         std::cout << "Backeting succeeds in " << nB << " iterations : \n";
         std::cout << "\t" << std::setprecision(16) << "phi1(" << a1 << ") = " << phi1 << '\n';
@@ -276,7 +276,7 @@ double FleuryLS::run()
             throw std::runtime_error(msg.str());
         }
 
-        if (verbose > 2)
+        if (verbose > 3)
         {
             std::cout << "Line search iteration # " << nLS << ": \n";
             std::cout << "\t" << std::setprecision(16) << "phi1(" << a1 << ") = " << phi1 << '\n';
@@ -319,7 +319,7 @@ double FleuryLS::run()
 
         A = (a2 - a3) * phi1 + (a3 - a1) * phi2 + (a1 - a2) * phi3;
 
-        if (verbose > 2)
+        if (verbose > 3)
         {
             std::cout << "\t" << std::setprecision(16) << "|A| = " << fabs(A) << "><" << tol << '\n';
         }
@@ -337,7 +337,7 @@ double FleuryLS::run()
              (A);
     }
 
-    if (verbose > 2)
+    if (verbose > 3)
     {
         std::cout << "Line search succeeds in " << nLS << " iterations : \n";
         std::cout << "\t" << std::setprecision(16) << "|A| = " << fabs(A) << '\n';
diff --git a/tbox/src/wMshDeform.cpp b/tbox/src/wMshDeform.cpp
index 72a3a5e..63ca062 100644
--- a/tbox/src/wMshDeform.cpp
+++ b/tbox/src/wMshDeform.cpp
@@ -32,10 +32,10 @@ using namespace tbox;
 
 MshDeform::MshDeform(std::shared_ptr<MshData> _msh,
                      std::shared_ptr<tbox::LinearSolver> _linsol,
-                     int _nDim, int nthrds) : wSharedObject(),
-                                              nDim(_nDim), nthreads(nthrds),
-                                              field(false), fixed(false), moving(false),
-                                              msh(_msh), linsol(_linsol)
+                     int _nDim, int nthrds, int vrb) : wSharedObject(),
+                                                       nDim(_nDim), nthreads(nthrds), verbose(vrb),
+                                                       field(false), fixed(false), moving(false),
+                                                       msh(_msh), linsol(_linsol)
 {
     // Check problem dimension
     if (nDim != 2 && nDim != 3)
@@ -453,19 +453,24 @@ void MshDeform::deform()
     Eigen::VectorXd f = Eigen::VectorXd::Zero(nDim * mshSize), q = Eigen::VectorXd::Zero(nDim * mshSize);
 
     // Build stiffness matrix and RHS
-    std::cout << "- Deforming mesh:\n"
-              << "assembling matrices... " << std::flush;
+    if (verbose > 0)
+        std::cout << "- Deforming mesh:\n"
+                  << "assembling matrices... " << std::flush;
     this->build(K);
     this->build(f);
-    std::cout << "done" << std::endl;
+    if (verbose > 0)
+        std::cout << "done" << std::endl;
     // Solve the SoE
-    std ::cout << "solving equations... " << std::flush;
+    if (verbose > 0)
+        std ::cout << "solving equations... " << std::flush;
     Eigen::Map<Eigen::VectorXd> f_(f.data(), f.size()), q_(q.data(), q.size());
     linsol->compute(K, f_, q_);
-    std::cout << "done (#it: " << linsol->getIterations() << ", error: " << std::scientific << linsol->getError() << std::fixed << ")" << std::endl;
+    if (verbose > 0)
+        std::cout << "done (#it: " << linsol->getIterations() << ", error: " << std::scientific << linsol->getError() << std::fixed << ")" << std::endl;
 
     // Update position
-    std ::cout << "updating mesh... " << std::flush;
+    if (verbose > 0)
+        std ::cout << "updating mesh... " << std::flush;
     for (auto n : msh->nodes)
     {
         for (int m = 0; m < nDim; m++)
@@ -486,8 +491,9 @@ void MshDeform::deform()
         e->update();
     for (auto e : intBndElems)
         e->update();
-    std::cout << "done\n"
-              << std::endl;
+    if (verbose > 0)
+        std::cout << "done\n"
+                  << std::endl;
 }
 
 /**
diff --git a/tbox/src/wMshDeform.h b/tbox/src/wMshDeform.h
index 91b7686..dec69f6 100644
--- a/tbox/src/wMshDeform.h
+++ b/tbox/src/wMshDeform.h
@@ -37,6 +37,7 @@ private:
     size_t mshSize;                                    ///< number of nodes in the mesh
     int nDim;                                          ///< dimension of the problem (2 or 3)
     int nthreads;                                      ///< number of threads
+    int verbose;                                       ///< verbosity level
     std::vector<int> rows;                             ///< unknown local index
     bool field;                                        ///< flag to check if field has been added
     bool fixed;                                        ///< flag to check if at least one fixed boundary has been added
@@ -64,7 +65,7 @@ public:
     std::shared_ptr<MshData> msh;               ///< mesh
     std::shared_ptr<tbox::LinearSolver> linsol; ///< linear (inner) solver
 
-    MshDeform(std::shared_ptr<MshData> _msh, std::shared_ptr<tbox::LinearSolver> _linsol, int _nDim, int nthrds = 1);
+    MshDeform(std::shared_ptr<MshData> _msh, std::shared_ptr<tbox::LinearSolver> _linsol, int _nDim, int nthrds = 1, int vrb = 1);
     virtual ~MshDeform() { std::cout << "~MshDeform()\n"; }
 
     void setField(std::string const &fldName);
-- 
GitLab