-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathEDInitMod.F90
1344 lines (1155 loc) · 62.8 KB
/
EDInitMod.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 EDInitMod
! ============================================================================
! Contains all modules to set up the ED structure.
! ============================================================================
use FatesConstantsMod , only : r8 => fates_r8
use FatesConstantsMod , only : ifalse
use FatesConstantsMod , only : itrue
use FatesConstantsMod , only : fates_unset_int
use FatesConstantsMod , only : primaryland
use FatesConstantsMod , only : nearzero
use FatesConstantsMod , only : rsnbl_math_prec
use EDTypesMod , only : min_patch_area_forced
use FatesConstantsMod , only : n_landuse_cats
use FatesConstantsMod , only : is_crop
use FatesConstantsMod , only : fates_unset_r8
use FatesConstantsMod , only : nearzero, area_error_4, area_error_3
use FatesGlobals , only : endrun => fates_endrun
use EDParamsMod , only : nclmax
use EDParamsMod , only : regeneration_model
use FatesGlobals , only : fates_log
use FatesInterfaceTypesMod , only : hlm_is_restart
use FatesInterfaceTypesMod , only : hlm_current_tod
use EDPftvarcon , only : EDPftvarcon_inst
use PRTParametersMod , only : prt_params
use EDCohortDynamicsMod , only : create_cohort, fuse_cohorts, sort_cohorts
use EDCohortDynamicsMod , only : InitPRTObject
use EDPatchDynamicsMod , only : set_patchno
use EDPhysiologyMod , only : calculate_sp_properties
use ChecksBalancesMod , only : SiteMassStock
use FatesInterfaceTypesMod , only : hlm_day_of_year
use FatesRadiationMemMod , only : num_swb
use EDTypesMod , only : ed_site_type
use FatesPatchMod , only : fates_patch_type
use FatesCohortMod , only : fates_cohort_type
use EDTypesMod , only : numWaterMem
use EDTypesMod , only : num_vegtemp_mem
use EDTypesMod , only : area, area_inv
use EDTypesMod , only : init_spread_near_bare_ground
use EDTypesMod , only : init_spread_inventory
use FatesConstantsMod , only : leaves_on
use FatesConstantsMod , only : leaves_off
use FatesConstantsMod , only : ihard_stress_decid
use FatesConstantsMod , only : isemi_stress_decid
use PRTGenericMod , only : num_elements
use PRTGenericMod , only : element_list
use EDTypesMod , only : phen_cstat_nevercold
use EDTypesMod , only : phen_cstat_iscold
use EDTypesMod , only : phen_dstat_timeoff
use EDTypesMod , only : phen_dstat_moistoff
use EDTypesMod , only : phen_cstat_notcold
use EDTypesMod , only : phen_dstat_moiston
use FatesInterfaceTypesMod , only : bc_in_type,bc_out_type
use FatesInterfaceTypesMod , only : hlm_use_planthydro
use FatesInterfaceTypesMod , only : hlm_use_inventory_init
use FatesInterfaceTypesMod , only : hlm_use_fixed_biogeog
use FatesInterfaceTypesMod , only : hlm_use_tree_damage
use FatesInterfaceTypesMod , only : hlm_use_sp
use FatesInterfaceTypesMod , only : hlm_use_luh
use FatesInterfaceTypesMod , only : numpft
use FatesInterfaceTypesMod , only : nleafage
use FatesInterfaceTypesMod , only : nlevsclass
use FatesInterfaceTypesMod , only : nlevcoage
use FatesInterfaceTypesMod , only : nlevdamage
use FatesInterfaceTypesMod , only : hlm_use_nocomp
use FatesInterfaceTypesMod , only : nlevage
use FatesAllometryMod , only : h2d_allom
use FatesAllometryMod , only : h_allom
use FatesAllometryMod , only : bagw_allom
use FatesAllometryMod , only : bbgw_allom
use FatesAllometryMod , only : bleaf
use FatesAllometryMod , only : bfineroot
use FatesAllometryMod , only : bsap_allom
use FatesAllometryMod , only : bdead_allom
use FatesAllometryMod , only : bstore_allom
use FatesAllometryMod , only : carea_allom
use PRTGenericMod , only : StorageNutrientTarget
use FatesInterfaceTypesMod, only : hlm_parteh_mode
use PRTGenericMod, only : prt_carbon_allom_hyp
use PRTGenericMod, only : prt_cnp_flex_allom_hyp
use PRTGenericMod, only : prt_vartypes
use PRTGenericMod, only : leaf_organ
use PRTGenericMod, only : fnrt_organ
use PRTGenericMod, only : sapw_organ
use PRTGenericMod, only : store_organ
use PRTGenericMod, only : struct_organ
use PRTGenericMod, only : repro_organ
use PRTGenericMod, only : carbon12_element
use PRTGenericMod, only : nitrogen_element
use PRTGenericMod, only : phosphorus_element
use PRTGenericMod, only : SetState
use FatesSizeAgeTypeIndicesMod,only : get_age_class_index
use DamageMainMod, only : undamaged_class
use FatesConstantsMod, only : n_term_mort_types
use FatesInterfaceTypesMod, only : hlm_num_luh2_transitions
use FatesConstantsMod, only : nocomp_bareground_land, nocomp_bareground
use FatesConstantsMod, only : min_nocomp_pftfrac_perlanduse
use EdTypesMod, only : dump_site
use SFNesterovMod, only : nesterov_index
! CIME GLOBALS
use shr_log_mod , only : errMsg => shr_log_errMsg
use shr_infnan_mod , only : isnan => shr_infnan_isnan
implicit none
private
logical :: debug = .false.
integer :: istat ! return status code
character(len=255) :: smsg ! Message string for deallocation errors
character(len=*), parameter, private :: sourcefile = &
__FILE__
public :: zero_site
public :: init_site_vars
public :: init_patches
public :: set_site_properties
private :: init_cohorts
! ============================================================================
contains
! ============================================================================
subroutine init_site_vars( site_in, bc_in, bc_out )
!
! !DESCRIPTION:
!
!
! !ARGUMENTS
type(ed_site_type), intent(inout) :: site_in
type(bc_in_type),intent(in) :: bc_in
type(bc_out_type),intent(in) :: bc_out
!
! !LOCAL VARIABLES:
!----------------------------------------------------------------------
integer :: el
!
allocate(site_in%term_nindivs_canopy(1:n_term_mort_types,1:nlevsclass,1:numpft))
allocate(site_in%term_nindivs_ustory(1:n_term_mort_types,1:nlevsclass,1:numpft))
allocate(site_in%demotion_rate(1:nlevsclass))
allocate(site_in%promotion_rate(1:nlevsclass))
allocate(site_in%imort_rate(1:nlevsclass,1:numpft))
allocate(site_in%fmort_rate_canopy(1:nlevsclass,1:numpft))
allocate(site_in%fmort_rate_ustory(1:nlevsclass,1:numpft))
allocate(site_in%fmort_rate_cambial(1:nlevsclass,1:numpft))
allocate(site_in%fmort_rate_crown(1:nlevsclass,1:numpft))
allocate(site_in%growthflux_fusion(1:nlevsclass,1:numpft))
allocate(site_in%mass_balance(1:num_elements))
allocate(site_in%iflux_balance(1:num_elements))
if (hlm_use_tree_damage .eq. itrue) then
allocate(site_in%term_nindivs_canopy_damage(1:nlevdamage, 1:nlevsclass, 1:numpft))
allocate(site_in%term_nindivs_ustory_damage(1:nlevdamage, 1:nlevsclass, 1:numpft))
allocate(site_in%imort_rate_damage(1:nlevdamage, 1:nlevsclass, 1:numpft))
allocate(site_in%imort_cflux_damage(1:nlevdamage, 1:nlevsclass))
allocate(site_in%term_cflux_canopy_damage(1:nlevdamage, 1:nlevsclass))
allocate(site_in%term_cflux_ustory_damage(1:nlevdamage, 1:nlevsclass))
allocate(site_in%fmort_rate_canopy_damage(1:nlevdamage, 1:nlevsclass, 1:numpft))
allocate(site_in%fmort_rate_ustory_damage(1:nlevdamage, 1:nlevsclass, 1:numpft))
allocate(site_in%fmort_cflux_canopy_damage(1:nlevdamage, 1:nlevsclass))
allocate(site_in%fmort_cflux_ustory_damage(1:nlevdamage, 1:nlevsclass))
else
allocate(site_in%term_nindivs_canopy_damage(1,1,1))
allocate(site_in%term_nindivs_ustory_damage(1,1,1))
allocate(site_in%imort_rate_damage(1,1,1))
allocate(site_in%imort_cflux_damage(1,1))
allocate(site_in%term_cflux_canopy_damage(1,1))
allocate(site_in%term_cflux_ustory_damage(1,1))
allocate(site_in%fmort_rate_canopy_damage(1,1,1))
allocate(site_in%fmort_rate_ustory_damage(1,1,1))
allocate(site_in%fmort_cflux_canopy_damage(1,1))
allocate(site_in%fmort_cflux_ustory_damage(1,1))
end if
allocate(site_in%term_carbonflux_canopy(1:n_term_mort_types,1:numpft))
allocate(site_in%term_carbonflux_ustory(1:n_term_mort_types,1:numpft))
allocate(site_in%imort_carbonflux(1:numpft))
allocate(site_in%fmort_carbonflux_canopy(1:numpft))
allocate(site_in%fmort_carbonflux_ustory(1:numpft))
allocate(site_in%term_abg_flux(1:nlevsclass,1:numpft))
allocate(site_in%imort_abg_flux(1:nlevsclass,1:numpft))
allocate(site_in%fmort_abg_flux(1:nlevsclass,1:numpft))
site_in%nlevsoil = bc_in%nlevsoil
allocate(site_in%rootfrac_scr(site_in%nlevsoil))
allocate(site_in%zi_soil(0:site_in%nlevsoil))
allocate(site_in%dz_soil(site_in%nlevsoil))
allocate(site_in%z_soil(site_in%nlevsoil))
allocate(site_in%area_pft(1:numpft,1:n_landuse_cats))
allocate(site_in%landuse_vector_gt_min(1:n_landuse_cats))
allocate(site_in%use_this_pft(1:numpft))
allocate(site_in%area_by_age(1:nlevage))
! for CNP dynamics, track the mean l2fr of recruits
! for different pfts and canopy positions
allocate(site_in%rec_l2fr(1:numpft,nclmax))
! SP mode
allocate(site_in%sp_tlai(1:numpft))
allocate(site_in%sp_tsai(1:numpft))
allocate(site_in%sp_htop(1:numpft))
! Allocate site-level flux diagnostics
! -----------------------------------------------------------------------
allocate(site_in%flux_diags%elem(1:num_elements))
do el=1,num_elements
allocate(site_in%flux_diags%elem(el)%surf_fine_litter_input(1:numpft))
allocate(site_in%flux_diags%elem(el)%root_litter_input(1:numpft))
end do
allocate(site_in%flux_diags%nh4_uptake_scpf(numpft*nlevsclass))
allocate(site_in%flux_diags%no3_uptake_scpf(numpft*nlevsclass))
allocate(site_in%flux_diags%sym_nfix_scpf(numpft*nlevsclass))
allocate(site_in%flux_diags%n_efflux_scpf(numpft*nlevsclass))
allocate(site_in%flux_diags%p_uptake_scpf(numpft*nlevsclass))
allocate(site_in%flux_diags%p_efflux_scpf(numpft*nlevsclass))
! Initialize the static soil
! arrays from the boundary (initial) condition
site_in%zi_soil(:) = bc_in%zi_sisl(:)
site_in%dz_soil(:) = bc_in%dz_sisl(:)
site_in%z_soil(:) = bc_in%z_sisl(:)
! Seed dispersal
allocate(site_in%seed_in(1:numpft))
allocate(site_in%seed_out(1:numpft))
allocate(nesterov_index :: site_in%fireWeather)
call site_in%fireWeather%Init()
end subroutine init_site_vars
! ============================================================================
subroutine zero_site( site_in )
!
! !DESCRIPTION:
!
! !USES:
use shr_infnan_mod , only : nan => shr_infnan_nan, assignment(=)
!
! !ARGUMENTS
type(ed_site_type), intent(inout) :: site_in
!
! !LOCAL VARIABLES:
integer :: el
!----------------------------------------------------------------------
site_in%oldest_patch => null() ! pointer to oldest patch at the site
site_in%youngest_patch => null() ! pointer to yngest patch at the site
! PHENOLOGY
site_in%cstatus = fates_unset_int ! are leaves in this pixel on or off?
site_in%dstatus(:) = fates_unset_int
site_in%grow_deg_days = nan ! growing degree days
site_in%snow_depth = nan
site_in%nchilldays = fates_unset_int
site_in%ncolddays = fates_unset_int
site_in%cleafondate = fates_unset_int ! doy of leaf on (cold)
site_in%cleafoffdate = fates_unset_int ! doy of leaf off (cold)
site_in%dleafondate(:) = fates_unset_int ! doy of leaf on (drought)
site_in%dleafoffdate(:) = fates_unset_int ! doy of leaf off (drought)
site_in%cndaysleafon = fates_unset_int ! days since leaf on (cold)
site_in%cndaysleafoff = fates_unset_int ! days since leaf off (cold)
site_in%dndaysleafon(:) = fates_unset_int ! days since leaf on (drought)
site_in%dndaysleafoff(:) = fates_unset_int ! days since leaf off (drought)
site_in%elong_factor(:) = nan ! Elongation factor (0 - full abscission; 1 - fully flushed)
site_in%liqvol_memory(:,:) = nan
site_in%smp_memory(:,:) = nan
site_in%vegtemp_memory(:) = nan ! record of last 10 days temperature for senescence model.
site_in%phen_model_date = fates_unset_int
! Disturbance rates tracking
site_in%primary_land_patchfusion_error = 0.0_r8
site_in%disturbance_rates(:,:,:) = 0.0_r8
site_in%landuse_transition_matrix(:,:) = 0.0_r8
! FIRE
site_in%FDI = 0.0_r8 ! daily fire danger index (0-1)
site_in%NF = 0.0_r8 ! daily lightning strikes per km2
site_in%NF_successful = 0.0_r8 ! daily successful iginitions per km2
do el=1,num_elements
! Zero the state variables used for checking mass conservation
call site_in%mass_balance(el)%ZeroMassBalState()
call site_in%mass_balance(el)%ZeroMassBalFlux()
end do
call site_in%flux_diags%ZeroFluxDiags()
! This will be initialized in FatesSoilBGCFluxMod:PrepCH4BCs()
! It checks to see if the value is below -9000. If it is,
! it will assume the first value of the smoother is set
site_in%ema_npp = -9999.9_r8
! termination and recruitment info
site_in%term_nindivs_canopy(:,:,:) = 0._r8
site_in%term_nindivs_ustory(:,:,:) = 0._r8
site_in%term_crownarea_canopy = 0._r8
site_in%term_crownarea_ustory = 0._r8
site_in%imort_crownarea = 0._r8
site_in%fmort_crownarea_canopy = 0._r8
site_in%fmort_crownarea_ustory = 0._r8
site_in%term_carbonflux_canopy(:,:) = 0._r8
site_in%term_carbonflux_ustory(:,:) = 0._r8
site_in%recruitment_rate(:) = 0._r8
site_in%imort_rate(:,:) = 0._r8
site_in%imort_carbonflux(:) = 0._r8
site_in%fmort_rate_canopy(:,:) = 0._r8
site_in%fmort_rate_ustory(:,:) = 0._r8
site_in%fmort_carbonflux_canopy(:) = 0._r8
site_in%fmort_carbonflux_ustory(:) = 0._r8
site_in%fmort_rate_cambial(:,:) = 0._r8
site_in%fmort_rate_crown(:,:) = 0._r8
site_in%term_abg_flux(:,:) = 0._r8
site_in%imort_abg_flux(:,:) = 0._r8
site_in%fmort_abg_flux(:,:) = 0._r8
! fusoin-induced growth flux of individuals
site_in%growthflux_fusion(:,:) = 0._r8
! demotion/promotion info
site_in%demotion_rate(:) = 0._r8
site_in%demotion_carbonflux = 0._r8
site_in%promotion_rate(:) = 0._r8
site_in%promotion_carbonflux = 0._r8
! damage transition info
site_in%imort_rate_damage(:,:,:) = 0._r8
site_in%term_nindivs_canopy_damage(:,:,:) = 0._r8
site_in%term_nindivs_ustory_damage(:,:,:) = 0._r8
site_in%imort_cflux_damage(:,:) = 0._r8
site_in%term_cflux_canopy_damage(:,:) = 0._r8
site_in%term_cflux_ustory_damage(:,:) = 0._r8
site_in%crownarea_canopy_damage = 0._r8
site_in%crownarea_ustory_damage = 0._r8
site_in%fmort_rate_canopy_damage(:,:,:) = 0._r8
site_in%fmort_rate_ustory_damage(:,:,:) = 0._r8
site_in%fmort_cflux_canopy_damage(:,:) = 0._r8
site_in%fmort_cflux_ustory_damage(:,:) = 0._r8
! Resources management (logging/harvesting, etc)
site_in%resources_management%harvest_debt = 0.0_r8
site_in%resources_management%harvest_debt_sec = 0.0_r8
site_in%resources_management%trunk_product_site = 0.0_r8
! canopy spread
site_in%spread = 0._r8
site_in%area_pft(:,:) = 0._r8
site_in%area_bareground = 0._r8
! Seed dispersal
site_in%seed_in(:) = 0.0_r8
site_in%seed_out(:) = 0.0_r8
site_in%use_this_pft(:) = fates_unset_int
site_in%area_by_age(:) = 0._r8
site_in%transition_landuse_from_off_to_on = .false.
end subroutine zero_site
! ============================================================================
subroutine set_site_properties( nsites, sites,bc_in )
!
! !DESCRIPTION:
!
! !USES:
use EDParamsMod, only : crop_lu_pft_vector
use EDParamsMod, only : max_nocomp_pfts_by_landuse
!
! !ARGUMENTS
integer, intent(in) :: nsites
type(ed_site_type) , intent(inout) :: sites(nsites)
type(bc_in_type), intent(in) :: bc_in(nsites)
!
! !LOCAL VARIABLES:
integer :: s
integer :: cstat ! cold status phenology flag
real(r8) :: GDD
integer :: dstat ! drought status phenology flag
real(r8) :: liqvolmem
real(r8) :: smpmem
real(r8) :: elong_factor ! Elongation factor (0 - fully off; 1 - fully on)
integer :: cleafon ! DOY for cold-decid leaf-on, initial guess
integer :: cleafoff ! DOY for cold-decid leaf-off, initial guess
integer :: dleafoff ! DOY for drought-decid leaf-off, initial guess
integer :: dleafon ! DOY for drought-decid leaf-on, initial guess
integer :: cndleafon ! days since leaf on (cold), initial guess
integer :: cndleafoff ! days since leaf off (cold), initial guess
integer :: dndleafon ! days since leaf on (drought), initial guess
integer :: dndleafoff ! days since leaf off (drought), initial guess
integer :: ft ! PFT loop
real(r8) :: sumarea ! area of PFTs in nocomp mode.
integer :: hlm_pft ! used in fixed biogeog mode
integer :: fates_pft ! used in fixed biogeog mode
integer :: i_landusetype
real(r8) :: temp_vec(numpft) ! temporary vector
integer :: i_pftcount
!----------------------------------------------------------------------
! If this is not a restart, we need to start with some reasonable
! starting points. If this is a restart, we leave the values
! as unset ints and reals, and let the restart values be read in
! after this routine
if ( hlm_is_restart == ifalse ) then
GDD = 30.0_r8
cleafon = 100
cleafoff = 300
cndleafon = 0
cndleafoff = 0
cstat = phen_cstat_notcold ! Leaves are on
dstat = phen_dstat_moiston ! Leaves are on
dleafoff = 300
dleafon = 100
dndleafon = 0
dndleafoff = 0
liqvolmem = 0.5_r8
smpmem = 0._r8
elong_factor = 1._r8
do s = 1,nsites
sites(s)%nchilldays = 0
sites(s)%ncolddays = 0 ! recalculated in phenology
! immediately, so yes this
! is memory-less, but needed
! for first value in history file
sites(s)%phen_model_date = 0
sites(s)%cleafondate = cleafon - hlm_day_of_year
sites(s)%cleafoffdate = cleafoff - hlm_day_of_year
sites(s)%cndaysleafon = cndleafon
sites(s)%cndaysleafoff = cndleafoff
sites(s)%dleafoffdate (1:numpft) = dleafoff - hlm_day_of_year
sites(s)%dleafondate (1:numpft) = dleafon - hlm_day_of_year
sites(s)%dndaysleafon (1:numpft) = dndleafon
sites(s)%dndaysleafoff(1:numpft) = dndleafoff
sites(s)%grow_deg_days = GDD
sites(s)%liqvol_memory(1:numWaterMem,1:numpft) = liqvolmem
sites(s)%smp_memory(1:numWaterMem,1:numpft) = smpmem
sites(s)%vegtemp_memory(1:num_vegtemp_mem) = 0._r8
sites(s)%cstatus = cstat
sites(s)%dstatus(1:numpft) = dstat
sites(s)%elong_factor(1:numpft) = elong_factor
sites(s)%NF = 0.0_r8
sites(s)%NF_successful = 0.0_r8
sites(s)%area_pft(:,:) = 0.0_r8
do ft = 1,numpft
sites(s)%rec_l2fr(ft,:) = prt_params%allom_l2fr(ft)
end do
! Its difficult to come up with a resonable starting smoothing value, so
! we initialize on a cold-start to -1
sites(s)%ema_npp = -9999._r8
if(hlm_use_fixed_biogeog.eq.itrue)then
use_fates_luh_if: if (hlm_use_luh .eq. itrue) then
! MAPPING OF FATES PFTs on to HLM_PFTs with land use
! add up the area associated with each FATES PFT
! where pft_areafrac_lu is the area of land in each HLM PFT and land use type (from surface dataset)
! hlm_pft_map is the area of that land in each FATES PFT (from param file)
! First check for NaNs in bc_in(s)%pft_areafrac_lu. If so, make everything bare ground.
if ( .not. (any( isnan( bc_in(s)%pft_areafrac_lu (:,:) )) .or. isnan( bc_in(s)%baregroundfrac))) then
do i_landusetype = 1, n_landuse_cats
if (.not. is_crop(i_landusetype)) then
do hlm_pft = 1,size( EDPftvarcon_inst%hlm_pft_map,2)
do fates_pft = 1,numpft ! loop round all fates pfts for all hlm pfts
sites(s)%area_pft(fates_pft,i_landusetype) = sites(s)%area_pft(fates_pft,i_landusetype) + &
EDPftvarcon_inst%hlm_pft_map(fates_pft,hlm_pft) * bc_in(s)%pft_areafrac_lu(hlm_pft,i_landusetype)
end do
end do !hlm_pft
else
! for crops, we need to use different logic because the bc_in(s)%pft_areafrac_lu() information only exists for natural PFTs
sites(s)%area_pft(crop_lu_pft_vector(i_landusetype),i_landusetype) = 1._r8
endif
end do
sites(s)%area_bareground = bc_in(s)%baregroundfrac
else
!if ( all( isnan( bc_in(s)%pft_areafrac_lu (:,:))) .and. isnan(bc_in(s)%baregroundfrac)) then
! if given all NaNs, then make everything bare ground
sites(s)%area_bareground = 1._r8
sites(s)%area_pft(:,:) = 0._r8
write(fates_log(),*) 'Nan values for pftareafrac. dumping site info.'
call dump_site(sites(s))
!else
! ! if only some things are NaN but not all, then something terrible has probably happened. crash.
! write(fates_log(),*) 'some but, not all, of the data in the PFT by LU matrix at this site is NaN.'
! write(fates_log(),*) 'recommend checking the dataset to see what has happened.'
! call endrun(msg=errMsg(sourcefile, __LINE__))
!endif
endif
else
! MAPPING OF FATES PFTs on to HLM_PFTs
! add up the area associated with each FATES PFT
! where pft_areafrac is the area of land in each HLM PFT and (from surface dataset)
! hlm_pft_map is the area of that land in each FATES PFT (from param file)
do hlm_pft = 1,size( EDPftvarcon_inst%hlm_pft_map,2)
do fates_pft = 1,numpft ! loop round all fates pfts for all hlm pfts
sites(s)%area_pft(fates_pft,primaryland) = sites(s)%area_pft(fates_pft,primaryland) + &
EDPftvarcon_inst%hlm_pft_map(fates_pft,hlm_pft) * bc_in(s)%pft_areafrac(hlm_pft)
end do
sites(s)%area_bareground = bc_in(s)%pft_areafrac(0)
end do !hlm_pft
endif use_fates_luh_if
! handle some edge cases
do i_landusetype = 1, n_landuse_cats
do ft = 1,numpft
! remove tiny patches to prevent numerical errors in terminate patches
if (sites(s)%area_pft(ft, i_landusetype) .lt. min_nocomp_pftfrac_perlanduse &
.and. sites(s)%area_pft(ft, i_landusetype) .gt. nearzero) then
if(debug) write(fates_log(),*) 'removing small numbers in site%area_pft',s,ft,i_landusetype,sites(s)%area_pft(ft, i_landusetype)
sites(s)%area_pft(ft, i_landusetype)=0.0_r8
endif
! if any areas are negative, then end run
if(sites(s)%area_pft(ft, i_landusetype).lt.0._r8)then
write(fates_log(),*) 'negative area',s,ft,i_landusetype,sites(s)%area_pft(ft, i_landusetype)
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
end do
! if in nocomp mode, and the number of nocomp PFTs of a given land use type is greater than the maximum number of patches
! allowed to be allocated for that land use type, then only keep the number of PFTs correspondign to the number of patches
! allowed on that land use type, starting with the PFTs with greatest area coverage and working down
if (hlm_use_nocomp .eq. itrue) then
do i_landusetype = 1, n_landuse_cats
! count how many PFTs have areas greater than zero and compare to the number of patches allowed
if (COUNT(sites(s)%area_pft(:, i_landusetype) .gt. 0._r8) > max_nocomp_pfts_by_landuse(i_landusetype)) then
! write current vector to log file
if(debug) write(fates_log(),*) 'too many PFTs for LU type ', i_landusetype, sites(s)%area_pft(:, i_landusetype)
! start from largest area, put that PFT's area into a temp vector, and then work down to successively smaller-area PFTs,
! at the end replace the original vector with the temp vector
temp_vec(:) = 0._r8
do i_pftcount = 1, max_nocomp_pfts_by_landuse(i_landusetype)
temp_vec(MAXLOC(sites(s)%area_pft(:, i_landusetype))) = &
sites(s)%area_pft(MAXLOC(sites(s)%area_pft(:, i_landusetype)), i_landusetype)
sites(s)%area_pft(MAXLOC(sites(s)%area_pft(:, i_landusetype)), i_landusetype) = 0._r8
end do
sites(s)%area_pft(:, i_landusetype) = temp_vec(:)
! write adjusted vector to log file
if(debug) write(fates_log(),*) 'new PFT vector for LU type', i_landusetype, sites(s)%area_pft(:, i_landusetype)
endif
end do
end if
! re-normalize PFT area to ensure it sums to one for each (active) land use type
! for nocomp cases, track bare ground area as a separate quantity
do i_landusetype = 1, n_landuse_cats
sumarea = sum(sites(s)%area_pft(:,i_landusetype))
if(sumarea.gt.nearzero)then
sites(s)%area_pft(:, i_landusetype) = sites(s)%area_pft(:, i_landusetype)/sumarea
else
! if no PFT area in primary lands, set bare ground fraction to one.
if ( i_landusetype .eq. primaryland) then
sites(s)%area_bareground = 1._r8
sites(s)%area_pft(:, i_landusetype) = 0._r8
endif
end if
end do
end if !fixed biogeog
do ft = 1,numpft
! Setting this to true ensures that all pfts
! are used for nocomp with no biogeog
sites(s)%use_this_pft(ft) = itrue
if(hlm_use_fixed_biogeog.eq.itrue)then
if(any(sites(s)%area_pft(ft,:).gt.0.0_r8))then
sites(s)%use_this_pft(ft) = itrue
else
sites(s)%use_this_pft(ft) = ifalse
end if !area
end if !SBG
end do !ft
! need to set the minimum amount of allowable land-use fraction on a given site. this is a function of the minimum allowable patch size,
! and for nocomp simulations also the bare ground fraction and the minimum pft fraction for a given land-use type.
if (hlm_use_nocomp .eq. itrue ) then
if ( (1._r8 - sites(s)%area_bareground) .gt. nearzero) then
sites(s)%min_allowed_landuse_fraction = min_patch_area_forced / (AREA * min_nocomp_pftfrac_perlanduse * (1._r8 - sites(s)%area_bareground))
else
! if all bare ground, shouldn't matter. but make it one anyway to really ignore land use (which should all be NaNs anyway)
sites(s)%min_allowed_landuse_fraction = 1._r8
endif
else
sites(s)%min_allowed_landuse_fraction = min_patch_area_forced / AREA
endif
end do !site loop
end if !restart
return
end subroutine set_site_properties
! ============================================================================
subroutine init_patches( nsites, sites, bc_in)
!
! !DESCRIPTION:
! initialize patches
! This may be call a near bare ground initialization, or it may
! load patches from an inventory.
use FatesPlantHydraulicsMod, only : updateSizeDepRhizHydProps
use FatesInventoryInitMod, only : initialize_sites_by_inventory
use FatesLandUseChangeMod, only : GetLUHStatedata
!
! !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:
integer :: s
integer :: el
real(r8) :: age !notional age of this patch
integer :: ageclass
real(r8) :: area_diff
real(r8) :: area_error
! dummy locals
real(r8) :: biomass_stock
real(r8) :: litter_stock
real(r8) :: seed_stock
integer :: n
integer :: start_patch
integer :: num_nocomp_pfts
integer :: nocomp_pft
real(r8) :: newparea, newparea_withlanduse
real(r8) :: total !check on area
real(r8) :: litt_init !invalid for satphen, 0 otherwise
real(r8) :: old_carea
logical :: is_first_patch
! integer :: n_luh_states
! integer :: luh_state_counter
real(r8) :: state_vector(n_landuse_cats) ! [m2/m2]
integer :: i_lu, i_lu_state
integer :: n_active_landuse_cats
integer :: end_landuse_idx
type(ed_site_type), pointer :: sitep
type(fates_patch_type), pointer :: newppft(:)
type(fates_patch_type), pointer :: newp
type(fates_cohort_type), pointer :: cohort
type(fates_patch_type), pointer :: currentPatch
! List out some nominal patch values that are used for Near Bear Ground initializations
! as well as initializing inventory
age = 0.0_r8
! ---------------------------------------------------------------------------------------------
! ---------------------------------------------------------------------------------------------
! Two primary options, either a Near Bear Ground (NBG) or Inventory based cold-start
! ---------------------------------------------------------------------------------------------
if ( hlm_use_inventory_init.eq.itrue ) then
! Initialize the site-level crown area spread factor (0-1)
! It is likely that closed canopy forest inventories
! have smaller spread factors than bare ground (they are crowded)
do s = 1, nsites
sites(s)%spread = init_spread_inventory
enddo
call initialize_sites_by_inventory(nsites,sites,bc_in)
! For carbon balance checks, we need to initialize the
! total carbon stock
do s = 1, nsites
do el=1,num_elements
call SiteMassStock(sites(s),el,sites(s)%mass_balance(el)%old_stock, &
biomass_stock,litter_stock,seed_stock)
end do
call set_patchno(sites(s))
enddo
else
if(hlm_use_nocomp.eq.itrue)then
num_nocomp_pfts = numpft
else !default
num_nocomp_pfts = 1
end if !nocomp
sites_loop: do s = 1, nsites
sites(s)%sp_tlai(:) = 0._r8
sites(s)%sp_tsai(:) = 0._r8
sites(s)%sp_htop(:) = 0._r8
! Initialize the site-level crown area spread factor (0-1)
! It is likely that closed canopy forest inventories
! have smaller spread factors than bare ground (they are crowded)
sites(s)%spread = init_spread_near_bare_ground
! read in luh state data to determine initial land use types
if (hlm_use_luh .eq. itrue) then
! Set the number of active land use categories to the maximum number
! This could be updated in the future to allow a variable number of
! categories based on which states are zero
n_active_landuse_cats = n_landuse_cats
call GetLUHStatedata(bc_in(s), state_vector)
! if the land use state vector is greater than the minimum value, set landuse_vector_gt_min flag to true
! otherwise set to false.
do i_lu_state = 1, n_landuse_cats
if (state_vector(i_lu_state) .gt. sites(s)%min_allowed_landuse_fraction) then
sites(s)%landuse_vector_gt_min(i_lu_state) = .true.
else
sites(s)%landuse_vector_gt_min(i_lu_state) = .false.
end if
end do
else
! If LUH2 data is not being used, we initialize with primarylands,
! i.e. array index equals '1'
n_active_landuse_cats = primaryland
state_vector(:) = 0._r8
state_vector(primaryland) = 1._r8
endif
! confirm that state vector sums to 1.
if (abs(sum(state_vector(:))-1._r8) .gt. rsnbl_math_prec) then
write(fates_log(),*) 'error that the state vector must sum to 1, but doesnt'
write(fates_log(),*) 'sum(state_vector)', sum(state_vector)
write(fates_log(),*) state_vector
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
is_first_patch = .true.
area_error = 0._r8
! first make a bare-ground patch if one is needed.
make_bareground_patch_if: if (hlm_use_nocomp.eq.itrue .and. hlm_use_fixed_biogeog .eq.itrue) then
newparea = area * sites(s)%area_bareground
if (newparea .gt. min_patch_area_forced) then
allocate(newp)
call newp%Create(age, newparea, nocomp_bareground_land, nocomp_bareground, &
num_swb, numpft, sites(s)%nlevsoil, hlm_current_tod, &
regeneration_model)
! set pointers for first patch (or only patch, if nocomp is false)
newp%patchno = 1
newp%younger => null()
newp%older => null()
sites(s)%youngest_patch => newp
sites(s)%oldest_patch => newp
is_first_patch = .false.
! Initialize the litter pools to zero, these
! pools will be populated by looping over the existing patches
! and transfering in mass
if(hlm_use_sp.eq.itrue)then
litt_init = fates_unset_r8
else
litt_init = 0._r8
end if
do el=1,num_elements
call newp%litter(el)%InitConditions(init_leaf_fines=litt_init, &
init_root_fines=litt_init, &
init_ag_cwd=litt_init, &
init_bg_cwd=litt_init, &
init_seed=litt_init, &
init_seed_germ=litt_init)
end do
else
area_error = area_error + newparea
endif
endif make_bareground_patch_if
if (hlm_use_luh .eq. itrue) then
end_landuse_idx = n_landuse_cats
else
end_landuse_idx = 1
endif
! Next, create the non-bareground patches. We do this for either of two scenarios:
! If 1) we are not doing both nocomp & fixed-biogeo
! 2) we are, but there is some non-zero bare-ground area
not_all_bare_if: if( ((1._r8 - sites(s)%area_bareground) > nearzero) .or. &
(.not.(hlm_use_nocomp.eq.itrue .and. hlm_use_fixed_biogeog.eq.itrue)) ) then
! now make one or more vegetated patches based on nocomp and land use logic
luh_state_loop: do i_lu_state = 1, end_landuse_idx
lu_state_present_if: if (state_vector(i_lu_state) .gt. nearzero) then
new_patch_nocomp_loop: do n = 1, num_nocomp_pfts
! set the PFT index for patches if in nocomp mode.
if(hlm_use_nocomp.eq.itrue)then
nocomp_pft = n
else
nocomp_pft = fates_unset_int
end if
if(hlm_use_nocomp.eq.itrue)then
! In no competition mode, if we are using the fixed_biogeog filter
! then each PFT has the area dictated by the surface dataset.
! If we are not using fixed biogeog model, each PFT gets the same area.
! i.e. each grid cell is divided exactly into the number of FATES PFTs.
if(hlm_use_fixed_biogeog.eq.itrue)then
newparea = sites(s)%area_pft(nocomp_pft,i_lu_state) * area * state_vector(i_lu_state) &
* (1._r8 - sites(s)%area_bareground)
else
newparea = area * state_vector(i_lu_state) / numpft
end if
else ! The default case is initialized w/ one patch with the area of the whole site.
newparea = area * state_vector(i_lu_state)
end if !nocomp mode
! Stop patches being initilialized when PFT not present in nocomop mode
new_patch_area_gt_zero: if(newparea .gt. min_patch_area_forced) then
allocate(newp)
call newp%Create(age, newparea, i_lu_state, nocomp_pft, &
num_swb, numpft, sites(s)%nlevsoil, hlm_current_tod, &
regeneration_model)
if (is_first_patch) then !is this the first patch?
! set pointers for first patch (or only patch, if nocomp is false)
newp%patchno = 1
newp%younger => null()
newp%older => null()
sites(s)%youngest_patch => newp
sites(s)%oldest_patch => newp
is_first_patch = .false.
else
! Set pointers for N>1 patches. Note this only happens when nocomp mode is on, or land use is on.
! The new patch is the 'youngest' one, arbitrarily.
newp%patchno = nocomp_pft + (i_lu_state-1) * numpft
newp%older => sites(s)%youngest_patch
newp%younger => null()
sites(s)%youngest_patch%younger => newp
sites(s)%youngest_patch => newp
end if
! Initialize the litter pools to zero, these
! pools will be populated by looping over the existing patches
! and transfering in mass
if(hlm_use_sp.eq.itrue)then
litt_init = fates_unset_r8
else
litt_init = 0._r8
end if
do el=1,num_elements
call newp%litter(el)%InitConditions(init_leaf_fines=litt_init, &
init_root_fines=litt_init, &
init_ag_cwd=litt_init, &
init_bg_cwd=litt_init, &
init_seed=litt_init, &
init_seed_germ=litt_init)
end do
sitep => sites(s)
call init_cohorts(sitep, newp, bc_in(s))
else
area_error = area_error+ newparea
end if new_patch_area_gt_zero
end do new_patch_nocomp_loop
end if lu_state_present_if
end do luh_state_loop
end if not_all_bare_if
! if we had to skip small patches above, resize things accordingly
if ( area_error .gt. nearzero) then
newp => sites(s)%oldest_patch
do while (associated(newp))
newp%area = newp%area * area/ (area - area_error)
newp => newp%younger
end do
endif
!check if the total area adds to the same as site area
total = 0.0_r8
newp => sites(s)%oldest_patch
do while (associated(newp))
total = total + newp%area
newp => newp%younger
end do
area_diff = total - area
if (abs(area_diff) > nearzero) then
if (abs(area_diff) < area_error_4) then ! this is a precision error
! adjust areas of all patches so that they add up to total area
newp => sites(s)%oldest_patch
do while (associated(newp))
newp%area = newp%area * (area / total)
newp => newp%younger
end do
else !this is a big error not just a precision error.
write(fates_log(),*) 'issue with patch area in EDinit', area_diff, total,sites(s)%lat,sites(s)%lon
write(fates_log(),*) 'hlm_use_nocomp: ',hlm_use_nocomp
write(fates_log(),*) 'hlm_use_fixed_biogeog: ',hlm_use_fixed_biogeog
newp => sites(s)%oldest_patch
do while (associated(newp))
write(fates_log(),*) newp%area, newp%nocomp_pft_label, newp%land_use_label
newp => newp%younger
end do
write(fates_log(),*) 'state_vector', state_vector
write(fates_log(),*) 'area_error', area_error
write(fates_log(),*) 'area_bareground', sites(s)%area_bareground
do i_lu_state = 1, end_landuse_idx
write(fates_log(),*) 'sites(s)%area_pft(:,i_lu_state)',i_lu_state, sites(s)%area_pft(:,i_lu_state)
end do
call endrun(msg=errMsg(sourcefile, __LINE__))
end if ! big error
end if ! too much patch area
! we might have messed up crown areas now - need to correct if SP mode
if (hlm_use_sp .eq. itrue) then
newp => sites(s)%oldest_patch
do while (associated(newp))
cohort => newp%tallest
do while (associated(cohort))
if (abs(cohort%c_area - newp%area) < area_error_3) then ! correct if it's a very small error
old_carea = cohort%c_area
cohort%c_area = cohort%c_area - (cohort%c_area - newp%area)
cohort%n = cohort%n*(cohort%c_area/old_carea)
end if
cohort => cohort%shorter
end do
newp => newp%younger
end do
end if
! For carbon balance checks, we need to initialize the
! total carbon stock
do el=1,num_elements
call SiteMassStock(sites(s),el,sites(s)%mass_balance(el)%old_stock, &
biomass_stock,litter_stock,seed_stock)
! Initialize the integrated flux balance diagnostics
! No need to initialize the instantaneous states, those are re-calculated
sites(s)%iflux_balance(el)%iflux_liveveg = &
(biomass_stock + seed_stock)*area_inv
sites(s)%iflux_balance(el)%iflux_litter = litter_stock * area_inv
end do
call set_patchno(sites(s))
enddo sites_loop !s
end if
! zero all the patch fire variables for the first timestep
do s = 1, nsites
currentPatch => sites(s)%youngest_patch
do while(associated(currentPatch))
currentPatch%litter_moisture(:) = 0._r8
currentPatch%fuel_eff_moist = 0._r8
currentPatch%livegrass = 0._r8
currentPatch%sum_fuel = 0._r8
currentPatch%fuel_bulkd = 0._r8
currentPatch%fuel_sav = 0._r8
currentPatch%fuel_mef = 0._r8