MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
Mf6FileKeystring.f90
Go to the documentation of this file.
1!> @brief Period block keystring-based input loader
2!!
3!! Each keystring item maps to a typed column in a StructArrayType.
4!! A dispatch keyword on each input row selects the target column.
5!!
6!! Simple dispatch: keyword matches a DOUBLE/STRING/INTEGER column;
7!! one value token is read into that column.
8!!
9!! Compound dispatch: keyword matches a KEYWORD-type column (e.g.
10!! FLOWING_WELL). The keyword token is stored directly; subsequent
11!! non-KEYWORD body columns are read in order.
12!!
13!<
15
16 use kindmodule, only: dp, i4b, lgp
34
35 implicit none
36 private
37 public :: keystringloadtype
38
39 !> @brief Wraps an allocatable int array for a per-column cache table.
40 !<
42 integer(I4B), dimension(:), allocatable :: vals
43 end type intarraytype
44
45 !> @brief Keystring period block loader
46 !!
47 !! Leading fixed columns (e.g. CELLID) followed by a dispatch
48 !! keyword that routes each input row to a typed item column.
49 !!
50 !<
52 type(timeseriesmanagertype), pointer :: tsmanager => null()
53 type(structarraytype), pointer :: structarray => null()
54 type(loadcontexttype) :: ctx !< input load context
55 type(loadmf6filetype) :: static_loader !< persistent static loader
56 logical(LGP) :: ts_active !< .true. if TS files are loaded
57 integer(I4B) :: nleading !< number of leading (pre-keystring) columns
58 ! cached once in allocate_subindex (df()-time), since
59 ! ctx%subindex_dependency's named dimension may be released by rp()-time
60 integer(I4B), dimension(:), allocatable :: subindex_nfeatures !< per-column feature count (0 = n/a)
61 type(intarraytype), dimension(:), allocatable :: subindex_offsets !< per-column offset table
62 integer(I4B), dimension(:), allocatable :: subindex_icol !< per-column subindex SA column
63 integer(I4B), dimension(:), allocatable :: subindex_head_icol !< per-column subindex head SA column
64 character(len=LENVARNAME) :: subindex_id_varname = '' !< leading column's mf6varname
65 contains
66 procedure :: ainit
67 procedure :: df
68 procedure :: ts_advance
69 procedure :: rp
70 procedure :: allocate_subindex
72 procedure :: allocate_items
73 procedure, private :: valid_address
74 procedure :: apply_auxiliary
75 procedure :: apply_subindex
76 procedure :: apply_items
77 procedure :: resolve_row_addr
80 procedure :: reset
81 procedure :: destroy
82 procedure :: create_structarray
83 end type keystringloadtype
84
85contains
86
87 subroutine ainit(this, mf6_input, component_name, component_input_name, &
88 input_name, iperblock, parser, iout)
89 use inputoutputmodule, only: getunit
93 class(keystringloadtype), intent(inout) :: this
94 type(modflowinputtype), intent(in) :: mf6_input
95 character(len=*), intent(in) :: component_name
96 character(len=*), intent(in) :: component_input_name
97 character(len=*), intent(in) :: input_name
98 integer(I4B), intent(in) :: iperblock
99 type(blockparsertype), pointer, intent(inout) :: parser
100 integer(I4B), intent(in) :: iout
101 type(characterstringtype), dimension(:), pointer, contiguous :: ts_fnames
102 character(len=LINELENGTH) :: fname
103 character(len=LENVARNAME) :: named_bound
104 logical(LGP) :: has_named_bound
105 integer(I4B) :: n, isize
106
107 call this%DynamicPkgLoadType%init(mf6_input, component_name, &
108 component_input_name, input_name, &
109 iperblock, iout)
110 this%ts_active = .false.
111 this%nleading = 0
112
113 allocate (this%tsmanager)
114 call tsmanager_cr(this%tsmanager, iout)
115
116 ! load static input (TS6_FILENAME tag sets static_loader%ts_active)
117 call this%static_loader%load(parser, mf6_input, this%nc_vars, &
118 this%input_name, iout)
119
120 ! add declared TS files to tsmanager
121 if (this%static_loader%ts_active) then
122 this%ts_active = .true.
123 call get_isize('TS6_FILENAME', mf6_input%mempath, isize)
124 if (isize > 0) then
125 call mem_setptr(ts_fnames, 'TS6_FILENAME', mf6_input%mempath)
126 do n = 1, size(ts_fnames)
127 fname = ts_fnames(n)
128 call this%tsmanager%add_tsfile(fname, getunit())
129 end do
130 end if
131 end if
132
133 ! find a DIMENSIONS parameter to alias as maxbound (skipped for
134 ! advanced packages, which get their count from PACKAGEDATA)
135 has_named_bound = .false.
136 if (.not. is_advanced(mf6_input)) then
137 do n = 1, size(mf6_input%param_dfns)
138 if (mf6_input%param_dfns(n)%blockname == 'DIMENSIONS') then
139 named_bound = trim(mf6_input%param_dfns(n)%mf6varname)
140 has_named_bound = .true.
141 exit
142 end if
143 end do
144 end if
145
146 ! init load context
147 if (has_named_bound) then
148 call this%ctx%init(mf6_input, named_bound=named_bound)
149 else
150 call this%ctx%init(mf6_input)
151 end if
152
153 ! params is fully elaborated: leading cols + item names
154 this%param_names = this%ctx%params
155 this%nparam = size(this%ctx%params)
156 this%nleading = this%ctx%nleading
157 call this%ctx%check_developmode(this%input_name)
158
159 ! finalize context setup (allocates NBOUND, NODEULIST, etc.)
160 call this%ctx%allocate_arrays()
161
162 ! pre-allocate structarray; reused across all periods
163 call this%create_structarray()
164
165 end subroutine ainit
166
167 subroutine df(this)
171 class(keystringloadtype), intent(inout) :: this
172 type(structarraytype), pointer :: sa
173 type(characterstringtype), dimension(:), pointer, contiguous :: &
174 auxnames => null()
175 integer(I4B), dimension(:), pointer, contiguous :: row_key => null()
176 integer(I4B) :: n, naux
177 ! init tsmanager (TDIS now available)
178 call this%tsmanager%tsmanager_df()
179 ! resolve aux names for PACKAGEDATA AUX TS registration
180 call get_isize('AUXILIARY', this%mf6_input%mempath, naux)
181 if (naux > 0) call mem_setptr(auxnames, 'AUXILIARY', this%mf6_input%mempath)
182 ! link static TS strlocs; preserve for re-registration after reset().
183 ! advanced packages address each link by its own block's key column,
184 ! not row position, so a later PERIOD override finds an AUX link and
185 ! listings report the key
186 do n = 1, this%static_loader%ts_sa_count()
187 sa => this%static_loader%get_ts_sa(n)
188 if (.not. associated(sa)) cycle
189 nullify (row_key)
190 if (this%ctx%is_advanced) then
191 ! key columns are hardcoded for now: PACKAGEDATA IFNO and LAK
192 ! OUTLETS OUTLETNO. TODO: look these up from DFN pk/fk metadata
193 select case (sa%struct_vectors(1)%idt%blockname)
194 case ('PACKAGEDATA')
195 call mem_setptr(row_key, 'PACKAGEDATA_IFNO', this%mf6_input%mempath)
196 case ('OUTLETS')
197 call mem_setptr(row_key, 'OUTLETS_OUTLETNO', this%mf6_input%mempath)
198 end select
199 end if
200 if (associated(row_key)) then
201 call sa%ts_update(this%tsmanager, &
202 this%mf6_input%subcomponent_name, &
203 this%ctx%iprpak, this%input_name, &
204 clear_strlocs=.false., auxname_cst=auxnames, &
205 ifno_map=row_key)
206 else
207 call sa%ts_update(this%tsmanager, &
208 this%mf6_input%subcomponent_name, &
209 this%ctx%iprpak, this%input_name, &
210 clear_strlocs=.false., auxname_cst=auxnames)
211 end if
212 end do
213 ! allocate feature-addressed (DZERO) and node-addressed (DNODATA) items
214 call this%allocate_items()
215 ! subindex df-time dimension fields + permanent arrays
216 if (this%ctx%is_advanced) call this%allocate_subindex()
217 end subroutine df
218
219 subroutine ts_advance(this)
220 class(keystringloadtype), intent(inout) :: this
221 call this%tsmanager%ad()
222 end subroutine ts_advance
223
224 subroutine rp(this, parser)
226 class(keystringloadtype), intent(inout) :: this
227 type(blockparsertype), pointer, intent(inout) :: parser
228
229 call this%reset()
230
231 call idm_log_header(this%mf6_input%component_name, &
232 this%mf6_input%subcomponent_name, this%iout)
233
234 this%ctx%nbound = &
235 this%structarray%read_from_parser_keystring(parser, this%ts_active, &
236 this%nleading, this%iout, &
237 this%input_name)
238
239 if (this%ctx%is_advanced) call this%apply_auxiliary()
240 if (this%ctx%is_advanced) call this%apply_subindex()
241 ! apply feature- and node-addressed items
242 call this%apply_items()
243
244 if (this%ts_active) then
245 call this%structarray%ts_update(this%tsmanager, &
246 this%mf6_input%subcomponent_name, &
247 this%ctx%iprpak, this%input_name)
248 end if
249
250 call idm_log_close(this%mf6_input%component_name, &
251 this%mf6_input%subcomponent_name, this%iout)
252 end subroutine rp
253
254 !> @brief Allocate ctx%subindex_dependency targets (subindex
255 !! params excluded from allocate_items), and cache
256 !! everything apply_subindex needs every period.
257 !<
258 subroutine allocate_subindex(this)
260 class(keystringloadtype), intent(inout) :: this
261 type(inputparamdefinitiontype), pointer :: idt, id_idt
262 character(len=LENVARNAME) :: dimname, subindex_tagname
263 integer(I4B) :: icol, sa_icol, padj, nfeatures, n
264 logical(LGP) :: found
265
266 if (.not. allocated(this%subindex_nfeatures)) then
267 allocate (this%subindex_nfeatures(this%structarray%count()))
268 allocate (this%subindex_offsets(this%structarray%count()))
269 allocate (this%subindex_icol(this%structarray%count()))
270 allocate (this%subindex_head_icol(this%structarray%count()))
271 this%subindex_nfeatures = 0
272 this%subindex_icol = 0
273 this%subindex_head_icol = 0
274 end if
275
276 padj = 0
277 if (this%ctx%has_setting_dispatch) padj = 1
278
279 id_idt => get_param_definition_type(this%mf6_input%param_dfns, &
280 this%mf6_input%component_type, &
281 this%mf6_input%subcomponent_type, &
282 this%ctx%blockname, &
283 this%param_names(1), &
284 this%input_name)
285 this%subindex_id_varname = trim(id_idt%mf6varname)
286
287 do icol = this%nleading + 1, this%nparam
288 if (.not. this%ctx%keystring_items(icol - this%nleading)%is_body) cycle
289 idt => get_param_definition_type(this%mf6_input%param_dfns, &
290 this%mf6_input%component_type, &
291 this%mf6_input%subcomponent_type, &
292 this%ctx%blockname, &
293 this%param_names(icol), &
294 this%input_name)
295 found = this%ctx%subindex_dependency(idt%tagname, dimname, &
296 subindex_tagname)
297 if (.not. found) cycle
298
299 sa_icol = icol + padj
300 nfeatures = this%ctx%resolve_item_nfeatures(idt%tagname, dimname, 0)
301 if (nfeatures < 1) cycle
302 this%subindex_nfeatures(sa_icol) = nfeatures
303 this%subindex_offsets(sa_icol)%vals = &
304 this%resolve_subindex_offsets(dimname)
305 call this%allocate_permanent_array( &
306 trim(idt%mf6varname), nfeatures, dzero)
307
308 ! resolve the subindex column (by ctx-given tag) and this
309 ! target's subindex head, both structurally (body_start/head_nbody)
310 do n = 1, this%structarray%count()
311 if (trim(this%structarray%struct_vectors(n)%idt%tagname) == &
312 trim(subindex_tagname)) this%subindex_icol(sa_icol) = n
313 if (this%structarray%struct_vectors(n)%head_nbody > 0) then
314 if (sa_icol >= this%structarray%struct_vectors(n)%body_start .and. &
315 sa_icol < this%structarray%struct_vectors(n)%body_start + &
316 this%structarray%struct_vectors(n)%head_nbody) &
317 this%subindex_head_icol(sa_icol) = n
318 end if
319 end do
320 ! neither subindex nor head found (misconfigured ctx entry): not a target
321 if (this%subindex_icol(sa_icol) == 0 .or. &
322 this%subindex_head_icol(sa_icol) == 0) &
323 this%subindex_nfeatures(sa_icol) = 0
324 end do
325 end subroutine allocate_subindex
326
327 !> @brief Allocate a permanent per-feature array named `name` with
328 !! init_value, unless already allocated.
329 !<
330 subroutine allocate_permanent_array(this, name, nfeatures, init_value)
332 class(keystringloadtype), intent(inout) :: this
333 character(len=*), intent(in) :: name
334 integer(I4B), intent(in) :: nfeatures
335 real(DP), intent(in) :: init_value
336 real(DP), dimension(:), pointer, contiguous :: featarr => null()
337 integer(I4B) :: isize
338
339 call get_isize(name, this%mf6_input%mempath, isize)
340 if (isize > 0) return ! already allocated (shouldn't happen; df() runs once)
341 call mem_allocate(featarr, nfeatures, name, this%mf6_input%mempath)
342 featarr = init_value
343 end subroutine allocate_permanent_array
344
345 !> @brief Allocate a permanent array for every IDM-managed item, using
346 !! the descriptor's per-item feature count and init value.
347 !<
348 subroutine allocate_items(this)
350 class(keystringloadtype), intent(inout) :: this
351 integer(I4B) :: k
352
353 if (.not. allocated(this%ctx%keystring_items)) return
354
355 do k = 1, size(this%ctx%keystring_items)
356 if (.not. this%ctx%keystring_items(k)%idm_managed) cycle
357 ! subindex arrays are allocated in allocate_subindex (df-time
358 ! dimension); skip here
359 if (this%ctx%keystring_items(k)%addr_mode == addr_subindex) cycle
360 if (this%ctx%keystring_items(k)%nfeatures < 1) cycle
361 call this%allocate_permanent_array( &
362 this%ctx%keystring_items(k)%idt%mf6varname, &
363 this%ctx%keystring_items(k)%nfeatures, &
364 this%ctx%keystring_items(k)%init_value)
365 end do
366 if (count_errors() > 0) then
367 call store_error_filename(this%input_name)
368 end if
369 end subroutine allocate_items
370
371 !> @brief Validate ifno against nfeatures, storing an error keyed by
372 !! ifno_tagname (the leading column's public tag) if out of range.
373 !<
374 function valid_address(this, ifno, nfeatures, ifno_tagname, row) result(valid)
375 use simmodule, only: store_error
376 use simvariablesmodule, only: errmsg
377 class(keystringloadtype), intent(inout) :: this
378 integer(I4B), intent(in) :: ifno
379 integer(I4B), intent(in) :: nfeatures
380 character(len=*), intent(in) :: ifno_tagname
381 integer(I4B), intent(in) :: row
382 logical(LGP) :: valid
383
384 valid = (ifno >= 1 .and. ifno <= nfeatures)
385 if (.not. valid) then
386 write (errmsg, '(a,1x,i0,1x,a,1x,i0,1x,a,1x,i0,a)') &
387 trim(ifno_tagname), ifno, 'on row', row, &
388 'must be greater than 0 and less than or equal to', nfeatures, '.'
389 call store_error(errmsg)
390 end if
391 end function valid_address
392
393 !> @brief Apply PERIOD AUXILIARY settings to the permanent AUX array.
394 !! TS-linked rows resolve via the struct array's own ts_strlocs;
395 !! literal rows resolve AUXNAME here and clear any stale TS link.
396 !<
397 subroutine apply_auxiliary(this)
401 class(keystringloadtype), intent(inout) :: this
402 integer(I4B), pointer :: nbound => null()
403 integer(I4B), dimension(:), pointer, contiguous :: period_ifno => null()
404 type(characterstringtype), dimension(:), pointer, contiguous :: &
405 period_setting => null()
406 type(characterstringtype), dimension(:), pointer, contiguous :: &
407 period_auxname => null()
408 type(characterstringtype), dimension(:), pointer, contiguous :: &
409 auxnames => null()
410 real(DP), dimension(:, :), pointer, contiguous :: aux => null()
411 real(DP), pointer :: bndElem
412 type(inputparamdefinitiontype), pointer :: idt
413 type(tsstringloctype), pointer :: ts_strloc
414 integer(I4B) :: i, n, ifno, jj, isize, naux, nfeatures, sa_icol, k, nts
415 logical(LGP) :: found
416 logical(LGP), dimension(:), allocatable :: handled
417 character(len=LINELENGTH) :: setting, auxname, thisauxname
418 character(len=LENVARNAME) :: ifno_tagname
419
420 call get_isize('AUXILIARY', this%mf6_input%mempath, naux)
421 if (naux <= 0) return
422
423 call get_isize('NBOUND', this%mf6_input%mempath, isize)
424 if (isize < 1) return
425 call mem_setptr(nbound, 'NBOUND', this%mf6_input%mempath)
426 if (nbound <= 0) return
427
428 call get_isize('AUXNAME', this%mf6_input%mempath, isize)
429 if (isize < 1) return
430
431 ! leading column's public tag (e.g. MAWNO for MWE), for the error
432 ! message below -- its memory-manager key is always IFNO (MF6INTERNAL)
433 idt => get_param_definition_type(this%mf6_input%param_dfns, &
434 this%mf6_input%component_type, &
435 this%mf6_input%subcomponent_type, &
436 this%ctx%blockname, &
437 this%param_names(1), &
438 this%input_name)
439 ifno_tagname = trim(idt%tagname)
440
441 ! AUXILIARY dispatch keyword's own mf6varname (e.g. LAK's
442 ! PERIOD_AUXILIARY), since SETTING stores mf6varname, not tagname
443 idt => get_param_definition_type(this%mf6_input%param_dfns, &
444 this%mf6_input%component_type, &
445 this%mf6_input%subcomponent_type, &
446 this%ctx%blockname, 'AUXILIARY', &
447 this%input_name)
448
449 call mem_setptr(period_ifno, 'IFNO', this%mf6_input%mempath)
450 call mem_setptr(period_setting, 'SETTING', this%mf6_input%mempath)
451 call mem_setptr(period_auxname, 'AUXNAME', this%mf6_input%mempath)
452 call mem_setptr(auxnames, 'AUXILIARY', this%mf6_input%mempath)
453 call mem_setptr(aux, 'AUX', this%mf6_input%mempath)
454 nfeatures = size(aux, 2)
455
456 sa_icol = 0
457 do n = 1, this%structarray%count()
458 if (is_auxval(this%structarray%struct_vectors(n)%idt)) then
459 sa_icol = n
460 exit
461 end if
462 end do
463 if (sa_icol == 0) return
464
465 allocate (handled(nbound))
466 handled = .false.
467
468 ! TS-linked rows: resolve AUXNAME here too, same as literal rows
469 nts = this%structarray%struct_vectors(sa_icol)%ts_strlocs%count()
470 do k = 1, nts
471 ts_strloc => this%structarray%struct_vectors(sa_icol)%get_ts_strloc(k)
472 i = ts_strloc%row
473 ifno = period_ifno(i)
474 if (.not. this%valid_address(ifno, nfeatures, ifno_tagname, i)) cycle
475 auxname = period_auxname(i)
476 jj = find_auxname_index(auxname, auxnames, naux)
477 if (jj < 1) cycle
478 thisauxname = auxnames(jj)
479 bndelem => aux(jj, ifno)
480 call read_value_or_time_series_adv(ts_strloc%token, ifno, jj, bndelem, &
481 this%mf6_input%subcomponent_name, &
482 'AUX', this%tsmanager, &
483 this%ctx%iprpak, trim(thisauxname))
484 handled(i) = .true.
485 end do
486
487 ! literal rows: resolve AUXNAME here, clear any stale link, assign
488 do i = 1, nbound
489 if (handled(i)) cycle
490 setting = period_setting(i)
491 if (trim(setting) /= trim(idt%mf6varname)) cycle
492 ifno = period_ifno(i)
493 if (.not. this%valid_address(ifno, nfeatures, ifno_tagname, i)) cycle
494 auxname = period_auxname(i)
495 jj = find_auxname_index(auxname, auxnames, naux)
496 if (jj < 1) cycle
497 thisauxname = auxnames(jj)
498 found = remove_existing_link(this%tsmanager, ifno, jj, &
499 this%mf6_input%subcomponent_name, &
500 'AUX', trim(thisauxname))
501 aux(jj, ifno) = this%structarray%struct_vectors(sa_icol)%dbl1d(i)
502 end do
503 if (count_errors() > 0) then
504 call store_error_filename(this%input_name)
505 end if
506 deallocate (handled)
507 call this%structarray%struct_vectors(sa_icol)%clear()
508 end subroutine apply_auxiliary
509
510 !> @brief Apply PERIOD settings for ctx%subindex_dependency's targets,
511 !! via ts_update_indexed -- same mechanism as BEDK/MANNING, but each
512 !! row's target index comes from a named subindex field through the
513 !! cached offset table rather than the leading id column alone.
514 !<
515 subroutine apply_subindex(this)
516 class(keystringloadtype), intent(inout) :: this
517 type(inputparamdefinitiontype), pointer :: idt, head_idt
518 real(DP), dimension(:), pointer, contiguous :: featarr => null()
519 integer(I4B), pointer :: nbound => null()
520 integer(I4B), dimension(:), allocatable :: row_addr
521 integer(I4B) :: icol, sa_icol, padj, isize
522
523 if (.not. allocated(this%subindex_nfeatures)) return
524
525 call get_isize('NBOUND', this%mf6_input%mempath, isize)
526 if (isize < 1) return
527 call mem_setptr(nbound, 'NBOUND', this%mf6_input%mempath)
528 if (nbound <= 0) return
529
530 padj = 0
531 if (this%ctx%has_setting_dispatch) padj = 1
532
533 do icol = this%nleading + 1, this%nparam
534 sa_icol = icol + padj
535 if (sa_icol > size(this%subindex_nfeatures)) cycle
536 if (this%subindex_nfeatures(sa_icol) < 1) cycle
537
538 idt => this%structarray%struct_vectors(sa_icol)%idt
539 head_idt => &
540 this%structarray%struct_vectors(this%subindex_head_icol(sa_icol))%idt
541 call mem_setptr(featarr, trim(idt%mf6varname), this%mf6_input%mempath)
542 row_addr = &
543 this%resolve_subindex_address( &
544 this%subindex_id_varname, trim(head_idt%mf6varname), &
545 this%subindex_icol(sa_icol), &
546 this%subindex_offsets(sa_icol)%vals, &
547 this%subindex_nfeatures(sa_icol), nbound)
548 call this%structarray%ts_update_indexed( &
549 sa_icol, this%tsmanager, this%mf6_input%subcomponent_name, &
550 this%ctx%iprpak, nbound, row_addr, trim(idt%tagname), featarr)
551 end do
552 end subroutine apply_subindex
553
554 !> @brief Apply IDM-managed feature- and node-addressed settings to their
555 !! permanent arrays; each row's address comes from resolve_row_addr.
556 !! Indexed-body and AUX are handled in their own routines.
557 !<
558 subroutine apply_items(this)
561 class(keystringloadtype), intent(inout) :: this
562 integer(I4B), pointer :: nbound => null()
563 integer(I4B), dimension(:, :), pointer, contiguous :: cellid => null()
564 integer(I4B), dimension(:), pointer, contiguous :: period_ifno => null()
565 type(characterstringtype), dimension(:), pointer, contiguous :: &
566 period_setting => null()
567 real(DP), dimension(:), pointer, contiguous :: featarr => null()
568 integer(I4B), dimension(:), allocatable :: row_addr
569 integer(I4B) :: i, k, isize, ndim
570 character(len=LINELENGTH) :: setting
571 character(len=LENVARNAME) :: dispatch_key
572 character(len=LENVARNAME) :: leading_tag
573 type(inputparamdefinitiontype), pointer :: lead_idt
574
575 if (.not. allocated(this%ctx%keystring_items)) return
576
577 call get_isize('NBOUND', this%mf6_input%mempath, isize)
578 if (isize < 1) return
579 call mem_setptr(nbound, 'NBOUND', this%mf6_input%mempath)
580 if (nbound <= 0) return
581
582 call mem_setptr(period_setting, 'SETTING', this%mf6_input%mempath)
583
584 ! addressing sources, resolved once
585 ndim = 0
586 leading_tag = ''
587 if (this%ctx%keystring_by_node) then
588 if (.not. associated(this%ctx%mshape)) return
589 ndim = size(this%ctx%mshape)
590 call mem_setptr(cellid, 'CELLID', this%mf6_input%mempath)
591 else
592 call mem_setptr(period_ifno, this%ctx%feature_id_varname, &
593 this%mf6_input%mempath)
594 ! leading id column's public tag (e.g. MAWNO), for the out-of-range
595 ! message -- the invalid value is the row's leading id, not the
596 ! setting. Feature-addressed only; node loads validate via CELLID.
597 lead_idt => get_param_definition_type(this%mf6_input%param_dfns, &
598 this%mf6_input%component_type, &
599 this%mf6_input%subcomponent_type, &
600 this%ctx%blockname, &
601 this%param_names(1), &
602 this%input_name)
603 leading_tag = trim(lead_idt%tagname)
604 end if
605
606 do k = 1, size(this%ctx%keystring_items)
607 if (.not. this%ctx%keystring_items(k)%idm_managed) cycle
608 if (this%ctx%keystring_items(k)%addr_mode == addr_subindex) cycle ! separate path
609 if (this%ctx%keystring_items(k)%nfeatures < 1) cycle
610 call mem_setptr(featarr, this%ctx%keystring_items(k)%idt%mf6varname, &
611 this%mf6_input%mempath)
612
613 ! dispatch key: a RECORD body matches its owning head's SETTING
614 ! keyword (the token on the row); a standalone item matches its own
615 ! mf6varname. resolve_row_addr maps the matched row to a feature.
616 if (this%ctx%keystring_items(k)%is_body) then
617 dispatch_key = this%ctx%keystring_items(k)%head_setting_varname
618 else
619 dispatch_key = this%ctx%keystring_items(k)%idt%mf6varname
620 end if
621
622 allocate (row_addr(nbound))
623 do i = 1, nbound
624 row_addr(i) = 0
625 setting = period_setting(i)
626 if (trim(setting) /= trim(dispatch_key)) cycle
627 row_addr(i) = &
628 this%resolve_row_addr(this%ctx%keystring_items(k), i, period_ifno, &
629 cellid, ndim, leading_tag)
630 end do
631 call this%structarray%ts_update_indexed( &
632 this%ctx%keystring_items(k)%sa_icol, this%tsmanager, &
633 this%mf6_input%subcomponent_name, this%ctx%iprpak, nbound, row_addr, &
634 this%ctx%keystring_items(k)%idt%tagname, featarr)
635 deallocate (row_addr)
636 end do
637 if (count_errors() > 0) then
638 call store_error_filename(this%input_name)
639 end if
640 end subroutine apply_items
641
642 !> @brief Resolve one row's target index for an item, dispatching on
643 !! addr_mode. Returns 0 (skip) if out of range.
644 !<
645 function resolve_row_addr(this, item, irow, period_ifno, cellid, ndim, &
646 leading_tag) &
647 result(addr)
648 use geomutilmodule, only: get_node
649 class(keystringloadtype), intent(inout) :: this
650 type(keystringitemtype), intent(in) :: item
651 integer(I4B), intent(in) :: irow
652 integer(I4B), dimension(:), pointer, contiguous, intent(in) :: period_ifno
653 integer(I4B), dimension(:, :), pointer, contiguous, intent(in) :: cellid
654 integer(I4B), intent(in) :: ndim
655 character(len=*), intent(in) :: leading_tag !< leading id column's public tag (for the out-of-range message)
656 integer(I4B) :: addr
657 integer(I4B) :: ifno, nodeu
658
659 addr = 0
660 select case (item%addr_mode)
661 case (addr_feature)
662 ifno = period_ifno(irow)
663 if (.not. this%valid_address(ifno, item%nfeatures, leading_tag, irow)) &
664 return
665 addr = ifno
666 case (addr_node)
667 if (ndim == 1) then
668 nodeu = cellid(1, irow)
669 else if (ndim == 2) then
670 nodeu = get_node(cellid(1, irow), 1, cellid(2, irow), &
671 this%ctx%mshape(1), 1, this%ctx%mshape(2))
672 else
673 nodeu = get_node(cellid(1, irow), cellid(2, irow), cellid(3, irow), &
674 this%ctx%mshape(1), this%ctx%mshape(2), &
675 this%ctx%mshape(3))
676 end if
677 if (nodeu < 1 .or. nodeu > item%nfeatures) return
678 addr = nodeu
679 end select
680 end function resolve_row_addr
681
682 !> @brief Per-feature cumulative offset table from an array-valued
683 !! dimension, permuted into feature-index order first. Indexed by
684 !! feature (not the dimension's own summed total).
685 !<
686 function resolve_subindex_offsets(this, dimname) result(offsets)
687 class(keystringloadtype), intent(inout) :: this
688 character(len=*), intent(in) :: dimname
689 integer(I4B), dimension(:), allocatable :: offsets
690 integer(I4B), dimension(:), pointer, contiguous :: counts_raw => null()
691 integer(I4B), dimension(:), pointer, contiguous :: pkg_ifno => null()
692 integer(I4B), dimension(:), allocatable :: counts
693 integer(I4B) :: i, n, running, ndomain
694
695 call mem_setptr(counts_raw, trim(dimname), this%mf6_input%mempath)
696 call mem_setptr(pkg_ifno, 'PACKAGEDATA_IFNO', this%mf6_input%mempath)
697 ndomain = size(counts_raw)
698 allocate (counts(ndomain))
699 counts = 0
700 do i = 1, size(counts_raw)
701 n = pkg_ifno(i)
702 if (n < 1 .or. n > ndomain) cycle
703 counts(n) = counts_raw(i)
704 end do
705
706 allocate (offsets(ndomain))
707 running = 1
708 do n = 1, ndomain
709 offsets(n) = running
710 running = running + counts(n)
711 end do
712 end function resolve_subindex_offsets
713
714 !> @brief Row -> resolved feature index for an item whose local
715 !! position comes from a subindex field, via the offset table.
716 !<
717 function resolve_subindex_address(this, id_mf6varname, head_mf6varname, &
718 subindex_icol, offsets, nfeatures, nbound) &
719 result(row_addr)
720 use simmodule, only: store_error
721 use simvariablesmodule, only: errmsg
722 class(keystringloadtype), intent(inout) :: this
723 character(len=*), intent(in) :: id_mf6varname
724 character(len=*), intent(in) :: head_mf6varname
725 integer(I4B), intent(in) :: subindex_icol !< SA column holding the subindex (e.g. IDV)
726 integer(I4B), dimension(:), intent(in) :: offsets
727 integer(I4B), intent(in) :: nfeatures
728 integer(I4B), intent(in) :: nbound
729 integer(I4B), dimension(:), allocatable :: row_addr
730 integer(I4B), dimension(:), pointer, contiguous :: period_ifno => null()
731 type(characterstringtype), dimension(:), pointer, contiguous :: &
732 period_setting => null()
733 integer(I4B) :: i, ifno, idx_local, nlocal
734 character(len=LINELENGTH) :: setting
735 character(len=LENVARNAME) :: id_tag, idx_tag
736
737 call mem_setptr(period_ifno, id_mf6varname, this%mf6_input%mempath)
738 call mem_setptr(period_setting, 'SETTING', this%mf6_input%mempath)
739 id_tag = trim(this%structarray%struct_vectors(1)%idt%tagname)
740 idx_tag = trim(this%structarray%struct_vectors(subindex_icol)%idt%tagname)
741 allocate (row_addr(nbound))
742 do i = 1, nbound
743 row_addr(i) = 0
744 setting = period_setting(i)
745 if (trim(setting) /= trim(head_mf6varname)) cycle
746 ifno = period_ifno(i)
747 if (ifno < 1 .or. ifno > size(offsets)) cycle
748 ! per-id local count from the cumulative offset table
749 if (ifno < size(offsets)) then
750 nlocal = offsets(ifno + 1) - offsets(ifno)
751 else
752 nlocal = nfeatures - offsets(ifno) + 1
753 end if
754 idx_local = this%structarray%struct_vectors(subindex_icol)%int1d(i)
755 if (idx_local < 1 .or. idx_local > nlocal) then
756 write (errmsg, '(a,1x,a,1x,i0,1x,a,1x,i0,1x,a,1x,a,1x,i0,a)') &
757 'index', trim(idx_tag), idx_local, 'must be between 1 and', nlocal, &
758 'for', trim(id_tag), ifno, '.'
759 call store_error(errmsg)
760 cycle
761 end if
762 row_addr(i) = offsets(ifno) + idx_local - 1
763 end do
764 end function resolve_subindex_address
765
766 subroutine reset(this)
770 class(keystringloadtype), intent(inout) :: this
771 type(structarraytype), pointer :: sa
772 type(characterstringtype), dimension(:), pointer, contiguous :: &
773 auxnames => null()
774 integer(I4B) :: n, naux
775 ! every KEYSTRING subtype with SETTING dispatch: PERIOD settings
776 ! persist across periods unless reissued, so TS links never reset
777 if (this%ctx%has_setting_dispatch) return
778 ! clear TS links
779 call this%tsmanager%reset(this%mf6_input%subcomponent_name)
780 ! re-register static TS links (strlocs preserved in df)
781 if (this%ts_active) then
782 call get_isize('AUXILIARY', this%mf6_input%mempath, naux)
783 if (naux > 0) call mem_setptr(auxnames, 'AUXILIARY', this%mf6_input%mempath)
784 do n = 1, this%static_loader%ts_sa_count()
785 sa => this%static_loader%get_ts_sa(n)
786 if (associated(sa)) then
787 call sa%ts_update(this%tsmanager, &
788 this%mf6_input%subcomponent_name, &
789 this%ctx%iprpak, this%input_name, &
790 clear_strlocs=.false., auxname_cst=auxnames)
791 end if
792 end do
793 end if
794 end subroutine reset
795
796 subroutine destroy(this)
797 class(keystringloadtype), intent(inout) :: this
798
799 call this%static_loader%cleanup()
800
801 call this%tsmanager%da()
802 deallocate (this%tsmanager)
803 nullify (this%tsmanager)
804
805 if (associated(this%structarray)) then
806 call destructstructarray(this%structarray)
807 end if
808
809 call this%ctx%destroy()
810 call this%DynamicPkgLoadType%destroy()
811 end subroutine destroy
812
813 subroutine create_structarray(this)
815 class(keystringloadtype), intent(inout) :: this
816 type(inputparamdefinitiontype), pointer :: idt
817 integer(I4B) :: icol, sa_icol, nrow_prealloc, nsub, padj
818 logical(LGP) :: has_setting
819
820 has_setting = this%ctx%has_setting_dispatch
821
822 ! use pre-allocated managed memory (maxbound = features * nkeystring_items);
823 ! fall back to deferred shape (-1) if maxbound is unavailable
824 if (associated(this%ctx%maxbound) .and. this%ctx%maxbound > 0) then
825 nrow_prealloc = this%ctx%maxbound
826 else
827 nrow_prealloc = -1
828 end if
829
830 ! SETTING column inserted at nleading+1 when has_setting
831 padj = 0
832 if (has_setting) padj = 1
833
834 if (has_setting .and. nrow_prealloc < 0) then
835 ! fallback for a genuinely unresolvable count (e.g. empty
836 ! PACKAGEDATA), when PACKAGEDATA/DIMENSIONS can't supply one
837 this%structarray => &
838 constructstructarray(this%mf6_input, this%nparam + padj, &
839 nrow_prealloc, 0, this%mf6_input%mempath, &
840 this%mf6_input%component_mempath, size_init=64)
841 else
842 this%structarray => &
843 constructstructarray(this%mf6_input, this%nparam + padj, &
844 nrow_prealloc, 0, this%mf6_input%mempath, &
845 this%mf6_input%component_mempath)
846 end if
847
848 ! create leading (pre-keystring) columns unchanged
849 do icol = 1, this%nleading
850 idt => get_param_definition_type(this%mf6_input%param_dfns, &
851 this%mf6_input%component_type, &
852 this%mf6_input%subcomponent_type, &
853 this%ctx%blockname, &
854 this%param_names(icol), this%input_name)
855 call this%structarray%mem_create_vector(icol, idt)
856 end do
857
858 ! create SETTING column (ctx owns setting_idt)
859 if (has_setting) then
860 sa_icol = this%nleading + 1
861 call this%structarray%mem_create_vector(sa_icol, this%ctx%setting_idt, &
862 charlen=lenvarname)
863 end if
864
865 ! create item columns
866 do icol = this%nleading + 1, this%nparam
867 sa_icol = icol + padj
868 idt => get_param_definition_type(this%mf6_input%param_dfns, &
869 this%mf6_input%component_type, &
870 this%mf6_input%subcomponent_type, &
871 this%ctx%blockname, &
872 this%param_names(icol), this%input_name)
873 ! nsub from descriptor: 0 = direct dispatch, N = KEYWORD compound with N body members
874 nsub = this%ctx%keystring_items(icol - this%nleading)%head_nbody
875 if (nsub > 0) then
876 ! metadata vector: no data allocated; body_start points to next SA col
877 call this%structarray%mem_create_metadata_vector(sa_icol, idt, &
878 sa_icol + 1, nsub)
879 else if (trim(idt%datatype) == 'STRING') then
880 ! string value columns (e.g. STATUS) stored at LENVARNAME
881 call this%structarray%mem_create_vector(sa_icol, idt, &
882 charlen=lenvarname)
883 else if (idt%datatype == 'DOUBLE' .and. &
884 (this%ctx%keystring_items(icol - this%nleading)%idm_managed .or. &
885 this%ctx%keystring_items(icol - this%nleading)%addr_mode == &
886 addr_subindex)) then
887 ! managed/subindex double: raw read array uses the suffixed input
888 ! name, leaving mf6varname for the permanent array the loader populates
889 call this%structarray%mem_create_vector(sa_icol, idt, &
890 varname=idm_input_varname(idt))
891 else
892 call this%structarray%mem_create_vector(sa_icol, idt)
893 end if
894 end do
895 end subroutine create_structarray
896
897end module mf6filekeystringmodule
This module contains the AsciiInputLoadTypeModule.
This module contains block parser methods.
integer(i4b) function getunit(this)
Get the unit number.
subroutine destroy(this)
This module contains simulation constants.
Definition Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition Constants.f90:45
real(dp), parameter dnodata
real no data constant
Definition Constants.f90:95
integer(i4b), parameter lenvarname
maximum length of a variable name
Definition Constants.f90:17
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
This module contains the DefinitionSelectModule.
type(inputparamdefinitiontype) function, pointer, public get_param_definition_type(input_definition_types, component_type, subcomponent_type, blockname, tagname, filename, found)
Return parameter definition.
integer(i4b) function, public get_node(ilay, irow, icol, nlay, nrow, ncol)
Get node number, given layer, row, and column indices for a structured grid. If any argument is inval...
Definition GeomUtil.f90:92
This module contains the Input Data Model Logger Module.
Definition IdmLogger.f90:7
subroutine, public idm_log_close(component, subcomponent, iout)
log the closing message
Definition IdmLogger.f90:56
subroutine, public idm_log_header(component, subcomponent, iout)
log a header message
Definition IdmLogger.f90:44
Input definition module.
This module defines variable data types.
Definition kind.f90:8
Load context for IDM generic dynamic loaders.
integer(i4b), parameter, public addr_feature
addressed by leading id (IFNO/BNDNO)
integer(i4b), parameter, public addr_subindex
addressed by (id, subindex)
logical(lgp) function, public is_advanced(mf6_input)
Return .true. if mf6_input is an advanced package.
subroutine destroy(this)
destroy input context object
integer(i4b), parameter, public addr_node
addressed by CELLID
This module contains the LoadMf6FileModule.
subroutine, public get_isize(name, mem_path, isize)
Get the number of elements for this variable.
Period block keystring-based input loader.
subroutine ainit(this, mf6_input, component_name, component_input_name, input_name, iperblock, parser, iout)
integer(i4b) function resolve_row_addr(this, item, irow, period_ifno, cellid, ndim, leading_tag)
Resolve one row's target index for an item, dispatching on addr_mode. Returns 0 (skip) if out of rang...
subroutine apply_items(this)
Apply IDM-managed feature- and node-addressed settings to their permanent arrays; each row's address ...
subroutine allocate_subindex(this)
Allocate ctxsubindex_dependency targets (subindex params excluded from allocate_items),...
subroutine allocate_permanent_array(this, name, nfeatures, init_value)
Allocate a permanent per-feature array named name with init_value, unless already allocated.
integer(i4b) function, dimension(:), allocatable resolve_subindex_offsets(this, dimname)
Per-feature cumulative offset table from an array-valued dimension, permuted into feature-index order...
subroutine apply_auxiliary(this)
Apply PERIOD AUXILIARY settings to the permanent AUX array. TS-linked rows resolve via the struct arr...
logical(lgp) function valid_address(this, ifno, nfeatures, ifno_tagname, row)
Validate ifno against nfeatures, storing an error keyed by ifno_tagname (the leading column's public ...
subroutine allocate_items(this)
Allocate a permanent array for every IDM-managed item, using the descriptor's per-item feature count ...
subroutine apply_subindex(this)
Apply PERIOD settings for ctxsubindex_dependency's targets, via ts_update_indexed – same mechanism as...
integer(i4b) function, dimension(:), allocatable resolve_subindex_address(this, id_mf6varname, head_mf6varname, subindex_icol, offsets, nfeatures, nbound)
Row -> resolved feature index for an item whose local position comes from a subindex field,...
subroutine create_structarray(this)
subroutine rp(this, parser)
This module contains the ModflowInputModule.
This module contains simulation methods.
Definition Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition Sim.f90:94
integer(i4b) function, public count_errors()
Return number of errors.
Definition Sim.f90:60
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
This module contains the StructArrayModule.
character(len=lenvarname) function, public idm_input_varname(idt)
Derive a TS-capable setting's raw input array name, distinct from its persistent array (allocated und...
logical(lgp) function, public is_auxval(idt)
True if idt is AUXVAL: its AUX index is resolved dynamically from a sibling AUXNAME,...
integer(i4b) function, public find_auxname_index(auxname, auxnames, naux)
Find the AUX array position matching auxname, 0 if none.
subroutine, public destructstructarray(struct_array)
destructor for a struct_array
type(structarraytype) function, pointer, public constructstructarray(mf6_input, ncol, nrow, blocknum, mempath, component_mempath, size_init)
constructor for a struct_array
This module contains the StructVectorModule.
logical function, public remove_existing_link(tsmanager, ii, jj, pkgname, auxorbnd, varname)
Remove an existing timeseries link if it is defined.
subroutine, public read_value_or_time_series_adv(textinput, ii, jj, bndelem, pkgname, auxorbnd, tsmanager, iprpak, varname)
Call this subroutine from advanced packages to define timeseries link for a variable (varName).
subroutine, public tsmanager_cr(this, iout, removetslinksoncompletion, extendtstoendofsimulation)
Create the tsmanager.
base abstract type for ascii source dynamic load
This class is used to store a single deferred-length character string. It was designed to work in an ...
Input parameter definition. Describes an input parameter.
One descriptor per expanded keystring item, so loaders iterate uniformly instead of re-deriving per-m...
Input load context for generic dynamic loaders and StructArray based static loads....
Static parser based input loader.
Wraps an allocatable int array for a per-column cache table.
derived type for storing input definition for a file
type for structured array
derived type which describes time series string field