From 641c52d07272415ec51f603c467f076a259ed250 Mon Sep 17 00:00:00 2001 From: Sebastian Beyer Date: Sat, 26 Oct 2024 19:58:31 +0200 Subject: [PATCH] Improve error handling for open files for clock file and mesh file Make a proper error message with the path where the file was expected and do proper mpi_abort. --- src/gen_modules_clock.F90 | 40 ++++++++++++++++++++++++++++++--------- src/oce_mesh.F90 | 18 ++++++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/gen_modules_clock.F90 b/src/gen_modules_clock.F90 index 0a287ab7e..43bbdc98f 100755 --- a/src/gen_modules_clock.F90 +++ b/src/gen_modules_clock.F90 @@ -2,6 +2,8 @@ module g_clock !combining RT and Lars version ! use g_config + use iso_fortran_env, only: error_unit + use mpi implicit none save real(kind=WP) :: timeold, timenew !time in a day, unit: sec @@ -76,6 +78,9 @@ subroutine clock_init(partit) type(t_partit), intent(in), target :: partit integer :: i, daystart, yearstart real(kind=WP) :: aux1, aux2, timestart + integer :: ierr + integer :: file_unit + character(512) :: errmsg ! the model initialized at timestart=timenew @@ -83,10 +88,17 @@ subroutine clock_init(partit) yearstart=yearnew ! init clock for this run - open(99,file=trim(ResultPath)//trim(runid)//'.clock',status='old') - read(99,*) timeold, dayold, yearold - read(99,*) timenew, daynew, yearnew - close(99) + open(newunit=file_unit, file=trim(ResultPath)//trim(runid)//'.clock', action='read', & + status='old', iostat=ierr, iomsg=errmsg) + if (ierr /= 0) then + write (unit=error_unit, fmt='(3A)') & + '### error: can not open file ', trim(ResultPath)//trim(runid)//'.clock', & + ', error: ' // trim(errmsg) + call MPI_Abort(MPI_COMM_WORLD, 1, ierr) + end if + read(unit=file_unit, fmt=*) timeold, dayold, yearold + read(unit=file_unit, fmt=*) timenew, daynew, yearnew + close(unit=file_unit) if(daynew==0) daynew=1 ! check if this is a restart or not @@ -157,6 +169,9 @@ subroutine clock_finish real(kind=WP) :: dum_timenew !time in a day, unit: sec integer :: dum_daynew !day in a year integer :: dum_yearnew !year before and after time step + integer :: ierr + integer :: file_unit + character(512) :: errmsg dum_timenew = timenew dum_daynew = daynew @@ -167,10 +182,17 @@ subroutine clock_finish dum_yearnew=yearold+1 endif - open(99,file=trim(ResultPath)//trim(runid)//'.clock',status='unknown') - write(99,*) timeold, dayold, yearold - write(99,*) dum_timenew, dum_daynew, dum_yearnew - close(99) + open(newunit=file_unit, file=trim(ResultPath)//trim(runid)//'.clock', action='write', & + status='unknown', iostat=ierr, iomsg=errmsg) + if (ierr /= 0) then + write (unit=error_unit, fmt='(3A)') & + '### error: can not open file ', trim(ResultPath)//trim(runid)//'.clock', & + ', error: ' // trim(errmsg) + call MPI_Abort(MPI_COMM_WORLD, 1, ierr) + end if + write(unit=file_unit, fmt=*) timeold, dayold, yearold + write(unit=file_unit, fmt=*) dum_timenew, dum_daynew, dum_yearnew + close(unit=file_unit) end subroutine clock_finish ! !---------------------------------------------------------------------------- @@ -211,4 +233,4 @@ end subroutine is_fleapyr ! !---------------------------------------------------------------------------- ! -end module g_clock +end module g_clock \ No newline at end of file diff --git a/src/oce_mesh.F90 b/src/oce_mesh.F90 index 1843e345b..0dba2f221 100755 --- a/src/oce_mesh.F90 +++ b/src/oce_mesh.F90 @@ -179,6 +179,7 @@ END SUBROUTINE mesh_setup ! Reads distributed mesh ! The mesh will be read only by 0 proc and broadcasted to the others. SUBROUTINE read_mesh(partit, mesh) +use iso_fortran_env, only: error_unit USE o_PARAM USE g_CONFIG USE MOD_MESH @@ -205,8 +206,10 @@ SUBROUTINE read_mesh(partit, mesh) integer, allocatable, dimension(:,:) :: ibuff real(kind=WP), allocatable, dimension(:,:) :: rbuff integer, allocatable, dimension(:,:) :: auxbuff ! will be used for reading aux3d.out - integer :: fileunit, iostat + integer :: fileunit character(32) :: mesh_checksum + integer :: ioerr + character(512) :: errmsg #include "associate_part_def.h" #include "associate_mesh_def.h" @@ -235,7 +238,14 @@ SUBROUTINE read_mesh(partit, mesh) if (mype==0) then file_name=trim(dist_mesh_dir)//'rpart.out' fileID=10 - open(fileID, file=trim(file_name)) + open(unit=fileID, file=trim(file_name), action='read', status='old', & + iostat=ioerr, iomsg=errmsg) + if (ioerr /= 0) then + write (unit=error_unit, fmt='(3A)') & + '### error: can not open file ', file_name, & + ', error: ' // trim(errmsg) + call MPI_Abort(MPI_COMM_FESOM, 1, ierror) + end if allocate(partit%part(npes+1)) part=>partit%part read(fileID,*) n @@ -254,7 +264,7 @@ SUBROUTINE read_mesh(partit, mesh) write(*,*) n write(*,*) 'error: NPES does not coincide with that of the mesh' call par_ex(partit%MPI_COMM_FESOM, partit%mype, 1) - STOP + call MPI_Abort(MPI_COMM_FESOM, 1, ierror) end if ! broadcasting partitioning vector to the other procs if (mype/=0) then @@ -2853,4 +2863,4 @@ subroutine check_total_volume(partit, mesh) end subroutine check_total_volume ! ! -!_______________________________________________________________________________ +!_______________________________________________________________________________ \ No newline at end of file