MODFLOW 6  version 6.9.0.dev0
USGS Modular Hydrologic Model
simmodule Module Reference

This module contains simulation methods. More...

Functions/Subroutines

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

Variables

type(messagestypesim_errors
 
type(messagestypesim_uniterrors
 
type(messagestypesim_warnings
 
type(messagestypesim_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 402 of file Sim.f90.

403  ! -- modules
405  ! -- dummy variables
406  logical, intent(inout) :: hasConverged !< boolean indicting if the
407  !! simulation is considered converged
408  ! -- format
409  character(len=*), parameter :: fmtfail = &
410  "(1x, 'Simulation convergence failure.', &
411  &' Simulation will terminate after output and deallocation.')"
412  !
413  ! -- Initialize hasConverged to True
414  hasconverged = .true.
415  !
416  ! -- Count number of failures
417  if (isimcnvg == 0) then
419  end if
420  !
421  ! -- Continue if 'CONTINUE' specified in simulation control file
422  if (isimcontinue == 1) then
423  if (isimcnvg == 0) then
424  isimcnvg = 1
425  end if
426  end if
427  !
428  ! -- save simulation failure message
429  if (isimcnvg == 0) then
430  call write_message('', fmt=fmtfail, iunit=iout)
431  hasconverged = .false.
432  end if
433 
This module contains simulation variables.
Definition: SimVariables.f90:9
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) 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 389 of file Sim.f90.

390  use simvariablesmodule, only: isimcnvg
391  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 58 of file Sim.f90.

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

◆ count_notes()

integer(i4b) function, public simmodule::count_notes

Definition at line 77 of file Sim.f90.

78  integer(I4B) :: ncount
79  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 70 of file Sim.f90.

71  integer(I4B) :: ncount
72  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 256 of file Sim.f90.

257  ! -- modules
259  ! -- dummy variables
260  character(len=*), intent(in) :: cblock !< block name
261  character(len=*), intent(in) :: cvar !< variable name
262  character(len=*), intent(in) :: cver !< version when variable was deprecated
263  character(len=*), intent(in), optional :: endmsg !< optional user defined message to append
264  !! at the end of the deprecation warning
265  integer(I4B), intent(in), optional :: iunit !< optional input file unit number with
266  !! the deprecated variable
267  ! -- local variables
268  character(len=MAXCHARLEN) :: message
269  character(len=LINELENGTH) :: fname
270  !
271  ! -- build message
272  write (message, '(a)') &
273  trim(cblock)//" BLOCK VARIABLE '"//trim(cvar)//"'"
274  if (present(iunit)) then
275  call get_filename(iunit, fname)
276  write (message, '(a,1x,3a)') &
277  trim(message), "IN FILE '", trim(fname), "'"
278  end if
279  write (message, '(a)') &
280  trim(message)//' WAS DEPRECATED IN VERSION '//trim(cver)//'.'
281  if (present(endmsg)) then
282  write (message, '(a,1x,2a)') trim(message), trim(endmsg), '.'
283  end if
284  !
285  ! -- store warning
286  call sim_warnings%store(message)
287 
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 472 of file Sim.f90.

473  ! -- modules
476  ! -- formats
477  character(len=*), parameter :: fmtnocnvg = &
478  &"(1x, 'Simulation convergence failure occurred ', i0, ' time(s).')"
479  !
480  ! -- Write message if nonconvergence occurred in at least one timestep
481  if (numnoconverge > 0) then
482  write (warnmsg, fmtnocnvg) numnoconverge
483  if (isimcontinue == 0) then
484  call sim_errors%store(warnmsg)
485  else
486  call sim_warnings%store(warnmsg)
487  end if
488  end if
489  !
490  ! -- write final message
491  if (isimcnvg == 0) then
492  call print_final_message('Premature termination of simulation.', iout)
493  else
494  call print_final_message('Normal termination of simulation.', iout)
495  end if
496  !
497  ! -- If the simulation did not converge and the continue
498  ! option was not set, then set the return code to 1. The
499  ! purpose of setting the returncode this way is that the
500  ! program will terminate without a stop code if the simulation
501  ! reached the end and the continue flag was set, even if the
502  ! the simulation did not converge.
503  if (isimcnvg == 0 .and. isimcontinue == 0) then
504  ireturnerr = 1
505  end if
506  !
507  ! -- destroy messages
508  call sim_errors%deallocate()
509  call sim_uniterrors%deallocate()
510  call sim_warnings%deallocate()
511  call sim_notes%deallocate()
512  !
513  ! -- return or halt
514  if (iforcestop == 1) then
515  call pstop(ireturnerr)
516  end if
517 
integer(i4b) ireturnerr
return code for program (0) successful, (1) non-convergence, (2) error
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 123 of file Sim.f90.

124  ! -- dummy variables
125  integer(I4B), intent(in) :: iunit !< open file unit number
126  character(len=*), intent(inout) :: fname !< file name attached to the open file unit number
127  ! -- local variables
128  integer(I4B) :: ipos
129  integer(I4B) :: ios
130  integer(I4B) :: ilen
131  !
132  ! -- get file name from unit number
133  ipos = 0
134  inquire (unit=iunit, name=fname)
135  !
136  ! -- determine the operating system
137  ios = get_os()
138  !
139  ! -- extract filename from full path, if present
140  ! forward slash on linux, unix, and osx
141  if (ios /= oswin) then
142  ipos = index(fname, '/', back=.true.)
143  end if
144  !
145  ! -- check for backslash on windows or undefined os and
146  ! forward slashes were not found
147  if (ios == oswin .or. ios == osundef) then
148  if (ipos < 1) then
149  ipos = index(fname, '\', back=.true.)
150  end if
151  end if
152  !
153  ! -- exclude the path from the file name
154  if (ipos > 0) then
155  ilen = len_trim(fname)
156  write (fname, '(a)') fname(ipos + 1:ilen)//' '
157  end if
158 
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 442 of file Sim.f90.

443  ! -- modules
446  !
447  ! -- initialize message lists
448  call sim_errors%init()
449  call sim_uniterrors%init()
450  call sim_warnings%init()
451  call sim_notes%init()
452  !
453  ! -- Write banner to screen (unit stdout)
454  call write_listfile_header(istdout, write_kind_info=.false., &
455  write_sys_command=.false.)
456  !
457  call write_message(' MODFLOW runs in '//trim(simulation_mode)//' mode', &
458  skipafter=1)
459  !
460  if (simulation_mode == 'PARALLEL' .and. nr_procs == 1) then
461  call store_warning('Running parallel MODFLOW on only 1 process')
462  end if
463  !
character(len=linelength) simulation_mode
integer(i4b) nr_procs
This module contains version information.
Definition: version.f90:7
subroutine write_listfile_header(iout, cmodel_type, write_sys_command, write_kind_info)
@ brief 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 84 of file Sim.f90.

85  integer(I4B), intent(in) :: imax !< maximum number of error messages that will be stored
86  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 333 of file Sim.f90.

334  ! -- dummy variables
335  character, optional, intent(in) :: stopmess * (*) !< optional message to print before
336  !! stopping the simulation
337  integer(I4B), optional, intent(in) :: ioutlocal !< optional output file to
338  !! final message to
339  ! -- local variables
340  character(len=*), parameter :: fmt = '(1x,a)'
341  character(len=*), parameter :: msg = 'Stopping due to error(s)'
342  !
343  ! -- print the accumulated messages
344  if (isim_level >= vall) then
345  call sim_notes%write_all('NOTES:', 'note(s)', &
346  iunit=iout)
347  call sim_warnings%write_all('WARNING REPORT:', 'warning(s)', &
348  iunit=iout)
349  end if
350  call sim_errors%write_all('ERROR REPORT:', 'error(s)', iunit=iout)
351  call sim_uniterrors%write_all('UNIT ERROR REPORT:', &
352  'file unit error(s)', iunit=iout)
353  !
354  ! -- write a stop message, if one is passed
355  if (present(stopmess)) then
356  if (stopmess .ne. ' ') then
357  call write_message(stopmess, fmt=fmt, iunit=iout)
358  call write_message(stopmess, fmt=fmt)
359  if (present(ioutlocal)) then
360  if (ioutlocal > 0 .and. ioutlocal /= iout) then
361  write (ioutlocal, fmt) trim(stopmess)
362  close (ioutlocal)
363  end if
364  end if
365  end if
366  end if
367  !
368  ! -- write console buffer output to stdout
369  flush (istdout)
370  !
371  ! -- determine if an error condition has occurred
372  if (sim_errors%count() > 0) then
373  ireturnerr = 2
374  if (present(ioutlocal)) then
375  if (ioutlocal > 0 .and. ioutlocal /= iout) write (ioutlocal, fmt) msg
376  end if
377  end if
378  !
379  ! -- close all open files
380  call sim_closefiles()
381 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sim_closefiles()

subroutine simmodule::sim_closefiles

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

Definition at line 525 of file Sim.f90.

526  ! -- local variables
527  integer(I4B) :: i
528  logical :: opened
529  character(len=7) :: output_file
530  !
531  ! -- close all open file units
532  do i = iustart, iunext - 1
533  !
534  ! -- determine if file unit i is open
535  inquire (unit=i, opened=opened)
536  !
537  ! -- skip file units that are no longer open
538  if (.not. opened) then
539  cycle
540  end if
541  !
542  ! -- flush the file if it can be written to
543  inquire (unit=i, write=output_file)
544  if (trim(adjustl(output_file)) == 'YES') then
545  flush (i)
546  end if
547  !
548  ! -- close file unit i
549  close (i)
550  end do
551 
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 91 of file Sim.f90.

92  ! -- dummy variable
93  character(len=*), intent(in) :: msg !< error message
94  logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
95  ! -- local variables
96  logical :: lterminate
97  !
98  ! -- process optional variables
99  if (present(terminate)) then
100  lterminate = terminate
101  else
102  lterminate = .false.
103  end if
104  !
105  ! -- store error
106  call sim_errors%store(msg)
107  !
108  ! -- terminate the simulation
109  if (lterminate) then
110  call ustop()
111  end if
112 
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 203 of file Sim.f90.

204  ! -- dummy variables
205  character(len=*), intent(in) :: filename !< erroring file name
206  logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
207  ! -- local variables
208  logical :: lterminate
209  character(len=LINELENGTH) :: errmsg
210  !
211  ! -- process optional variables
212  if (present(terminate)) then
213  lterminate = terminate
214  else
215  lterminate = .true.
216  end if
217  !
218  ! -- store error unit
219  write (errmsg, '(3a)') &
220  "ERROR OCCURRED WHILE READING FILE '", trim(adjustl(filename)), "'"
221  call sim_uniterrors%store(errmsg)
222  !
223  ! -- terminate the simulation
224  if (lterminate) then
225  call ustop()
226  end if
227 
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 168 of file Sim.f90.

169  ! -- dummy variables
170  integer(I4B), intent(in) :: iunit !< open file unit number
171  logical, optional, intent(in) :: terminate !< boolean indicating if the simulation should be terminated
172  ! -- local variables
173  logical :: lterminate
174  character(len=LINELENGTH) :: fname
175  character(len=LINELENGTH) :: errmsg
176  !
177  ! -- process optional variables
178  if (present(terminate)) then
179  lterminate = terminate
180  else
181  lterminate = .true.
182  end if
183  !
184  ! -- store error unit
185  call get_filename(iunit, fname)
186  write (errmsg, '(3a)') &
187  "Error occurred while reading file '", trim(adjustl(fname)), "'"
188  call sim_uniterrors%store(errmsg)
189  !
190  ! -- terminate the simulation
191  if (lterminate) then
192  call ustop()
193  end if
194 
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 295 of file Sim.f90.

296  ! -- modules
298  ! -- dummy variables
299  character(len=*), intent(in) :: note !< note
300  !
301  ! -- store note
302  call sim_notes%store(note)
303 

◆ 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 236 of file Sim.f90.

237  ! -- dummy variables
238  character(len=*), intent(in) :: msg !< warning message
239  character(len=*), intent(in), optional :: substring !< optional string that can be used
240  !! to prevent storing duplicate messages
241  !
242  ! -- store warning
243  if (present(substring)) then
244  call sim_warnings%store(msg, substring)
245  else
246  call sim_warnings%store(msg)
247  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 312 of file Sim.f90.

313  ! -- dummy variables
314  character, optional, intent(in) :: stopmess * (*) !< optional message to print before
315  !! stopping the simulation
316  integer(I4B), optional, intent(in) :: ioutlocal !< optional output file to
317  !! final message to
318  !
319  ! -- print the final message
320  call print_final_message(stopmess, ioutlocal)
321  !
322  ! -- terminate with the appropriate error code
323  call pstop(ireturnerr)
324 
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