Skip to content

Commit 93dabd1

Browse files
committed
idm SA deferred size updates
1 parent 8d1a53e commit 93dabd1

3 files changed

Lines changed: 87 additions & 21 deletions

File tree

src/Utilities/Idm/mf6blockfile/LoadMf6File.f90

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,18 @@ subroutine parse_structarray_block(this, iblk)
627627
nrows = -1
628628
end if
629629

630-
! create a structured array
631-
this%structarray => constructStructArray(this%mf6_input, ncol, nrows, &
632-
blocknum, this%mf6_input%mempath, &
633-
this%mf6_input%component_mempath)
630+
! create a structured array; use a larger deferred init for blocks with no
631+
! explicit shape, which include APT based advanced packages.
632+
if (nrows < 0) then
633+
this%structarray => constructStructArray(this%mf6_input, ncol, nrows, &
634+
blocknum, this%mf6_input%mempath, &
635+
this%mf6_input%component_mempath, &
636+
size_init=64)
637+
else
638+
this%structarray => constructStructArray(this%mf6_input, ncol, nrows, &
639+
blocknum, this%mf6_input%mempath, &
640+
this%mf6_input%component_mempath)
641+
end if
634642
! create structarray vectors for each column
635643
do icol = 1, ncol
636644
! if block is reloadable, block number is first column

src/Utilities/Idm/mf6blockfile/Mf6FileKeystring.f90

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,26 @@ end subroutine ainit
133133

134134
subroutine df(this)
135135
use StructArrayModule, only: StructArrayType
136+
use MemoryManagerModule, only: mem_setptr, get_isize
137+
use CharacterStringModule, only: CharacterStringType
136138
class(KeystringLoadType), intent(inout) :: this
137139
type(StructArrayType), pointer :: sa
138-
integer(I4B) :: n
140+
type(CharacterStringType), dimension(:), pointer, contiguous :: &
141+
auxnames => null()
142+
integer(I4B) :: n, naux
139143
! init tsmanager (TDIS now available)
140144
call this%tsmanager%tsmanager_df()
145+
! resolve aux names for PACKAGEDATA AUX TS registration
146+
call get_isize('AUXILIARY', this%mf6_input%mempath, naux)
147+
if (naux > 0) call mem_setptr(auxnames, 'AUXILIARY', this%mf6_input%mempath)
141148
! register static TS links; mark as static so they survive reset
142149
do n = 1, this%static_loader%ts_sa_count()
143150
sa => this%static_loader%get_ts_sa(n)
144151
if (associated(sa)) then
145152
call sa%ts_update(this%tsmanager, &
146153
this%mf6_input%subcomponent_name, &
147154
this%ctx%iprpak, this%input_name, &
148-
is_static=.true.)
155+
is_static=.true., auxname_cst=auxnames)
149156
end if
150157
end do
151158
end subroutine df
@@ -221,10 +228,18 @@ subroutine create_structarray(this)
221228
padj = 0
222229
if (this%ctx%is_advanced) padj = 1
223230

224-
this%structarray => &
225-
constructStructArray(this%mf6_input, this%nparam + padj, &
226-
nrow_prealloc, 0, this%mf6_input%mempath, &
227-
this%mf6_input%component_mempath)
231+
if (this%ctx%is_advanced .and. nrow_prealloc < 0) then
232+
! includes APT based advanced packages
233+
this%structarray => &
234+
constructStructArray(this%mf6_input, this%nparam + padj, &
235+
nrow_prealloc, 0, this%mf6_input%mempath, &
236+
this%mf6_input%component_mempath, size_init=64)
237+
else
238+
this%structarray => &
239+
constructStructArray(this%mf6_input, this%nparam + padj, &
240+
nrow_prealloc, 0, this%mf6_input%mempath, &
241+
this%mf6_input%component_mempath)
242+
end if
228243

229244
! create leading (pre-keystring) columns unchanged
230245
do icol = 1, this%nleading

src/Utilities/Idm/mf6blockfile/StructArray.f90

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ module StructArrayModule
7777
!> @brief constructor for a struct_array
7878
!<
7979
function constructStructArray(mf6_input, ncol, nrow, blocknum, mempath, &
80-
component_mempath) result(struct_array)
80+
component_mempath, size_init) result(struct_array)
8181
type(ModflowInputType), intent(in) :: mf6_input
8282
integer(I4B), intent(in) :: ncol !< number of columns in the StructArrayType
8383
integer(I4B), intent(in) :: nrow !< number of rows in the StructArrayType
8484
integer(I4B), intent(in) :: blocknum !< valid block number or 0
8585
character(len=*), intent(in) :: mempath !< memory path for storing the vector
8686
character(len=*), intent(in) :: component_mempath
87+
integer(I4B), optional, intent(in) :: size_init !< initial deferred allocation size (default 5)
8788
type(StructArrayType), pointer :: struct_array !< new StructArrayType
8889

8990
! allocate StructArrayType
@@ -100,6 +101,7 @@ function constructStructArray(mf6_input, ncol, nrow, blocknum, mempath, &
100101
if (struct_array%nrow == -1) then
101102
struct_array%nrow = 0
102103
struct_array%deferred_shape = .true.
104+
if (present(size_init)) struct_array%deferred_size_init = size_init
103105
end if
104106

105107
! set blocknum
@@ -397,10 +399,16 @@ subroutine allocate_dbl1d_type(this, sv)
397399

398400
if (sv%idt%shape == 'NAUX') then
399401
call mem_setptr(naux, sv%idt%shape, this%mempath)
400-
call mem_allocate(dbl2d, naux, this%nrow, sv%idt%mf6varname, this%mempath)
402+
403+
if (this%deferred_shape) then
404+
! deferred: plain allocate so check_reallocate can grow it safely
405+
allocate (dbl2d(naux, sv%size))
406+
else
407+
call mem_allocate(dbl2d, naux, this%nrow, sv%idt%mf6varname, this%mempath)
408+
end if
401409

402410
! initialize
403-
do m = 1, this%nrow
411+
do m = 1, sv%size
404412
do n = 1, naux
405413
dbl2d(n, m) = DZERO
406414
end do
@@ -420,11 +428,11 @@ subroutine allocate_dbl1d_type(this, sv)
420428
call mem_setptr(nseg_1, 'NSEG_1', this%mempath)
421429
end if
422430

423-
! allocate
424-
call mem_allocate(dbl2d, nseg_1, this%nrow, sv%idt%mf6varname, this%mempath)
431+
! allocate; use sv%size for deferred-compatible initial row count
432+
call mem_allocate(dbl2d, nseg_1, sv%size, sv%idt%mf6varname, this%mempath)
425433

426434
! initialize
427-
do m = 1, this%nrow
435+
do m = 1, sv%size
428436
do n = 1, nseg_1
429437
dbl2d(n, m) = DZERO
430438
end do
@@ -448,6 +456,7 @@ subroutine load_deferred_vector(this, icol)
448456
integer(I4B), dimension(:), pointer, contiguous :: p_int1d
449457
integer(I4B), dimension(:, :), pointer, contiguous :: p_int2d
450458
real(DP), dimension(:), pointer, contiguous :: p_dbl1d
459+
real(DP), dimension(:, :), pointer, contiguous :: p_dbl2d
451460
type(CharacterStringType), dimension(:), pointer, contiguous :: p_charstr1d
452461
character(len=LENVARNAME) :: varname
453462
logical(LGP) :: overwrite
@@ -612,9 +621,24 @@ subroutine load_deferred_vector(this, icol)
612621
this%struct_vectors(icol)%int2d => p_int2d
613622
this%struct_vectors(icol)%size = this%nrow
614623
case (MTYPE_DBL2D)
615-
errmsg = 'StructArray::load_deferred_vector &
616-
&dbl2d reallocate unimplemented.'
617-
call store_error(errmsg, terminate=.TRUE.)
624+
if (isize > -1) then
625+
errmsg = 'StructArray::load_deferred_vector &
626+
&dbl2d reallocate unimplemented.'
627+
call store_error(errmsg, terminate=.TRUE.)
628+
else
629+
call mem_allocate(p_dbl2d, this%struct_vectors(icol)%intshape, &
630+
this%nrow, varname, this%mempath)
631+
do i = 1, this%nrow
632+
do j = 1, this%struct_vectors(icol)%intshape
633+
p_dbl2d(j, i) = this%struct_vectors(icol)%dbl2d(j, i)
634+
end do
635+
end do
636+
end if
637+
638+
deallocate (this%struct_vectors(icol)%dbl2d)
639+
640+
this%struct_vectors(icol)%dbl2d => p_dbl2d
641+
this%struct_vectors(icol)%size = this%nrow
618642
case default
619643
end select
620644
end subroutine load_deferred_vector
@@ -717,6 +741,7 @@ subroutine check_reallocate(this)
717741
integer(I4B), dimension(:), pointer, contiguous :: p_int1d
718742
integer(I4B), dimension(:, :), pointer, contiguous :: p_int2d
719743
real(DP), dimension(:), pointer, contiguous :: p_dbl1d
744+
real(DP), dimension(:, :), pointer, contiguous :: p_dbl2d
720745
type(CharacterStringType), dimension(:), pointer, contiguous :: p_charstr1d
721746
integer(I4B) :: reallocate_mult
722747

@@ -792,7 +817,24 @@ subroutine check_reallocate(this)
792817
this%struct_vectors(j)%int2d => p_int2d
793818
this%struct_vectors(j)%size = newsize
794819
end if
795-
! TODO: case (6)
820+
case (MTYPE_DBL2D)
821+
if (this%nrow > this%struct_vectors(j)%size) then
822+
newsize = this%struct_vectors(j)%size * reallocate_mult
823+
allocate (p_dbl2d(this%struct_vectors(j)%intshape, newsize))
824+
825+
do i = 1, this%struct_vectors(j)%size
826+
do k = 1, this%struct_vectors(j)%intshape
827+
p_dbl2d(k, i) = this%struct_vectors(j)%dbl2d(k, i)
828+
end do
829+
end do
830+
831+
deallocate (this%struct_vectors(j)%dbl2d)
832+
833+
this%struct_vectors(j)%dbl2d => p_dbl2d
834+
this%struct_vectors(j)%size = newsize
835+
end if
836+
case (MTYPE_UNDEF, MTYPE_INTVEC)
837+
! metadata-only or unsupported: skip reallocation check
796838
case default
797839
errmsg = 'IDM unimplemented. StructArray::check_reallocate &
798840
&unsupported memtype.'
@@ -1222,8 +1264,9 @@ subroutine ts_update(this, tsmanager, subcomp_name, iprpak, input_name, &
12221264
logical(LGP) :: do_clear, do_static
12231265

12241266
do_clear = .true.
1225-
if (present(clear_strlocs)) do_clear = clear_strlocs
12261267
do_static = .false.
1268+
1269+
if (present(clear_strlocs)) do_clear = clear_strlocs
12271270
if (present(is_static)) do_static = is_static
12281271

12291272
! find BOUNDNAME column (0 = none)

0 commit comments

Comments
 (0)