Skip to content

Commit

Permalink
Merge pull request #3276 from GEOS-ESM/bugfix/wdboggs/I3248_test_max_…
Browse files Browse the repository at this point in the history
…action

Bugfix/wdboggs/i3248 test max action
  • Loading branch information
darianboggs authored Jan 6, 2025
2 parents 7f99e3a + 3dd3dd6 commit 2f6f781
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 88 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Implemented workarounds to avoid needing `-dusty` for NAG. (Related PR in ESMA_CMake.)
- Added constructor for DSO_SetServicesWrapper
- Change macro in field/undo_function_overload.macro
- Fixed bug with AccumulatorAction and subtypes

## [Unreleased]

Expand Down
84 changes: 60 additions & 24 deletions generic3g/tests/Test_AccumulatorAction.pf
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#include "MAPL_TestErr.h"
#include "unused_dummy.H"
module Test_AccumulatorAction
use mapl3g_AccumulatorAction
use accumulator_action_test_common
use esmf
use funit
use MAPL_FieldUtils
use pfunit
use ESMF_TestMethod_mod
implicit none

contains

@Test
subroutine test_construct_AccumulatorAction()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_construct_AccumulatorAction(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc

@assertFalse(acc%update_calculated, 'updated_calculated .TRUE.')
@assertFalse(acc%initialized, 'initialized .TRUE.')

end subroutine test_construct_AccumulatorAction

@Test
subroutine test_initialize()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_initialize(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -36,8 +38,9 @@ contains

end subroutine test_initialize

@Test
subroutine test_invalidate()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_invalidate(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -62,8 +65,9 @@ contains

end subroutine test_invalidate

@Test
subroutine test_update()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_update(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand Down Expand Up @@ -105,8 +109,9 @@ contains

end subroutine test_update

@Test
subroutine test_accumulate()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_accumulate(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -119,7 +124,7 @@ contains
typekind = ESMF_TYPEKIND_R4
call initialize_objects(importState, exportState, clock, typekind, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call initialize_field(update_field, typekind=typekind, _RC)
call initialize_field(update_field, acc%accumulation_field, _RC)
call FieldSet(update_field, value_r4, _RC)
call acc%accumulate(update_field, _RC)
matches_expected = FieldIsConstant(acc%accumulation_field, value_r4, _RC)
Expand All @@ -129,8 +134,9 @@ contains

end subroutine test_accumulate

@Test
subroutine test_clear()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_clear(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -148,31 +154,61 @@ contains

end subroutine test_clear

@Test
subroutine test_accumulate_R4()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_accumulate_R4(this)
class(ESMF_TestMethod), intent(inout) :: this
type(AccumulatorAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer :: status
real(kind=R4), parameter :: INITIAL_VALUE = 2.0_R4
real(kind=R4) :: update_value = 3.0_R4
real(kind=R4), parameter :: UPDATE_VALUE = 3.0_R4
real(kind=R4) :: expected_value
real(kind=R4), pointer :: upPtr(:), accPtr(:)
type(ESMF_Field) :: update_field
logical :: field_is_expected_value
integer :: n

! first accumulate
call initialize_objects(importState, exportState, clock, ESMF_TYPEKIND_R4, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call initialize_field(update_field, typekind=typekind, _RC)
call FieldSet(update_field, update_value, _RC)
call initialize_field(update_field, acc%accumulation_field, _RC)
call FieldSet(update_field, UPDATE_VALUE, _RC)
call FieldSet(acc%accumulation_field, INITIAL_VALUE, _RC)
call acc%accumulate_R4(update_field, _RC)
expected_value = INITIAL_VALUE + update_value
expected_value = INITIAL_VALUE + UPDATE_VALUE
field_is_expected_value = FieldIsConstant(acc%accumulation_field, expected_value, _RC)
@assertTrue(field_is_expected_value, 'accumulation_field not equal to expected_value.')
@assertTrue(field_is_expected_value, 'accumulation_field not equal to expected_value. (first test)')
! second accumulate
call acc%accumulate_R4(update_field, _RC)
expected_value = expected_value + update_value
expected_value = expected_value + UPDATE_VALUE
field_is_expected_value = FieldIsConstant(acc%accumulation_field, expected_value, _RC)
@assertTrue(field_is_expected_value, 'accumulation_field not equal to expected_value.')
@assertTrue(field_is_expected_value, 'accumulation_field not equal to expected_value. (second test)')

! one update point to undef
expected_value = UPDATE_VALUE
call acc%initialize(importState, exportState, clock, _RC)
call assign_fptr(update_field, upPtr, _RC)
call assign_fptr(acc%accumulation_field, accPtr, _RC)
n = size(upPtr)
call set_undef(upPtr(n))
call acc%accumulate_R4(update_field, _RC)
@assertTrue(undef(accPtr(n)), 'invalid point is not UNDEF.')
@assertTrue(all(pack(accPtr, .not. undef(accPtr)) == expected_value), 'valid point not equal to expected value. (update undef)')

! one accumulation point to undef
call acc%initialize(importState, exportState, clock, _RC)
call assign_fptr(update_field, upPtr, _RC)
upPtr = UPDATE_VALUE
call assign_fptr(acc%accumulation_field, accPtr, _RC)
accPtr = INITIAL_VALUE
n = size(accPtr)
call set_undef(accPtr(n))
call acc%accumulate_R4(update_field, _RC)
expected_value = INITIAL_VALUE + UPDATE_VALUE
@assertTrue(undef(accPtr(n)), 'invalid point is not UNDEF.')
@assertTrue(all(pack(accPtr, .not. undef(accPtr)) == expected_value), 'valid point not equal to expected value. (accumulation undef)')

call ESMF_FieldDestroy(update_field, _RC)
call destroy_objects(importState, exportState, clock, _RC)

Expand Down
12 changes: 7 additions & 5 deletions generic3g/tests/Test_MaxAction.pf
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ module Test_MaxAction
use mapl3g_MaxAction
use accumulator_action_test_common
use esmf
use funit
use MAPL_FieldUtils
use pfunit
use ESMF_TestMethod_mod
implicit none

contains

@Test
subroutine test_max_accumulate_R4()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_max_accumulate_R4(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MaxAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -27,7 +29,7 @@ contains
call set_undef(undef_value)
call initialize_objects(importState, exportState, clock, tk, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call initialize_field(update_field, typekind=tk, _RC)
call initialize_field(update_field, acc%accumulation_field, _RC)
call assign_fptr(acc%accumulation_field, accPtr, _RC)
call assign_fptr(update_field, upPtr, _RC)
n = size(upPtr)
Expand All @@ -36,7 +38,7 @@ contains
upPtr(i:n) = [UPDATE_VALUE, undef_value, UPDATE_VALUE, UPDATE_VALUE+ACCUMULATED_VALUE]
expected = [UPDATE_VALUE, ACCUMULATED_VALUE, ACCUMULATED_VALUE, UPDATE_VALUE+ACCUMULATED_VALUE]
call acc%accumulate_R4(update_field, _RC)
@assertEqual(expected, accPtr, 'accumulated_field not equal to expected values')
@assertEqual(expected, accPtr(i:n), 'accumulation_field not equal to expected values')
call ESMF_FieldDestroy(update_field, _RC)
call destroy_objects(importState, exportState, clock, _RC)

Expand Down
77 changes: 43 additions & 34 deletions generic3g/tests/Test_MeanAction.pf
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "MAPL_TestErr.h"
module Test_MeanAction

use mapl3g_MeanAction
use accumulator_action_test_common
use esmf
use funit
use pfunit
use MAPL_FieldUtils
use ESMF_TestMethod_mod
implicit none

contains

@Test
subroutine test_calculate_mean_R4()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_calculate_mean_R4(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand Down Expand Up @@ -46,11 +47,13 @@ contains
call acc%calculate_mean_R4(_RC)
@assertTrue(all(pack(fptr, mask) == MEAN), 'Some valid points not equal to MEAN')
@assertTrue(undef(fptr(n)), 'mean at point was not UNDEF')
call destroy_objects(importState, exportState, clock, _RC)

end subroutine test_calculate_mean_R4

@Test
subroutine test_clear()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_clear(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -71,8 +74,9 @@ contains

end subroutine test_clear

@Test
subroutine test_invalidate()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_invalidate(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -97,49 +101,51 @@ contains
counter_is_set = all(fptr == N)
@assertTrue(counter_is_set, 'counter_scalar not equal to N')
call destroy_objects(importState, exportState, clock, _RC)
call ESMF_FieldDestroy(importField)

end subroutine test_invalidate

subroutine test_accumulate_mean_R4()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_accumulate_mean_R4(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer :: status
type(ESMF_Field) :: update_field
real(kind=ESMF_KIND_R4), pointer :: upPtr(:), accPtr(:)
real(kind=ESMF_KIND_R4), parameter :: IMPORT_VALUE = 2.0_R4
real(kind=ESMF_KIND_R4), parameter :: UPDATE_VALUE = 3.0_R4
real(kind=ESMF_KIND_R4) :: result_value = IMPORT_VALUE
type(ESMF_Field) :: update_field
real(kind=ESMF_KIND_R4), pointer :: upPtr(:) => null()
real(kind=ESMF_KIND_R4), pointer :: accPtr(:) => null()
integer(kind=I4), pointer :: countPtr(:) => null()
integer(kind=I4), allocatable :: expected_count(:)
integer :: n
type(ESMF_Field) :: importField

call initialize_objects(importState, exportState, clock, ESMF_TYPEKIND_R4, _RC)
call get_field(importState, importField, _RC)
call FieldSet(importField, IMPORT_VALUE, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call initialize_field(update_field, typekind=ESMF_TYPEKIND_R4, _RC)
call initialize_field(update_field, acc%accumulation_field, _RC)
! set update field
call FieldSet(update_field, UPDATE_VALUE, _RC)
call assign_fptr(update_field, upPtr, _RC)
upPtr = UPDATE_VALUE

! update_field not undef
! set last element of update field to UNDEF
n = size(upPtr)
call set_undef(upPtr(n))
! run subroutine to test
call acc%accumulate_R4(update_field, _RC)
result_value = result_value + UPDATE_VALUE
call assign_fptr(acc%accumulation_field, accPtr, _RC)
@assertTrue(all(accPtr == result_value), 'accumulation_field not equal to expected value.')
call assign_fptr(acc%counter_field, countPtr, _RC)
allocate(expected_count(size(countPtr)))
expected_count = 1_I4
expected_count(n) = 0_I4
@assertEqual(expected_count, countPtr, 'Counts do not match.')

! update_field undef at point
call FieldSet(importField, result_value, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call acc%accumulate_R4(update_field, _RC)
result_value = result_value + UPDATE_VALUE
@assertTrue(undef(accPtr(n)), 'invalid point is not UNDEF')
@assertTrue(all(pack(accPtr, .not. undef(upPtr)) == result_value), 'valid point not equal to expected value.')
call ESMF_FieldDestroy(update_field)
call destroy_objects(importState, exportState, clock, _RC)

end subroutine test_accumulate_mean_R4

@Test
subroutine test_initialize()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_initialize(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -156,8 +162,9 @@ contains

end subroutine test_initialize

@Test
subroutine test_accumulate_with_undef_some_steps()
@Test(type=ESMF_TestMethod, npes=[1])
subroutine test_accumulate_with_undef_some_steps(this)
class(ESMF_TestMethod), intent(inout) :: this
type(MeanAction) :: acc
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
Expand All @@ -171,7 +178,7 @@ contains

call initialize_objects(importState, exportState, clock, ESMF_TYPEKIND_R4, _RC)
call acc%initialize(importState, exportState, clock, _RC)
call initialize_field(update_field, typekind=ESMF_TYPEKIND_R4, _RC)
call initialize_field(update_field, acc%accumulation_field, _RC)
call assign_fptr(update_field, upPtr, _RC)
upPtr = UPDATE_VALUE
allocate(mask(size(upPtr)))
Expand All @@ -198,6 +205,8 @@ contains
call assign_fptr(acc%accumulation_field, accPtr, _RC)
@assertEqual(4*UPDATE_VALUE, accPtr(n), 'Missing point does not match.')
@assertTrue(all(pack(accPtr, mask) == 5*UPDATE_VALUE), 'Other points do not match.')
call destroy_objects(importState, exportState, clock, _RC)
call ESMF_FieldDestroy(update_field)

end subroutine test_accumulate_with_undef_some_steps

Expand Down
Loading

0 comments on commit 2f6f781

Please sign in to comment.