MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
SourceLoad.F90
Go to the documentation of this file.
1 !> @brief This module contains the SourceLoadModule
2 !!
3 !! This module contains the routines needed to generate
4 !! a loader object for an input source and routines
5 !! that distribute processing to a particular source.
6 !!
7 !<
9 
10  use kindmodule, only: dp, i4b, lgp
11  use simvariablesmodule, only: errmsg, iout
17 
18  implicit none
19  private
20  public :: create_input_loader
21  public :: open_source_file
23  public :: remote_model_ndim
24  public :: export_cr, export_da
26  public :: nc_close
27  public :: netcdf_context
28 
29 contains
30 
31  !> @brief factory function to create and setup model package static loader
32  !<
33  function create_input_loader(component_type, subcomponent_type, &
34  component_name, subcomponent_name, input_type, &
35  input_fname, component_fname, nc_vars) &
36  result(loader)
39  character(len=*), intent(in) :: component_type
40  character(len=*), intent(in) :: subcomponent_type
41  character(len=*), intent(in) :: component_name
42  character(len=*), intent(in) :: subcomponent_name
43  character(len=*), intent(in) :: input_type
44  character(len=*), intent(in) :: input_fname
45  character(len=*), intent(in) :: component_fname
46  type(ncfilevarstype), pointer, optional, intent(in) :: nc_vars
47  class(staticpkgloadbasetype), pointer :: loader
48  type(modflowinputtype) :: mf6_input
49  character(len=LENPACKAGENAME) :: source_type
50  character(len=LENPACKAGENAME) :: sc_name
51 
52  ! set subcomponent name
53  sc_name = idm_subcomponent_name(component_type, subcomponent_type, &
54  subcomponent_name)
55  ! create description of input
56  mf6_input = getmodflowinput(input_type, component_type, subcomponent_type, &
57  component_name, sc_name, input_fname)
58  ! set package source
59  source_type = package_source_type(input_fname)
60 
61  ! set source loader for model package
62  loader => package_loader(source_type)
63 
64  ! initialize loader
65  call loader%init(mf6_input, component_name, component_fname, input_fname)
66 
67  ! initialize loader netcdf variables data structure
68  if (present(nc_vars)) then
69  call nc_vars%create_varlists(component_name, sc_name, loader%nc_vars)
70  else
71  call loader%nc_vars%init(component_name)
72  end if
73  end function create_input_loader
74 
75  !> @brief allocate source model package static loader
76  !<
77  function package_loader(source_type) result(loader)
80  character(len=*), intent(inout) :: source_type
81  class(mf6filestaticpkgloadtype), pointer :: mf6file_loader
82  class(staticpkgloadbasetype), pointer :: loader
83 
84  ! initialize
85  nullify (loader)
86 
87  ! allocate derived object
88  select case (source_type)
89  case ('MF6FILE')
90  allocate (mf6file_loader)
91  loader => mf6file_loader
92  allocate (loader%nc_vars)
93  case default
94  write (errmsg, '(a)') &
95  'Simulation package input source type "'//trim(source_type)// &
96  '" not currently supported.'
97  call store_error(errmsg, .true.)
98  end select
99  end function package_loader
100 
101  function open_source_file(pkgtype, filename, modelfname, iout) result(fd)
103  use idmmf6filemodule, only: open_mf6file
104  character(len=*), intent(in) :: pkgtype
105  character(len=*), intent(in) :: filename
106  character(len=*), intent(in) :: modelfname
107  integer(I4B), intent(in) :: iout
108  integer(I4B) :: fd
109  character(len=LENPACKAGENAME) :: source_type
110 
111  ! initialize
112  fd = 0
113 
114  ! set source type
115  source_type = package_source_type(filename)
116  !
117  select case (source_type)
118  case ('MF6FILE')
119  fd = open_mf6file(pkgtype, filename, modelfname, iout)
120  case default
121  end select
122  end function open_source_file
123 
124  subroutine load_modelnam(mtype, mfname, mname, iout)
125  use simvariablesmodule, only: simfile
127  use idmmf6filemodule, only: input_load
128  character(len=*), intent(in) :: mtype
129  character(len=*), intent(in) :: mfname
130  character(len=*), intent(in) :: mname
131  integer(I4B), intent(in) :: iout
132  type(modflowinputtype) :: mf6_input
133  character(len=LENPACKAGENAME) :: source_type
134 
135  ! set source type
136  source_type = package_source_type(mfname)
137 
138  ! create description of input
139  mf6_input = getmodflowinput(mtype, idm_component_type(mtype), 'NAM', &
140  mname, 'NAM', mfname)
141  select case (source_type)
142  case ('MF6FILE')
143  call input_load(mfname, mf6_input, simfile, iout)
144  case default
145  end select
146  end subroutine load_modelnam
147 
148  subroutine load_simnam()
149  use simvariablesmodule, only: simfile, iout
151  use messagemodule, only: write_message
152  use idmmf6filemodule, only: input_load
154  type(modflowinputtype) :: mf6_input, hpc_input
155  character(len=LINELENGTH) :: hpc6_filename
156  character(len=LINELENGTH) :: line
157  logical(LGP) :: lexist
158 
159  ! load mfsim.nam if it exists
160  inquire (file=trim(adjustl(simfile)), exist=lexist)
161 
162  if (lexist) then
163  ! write name of namfile to stdout
164  write (line, '(2(1x,a))') 'Using Simulation name file:', &
165  trim(adjustl(simfile))
166  call write_message(line, skipafter=1)
167  ! create description of input
168  mf6_input = getmodflowinput('NAM6', 'SIM', 'NAM', 'SIM', 'NAM', simfile)
169  ! open namfile and load to input context
170  call input_load(simfile, mf6_input, simfile, iout)
171  ! load optional HPC configuration file
172  if (filein_fname(hpc6_filename, 'HPC6_FILENAME', mf6_input%mempath, &
173  simfile)) then
174  hpc_input = getmodflowinput('HPC6', 'UTL', 'HPC', 'UTL', 'HPC')
175  call input_load(hpc6_filename, hpc_input, simfile, iout)
176  end if
177  end if
178  end subroutine load_simnam
179 
180  subroutine load_simtdis()
181  use simvariablesmodule, only: simfile, iout
186  use idmmf6filemodule, only: input_load
187  character(len=LENMEMPATH) :: input_mempath
188  type(modflowinputtype) :: mf6_input
189  character(len=LENPACKAGENAME) :: source_type
190  character(len=:), pointer :: tdis6
191  logical(LGP) :: lexist
192 
193  ! set input memory path
194  input_mempath = create_mem_path('SIM', 'NAM', idm_context)
195 
196  ! set pointers to input context timing params
197  call mem_setptr(tdis6, 'TDIS6', input_mempath)
198 
199  ! create timing
200  if (tdis6 /= '') then
201  ! set source type
202  source_type = package_source_type(tdis6)
203  select case (source_type)
204  case ('MF6FILE')
205  inquire (file=trim(adjustl(tdis6)), exist=lexist)
206  if (lexist) then
207  ! create description of input
208  mf6_input = getmodflowinput('TDIS6', 'SIM', 'TDIS', &
209  'SIM', 'TDIS', simfile)
210  ! open namfile and load to input context
211  call input_load(tdis6, mf6_input, simfile, iout)
212  else
213  write (errmsg, '(a)') &
214  'Simulation TIMING input file "'//trim(tdis6)// &
215  '" does not exist.'
216  call store_error(errmsg)
217  call store_error_filename(simfile)
218  end if
219  case default
220  end select
221  end if
222  end subroutine load_simtdis
223 
224  function remote_model_ndim(mtype, mfname) result(ncelldim)
226  use constantsmodule, only: linelength
229  character(len=*), intent(in) :: mtype
230  character(len=*), intent(in) :: mfname
231  integer(I4B) :: ncelldim
232  character(len=LENPACKAGENAME) :: source_type
233  type(blockparsertype) :: parser
234  integer(I4B) :: ierr, inunit
235  logical(LGP) :: isfound, endofblock
236  character(len=LINELENGTH) :: ptype
237 
238  ! initialize
239  ncelldim = 0
240 
241  ! set source type
242  source_type = package_source_type(mfname)
243  select case (source_type)
244  case ('MF6FILE')
245  ! open name file
246  inunit = getunit()
247  call openfile(inunit, 0, trim(adjustl(mfname)), mtype, &
248  'FORMATTED', 'SEQUENTIAL', 'OLD')
249  ! initialize parser
250  call parser%Initialize(inunit, 0)
251  ! get options block
252  call parser%GetBlock('OPTIONS', isfound, ierr, &
253  supportopenclose=.true., blockrequired=.false.)
254  ! iterate through options
255  if (isfound) then
256  do
257  call parser%GetNextLine(endofblock)
258  if (endofblock) exit
259  end do
260  end if
261  ! get packages block
262  call parser%GetBlock('PACKAGES', isfound, ierr, &
263  supportopenclose=.true., blockrequired=.true.)
264  if (isfound) then
265  ! read through packages
266  do
267  call parser%GetNextLine(endofblock)
268  if (endofblock) exit
269  call parser%GetStringCaps(ptype)
270 
271  select case (ptype)
272  case ('DIS6')
273  ncelldim = 3
274  exit
275  case ('DIS2D6')
276  ncelldim = 2
277  exit
278  case ('DISV6')
279  ncelldim = 2
280  exit
281  case ('DISU6')
282  ncelldim = 1
283  exit
284  case default
285  write (errmsg, '(a)') &
286  'Unknown discretization type "'//trim(ptype)// &
287  '" not currently supported.'
288  call store_error(errmsg, .true.)
289  end select
290  end do
291  end if
292 
293  call parser%clear()
294  case default
295  end select
296  end function remote_model_ndim
297 
298  !> @brief create model exports list
299  !<
300  subroutine export_cr()
302 #if defined(__WITH_NETCDF__)
304 #endif
306  ! are netcdf exports elected
307  if (nc_export_active()) then
308 #if defined(__WITH_NETCDF__)
309  call nc_export_create()
310 #else
311  write (errmsg, '(a)') &
312  'Model namefile NETCDF_STUCTURED or NETCDF_MESH2D option configured &
313  &but NetCDF libraries are not available.'
314  call store_error(errmsg, .true.)
315 #endif
316  end if
317  end subroutine export_cr
318 
319  !> @brief model exports post prepare step actions
320  !<
321  subroutine export_post_prepare()
324  end subroutine export_post_prepare
325 
326  !> @brief model exports post step actions
327  !<
328  subroutine export_post_step()
331  end subroutine export_post_step
332 
333  !> @brief deallocate model export objects and list
334  !<
335  subroutine export_da()
337  call modelexports_destroy()
338  end subroutine export_da
339 
340  !> @brief close an open netcdf file
341  !<
342  subroutine nc_close(ncid, nc_fname)
343 #if defined(__WITH_NETCDF__)
344  use netcdfcommonmodule, only: nc_fclose
345 #endif
346  integer(I4B), intent(in) :: ncid
347  character(len=*), intent(in) :: nc_fname
348  if (ncid > 0) then
349 #if defined(__WITH_NETCDF__)
350  call nc_fclose(ncid, nc_fname)
351 #endif
352  end if
353  end subroutine nc_close
354 
355  !> @brief create model netcdf context
356  !<
357  function netcdf_context(modeltype, component_type, modelname, &
358  modelfname, iout) result(nc_vars)
362 #if defined(__WITH_NETCDF__)
364 #endif
365  character(len=*), intent(in) :: modeltype
366  character(len=*), intent(in) :: component_type
367  character(len=*), intent(in) :: modelname
368  character(len=*), intent(in) :: modelfname
369  integer(I4B), intent(in) :: iout
370  type(ncfilevarstype), pointer :: nc_vars
371  character(len=LENMEMPATH) :: input_mempath
372  character(len=LINELENGTH) :: nc_fname
373  integer(I4B) :: ncid
374 
375  ! set input memory path
376  input_mempath = create_mem_path(modelname, 'NAM', idm_context)
377 
378  ! allocate context object
379  allocate (nc_vars)
380 
381  ! check if optional netcdf input file was provided
382  if (filein_fname(nc_fname, 'NETCDF_FNAME', input_mempath, modelfname)) then
383 #if defined(__WITH_NETCDF__)
384  ! open nc input file
385  ncid = open_ncfile(nc_fname, iout)
386  ! read the file and build the context
387  call create_netcdf_context(modeltype, modelname, modelfname, &
388  nc_vars, nc_fname, ncid, iout)
389 #else
390  write (errmsg, '(a)') &
391  'Cannot load model packages. NetCDF &
392  &keyword specified in input file but &
393  &NetCDF libraries are not available.'
394  call store_error(errmsg)
395  call store_error_filename(modelfname)
396 #endif
397  else
398  ncid = 0
399  call nc_vars%init(modelname, '', ncid, '')
400  end if
401  end function netcdf_context
402 
403 end module sourceloadmodule
This module contains block parser methods.
Definition: BlockParser.f90:7
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 lenmodelname
maximum length of the model name
Definition: Constants.f90:22
integer(i4b), parameter lenpackagename
maximum length of the package name
Definition: Constants.f90:23
integer(i4b), parameter lenpackagetype
maximum length of a package type (DIS6, SFR6, CSUB6, etc.)
Definition: Constants.f90:38
integer(i4b), parameter lenftype
maximum length of a package type (DIS, WEL, OC, etc.)
Definition: Constants.f90:39
integer(i4b), parameter lenmempath
maximum length of the memory path
Definition: Constants.f90:27
This module contains the IdmMf6FileModule.
Definition: IdmMf6File.f90:10
integer(i4b) function, public open_mf6file(filetype, filename, component_fname, iout)
open a model package files
Definition: IdmMf6File.f90:330
subroutine, public input_load(filename, mf6_input, component_filename, iout, nc_vars)
input load for traditional mf6 simulation static input file
Definition: IdmMf6File.f90:59
This module contains the InputLoadTypeModule.
integer(i4b) function, public getunit()
Get a free unit number.
subroutine, public openfile(iu, iout, fname, ftype, fmtarg_opt, accarg_opt, filstat_opt, mode_opt)
Open a file.
Definition: InputOutput.f90:30
This module defines variable data types.
Definition: kind.f90:8
character(len=lenmempath) function create_mem_path(component, subcomponent, context)
returns the path to the memory object
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:210
This module contains the ModelExportModule.
Definition: ModelExport.f90:8
subroutine, public modelexports_post_prepare()
export model list post prepare step
subroutine, public modelexports_destroy()
destroy export model list
subroutine, public modelexports_post_step()
export model list post step
subroutine, public modelexports_create(iout)
create export container variable for all local models
Definition: ModelExport.f90:77
logical(lgp) function, public nc_export_active()
is netcdf export configured for any model
Definition: ModelExport.f90:60
This module contains the ModflowInputModule.
Definition: ModflowInput.f90:9
type(modflowinputtype) function, public getmodflowinput(pkgtype, component_type, subcomponent_type, component_name, subcomponent_name, filename)
function to return ModflowInputType
This module contains the NCContextBuildModule.
integer(i4b) function, public open_ncfile(nc_fname, iout)
open netcdf file
subroutine, public create_netcdf_context(modeltype, modelname, input_name, nc_vars, nc_fname, ncid, iout)
create internal description of modflow6 input variables in netcdf file
This module contains the NCExportCreateModule.
subroutine, public nc_export_create()
initialize netcdf model export type
This module contains the NCFileVarsModule.
Definition: NCFileVars.f90:7
This module contains the NetCDFCommonModule.
Definition: NetCDFCommon.f90:6
subroutine, public nc_fclose(ncid, nc_fname)
Close netcdf file.
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition: Sim.f90:203
This module contains simulation variables.
Definition: SimVariables.f90:9
character(len=maxcharlen) errmsg
error message string
character(len=linelength) idm_context
integer(i4b) iout
file unit number for simulation output
character(len=linelength) simfile
simulation name file
This module contains the SourceCommonModule.
Definition: SourceCommon.f90:7
character(len=lenpackagename) function, public package_source_type(sourcename)
source identifier from model namfile FNAME array
character(len=lencomponentname) function, public idm_component_type(component)
component from package or model type
character(len=lenpackagename) function, public idm_subcomponent_name(component_type, subcomponent_type, sc_name)
model package subcomponent name
logical(lgp) function, public filein_fname(filename, tagname, input_mempath, input_fname)
enforce and set a single input filename provided via FILEIN keyword
This module contains the SourceLoadModule.
Definition: SourceLoad.F90:8
subroutine, public export_da()
deallocate model export objects and list
Definition: SourceLoad.F90:336
subroutine, public load_simnam()
Definition: SourceLoad.F90:149
integer(i4b) function, public open_source_file(pkgtype, filename, modelfname, iout)
Definition: SourceLoad.F90:102
subroutine, public load_simtdis()
Definition: SourceLoad.F90:181
class(staticpkgloadbasetype) function, pointer package_loader(source_type)
allocate source model package static loader
Definition: SourceLoad.F90:78
subroutine, public export_cr()
create model exports list
Definition: SourceLoad.F90:301
type(ncfilevarstype) function, pointer, public netcdf_context(modeltype, component_type, modelname, modelfname, iout)
create model netcdf context
Definition: SourceLoad.F90:359
subroutine, public nc_close(ncid, nc_fname)
close an open netcdf file
Definition: SourceLoad.F90:343
subroutine, public export_post_prepare()
model exports post prepare step actions
Definition: SourceLoad.F90:322
integer(i4b) function, public remote_model_ndim(mtype, mfname)
Definition: SourceLoad.F90:225
subroutine, public export_post_step()
model exports post step actions
Definition: SourceLoad.F90:329
class(staticpkgloadbasetype) function, pointer, public create_input_loader(component_type, subcomponent_type, component_name, subcomponent_name, input_type, input_fname, component_fname, nc_vars)
factory function to create and setup model package static loader
Definition: SourceLoad.F90:37
subroutine, public load_modelnam(mtype, mfname, mname, iout)
Definition: SourceLoad.F90:125
Base abstract type for static input loader.
derived type for storing input definition for a file
Type describing modflow6 input variables in model NetCDF file.
Definition: NCFileVars.f90:48