-
Notifications
You must be signed in to change notification settings - Fork 92
/
EDCanopyStructureMod.F90
2265 lines (1794 loc) · 103 KB
/
EDCanopyStructureMod.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module EDCanopyStructureMod
! =====================================================================================
! Code to determine whether the canopy is closed, and which plants are either in the
! understorey or overstorey. This is obviosuly far too complicated for it's own good
! =====================================================================================
use FatesConstantsMod , only : r8 => fates_r8
use FatesConstantsMod , only : itrue, ifalse
use FatesConstantsMod , only : tinyr8
use FatesConstantsMod , only : nearzero, area_error_1
use FatesConstantsMod , only : rsnbl_math_prec
use FatesConstantsMod , only : nocomp_bareground
use FatesGlobals , only : fates_log
use EDPftvarcon , only : EDPftvarcon_inst
use PRTParametersMod , only : prt_params
use FatesAllometryMod , only : carea_allom
use EDCohortDynamicsMod , only : terminate_cohorts, terminate_cohort, fuse_cohorts
use EDCohortDynamicsMod , only : InitPRTObject
use FatesAllometryMod , only : tree_lai
use FatesAllometryMod , only : tree_sai
use EDtypesMod , only : ed_site_type
use FatesPatchMod, only : fates_patch_type
use FatesCohortMod, only : fates_cohort_type
use EDParamsMod , only : nclmax
use EDParamsMod , only : nlevleaf
use EDtypesMod , only : AREA
use EDLoggingMortalityMod , only : UpdateHarvestC
use FatesGlobals , only : endrun => fates_endrun
use FatesInterfaceTypesMod , only : hlm_days_per_year
use FatesInterfaceTypesMod , only : hlm_use_planthydro
use FatesInterfaceTypesMod , only : hlm_use_cohort_age_tracking
use FatesInterfaceTypesMod , only : hlm_use_sp
use FatesInterfaceTypesMod , only : numpft
use FatesInterfaceTypesMod, only : bc_in_type
use FatesPlantHydraulicsMod, only : UpdateH2OVeg,InitHydrCohort, RecruitWaterStorage
use PRTGenericMod, only : leaf_organ
use PRTGenericMod, only : leaf_organ
use PRTGenericMod, only : fnrt_organ
use PRTGenericMod, only : sapw_organ
use PRTGenericMod, only : store_organ
use PRTGenericMod, only : repro_organ
use PRTGenericMod, only : struct_organ
use PRTGenericMod, only : SetState
use PRTGenericMod, only : carbon12_element
! CIME Globals
use shr_log_mod , only : errMsg => shr_log_errMsg
implicit none
private
public :: canopy_structure
public :: canopy_spread
public :: calc_areaindex
public :: canopy_summarization
public :: update_hlm_dynamics
public :: UpdateFatesAvgSnowDepth
public :: UpdatePatchLAI
public :: UpdateCohortLAI
logical, parameter :: debug=.false.
character(len=*), parameter, private :: sourcefile = &
__FILE__
integer :: istat ! return status code
character(len=255) :: smsg ! Message string for deallocation errors
real(r8), parameter :: area_target_precision = 1.0E-11_r8 ! Area conservation
! will attempt to reduce errors
! below this level
real(r8), parameter :: area_check_precision = 1.0E-7_r8 ! Area conservation checks must
! be within this absolute tolerance
real(r8), parameter :: area_check_rel_precision = 1.0E-4_r8 ! Area conservation checks must
! be within this relative tolerance
real(r8), parameter :: similar_height_tol = 1.0E-3_r8 ! I think trees that differ by 1mm
! can be roughly considered the same right?
! 10/30/09: Created by Rosie Fisher
! 2017/2018: Modifications and updates by Ryan Knox
! ============================================================================
contains
! ============================================================================
subroutine canopy_structure( currentSite , bc_in )
!
! !DESCRIPTION:
! create cohort instance
!
! This routine allocates the 'canopy_layer' attribute to each cohort
! All top leaves in the same canopy layer get the same light resources.
! The first canopy layer is the 'canopy' or 'overstorey'. The second is the 'understorey'.
! More than two layers is not permitted at the moment
! Seeds germinating into the 3rd or higher layers are automatically removed.
!
! ------Perfect Plasticity-----
! The idea of these canopy layers derives originally from Purves et al. 2009
! Their concept is that, given enoughplasticity in canopy position, size, shape and depth
! all of the gound area will be filled perfectly by leaves, and additional leaves will have
! to exist in the understorey.
! Purves et al. use the concept of 'Z*' to assume that the height required to attain a place in the
! canopy is spatially uniform. In this implementation, described in Fisher et al. (2010, New Phyt) we
! extent that concept to assume that position in the canopy has some random element, and that BOTH height
! and chance combine to determine whether trees get into the canopy.
! Thus, when the canopy is closed and there is excess area, some of it must be demoted
! If we demote -all- the trees less than a given height, there is a massive advantage in being the cohort that is
! the biggest when the canopy is closed.
! In this implementation, the amount demoted, ('weight') is a function of the height weighted by the competitive exclusion
! parameter (ED_val_comp_excln).
! Complexity in this routine results from a few things.
! Firstly, the complication of the demotion amount sometimes being larger than the cohort area (for a very small, short cohort)
! Second, occasionaly, disturbance (specifically fire) can cause the canopy layer to become less than closed,
! without changing the area of the patch. If this happens, then some of the plants in the lower layer need to be 'promoted' so
! all of the routine has to happen in both the downwards and upwards directions.
!
! The order of events here is therefore:
! (The entire subroutine has a single outer 'patch' loop.
! Section 1: figure out the total area, and whether there are >1 canopy layers at all.
!
! Sorts out cohorts into canopy and understorey layers...
!
! !USES:
use EDParamsMod, only : ED_val_comp_excln
use EDTypesMod , only : min_patch_area
!
! !ARGUMENTS
type(ed_site_type) , intent(inout), target :: currentSite
type(bc_in_type), intent(in) :: bc_in
!
! !LOCAL VARIABLES:
type(fates_patch_type) , pointer :: currentPatch
type(fates_cohort_type), pointer :: currentCohort
integer :: i_lyr ! current layer index
integer :: z ! Current number of canopy layers. (1= canopy, 2 = understorey)
integer :: ipft
real(r8) :: arealayer(nclmax+2) ! Amount of plant area currently in each canopy layer
integer :: patch_area_counter ! count iterations used to solve canopy areas
logical :: area_not_balanced ! logical controlling if the patch layer areas
! have successfully been redistributed
integer :: return_code ! math checks on variables will return>0 if problems exist
! We only iterate because of possible imprecisions generated by the cohort
! termination process. These should be super small, so at the most
! try to re-balance 3 times. If that doesn't give layer areas
! within tolerance of canopy area, there is something wrong
integer, parameter :: max_patch_iterations = 10
!----------------------------------------------------------------------
currentPatch => currentSite%oldest_patch
!
! zero site-level demotion / promotion tracking info
currentSite%demotion_rate(:) = 0._r8
currentSite%promotion_rate(:) = 0._r8
currentSite%demotion_carbonflux = 0._r8
currentSite%promotion_carbonflux = 0._r8
!
! Section 1: Check total canopy area.
!
do while (associated(currentPatch)) ! Patch loop
! ------------------------------------------------------------------------------
! Perform numerical checks on some cohort and patch structures
! ------------------------------------------------------------------------------
! canopy layer has a special bounds check
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if( currentCohort%canopy_layer < 1 .or. currentCohort%canopy_layer > nclmax+1 ) then
write(fates_log(),*) 'lat:',currentSite%lat
write(fates_log(),*) 'lon:',currentSite%lon
write(fates_log(),*) 'BOGUS CANOPY LAYER: ',currentCohort%canopy_layer
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
currentCohort => currentCohort%shorter
enddo
! Does any layer have excess area in it? Keep going until it does not...
patch_area_counter = 0
area_not_balanced = .true.
do while(area_not_balanced)
! ---------------------------------------------------------------------------
! Demotion Phase: Identify upper layers that are too full, and demote them to
! the layers below.
! ---------------------------------------------------------------------------
! Its possible that before we even enter this scheme
! some cohort numbers are very low. Terminate them.
call terminate_cohorts(currentSite, currentPatch, 1, 12, bc_in)
! Calculate how many layers we have in this canopy
! This also checks the understory to see if its crown
! area is large enough to warrant a temporary sub-understory layer
z = NumPotentialCanopyLayers(currentPatch,currentSite%spread,include_substory=.false.)
do i_lyr = 1,z ! Loop around the currently occupied canopy layers.
call DemoteFromLayer(currentSite, currentPatch, i_lyr, bc_in)
end do
! After demotions, we may then again have cohorts that are very very
! very sparse, remove them
call terminate_cohorts(currentSite, currentPatch, 1,13,bc_in)
call fuse_cohorts(currentSite, currentPatch, bc_in)
! Remove cohorts for various other reasons
call terminate_cohorts(currentSite, currentPatch, 2,13,bc_in)
! ---------------------------------------------------------------------------------------
! Promotion Phase: Identify if any upper-layers are underful and layers below them
! have cohorts that can be split and promoted to the layer above.
! ---------------------------------------------------------------------------------------
! Re-calculate Number of layers without the false substory
z = NumPotentialCanopyLayers(currentPatch,currentSite%spread,include_substory=.false.)
! We only promote if we have at least two layers
if (z>1) then
do i_lyr=1,z-1
call PromoteIntoLayer(currentSite, currentPatch, i_lyr)
end do
! Remove cohorts that are incredibly sparse
call terminate_cohorts(currentSite, currentPatch, 1,14,bc_in)
call fuse_cohorts(currentSite, currentPatch, bc_in)
! Remove cohorts for various other reasons
call terminate_cohorts(currentSite, currentPatch, 2,14,bc_in)
end if
! ---------------------------------------------------------------------------------------
! Check on Layer Area (if the layer differences are not small
! Continue trying to demote/promote. Its possible on the first pass through,
! that cohort fusion has nudged the areas a little bit.
! ---------------------------------------------------------------------------------------
z = NumPotentialCanopyLayers(currentPatch,currentSite%spread,include_substory=.false.)
area_not_balanced = .false.
do i_lyr = 1,z
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr,arealayer(i_lyr))
if( ((arealayer(i_lyr)-currentPatch%area)/currentPatch%area > area_check_rel_precision) .or. &
((arealayer(i_lyr)-currentPatch%area) > area_check_precision ) ) then
area_not_balanced = .true.
endif
enddo
! ---------------------------------------------------------------------------------------
! Gracefully exit if too many iterations have gone by
! ---------------------------------------------------------------------------------------
patch_area_counter = patch_area_counter + 1
if(patch_area_counter > max_patch_iterations .and. area_not_balanced) then
write(fates_log(),*) 'PATCH AREA CHECK NOT CLOSING'
write(fates_log(),*) 'patch area:',currentpatch%area
do i_lyr = 1,z
write(fates_log(),*) 'layer: ',i_lyr,' area: ',arealayer(i_lyr)
write(fates_log(),*) 'rel error: ',(arealayer(i_lyr)-currentPatch%area)/currentPatch%area
write(fates_log(),*) 'abs error: ',arealayer(i_lyr)-currentPatch%area
enddo
write(fates_log(),*) 'lat:',currentSite%lat
write(fates_log(),*) 'lon:',currentSite%lon
write(fates_log(),*) 'spread:',currentSite%spread
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
write(fates_log(),*) 'coh ilayer:',currentCohort%canopy_layer
write(fates_log(),*) 'coh dbh:',currentCohort%dbh
write(fates_log(),*) 'coh pft:',currentCohort%pft
write(fates_log(),*) 'coh n:',currentCohort%n
write(fates_log(),*) 'coh carea:',currentCohort%c_area
ipft=currentCohort%pft
write(fates_log(),*) 'maxh:',prt_params%allom_dbh_maxheight(ipft)
write(fates_log(),*) 'lmode: ',prt_params%allom_lmode(ipft)
write(fates_log(),*) 'd2bl2: ',prt_params%allom_d2bl2(ipft)
write(fates_log(),*) 'd2bl_ediff: ',prt_params%allom_blca_expnt_diff(ipft)
write(fates_log(),*) 'd2ca_min: ',prt_params%allom_d2ca_coefficient_min(ipft)
write(fates_log(),*) 'd2ca_max: ',prt_params%allom_d2ca_coefficient_max(ipft)
currentCohort => currentCohort%shorter
enddo
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
enddo ! do while(area_not_balanced)
! Set current canopy layer occupancy indicator.
currentPatch%NCL_p = min(nclmax,z)
! -------------------------------------------------------------------------------------------
! if we are using "strict PPA", then calculate a z_star value as
! the height of the smallest tree in the canopy
! loop from top to bottom and locate the shortest cohort in level 1 whose shorter
! neighbor is in level 2 set zstar as the ehight of that shortest level 1 cohort
! -------------------------------------------------------------------------------------------
if ( ED_val_comp_excln .lt. 0.0_r8) then
currentPatch%zstar = 0._r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer .eq. 2)then
if (associated(currentCohort%taller)) then
if (currentCohort%taller%canopy_layer .eq. 1 ) then
currentPatch%zstar = currentCohort%taller%height
endif
endif
endif
currentCohort => currentCohort%shorter
enddo
endif
currentPatch => currentPatch%younger
enddo !patch
return
end subroutine canopy_structure
! ==============================================================================================
subroutine DemoteFromLayer(currentSite,currentPatch,i_lyr,bc_in)
use EDParamsMod, only : ED_val_comp_excln
! !ARGUMENTS
type(ed_site_type), intent(inout) :: currentSite
type(fates_patch_type), intent(inout) :: currentPatch
integer, intent(in) :: i_lyr ! Current canopy layer of interest
type(bc_in_type), intent(in) :: bc_in
! !LOCAL VARIABLES:
type(fates_cohort_type), pointer :: currentCohort
type(fates_cohort_type), pointer :: copyc
type(fates_cohort_type), pointer :: nextc ! The next cohort in line
integer :: i_cwd ! Index for CWD pool
real(r8) :: cc_loss ! cohort crown area loss in demotion (m2)
real(r8) :: leaf_c ! leaf carbon [kg]
real(r8) :: fnrt_c ! fineroot carbon [kg]
real(r8) :: sapw_c ! sapwood carbon [kg]
real(r8) :: store_c ! storage carbon [kg]
real(r8) :: struct_c ! structure carbon [kg]
real(r8) :: scale_factor ! for prob. exclusion - scales weight to a fraction
real(r8) :: scale_factor_min ! "" minimum before exeedance of 1
real(r8) :: scale_factor_res ! "" applied to residual areas
real(r8) :: area_res ! residual area to demote after weakest cohort hits max
real(r8) :: newarea
real(r8) :: demote_area
real(r8) :: sumweights
real(r8) :: sumequal ! for rank-ordered same-size cohorts
! this tallies their excluded area
real(r8) :: arealayer ! the area of the current canopy layer
logical :: tied_size_with_neighbors
real(r8) :: total_crownarea_of_tied_cohorts
! First, determine how much total canopy area we have in this layer
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr,arealayer)
demote_area = arealayer - currentPatch%area
if ( demote_area > area_target_precision ) then
! Is this layer currently over-occupied?
! In that case, we need to work out which cohorts to demote.
! We go in order from shortest to tallest for ranked demotion
sumweights = 0.0_r8
currentCohort => currentPatch%shortest
do while (associated(currentCohort))
call carea_allom(currentCohort%dbh,currentCohort%n, &
currentSite%spread,currentCohort%pft, &
currentCohort%crowndamage, currentCohort%c_area)
if(debug) then
if(currentCohort%c_area<0._r8)then
write(fates_log(),*) 'negative c_area stage 1d: ',currentCohort%dbh,i_lyr,currentCohort%n, &
currentSite%spread,currentCohort%pft,currentCohort%c_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
if( currentCohort%canopy_layer == i_lyr)then
if (ED_val_comp_excln .ge. 0.0_r8 ) then
! ----------------------------------------------------------
! Stochastic method.
! Weight cohort demotion by inverse size to a constant power.
! In this hypothesis, it is assumed that even the tallest
! cohorts have a chance (although smaller) of being forced
! to the understory.
! ----------------------------------------------------------
currentCohort%excl_weight = 1._r8 / (currentCohort%height**ED_val_comp_excln)
sumweights = sumweights + currentCohort%excl_weight
else
! -----------------------------------------------------------
! Rank ordered deterministic method
! -----------------------------------------------------------
! If there are cohorts that have the exact same height (which is possible, really)
! we don't want to unilaterally promote/demote one before the others.
! So we <>mote them as a unit
! now we need to go through and figure out how many equal-size cohorts there are.
! then we need to go through, add up the collective crown areas of all equal-sized
! and equal-canopy-layer cohorts,
! and then demote from each as if they were a single group
total_crownarea_of_tied_cohorts = currentCohort%c_area
tied_size_with_neighbors = .false.
nextc => currentCohort%taller
do while (associated(nextc))
if ( abs(nextc%height - currentCohort%height) < similar_height_tol ) then
if( nextc%canopy_layer .eq. currentCohort%canopy_layer ) then
tied_size_with_neighbors = .true.
total_crownarea_of_tied_cohorts = &
total_crownarea_of_tied_cohorts + nextc%c_area
end if
else
exit
endif
nextc => nextc%taller
end do
if ( tied_size_with_neighbors ) then
currentCohort%excl_weight = &
max(0.0_r8,min(currentCohort%c_area, &
(currentCohort%c_area/total_crownarea_of_tied_cohorts) * &
(demote_area - sumweights) ))
sumequal = currentCohort%excl_weight
nextc => currentCohort%taller
do while (associated(nextc))
if ( abs(nextc%height - currentCohort%height) < similar_height_tol ) then
if (nextc%canopy_layer .eq. currentCohort%canopy_layer ) then
! now we know the total crown area of all equal-sized,
! equal-canopy-layer cohorts
nextc%excl_weight = &
max(0.0_r8,min(nextc%c_area, &
(nextc%c_area/total_crownarea_of_tied_cohorts) * &
(demote_area - sumweights) ))
sumequal = sumequal + nextc%excl_weight
end if
else
exit
endif
nextc => nextc%taller
end do
! Update the current cohort pointer to the last similar cohort
! Its ok if this is not in the right layer
if(associated(nextc))then
currentCohort => nextc%shorter
else
currentCohort => currentPatch%tallest
end if
sumweights = sumweights + sumequal
else
currentCohort%excl_weight = &
max(min(currentCohort%c_area, demote_area - sumweights ), 0._r8)
sumweights = sumweights + currentCohort%excl_weight
end if
endif
endif
currentCohort => currentCohort%taller
enddo
! If this is probabalistic demotion, we need to do a round of normalization.
! And then a few rounds where we pre-calculate the demotion areas
! and adjust things if the demoted area wants to be greater than
! what is available. The math is too hard to explain here, see
! the tech note section on promotion/demotion.
if (ED_val_comp_excln .ge. 0.0_r8 ) then
scale_factor_min = 1.e10_r8
scale_factor = 0._r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
currentCohort%excl_weight = currentCohort%excl_weight/sumweights
if( 1._r8/currentCohort%excl_weight < scale_factor_min ) &
scale_factor_min = 1._r8/currentCohort%excl_weight
scale_factor = scale_factor + currentCohort%excl_weight * currentCohort%c_area
endif
currentCohort => currentCohort%shorter
enddo
! This is the factor by which we need to multiply
! the demotion probabilities, so the sum result equals
! the total amount to demote
scale_factor = demote_area/scale_factor
if(scale_factor <= scale_factor_min) then
! Trivial case, all of the demotion fractions are less than 1.
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
currentCohort%excl_weight = currentCohort%c_area * currentCohort%excl_weight * scale_factor
if(debug) then
if((currentCohort%excl_weight > (currentCohort%c_area+area_target_precision)) .or. &
(currentCohort%excl_weight < 0._r8) ) then
write(fates_log(),*) 'exclusion area too big (1)'
write(fates_log(),*) 'currentCohort%c_area: ',currentCohort%c_area
write(fates_log(),*) 'dbh: ',currentCohort%dbh
write(fates_log(),*) 'n: ',currentCohort%n
write(fates_log(),*) 'spread: ',currentSite%spread
write(fates_log(),*) 'pft: ',currentCohort%pft
write(fates_log(),*) 'currentCohort%excl_weight: ',currentCohort%excl_weight
write(fates_log(),*) 'excess: ',currentCohort%excl_weight - currentCohort%c_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
endif
currentCohort => currentCohort%shorter
enddo
else
! Non-trivial case, at least 1 cohort's demotion
! rate would exceed its area, given the trivial scale factor
area_res = 0._r8
scale_factor_res = 0._r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
area_res = area_res + &
currentCohort%c_area * currentCohort%excl_weight * &
scale_factor_min
scale_factor_res = scale_factor_res + &
currentCohort%c_area * &
(1._r8 - (currentCohort%excl_weight * scale_factor_min))
endif
currentCohort => currentCohort%shorter
enddo
area_res = demote_area - area_res
scale_factor_res = area_res / scale_factor_res
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
currentCohort%excl_weight = currentCohort%c_area * &
(currentCohort%excl_weight * scale_factor_min + &
(1._r8 - (currentCohort%excl_weight*scale_factor_min) ) * scale_factor_res)
if(debug)then
if((currentCohort%excl_weight > &
(currentCohort%c_area+area_target_precision)) .or. &
(currentCohort%excl_weight < 0._r8) ) then
write(fates_log(),*) 'exclusion area error (2)'
write(fates_log(),*) 'currentCohort%c_area: ',currentCohort%c_area
write(fates_log(),*) 'currentCohort%excl_weight: ', &
currentCohort%excl_weight
write(fates_log(),*) 'excess: ', &
currentCohort%excl_weight - currentCohort%c_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
endif
currentCohort => currentCohort%shorter
enddo
end if
end if
! perform a check and see if the demotions meet the demand
sumweights = 0._r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
sumweights = sumweights + currentCohort%excl_weight
end if
currentCohort => currentCohort%shorter
end do
if (abs(sumweights - demote_area) > area_check_precision ) then
write(fates_log(),*) 'demotions dont add up'
write(fates_log(),*) 'sum demotions: ',sumweights
write(fates_log(),*) 'area needed to be demoted: ',demote_area
write(fates_log(),*) 'excess: ',sumweights - demote_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
! Weights have been calculated. Now move them to the lower layer
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
nextc => currentCohort%shorter
if(currentCohort%canopy_layer == i_lyr )then
cc_loss = currentCohort%excl_weight
leaf_c = currentCohort%prt%GetState(leaf_organ,carbon12_element)
store_c = currentCohort%prt%GetState(store_organ,carbon12_element)
fnrt_c = currentCohort%prt%GetState(fnrt_organ,carbon12_element)
sapw_c = currentCohort%prt%GetState(sapw_organ,carbon12_element)
struct_c = currentCohort%prt%GetState(struct_organ,carbon12_element)
if ( (cc_loss-currentCohort%c_area) > -nearzero .and. &
(cc_loss-currentCohort%c_area) < area_target_precision ) then
! If the whole cohort is being demoted, just change its
! layer index
currentCohort%canopy_layer = i_lyr+1
! keep track of number and biomass of demoted cohort
currentSite%demotion_rate(currentCohort%size_class) = &
currentSite%demotion_rate(currentCohort%size_class) + currentCohort%n
currentSite%demotion_carbonflux = currentSite%demotion_carbonflux + &
(leaf_c + store_c + fnrt_c + sapw_c + struct_c) * currentCohort%n
elseif( (cc_loss < currentCohort%c_area) .and. &
(cc_loss > area_target_precision) ) then
! If only part of the cohort is demoted
! then it must be split (little more complicated)
! Make a copy of the current cohort. The copy and the original
! conserve total number density of the original. The copy
! remains in the upper-story. The original is the one
! demoted to the understory
allocate(copyc)
! (keep as an example)
! Initialize running means
!allocate(copyc%tveg_lpa)
!!allocate(copyc%l2fr_ema)
! Note, no need to give a starter value here,
! that will be taken care of in copy()
!!call copyc%l2fr_ema%InitRMean(ema_60day)
! Initialize the PARTEH object and point to the
! correct boundary condition fields
copyc%prt => null()
call InitPRTObject(copyc%prt)
if( hlm_use_planthydro.eq.itrue ) then
call InitHydrCohort(currentSite,copyc)
endif
call currentCohort%Copy(copyc)
call copyc%InitPRTBoundaryConditions()
newarea = currentCohort%c_area - cc_loss
copyc%n = currentCohort%n*newarea/currentCohort%c_area
currentCohort%n = currentCohort%n - copyc%n
copyc%canopy_layer = i_lyr !the taller cohort is the copy
! Demote the current cohort to the understory.
currentCohort%canopy_layer = i_lyr + 1
! keep track of number and biomass of demoted cohort
currentSite%demotion_rate(currentCohort%size_class) = &
currentSite%demotion_rate(currentCohort%size_class) + currentCohort%n
currentSite%demotion_carbonflux = currentSite%demotion_carbonflux + &
(leaf_c + store_c + fnrt_c + sapw_c + struct_c) * currentCohort%n
call carea_allom(copyc%dbh,copyc%n,currentSite%spread,copyc%pft, &
copyc%crowndamage, copyc%c_area)
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,currentCohort%crowndamage, currentCohort%c_area)
!----------- Insert copy into linked list ------------------------!
copyc%shorter => currentCohort
if(associated(currentCohort%taller))then
copyc%taller => currentCohort%taller
currentCohort%taller%shorter => copyc
else
currentPatch%tallest => copyc
copyc%taller => null()
endif
currentCohort%taller => copyc
elseif(cc_loss > currentCohort%c_area)then
write(fates_log(),*) 'more area than the cohort has is being demoted'
write(fates_log(),*) 'loss:',cc_loss
write(fates_log(),*) 'existing area:',currentCohort%c_area
write(fates_log(),*) 'excess: ',cc_loss - currentCohort%c_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
! kill the ones which go into canopy layers that are not allowed
! USE THIS OVERRIDE IF YOU ARE FORCING A ONE COHORT SIMULATION
! (also make sure to turn off germination, external seed rain,
! (use only one PFT, and make sure disturb_frac is 0)
! (RGK-0822)
!if(currentCohort%canopy_layer>1) then
if(currentCohort%canopy_layer>nclmax )then
! put the litter from the terminated cohorts
! straight into the fragmenting pools
call terminate_cohort(currentSite,currentPatch,currentCohort,bc_in)
deallocate(currentCohort, stat=istat, errmsg=smsg)
if (istat/=0) then
write(fates_log(),*) 'dealloc012: fail on deallocate(currentCohort):'//trim(smsg)
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
else
call carea_allom(currentCohort%dbh,currentCohort%n, &
currentSite%spread,currentCohort%pft,currentCohort%crowndamage, &
currentCohort%c_area)
end if
endif !canopy layer = i_ly
! We dont use our typical (point to smaller)
! here, because, we may had deallocated the existing
! currentCohort
currentCohort => nextc
enddo !currentCohort
! Update the area calculations of the current layer
! And the layer below that may or may not had recieved
! Demotions
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr,arealayer)
if ( (abs(arealayer - currentPatch%area)/arealayer > area_check_rel_precision ) .or. &
(abs(arealayer - currentPatch%area) > area_check_precision) ) then
write(fates_log(),*) 'demotion did not trim area within tolerance'
write(fates_log(),*) 'arealayer:',arealayer
write(fates_log(),*) 'patch%area:',currentPatch%area
write(fates_log(),*) 'ilayer: ',i_lyr
write(fates_log(),*) 'bias:',arealayer - currentPatch%area
write(fates_log(),*) 'rel bias:',(arealayer - currentPatch%area)/arealayer
write(fates_log(),*) 'demote_area:',demote_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
return
end subroutine DemoteFromLayer
! ==============================================================================================
subroutine PromoteIntoLayer(currentSite,currentPatch,i_lyr)
! -------------------------------------------------------------------------------------------
! Check whether the intended 'full' layers are actually filling all the space.
! If not, promote some fraction of cohorts upwards.
! THIS SECTION MIGHT BE TRIGGERED BY A FIRE OR MORTALITY EVENT, FOLLOWED BY A PATCH FUSION,
! SO THE TOP LAYER IS NO LONGER FULL.
! -------------------------------------------------------------------------------------------
use EDParamsMod, only : ED_val_comp_excln
! !ARGUMENTS
type(ed_site_type), intent(inout), target :: currentSite
type(fates_patch_type), intent(inout), target :: currentPatch
integer, intent(in) :: i_lyr ! Current canopy layer of interest
! !LOCAL VARIABLES:
type(fates_cohort_type), pointer :: currentCohort
type(fates_cohort_type), pointer :: copyc
type(fates_cohort_type), pointer :: nextc ! the next cohort, or used for looping
! cohorts against the current
real(r8) :: scale_factor ! for prob. exclusion - scales weight to a fraction
real(r8) :: scale_factor_min ! "" minimum before exeedance of 1
real(r8) :: scale_factor_res ! "" applied to residual areas
real(r8) :: area_res ! residual area to demote after weakest cohort hits max
real(r8) :: promote_area
real(r8) :: newarea
real(r8) :: sumweights
real(r8) :: sumequal ! for tied cohorts, the sum of weights in
! their group
real(r8) :: cc_gain ! cohort crown area gain in promotion (m2)
real(r8) :: arealayer_current ! area (m2) of the current canopy layer
real(r8) :: arealayer_below ! area (m2) of the layer below the current layer
real(r8) :: leaf_c ! leaf carbon [kg]
real(r8) :: fnrt_c ! fineroot carbon [kg]
real(r8) :: sapw_c ! sapwood carbon [kg]
real(r8) :: store_c ! storage carbon [kg]
real(r8) :: struct_c ! structure carbon [kg]
logical :: tied_size_with_neighbors
real(r8) :: total_crownarea_of_tied_cohorts
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr,arealayer_current)
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr+1,arealayer_below)
! how much do we need to gain?
promote_area = currentPatch%area - arealayer_current
if( promote_area > area_target_precision ) then
if(arealayer_below <= promote_area ) then
! ---------------------------------------------------------------------------
! Promote all cohorts from layer below if that whole layer has area smaller
! than the tolerance on the gains needed into current layer
! ---------------------------------------------------------------------------
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
!look at the cohorts in the canopy layer below...
if(currentCohort%canopy_layer == i_lyr+1)then
leaf_c = currentCohort%prt%GetState(leaf_organ,carbon12_element)
store_c = currentCohort%prt%GetState(store_organ,carbon12_element)
fnrt_c = currentCohort%prt%GetState(fnrt_organ,carbon12_element)
sapw_c = currentCohort%prt%GetState(sapw_organ,carbon12_element)
struct_c = currentCohort%prt%GetState(struct_organ,carbon12_element)
currentCohort%canopy_layer = i_lyr
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,currentCohort%crowndamage, currentCohort%c_area)
! keep track of number and biomass of promoted cohort
currentSite%promotion_rate(currentCohort%size_class) = &
currentSite%promotion_rate(currentCohort%size_class) + currentCohort%n
currentSite%promotion_carbonflux = currentSite%promotion_carbonflux + &
(leaf_c + fnrt_c + store_c + sapw_c + struct_c) * currentCohort%n
endif
currentCohort => currentCohort%shorter
enddo
else
! ---------------------------------------------------------------------------
! This is the non-trivial case where the lower layer can accomodate
! more than what is necessary.
! ---------------------------------------------------------------------------
! figure out with what weighting we need to promote cohorts.
! This is the opposite of the demotion weighting...
sumweights = 0.0_r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,currentCohort%crowndamage,currentCohort%c_area)
if(currentCohort%canopy_layer == i_lyr+1)then !look at the cohorts in the canopy layer below...
if (ED_val_comp_excln .ge. 0.0_r8 ) then
! ------------------------------------------------------------------
! Stochastic case, as above (in demotion portion of code)
! ------------------------------------------------------------------
currentCohort%prom_weight = currentCohort%height**ED_val_comp_excln
sumweights = sumweights + currentCohort%prom_weight
else
! ------------------------------------------------------------------
! Rank ordered deterministic method
! If there are cohorts that have the exact same height (which is possible, really)
! we don't want to unilaterally promote/demote one before the others.
! So we <>mote them as a unit
! now we need to go through and figure out how many equal-size cohorts there are.
! then we need to go through, add up the collective crown areas of all equal-sized
! and equal-canopy-layer cohorts,
! and then demote from each as if they were a single group
! ------------------------------------------------------------------
total_crownarea_of_tied_cohorts = currentCohort%c_area
tied_size_with_neighbors = .false.
nextc => currentCohort%shorter
do while (associated(nextc))
if ( abs(nextc%height - currentCohort%height) < similar_height_tol ) then
if( nextc%canopy_layer .eq. currentCohort%canopy_layer ) then
tied_size_with_neighbors = .true.
total_crownarea_of_tied_cohorts = &
total_crownarea_of_tied_cohorts + nextc%c_area
end if
else
exit
endif
nextc => nextc%shorter
end do
if ( tied_size_with_neighbors ) then
currentCohort%prom_weight = &
max(0.0_r8,min(currentCohort%c_area, &
(currentCohort%c_area/total_crownarea_of_tied_cohorts) * &
(promote_area - sumweights) ))
sumequal = currentCohort%prom_weight
nextc => currentCohort%shorter
do while (associated(nextc))
if ( abs(nextc%height - currentCohort%height) < similar_height_tol ) then
if (nextc%canopy_layer .eq. currentCohort%canopy_layer ) then
! now we know the total crown area of all equal-sized,
! equal-canopy-layer cohorts
nextc%prom_weight = &
max(0.0_r8,min(nextc%c_area, &
(nextc%c_area/total_crownarea_of_tied_cohorts) * &
(promote_area - sumweights) ))
sumequal = sumequal + nextc%prom_weight
end if
else
exit
endif
nextc => nextc%shorter
end do
! Update the current cohort pointer to the last similar cohort
! Its ok if this is not in the right layer
if(associated(nextc))then
currentCohort => nextc%taller
else
currentCohort => currentPatch%shortest
end if
sumweights = sumweights + sumequal
else
currentCohort%prom_weight = &
max(min(currentCohort%c_area, promote_area - sumweights ), 0._r8)
sumweights = sumweights + currentCohort%prom_weight
end if
endif
endif
currentCohort => currentCohort%shorter
enddo !currentCohort
! If this is probabalistic promotion, we need to do a round of normalization.
! And then a few rounds where we pre-calculate the promotion areas
! and adjust things if the promoted area wants to be greater than
! what is available.
if (ED_val_comp_excln .ge. 0.0_r8 ) then
scale_factor_min = 1.e10_r8
scale_factor = 0._r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == (i_lyr+1) ) then
currentCohort%prom_weight = currentCohort%prom_weight/sumweights
if( 1._r8/currentCohort%prom_weight < scale_factor_min ) &
scale_factor_min = 1._r8/currentCohort%prom_weight
scale_factor = scale_factor + currentCohort%prom_weight * currentCohort%c_area
endif
currentCohort => currentCohort%shorter
enddo
! This is the factor by which we need to multiply
! the demotion probabilities, so the sum result equals
! the total amount to demote
scale_factor = promote_area/scale_factor