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

Sebastianwolf/error checking #45

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7b686a3
Added a couple of missing type declaration statements.
Mar 30, 2021
47a16b2
* Added first version of error-checking with macros.
sebastianwolf Apr 19, 2021
0c6ed1b
Added list of strings containing error names. Care needs be taken wit…
sebastianwolf Apr 19, 2021
220a1c0
Removed none in err_names. Now array indexing fits with err-codes.
sebastianwolf Apr 19, 2021
f85f8a1
Exception handling: Added handlers which are less brutal and allow do…
dmay23 Apr 19, 2021
eb30715
Exceptions: Integrating changes into API and demo driver.
dmay23 Apr 19, 2021
c26e9b0
Resolved conflict
dmay23 Apr 19, 2021
7d4ad8d
Exceptions: Remove old storage of ierr/msg in the error module.
dmay23 Apr 19, 2021
56f8b61
Merge remote-tracking branch 'origin/sebastianwolf/ErrorChecking' int…
sebastianwolf Apr 19, 2021
4f6b9f9
Exceptions: Clean up
dmay23 Apr 19, 2021
8169c1d
Exceptions: Put error codes in their own module and their own file fo…
dmay23 Apr 19, 2021
b142b50
Merge remote-tracking branch 'origin/sebastianwolf/ErrorChecking' int…
sebastianwolf Apr 20, 2021
673054e
* Added ierr error code to all API funcs
sebastianwolf Apr 20, 2021
056bb25
* Added error checking to Diffusion and Flowrouting errors.
sebastianwolf Apr 20, 2021
97e127d
* cleanup
sebastianwolf Apr 21, 2021
5d7420e
* removed debug files and Makefile from src
sebastianwolf Apr 21, 2021
06793f1
cleanup
sebastianwolf Apr 21, 2021
9b0f0a5
Fixed small arg error in api
sebastianwolf Apr 22, 2021
e1bd453
Merge remote-tracking branch 'upstream/master' into sebastianwolf/Err…
sebastianwolf Apr 23, 2021
3f4c4f4
Added ErrorCodes to compiled files list
sebastianwolf Apr 26, 2021
4880638
Fixed typo
sebastianwolf Apr 26, 2021
135dd2c
Error macros need line length longer than the standard of 132 chars. …
sebastianwolf Apr 26, 2021
ad8a4ce
Merge remote-tracking branch 'upstream/master' into sebastianwolf/Err…
sebastianwolf Apr 26, 2021
d9a0a22
wrap all api subroutines in a new module
benbovy Apr 26, 2021
dff733b
add FSCAPE_INITERR and FSCAPE_CHKERR_OPT macros
benbovy Apr 26, 2021
7241ec9
insert new macros in API functions
benbovy Apr 26, 2021
fd8c433
update examples
benbovy Apr 26, 2021
ba65b11
hopefully fix python builds
benbovy Apr 26, 2021
fbabf5b
try fix Flang (Python bindings) builds
benbovy Apr 27, 2021
84edb41
reduce expanded macro line lengths (flang compat)
benbovy Apr 27, 2021
8c71965
python bindings: do not print error messages
benbovy Apr 27, 2021
09d9607
Added iso_c bindings for api functions. The function-names when calli…
sebastianwolf Jun 1, 2021
bfaba1b
Merge branch 'sebastianwolf/bindC' into sebastianwolf/ErrorChecking
sebastianwolf Jun 1, 2021
5faa737
Updated Cmakelist to conform with standard 2018. That is required for…
sebastianwolf Jun 1, 2021
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
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(FASTSCAPELIB_SRC_FILES
${FASTSCAPELIB_SRC_DIR}/Diffusion.f90
${FASTSCAPELIB_SRC_DIR}/FastScape_api.f90
${FASTSCAPELIB_SRC_DIR}/FastScape_ctx.f90
${FASTSCAPELIB_SRC_DIR}/FastScapeErrorCodes.f90
${FASTSCAPELIB_SRC_DIR}/LocalMinima.f90
${FASTSCAPELIB_SRC_DIR}/Marine.f90
${FASTSCAPELIB_SRC_DIR}/Strati.f90
Expand Down Expand Up @@ -45,7 +46,7 @@ set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -cpp")
# =====

if(CMAKE_Fortran_COMPILER_ID MATCHES "Flang|GNU")
set(dialect "-ffree-form -std=f2008 -fimplicit-none -fall-intrinsics")
set(dialect "-ffree-form -std=f2008 -fimplicit-none -fall-intrinsics -ffree-line-length-none")
set(ioflags "-fconvert=big-endian")
set(f77flags "-std=legacy -ffixed-form")
set(bounds "-fbounds-check")
Expand Down Expand Up @@ -99,9 +100,15 @@ endif()
# Fortran library (static/shared)
# ===============================

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/mod)

if(BUILD_FASTSCAPELIB_STATIC OR BUILD_FASTSCAPELIB_SHARED)
# build temp library
set(FASTSCAPELIB_OBJECTS libfastscape_objects)
add_library(${FASTSCAPELIB_OBJECTS} OBJECT ${FASTSCAPELIB_SRC_FILES})

# install api Fortran module
install(FILES ${CMAKE_Fortran_MODULE_DIRECTORY}/fastscapeapi.mod DESTINATION include)
endif()

if(BUILD_FASTSCAPELIB_STATIC)
Expand Down
23 changes: 15 additions & 8 deletions src/Diffusion.f90
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
subroutine Diffusion ()
#include "Error.fpp"
subroutine Diffusion (ierr)

! subroutine to solve the diffusion equation by ADI

Expand All @@ -11,6 +12,7 @@ subroutine Diffusion ()
integer i,j,ij
double precision factxp,factxm,factyp,factym,dx,dy
character cbc*4
integer ierr

!print*,'Diffusion'

Expand Down Expand Up @@ -79,7 +81,7 @@ subroutine Diffusion ()
inf(nx)=-factxm
f(nx)=zintp(nx,j)+factyp*zintp(nx,j+1)-(factyp+factym)*zintp(nx,j)+factym*zintp(nx,j-1)
endif
call tridag (inf,diag,sup,f,res,nx)
call tridag (inf,diag,sup,f,res,nx,ierr);FSCAPE_CHKERR(ierr)
do i=1,nx
zint(i,j)=res(i)
enddo
Expand Down Expand Up @@ -131,7 +133,7 @@ subroutine Diffusion ()
inf(ny)=-factym
f(ny)=zint(i,ny)+factxp*zint(i+1,ny)-(factxp+factxm)*zint(i,ny)+factxm*zint(i-1,ny)
endif
call tridag (inf,diag,sup,f,res,ny)
call tridag (inf,diag,sup,f,res,ny,ierr);FSCAPE_CHKERR(ierr)
do j=1,ny
zintp(i,j)=res(j)
enddo
Expand Down Expand Up @@ -161,19 +163,23 @@ end subroutine Diffusion

! subroutine to solve a tri-diagonal system of equations (from Numerical Recipes)

SUBROUTINE tridag(a,b,c,r,u,n)
SUBROUTINE tridag(a,b,c,r,u,n,ierr)

use FastScapeErrorCodes
implicit none

INTEGER n
double precision a(n),b(n),c(n),r(n),u(n)
INTEGER j
double precision bet
double precision,dimension(:),allocatable::gam

integer,intent(inout):: ierr
allocate (gam(n))

if(b(1).eq.0.d0) stop 'in tridag'
if(b(1).eq.0.d0) then
FSCAPE_RAISE_MESSAGE('Tridag error b(1)=0: cannot solve system',ERR_TridiagNotSolvable,ierr);FSCAPE_CHKERR(ierr)
end if
!if(b(1).eq.0.d0) stop 'in tridag'

! first pass

Expand All @@ -183,8 +189,9 @@ SUBROUTINE tridag(a,b,c,r,u,n)
gam(j)=c(j-1)/bet
bet=b(j)-a(j)*gam(j)
if(bet.eq.0.) then
print*,'tridag failed'
stop
FSCAPE_RAISE_MESSAGE('Tridag error bet=0: cannot solve system',ERR_TridiagNotSolvable,ierr);FSCAPE_CHKERR(ierr)
! print*,'tridag failed'
! stop
endif
u(j)=(r(j)-a(j)*u(j-1))/bet
11 continue
Expand Down
69 changes: 69 additions & 0 deletions src/Error.fpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

! Error.fpp

!
! Macros for error handling.
! Fortran preprocessor must be enabled: -fpp.
!

!
! Initialize ierr argument in API functions
!
#define FSCAPE_INITERR(ierr, ierr_, fname) \
ierr_ = 0; \
if (present(ierr)) then; \
ierr = ierr_; \
else; \
print '(A)', "*** Depreciation warning *** "; \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benbovy This should read "Deprecation" not "Depreciation"

print '(A,A,A)', "Calling ", TRIM((fname)), " without 'ierr' argument (integer) is depreciated! Please update your code!"; \
Copy link

@hpc4geo hpc4geo Apr 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benbovy This should read "deprecated" not "depreciated"

end if;

!
! Raise an exception by pushing the name of the exception + file name, line number to stdout
! Sets ierr to the error type specified by err_type
!
#define FSCAPE_RAISE(err_type, ierr) \
ierr = (err_type); \
print '(A,I4,A)', "*** FastScape exception *** -> " // TRIM(err_names((ierr))) // " (" // __FILE__ // ", line", __LINE__, ")";

!
! Raise an exception by pushing custom user provided message to stdout,
! along with the name of the exception + file name, line number to stdout
! Sets ierr to the error type specified by err_type
!
#define FSCAPE_RAISE_MESSAGE(msg, err_type, ierr) \
print '(A,A)', "*** FastScape exception *** ", TRIM((msg)) // "!"; \
FSCAPE_RAISE(err_type, ierr);

!
! Calls return if error code is non-zero.
!
#define FSCAPE_CHKERR(ierr) \
if (ierr /= 0) then; \
return; \
end if;

!
! Either calls return or stop if error code is non-zero, dependening on whether the
! the ierr argument is present.
!
#define FSCAPE_CHKERR_OPT(ierr, ierr_) \
if (ierr_ /= 0) then; \
if (present(ierr)) then; \
ierr = ierr_; \
return; \
else; \
print '(A)', "*** Execution being halted by calling stop!"; \
print '(A)', "Call all API routine with 'ierr' argument to avoid this."; \
stop; \
end if; \
end if;

!
! Force stop if error code is non-zero.
! This should only be called in driver code - never within library code or the API.
!
#define FSCAPE_CHKERR_ABORT(ierr) \
if (ierr /= 0) then; \
stop; \
end if;
23 changes: 23 additions & 0 deletions src/FastScapeErrorCodes.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

module FastScapeErrorCodes
implicit none
integer, parameter :: ERR_None = 0, &
ERR_Default = 1, &
ERR_FileNotFound = 2, &
ERR_FileOpenFailed = 3, &
ERR_ParameterInvalid = 4, &
ERR_ParameterOutOfRange = 5, &
ERR_TridiagNotSolvable = 6, &
ERR_SetupNotRun = 7, &
ERR_NotConverged = 8

character(len=50), dimension(8) :: err_names = [character(len=50) :: "Default run time error", &
"File error: File not found", &
"File error: File open failed", &
"Parameter error: Input invalid", &
"Parameter error: Out of range" , &
"Solver error: Tridiag not solvable", &
"Order invalid: Must call SetUp() first", &
"Solver error: not converged" ]

end module FastScapeErrorCodes
Loading