PetscException.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 
00030 #include "PetscException.hpp"
00031 
00032 
00033 //Positive codes mean that there's an error
00034 //Zero means success
00035 //Negative codes should never happen, but we'll throw anyway
00036 void PetscException(PetscInt petscError,
00037                     unsigned line,
00038                     const char* funct,
00039                     const char* file)
00040 {
00041     if (petscError != 0)
00042     {
00043         const char*  p_text;
00044         char default_message[30]="Unknown PETSc error code";
00045 
00046         //PetscErrorMessage will swing p_text to point to the error code's message
00047         //...but only if it's a valid code
00048         PetscErrorMessage(petscError,  &p_text, NULL);
00049         if (p_text == 0)
00050         {
00051             p_text=default_message;
00052         }
00053 
00054         std::stringstream err_string;
00055         err_string << p_text;
00056         err_string << " in function '";
00057         err_string << funct;
00058         err_string << "' on line ";
00059         err_string << line;
00060         err_string << " of file ";
00061         err_string << file;
00062 
00063         EXCEPTION(err_string.str());
00064     }
00065 }
00066 
00067 //Positive codes mean that the KSP converged
00068 //Negative codes mean that the KSP diverged i.e. there's a problem
00069 void KspException(PetscInt kspError,
00070                   unsigned line,
00071                   const char* funct,
00072                   const char* file)
00073 {
00074     if (kspError < 0)
00075     {
00076         std::string err_string;
00077 
00078   #if (PETSC_VERSION_MINOR == 2) //Old API
00079         switch (kspError)
00080         {
00081             case KSP_DIVERGED_ITS:
00082                 err_string = "KSP_DIVERGED_ITS";
00083                 break;
00084             case KSP_DIVERGED_DTOL:
00085                 err_string = "KSP_DIVERGED_DTOL";
00086                 break;
00087             case KSP_DIVERGED_BREAKDOWN:
00088                 err_string = "KSP_DIVERGED_BREAKDOWN";
00089                 break;
00090             case KSP_DIVERGED_BREAKDOWN_BICG:
00091                 err_string = "KSP_DIVERGED_BREAKDOWN_BICG";
00092                 break;
00093             case KSP_DIVERGED_NONSYMMETRIC:
00094                 err_string = "KSP_DIVERGED_NONSYMMETRIC";
00095                 break;
00096             case KSP_DIVERGED_INDEFINITE_PC:
00097                 err_string = "KSP_DIVERGED_INDEFINITE_PC";
00098                 break;
00099             default:
00100                 err_string = "Unknown KSP error code";
00101           }
00102   #else
00103         // This array contains the strings describing KSP
00104         // convergence/divergence reasons. It is exported by
00105         // libpetscksp.a
00106         extern const char **KSPConvergedReasons;
00107 
00108         // The code for the last known error (-10) is hardcoded in PETSc,
00109         // in future releases it might change. It is defined in
00110         // src/ksp/ksp/interface/dlregisksp.c
00111         if (kspError >= -10) err_string = KSPConvergedReasons[kspError];
00112         else err_string = "Unknown KSP error code";
00113   #endif
00114 
00115         err_string += " in function '";
00116         err_string += funct;
00117         err_string += "' on line ";
00118         err_string += line;
00119         err_string += " of file ";
00120         err_string += file;
00121 
00122         EXCEPTION(err_string);
00123     }
00124 }

Generated on Wed Mar 18 12:51:50 2009 for Chaste by  doxygen 1.5.5