From 94d8a12ec97b1c382f9ff6ef363d11a55f1e78d9 Mon Sep 17 00:00:00 2001 From: Carlos Brito Date: Wed, 9 Aug 2023 17:34:34 -0400 Subject: [PATCH] Add option to silence exceptions --- CMakeLists.txt | 1 + examples/Misc/RS2023/L2_Grid_Plot.py | 85 +++++++++++++++------------- src/Rodin/Alert/Alert.h | 2 +- src/Rodin/Alert/Exception.cpp | 16 ++++-- src/Rodin/Alert/Exception.h | 3 + src/Rodin/Alert/Warning.cpp | 8 +-- src/Rodin/Configure.h.in | 9 +++ 7 files changed, 75 insertions(+), 49 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ea586d7bb..128a9b74d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ option(RODIN_USE_SPQR "Use Rodin with SPQR support" option(RODIN_USE_PARDISO "Use Rodin with Pardiso support" OFF) option(RODIN_USE_APPLE_ACCELERATE "Use Rodin with Apple Accelerate support" OFF) option(RODIN_SILENCE_WARNINGS "Silence warnings outputted by Rodin" OFF) +option(RODIN_SILENCE_EXCEPTIONS "Silence exceptions thrown by Rodin" ON) option(RODIN_CODE_COVERAGE "Compile with code coverage flags" OFF) option(RODIN_LTO "Compile with link time optimization" OFF) option(RODIN_FEATURE_SUMMARY "Print Rodin feature summary" ON) diff --git a/examples/Misc/RS2023/L2_Grid_Plot.py b/examples/Misc/RS2023/L2_Grid_Plot.py index 9ddb5aceb..59bf0f4e8 100644 --- a/examples/Misc/RS2023/L2_Grid_Plot.py +++ b/examples/Misc/RS2023/L2_Grid_Plot.py @@ -9,7 +9,9 @@ plt.style.use("bmh") conductivities = [2.0] -waveNumbers = range(1, 100) +waveNumbers = [ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 60, 80, 90 ] +angles = [ 0 ] + x_column = 'm' y_column = 'epsilon' @@ -17,13 +19,15 @@ latex_label = { 'm' : '$m$', 'epsilon' : '$\epsilon$', - 'waveNumber' : '$k$' + 'waveNumber' : '$k$', + 'angle': '$\theta$' } file_label = { 'm' : 'Period', 'epsilon' : 'Epsilon', - 'waveNumber' : 'Wavenumber' + 'waveNumber' : 'Wavenumber', + 'angle' : 'Angle', } if __name__ == '__main__': @@ -34,45 +38,50 @@ df = pd.read_csv(args.filename) print('Read dataset !') - for waveNumber in waveNumbers: - for conductivity in conductivities: - plt.figure() - print('Processing ', waveNumber) - sdf = df[ - (df['conductivity'] > conductivity - 0.001) & - (df['conductivity'] < conductivity + 0.001) & - (df['waveNumber'] > waveNumber - 0.001) & - (df['waveNumber'] < waveNumber + 0.001) - ] + for angle in angles: + for waveNumber in waveNumbers: + for conductivity in conductivities: + plt.figure() + print('Processing ', waveNumber) + sdf = df[ + (df['conductivity'] > conductivity - 0.001) & + (df['conductivity'] < conductivity + 0.001) & + (df['waveNumber'] > waveNumber - 0.001) & + (df['waveNumber'] < waveNumber + 0.001) & + (df['angle'] > angle - 0.001) & + (df['angle'] < angle + 0.001) + ] - thresh = sdf['error'].median() - sdf.loc[sdf['error'] > thresh, 'error'] = thresh + # thresh = sdf['error'].median() + # sdf.loc[sdf['error'] > thresh, 'error'] = thresh - plt.hexbin( - sdf.loc[:, x_column], - sdf.loc[:, y_column], - gridsize=45, - reduce_C_function=np.mean, - cmap='inferno', - C=sdf.loc[:, 'error'], edgecolors='face', linewidths=0.5) + plt.hexbin( + sdf.loc[:, x_column], + sdf.loc[:, y_column], + gridsize=45, + reduce_C_function=np.mean, + cmap='inferno', + C=sdf.loc[:, 'error'], edgecolors='face', linewidths=0.5) - # plt.contourf( - # sdf.loc[:, 'm'], - # sdf.loc[:, 'epsilon'], - # sdf.loc[:, 'error']) - plt.colorbar() + # plt.contourf( + # sdf.loc[:, 'm'], + # sdf.loc[:, 'epsilon'], + # sdf.loc[:, 'error']) + plt.colorbar() - plt.xlabel(latex_label[x_column]) - plt.ylabel(latex_label[y_column]) - plt.title( - '$|| u_\epsilon - u_0 ||_{L^2 (Q)} \ - \ \mathrm{with} \ \gamma |_{B(x, e)} = %.2E$, k = %.2E' % (conductivity, waveNumber), - pad=20) - out=("%s_VS_%s_Gamma=%.2E_k=%.2E") % ( - file_label[x_column], file_label[y_column], conductivity, waveNumber) - plt.savefig(out + '.svg') - plt.savefig(out + '.png') - # plt.show() + plt.xlabel(latex_label[x_column]) + plt.ylabel(latex_label[y_column]) + plt.title( + '$|| u_\epsilon - u_0 ||_{L^2 (Q)} \ + \ \mathrm{with} \ \gamma |_{B(x, e)} = %.2E, \ k = %.2E, \ + \ \\theta = %d^\circ $' % (conductivity, waveNumber, (180.0 / math.pi) * angle), + pad=20) + out=("%s_VS_%s_Gamma=%.2E_Wavenumber=%.2E_Angle=%.2E") % ( + file_label[x_column], file_label[y_column], conductivity, + waveNumber, (180.0 / math.pi) * angle) + plt.savefig(out + '.svg') + plt.savefig(out + '.png') + # plt.show() diff --git a/src/Rodin/Alert/Alert.h b/src/Rodin/Alert/Alert.h index 64aa01e62..3c68676fb 100644 --- a/src/Rodin/Alert/Alert.h +++ b/src/Rodin/Alert/Alert.h @@ -23,7 +23,7 @@ namespace Rodin::Alert { template < class U, - class = decltype(std::declval() << std::declval())> + class = decltype(std::declval() << std::declval())> static std::true_type test(U*); template diff --git a/src/Rodin/Alert/Exception.cpp b/src/Rodin/Alert/Exception.cpp index bdc26adaa..257fd5606 100644 --- a/src/Rodin/Alert/Exception.cpp +++ b/src/Rodin/Alert/Exception.cpp @@ -5,9 +5,10 @@ * https://www.boost.org/LICENSE_1_0.txt) */ #include - #include +#include "Rodin/Configure.h" + #include "Exception.h" namespace Rodin::Alert @@ -18,11 +19,14 @@ namespace Rodin::Alert void Exception::raise() const { - // std::cerr << rang::fg::red - // << "Error: " - // << rang::fg::reset - // << what() - // << std::endl; +#ifdef RODIN_SILENCE_EXCEPTIONS +#else + std::cerr << rang::fg::red + << RODIN_ALERT_WARNING_PREFIX + << rang::fg::reset + << what() + << std::endl; +#endif throw *this; } } diff --git a/src/Rodin/Alert/Exception.h b/src/Rodin/Alert/Exception.h index cf6da4fb6..b9a2d5d7c 100644 --- a/src/Rodin/Alert/Exception.h +++ b/src/Rodin/Alert/Exception.h @@ -7,6 +7,9 @@ #ifndef RODIN_ALERT_EXCEPTION_H #define RODIN_ALERT_EXCEPTION_H +#define RODIN_ALERT_EXCEPTION_PREFIX "Error: " +#define RODIN_ALERT_EXCEPTION_PREFIX_LENGTH (sizeof(RODIN_ALERT_WARNING_PREFIX) - 1) + #include #include "Alert.h" diff --git a/src/Rodin/Alert/Warning.cpp b/src/Rodin/Alert/Warning.cpp index 201abb459..d8319b61c 100644 --- a/src/Rodin/Alert/Warning.cpp +++ b/src/Rodin/Alert/Warning.cpp @@ -22,10 +22,10 @@ namespace Rodin::Alert #ifdef RODIN_SILENCE_WARNINGS #else std::cerr << rang::fg::yellow - << RODIN_ALERT_WARNING_PREFIX - << rang::fg::reset - << what() - << std::endl; + << RODIN_ALERT_WARNING_PREFIX + << rang::fg::reset + << what() + << std::endl; #endif } } diff --git a/src/Rodin/Configure.h.in b/src/Rodin/Configure.h.in index b6b888031..c8657411e 100644 --- a/src/Rodin/Configure.h.in +++ b/src/Rodin/Configure.h.in @@ -90,6 +90,15 @@ */ #cmakedefine RODIN_SILENCE_WARNINGS +/** + * @ingroup RodinDirectives + * @brief Indicates if Rodin exceptions are silenced. + * + * If defined, this directive will prevent Rodin from outputting the error + * messages. It does not prevent the exception from being thrown. + */ +#cmakedefine RODIN_SILENCE_EXCEPTIONS + /** * @ingroup RodinDirectives * @brief Indicates the Rodin resources directory.