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 #include "Warnings.hpp"
00034 
00035 
00036 /*
00037  * Positive codes mean that there's an error.
00038  * Zero means success.
00039  * Negative codes should never happen, but we'll throw anyway.
00040  */
00041 void PetscException(PetscInt petscError,
00042                     unsigned line,
00043                     const char* funct,
00044                     const char* file)
00045 {
00046     if (petscError != 0)
00047     {
00048         const char*  p_text;
00049         char default_message[30]="Unknown PETSc error code";
00050 
00051         /*
00052          * PetscErrorMessage will swing p_text to point to the error code's message
00053          * ...but only if it's a valid code.
00054          */
00055         PetscErrorMessage(petscError, &p_text, NULL);
00056         if (p_text == 0)
00057         {
00058             p_text=default_message;
00059         }
00060         EXCEPTION(p_text << " in function '" << funct  << "' on line "
00061                   << line << " of file " << file);
00062     }
00063 }
00064 
00065 /*
00066  * Positive codes mean that the KSP converged.
00067  * Negative codes mean that the KSP diverged, i.e. there's a problem.
00068  */
00069 std::string GetKspErrorMessage(PetscInt kspError)
00070 {
00071     std::string err_string;
00072 
00073 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
00074     switch (kspError)
00075     {
00076         case KSP_DIVERGED_ITS:
00077             err_string = "KSP_DIVERGED_ITS";
00078             break;
00079         case KSP_DIVERGED_DTOL:
00080             err_string = "KSP_DIVERGED_DTOL";
00081             break;
00082         case KSP_DIVERGED_BREAKDOWN:
00083             err_string = "KSP_DIVERGED_BREAKDOWN";
00084             break;
00085         case KSP_DIVERGED_BREAKDOWN_BICG:
00086             err_string = "KSP_DIVERGED_BREAKDOWN_BICG";
00087             break;
00088         case KSP_DIVERGED_NONSYMMETRIC:
00089             err_string = "KSP_DIVERGED_NONSYMMETRIC";
00090             break;
00091         case KSP_DIVERGED_INDEFINITE_PC:
00092             err_string = "KSP_DIVERGED_INDEFINITE_PC";
00093             break;
00094         default:
00095             err_string = "Unknown KSP error code";
00096     }
00097 #else
00098     // This array contains the strings describing KSP convergence/divergence reasons.
00099     // It is exported by libpetscksp.a
00100     extern const char **KSPConvergedReasons;
00101 
00102     // The code for the last known error (-10) is hardcoded in PETSc, in future releases it might change.
00103     // It is defined in src/ksp/ksp/interface/dlregisksp.c
00104     if (kspError >= -10)
00105     {
00106         err_string = KSPConvergedReasons[kspError];
00107     }
00108     else
00109     {
00110         err_string = "Unknown KSP error code";
00111     }
00112     #endif
00113 
00114     return err_string;
00115 }
00116 
00117 /*
00118  * Positive codes mean that the KSP converged.
00119  * Negative codes mean that the KSP diverged, i.e. there's a problem.
00120  */
00121 void KspException(PetscInt kspError,
00122                   unsigned line,
00123                   const char* funct,
00124                   const char* file)
00125 {
00126     if (kspError < 0)
00127     {
00128         std::string err_string = GetKspErrorMessage(kspError);
00129 
00130         err_string += " in function '";
00131         err_string += funct;
00132         err_string += "' on line ";
00133         err_string += line;
00134         err_string += " of file ";
00135         err_string += file;
00136 
00137         EXCEPTION(err_string);
00138     }
00139 }
00140 
00141 void KspWarnIfFailed(PetscInt kspError)
00142 {
00143     if (kspError < 0)
00144     {
00145         std::string message = "Linear solve failed: " + GetKspErrorMessage(kspError);
00146         WARNING(message);
00147     }
00148 }
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3