CP2K 2.4 (Revision 12889)

cp_common_uses.h

Go to the documentation of this file.
00001 ! Common use statements and preprocessor macros
00002 ! should be included in the use statements
00003 
00004   USE cp_log_handling
00005   USE cp_error_handling
00006 
00007 #ifndef CP_PREP_GLOBALS_H
00008 #define CP_PREP_GLOBALS_H
00009 
00010 ! The following macros are here to facilitate the use of error handling
00011 ! proposed in cp_error_handling.
00012 ! they assume at least
00013 ! 'use cp_error_handling, only: cp_assert, cp_a_l, cp_error_type'
00014 ! and 'use cp_log_handling, only: cp_to_string'
00015 ! They ere useful because they give a reference to the file and line
00016 ! number in the error message.
00017 
00018 
00019 ! this macro expands to a string that contains the filename.
00020 ! if the path is long the filename could make that some lines of code
00021 ! become too long and overlow (fortran compilers have a maximum line length)
00022 ! in this case substitute __FILE__ with "file" down here.
00023 ! obviously then the error messages will not give the filename.
00024 ! (otherwise make the file reference in the makefile relative vs. absolute)
00025 #ifdef __SHORT_FILE__
00026 #define CPSourceFileRef __SHORT_FILE__//' line '//TRIM(ADJUSTL(cp_to_string(__LINE__)))
00027 #else
00028 #define CPSourceFileRef __FILE__//' line '//TRIM(ADJUSTL(cp_to_string(__LINE__)))
00029 #endif
00030 
00031 ! if the following macro is defined the longest form of macro
00032 ! expansions is used (and the error messages are more meaningful)
00033 
00034 #ifdef FD_LONG_EXPANSIONS
00035 
00036 ! inlines the test but does not write the file name, but that should
00037 ! be easily recovered from the routineP variable (that should contain
00038 ! the module name).
00039 
00040 ! (cp_precondition_failed = 200)
00041 #define CPPrecondition(cond,level,routineP,error,failure) \
00042 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,200,routineP,\
00043 'PRECONDITION(cond) failed at line '//TRIM(ADJUSTL(cp_to_string(__LINE__))),error,failure)
00044 
00045 ! (cp_postcondition_failed = -200)
00046 #define CPPostcondition(cond,level,routineP,error,failure) \
00047 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,-200,routineP,\
00048 'POSTCONDITION(cond) failed at line '//cp_to_string(__LINE__),error,failure)
00049 
00050 ! (cp_invariant_failed = -100)
00051 #define CPInvariant(cond,level,routineP,error,failure) \
00052 IF(.NOT.(cond)) CALL cp_assert(.FALSE., level, -100,routineP,\
00053 'INVARIANT (cond) failed at line '//cp_to_string(__LINE__),error,failure)
00054 
00055 ! (cp_assert_failed = -300)
00056 #define CPAssert(cond,level,routineP,error,failure) \
00057 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,-300,routineP,\
00058 'ASSERTION (cond) failed at line '//cp_to_string(__LINE__),error,failure)
00059 
00060 #define CPErrorMessage(level,routineP,msg,error) \
00061 CALL cp_error_message(level,routineP,msg//' at line '//cp_to_string(__LINE__),error)
00062 
00063 #define CPPreconditionNoFail(cond,level,routineP,error) \
00064 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,200,routineP,\
00065 'PRECONDITION(cond) failed at line '//cp_to_string(__LINE__),error)
00066 
00067 ! (cp_postcondition_failed = -200)
00068 #define CPPostconditionNoFail(cond,level,routineP,error) \
00069 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,-200,routineP,\
00070 'POSTCONDITION(cond) failed at line '//cp_to_string(__LINE__),error)
00071 
00072 ! (cp_invariant_failed = -100)
00073 #define CPInvariantNoFail(cond,level,routineP,error) \
00074 IF(.NOT.(cond)) CALL cp_assert(.FALSE., level, -100,routineP,\
00075 'INVARIANT (cond) failed at line '//cp_to_string(__LINE__),error)
00076 
00077 ! (cp_assert_failed = -300)
00078 #define CPAssertNoFail(cond,level,routineP,error) \
00079 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,-300,routineP,\
00080 'ASSERTION (cond) failed at line '//cp_to_string(__LINE__),error)
00081 
00082 
00083 #define CPPreconditionNoErr(cond, level, routineN) \
00084 IF(.NOT.(cond)) CALL cp_assert(.FALSE., level, 200, routineN ,\
00085 "PRECONDITION failed on line " //cp_to_string(__LINE__))
00086 
00087 #define CPPostconditionNoErr(cond, level, routineN) \
00088 IF(.NOT.(cond)) CALL cp_assert(.FALSE., level, -200, routineN ,\
00089 "POSTCONDITION failed on line " //cp_to_string(__LINE__))
00090 
00091 #define CPInvariantNoErr(cond,level,routineP) \
00092 IF(.NOT.(cond)) CALL cp_assert(.FALSE., level, -100,routineP,\
00093 'INVARIANT (cond) failed at line '//cp_to_string(__LINE__))
00094 
00095 ! (cp_assert_failed = -300)
00096 #define CPAssertNoErr(cond,level,routineP) \
00097 IF(.NOT.(cond)) CALL cp_assert(.FALSE.,level,-300,routineP,\
00098 'ASSERTION (cond) failed at line '//cp_to_string(__LINE__))
00099 
00100 #else
00101 ! same as before but trying to use a small amount of characters
00102 ! the test is not inlined (you have always a function call)
00103 
00104 #define CPPrecondition(cond,level,routineP,error,failure) \
00105 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error,failure)
00106 #define CPPostcondition(cond,level,routineP,error,failure) \
00107 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error,failure)
00108 #define CPInvariant(cond,level,routineP,error,failure) \
00109 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error,failure)
00110 #define CPAssert(cond,level,routineP,error,failure) \
00111 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error,failure)
00112 #define CPErrorMessage(level,routineP,msg,error) \
00113 CALL cp_error_message(level,routineP,msg,error)
00114 #define CPPreconditionNoFail(cond,level,routineP,error) \
00115 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error)
00116 #define CPPostconditionNoFail(cond,level,routineP,error) \
00117 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error)
00118 #define CPInvariantNoFail(cond,level,routineP,error) \
00119 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error)
00120 #define CPAssertNoFail(cond,level,routineP,error) \
00121 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__,error)
00122 #define CPPreconditionNoErr(cond, level, routineN) \
00123 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineN,__LINE__)
00124 #define CPPostconditionNoErr(cond, level, routineN) \
00125 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineN ,__LINE__)
00126 #define CPInvariantNoErr(cond,level,routineP) \
00127 IF(.NOT.(cond))CALL cp_a_l(0==1, level, routineP,__LINE__)
00128 #define CPAssertNoErr(cond,level,routineP) \
00129 IF(.NOT.(cond))CALL cp_a_l(0==1,level,routineP,__LINE__)
00130 
00131 #endif
00132 
00133 #define CPAAssert(condition) \
00134 CALL cp_simple_assert(condition, routineP, __LINE__)
00135 #endif