-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickChem_GridCompMod.F90
608 lines (425 loc) · 20.2 KB
/
QuickChem_GridCompMod.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
#include "MAPL_Generic.h"
#define QUICKCHEM_RESOURCE_FILE 'QuickChem_GridComp.rc'
!=============================================================================
!BOP
! !MODULE: QuickChem_GridCompMod - The Quick Chemistry Grid Component
! !INTERFACE:
module QuickChem_GridCompMod
! !USES:
use ESMF
use MAPL
use QuickChem_Generic
! !Establish the Childen's SetServices
!-----------------------------------
use OH_GridCompMod, only : OH_setServices => SetServices
! use XX_GridCompMod, only : XX_setServices => SetServices
implicit none
private
! !PUBLIC MEMBER FUNCTIONS:
public SetServices
public IS_QC_INSTANCE_RUNNING
! Private State
type :: Instance
integer :: id = -1
logical :: is_active
character(:), allocatable :: name
end type Instance
type Constituent
type(Instance), allocatable :: instances(:)
integer :: n_active
end type Constituent
type QuickChem_State
private
type(Constituent) :: OH
! type(Constituent) :: XX
! real, allocatable :: wavelengths_profile(:) ! wavelengths for profile aop [nm]
! real, allocatable :: wavelengths_vertint(:) ! wavelengths for vertically integrated aop [nm]
end type QuickChem_State
type wrap_
type (QuickChem_State), pointer :: PTR => null()
end type wrap_
! !DESCRIPTION:
!
! {\tt QuickChem} is a gridded component that includes OH, etc
!
!
! !REVISION HISTORY:
!
! 2022.02.04 Manyin First crack, adapted from GOCART2G.
!
!EOP
!============================================================================
contains
!BOP
! !IROUTINE: SetServices -- Sets ESMF services for this component
! !INTERFACE:
subroutine SetServices (GC, RC)
! !ARGUMENTS:
type (ESMF_GridComp), intent(INOUT) :: GC ! gridded component
integer, optional :: RC ! return code
! !DESCRIPTION: This version uses MAPL_GenericSetServices, which sets
! the Initialize and Finalize services to generic versions. It also
! allocates our instance of a generic state and puts it in the
! gridded component (GC). Here we only set the two-stage run method and
! declare the data services.
! !REVISION HISTORY:
! 14oct2019 Sherman, da Silva, Darmenov, Clune - First attempt at refactoring for ESMF compatibility
!EOP
!============================================================================
!
! Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
type (ESMF_Config) :: myCF ! QUICKCHEM_RESOURCE_FILE
type (ESMF_Config) :: cf ! universal config
type (QuickChem_State), pointer :: self
type (wrap_) :: wrap
! integer :: n_wavelengths_profile, n_wavelengths_vertint, n_wavelengths_diagmie
! integer, allocatable, dimension(:) :: wavelengths_diagmie
__Iam__('SetServices')
!****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet (GC, name=comp_name, config=cf, __RC__)
IF (index(Iam,'::') == 0) Iam = trim(COMP_NAME)//'::'//Iam
! Wrap internal state for storing in GC
! -------------------------------------
allocate (self, __STAT__)
wrap%ptr => self
! Set the Initialize, Run, Finalize entry points
! ------------------------------------------------
call MAPL_GridCompSetEntryPoint (GC, ESMF_Method_Initialize, Initialize, __RC__)
call MAPL_GridCompSetEntryPoint (GC, ESMF_Method_Run, Run1, __RC__)
call MAPL_GridCompSetEntryPoint (GC, ESMF_Method_Run, Run2, __RC__)
! Store internal state in GC
! --------------------------
call ESMF_UserCompSetInternalState (GC, 'QuickChem_State', wrap, STATUS)
VERIFY_(STATUS)
myCF = ESMF_ConfigCreate (__RC__)
call ESMF_ConfigLoadFile (myCF, QUICKCHEM_RESOURCE_FILE, __RC__)
!!! OLD GOCART2G section, saved as an example:
!!!
! Retrieve wavelengths from QUICKCHEM_RESOURCE_FILE
! n_wavelengths_profile = ESMF_ConfigGetLen (myCF, label='wavelengths_for_profile_aop_in_nm:', __RC__)
! n_wavelengths_vertint = ESMF_ConfigGetLen (myCF, label='wavelengths_for_vertically_integrated_aop_in_nm:', __RC__)
! n_wavelengths_diagmie = ESMF_ConfigGetLen (myCF, label='aerosol_monochromatic_optics_wavelength_in_nm_from_LUT:', __RC__)
! allocate(self%wavelengths_profile(n_wavelengths_profile), self%wavelengths_vertint(n_wavelengths_vertint), &
! wavelengths_diagmie(n_wavelengths_diagmie), __STAT__)
! call ESMF_ConfigGetAttribute (myCF, self%wavelengths_profile, label='wavelengths_for_profile_aop_in_nm:', __RC__)
! call ESMF_ConfigGetAttribute (myCF, self%wavelengths_vertint, label='wavelengths_for_vertically_integrated_aop_in_nm:', __RC__)
! call ESMF_ConfigGetAttribute (myCF, wavelengths_diagmie, label='aerosol_monochromatic_optics_wavelength_in_nm_from_LUT:', __RC__)
! Set wavelengths in universal config
! call MAPL_ConfigSetAttribute (cf, self%wavelengths_profile, label='wavelengths_for_profile_aop_in_nm:', __RC__)
! call MAPL_ConfigSetAttribute (cf, self%wavelengths_vertint, label='wavelengths_for_vertically_integrated_aop_in_nm:', __RC__)
! call MAPL_ConfigSetAttribute (cf, wavelengths_diagmie, label='aerosol_monochromatic_optics_wavelength_in_nm_from_LUT:', __RC__)
!!!
! Get instances for each species
! -----------------------------------------------------
call getInstances_('OH', myCF, species=self%OH, __RC__)
! call getInstances_('XX', myCF, species=self%XX, __RC__)
! Nitrate currently only supports one active instance
! if (self%NI%n_active > 1) then
! if(mapl_am_i_root()) print*,'WARNING: GOCART can only support one active nitrate instance. Check the RC/'//QUICKCHEM_RESOURCE_FILE
! end if
call ESMF_ConfigDestroy(myCF, __RC__)
! Create children's gridded components and invoke their SetServices
! Active instances are created first
! -----------------------------------------------------------------
call createInstances_(self, GC, __RC__)
! Define EXPORT states
! For an example of exporting a MAPL_StateItem and
! a MAPL_BundleItem, see GOCART2G_GridCompMod.F90
#include "QuickChem_Export___.h"
! Allow children of Chemistry to connect to these fields:
if (size(self%OH%instances) > 0) call MAPL_AddExportSpec (GC, SHORT_NAME='OH', CHILD_ID=self%OH%instances(1)%id, __RC__)
! For an example of adding connectivities between QuickChem species
! see GOCART2G_GridCompMod.F90
! Set generic services
! ----------------------------------
call MAPL_GenericSetServices (GC, __RC__)
RETURN_(ESMF_SUCCESS)
end subroutine SetServices
!============================================================================
!BOP
! !IROUTINE: Initialize -- Initialize method for the composite Gridded Component
! !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: This initializes the QuickChem Grid Component. It primarily creates
! its exports and establishes its children.
! !REVISION HISTORY:
! 2022.02.04 Manyin Adapted from GOCART2G
!EOP
!============================================================================
! Locals
character (len=ESMF_MAXSTR) :: COMP_NAME
type (MAPL_MetaComp), pointer :: MAPL
type (ESMF_GridComp), pointer :: gcs(:)
type (ESMF_State), pointer :: gex(:)
type (ESMF_Grid) :: grid
type (ESMF_Config) :: CF
type (QuickChem_State), pointer :: self
type (wrap_) :: wrap
integer :: dims(3)
__Iam__('Initialize')
!****************************************************************************
! Begin...
! Get the target components name and set-up traceback handle.
! -----------------------------------------------------------
call ESMF_GridCompGet (GC, grid=grid, name=COMP_NAME, __RC__)
IF (index(Iam,'::') == 0) Iam = trim(COMP_NAME)//'::'//Iam
if (mapl_am_i_root()) then
print *, TRIM(Iam)//': Starting...'
print *,' '
end if
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC (GC, MAPL, __RC__)
call MAPL_GridGet ( grid, localCellCountPerDim=dims, __RC__ )
! Call Generic Initialize
! ----------------------------------------
call MAPL_GenericInitialize (GC, import, export, clock, __RC__)
! Get my internal state
! ---------------------
call ESMF_UserCompGetInternalState (GC, 'QuickChem_State', wrap, STATUS)
VERIFY_(STATUS)
self => wrap%ptr
CF = ESMF_ConfigCreate (__RC__)
call ESMF_ConfigLoadFile (CF, 'AGCM.rc', __RC__) ! should the rc file be changed?
! Get children and their export states from my generic state
! -----------------------------------------------------------
call MAPL_Get (MAPL, gcs=gcs, gex=gex, __RC__ )
! For an example of filling export states with the children's states
! see GOCART2G_GridCompMod.F90
RETURN_(ESMF_SUCCESS)
end subroutine Initialize
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!BOP
! !IROUTINE: RUN -- Run method for GOCART2G
! !INTERFACE:
subroutine Run1 (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: Run method
!EOP
!============================================================================
! Locals
character(len=ESMF_MAXSTR) :: COMP_NAME
type (MAPL_MetaComp), pointer :: MAPL
type (ESMF_GridComp), pointer :: gcs(:)
type (ESMF_State), pointer :: gim(:)
type (ESMF_State), pointer :: gex(:)
type (ESMF_State) :: internal
integer :: i
__Iam__('Run1')
!****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet( GC, NAME=COMP_NAME, __RC__ )
IF (index(Iam,'::') == 0) Iam = trim(COMP_NAME)//'::'//Iam
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC ( GC, MAPL, RC=STATUS )
VERIFY_(STATUS)
! Get parameters from generic state.
! -----------------------------------
call MAPL_Get ( MAPL, gcs=gcs, gim=gim, gex=gex, INTERNAL_ESMF_STATE=internal, __RC__ )
! Run the children
! -----------------
do i = 1, size(gcs)
call ESMF_GridCompRun (gcs(i), importState=gim(i), exportState=gex(i), phase=1, clock=clock, __RC__)
end do
RETURN_(ESMF_SUCCESS)
end subroutine Run1
!============================================================================
!BOP
! !IROUTINE: RUN2 -- Run2 method for GOCART2G component
! !INTERFACE:
subroutine Run2 (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: This version uses the MAPL\_GenericSetServices. This function sets
! the Initialize and Finalize services, as well as allocating
!EOP
!============================================================================
! Locals
character(len=ESMF_MAXSTR) :: COMP_NAME
type (MAPL_MetaComp), pointer :: MAPL
type (ESMF_GridComp), pointer :: gcs(:)
type (ESMF_State), pointer :: gim(:)
type (ESMF_State), pointer :: gex(:)
type (ESMF_State) :: internal
type (QuickChem_State), pointer :: self
type (wrap_) :: wrap
character(len=ESMF_MAXSTR) :: child_name
integer :: i, n, w
real, pointer, dimension(:,:) :: LATS
real, pointer, dimension(:,:) :: LONS
#include "QuickChem_DeclarePointer___.h"
__Iam__('Run2')
!****************************************************************************
! Begin...
! Get my name and set-up traceback handle
! ---------------------------------------
call ESMF_GridCompGet( GC, NAME=COMP_NAME, __RC__ )
IF (index(Iam,'::') == 0) Iam = trim(COMP_NAME)//'::'//Iam
! Get my internal MAPL_Generic state
! -----------------------------------
call MAPL_GetObjectFromGC ( GC, MAPL, RC=STATUS )
VERIFY_(STATUS)
! Get parameters from generic state.
! -----------------------------------
call MAPL_Get ( MAPL, gcs=gcs, gim=gim, gex=gex, INTERNAL_ESMF_STATE=internal, &
LONS=LONS, LATS=LATS, __RC__ )
! Get my internal state
! ---------------------
call ESMF_UserCompGetInternalState (GC, 'QuickChem_State', wrap, STATUS)
VERIFY_(STATUS)
self => wrap%ptr
#include "QuickChem_GetPointer___.h"
! Run the children
! -----------------
do i = 1, size(gcs)
call ESMF_GridCompGet (gcs(i), NAME=child_name, __RC__ )
if ((index(child_name, 'data')) == 0) then ! only execute Run2 method if a computational instance
call ESMF_GridCompRun (gcs(i), importState=gim(i), exportState=gex(i), phase=2, clock=clock, __RC__)
end if
end do
! in G2G there was a section that computed total aerosol diagnostic values for export
RETURN_(ESMF_SUCCESS)
end subroutine Run2
!===============================================================================
subroutine getInstances_ (species_name, myCF, species, rc)
! Description: Fills the QuickChem_State (aka, self%instance_XYZ) with user
! defined instances from the QUICKCHEM_RESOURCE_FILE
implicit none
character (len=*), intent(in ) :: species_name
type (ESMF_Config), intent(inout) :: myCF
type(Constituent), intent(inout) :: species
integer, intent( out) :: rc
! locals
integer :: i
integer :: n_active
integer :: n_passive
integer :: n_instances
character (len=ESMF_MAXSTR) :: inst_name
__Iam__('QuickChem::getInstances_')
!--------------------------------------------------------------------------------------
! Begin...
n_active = ESMF_ConfigGetLen (myCF, label='ACTIVE_INSTANCES_'//trim(species_name)//':', __RC__)
n_passive = ESMF_ConfigGetLen (myCF, label='PASSIVE_INSTANCES_'//trim(species_name)//':', __RC__)
n_instances = n_active + n_passive
allocate (species%instances(n_instances), __STAT__)
! !Fill the instances list with active instances first
call ESMF_ConfigFindLabel (myCF, 'ACTIVE_INSTANCES_'//trim(species_name)//':', __RC__)
do i = 1, n_active
call ESMF_ConfigGetAttribute (myCF, inst_name, __RC__)
species%instances(i)%name = inst_name
species%instances(i)%is_active = .true.
end do
species%n_active = n_active
! !Now fill instances list with passive instances
call ESMF_ConfigFindLabel (myCF, 'PASSIVE_INSTANCES_'//trim(species_name)//':', __RC__)
do i = n_active+1, n_active+n_passive
call ESMF_ConfigGetAttribute (myCF, inst_name, __RC__)
species%instances(i)%name = inst_name
species%instances(i)%is_active = .false.
end do
RETURN_(ESMF_SUCCESS)
end subroutine getInstances_
!====================================================================================
subroutine createInstances_ (self, GC, rc)
! Description: Creates QuickChem children. Active instances must be created first. If
! additional QuickChem children are added, this subroutine will need to be updated.
implicit none
type (QuickChem_State), pointer, intent(in ) :: self
type (ESMF_GridComp), intent(inout) :: GC
integer, intent( out) :: rc
! locals
integer :: i
__Iam__('QuickChem::createInstances_')
!-----------------------------------------------------------------------------------
! Begin...
! Active instances must be created first! This ordering is necessary for
! filing the QC_AERO states that are passed to radiation.
! This is achieved by arranging the names of the active instances first.
call addChildren__ (gc, self%OH, setServices=OH_setServices, __RC__)
! call addChildren__ (gc, self%xx, SETsErvices=XX_setServices, __RC__)
RETURN_(ESMF_SUCCESS)
contains
subroutine addChildren__ (gc, species, setServices, rc)
type (ESMF_GridComp), intent(inout) :: gc
type(Constituent), intent(inout) :: species
external :: setServices
integer, intent( out) :: rc
! local
integer :: n
__Iam__('QuickChem::createInstances_::addChildren__')
n=size(species%instances)
do i = 1, n
species%instances(i)%id = MAPL_AddChild(gc, name=species%instances(i)%name, SS=SetServices, __RC__)
end do
RETURN_(ESMF_SUCCESS)
end subroutine addChildren__
end subroutine createInstances_
! !IROUTINE: IS_QC_INSTANCE_RUNNING -- Scan the resource file for Instance name
! !INTERFACE:
subroutine IS_QC_INSTANCE_RUNNING (species_name, instance_name, running, RC)
implicit none
! !ARGUMENTS:
character (len=*), intent(in ) :: species_name
character (len=*), intent(in ) :: instance_name
logical, intent( out) :: running
integer, optional :: RC ! return code
! !DESCRIPTION: Determine whether a particular instance is running
! by interrogating the .rc file; intended to help parent GC determine
! whether to AddConnectivity
! !REVISION HISTORY:
! 11jul2022 Manyin - first crack
!EOP
!============================================================================
!
! Locals
type (ESMF_Config) :: myCF ! fill from QUICKCHEM_RESOURCE_FILE
integer :: i
integer :: n_active
integer :: n_passive
integer :: n_instances
character (len=ESMF_MAXSTR) :: inst_name
__Iam__('QuickChem::IS_QC_INSTANCE_RUNNING')
running = .FALSE.
myCF = ESMF_ConfigCreate (__RC__)
call ESMF_ConfigLoadFile (myCF, QUICKCHEM_RESOURCE_FILE, __RC__)
n_active = ESMF_ConfigGetLen (myCF, label= 'ACTIVE_INSTANCES_'//trim(species_name)//':', __RC__)
n_passive = ESMF_ConfigGetLen (myCF, label='PASSIVE_INSTANCES_'//trim(species_name)//':', __RC__)
n_instances = n_active + n_passive
! !Check the active instances first
call ESMF_ConfigFindLabel (myCF, 'ACTIVE_INSTANCES_'//trim(species_name)//':', __RC__)
do i = 1, n_active
call ESMF_ConfigGetAttribute (myCF, inst_name, __RC__)
if ( TRIM(inst_name) == TRIM(instance_name) ) running = .TRUE.
end do
! !Now check the passive instances
call ESMF_ConfigFindLabel (myCF, 'PASSIVE_INSTANCES_'//trim(species_name)//':', __RC__)
do i = n_active+1, n_active+n_passive
call ESMF_ConfigGetAttribute (myCF, inst_name, __RC__)
if ( TRIM(inst_name) == TRIM(instance_name) ) running = .TRUE.
end do
call ESMF_ConfigDestroy(myCF, __RC__)
RETURN_(ESMF_SUCCESS)
end subroutine IS_QC_INSTANCE_RUNNING
end module QuickChem_GridCompMod