MODFLOW 6  version 6.6.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 401 of file Sim.f90.

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

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

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

472  ! -- modules
475  ! -- formats
476  character(len=*), parameter :: fmtnocnvg = &
477  &"(1x, 'Simulation convergence failure occurred ', i0, ' time(s).')"
478  !
479  ! -- Write message if nonconvergence occurred in at least one timestep
480  if (numnoconverge > 0) then
481  write (warnmsg, fmtnocnvg) numnoconverge
482  if (isimcontinue == 0) then
483  call sim_errors%store(warnmsg)
484  else
485  call sim_warnings%store(warnmsg)
486  end if
487  end if
488  !
489  ! -- write final message
490  if (isimcnvg == 0) then
491  call print_final_message('Premature termination of simulation.', iout)
492  else
493  call print_final_message('Normal termination of simulation.', iout)
494  end if
495  !
496  ! -- If the simulation did not converge and the continue
497  ! option was not set, then set the return code to 1. The
498  ! purpose of setting the returncode this way is that the
499  ! program will terminate without a stop code if the simulation
500  ! reached the end and the continue flag was set, even if the
501  ! the simulation did not converge.
502  if (isimcnvg == 0 .and. isimcontinue == 0) then
503  ireturnerr = 1
504  end if
505  !
506  ! -- destroy messages
507  call sim_errors%deallocate()
508  call sim_uniterrors%deallocate()
509  call sim_warnings%deallocate()
510  call sim_notes%deallocate()
511  !
512  ! -- return or halt
513  if (iforcestop == 1) then
514  call pstop(ireturnerr)
515  end if
516 
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  inquire (unit=iunit, name=fname)
134  !
135  ! -- determine the operating system
136  ios = get_os()
137  !
138  ! -- extract filename from full path, if present
139  ! forward slash on linux, unix, and osx
140  if (ios /= oswin) then
141  ipos = index(fname, '/', back=.true.)
142  end if
143  !
144  ! -- check for backslash on windows or undefined os and
145  ! forward slashes were not found
146  if (ios == oswin .or. ios == osundef) then
147  if (ipos < 1) then
148  ipos = index(fname, '\', back=.true.)
149  end if
150  end if
151  !
152  ! -- exclude the path from the file name
153  if (ipos > 0) then
154  ilen = len_trim(fname)
155  write (fname, '(a)') fname(ipos + 1:ilen)//' '
156  end if
157 
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 441 of file Sim.f90.

442  ! -- modules
445  !
446  ! -- initialize message lists
447  call sim_errors%init()
448  call sim_uniterrors%init()
449  call sim_warnings%init()
450  call sim_notes%init()
451  !
452  ! -- Write banner to screen (unit stdout)
453  call write_listfile_header(istdout, write_kind_info=.false., &
454  write_sys_command=.false.)
455  !
456  call write_message(' MODFLOW runs in '//trim(simulation_mode)//' mode', &
457  skipafter=1)
458  !
459  if (simulation_mode == 'PARALLEL' .and. nr_procs == 1) then
460  call store_warning('Running parallel MODFLOW on only 1 process')
461  end if
462  !
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:98
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 332 of file Sim.f90.

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

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

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

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

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

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

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

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