MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
SourceCommon.f90
Go to the documentation of this file.
1!> @brief This module contains the SourceCommonModule
2!!
3!! This module contains source independent input
4!! processing helper routines.
5!!
6!<
8
9 use kindmodule, only: dp, i4b, lgp
10 use simvariablesmodule, only: errmsg
14
15 implicit none
16 private
17 public :: package_source_type
19 public :: idm_pkg_instance_name
20 public :: idm_utl_type
21 public :: set_model_shape
22 public :: get_shape_from_string
23 public :: get_layered_shape
24 public :: file_ext
25 public :: ifind_charstr
26 public :: filein_fname
27 public :: inlen_check
28
29contains
30
31 !> @brief source identifier from model namfile FNAME array
32 !!
33 !! Return the source type for a package listed in the
34 !! model nam file packages block FNAME field.
35 !!
36 !<
37 function package_source_type(sourcename) result(sourcetype)
38 use inputoutputmodule, only: upcase
39 character(len=*), intent(in) :: sourcename
40 character(len=LENPACKAGENAME) :: sourcetype
41 character(len=LENPACKAGENAME) :: ext
42 ext = file_ext(sourcename)
43 select case (ext)
44 case default
45 sourcetype = 'MF6FILE'
46 end select
47 end function package_source_type
48
49 !> @brief component from package or model type
50 !!
51 !! Return the component type typically derived from package file type,
52 !! i.e. return GWF when input is GWF6. This function checks the
53 !! resultant component type and throws a terminating error if not
54 !! supported by IDM in some capacity.
55 !!
56 !<
57 function idm_component_type(component) result(component_type)
59 character(len=*), intent(in) :: component
60 character(len=LENCOMPONENTNAME) :: component_type
61 integer(I4B) :: i, ilen, idx
62
63 ! initialize
64 component_type = ''
65 idx = 0
66
67 ilen = len_trim(component)
68 do i = 1, ilen
69 if (component(i:i) == '6' .or. component(i:i) == '-') then
70 else
71 idx = idx + 1
72 component_type(idx:idx) = component(i:i)
73 end if
74 end do
75
76 if (.not. idm_component(component_type)) then
77 write (errmsg, '(a)') &
78 'IDP input error, unrecognized component: "'//trim(component)//'"'
79 call store_error(errmsg, .true.)
80 end if
81 end function idm_component_type
82
83 !> @brief component from package or model type
84 !!
85 !! Return the subcomponent type typically derived from package file type,
86 !! i.e. return CHD when input is CHD6. Note this function is called on
87 !! file types that are both idm integrated and not and should not set
88 !! an error based on this difference.
89 !!
90 !<
91 function idm_subcomponent_type(component, subcomponent) &
92 result(subcomponent_type)
93 character(len=*), intent(in) :: component !< component, e.g. GWF6
94 character(len=*), intent(in) :: subcomponent !< subcomponent, e.g. CHD6
95 character(len=LENCOMPONENTNAME) :: subcomponent_type
96 character(len=LENCOMPONENTNAME) :: component_type
97 integer(I4B) :: i, ilen, idx
98
99 ! initialize
100 subcomponent_type = ''
101 idx = 0
102
103 ! verify component
104 component_type = idm_component_type(component)
105
106 ilen = len_trim(subcomponent)
107 do i = 1, ilen
108 if (subcomponent(i:i) == '6' .or. subcomponent(i:i) == '-') then
109 else
110 idx = idx + 1
111 subcomponent_type(idx:idx) = subcomponent(i:i)
112 end if
113 end do
114 end function idm_subcomponent_type
115
116 !> @brief model package subcomponent name
117 !!
118 !! Return the IDM component name, which is the package type for
119 !! base packages and the package name for multi package (i.e.
120 !! stress) types.
121 !!
122 !<
123 function idm_subcomponent_name(component_type, subcomponent_type, sc_name) &
124 result(subcomponent_name)
126 character(len=*), intent(in) :: component_type
127 character(len=*), intent(in) :: subcomponent_type
128 character(len=*), intent(in) :: sc_name
129 character(len=LENPACKAGENAME) :: subcomponent_name
130 subcomponent_name = ''
131 if (idm_utl_type(component_type, subcomponent_type) .or. &
132 idm_multi_package(component_type, subcomponent_type)) then
133 subcomponent_name = sc_name
134 else
135 subcomponent_name = subcomponent_type
136 end if
137 end function idm_subcomponent_name
138
139 !> @brief default name for a multi-package instance
140 !!
141 !! Return the default '<TYPE>-<N>' name for the Nth instance of a
142 !! multi-package type, where N is the 1-based count of packages of
143 !! this type encountered in an in-order traversal of the model
144 !! namefile packages block. This is the IDM internal package
145 !! naming convention when user names are not provided.
146 !!
147 !<
148 function idm_pkg_instance_name(pkg_type, inst) result(sc_name)
149 character(len=*), intent(in) :: pkg_type
150 integer(I4B), intent(in) :: inst
151 character(len=LENPACKAGENAME) :: sc_name
152 write (sc_name, '(a,i0)') trim(pkg_type)//'-', inst
153 end function idm_pkg_instance_name
154
155 !> @brief is utility type
156 !!
157 !! Is this subcomponent type an idm integrated utility type.
158 !!
159 !<
160 function idm_utl_type(component, subcomponent) &
161 result(utl_type)
163 character(len=*), intent(in) :: component
164 character(len=*), intent(in) :: subcomponent !< subcomponent string, e.g. TVK, TVK6, or NPF-TVK1
165 character(len=LENCOMPONENTNAME) :: subcomponent_type
166 logical(LGP) :: utl_type
167 integer(I4B) :: ilen, idx
168 ilen = len_trim(subcomponent)
169 subcomponent_type = ''
170 idx = index(subcomponent(1:ilen), '-')
171 if (idx > 0) then
172 ! strip '-N' instance suffix (e.g. NPF-TVK1 -> TVK1, further strip below)
173 subcomponent_type = subcomponent(1:idx - 1)
174 else if (ilen > 0 .and. subcomponent(ilen:ilen) == '6') then
175 ! strip trailing '6' package-type suffix (e.g. TVK6 -> TVK)
176 subcomponent_type = subcomponent(1:ilen - 1)
177 else
178 subcomponent_type = subcomponent(1:ilen)
179 end if
180 utl_type = idm_integrated('UTL', subcomponent_type)
181 end function idm_utl_type
182
183 !> @brief input file extension
184 !!
185 !! Return a file extension, or an empty string if
186 !! not identified.
187 !!
188 !<
189 function file_ext(filename) result(ext)
191 character(len=*), intent(in) :: filename
192 character(len=LENPACKAGETYPE) :: ext
193 integer(I4B) :: idx
194 ! initialize
195 ext = ''
196 idx = 0
197 ! identify '.' character position from back of string
198 idx = index(filename, '.', back=.true.)
199 if (idx > 0) then
200 ext = filename(idx + 1:len_trim(filename))
201 end if
202 end function file_ext
203
204 subroutine get_shape_from_string(shape_string, array_shape, memoryPath)
207 character(len=*), intent(in) :: shape_string
208 integer(I4B), dimension(:), allocatable, intent(inout) :: array_shape
209 character(len=*), intent(in) :: memorypath !< memorypath to put loaded information
210 integer(I4B) :: ndim
211 integer(I4B) :: i
212 integer(I4B), pointer :: int_ptr
213 character(len=16), dimension(:), allocatable :: array_shape_string
214 character(len=:), allocatable :: shape_string_copy
215
216 ! parse the string into multiple words
217 shape_string_copy = trim(shape_string)//' '
218 call parseline(shape_string_copy, ndim, array_shape_string)
219 allocate (array_shape(ndim))
220
221 ! find shape in memory manager and put into array_shape
222 do i = 1, ndim
223 call mem_setptr(int_ptr, array_shape_string(i), memorypath)
224 array_shape(i) = int_ptr
225 end do
226 end subroutine get_shape_from_string
227
228 subroutine get_layered_shape(mshape, nlay, layer_shape)
229 integer(I4B), dimension(:), intent(in) :: mshape
230 integer(I4B), intent(out) :: nlay
231 integer(I4B), dimension(:), allocatable, intent(out) :: layer_shape
232 integer(I4B) :: ndim
233
234 ndim = size(mshape)
235 nlay = 0
236
237 if (ndim == 1) then ! disu
238 nlay = 1
239 allocate (layer_shape(1))
240 layer_shape(1) = mshape(1)
241 else if (ndim == 2) then ! disv
242 nlay = mshape(1)
243 allocate (layer_shape(1))
244 layer_shape(1) = mshape(2)
245 else if (ndim == 3) then ! disu
246 nlay = mshape(1)
247 allocate (layer_shape(2))
248 layer_shape(1) = mshape(3) ! ncol
249 layer_shape(2) = mshape(2) ! nrow
250 end if
251 end subroutine get_layered_shape
252
253 !> @brief routine for setting the model shape
254 !!
255 !! The model shape must be set in the memory manager because
256 !! individual packages need to know the shape of the arrays
257 !! to read.
258 !!
259 !<
260 subroutine set_model_shape(ftype, fname, model_mempath, dis_mempath, &
261 model_shape)
262 use constantsmodule, only: disundef, dis, disv, disu, dis2d, disv1d, &
263 disv2d
265 character(len=*), intent(in) :: ftype
266 character(len=*), intent(in) :: fname
267 character(len=*), intent(in) :: model_mempath
268 character(len=*), intent(in) :: dis_mempath
269 integer(I4B), dimension(:), pointer, contiguous, intent(inout) :: model_shape
270 integer(I4B), pointer :: ndim1
271 integer(I4B), pointer :: ndim2
272 integer(I4B), pointer :: ndim3
273 integer(I4B), pointer :: ncelldim
274 integer(I4B), pointer :: distype
275 integer(I4B) :: dim1_size, dim2_size, dim3_size, dis_type
276
277 ! initialize dis_type
278 dis_type = disundef
279
280 ! allocate and set model shape in model input context
281 select case (ftype)
282 case ('DIS6')
283 ! set dis_type
284 dis_type = dis
285 call get_isize('NLAY', dis_mempath, dim1_size)
286 call get_isize('NROW', dis_mempath, dim2_size)
287 call get_isize('NCOL', dis_mempath, dim3_size)
288
289 if (dim1_size <= 0) then
290 write (errmsg, '(a)') &
291 'Required input dimension "NLAY" not found.'
292 call store_error(errmsg)
293 end if
294
295 if (dim2_size <= 0) then
296 write (errmsg, '(a)') &
297 'Required input dimension "NROW" not found.'
298 call store_error(errmsg)
299 end if
300
301 if (dim3_size <= 0) then
302 write (errmsg, '(a)') &
303 'Required input dimension "NCOL" not found.'
304 call store_error(errmsg)
305 end if
306
307 if (dim1_size >= 1 .and. dim2_size >= 1 .and. dim3_size >= 1) then
308 call mem_allocate(model_shape, 3, 'MODEL_SHAPE', model_mempath)
309 call mem_setptr(ndim1, 'NLAY', dis_mempath)
310 call mem_setptr(ndim2, 'NROW', dis_mempath)
311 call mem_setptr(ndim3, 'NCOL', dis_mempath)
312 model_shape = [ndim1, ndim2, ndim3]
313 else
314 call store_error_filename(fname)
315 end if
316 case ('DIS2D6')
317 ! set dis_type
318 dis_type = dis2d
319 call get_isize('NROW', dis_mempath, dim1_size)
320 call get_isize('NCOL', dis_mempath, dim2_size)
321
322 if (dim1_size <= 0) then
323 write (errmsg, '(a)') &
324 'Required input dimension "NROW" not found.'
325 call store_error(errmsg)
326 end if
327
328 if (dim2_size <= 0) then
329 write (errmsg, '(a)') &
330 'Required input dimension "NCOL" not found.'
331 call store_error(errmsg)
332 end if
333
334 if (dim1_size >= 1 .and. dim2_size >= 1) then
335 call mem_allocate(model_shape, 2, 'MODEL_SHAPE', model_mempath)
336 call mem_setptr(ndim1, 'NROW', dis_mempath)
337 call mem_setptr(ndim2, 'NCOL', dis_mempath)
338 model_shape = [ndim1, ndim2]
339 else
340 call store_error_filename(fname)
341 end if
342 case ('DISV6')
343 ! set dis_type
344 dis_type = disv
345 call get_isize('NLAY', dis_mempath, dim1_size)
346 call get_isize('NCPL', dis_mempath, dim2_size)
347
348 if (dim1_size <= 0) then
349 write (errmsg, '(a)') &
350 'Required input dimension "NLAY" not found.'
351 call store_error(errmsg)
352 end if
353
354 if (dim2_size <= 0) then
355 write (errmsg, '(a)') &
356 'Required input dimension "NCPL" not found.'
357 call store_error(errmsg)
358 end if
359
360 if (dim1_size >= 1 .and. dim2_size >= 1) then
361 call mem_allocate(model_shape, 2, 'MODEL_SHAPE', model_mempath)
362 call mem_setptr(ndim1, 'NLAY', dis_mempath)
363 call mem_setptr(ndim2, 'NCPL', dis_mempath)
364 model_shape = [ndim1, ndim2]
365 else
366 call store_error_filename(fname)
367 end if
368 case ('DISV2D6')
369 ! set dis_type
370 dis_type = disv2d
371 call get_isize('NODES', dis_mempath, dim1_size)
372
373 if (dim1_size <= 0) then
374 write (errmsg, '(a)') &
375 'Required input dimension "NODES" not found.'
376 call store_error(errmsg)
377 end if
378
379 if (dim1_size >= 1) then
380 call mem_allocate(model_shape, 1, 'MODEL_SHAPE', model_mempath)
381 call mem_setptr(ndim1, 'NODES', dis_mempath)
382 model_shape = [ndim1]
383 else
384 call store_error_filename(fname)
385 end if
386 case ('DISU6', 'DISV1D6')
387 ! set dis_type
388 if (ftype == 'DISU6') then
389 dis_type = disu
390 else if (ftype == 'DISV1D6') then
391 dis_type = disv1d
392 end if
393
394 call get_isize('NODES', dis_mempath, dim1_size)
395
396 if (dim1_size <= 0) then
397 write (errmsg, '(a)') &
398 'Required input dimension "NODES" not found.'
399 call store_error(errmsg)
400 call store_error_filename(fname)
401 end if
402
403 call mem_allocate(model_shape, 1, 'MODEL_SHAPE', model_mempath)
404 call mem_setptr(ndim1, 'NODES', dis_mempath)
405 model_shape = [ndim1]
406 case default
407 errmsg = 'Unknown discretization type. IDM cannot set shape for "' &
408 //trim(ftype)//"'"
409 call store_error(errmsg)
410 call store_error_filename(fname)
411 end select
412
413 ! allocate and set ncelldim in model input context
414 call mem_allocate(ncelldim, 'NCELLDIM', model_mempath)
415 ncelldim = size(model_shape)
416
417 ! allocate and set distype in model input context
418 call mem_allocate(distype, 'DISENUM', model_mempath)
419 distype = dis_type
420 end subroutine set_model_shape
421
422 function ifind_charstr(array, str)
424 implicit none
425 integer(I4B) :: ifind_charstr
426 type(characterstringtype), dimension(:), intent(in) :: array
427 character(len=*) :: str
428 character(len=LINELENGTH) :: compare_str
429 integer(I4B) :: i
430 ifind_charstr = -1
431 findloop: do i = 1, size(array)
432 compare_str = array(i)
433 if (compare_str == str) then
434 ifind_charstr = i
435 exit findloop
436 end if
437 end do findloop
438 end function ifind_charstr
439
440 !> @brief enforce and set a single input filename provided via FILEIN keyword
441 !!
442 !! Set a FILEIN filename provided via an OPTIONS block.
443 !! Only use this function if a maximum of one FILEIN file name
444 !! string is expected.
445 !!
446 !! Return true if single FILEIN file name found and set, return
447 !! false if FILEIN tag not found.
448 !!
449 !<
450 function filein_fname(filename, tagname, input_mempath, input_fname) &
451 result(found)
454 character(len=*), intent(inout) :: filename
455 character(len=*), intent(in) :: tagname
456 character(len=*), intent(in) :: input_mempath
457 character(len=*), intent(in) :: input_fname
458 logical(LGP) :: found
459 type(characterstringtype), dimension(:), pointer, &
460 contiguous :: fnames
461 integer(I4B) :: isize
462
463 ! initialize
464 found = .false.
465 filename = ''
466
467 call get_isize(tagname, input_mempath, isize)
468
469 if (isize > 0) then
470 if (isize /= 1) then
471 errmsg = 'Multiple FILEIN keywords detected for tag "'//trim(tagname)// &
472 '" in OPTIONS block. Only one entry allowed.'
473 call store_error(errmsg)
474 call store_error_filename(input_fname)
475 end if
476
477 call mem_setptr(fnames, tagname, input_mempath)
478 filename = fnames(1)
479 found = .true.
480 end if
481 end function filein_fname
482
483 !> @brief store an error for input exceeding internal name length
484 !<
485 subroutine inlen_check(input_name, mf6_name, maxlen, name_type)
487 type(characterstringtype), intent(in) :: input_name
488 character(len=*), intent(inout) :: mf6_name
489 integer(I4B), intent(in) :: maxlen
490 character(len=*), intent(in) :: name_type
491 character(len=LINELENGTH) :: input_str
492 integer(I4B) :: ilen
493
494 ! initialize
495 mf6_name = ''
496 input_str = input_name
497 ilen = len_trim(input_str)
498 if (ilen > maxlen) then
499 write (errmsg, '(a,i0,a)') &
500 'Input name "'//trim(input_str)//'" exceeds maximum allowed length (', &
501 maxlen, ') for '//trim(name_type)//'.'
502 call store_error(errmsg)
503 end if
504
505 ! set truncated name
506 mf6_name = trim(input_str)
507 end subroutine inlen_check
508
509end module sourcecommonmodule
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 lencomponentname
maximum length of a component name
Definition Constants.f90:18
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
@ disu
DISV6 discretization.
@ dis
DIS6 discretization.
@ disv2d
DISV2D6 discretization.
@ disv1d
DISV1D6 discretization.
@ dis2d
DIS2D6 discretization.
@ disv
DISU6 discretization.
@ disundef
undefined discretization
logical function, public idm_integrated(component, subcomponent)
logical function, public idm_component(component)
logical function, public idm_multi_package(component, subcomponent)
subroutine, public parseline(line, nwords, words, inunit, filename)
Parse a line into words.
subroutine, public upcase(word)
Convert to upper case.
This module defines variable data types.
Definition kind.f90:8
subroutine, public get_isize(name, mem_path, isize)
Get the number of elements for this variable.
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
This module contains the SourceCommonModule.
character(len=lenpackagename) function, public package_source_type(sourcename)
source identifier from model namfile FNAME array
subroutine, public get_layered_shape(mshape, nlay, layer_shape)
character(len=lencomponentname) function, public idm_subcomponent_type(component, subcomponent)
component from package or model type
character(len=lenpackagename) function, public idm_pkg_instance_name(pkg_type, inst)
default name for a multi-package instance
subroutine, public inlen_check(input_name, mf6_name, maxlen, name_type)
store an error for input exceeding internal name length
logical(lgp) function, public idm_utl_type(component, subcomponent)
is utility type
subroutine, public set_model_shape(ftype, fname, model_mempath, dis_mempath, model_shape)
routine for setting the model shape
character(len=lencomponentname) function, public idm_component_type(component)
component from package or model type
character(len=lenpackagetype) function, public file_ext(filename)
input file extension
integer(i4b) function, public ifind_charstr(array, str)
character(len=lenpackagename) function, public idm_subcomponent_name(component_type, subcomponent_type, sc_name)
model package subcomponent name
subroutine, public get_shape_from_string(shape_string, array_shape, memorypath)
logical(lgp) function, public filein_fname(filename, tagname, input_mempath, input_fname)
enforce and set a single input filename provided via FILEIN keyword
This class is used to store a single deferred-length character string. It was designed to work in an ...