From 7b94cbc047672cd4577fd6e3d81fa61dda32f17d Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Thu, 23 Jan 2025 12:05:08 -0700 Subject: [PATCH 1/5] add esmf_logmsg to shr_abort --- src/shr_abort_mod.F90 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/shr_abort_mod.F90 b/src/shr_abort_mod.F90 index 9e4de5b..a8d422f 100644 --- a/src/shr_abort_mod.F90 +++ b/src/shr_abort_mod.F90 @@ -35,12 +35,15 @@ module shr_abort_mod contains !=============================================================================== - subroutine shr_abort_abort(string,rc) + subroutine shr_abort_abort(string,rc, line, file) + use esmf, only : ESMF_LOGWRITE, ESMF_LOGMSG_ERROR ! Consistent stopping mechanism !----- arguments ----- character(len=*) , intent(in), optional :: string ! error message string integer(shr_kind_in), intent(in), optional :: rc ! error code + integer(shr_kind_in), intent(in), optional :: line + character(len=*), intent(in), optional :: file !----- local ----- logical :: flag @@ -58,6 +61,8 @@ subroutine shr_abort_abort(string,rc) call print_error_to_logs("ERROR", local_string) + call ESMF_LogWrite(local_string, ESMF_LOGMSG_ERROR, line=line, file=file) + call shr_abort_backtrace() call shr_mpi_initialized(flag) From 4236719bd01c71b70db45d3650ef904115b06e07 Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Thu, 23 Jan 2025 12:27:10 -0700 Subject: [PATCH 2/5] replace calls to esmf_logmsg_error --- src/nuopc_shr_methods.F90 | 62 ++++++++++++--------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/nuopc_shr_methods.F90 b/src/nuopc_shr_methods.F90 index 7055cc8..6d542e5 100644 --- a/src/nuopc_shr_methods.F90 +++ b/src/nuopc_shr_methods.F90 @@ -2,7 +2,7 @@ module nuopc_shr_methods use ESMF , only : operator(<), operator(/=), operator(+) use ESMF , only : operator(-), operator(*) , operator(>=) use ESMF , only : operator(<=), operator(>), operator(==), MOD - use ESMF , only : ESMF_LOGERR_PASSTHRU, ESMF_LogFoundError, ESMF_LOGMSG_ERROR, ESMF_MAXSTR + use ESMF , only : ESMF_LOGERR_PASSTHRU, ESMF_LogFoundError, ESMF_MAXSTR use ESMF , only : ESMF_SUCCESS, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_FAILURE use ESMF , only : ESMF_State, ESMF_StateGet use ESMF , only : ESMF_Field, ESMF_FieldGet @@ -373,9 +373,7 @@ subroutine state_diagnose(State, string, rc) call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO) endif else - call ESMF_LogWrite(trim(subname)//": ERROR rank not supported ", ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(trim(subname)//": ERROR rank not supported ") endif enddo @@ -411,10 +409,8 @@ subroutine field_getfldptr(field, fldptr1, fldptr2, rank, abort, rc) ! ---------------------------------------------- if (.not.present(rc)) then - call ESMF_LogWrite(trim(subname)//": ERROR rc not present ", & - ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u) - rc = ESMF_FAILURE - return + call shr_sys_abort(trim(subname)//": ERROR rc not present ", & + line=__LINE__, file=u_FILE_u) endif rc = ESMF_SUCCESS @@ -465,27 +461,21 @@ subroutine field_getfldptr(field, fldptr1, fldptr2, rank, abort, rc) ESMF_LOGMSG_INFO) elseif (lrank == 1) then if (.not.present(fldptr1)) then - call ESMF_LogWrite(trim(subname)//": ERROR missing rank=1 array ", & - ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u) - rc = ESMF_FAILURE - return + call shr_abort_mod(trim(subname)//": ERROR missing rank=1 array ", & + line=__LINE__, file=u_FILE_u) endif call ESMF_FieldGet(field, farrayPtr=fldptr1, rc=rc) if (chkerr(rc,__LINE__,u_FILE_u)) return elseif (lrank == 2) then if (.not.present(fldptr2)) then - call ESMF_LogWrite(trim(subname)//": ERROR missing rank=2 array ", & - ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u) - rc = ESMF_FAILURE - return + call shr_sys_abort(trim(subname)//": ERROR missing rank=2 array ", & + line=__LINE__, file=u_FILE_u) endif call ESMF_FieldGet(field, farrayPtr=fldptr2, rc=rc) if (chkerr(rc,__LINE__,u_FILE_u)) return else - call ESMF_LogWrite(trim(subname)//": ERROR in rank ", & - ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u) - rc = ESMF_FAILURE - return + call shr_sys_abort(trim(subname)//": ERROR in rank ", & + line=__LINE__, file=u_FILE_u) endif endif ! status @@ -566,14 +556,10 @@ subroutine alarmInit( clock, alarm, option, & ! Error checks if (trim(option) == optdate) then if (.not. present(opt_ymd)) then - call ESMF_LogWrite(trim(subname)//trim(option)//' requires opt_ymd', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(trim(subname)//trim(option)//' requires opt_ymd') end if if (lymd < 0 .or. ltod < 0) then - call ESMF_LogWrite(subname//trim(option)//'opt_ymd, opt_tod invalid', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(subname//trim(option)//'opt_ymd, opt_tod invalid') end if else if (& trim(option) == optNSteps .or. trim(option) == trim(optNSteps)//'s' .or. & @@ -584,14 +570,10 @@ subroutine alarmInit( clock, alarm, option, & trim(option) == optNMonths .or. trim(option) == trim(optNMonths)//'s' .or. & trim(option) == optNYears .or. trim(option) == trim(optNYears)//'s' ) then if (.not.present(opt_n)) then - call ESMF_LogWrite(subname//trim(option)//' requires opt_n', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(subname//trim(option)//' requires opt_n') end if if (opt_n <= 0) then - call ESMF_LogWrite(subname//trim(option)//' invalid opt_n', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(subname//trim(option)//' invalid opt_n') end if end if call ESMF_TimeIntervalSet(AlarmInterval, yy=9999, rc=rc) @@ -629,9 +611,7 @@ subroutine alarmInit( clock, alarm, option, & AlarmInterval = AlarmInterval * opt_n ! timestepinterval*0 is 0 of kind ESMF_TimeStepInterval if (mod(AlarmInterval, TimestepInterval) /= (TimestepInterval*0)) then - call ESMF_LogWrite(subname//'illegal Alarm setting for '//trim(alarmname), ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(subname//'illegal Alarm setting for '//trim(alarmname)) endif update_nextalarm = .true. @@ -691,9 +671,7 @@ subroutine alarmInit( clock, alarm, option, & if (ChkErr(rc,__LINE__,u_FILE_u)) return case default - call ESMF_LogWrite(subname//'unknown option '//trim(option), ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort(subname//'unknown option '//trim(option)) end select @@ -804,14 +782,10 @@ integer function get_minimum_timestep(gcomp, rc) enddo if(get_minimum_timestep == huge(1)) then - call ESMF_LogWrite('minimum_timestep_error: this option is not supported ', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort('minimum_timestep_error: this option is not supported ') endif if(get_minimum_timestep <= 0) then - call ESMF_LogWrite('minimum_timestep_error ERROR ', ESMF_LOGMSG_ERROR) - rc = ESMF_FAILURE - return + call shr_sys_abort('minimum_timestep_error ERROR ') endif end function get_minimum_timestep From c58b45bc6c1496114fde0e1cd83b5f5ea64c10f5 Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Thu, 23 Jan 2025 12:57:59 -0700 Subject: [PATCH 3/5] esmf is always available, use it --- src/shr_abort_mod.F90 | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/shr_abort_mod.F90 b/src/shr_abort_mod.F90 index a8d422f..f3fe018 100644 --- a/src/shr_abort_mod.F90 +++ b/src/shr_abort_mod.F90 @@ -10,7 +10,6 @@ module shr_abort_mod use, intrinsic :: iso_fortran_env, only: output_unit, error_unit use shr_kind_mod, only : shr_kind_in, shr_kind_cx - use shr_mpi_mod , only : shr_mpi_initialized, shr_mpi_abort use shr_log_mod , only : s_logunit => shr_log_Unit #ifdef CPRNAG @@ -36,7 +35,7 @@ module shr_abort_mod !=============================================================================== subroutine shr_abort_abort(string,rc, line, file) - use esmf, only : ESMF_LOGWRITE, ESMF_LOGMSG_ERROR + use esmf, only : ESMF_LOGWRITE, ESMF_LOGMSG_ERROR, ESMF_FINALIZE, ESMF_END_ABORT ! Consistent stopping mechanism !----- arguments ----- @@ -58,6 +57,9 @@ subroutine shr_abort_abort(string,rc, line, file) else local_string = "Unknown error submitted to shr_abort_abort." end if + if(present(rc)) then + write(local_string, *) trim(local_string), ' rc=',rc + endif call print_error_to_logs("ERROR", local_string) @@ -65,15 +67,7 @@ subroutine shr_abort_abort(string,rc, line, file) call shr_abort_backtrace() - call shr_mpi_initialized(flag) - - if (flag) then - if (present(rc)) then - call shr_mpi_abort(trim(local_string),rc) - else - call shr_mpi_abort(trim(local_string)) - endif - endif + call ESMF_Finalize(endflag=ESMF_END_ABORT) ! A compiler's abort method may print a backtrace or do other nice ! things, but in fact we can rarely leverage this, because MPI_Abort From 2a6ab9d5293aa55158e0f76f3a36d75623995668 Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Thu, 23 Jan 2025 13:26:43 -0700 Subject: [PATCH 4/5] remove unused variable --- src/shr_abort_mod.F90 | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/shr_abort_mod.F90 b/src/shr_abort_mod.F90 index f3fe018..6e3a906 100644 --- a/src/shr_abort_mod.F90 +++ b/src/shr_abort_mod.F90 @@ -44,9 +44,6 @@ subroutine shr_abort_abort(string,rc, line, file) integer(shr_kind_in), intent(in), optional :: line character(len=*), intent(in), optional :: file - !----- local ----- - logical :: flag - ! Local version of the string. ! (Gets a default value if string is not present.) character(len=shr_kind_cx) :: local_string From f2f86fb034dd448325260f95d15e925e7ba1956a Mon Sep 17 00:00:00 2001 From: Jim Edwards Date: Thu, 23 Jan 2025 13:31:03 -0700 Subject: [PATCH 5/5] fix typo --- src/nuopc_shr_methods.F90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nuopc_shr_methods.F90 b/src/nuopc_shr_methods.F90 index 6d542e5..f67e08f 100644 --- a/src/nuopc_shr_methods.F90 +++ b/src/nuopc_shr_methods.F90 @@ -461,7 +461,7 @@ subroutine field_getfldptr(field, fldptr1, fldptr2, rank, abort, rc) ESMF_LOGMSG_INFO) elseif (lrank == 1) then if (.not.present(fldptr1)) then - call shr_abort_mod(trim(subname)//": ERROR missing rank=1 array ", & + call shr_sys_abort(trim(subname)//": ERROR missing rank=1 array ", & line=__LINE__, file=u_FILE_u) endif call ESMF_FieldGet(field, farrayPtr=fldptr1, rc=rc)