-
Notifications
You must be signed in to change notification settings - Fork 0
/
RRGMod.F90
1696 lines (1396 loc) · 62.1 KB
/
RRGMod.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
#include "MAPL_Generic.h"
!=============================================================================
!BOP
! !MODULE: RRG_GridCompMod
! !INTERFACE:
module RRG_GridCompMod
! !USES:
use ESMF
use MAPL
use types_mod
use global_mod
use utils_mod
use geos_simplephotolysismod
use diagnostics
implicit none
private
integer, parameter :: DP=kind(1.0d0)
integer :: status ! module-wide scope
! Users can add new species to the system w/o
! having to add new code other than here in the main interface module.
integer, save :: nCH4, nCO2, nCO, nTR ! number of instances
type(gas_instance), pointer :: CO2(:) => null()
type(gas_instance), pointer :: CO(:) => null()
type(gas_instance), pointer :: CH4(:) => null()
type(gas_instance), pointer :: TR(:) => null() ! passive tracer that can carry anything
! !PUBLIC MEMBER FUNCTIONS:
PUBLIC SetServices
! !DESCRIPTION: This module implements the atmospheric component of the global
! carbon cycle within the NASA GEOS modeling system using MAPL/ESMF
!
! For chem species C, the system simply solves dC/dt = P - L
! P (prod) and L (loss) are computed and the system is integrated.
! The only operator splitting is between surface fluxes and 3-D ops (chem). This
! is done with two run methods. GridCompRun1 does surface fluxes, and GridCompRun2
! does the chemistry.
!
! OPERATING ASSUMPTIONS:
! 1) Incoming surface fluxes are kg <species> m-2 s-1. e.g. kgCO2/m2/s
! 2)
! NOTES:
! 1) TOTAL/AGGREGATE FIELDS: Totals of CO2, CO & CH4 are NOT independent instances.
! They are used for diagnostics and coupling with other modules (e.g. grid comps)
! They are not directly modified by any process.
!
! !REVISION HISTORY:
! 02Dec2022 M.S.Long First pass
! 08Feb2023 M.S.Long Preliminary tests against GOCART CH4, CO & CO2. Passed.
! 10Feb2023 M.S.Long Convert operations from kg/kg to mol/mol. Species are
! advected as mol/mol (<-assuming this is appropriate)
!
!EOP
!===========================================================================
contains
!============================================================================
!BOP
! !IROUTINE: SetServices
! !INTERFACE:
subroutine SetServices ( GC, RC )
! !ARGUMENTS:
type (ESMF_GridComp), intent(INOUT) :: GC ! gridded component
integer, intent( OUT) :: RC ! return code
! DESCRIPTION:
! !REVISION HISTORY:
! 02Dec2022 M.S.Long First pass
!EOP
!============================================================================
! !Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
type (ESMF_Config) :: cfg
type (ESMF_Config) :: universal_cfg
character (len=255) :: name
integer :: i,j,n
logical :: present, found
__Iam__('SetServices')
!****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet (GC, NAME=COMP_NAME, config=universal_cfg, __RC__)
Iam = trim(COMP_NAME) // '::' // Iam
!==============================================================
! S E T T H E I M P O R T S T A T E
! using include is just so much cleaner
#include "IMPORTS.h"
! ===============================================================
! S E T U P T H E E X P O R T S T A T E
! using include is just so much cleaner
#include "EXPORTS.h"
! ===============================================================
! ===============================================================
! R E A D C O N F I G A N D S E T U P I N S T A N C E S
!
! Load resource file
! -------------------
cfg = ESMF_ConfigCreate (__RC__)
call ESMF_ConfigLoadFile (cfg, 'RRG_GridComp.rc', rc=status)
if (status /= 0) then
if (mapl_am_i_root()) print*,'RRG_GridComp.rc does not exist!'
VERIFY_(STATUS)
end if
! Read & set cntrl object
! -----------------------
call ESMF_ConfigFindLabel(cfg,label='strictMassBalance:',isPresent=present,rc=status)
VERIFY_(STATUS)
if (present) then
call ESMF_ConfigGetAttribute(cfg,cntrl%strictMassBalance,rc=status)
VERIFY_(STATUS)
endif
call ESMF_ConfigFindLabel(cfg,label='wellMixedSurfaceExchange:',isPresent=present,rc=status)
VERIFY_(STATUS)
if (present) then
call ESMF_ConfigGetAttribute(cfg,cntrl%wellmixed_sfcexch,rc=status)
VERIFY_(STATUS)
endif
! Load instances
! --------------
call ProcessInstances( GC, Cfg, CO, 'CO', 28.0104, nCO, rc=status ); VERIFY_(status)
call ProcessInstances( GC, Cfg, CO2, 'CO2', 44.0098, nCO2, rc=status ); VERIFY_(status)
call ProcessInstances( GC, Cfg, CH4, 'CH4', 16.0422, nCH4, rc=status ); VERIFY_(status)
call ProcessInstances( GC, Cfg, TR, 'TR', 1.0000, nTR, rc=status ); VERIFY_(status)
if (nCO .gt. 0 .and. nCH4 .eq. 0) &
call MAPL_AddImportSpec(GC, &
SHORT_NAME = 'CO_CH4', &
LONG_NAME = 'source species', &
UNITS = '1', &
DIMS = MAPL_DimsHorzVert, &
VLOCATION = MAPL_VLocationCenter, &
RESTART = MAPL_RestartSkip, &
__RC__ )
! Fluxes
! ------
call RegisterFluxWithMAPL( GC, cfg, 'CO' , status ); VERIFY_(status)
call RegisterFluxWithMAPL( GC, cfg, 'CO2', status ); VERIFY_(status)
call RegisterFluxWithMAPL( GC, cfg, 'CH4', status ); VERIFY_(status)
call RegisterFluxWithMAPL( GC, cfg, 'TR', status ); VERIFY_(status)
! Set entry points
! ------------------------
call MAPL_GridCompSetEntryPoint (GC, ESMF_METHOD_INITIALIZE, Initialize, __RC__)
call MAPL_GridCompSetEntryPoint (GC, ESMF_METHOD_RUN, GridCompRun1, __RC__)
call MAPL_GridCompSetEntryPoint (GC, ESMF_METHOD_RUN, GridCompRun2, __RC__)
call MAPL_GridCompSetEntryPoint (GC, ESMF_METHOD_FINALIZE, Finalize, __RC__)
! Set generic services
! ----------------------------------
call MAPL_GenericSetServices (GC, __RC__)
RETURN_(ESMF_SUCCESS)
end subroutine SetServices
!============================================================================
!BOP
! !IROUTINE: Initialize
! !INTERFACE:
subroutine Initialize (GC, IMPORT, EXPORT, CLOCK, RC)
! !ARGUMENTS:
type (ESMF_GridComp), intent(inout) :: GC ! Gridded component
type (ESMF_State), intent(inout) :: IMPORT ! Import state
type (ESMF_State), intent(inout) :: EXPORT ! Export state
type (ESMF_Clock), intent(inout) :: CLOCK ! The clock
integer, optional, intent( out) :: RC ! Error code
! !DESCRIPTION:
! !REVISION HISTORY:
!EOP
!============================================================================
! !Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
character (len=255) :: name, photFile
type (MAPL_MetaComp), pointer :: MAPL
type (ESMF_Grid) :: grid
type (ESMF_State) :: internal
type (ESMF_Config) :: cfg, universal_cfg
type (ESMF_FieldBundle) :: Bundle_DP
type(ESMF_Field) :: field
integer :: i, n, dims(3)
type (MAPL_VarSpec), pointer :: INTERNALspec(:) ! This is used to access GC information, e.g. field names, etc.
__Iam__('Initialize')
!****************************************************************************
! Begin...
! Get the target components name and set-up traceback handle.
! -----------------------------------------------------------
call ESMF_GridCompGet (GC, grid=grid, name=COMP_NAME, config=universal_cfg, __RC__)
Iam = trim(COMP_NAME) // '::' //trim(Iam)
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC (GC, MAPL, __RC__)
! Get dimensions
! ---------------
call MAPL_GridGet (grid, localCellCountPerDim=dims, __RC__ )
params%im = dims(1)
params%jm = dims(2)
params%km = dims(3)
! Get DTs
! -------
call MAPL_GetResource(mapl, params%HDT, Label='RUN_DT:', __RC__)
call MAPL_GetResource(mapl, params%CDT, Label='GOCART_DT:', default=real(params%HDT), __RC__)
! Set some quantities
! -------------------
params%AVO = MAPL_AVOGAD*1e-3 ! Why is AVO in mcl/kmol?
params%AIRMW = MAPL_AIRMW
grav = MAPL_GRAV
params%RadToDeg = 180./MAPL_PI
! Load resource file
! ------------------
cfg = ESMF_ConfigCreate (__RC__)
call ESMF_ConfigLoadFile (cfg, 'RRG_GridComp.rc', rc=status)
if (status /= 0) then
if (mapl_am_i_root()) print*,'RRG_GridComp.rc does not exist!'
VERIFY_(STATUS)
end if
! Call Generic Initialize
! ----------------------------------------
call MAPL_GenericInitialize (GC, import, export, clock, __RC__)
! Get parameters from generic state.
! -----------------------------------
call MAPL_Get ( mapl, INTERNAL_ESMF_STATE = internal, &
LONS = params%LONS, & ! Radians
LATS = params%LATS, & ! Radians
INTERNALspec = INTERNALspec, &
__RC__ )
if (nCO .gt. 0) call ReadFluxTable( import, internal, cfg, 'CO', __RC__ )
if (nCO2 .gt. 0) call ReadFluxTable( import, internal, cfg, 'CO2', __RC__ )
if (nCH4 .gt. 0) call ReadFluxTable( import, internal, cfg, 'CH4', __RC__ )
if (nTR .gt. 0) call ReadFluxTable( import, internal, cfg, 'TR', __RC__ )
! Read Masks
! ----------
if (nCO .gt. 0) call ReadMasksTable( import, cfg, CO , __RC__ )
if (nCO2 .gt. 0) call ReadMasksTable( import, cfg, CO2, __RC__ )
if (nCH4 .gt. 0) call ReadMasksTable( import, cfg, CH4, __RC__ )
if (nTR .gt. 0) call ReadMasksTable( import, cfg, TR, __RC__ )
!DEBUG if (MAPL_am_I_root()) call util_dumpinstances()
!DEBUG if (MAPL_am_I_root()) call util_dumpfluxes()
! Set physical parameters
! - Henry's Law constants. Only needed for GEOS
! set to ZERO so that no scavenging happens in MOIST
do i=1,nCO
call ESMF_StateGet(internal, trim(CO(i)%name), field, __RC__)
call ESMF_AttributeSet(field, 'SetofHenryLawCts', (/0,0,0,0/), __RC__)
end do
do i=1,nCO2
call ESMF_StateGet(internal, trim(CO2(i)%name), field, __RC__)
call ESMF_AttributeSet(field, 'SetofHenryLawCts', (/0,0,0,0/), __RC__)
end do
do i=1,nCH4
call ESMF_StateGet(internal, trim(CH4(i)%name), field, __RC__)
call ESMF_AttributeSet(field, 'SetofHenryLawCts', (/0,0,0,0/), __RC__)
end do
do i=1,nTR
call ESMF_StateGet(internal, trim(TR(i)%name), field, __RC__)
call ESMF_AttributeSet(field, 'SetofHenryLawCts', (/0,0,0,0/), __RC__)
end do
call ESMF_ConfigFindLabel(cfg,label='photolysisFile:',rc=status)
VERIFY_(STATUS)
call ESMF_ConfigGetAttribute(cfg,photfile,rc=status)
VERIFY_(STATUS)
call readPhotTables( trim(photfile), params%km, status )
VERIFY_(STATUS)
! Mask to prevent emissions from the Great Lakes and the Caspian Sea
! ------------------------------------------------------------------
! allocate(self%deep_lakes_mask(ubound(lons, 1),ubound(lons, 2)), __STAT__)
! call deepLakesMask (lons, lats, real(MAPL_RADIANS_TO_DEGREES), self%deep_lakes_mask, __RC__)
RETURN_(ESMF_SUCCESS)
end subroutine Initialize
!============================================================================
!BOP
! !IROUTINE: GridCompRun1
! !INTERFACE:
subroutine GridCompRun1 (GC, import, export, clock, RC)
! !USES:
use surface_mod
use global_mod
use integration_mod
! !ARGUMENTS:
type (ESMF_GridComp), intent(inout) :: GC ! Gridded component
type (ESMF_State), intent(inout) :: import ! Import state
type (ESMF_State), intent(inout) :: export ! Export state
type (ESMF_Clock), intent(inout) :: clock ! The clock
integer, optional, intent( out) :: RC ! Error code:
! !DESCRIPTION: dC/dt = P - L
! P & L are computed for all species and integrated simultaneously
!EOP
!============================================================================
! !Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
type (MAPL_MetaComp), pointer :: mapl
type (ESMF_State) :: internal
type (ESMF_Grid) :: grid
type (MAPL_VarSpec), pointer :: INTERNALspec(:) ! This is used to access GC information, e.g. field names, etc. (MSL)
integer i,j,k,n
type(ESMF_Time) :: time
integer :: iyr, imm, idd, ihr, imn, isc
real, pointer :: CO2_total(:,:,:), CH4_total(:,:,:), CO_total(:,:,:), TR_total(:,:,:)
logical, save :: first = .true. ! I don't like using this but it has to happen. ExtData doesn't fill masks until run() and I don't want to repeat operations
real, pointer :: ptr2d(:,:), ptr3d(:,:,:)
type(ESMF_Alarm) :: ALARM
__Iam__('Run1')
!*****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet (GC, grid=grid, NAME=COMP_NAME, __RC__)
Iam = trim(COMP_NAME) //'::'// Iam
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC (GC, mapl, __RC__)
! Get parameters from generic state.
! -----------------------------------
call MAPL_Get (mapl, INTERNAL_ESMF_STATE=internal, INTERNALspec=INTERNALspec, __RC__)
call MAPL_Get (mapl, RUNALARM=ALARM, __RC__)
! Get current time
! -----------------------------------
call ESMF_ClockGet(CLOCK,currTIME=TIME,rc=STATUS)
VERIFY_(STATUS)
call ESMF_TimeGet(TIME ,YY=IYR, MM=IMM, DD=IDD, H=IHR, M=IMN, S=ISC, rc=STATUS)
VERIFY_(STATUS)
call MAPL_PackTime(params%NYMD,IYR,IMM,IDD)
call MAPL_PackTime(params%NHMS,IHR,IMN,ISC)
! ===============================================================
! G E T D A T A P O I N T E R S
! Associate the met fields
! -----------------------------------
call MAPL_GetPointer(import,met%area, 'AREA', __RC__) ! Grid box area, m^2
call MAPL_GetPointer(import,met%pblh, 'ZPBL', __RC__) ! pblh
call MAPL_GetPointer(import,met%zle, 'ZLE', __RC__) ! zle
call MAPL_GetPointer(import,met%ple, 'PLE', __RC__) ! ple
call MAPL_GetPointer(import,met%delp, 'DELP', __RC__) ! delp
call MAPL_GetPointer(import,met%qtot, 'QTOT', __RC__)
! Get the instance data pointers
do i=1,NINSTANCES
call MAPL_GetPointer(internal, instances(i)%p%data3d, trim(instances(i)%p%name), __RC__)
allocate( instances(i)%p%prod, instances(i)%p%loss, mold=instances(i)%p%data3d, __STAT__ ) ! allocate the prod/loss arrays for each instance
instances(i)%p%prod = 0.e0; instances(i)%p%loss = 0.e0
enddo
! Get pointers to the aggregates/totals
call MAPL_GetPointer(internal, aggregate(ispecies('CO2'))%q, 'CO2', notFoundOK=.TRUE., __RC__)
if (associated(aggregate(ispecies('CO2'))%q)) CO2_total => aggregate(ispecies('CO2'))%q ! Aggregate is used under the hood
call MAPL_GetPointer(internal, aggregate(ispecies('CO'))%q, 'CO' , notFoundOK=.TRUE., __RC__)
if (associated(aggregate(ispecies('CO'))%q)) CO_total => aggregate(ispecies('CO'))%q ! Aggregate is used under the hood
call MAPL_GetPointer(internal, aggregate(ispecies('CH4'))%q, 'CH4', notFoundOK=.TRUE., __RC__)
if (associated(aggregate(ispecies('CH4'))%q)) CH4_total => aggregate(ispecies('CH4'))%q ! Aggregate is used under the hood
! Even though TR_total does not make sense, adding it here because the code needs it, and this is an atypical use of RRG anyway
call MAPL_GetPointer(internal, aggregate(ispecies('TR'))%q, 'TR', notFoundOK=.TRUE., __RC__)
if (associated(aggregate(ispecies('TR'))%q)) TR_total => aggregate(ispecies('TR'))%q ! Aggregate is used under the hood
! ===============================================================
! ===============================================================
! P R O C E S S M A S K S
if (first) then
call ProcessExtdataMasks( IMPORT, __RC__ )
first = .false.
endif
! ===============================================================
! R U N T H E O P E R A T I O N S
! Aggregate instances into the totals prior to operations
call util_aggregate( RC )
VERIFY_(RC)
! Fill pointers for surface fluxes
if (allocated(sfc_flux)) call fillFluxes( import, sfc_flux, __RC__ )
! -- surface fluxes for all instances
call surface_prodloss( RC )
! -- integration
call integrate_forwardeuler( RC )
! -- post processing
if (cntrl%strictMassBalance) call util_accumulatenegatives( RC )
! Aggregate instances into the totals after operations
call util_aggregate( RC )
! ===============================================================
! ===============================================================
! C O M P U T E A N D P A S S D I A G N O S T I C S
! ===============================================================
! Set emission diagnostic
call MAPL_GetPointer( export, Ptr2d, 'CO2_EM', __RC__ )
if (associated(Ptr2d)) then
call diag_sfcflux( 'CO2', Ptr2d, RC )
Ptr2d => null()
endif
! Set emission diagnostic
call MAPL_GetPointer( export, Ptr2d, 'CH4_EM', __RC__ )
if (associated(Ptr2d)) then
call diag_sfcflux( 'CH4', Ptr2d, RC )
Ptr2d => null()
endif
! Set emission diagnostic
call MAPL_GetPointer( export, Ptr2d, 'CO_EM', __RC__ )
if (associated(Ptr2d)) then
call diag_sfcflux( 'CO', Ptr2d, RC )
Ptr2d => null()
endif
! Set emission diagnostic
call MAPL_GetPointer( export, Ptr2d, 'TR_EM', __RC__ )
if (associated(Ptr2d)) then
call diag_sfcflux( 'TR', Ptr2d, RC )
Ptr2d => null()
endif
!
!call MAPL_GetPointer( export, Ptr3D, 'CO2DRY', __RC__)
!if (associated(Ptr3d)) then
! Ptr3d = (CO2_total*MAPL_AIRMW/44.0098)/(1.e0 - met%qtot)
! Ptr3d => null()
!endif
!
!call MAPL_GetPointer( export, Ptr3D, 'CH4DRY', __RC__)
!if (associated(Ptr3d)) then
! Ptr3d = (CH4_total*MAPL_AIRMW/16.0422)/(1.e0 - met%qtot)
! Ptr3d => null()
!endif
!
!call MAPL_GetPointer( export, Ptr3D, 'CODRY', __RC__)
!if (associated(Ptr3d)) then
! Ptr3d = (CO_total*MAPL_AIRMW/28.0104)/(1.e0 - met%qtot)
! Ptr3d => null()
! endif
! ===============================================================
! D O N E
! Cleanup
! Lots of memory leak potential here. Need to be thorough
do i=1,NINSTANCES
! deallocate the prod/loss arrays for each instance
deallocate( instances(i)%p%prod, instances(i)%p%loss, __STAT__ )
instances(i)%p%data3d => null()
enddo
CO2_total => null(); CO_total => null(); CH4_total => null(); TR_total => null()
do i=1,nspecies
if (associated(aggregate(i)%q)) aggregate(i)%q => null()
enddo
if (allocated(sfc_flux)) then
do i=1,size(sfc_flux)
if (associated(sfc_flux(i)%flux)) sfc_flux(i)%flux => null()
enddo
endif
RETURN_(ESMF_SUCCESS)
end subroutine GridCompRun1
!============================================================================
!BOP
! !IROUTINE: GridCompRun2
! !INTERFACE:
subroutine GridCompRun2 (GC, import, export, clock, RC)
! !USES:
use surface_mod
use global_mod
use integration_mod
! Species chemistry modules
use COchem_mod
use CO2chem_mod
use CH4chem_mod
! !ARGUMENTS:
type (ESMF_GridComp), intent(inout) :: GC ! Gridded component
type (ESMF_State), intent(inout) :: import ! Import state
type (ESMF_State), intent(inout) :: export ! Export state
type (ESMF_Clock), intent(inout) :: clock ! The clock
integer, optional, intent( out) :: RC ! Error code:
! !DESCRIPTION: dC/dt = P - L
! P & L are computed for all species and integrated simultaneously
!EOP
!============================================================================
! !Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
type (MAPL_MetaComp), pointer :: mapl
type (ESMF_State) :: internal
type (ESMF_Grid) :: grid
type (MAPL_VarSpec), pointer :: INTERNALspec(:) ! This is used to access GC information, e.g. field names, etc. (MSL)
integer i,j,k,n
type(ESMF_Time) :: time
integer :: iyr, imm, idd, ihr, imn, isc
integer :: iCO2, iCH4, iCO, iTR
real, pointer :: CO2_total(:,:,:), CH4_total(:,:,:), CO_total(:,:,:), TR_total(:,:,:)
logical, save :: first = .true. ! I don't like using this but it has to happen. ExtData doesn't fill masks until run() and I don't want to repeat operations
real, pointer :: ptr2d(:,:), ptr3d(:,:,:), CO2ptr(:,:,:), CH4ptr(:,:,:), COptr(:,:,:), TRptr(:,:,:)
real, pointer, dimension(:,:,:) :: O3, OH, Cl, O1D
real(ESMF_KIND_R4), allocatable :: O3col(:,:,:), O2col(:,:,:), CO2photj(:,:,:), CH4photj(:,:,:)
real(ESMF_KIND_R4), allocatable :: ZTH(:,:)
real(ESMF_KIND_R4), allocatable :: SLR(:,:)
type (MAPL_SunOrbit) :: ORBIT
type(ESMF_Alarm) :: ALARM
real :: r, m
__Iam__('Run2')
!*****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet (GC, grid=grid, NAME=COMP_NAME, __RC__)
Iam = trim(COMP_NAME) //'::'// Iam
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC (GC, mapl, __RC__)
! Get parameters from generic state.
! -----------------------------------
call MAPL_Get (mapl, INTERNAL_ESMF_STATE=internal, INTERNALspec=INTERNALspec, __RC__)
call MAPL_Get (mapl, ORBIT=ORBIT, RUNALARM=ALARM, __RC__)
! Get current time
! -----------------------------------
call ESMF_ClockGet(CLOCK,currTIME=TIME,rc=STATUS)
VERIFY_(STATUS)
call ESMF_TimeGet(TIME ,YY=IYR, MM=IMM, DD=IDD, H=IHR, M=IMN, S=ISC, rc=STATUS)
VERIFY_(STATUS)
call MAPL_PackTime(params%NYMD,IYR,IMM,IDD)
call MAPL_PackTime(params%NHMS,IHR,IMN,ISC)
! ===============================================================
! G E T D A T A P O I N T E R S
! Associate the met fields
! -----------------------------------
call MAPL_GetPointer(import,met%pblh, 'ZPBL', __RC__)
call MAPL_GetPointer(import,met%T, 'T', __RC__)
call MAPL_GetPointer(import,met%zle, 'ZLE', __RC__)
call MAPL_GetPointer(import,met%ple, 'PLE', __RC__)
call MAPL_GetPointer(import,met%delp, 'DELP', __RC__)
call MAPL_GetPointer(import,met%q, 'Q', __RC__)
! call MAPL_GetPointer(import,met%qctot, 'QCTOT', __RC__)
call MAPL_GetPointer(import,met%qtot, 'QTOT', __RC__)
call MAPL_GetPointer(import,met%rho, 'AIRDENS', __RC__)
CALL MAPL_GetPointer(import, O3, 'O3', __RC__)
CALL MAPL_GetPointer(import, OH, 'RRG_OH', __RC__)
CALL MAPL_GetPointer(import, Cl, 'RRG_Cl', __RC__)
CALL MAPL_GetPointer(import, O1D, 'RRG_O1D', __RC__)
allocate( met%cosz(size(params%lats,1), size(params%lats,2)), __STAT__)
allocate( met%slr (size(params%lats,1), size(params%lats,2)), __STAT__)
allocate( O3col(params%im,params%jm,params%km), __STAT__)
allocate( O2col, CO2photj, CH4photj, mold=o3col, __STAT__)
CH4photj = 0.
! Get the instance data pointers
do i=1,NINSTANCES
call MAPL_GetPointer(internal, instances(i)%p%data3d, trim(instances(i)%p%name), __RC__)
allocate( instances(i)%p%prod, instances(i)%p%loss, mold=instances(i)%p%data3d, __STAT__ ) ! allocate the prod/loss arrays for each instance
instances(i)%p%prod = 0.e0; instances(i)%p%loss = 0.e0
enddo
! Get pointers to the aggregates/totals
! CO_total, CO2_total and CH4_total variables are made available for convenience.
call MAPL_GetPointer(internal, aggregate(ispecies('CO2'))%q, 'CO2', notFoundOK=.TRUE.,__RC__)
if (associated(aggregate(ispecies('CO2'))%q)) CO2_total => aggregate(ispecies('CO2'))%q ! Aggregate is used under the hood
call MAPL_GetPointer(internal, aggregate(ispecies('CO'))%q, 'CO' , notFoundOK=.TRUE.,__RC__)
if (associated(aggregate(ispecies('CO'))%q)) CO_total => aggregate(ispecies('CO'))%q ! Aggregate is used under the hood
call MAPL_GetPointer(internal, aggregate(ispecies('CH4'))%q, 'CH4', notFoundOK=.TRUE.,__RC__)
if (associated(aggregate(ispecies('CH4'))%q)) CH4_total => aggregate(ispecies('CH4'))%q ! Aggregate is used under the hood
call MAPL_GetPointer(internal, aggregate(ispecies('TR'))%q, 'TR', notFoundOK=.TRUE.,__RC__)
if (associated(aggregate(ispecies('TR'))%q)) TR_total => aggregate(ispecies('TR'))%q ! Aggregate is used under the hood
! ===============================================================
! ===============================================================
! S E T U P P H O T O L Y S I S
! Update solar zenith angle
! --------------------------
call MAPL_SunGetInsolation(params%lons, params%lats, orbit, met%cosz, met%slr, clock=clock, __RC__)
! Compute the O2 & O3 column
! -- adapted from GOCART
! --------------------------
! Below, I don't understand where '0.50' comes from (unless its for the average delp), and why a 1.e-4 factor is needed
! to make O2col work. This was transferred from CH4_GridCompMod.F90 in GOCART. -- MSL
r = MAPL_AVOGAD*0.50/(MAPL_GRAV*MAPL_AIRMW) ! r = Nsuba*0.50/(mwtAir*grav), copied from CFC_GridCompMod.F90 6.022e26 = mcl/kmole
m = MAPL_AIRMW/48.0e0 ! MW_air/MW_O3
O3col(:,:,1) = 1.1e11 + m*O3(:,:,1)* met%delp(:,:,1)*r ! 1.1e11 = O3 above 80km (cm-2); O3 is mmr, we need vmr for this so MW conversion is done here
O2col(:,:,1) = 7.072926E+19 + 0.20946 * met%delp(:,:,1)*r*1e-4 ! 7.072926E+19 = O2 above 80 km (cm-2); O2VMR = 0.20946
DO k=2,params%km
O3col(:,:,k) = O3col(:,:,k-1) + m*r*(O3(:,:,k-1)*met%delp(:,:,k-1) + O3(:,:,k)*met%delp(:,:,k))
O2col(:,:,k) = O2col(:,:,k-1) + r*0.20946*(met%delp(:,:,k-1)+met%delp(:,:,k))*1e-4
END DO
O3col = 0.
! Compute the photolysis rate for CO2 + hv -> CO + O*
met%photj = 0.e0
do k=1,params%km
do j=1,params%jm
do i=1,params%im
call CO2_photolysis_rate(i,j,k, params%km-k+1, met, O3col(i,j,k), CO2photj(i,j,k))
call CH4_photolysis_rate(i,j,k, met, O2col(i,j,k), 94.0, CH4photj(i,j,k))
enddo
enddo
enddo
! ===============================================================
! ===============================================================
! R U N T H E O P E R A T I O N S
! Aggregate instances into the totals prior to operations
call util_aggregate( RC )
! Compute prod/loss & integrate
! -- each species' chemistry
! -- CURRENTLY: OH, O1D and Cl are in mcl/cm3
! ==================================================
! This section allows species operations
! to use external sources for CO2 & CH4 in the
! case where a user disables a species (e.g. declares no instances of CH4)
! At this point, local species (CO, CH4 & CO2) are mol/mol.
! <<>> the commented code below is meant to enable CO to use
! RRG's CH4 in place of an offline field
! if (nCH4 .gt. 0) then
! CH4ptr => CH4_total
! CH4ptr = CH4ptr !*params%AirMW/16.0422
! else
!call MAPL_GetPointer(import, CH4ptr, 'CO_CH4',__RC__) ! Sourish
! endif
if (nCO .gt. 0) then
if (nCH4 .gt. 0) then
CH4ptr => CH4_total
else
call MAPL_GetPointer(import, CH4ptr, 'CO_CH4',__RC__)
end if
end if
! If we want to run CO2 photolysis at some point...
if (nCO2 .gt. 0) CO2ptr => CO2_total
if (nCO2 .gt. 0) then
if (nCO .gt. 0) then
COptr => CO_total
else
!call MAPL_GetPointer(import, COptr, 'CO2_CO',__RC__) ! Sourish :: Needs external input for CO field
end if
end if
!if (nCO .gt. 0) then
!COptr => CO_total
!else
!! Need an external CO source. call MAPL_GetPointer(import, COptr, 'CO_?',__RC__)
!endif
! ==================================================
if (nCO .gt. 0) call CO_prodloss( CO, OH, O1D, Cl, CO2photj, CH4photj, CH4ptr, CO2ptr, RC )
if (nCO2 .gt. 0) call CO2_prodloss( CO2, OH, COptr, RC ) ! Currently nothing in here. Just in case...
if (nCH4 .gt. 0) call CH4_prodloss( CH4, OH, O1D, Cl, CH4photj, RC )
CH4ptr => null()
CO2ptr => null()
COptr => null()
TRptr => null()
! -- integration
call integrate_forwardeuler( RC )
! -- post processing
if (cntrl%strictMassBalance) call util_accumulatenegatives( RC )
! Aggregate instances into the totals after operations
call util_aggregate( RC )
! ===============================================================
! ===============================================================
! C O M P U T E A N D P A S S D I A G N O S T I C S
call MAPL_GetPointer( export, Ptr3d, 'CO2_ProdLoss', __RC__ )
if (associated(Ptr3d)) then
call diag_prodloss( CO2(:), Ptr3d, RC )
Ptr3d = Ptr3d * met%delp/grav ! Convert to kg/m2/s
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'CO2DRY', __RC__)
if (associated(Ptr3d)) then
Ptr3d = (CO2_total )/(1.e0 - met%qtot)
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'CH4DRY', __RC__)
if (associated(Ptr3d)) then
Ptr3d = (CH4_total )/(1.e0 - met%qtot)
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'CODRY', __RC__)
if (associated(Ptr3d)) then
Ptr3d = (CO_total )/(1.e0 - met%qtot)
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'CO2photj', __RC__)
if (associated(Ptr3d)) then
Ptr3d = CO2photj
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'CH4photj', __RC__)
if (associated(Ptr3d)) then
Ptr3d = CH4photj
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'O3col', __RC__)
if (associated(Ptr3d)) then
Ptr3d = log10(O3col)
Ptr3d => null()
endif
call MAPL_GetPointer( export, Ptr3D, 'O2col', __RC__)
if (associated(Ptr3d)) then
Ptr3d = log10(O2col)
Ptr3d => null()
endif
! ===============================================================
! ===============================================================
! D O N E
! Cleanup
! Lots of memory leak potential here. Beware!
do i=1,NINSTANCES
! deallocate the prod/loss arrays for each instance
deallocate( instances(i)%p%prod, instances(i)%p%loss, __STAT__ )
instances(i)%p%data3d => null()
enddo
CO2_total => null(); CO_total => null(); CH4_total => null()
do i=1,nspecies
aggregate(i)%q => null()
enddo
deallocate(met%cosz, met%slr, O3col, O2col, CO2photj, CH4photj, __STAT__ )
RETURN_(ESMF_SUCCESS)
end subroutine GridCompRun2
!============================================================================
subroutine Finalize ( GC, IMPORT, EXPORT, clock, RC )
! !USES:
implicit NONE
! !INPUT PARAMETERS:
type(ESMF_Clock), intent(inout) :: clock ! The clock
! !OUTPUT PARAMETERS:
type(ESMF_GridComp), intent(inout) :: GC ! Grid Component
type(ESMF_State), intent(inout) :: IMPORT ! Import State
type(ESMF_State), intent(inout) :: EXPORT ! Export State
integer, intent(out) :: RC ! Error return code:
! 0 - all is well
! 1 -
! !DESCRIPTION: This is a simple ESMF wrapper.
!
! !REVISION HISTORY:
!
! 27Feb2005 da Silva First crack.
! 18Jan2023 M.Long - Adapted for GHG
!
!EOP
!-------------------------------------------------------------------------
! ErrLog Variables
! ----------------
character(len=ESMF_MAXSTR) :: IAm = 'Finalize_'
integer :: STATUS
character(len=ESMF_MAXSTR) :: COMP_NAME
integer :: i
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet( GC, NAME=COMP_NAME, RC=STATUS )
VERIFY_(STATUS)
Iam = trim(COMP_NAME) // '::' // 'Finalize_'
! Instances
do i=1,size(CO)
if (associated(CO(i)%data3d)) deallocate(CO(i)%data3d)
if ( allocated(CO(i)%prod) ) deallocate(CO(i)%prod)
if ( allocated(CO(i)%loss) ) deallocate(CO(i)%loss)
if (associated(CO(i)%mask) ) deallocate(CO(i)%mask)
enddo
if (associated(CO)) deallocate(CO)
do i=1,size(CO2)
if (associated(CO2(i)%data3d)) deallocate(CO2(i)%data3d)
if ( allocated(CO2(i)%prod) ) deallocate(CO2(i)%prod)
if ( allocated(CO2(i)%loss) ) deallocate(CO2(i)%loss)
if (associated(CO2(i)%mask) ) deallocate(CO2(i)%mask)
enddo
if (associated(CO2)) deallocate(CO2)
do i=1,size(CH4)
if (associated(CH4(i)%data3d)) deallocate(CH4(i)%data3d)
if ( allocated(CH4(i)%prod) ) deallocate(CH4(i)%prod)
if ( allocated(CH4(i)%loss) ) deallocate(CH4(i)%loss)
if (associated(CH4(i)%mask) ) deallocate(CH4(i)%mask)
enddo
if (associated(CH4)) deallocate(CH4)
do i=1,size(TR)
if (associated(TR(i)%data3d)) deallocate(TR(i)%data3d)
if ( allocated(TR(i)%prod) ) deallocate(TR(i)%prod)
if ( allocated(TR(i)%loss) ) deallocate(TR(i)%loss)
if (associated(TR(i)%mask) ) deallocate(TR(i)%mask)
enddo
if (associated(TR)) deallocate(TR)
do i=1,size(instances)
if (associated(instances(i)%p%data3d)) deallocate(instances(i)%p%data3d)
if ( allocated(instances(i)%p%prod) ) deallocate(instances(i)%p%prod)
if ( allocated(instances(i)%p%loss) ) deallocate(instances(i)%p%loss)
if (associated(instances(i)%p%mask) ) deallocate(instances(i)%p%mask)
enddo
if (allocated(instances)) deallocate(instances)
! Surface fluxes
do i=1,size(sfc_flux)
if (associated(sfc_flux(i)%flux)) deallocate(sfc_flux(i)%flux)
enddo
if (allocated(sfc_flux)) deallocate(sfc_flux)
! Aggregates
do i=1,size(aggregate)
if (associated(aggregate(i)%q)) deallocate(aggregate(i)%q)
enddo
if (allocated(aggregate)) deallocate(aggregate)
! Parameters
if (associated(params%lats)) deallocate(params%lats)
if (associated(params%lons)) deallocate(params%lons)
if (allocated(species)) deallocate(species)
! Finalize GEOS Generic
! ---------------------
!ALT: do not deallocate "foreign objects"
call MAPL_GenericFinalize ( GC, IMPORT, EXPORT, CLOCK, __RC__ )
RETURN_(ESMF_SUCCESS)
end subroutine Finalize
!============================================================================
! G E O S / M A P L / E S M F S E R V I C E R O U T I N E S
!============================================================================
subroutine fillFluxes( import, sfc_flux, RC )
type (ESMF_State), intent(inout) :: import ! Import state
type(surface_flux), intent(inout) :: sfc_flux(:)
integer, intent(out) :: RC
integer :: i
__Iam__('RRG:fillFluxes')
RC = 0
! This routine fills the flux fields from the ESMF import state
! -- loop over the registered fluxes
do i=1,size(sfc_flux)
! Right now we are assuming all fluxes are 2-D
CALL MAPL_GetPointer( import, sfc_flux(i)%flux, trim(sfc_flux(i)%name), notFoundOK=.FALSE., RC=RC )
if (RC .eq. ESMF_RC_NOT_FOUND) then
write(*,*) 'Could not find flux, '//trim(sfc_flux(i)%name)//' in imports. Aborting'
RETURN_(RC)
endif
if (RC .ne. ESMF_SUCCESS) then
RETURN_(RC)
endif
enddo
RETURN_(ESMF_SUCCESS)
end subroutine fillFluxes
subroutine RegisterFluxWithMAPL( GC, cfg, species, RC )
type (ESMF_GridComp), intent(INOUT) :: GC ! gridded component
type (ESMF_Config) :: cfg ! Configuration
character(*), intent(in) :: species
integer, optional :: RC
character(len=255) :: string
logical :: tend
__Iam__('RegisterFluxWithMAPL')
! Read the table in config
call ESMF_ConfigFindLabel( cfg,trim(species)//'_surface_flux_pairs::',rc=RC )
if (RC /= ESMF_SUCCESS) then ! Label not found
if (MAPL_am_I_root()) write(*,*) '<<>> Fluxes not found for '//trim(species)//'. Exiting.'
!_VERIFY(RC)
RETURN_(ESMF_SUCCESS)
endif