error_msg_class.f90
Go to the documentation of this file.
1 !> @file error_msg_class.f90
2 !!
3 !! The error file code for this file is ***W16***.
4 !! @brief Module \ref error_msg_class with error handling routines
5 !!
6 
7 !> Error handling routines
8 !!
9 !! @author Darko Mocelj, Christian Winteler
10 !! @date 31.07.2003
11 !!
12 !! \b Edited:
13 !! - MR 11.01.2020 - Rewrote the module
14 !! .
15 #include "macros.h"
17  implicit none
18 
19  private
20  integer,parameter :: error_id=0 !< Default standard error unit in fortran
21 
22  logical,private :: write_header_init=.true. !< Init flag for write_data_to_std_out
23  logical,public :: data_creation_mode=.false. !< Flag for rate creation mode
24  !
25  ! Public fields and methods of the module (every routine is public)
26  !
27  public:: &
30  private::&
32 contains
33 
34 !> Write the header of the standard output (usually _OUT_)
35 !!
36 !! This subroutine writes the first header of the out file.
37 !! This looks e.g., like:
38 !! \file{
39 !! WinNet - Nuclear reaction network
40 !! \n
41 !! \n
42 !! Option : Value Unit
43 !! \-------------------------------------------------------------- }
44 !!
45 !! \b Edited:
46 !! - 08.08.22, M.R: Implemented git version
47 !! .
48 !!
49 !! @author Moritz Reichert
50 !! @date 27.01.21
51 subroutine write_header()
52  implicit none
53  character(len=40) :: msg !< helper string
54  character(len=62) :: underline !< helper string
55  character(len=10) :: value_len !< helper string
56  character(len=8) :: unit_len !< helper string
57 
58  write(*,*) " WinNet - Nuclear reaction network"
59  write(*,*) " ==================================="
60  if (data_creation_mode) then
61  write(*,*) " (Data creation mode)"
62  end if
63 
64  write(*,*) ""
65  write(*,*) ""
66  msg = "Option"
67  value_len = "Value"
68  unit_len = "Unit"
69  write(*,"(A)") adjustl(msg)//" : "//adjustr(value_len)//" "//adjustr(unit_len)
70  underline = "--------------------------------------------------------------"
71  write(*,"(A)") underline
72  write_header_init = .false.
73 
74  ! Output the Git version if known
75 #ifdef GTAG
76  call write_data_to_std_out("Release",str(gtag))
77 #else
78  call write_data_to_std_out("Release","Unknown")
79 #endif
80  ! Output the git hash if known
81 #ifdef GHASH
82  call write_data_to_std_out("Git hash",str(ghash))
83 #else
84  call write_data_to_std_out("Git hash","Unknown")
85 #endif
86 
87 end subroutine write_header
88 
89 
90 !> Write the final stats for rate creation
91 !!
92 !! This subroutine writes the final stats when
93 !! only creating a folder with prepared binary data.
94 !!
95 !! @author M. Reichert
96 !! @date 22.07.23
98  implicit none
99 
100  info_entry('write_final_stats_rate_creation')
101 
102  write(*,'(A)') '----------------------------------'
103  write(*,'(A)') 'Finished data creation successfully.'
104 
105 
106  info_exit('write_final_stats_rate_creation')
107 
108 end subroutine write_final_stats_rate_creation
109 
110 
111 
112 !> Write data to the standard output (usually _OUT_)
113 !!
114 !! This subroutine formats the output to a uniform design.
115 !! an example line is:
116 !! \file{
117 !! Network size : 6545 }
118 !!
119 !! @author Moritz Reichert
120 !! @date 27.01.21
121 subroutine write_data_to_std_out(str_msg,value_str,unit)
122  implicit none
123  character(len=*),intent(in) :: str_msg !< Message oriented to the left
124  character(len=*),intent(in) :: value_str !< Value represented as a string
125  character(len=*),intent(in),optional :: unit !< unit string
126  character(len=40) :: msg !< helper string
127  character(len=10) :: value_len !< helper string
128  character(len=8) :: unit_len !< helper string
129  character(len=62) :: tot_msg !< Message
130 
131  if (write_header_init) call write_header()
132 
133  msg = str_msg
134  value_len = value_str
135  if (present(unit)) then
136  unit_len = unit
137  tot_msg = adjustl(msg)//" : "//adjustr(value_len)//" "//adjustr(unit_len)
138  else
139  tot_msg = adjustl(msg)//" : "//adjustr(value_len)
140  end if
141 
142  write(*,"(A)") tot_msg
143 
144 end subroutine write_data_to_std_out
145 
146 
147 
148 !> Converts a string to an integer
149 !!
150 !! If the string is not a valid integer, an error message is raised.
151 !!
152 !! @author Moritz Reichert
153 !! @date 01.06.22
154 function str_to_int(input_string)
155  implicit none
156  character(len=*),intent(in) :: input_string !< String from param file
157  integer :: str_to_int !< Converted integer value from input string
158  integer :: rstat !< iostat flag
159 
160  !< Convert string to integer
161  read(input_string,'(I10)',iostat=rstat) str_to_int
162 
163  ! Raise an exception if converting does not work
164  if (rstat .ne. 0) then
165  call raise_exception('Could not parse string "'//trim(adjustl(input_string))//&
166  '". ', &
167  "str_to_int",&
168  160003)
169  end if
170 end function str_to_int
171 
172 !> Converts a string to a float
173 !!
174 !! If the string is not a valid integer, an error message is raised.
175 !!
176 !! @author Moritz Reichert
177 !! @date 01.06.22
178 function str_to_float(input_string)
179  implicit none
180  character(len=*),intent(in) :: input_string !< String from param file
181  real(r_kind) :: str_to_float !< Converted integer value from input string
182  integer :: rstat !< iostat flag
183 
184  !< Convert string to integer
185  read(input_string,*,iostat=rstat) str_to_float
186 
187  ! Raise an exception if converting does not work
188  if (rstat .ne. 0) then
189  call raise_exception('Could not parse string "'//trim(adjustl(input_string))//&
190  '". ', &
191  "str_to_float",&
192  160004)
193  end if
194 end function str_to_float
195 
196 
197 !>
198 !! Converts a given real to a string with format "(1pE10.2)".
199 !!
200 !! This function is often used in error messages as it is useful to convert
201 !! a number to a string in the message directly
202 !!
203 !! @author Moritz Reichert
204 function num_to_str(num)
205  implicit none
206  real(r_kind), intent(in) :: num !< Input float
207  character(:),allocatable :: num_to_str !< converted float
208  real(r_kind) :: num_h !< Helper variable
209  character(len=50) :: out_msg !< Helper variable
210  num_h = num
211  write(out_msg,"(1pE10.2)") num_h
212  num_to_str = trim(adjustl(out_msg))
213 end function num_to_str
214 
215 
216 !>
217 !! Converts a given integer to a string.
218 !!
219 !! This function is often used in error messages as it is useful to convert
220 !! a number to a string in the message directly.
221 !!
222 !! @author Moritz Reichert
223 function int_to_str(num)
224  implicit none
225  integer, intent(in) :: num !< Input integer
226  character(:),allocatable :: int_to_str !< converted integer
227  integer :: num_h !< Helper variable
228  character(len=50) :: out_msg !< Helper variable
229  num_h = num
230  write(out_msg,*) num_h
231  int_to_str = trim(adjustl(out_msg))! Output a trimmed value
232 end function int_to_str
233 
234 
235 !>
236 !! Raise a exception with a given error message
237 !!
238 !! This subroutine is called when some inconsistency occured. It prints
239 !! a message to the OUT file that an error occured and prints the rest to the
240 !! standard error unit (usually file ERR in WinNet). This subroutine
241 !! also terminates the program.
242 !!
243 !! @author Moritz Reichert
244 subroutine raise_exception(msg,sub,error_code)
245  implicit none
246  character(len=*), intent(in) :: msg !< Error message
247  character(len=*), optional, intent(in) :: sub !< in which subroutine [opt]
248  integer, optional, intent(in) :: error_code !< Errorcode, see \ref error_codes
249  character(len=200) :: h_msg
250  character(len=30) :: ecode_msg
251 
252  ! Write a message to the OUT file
253  write(*,*) "An error occured. Check 'ERR' file for further information."
254 
255  ! Create error code
256  if (present(error_code)) then
257  ecode_msg="ERROR: W"//int_to_str(error_code)
258  else
259  ecode_msg="ERROR: W000000"
260  end if
261 
262  ! Give the subroutine if possible
263  if (present(sub)) then
264  h_msg = "Location: "//sub//"(): "
265  write(error_id,*)
266  else
267  h_msg = "Location: "
268  end if
269 
270  ! Write the error to the standard error output
271  write(error_id,"(A)") trim(adjustl(ecode_msg))
272  write(error_id,"(A)")
273  write(error_id,"(A)") trim(adjustl(h_msg))
274  write(error_id,"(A)")
275  write(error_id,"(A)") trim(adjustl(msg))
276  write(error_id,*)
277  ! Stop the code
278  stop "Exiting."
279 
280 end subroutine raise_exception
281 
282 end module error_msg_class
error_msg_class
Error handling routines.
Definition: error_msg_class.f90:16
error_msg_class::int_to_str
character(:) function, allocatable, public int_to_str(num)
Converts a given integer to a string.
Definition: error_msg_class.f90:224
error_msg_class::raise_exception
subroutine, public raise_exception(msg, sub, error_code)
Raise a exception with a given error message.
Definition: error_msg_class.f90:245
error_msg_class::str_to_float
real(r_kind) function, public str_to_float(input_string)
Converts a string to a float.
Definition: error_msg_class.f90:179
error_msg_class::write_data_to_std_out
subroutine, public write_data_to_std_out(str_msg, value_str, unit)
Write data to the standard output (usually OUT)
Definition: error_msg_class.f90:122
error_msg_class::data_creation_mode
logical, public data_creation_mode
Flag for rate creation mode.
Definition: error_msg_class.f90:23
error_msg_class::write_header
subroutine, private write_header()
Write the header of the standard output (usually OUT)
Definition: error_msg_class.f90:52
error_msg_class::write_final_stats_rate_creation
subroutine, public write_final_stats_rate_creation
Write the final stats for rate creation.
Definition: error_msg_class.f90:98
error_msg_class::str_to_int
integer function, public str_to_int(input_string)
Converts a string to an integer.
Definition: error_msg_class.f90:155
error_msg_class::num_to_str
character(:) function, allocatable, public num_to_str(num)
Converts a given real to a string with format "(1pE10.2)".
Definition: error_msg_class.f90:205
r_kind
#define r_kind
Definition: macros.h:46
error_msg_class::error_id
integer, parameter error_id
Default standard error unit in fortran.
Definition: error_msg_class.f90:20
error_msg_class::write_header_init
logical, private write_header_init
Init flag for write_data_to_std_out.
Definition: error_msg_class.f90:22