This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEDCanopyStructureMod.F90
1849 lines (1447 loc) · 88.2 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
use FatesConstantsMod , only : rsnbl_math_prec
use FatesGlobals , only : fates_log
use EDPftvarcon , only : EDPftvarcon_inst
use FatesAllometryMod , only : carea_allom
use EDCohortDynamicsMod , only : copy_cohort, terminate_cohorts, fuse_cohorts
use FatesAllometryMod , only : tree_lai
use FatesAllometryMod , only : tree_sai
use EDtypesMod , only : ed_site_type, ed_patch_type, ed_cohort_type, ncwd
use EDTypesMod , only : nclmax
use EDTypesMod , only : nlevleaf
use EDtypesMod , only : AREA
use FatesGlobals , only : endrun => fates_endrun
use FatesInterfaceMod , only : hlm_days_per_year
use FatesInterfaceMod , only : hlm_use_planthydro
use FatesInterfaceMod , only : numpft
use FatesPlantHydraulicsMod, only : UpdateH2OVeg,InitHydrCohort
! 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
logical, parameter :: DEBUG=.false.
character(len=*), parameter, private :: sourcefile = &
__FILE__
real(r8), parameter :: area_target_precision = 1.0E-11_r8 ! Area conservation must be within this tolerance
real(r8), parameter :: area_check_precision = 1.0E-9_r8 ! Area conservation checks must be within this tolerance
! 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 : ncwd
use EDTypesMod , only : min_patch_area
use EDTypesMod , only : val_check_ed_vars
use FatesInterfaceMod, only : bc_in_type
!
! !ARGUMENTS
type(ed_site_type) , intent(inout), target :: currentSite
type(bc_in_type), intent(in) :: bc_in
!
! !LOCAL VARIABLES:
type(ed_patch_type) , pointer :: currentPatch
type(ed_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
! ------------------------------------------------------------------------------
! call val_check_ed_vars(currentPatch,'co_n:co_dbh:pa_area',return_code)
! ! No need to make error message, already generated in math_check_ed_vars
! if(return_code>0) call endrun(msg=errMsg(sourcefile, __LINE__))
! 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.
! ---------------------------------------------------------------------------
! 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)
end do
! Remove cohorts that are incredibly sparse
call terminate_cohorts(currentSite, currentPatch, 1)
call fuse_cohorts(currentSite, currentPatch, bc_in)
! Remove cohorts for various other reasons
call terminate_cohorts(currentSite, currentPatch, 2)
! ---------------------------------------------------------------------------------------
! 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)
call fuse_cohorts(currentSite, currentPatch, bc_in)
! Remove cohorts for various other reasons
call terminate_cohorts(currentSite, currentPatch, 2)
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_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
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:',EDPftvarcon_inst%allom_dbh_maxheight(ipft)
write(fates_log(),*) 'lmode: ',EDPftvarcon_inst%allom_lmode(ipft)
write(fates_log(),*) 'd2bl2: ',EDPftvarcon_inst%allom_d2bl2(ipft)
write(fates_log(),*) 'd2bl_ediff: ',EDPftvarcon_inst%allom_blca_expnt_diff(ipft)
write(fates_log(),*) 'd2ca_min: ',EDPftvarcon_inst%allom_d2ca_coefficient_min(ipft)
write(fates_log(),*) 'd2ca_max: ',EDPftvarcon_inst%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%hite
endif
endif
endif
currentCohort => currentCohort%shorter
enddo
endif
currentPatch => currentPatch%younger
enddo !patch
return
end subroutine canopy_structure
! ==============================================================================================
subroutine DemoteFromLayer(currentSite,currentPatch,i_lyr)
use EDParamsMod, only : ED_val_comp_excln
use EDtypesMod , only : ncwd
use SFParamsMod, only : SF_val_CWD_frac
! !ARGUMENTS
type(ed_site_type), intent(inout), target :: currentSite
type(ed_patch_type), intent(inout), target :: currentPatch
integer, intent(in) :: i_lyr ! Current canopy layer of interest
! !LOCAL VARIABLES:
type(ed_cohort_type), pointer :: currentCohort,copyc
integer :: i_cwd ! Index for CWD pool
real(r8) :: cc_loss ! cohort crown area loss in demotion (m2)
real(r8) :: lossarea
real(r8) :: newarea
real(r8) :: demote_area
real(r8) :: remainder_area
real(r8) :: remainder_area_hold
real(r8) :: sumweights
real(r8) :: sumweights_old
real(r8) :: arealayer ! the area of the current canopy layer
integer :: exceedance_counter ! when seeking to rebalance demotion exceedance
! keep a loop counter to check for hangs
! 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/currentPatch%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%c_area)
if( currentCohort%canopy_layer == i_lyr)then
if (ED_val_comp_excln .ge. 0.0_r8 ) then
! normal (stochastic) case. weight cohort demotion by
! inverse size to a constant power
currentCohort%excl_weight = &
currentCohort%n/(currentCohort%dbh**ED_val_comp_excln)
else
! Rank ordered deterministic method
currentCohort%excl_weight = &
max(min(currentCohort%c_area, demote_area - sumweights ), 0._r8)
endif
sumweights = sumweights + currentCohort%excl_weight
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. sumweights_old is used to sum up the exclusion
! area of those cohorts that have not reached their total canopy area
! yet.
if (ED_val_comp_excln .ge. 0.0_r8 ) then
remainder_area = 0.0_r8
sumweights_old = 0.0_r8
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr) then
currentCohort%excl_weight = demote_area*currentCohort%excl_weight/sumweights
if( currentCohort%excl_weight > currentCohort%c_area ) then
remainder_area = remainder_area + currentCohort%excl_weight-currentCohort%c_area
currentCohort%excl_weight = currentCohort%c_area
else
sumweights_old = sumweights_old + currentCohort%excl_weight
end if
endif
currentCohort => currentCohort%shorter
enddo
exceedance_counter = 0
do while(remainder_area/demote_area > area_target_precision )
! Keep attempting to add exceedance to members that have not
! lost more area than they started with..
sumweights = 0.0_r8
remainder_area_hold = remainder_area
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr)then
if ( currentCohort%excl_weight < (currentCohort%c_area-nearzero) ) then
! Calculate how much exceeded demotion from filled
! cohorts must be transferred to this cohort
! Two requirements: 1) the tacked-on demotion can not also
! exceed this cohort's area. And, 2) the tacked-on
! demotion can't exceed the amount left
cc_loss = min(remainder_area, &
remainder_area_hold * currentCohort%excl_weight/sumweights_old, &
currentCohort%c_area-currentCohort%excl_weight)
! Reduce the remainder_area
remainder_area = remainder_area - cc_loss
! Update this cohorts exclusion
currentCohort%excl_weight = currentCohort%excl_weight + cc_loss
! Update the sum of weights for cohorts where exceedance can
! still be spread to
if( currentCohort%excl_weight < (currentCohort%c_area-nearzero) ) then
sumweights = sumweights + currentCohort%excl_weight
end if
end if
end if
currentCohort => currentCohort%shorter
end do
! Update the sum of weights for the next loop
sumweights_old = sumweights
exceedance_counter = exceedance_counter + 1
if( exceedance_counter > 100 ) then
write(fates_log(),*) 'It is taking a long time to distribute demotion exceedance'
write(fates_log(),*) '(ie greater than c_area) to neighbors... exiting'
write(fates_log(),*) 'sumweights:',sumweights
write(fates_log(),*) 'remainder_area:',remainder_area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
end if
! Weights have been calculated. Now move them to the lower layer
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
cc_loss = currentCohort%excl_weight
if(currentCohort%canopy_layer == i_lyr .and. cc_loss>nearzero )then
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 + &
currentCohort%b_total() * currentCohort%n
elseif(cc_loss > nearzero .and. cc_loss < currentCohort%c_area )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)
call copy_cohort(currentCohort, copyc)
if( hlm_use_planthydro.eq.itrue ) then
call InitHydrCohort(currentSite,copyc)
endif
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 + &
currentCohort%b_total() * currentCohort%n
call carea_allom(copyc%dbh,copyc%n,currentSite%spread,copyc%pft,copyc%c_area)
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,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
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
! kill the ones which go into canopy layers that are not allowed
if(i_lyr+1 > nclmax)then
! put the litter from the terminated cohorts into the fragmenting pools
do i_cwd=1,ncwd
currentPatch%CWD_AG(i_cwd) = currentPatch%CWD_AG(i_cwd) + &
(currentCohort%bdead+currentCohort%bsw) * &
EDPftvarcon_inst%allom_agb_frac(currentCohort%pft) * &
SF_val_CWD_frac(i_cwd)*currentCohort%n/currentPatch%area
currentPatch%CWD_BG(i_cwd) = currentPatch%CWD_BG(i_cwd) + &
(currentCohort%bdead+currentCohort%bsw) * &
(1.0_r8-EDPftvarcon_inst%allom_agb_frac(currentCohort%pft)) * &
SF_val_CWD_frac(i_cwd)*currentCohort%n/currentPatch%area !litter flux per m2.
enddo
currentPatch%leaf_litter(currentCohort%pft) = &
currentPatch%leaf_litter(currentCohort%pft) + (currentCohort%bl)* &
currentCohort%n/currentPatch%area ! leaf litter flux per m2.
currentPatch%root_litter(currentCohort%pft) = &
currentPatch%root_litter(currentCohort%pft) + &
(currentCohort%br+currentCohort%bstore)*currentCohort%n/currentPatch%area
! keep track of the above fluxes at the site level as a
! CWD/litter input flux (in kg / site-m2 / yr)
do i_cwd=1,ncwd
currentSite%CWD_AG_diagnostic_input_carbonflux(i_cwd) = &
currentSite%CWD_AG_diagnostic_input_carbonflux(i_cwd) &
+ currentCohort%n*(currentCohort%bdead+currentCohort%bsw) * &
SF_val_CWD_frac(i_cwd) * EDPftvarcon_inst%allom_agb_frac(currentCohort%pft) &
* hlm_days_per_year / AREA
currentSite%CWD_BG_diagnostic_input_carbonflux(i_cwd) = &
currentSite%CWD_BG_diagnostic_input_carbonflux(i_cwd) &
+ currentCohort%n*(currentCohort%bdead+currentCohort%bsw) * &
SF_val_CWD_frac(i_cwd) * (1.0_r8 - &
EDPftvarcon_inst%allom_agb_frac(currentCohort%pft)) * hlm_days_per_year / AREA
enddo
currentSite%leaf_litter_diagnostic_input_carbonflux(currentCohort%pft) = &
currentSite%leaf_litter_diagnostic_input_carbonflux(currentCohort%pft) + &
currentCohort%n * (currentCohort%bl) * hlm_days_per_year / AREA
currentSite%root_litter_diagnostic_input_carbonflux(currentCohort%pft) = &
currentSite%root_litter_diagnostic_input_carbonflux(currentCohort%pft) + &
currentCohort%n * (currentCohort%br+currentCohort%bstore) * hlm_days_per_year / AREA
currentCohort%n = 0.0_r8
currentCohort%c_area = 0.0_r8
currentCohort%canopy_layer = i_lyr
end if
call carea_allom(currentCohort%dbh,currentCohort%n, &
currentSite%spread,currentCohort%pft,currentCohort%c_area)
endif !canopy layer = i_ly
currentCohort => currentCohort%shorter
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_target_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(),*) 'error:',abs(arealayer - currentPatch%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(ed_patch_type), intent(inout), target :: currentPatch
integer, intent(in) :: i_lyr ! Current canopy layer of interest
! !LOCAL VARIABLES:
type(ed_cohort_type), pointer :: currentCohort
type(ed_cohort_type), pointer :: copyc
real(r8) :: promote_area
real(r8) :: newarea
real(r8) :: sumweights
real(r8) :: sumweights_old
integer :: exceedance_counter
real(r8) :: remainder_area
real(r8) :: remainder_area_hold
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
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/currentPatch%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
currentCohort%canopy_layer = i_lyr
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,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 + &
currentCohort%b_total() * 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%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
! normal (stochastic) case, as above.
currentCohort%prom_weight = currentCohort%n*currentCohort%dbh**ED_val_comp_excln
else
currentCohort%prom_weight = max(min(currentCohort%c_area, &
promote_area - sumweights ), 0._r8)
endif
sumweights = sumweights + currentCohort%prom_weight
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
remainder_area = 0.0_r8
sumweights_old = 0.0_r8
currentCohort => currentPatch%tallest !start from the tallest cohort
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr+1) then !still looking at the layer beneath.
currentCohort%prom_weight = promote_area*currentCohort%prom_weight/sumweights
if( currentCohort%prom_weight > currentCohort%c_area ) then
remainder_area = remainder_area + currentCohort%prom_weight-currentCohort%c_area
currentCohort%prom_weight = currentCohort%c_area
else
sumweights_old = sumweights_old + currentCohort%prom_weight
end if
endif
currentCohort => currentCohort%shorter
enddo
exceedance_counter = 0
do while(remainder_area/promote_area > area_target_precision )
sumweights = 0.0_r8
remainder_area_hold = remainder_area
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
if(currentCohort%canopy_layer == i_lyr+1)then
if ( currentCohort%prom_weight < (currentCohort%c_area-nearzero) ) then
! Calculate how much exceeded promotion from filled
! cohorts must be transferred to this cohort
! Two requirements: 1) the tacked-on promotion can not also
! exceed this cohort's area. And, 2) the tacked-on
! promotion can't exceed the amount left
! This is how promotion is transferred from this cohort
cc_gain = min(remainder_area, &
remainder_area_hold * currentCohort%prom_weight/sumweights_old, &
currentCohort%c_area-currentCohort%prom_weight)
! Reduce the remainder_area
remainder_area = remainder_area - cc_gain
! Update this cohort's promotion
currentCohort%prom_weight = currentCohort%prom_weight + cc_gain
! Update the sum of weights for cohorts where exceedance can
! still be spread to
if( currentCohort%prom_weight < (currentCohort%c_area-nearzero) ) then
sumweights = sumweights + currentCohort%prom_weight
end if
end if
end if
currentCohort => currentCohort%shorter
end do
sumweights_old = sumweights
exceedance_counter = exceedance_counter + 1
if( exceedance_counter > 100 ) then
write(fates_log(),*) 'It is taking a long time to distribute promotion exceedance'
write(fates_log(),*) '(ie greater than c_area) to neighbors... exiting'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
end if
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
!All the trees in this layer need to promote some area upwards...
if(currentCohort%canopy_layer == i_lyr+1)then
cc_gain = currentCohort%prom_weight
if ( (cc_gain-currentCohort%c_area) > -nearzero .and. &
(cc_gain-currentCohort%c_area) < area_target_precision ) then
currentCohort%canopy_layer = i_lyr
! 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 + &
currentCohort%b_total() * currentCohort%n
elseif ( cc_gain > nearzero .and. cc_gain < currentCohort%c_area) then
allocate(copyc)
call copy_cohort(currentCohort, copyc) !makes an identical copy...
if( hlm_use_planthydro.eq.itrue ) then
call InitHydrCohort(CurrentSite,copyc)
endif
newarea = currentCohort%c_area - cc_gain !new area of existing cohort
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,currentCohort%c_area)
! number of individuals in promoted cohort.
copyc%n = currentCohort%n*cc_gain/currentCohort%c_area
! number of individuals in cohort remaining in understorey
currentCohort%n = currentCohort%n - copyc%n
currentCohort%canopy_layer = i_lyr + 1 ! keep current cohort in the understory.
copyc%canopy_layer = i_lyr ! promote copy to the higher canopy layer.
! keep track of number and biomass of promoted cohort
currentSite%promotion_rate(copyc%size_class) = &
currentSite%promotion_rate(copyc%size_class) + copyc%n
currentSite%promotion_carbonflux = currentSite%promotion_carbonflux + &
copyc%b_total() * copyc%n
call carea_allom(currentCohort%dbh,currentCohort%n,currentSite%spread, &
currentCohort%pft,currentCohort%c_area)
call carea_allom(copyc%dbh,copyc%n,currentSite%spread,copyc%pft,copyc%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_gain > currentCohort%c_area)then
write(fates_log(),*) 'more area than the cohort has is being promoted'
write(fates_log(),*) 'loss:',cc_gain
write(fates_log(),*) 'existing area:',currentCohort%c_area
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
endif ! if(currentCohort%canopy_layer == i_lyr+1) then
currentCohort => currentCohort%shorter
enddo !currentCohort
call CanopyLayerArea(currentPatch,currentSite%spread,i_lyr,arealayer_current)
if ( abs(arealayer_current - currentPatch%area)/arealayer_current &
> area_target_precision ) then
write(fates_log(),*) 'promotion did not bring area within tolerance'
write(fates_log(),*) 'arealayer:',arealayer_current
write(fates_log(),*) 'patch%area:',currentPatch%area
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
end if
return
end subroutine PromoteIntoLayer
! ============================================================================
subroutine canopy_spread( currentSite )
!
! !DESCRIPTION:
! Calculates the spatial spread of tree canopies based on canopy closure.
!
! !USES:
use EDTypesMod , only : AREA
use EDParamsMod, only : ED_val_canopy_closure_thresh
!
! !ARGUMENTS
type (ed_site_type), intent(inout), target :: currentSite
!
! !LOCAL VARIABLES:
type (ed_cohort_type), pointer :: currentCohort
type (ed_patch_type) , pointer :: currentPatch
real(r8) :: sitelevel_canopyarea ! Amount of canopy in top layer at the site level
real(r8) :: inc ! Arbitrary daily incremental change in canopy area
integer :: z
!----------------------------------------------------------------------
inc = 0.05_r8
currentPatch => currentSite%oldest_patch
sitelevel_canopyarea = 0.0_r8
do while (associated(currentPatch))
!calculate canopy area in each patch...
currentCohort => currentPatch%tallest
do while (associated(currentCohort))
call carea_allom(currentCohort%dbh,currentCohort%n, &
currentSite%spread,currentCohort%pft,currentCohort%c_area)
if( (EDPftvarcon_inst%woody(currentCohort%pft) .eq. 1 ) .and. &
(currentCohort%canopy_layer .eq. 1 ) ) then
sitelevel_canopyarea = sitelevel_canopyarea + currentCohort%c_area
endif
currentCohort => currentCohort%shorter
enddo
currentPatch => currentPatch%younger
enddo !currentPatch
!If the canopy area is approaching closure, squash the tree canopies and make them taller and thinner
if( sitelevel_canopyarea/AREA .gt. ED_val_canopy_closure_thresh ) then
currentSite%spread = currentSite%spread - inc
else
currentSite%spread = currentSite%spread + inc
endif
! put within bounds to make sure it stays between 0 and 1
currentSite%spread = max(min(currentSite%spread, 1._r8), 0._r8)
end subroutine canopy_spread
! =====================================================================================
subroutine canopy_summarization( nsites, sites, bc_in )
! ----------------------------------------------------------------------------------
! Much of this routine was once ed_clm_link minus all the IO and history stuff
! ---------------------------------------------------------------------------------
use FatesInterfaceMod , only : bc_in_type
use EDPatchDynamicsMod , only : set_patchno
use FatesAllometryMod , only : set_root_fraction
use FatesAllometryMod , only : i_hydro_rootprof_context
use FatesSizeAgeTypeIndicesMod, only : sizetype_class_index
use EDtypesMod , only : area
use EDPftvarcon , only : EDPftvarcon_inst
! !ARGUMENTS
integer , intent(in) :: nsites
type(ed_site_type) , intent(inout), target :: sites(nsites)
type(bc_in_type) , intent(in) :: bc_in(nsites)
!
! !LOCAL VARIABLES:
type (ed_patch_type) , pointer :: currentPatch
type (ed_cohort_type) , pointer :: currentCohort
integer :: s
integer :: ft ! plant functional type
integer :: ifp
integer :: patchn ! identification number for each patch.
real(r8) :: canopy_leaf_area ! total amount of leaf area in the vegetated area. m2.
!----------------------------------------------------------------------
if ( DEBUG ) then
write(fates_log(),*) 'in canopy_summarization'
endif
do s = 1,nsites
! --------------------------------------------------------------------------------
! Set the patch indices (this is usefull mostly for communicating with a host or
! driving model. Loops through all patches and sets cpatch%patchno to the integer
! order of oldest to youngest where the oldest is 1.
! --------------------------------------------------------------------------------
call set_patchno( sites(s) )
currentPatch => sites(s)%oldest_patch
do while(associated(currentPatch))
! Calculate rooting depth fractions for the patch x pft
! Note that we are calling for the root fractions in the hydrologic context.
! See explanation in FatesAllometryMod. In other locations, this
! function is called to return the profile of biomass as used for litter
do ft = 1, numpft
call set_root_fraction(currentPatch%rootfr_ft(ft,1:bc_in(s)%nlevsoil), ft, &
bc_in(s)%zi_sisl,lowerb=lbound(bc_in(s)%zi_sisl,1), &
icontext=i_hydro_rootprof_context)
end do
!zero cohort-summed variables.
currentPatch%total_canopy_area = 0.0_r8