From: guido Date: Wed, 10 Dec 2003 15:16:36 +0000 (+0000) Subject: catch exceptions X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b99d0fdff64a912c6065e63e4e20b7d4d1924375;p=dealii-svn.git catch exceptions git-svn-id: https://svn.dealii.org/trunk@8252 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/solver_cg.h b/deal.II/lac/include/lac/solver_cg.h index 0ebee15b0f..142a67d777 100644 --- a/deal.II/lac/include/lac/solver_cg.h +++ b/deal.II/lac/include/lac/solver_cg.h @@ -167,6 +167,9 @@ class SolverCG : public Solver * Additional parameters. */ AdditionalData additional_data; + + private: + void cleanup(); }; /*@}*/ @@ -200,6 +203,19 @@ SolverCG::criterion() +template +void +SolverCG::cleanup() +{ + this->memory.free(Vr); + this->memory.free(Vp); + this->memory.free(Vz); + this->memory.free(VAp); + deallog.pop(); +} + + + template void SolverCG::print_vectors(const unsigned int, @@ -227,93 +243,88 @@ SolverCG::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) diff --git a/deal.II/lac/include/lac/solver_richardson.h b/deal.II/lac/include/lac/solver_richardson.h index f934663d21..d2fc5e732f 100644 --- a/deal.II/lac/include/lac/solver_richardson.h +++ b/deal.II/lac/include/lac/solver_richardson.h @@ -231,17 +231,16 @@ SolverRichardson::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::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