#include "MAPL_Generic.h" module Test_StateArithmetic use state_utils_setup use ESMF use pfunit use MAPL_ExceptionHandling use MAPL_StateUtils use ESMF_TestMethod_mod implicit none contains @Before subroutine set_up_data(this) class(ESMF_TestMethod), intent(inout) :: this integer :: status, rc grid = ESMF_GridCreateNoPeriDim(countsPerDeDim1=[3], countsPerDeDim2=[3], _RC) field_2d = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R4, name="field_2d", _RC) field_3d = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R4, name="field_3d",ungriddedLBound=[1],ungriddedUBound=[2], _RC) extra_2d = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R4, name="extra_2d", _RC) extra_3d = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R4, name="extra_3d",ungriddedLBound=[1],ungriddedUBound=[2], _RC) mask_field = ESMF_FieldCreate(grid, ESMF_TYPEKIND_R4, name="region_mask", _RC) state = ESMF_StateCreate(fieldList=[field_2d,field_3d,mask_field,extra_2d,extra_3d], _RC) end subroutine set_up_data @after subroutine teardown(this) class(ESMF_TestMethod), intent(inout) :: this call ESMF_FieldDestroy(field_2d, noGarbage=.true.) call ESMF_FieldDestroy(field_3d, noGarbage=.true.) call ESMF_FieldDestroy(extra_2d, noGarbage=.true.) call ESMF_FieldDestroy(extra_3d, noGarbage=.true.) call ESMF_FieldDestroy(mask_field, noGarbage=.true.) call ESMF_StateDestroy(state, noGarbage=.true.) end subroutine teardown @Test(type=ESMF_TestMethod, npes=[1]) subroutine test_arithmetic_2d(this) class(ESMF_TestMethod), intent(inout) :: this integer :: status, rc real(ESMF_KIND_R4), pointer :: ptr2d(:,:), extra_ptr(:,:) real(ESMF_KIND_R4), allocatable :: expected_array(:,:) real(ESMF_KIND_R4) :: rval character(len=:), allocatable :: expr call ESMF_FieldGet(extra_2d, 0, farrayPtr=extra_ptr, _RC) call ESMF_FieldGet(field_2d, 0, farrayPtr=ptr2d, _RC) expr = "field_2d+2.0*sqrt(extra_2d)" rval = 17.0 ptr2d = rval rval = 16.0 extra_ptr = rval allocate(expected_array(3,3),_STAT) expected_array = 17.0 + 2.0*sqrt(16.0) call MAPL_StateEval(state, expr, field_2d, _RC) @assertEqual(expected_array, ptr2d) _RETURN(_SUCCESS) end subroutine test_arithmetic_2d @Test(type=ESMF_TestMethod, npes=[1]) subroutine test_arithmetic_3d(this) class(ESMF_TestMethod), intent(inout) :: this integer :: status, rc real(ESMF_KIND_R4), pointer :: ptr3d(:,:,:), extra_ptr(:,:,:) real(ESMF_KIND_R4), allocatable :: expected_array(:,:,:) real(ESMF_KIND_R4) :: rval character(len=:), allocatable :: expr call ESMF_FieldGet(extra_3d, 0, farrayPtr=extra_ptr, _RC) call ESMF_FieldGet(field_3d, 0, farrayPtr=ptr3d, _RC) expr = "field_3d+2.0*sqrt(extra_3d)" rval = 17.0 ptr3d = rval rval = 16.0 extra_ptr = rval allocate(expected_array(3,3,2),_STAT) expected_array = 17.0 + 2.0*sqrt(16.0) call MAPL_StateEval(state, expr, field_3d, _RC) @assertEqual(expected_array, ptr3d) _RETURN(_SUCCESS) end subroutine test_arithmetic_3d @Test(type=ESMF_TestMethod, npes=[1]) subroutine test_arithmetic_mixed(this) class(ESMF_TestMethod), intent(inout) :: this integer :: status, rc real(ESMF_KIND_R4), pointer :: ptr3d(:,:,:), extra_ptr(:,:) real(ESMF_KIND_R4), allocatable :: expected_array(:,:,:) real(ESMF_KIND_R4) :: rval character(len=:), allocatable :: expr call ESMF_FieldGet(extra_2d, 0, farrayPtr=extra_ptr, _RC) call ESMF_FieldGet(field_3d, 0, farrayPtr=ptr3d, _RC) expr = "field_3d*extra_2d" rval = 5.0 ptr3d = rval rval = 3.0 extra_ptr = rval allocate(expected_array(3,3,2),_STAT) expected_array = 15.0 call MAPL_StateEval(state, expr, field_3d, _RC) @assertEqual(expected_array, ptr3d) _RETURN(_SUCCESS) end subroutine test_arithmetic_mixed end module Test_StateArithmetic