PetscException.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include <sstream>
00030 
00031 #include "PetscException.hpp"
00032 #include "Exception.hpp"
00033 
00034 
00035 //Positive codes mean that there's an error
00036 //Zero means success
00037 //Negative codes should never happen, but we'll throw anyway
00038 void PetscException(PetscInt petscError,
00039                     unsigned line,
00040                     const char* funct,
00041                     const char* file)
00042 {
00043     if (petscError != 0)
00044     {
00045         const char*  p_text;
00046         char default_message[30]="Unknown PETSc error code";
00047 
00048         //PetscErrorMessage will swing p_text to point to the error code's message
00049         //...but only if it's a valid code
00050         PetscErrorMessage(petscError,  &p_text, NULL);
00051         if (p_text == 0)
00052         {
00053             p_text=default_message;
00054         }
00055 
00056         std::stringstream err_string;
00057         err_string << p_text;
00058         err_string << " in function '";
00059         err_string << funct;
00060         err_string << "' on line ";
00061         err_string << line;
00062         err_string << " of file ";
00063         err_string << file;
00064 
00065         EXCEPTION(err_string.str());
00066     }
00067 }
00068 
00069 //Positive codes mean that the KSP converged
00070 //Negative codes mean that the KSP diverged i.e. there's a problem
00071 void KspException(PetscInt kspError,
00072                   unsigned line,
00073                   const char* funct,
00074                   const char* file)
00075 {
00076     if (kspError < 0)
00077     {
00078         std::string err_string;
00079 
00080 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00081         switch (kspError)
00082         {
00083             case KSP_DIVERGED_ITS:
00084                 err_string = "KSP_DIVERGED_ITS";
00085                 break;
00086             case KSP_DIVERGED_DTOL:
00087                 err_string = "KSP_DIVERGED_DTOL";
00088                 break;
00089             case KSP_DIVERGED_BREAKDOWN:
00090                 err_string = "KSP_DIVERGED_BREAKDOWN";
00091                 break;
00092             case KSP_DIVERGED_BREAKDOWN_BICG:
00093                 err_string = "KSP_DIVERGED_BREAKDOWN_BICG";
00094                 break;
00095             case KSP_DIVERGED_NONSYMMETRIC:
00096                 err_string = "KSP_DIVERGED_NONSYMMETRIC";
00097                 break;
00098             case KSP_DIVERGED_INDEFINITE_PC:
00099                 err_string = "KSP_DIVERGED_INDEFINITE_PC";
00100                 break;
00101             default:
00102                 err_string = "Unknown KSP error code";
00103           }
00104   #else
00105         // This array contains the strings describing KSP
00106         // convergence/divergence reasons. It is exported by
00107         // libpetscksp.a
00108         extern const char **KSPConvergedReasons;
00109 
00110         // The code for the last known error (-10) is hardcoded in PETSc,
00111         // in future releases it might change. It is defined in
00112         // src/ksp/ksp/interface/dlregisksp.c
00113         if (kspError >= -10) err_string = KSPConvergedReasons[kspError];
00114         else err_string = "Unknown KSP error code";
00115   #endif
00116 
00117         err_string += " in function '";
00118         err_string += funct;
00119         err_string += "' on line ";
00120         err_string += line;
00121         err_string += " of file ";
00122         err_string += file;
00123 
00124         EXCEPTION(err_string);
00125     }
00126 }

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5