From def73f5987a8e822ce4c8a27f02428dd91930ca9 Mon Sep 17 00:00:00 2001 From: mjreno Date: Thu, 23 May 2024 09:42:34 -0400 Subject: [PATCH] refactor(idm): verify and store error based on length of user input names (#1775) * check input length of model,exg,pacakge names * delay model package name error until all names checked --------- Co-authored-by: mjreno --- src/Utilities/Idm/IdmLoad.f90 | 24 ++++++++++++------ src/Utilities/Idm/ModelPackageInputs.f90 | 10 ++++++-- src/Utilities/Idm/SourceCommon.f90 | 31 +++++++++++++++++++++++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/Utilities/Idm/IdmLoad.f90 b/src/Utilities/Idm/IdmLoad.f90 index eb3fd089cc0..27576bdeb61 100644 --- a/src/Utilities/Idm/IdmLoad.f90 +++ b/src/Utilities/Idm/IdmLoad.f90 @@ -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, & @@ -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 @@ -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 @@ -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 @@ -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) = '' @@ -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 ! diff --git a/src/Utilities/Idm/ModelPackageInputs.f90 b/src/Utilities/Idm/ModelPackageInputs.f90 index cda83dd286a..8ca93fa584d 100644 --- a/src/Utilities/Idm/ModelPackageInputs.f90 +++ b/src/Utilities/Idm/ModelPackageInputs.f90 @@ -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 @@ -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 @@ -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 diff --git a/src/Utilities/Idm/SourceCommon.f90 b/src/Utilities/Idm/SourceCommon.f90 index 4e1246484f9..e84d229d528 100644 --- a/src/Utilities/Idm/SourceCommon.f90 +++ b/src/Utilities/Idm/SourceCommon.f90 @@ -21,6 +21,7 @@ module SourceCommonModule public :: file_ext public :: ifind_charstr public :: filein_fname + public :: inlen_check contains @@ -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 @@ -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