Skip to content

Commit

Permalink
Update precision to have a CAM and SAM precision, and add sp and dp o…
Browse files Browse the repository at this point in the history
…ptions to the test_utils routines.
  • Loading branch information
jatkinson1000 committed Feb 1, 2024
1 parent d1b7651 commit ccf28a4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 22 deletions.
3 changes: 2 additions & 1 deletion NN_module/precision.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module precision

public
integer, parameter :: c_wp = c_sp
integer, parameter :: wp = 4
integer, parameter :: wps = sp
integer, parameter :: wpc = dp

end module precision
113 changes: 92 additions & 21 deletions NN_module/test_utils.f90
Original file line number Diff line number Diff line change
@@ -1,42 +1,113 @@
module test_utils

use :: precision, only: wp
use :: precision, only: sp, dp

implicit none

character(len=15) :: pass = char(27)//'[32m'//'PASSED'//char(27)//'[0m'
character(len=15) :: fail = char(27)//'[31m'//'FAILED'//char(27)//'[0m'

interface assert_array_equal
module procedure assert_array_equal_1d, assert_array_equal_2d, assert_array_equal_3d
module procedure &
assert_array_equal_1d_sp, assert_array_equal_2d_sp, assert_array_equal_3d_sp, &
assert_array_equal_1d_dp, assert_array_equal_2d_dp, assert_array_equal_3d_dp
end interface

interface print_assert
module procedure print_assert
module procedure print_assert_sp, print_assert_dp
end interface

contains

subroutine print_assert(test_name, is_close, relative_error)
subroutine print_assert_sp(test_name, is_close, relative_error)

character(len=*), intent(in) :: test_name
logical, intent(in) :: is_close
real(wp), intent(in) :: relative_error
real(sp), intent(in) :: relative_error

if (is_close) then
write(*, '(A, " :: [", A, "] maximum relative error = ", E11.4)') pass, trim(test_name), relative_error
else
write(*, '(A, " :: [", A, "] maximum relative error = ", E11.4)') fail, trim(test_name), relative_error
end if

end subroutine print_assert
end subroutine print_assert_sp

subroutine assert_array_equal_1d(a, b, test_name, rtol_opt)
subroutine print_assert_dp(test_name, is_close, relative_error)

character(len=*), intent(in) :: test_name
real(wp), intent(in), dimension(:) :: a, b
real(wp), intent(in), optional :: rtol_opt
real(wp) :: relative_error, rtol
logical, intent(in) :: is_close
real(dp), intent(in) :: relative_error

if (is_close) then
write(*, '(A, " :: [", A, "] maximum relative error = ", E11.4)') pass, trim(test_name), relative_error
else
write(*, '(A, " :: [", A, "] maximum relative error = ", E11.4)') fail, trim(test_name), relative_error
end if

end subroutine print_assert_dp

subroutine assert_array_equal_1d_sp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(sp), intent(in), dimension(:) :: a, b
real(sp), intent(in), optional :: rtol_opt
real(sp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
else
rtol = rtol_opt
end if

relative_error = maxval(abs(a/b - 1.0))

call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_1d_sp

subroutine assert_array_equal_2d_sp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(sp), intent(in), dimension(:,:) :: a, b
real(sp), intent(in), optional :: rtol_opt
real(sp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
else
rtol = rtol_opt
end if

relative_error = maxval(abs(a/b - 1.0))
call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_2d_sp

subroutine assert_array_equal_3d_sp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(sp), intent(in), dimension(:,:,:) :: a, b
real(sp), intent(in), optional :: rtol_opt
real(sp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
else
rtol = rtol_opt
end if

relative_error = maxval(abs(a/b - 1.0))
call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_3d_sp

subroutine assert_array_equal_1d_dp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(dp), intent(in), dimension(:) :: a, b
real(dp), intent(in), optional :: rtol_opt
real(dp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
Expand All @@ -48,14 +119,14 @@ subroutine assert_array_equal_1d(a, b, test_name, rtol_opt)

call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_1d
end subroutine assert_array_equal_1d_dp

subroutine assert_array_equal_2d(a, b, test_name, rtol_opt)
subroutine assert_array_equal_2d_dp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(wp), intent(in), dimension(:,:) :: a, b
real(wp), intent(in), optional :: rtol_opt
real(wp) :: relative_error, rtol
real(dp), intent(in), dimension(:,:) :: a, b
real(dp), intent(in), optional :: rtol_opt
real(dp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
Expand All @@ -66,14 +137,14 @@ subroutine assert_array_equal_2d(a, b, test_name, rtol_opt)
relative_error = maxval(abs(a/b - 1.0))
call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_2d
end subroutine assert_array_equal_2d_dp

subroutine assert_array_equal_3d(a, b, test_name, rtol_opt)
subroutine assert_array_equal_3d_dp(a, b, test_name, rtol_opt)

character(len=*), intent(in) :: test_name
real(wp), intent(in), dimension(:,:,:) :: a, b
real(wp), intent(in), optional :: rtol_opt
real(wp) :: relative_error, rtol
real(dp), intent(in), dimension(:,:,:) :: a, b
real(dp), intent(in), optional :: rtol_opt
real(dp) :: relative_error, rtol

if (.not. present(rtol_opt)) then
rtol = 1.0e-5
Expand All @@ -84,6 +155,6 @@ subroutine assert_array_equal_3d(a, b, test_name, rtol_opt)
relative_error = maxval(abs(a/b - 1.0))
call print_assert(test_name, (rtol > relative_error), relative_error)

end subroutine assert_array_equal_3d
end subroutine assert_array_equal_3d_dp

end module test_utils

0 comments on commit ccf28a4

Please sign in to comment.