Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for open files for clock file and mesh file #642

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -75,17 +77,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 @@ -156,6 +168,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 @@ -166,10 +181,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 @@ -210,4 +232,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
!
!
!_______________________________________________________________________________
!_______________________________________________________________________________
Loading