-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeTRSimulation.F90
1805 lines (1563 loc) · 72.2 KB
/
BeTRSimulation.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 BeTRSimulation
!
! !DESCRIPTION:
! BeTR simulation base class.
!
! BeTR simulation class are API definitions, mapping data
! structures from a specific LSM, e.g. CLM, ALM, into BeTR data
! structures.
!
use VegetationDataType, only : veg_es, veg_wf
use ColumnDataType , only : col_es, col_ws, col_wf
use abortutils , only : endrun
use elm_varctl , only : iulog, use_cn
use shr_log_mod , only : errMsg => shr_log_errMsg
use shr_kind_mod , only : r8 => shr_kind_r8
use tracer_varcon , only : betr_nlevsoi, betr_nlevsno, betr_nlevtrc_soil
use BeTR_decompMod , only : betr_bounds_type
use decompMod , only : bounds_type
#if (defined SBETR)
use PatchType , only : patch_type
use ColumnType , only : column_type
use LandunitType , only : landunit_type
#else
use ColumnType , only : column_type => column_physical_properties
use VegetationType , only : patch_type => vegetation_physical_properties
use LandunitType , only : landunit_type => landunit_physical_properties
#endif
! !USES:
use BetrType , only : betr_type, create_betr_type
use betr_ctrl , only : max_betr_hist_type, betr_offline, max_betr_rest_type, biulog
use betr_constants , only : betr_string_length
use betr_constants , only : betr_filename_length
use betr_regression_module , only : betr_regression_type
use BeTR_biogeophysInputType , only : betr_biogeophys_input_type, create_betr_biogeophys_input
use BeTR_biogeoStateType , only : betr_biogeo_state_type, create_betr_biogeo_state
use BeTR_biogeoFluxType , only : betr_biogeo_flux_type, create_betr_biogeoFlux
use BeTR_TimeMod , only : betr_time_type
use betr_varcon , only : betr_maxpatch_pft
use BetrStatusType , only : betr_status_type, create_betr_status_type
use BetrStatusSimType , only : betr_status_sim_type, create_betr_status_sim_type
use betr_columnType , only : betr_column_type, create_betr_column_type
use betr_patchType , only : betr_patch_type, create_betr_patch_type
use betr_varcon , only : spval => bspval
use BeTRHistVarType , only : betr_hist_var_type
implicit none
private
character(len=*), private, parameter :: mod_filename = &
__FILE__
type, public :: betr_simulation_type
type(betr_type) , public, pointer :: betr(:) => null()
type(betr_biogeophys_input_type) , public, pointer :: biophys_forc(:) => null()
type(betr_biogeo_state_type) , public, pointer :: biogeo_state(:) => null()
type(betr_biogeo_flux_type) , public, pointer :: biogeo_flux(:) => null()
type(betr_status_type) , public, pointer :: bstatus(:) => null()
type(betr_status_sim_type) , public, pointer :: bsimstatus
logical , public, pointer :: active_col(:) => null()
type(betr_column_type) , public, pointer :: betr_col(:) => null()
type(betr_patch_type) , public, pointer :: betr_pft(:) => null()
character(len=betr_filename_length) , private :: base_filename
character(len=betr_filename_length) , private :: hist_filename
type(betr_regression_type), private :: regression
type(betr_time_type), public :: betr_time
integer, public :: num_soilp
integer, public, allocatable :: filter_soilp(:)
integer, public :: num_jtops
integer, public, allocatable :: jtops(:)
integer, public :: num_soilc
integer, public, allocatable :: filter_soilc(:)
type(betr_hist_var_type), allocatable :: state_hist1d_var(:)
type(betr_hist_var_type), allocatable :: state_hist2d_var(:)
type(betr_hist_var_type), allocatable :: flux_hist1d_var(:)
type(betr_hist_var_type), allocatable :: flux_hist2d_var(:)
integer :: num_hist_state1d
integer :: num_hist_state2d
integer :: num_hist_flux1d
integer :: num_hist_flux2d
integer :: num_rest_state1d
integer :: num_rest_state2d
real(r8), pointer :: hist_states_2d(:,:,:)
real(r8), pointer :: hist_states_1d(:,:)
real(r8), pointer :: rest_states_2d(:,:,:)
real(r8), pointer :: rest_states_1d(:,:)
real(r8), pointer :: hist_fluxes_1d(:,:)
real(r8), pointer :: hist_fluxes_2d(:,:,:)
logical, public :: active_soibgc
! FIXME(bja, 201603) most of these types should be private!
! NOTE(bja, 201603) BeTR types only, no LSM specific types here!
contains
procedure, public :: BeTRInit
procedure, public :: BeTRSetFilter
procedure, public :: SetClock
procedure, public :: InitOnline => BeTRSimulationInit
procedure, public :: Init => BeTRSimulationInitOffline
procedure, public :: ConsistencyCheck => BeTRSimulationConsistencyCheck
procedure, public :: PreDiagSoilColWaterFlux => BeTRSimulationPreDiagSoilColWaterFlux
procedure, public :: DiagnoseDtracerFreezeThaw=> BeTRSimulationDiagnoseDtracerFreezeThaw
procedure, public :: DiagAdvWaterFlux => BeTRSimulationDiagAdvWaterFlux
procedure, public :: DiagDrainWaterFlux => BeTRSimulationDiagDrainWaterFlux
procedure, public :: BeginSnowLayerAdjst => BeTRSimulationBeginTracerSnowLayerAdjst
procedure, public :: EndSnowLayerAdjst => BeTRSimulationEndTracerSnowLayerAdjst
procedure, public :: CombineSnowLayers => BeTRSimulationCombineSnowLayers
procedure, public :: DvideSnowLayers => BeTRSimulationDvideSnowLayers
procedure, public :: StepWithoutDrainage => BeTRSimulationStepWithoutDrainage
procedure, public :: StepWithDrainage => BeTRSimulationStepWithDrainage
procedure, public :: BeginMassBalanceCheck => BeTRSimulationBeginMassBalanceCheck
procedure, public :: MassBalanceCheck => BeTRSimulationMassBalanceCheck
procedure, public :: BeTRSetBiophysForcing => BeTRSimulationSetBiophysForcing
procedure, public :: RetrieveBiogeoFlux => BeTRSimulationRetrieveBiogeoFlux
procedure, public :: DiagnoseLnd2atm => BeTRSimulationDiagnoseLnd2atm
procedure, public :: CreateOfflineHistory => hist_htapes_create
procedure, public :: WriteOfflineHistory => hist_write
procedure, public :: WriteRegressionOutput
!the following are used to interact with lsm
procedure, public :: do_soibgc
procedure, public :: BeTRRestart => BeTRSimulationRestart
procedure, public :: BeTRRestartOffline => BeTRSimulationRestartOffline
procedure, public :: BeTRRestartOpen => BeTRSimulationRestartOpen
procedure, public :: BeTRRestartClose => BeTRSimulationRestartClose
procedure, private :: BeTRCreateHistory => BeTRSimulationCreateHistory
procedure, public :: HistRetrieval => BeTRSimulationHistRetrieval
procedure, private :: BeTRRetrieveHistoryState => BeTRSimulationRetrieveHistoryState
procedure, private :: BeTRRetrieveHistoryFlux => BeTRSimulationRetrieveHistoryFlux
procedure, public :: BeTRSetcps => BeTRSimulationSetcps
procedure, public :: BeTRSetBounds => BeTRSimulationSetBounds
procedure, private:: hist_create_states
procedure, private:: hist_create_fluxes
procedure, private:: hist_output_states
procedure, private:: hist_output_fluxes
procedure, private:: RestAlloc => BeTRSimulationRestartAlloc
procedure, private:: HistAlloc => BeTRSimulationHistoryAlloc
end type betr_simulation_type
public :: BeTRSimulationInit
contains
subroutine SetClock(this, dtime, nelapstep)
implicit none
class(betr_simulation_type) , intent(inout) :: this
real(r8), intent(in) :: dtime
integer, intent(in) :: nelapstep
call this%betr_time%setClock(dtime, nelapstep)
end subroutine SetClock
!-------------------------------------------------------------------------------
subroutine BeTRSimulationInit(this, bounds, lun, col, pft, waterstate, namelist_buffer, masterproc)
!
! DESCRIPTIONS
! Dummy routine for inheritance purposes. don't use.
!
!USES
use WaterstateType , only : waterstate_type
use betr_constants , only : betr_namelist_buffer_size, betr_filename_length
implicit none
class(betr_simulation_type) , intent(inout) :: this
type(landunit_type) , intent(in) :: lun
type(column_type) , intent(in) :: col
type(patch_type) , intent(in) :: pft
type(bounds_type) , intent(in) :: bounds
character(len=*) , intent(in) :: namelist_buffer
type(waterstate_type) , intent(inout) :: waterstate
logical, optional , intent(in) :: masterproc
character(len=*), parameter :: subname = 'BeTRSimulationInit'
call endrun(msg="ERROR "//subname//" unimplemented. "//errmsg(mod_filename, __LINE__))
if (this%num_soilc > 0) continue
if (bounds%begc > 0) continue
if (size(waterstate%h2osoi_liq_col) > 0) continue
if (len(namelist_buffer) > 0) continue
end subroutine BeTRSimulationInit
!-------------------------------------------------------------------------------
subroutine BeTRSimulationInitOffline(this, bounds, lun, col, pft, waterstate, &
namelist_buffer, base_filename)
!
! DESCRIPTIONS
! Dummy routine for inheritance purposes. don't use.
!
!USES
use WaterstateType , only : waterstate_type
use betr_constants , only : betr_namelist_buffer_size, betr_filename_length
implicit none
class(betr_simulation_type) , intent(inout) :: this
type(landunit_type) , intent(in) :: lun
type(column_type) , intent(in) :: col
type(patch_type) , intent(in) :: pft
type(bounds_type) , intent(in) :: bounds
type(waterstate_type) , intent(inout) :: waterstate
character(len=*) , intent(in) :: namelist_buffer
character(len=*) , intent(in) :: base_filename
character(len=*), parameter :: subname = 'BeTRSimulationInit'
call endrun(msg="ERROR "//subname//" unimplemented. "//errmsg(mod_filename, __LINE__))
if (this%num_soilc > 0) continue
if (bounds%begc > 0) continue
if (size(waterstate%h2osoi_liq_col) > 0) continue
if (len(base_filename) > 0) continue
if (len(namelist_buffer) > 0) continue
call this%betr_time%Init(namelist_buffer)
end subroutine BeTRSimulationInitOffline
!-------------------------------------------------------------------------------
subroutine BeTRSetFilter(this, maxpft_per_col, boffline)
!
!DESCRIPTION
! set betr filter, only used for standalone applicaitons
use betr_ctrl , only : betr_offline
implicit none
!ARGUMENTS
class(betr_simulation_type), intent(inout) :: this
integer, intent(in) :: maxpft_per_col
logical, intent(in) :: boffline
integer :: p
!by default, surface litter layer is off
this%num_jtops = 1
allocate(this%jtops(this%num_jtops))
this%jtops(:) = 1
this%num_soilc = 1
allocate(this%filter_soilc(this%num_soilc))
this%filter_soilc(:) = 1
this%num_soilp = maxpft_per_col
allocate(this%filter_soilp(this%num_soilp))
do p = 1, maxpft_per_col
this%filter_soilp(p) = p
enddo
betr_offline=boffline
betr_maxpatch_pft = maxpft_per_col
end subroutine BeTRSetFilter
!-------------------------------------------------------------------------------
subroutine BeTRInit(this, bounds, lun, col, pft, waterstate, namelist_buffer, &
base_filename, masterproc)
!
! DESCRIPTION
! initialize BeTR
!
!!USES
use WaterStateType , only : waterstate_type
use betr_constants , only : betr_namelist_buffer_size
use betr_constants , only : betr_filename_length
use betr_varcon , only : betr_maxpatch_pft
use landunit_varcon, only : istsoil, istcrop
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
type(landunit_type) , intent(in) :: lun
type(column_type) , intent(in) :: col
type(patch_type) , intent(in) :: pft
type(waterstate_type) , intent(in) :: waterstate
character(len=*) , intent(in) :: namelist_buffer
character(len=*) , optional, intent(in) :: base_filename
logical, optional , intent(in) :: masterproc
!TEMPORARY VARIABLES
character(len=*), parameter :: subname = 'BeTRInit'
type(betr_bounds_type) :: betr_bounds
integer :: c, l
logical :: asoibgc
!print*,'base_filename',trim(base_filename)
biulog = iulog
if(present(base_filename))then
this%base_filename = base_filename
else
this%base_filename = ''
endif
if(present(masterproc))then
call this%betr_time%Init(namelist_buffer, masterproc)
else
call this%betr_time%Init(namelist_buffer)
endif
!allocate memory
!allocate(this%betr(bounds%begc:bounds%endc))
call this%bsimstatus%reset()
!grid horizontal bounds
call this%BeTRSetBounds(betr_bounds)
call this%BeTRSetcps(bounds, col, pft)
call this%BeTRSetBiophysForcing(bounds, col, pft, betr_bounds%lbj, betr_bounds%ubj, &
waterstate_vars = waterstate)
end subroutine BeTRInit
!---------------------------------------------------------------------------------
subroutine BeTRSimulationRestartAlloc(this, bounds)
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer :: begc, endc
begc = bounds%begc; endc=bounds%endc
allocate(this%rest_states_1d(begc:endc, 1:this%num_rest_state1d))
this%rest_states_1d(:,:)=spval
allocate(this%rest_states_2d(begc:endc, 1:betr_nlevtrc_soil, 1:this%num_rest_state2d))
this%rest_states_2d(:,:,:)=spval
end subroutine BeTRSimulationRestartAlloc
!---------------------------------------------------------------------------------
subroutine BeTRSimulationHistoryAlloc(this, bounds)
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer :: begc, endc
begc = bounds%begc; endc=bounds%endc
!state variables
allocate(this%state_hist1d_var(this%num_hist_state1d))
allocate(this%state_hist2d_var(this%num_hist_state2d))
allocate(this%hist_states_2d(begc:endc, 1:betr_nlevtrc_soil, 1:this%num_hist_state2d))
allocate(this%hist_states_1d(begc:endc, 1:this%num_hist_state1d))
!flux variables
allocate(this%flux_hist1d_var(this%num_hist_flux1d))
allocate(this%flux_hist2d_var(this%num_hist_flux2d))
allocate(this%hist_fluxes_2d(begc:endc, 1:betr_nlevtrc_soil, 1:this%num_hist_flux2d))
allocate(this%hist_fluxes_1d(begc:endc, 1:this%num_hist_flux1d))
end subroutine BeTRSimulationHistoryAlloc
!---------------------------------------------------------------------------------
subroutine BeTRSimulationRestartOpen(this, fname, flag, ncid)
!
!! DESCRIPTION
! open restart file, note it is only used in the stand alone mode
! note
! !USES:
use bncdio_pio, only : file_desc_t, ncd_nowrite, ncd_pio_openfile, ncd_pio_createfile
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
character(len=*), intent(in) :: fname
type(file_desc_t) , intent(out) :: ncid ! netcdf id
character(len=*), intent(in) :: flag
integer :: c
print*,'open restart file ',trim(fname), ' for ',trim(flag)
if(trim(flag)=='read')then
call ncd_pio_openfile(ncid, trim(fname), ncd_nowrite)
elseif(trim(flag)=='write')then
call ncd_pio_createfile(ncid, trim(fname))
endif
print*,'creating file succeeded'
end subroutine BeTRSimulationRestartOpen
!---------------------------------------------------------------------------------
subroutine BeTRSimulationRestartClose(this, ncid)
!
!! DESCRIPTION
! initialize for restart run
! !USES:
use bncdio_pio, only : file_desc_t, ncd_pio_closefile
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(file_desc_t) , intent(inout) :: ncid ! netcdf id
call ncd_pio_closefile(ncid)
end subroutine BeTRSimulationRestartClose
!---------------------------------------------------------------------------------
subroutine BeTRSimulationStepWithoutDrainage(this, bounds, col, pft)
!DESCRPTION
!interface for StepWithoutDrainage
!
! USES
use SoilStateType , only : soilstate_type
use WaterStateType , only : Waterstate_Type
use TemperatureType , only : temperature_type
use ChemStateType , only : chemstate_type
use WaterfluxType , only : waterflux_type
use atm2lndType , only : atm2lnd_type
use SoilHydrologyType , only : soilhydrology_type
use CNCarbonFluxType , only : carbonflux_type
use CanopyStateType , only : canopystate_type
use BeTR_TimeMod , only : betr_time_type
use pftvarcon , only : crop
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds ! bounds
type(column_type) , intent(in) :: col ! column type
type(patch_type) , intent(in) :: pft
! remove compiler warnings about unused dummy args
if (this%num_soilc > 0) continue
if (this%betr_time%tstep > 0) continue
if (bounds%begc > 0) continue
if (size(col%z) > 0) continue
end subroutine BeTRSimulationStepWithoutDrainage
!---------------------------------------------------------------------------------
subroutine BeTRSimulationDiagnoseLnd2atm(this, bounds, col, lnd2atm_vars)
!
!DESCRIPTION
!interface for using diagnose land fluxes to atm and river copmonents
!
!USES
use MathfuncMod , only : safe_div
use lnd2atmType , only : lnd2atm_type
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
type(column_type) , intent(in) :: col ! column type
type(lnd2atm_type) , intent(inout) :: lnd2atm_vars
! remove compiler warnings about unused dummy args
if (this%num_soilc > 0) continue
if (bounds%begc > 0) continue
if (size(col%z) > 0) continue
end subroutine BeTRSimulationDiagnoseLnd2atm
!---------------------------------------------------------------------------------
subroutine BeTRSimulationStepWithDrainage(this, bounds, col)
!
!DESCRIPTION
!interface for using StepWithDrainage
!
!USES
use MathfuncMod , only : safe_div
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
type(column_type) , intent(in) :: col ! column type
! remove compiler warnings about unused dummy args
if (this%num_soilc > 0) continue
if (bounds%begc > 0) continue
if (size(col%z) > 0) continue
end subroutine BeTRSimulationStepWithDrainage
!---------------------------------------------------------------------------------
subroutine BeTRSimulationBeginMassBalanceCheck(this, bounds)
!DESCRIPTION
!stage tracer mass balance check
!
!USES
use TracerBalanceMod, only : begin_betr_tracer_massbalance
implicit none
!ARGUMENTS
class(betr_simulation_type), intent(inout) :: this
type(bounds_type), intent(in) :: bounds
!TEMPORARY VARIABLES
type(betr_bounds_type) :: betr_bounds
integer :: lbj, ubj
integer :: c
!set lbj and ubj
call this%BeTRSetBounds(betr_bounds)
do c = bounds%begc, bounds%endc
if(.not. this%active_col(c))cycle
call begin_betr_tracer_massbalance(betr_bounds, &
this%betr_col(c), this%num_soilc, this%filter_soilc, &
this%betr(c)%tracers, this%betr(c)%tracerstates, &
this%betr(c)%tracerfluxes, this%bstatus(c))
if(this%bstatus(c)%check_status())then
call this%bsimstatus%setcol(c)
call this%bsimstatus%set_msg(this%bstatus(c)%print_msg(),this%bstatus(c)%print_err())
exit
endif
enddo
if(this%bsimstatus%check_status()) &
call endrun(msg=trim(this%bsimstatus%print_msg()))
end subroutine BeTRSimulationBeginMassBalanceCheck
!---------------------------------------------------------------------------------
subroutine BeTRSimulationMassBalanceCheck(this, bounds)
!DESCRIPTION
! do tracer mass balance check
!
!USES
use TracerBalanceMod , only : betr_tracer_massbalance_check
use BeTR_TimeMod , only : betr_time_type
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
!TEMPORARY VARIABLES
type(betr_bounds_type) :: betr_bounds
integer :: c
!set lbj and ubj
call this%BeTRSetBounds(betr_bounds)
do c = bounds%begc, bounds%endc
if(.not. this%active_col(c))cycle
call betr_tracer_massbalance_check(this%betr_time, betr_bounds, &
this%betr_col(c), this%num_soilc, this%filter_soilc, &
this%betr(c)%tracers, this%betr(c)%tracerstates, &
this%betr(c)%tracerfluxes, this%bstatus(c))
if(this%bstatus(c)%check_status())then
call this%bsimstatus%setcol(c)
call this%bsimstatus%set_msg(this%bstatus(c)%print_msg(),this%bstatus(c)%print_err())
exit
endif
enddo
if(this%bsimstatus%check_status()) then
print*,this%bsimstatus%cindex
print*,trim(this%bsimstatus%print_msg())
call endrun(msg=trim(this%bsimstatus%print_msg()))
endif
end subroutine BeTRSimulationMassBalanceCheck
!-------------------------------------------------------------------------------
subroutine hist_htapes_create(this, bounds, betr_nlevtrc_soil, &
num_hist_state1d, num_hist_state2d, num_hist_flux1d, num_hist_flux2d)
!
! DESCRIPTIONS
! create history file and define output variables, only for standalone applicaitons
!
! USES
use netcdf , only : nf90_float
use bncdio_pio , only : file_desc_t
use bncdio_pio , only : ncd_pio_createfile
use bncdio_pio , only : ncd_pio_closefile
use bncdio_pio , only : ncd_enddef
use bncdio_pio , only : ncd_defvar
use bncdio_pio , only : ncd_putvar
use bhistFileMod , only : hist_file_create, hist_def_fld1d, hist_def_fld2d
use betr_varcon , only : bspval
use betr_columnType , only : betr_column_type
!
!ARGUMENTS
implicit none
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds ! bounds
integer , intent(in) :: betr_nlevtrc_soil
integer , intent(in) :: num_hist_state1d
integer , intent(in) :: num_hist_state2d
integer , intent(in) :: num_hist_flux1d
integer , intent(in) :: num_hist_flux2d
!TEMPORARY VARIABLES
integer :: jj, kk, c
integer :: ncol
type(file_desc_t) :: ncid
character(len=*), parameter :: subname = 'hist_htapes_create'
c = 1
associate( &
ntracers => this%betr(c)%tracers%ntracers, &
ngwmobile_tracers => this%betr(c)%tracers%ngwmobile_tracers, &
is_volatile => this%betr(c)%tracers%is_volatile, &
is_h2o => this%betr(c)%tracers%is_h2o, &
is_isotope => this%betr(c)%tracers%is_isotope, &
volatileid => this%betr(c)%tracers%volatileid, &
tracernames => this%betr(c)%tracers%tracernames &
)
ncol = bounds%endc-bounds%begc + 1
this%hist_filename = trim(this%base_filename) // '.output.nc'
call ncd_pio_createfile(ncid, this%hist_filename)
call hist_file_create(ncid, betr_nlevtrc_soil, ncol)
call ncd_defvar(ncid, "ZSOI", nf90_float, &
dim1name="ncol",dim2name="levtrc", &
long_name="grid center" , &
units="m", missing_value=bspval, fill_value=bspval)
call hist_def_fld2d(ncid, varname="QFLX_ADV", nf90_type=nf90_float,dim1name="ncol", &
dim2name="levtrc",long_name="advective flux / velocity", units="m/s")
write(iulog,*)'hist_create_states'
call this%hist_create_states(bounds, betr_nlevtrc_soil, num_hist_state1d, num_hist_state2d, ncid=ncid)
write(iulog,*)'hist_create_fluxes'
call this%hist_create_fluxes(bounds, betr_nlevtrc_soil, num_hist_flux1d, num_hist_flux2d, ncid=ncid)
call ncd_enddef(ncid)
call ncd_putvar(ncid,"ZSOI",1,this%betr_col(c)%z(1:1,1:betr_nlevtrc_soil))
call ncd_pio_closefile(ncid)
end associate
end subroutine hist_htapes_create
!-------------------------------------------------------------------------------
subroutine hist_write(this, bounds, record, numf, filter, time_vars, velocity)
!
! DESCRIPTION
! output hist file, only for standalone applications
!
! USES
use bncdio_pio , only : file_desc_t
use bncdio_pio , only : ncd_pio_openfile_for_write
use bncdio_pio , only : ncd_putvar
use bncdio_pio , only : ncd_pio_closefile
use BeTR_TimeMod , only : betr_time_type
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer , intent(in) :: record
integer , intent(in) :: numf
integer , intent(in) :: filter(:)
type(betr_time_type) , intent(in) :: time_vars
real(r8) , intent(in) :: velocity(:, :)
!TEMPORARY VARIABLES
type(file_desc_t) :: ncid
integer :: jj
integer :: c
character(len=*), parameter :: subname='hist_write'
c = 1
associate( &
ntracers => this%betr(c)%tracers%ntracers, &
ngwmobile_tracers => this%betr(c)%tracers%ngwmobile_tracers, &
is_volatile => this%betr(c)%tracers%is_volatile, &
is_h2o => this%betr(c)%tracers%is_h2o, &
is_isotope => this%betr(c)%tracers%is_isotope, &
volatileid => this%betr(c)%tracers%volatileid, &
tracernames => this%betr(c)%tracers%tracernames &
)
call ncd_pio_openfile_for_write(ncid, this%hist_filename)
if (mod(time_vars%time, 86400._r8)==0) then
write(iulog,*)'day', time_vars%time/86400._r8
end if
call ncd_putvar(ncid, "time", record, time_vars%time)
do c = bounds%begc, bounds%endc
call ncd_putvar(ncid, 'QFLX_ADV', record, velocity(c:c, 1:betr_nlevtrc_soil))
enddo
call this%HistRetrieval(bounds, numf, filter)
call this%hist_output_states(ncid, record, bounds, numf, filter, betr_nlevtrc_soil, &
this%num_hist_state1d, this%num_hist_state2d)
call this%hist_output_fluxes(ncid, record, bounds, numf, filter, betr_nlevtrc_soil, &
this%num_hist_flux1d, this%num_hist_flux2d)
call ncd_pio_closefile(ncid)
end associate
end subroutine hist_write
!---------------------------------------------------------------------------------
subroutine BeTRSimulationHistRetrieval(this, bounds, numf, filter)
use tracer_varcon , only : betr_nlevtrc_soil
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer, intent(in) :: numf
integer, intent(in) :: filter(:)
call this%BeTRRetrieveHistoryState(bounds, numf, filter)
call this%BeTRRetrieveHistoryFlux(bounds, numf, filter)
end subroutine BeTRSimulationHistRetrieval
!---------------------------------------------------------------------------------
subroutine BeTRSimulationConsistencyCheck(this, &
bounds, ubj, num_soilc, filter_soilc, waterstate_vars)
! DESCRIPTION
! Do consistency check, can be overwritten for varies purpose
!
! USES
use WaterStateType, only : Waterstate_Type
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer , intent(in) :: num_soilc ! number of columns in column filter_soilc
integer , intent(in) :: filter_soilc(:) ! column filter_soilc
integer , intent(in) :: ubj
type(Waterstate_Type) , intent(in) :: waterstate_vars ! water state variables
! remove compiler warnings
if (this%num_soilc > 0) continue
if (bounds%begc > 0) continue
if (ubj > 0) continue
if (num_soilc > 0) continue
if (size(filter_soilc) > 0) continue
if (associated(col_ws%h2osoi_liq)) continue
end subroutine BeTRSimulationConsistencyCheck
!---------------------------------------------------------------------------------
subroutine WriteRegressionOutput(this, velocity)
!DESCRIPTION
! output for regression test
!
! USES
use bshr_kind_mod , only : r8 => shr_kind_r8
use betr_constants , only : betr_string_length
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
real(r8) , intent(in) :: velocity(:, :)
!TEMPORARY VARIABLES
integer :: jj, tt, begc, endc, c
character(len=betr_string_length) :: category
character(len=betr_string_length) :: name
integer :: loc_id
! FIXME(bja, 201603) should we output units as well...?
begc = 1
endc = 1
c= 1
if (this%regression%write_regression_output) then
call this%regression%OpenOutput()
! NOTE(bja, 201603) currently we are allocating all tracer
! state vars all the time.
do tt = 1, this%betr(c)%tracers%ntracers
if (tt <= this%betr(c)%tracers%ngwmobile_tracers) then
category = 'concentration'
name = trim(this%betr(c)%tracers%tracernames(tt)) // '_total_aqueous_conc'
call this%regression%WriteData(category, name, &
this%betr(c)%tracerstates%tracer_conc_mobile_col(begc, :, tt))
end if
if (tt <= this%betr(c)%tracers%nvolatile_tracers) then
if(.not. this%betr(c)%tracers%is_volatile(tt))cycle
category = 'pfraction'
loc_id=this%betr(c)%tracers%volatileid(tt)
name = trim(this%betr(c)%tracers%tracernames(tt)) // '_gas_partial_fraction'
call this%regression%WriteData(category, name, &
this%betr(c)%tracerstates%tracer_P_gas_frac_col(begc, :, loc_id))
end if
end do
name = 'total_gas_pressure'
category = 'pressure'
call this%regression%WriteData(category, name, &
this%betr(c)%tracerstates%tracer_P_gas_col(begc, :))
name = 'advective flux'
category = 'velocity'
call this%regression%WriteData(category, name, &
velocity(begc, :))
call this%regression%CloseOutput()
end if
end subroutine WriteRegressionOutput
!------------------------------------------------------------------------
subroutine BeTRSimulationSetBiophysForcing(this, bounds, col, pft, lbj, ubj, carbonflux_vars, waterstate_vars, &
waterflux_vars, temperature_vars, soilhydrology_vars, atm2lnd_vars, canopystate_vars, &
chemstate_vars, soilstate_vars)
!DESCRIPTION
!pass in biogeophysical variables for running betr
!USES
use SoilStateType , only : soilstate_type
use WaterStateType , only : Waterstate_Type
use TemperatureType , only : temperature_type
use ChemStateType , only : chemstate_type
use WaterfluxType , only : waterflux_type
use atm2lndType , only : atm2lnd_type
use SoilHydrologyType , only : soilhydrology_type
use CNCarbonFluxType , only : carbonflux_type
use CanopyStateType , only : canopystate_type
use MathfuncMod , only : isnan => bisnan
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
type(patch_type) , intent(in) :: pft
type(column_type) , intent(in) :: col ! column type
integer , intent(in) :: lbj, ubj
type(carbonflux_type) , optional, intent(in) :: carbonflux_vars
type(Waterstate_Type) , optional, intent(in) :: Waterstate_vars
type(waterflux_type) , optional, intent(in) :: waterflux_vars
type(temperature_type) , optional, intent(in) :: temperature_vars
type(soilhydrology_type) , optional, intent(in) :: soilhydrology_vars
type(atm2lnd_type) , optional, intent(in) :: atm2lnd_vars
type(canopystate_type) , optional, intent(in) :: canopystate_vars
type(chemstate_type) , optional, intent(in) :: chemstate_vars
type(soilstate_type) , optional, intent(in) :: soilstate_vars
!TEMPORARY VARIABLES
integer :: p, pi, cc, c, l, pp
integer :: npft_loc
cc = 1
do c = bounds%begc, bounds%endc
if(.not. this%active_col(c))cycle
if(present(carbonflux_vars))then
npft_loc = ubound(carbonflux_vars%annsum_npp_patch,1)-lbound(carbonflux_vars%annsum_npp_patch,1)+1
if(col%pfti(c) /= lbound(carbonflux_vars%annsum_npp_patch,1) .and. npft_loc/=col%npfts(c))then
do pi = 1, betr_maxpatch_pft
this%biophys_forc(c)%annsum_npp_patch(pi) = 0._r8
this%biophys_forc(c)%agnpp_patch(pi) = 0._r8
this%biophys_forc(c)%bgnpp_patch(pi) = 0._r8
enddo
else
if(use_cn)then
pp = 0
do pi = 1, betr_maxpatch_pft
if (pi <= col%npfts(c)) then
p = col%pfti(c) + pi - 1
if (pft%active(p)) then
pp = pp + 1
this%biophys_forc(c)%annsum_npp_patch(pp) = carbonflux_vars%annsum_npp_patch(p)
this%biophys_forc(c)%agnpp_patch(pp) = carbonflux_vars%agnpp_patch(p)
this%biophys_forc(c)%bgnpp_patch(pp) = carbonflux_vars%bgnpp_patch(p)
endif
endif
enddo
else
npft_loc = ubound(carbonflux_vars%annsum_npp_patch,1)-lbound(carbonflux_vars%annsum_npp_patch,1)+1
if(col%pfti(c) /= lbound(carbonflux_vars%annsum_npp_patch,1) .and. npft_loc/=col%npfts(c))then
do pi = 1, betr_maxpatch_pft
this%biophys_forc(c)%annsum_npp_patch(pi) = 0._r8
this%biophys_forc(c)%agnpp_patch(pi) = 0._r8
this%biophys_forc(c)%bgnpp_patch(pi) = 0._r8
enddo
endif
endif
endif
endif
!assign waterstate
if(present(waterstate_vars))then
this%biophys_forc(c)%finundated_col(cc) = col_ws%finundated(c)
this%biophys_forc(c)%frac_h2osfc_col(cc) = col_ws%frac_h2osfc(c)
this%biophys_forc(c)%h2osoi_liq_col(cc,lbj:ubj) = col_ws%h2osoi_liq(c,lbj:ubj)
this%biophys_forc(c)%h2osoi_ice_col(cc,lbj:ubj) = col_ws%h2osoi_ice(c,lbj:ubj)
this%biophys_forc(c)%h2osoi_liqvol_col(cc,lbj:ubj) = col_ws%h2osoi_liqvol(c,lbj:ubj)
this%biophys_forc(c)%h2osoi_icevol_col(cc,lbj:ubj) = col_ws%h2osoi_icevol(c,lbj:ubj)
this%biophys_forc(c)%h2osoi_vol_col(cc,lbj:ubj) = col_ws%h2osoi_vol(c,lbj:ubj)
this%biophys_forc(c)%air_vol_col(cc,lbj:ubj) = col_ws%air_vol(c,lbj:ubj)
this%biophys_forc(c)%rho_vap(cc,lbj:ubj) = waterstate_vars%rho_vap_col(c,lbj:ubj)
this%biophys_forc(c)%rhvap_soi(cc,lbj:ubj) = waterstate_vars%rhvap_soi_col(c,lbj:ubj)
this%biophys_forc(c)%smp_l_col(cc,lbj:ubj) = col_ws%smp_l(c,lbj:ubj)
endif
if(present(waterflux_vars))then
this%biogeo_flux(c)%qflx_infl_col(cc) = col_wf%qflx_infl(c)
this%biogeo_flux(c)%qflx_totdrain_col(cc) = col_wf%qflx_totdrain(c)
this%biogeo_flux(c)%qflx_gross_evap_soil_col(cc) = col_wf%qflx_gross_evap_soil(c)
this%biogeo_flux(c)%qflx_gross_infl_soil_col(cc) = col_wf%qflx_gross_infl_soil(c)
this%biophys_forc(c)%qflx_surf_col(cc) = col_wf%qflx_surf(c)
this%biophys_forc(c)%qflx_dew_grnd_col(cc) = col_wf%qflx_dew_grnd(c)
this%biophys_forc(c)%qflx_dew_snow_col(cc) = col_wf%qflx_dew_snow(c)
this%biophys_forc(c)%qflx_sub_snow_vol_col(cc) = col_wf%qflx_sub_snow_vol(c)
this%biophys_forc(c)%qflx_sub_snow_col(cc) = col_wf%qflx_sub_snow(c)
this%biophys_forc(c)%qflx_h2osfc2topsoi_col(cc) = col_wf%qflx_h2osfc2topsoi(c)
this%biophys_forc(c)%qflx_snow2topsoi_col(cc) = col_wf%qflx_snow2topsoi(c)
this%biophys_forc(c)%qflx_rootsoi_col(cc,lbj:ubj) = col_wf%qflx_rootsoi(c,lbj:ubj)*1.e-3_r8
this%biogeo_flux(c)%qflx_adv_col(cc,lbj-1:ubj) = col_wf%qflx_adv(c,lbj-1:ubj)
this%biogeo_flux(c)%qflx_drain_vr_col(cc,lbj:ubj) = col_wf%qflx_drain_vr(c,lbj:ubj)
pp = 0
do pi = 1, betr_maxpatch_pft
if (pi <= col%npfts(c)) then
p = col%pfti(c) + pi - 1
if (pft%active(p)) then
pp = pp + 1
this%biophys_forc(c)%qflx_tran_veg_patch(pp) = veg_wf%qflx_tran_veg(p)
this%biophys_forc(c)%qflx_rootsoi_frac_patch(pp,lbj:ubj) = veg_wf%qflx_rootsoi_frac(p,lbj:ubj)
endif
endif
enddo
endif
if(present(temperature_vars))then
this%biophys_forc(c)%t_soi_10cm(cc) = col_es%t_soi10cm(c)
this%biophys_forc(c)%t_soisno_col(cc,lbj:ubj) = col_es%t_soisno(c,lbj:ubj)
pp = 0
do pi = 1, betr_maxpatch_pft
if (pi <= col%npfts(c)) then
p = col%pfti(c) + pi - 1
if (pft%active(p)) then
pp = pp + 1
this%biophys_forc(c)%t_veg_patch(pp) = veg_es%t_veg(p)
endif
endif
enddo
endif
if(present(soilhydrology_vars))then
this%biophys_forc(c)%qflx_bot_col(cc) = soilhydrology_vars%qcharge_col(c)
this%biophys_forc(c)%fracice_col(cc,lbj:ubj) = soilhydrology_vars%fracice_col(c,lbj:ubj)
endif
if(present(atm2lnd_vars))then
this%biophys_forc(c)%forc_pbot_downscaled_col(cc) = atm2lnd_vars%forc_pbot_downscaled_col(c)
this%biophys_forc(c)%forc_t_downscaled_col(cc) = atm2lnd_vars%forc_t_downscaled_col(c)
endif
if(present(canopystate_vars))then
this%biophys_forc(c)%altmax_col(cc) = canopystate_vars%altmax_col(c)
this%biophys_forc(c)%altmax_lastyear_col(cc) = canopystate_vars%altmax_lastyear_col(c)
pp = 0
do pi = 1, betr_maxpatch_pft
if (pi <= col%npfts(c)) then
p = col%pfti(c) + pi - 1
if (pft%active(p)) then
pp = pp + 1
this%biophys_forc(c)%lbl_rsc_h2o_patch(pp) = canopystate_vars%lbl_rsc_h2o_patch(p)
this%biophys_forc(c)%elai_patch(pp) = canopystate_vars%elai_patch(p)
endif
endif
enddo
endif
if(present(chemstate_vars))then
this%biophys_forc(c)%soil_pH(cc,lbj:ubj) = chemstate_vars%soil_pH(c,lbj:ubj)
endif
if(present(soilstate_vars))then
this%biophys_forc(c)%bsw_col(cc,lbj:ubj) = soilstate_vars%bsw_col(c,lbj:ubj)
this%biophys_forc(c)%watsat_col(cc,lbj:ubj) = soilstate_vars%watsat_col(c,lbj:ubj)
this%biophys_forc(c)%eff_porosity_col(cc,lbj:ubj) = soilstate_vars%eff_porosity_col(c,lbj:ubj)
this%biophys_forc(c)%cellorg_col(cc,lbj:ubj) = soilstate_vars%cellorg_col(c,lbj:ubj)
this%biophys_forc(c)%cellclay_col(cc,lbj:ubj) = soilstate_vars%cellclay_col(c,lbj:ubj)
this%biophys_forc(c)%cellsand_col(cc,lbj:ubj) = soilstate_vars%cellsand_col(c,lbj:ubj)
this%biophys_forc(c)%bd_col(cc,lbj:ubj) = soilstate_vars%bd_col(c,lbj:ubj)
this%biophys_forc(c)%watfc_col(cc,lbj:ubj) = soilstate_vars%watfc_col(c,lbj:ubj)
this%biophys_forc(c)%sucsat_col(cc,lbj:ubj) = soilstate_vars%sucsat_col(c,lbj:ubj)
pp = 0
do pi = 1, betr_maxpatch_pft
if (pi <= col%npfts(c)) then
p = col%pfti(c) + pi - 1
if (pft%active(p)) then
pp = pp + 1
this%biophys_forc(c)%rootfr_patch(pp,lbj:ubj) = soilstate_vars%rootfr_patch(p,lbj:ubj)
endif
endif
enddo
endif
enddo
end subroutine BeTRSimulationSetBiophysForcing
!------------------------------------------------------------------------
subroutine BeTRSimulationRetrieveBiogeoFlux(this, bounds, lbj,ubj, carbonflux_vars, &
waterflux_vars)
! DESCRIPTIONS
! update and return fluxes, this eventually will be expanded to
! include other fluxes
! USES
use WaterfluxType , only : waterflux_type
use CNCarbonFluxType , only : carbonflux_type
implicit none
!ARGUMENTS
class(betr_simulation_type) , intent(inout) :: this
type(bounds_type) , intent(in) :: bounds
integer , intent(in) :: lbj, ubj
type(carbonflux_type) , optional, intent(inout) :: carbonflux_vars
type(waterflux_type) , optional, intent(inout) :: waterflux_vars
integer :: begp, begc, endp, endc
integer :: p, c, cc
cc = 1
if(present(carbonflux_vars))then
!do nothing
endif
if(present(waterflux_vars))then
do c = bounds%begc, bounds%endc
if(.not. this%active_col(c))cycle
col_wf%qflx_infl(c) = this%biogeo_flux(c)%qflx_infl_col(cc)
col_wf%qflx_adv(c,lbj-1:ubj) = this%biogeo_flux(c)%qflx_adv_col(cc,lbj-1:ubj)
col_wf%qflx_totdrain(c) = this%biogeo_flux(c)%qflx_totdrain_col(cc)
col_wf%qflx_gross_evap_soil(c) = this%biogeo_flux(c)%qflx_gross_evap_soil_col(cc)
col_wf%qflx_gross_infl_soil(c) = this%biogeo_flux(c)%qflx_gross_infl_soil_col(cc)
col_wf%qflx_drain_vr(c,1:ubj) = this%biogeo_flux(c)%qflx_drain_vr_col(cc,1:ubj)
enddo
endif
end subroutine BeTRSimulationRetrieveBiogeoFlux
!------------------------------------------------------------------------
subroutine BeTRSimulationPreDiagSoilColWaterFlux(this, num_nolakec, filter_nolakec)
!DESCRIPTION
!prepare for water flux diagnosis. it is called before diagnosing the advective fluxes and applying
!freeze-thaw tracer partition.
!
!USES