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

Update to cohort insertion and sorting routines #1317

Open
wants to merge 54 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
789ecab
initial test
Dec 12, 2024
2b01354
Merge branch 'cmake_bug_fix' into lai_test
Dec 12, 2024
b1ea3a3
lai test
Dec 16, 2024
86f8551
add factory
Dec 30, 2024
b0a316c
add cohort factory
Dec 30, 2024
85837dd
add cohort test
Dec 30, 2024
b03b09d
fixing inits
Jan 2, 2025
ffe501d
add cohort and patch tests
Jan 3, 2025
72bf14e
update tests
Jan 6, 2025
ef8db94
add timing and random modules
Jan 7, 2025
3f4c8ac
update testing
Jan 7, 2025
017e5d5
add more patch types
Jan 7, 2025
d4563be
add more patch types
Jan 7, 2025
81e2f78
new insert and sort methods
Jan 8, 2025
7613837
update tests
Jan 8, 2025
623ce63
merge in main
Jan 8, 2025
e9aa847
put back tree lai
Jan 8, 2025
3853182
put back tree lai
Jan 8, 2025
c08819f
update names
Jan 8, 2025
d6b4109
add some tests
adrifoster Jan 8, 2025
bdbe443
testing
adrifoster Jan 10, 2025
05f8794
add unit tests
Jan 10, 2025
b07e3e7
merge in changes
Jan 10, 2025
a66b53c
update
adrifoster Jan 10, 2025
86ed336
testing
adrifoster Jan 10, 2025
424be20
update
adrifoster Jan 10, 2025
c173889
add new insertcohort
Jan 14, 2025
d870f88
fix indentation
Jan 14, 2025
b9a46b7
update
Jan 15, 2025
d2a93bc
testing
adrifoster Jan 15, 2025
464441e
merge in changes
adrifoster Jan 15, 2025
9d03446
remove old sort
Jan 15, 2025
96df856
remove nullify
adrifoster Jan 15, 2025
d81ca0f
both changes
adrifoster Jan 15, 2025
fcf3f7c
update to new routine
adrifoster Jan 16, 2025
63232ef
remove check
adrifoster Jan 21, 2025
6ec0248
update tests
adrifoster Jan 22, 2025
07b3f99
update
Jan 22, 2025
a0b1c60
Merge branch 'patch_testing' of https://github.com/adrifoster/fates i…
Jan 22, 2025
b47f601
add more unit tests
Jan 22, 2025
8aa66d1
more unit tests
Jan 22, 2025
e7ae132
add old and new tests
Jan 22, 2025
f3ccab2
Merge branch 'main' into patch_testing
adrifoster Jan 22, 2025
cccb7f3
move to new methods
adrifoster Jan 22, 2025
b147cd3
remove old routines
Jan 23, 2025
8af82c5
get rid of tooling
Jan 23, 2025
c3ddfce
clean up
Jan 23, 2025
fded3f2
remove unused variable
adrifoster Jan 23, 2025
cb1276a
add more units
adrifoster Jan 23, 2025
a339670
update for tests
adrifoster Jan 26, 2025
491c948
more tests
adrifoster Jan 27, 2025
18f3602
some requested changes
Jan 28, 2025
5d5b086
add helper method for patch testing
Jan 28, 2025
53df550
add more tests
Jan 29, 2025
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
1 change: 1 addition & 0 deletions biogeochem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ list(APPEND fates_sources
FatesCohortMod.F90
FatesAllometryMod.F90
DamageMainMod.F90
EDCohortDynamicsMod.F90
FatesPatchMod.F90)

sourcelist_to_parent(fates_sources)
481 changes: 104 additions & 377 deletions biogeochem/EDCohortDynamicsMod.F90

Large diffs are not rendered by default.

110 changes: 18 additions & 92 deletions biogeochem/EDPatchDynamicsMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module EDPatchDynamicsMod
use EDPftvarcon , only : EDPftvarcon_inst
use EDPftvarcon , only : GetDecompyFrac
use PRTParametersMod , only : prt_params
use EDCohortDynamicsMod , only : fuse_cohorts, sort_cohorts, insert_cohort
use EDCohortDynamicsMod , only : fuse_cohorts
use EDTypesMod , only : area_site => area
use ChecksBalancesMod , only : PatchMassStock
use FatesLitterMod , only : ncwd
Expand Down Expand Up @@ -524,17 +524,13 @@ subroutine spawn_patches( currentSite, bc_in)
type (fates_patch_type) , pointer :: currentPatch
type (fates_cohort_type), pointer :: currentCohort
type (fates_cohort_type), pointer :: nc
type (fates_cohort_type), pointer :: storesmallcohort
type (fates_cohort_type), pointer :: storebigcohort
real(r8) :: site_areadis_primary ! total area disturbed (to primary forest) in m2 per site per day
real(r8) :: site_areadis_secondary ! total area disturbed (to secondary forest) in m2 per site per day
real(r8) :: patch_site_areadis ! total area disturbed in m2 per patch per day
real(r8) :: site_areadis ! total site area disturbed in m2 per day
real(r8) :: age ! notional age of this patch in years
integer :: el ! element loop index
integer :: pft ! pft loop index
integer :: tnull ! is there a tallest cohort?
integer :: snull ! is there a shortest cohort?
integer :: levcan ! canopy level
real(r8) :: leaf_c ! leaf carbon [kg]
real(r8) :: fnrt_c ! fineroot carbon [kg]
Expand Down Expand Up @@ -570,9 +566,6 @@ subroutine spawn_patches( currentSite, bc_in)
logical :: buffer_patch_used
!---------------------------------------------------------------------

storesmallcohort => null() ! storage of the smallest cohort for insertion routine
storebigcohort => null() ! storage of the largest cohort for insertion routine

if (hlm_use_nocomp .eq. itrue) then
min_nocomp_pft = 0
max_nocomp_pft = numpft
Expand Down Expand Up @@ -1257,30 +1250,7 @@ subroutine spawn_patches( currentSite, bc_in)
! if some plants in the new temporary cohort survived the transfer to the new patch,
! then put the cohort into the linked list.
cohort_n_gt_zero: if (nc%n > 0.0_r8) then
storebigcohort => newPatch%tallest
storesmallcohort => newPatch%shortest
if(associated(newPatch%tallest))then
tnull = 0
else
tnull = 1
newPatch%tallest => nc
nc%taller => null()
endif

if(associated(newPatch%shortest))then
snull = 0
else
snull = 1
newPatch%shortest => nc
nc%shorter => null()
endif

call insert_cohort(newPatch, nc, newPatch%tallest, newPatch%shortest, &
tnull, snull, storebigcohort, storesmallcohort)

newPatch%tallest => storebigcohort
newPatch%shortest => storesmallcohort

call newPatch%InsertCohort(nc)
else
! sadly, no plants in the cohort survived. on the bright side, we can deallocate their memory.
call nc%FreeMemory()
Expand All @@ -1293,8 +1263,10 @@ subroutine spawn_patches( currentSite, bc_in)

currentCohort => currentCohort%taller
enddo cohortloop
call newPatch%ValidateCohorts()

call sort_cohorts(currentPatch)
call currentPatch%SortCohorts()
call currentPatch%ValidateCohorts()

!update area of donor patch
oldarea = currentPatch%area
Expand Down Expand Up @@ -1325,7 +1297,8 @@ subroutine spawn_patches( currentSite, bc_in)
call terminate_cohorts(currentSite, currentPatch, 1,16,bc_in)
call fuse_cohorts(currentSite,currentPatch, bc_in)
call terminate_cohorts(currentSite, currentPatch, 2,16,bc_in)
call sort_cohorts(currentPatch)
call currentPatch%SortCohorts()
call currentPatch%ValidateCohorts()

end if areadis_gt_zero_if ! if ( newPatch%area > nearzero ) then

Expand All @@ -1352,7 +1325,8 @@ subroutine spawn_patches( currentSite, bc_in)
call terminate_cohorts(currentSite, newPatch, 1,17, bc_in)
call fuse_cohorts(currentSite,newPatch, bc_in)
call terminate_cohorts(currentSite, newPatch, 2,17, bc_in)
call sort_cohorts(newPatch)
call newPatch%SortCohorts()
call newPatch%ValidateCohorts()
endif


Expand Down Expand Up @@ -1683,11 +1657,7 @@ subroutine split_patch(currentSite, currentPatch, new_patch, fraction_to_keep, a
! !LOCAL VARIABLES:
integer :: el ! element loop index
type (fates_cohort_type), pointer :: nc
type (fates_cohort_type), pointer :: storesmallcohort
type (fates_cohort_type), pointer :: storebigcohort
type (fates_cohort_type), pointer :: currentCohort
integer :: tnull ! is there a tallest cohort?
integer :: snull ! is there a shortest cohort?
integer :: pft
real(r8) :: temp_area

Expand Down Expand Up @@ -1754,34 +1724,14 @@ subroutine split_patch(currentSite, currentPatch, new_patch, fraction_to_keep, a
! loss of individuals from source patch due to area shrinking
currentCohort%n = currentCohort%n * fraction_to_keep

storebigcohort => new_patch%tallest
storesmallcohort => new_patch%shortest
if(associated(new_patch%tallest))then
tnull = 0
else
tnull = 1
new_patch%tallest => nc
nc%taller => null()
endif

if(associated(new_patch%shortest))then
snull = 0
else
snull = 1
new_patch%shortest => nc
nc%shorter => null()
endif

call insert_cohort(new_patch, nc, new_patch%tallest, new_patch%shortest, &
tnull, snull, storebigcohort, storesmallcohort)

new_patch%tallest => storebigcohort
new_patch%shortest => storesmallcohort
call new_patch%InsertCohort(nc)

currentCohort => currentCohort%taller
enddo ! currentCohort
call new_patch%ValidateCohorts()

call sort_cohorts(currentPatch)
call currentPatch%SortCohorts()
call currentPatch%ValidateCohorts()

!update area of donor patch
currentPatch%area = currentPatch%area - temp_area
Expand Down Expand Up @@ -3124,7 +3074,8 @@ subroutine fuse_patches( csite, bc_in )
tmpptr => currentPatch%older
call fuse_2_patches(csite, currentPatch, tpp)
call fuse_cohorts(csite,tpp, bc_in)
call sort_cohorts(tpp)
call tpp%SortCohorts()
call tpp%ValidateCohorts()
currentPatch => tmpptr

!------------------------------------------------------------------------!
Expand Down Expand Up @@ -3246,10 +3197,7 @@ subroutine fuse_2_patches(csite, dp, rp)
! !LOCAL VARIABLES:
type (fates_cohort_type), pointer :: currentCohort ! Current Cohort
type (fates_cohort_type), pointer :: nextc ! Remembers next cohort in list
type (fates_cohort_type), pointer :: storesmallcohort
type (fates_cohort_type), pointer :: storebigcohort
integer :: c,p !counters for pft and litter size class.
integer :: tnull,snull ! are the tallest and shortest cohorts associated?
integer :: el ! loop counting index for elements
integer :: pft ! loop counter for pfts
type(fates_patch_type), pointer :: youngerp ! pointer to the patch younger than donor
Expand Down Expand Up @@ -3329,31 +3277,8 @@ subroutine fuse_2_patches(csite, dp, rp)
endif

do while(associated(dp%shortest))

storebigcohort => rp%tallest
storesmallcohort => rp%shortest

if(associated(rp%tallest))then
tnull = 0
else
tnull = 1
rp%tallest => currentCohort
endif

if(associated(rp%shortest))then
snull = 0
else
snull = 1
rp%shortest => currentCohort
endif

call insert_cohort(rp, currentCohort, rp%tallest, rp%shortest, &
tnull, snull, storebigcohort, storesmallcohort)

rp%tallest => storebigcohort
rp%shortest => storesmallcohort

!currentCohort%patchptr => rp

call rp%InsertCohort(currentCohort)

currentCohort => nextc

Expand All @@ -3364,6 +3289,7 @@ subroutine fuse_2_patches(csite, dp, rp)
endif

enddo !cohort
call rp%ValidateCohorts()
endif !are there any cohorts?

call patch_pft_size_profile(rp) ! Recalculate the patch size profile for the resulting patch
Expand Down
5 changes: 3 additions & 2 deletions biogeochem/EDPhysiologyMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module EDPhysiologyMod
use EDPftvarcon , only : GetDecompyFrac
use FatesInterfaceTypesMod, only : bc_in_type
use FatesInterfaceTypesMod, only : bc_out_type
use EDCohortDynamicsMod , only : create_cohort, sort_cohorts
use EDCohortDynamicsMod , only : create_cohort
use EDCohortDynamicsMod , only : InitPRTObject
use FatesAllometryMod , only : tree_lai_sai
use FatesAllometryMod , only : leafc_from_treelai
Expand Down Expand Up @@ -681,7 +681,7 @@ subroutine trim_canopy( currentSite )
! Add debug diagnstic output to determine which patch
if (debug) then
write(fates_log(),*) 'Current patch:', ipatch
write(fates_log(),*) 'Current patch cohorts:', currentPatch%countcohorts
write(fates_log(),*) 'Current patch cohorts:', currentPatch%num_cohorts
endif

icohort = 1
Expand Down Expand Up @@ -2791,6 +2791,7 @@ subroutine recruitment(currentSite, currentPatch, bc_in)
endif any_recruits
endif use_this_pft_if
enddo !pft loop
call currentPatch%ValidateCohorts()
end subroutine recruitment

! ======================================================================================
Expand Down
9 changes: 4 additions & 5 deletions biogeochem/FatesCohortMod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module FatesCohortMod

implicit none
private

! PARAMETERS
character(len=*), parameter, private :: sourcefile = __FILE__

Expand All @@ -77,7 +77,6 @@ module FatesCohortMod
!---------------------------------------------------------------------------

! VEGETATION STRUCTURE

integer :: pft ! pft index
real(r8) :: n ! number of individuals in cohort per 'area' (10000m2 default) [/m2]
real(r8) :: dbh ! diameter at breast height [cm]
Expand Down Expand Up @@ -450,7 +449,7 @@ subroutine NanValues(this)
this%cambial_mort = nan
this%crownfire_mort = nan
this%fire_mort = nan

end subroutine NanValues

!===========================================================================
Expand Down Expand Up @@ -547,7 +546,7 @@ subroutine Create(this, prt, pft, nn, height, coage, dbh, status, &
!
! DESCRIPTION:
! set up values for a newly created cohort

! ARGUMENTS
class(fates_cohort_type), intent(inout), target :: this ! cohort object
class(prt_vartypes), intent(inout), pointer :: prt ! The allocated PARTEH object
Expand All @@ -574,7 +573,7 @@ subroutine Create(this, prt, pft, nn, height, coage, dbh, status, &

! initialize cohort
call this%Init(prt)

! set values
this%pft = pft
this%crowndamage = crowndamage
Expand Down
Loading