From f25e25dae24fd70b81512e59ee451f398fff4a5f Mon Sep 17 00:00:00 2001 From: Ryan Knox Date: Thu, 26 Apr 2018 20:56:42 -0700 Subject: [PATCH] Moved deallocation to outside of conditional. --- biogeochem/EDCohortDynamicsMod.F90 | 52 +++++++++++++++--------------- biogeochem/EDPatchDynamicsMod.F90 | 45 ++++++++++---------------- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/biogeochem/EDCohortDynamicsMod.F90 b/biogeochem/EDCohortDynamicsMod.F90 index 7621cdb1af..892fde5e13 100644 --- a/biogeochem/EDCohortDynamicsMod.F90 +++ b/biogeochem/EDCohortDynamicsMod.F90 @@ -415,7 +415,7 @@ subroutine terminate_cohorts( currentSite, currentPatch, level ) !---------------------------------------------------------------------- - currentCohort => currentPatch%shortest + currentCohort => currentPatch%shortest do while (associated(currentCohort)) terminate = 0 @@ -525,30 +525,30 @@ subroutine terminate_cohorts( currentSite, currentPatch, level ) currentSite%root_litter_diagnostic_input_carbonflux(currentCohort%pft) + & currentCohort%n * (currentCohort%br+currentCohort%bstore) * hlm_days_per_year / AREA - - ! Set pointers and remove the current cohort from the list - - shorterCohort => currentCohort%shorter - - if (.not. associated(tallerCohort)) then - currentPatch%tallest => shorterCohort - if(associated(shorterCohort)) shorterCohort%taller => null() - else - tallerCohort%shorter => shorterCohort - endif - - if (.not. associated(shorterCohort)) then - currentPatch%shortest => tallerCohort - if(associated(tallerCohort)) tallerCohort%shorter => null() - else - shorterCohort%taller => tallerCohort - endif - - ! At this point, nothing should be pointing to current Cohort - if (hlm_use_planthydro.eq.itrue) call DeallocateHydrCohort(currentCohort) - deallocate(currentCohort) - + end if + + ! Set pointers and remove the current cohort from the list + shorterCohort => currentCohort%shorter + + if (.not. associated(tallerCohort)) then + currentPatch%tallest => shorterCohort + if(associated(shorterCohort)) shorterCohort%taller => null() + else + tallerCohort%shorter => shorterCohort + endif + + if (.not. associated(shorterCohort)) then + currentPatch%shortest => tallerCohort + if(associated(tallerCohort)) tallerCohort%shorter => null() + else + shorterCohort%taller => tallerCohort endif + + ! At this point, nothing should be pointing to current Cohort + if (hlm_use_planthydro.eq.itrue) call DeallocateHydrCohort(currentCohort) + deallocate(currentCohort) + nullify(currentCohort) + endif currentCohort => tallerCohort enddo @@ -624,7 +624,7 @@ subroutine fuse_cohorts(currentPatch, bc_in) nextc => currentPatch%tallest do while (associated(nextc)) - nextnextc => nextc%shorter + nextnextc => nextc%shorter diff = abs((currentCohort%dbh - nextc%dbh)/(0.5*(currentCohort%dbh + nextc%dbh))) !Criteria used to divide up the height continuum into different cohorts. @@ -843,7 +843,7 @@ subroutine fuse_cohorts(currentPatch, bc_in) ! At this point, nothing should be pointing to current Cohort if (hlm_use_planthydro.eq.itrue) call DeallocateHydrCohort(nextc) deallocate(nextc) - + nullify(nextc) endif ! if( currentCohort%isnew.eqv.nextc%isnew ) then endif !canopy layer diff --git a/biogeochem/EDPatchDynamicsMod.F90 b/biogeochem/EDPatchDynamicsMod.F90 index 14f63d4045..0a325e91a7 100644 --- a/biogeochem/EDPatchDynamicsMod.F90 +++ b/biogeochem/EDPatchDynamicsMod.F90 @@ -1690,12 +1690,6 @@ subroutine terminate_patches(currentSite) ! !DESCRIPTION: ! Terminate Patches if they are too small ! - ! Note that we are not going to apply this to the youngest patch - ! We do this because the youngest patches are constantly being created - ! and fused with other new patches each day, each with small areas. If we - ! terminate them immediately, then we will constantly be culling off - ! these new areas before they can contribute to the pool. - ! This is really intended for older, established patches. ! ! !ARGUMENTS: type(ed_site_type), target, intent(inout) :: currentSite @@ -1709,41 +1703,36 @@ subroutine terminate_patches(currentSite) !--------------------------------------------------------------------- currentPatch => currentSite%youngest_patch - if(associated(currentPatch%older)) then - - - ! We ignore the youngest patch, immediately go to the next - ! If it is not associated (DNE..) then we are done. - - currentPatch => currentPatch%older - do while(associated(currentPatch)) + do while(associated(currentPatch)) + + if(currentPatch%area <= min_patch_area)then - if(currentPatch%area <= min_patch_area)then + if ( .not.associated(currentPatch,currentSite%youngest_patch) ) then if(associated(currentPatch%older) )then - + if(debug) & write(fates_log(),*) 'fusing to older patch because this one is too small',& currentPatch%area, & currentPatch%older%area ! We set a pointer to this patch, because - ! it will be returned by the subtoutine as de-referenced - + ! it will be returned by the subroutine as de-referenced + olderPatch => currentPatch%older call fuse_2_patches(currentSite, olderPatch, currentPatch) ! The fusion process has updated the "older" pointer on currentPatch ! for us. - + ! This logic checks to make sure that the younger patch is not the youngest ! patch. As mentioned earlier, we try not to fuse it. - + elseif( .not. associated(currentPatch%younger,currentSite%youngest_patch) ) then - + if(debug) & - write(fates_log(),*) 'fusing to younger patch because oldest one is too small', & - currentPatch%area + write(fates_log(),*) 'fusing to younger patch because oldest one is too small', & + currentPatch%area youngerPatch => currentPatch%younger call fuse_2_patches(currentSite, youngerPatch, currentPatch) @@ -1752,12 +1741,12 @@ subroutine terminate_patches(currentSite) endif endif + endif - currentPatch => currentPatch%older - - enddo - end if - + currentPatch => currentPatch%older + + enddo + !check area is not exceeded areatot = 0._r8 currentPatch => currentSite%oldest_patch