bianchi2_error_mod.f90
Go to the documentation of this file.
1 !------------------------------------------------------------------------------
2 ! bianchi2_error_mod -- BIANCHI2 library error class
3 !
9 !------------------------------------------------------------------------------
10 
12 
13  use s2_types_mod, only: s2_string_len
14 
15  implicit none
16 
17  private
18 
19 
20  !---------------------------------------
21  ! Subroutine and function scope
22  !---------------------------------------
23 
24  public :: bianchi2_error
25 
26 
27  !---------------------------------------
28  ! Global variables
29  !---------------------------------------
30 
31  integer, parameter :: BIANCHI2_ERROR_NUM = 11
32 
33  integer, public, parameter :: &
34  BIANCHI2_ERROR_NONE = 0, &
35  BIANCHI2_ERROR_INIT = 1, &
36  BIANCHI2_ERROR_NOT_INIT = 2, &
37  BIANCHI2_ERROR_INIT_FAIL = 3, &
38  BIANCHI2_ERROR_MEM_ALLOC_FAIL = 4, &
39  BIANCHI2_ERROR_SIM_PARAM_INVALID = 5, &
40  BIANCHI2_ERROR_SIM_NARG = 6, &
41  BIANCHI2_ERROR_SKY_NUM_FAIL = 7, &
42  BIANCHI2_ERROR_TMPLFIT_FAIL = 8, &
43  BIANCHI2_ERROR_PLM1TABLE_THETA_INVALID = 9, &
44  BIANCHI2_ERROR_PLM1TABLE_L_INVALID = 10
45 
46  ! Each element of the error_comment array must have the same length, thus
47  ! space with trailing space characters. When come to use trim to remove
48  ! trailing spaces.
49  ! Comment associated with each error type.
50  character(len=S2_STRING_LEN), parameter :: &
51  error_comment(BIANCHI2_ERROR_NUM) = &
52  (/ &
53  'No error ', &
54  'Attempt to initialise object that has already been initialised ', &
55  'Object not initialised ', &
56  'Object initialisation failed ', &
57  'Memory allocation failed ', &
58  'Invalid simulation parameter ', &
59  'Invalid number of command line parameters ', &
60  'Numerical routine failed ', &
61  'Template fitting failed ', &
62  'Invalid theta for Plm1 lookup table (not on regular grid) ', &
63  'Invalid l for Plm1 lookup table (out of range) ' &
64  /)
65 
67  logical, parameter :: &
68  halt_default(BIANCHI2_ERROR_NUM) = &
69  (/ &
70  .false., &
71  .true., &
72  .true., &
73  .true., &
74  .true., &
75  .true., &
76  .true., &
77  .true., &
78  .true., &
79  .true., &
80  .true. /)
81 
82 
83  !----------------------------------------------------------------------------
84 
85  contains
86 
87 
88  !--------------------------------------------------------------------------
89  ! bianchi2_error
90  !
104  !--------------------------------------------------------------------------
105 
106  subroutine bianchi2_error(error_code, procedure, comment_add, &
107  comment_out, halt_in)
108 
109  integer, intent(in) :: error_code
110  character(len=*), intent(in), optional :: procedure, comment_add
111  character(len=*), intent(inout), optional :: comment_out
112  logical, intent(in), optional :: halt_in
113 
114  logical :: halt
115  character(len=*), parameter :: comment_prefix = 'BIANCHI2_ERROR: '
116 
117  !---------------------------------------
118  ! Display error message
119  !---------------------------------------
120 
121  if(present(procedure)) then
122 
123  if(present(comment_add)) then
124  write(*,'(a,a,a,a,a,a,a,a)') comment_prefix, 'Error ''', &
125  trim(error_comment(error_code+1)), &
126  ''' occured in procedure ''', &
127  trim(procedure), &
128  '''', &
129  ' - ', trim(comment_add)
130  else
131  write(*,'(a,a,a,a,a,a)') comment_prefix, 'Error ''', &
132  trim(error_comment(error_code+1)), &
133  ''' occured in procedure ''', &
134  trim(procedure), &
135  ''''
136  end if
137 
138  else
139 
140  if(present(comment_add)) then
141  write(*,'(a,a,a,a)') comment_prefix, &
142  trim(error_comment(error_code+1)), &
143  ' - ', trim(comment_add)
144  else
145  write(*,'(a,a)') comment_prefix, trim(error_comment(error_code+1))
146  end if
147 
148  end if
149 
150  ! Copy error comment if comment_out present.
151  if(present(comment_out)) comment_out = error_comment(error_code+1)
152 
153  !---------------------------------------
154  ! Halt program execution if required
155  !---------------------------------------
156 
157  if( present(halt_in) ) then
158  halt = halt_in
159  else
160  halt = halt_default(error_code+1)
161  end if
162 
163  if( halt ) then
164  write(*,'(a,a,a,a,a)') comment_prefix, &
165  ' Halting program execution ', &
166  'due to error ''', trim(error_comment(error_code+1)), ''''
167  stop
168  end if
169 
170  end subroutine bianchi2_error
171 
172 
173 end module bianchi2_error_mod