#ifndef ERRORS_H #define ERRORS_H #include #include #include #include #include "blitz/array.h" #include "simulation/mpi_communication.h" // if any debug is defined, define the main function to print the debugging message #if defined debug_level3 || defined debug_level2 || defined debug_level1 || \ defined debug_level0 inline void DEBUG(const std::string& message) { std::cout << "#DEBUG<" << MpiCommunication::mpi_rank << ">: " << message << std::endl; } // depending on the debug level, activate the corresponding debugging function #if defined debug_level0 // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 0 inline void DEBUG_LEVEL_0(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 1 inline void DEBUG_LEVEL_1(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 2 inline void DEBUG_LEVEL_2(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 3 inline void DEBUG_LEVEL_3(const std::string& message) {} #elif defined debug_level1 // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 0 inline void DEBUG_LEVEL_0(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 1 inline void DEBUG_LEVEL_1(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 2 inline void DEBUG_LEVEL_2(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 3 inline void DEBUG_LEVEL_3(const std::string& message) {} #elif defined debug_level2 // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 0 inline void DEBUG_LEVEL_0(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 1 inline void DEBUG_LEVEL_1(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 2 inline void DEBUG_LEVEL_2(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 3 inline void DEBUG_LEVEL_3(const std::string& message) {} #else // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 0 inline void DEBUG_LEVEL_0(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 1 inline void DEBUG_LEVEL_1(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 2 inline void DEBUG_LEVEL_2(const std::string& message) { DEBUG(message); } // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 3 inline void DEBUG_LEVEL_3(const std::string& message) { DEBUG(message); } #endif #else inline void DEBUG(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 0 inline void DEBUG_LEVEL_0(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 1 inline void DEBUG_LEVEL_1(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 2 inline void DEBUG_LEVEL_2(const std::string& message) {} // prints the message string to stdout if the code has been compiled with a // DEBUG_LEVEL > 3 inline void DEBUG_LEVEL_3(const std::string& message) {} #endif // for MPI process 0 print the message string to stdout inline void SIM_INFO(const std::string& message) { if (MpiCommunication::mpi_rank == 0) { std::cout << "#INFO: " << message << std::endl; } } // writes message string to stderr and exits the program with an EXIT_FAILURE inline void ERROR(const std::string& message) { fprintf(stderr, "ERROR <%d> %s\n", MpiCommunication::mpi_rank, message.c_str()); exit(EXIT_FAILURE); // throw std::runtime_error(message); } // assert if value is positive otherwise exit the program and print the message to stderr template void ASSERT_POSITIVE(T* value, const std::string& message) { if (!(*value > 0)) { ERROR(message); } } // assert if value is not negative otherwise exit the program and print the message to stderr template void ASSERT_NOTNEGATIVE(T* value, const std::string& message) { if (!(*value >= 0)) { ERROR(message); } } // assert if value exists otherwise exit the program and print the message to stderr template void ASSERT_EXISTS(T* value, const std::string& message) { if (!value) { ERROR(message); } } // assert if value is finite otherwise exit the program and print the message to stderr inline void ASSERT_FINITE(double value, const std::string& message) { if (!std::isfinite(value)) { ERROR(message); } } // assert if the blitz::TinyVector is finite otherwise // exit the program and print the message to stderr inline void ASSERT_FINITE(const blitz::TinyVector& value, const std::string& message) { if (!std::isfinite(value(0)) || !std::isfinite(value(1)) || !std::isfinite(value(2))) { ERROR(message); } } #endif /* ERRORS_H */