forked from ESCOMP/CTSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clmfates_interfaceMod.F90
2772 lines (2170 loc) · 116 KB
/
clmfates_interfaceMod.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 CLMFatesInterfaceMod
! -------------------------------------------------------------------------------------
! This module contains various functions and definitions to aid in the
! coupling of the FATES library/API with the CLM/ALM/ATS/etc model driver.
! All connections between the two models should occur in this file alone.
!
! This is also the only location where CLM code is allowed to see FATES memory
! structures.
! The routines here, that call FATES library routines, will not pass any types defined
! by the driving land model (HLM).
!
! either native type arrays (int,real,log, etc) or packed into fates boundary condition
! structures.
!
! Note that CLM/ALM does use Shared Memory Parallelism (SMP), where processes such as
! the update of state variables are forked. However, IO is not assumed to be
! threadsafe and therefore memory spaces reserved for IO must be continuous vectors,
! and moreover they must be pushed/pulled from history IO for each individual
! bounds_proc memory space as a unit.
!
! Therefore, the state variables in the clm_fates communicator is vectorized by
! threadcount, and the IO communication arrays are not.
!
!
! Conventions:
! keep line widths within 90 spaces
! HLM acronym = Host Land Model
!
! -------------------------------------------------------------------------------------
! use ed_driver_interface, only:
! Used CLM Modules
use PatchType , only : patch
use shr_kind_mod , only : r8 => shr_kind_r8
use decompMod , only : bounds_type
use WaterStateType , only : waterstate_type
use WaterFluxType , only : waterflux_type
use CanopyStateType , only : canopystate_type
use TemperatureType , only : temperature_type
use EnergyFluxType , only : energyflux_type
use SoilStateType , only : soilstate_type
use clm_varctl , only : iulog
use clm_varctl , only : use_vertsoilc
use clm_varctl , only : fates_parteh_mode
use clm_varctl , only : use_fates
use clm_varctl , only : fates_spitfire_mode
use clm_varctl , only : use_fates_planthydro
use clm_varctl , only : use_fates_cohort_age_tracking
use clm_varctl , only : use_fates_ed_st3
use clm_varctl , only : use_fates_ed_prescribed_phys
use clm_varctl , only : use_fates_logging
use clm_varctl , only : use_fates_inventory_init
use clm_varctl , only : use_fates_fixed_biogeog
use clm_varctl , only : fates_inventory_ctrl_filename
use clm_varcon , only : tfrz
use clm_varcon , only : spval
use clm_varcon , only : denice
use clm_varcon , only : ispval
use clm_varpar , only : natpft_size, natpft_ub, natpft_lb
use clm_varpar , only : numrad
use clm_varpar , only : ivis
use clm_varpar , only : inir
use clm_varpar , only : nlevgrnd
use clm_varpar , only : nlevdecomp
use clm_varpar , only : nlevdecomp_full
use PhotosynthesisMod , only : photosyns_type
use atm2lndType , only : atm2lnd_type
use SurfaceAlbedoType , only : surfalb_type
use SolarAbsorbedType , only : solarabs_type
use SoilBiogeochemCarbonFluxType, only : soilbiogeochem_carbonflux_type
use SoilBiogeochemCarbonStateType, only : soilbiogeochem_carbonstate_type
use FrictionVelocityMod , only : frictionvel_type
use clm_time_manager , only : is_restart
use ncdio_pio , only : file_desc_t, ncd_int, ncd_double
use restUtilMod, only : restartvar
use clm_time_manager , only : get_days_per_year, &
get_curr_date, &
get_ref_date, &
timemgr_datediff, &
is_beg_curr_day, &
get_step_size, &
get_nstep
use spmdMod , only : masterproc
use decompMod , only : get_proc_bounds, &
get_proc_clumps, &
get_clump_bounds
use GridCellType , only : grc
use ColumnType , only : col
use LandunitType , only : lun
use landunit_varcon , only : istsoil
use abortutils , only : endrun
use shr_log_mod , only : errMsg => shr_log_errMsg
use clm_varcon , only : dzsoi_decomp
use FuncPedotransferMod, only: get_ipedof
! use SoilWaterPlantSinkMod, only : Compute_EffecRootFrac_And_VertTranSink_Default
! Used FATES Modules
use FatesInterfaceTypesMod , only : fates_interface_type
use FatesInterfaceMod, only : FatesInterfaceInit, FatesReportParameters
use FatesInterfaceMod, only : SetFatesGlobalElements
use FatesInterfaceMod , only : allocate_bcin
use FatesInterfaceMod , only : allocate_bcout
use FatesInterfaceMod , only : zero_bcs
use FatesInterfaceMod , only : SetFatesTime
use FatesInterfaceMod , only : set_fates_ctrlparms
use FatesHistoryInterfaceMod, only : fates_history_interface_type
use FatesRestartInterfaceMod, only : fates_restart_interface_type
use EDTypesMod , only : ed_patch_type
use EDTypesMod , only : num_elements
use FatesInterfaceTypesMod , only : hlm_numlevgrnd
use EDMainMod , only : ed_ecosystem_dynamics
use EDMainMod , only : ed_update_site
use EDInitMod , only : zero_site
use EDInitMod , only : init_site_vars
use EDInitMod , only : init_patches
use EDInitMod , only : set_site_properties
use EDPftVarcon , only : EDpftvarcon_inst
use EDSurfaceRadiationMod , only : ED_SunShadeFracs, ED_Norman_Radiation
use EDBtranMod , only : btran_ed, &
get_active_suction_layers
use EDCanopyStructureMod , only : canopy_summarization, update_hlm_dynamics
use FatesPlantRespPhotosynthMod, only : FatesPlantRespPhotosynthDrive
use EDAccumulateFluxesMod , only : AccumulateFluxes_ED
use EDPhysiologyMod , only : FluxIntoLitterPools
use FatesPlantHydraulicsMod, only : hydraulics_drive
use FatesPlantHydraulicsMod, only : HydrSiteColdStart
use FatesPlantHydraulicsMod, only : InitHydrSites
use FatesPlantHydraulicsMod, only : RestartHydrStates
use dynSubgridControlMod , only : get_do_harvest
use dynHarvestMod , only : num_harvest_inst, harvest_varnames
use dynHarvestMod , only : harvest_units, mass_units, unitless_units
use dynHarvestMod , only : dynHarvest_interp_resolve_harvesttypes
use FatesConstantsMod , only : hlm_harvest_area_fraction
use FatesConstantsMod , only : hlm_harvest_carbon
use FATESFireBase , only : fates_fire_base_type
implicit none
type, public :: f2hmap_type
! This is the associated column index of each FATES site
integer, allocatable :: fcolumn (:)
! This is the associated site index of any HLM columns
! This vector may be sparse, and non-sites have index 0
integer, allocatable :: hsites (:)
end type f2hmap_type
type, public :: hlm_fates_interface_type
! private
! See above for descriptions of the sub-types populated
! by thread. This type is somewhat self-explanatory, in that it simply
! breaks up memory and process by thread. Each thread will have its
! own list of sites, and boundary conditions for those sites
type(fates_interface_type), allocatable :: fates (:)
! This memory structure is used to map fates sites
! into the host model. Currently, the FATES site
! and its column number matching are its only members
type(f2hmap_type), allocatable :: f2hmap(:)
! fates_hist is the interface class for the history output
type(fates_history_interface_type) :: fates_hist
! fates_restart is the inteface calss for restarting the model
type(fates_restart_interface_type) :: fates_restart
! fates_fire_data_method determines the fire data passed from HLM to FATES
class(fates_fire_base_type), allocatable :: fates_fire_data_method
contains
procedure, public :: init
procedure, public :: check_hlm_active
procedure, public :: restart
procedure, public :: init_coldstart
procedure, public :: dynamics_driv
procedure, public :: wrap_sunfrac
procedure, public :: wrap_btran
procedure, public :: wrap_photosynthesis
procedure, public :: wrap_accumulatefluxes
procedure, public :: prep_canopyfluxes
procedure, public :: wrap_canopy_radiation
procedure, public :: wrap_bgc_summary
procedure, public :: TransferZ0mDisp
procedure, public :: InterpFileInputs ! Interpolate inputs from files
procedure, public :: Init2 ! Initialization after determining subgrid weights
procedure, public :: InitAccBuffer ! Initialize any accumulation buffers
procedure, public :: InitAccVars ! Initialize any accumulation variables
procedure, public :: UpdateAccVars ! Update any accumulation variables
procedure, private :: init_history_io
procedure, private :: wrap_update_hlmfates_dyn
procedure, private :: init_soil_depths
procedure, public :: ComputeRootSoilFlux
procedure, public :: wrap_hydraulics_drive
end type hlm_fates_interface_type
! hlm_bounds_to_fates_bounds is not currently called outside the interface.
! Although there may be good reasons to, I privatized it so that the next
! developer will at least question its usage (RGK)
private :: hlm_bounds_to_fates_bounds
! The GetAndSetTime function is used to get the current time from the CLM
! time procedures and then set to the fates global time variables during restart,
! init_coldstart, and dynamics_driv function calls
private :: GetAndSetTime
logical :: debug = .false.
character(len=*), parameter, private :: sourcefile = &
__FILE__
public :: CLMFatesGlobals
contains
subroutine CLMFatesGlobals()
! --------------------------------------------------------------------------------
! This is one of the first calls to fates
! Used for setting dimensions. This MUST
! be called after NL variables are specified and
! after the FATES parameter file has been read in
! Aside from setting global dimension info, which
! is used in the history file, we also transfer
! over the NL variables to FATES global settings.
! --------------------------------------------------------------------------------
logical :: verbose_output
integer :: pass_masterproc
integer :: pass_vertsoilc
integer :: pass_ed_st3
integer :: pass_num_lu_harvest_cats
integer :: pass_lu_harvest
integer :: pass_logging
integer :: pass_ed_prescribed_phys
integer :: pass_planthydro
integer :: pass_inventory_init
integer :: pass_is_restart
integer :: pass_cohort_age_tracking
integer :: pass_biogeog
if (use_fates) then
verbose_output = .false.
call FatesInterfaceInit(iulog, verbose_output)
! Force FATES parameters that are recieve type, to the unset value
call set_fates_ctrlparms('flush_to_unset')
! Send parameters individually
call set_fates_ctrlparms('num_sw_bbands',ival=numrad)
call set_fates_ctrlparms('vis_sw_index',ival=ivis)
call set_fates_ctrlparms('nir_sw_index',ival=inir)
call set_fates_ctrlparms('num_lev_ground',ival=nlevgrnd)
call set_fates_ctrlparms('hlm_name',cval='CLM')
call set_fates_ctrlparms('hio_ignore_val',rval=spval)
call set_fates_ctrlparms('soilwater_ipedof',ival=get_ipedof(0))
call set_fates_ctrlparms('max_patch_per_site',ival=(natpft_size-1))
call set_fates_ctrlparms('parteh_mode',ival=fates_parteh_mode)
call set_fates_ctrlparms('spitfire_mode',ival=fates_spitfire_mode)
if(is_restart()) then
pass_is_restart = 1
else
pass_is_restart = 0
end if
call set_fates_ctrlparms('is_restart',ival=pass_is_restart)
if(use_vertsoilc) then
pass_vertsoilc = 1
else
pass_vertsoilc = 0
end if
call set_fates_ctrlparms('use_vertsoilc',ival=pass_vertsoilc)
if(use_fates_fixed_biogeog)then
pass_biogeog = 1
else
pass_biogeog = 0
end if
call set_fates_ctrlparms('use_fixed_biogeog',ival=pass_biogeog)
if(use_fates_ed_st3) then
pass_ed_st3 = 1
else
pass_ed_st3 = 0
end if
call set_fates_ctrlparms('use_ed_st3',ival=pass_ed_st3)
if(use_fates_logging) then
pass_logging = 1
else
pass_logging = 0
end if
call set_fates_ctrlparms('use_logging',ival=pass_logging)
if(use_fates_ed_prescribed_phys) then
pass_ed_prescribed_phys = 1
else
pass_ed_prescribed_phys = 0
end if
call set_fates_ctrlparms('use_ed_prescribed_phys',ival=pass_ed_prescribed_phys)
if(use_fates_planthydro) then
pass_planthydro = 1
else
pass_planthydro = 0
end if
call set_fates_ctrlparms('use_planthydro',ival=pass_planthydro)
if(use_fates_cohort_age_tracking) then
pass_cohort_age_tracking = 1
else
pass_cohort_age_tracking = 0
end if
call set_fates_ctrlparms('use_cohort_age_tracking',ival=pass_cohort_age_tracking)
! check fates logging namelist value first because hlm harvest overrides it
if(use_fates_logging) then
pass_logging = 1
else
pass_logging = 0
end if
if(get_do_harvest()) then
pass_logging = 1
pass_num_lu_harvest_cats = num_harvest_inst
pass_lu_harvest = 1
else
pass_lu_harvest = 0
pass_num_lu_harvest_cats = 0
end if
call set_fates_ctrlparms('use_lu_harvest',ival=pass_lu_harvest)
call set_fates_ctrlparms('num_lu_harvest_cats',ival=pass_num_lu_harvest_cats)
call set_fates_ctrlparms('use_logging',ival=pass_logging)
if(use_fates_inventory_init) then
pass_inventory_init = 1
else
pass_inventory_init = 0
end if
call set_fates_ctrlparms('use_inventory_init',ival=pass_inventory_init)
call set_fates_ctrlparms('inventory_ctrl_file',cval=fates_inventory_ctrl_filename)
if(masterproc)then
pass_masterproc = 1
else
pass_masterproc = 0
end if
call set_fates_ctrlparms('masterproc',ival=pass_masterproc)
! Check through FATES parameters to see if all have been set
call set_fates_ctrlparms('check_allset')
end if
! This determines the total amount of space it requires in its largest
! dimension. We are currently calling that the "cohort" dimension, but
! it is really a utility dimension that captures the models largest
! size need.
! Sets:
! fates_maxElementsPerPatch
! num_elements
! fates_maxElementsPerSite (where a site is roughly equivalent to a column)
! (Note: this needs to be called when use_fates=.false. as well, becuase
! it will return some nominal dimension sizes of 1
call SetFatesGlobalElements(use_fates)
return
end subroutine CLMFatesGlobals
! ====================================================================================
subroutine init(this, bounds_proc )
! ---------------------------------------------------------------------------------
! This initializes the hlm_fates_interface_type
!
! sites is the root of the fates state hierarchy (instantaneous info on
! the state of the ecosystem). As such, it governs the connection points between
! the host (which also dictates its allocation) and its patch structures.
!
! sites may associate with different scales in different models. In
! CLM, it is being designed to relate to column scale.
!
! This global may become relegated to this module.
!
! Note: CLM/ALM currently wants sites to be allocated even if ed
! is not turned on
! ---------------------------------------------------------------------------------
use FatesInterfaceTypesMod, only : numpft_fates => numpft
use FatesParameterDerivedMod, only : param_derived
use subgridMod, only : natveg_patch_exists
use clm_instur , only : wt_nat_patch
use CNFireFactoryMod , only: create_fates_fire_data_method
implicit none
! Input Arguments
class(hlm_fates_interface_type), intent(inout) :: this
type(bounds_type),intent(in) :: bounds_proc
! local variables
integer :: nclumps ! Number of threads
integer :: nc ! thread index
integer :: s ! FATES site index
integer :: c ! HLM column index
integer :: l ! HLM LU index
integer :: g ! HLM grid index
integer :: m ! HLM PFT index
integer :: ft ! FATES PFT index
integer :: pi,pf
integer, allocatable :: collist (:)
type(bounds_type) :: bounds_clump
integer :: nmaxcol
integer :: ndecomp
! Initialize the FATES communicators with the HLM
! This involves to stages
! 1) allocate the vectors
! 2) add the history variables defined in clm_inst to the history machinery
! Parameter Routines
call param_derived%Init( numpft_fates )
nclumps = get_proc_clumps()
allocate(this%fates(nclumps))
allocate(this%f2hmap(nclumps))
if(debug)then
write(iulog,*) 'clm_fates%init(): allocating for ',nclumps,' threads'
end if
nclumps = get_proc_clumps()
!$OMP PARALLEL DO PRIVATE (nc,bounds_clump,nmaxcol,s,c,l,g,collist,pi,pf)
do nc = 1,nclumps
call get_clump_bounds(nc, bounds_clump)
nmaxcol = bounds_clump%endc - bounds_clump%begc + 1
allocate(collist(1:nmaxcol))
! Allocate the mapping that points columns to FATES sites, 0 is NA
allocate(this%f2hmap(nc)%hsites(bounds_clump%begc:bounds_clump%endc))
! Initialize all columns with a zero index, which indicates no FATES site
this%f2hmap(nc)%hsites(:) = 0
s = 0
do c = bounds_clump%begc,bounds_clump%endc
l = col%landunit(c)
! These are the key constraints that determine if this column
! will have a FATES site associated with it
! INTERF-TODO: WE HAVE NOT FILTERED OUT FATES SITES ON INACTIVE COLUMNS.. YET
! NEED A RUN-TIME ROUTINE THAT CLEARS AND REWRITES THE SITE LIST
if (lun%itype(l) == istsoil ) then
s = s + 1
collist(s) = c
this%f2hmap(nc)%hsites(c) = s
if(debug)then
write(iulog,*) 'clm_fates%init(): thread',nc,': found column',c,'with lu',l
write(iulog,*) 'LU type:', lun%itype(l)
end if
endif
enddo
if(debug)then
write(iulog,*) 'clm_fates%init(): thread',nc,': allocated ',s,' sites'
end if
! Allocate vectors that match FATES sites with HLM columns
! RGK: Sites and fcolumns are forced as args during clm_driv() as of 6/4/2016
! We may have to give these a dummy allocation of 1, which should
! not be a problem since we always iterate on nsites.
allocate(this%f2hmap(nc)%fcolumn(s))
! Assign the h2hmap indexing
this%f2hmap(nc)%fcolumn(1:s) = collist(1:s)
! Deallocate the temporary arrays
deallocate(collist)
! Set the number of FATES sites
this%fates(nc)%nsites = s
! Allocate the FATES sites
allocate (this%fates(nc)%sites(this%fates(nc)%nsites))
! Allocate the FATES boundary arrays (in)
allocate(this%fates(nc)%bc_in(this%fates(nc)%nsites))
! Allocate the FATES boundary arrays (out)
allocate(this%fates(nc)%bc_out(this%fates(nc)%nsites))
! Allocate and Initialize the Boundary Condition Arrays
! These are staticaly allocated at maximums, so
! No information about the patch or cohort structure is needed at this step
do s = 1, this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
if (use_vertsoilc) then
ndecomp = col%nbedrock(c)
else
ndecomp = 1
end if
call allocate_bcin(this%fates(nc)%bc_in(s),col%nbedrock(c),ndecomp, num_harvest_inst)
call allocate_bcout(this%fates(nc)%bc_out(s),col%nbedrock(c),ndecomp)
call zero_bcs(this%fates(nc),s)
! Pass any grid-cell derived attributes to the site
! ---------------------------------------------------------------------------
c = this%f2hmap(nc)%fcolumn(s)
g = col%gridcell(c)
this%fates(nc)%sites(s)%lat = grc%latdeg(g)
this%fates(nc)%sites(s)%lon = grc%londeg(g)
! initialize static layers for reduced complexity FATES versions from HLM
! maybe make this into a subroutine of it's own later.
do m = natpft_lb,natpft_ub-1
ft = m-natpft_lb+1
if (natveg_patch_exists(g, m)) then
this%fates(nc)%bc_in(s)%pft_areafrac(ft)=wt_nat_patch(g,m)
else
this%fates(nc)%bc_in(s)%pft_areafrac(ft)=0._r8
end if
end do
end do !site
! Initialize site-level static quantities dictated by the HLM
! currently ground layering depth
call this%init_soil_depths(nc)
if (use_fates_planthydro) then
call InitHydrSites(this%fates(nc)%sites,this%fates(nc)%bc_in)
end if
if( this%fates(nc)%nsites == 0 ) then
write(iulog,*) 'Clump ',nc,' had no valid FATES sites'
write(iulog,*) 'This will likely cause problems until code is improved'
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
! Set patch itypes on natural veg columns to nonsense
! This will force a crash if the model outside of FATES tries to think
! of the patch as a PFT.
do s = 1, this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
pi = col%patchi(c)+1
pf = col%patchf(c)
! patch%itype(pi:pf) = ispval
patch%is_fates(pi:pf) = .true.
end do
end do
!$OMP END PARALLEL DO
call this%init_history_io(bounds_proc)
! Report Fates Parameters (debug flag in lower level routines)
call FatesReportParameters(masterproc)
! Fire data to send to FATES
call create_fates_fire_data_method( this%fates_fire_data_method )
end subroutine init
! ===================================================================================
subroutine check_hlm_active(this, nc, bounds_clump)
! ---------------------------------------------------------------------------------
! This subroutine is not currently used. It is just a utility that may come
! in handy when we have dynamic sites in FATES
! ---------------------------------------------------------------------------------
implicit none
class(hlm_fates_interface_type), intent(inout) :: this
integer :: nc
type(bounds_type),intent(in) :: bounds_clump
! local variables
integer :: c
do c = bounds_clump%begc,bounds_clump%endc
! FATES ACTIVE BUT HLM IS NOT
if(this%f2hmap(nc)%hsites(c)>0 .and. .not.col%active(c)) then
write(iulog,*) 'INACTIVE COLUMN WITH ACTIVE FATES SITE'
write(iulog,*) 'c = ',c
call endrun(msg=errMsg(sourcefile, __LINE__))
elseif (this%f2hmap(nc)%hsites(c)==0 .and. col%active(c)) then
write(iulog,*) 'ACTIVE COLUMN WITH INACTIVE FATES SITE'
write(iulog,*) 'c = ',c
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
end do
end subroutine check_hlm_active
! ------------------------------------------------------------------------------------
subroutine dynamics_driv(this, nc, bounds_clump, &
atm2lnd_inst, soilstate_inst, temperature_inst, &
waterstate_inst, canopystate_inst, soilbiogeochem_carbonflux_inst, &
frictionvel_inst)
! This wrapper is called daily from clm_driver
! This wrapper calls ed_driver, which is the daily dynamics component of FATES
! ed_driver is not a hlm_fates_inst_type procedure because we need an extra step
! to process array bounding information
! !USES
use CNFireFactoryMod, only: scalar_lightning
! !ARGUMENTS:
implicit none
class(hlm_fates_interface_type), intent(inout) :: this
type(bounds_type),intent(in) :: bounds_clump
type(atm2lnd_type) , intent(in) :: atm2lnd_inst
type(soilstate_type) , intent(in) :: soilstate_inst
type(temperature_type) , intent(in) :: temperature_inst
integer , intent(in) :: nc
type(waterstate_type) , intent(inout) :: waterstate_inst
type(canopystate_type) , intent(inout) :: canopystate_inst
type(soilbiogeochem_carbonflux_type), intent(inout) :: soilbiogeochem_carbonflux_inst
type(frictionvel_type) , intent(inout) :: frictionvel_inst
! !LOCAL VARIABLES:
integer :: s ! site index
integer :: g ! grid-cell index (HLM)
integer :: c ! column index (HLM)
integer :: ifp ! patch index
integer :: p ! HLM patch index
integer :: nlevsoil ! number of soil layers at the site
integer :: nld_si ! site specific number of decomposition layers
integer :: begg,endg
real(r8) :: harvest_rates(bounds_clump%begg:bounds_clump%endg,num_harvest_inst)
logical :: after_start_of_harvest_ts
integer :: iharv
real(r8), pointer :: lnfm24(:)
integer :: ier
!-----------------------------------------------------------------------
! ---------------------------------------------------------------------------------
! Part I.
! Prepare input boundary conditions for FATES dynamics
! Note that timing information is the same across all sites, this may
! seem redundant, but it is possible that we may have asynchronous site simulations
! one day. The cost of holding site level boundary conditions is minimal
! and it keeps all the boundaries in one location
! ---------------------------------------------------------------------------------
begg = bounds_clump%begg; endg = bounds_clump%endg
! Set the FATES global time and date variables
call GetAndSetTime
if (get_do_harvest()) then
call dynHarvest_interp_resolve_harvesttypes(bounds_clump, &
harvest_rates=harvest_rates(begg:endg,1:num_harvest_inst), &
after_start_of_harvest_ts=after_start_of_harvest_ts)
endif
if (fates_spitfire_mode > scalar_lightning) then
allocate(lnfm24(bounds_clump%begg:bounds_clump%endg), stat=ier)
if (ier /= 0) then
call endrun(msg="allocation error for lnfm24"//&
errmsg(sourcefile, __LINE__))
endif
lnfm24 = this%fates_fire_data_method%GetLight24()
end if
do s=1,this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
g = col%gridcell(c)
if (fates_spitfire_mode > scalar_lightning) then
do ifp = 1, this%fates(nc)%sites(s)%youngest_patch%patchno
p = ifp + col%patchi(c)
this%fates(nc)%bc_in(s)%lightning24(ifp) = lnfm24(g) * 24._r8 ! #/km2/hr to #/km2/day
this%fates(nc)%bc_in(s)%pop_density(ifp) = this%fates_fire_data_method%forc_hdm(g)
end do
end if
nlevsoil = this%fates(nc)%bc_in(s)%nlevsoil
this%fates(nc)%bc_in(s)%h2o_liqvol_sl(1:nlevsoil) = &
waterstate_inst%h2osoi_vol_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%max_rooting_depth_index_col = &
min(nlevsoil, canopystate_inst%altmax_lastyear_indx_col(c))
do ifp = 1, this%fates(nc)%sites(s)%youngest_patch%patchno
p = ifp+col%patchi(c)
this%fates(nc)%bc_in(s)%t_veg24_pa(ifp) = &
temperature_inst%t_veg24_patch(p)
this%fates(nc)%bc_in(s)%precip24_pa(ifp) = &
atm2lnd_inst%prec24_patch(p)
this%fates(nc)%bc_in(s)%relhumid24_pa(ifp) = &
atm2lnd_inst%rh24_patch(p)
this%fates(nc)%bc_in(s)%wind24_pa(ifp) = &
atm2lnd_inst%wind24_patch(p)
end do
if(use_fates_planthydro)then
this%fates(nc)%bc_in(s)%hksat_sisl(1:nlevsoil) = soilstate_inst%hksat_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%watsat_sisl(1:nlevsoil) = soilstate_inst%watsat_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%watres_sisl(1:nlevsoil) = soilstate_inst%watres_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%sucsat_sisl(1:nlevsoil) = soilstate_inst%sucsat_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%bsw_sisl(1:nlevsoil) = soilstate_inst%bsw_col(c,1:nlevsoil)
this%fates(nc)%bc_in(s)%h2o_liq_sisl(1:nlevsoil) = waterstate_inst%h2osoi_liq_col(c,1:nlevsoil)
end if
! get the harvest data, which is by gridcell
! for now there is one veg column per gridcell, so store all harvest data in each site
! this will eventually change
! today's hlm harvest flag needs to be set no matter what
if (get_do_harvest()) then
if (after_start_of_harvest_ts) then
this%fates(nc)%bc_in(s)%hlm_harvest_rates(1:num_harvest_inst) = harvest_rates(g,1:num_harvest_inst)
else
this%fates(nc)%bc_in(s)%hlm_harvest_rates(1:num_harvest_inst) = 0._r8
end if
this%fates(nc)%bc_in(s)%hlm_harvest_catnames(1:num_harvest_inst) = harvest_varnames(1:num_harvest_inst)
! also pass the units that the harvest rates are specified in
if (trim(harvest_units) .eq. trim(unitless_units)) then
this%fates(nc)%bc_in(s)%hlm_harvest_units = hlm_harvest_area_fraction
else if (trim(harvest_units) .eq. trim(mass_units)) then
this%fates(nc)%bc_in(s)%hlm_harvest_units = hlm_harvest_carbon
else
write(iulog,*) 'units field not one of the specified options.'
write(iulog,*) harvest_units
call endrun(msg=errMsg(sourcefile, __LINE__))
end if
endif
end do
! ---------------------------------------------------------------------------------
! Part II: Call the FATES model now that input boundary conditions have been
! provided.
! ---------------------------------------------------------------------------------
do s = 1,this%fates(nc)%nsites
call ed_ecosystem_dynamics(this%fates(nc)%sites(s), &
this%fates(nc)%bc_in(s))
call ed_update_site(this%fates(nc)%sites(s), &
this%fates(nc)%bc_in(s))
enddo
! call subroutine to aggregate fates litter output fluxes and
! package them for handing across interface
call FluxIntoLitterPools(this%fates(nc)%nsites, &
this%fates(nc)%sites, &
this%fates(nc)%bc_in, &
this%fates(nc)%bc_out)
! ---------------------------------------------------------------------------------
! Part III: Process FATES output into the dimensions and structures that are part
! of the HLMs API. (column, depth, and litter fractions)
! ---------------------------------------------------------------------------------
do s = 1, this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_lab_c_col(c,1:nlevdecomp) = 0.0_r8
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_cel_c_col(c,1:nlevdecomp) = 0.0_r8
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_lig_c_col(c,1:nlevdecomp) = 0.0_r8
nld_si = this%fates(nc)%bc_in(s)%nlevdecomp
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_lab_c_col(c,1:nld_si) = &
this%fates(nc)%bc_out(s)%litt_flux_lab_c_si(1:nld_si)
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_cel_c_col(c,1:nld_si) = &
this%fates(nc)%bc_out(s)%litt_flux_cel_c_si(1:nld_si)
soilbiogeochem_carbonflux_inst%FATES_c_to_litr_lig_c_col(c,1:nld_si) = &
this%fates(nc)%bc_out(s)%litt_flux_lig_c_si(1:nld_si)
end do
! ---------------------------------------------------------------------------------
! Part III.2 (continued).
! Update diagnostics of the FATES ecosystem structure that are used in the HLM.
! ---------------------------------------------------------------------------------
call this%wrap_update_hlmfates_dyn(nc, &
bounds_clump, &
waterstate_inst, &
canopystate_inst, &
frictionvel_inst)
! ---------------------------------------------------------------------------------
! Part IV:
! Update history IO fields that depend on ecosystem dynamics
! ---------------------------------------------------------------------------------
call this%fates_hist%update_history_dyn( nc, &
this%fates(nc)%nsites, &
this%fates(nc)%sites)
if (masterproc) then
write(iulog, *) 'clm: leaving fates model', bounds_clump%begg, &
bounds_clump%endg
end if
return
end subroutine dynamics_driv
! ------------------------------------------------------------------------------------
subroutine wrap_update_hlmfates_dyn(this, nc, bounds_clump, &
waterstate_inst, canopystate_inst, frictionvel_inst )
! ---------------------------------------------------------------------------------
! This routine handles the updating of vegetation canopy diagnostics, (such as lai)
! that either requires HLM boundary conditions (like snow accumulation) or
! provides boundary conditions (such as vegetation fractional coverage)
! ---------------------------------------------------------------------------------
implicit none
class(hlm_fates_interface_type), intent(inout) :: this
type(bounds_type),intent(in) :: bounds_clump
integer , intent(in) :: nc
type(waterstate_type) , intent(inout) :: waterstate_inst
type(canopystate_type) , intent(inout) :: canopystate_inst
type(frictionvel_type) , intent(inout) :: frictionvel_inst
integer :: npatch ! number of patches in each site
integer :: ifp ! index FATES patch
integer :: p ! HLM patch index
integer :: s ! site index
integer :: c ! column index
associate( &
tlai => canopystate_inst%tlai_patch , &
elai => canopystate_inst%elai_patch , &
tsai => canopystate_inst%tsai_patch , &
esai => canopystate_inst%esai_patch , &
htop => canopystate_inst%htop_patch , &
hbot => canopystate_inst%hbot_patch , &
z0m => frictionvel_inst%z0m_patch , & ! Output: [real(r8) (:) ] momentum roughness length (m)
displa => canopystate_inst%displa_patch, &
dleaf_patch => canopystate_inst%dleaf_patch, &
snow_depth => waterstate_inst%snow_depth_col, &
frac_sno_eff => waterstate_inst%frac_sno_eff_col, &
frac_veg_nosno_alb => canopystate_inst%frac_veg_nosno_alb_patch)
! Process input boundary conditions to FATES
! --------------------------------------------------------------------------------
do s=1,this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
this%fates(nc)%bc_in(s)%snow_depth_si = snow_depth(c)
this%fates(nc)%bc_in(s)%frac_sno_eff_si = frac_sno_eff(c)
end do
! Canopy diagnostics for FATES
call canopy_summarization(this%fates(nc)%nsites, &
this%fates(nc)%sites, &
this%fates(nc)%bc_in)
! Canopy diagnostic outputs for HLM
call update_hlm_dynamics(this%fates(nc)%nsites, &
this%fates(nc)%sites, &
this%f2hmap(nc)%fcolumn, &
this%fates(nc)%bc_out )
!---------------------------------------------------------------------------------
! CHANGING STORED WATER DURING PLANT DYNAMICS IS NOT FULLY IMPLEMENTED
! LEAVING AS A PLACE-HOLDER FOR NOW.
! ! Diagnose water storage in canopy if hydraulics is on
! ! This updates the internal value and the bc_out value.
! ! If hydraulics is off, it returns 0 storage
if ( use_fates_planthydro ) then
do s = 1, this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
waterstate_inst%total_plant_stored_h2o_col(c) = &
this%fates(nc)%bc_out(s)%plant_stored_h2o_si
end do
end if
!---------------------------------------------------------------------------------
! Convert FATES dynamics into HLM usable information
! Initialize weighting variables (note FATES is the only HLM module
! that uses "is_veg" and "is_bareground". The entire purpose of these
! variables is to inform patch%wtcol(p). wt_ed is imposed on wtcol,
! but only for FATES columns.
patch%is_veg(bounds_clump%begp:bounds_clump%endp) = .false.
patch%is_bareground(bounds_clump%begp:bounds_clump%endp) = .false.
patch%wt_ed(bounds_clump%begp:bounds_clump%endp) = 0.0_r8
do s = 1,this%fates(nc)%nsites
c = this%f2hmap(nc)%fcolumn(s)
! Other modules may have AI's we only flush values
! that are on the naturally vegetated columns
elai(col%patchi(c):col%patchf(c)) = 0.0_r8
tlai(col%patchi(c):col%patchf(c)) = 0.0_r8
esai(col%patchi(c):col%patchf(c)) = 0.0_r8
tsai(col%patchi(c):col%patchf(c)) = 0.0_r8
htop(col%patchi(c):col%patchf(c)) = 0.0_r8
hbot(col%patchi(c):col%patchf(c)) = 0.0_r8
! FATES does not dictate bare-ground so turbulent
! variables are not over-written.
z0m(col%patchi(c)+1:col%patchf(c)) = 0.0_r8
displa(col%patchi(c)+1:col%patchf(c)) = 0.0_r8
dleaf_patch(col%patchi(c)+1:col%patchf(c)) = 0.0_r8
frac_veg_nosno_alb(col%patchi(c):col%patchf(c)) = 0.0_r8
! Set the bareground patch indicator
patch%is_bareground(col%patchi(c)) = .true.
npatch = this%fates(nc)%sites(s)%youngest_patch%patchno
! Precision errors on the canopy_fraction_pa sum, even small (e-12)
! do exist, and can create potentially negetive bare-soil fractions
! (ie -1e-12 or smaller). Even though this is effectively zero,
! it can generate weird logic scenarios in the ctsm/elm code, so we
! protext it here with a lower bound of 0.0_r8.
patch%wt_ed(col%patchi(c)) = max(0.0_r8, &
1.0_r8-sum(this%fates(nc)%bc_out(s)%canopy_fraction_pa(1:npatch)))
if(sum(this%fates(nc)%bc_out(s)%canopy_fraction_pa(1:npatch))>1.0_r8)then
write(iulog,*)'Projected Canopy Area of all FATES patches'
write(iulog,*)'cannot exceed 1.0'
!end_run()
end if
do ifp = 1, npatch
p = ifp+col%patchi(c)
! bc_out(s)%canopy_fraction_pa(ifp) is the area fraction
! the site's total ground area that is occupied by the
! area footprint of the current patch's vegetation canopy