MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
simmodule Module Reference

This module contains simulation methods. More...

Functions/Subroutines

integer(i4b) function, public count_errors ()
 Return number of errors.
integer(i4b) function, public count_warnings ()
 Return number of warnings.
integer(i4b) function, public count_notes ()
 Return the number of notes stored.
subroutine, public maxerrors (imax)
 Set the maximum number of errors to be stored.
subroutine, public store_error (msg, terminate)
 Store an error message.
subroutine get_filename (iunit, fname)
 Get the file name.
subroutine, public store_error_unit (iunit, terminate)
 Store the file unit number.
subroutine, public store_error_filename (filename, terminate)
 Store the erroring file name.
subroutine, public store_warning (msg, substring)
 Store warning message.
subroutine, public deprecation_warning (cblock, cvar, cver, endmsg, iunit)
 Store deprecation warning message.
subroutine, public store_note (note)
 Store note.
subroutine, public ustop (stopmess, ioutlocal)
 Stop the simulation.
subroutine print_final_message (stopmess, ioutlocal)
 Print the final messages.
subroutine, public converge_reset ()
 Reset the simulation convergence flag.
subroutine, public converge_check (hasconverged)
 Simulation convergence check.
subroutine, public initial_message ()
 Print the header and initializes messaging.
subroutine, public final_message ()
 Create final message.
subroutine sim_closefiles ()
 Close all open files.

Variables

type(messagestype) sim_errors
type(messagestype) sim_uniterrors
type(messagestype) sim_warnings
type(messagestype) sim_notes

Detailed Description

This module contains simulation methods for storing warning and error messages and notes. This module also has methods for counting warnings, errors, and notes in addition to stopping the simulation. The module does not have any dependencies on models, exchanges, or solutions in a simulation.

Function/Subroutine Documentation

◆ converge_check()

subroutine, public simmodule::converge_check ( logical, intent(inout) hasconverged)

Subroutine to check simulation convergence. If the continue option is set the simulation convergence flag is set to True if the simulation did not actually converge for a time step and the non-convergence counter is incremented.

Parameters
[in,out]hasconvergedboolean indicting if the simulation is considered converged

Definition at line 406 of file Sim.f90.

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
This module contains simulation variables.
integer(i4b) isimcontinue
simulation continue flag (1) to continue if isimcnvg = 0, (0) to terminate
integer(i4b) numnoconverge
number of times the simulation did not converge
integer(i4b) iout
file unit number for simulation output
integer(i4b) isimcnvg
simulation convergence flag (1) if all objects have converged, (0) otherwise
Here is the call graph for this function:
Here is the caller graph for this function:

◆ converge_reset()

subroutine, public simmodule::converge_reset

Subroutine to reset the simulation convergence flag.

Definition at line 393 of file Sim.f90.

395 isimcnvg = 1
Here is the caller graph for this function:

◆ count_errors()

integer(i4b) function, public simmodule::count_errors

Function to return the number of errors messages that have been stored.

Returns
ncount number of error messages stored

Definition at line 59 of file Sim.f90.

60 integer(I4B) :: ncount
61 ncount = sim_errors%count()

◆ count_notes()

integer(i4b) function, public simmodule::count_notes

Definition at line 78 of file Sim.f90.

79 integer(I4B) :: ncount
80 ncount = sim_notes%count()

◆ count_warnings()

integer(i4b) function, public simmodule::count_warnings

Function to return the number of warning messages that have been stored.

Returns
ncount number of warning messages stored

Definition at line 71 of file Sim.f90.

72 integer(I4B) :: ncount
73 ncount = sim_warnings%count()

◆ deprecation_warning()

subroutine, public simmodule::deprecation_warning ( character(len=*), intent(in) cblock,
character(len=*), intent(in) cvar,
character(len=*), intent(in) cver,
character(len=*), intent(in), optional endmsg,
integer(i4b), intent(in), optional iunit )

Subroutine to store a warning message for deprecated variables and printing at the end of simulation.

Parameters
[in]cblockblock name
[in]cvarvariable name
[in]cverversion when variable was deprecated
[in]endmsgoptional user defined message to append at the end of the deprecation warning
[in]iunitoptional input file unit number with the deprecated variable

Definition at line 260 of file Sim.f90.

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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ final_message()

subroutine, public simmodule::final_message

Subroutine that creates the appropriate final message and terminates the program with an error message, if necessary.

Definition at line 476 of file Sim.f90.

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
integer(i4b) iforcestop
forced stop flag (1) forces a call to ustop(..) when the simulation has ended, (0) doesn't
integer(i4b) ireturnerr
return code for program (0) successful, (1) non-convergence, (2) error
character(len=maxcharlen) warnmsg
warning message string
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_filename()

subroutine simmodule::get_filename ( integer(i4b), intent(in) iunit,
character(len=*), intent(inout) fname )
private

Subroutine to get the file name from the unit number for a open file. If the INQUIRE function returns the full path (for example, the INTEL compiler) then the returned file name (fname) is limited to the filename without the path.

Parameters
[in]iunitopen file unit number
[in,out]fnamefile name attached to the open file unit number

Definition at line 125 of file Sim.f90.

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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ initial_message()

subroutine, public simmodule::initial_message

Subroutine that prints the initial message and initializes the notes, warning messages, unit errors, and error messages.

Definition at line 446 of file Sim.f90.

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 !
character(len=linelength) simulation_mode
integer(i4b) nr_procs
integer(i4b) istdout
unit number for stdout
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ maxerrors()

subroutine, public simmodule::maxerrors ( integer(i4b), intent(in) imax)
Parameters
[in]imaxmaximum number of error messages that will be stored

Definition at line 85 of file Sim.f90.

86 integer(I4B), intent(in) :: imax !< maximum number of error messages that will be stored
87 call sim_errors%set_max(imax)
Here is the caller graph for this function:

◆ print_final_message()

subroutine simmodule::print_final_message ( character, dimension (*), intent(in), optional stopmess,
integer(i4b), intent(in), optional ioutlocal )
private

Subroutine to print the notes, warnings, errors and the final message (if passed). The subroutine also closes all open files.

Parameters
[in]stopmessoptional message to print before stopping the simulation
[in]ioutlocaloptional output file to final message to

Definition at line 337 of file Sim.f90.

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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sim_closefiles()

subroutine simmodule::sim_closefiles
private

Subroutine that closes all open files at the end of the simulation.

Definition at line 529 of file Sim.f90.

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
Here is the caller graph for this function:

◆ store_error()

subroutine, public simmodule::store_error ( character(len=*), intent(in) msg,
logical, intent(in), optional terminate )
Parameters
[in]msgerror message
[in]terminateboolean indicating if the simulation should be terminated

Definition at line 93 of file Sim.f90.

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
Here is the call graph for this function:

◆ store_error_filename()

subroutine, public simmodule::store_error_filename ( character(len=*), intent(in) filename,
logical, intent(in), optional terminate )

Subroutine to store the file name issuing an error. By default, the simulation is terminated when this subroutine is called

Parameters
[in]filenameerroring file name
[in]terminateboolean indicating if the simulation should be terminated

Definition at line 207 of file Sim.f90.

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
Here is the call graph for this function:

◆ store_error_unit()

subroutine, public simmodule::store_error_unit ( integer(i4b), intent(in) iunit,
logical, intent(in), optional terminate )

Subroutine to convert the unit number for a open file to a file name and indicate that there is an error reading from the file. By default, the simulation is terminated when this subroutine is called.

Parameters
[in]iunitopen file unit number
[in]terminateboolean indicating if the simulation should be terminated

Definition at line 171 of file Sim.f90.

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
Here is the call graph for this function:

◆ store_note()

subroutine, public simmodule::store_note ( character(len=*), intent(in) note)

Subroutine to store a note for printing at the end of the simulation.

Definition at line 299 of file Sim.f90.

300 ! -- modules
302 ! -- dummy variables
303 character(len=*), intent(in) :: note !< note
304 !
305 ! -- store note
306 call sim_notes%store(note)
307

◆ store_warning()

subroutine, public simmodule::store_warning ( character(len=*), intent(in) msg,
character(len=*), intent(in), optional substring )

Subroutine to store a warning message for printing at the end of the simulation.

Parameters
[in]msgwarning message
[in]substringoptional string that can be used to prevent storing duplicate messages

Definition at line 240 of file Sim.f90.

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
Here is the caller graph for this function:

◆ ustop()

subroutine, public simmodule::ustop ( character, dimension (*), intent(in), optional stopmess,
integer(i4b), intent(in), optional ioutlocal )

Subroutine to stop the simulations with option to print message before stopping with the active error code.

Parameters
[in]stopmessoptional message to print before stopping the simulation
[in]ioutlocaloptional output file to final message to

Definition at line 316 of file Sim.f90.

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
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ sim_errors

type(messagestype) simmodule::sim_errors
private

Definition at line 44 of file Sim.f90.

44 type(MessagesType) :: sim_errors

◆ sim_notes

type(messagestype) simmodule::sim_notes
private

Definition at line 47 of file Sim.f90.

47 type(MessagesType) :: sim_notes

◆ sim_uniterrors

type(messagestype) simmodule::sim_uniterrors
private

Definition at line 45 of file Sim.f90.

45 type(MessagesType) :: sim_uniterrors

◆ sim_warnings

type(messagestype) simmodule::sim_warnings
private

Definition at line 46 of file Sim.f90.

46 type(MessagesType) :: sim_warnings