MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
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
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
29contains
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 load_scope, input_fname, component_fname, &
36 nc_vars) &
37 result(loader)
40 character(len=*), intent(in) :: component_type
41 character(len=*), intent(in) :: subcomponent_type
42 character(len=*), intent(in) :: component_name
43 character(len=*), intent(in) :: subcomponent_name
44 character(len=*), intent(in) :: input_type
45 character(len=*), intent(in) :: load_scope
46 character(len=*), intent(in) :: input_fname
47 character(len=*), intent(in) :: component_fname
48 type(ncfilevarstype), pointer, optional, intent(in) :: nc_vars
49 class(staticpkgloadbasetype), pointer :: loader
50 type(modflowinputtype) :: mf6_input
51 character(len=LENPACKAGENAME) :: source_type
52 character(len=LENPACKAGENAME) :: sc_name
53
54 ! set subcomponent name
55 sc_name = idm_subcomponent_name(component_type, subcomponent_type, &
56 subcomponent_name)
57 ! create description of input
58 mf6_input = getmodflowinput(input_type, component_type, subcomponent_type, &
59 component_name, sc_name, load_scope, &
60 input_fname)
61 ! set package source
62 source_type = package_source_type(input_fname)
63
64 ! set source loader for model package
65 loader => package_loader(source_type)
66
67 ! initialize loader
68 call loader%init(mf6_input, component_name, component_fname, input_fname)
69
70 ! initialize loader netcdf variables data structure
71 if (present(nc_vars)) then
72 call nc_vars%create_varlists(component_name, sc_name, loader%nc_vars)
73 else
74 call loader%nc_vars%init(component_name)
75 end if
76 end function create_input_loader
77
78 !> @brief allocate source model package static loader
79 !<
80 function package_loader(source_type) result(loader)
83 character(len=*), intent(inout) :: source_type
84 class(mf6filestaticpkgloadtype), pointer :: mf6file_loader
85 class(staticpkgloadbasetype), pointer :: loader
86
87 ! initialize
88 nullify (loader)
89
90 ! allocate derived object
91 select case (source_type)
92 case ('MF6FILE')
93 allocate (mf6file_loader)
94 loader => mf6file_loader
95 allocate (loader%nc_vars)
96 case default
97 write (errmsg, '(a)') &
98 'Simulation package input source type "'//trim(source_type)// &
99 '" not currently supported.'
100 call store_error(errmsg, .true.)
101 end select
102 end function package_loader
103
104 function open_source_file(pkgtype, filename, modelfname, iout) result(fd)
107 character(len=*), intent(in) :: pkgtype
108 character(len=*), intent(in) :: filename
109 character(len=*), intent(in) :: modelfname
110 integer(I4B), intent(in) :: iout
111 integer(I4B) :: fd
112 character(len=LENPACKAGENAME) :: source_type
113
114 ! initialize
115 fd = 0
116
117 ! set source type
118 source_type = package_source_type(filename)
119 !
120 select case (source_type)
121 case ('MF6FILE')
122 fd = open_mf6file(pkgtype, filename, modelfname, iout)
123 case default
124 end select
125 end function open_source_file
126
127 subroutine load_modelnam(mtype, mfname, mname, iout)
128 use simvariablesmodule, only: simfile
131 character(len=*), intent(in) :: mtype
132 character(len=*), intent(in) :: mfname
133 character(len=*), intent(in) :: mname
134 integer(I4B), intent(in) :: iout
135 type(modflowinputtype) :: mf6_input
136 character(len=LENPACKAGENAME) :: source_type
137
138 ! set source type
139 source_type = package_source_type(mfname)
140
141 ! create description of input
142 mf6_input = getmodflowinput(mtype, idm_component_type(mtype), 'NAM', &
143 mname, 'NAM', 'SIM', mfname)
144 select case (source_type)
145 case ('MF6FILE')
146 call input_load(mfname, mf6_input, simfile, iout)
147 case default
148 end select
149 end subroutine load_modelnam
150
151 subroutine load_simnam()
157 type(modflowinputtype) :: mf6_input, hpc_input
158 character(len=LINELENGTH) :: hpc6_filename
159 character(len=LINELENGTH) :: line
160 logical(LGP) :: lexist
161
162 ! load mfsim.nam if it exists
163 inquire (file=trim(adjustl(simfile)), exist=lexist)
164
165 if (lexist) then
166 ! write name of namfile to stdout
167 write (line, '(2(1x,a))') 'Using Simulation name file:', &
168 trim(adjustl(simfile))
169 call write_message(line, skipafter=1)
170 ! create description of input
171 mf6_input = getmodflowinput('NAM6', 'SIM', 'NAM', 'SIM', 'NAM', 'ROOT', &
172 simfile)
173 ! open namfile and load to input context
174 call input_load(simfile, mf6_input, simfile, iout)
175 ! load optional HPC configuration file
176 if (filein_fname(hpc6_filename, 'HPC6_FILENAME', mf6_input%mempath, &
177 simfile)) then
178 hpc_input = getmodflowinput('HPC6', 'UTL', 'HPC', 'UTL', 'HPC', 'SIM')
179 call input_load(hpc6_filename, hpc_input, simfile, iout)
180 end if
181 end if
182 end subroutine load_simnam
183
184 subroutine load_simtdis()
191 character(len=LENMEMPATH) :: input_mempath
192 type(modflowinputtype) :: mf6_input
193 character(len=LENPACKAGENAME) :: source_type
194 character(len=:), pointer :: tdis6
195 logical(LGP) :: lexist
196
197 ! set input memory path
198 input_mempath = create_mem_path('SIM', 'NAM', idm_context)
199
200 ! set pointers to input context timing params
201 call mem_setptr(tdis6, 'TDIS6', input_mempath)
202
203 ! create timing
204 if (tdis6 /= '') then
205 ! set source type
206 source_type = package_source_type(tdis6)
207 select case (source_type)
208 case ('MF6FILE')
209 inquire (file=trim(adjustl(tdis6)), exist=lexist)
210 if (lexist) then
211 ! create description of input
212 mf6_input = getmodflowinput('TDIS6', 'SIM', 'TDIS', &
213 'SIM', 'TDIS', 'SIM', simfile)
214 ! open namfile and load to input context
215 call input_load(tdis6, mf6_input, simfile, iout)
216 else
217 write (errmsg, '(a)') &
218 'Simulation TIMING input file "'//trim(tdis6)// &
219 '" does not exist.'
220 call store_error(errmsg)
222 end if
223 case default
224 end select
225 end if
226 end subroutine load_simtdis
227
228 function remote_model_ndim(mtype, mfname) result(ncelldim)
230 use constantsmodule, only: linelength
233 character(len=*), intent(in) :: mtype
234 character(len=*), intent(in) :: mfname
235 integer(I4B) :: ncelldim
236 character(len=LENPACKAGENAME) :: source_type
237 type(blockparsertype) :: parser
238 integer(I4B) :: ierr, inunit
239 logical(LGP) :: isfound, endofblock
240 character(len=LINELENGTH) :: ptype
241
242 ! initialize
243 ncelldim = 0
244
245 ! set source type
246 source_type = package_source_type(mfname)
247 select case (source_type)
248 case ('MF6FILE')
249 ! open name file
250 inunit = getunit()
251 call openfile(inunit, 0, trim(adjustl(mfname)), mtype, &
252 'FORMATTED', 'SEQUENTIAL', 'OLD')
253 ! initialize parser
254 call parser%Initialize(inunit, 0)
255 ! get options block
256 call parser%GetBlock('OPTIONS', isfound, ierr, &
257 supportopenclose=.true., blockrequired=.false.)
258 ! iterate through options
259 if (isfound) then
260 do
261 call parser%GetNextLine(endofblock)
262 if (endofblock) exit
263 end do
264 end if
265 ! get packages block
266 call parser%GetBlock('PACKAGES', isfound, ierr, &
267 supportopenclose=.true., blockrequired=.true.)
268 if (isfound) then
269 ! read through packages
270 do
271 call parser%GetNextLine(endofblock)
272 if (endofblock) exit
273 call parser%GetStringCaps(ptype)
274
275 select case (ptype)
276 case ('DIS6')
277 ncelldim = 3
278 exit
279 case ('DIS2D6')
280 ncelldim = 2
281 exit
282 case ('DISV6')
283 ncelldim = 2
284 exit
285 case ('DISU6')
286 ncelldim = 1
287 exit
288 case default
289 write (errmsg, '(a)') &
290 'Unknown discretization type "'//trim(ptype)// &
291 '" not currently supported.'
292 call store_error(errmsg, .true.)
293 end select
294 end do
295 end if
296
297 call parser%clear()
298 case default
299 end select
300 end function remote_model_ndim
301
302 !> @brief create model exports list
303 !<
304 subroutine export_cr()
306#if defined(__WITH_NETCDF__)
308#endif
309 call modelexports_create(iout)
310 ! are netcdf exports elected
311 if (nc_export_active()) then
312#if defined(__WITH_NETCDF__)
313 call nc_export_create()
314#else
315 write (errmsg, '(a)') &
316 'Model namefile NETCDF_STUCTURED or NETCDF_MESH2D option configured &
317 &but NetCDF libraries are not available.'
318 call store_error(errmsg, .true.)
319#endif
320 end if
321 end subroutine export_cr
322
323 !> @brief model exports post prepare step actions
324 !<
328 end subroutine export_post_prepare
329
330 !> @brief model exports post step actions
331 !<
332 subroutine export_post_step()
335 end subroutine export_post_step
336
337 !> @brief deallocate model export objects and list
338 !<
339 subroutine export_da()
342 end subroutine export_da
343
344 !> @brief close an open netcdf file
345 !<
346 subroutine nc_close(ncid, nc_fname)
347#if defined(__WITH_NETCDF__)
349#endif
350 integer(I4B), intent(in) :: ncid
351 character(len=*), intent(in) :: nc_fname
352 if (ncid > 0) then
353#if defined(__WITH_NETCDF__)
354 call nc_fclose(ncid, nc_fname)
355#endif
356 end if
357 end subroutine nc_close
358
359 !> @brief create model netcdf context
360 !<
361 function netcdf_context(modeltype, component_type, modelname, &
362 modelfname, iout) result(nc_vars)
366#if defined(__WITH_NETCDF__)
368#endif
369 character(len=*), intent(in) :: modeltype
370 character(len=*), intent(in) :: component_type
371 character(len=*), intent(in) :: modelname
372 character(len=*), intent(in) :: modelfname
373 integer(I4B), intent(in) :: iout
374 type(ncfilevarstype), pointer :: nc_vars
375 character(len=LENMEMPATH) :: input_mempath
376 character(len=LINELENGTH) :: nc_fname
377 integer(I4B) :: ncid
378
379 ! set input memory path
380 input_mempath = create_mem_path(modelname, 'NAM', idm_context)
381
382 ! allocate context object
383 allocate (nc_vars)
384
385 ! check if optional netcdf input file was provided
386 if (filein_fname(nc_fname, 'NETCDF_FNAME', input_mempath, modelfname)) then
387#if defined(__WITH_NETCDF__)
388 ! open nc input file
389 ncid = open_ncfile(nc_fname, iout)
390 ! read the file and build the context
391 call create_netcdf_context(modeltype, modelname, modelfname, &
392 nc_vars, nc_fname, ncid, iout)
393#else
394 write (errmsg, '(a)') &
395 'Cannot load model packages. NetCDF &
396 &keyword specified in input file but &
397 &NetCDF libraries are not available.'
398 call store_error(errmsg)
399 call store_error_filename(modelfname)
400#endif
401 else
402 ncid = 0
403 call nc_vars%init(modelname, '', ncid, '')
404 end if
405 end function netcdf_context
406
407end module sourceloadmodule
This module contains block parser methods.
integer(i4b) function getunit(this)
Get the unit number.
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.
integer(i4b) function, public open_mf6file(filetype, filename, component_fname, iout)
open a model package files
subroutine, public input_load(filename, mf6_input, component_filename, iout, nc_vars)
input load for traditional mf6 simulation static input file
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.
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:212
This module contains the ModelExportModule.
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
logical(lgp) function, public nc_export_active()
is netcdf export configured for any model
This module contains the ModflowInputModule.
type(modflowinputtype) function, public getmodflowinput(pkgtype, component_type, subcomponent_type, component_name, subcomponent_name, load_scope, 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.
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:94
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition Sim.f90:208
This module contains simulation variables.
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.
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
subroutine, public load_simnam()
integer(i4b) function, public open_source_file(pkgtype, filename, modelfname, iout)
class(staticpkgloadbasetype) function, pointer, public create_input_loader(component_type, subcomponent_type, component_name, subcomponent_name, input_type, load_scope, input_fname, component_fname, nc_vars)
factory function to create and setup model package static loader
subroutine, public load_simtdis()
class(staticpkgloadbasetype) function, pointer package_loader(source_type)
allocate source model package static loader
subroutine, public export_cr()
create model exports list
type(ncfilevarstype) function, pointer, public netcdf_context(modeltype, component_type, modelname, modelfname, iout)
create model netcdf context
subroutine, public nc_close(ncid, nc_fname)
close an open netcdf file
subroutine, public export_post_prepare()
model exports post prepare step actions
integer(i4b) function, public remote_model_ndim(mtype, mfname)
subroutine, public export_post_step()
model exports post step actions
subroutine, public load_modelnam(mtype, mfname, mname, iout)
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.