Skip to content

Commit

Permalink
Improve error handling for open files for clock file and mesh file
Browse files Browse the repository at this point in the history
Make a proper error message with the path where the file was
expected and do proper mpi_abort.
  • Loading branch information
Sebastian Beyer authored and sebastianbeyer committed Oct 31, 2024
1 parent 8231e55 commit 641c52d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
40 changes: 31 additions & 9 deletions src/gen_modules_clock.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,17 +78,27 @@ 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
daystart=daynew
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
Expand Down Expand Up @@ -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
Expand All @@ -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
!
!----------------------------------------------------------------------------
Expand Down Expand Up @@ -211,4 +233,4 @@ end subroutine is_fleapyr
!
!----------------------------------------------------------------------------
!
end module g_clock
end module g_clock
18 changes: 14 additions & 4 deletions src/oce_mesh.F90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -2853,4 +2863,4 @@ subroutine check_total_volume(partit, mesh)
end subroutine check_total_volume
!
!
!_______________________________________________________________________________
!_______________________________________________________________________________

0 comments on commit 641c52d

Please sign in to comment.