-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcop_comp_nuopc.F90
976 lines (769 loc) · 38.5 KB
/
cop_comp_nuopc.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
module cop_comp_nuopc
!-----------------------------------------------------------------------------
! This is the NUOPC cap for generic co-processing component (COP)
!-----------------------------------------------------------------------------
use ESMF, only: operator(==)
use ESMF, only: ESMF_GridComp
use ESMF, only: ESMF_Field, ESMF_FieldGet, ESMF_FieldEmptySet
use ESMF, only: ESMF_State, ESMF_StateGet, ESMF_StateRemove
use ESMF, only: ESMF_Clock, ESMF_LogWrite, ESMF_LogFoundAllocError
use ESMF, only: ESMF_StateGet, ESMF_GridCompSetEntryPoint
use ESMF, only: ESMF_GridCompGetInternalState, ESMF_GridCompSetInternalState
use ESMF, only: ESMF_AttributeGet, ESMF_DistGridConnection
use ESMF, only: ESMF_Grid, ESMF_GridCreate, ESMF_GridGet
use ESMF, only: ESMF_Mesh, ESMF_MeshCreate, ESMF_MeshGet, ESMF_MeshEmptyCreate
use ESMF, only: ESMF_DistGrid, ESMF_DistGridCreate, ESMF_DistGridGet
use ESMF, only: ESMF_FAILURE, ESMF_LOGMSG_INFO, ESMF_SUCCESS
use ESMF, only: ESMF_METHOD_INITIALIZE, ESMF_MAXSTR, ESMF_LOGMSG_WARNING
use ESMF, only: ESMF_GEOMTYPE_GRID, ESMF_GEOMTYPE_MESH
use ESMF, only: ESMF_FIELDSTATUS_GRIDSET, ESMF_FIELDSTATUS_EMPTY
use ESMF, only: ESMF_FIELDSTATUS_COMPLETE, ESMF_StateItem_Flag
use ESMF, only: ESMF_STATEITEM_STATE, ESMF_STATEITEM_FIELD
use ESMF, only: ESMF_LOGMSG_ERROR, ESMF_METHOD_RUN
use ESMF, only: ESMF_GeomType_Flag, ESMF_FieldStatus_Flag
use ESMF, only: ESMF_Time, ESMF_TimeGet
use ESMF, only: ESMF_Clock, ESMF_ClockGet, ESMF_UtilStringLowerCase
use ESMF, only: ESMF_DistGridGet, ESMF_DistGridConnection
use ESMF, only: ESMF_FieldCreate, ESMF_StateIsCreated
use ESMF, only: ESMF_GridGetCoord, ESMF_STAGGERLOC_CORNER
use ESMF, only: ESMF_TYPEKIND_R8, ESMF_MESHLOC_ELEMENT
use NUOPC, only: NUOPC_CompDerive
use NUOPC, only: NUOPC_CompSpecialize
use NUOPC, only: NUOPC_CompFilterPhaseMap, NUOPC_CompSetEntryPoint
use NUOPC, only: NUOPC_SetAttribute, NUOPC_GetAttribute
use NUOPC, only: NUOPC_CompAttributeGet, NUOPC_CompAttributeSet
use NUOPC, only: NUOPC_Realize
use NUOPC, only: NUOPC_AddNamespace
use NUOPC_Model, only: SetVM
use NUOPC_Model, only: NUOPC_ModelGet
use NUOPC_Model, only: model_routine_SS => SetServices
use NUOPC_Model, only: model_routine_Run => routine_Run
use NUOPC_Model, only: model_label_DataInitialize => label_DataInitialize
use NUOPC_Model, only: model_label_Advance => label_Advance
use NUOPC_Model, only: label_Advertise
use NUOPC_Model, only: label_ModifyAdvertised
use NUOPC_Model, only: label_AcceptTransfer
use NUOPC_Model, only: label_RealizeAccepted
use NUOPC_Model, only: label_Advance
use cop_comp_shr, only: ChkErr
use cop_comp_shr, only: FB_init_pointer
use cop_comp_shr, only: StringListGetName
use cop_comp_shr, only: StringListGetNum
use cop_comp_internalstate, only: InternalState
use cop_comp_internalstate, only: InternalStateInit
use cop_phases_io, only: cop_phases_io_run
use cop_phases_python, only: cop_phases_python_run
use cop_phases_catalyst, only: cop_phases_catalyst_run
implicit none
private
!-----------------------------------------------------------------------------
! Public module routines
!-----------------------------------------------------------------------------
public :: SetServices
public :: SetVM
!-----------------------------------------------------------------------------
! Private module routines
!-----------------------------------------------------------------------------
private :: DataInitialize
!-----------------------------------------------------------------------------
! Private module data
!-----------------------------------------------------------------------------
integer :: dbug = 0
character(len=*), parameter :: modName = "(cop_comp_nuopc)"
character(len=*), parameter :: u_FILE_u = __FILE__
!===============================================================================
contains
!===============================================================================
subroutine SetServices(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
character(len=*), parameter :: subname = trim(modName)//':(SetServices) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
!------------------
! register the generic methods
!------------------
call NUOPC_CompDerive(gcomp, model_routine_SS, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! specialize model
!------------------
! It is used to set FieldTransferPolicy
call NUOPC_CompSpecialize(gcomp, specLabel=label_Advertise, specRoutine=Advertise, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! It is used to remove some fields from the list of mirrored fields
call NUOPC_CompSpecialize(gcomp, specLabel=label_ModifyAdvertised, specRoutine=ModifyAdvertised, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! It is used to change domain decomposition
call NUOPC_CompSpecialize(gcomp, specLabel=label_AcceptTransfer, specRoutine=AcceptTransfer, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! It is used to realized mirrored fields and also update decomposition
call NUOPC_CompSpecialize(gcomp, specLabel=label_RealizeAccepted, specRoutine=RealizeAccepted, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! It is used for data initialization
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_DataInitialize, specRoutine=DataInitialize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! It is used to run user specified phase to process the data
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Advance, specRoutine=Advance, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! setup custom phases
!------------------
! I/O phase
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_RUN, phaseLabelList=(/ 'cop_phases_io' /), userRoutine=model_routine_Run, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Advance, specPhaseLabel="cop_phases_io", specRoutine=cop_phases_io_run, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Python phase
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_RUN, phaseLabelList=(/ 'cop_phases_python' /), userRoutine=model_routine_Run, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Advance, specPhaseLabel="cop_phases_python", specRoutine=cop_phases_python_run, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! ParaView Catalyst phase
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_RUN, phaseLabelList=(/"cop_phases_catalyst"/), userRoutine=model_routine_Run, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Advance, specPhaseLabel="cop_phases_catalyst", specRoutine=cop_phases_catalyst_run, rc=rc)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine SetServices
!-----------------------------------------------------------------------------
subroutine Advertise(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: stat
type(InternalState) :: is_local
type(ESMF_State) :: importState, exportState
character(len=*), parameter :: subname = trim(modName)//':(Advertise) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
!------------------
! Allocate memory for the internal state
!------------------
allocate(is_local%wrap, stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Allocation of the internal state memory failed.", line=__LINE__, file=u_FILE_u)) then
return
end if
call ESMF_GridCompSetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! Query for importState and exportState
!------------------
call NUOPC_ModelGet(gcomp, importState=importState, exportState=exportState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! Set attribute for field mirroring
! It indicates that fields should be mirrored in the State of a connected component
!------------------
call NUOPC_SetAttribute(importState, "FieldTransferPolicy", "transferAllWithNamespace", rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine Advertise
!-----------------------------------------------------------------------------
subroutine ModifyAdvertised(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: i, j, k, n, m
integer :: stat
integer :: fieldCount, itemCount
integer :: importItemCount, importNestedItemCount
logical :: isPresent, isSet, isFound
type(InternalState) :: is_local
type(ESMF_Field) :: field
type(ESMF_State) :: importState
type(ESMF_State) :: importNestedState
character(ESMF_MAXSTR), allocatable :: fieldNamesToKeep(:)
character(ESMF_MAXSTR), allocatable :: lfieldnamelist(:)
character(ESMF_MAXSTR), allocatable :: fieldNamesToRemove(:)
character(ESMF_MAXSTR) :: namespace
character(ESMF_MAXSTR) :: message, cname, cvalue, scalar_field_name = ''
character(ESMF_MAXSTR), allocatable :: importItemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: importItemTypeList(:)
character(ESMF_MAXSTR), allocatable :: importNestedItemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: importNestedItemTypeList(:)
character(len=*), parameter :: subname = trim(modName)//':(ModifyAdvertised) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
!------------------
! Query internal state
!------------------
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! Query for ScalarFieldName
!------------------
scalar_field_name = ""
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldName", value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
scalar_field_name = trim(cvalue)
call ESMF_LogWrite(trim(subname)//": ScalarFieldName = "//trim(scalar_field_name), ESMF_LOGMSG_INFO)
endif
!------------------
! Query for KeepFieldList (only list of fields will be available)
!------------------
call NUOPC_CompAttributeGet(gcomp, name="KeepFieldList", value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
m = 0
if (isPresent .and. isSet) then
! Get number of fields in the list
m = StringListGetNum(cvalue, ":")
if (m > 0) then
! Allocate temporary array for field list
allocate(fieldNamesToKeep(m))
! Loop over occurances and fill the field list
do n = 1, m
call StringListGetName(cvalue, n, cname, ':', rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
fieldNamesToKeep(n) = trim(cname)
write(message, fmt='(A,I2.2,A)') trim(subname)//': KeepFieldList(',n,') = '//trim(fieldNamesToKeep(n))
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_INFO)
end do
end if
endif
!------------------
! Query for RemoveFieldList (remove fields from mirrored field list)
!------------------
if (size(fieldNamesToKeep, dim=1) > 0) then
call ESMF_LogWrite(trim(subname)//': KeepFieldList is provided. Neglecting entries of RemoveFieldList!', ESMF_LOGMSG_INFO)
else
call NUOPC_CompAttributeGet(gcomp, name="RemoveFieldList", value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
m = 0
if (isPresent .and. isSet) then
! Add scalar field to remove list
if (trim(scalar_field_name) /= '') then
if (trim(cvalue) == '') then
cvalue = trim(scalar_field_name)
else
cvalue = trim(cvalue)//':'//trim(scalar_field_name)
end if
end if
! Get number of fields in the list
m = StringListGetNum(cvalue, ':')
if (m > 0) then
! Allocate temporary array for field list
allocate(fieldNamesToRemove(m))
! Loop over occurances and fill the field list
do n = 1, m
call StringListGetName(cvalue, n, cname, ':', rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
fieldNamesToRemove(n) = trim(cname)
write(message, fmt='(A,I2.2,A)') trim(subname)//': RemoveFieldList(',n,') = '//trim(fieldNamesToRemove(n))
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_INFO)
end do
end if
else
! Allocate temporary array for field list
allocate(fieldNamesToRemove(1))
fieldNamesToRemove(1) = trim(scalar_field_name)
end if
end if
!------------------
! Query for importState
!------------------
call NUOPC_ModelGet(gcomp, importState=importState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! Loop over import fields and remove/keep requested fields if there are
!------------------
call ESMF_StateGet(importState, nestedFlag=.false., itemCount=importItemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data structures
allocate(importItemNameList(importItemCount))
allocate(importItemTypeList(importItemCount))
! Query state
call ESMF_StateGet(importState, nestedFlag=.false., itemNameList=importItemNameList, itemTypeList=importItemTypeList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Loop over nested states if they are found and filter fields
do i = 1, importItemCount
if (importItemTypeList(i) == ESMF_STATEITEM_STATE) then
! Get the associated nested state
call ESMF_StateGet(importState, itemName=importItemNameList(i), nestedState=importNestedState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query nested state
call ESMF_StateGet(importNestedState, itemCount=importNestedItemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data structures
allocate(importNestedItemNameList(importNestedItemCount))
allocate(importNestedItemTypeList(importNestedItemCount))
! Query item name and types in the nested state
call ESMF_StateGet(importNestedState, itemNameList=importNestedItemNameList, &
itemTypeList=importNestedItemTypeList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query namespace
call NUOPC_GetAttribute(importNestedState, name="Namespace", value=namespace, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
namespace = ESMF_UtilStringLowerCase(trim(namespace))
! Print debug information
write(message, fmt='(A,I5,A)') trim(subname)//': nested import state from '//trim(namespace)//' has ', importNestedItemCount, ' item'
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_INFO)
! Keep only desired fields
if (size(fieldNamesToKeep, dim=1) > 0) then
do j = 1, importNestedItemCount
call ESMF_LogWrite(trim(subname)//': '//trim(importNestedItemNameList(j)), ESMF_LOGMSG_INFO)
! Check field is in the keep list
isFound = .false.
do k = 1, size(fieldNamesToKeep, dim=1)
if (trim(importNestedItemNameList(j)) == trim(fieldNamesToKeep(k))) then
isFound = .true.
cycle
end if
end do
! Remove field from import state if it is not specified in the keep list
if (.not. isFound) then
call ESMF_LogWrite(trim(subname)//': '//trim(importNestedItemNameList(j))//' will be removed', ESMF_LOGMSG_INFO)
call ESMF_StateRemove(importNestedState, itemNameList=(/trim(importNestedItemNameList(j))/), relaxedFlag=.true., rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
cycle
end if
end do
end if
if (size(fieldNamesToRemove, dim=1) > 0) then
! Print out field names that will be removed
do j = 1, size(fieldNamesToRemove, dim=1)
call ESMF_LogWrite(trim(subname)//': '//trim(fieldNamesToRemove(j))//' will be removed', ESMF_LOGMSG_INFO)
end do
! Remove field/s from state
call ESMF_StateRemove(importNestedState, itemNameList=fieldNamesToRemove, relaxedFlag=.true., rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
! Clean memory
deallocate(importNestedItemNameList)
deallocate(importNestedItemTypeList)
end if
end do
! Clean memory
deallocate(importItemNameList)
deallocate(importItemTypeList)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine ModifyAdvertised
!-----------------------------------------------------------------------------
subroutine AcceptTransfer(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: n
integer :: itemCount
logical :: hasNestedState
type(InternalState) :: is_local
type(ESMF_State) :: importState
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: itemTypeList(:)
character(len=*), parameter :: subname = trim(modName)//':(AcceptTransfer) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! Get the internal state
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Initialize internal state
call InternalStateInit(gcomp, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query import state
call NUOPC_ModelGet(gcomp, importState=importState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query state for nested states
call ESMF_StateGet(importState, nestedFlag=.false., itemCount=itemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Check for nested states
hasNestedState = .false.
if (itemCount > 0) then
! Allocate temporary data structures
allocate(itemNameList(itemCount))
allocate(itemTypeList(itemCount))
! Query state
call ESMF_StateGet(importState, nestedFlag=.false., itemNameList=itemNameList, itemTypeList=itemTypeList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Set flag if nested state is found
do n = 1, itemCount
if (itemTypeList(n) == ESMF_STATEITEM_STATE) hasNestedState = .true.
end do
end if
! Loop over nested states
if (hasNestedState) then
do n = 1, itemCount
if (itemTypeList(n) == ESMF_STATEITEM_STATE) then
! Get the associated nested state
call ESMF_StateGet(importState, itemName=itemNameList(n), nestedState=is_local%wrap%NStateImp(n), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Replace decomposition
call ModifyDecomp(is_local%wrap%NStateImp(n), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
end do
end if
! Clean memory
if (allocated(itemNameList)) deallocate(itemNameList)
if (allocated(itemTypeList)) deallocate(itemTypeList)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine AcceptTransfer
!-----------------------------------------------------------------------------
subroutine RealizeAccepted(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: n, m
integer :: itemCount
type(InternalState) :: is_local
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: itemTypeList(:)
character(len=*), parameter :: subname = trim(modName)//':(RealizeAccepted) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! Get the internal state
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Loop over states
do n = 1, is_local%wrap%numComp
! Query state
call ESMF_StateGet(is_local%wrap%NStateImp(n), itemCount=itemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data structures
allocate(itemNameList(itemCount))
allocate(itemTypeList(itemCount))
! Query state
call ESMF_StateGet(is_local%wrap%NStateImp(n), nestedFlag=.false., itemNameList=itemNameList, itemTypeList=itemTypeList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Loop over fields and realize them
do m = 1, itemCount
if (itemTypeList(m) == ESMF_STATEITEM_FIELD) then
! Replace grid with mesh
call GridToMesh(is_local%wrap%NStateImp(n), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Realize field
call NUOPC_Realize(is_local%wrap%NStateImp(n), fieldName=trim(itemNameList(m)), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
end do
! Clean memory
if (allocated(itemNameList)) deallocate(itemNameList)
if (allocated(itemTypeList)) deallocate(itemTypeList)
end do
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine RealizeAccepted
!-----------------------------------------------------------------------------
subroutine DataInitialize(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: n
type(InternalState) :: is_local
logical, save :: first_call = .true.
character(len=*), parameter :: subname = trim(modName)//':(DataInitialize) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! Get the internal state
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! First call block
if (first_call) then
! Create field bundle FBImp
do n = 1, is_local%wrap%numComp
if (ESMF_StateIsCreated(is_local%wrap%NStateImp(n), rc=rc)) then
! Print debug info
call ESMF_LogWrite(trim(subname)//': initializing FBs for '//trim(is_local%wrap%compName(n)), ESMF_LOGMSG_INFO)
! Create FBImp(:) with pointers directly into NStateImp(:)
call FB_init_pointer(is_local%wrap%NStateImp(n), is_local%wrap%FBImp(n), name='FBImp'//trim(is_local%wrap%compName(n)), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
end do
! Set first call flag
first_call = .false.
! Return
return
end if
! Set InitializeDataComplete Component Attribute to "true", indicating
! to the driver that this Component has fully initialized its data
call NUOPC_CompAttributeSet(gcomp, name="InitializeDataComplete", value="true", rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite("COP - Initialize-Data-Dependency allDone check Passed", ESMF_LOGMSG_INFO)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine DataInitialize
!-----------------------------------------------------------------------------
subroutine GridToMesh(state, rc)
! input/output variables
type(ESMF_State), intent(inout) :: state
integer, intent(out) :: rc
! local variables
integer :: n
integer :: itemCount
logical :: isPresent, meshCreated
type(ESMF_Field) :: field, meshField
type(ESMF_Grid) :: grid
type(ESMF_Mesh) :: mesh
type(ESMF_FieldStatus_Flag) :: fieldStatus
type(ESMF_GeomType_Flag) :: geomtype
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
character(ESMF_MAXSTR) :: fieldName
character(ESMF_MAXSTR) :: message
character(len=*), parameter :: subname = trim(modName)//':(GridToMesh) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! Query state
call ESMF_StateGet(state, itemCount=itemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data structures
allocate(itemNameList(itemCount))
! Query item name in nested state
call ESMF_StateGet(state, itemNameList=itemNameList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Loop over fields
meshCreated = .false.
do n = 1, itemCount
! Query state to get field
call ESMF_StateGet(state, field=field, itemName=itemNameList(n), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query field for its geom type
call ESMF_FieldGet(field, geomtype=geomtype, name=fieldName, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Input fields contains grid - need to convert to mesh
if (geomtype == ESMF_GEOMTYPE_GRID) then
! Grab grid
call ESMF_FieldGet(field, grid=grid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Check corner stagger
call ESMF_GridGetCoord(grid, staggerloc=ESMF_STAGGERLOC_CORNER, isPresent=isPresent, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
write(message,'(A,L)') trim(subname)//': has ESMF_STAGGERLOC_CORNER =', isPresent
call ESMF_LogWrite(message, ESMF_LOGMSG_INFO)
! Convert grid to mesh
if (.not. meshCreated) then
call ESMF_LogWrite(trim(subname)//": create mesh from grid using "//trim(fieldName), ESMF_LOGMSG_INFO)
mesh = ESMF_MeshCreate(grid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
meshCreated = .true.
end if
! Create field on mesh
meshField = ESMF_FieldCreate(mesh, typekind=ESMF_TYPEKIND_R8, meshloc=ESMF_MESHLOC_ELEMENT, name=trim(itemNameList(n)), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Swap grid for mesh, at this point, only connected fields are in the state
call NUOPC_Realize(state, field=meshField, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Check field
call ESMF_FieldGet(meshField, status=fieldStatus, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (fieldStatus == ESMF_FIELDSTATUS_GRIDSET ) then
call ESMF_LogWrite(trim(subname)//": ERROR fieldStatus not complete ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
end if ! fieldStatus
end if ! geomType
end do ! itemCount
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine GridToMesh
!-----------------------------------------------------------------------------
subroutine ModifyDecomp(state, rc)
! input/output variables
type(ESMF_State), intent(inout) :: state
integer, intent(out) :: rc
! local variables
integer :: n, m
integer :: itemCount, arbDimCount
integer :: dimCount, tileCount, connectionCount
type(ESMF_Grid) :: grid, newgrid
type(ESMF_Mesh) :: mesh, newmesh
type(ESMF_DistGrid) :: distgrid
type(ESMF_DistGrid) :: elemdistgrid, newelemdistgrid
type(ESMF_Field) :: field
type(ESMF_GeomType_Flag) :: geomtype
type(ESMF_FieldStatus_Flag) :: fieldStatus
type(ESMF_DistGridConnection), allocatable :: connectionList(:)
integer, allocatable :: minIndexPTile(:,:), maxIndexPTile(:,:)
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
character(ESMF_MAXSTR) :: message
character(len=*), parameter :: subname = trim(modName)//':(ModifyDecomp) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! Query state
call ESMF_StateGet(state, itemCount=itemCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data structures
allocate(itemNameList(itemCount))
! Query item name in nested state
call ESMF_StateGet(state, itemNameList=itemNameList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Loop over fields
! Assuming that all fields share the same grid/mesh and because it is more efficient
do n = 1, min(itemCount, 1)
! Query state to get field
call ESMF_StateGet(state, field=field, itemName=itemNameList(n), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query field to get its status
call ESMF_FieldGet(field, status=fieldStatus, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (fieldStatus == ESMF_FIELDSTATUS_GRIDSET) then
! Query field to get geom type
call ESMF_FieldGet(field, geomtype=geomtype, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (geomtype == ESMF_GEOMTYPE_GRID) then
call ESMF_LogWrite(trim(subname)//": geomtype is ESMF_GEOMTYPE_GRID for "//trim(itemNameList(n)), ESMF_LOGMSG_INFO)
! Query field to get arbitrary dimension count
call ESMF_AttributeGet(field, name="ArbDimCount", value=arbDimCount, &
convention="NUOPC", purpose="Instance", rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Make decision on whether the incoming Grid is arbDistr or not
if (arbDimCount > 0) then
! Allocate temporary data arrays
allocate(minIndexPTile(arbDimCount,1),maxIndexPTile(arbDimCount,1))
! Query min and max indexes
call ESMF_AttributeGet(field, name="MinIndex", valueList=minIndexPTile(:,1), convention="NUOPC", purpose="Instance", rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_AttributeGet(field, name="MaxIndex", valueList=maxIndexPTile(:,1), convention="NUOPC", purpose="Instance", rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new distgrid for new decomposition
distgrid = ESMF_DistGridCreate(minIndexPTile=minIndexPTile, maxIndexPTile=maxIndexPTile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new grid from new distgrid
newgrid = ESMF_GridCreate(distgrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
do m = 1, arbDimCount
write(message,'(A,3i8)') trim(subname)//':PTile =', m, minIndexPTile(m,1), maxIndexPTile(m,1)
call ESMF_LogWrite(message, ESMF_LOGMSG_INFO)
end do
else ! arbdimcount <= 0
! Query field for its grid
call ESMF_FieldGet(field, grid=grid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query grid for distgrid
call ESMF_GridGet(grid, distgrid=distgrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query distgrid properties
call ESMF_DistGridGet(distgrid, dimCount=dimCount, tileCount=tileCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data arrays
allocate(minIndexPTile(dimCount, tileCount))
allocate(maxIndexPTile(dimCount, tileCount))
! Query distgrid
call ESMF_DistGridGet(distgrid, minIndexPTile=minIndexPTile, maxIndexPTile=maxIndexPTile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (dimCount == 2) then
! Query distgrid to get connection count
call ESMF_DistGridGet(distgrid, connectionCount=connectionCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Allocate temporary data array
allocate(connectionList(connectionCount))
! Query distgrid for connectionList
call ESMF_DistGridGet(distgrid, connectionList=connectionList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
distgrid = ESMF_DistGridCreate(minIndexPTile=minIndexPTile, maxIndexPTile=maxIndexPTile, connectionList=connectionList, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new grid with new distgrid
newgrid = ESMF_GridCreate(distgrid, gridEdgeLWidth=(/0,0/), gridEdgeUWidth=(/0,1/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Clean memory
deallocate(connectionList)
else
! Create new distgrid
distgrid = ESMF_DistGridCreate(minIndexPTile=minIndexPTile, maxIndexPTile=maxIndexPTile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new grid
newgrid = ESMF_GridCreate(distgrid, gridEdgeLWidth=(/0/), gridEdgeUWidth=(/0/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if ! dimCount
! Clean memory
deallocate(minIndexPTile, maxIndexPTile)
endif ! arbdimCount
! Swap all grids in the state
do m = 1, itemCount
! Query state to get field
call ESMF_StateGet(state, field=field, itemName=itemNameList(m), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query field to get its status
call ESMF_FieldGet(field, status=fieldStatus, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (fieldStatus == ESMF_FIELDSTATUS_EMPTY .or. fieldStatus == ESMF_FIELDSTATUS_GRIDSET) then
call ESMF_FieldEmptySet(field, grid=newgrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//": attach grid for "//trim(itemNameList(m)), ESMF_LOGMSG_INFO)
else
call ESMF_LogWrite(trim(subname)//": NOT replacing grid for field: "//trim(itemNameList(m)), ESMF_LOGMSG_WARNING)
end if ! field status
end do ! fields
else if (geomtype == ESMF_GEOMTYPE_MESH) then
call ESMF_LogWrite(trim(subname)//": geomtype is ESMF_GEOMTYPE_MESH for "//trim(itemNameList(n)), ESMF_LOGMSG_INFO)
! Query field to get mesh
call ESMF_FieldGet(field, mesh=mesh, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query mesh to get decomposition information
call ESMF_MeshGet(mesh, elementDistGrid=elemDistGrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new decomposition based on existing one
newelemDistGrid = ESMF_DistGridCreate(elemDistGrid, balanceflag=.true., rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Create new mesh with new decomposition
newmesh = ESMF_MeshEmptyCreate(elementDistGrid=newelemDistGrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Swap all meshes in the state
do m = 1, itemCount
! Query state to get field
call ESMF_StateGet(state, field=field, itemName=itemNameList(m), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Query field to get its status
call ESMF_FieldGet(field, status=fieldStatus, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (fieldStatus == ESMF_FIELDSTATUS_EMPTY .or. fieldStatus == ESMF_FIELDSTATUS_GRIDSET) then
call ESMF_FieldEmptySet(field, mesh=newmesh, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//": attach mesh for "//trim(itemNameList(m)), ESMF_LOGMSG_INFO)
else
call ESMF_LogWrite(trim(subname)//": NOT replacing mesh for field: "//trim(itemNameList(m)), ESMF_LOGMSG_WARNING)
end if ! field status
end do ! fields
else
call ESMF_LogWrite(trim(subname)//": ERROR geomtype not supported ", ESMF_LOGMSG_ERROR)
rc=ESMF_FAILURE
return
end if ! geomtype
elseif (fieldStatus == ESMF_FIELDSTATUS_EMPTY) then
call ESMF_LogWrite(trim(subname)//": provide grid for "//trim(itemNameList(n)), ESMF_LOGMSG_INFO)
elseif (fieldStatus == ESMF_FIELDSTATUS_COMPLETE) then
call ESMF_LogWrite(trim(subname)//": no grid provided for "//trim(itemNameList(n)), ESMF_LOGMSG_INFO)
end if ! field status
end do ! fields
! Clean memory
deallocate(itemNameList)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine ModifyDecomp
!-----------------------------------------------------------------------------
subroutine Advance(gcomp, rc)
! input/output variables
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
type(ESMF_Time) :: currTime
type(ESMF_Clock) :: clock
type(ESMF_State) :: importState, exportState
logical :: isPresent, isSet
character(len=ESMF_MAXSTR) :: message
character(len=ESMF_MAXSTR) :: cvalue
character(len=ESMF_MAXSTR) :: timeStr
character(len=*), parameter :: subname = trim(modName)//':(Advance) '
!---------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine Advance
end module cop_comp_nuopc