From 021454173d3c7c414687bcbe92ccb88876a84269 Mon Sep 17 00:00:00 2001 From: Gregory Lemieux Date: Tue, 26 Nov 2024 12:58:57 -0800 Subject: [PATCH 1/5] initial commit of parteh unit test This adds a single parteh test for the CNP allometric mod --- testing/CMakeLists.txt | 1 + .../unit_testing/parteh_test/CMakeLists.txt | 5 + .../parteh_test/test_PRTAllometricCNP.pf | 98 +++++++++++++++++++ testing/unit_tests.cfg | 3 + 4 files changed, 107 insertions(+) create mode 100644 testing/unit_testing/parteh_test/CMakeLists.txt create mode 100644 testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt index 7c19f7cc99..2f4e2cb497 100644 --- a/testing/CMakeLists.txt +++ b/testing/CMakeLists.txt @@ -8,3 +8,4 @@ add_subdirectory(functional_testing/fire fates_fuel_ftest) ## Unit tests add_subdirectory(unit_testing/fire_weather_test fates_fire_weather_utest) add_subdirectory(unit_testing/fire_fuel_test fates_fire_fuel_utest) +add_subdirectory(unit_testing/parteh_test fates_parteh_utest) diff --git a/testing/unit_testing/parteh_test/CMakeLists.txt b/testing/unit_testing/parteh_test/CMakeLists.txt new file mode 100644 index 0000000000..a29b276e47 --- /dev/null +++ b/testing/unit_testing/parteh_test/CMakeLists.txt @@ -0,0 +1,5 @@ +set(pfunit_sources test_PRTAllometricCNP.pf) + +add_pfunit_ctest(PRTAllometricCNP + TEST_SOURCES "${pfunit_sources}" + LINK_LIBRARIES fates csm_share) diff --git a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf new file mode 100644 index 0000000000..f7e2d6ab07 --- /dev/null +++ b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf @@ -0,0 +1,98 @@ +module test_PRTAllometricCNP + ! + ! DESCRIPTION: + ! Test the FATES PRTAllometricCNPMod subroutines + ! + use funit + use FatesConstantsMod, only : r8 => fates_r8 + use FatesConstantsMod, only : default_regeneration + use FatesConstantsMod, only : mm_per_cm + use FatesConstantsMod, only : min_max_dbh_for_trees + use PRTParametersMod, only : prt_params + use PRTGenericMod, only : RegisterBCInOut + use PRTGenericMod, only : RegisterBCIn + use PRTGenericMod, only : store_organ + use PRTAllometricCNPMod, only : cnp_allom_prt_vartypes + use PRTAllometricCNPMod, only : stoich_growth_min + use PRTAllometricCNPMod, only : repro_id + use PRTAllometricCNPMod, only : acnp_bc_inout_id_dbh + use PRTAllometricCNPMod, only : acnp_bc_in_id_pft + use PRTAllometricCNPMod, only : acnp_bc_in_id_nc_repro + use PRTAllometricCNPMod, only : acnp_bc_in_id_pc_repro + + implicit none + + @TestCase + type, extends(TestCase) :: TestPRTAllomCNP + type(cnp_allom_prt_vartypes) :: cnp_allom_prt + contains + procedure :: setUp + end type TestPRTAllomCNP + + ! assertEqual tolerance + real(r8), parameter :: tol = 1.e-13_r8 + + ! Globals and parameters + regeneration_model = default_regeneration + + contains + + !subroutine setUp(this) + ! class(TestPRTAllomCNP), intent(inout) :: this + !end subroutine setUp + + @Test + subroutine EstimateGrowthNC_ReproBoundsCheck(this) + + ! Test that CN growth estimate passes if the C reproductive fraction is exactly one + + class(TestPRTAllomCNP), intent(inout) :: this ! test object + + !! Globals + ! Initialize the prt_param type and values + allocate(prt_params%allom_dbh_maxheight(acnp_bc_in_id_pft)) + allocate(prt_params%dbh_repro_threshold(acnp_bc_in_id_pft)) + allocate(prt_params%seed_alloc(acnp_bc_in_id_pft)) + prt_params%allom_dbh_maxheight(acnp_bc_in_id_pft) = 1000._r8 ! current default param + prt_params%dbh_repro_threshold(acnp_bc_in_id_pft) = 90._r8 ! current default param + prt_params%seed_alloc(acnp_bc_in_id_pft) = 1._r8 ! assigned to repro_c_frac + + ! Unused for repro check + allocate(prt_params%seed_alloc_mature(acnp_bc_in_id_pft)) + allocate(prt_params%repro_alloc_b(acnp_bc_in_id_pft)) + allocate(prt_params%repro_alloc_a(acnp_bc_in_id_pft)) + allocate(prt_params%organ_param_id(1)) + allocate(prt_params%nitr_stoich_p1(1,1)) + allocate(prt_params%phos_stoich_p1(1,1)) + + ! Manually initialize minimum necessary PRT vartype subtypes + allocate(this%cnp_allom_prt%bc_inout(acnp_bc_inout_id_dbh)) + allocate(this%cnp_allom_prt%bc_in(acnp_bc_in_id_pc_repro)) + this%cnp_allom_prt%RegisterBCInOut(acnp_bc_inout_id_dbh, bc_rval=1._r8) ! acnp_bc_inout_id_dbh = 1 + this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_pft, bc_ival=1) ! acnp_bc_in_id_pft = 1 + this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_nc_repro, bc_ival=0._r8) ! acnp_bc_in_id_nc_repro = 5 + this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_pc_repro, bc_ival=0._r8) ! acnp_bc_in_id_pc_repro = 6 + + !! Arguments + ! Dummy arguments, not used for repro_id case + real(r8) :: dummy_target_c(1) = 0._r8 + real(r8) :: dummy_target_dcdd(1) = 0._r8 + + ! Necessary arguments + logical :: state_mask(repro_id) = .false. ! state mask, intent(in) + real(r8) :: avg_nc ! Average N:C ratio, intent(out) + real(r8) :: avg_pc ! Average P:C ratio, intent(out) + + ! only test the repro_id case + state_mask(repro_id) = .true. + + call EstimateGrowthNC(this%cnp_allom_prt,dummy_target_c,dumm_target_dcdd, & + state_mask,avg_nc,avg_pc) + + @assertEqual(avg_nc, 1._r8, tolerance=tol) + @assertEqual(avg_pc, 1._r8, tolerance=tol) + + end subroutine EstimateGrowthNC_BoundsCheck + + +end module test_PRTAllometricCNP diff --git a/testing/unit_tests.cfg b/testing/unit_tests.cfg index 179b924735..cd776e6130 100644 --- a/testing/unit_tests.cfg +++ b/testing/unit_tests.cfg @@ -3,3 +3,6 @@ test_dir = fates_fire_weather_utest [fire_fuel] test_dir = fates_fire_fuel_utest + +[parteh] +test_dir = fates_parteh_utest From 6f4c5b5036e9865296edc064db47688889fac364 Mon Sep 17 00:00:00 2001 From: Gregory Lemieux Date: Tue, 26 Nov 2024 15:26:51 -0700 Subject: [PATCH 2/5] remove unused prt register subroutine --- testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf | 2 -- 1 file changed, 2 deletions(-) diff --git a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf index f7e2d6ab07..41679441ac 100644 --- a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf +++ b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf @@ -9,8 +9,6 @@ module test_PRTAllometricCNP use FatesConstantsMod, only : mm_per_cm use FatesConstantsMod, only : min_max_dbh_for_trees use PRTParametersMod, only : prt_params - use PRTGenericMod, only : RegisterBCInOut - use PRTGenericMod, only : RegisterBCIn use PRTGenericMod, only : store_organ use PRTAllometricCNPMod, only : cnp_allom_prt_vartypes use PRTAllometricCNPMod, only : stoich_growth_min From 7759ab6c9b9262b5f0ed5f22801570948edf967e Mon Sep 17 00:00:00 2001 From: Gregory Lemieux Date: Tue, 26 Nov 2024 16:18:02 -0700 Subject: [PATCH 3/5] fix order of statements and allocations --- .../parteh_test/test_PRTAllometricCNP.pf | 49 +++++++++++-------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf index 41679441ac..0ba128ec74 100644 --- a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf +++ b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf @@ -9,10 +9,9 @@ module test_PRTAllometricCNP use FatesConstantsMod, only : mm_per_cm use FatesConstantsMod, only : min_max_dbh_for_trees use PRTParametersMod, only : prt_params - use PRTGenericMod, only : store_organ + use PRTGenericMod, only : store_organ use PRTAllometricCNPMod, only : cnp_allom_prt_vartypes use PRTAllometricCNPMod, only : stoich_growth_min - use PRTAllometricCNPMod, only : repro_id use PRTAllometricCNPMod, only : acnp_bc_inout_id_dbh use PRTAllometricCNPMod, only : acnp_bc_in_id_pft use PRTAllometricCNPMod, only : acnp_bc_in_id_nc_repro @@ -23,20 +22,18 @@ module test_PRTAllometricCNP @TestCase type, extends(TestCase) :: TestPRTAllomCNP type(cnp_allom_prt_vartypes) :: cnp_allom_prt - contains - procedure :: setUp + !contains + ! procedure :: setUp end type TestPRTAllomCNP ! assertEqual tolerance real(r8), parameter :: tol = 1.e-13_r8 - ! Globals and parameters - regeneration_model = default_regeneration - contains !subroutine setUp(this) ! class(TestPRTAllomCNP), intent(inout) :: this + ! ! Globals and parameters !end subroutine setUp @Test @@ -47,10 +44,29 @@ module test_PRTAllometricCNP class(TestPRTAllomCNP), intent(inout) :: this ! test object !! Globals + ! targets for associations + integer, parameter :: repro_id = 5 ! non-public parameter from PRTAllometricCNPMod + integer :: regeneration_model = default_regeneration + integer, target :: ipft = 1 + real(r8), target :: dbh = 1._r8 + real(r8), target :: nc_repro = 0._r8 + real(r8), target :: pc_repro = 0._r8 + + !! subroutine arguments + ! Dummy arguments, not used for repro_id case + real(r8) :: dummy_target_c(1) = 0._r8 + real(r8) :: dummy_target_dcdd(1) = 0._r8 + + ! Necessary arguments + logical :: state_mask(repro_id) = .false. ! state mask, intent(in) + real(r8) :: avg_nc ! Average N:C ratio, intent(out) + real(r8) :: avg_pc ! Average P:C ratio, intent(out) + ! Initialize the prt_param type and values allocate(prt_params%allom_dbh_maxheight(acnp_bc_in_id_pft)) allocate(prt_params%dbh_repro_threshold(acnp_bc_in_id_pft)) allocate(prt_params%seed_alloc(acnp_bc_in_id_pft)) + prt_params%allom_dbh_maxheight(acnp_bc_in_id_pft) = 1000._r8 ! current default param prt_params%dbh_repro_threshold(acnp_bc_in_id_pft) = 90._r8 ! current default param prt_params%seed_alloc(acnp_bc_in_id_pft) = 1._r8 ! assigned to repro_c_frac @@ -66,20 +82,11 @@ module test_PRTAllometricCNP ! Manually initialize minimum necessary PRT vartype subtypes allocate(this%cnp_allom_prt%bc_inout(acnp_bc_inout_id_dbh)) allocate(this%cnp_allom_prt%bc_in(acnp_bc_in_id_pc_repro)) - this%cnp_allom_prt%RegisterBCInOut(acnp_bc_inout_id_dbh, bc_rval=1._r8) ! acnp_bc_inout_id_dbh = 1 - this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_pft, bc_ival=1) ! acnp_bc_in_id_pft = 1 - this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_nc_repro, bc_ival=0._r8) ! acnp_bc_in_id_nc_repro = 5 - this%cnp_allom_prt%RegisterBCIn(acnp_bc_in_id_pc_repro, bc_ival=0._r8) ! acnp_bc_in_id_pc_repro = 6 - - !! Arguments - ! Dummy arguments, not used for repro_id case - real(r8) :: dummy_target_c(1) = 0._r8 - real(r8) :: dummy_target_dcdd(1) = 0._r8 - ! Necessary arguments - logical :: state_mask(repro_id) = .false. ! state mask, intent(in) - real(r8) :: avg_nc ! Average N:C ratio, intent(out) - real(r8) :: avg_pc ! Average P:C ratio, intent(out) + this%cnp_allom_prt%bc_inout(acnp_bc_inout_id_dbh)%rval => dbh + this%cnp_allom_prt%bc_in(acnp_bc_in_id_pft)%ival => ipft + this%cnp_allom_prt%bc_in(acnp_bc_in_id_nc_repro)%rval => nc_repro + this%cnp_allom_prt%bc_in(acnp_bc_in_id_pc_repro)%rval => pc_repro ! only test the repro_id case state_mask(repro_id) = .true. @@ -90,7 +97,7 @@ module test_PRTAllometricCNP @assertEqual(avg_nc, 1._r8, tolerance=tol) @assertEqual(avg_pc, 1._r8, tolerance=tol) - end subroutine EstimateGrowthNC_BoundsCheck + end subroutine EstimateGrowthNC_ReproBoundsCheck end module test_PRTAllometricCNP From 259e9e6e101e4f30435be8c77dce5348e3af02b7 Mon Sep 17 00:00:00 2001 From: Gregory Lemieux Date: Mon, 2 Dec 2024 15:52:32 -0700 Subject: [PATCH 4/5] correct state_mask initialization --- .../parteh_test/test_PRTAllometricCNP.pf | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf index 0ba128ec74..91de9b6320 100644 --- a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf +++ b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf @@ -45,12 +45,12 @@ module test_PRTAllometricCNP !! Globals ! targets for associations - integer, parameter :: repro_id = 5 ! non-public parameter from PRTAllometricCNPMod - integer :: regeneration_model = default_regeneration - integer, target :: ipft = 1 - real(r8), target :: dbh = 1._r8 - real(r8), target :: nc_repro = 0._r8 - real(r8), target :: pc_repro = 0._r8 + integer, parameter :: repro_id = 5 ! non-public parameter from PRTAllometricCNPMod + integer, parameter :: num_intgr_vars = 7 + integer, target :: ipft = 1 + real(r8), target :: dbh = 1._r8 + real(r8), target :: nc_repro = 0._r8 + real(r8), target :: pc_repro = 0._r8 !! subroutine arguments ! Dummy arguments, not used for repro_id case @@ -58,9 +58,9 @@ module test_PRTAllometricCNP real(r8) :: dummy_target_dcdd(1) = 0._r8 ! Necessary arguments - logical :: state_mask(repro_id) = .false. ! state mask, intent(in) - real(r8) :: avg_nc ! Average N:C ratio, intent(out) - real(r8) :: avg_pc ! Average P:C ratio, intent(out) + logical :: state_mask(num_intgr_vars) = .false. ! state mask, intent(in) + real(r8) :: avg_nc ! Average N:C ratio, intent(out) + real(r8) :: avg_pc ! Average P:C ratio, intent(out) ! Initialize the prt_param type and values allocate(prt_params%allom_dbh_maxheight(acnp_bc_in_id_pft)) From 222daff70163311d7cbaaae7c3f91aab024b2bba Mon Sep 17 00:00:00 2001 From: Gregory Lemieux Date: Mon, 2 Dec 2024 15:53:02 -0700 Subject: [PATCH 5/5] correct call to testable subroutine --- testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf index 91de9b6320..87df978c5e 100644 --- a/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf +++ b/testing/unit_testing/parteh_test/test_PRTAllometricCNP.pf @@ -91,7 +91,7 @@ module test_PRTAllometricCNP ! only test the repro_id case state_mask(repro_id) = .true. - call EstimateGrowthNC(this%cnp_allom_prt,dummy_target_c,dumm_target_dcdd, & + call this%cnp_allom_prt%EstimateGrowthNC(dummy_target_c,dummy_target_dcdd, & state_mask,avg_nc,avg_pc) @assertEqual(avg_nc, 1._r8, tolerance=tol)