]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
catch exceptions
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 10 Dec 2003 15:16:36 +0000 (15:16 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 10 Dec 2003 15:16:36 +0000 (15:16 +0000)
git-svn-id: https://svn.dealii.org/trunk@8252 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/solver_cg.h
deal.II/lac/include/lac/solver_richardson.h

index 0ebee15b0f5562ddd01829eddf2c5826cdb216cb..142a67d77723dd96be0d012b9211fe6025d6fd93 100644 (file)
@@ -167,6 +167,9 @@ class SolverCG : public Solver<VECTOR>
                                      * Additional parameters.
                                      */
     AdditionalData additional_data;
+
+  private:
+    void cleanup();
 };
 
 /*@}*/
@@ -200,6 +203,19 @@ SolverCG<VECTOR>::criterion()
 
 
 
+template<class VECTOR>
+void
+SolverCG<VECTOR>::cleanup()
+{
+  this->memory.free(Vr);
+  this->memory.free(Vp);
+  this->memory.free(Vz);
+  this->memory.free(VAp);
+  deallog.pop();
+}
+
+
+
 template<class VECTOR>
 void
 SolverCG<VECTOR>::print_vectors(const unsigned int,
@@ -227,93 +243,88 @@ SolverCG<VECTOR>::solve (const MATRIX         &A,
   Vp  = this->memory.alloc();
   Vz  = this->memory.alloc();
   VAp = this->memory.alloc();
-                                  // define some aliases for simpler access
-  VECTOR& g  = *Vr; 
-  VECTOR& h  = *Vp; 
-  VECTOR& d  = *Vz; 
-  VECTOR& Ad = *VAp;
-                                  // resize the vectors, but do not set
-                                  // the values since they'd be overwritten
-                                  // soon anyway.
-  g.reinit(x, true);
-  h.reinit(x, true);
-  d.reinit(x, true);
-  Ad.reinit(x, true);
-                                  // Implementation taken from the DEAL
-                                  // library
-  int  it=0;
-  double res,gh,alpha,beta;
-
-                                  // compute residual. if vector is
-                                  // zero, then short-circuit the
-                                  // full computation
-  if (!x.all_zero())
+
+  try {
+                                    // define some aliases for simpler access
+    VECTOR& g  = *Vr; 
+    VECTOR& h  = *Vp; 
+    VECTOR& d  = *Vz; 
+    VECTOR& Ad = *VAp;
+                                    // resize the vectors, but do not set
+                                    // the values since they'd be overwritten
+                                    // soon anyway.
+    g.reinit(x, true);
+    h.reinit(x, true);
+    d.reinit(x, true);
+    Ad.reinit(x, true);
+                                    // Implementation taken from the DEAL
+                                    // library
+    int  it=0;
+    double res,gh,alpha,beta;
+    
+                                    // compute residual. if vector is
+                                    // zero, then short-circuit the
+                                    // full computation
+    if (!x.all_zero())
+      {
+       A.vmult(g,x);
+       g.sadd(-1.,1.,b);
+      }
+    else
+      g = b;
+    res = g.l2_norm();
+    
+    conv = this->control().check(0,res);
+    if (conv) 
+      {
+       cleanup();
+       return;
+      }
+    
+    g.scale(-1.);
+    precondition.vmult(h,g);
+    
+    d.equ(-1.,h);
+    
+    gh = g*h;
+    
+    while (conv == SolverControl::iterate)
+      {
+       it++;
+       A.vmult(Ad,d);
+       
+       alpha = d*Ad;
+       alpha = gh/alpha;
+       
+       g.add(alpha,Ad);
+       x.add(alpha,d );
+       res = g.l2_norm();
+       
+       print_vectors(it, x, g, d);
+       
+       conv = this->control().check(it,res);
+       if (conv)
+         break;
+       
+       precondition.vmult(h,g);
+       
+       beta = gh;
+       gh   = g*h;
+       beta = gh/beta;
+       
+       if (additional_data.log_coefficients)
+         deallog << "alpha-beta:" << alpha << '\t' << beta << std::endl;
+       
+       d.sadd(beta,-1.,h);
+      }
+  }
+  catch (...)
     {
-      A.vmult(g,x);
-      g.sadd(-1.,1.,b);
+      cleanup();
+      throw;
     }
-  else
-    g = b;
-  res = g.l2_norm();
-  
-  conv = this->control().check(0,res);
-  if (conv) 
-    {
-      this->memory.free(Vr);
-      this->memory.free(Vp);
-      this->memory.free(Vz);
-      this->memory.free(VAp);
-      deallog.pop();
-      return;
-    };
-  
-  g.scale(-1.);
-  precondition.vmult(h,g);
-  d.equ(-1.,h);
-  gh = g*h;
-  while (conv == SolverControl::iterate)
-    {
-      it++;
-      A.vmult(Ad,d);
-      
-      alpha = d*Ad;
-      alpha = gh/alpha;
-      
-      g.add(alpha,Ad);
-      x.add(alpha,d );
-      res = g.l2_norm();
-
-      print_vectors(it, x, g, d);
-      
-      conv = this->control().check(it,res);
-      if (conv)
-       break;
-      
-      precondition.vmult(h,g);
-      
-      beta = gh;
-      gh   = g*h;
-      beta = gh/beta;
-      
-      if (additional_data.log_coefficients)
-       deallog << "alpha-beta:" << alpha << '\t' << beta << std::endl;
-      
-      d.sadd(beta,-1.,h);
-    };
-
-
                                   // Deallocate Memory
-  this->memory.free(Vr);
-  this->memory.free(Vp);
-  this->memory.free(Vz);
-  this->memory.free(VAp);
-                                  // Output
-  deallog.pop();
+  cleanup();
                                   // in case of failure: throw
                                   // exception
   if (this->control().last_check() != SolverControl::success)
index f934663d212b9a2f90a82ef8d55c2ac20c207535..d2fc5e732f0073fe761b1bd22d07b35990be4c4b 100644 (file)
@@ -231,17 +231,16 @@ SolverRichardson<VECTOR>::solve (const MATRIX         &A,
          print_vectors(iter,x,r,d);
        }
     }
-  catch (const ExceptionBase& e)
+  catch (...)
     {
       this->memory.free(Vr);
       this->memory.free(Vd);
       deallog.pop();
-      throw e;
+      throw;
     }
                                   // Deallocate Memory
   this->memory.free(Vr);
   this->memory.free(Vd);
-
   deallog.pop();
 
                                   // in case of failure: throw
@@ -289,18 +288,17 @@ SolverRichardson<VECTOR>::Tsolve (const MATRIX         &A,
          print_vectors(iter,x,r,d);
        }
     }
-  catch (const ExceptionBase& e)
+  catch (...)
     {
       this->memory.free(Vr);
       this->memory.free(Vd);
       deallog.pop();
-      throw e;
+      throw;
     }
   
                                   // Deallocate Memory
   this->memory.free(Vr);
   this->memory.free(Vd);
-
   deallog.pop();
                                   // in case of failure: throw
                                   // exception

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.