PetscException.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <sstream>
00037
00038 #include "PetscException.hpp"
00039 #include "Exception.hpp"
00040 #include "Warnings.hpp"
00041
00042
00043
00044
00045
00046
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
00060
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
00074
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
00106
00107 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
00108
00109 extern const char * const *KSPConvergedReasons;
00110 #else
00111 extern const char **KSPConvergedReasons;
00112 #endif
00113
00114
00115
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
00131
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 }