Skip to content

Commit

Permalink
Merge pull request #62 from jedwards4b/add_esmf_logmsg_to_shr_abort
Browse files Browse the repository at this point in the history
add esmf_logmsg to shr_abort
  • Loading branch information
jedwards4b authored Jan 24, 2025
2 parents c5e7603 + f2f86fb commit 2bb87b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 58 deletions.
62 changes: 18 additions & 44 deletions src/nuopc_shr_methods.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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_sys_abort(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
Expand Down Expand Up @@ -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. &
Expand All @@ -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)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
24 changes: 10 additions & 14 deletions src/shr_abort_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,15 +34,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, ESMF_FINALIZE, ESMF_END_ABORT
! Consistent stopping mechanism

!----- arguments -----
character(len=*) , intent(in), optional :: string ! error message string
integer(shr_kind_in), intent(in), optional :: rc ! error code

!----- local -----
logical :: flag
integer(shr_kind_in), intent(in), optional :: line
character(len=*), intent(in), optional :: file

! Local version of the string.
! (Gets a default value if string is not present.)
Expand All @@ -55,20 +54,17 @@ subroutine shr_abort_abort(string,rc)
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)

call ESMF_LogWrite(local_string, ESMF_LOGMSG_ERROR, line=line, file=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
Expand Down

0 comments on commit 2bb87b8

Please sign in to comment.