MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
IdmMf6File.f90
Go to the documentation of this file.
1!> @brief This module contains the IdmMf6FileModule
2!!
3!! This module contains high-level routines for loading
4!! MODFLOW 6 ASCII source input. This module implements the
5!! loader types that the IdmLoadModule creates and invokes.
6!! It also creates and manages dynamic ASCII input loaders
7!! for all supported types of MODFLOW 6 ASCII dynamic input.
8!!
9!<
11
12 use kindmodule, only: dp, i4b, lgp
13 use simvariablesmodule, only: errmsg
21
22 implicit none
23 private
24 public :: input_load
26 public :: open_mf6file
27
28 !> @brief MF6File static loader type
29 !<
31 contains
32 procedure :: init => static_init
33 procedure :: load => static_load
34 procedure :: destroy => static_destroy
36
37 !> @brief MF6File dynamic loader type
38 !<
40 type(blockparsertype), pointer :: parser !< parser for MF6File period blocks
41 integer(I4B), pointer :: iper => null() !< memory managed variable, loader iper
42 integer(I4B), pointer :: ionper => null() !< memory managed variable, next load period
43 logical(LGP), pointer :: hasperiodblocks => null() !< memory managed variable, file has a PERIOD block
44 class(asciidynamicpkgloadbasetype), pointer :: rp_loader => null()
45 contains
46 procedure :: init => dynamic_init
47 procedure :: df => dynamic_df
48 procedure :: ts_advance => dynamic_ad
49 procedure :: rp => dynamic_rp
50 procedure :: read_ionper => dynamic_read_ionper
51 procedure :: create_loader => dynamic_create_loader
52 procedure :: destroy => dynamic_destroy
54
55contains
56
57 !> @brief input load for traditional mf6 simulation static input file
58 !<
59 subroutine input_load(filename, mf6_input, component_filename, iout, nc_vars)
61 character(len=*), intent(in) :: filename
62 type(modflowinputtype), intent(in) :: mf6_input
63 character(len=*), intent(in) :: component_filename !< component (e.g. model) filename
64 integer(I4B), intent(in) :: iout !< unit number for output
65 type(ncpackagevarstype), pointer, optional, intent(in) :: nc_vars
66 type(blockparsertype), allocatable, target :: parser !< block parser
67 type(ncpackagevarstype), pointer :: netcdf_vars
68 type(loadmf6filetype) :: loader
69 integer(I4B) :: inunit
70
71 if (present(nc_vars)) then
72 netcdf_vars => nc_vars
73 else
74 nullify (netcdf_vars)
75 end if
76
77 ! open input file
78 inunit = open_mf6file(mf6_input%pkgtype, filename, component_filename, iout)
79
80 ! allocate and initialize parser
81 allocate (parser)
82 call parser%Initialize(inunit, iout)
83
84 ! invoke the load routine
85 call loader%load(parser, mf6_input, netcdf_vars, filename, iout)
86
87 ! clear parser file handles
88 call parser%clear()
89
90 ! cleanup
91 deallocate (parser)
92 end subroutine input_load
93
94 !> @brief static loader init
95 !<
96 subroutine static_init(this, mf6_input, component_name, component_input_name, &
97 input_name)
98 class(mf6filestaticpkgloadtype), intent(inout) :: this
99 type(modflowinputtype), intent(in) :: mf6_input
100 character(len=*), intent(in) :: component_name
101 character(len=*), intent(in) :: component_input_name
102 character(len=*), intent(in) :: input_name
103 ! initialize base type
104 call this%StaticPkgLoadType%init(mf6_input, component_name, &
105 component_input_name, input_name)
106 end subroutine static_init
107
108 !> @brief load routine for static loader
109 !<
110 function static_load(this, iout) result(rp_loader)
111 class(mf6filestaticpkgloadtype), intent(inout) :: this
112 integer(I4B), intent(in) :: iout
113 class(dynamicpkgloadbasetype), pointer :: rp_loader
114 class(mf6filedynamicpkgloadtype), pointer :: mf6_loader
115
116 ! initialize return pointer
117 nullify (rp_loader)
118
119 ! load model package to input context
120 if (this%iperblock > 0) then
121 ! allocate dynamic loader
122 allocate (mf6_loader)
123 ! point to nc_vars structure
124 mf6_loader%nc_vars => this%nc_vars
125 ! nullify nc_vars pointer so it isn't deallocated
126 nullify (this%nc_vars)
127 ! initialize dynamic loader
128 call mf6_loader%init(this%mf6_input, this%component_name, &
129 this%component_input_name, this%input_name, &
130 this%iperblock, iout)
131 ! set return pointer to base dynamic loader
132 rp_loader => mf6_loader
133 else
134 ! load static input
135 call input_load(this%input_name, this%mf6_input, &
136 this%component_input_name, iout, this%nc_vars)
137 end if
138 end function static_load
139
140 !> @brief static loader destroy
141 !<
142 subroutine static_destroy(this)
143 class(mf6filestaticpkgloadtype), intent(inout) :: this
144 ! deallocate base type
145 call this%StaticPkgLoadType%destroy()
146 end subroutine static_destroy
147
148 !> @brief dynamic loader init
149 !<
150 subroutine dynamic_init(this, mf6_input, component_name, component_input_name, &
151 input_name, iperblock, iout)
154 class(mf6filedynamicpkgloadtype), intent(inout) :: this
155 type(modflowinputtype), intent(in) :: mf6_input
156 character(len=*), intent(in) :: component_name
157 character(len=*), intent(in) :: component_input_name
158 character(len=*), intent(in) :: input_name
159 integer(I4B), intent(in) :: iperblock
160 integer(I4B), intent(in) :: iout
161 integer(I4B) :: inunit
162
163 ! initialize base loader
164 call this%DynamicPkgLoadType%init(mf6_input, component_name, &
165 component_input_name, input_name, &
166 iperblock, iout)
167 ! allocate scalars
168 call mem_allocate(this%iper, 'IPER', mf6_input%mempath)
169 call mem_allocate(this%ionper, 'IONPER', mf6_input%mempath)
170 call mem_allocate(this%hasperiodblocks, 'HASPERIODBLOCKS', &
171 mf6_input%mempath)
172
173 ! initialize
174 this%iper = 0
175 this%ionper = 0
176 this%hasperiodblocks = .false.
177
178 ! open input file
179 inunit = open_mf6file(mf6_input%pkgtype, input_name, &
180 component_input_name, iout)
181
182 ! allocate and initialize parser
183 allocate (this%parser)
184 call this%parser%Initialize(inunit, iout)
185
186 ! allocate and initialize loader
187 call this%create_loader()
188 end subroutine dynamic_init
189
190 !> @brief define routine for dynamic loader
191 !<
192 subroutine dynamic_df(this)
193 class(mf6filedynamicpkgloadtype), intent(inout) :: this
194 ! invoke loader define
195 call this%rp_loader%df()
196 ! read first ionper
197 call this%read_ionper()
198 end subroutine dynamic_df
199
200 !> @brief advance routine for dynamic loader
201 !<
202 subroutine dynamic_ad(this)
203 class(mf6filedynamicpkgloadtype), intent(inout) :: this
204 ! invoke loader advance
205 call this%rp_loader%ts_advance()
206 end subroutine dynamic_ad
207
208 !> @brief read and prepare routine for dynamic loader
209 !<
210 subroutine dynamic_rp(this)
211 use tdismodule, only: kper, nper
212 class(mf6filedynamicpkgloadtype), intent(inout) :: this
213
214 ! check if ready to load
215 if (this%ionper /= kper) return
216
217 ! dynamic load
218 call this%rp_loader%rp(this%parser)
219
220 ! update loaded iper
221 this%iper = kper
222
223 ! read next iper
224 if (kper < nper) then
225 call this%read_ionper()
226 else
227 this%ionper = nper + 1
228 end if
229 end subroutine dynamic_rp
230
231 !> @brief dynamic loader read ionper of next period block
232 !<
233 subroutine dynamic_read_ionper(this)
234 use tdismodule, only: kper, nper
235 class(mf6filedynamicpkgloadtype), intent(inout) :: this
236 character(len=LINELENGTH) :: line
237 logical(LGP) :: isblockfound
238 integer(I4B) :: ierr
239 character(len=*), parameter :: fmtblkerr = &
240 &"('Looking for BEGIN PERIOD iper. Found ', a, ' instead.')"
241
242 call this%parser%GetBlock('PERIOD', isblockfound, ierr, &
243 supportopenclose=.true., &
244 blockrequired=.false.)
245 ! set first period block IPER
246 if (isblockfound) then
247 ! the file has a PERIOD block, whatever its number. only set
248 ! here: at end of file IONPER is nper + 1, the same value as
249 ! a block numbered nper + 1, so IONPER can't tell them apart.
250 ! never set false: later calls reach end of file after the
251 ! last block, which would clear it for a file that has blocks.
252 this%hasperiodblocks = .true.
253 this%ionper = this%parser%GetInteger()
254 if (this%ionper <= this%iper) then
255 write (errmsg, '(a, i0, a, i0, a, i0, a)') &
256 'Error in stress period ', kper, &
257 '. Period numbers not increasing. Found ', this%ionper, &
258 ' but last period block was assigned ', this%iper, '.'
259 call store_error(errmsg)
260 call this%parser%StoreErrorUnit()
261 end if
262 else
263 ! PERIOD block not found
264 if (ierr < 0) then
265 ! End of file found; data applies for remainder of simulation.
266 this%ionper = nper + 1
267 else
268 ! Found invalid block
269 call this%parser%GetCurrentLine(line)
270 write (errmsg, fmtblkerr) adjustl(trim(line))
271 call store_error(errmsg)
272 call this%parser%StoreErrorUnit()
273 end if
274 end if
275 end subroutine dynamic_read_ionper
276
277 !> @brief allocate a dynamic loader based on load context
278 !<
279 subroutine dynamic_create_loader(this)
286 class(mf6filedynamicpkgloadtype), intent(inout) :: this
287 class(listloadtype), pointer :: list_loader
288 class(gridarrayloadtype), pointer :: arrgrid_loader
289 class(layerarrayloadtype), pointer :: arrlayer_loader
290 class(settingloadtype), pointer :: setting_loader
291 class(keystringloadtype), pointer :: keystring_loader
292 class(stoinputtype), pointer :: sto_loader
293
294 ! allocate and set loader
295 if (this%mf6_input%subcomponent_type == 'STO') then
296 allocate (sto_loader)
297 this%rp_loader => sto_loader
298 else if (this%mf6_input%subcomponent_type == 'OC' .or. &
299 this%mf6_input%subcomponent_type == 'PRP') then
300 allocate (setting_loader)
301 this%rp_loader => setting_loader
302 else if (this%has_keystring) then
303 allocate (keystring_loader)
304 this%rp_loader => keystring_loader
305 else if (this%readasarrays) then
306 allocate (arrlayer_loader)
307 this%rp_loader => arrlayer_loader
308 else if (this%readarraygrid) then
309 allocate (arrgrid_loader)
310 this%rp_loader => arrgrid_loader
311 else
312 allocate (list_loader)
313 this%rp_loader => list_loader
314 end if
315
316 ! set nc_vars pointer
317 this%rp_loader%nc_vars => this%nc_vars
318
319 ! initialize loader
320 call this%rp_loader%ainit(this%mf6_input, &
321 this%component_name, &
322 this%component_input_name, &
323 this%input_name, &
324 this%iperblock, &
325 this%parser, &
326 this%iout)
327 end subroutine dynamic_create_loader
328
329 !> @brief dynamic loader destroy
330 !<
331 subroutine dynamic_destroy(this)
333 class(mf6filedynamicpkgloadtype), intent(inout) :: this
334
335 ! deallocate scalars
336 call mem_deallocate(this%iper)
337 call mem_deallocate(this%ionper)
338 call mem_deallocate(this%hasperiodblocks)
339
340 ! deallocate loader
341 nullify (this%rp_loader%nc_vars)
342 call this%rp_loader%destroy()
343 deallocate (this%rp_loader)
344
345 ! deallocate parser
346 call this%parser%clear()
347 deallocate (this%parser)
348
349 ! deallocate input context
350 call this%DynamicPkgLoadType%destroy()
351 end subroutine dynamic_destroy
352
353 !> @brief open a model package files
354 !<
355 function open_mf6file(filetype, filename, component_fname, iout) result(inunit)
357 character(len=*), intent(in) :: filetype
358 character(len=*), intent(in) :: filename
359 character(len=*), intent(in) :: component_fname
360 integer(I4B), intent(in) :: iout
361 integer(I4B) :: inunit
362
363 ! initialize
364 inunit = 0
365
366 if (filename /= '') then
367 ! get unit number and open file
368 inunit = getunit()
369 call openfile(inunit, iout, trim(adjustl(filename)), filetype, &
370 'FORMATTED', 'SEQUENTIAL', 'OLD')
371 else
372 write (errmsg, '(a,a,a)') &
373 'File unspecified, cannot load model or package &
374 &type "', trim(filetype), '".'
375 call store_error(errmsg)
376 call store_error_filename(component_fname)
377 end if
378 end function open_mf6file
379
380end module idmmf6filemodule
subroutine init()
This module contains the AsciiInputLoadTypeModule.
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
This module contains the GridArrayLoadModule.
This module contains the IdmMf6FileModule.
integer(i4b) function, public open_mf6file(filetype, filename, component_fname, iout)
open a model package files
subroutine dynamic_create_loader(this)
allocate a dynamic loader based on load context
subroutine, public input_load(filename, mf6_input, component_filename, iout, nc_vars)
input load for traditional mf6 simulation static input file
subroutine dynamic_rp(this)
read and prepare routine for dynamic loader
subroutine dynamic_read_ionper(this)
dynamic loader read ionper of next period block
class(dynamicpkgloadbasetype) function, pointer static_load(this, iout)
load routine for static loader
Input definition module.
This module contains the InputLoadTypeModule.
subroutine dynamic_ad(this)
dynamic package loader advance
subroutine static_init(this, mf6_input, component_name, component_input_name, input_name)
initialize static package loader
subroutine dynamic_init(this, mf6_input, component_name, component_input_name, input_name, iperblock, iout)
initialize dynamic package loader
subroutine static_destroy(this)
subroutine dynamic_destroy(this)
dynamic package loader destroy
subroutine dynamic_df(this)
dynamic package loader define
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
This module contains the LayerArrayLoadModule.
This module contains the ListLoadModule.
This module contains the LoadMf6FileModule.
Period block keystring-based input loader.
This module contains the Mf6FileSettingLoadModule.
This module contains the Mf6FileStoInputModule.
This module contains the ModflowInputModule.
This module contains the NCFileVarsModule.
Definition NCFileVars.f90:7
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
integer(i4b) iout
file unit number for simulation output
integer(i4b), pointer, public kper
current stress period number
Definition tdis.f90:26
integer(i4b), pointer, public nper
number of stress period
Definition tdis.f90:24
base abstract type for ascii source dynamic load
Ascii grid based dynamic loader type.
Input parameter definition. Describes an input parameter.
Base abstract type for dynamic input loader.
Base abstract type for static input loader.
Ascii array layer dynamic loader type.
list input loader for dynamic packages.
Static parser based input loader.
derived type for storing input definition for a file
Type describing input variables for a package in NetCDF file.