From d474ec1e24f005fd6b089f4a3bc5b63635b7572c Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Wed, 20 Feb 2019 23:38:05 +0100
Subject: [PATCH] don't call Msg::Fatal unless it's really fatal

---
 Mesh/BDS.cpp                     |  8 ++++----
 Mesh/BDS.h                       | 12 ++++++------
 Mesh/delaunay3d.cpp              |  2 +-
 Mesh/meshGFace.cpp               |  3 +--
 Mesh/meshGRegionLocalMeshMod.cpp |  3 ++-
 Mesh/qualityMeasuresJacobian.cpp | 11 +++++++----
 Numeric/bezierBasis.cpp          |  4 ++--
 Numeric/fullMatrix.cpp           | 21 ++++++++++++++++-----
 Numeric/pointsGenerators.cpp     |  3 ++-
 Plugin/AnalyseCurvedMesh.cpp     |  3 ++-
 Plugin/Distance.cpp              |  6 +++---
 Solver/eigenSolver.cpp           |  6 ++++--
 Solver/linearSystemPETSc.hpp     |  5 ++++-
 13 files changed, 54 insertions(+), 33 deletions(-)

diff --git a/Mesh/BDS.cpp b/Mesh/BDS.cpp
index 0c240c8a8f..ba2362912f 100644
--- a/Mesh/BDS.cpp
+++ b/Mesh/BDS.cpp
@@ -343,7 +343,7 @@ BDS_Edge *BDS_Mesh::recover_edge(int num1, int num2, bool &_fatal,
   BDS_Point *p2 = find_point(num2);
 
   if(!p1 || !p2) {
-    Msg::Fatal("Could not find points %d or %d in BDS mesh", num1, num2);
+    Msg::Error("Could not find points %d or %d in BDS mesh", num1, num2);
     return 0;
   }
 
@@ -489,7 +489,7 @@ BDS_Edge *BDS_Mesh::add_edge(int const p1, int const p2)
   BDS_Point *pp2 = find_point(p2);
 
   if(!pp1 || !pp2) {
-    Msg::Fatal("Could not find points %d or %d in BDS mesh", p1, p2);
+    Msg::Error("Could not find points %d or %d in BDS mesh", p1, p2);
     return 0;
   }
   edges.push_back(new BDS_Edge(pp1, pp2));
@@ -533,8 +533,8 @@ void BDS_Mesh::del_edge(BDS_Edge *e)
 
 void BDS_Mesh::del_point(BDS_Point *p)
 {
-  points.erase(p);
-  delete p;
+  if(points.erase(p))
+    delete p;
 }
 
 void BDS_Mesh::add_geom(int p1, int p2)
diff --git a/Mesh/BDS.h b/Mesh/BDS.h
index 47ac988e41..8ab3913b15 100644
--- a/Mesh/BDS.h
+++ b/Mesh/BDS.h
@@ -206,13 +206,13 @@ public:
   BDS_Face *otherFace(const BDS_Face *f) const
   {
     if(numfaces() != 2) {
-      Msg::Fatal("otherFace wrong, ony %d faces attached to edge %d %d",
+      Msg::Error("otherFace wrong, ony %d faces attached to edge %d %d",
                  numfaces(), p1->iD, p2->iD);
       return 0;
     }
     if(f == _faces[0]) return _faces[1];
     if(f == _faces[1]) return _faces[0];
-    Msg::Fatal("otherFace wrong: the edge does not belong to the face");
+    Msg::Error("otherFace wrong: the edge does not belong to the face");
     return 0;
   }
   void del(BDS_Face *t)
@@ -252,26 +252,26 @@ public:
   BDS_Edge *oppositeEdge(BDS_Point *p)
   {
     if(e4) {
-      Msg::Fatal("oppositeEdge to point %d cannot be applied to a quad", p->iD);
+      Msg::Error("oppositeEdge to point %d cannot be applied to a quad", p->iD);
       return 0;
     }
     if(e1->p1 != p && e1->p2 != p) return e1;
     if(e2->p1 != p && e2->p2 != p) return e2;
     if(e3->p1 != p && e3->p2 != p) return e3;
-    Msg::Fatal("point %d does not belong to this triangle", p->iD);
+    Msg::Error("point %d does not belong to this triangle", p->iD);
     return 0;
   }
   BDS_Point *oppositeVertex(BDS_Edge *e)
   {
     if(e4) {
-      Msg::Fatal("oppositeVertex to edge %d %d cannot be applied to a quad",
+      Msg::Error("oppositeVertex to edge %d %d cannot be applied to a quad",
                  e->p1->iD, e->p2->iD);
       return 0;
     }
     if(e == e1) return e2->commonvertex(e3);
     if(e == e2) return e1->commonvertex(e3);
     if(e == e3) return e1->commonvertex(e2);
-    Msg::Fatal("edge  %d %d does not belong to this triangle", e->p1->iD,
+    Msg::Error("edge  %d %d does not belong to this triangle", e->p1->iD,
                e->p2->iD);
     return 0;
   }
diff --git a/Mesh/delaunay3d.cpp b/Mesh/delaunay3d.cpp
index 8c71d26b99..46818a265a 100644
--- a/Mesh/delaunay3d.cpp
+++ b/Mesh/delaunay3d.cpp
@@ -129,7 +129,7 @@ static bool inSphereTest_s(Vert *va, Vert *vb, Vert *vc, Vert *vd, Vert *ve)
       double oriB = -robustPredicates::orient3d(
         (double *)pt[0], (double *)pt[2], (double *)pt[3], (double *)pt[4]);
       if(oriB == 0.0) {
-        Msg::Fatal("Symbolic perturbation failed in icCircle Predicate");
+        Msg::Error("Symbolic perturbation failed in icCircle Predicate");
       }
       // Flip the sign if there are odd number of swaps.
       if((swaps % 2) != 0) oriB = -oriB;
diff --git a/Mesh/meshGFace.cpp b/Mesh/meshGFace.cpp
index 8070222ec2..f2653a3d0f 100644
--- a/Mesh/meshGFace.cpp
+++ b/Mesh/meshGFace.cpp
@@ -1953,8 +1953,7 @@ static bool buildConsecutiveListOfVertices(
       // has to be taken with the other parametric coordinates (because it is
       // only present once in the closure of the domain).
       for(std::map<BDS_Point *, MVertex *, PointLessThan>::iterator it =
-            recoverMapLocal.begin();
-          it != recoverMapLocal.end(); ++it) {
+            recoverMapLocal.begin(); it != recoverMapLocal.end(); ++it) {
         m->del_point(it->first);
       }
       return false;
diff --git a/Mesh/meshGRegionLocalMeshMod.cpp b/Mesh/meshGRegionLocalMeshMod.cpp
index e6c393ef67..a000168cda 100644
--- a/Mesh/meshGRegionLocalMeshMod.cpp
+++ b/Mesh/meshGRegionLocalMeshMod.cpp
@@ -488,7 +488,8 @@ void buildVertexCavity_recur(MTet4 *t, MVertex *v, std::vector<MTet4 *> &cavity)
 {
   // if (recur > 20)printf("oufti %d\n",recur);
   if(t->isDeleted()) {
-    Msg::Fatal("a deleted triangle is a neighbor of a non deleted triangle");
+    Msg::Error("a deleted triangle is a neighbor of a non deleted triangle");
+    return;
   }
   int iV = -1;
   for(int i = 0; i < 4; i++) {
diff --git a/Mesh/qualityMeasuresJacobian.cpp b/Mesh/qualityMeasuresJacobian.cpp
index 0ea365882f..fead64d7df 100644
--- a/Mesh/qualityMeasuresJacobian.cpp
+++ b/Mesh/qualityMeasuresJacobian.cpp
@@ -601,8 +601,9 @@ namespace jacobianBasedQuality {
     : _CoeffData(depth), _coeffs(v.getDataPtr(), v.size()), _bfs(bfs)
   {
     if(!v.getOwnData()) {
-      Msg::Fatal("Cannot create an instance of _CoeffDataJac from a "
+      Msg::Error("Cannot create an instance of _CoeffDataJac from a "
                  "fullVector that does not own its data.");
+      return;
     }
     // _coeffs reuses the data of v, this avoid to allocate a new array and to
     // copy data that are not used outside of this object.
@@ -655,8 +656,9 @@ namespace jacobianBasedQuality {
       _bfsDet(bfsDet), _bfsMat(bfsMat), _type(type)
   {
     if(!det.getOwnData() || !mat.getOwnData()) {
-      Msg::Fatal("Cannot create an instance of _CoeffDataIGE from a "
+      Msg::Error("Cannot create an instance of _CoeffDataIGE from a "
                  "fullVector or a fullMatrix that does not own its data.");
+      return;
     }
     // _coeffsJacDet and _coeffsJacMat reuse data, this avoid to allocate new
     // arrays and to copy data that are not used outside of this object.
@@ -850,8 +852,9 @@ namespace jacobianBasedQuality {
       _bfsDet(bfsDet), _bfsMat(bfsMat)
   {
     if(!det.getOwnData() || !mat.getOwnData()) {
-      Msg::Fatal("Cannot create an instance of _CoeffDataIGE from a "
+      Msg::Error("Cannot create an instance of _CoeffDataIGE from a "
                  "fullVector or a fullMatrix that does not own its data.");
+      return;
     }
     // _coeffsJacDet and _coeffsMetric reuse data, this avoid to allocate new
     // arrays and to copy data that are not used outside of this object.
@@ -1035,7 +1038,7 @@ namespace jacobianBasedQuality {
                                bool lower, bool positiveDenom)
   {
     if(numerator.size() != denominator.size()) {
-      Msg::Fatal("In order to compute a bound on a rational function, I need "
+      Msg::Error("In order to compute a bound on a rational function, I need "
                  "vectors of the same size! (%d vs %d)",
                  numerator.size(), denominator.size());
       return 0;
diff --git a/Numeric/bezierBasis.cpp b/Numeric/bezierBasis.cpp
index 64ad1dda1c..86c8a1b826 100644
--- a/Numeric/bezierBasis.cpp
+++ b/Numeric/bezierBasis.cpp
@@ -309,7 +309,7 @@ namespace {
                                            int order, int dimSimplex)
   {
     if(exponent.size1() != point.size1() || exponent.size2() != point.size2()) {
-      Msg::Fatal("Wrong sizes for bez2lag matrix generation %d %d -- %d %d",
+      Msg::Error("Wrong sizes for bez2lag matrix generation %d %d -- %d %d",
                  exponent.size1(), point.size1(), exponent.size2(),
                  point.size2());
       return fullMatrix<double>(1, 1);
@@ -352,7 +352,7 @@ namespace {
   {
     if(exponent.size1() != point.size1() || exponent.size2() != point.size2() ||
        exponent.size2() != 3) {
-      Msg::Fatal(
+      Msg::Error(
         "Wrong sizes for pyramid's bez2lag matrix generation %d %d -- %d %d",
         exponent.size1(), point.size1(), exponent.size2(), point.size2());
       return fullMatrix<double>(1, 1);
diff --git a/Numeric/fullMatrix.cpp b/Numeric/fullMatrix.cpp
index 482fd816ae..132e1e091e 100644
--- a/Numeric/fullMatrix.cpp
+++ b/Numeric/fullMatrix.cpp
@@ -82,14 +82,20 @@ void fullVector<std::complex<double> >::setAll(
 
 template <> void fullMatrix<int>::setAll(const fullMatrix<int> &m)
 {
-  if(_r != m._r || _c != m._c) Msg::Fatal("fullMatrix size does not match");
+  if(_r != m._r || _c != m._c){
+    Msg::Error("fullMatrix size does not match");
+    return;
+  }
   int N = _r * _c;
   for(int i = 0; i < N; ++i) _data[i] = m._data[i];
 }
 
 template <> void fullMatrix<double>::setAll(const fullMatrix<double> &m)
 {
-  if(_r != m._r || _c != m._c) Msg::Fatal("fullMatrix size does not match");
+  if(_r != m._r || _c != m._c){
+    Msg::Error("fullMatrix size does not match");
+    return;
+  }
   int N = _r * _c;
   int stride = 1;
   F77NAME(dcopy)(&N, m._data, &stride, _data, &stride);
@@ -99,7 +105,10 @@ template <>
 void fullMatrix<std::complex<double> >::setAll(
   const fullMatrix<std::complex<double> > &m)
 {
-  if(_r != m._r || _c != m._c) Msg::Fatal("fullMatrix size does not match");
+  if(_r != m._r || _c != m._c){
+    Msg::Error("fullMatrix size does not match");
+    return;
+  }
   int N = _r * _c;
   int stride = 1;
   F77NAME(zcopy)(&N, m._data, &stride, _data, &stride);
@@ -371,8 +380,10 @@ template <> bool fullMatrix<double>::invert(fullMatrix<double> &result) const
   if(result.size2() != M || result.size1() != N) {
     if(result._own_data || !result._data)
       result.resize(M, N, false);
-    else
-      Msg::Fatal("FullMatrix: Bad dimension, I cannot write in proxy");
+    else{
+      Msg::Error("FullMatrix: Bad dimension, I cannot write in proxy");
+      return false;
+    }
   }
   result.setAll(*this);
   F77NAME(dgetrf)(&M, &N, result._data, &lda, ipiv, &info);
diff --git a/Numeric/pointsGenerators.cpp b/Numeric/pointsGenerators.cpp
index a85fee8b3e..936d628ef2 100644
--- a/Numeric/pointsGenerators.cpp
+++ b/Numeric/pointsGenerators.cpp
@@ -980,8 +980,9 @@ fullMatrix<double> gmshGenerateMonomialsPyramidGeneral(bool pyr, int nij,
                                                        bool forSerendipPoints)
 {
   if(nij < 0 || nk < 0) {
-    Msg::Fatal("Wrong arguments for pyramid's monomials generation ! (%d & %d)",
+    Msg::Error("Wrong arguments for pyramid's monomials generation ! (%d & %d)",
                nij, nk);
+    nij = nk = 1;
   }
   if(!pyr && nk > 0 && nij == 0) {
     Msg::Error("Wrong argument association for pyramid's monomials generation! "
diff --git a/Plugin/AnalyseCurvedMesh.cpp b/Plugin/AnalyseCurvedMesh.cpp
index 9dc19aa0a0..5d77f7a345 100644
--- a/Plugin/AnalyseCurvedMesh.cpp
+++ b/Plugin/AnalyseCurvedMesh.cpp
@@ -278,7 +278,8 @@ void GMSH_AnalyseCurvedMeshPlugin::_computeMinMaxJandValidity(int dim)
     for(GModel::eiter it = _m->firstEdge(); it != _m->lastEdge(); it++)
       entities.insert(*it);
     break;
-  default: Msg::Fatal("This should not happen."); return;
+  default:
+    return;
   }
 
   int cntInverted = 0;
diff --git a/Plugin/Distance.cpp b/Plugin/Distance.cpp
index d04e413b53..ed77fe7c41 100644
--- a/Plugin/Distance.cpp
+++ b/Plugin/Distance.cpp
@@ -550,7 +550,7 @@ PView *GMSH_DistancePlugin::execute(PView *v)
         case 3: fprintf(f5, "ST("); break;
         case 4: fprintf(f5, "SQ("); break;
         default:
-          Msg::Fatal("Error in Plugin 'Distance' (numNodes=%g).", numNodes);
+          Msg::Error("Error in Plugin 'Distance' (numNodes=%g).", numNodes);
           break;
         }
       else if(_maxDim == 3)
@@ -560,8 +560,8 @@ PView *GMSH_DistancePlugin::execute(PView *v)
         case 6: fprintf(f5, "SI("); break;
         case 5: fprintf(f5, "SY("); break;
         default:
-          Msg::Fatal("Error in Plugin 'Distance' (numNodes=%g).", numNodes);
-          break;
+          Msg::Error("Error in Plugin 'Distance' (numNodes=%g).", numNodes);
+          continue;
         }
 
       for(int j = 0; j < numNodes; j++) {
diff --git a/Solver/eigenSolver.cpp b/Solver/eigenSolver.cpp
index 6e6b0ff4cf..0f0764d7c1 100644
--- a/Solver/eigenSolver.cpp
+++ b/Solver/eigenSolver.cpp
@@ -76,8 +76,10 @@ bool eigenSolver::solve(int numEigenValues, std::string which,
     _check(EPSSetType(eps, EPSARPACK));
   else if(method == "power")
     _check(EPSSetType(eps, EPSPOWER));
-  else
-    Msg::Fatal("eigenSolver: method '%s' not available", method.c_str());
+  else{
+    Msg::Error("eigenSolver: method '%s' not available", method.c_str());
+    _check(EPSSetType(eps, EPSKRYLOVSCHUR));
+  }
 
   // override these options at runtime, petsc-style
   _check(EPSSetFromOptions(eps));
diff --git a/Solver/linearSystemPETSc.hpp b/Solver/linearSystemPETSc.hpp
index a7772834fa..56bb0a993b 100644
--- a/Solver/linearSystemPETSc.hpp
+++ b/Solver/linearSystemPETSc.hpp
@@ -120,7 +120,10 @@ void linearSystemPETSc<scalar>::insertInSparsityPattern(int i, int j)
 template <class scalar> void linearSystemPETSc<scalar>::preAllocateEntries()
 {
   if(_entriesPreAllocated) return;
-  if(!_isAllocated) Msg::Fatal("system must be allocated first");
+  if(!_isAllocated){
+    Msg::Error("System must be allocated first");
+    return;
+  }
   int blockSize = _getBlockSizeFromParameters();
   std::vector<int> nByRowDiag(_localSize), nByRowOffDiag(_localSize);
   if(_sparsity.getNbRows() == 0) {
-- 
GitLab