Chaste Release::3.1
PetscException.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include <sstream>
00037 
00038 #include "PetscException.hpp"
00039 #include "Exception.hpp"
00040 #include "Warnings.hpp"
00041 
00042 
00043 /*
00044  * Positive codes mean that there's an error.
00045  * Zero means success.
00046  * Negative codes should never happen, but we'll throw anyway.
00047  */
00048 void PetscException(PetscInt petscError,
00049                     unsigned line,
00050                     const char* funct,
00051                     const char* file)
00052 {
00053     if (petscError != 0)
00054     {
00055         const char*  p_text;
00056         char default_message[30]="Unknown PETSc error code";
00057 
00058         /*
00059          * PetscErrorMessage will swing p_text to point to the error code's message
00060          * ...but only if it's a valid code.
00061          */
00062         PetscErrorMessage(petscError, &p_text, NULL);
00063         if (p_text == 0)
00064         {
00065             p_text=default_message;
00066         }
00067         EXCEPTION(p_text << " in function '" << funct  << "' on line "
00068                   << line << " of file " << file);
00069     }
00070 }
00071 
00072 /*
00073  * Positive codes mean that the KSP converged.
00074  * Negative codes mean that the KSP diverged, i.e. there's a problem.
00075  */
00076 std::string GetKspErrorMessage(PetscInt kspError)
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 convergence/divergence reasons.
00106     // It is exported by libpetscksp.a
00107 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00108     //External declaration of KSPConvergedReasons already happens
00109     extern const char * const *KSPConvergedReasons;
00110 #else
00111     extern const char **KSPConvergedReasons;
00112 #endif
00113 
00114     // The code for the last known error (-10) is hardcoded in PETSc, in future releases it might change.
00115     // It is defined in src/ksp/ksp/interface/dlregisksp.c
00116     if (kspError >= -10)
00117     {
00118         err_string = KSPConvergedReasons[kspError];
00119     }
00120     else
00121     {
00122         err_string = "Unknown KSP error code";
00123     }
00124     #endif
00125 
00126     return err_string;
00127 }
00128 
00129 /*
00130  * Positive codes mean that the KSP converged.
00131  * Negative codes mean that the KSP diverged, i.e. there's a problem.
00132  */
00133 void KspException(PetscInt kspError,
00134                   unsigned line,
00135                   const char* funct,
00136                   const char* file)
00137 {
00138     if (kspError < 0)
00139     {
00140         std::string err_string = GetKspErrorMessage(kspError);
00141 
00142         err_string += " in function '";
00143         err_string += funct;
00144         err_string += "' on line ";
00145         err_string += line;
00146         err_string += " of file ";
00147         err_string += file;
00148 
00149         EXCEPTION(err_string);
00150     }
00151 }
00152 
00153 void KspWarnIfFailed(PetscInt kspError)
00154 {
00155     if (kspError < 0)
00156     {
00157         std::string message = "Linear solve failed: " + GetKspErrorMessage(kspError);
00158         WARNING(message);
00159     }
00160 }