Skip to content

Commit

Permalink
refactor(idm): verify and store error based on length of user input n…
Browse files Browse the repository at this point in the history
…ames (#1775)

* check input length of model,exg,pacakge names

* delay model package name error until all names checked

---------

Co-authored-by: mjreno <mjreno@IGSAAA071L01144.gs.doi.net>
  • Loading branch information
mjreno and mjreno committed May 23, 2024
1 parent bb4a89a commit def73f5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
24 changes: 17 additions & 7 deletions src/Utilities/Idm/IdmLoad.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module IdmLoadModule
use SimVariablesModule, only: errmsg
use ConstantsModule, only: LINELENGTH, LENMEMPATH, LENMODELNAME, &
LENEXCHANGENAME, LENCOMPONENTNAME
use SimModule, only: store_error, store_error_filename
use SimModule, only: store_error, count_errors, store_error_filename
use ListModule, only: ListType
use InputLoadTypeModule, only: StaticPkgLoadBaseType, &
DynamicPkgLoadBaseType, &
Expand Down Expand Up @@ -213,10 +213,10 @@ subroutine load_models(iout)
use MemoryHelperModule, only: create_mem_path
use MemoryManagerModule, only: mem_setptr
use CharacterStringModule, only: CharacterStringType
use SimVariablesModule, only: idm_context
use DistributedSimModule, only: DistributedSimType, get_dsim
use SimVariablesModule, only: idm_context, simfile
use ModelPackageInputsModule, only: ModelPackageInputsType
use SourceCommonModule, only: idm_component_type
use SourceCommonModule, only: idm_component_type, inlen_check
use SourceLoadModule, only: load_modelnam
! -- dummy
integer(I4B), intent(in) :: iout
Expand Down Expand Up @@ -252,7 +252,12 @@ subroutine load_models(iout)
! -- attributes for this model
mtype = mtypes(n)
mfname = mfnames(n)
mname = mnames(n)
call inlen_check(mnames(n), mname, LENMODELNAME, 'MODELNAME')
!
! -- terminate if errors were detected
if (count_errors() > 0) then
call store_error_filename(simfile)
end if
!
! -- load specified model inputs
if (model_loadmask(n) > 0) then
Expand Down Expand Up @@ -290,7 +295,8 @@ subroutine load_exchanges(iout)
use CharacterStringModule, only: CharacterStringType
use SimVariablesModule, only: idm_context, simfile
use DistributedSimModule, only: DistributedSimType, get_dsim
use SourceCommonModule, only: idm_subcomponent_type, ifind_charstr
use SourceCommonModule, only: idm_subcomponent_type, ifind_charstr, &
inlen_check
use SourceLoadModule, only: create_input_loader, remote_model_ndim
! -- dummy
integer(I4B), intent(in) :: iout
Expand Down Expand Up @@ -348,8 +354,8 @@ subroutine load_exchanges(iout)
! -- attributes for this exchange
exgtype = etypes(n)
efname = efiles(n)
mname1 = emnames_a(n)
mname2 = emnames_b(n)
call inlen_check(emnames_a(n), mname1, LENMODELNAME, 'MODELNAME')
call inlen_check(emnames_b(n), mname2, LENMODELNAME, 'MODELNAME')
!
! initialize mempath as no path
emempaths(n) = ''
Expand All @@ -364,6 +370,10 @@ subroutine load_exchanges(iout)
if (m1_idx <= 0) errmsg = trim(errmsg)//' '//trim(mname1)
if (m2_idx <= 0) errmsg = trim(errmsg)//' '//trim(mname2)
call store_error(errmsg)
end if
!
! -- terminate if errors were detected
if (count_errors() > 0) then
call store_error_filename(simfile)
end if
!
Expand Down
10 changes: 8 additions & 2 deletions src/Utilities/Idm/ModelPackageInputs.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ModelPackageInputsModule
use SimVariablesModule, only: errmsg
use ConstantsModule, only: LINELENGTH, LENMEMPATH, LENMODELNAME, LENFTYPE, &
LENPACKAGETYPE, LENPACKAGENAME, LENCOMPONENTNAME
use SimModule, only: store_error, store_error_filename
use SimModule, only: store_error, count_errors, store_error_filename
use SimVariablesModule, only: iout
use ArrayHandlersModule, only: expandarray
use CharacterStringModule, only: CharacterStringType
Expand Down Expand Up @@ -384,6 +384,7 @@ end subroutine modelpkgs_add
subroutine modelpkgs_addpkgs(this)
! -- modules
use MemoryManagerModule, only: mem_setptr
use SourceCommonModule, only: inlen_check
! -- dummy
class(ModelPackageInputsType) :: this
! -- local
Expand All @@ -410,12 +411,17 @@ subroutine modelpkgs_addpkgs(this)
! -- attributes for this package
ftype = ftypes(n)
fname = fnames(n)
pname = pnames(n)
call inlen_check(pnames(n), pname, LENPACKAGENAME, 'PACKAGENAME')
!
! -- add this instance to package list
call this%add(ftype, fname, pname)
end do
!
! -- terminate if errors were detected
if (count_errors() > 0) then
call store_error_filename(this%modelfname)
end if
!
! --
return
end subroutine modelpkgs_addpkgs
Expand Down
31 changes: 30 additions & 1 deletion src/Utilities/Idm/SourceCommon.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module SourceCommonModule
public :: file_ext
public :: ifind_charstr
public :: filein_fname
public :: inlen_check

contains

Expand Down Expand Up @@ -445,7 +446,6 @@ end function ifind_charstr
!<
function filein_fname(filename, tagname, input_mempath, input_fname) &
result(found)
use SimModule, only: store_error, store_error_filename
use MemoryManagerModule, only: mem_setptr, get_isize
use CharacterStringModule, only: CharacterStringType
character(len=*), intent(inout) :: filename
Expand Down Expand Up @@ -483,4 +483,33 @@ function filein_fname(filename, tagname, input_mempath, input_fname) &
return
end function filein_fname

!> @brief store an error for input exceeding internal name length
!<
subroutine inlen_check(input_name, mf6_name, maxlen, name_type)
use CharacterStringModule, only: CharacterStringType
type(CharacterStringType), intent(in) :: input_name
character(len=*), intent(inout) :: mf6_name
integer(I4B), intent(in) :: maxlen
character(len=*), intent(in) :: name_type
character(len=LINELENGTH) :: input_str
integer(I4B) :: ilen
!
! -- initialize
mf6_name = ''
input_str = input_name
ilen = len_trim(input_str)
if (ilen > maxlen) then
write (errmsg, '(a,i0,a)') &
'Input name "'//trim(input_str)//'" exceeds maximum allowed length (', &
maxlen, ') for '//trim(name_type)//'.'
call store_error(errmsg)
end if
!
! -- set truncated name
mf6_name = trim(input_str)
!
! -- return
return
end subroutine inlen_check

end module SourceCommonModule

0 comments on commit def73f5

Please sign in to comment.