-
Notifications
You must be signed in to change notification settings - Fork 88
/
FatesInterfaceMod.F90
1721 lines (1417 loc) · 68.8 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 EDTypesMod , only : maxPatchesPerSite
use EDTypesMod , only : maxCohortsPerPatch
use EDTypesMod , only : maxSWb
use EDTypesMod , only : ivis
use EDTypesMod , only : inir
use EDTypesMod , only : nclmax
use EDTypesMod , only : nlevleaf
use EDTypesMod , only : maxpft
use EDTypesMod , only : do_fates_salinity
use EDTypesMod , only : numWaterMem
use EDTypesMod , only : numlevsoil_max
use FatesConstantsMod , only : r8 => fates_r8
use FatesConstantsMod , only : itrue,ifalse
use FatesConstantsMod , only : nearzero
use FatesGlobals , only : fates_global_verbose
use FatesGlobals , only : fates_log
use FatesGlobals , only : endrun => fates_endrun
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 : 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 CLMFatesParamInterfaceMod , only : FatesReadParameters
use EDTypesMod , only : p_uptake_mode
use EDTypesMod , only : n_uptake_mode
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 : cohort_np_comp_scaling
use FatesConstantsMod , only : pft_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
use PRTAllometricCarbonMod , only : InitPRTGlobalAllometricCarbon
use PRTAllometricCNPMod , only : InitPRTGlobalAllometricCNP
! CIME Globals
use shr_log_mod , only : errMsg => shr_log_errMsg
use shr_infnan_mod , only : nan => shr_infnan_nan, assignment(=)
! Just use everything from FatesInterfaceTypesMod, this is
! its sister code
use FatesInterfaceTypesMod
implicit none
private
character(len=*), parameter :: sourcefile = &
__FILE__
! Make public necessary subroutines and functions
public :: FatesInterfaceInit
public :: set_fates_ctrlparms
public :: SetFatesTime
public :: SetFatesGlobalElements
public :: FatesReportParameters
public :: allocate_bcin
public :: allocate_bcout
public :: allocate_bcpconst
public :: set_bcpconst
public :: zero_bcs
public :: set_bcs
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%eca_km_nh4(numpft))
allocate(bc_pconst%eca_vmax_nh4(numpft))
allocate(bc_pconst%eca_km_no3(numpft))
allocate(bc_pconst%eca_vmax_no3(numpft))
allocate(bc_pconst%eca_km_p(numpft))
allocate(bc_pconst%eca_vmax_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%eca_km_nh4(1:numpft) = EDPftvarcon_inst%eca_km_nh4(1:numpft)
bc_pconst%eca_vmax_nh4(1:numpft) = EDPftvarcon_inst%eca_vmax_nh4(1:numpft)
bc_pconst%eca_km_no3(1:numpft) = EDPftvarcon_inst%eca_km_no3(1:numpft)
bc_pconst%eca_vmax_no3(1:numpft) = EDPftvarcon_inst%eca_vmax_no3(1:numpft)
bc_pconst%eca_km_p(1:numpft) = EDPftvarcon_inst%eca_km_p(1:numpft)
bc_pconst%eca_vmax_p(1:numpft) = EDPftvarcon_inst%eca_vmax_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
if(fates_np_comp_scaling.eq.cohort_np_comp_scaling) then
bc_pconst%j_uptake(1:nlevdecomp) = 1
else
do j=1,nlevdecomp
bc_pconst%j_uptake(j) = j
end do
end if
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)%t_veg24_pa(:) = 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)%lightning24(:) = 0.0_r8
fates%bc_in(s)%pop_density(:) = 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)%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
! 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
case(prt_cnp_flex_allom_hyp)
fates%bc_in(s)%plant_n_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)%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
return
end subroutine zero_bcs
! ===========================================================================
subroutine allocate_bcin(bc_in, nlevsoil_in, nlevdecomp_in, num_lu_harvest_cats)
! ---------------------------------------------------------------------------------
! 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
! 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
if( (nlevsoil_in*ndcmpy) > fates_maxElementsPerPatch .or. &
(nlevsoil_in*ncwd) > fates_maxElementsPerPatch) then
write(fates_log(), *) 'The restart files require that space is allocated'
write(fates_log(), *) 'to accomodate the multi-dimensional patch arrays'
write(fates_log(), *) 'that are nlevsoil*numpft and nlevsoil*ncwd'
write(fates_log(), *) 'fates_maxElementsPerPatch = ',fates_maxElementsPerPatch
write(fates_log(), *) 'nlevsoil = ',nlevsoil_in
write(fates_log(), *) 'dcmpy = ',ndcmpy
write(fates_log(), *) 'ncwd = ',ncwd
write(fates_log(), *) 'numpft*nlevsoil = ',nlevsoil_in*numpft
write(fates_log(), *) 'ncwd*nlevsoil = ',ncwd * nlevsoil_in
write(fates_log(), *) 'To increase max_elements, change numlevsoil_max'
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
if(fates_np_comp_scaling.eq.cohort_np_comp_scaling) then
allocate(bc_in%plant_n_uptake_flux(max_comp_per_site,1))
allocate(bc_in%plant_p_uptake_flux(max_comp_per_site,1))
else
allocate(bc_in%plant_n_uptake_flux(max_comp_per_site,bc_in%nlevdecomp))
allocate(bc_in%plant_p_uptake_flux(max_comp_per_site,bc_in%nlevdecomp))
end if
else
allocate(bc_in%plant_n_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
allocate(bc_in%lightning24(maxPatchesPerSite))
allocate(bc_in%pop_density(maxPatchesPerSite))
! Vegetation Dynamics
allocate(bc_in%t_veg24_pa(maxPatchesPerSite))
allocate(bc_in%wind24_pa(maxPatchesPerSite))
allocate(bc_in%relhumid24_pa(maxPatchesPerSite))
allocate(bc_in%precip24_pa(maxPatchesPerSite))
! Radiation
allocate(bc_in%solad_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_in%solai_parb(maxPatchesPerSite,hlm_numSWb))
! 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(maxPatchesPerSite))
allocate(bc_in%dayl_factor_pa(maxPatchesPerSite))
allocate(bc_in%esat_tv_pa(maxPatchesPerSite))
allocate(bc_in%eair_pa(maxPatchesPerSite))
allocate(bc_in%oair_pa(maxPatchesPerSite))
allocate(bc_in%cair_pa(maxPatchesPerSite))
allocate(bc_in%rb_pa(maxPatchesPerSite))
allocate(bc_in%t_veg_pa(maxPatchesPerSite))
allocate(bc_in%tgcm_pa(maxPatchesPerSite))
allocate(bc_in%t_soisno_sl(nlevsoil_in))
! Canopy Radiation
allocate(bc_in%filter_vegzen_pa(maxPatchesPerSite))
allocate(bc_in%coszen_pa(maxPatchesPerSite))
allocate(bc_in%albgr_dir_rb(hlm_numSWb))
allocate(bc_in%albgr_dif_rb(hlm_numSWb))
! Plant-Hydro BC's
if (hlm_use_planthydro.eq.itrue) then
allocate(bc_in%qflx_transp_pa(maxPatchesPerSite))
allocate(bc_in%swrad_net_pa(maxPatchesPerSite))
allocate(bc_in%lwrad_net_pa(maxPatchesPerSite))
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
allocate(bc_in%pft_areafrac(maxpft))
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(maxPatchesPerSite))
allocate(bc_out%laisun_pa(maxPatchesPerSite))
allocate(bc_out%laisha_pa(maxPatchesPerSite))
! Hydrology
allocate(bc_out%active_suction_sl(nlevsoil_in))
allocate(bc_out%rootr_pasl(maxPatchesPerSite,nlevsoil_in))
allocate(bc_out%btran_pa(maxPatchesPerSite))
! Photosynthesis
allocate(bc_out%rssun_pa(maxPatchesPerSite))
allocate(bc_out%rssha_pa(maxPatchesPerSite))
! Canopy Radiation
allocate(bc_out%albd_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%albi_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%fabd_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%fabi_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%ftdd_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%ftid_parb(maxPatchesPerSite,hlm_numSWb))
allocate(bc_out%ftii_parb(maxPatchesPerSite,hlm_numSWb))
! 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
if(trim(hlm_nu_com).eq.'ECA') then
allocate(bc_out%veg_rootc(max_comp_per_site,nlevdecomp_in))
allocate(bc_out%decompmicc(nlevdecomp_in))
allocate(bc_out%ft_index(max_comp_per_site))
allocate(bc_out%cn_scalar(max_comp_per_site))
allocate(bc_out%cp_scalar(max_comp_per_site))
end if
! 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))
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(maxPatchesPerSite))
allocate(bc_out%esai_pa(maxPatchesPerSite))
allocate(bc_out%tlai_pa(maxPatchesPerSite))
allocate(bc_out%tsai_pa(maxPatchesPerSite))
allocate(bc_out%htop_pa(maxPatchesPerSite))
allocate(bc_out%hbot_pa(maxPatchesPerSite))
allocate(bc_out%dleaf_pa(maxPatchesPerSite))
allocate(bc_out%displa_pa(maxPatchesPerSite))
allocate(bc_out%z0m_pa(maxPatchesPerSite))
allocate(bc_out%canopy_fraction_pa(maxPatchesPerSite))
allocate(bc_out%frac_veg_nosno_alb_pa(maxPatchesPerSite))
! 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 SetFatesGlobalElements(use_fates)
! --------------------------------------------------------------------------------
!
! This is the first FATES routine that is called.
!
! 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 BE CALLED AFTER NL VARIABLES ARE READ (ie hlm_parteh_mode,etc)
! This subroutine must ALSO BE CALLED BEFORE the history file dimensions
! are set.
!
! This routine requires no information from the HLM. This routine is responsible
! for generating the globals that are required by the HLM that are entirely
! FATES derived.
!
! --------------------------------------------------------------------------------
implicit none
logical,intent(in) :: use_fates ! Is fates turned on?
integer :: i
if (use_fates) then
! first read the non-PFT parameters
call FatesReadParameters()
! Identify the number of PFTs by evaluating a pft array
! Using wood density as that is not expected to be deprecated any time soon
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
! These values are used to define the restart file allocations and general structure
! of memory for the cohort arrays
if ( hlm_use_cohort_age_tracking .eq. itrue) then
maxCohortsPerPatch = 300
else
maxCohortsPerPatch = 100
end if
! These values are used to define the restart file allocations and general structure
! of memory for the cohort arrays
fates_maxElementsPerPatch = max(maxCohortsPerPatch, ndcmpy*numlevsoil_max ,ncwd*numlevsoil_max)
if (maxPatchesPerSite * fates_maxElementsPerPatch < numWaterMem) then
write(fates_log(), *) 'By using such a tiny number of maximum patches and maximum cohorts'
write(fates_log(), *) ' this could create problems for indexing in restart files'
write(fates_log(), *) ' The multiple of the two has to be greater than numWaterMem'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
fates_maxElementsPerSite = maxPatchesPerSite * fates_maxElementsPerPatch
! 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 (hlm_parteh_mode .eq. prt_cnp_flex_allom_hyp ) then
if(fates_np_comp_scaling.eq.cohort_np_comp_scaling) then
max_comp_per_site = fates_maxElementsPerSite
elseif(fates_np_comp_scaling.eq.pft_np_comp_scaling) then
max_comp_per_site = numpft
else
write(fates_log(), *) 'An unknown nutrient competitor scaling method was chosen?'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
else
max_comp_per_site = 1
end if
! Identify number of size and age class bins for history output
! assume these arrays are 1-indexed
nlevsclass = size(ED_val_history_sizeclass_bin_edges,dim=1)
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)
! 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
! Initialize Hydro globals
! (like water retention functions)
! this needs to know the number of PFTs, which is
! determined in that call
call InitHydroGlobals()
! Initialize the Plant Allocation and Reactive Transport
! global functions and mapping tables
! Also associate the elements defined in PARTEH with a list in FATES
! "element_list" is useful because it allows the fates side of the code
! to loop through elements, and call the correct PARTEH interfaces
! automatically.
call InitPARTEHGlobals()
! Set Various Mapping Arrays used in history output as well
! These will not be used if use_ed or use_fates is false
call fates_history_maps()
else
! If we are not using FATES, the cohort dimension is still
! going to be initialized, lets set it to the smallest value
! possible so that the dimensioning info takes up little space
fates_maxElementsPerPatch = 1
fates_maxElementsPerSite = 1
end if
end subroutine SetFatesGlobalElements
! ======================================================================
subroutine InitPARTEHGlobals()
! Initialize the Plant Allocation and Reactive Transport
! global functions and mapping tables
! Also associate the elements defined in PARTEH with a list in FATES
! "element_list" is useful because it allows the fates side of the code
! to loop through elements, and call the correct PARTEH interfaces
! automatically.
select case(hlm_parteh_mode)
case(prt_carbon_allom_hyp)
num_elements = 1
allocate(element_list(num_elements))
element_list(1) = carbon12_element
element_pos(:) = 0
element_pos(carbon12_element) = 1
call InitPRTGlobalAllometricCarbon()
case(prt_cnp_flex_allom_hyp)
num_elements = 3
allocate(element_list(num_elements))
element_list(1) = carbon12_element
element_list(2) = nitrogen_element
element_list(3) = phosphorus_element
element_pos(:) = 0
element_pos(carbon12_element) = 1
element_pos(nitrogen_element) = 2
element_pos(phosphorus_element) = 3
call InitPRTGlobalAllometricCNP()
case DEFAULT
write(fates_log(),*) 'You specified an unknown PRT module'
write(fates_log(),*) 'Check your setting for fates_parteh_mode'
write(fates_log(),*) 'in the CLM namelist. The only valid value now is 1'
write(fates_log(),*) 'Aborting'
call endrun(msg=errMsg(sourcefile, __LINE__))
end select
end subroutine InitPARTEHGlobals
!==============================================================================================
subroutine fates_history_maps
use EDTypesMod, only : NFSC
use EDTypesMod, only : nclmax
use EDTypesMod, only : nlevleaf
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
! ------------------------------------------------------------------------------------------
! This subroutine allocates and populates the variables
! that define the mapping of variables in history files in multiplexed dimensions like
! the "scpf" format
! back to
! their respective single component dimensions, like size-class "sc" and pft "pf"
! ------------------------------------------------------------------------------------------
integer :: i
integer :: isc
integer :: ipft
integer :: icwd
integer :: ifuel
integer :: ican
integer :: ileaf
integer :: iage
integer :: iheight
integer :: icoage
integer :: iel
allocate( fates_hdim_levsclass(1:nlevsclass ))
allocate( fates_hdim_pfmap_levscpf(1:nlevsclass*numpft))
allocate( fates_hdim_scmap_levscpf(1:nlevsclass*numpft))
allocate( fates_hdim_levpft(1:numpft ))
allocate( fates_hdim_levfuel(1:NFSC ))
allocate( fates_hdim_levcwdsc(1:NCWD ))
allocate( fates_hdim_levage(1:nlevage ))
allocate( fates_hdim_levheight(1:nlevheight ))
allocate( fates_hdim_levcoage(1:nlevcoage ))
allocate( fates_hdim_pfmap_levcapf(1:nlevcoage*numpft))
allocate( fates_hdim_camap_levcapf(1:nlevcoage*numpft))
allocate( fates_hdim_levcan(nclmax))
allocate( fates_hdim_levelem(num_elements))
allocate( fates_hdim_canmap_levcnlf(nlevleaf*nclmax))
allocate( fates_hdim_lfmap_levcnlf(nlevleaf*nclmax))
allocate( fates_hdim_canmap_levcnlfpf(nlevleaf*nclmax*numpft))
allocate( fates_hdim_lfmap_levcnlfpf(nlevleaf*nclmax*numpft))
allocate( fates_hdim_pftmap_levcnlfpf(nlevleaf*nclmax*numpft))
allocate( fates_hdim_scmap_levscag(nlevsclass * nlevage ))
allocate( fates_hdim_agmap_levscag(nlevsclass * nlevage ))
allocate( fates_hdim_scmap_levscagpft(nlevsclass * nlevage * numpft))
allocate( fates_hdim_agmap_levscagpft(nlevsclass * nlevage * numpft))
allocate( fates_hdim_pftmap_levscagpft(nlevsclass * nlevage * numpft))
allocate( fates_hdim_agmap_levagepft(nlevage * numpft))
allocate( fates_hdim_pftmap_levagepft(nlevage * numpft))
allocate( fates_hdim_elmap_levelpft(num_elements*numpft))
allocate( fates_hdim_elmap_levelcwd(num_elements*ncwd))
allocate( fates_hdim_elmap_levelage(num_elements*nlevage))
allocate( fates_hdim_pftmap_levelpft(num_elements*numpft))
allocate( fates_hdim_cwdmap_levelcwd(num_elements*ncwd))
allocate( fates_hdim_agemap_levelage(num_elements*nlevage))
! Fill the IO array of plant size classes
fates_hdim_levsclass(:) = ED_val_history_sizeclass_bin_edges(:)
fates_hdim_levage(:) = ED_val_history_ageclass_bin_edges(:)
fates_hdim_levheight(:) = ED_val_history_height_bin_edges(:)
fates_hdim_levcoage(:) = ED_val_history_coageclass_bin_edges(:)
! make pft array
do ipft=1,numpft
fates_hdim_levpft(ipft) = ipft
end do
! make fuel array
do ifuel=1,NFSC
fates_hdim_levfuel(ifuel) = ifuel
end do
! make cwd array
do icwd=1,NCWD
fates_hdim_levcwdsc(icwd) = icwd
end do
! make canopy array
do ican = 1,nclmax
fates_hdim_levcan(ican) = ican
end do
! Make an element array, each index is the PARTEH global identifier index
do iel = 1, num_elements
fates_hdim_levelem(iel) = element_list(iel)
end do
i = 0
do iel = 1, num_elements
do ipft=1,numpft
i = i+1
fates_hdim_elmap_levelpft(i) = iel
fates_hdim_pftmap_levelpft(i) = ipft
end do
end do
i = 0
do iel = 1, num_elements
do icwd = 1, ncwd
i = i+1
fates_hdim_elmap_levelcwd(i) = iel
fates_hdim_cwdmap_levelcwd(i) = icwd
end do
end do
i = 0
do iel = 1, num_elements
do iage=1,nlevage
i = i+1
fates_hdim_elmap_levelage(i) = iel
fates_hdim_agemap_levelage(i) = iage
end do
end do
! Fill the IO arrays that match pft and size class to their combined array
i=0
do ipft=1,numpft
do isc=1,nlevsclass
i=i+1
fates_hdim_pfmap_levscpf(i) = ipft
fates_hdim_scmap_levscpf(i) = isc
end do
end do
i=0
do ipft=1,numpft
do icoage=1,nlevcoage
i=i+1
fates_hdim_pfmap_levcapf(i) = ipft