-
Notifications
You must be signed in to change notification settings - Fork 92
/
FatesInterfaceMod.F90
2527 lines (2043 loc) · 104 KB
/
FatesInterfaceMod.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 FatesInterfaceMod
! ------------------------------------------------------------------------------------
! This is the FATES public API
! A host land model has defined and allocated a structure "fates" as
! defined by fates_interface_type
!
! It is also likely/possible that this type is defined as a vector
! which is allocated by thread
! ------------------------------------------------------------------------------------
use EDTypesMod , only : ed_site_type
use EDParamsMod , only : dinc_vai
use EDParamsMod , only : dlower_vai
use EDParamsMod , only : ED_val_vai_top_bin_width
use EDParamsMod , only : ED_val_vai_width_increase_factor
use EDParamsMod , only : ED_val_history_damage_bin_edges
use EDParamsMod , only : maxpatch_total
use EDParamsMod , only : maxpatches_by_landuse
use EDParamsMod , only : max_cohort_per_patch
use FatesRadiationMemMod , only : num_swb,ivis,inir
use EDParamsMod , only : regeneration_model
use EDParamsMod , only : nclmax
use EDParamsMod , only : nlevleaf
use EDParamsMod , only : maxpft
use EDTypesMod , only : do_fates_salinity
use EDTypesMod , only : numWaterMem
use EDTypesMod , only : numlevsoil_max
use EDTypesMod , only : ed_site_type
use FatesPatchMod , only : fates_patch_type
use FatesCohortMod , only : fates_cohort_type
use EDTypesMod , only : area_inv
use EDTypesMod , only : num_vegtemp_mem
use FatesConstantsMod , only : r8 => fates_r8
use FatesConstantsMod , only : itrue,ifalse
use FatesConstantsMod , only : nearzero
use FatesConstantsMod , only : sec_per_day
use FatesConstantsMod , only : days_per_year
use FatesConstantsMod , only : TRS_regeneration
use FatesConstantsMod , only : g_per_kg
use FatesConstantsMod , only : n_landuse_cats
use FatesConstantsMod , only : primaryland
use FatesConstantsMod , only : secondaryland
use FatesConstantsMod , only : n_crop_lu_types
use FatesConstantsMod , only : n_term_mort_types
use FatesGlobals , only : fates_global_verbose
use FatesGlobals , only : fates_log
use FatesGlobals , only : endrun => fates_endrun
use FatesConstantsMod , only : fates_unset_r8
use FatesLitterMod , only : ncwd
use FatesLitterMod , only : ndcmpy
use EDPftvarcon , only : FatesReportPFTParams
use EDPftvarcon , only : FatesCheckParams
use EDPftvarcon , only : EDPftvarcon_inst
use SFParamsMod , only : SpitFireCheckParams
use EDParamsMod , only : FatesReportParams
use EDParamsMod , only : bgc_soil_salinity
use FatesPlantHydraulicsMod , only : InitHydroGlobals
use EDParamsMod , only : photo_temp_acclim_timescale
use EDParamsMod , only : photo_temp_acclim_thome_time
use EDParamsMod , only : sdlng_emerg_h2o_timescale
use EDParamsMod , only : sdlng_mort_par_timescale
use EDParamsMod , only : sdlng2sap_par_timescale
use EDParamsMod , only : sdlng_mdd_timescale
use EDParamsMod , only : ED_val_history_sizeclass_bin_edges
use EDParamsMod , only : ED_val_history_ageclass_bin_edges
use EDParamsMod , only : ED_val_history_height_bin_edges
use EDParamsMod , only : ED_val_history_coageclass_bin_edges
use FatesParametersInterface , only : fates_param_reader_type
use FatesParametersInterface , only : fates_parameters_type
use EDParamsMod , only : FatesRegisterParams, FatesReceiveParams
use SFParamsMod , only : SpitFireRegisterParams, SpitFireReceiveParams
use PRTInitParamsFATESMod , only : PRTRegisterParams, PRTReceiveParams
use FatesSynchronizedParamsMod, only : FatesSynchronizedParamsInst
use EDParamsMod , only : p_uptake_mode
use EDParamsMod , only : n_uptake_mode
use EDTypesMod , only : ed_site_type
use FatesConstantsMod , only : prescribed_p_uptake
use FatesConstantsMod , only : prescribed_n_uptake
use FatesConstantsMod , only : coupled_p_uptake
use FatesConstantsMod , only : coupled_n_uptake
use FatesConstantsMod , only : fates_np_comp_scaling
use FatesConstantsMod , only : coupled_np_comp_scaling
use FatesConstantsMod , only : trivial_np_comp_scaling
use PRTGenericMod , only : num_elements
use PRTGenericMod , only : element_list
use PRTGenericMod , only : element_pos
use EDParamsMod , only : eca_plant_escalar
use PRTGenericMod , only : prt_carbon_allom_hyp
use PRTGenericMod , only : prt_cnp_flex_allom_hyp
use PRTGenericMod , only : carbon12_element
use PRTGenericMod , only : nitrogen_element
use PRTGenericMod , only : phosphorus_element
use PRTGenericMod , only : num_organ_types
use PRTGenericMod , only : leaf_organ, fnrt_organ, store_organ
use PRTGenericMod , only : sapw_organ, struct_organ, repro_organ
use PRTParametersMod , only : prt_params
use PRTInitParamsFatesMod , only : PRTCheckParams, PRTDerivedParams
use PRTAllometricCarbonMod , only : InitPRTGlobalAllometricCarbon
use PRTAllometricCNPMod , only : InitPRTGlobalAllometricCNP
use FatesRunningMeanMod , only : ema_24hr
use FatesRunningMeanMod , only : ema_sdlng_emerg_h2o, ema_sdlng_mort_par
use FatesRunningMeanMod , only : ema_sdlng_mdd, ema_sdlng2sap_par
use FatesRunningMeanMod , only : fixed_24hr
use FatesRunningMeanMod , only : ema_lpa
use FatesRunningMeanMod , only : ema_longterm
use FatesRunningMeanMod , only : ema_60day
use FatesRunningMeanMod , only : moving_ema_window
use FatesRunningMeanMod , only : fixed_window
use FatesHistoryInterfaceMod , only : fates_hist
use FatesHydraulicsMemMod , only : nshell
use FatesHydraulicsMemMod , only : nlevsoi_hyd_max
use FatesTwoStreamUtilsMod, only : TransferRadParams
! CIME Globals
use shr_log_mod , only : errMsg => shr_log_errMsg
use shr_infnan_mod , only : nan => shr_infnan_nan, assignment(=)
use shr_kind_mod , only : SHR_KIND_CL
! Just use everything from FatesInterfaceTypesMod, this is
! its sister code
use FatesInterfaceTypesMod
implicit none
private
type, public :: fates_interface_type
! This is the root of the ED/FATES hierarchy of instantaneous state variables
! ie the root of the linked lists. Each path list is currently associated with a
! grid-cell, this is intended to be migrated to columns
integer :: nsites
type(ed_site_type), pointer :: sites(:)
! These are boundary conditions that the FATES models are required to be filled.
! These values are filled by the driver or HLM. Once filled, these have an
! intent(in) status. Each site has a derived type structure, which may include
! a scalar for site level data, a patch vector, potentially cohort vectors (but
! not yet atm) and other dimensions such as soil-depth or pft. These vectors
! are initialized by maximums, and the allocations are static in time to avoid
! having to allocate/de-allocate memory
type(bc_in_type), allocatable :: bc_in(:)
! These are the boundary conditions that the FATES model returns to its HLM or
! driver. It has the same allocation strategy and similar vector types.
type(bc_out_type), allocatable :: bc_out(:)
! These are parameter constants that FATES may need to provide a host model
! We have other methods of reading in input parameters. Since these
! are parameter constants, we don't need them allocated over every site,one
! instance is fine.
type(bc_pconst_type) :: bc_pconst
end type fates_interface_type
character(len=*), parameter :: sourcefile = &
__FILE__
! Make public necessary subroutines and functions
public :: FatesInterfaceInit
public :: set_fates_ctrlparms
public :: SetFatesTime
public :: SetFatesGlobalElements1
public :: SetFatesGlobalElements2
public :: FatesReportParameters
public :: allocate_bcin
public :: allocate_bcout
public :: allocate_bcpconst
public :: set_bcpconst
public :: zero_bcs
public :: set_bcs
public :: UpdateFatesRMeansTStep
public :: InitTimeAveragingGlobals
private :: FatesReadParameters
public :: DetermineGridCellNeighbors
logical :: debug = .false. ! for debugging this module
contains
! ====================================================================================
subroutine FatesInterfaceInit(log_unit,global_verbose)
use FatesGlobals, only : FatesGlobalsInit
implicit none
integer, intent(in) :: log_unit
logical, intent(in) :: global_verbose
call FatesGlobalsInit(log_unit,global_verbose)
end subroutine FatesInterfaceInit
! ====================================================================================
! INTERF-TODO: THIS IS A PLACE-HOLDER ROUTINE, NOT CALLED YET...
subroutine fates_clean(this)
implicit none
! Input Arguments
class(fates_interface_type), intent(inout) :: this
! Incrementally walk through linked list and deallocate
! Deallocate the site list
! deallocate (this%sites)
return
end subroutine fates_clean
! ====================================================================================
subroutine allocate_bcpconst(bc_pconst,nlevdecomp)
type(bc_pconst_type), intent(inout) :: bc_pconst
integer , intent(in) :: nlevdecomp
allocate(bc_pconst%vmax_nh4(numpft))
allocate(bc_pconst%vmax_no3(numpft))
allocate(bc_pconst%vmax_p(numpft))
allocate(bc_pconst%eca_km_nh4(numpft))
allocate(bc_pconst%eca_km_no3(numpft))
allocate(bc_pconst%eca_km_p(numpft))
allocate(bc_pconst%eca_km_ptase(numpft))
allocate(bc_pconst%eca_vmax_ptase(numpft))
allocate(bc_pconst%eca_alpha_ptase(numpft))
allocate(bc_pconst%eca_lambda_ptase(numpft))
allocate(bc_pconst%j_uptake(nlevdecomp))
return
end subroutine allocate_bcpconst
! ====================================================================================
subroutine set_bcpconst(bc_pconst,nlevdecomp)
type(bc_pconst_type), intent(inout) :: bc_pconst
integer , intent(in) :: nlevdecomp
integer :: j
bc_pconst%vmax_nh4(1:numpft) = EDPftvarcon_inst%vmax_nh4(1:numpft)
bc_pconst%vmax_no3(1:numpft) = EDPftvarcon_inst%vmax_no3(1:numpft)
bc_pconst%vmax_p(1:numpft) = EDPftvarcon_inst%vmax_p(1:numpft)
bc_pconst%eca_km_nh4(1:numpft) = EDPftvarcon_inst%eca_km_nh4(1:numpft)
bc_pconst%eca_km_no3(1:numpft) = EDPftvarcon_inst%eca_km_no3(1:numpft)
bc_pconst%eca_km_p(1:numpft) = EDPftvarcon_inst%eca_km_p(1:numpft)
bc_pconst%eca_km_ptase(1:numpft) = EDPftvarcon_inst%eca_km_ptase(1:numpft)
bc_pconst%eca_vmax_ptase(1:numpft) = EDPftvarcon_inst%eca_vmax_ptase(1:numpft)
bc_pconst%eca_alpha_ptase(1:numpft) = EDPftvarcon_inst%eca_alpha_ptase(1:numpft)
bc_pconst%eca_lambda_ptase(1:numpft) = EDPftvarcon_inst%eca_lambda_ptase(1:numpft)
bc_pconst%eca_plant_escalar = eca_plant_escalar
return
end subroutine set_bcpconst
! ====================================================================================
subroutine zero_bcs(fates,s)
type(fates_interface_type), intent(inout) :: fates
integer, intent(in) :: s
! Input boundaries
fates%bc_in(s)%lightning24(:) = 0.0_r8
fates%bc_in(s)%pop_density(:) = 0.0_r8
fates%bc_in(s)%precip24_pa(:) = 0.0_r8
fates%bc_in(s)%relhumid24_pa(:) = 0.0_r8
fates%bc_in(s)%wind24_pa(:) = 0.0_r8
fates%bc_in(s)%solad_parb(:,:) = 0.0_r8
fates%bc_in(s)%solai_parb(:,:) = 0.0_r8
fates%bc_in(s)%smp_sl(:) = 0.0_r8
fates%bc_in(s)%eff_porosity_sl(:) = 0.0_r8
fates%bc_in(s)%watsat_sl(:) = 0.0_r8
fates%bc_in(s)%tempk_sl(:) = 0.0_r8
fates%bc_in(s)%h2o_liqvol_sl(:) = 0.0_r8
fates%bc_in(s)%filter_vegzen_pa(:) = .false.
fates%bc_in(s)%coszen_pa(:) = 0.0_r8
fates%bc_in(s)%fcansno_pa(:) = 0.0_r8
fates%bc_in(s)%albgr_dir_rb(:) = 0.0_r8
fates%bc_in(s)%albgr_dif_rb(:) = 0.0_r8
fates%bc_in(s)%max_rooting_depth_index_col = 0
fates%bc_in(s)%tot_het_resp = 0.0_r8
fates%bc_in(s)%tot_somc = 0.0_r8
fates%bc_in(s)%tot_litc = 0.0_r8
fates%bc_in(s)%snow_depth_si = 0.0_r8
fates%bc_in(s)%frac_sno_eff_si = 0.0_r8
fates%bc_in(s)%w_scalar_sisl(:) = 0.0_r8
fates%bc_in(s)%t_scalar_sisl(:) = 0.0_r8
if(do_fates_salinity)then
fates%bc_in(s)%salinity_sl(:) = 0.0_r8
endif
if (hlm_use_planthydro.eq.itrue) then
fates%bc_in(s)%qflx_transp_pa(:) = 0.0_r8
fates%bc_in(s)%swrad_net_pa(:) = 0.0_r8
fates%bc_in(s)%lwrad_net_pa(:) = 0.0_r8
fates%bc_in(s)%watsat_sisl(:) = 0.0_r8
fates%bc_in(s)%watres_sisl(:) = 0.0_r8
fates%bc_in(s)%sucsat_sisl(:) = 0.0_r8
fates%bc_in(s)%bsw_sisl(:) = 0.0_r8
fates%bc_in(s)%hksat_sisl(:) = 0.0_r8
end if
! Output boundaries
fates%bc_out(s)%active_suction_sl(:) = .false.
fates%bc_out(s)%fsun_pa(:) = 0.0_r8
fates%bc_out(s)%laisun_pa(:) = 0.0_r8
fates%bc_out(s)%laisha_pa(:) = 0.0_r8
fates%bc_out(s)%rootr_pasl(:,:) = 0.0_r8
fates%bc_out(s)%btran_pa(:) = 0.0_r8
! MIMIC litter quality, always initialize to unset
fates%bc_out(s)%litt_flux_ligc_per_n = fates_unset_r8
! Fates -> BGC fragmentation mass fluxes
select case(hlm_parteh_mode)
case(prt_carbon_allom_hyp)
fates%bc_out(s)%litt_flux_cel_c_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lig_c_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lab_c_si(:) = 0._r8
! Yes, zero out N flux arrays for c-only runs.
! This is because we want these on (and zero)
! with CLM.
fates%bc_out(s)%litt_flux_cel_n_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lig_n_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lab_n_si(:) = 0._r8
case(prt_cnp_flex_allom_hyp)
fates%bc_in(s)%plant_nh4_uptake_flux(:,:) = 0._r8
fates%bc_in(s)%plant_no3_uptake_flux(:,:) = 0._r8
fates%bc_in(s)%plant_p_uptake_flux(:,:) = 0._r8
fates%bc_out(s)%source_p(:) = 0._r8
fates%bc_out(s)%source_nh4(:) = 0._r8
fates%bc_out(s)%litt_flux_cel_c_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lig_c_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lab_c_si(:) = 0._r8
fates%bc_out(s)%litt_flux_cel_n_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lig_n_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lab_n_si(:) = 0._r8
fates%bc_out(s)%litt_flux_cel_p_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lig_p_si(:) = 0._r8
fates%bc_out(s)%litt_flux_lab_p_si(:) = 0._r8
case default
write(fates_log(), *) 'An unknown parteh hypothesis was passed'
write(fates_log(), *) 'while zeroing output boundary conditions'
write(fates_log(), *) 'hlm_parteh_mode: ',hlm_parteh_mode
call endrun(msg=errMsg(sourcefile, __LINE__))
end select
fates%bc_out(s)%rssun_pa(:) = 0.0_r8
fates%bc_out(s)%rssha_pa(:) = 0.0_r8
fates%bc_out(s)%albd_parb(:,:) = 0.0_r8
fates%bc_out(s)%albi_parb(:,:) = 0.0_r8
fates%bc_out(s)%fabd_parb(:,:) = 0.0_r8
fates%bc_out(s)%fabi_parb(:,:) = 0.0_r8
fates%bc_out(s)%ftdd_parb(:,:) = 0.0_r8
fates%bc_out(s)%ftid_parb(:,:) = 0.0_r8
fates%bc_out(s)%ftii_parb(:,:) = 0.0_r8
fates%bc_out(s)%elai_pa(:) = 0.0_r8
fates%bc_out(s)%esai_pa(:) = 0.0_r8
fates%bc_out(s)%tlai_pa(:) = 0.0_r8
fates%bc_out(s)%tsai_pa(:) = 0.0_r8
fates%bc_out(s)%htop_pa(:) = 0.0_r8
fates%bc_out(s)%hbot_pa(:) = 0.0_r8
fates%bc_out(s)%displa_pa(:) = 0.0_r8
fates%bc_out(s)%z0m_pa(:) = 0.0_r8
fates%bc_out(s)%dleaf_pa(:) = 0.0_r8
fates%bc_out(s)%nocomp_pft_label_pa(:) = 0
fates%bc_out(s)%canopy_fraction_pa(:) = 0.0_r8
fates%bc_out(s)%frac_veg_nosno_alb_pa(:) = 0.0_r8
if (hlm_use_planthydro.eq.itrue) then
fates%bc_out(s)%qflx_soil2root_sisl(:) = 0.0_r8
fates%bc_out(s)%qflx_ro_sisl(:) = 0.0_r8
end if
fates%bc_out(s)%plant_stored_h2o_si = 0.0_r8
! Land Use realated
fates%bc_out(s)%gpp_site = 0.0_r8
fates%bc_out(s)%ar_site = 0.0_r8
fates%bc_out(s)%hrv_deadstemc_to_prod10c = 0.0_r8
fates%bc_out(s)%hrv_deadstemc_to_prod100c = 0.0_r8
if (hlm_use_luh .eq. itrue) then
fates%bc_in(s)%hlm_luh_states(:) = 0.0_r8
fates%bc_in(s)%hlm_luh_transitions(:) = 0.0_r8
end if
return
end subroutine zero_bcs
! ===========================================================================
subroutine allocate_bcin(bc_in, nlevsoil_in, nlevdecomp_in, num_lu_harvest_cats, num_luh2_states, &
num_luh2_transitions, surfpft_lb,surfpft_ub)
! ---------------------------------------------------------------------------------
! Allocate and Initialze the FATES boundary condition vectors
! ---------------------------------------------------------------------------------
implicit none
type(bc_in_type), intent(inout) :: bc_in
integer,intent(in) :: nlevsoil_in
integer,intent(in) :: nlevdecomp_in
integer,intent(in) :: num_lu_harvest_cats
integer,intent(in) :: num_luh2_states
integer,intent(in) :: num_luh2_transitions
integer,intent(in) :: surfpft_lb,surfpft_ub ! dimension bounds of the array holding surface file pft data
! Allocate input boundaries
bc_in%nlevsoil = nlevsoil_in
if(nlevsoil_in > numlevsoil_max) then
write(fates_log(), *) 'The number of soil layers imposed by the host model'
write(fates_log(), *) 'is larger than what we have allocated in our static'
write(fates_log(), *) 'arrays. Please increase the size of numlevsoil_max'
write(fates_log(), *) 'found in EDTypesMod.F90'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
bc_in%nlevdecomp = nlevdecomp_in
if (hlm_use_vertsoilc == itrue) then
if(bc_in%nlevdecomp .ne. bc_in%nlevsoil) then
write(fates_log(), *) 'The host has signaled a vertically resolved'
write(fates_log(), *) 'soil decomposition model. Therefore, the '
write(fates_log(), *) 'total number of soil layers should equal the'
write(fates_log(), *) 'total number of decomposition layers.'
write(fates_log(), *) 'nlevdecomp: ',bc_in%nlevdecomp
write(fates_log(), *) 'nlevsoil: ',bc_in%nlevsoil
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
else
if(bc_in%nlevdecomp .ne. 1)then
write(fates_log(), *) 'The host has signaled a non-vertically resolved'
write(fates_log(), *) 'soil decomposition model. Therefore, the '
write(fates_log(), *) 'total number of decomposition layers should be 1.'
write(fates_log(), *) 'nlevdecomp: ',bc_in%nlevdecomp
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end if
! Plant Nutrient Aquisition variables
! If we are up-scaling to PFT, then we need to pass bach PFTxlayer
! if we don't, then there is ambiguity in the uptake. If we
! do not upscale to PFT, then we can simply send back the
! uptake for each cohort, and don't need to allocate by layer
! Allocating differently could save a lot of memory and time
if (hlm_parteh_mode .eq. prt_cnp_flex_allom_hyp) then
allocate(bc_in%plant_nh4_uptake_flux(max_comp_per_site,1))
allocate(bc_in%plant_no3_uptake_flux(max_comp_per_site,1))
allocate(bc_in%plant_p_uptake_flux(max_comp_per_site,1))
else
allocate(bc_in%plant_nh4_uptake_flux(1,1))
allocate(bc_in%plant_no3_uptake_flux(1,1))
allocate(bc_in%plant_p_uptake_flux(1,1))
end if
allocate(bc_in%zi_sisl(0:nlevsoil_in))
allocate(bc_in%dz_sisl(nlevsoil_in))
allocate(bc_in%z_sisl(nlevsoil_in))
allocate(bc_in%decomp_id(nlevsoil_in))
allocate(bc_in%dz_decomp_sisl(nlevdecomp_in))
allocate(bc_in%w_scalar_sisl(nlevsoil_in))
allocate(bc_in%t_scalar_sisl(nlevsoil_in))
! Lightning (or successful ignitions) and population density
! Fire related variables
allocate(bc_in%lightning24(maxpatch_total))
allocate(bc_in%pop_density(maxpatch_total))
allocate(bc_in%wind24_pa(maxpatch_total))
allocate(bc_in%relhumid24_pa(maxpatch_total))
allocate(bc_in%precip24_pa(maxpatch_total))
! Radiation
allocate(bc_in%solad_parb(maxpatch_total,num_swb))
allocate(bc_in%solai_parb(maxpatch_total,num_swb))
! Hydrology
allocate(bc_in%smp_sl(nlevsoil_in))
allocate(bc_in%eff_porosity_sl(nlevsoil_in))
allocate(bc_in%watsat_sl(nlevsoil_in))
allocate(bc_in%tempk_sl(nlevsoil_in))
allocate(bc_in%h2o_liqvol_sl(nlevsoil_in))
!BGC
if(do_fates_salinity) then
allocate(bc_in%salinity_sl(nlevsoil_in))
endif
! Photosynthesis
allocate(bc_in%filter_photo_pa(maxpatch_total))
allocate(bc_in%dayl_factor_pa(maxpatch_total))
allocate(bc_in%esat_tv_pa(maxpatch_total))
allocate(bc_in%eair_pa(maxpatch_total))
allocate(bc_in%oair_pa(maxpatch_total))
allocate(bc_in%cair_pa(maxpatch_total))
allocate(bc_in%rb_pa(maxpatch_total))
allocate(bc_in%t_veg_pa(maxpatch_total))
allocate(bc_in%tgcm_pa(maxpatch_total))
allocate(bc_in%t_soisno_sl(nlevsoil_in))
! Canopy Radiation
allocate(bc_in%filter_vegzen_pa(maxpatch_total))
allocate(bc_in%coszen_pa(maxpatch_total))
allocate(bc_in%fcansno_pa(maxpatch_total))
allocate(bc_in%albgr_dir_rb(num_swb))
allocate(bc_in%albgr_dif_rb(num_swb))
! Plant-Hydro BC's
if (hlm_use_planthydro.eq.itrue) then
allocate(bc_in%qflx_transp_pa(maxpatch_total))
allocate(bc_in%swrad_net_pa(maxpatch_total))
allocate(bc_in%lwrad_net_pa(maxpatch_total))
allocate(bc_in%watsat_sisl(nlevsoil_in))
allocate(bc_in%watres_sisl(nlevsoil_in))
allocate(bc_in%sucsat_sisl(nlevsoil_in))
allocate(bc_in%bsw_sisl(nlevsoil_in))
allocate(bc_in%hksat_sisl(nlevsoil_in))
allocate(bc_in%h2o_liq_sisl(nlevsoil_in)); bc_in%h2o_liq_sisl = nan
end if
! Land use
! harvest flag denote data from hlm,
! while the logging flag signifies only that logging is occurring (which could just be FATES logging)
if (hlm_use_lu_harvest .gt. 0) then
allocate(bc_in%hlm_harvest_rates(num_lu_harvest_cats))
allocate(bc_in%hlm_harvest_catnames(num_lu_harvest_cats))
else ! LoggingMortality_frac needs these passed to it regardless of harvest
allocate(bc_in%hlm_harvest_rates(0))
allocate(bc_in%hlm_harvest_catnames(0))
end if
if ( hlm_use_fixed_biogeog .eq. itrue) then
if (hlm_use_luh .eq. itrue ) then
allocate(bc_in%pft_areafrac_lu(size( EDPftvarcon_inst%hlm_pft_map,2),n_landuse_cats-n_crop_lu_types))
else
allocate(bc_in%pft_areafrac(surfpft_lb:surfpft_ub))
endif
endif
! LUH2 state and transition data
if (hlm_use_luh .eq. itrue) then
allocate(bc_in%hlm_luh_states(num_luh2_states))
allocate(bc_in%hlm_luh_state_names(num_luh2_states))
allocate(bc_in%hlm_luh_transitions(num_luh2_transitions))
allocate(bc_in%hlm_luh_transition_names(num_luh2_transitions))
end if
! Variables for SP mode.
if(hlm_use_sp.eq.itrue) then
allocate(bc_in%hlm_sp_tlai(surfpft_lb:surfpft_ub))
allocate(bc_in%hlm_sp_tsai(surfpft_lb:surfpft_ub))
allocate(bc_in%hlm_sp_htop(surfpft_lb:surfpft_ub))
end if
return
end subroutine allocate_bcin
! ====================================================================================
subroutine allocate_bcout(bc_out, nlevsoil_in, nlevdecomp_in)
! ---------------------------------------------------------------------------------
! Allocate and Initialze the FATES boundary condition vectors
! ---------------------------------------------------------------------------------
implicit none
type(bc_out_type), intent(inout) :: bc_out
integer,intent(in) :: nlevsoil_in
integer,intent(in) :: nlevdecomp_in
! Radiation
allocate(bc_out%fsun_pa(maxpatch_total))
allocate(bc_out%laisun_pa(maxpatch_total))
allocate(bc_out%laisha_pa(maxpatch_total))
! Hydrology
allocate(bc_out%active_suction_sl(nlevsoil_in))
allocate(bc_out%rootr_pasl(maxpatch_total,nlevsoil_in))
allocate(bc_out%btran_pa(maxpatch_total))
! Photosynthesis
allocate(bc_out%rssun_pa(maxpatch_total))
allocate(bc_out%rssha_pa(maxpatch_total))
! Canopy Radiation
allocate(bc_out%albd_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%albi_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%fabd_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%fabi_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%ftdd_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%ftid_parb(fates_maxPatchesPerSite,num_swb))
allocate(bc_out%ftii_parb(fates_maxPatchesPerSite,num_swb))
! We allocate the boundary conditions to the BGC
! model, regardless of what scheme we use. The BGC
! model in ELM allocates all species C,N,P even if they
! are not turned on. Also, it is feasible that the
! one would want to allow soil BGC nutrient dynamics
! to proceed even if we are not passing source fluxes
! or uptake from FATES.
! When FATES does not have nutrients enabled, these
! arrays are indexed by 1.
!if(trim(hlm_nu_com).eq.'RD') then
! allocate(bc_out%n_demand(max_comp_per_site))
! allocate(bc_out%p_demand(max_comp_per_site))
!end if
! Used in both
allocate(bc_out%veg_rootc(max_comp_per_site,nlevdecomp_in))
allocate(bc_out%ft_index(max_comp_per_site))
if(trim(hlm_nu_com).eq.'ECA') then
allocate(bc_out%decompmicc(nlevdecomp_in))
allocate(bc_out%cn_scalar(max_comp_per_site))
allocate(bc_out%cp_scalar(max_comp_per_site))
end if
! Include the bare-ground patch for these patch-level boundary conditions
! (it will always be zero for all of these)
!if(hlm_use_ch4.eq.itrue) then
allocate(bc_out%annavg_agnpp_pa(0:maxpatch_total));bc_out%annavg_agnpp_pa(:)=nan
allocate(bc_out%annavg_bgnpp_pa(0:maxpatch_total));bc_out%annavg_bgnpp_pa(:)=nan
allocate(bc_out%annsum_npp_pa(0:maxpatch_total));bc_out%annsum_npp_pa(:)=nan
allocate(bc_out%frootc_pa(0:maxpatch_total));bc_out%frootc_pa(:)=nan
allocate(bc_out%root_resp(nlevsoil_in));bc_out%root_resp(:)=nan
allocate(bc_out%woody_frac_aere_pa(0:maxpatch_total));bc_out%woody_frac_aere_pa(:)=nan
allocate(bc_out%rootfr_pa(0:maxpatch_total,nlevsoil_in))
bc_out%rootfr_pa(:,:)=nan
! Give the bare-ground root fractions a nominal fraction of unity over depth
bc_out%rootfr_pa(0,1:nlevsoil_in)=1._r8/real(nlevsoil_in,r8)
!end if
bc_out%ema_npp = nan
! Fates -> BGC fragmentation mass fluxes
select case(hlm_parteh_mode)
case(prt_carbon_allom_hyp)
allocate(bc_out%litt_flux_cel_c_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lig_c_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lab_c_si(nlevdecomp_in))
! Yes, allocate N flux arrays for c-only runs.
! This is because we want these on (and zero)
! with CLM.
allocate(bc_out%litt_flux_cel_n_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lig_n_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lab_n_si(nlevdecomp_in))
case(prt_cnp_flex_allom_hyp)
allocate(bc_out%litt_flux_cel_c_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lig_c_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lab_c_si(nlevdecomp_in))
allocate(bc_out%litt_flux_cel_n_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lig_n_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lab_n_si(nlevdecomp_in))
allocate(bc_out%litt_flux_cel_p_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lig_p_si(nlevdecomp_in))
allocate(bc_out%litt_flux_lab_p_si(nlevdecomp_in))
allocate(bc_out%source_nh4(nlevdecomp_in))
allocate(bc_out%source_p(nlevdecomp_in))
case default
write(fates_log(), *) 'An unknown parteh hypothesis was passed'
write(fates_log(), *) 'to the site level output boundary conditions'
write(fates_log(), *) 'hlm_parteh_mode: ',hlm_parteh_mode
call endrun(msg=errMsg(sourcefile, __LINE__))
end select
! Canopy Structure
allocate(bc_out%elai_pa(maxpatch_total))
allocate(bc_out%esai_pa(maxpatch_total))
allocate(bc_out%tlai_pa(maxpatch_total))
allocate(bc_out%tsai_pa(maxpatch_total))
allocate(bc_out%htop_pa(maxpatch_total))
allocate(bc_out%hbot_pa(maxpatch_total))
allocate(bc_out%dleaf_pa(maxpatch_total))
allocate(bc_out%displa_pa(maxpatch_total))
allocate(bc_out%z0m_pa(maxpatch_total))
allocate(bc_out%canopy_fraction_pa(maxpatch_total))
allocate(bc_out%frac_veg_nosno_alb_pa(maxpatch_total))
allocate(bc_out%nocomp_pft_label_pa(maxpatch_total))
! Plant-Hydro BC's
if (hlm_use_planthydro.eq.itrue) then
allocate(bc_out%qflx_soil2root_sisl(nlevsoil_in))
allocate(bc_out%qflx_ro_sisl(nlevsoil_in))
end if
return
end subroutine allocate_bcout
! ====================================================================================
subroutine set_bcs(bc_in)
! --------------------------------------------------------------------------------
!
! This subroutine is called directly from the HLM to set boundary condition not yet
! functional from hlm. This allows flexibility for model testing.
!
! This subroutine MUST BE CALLED AFTER the FATES PFT parameter file has been read in,
! and the EDPftvarcon_inst structure has been made.
! This subroutine must ALSO BE CALLED BEFORE the history file dimensions
! are set.
!
! --------------------------------------------------------------------------------
implicit none
type(bc_in_type), intent(inout) :: bc_in
! Input boundaries
! Warning: these "z" type variables
! are written only once at the beginning
! so THIS ROUTINE SHOULD NOT BE CALLED AFTER
! INITIALIZATION
if(do_fates_salinity)then
bc_in%salinity_sl(:) = bgc_soil_salinity
endif
end subroutine set_bcs
! ===================================================================================
subroutine SetFatesGlobalElements1(use_fates,surf_numpft,surf_numcft,param_reader)
! --------------------------------------------------------------------------------
!
! This is the first FATES routine that is called.
!
! spmode,biogeog and nocomp mode flags have been passed prior to this call
! --------------------------------------------------------------------------------
implicit none
logical, intent(in) :: use_fates ! Is fates turned on?
integer, intent(in) :: surf_numpft ! Number of PFTs in surface dataset
integer, intent(in) :: surf_numcft ! Number of CFTs in surface dataset
class(fates_param_reader_type), intent(in) :: param_reader ! HLM-provided param file reader
integer :: fates_numpft ! Number of PFTs tracked in FATES
if (use_fates) then
! Self explanatory, read the fates parameter file
call FatesReadParameters(param_reader)
fates_numpft = size(prt_params%wood_density,dim=1)
if(hlm_use_sp==itrue)then
! For an SP run we also just use the primary patches
! to hold all PFTs. So create the same number of
! patches as the number of PFTs
maxpatches_by_landuse(primaryland) = fates_numpft
maxpatches_by_landuse(secondaryland:n_landuse_cats) = 0
maxpatch_total = fates_numpft
! If this is an SP run, we actually need enough patches on the
! CLM/ELM side of the code to hold the LAI data. This
! number may be larger than what fates requires. Of course
! we may have multiple PFTs in the surface datafile mapping
! to FATES. The surf_numpft includes the bare ground.
! maxpatch_total does not include the bare ground (so add 1)
fates_maxPatchesPerSite = max(surf_numpft+surf_numcft,maxpatch_total+1)
else
! If we are using fixed biogeography or no-comp then we
! can also apply those constraints to maxpatch_primaryland and secondary
! and that value will match fates_maxPatchesPerSite
if(hlm_use_nocomp==itrue) then
maxpatches_by_landuse(primaryland) = max(maxpatches_by_landuse(primaryland),fates_numpft)
maxpatch_total = sum(maxpatches_by_landuse(:))
!if(maxpatch_primary<fates_numpft)then
! write(fates_log(),*) 'warning: lower number of patches than pfts'
! write(fates_log(),*) 'this may become a problem in nocomp mode'
!end if
end if
! maxpatch_total does not include the bare ground (so add 1)
fates_maxPatchesPerSite = maxpatch_total+1
end if
end if
end subroutine SetFatesGlobalElements1
! ====================================================================================
subroutine SetFatesGlobalElements2(use_fates)
! --------------------------------------------------------------------------------
!
! This is the second FATES routine that is called.
!
! --------------------------------------------------------------------------------
use FatesConstantsMod, only : fates_check_param_set
logical,intent(in) :: use_fates ! Is fates turned on?
integer :: i
if (use_fates) then
if(lbound(prt_params%wood_density(:),dim=1) .eq. 0 ) then
numpft = size(prt_params%wood_density,dim=1)-1
elseif(lbound(prt_params%wood_density(:),dim=1) .eq. 1 ) then
numpft = size(prt_params%wood_density,dim=1)
else
write(fates_log(), *) 'While assessing the number of FATES PFTs,'
write(fates_log(), *) 'it was found that the lower bound was neither 0 or 1?'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
if(numpft>maxpft) then
write(fates_log(), *) 'The number of PFTs dictated by the FATES parameter file'
write(fates_log(), *) 'is larger than the maximum allowed. Increase the FATES parameter constant'
write(fates_log(), *) 'FatesInterfaceMod.F90:maxpft accordingly'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
! Identify the number of leaf age-classes
if( (lbound(prt_params%leaf_long(:,:),dim=2) .eq. 0) .or. &
(ubound(prt_params%leaf_long(:,:),dim=2) .eq. 0) ) then
write(fates_log(), *) 'While assessing the number of FATES leaf age classes,'
write(fates_log(), *) 'The second dimension of leaf_long was 0?'
call endrun(msg=errMsg(sourcefile, __LINE__))
else
nleafage = size(prt_params%leaf_long,dim=2)
end if
nlevsclass = size(ED_val_history_sizeclass_bin_edges,dim=1)
! These values are used to define the restart file allocations and general structure
! of memory for the cohort arrays
if(hlm_use_sp.eq.itrue) then
fates_maxElementsPerPatch = num_swb
else
fates_maxElementsPerPatch = max(num_swb,max_cohort_per_patch, ndcmpy*hlm_maxlevsoil ,ncwd*hlm_maxlevsoil)
end if
fates_maxElementsPerSite = max(fates_maxPatchesPerSite * fates_maxElementsPerPatch, &
numWatermem*numpft, num_vegtemp_mem, num_elements, nlevsclass*numpft*n_term_mort_types)
if(hlm_use_planthydro==itrue)then
fates_maxElementsPerSite = max(fates_maxElementsPerSite, nshell*nlevsoi_hyd_max )
end if
! Set the maximum number of nutrient aquisition competitors per site
! This is used to set array sizes for the boundary conditions.
! Note: since BGC code may be active even when no nutrients
! present, we still need to allocate things when no nutrients
if (any(abs(EDPftvarcon_inst%prescribed_nuptake(:)) > nearzero )) then
n_uptake_mode = prescribed_n_uptake
else
n_uptake_mode = coupled_n_uptake
end if
if (any(abs(EDPftvarcon_inst%prescribed_puptake(:)) > nearzero )) then
p_uptake_mode = prescribed_p_uptake
else
p_uptake_mode = coupled_p_uptake
end if
if (hlm_parteh_mode .eq. prt_cnp_flex_allom_hyp ) then
if((p_uptake_mode==coupled_p_uptake) .or. (n_uptake_mode==coupled_n_uptake))then
max_comp_per_site = fates_maxElementsPerSite
fates_np_comp_scaling = coupled_np_comp_scaling
else
max_comp_per_site = 1
fates_np_comp_scaling = trivial_np_comp_scaling
end if
else
max_comp_per_site = 1
fates_np_comp_scaling = trivial_np_comp_scaling
end if
! calculate the bin edges for radiative transfer calculations
! VAI bin widths array
do i = 1,nlevleaf
dinc_vai(i) = ED_val_vai_top_bin_width * ED_val_vai_width_increase_factor ** (i-1)
end do
! lower edges of VAI bins
do i = 1,nlevleaf
dlower_vai(i) = sum(dinc_vai(1:i))
end do
! Identify number of size and age class bins for history output
! assume these arrays are 1-indexed
nlevage = size(ED_val_history_ageclass_bin_edges,dim=1)
nlevheight = size(ED_val_history_height_bin_edges,dim=1)
nlevcoage = size(ED_val_history_coageclass_bin_edges,dim=1)
nlevdamage = size(ED_val_history_damage_bin_edges, dim=1)
! do some checks on the size, age, and height bin arrays to make sure they make sense:
! make sure that all start at zero, and that both are monotonically increasing
if ( ED_val_history_sizeclass_bin_edges(1) .ne. 0._r8 ) then
write(fates_log(), *) 'size class bins specified in parameter file must start at zero'
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
if ( ED_val_history_ageclass_bin_edges(1) .ne. 0._r8 ) then
write(fates_log(), *) 'age class bins specified in parameter file must start at zero'
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
if ( ED_val_history_height_bin_edges(1) .ne. 0._r8 ) then
write(fates_log(), *) 'height class bins specified in parameter file must start at zero'
call endrun(msg=errMsg(sourcefile, __LINE__))
endif
do i = 2,nlevsclass
if ( (ED_val_history_sizeclass_bin_edges(i) - ED_val_history_sizeclass_bin_edges(i-1)) .le. 0._r8) then
write(fates_log(), *) 'size class bins specified in parameter file must be monotonically increasing'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
do i = 2,nlevage
if ( (ED_val_history_ageclass_bin_edges(i) - ED_val_history_ageclass_bin_edges(i-1)) .le. 0._r8) then
write(fates_log(), *) 'age class bins specified in parameter file must be monotonically increasing'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
do i = 2,nlevheight
if ( (ED_val_history_height_bin_edges(i) - ED_val_history_height_bin_edges(i-1)) .le. 0._r8) then
write(fates_log(), *) 'height class bins specified in parameter file must be monotonically increasing'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
do i = 2,nlevcoage
if ( (ED_val_history_coageclass_bin_edges(i) - ED_val_history_coageclass_bin_edges(i-1)) .le. 0._r8) then
write(fates_log(), *) 'cohort age class bins specified in parameter file must be monotonically increasing'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
! Set the fates dispersal kernel mode if there are any seed dispersal parameters set.
! The validation of the parameter values is check in FatesCheckParams prior to this check.
! This is currently hard coded, but could be added as a fates parameter file option,
! particularly one that is pft dependent.
if(any(EDPftvarcon_inst%seed_dispersal_pdf_scale .lt. fates_check_param_set)) then
fates_dispersal_kernel_mode = fates_dispersal_kernel_exponential
! fates_dispersal_kernel_mode = fates_dispersal_kernel_exppower
! fates_dispersal_kernel_mode = fates_dispersal_kernel_logsech
end if
! Initialize Hydro globals
! (like water retention functions)
! this needs to know the number of PFTs, which is
! determined in that call
call InitHydroGlobals()