-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAerosol_Cap.F90
575 lines (483 loc) · 18.5 KB
/
Aerosol_Cap.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
#include "MAPL_ErrLog.h"
module Aerosol_Cap
use ESMF
use NUOPC
use NUOPC_Model, only : &
NUOPC_ModelGet, &
SetVM, &
model_routine_SS => SetServices, &
model_routine_Run => routine_Run, &
model_label_Advance => label_Advance, &
model_label_DataInitialize => label_DataInitialize, &
model_label_Finalize => label_Finalize
use Aerosol_Comp_mod
use Aerosol_Diag_mod
use Aerosol_Internal_mod
use Aerosol_Logger_mod
use Aerosol_Shared_mod
use Aerosol_Tracer_mod
use Aerosol_GridComp_mod, only : &
aerosol_routine_SS => SetServices
use MAPL
use mpi
implicit none
! -- model name
character(len=*), parameter :: modelName = "UFS Aerosols"
! -- import fields
character(len=*), dimension(*), parameter :: &
importFieldNames = [ &
"inst_pres_interface ", &
"inst_pres_levels ", &
"inst_geop_interface ", &
"inst_geop_levels ", &
"inst_temp_levels ", &
"inst_zonal_wind_levels ", &
"inst_merid_wind_levels ", &
"inst_cloud_frac_levels ", &
"inst_ice_nonconv_tendency_levels ", &
"inst_liq_nonconv_tendency_levels ", &
"inst_tracer_mass_frac ", &
"inst_pbl_height ", &
"surface_cell_area ", &
"inst_convective_rainfall_amount ", &
"inst_rainfall_amount ", &
"inst_zonal_wind_height10m ", &
"inst_merid_wind_height10m ", &
"inst_friction_velocity ", &
"inst_land_sea_mask ", &
"inst_temp_height_surface ", &
"inst_up_sensi_heat_flx ", &
"inst_surface_roughness ", &
"inst_surface_soil_wetness ", &
"inst_soil_moisture_content ", &
"ice_fraction_in_atm ", &
"lake_fraction ", &
"ocean_fraction ", &
"surface_snow_area_fraction " &
]
! -- export fields
character(len=*), dimension(*), parameter :: &
exportFieldNames = [ &
"inst_tracer_mass_frac ", &
"inst_tracer_up_surface_flx ", &
"inst_tracer_down_surface_flx " &
]
private
public :: SetServices
public :: SetVM
contains
subroutine SetServices(model, rc)
type(ESMF_GridComp) :: model
integer, intent(out) :: rc
! begin
rc = ESMF_SUCCESS
! the NUOPC model component will register the generic methods
call NUOPC_CompDerive(model, model_routine_SS, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! switch to IPDv03
call ESMF_GridCompSetEntryPoint(model, ESMF_METHOD_INITIALIZE, &
userRoutine=ModelInitializeP0, phase=0, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! set entry point for methods that require specific implementation
call NUOPC_CompSetEntryPoint(model, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p1"/), userRoutine=ModelInitializeP1, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! - set component's initialization status
call NUOPC_CompSpecialize(model, &
specLabel=model_label_DataInitialize, specRoutine=ModelDataInitialize, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
call NUOPC_CompSpecialize(model, specLabel=model_label_Advance, &
specRoutine=ModelAdvance, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! - finalize method
call NUOPC_CompSpecialize(model, specLabel=model_label_Finalize, &
specRoutine=ModelFinalize, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end subroutine SetServices
subroutine ModelInitializeP0(model, importState, exportState, clock, rc)
type(ESMF_GridComp) :: model
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! local variables
integer :: verbosity
character(len=ESMF_MAXSTR) :: name
! local parameters
character(len=*), parameter :: rName = "ModelInitializeP0"
! begin
rc = ESMF_SUCCESS
! startup
call AerosolLog(modelName//': Initializing ...', rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! get component's info
call NUOPC_CompGet(model, name=name, verbosity=verbosity, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! intro
call NUOPC_LogIntro(name, rName, verbosity, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! switch to IPDv01 by filtering all other phaseMap entries
call NUOPC_CompFilterPhaseMap(model, ESMF_METHOD_INITIALIZE, &
acceptStringList=(/"IPDv03p"/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! extro
call NUOPC_LogExtro(name, rName, verbosity, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end subroutine ModelInitializeP0
subroutine ModelInitializeP1(model, importState, exportState, clock, rc)
type(ESMF_GridComp) :: model
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! begin
rc = ESMF_SUCCESS
! -- advertise imported fields
call NUOPC_Advertise(importState, importFieldNames, &
TransferOfferGeomObject="cannot provide", &
SharePolicyField="share", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! -- advertise exported fields
call NUOPC_Advertise(exportState, exportFieldNames, &
TransferOfferGeomObject="cannot provide", &
SharePolicyField="share", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end subroutine ModelInitializeP1
subroutine ModelDataInitialize(model, rc)
type(ESMF_GridComp) :: model
integer, intent(out) :: rc
! local variables
integer :: stat, urc
integer :: localDeCount
integer :: modelComm
integer :: status
integer :: lb(2), ub(2)
integer :: item, nlev, rank
type(ESMF_Clock) :: clock
type(ESMF_Grid) :: grid
type(ESMF_State) :: importState, exportState
type(ESMF_VM) :: vm
type(ESMF_Info) :: tracerInfo
type(ESMF_Field), pointer :: fieldList(:)
type(MAPL_Cap), pointer :: cap
type(MAPL_CapOptions) :: maplCapOptions
type(Aerosol_InternalState_T) :: is
type(Aerosol_Tracer_T), pointer :: trp
! begin
rc = ESMF_SUCCESS
! retrieve import/export states and clock from NUOPC component and
! pass them to the GOCART component
call NUOPC_ModelGet(model, modelClock=clock, &
importState=importState, exportState=exportState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! retrieve member list from import state, if any
nullify(fieldList)
call NUOPC_GetStateMemberLists(importState, fieldList=fieldList, &
nestedFlag=.true., rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! retrieve model's metadata from imported tracer field
call AerosolModelGet(model, grid=grid, numLevels=nlev, tracerInfo=tracerInfo, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg="Unable to retrieve model grid and metadata ", &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! retrieve model's MPI communicator
call ESMF_GridCompGet(model, vm=vm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
call ESMF_VMGet(vm, mpiCommunicator=modelComm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! allocate memory for the internal state and store it into component
allocate(is % wrap, stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Unable to allocate internal state", &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
nullify(is % wrap % maplCap)
nullify(is % wrap % tracers)
! set MAPL options
maplCapOptions = MAPL_CapOptions(_RC)
maplCapOptions % use_comm_world = .false.
maplCapOptions % comm = modelComm
maplCapOptions % logging_config = ""
call MPI_Comm_size(modelComm, maplCapOptions % npes_model, urc)
if (urc /= MPI_SUCCESS) then
call ESMF_LogSetError(ESMF_RC_INTNRL_BAD, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)
return ! bail out
end if
! startup MAPL
allocate(is % wrap % maplCap, stat=stat, source = MAPL_Cap("MAPL Driver", aerosol_routine_SS, cap_options=maplCapOptions, rc=rc))
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Unable to allocate MAPL cap", &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
cap => is % wrap % maplCap
! initialize MAPL I/O on the same PETs as the model component
call cap % initialize_io_clients_servers(cap % get_comm_world(), _RC)
! create aerosol grid component
call cap % initialize_cap_gc(_RC)
call cap % cap_gc % set_services(rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! provide model grid to MAPL
call cap % cap_gc % set_grid(grid, lm=nlev, _RC)
! provide model clock to MAPL
call cap % cap_gc % set_clock(clock, _RC)
! initialize aerosol grid component
call cap % cap_gc % initialize(rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! create maps linking imported aerosol tracers to MAPL fields
! initialize tracer datatype containing tracer names and mapping information
allocate(is % wrap % tracers, stat=stat, source = AerosolTracer(cap % get_cap_rc_file(), tracerInfo, rc=rc))
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Unable to allocate tracers data structure", &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
trp => is % wrap % tracers
! print tracer maps
call AerosolTracerPrint(trp, 'GOCART2G Tracer Map')
! set component's internal state
call ESMF_GridCompSetInternalState(model, is, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! -> set InitializeDataComplete Component Attribute to "true", indicating
! to the driver that this Component has fully initialized its data
call NUOPC_CompAttributeSet(model, &
name="InitializeDataComplete", value="true", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! log end of model's initialize
call AerosolLog(modelName//': Model initialized', rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end subroutine ModelDataInitialize
subroutine ModelAdvance(model, rc)
type(ESMF_GridComp) :: model
integer, intent(out) :: rc
! local variables
integer :: diagnostic
character(len=ESMF_MAXSTR) :: name
character(len=ESMF_MAXSTR) :: msgString, timeString
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
type(ESMF_Time) :: currTime
type(ESMF_TimeInterval) :: timeStep
type(MAPL_Cap), pointer :: cap
type(Aerosol_InternalState_T) :: is
type(Aerosol_InternalData_T), pointer :: this
! local parameters
character(len=*), parameter :: rName = "ModelAdvance"
! begin
rc = ESMF_SUCCESS
! get component's information
call NUOPC_CompGet(model, name=name, diagnostic=diagnostic, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! query the Component for its clock, importState and exportState
call NUOPC_ModelGet(model, modelClock=clock, importState=importState, &
exportState=exportState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! log time step
call AerosolLogStep(clock, msg=modelName, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! get component's internal state
call ESMF_GridCompGetInternalState(model, is, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
nullify(cap)
if (associated(is % wrap)) cap => is % wrap % maplCap
! advance MAPL component
if (associated(cap)) then
! -- import
call AerosolStateUpdate(model, cap, "import", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! -- run cap
call cap % cap_gc % run(rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! -- export tracers
call AerosolStateUpdate(model, cap, "export", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! -- export diagnostics
call AerosolDiagUpdate(model, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end if
! print field diagnostics
if (btest(diagnostic,17)) then
call AerosolFieldDiagnostics(model, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
if (associated(cap)) then
call MAPLFieldDiagnostics(model, cap % cap_gc % import_state, "import", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
call MAPLFieldDiagnostics(model, cap % cap_gc % export_state, "export", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end if
end if
end subroutine ModelAdvance
subroutine ModelFinalize(model, rc)
type(ESMF_GridComp) :: model
integer, intent(out) :: rc
! local variables
integer :: status
type(MAPL_Cap), pointer :: cap
type(Aerosol_InternalState_T) :: is
type(Aerosol_InternalData_T), pointer :: this
! begin
rc = ESMF_SUCCESS
! finalize
call AerosolLog(modelName//': Finalizing ...', rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! get component's internal state
call ESMF_GridCompGetInternalState(model, is, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
nullify(this, cap)
if (associated(is % wrap)) then
this => is % wrap
cap => this % maplCap
if (associated(cap)) then
! finalize MAPL component
call cap % cap_gc % finalize(_RC)
! finalize I/O
call cap % finalize_io_clients_servers(_RC)
! finalize MAPL framework
call MAPL_Finalize(comm=cap % get_comm_world(), _RC)
! deallocate MAPL Cap
deallocate(this % maplCap, stat=status)
if (ESMF_LogFoundDeallocError(statusToCheck=status, &
msg="Failed to free MAPL Cap.", &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
nullify(this % maplCap, cap)
end if
! finally, deallocate internal state
deallocate(is % wrap, stat=status)
if (ESMF_LogFoundDeallocError(statusToCheck=status, &
msg="Failed to free internal state memory.", &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
nullify(is % wrap, this)
end if
! log completion of model's finalize
call AerosolLog(modelName//': Model finalized', rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
end subroutine ModelFinalize
end module Aerosol_Cap