MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
Sim.f90
Go to the documentation of this file.
1!> @brief This module contains simulation methods
2!!
3!! This module contains simulation methods for storing warning and error
4!! messages and notes. This module also has methods for counting warnings,
5!! errors, and notes in addition to stopping the simulation. The module does
6!! not have any dependencies on models, exchanges, or solutions in a
7!! simulation.
8!!
9!<
11
12 use kindmodule, only: dp, i4b
13 use errorutilmodule, only: pstop
14 use definedmacros, only: get_os
16 done, &
17 iustart, iulast, &
24
25 implicit none
26
27 private
28 public :: count_errors
29 public :: store_error
30 public :: ustop
31 public :: converge_reset
32 public :: converge_check
33 public :: initial_message
34 public :: final_message
35 public :: store_warning
36 public :: deprecation_warning
37 public :: store_note
38 public :: count_warnings
39 public :: count_notes
40 public :: store_error_unit
41 public :: store_error_filename
42 public :: maxerrors
43
48
49contains
50
51 !> @brief Return number of errors
52 !!
53 !! Function to return the number of errors messages that have been stored.
54 !!
55 !! @return ncount number of error messages stored
56 !!
57 !! @hidecallergraph
58 !<
59 function count_errors() result(ncount)
60 integer(I4B) :: ncount
61 ncount = sim_errors%count()
62 end function count_errors
63
64 !> @brief Return number of warnings
65 !!
66 !! Function to return the number of warning messages that have been stored.
67 !!
68 !! @return ncount number of warning messages stored
69 !!
70 !<
71 function count_warnings() result(ncount)
72 integer(I4B) :: ncount
73 ncount = sim_warnings%count()
74 end function count_warnings
75
76 !> @brief Return the number of notes stored.
77 !<
78 function count_notes() result(ncount)
79 integer(I4B) :: ncount
80 ncount = sim_notes%count()
81 end function count_notes
82
83 !> @brief Set the maximum number of errors to be stored.
84 !<
85 subroutine maxerrors(imax)
86 integer(I4B), intent(in) :: imax !< maximum number of error messages that will be stored
87 call sim_errors%set_max(imax)
88 end subroutine maxerrors
89
90 !> @brief Store an error message.
91 !! @hidecallergraph
92 !<
93 subroutine store_error(msg, terminate)
94 ! -- dummy variable
95 character(len=*), intent(in) :: msg !< error message
96 logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
97 ! -- local variables
98 logical :: lterminate
99 !
100 ! -- process optional variables
101 if (present(terminate)) then
102 lterminate = terminate
103 else
104 lterminate = .false.
105 end if
106 !
107 ! -- store error
108 call sim_errors%store(msg)
109 !
110 ! -- terminate the simulation
111 if (lterminate) then
112 call ustop()
113 end if
114
115 end subroutine store_error
116
117 !> @brief Get the file name
118 !!
119 !! Subroutine to get the file name from the unit number for a open file.
120 !! If the INQUIRE function returns the full path (for example, the INTEL
121 !! compiler) then the returned file name (fname) is limited to the filename
122 !! without the path.
123 !!
124 !<
125 subroutine get_filename(iunit, fname)
126 ! -- dummy variables
127 integer(I4B), intent(in) :: iunit !< open file unit number
128 character(len=*), intent(inout) :: fname !< file name attached to the open file unit number
129 ! -- local variables
130 integer(I4B) :: ipos
131 integer(I4B) :: ios
132 integer(I4B) :: ilen
133 !
134 ! -- get file name from unit number
135 ipos = 0
136 inquire (unit=iunit, name=fname)
137 !
138 ! -- determine the operating system
139 ios = get_os()
140 !
141 ! -- extract filename from full path, if present
142 ! forward slash on linux, unix, and osx
143 if (ios /= oswin) then
144 ipos = index(fname, '/', back=.true.)
145 end if
146 !
147 ! -- check for backslash on windows or undefined os and
148 ! forward slashes were not found
149 if (ios == oswin .or. ios == osundef) then
150 if (ipos < 1) then
151 ipos = index(fname, '\', back=.true.)
152 end if
153 end if
154 !
155 ! -- exclude the path from the file name
156 if (ipos > 0) then
157 ilen = len_trim(fname)
158 write (fname, '(a)') fname(ipos + 1:ilen)//' '
159 end if
160
161 end subroutine get_filename
162
163 !> @brief Store the file unit number
164 !!
165 !! Subroutine to convert the unit number for a open file to a file name
166 !! and indicate that there is an error reading from the file. By default,
167 !! the simulation is terminated when this subroutine is called.
168 !!
169 !! @hidecallergraph
170 !<
171 subroutine store_error_unit(iunit, terminate)
172 ! -- dummy variables
173 integer(I4B), intent(in) :: iunit !< open file unit number
174 logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
175 ! -- local variables
176 logical :: lterminate
177 character(len=LINELENGTH) :: fname
178 character(len=LINELENGTH) :: errmsg
179 !
180 ! -- process optional variables
181 if (present(terminate)) then
182 lterminate = terminate
183 else
184 lterminate = .true.
185 end if
186 !
187 ! -- store error unit
188 call get_filename(iunit, fname)
189 write (errmsg, '(3a)') &
190 "Error occurred while reading file '", trim(adjustl(fname)), "'"
191 call sim_uniterrors%store(errmsg)
192 !
193 ! -- terminate the simulation
194 if (lterminate) then
195 call ustop()
196 end if
197
198 end subroutine store_error_unit
199
200 !> @brief Store the erroring file name
201 !!
202 !! Subroutine to store the file name issuing an error. By default,
203 !! the simulation is terminated when this subroutine is called
204 !!
205 !! @hidecallergraph
206 !<
207 subroutine store_error_filename(filename, terminate)
208 ! -- dummy variables
209 character(len=*), intent(in) :: filename !< erroring file name
210 logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
211 ! -- local variables
212 logical :: lterminate
213 character(len=LINELENGTH) :: errmsg
214 !
215 ! -- process optional variables
216 if (present(terminate)) then
217 lterminate = terminate
218 else
219 lterminate = .true.
220 end if
221 !
222 ! -- store error unit
223 write (errmsg, '(3a)') &
224 "ERROR OCCURRED WHILE READING FILE '", trim(adjustl(filename)), "'"
225 call sim_uniterrors%store(errmsg)
226 !
227 ! -- terminate the simulation
228 if (lterminate) then
229 call ustop()
230 end if
231
232 end subroutine store_error_filename
233
234 !> @brief Store warning message
235 !!
236 !! Subroutine to store a warning message for printing at the end of
237 !! the simulation.
238 !!
239 !<
240 subroutine store_warning(msg, substring)
241 ! -- dummy variables
242 character(len=*), intent(in) :: msg !< warning message
243 character(len=*), intent(in), optional :: substring !< optional string that can be used
244 !! to prevent storing duplicate messages
245 !
246 ! -- store warning
247 if (present(substring)) then
248 call sim_warnings%store(msg, substring)
249 else
250 call sim_warnings%store(msg)
251 end if
252 end subroutine store_warning
253
254 !> @brief Store deprecation warning message
255 !!
256 !! Subroutine to store a warning message for deprecated variables
257 !! and printing at the end of simulation.
258 !!
259 !<
260 subroutine deprecation_warning(cblock, cvar, cver, endmsg, iunit)
261 ! -- modules
263 ! -- dummy variables
264 character(len=*), intent(in) :: cblock !< block name
265 character(len=*), intent(in) :: cvar !< variable name
266 character(len=*), intent(in) :: cver !< version when variable was deprecated
267 character(len=*), intent(in), optional :: endmsg !< optional user defined message to append
268 !! at the end of the deprecation warning
269 integer(I4B), intent(in), optional :: iunit !< optional input file unit number with
270 !! the deprecated variable
271 ! -- local variables
272 character(len=MAXCHARLEN) :: message
273 character(len=LINELENGTH) :: fname
274 !
275 ! -- build message
276 write (message, '(a)') &
277 trim(cblock)//" BLOCK VARIABLE '"//trim(cvar)//"'"
278 if (present(iunit)) then
279 call get_filename(iunit, fname)
280 write (message, '(a,1x,3a)') &
281 trim(message), "IN FILE '", trim(fname), "'"
282 end if
283 write (message, '(a)') &
284 trim(message)//' WAS DEPRECATED IN VERSION '//trim(cver)//'.'
285 if (present(endmsg)) then
286 write (message, '(a,1x,2a)') trim(message), trim(endmsg), '.'
287 end if
288 !
289 ! -- store warning
290 call sim_warnings%store(message)
291
292 end subroutine deprecation_warning
293
294 !> @brief Store note
295 !!
296 !! Subroutine to store a note for printing at the end of the simulation.
297 !!
298 !<
299 subroutine store_note(note)
300 ! -- modules
302 ! -- dummy variables
303 character(len=*), intent(in) :: note !< note
304 !
305 ! -- store note
306 call sim_notes%store(note)
307
308 end subroutine store_note
309
310 !> @brief Stop the simulation.
311 !!
312 !! Subroutine to stop the simulations with option to print message
313 !! before stopping with the active error code.
314 !!
315 !<
316 subroutine ustop(stopmess, ioutlocal)
317 ! -- dummy variables
318 character, optional, intent(in) :: stopmess * (*) !< optional message to print before
319 !! stopping the simulation
320 integer(I4B), optional, intent(in) :: ioutlocal !< optional output file to
321 !! final message to
322 !
323 ! -- print the final message
324 call print_final_message(stopmess, ioutlocal)
325 !
326 ! -- terminate with the appropriate error code
327 call pstop(ireturnerr)
328
329 end subroutine ustop
330
331 !> @brief Print the final messages
332 !!
333 !! Subroutine to print the notes, warnings, errors and the final message (if passed).
334 !! The subroutine also closes all open files.
335 !!
336 !<
337 subroutine print_final_message(stopmess, ioutlocal)
338 ! -- dummy variables
339 character, optional, intent(in) :: stopmess * (*) !< optional message to print before
340 !! stopping the simulation
341 integer(I4B), optional, intent(in) :: ioutlocal !< optional output file to
342 !! final message to
343 ! -- local variables
344 character(len=*), parameter :: fmt = '(1x,a)'
345 character(len=*), parameter :: msg = 'Stopping due to error(s)'
346 !
347 ! -- print the accumulated messages
348 if (isim_level >= vall) then
349 call sim_notes%write_all('NOTES:', 'note(s)', &
350 iunit=iout)
351 call sim_warnings%write_all('WARNING REPORT:', 'warning(s)', &
352 iunit=iout)
353 end if
354 call sim_errors%write_all('ERROR REPORT:', 'error(s)', iunit=iout)
355 call sim_uniterrors%write_all('UNIT ERROR REPORT:', &
356 'file unit error(s)', iunit=iout)
357 !
358 ! -- write a stop message, if one is passed
359 if (present(stopmess)) then
360 if (stopmess .ne. ' ') then
361 call write_message(stopmess, fmt=fmt, iunit=iout)
362 call write_message(stopmess, fmt=fmt)
363 if (present(ioutlocal)) then
364 if (ioutlocal > 0 .and. ioutlocal /= iout) then
365 write (ioutlocal, fmt) trim(stopmess)
366 close (ioutlocal)
367 end if
368 end if
369 end if
370 end if
371 !
372 ! -- write console buffer output to stdout
373 flush (istdout)
374 !
375 ! -- determine if an error condition has occurred
376 if (sim_errors%count() > 0) then
377 ireturnerr = 2
378 if (present(ioutlocal)) then
379 if (ioutlocal > 0 .and. ioutlocal /= iout) write (ioutlocal, fmt) msg
380 end if
381 end if
382 !
383 ! -- close all open files
384 call sim_closefiles()
385
386 end subroutine print_final_message
387
388 !> @brief Reset the simulation convergence flag
389 !!
390 !! Subroutine to reset the simulation convergence flag.
391 !!
392 !<
393 subroutine converge_reset()
395 isimcnvg = 1
396 end subroutine converge_reset
397
398 !> @brief Simulation convergence check
399 !!
400 !! Subroutine to check simulation convergence. If the continue option is
401 !! set the simulation convergence flag is set to True if the simulation
402 !! did not actually converge for a time step and the non-convergence counter
403 !! is incremented.
404 !!
405 !<
406 subroutine converge_check(hasConverged)
407 ! -- modules
409 ! -- dummy variables
410 logical, intent(inout) :: hasconverged !< boolean indicting if the
411 !! simulation is considered converged
412 ! -- format
413 character(len=*), parameter :: fmtfail = &
414 "(1x, 'Simulation convergence failure.', &
415 &' Simulation will terminate after output and deallocation.')"
416 !
417 ! -- Initialize hasConverged to True
418 hasconverged = .true.
419 !
420 ! -- Count number of failures
421 if (isimcnvg == 0) then
423 end if
424 !
425 ! -- Continue if 'CONTINUE' specified in simulation control file
426 if (isimcontinue == 1) then
427 if (isimcnvg == 0) then
428 isimcnvg = 1
429 end if
430 end if
431 !
432 ! -- save simulation failure message
433 if (isimcnvg == 0) then
434 call write_message('', fmt=fmtfail, iunit=iout)
435 hasconverged = .false.
436 end if
437
438 end subroutine converge_check
439
440 !> @brief Print the header and initializes messaging
441 !!
442 !! Subroutine that prints the initial message and initializes the notes,
443 !! warning messages, unit errors, and error messages.
444 !!
445 !<
446 subroutine initial_message()
447 ! -- modules
450 !
451 ! -- initialize message lists
452 call sim_errors%init()
453 call sim_uniterrors%init()
454 call sim_warnings%init()
455 call sim_notes%init()
456 !
457 ! -- Write banner to screen (unit stdout)
458 call write_listfile_header(istdout, write_kind_info=.false., &
459 write_sys_command=.false.)
460 !
461 call write_message(' MODFLOW runs in '//trim(simulation_mode)//' mode', &
462 skipafter=1)
463 !
464 if (simulation_mode == 'PARALLEL' .and. nr_procs == 1) then
465 call store_warning('Running parallel MODFLOW on only 1 process')
466 end if
467 !
468 end subroutine initial_message
469
470 !> @brief Create final message
471 !!
472 !! Subroutine that creates the appropriate final message and
473 !! terminates the program with an error message, if necessary.
474 !!
475 !<
476 subroutine final_message()
477 ! -- modules
480 ! -- formats
481 character(len=*), parameter :: fmtnocnvg = &
482 &"(1x, 'Simulation convergence failure occurred ', i0, ' time(s).')"
483 !
484 ! -- Write message if nonconvergence occurred in at least one timestep
485 if (numnoconverge > 0) then
486 write (warnmsg, fmtnocnvg) numnoconverge
487 if (isimcontinue == 0) then
488 call sim_errors%store(warnmsg)
489 else
490 call sim_warnings%store(warnmsg)
491 end if
492 end if
493 !
494 ! -- write final message
495 if (isimcnvg == 0) then
496 call print_final_message('Premature termination of simulation.', iout)
497 else
498 call print_final_message('Normal termination of simulation.', iout)
499 end if
500 !
501 ! -- If the simulation did not converge and the continue
502 ! option was not set, then set the return code to 1. The
503 ! purpose of setting the returncode this way is that the
504 ! program will terminate without a stop code if the simulation
505 ! reached the end and the continue flag was set, even if the
506 ! the simulation did not converge.
507 if (isimcnvg == 0 .and. isimcontinue == 0) then
508 ireturnerr = 1
509 end if
510 !
511 ! -- destroy messages
512 call sim_errors%deallocate()
513 call sim_uniterrors%deallocate()
514 call sim_warnings%deallocate()
515 call sim_notes%deallocate()
516 !
517 ! -- return or halt
518 if (iforcestop == 1) then
519 call pstop(ireturnerr)
520 end if
521
522 end subroutine final_message
523
524 !> @brief Close all open files
525 !!
526 !! Subroutine that closes all open files at the end of the simulation.
527 !!
528 !<
529 subroutine sim_closefiles()
530 ! -- local variables
531 integer(I4B) :: i
532 logical :: opened
533 character(len=7) :: output_file
534 !
535 ! -- close all open file units
536 do i = iustart, iunext - 1
537 !
538 ! -- determine if file unit i is open
539 inquire (unit=i, opened=opened)
540 !
541 ! -- skip file units that are no longer open
542 if (.not. opened) then
543 cycle
544 end if
545 !
546 ! -- flush the file if it can be written to
547 inquire (unit=i, write=output_file)
548 if (trim(adjustl(output_file)) == 'YES') then
549 flush (i)
550 end if
551 !
552 ! -- close file unit i
553 close (i)
554 end do
555
556 end subroutine sim_closefiles
557
558end module simmodule
This module contains simulation constants.
Definition Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition Constants.f90:45
integer(i4b), parameter iulast
maximum file unit number (this allows for 9000 open files)
Definition Constants.f90:58
integer(i4b), parameter iustart
starting file unit number
Definition Constants.f90:57
@ osundef
unknown operating system
@ oswin
Windows operating system.
integer(i4b), parameter maxcharlen
maximum length of char string
Definition Constants.f90:47
@ vsummary
write summary output
@ vdebug
write debug output
@ vall
write all simulation notes and warnings
real(dp), parameter done
real constant 1
Definition Constants.f90:76
integer(i4b) function, public get_os()
Get operating system.
Definition defmacro.F90:17
subroutine pstop(status, message)
Stop the program, optionally specifying an error status code.
Definition ErrorUtil.f90:24
This module defines variable data types.
Definition kind.f90:8
Store and issue logging messages to output units.
Definition Message.f90:2
subroutine, public write_message(text, iunit, fmt, skipbefore, skipafter, advance)
Write a message to an output unit.
Definition Message.f90:212
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public ustop(stopmess, ioutlocal)
Stop the simulation.
Definition Sim.f90:317
subroutine, public store_warning(msg, substring)
Store warning message.
Definition Sim.f90:241
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
subroutine, public converge_check(hasconverged)
Simulation convergence check.
Definition Sim.f90:407
subroutine, public maxerrors(imax)
Set the maximum number of errors to be stored.
Definition Sim.f90:86
type(messagestype) sim_uniterrors
Definition Sim.f90:45
subroutine, public converge_reset()
Reset the simulation convergence flag.
Definition Sim.f90:394
subroutine, public initial_message()
Print the header and initializes messaging.
Definition Sim.f90:447
integer(i4b) function, public count_errors()
Return number of errors.
Definition Sim.f90:60
subroutine sim_closefiles()
Close all open files.
Definition Sim.f90:530
integer(i4b) function, public count_notes()
Return the number of notes stored.
Definition Sim.f90:79
integer(i4b) function, public count_warnings()
Return number of warnings.
Definition Sim.f90:72
subroutine, public final_message()
Create final message.
Definition Sim.f90:477
subroutine, public deprecation_warning(cblock, cvar, cver, endmsg, iunit)
Store deprecation warning message.
Definition Sim.f90:261
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition Sim.f90:208
subroutine, public store_error_unit(iunit, terminate)
Store the file unit number.
Definition Sim.f90:172
subroutine get_filename(iunit, fname)
Get the file name.
Definition Sim.f90:126
type(messagestype) sim_warnings
Definition Sim.f90:46
subroutine print_final_message(stopmess, ioutlocal)
Print the final messages.
Definition Sim.f90:338
type(messagestype) sim_notes
Definition Sim.f90:47
type(messagestype) sim_errors
Definition Sim.f90:44
subroutine, public store_note(note)
Store note.
Definition Sim.f90:300
This module contains simulation variables.
character(len=maxcharlen) errmsg
error message string
integer(i4b) iforcestop
forced stop flag (1) forces a call to ustop(..) when the simulation has ended, (0) doesn't
integer(i4b) isimcontinue
simulation continue flag (1) to continue if isimcnvg = 0, (0) to terminate
character(len=linelength) simulation_mode
integer(i4b) nr_procs
integer(i4b) isim_level
simulation output level
integer(i4b) ireturnerr
return code for program (0) successful, (1) non-convergence, (2) error
character(len=maxcharlen) warnmsg
warning message string
integer(i4b) numnoconverge
number of times the simulation did not converge
integer(i4b) iout
file unit number for simulation output
integer(i4b) iunext
next file unit number to assign
integer(i4b) istdout
unit number for stdout
integer(i4b) isimcnvg
simulation convergence flag (1) if all objects have converged, (0) otherwise
This module contains version information.
Definition version.f90:7
subroutine write_listfile_header(iout, cmodel_type, write_sys_command, write_kind_info)
Write program header.
Definition version.f90:103
Container for related messages.
Definition Message.f90:21