forked from mom-ocean/MOM6
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmom_cap.F90
2532 lines (2187 loc) · 104 KB
/
mom_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
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
!> This module contains a set of subroutines that are required by NUOPC.
module MOM_cap_mod
use constants_mod, only: constants_init
use diag_manager_mod, only: diag_manager_init, diag_manager_end
use field_manager_mod, only: field_manager_init, field_manager_end
use fms_mod, only: fms_init, fms_end, open_namelist_file, check_nml_error
use fms_mod, only: close_file, file_exist, uppercase
use fms_io_mod, only: fms_io_exit
use mpp_domains_mod, only: domain2d, mpp_get_compute_domain, mpp_get_compute_domains
use mpp_domains_mod, only: mpp_get_ntile_count, mpp_get_pelist, mpp_get_global_domain
use mpp_domains_mod, only: mpp_get_domain_npes
use mpp_io_mod, only: mpp_open, MPP_RDONLY, MPP_ASCII, MPP_OVERWR, MPP_APPEND, mpp_close, MPP_SINGLE
use mpp_mod, only: stdlog, stdout, mpp_root_pe, mpp_clock_id
use mpp_mod, only: mpp_clock_begin, mpp_clock_end, MPP_CLOCK_SYNC
use mpp_mod, only: MPP_CLOCK_DETAILED, CLOCK_COMPONENT, MAXPES
use time_interp_external_mod, only: time_interp_external_init
use time_manager_mod, only: set_calendar_type, time_type, increment_date
use time_manager_mod, only: set_time, set_date, get_time, get_date, month_name
use time_manager_mod, only: GREGORIAN, JULIAN, NOLEAP, THIRTY_DAY_MONTHS, NO_CALENDAR
use time_manager_mod, only: operator( <= ), operator( < ), operator( >= )
use time_manager_mod, only: operator( + ), operator( - ), operator( / )
use time_manager_mod, only: operator( * ), operator( /= ), operator( > )
use time_manager_mod, only: date_to_string
use time_manager_mod, only: fms_get_calendar_type => get_calendar_type
use MOM_domains, only: MOM_infra_init, num_pes, root_pe, pe_here
use MOM_file_parser, only: get_param, log_version, param_file_type, close_param_file
use MOM_get_input, only: Get_MOM_Input, directories
use MOM_domains, only: pass_var
use MOM_error_handler, only: MOM_error, FATAL, is_root_pe
use MOM_ocean_model_nuopc, only: ice_ocean_boundary_type
use MOM_grid, only: ocean_grid_type, get_global_grid_size
use MOM_ocean_model_nuopc, only: ocean_model_restart, ocean_public_type, ocean_state_type
use MOM_ocean_model_nuopc, only: ocean_model_init_sfc
use MOM_ocean_model_nuopc, only: ocean_model_init, update_ocean_model, ocean_model_end
use MOM_ocean_model_nuopc, only: get_ocean_grid, get_eps_omesh
use MOM_cap_time, only: AlarmInit
use MOM_cap_methods, only: mom_import, mom_export, mom_set_geomtype
#ifdef CESMCOUPLED
use shr_file_mod, only: shr_file_setLogUnit, shr_file_getLogUnit
#endif
use time_utils_mod, only: esmf2fms_time
use, intrinsic :: iso_fortran_env, only: output_unit
use ESMF, only: ESMF_ClockAdvance, ESMF_ClockGet, ESMF_ClockPrint
use ESMF, only: ESMF_ClockGetAlarm, ESMF_ClockGetNextTime, ESMF_ClockAdvance
use ESMF, only: ESMF_ClockSet, ESMF_Clock, ESMF_GeomType_Flag, ESMF_LOGMSG_INFO
use ESMF, only: ESMF_Grid, ESMF_GridCreate, ESMF_GridAddCoord
use ESMF, only: ESMF_GridGetCoord, ESMF_GridAddItem, ESMF_GridGetItem
use ESMF, only: ESMF_GridComp, ESMF_GridCompSetEntryPoint, ESMF_GridCompGet
use ESMF, only: ESMF_LogFoundError, ESMF_LogWrite, ESMF_LogSetError
use ESMF, only: ESMF_LOGERR_PASSTHRU, ESMF_KIND_R8, ESMF_RC_VAL_WRONG
use ESMF, only: ESMF_GEOMTYPE_MESH, ESMF_GEOMTYPE_GRID, ESMF_SUCCESS
use ESMF, only: ESMF_METHOD_INITIALIZE, ESMF_MethodRemove, ESMF_State
use ESMF, only: ESMF_LOGMSG_INFO, ESMF_RC_ARG_BAD, ESMF_VM, ESMF_Time
use ESMF, only: ESMF_TimeInterval, ESMF_MAXSTR, ESMF_VMGetCurrent
use ESMF, only: ESMF_VMGet, ESMF_TimeGet, ESMF_TimeIntervalGet, ESMF_MeshGet
use ESMF, only: ESMF_MethodExecute, ESMF_Mesh, ESMF_DeLayout, ESMF_Distgrid
use ESMF, only: ESMF_DistGridConnection, ESMF_StateItem_Flag, ESMF_KIND_I4
use ESMF, only: ESMF_KIND_I8, ESMF_FAILURE, ESMF_DistGridCreate, ESMF_MeshCreate
use ESMF, only: ESMF_FILEFORMAT_ESMFMESH, ESMF_DELayoutCreate, ESMF_DistGridConnectionSet
use ESMF, only: ESMF_DistGridGet, ESMF_STAGGERLOC_CORNER, ESMF_GRIDITEM_MASK
use ESMF, only: ESMF_TYPEKIND_I4, ESMF_TYPEKIND_R8, ESMF_STAGGERLOC_CENTER
use ESMF, only: ESMF_GRIDITEM_AREA, ESMF_Field, ESMF_ALARM, ESMF_VMLogMemInfo
use ESMF, only: ESMF_AlarmIsRinging, ESMF_AlarmRingerOff, ESMF_StateRemove
use ESMF, only: ESMF_FieldCreate, ESMF_LOGMSG_ERROR, ESMF_LOGMSG_WARNING
use ESMF, only: ESMF_COORDSYS_SPH_DEG, ESMF_GridCreate, ESMF_INDEX_DELOCAL
use ESMF, only: ESMF_MESHLOC_ELEMENT, ESMF_RC_VAL_OUTOFRANGE, ESMF_StateGet
use ESMF, only: ESMF_TimePrint, ESMF_AlarmSet, ESMF_FieldGet, ESMF_Array
use ESMF, only: ESMF_ArrayCreate
use ESMF, only: ESMF_RC_FILE_OPEN, ESMF_RC_FILE_READ, ESMF_RC_FILE_WRITE
use ESMF, only: ESMF_VMBroadcast
use ESMF, only: ESMF_AlarmCreate, ESMF_ClockGetAlarmList, ESMF_AlarmList_Flag
use ESMF, only: ESMF_AlarmGet, ESMF_AlarmIsCreated, ESMF_ALARMLIST_ALL, ESMF_AlarmIsEnabled
use ESMF, only: ESMF_STATEITEM_NOTFOUND, ESMF_FieldWrite
use ESMF, only: operator(==), operator(/=), operator(+), operator(-)
! TODO ESMF_GridCompGetInternalState does not have an explicit Fortran interface.
!! Model does not compile with "use ESMF, only: ESMF_GridCompGetInternalState"
!! Is this okay?
use NUOPC, only: NUOPC_CompDerive, NUOPC_CompSetEntryPoint, NUOPC_CompSpecialize
use NUOPC, only: NUOPC_CompFilterPhaseMap, NUOPC_CompAttributeGet, NUOPC_CompAttributeAdd
use NUOPC, only: NUOPC_Advertise, NUOPC_SetAttribute, NUOPC_IsUpdated, NUOPC_Write
use NUOPC, only: NUOPC_IsConnected, NUOPC_Realize, NUOPC_CompAttributeSet
use NUOPC_Model, only: NUOPC_ModelGet
use NUOPC_Model, only: model_routine_SS => SetServices
use NUOPC_Model, only: model_label_Advance => label_Advance
use NUOPC_Model, only: model_label_DataInitialize => label_DataInitialize
use NUOPC_Model, only: model_label_SetRunClock => label_SetRunClock
use NUOPC_Model, only: model_label_Finalize => label_Finalize
use NUOPC_Model, only: SetVM
implicit none; private
public SetServices
public SetVM
!> Internal state type with pointers to three types defined by MOM.
type ocean_internalstate_type
type(ocean_public_type), pointer :: ocean_public_type_ptr
type(ocean_state_type), pointer :: ocean_state_type_ptr
type(ice_ocean_boundary_type), pointer :: ice_ocean_boundary_type_ptr
end type
!> Wrapper-derived type required to associate an internal state instance
!! with the ESMF/NUOPC component
type ocean_internalstate_wrapper
type(ocean_internalstate_type), pointer :: ptr
end type
!> Contains field information
type fld_list_type
character(len=64) :: stdname
character(len=64) :: shortname
character(len=64) :: transferOffer
end type fld_list_type
integer,parameter :: fldsMax = 100
integer :: fldsToOcn_num = 0
type (fld_list_type) :: fldsToOcn(fldsMax)
integer :: fldsFrOcn_num = 0
type (fld_list_type) :: fldsFrOcn(fldsMax)
integer :: debug = 0
integer :: import_slice = 1
integer :: export_slice = 1
character(len=256) :: tmpstr
logical :: write_diagnostics = .false.
logical :: overwrite_timeslice = .false.
character(len=32) :: runtype !< run type
integer :: logunit !< stdout logging unit number
logical :: profile_memory = .true.
logical :: grid_attach_area = .false.
character(len=128) :: scalar_field_name = ''
integer :: scalar_field_count = 0
integer :: scalar_field_idx_grid_nx = 0
integer :: scalar_field_idx_grid_ny = 0
character(len=*),parameter :: u_FILE_u = &
__FILE__
#ifdef CESMCOUPLED
logical :: cesm_coupled = .true.
type(ESMF_GeomType_Flag) :: geomtype = ESMF_GEOMTYPE_MESH
#else
logical :: cesm_coupled = .false.
type(ESMF_GeomType_Flag) :: geomtype = ESMF_GEOMTYPE_GRID
#endif
character(len=8) :: restart_mode = 'cmeps'
contains
!> NUOPC SetService method is the only public entry point.
!! SetServices registers all of the user-provided subroutines
!! in the module with the NUOPC layer.
!!
!! @param gcomp an ESMF_GridComp object
!! @param rc return code
subroutine SetServices(gcomp, rc)
type(ESMF_GridComp) :: gcomp !< an ESMF_GridComp object
integer, intent(out) :: rc !< return code
! local variables
character(len=*),parameter :: subname='(MOM_cap:SetServices)'
rc = ESMF_SUCCESS
! the NUOPC model component will register the generic methods
call NUOPC_CompDerive(gcomp, model_routine_SS, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! switching to IPD versions
call ESMF_GridCompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
userRoutine=InitializeP0, phase=0, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! set entry point for methods that require specific implementation
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p1"/), userRoutine=InitializeAdvertise, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p3"/), userRoutine=InitializeRealize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!------------------
! attach specializing method(s)
!------------------
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_DataInitialize, &
specRoutine=DataInitialize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Advance, &
specRoutine=ModelAdvance, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_MethodRemove(gcomp, label=model_label_SetRunClock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_SetRunClock, &
specRoutine=ModelSetRunClock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Finalize, &
specRoutine=ocean_model_finalize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end subroutine SetServices
!> First initialize subroutine called by NUOPC. The purpose
!! is to set which version of the Initialize Phase Definition (IPD)
!! to use.
!!
!! For this MOM cap, we are using IPDv01.
!!
!! @param gcomp an ESMF_GridComp object
!! @param importState an ESMF_State object for import fields
!! @param exportState an ESMF_State object for export fields
!! @param clock an ESMF_Clock object
!! @param rc return code
subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp !< ESMF_GridComp object
type(ESMF_State) :: importState, exportState !< ESMF_State object for
!! import/export fields
type(ESMF_Clock) :: clock !< ESMF_Clock object
integer, intent(out) :: rc !< return code
! local variables
logical :: isPresent, isSet
integer :: iostat
character(len=64) :: value, logmsg
character(len=*),parameter :: subname='(MOM_cap:InitializeP0)'
rc = ESMF_SUCCESS
! Switch to IPDv03 by filtering all other phaseMap entries
call NUOPC_CompFilterPhaseMap(gcomp, ESMF_METHOD_INITIALIZE, &
acceptStringList=(/"IPDv03p"/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
write_diagnostics = .false.
call NUOPC_CompAttributeGet(gcomp, name="DumpFields", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) write_diagnostics=(trim(value)=="true")
write(logmsg,*) write_diagnostics
call ESMF_LogWrite('MOM_cap:DumpFields = '//trim(logmsg), ESMF_LOGMSG_INFO)
overwrite_timeslice = .false.
call NUOPC_CompAttributeGet(gcomp, name="OverwriteSlice", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) overwrite_timeslice=(trim(value)=="true")
write(logmsg,*) overwrite_timeslice
call ESMF_LogWrite('MOM_cap:OverwriteSlice = '//trim(logmsg), ESMF_LOGMSG_INFO)
profile_memory = .false.
call NUOPC_CompAttributeGet(gcomp, name="ProfileMemory", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) profile_memory=(trim(value)=="true")
write(logmsg,*) profile_memory
call ESMF_LogWrite('MOM_cap:ProfileMemory = '//trim(logmsg), ESMF_LOGMSG_INFO)
grid_attach_area = .false.
call NUOPC_CompAttributeGet(gcomp, name="GridAttachArea", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) grid_attach_area=(trim(value)=="true")
write(logmsg,*) grid_attach_area
call ESMF_LogWrite('MOM_cap:GridAttachArea = '//trim(logmsg), ESMF_LOGMSG_INFO)
scalar_field_name = ""
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldName", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
scalar_field_name = trim(value)
call ESMF_LogWrite('MOM_cap:ScalarFieldName = '//trim(scalar_field_name), ESMF_LOGMSG_INFO)
endif
scalar_field_count = 0
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldCount", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(value, '(i)', iostat=iostat) scalar_field_count
if (iostat /= 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg=subname//": ScalarFieldCount not an integer: "//trim(value), &
line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
write(logmsg,*) scalar_field_count
call ESMF_LogWrite('MOM_cap:ScalarFieldCount = '//trim(logmsg), ESMF_LOGMSG_INFO)
endif
scalar_field_idx_grid_nx = 0
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxGridNX", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(value, '(i)', iostat=iostat) scalar_field_idx_grid_nx
if (iostat /= 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg=subname//": ScalarFieldIdxGridNX not an integer: "//trim(value), &
line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
write(logmsg,*) scalar_field_idx_grid_nx
call ESMF_LogWrite('MOM_cap:ScalarFieldIdxGridNX = '//trim(logmsg), ESMF_LOGMSG_INFO)
endif
scalar_field_idx_grid_ny = 0
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxGridNY", value=value, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(value, '(i)', iostat=iostat) scalar_field_idx_grid_ny
if (iostat /= 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg=subname//": ScalarFieldIdxGridNY not an integer: "//trim(value), &
line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
write(logmsg,*) scalar_field_idx_grid_ny
call ESMF_LogWrite('MOM_cap:ScalarFieldIdxGridNY = '//trim(logmsg), ESMF_LOGMSG_INFO)
endif
end subroutine
!> Called by NUOPC to advertise import and export fields. "Advertise"
!! simply means that the standard names of all import and export
!! fields are supplied. The NUOPC layer uses these to match fields
!! between components in the coupled system.
!!
!! @param gcomp an ESMF_GridComp object
!! @param importState an ESMF_State object for import fields
!! @param exportState an ESMF_State object for export fields
!! @param clock an ESMF_Clock object
!! @param rc return code
subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp !< ESMF_GridComp object
type(ESMF_State) :: importState, exportState !< ESMF_State object for
!! import/export fields
type(ESMF_Clock) :: clock !< ESMF_Clock object
integer, intent(out) :: rc !< return code
! local variables
type(ESMF_VM) :: vm
type(ESMF_Time) :: MyTime
type(ESMF_TimeInterval) :: TINT
type (ocean_public_type), pointer :: ocean_public => NULL()
type (ocean_state_type), pointer :: ocean_state => NULL()
type(ice_ocean_boundary_type), pointer :: Ice_ocean_boundary => NULL()
type(ocean_internalstate_wrapper) :: ocean_internalstate
type(ocean_grid_type), pointer :: ocean_grid => NULL()
type(time_type) :: Run_len !< length of experiment
type(time_type) :: time0 !< Start time of coupled model's calendar.
type(time_type) :: time_start !< The time at which to initialize the ocean model
type(time_type) :: Time_restart
type(time_type) :: DT
integer :: DT_OCEAN
integer :: isc,iec,jsc,jec
integer :: year=0, month=0, day=0, hour=0, minute=0, second=0
integer :: mpi_comm_mom
integer :: i,n
character(len=256) :: stdname, shortname
character(len=32) :: starttype ! model start type
character(len=512) :: diro
character(len=512) :: logfile
character(ESMF_MAXSTR) :: cvalue
logical :: isPresent, isPresentDiro, isPresentLogfile, isSet
logical :: existflag
integer :: userRc
integer :: localPet
integer :: iostat
integer :: readunit
character(len=512) :: restartfile ! Path/Name of restart file
character(len=*), parameter :: subname='(MOM_cap:InitializeAdvertise)'
character(len=32) :: calendar
!--------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' enter', ESMF_LOGMSG_INFO)
allocate(Ice_ocean_boundary)
!allocate(ocean_state) ! ocean_model_init allocate this pointer
allocate(ocean_public)
allocate(ocean_internalstate%ptr)
ocean_internalstate%ptr%ice_ocean_boundary_type_ptr => Ice_ocean_boundary
ocean_internalstate%ptr%ocean_public_type_ptr => ocean_public
ocean_internalstate%ptr%ocean_state_type_ptr => ocean_state
call ESMF_VMGetCurrent(vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(VM, mpiCommunicator=mpi_comm_mom, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_ClockGet(CLOCK, currTIME=MyTime, TimeStep=TINT, RC=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_TimeGet (MyTime, YY=YEAR, MM=MONTH, DD=DAY, H=HOUR, M=MINUTE, S=SECOND, RC=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
CALL ESMF_TimeIntervalGet(TINT, S=DT_OCEAN, RC=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call fms_init(mpi_comm_mom)
call constants_init
call field_manager_init
! determine the calendar
if (cesm_coupled) then
call NUOPC_CompAttributeGet(gcomp, name="calendar", value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) calendar
select case (trim(calendar))
case ("NO_LEAP")
call set_calendar_type (NOLEAP)
case ("GREGORIAN")
call set_calendar_type (GREGORIAN)
case default
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg=subname//": Calendar not supported in MOM6: "//trim(calendar), &
line=__LINE__, file=__FILE__, rcToReturn=rc)
end select
else
call set_calendar_type (NOLEAP)
endif
else
call set_calendar_type (JULIAN)
endif
call diag_manager_init
! this ocean connector will be driven at set interval
DT = set_time (DT_OCEAN, 0)
! get current time
time_start = set_date (YEAR,MONTH,DAY,HOUR,MINUTE,SECOND)
if (is_root_pe()) then
write(logunit,*) subname//'current time: y,m,d-',year,month,day,'h,m,s=',hour,minute,second
endif
! get start/reference time
call ESMF_ClockGet(CLOCK, refTime=MyTime, RC=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_TimeGet (MyTime, YY=YEAR, MM=MONTH, DD=DAY, H=HOUR, M=MINUTE, S=SECOND, RC=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
time0 = set_date (YEAR,MONTH,DAY,HOUR,MINUTE,SECOND)
if (is_root_pe()) then
write(logunit,*) subname//'start time: y,m,d-',year,month,day,'h,m,s=',hour,minute,second
endif
! rsd need to figure out how to get this without share code
!call shr_nuopc_get_component_instance(gcomp, inst_suffix, inst_index)
!inst_name = "OCN"//trim(inst_suffix)
! reset shr logging to my log file
if (is_root_pe()) then
call NUOPC_CompAttributeGet(gcomp, name="diro", &
isPresent=isPresentDiro, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(gcomp, name="logfile", &
isPresent=isPresentLogfile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresentDiro .and. isPresentLogfile) then
call NUOPC_CompAttributeGet(gcomp, name="diro", value=diro, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(gcomp, name="logfile", value=logfile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
open(newunit=logunit,file=trim(diro)//"/"//trim(logfile))
else
logunit = output_unit
endif
else
logunit = output_unit
endif
starttype = ""
call NUOPC_CompAttributeGet(gcomp, name='start_type', value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) starttype
else
call ESMF_LogWrite('MOM_cap:start_type unset - using input.nml for restart option', &
ESMF_LOGMSG_INFO)
endif
runtype = ""
if (trim(starttype) == trim('startup')) then
runtype = "initial"
else if (trim(starttype) == trim('continue') ) then
runtype = "continue"
else if (trim(starttype) == trim('branch')) then
runtype = "continue"
else if (len_trim(starttype) > 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg=subname//": unknown starttype - "//trim(starttype), &
line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
if (len_trim(runtype) > 0) then
call ESMF_LogWrite('MOM_cap:startup = '//trim(runtype), ESMF_LOGMSG_INFO)
endif
restartfile = ""
if (runtype == "initial") then
restartfile = "n"
else if (runtype == "continue") then ! hybrid or branch or continuos runs
if (cesm_coupled) then
call ESMF_LogWrite('MOM_cap: restart requested, using rpointer.ocn', ESMF_LOGMSG_WARNING)
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=u_FILE_u)) return
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=u_FILE_u)) return
if (localPet == 0) then
! this hard coded for rpointer.ocn right now
open(newunit=readunit, file='rpointer.ocn', form='formatted', status='old', iostat=iostat)
if (iostat /= 0) then
call ESMF_LogSetError(ESMF_RC_FILE_OPEN, msg=subname//' ERROR opening rpointer.ocn', &
line=__LINE__, file=u_FILE_u, rcToReturn=rc)
return
endif
read(readunit,'(a)', iostat=iostat) restartfile
if (iostat /= 0) then
call ESMF_LogSetError(ESMF_RC_FILE_READ, msg=subname//' ERROR reading rpointer.ocn', &
line=__LINE__, file=u_FILE_u, rcToReturn=rc)
return
endif
close(readunit)
endif
! broadcast attribute set on master task to all tasks
call ESMF_VMBroadcast(vm, restartfile, count=ESMF_MAXSTR-1, rootPet=0, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=u_FILE_u)) return
else
call ESMF_LogWrite('MOM_cap: restart requested, use input.nml', ESMF_LOGMSG_WARNING)
endif
endif
ocean_public%is_ocean_pe = .true.
call ocean_model_init(ocean_public, ocean_state, time0, time_start, input_restart_file=trim(restartfile))
call ocean_model_init_sfc(ocean_state, ocean_public)
call mpp_get_compute_domain(ocean_public%domain, isc, iec, jsc, jec)
allocate ( Ice_ocean_boundary% u_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% v_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% t_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% q_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% salt_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% lw_flux (isc:iec,jsc:jec), &
Ice_ocean_boundary% sw_flux_vis_dir (isc:iec,jsc:jec), &
Ice_ocean_boundary% sw_flux_vis_dif (isc:iec,jsc:jec), &
Ice_ocean_boundary% sw_flux_nir_dir (isc:iec,jsc:jec), &
Ice_ocean_boundary% sw_flux_nir_dif (isc:iec,jsc:jec), &
Ice_ocean_boundary% lprec (isc:iec,jsc:jec), &
Ice_ocean_boundary% fprec (isc:iec,jsc:jec), &
Ice_ocean_boundary% seaice_melt_heat (isc:iec,jsc:jec),&
Ice_ocean_boundary% seaice_melt (isc:iec,jsc:jec), &
Ice_ocean_boundary% mi (isc:iec,jsc:jec), &
Ice_ocean_boundary% p (isc:iec,jsc:jec), &
Ice_ocean_boundary% lrunoff_hflx (isc:iec,jsc:jec), &
Ice_ocean_boundary% frunoff_hflx (isc:iec,jsc:jec), &
Ice_ocean_boundary% lrunoff (isc:iec,jsc:jec), &
Ice_ocean_boundary% frunoff (isc:iec,jsc:jec))
Ice_ocean_boundary%u_flux = 0.0
Ice_ocean_boundary%v_flux = 0.0
Ice_ocean_boundary%t_flux = 0.0
Ice_ocean_boundary%q_flux = 0.0
Ice_ocean_boundary%salt_flux = 0.0
Ice_ocean_boundary%lw_flux = 0.0
Ice_ocean_boundary%sw_flux_vis_dir = 0.0
Ice_ocean_boundary%sw_flux_vis_dif = 0.0
Ice_ocean_boundary%sw_flux_nir_dir = 0.0
Ice_ocean_boundary%sw_flux_nir_dif = 0.0
Ice_ocean_boundary%lprec = 0.0
Ice_ocean_boundary%fprec = 0.0
Ice_ocean_boundary%seaice_melt = 0.0
Ice_ocean_boundary%seaice_melt_heat= 0.0
Ice_ocean_boundary%mi = 0.0
Ice_ocean_boundary%p = 0.0
Ice_ocean_boundary%lrunoff_hflx = 0.0
Ice_ocean_boundary%frunoff_hflx = 0.0
Ice_ocean_boundary%lrunoff = 0.0
Ice_ocean_boundary%frunoff = 0.0
if (ocean_state%use_waves) then
Ice_ocean_boundary%num_stk_bands=ocean_state%Waves%NumBands
allocate ( Ice_ocean_boundary% ustk0 (isc:iec,jsc:jec), &
Ice_ocean_boundary% vstk0 (isc:iec,jsc:jec), &
Ice_ocean_boundary% ustkb (isc:iec,jsc:jec,Ice_ocean_boundary%num_stk_bands), &
Ice_ocean_boundary% vstkb (isc:iec,jsc:jec,Ice_ocean_boundary%num_stk_bands), &
Ice_ocean_boundary%stk_wavenumbers (Ice_ocean_boundary%num_stk_bands))
Ice_ocean_boundary%ustk0 = 0.0
Ice_ocean_boundary%vstk0 = 0.0
Ice_ocean_boundary%stk_wavenumbers = ocean_state%Waves%WaveNum_Cen
Ice_ocean_boundary%ustkb = 0.0
Ice_ocean_boundary%vstkb = 0.0
endif
ocean_internalstate%ptr%ocean_state_type_ptr => ocean_state
call ESMF_GridCompSetInternalState(gcomp, ocean_internalstate, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (len_trim(scalar_field_name) > 0) then
call fld_list_add(fldsToOcn_num, fldsToOcn, trim(scalar_field_name), "will_provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, trim(scalar_field_name), "will_provide")
end if
if (cesm_coupled) then
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Sw_lamult" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Sw_ustokes" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Sw_vstokes" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Sw_hstokes" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Fioi_melth" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "Fioi_meltw" , "will provide")
!call fld_list_add(fldsFrOcn_num, fldsFrOcn, "So_fswpen" , "will provide")
else
!call fld_list_add(fldsToOcn_num, fldsToOcn, "mass_of_overlying_sea_ice" , "will provide")
!call fld_list_add(fldsFrOcn_num, fldsFrOcn, "sea_lev" , "will provide")
endif
!--------- import fields -------------
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_salt_rate" , "will provide") ! from ice
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_zonal_moment_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_merid_moment_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_sensi_heat_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_evap_rate" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_net_lw_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_net_sw_vis_dir_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_net_sw_vis_dif_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_net_sw_ir_dir_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_net_sw_ir_dif_flx" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_prec_rate" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_fprec_rate" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "inst_pres_height_surface" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "Foxx_rofl" , "will provide") !-> liquid runoff
call fld_list_add(fldsToOcn_num, fldsToOcn, "Foxx_rofi" , "will provide") !-> ice runoff
call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_fresh_water_to_ocean_rate", "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "net_heat_flx_to_ocn" , "will provide")
!These are not currently used and changing requires a nuopc dictionary change
!call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_runoff_heat_flx" , "will provide")
!call fld_list_add(fldsToOcn_num, fldsToOcn, "mean_calving_heat_flx" , "will provide")
if (ocean_state%use_waves) then
if (Ice_ocean_boundary%num_stk_bands > 3) then
call MOM_error(FATAL, "Number of Stokes Bands > 3, NUOPC cap not set up for this")
endif
call fld_list_add(fldsToOcn_num, fldsToOcn, "eastward_partitioned_stokes_drift_1" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "northward_partitioned_stokes_drift_1", "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "eastward_partitioned_stokes_drift_2" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "northward_partitioned_stokes_drift_2", "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "eastward_partitioned_stokes_drift_3" , "will provide")
call fld_list_add(fldsToOcn_num, fldsToOcn, "northward_partitioned_stokes_drift_3", "will provide")
endif
!--------- export fields -------------
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "ocean_mask" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "sea_surface_temperature" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "s_surf" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "ocn_current_zonal" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "ocn_current_merid" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "sea_surface_slope_zonal" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "sea_surface_slope_merid" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "freezing_melting_potential" , "will provide")
call fld_list_add(fldsFrOcn_num, fldsFrOcn, "So_bldepth" , "will provide")
do n = 1,fldsToOcn_num
call NUOPC_Advertise(importState, standardName=fldsToOcn(n)%stdname, name=fldsToOcn(n)%shortname, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
enddo
do n = 1,fldsFrOcn_num
call NUOPC_Advertise(exportState, standardName=fldsFrOcn(n)%stdname, name=fldsFrOcn(n)%shortname, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
enddo
end subroutine InitializeAdvertise
!> Called by NUOPC to realize import and export fields. "Realizing" a field
!! means that its grid has been defined and an ESMF_Field object has been
!! created and put into the import or export State.
!!
!! @param gcomp an ESMF_GridComp object
!! @param importState an ESMF_State object for import fields
!! @param exportState an ESMF_State object for export fields
!! @param clock an ESMF_Clock object
!! @param rc return code
subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp !< ESMF_GridComp object
type(ESMF_State) :: importState, exportState !< ESMF_State object for
!! import/export fields
type(ESMF_Clock) :: clock !< ESMF_Clock object
integer, intent(out) :: rc !< return code
! Local Variables
type(ESMF_VM) :: vm
type(ESMF_Grid) :: gridIn, gridOut
type(ESMF_Mesh) :: Emesh, EmeshTemp
type(ESMF_DeLayout) :: delayout
type(ESMF_Distgrid) :: Distgrid
type(ESMF_DistGridConnection), allocatable :: connectionList(:)
type(ESMF_StateItem_Flag) :: itemFlag
type (ocean_public_type), pointer :: ocean_public => NULL()
type (ocean_state_type), pointer :: ocean_state => NULL()
type(ice_ocean_boundary_type), pointer :: Ice_ocean_boundary => NULL()
type(ocean_grid_type) , pointer :: ocean_grid
type(ocean_internalstate_wrapper) :: ocean_internalstate
integer :: npet, ntiles
integer :: nxg, nyg, cnt
integer :: isc,iec,jsc,jec
integer, allocatable :: xb(:),xe(:),yb(:),ye(:),pe(:)
integer, allocatable :: deBlockList(:,:,:)
integer, allocatable :: petMap(:)
integer, allocatable :: deLabelList(:)
integer, allocatable :: indexList(:)
integer :: ioff, joff
integer :: i, j, n, i1, j1, n1, jlast
integer :: lbnd1,ubnd1,lbnd2,ubnd2
integer :: lbnd3,ubnd3,lbnd4,ubnd4
integer :: nblocks_tot
logical :: found
integer(ESMF_KIND_I4), pointer :: dataPtr_mask(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr_area(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr_xcen(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr_ycen(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr_xcor(:,:)
real(ESMF_KIND_R8), pointer :: dataPtr_ycor(:,:)
integer :: mpicom
integer :: localPet
integer :: lsize
integer :: ig,jg, ni,nj,k
integer, allocatable :: gindex(:) ! global index space
character(len=128) :: fldname
character(len=256) :: cvalue
character(len=256) :: frmt ! format specifier for several error msgs
character(len=512) :: err_msg ! error messages
character(len=*), parameter :: subname='(MOM_cap:InitializeRealize)'
integer :: spatialDim
integer :: numOwnedElements
type(ESMF_Array) :: elemMaskArray
real(ESMF_KIND_R8) , pointer :: ownedElemCoords(:)
real(ESMF_KIND_R8) , pointer :: lat(:), latMesh(:)
real(ESMF_KIND_R8) , pointer :: lon(:), lonMesh(:)
integer(ESMF_KIND_I4) , pointer :: mask(:), maskMesh(:)
real(ESMF_KIND_R8) :: diff_lon, diff_lat
real :: eps_omesh
!--------------------------------
rc = ESMF_SUCCESS
call shr_file_setLogUnit (logunit)
!----------------------------------------------------------------------------
! Get pointers to ocean internal state
!----------------------------------------------------------------------------
call ESMF_GridCompGetInternalState(gcomp, ocean_internalstate, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
Ice_ocean_boundary => ocean_internalstate%ptr%ice_ocean_boundary_type_ptr
ocean_public => ocean_internalstate%ptr%ocean_public_type_ptr
ocean_state => ocean_internalstate%ptr%ocean_state_type_ptr
!----------------------------------------------------------------------------
! Get mpi information
!----------------------------------------------------------------------------
call ESMF_VMGetCurrent(vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, petCount=npet, mpiCommunicator=mpicom, localPet=localPet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------
! global mom grid size
!---------------------------------
call mpp_get_global_domain(ocean_public%domain, xsize=nxg, ysize=nyg)
write(tmpstr,'(a,2i6)') subname//' nxg,nyg = ',nxg,nyg
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
!---------------------------------
! number of tiles per PET, assumed to be 1, and number of pes (tiles) total
!---------------------------------
ntiles=mpp_get_ntile_count(ocean_public%domain) ! this is tiles on this pe
if (ntiles /= 1) then
rc = ESMF_FAILURE
call ESMF_LogWrite(subname//' ntiles must be 1', ESMF_LOGMSG_ERROR)
endif
ntiles=mpp_get_domain_npes(ocean_public%domain)
write(tmpstr,'(a,1i6)') subname//' ntiles = ',ntiles
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
!---------------------------------
! get start and end indices of each tile and their PET
!---------------------------------
allocate(xb(ntiles),xe(ntiles),yb(ntiles),ye(ntiles),pe(ntiles))
call mpp_get_compute_domains(ocean_public%domain, xbegin=xb, xend=xe, ybegin=yb, yend=ye)
call mpp_get_pelist(ocean_public%domain, pe)
if (debug > 0) then
do n = 1,ntiles
write(tmpstr,'(a,6i6)') subname//' tiles ',n,pe(n),xb(n),xe(n),yb(n),ye(n)
call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
enddo
endif
!---------------------------------
! Create either a grid or a mesh
!---------------------------------
!Get the ocean grid and sizes of global and computational domains
call get_ocean_grid(ocean_state, ocean_grid)
if (geomtype == ESMF_GEOMTYPE_MESH) then
!---------------------------------
! Create a MOM6 mesh
!---------------------------------
call get_global_grid_size(ocean_grid, ni, nj)
lsize = ( ocean_grid%iec - ocean_grid%isc + 1 ) * ( ocean_grid%jec - ocean_grid%jsc + 1 )
! Create the global index space for the computational domain
allocate(gindex(lsize))
k = 0
do j = ocean_grid%jsc, ocean_grid%jec
jg = j + ocean_grid%jdg_offset
do i = ocean_grid%isc, ocean_grid%iec
ig = i + ocean_grid%idg_offset
k = k + 1 ! Increment position within gindex
gindex(k) = ni * (jg - 1) + ig
enddo
enddo
DistGrid = ESMF_DistGridCreate(arbSeqIndexList=gindex, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! read in the mesh
call NUOPC_CompAttributeGet(gcomp, name='mesh_ocn', value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
EMeshTemp = ESMF_MeshCreate(filename=trim(cvalue), fileformat=ESMF_FILEFORMAT_ESMFMESH, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (localPet == 0) then
write(logunit,*)'mesh file for mom6 domain is ',trim(cvalue)
endif
! recreate the mesh using the above distGrid
EMesh = ESMF_MeshCreate(EMeshTemp, elementDistgrid=Distgrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Check for consistency of lat, lon and mask between mesh and mom6 grid
call ESMF_MeshGet(Emesh, spatialDim=spatialDim, numOwnedElements=numOwnedElements, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(ownedElemCoords(spatialDim*numOwnedElements))
allocate(lonMesh(numOwnedElements), lon(numOwnedElements))
allocate(latMesh(numOwnedElements), lat(numOwnedElements))
allocate(maskMesh(numOwnedElements), mask(numOwnedElements))
call ESMF_MeshGet(Emesh, ownedElemCoords=ownedElemCoords, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
do n = 1,numOwnedElements
lonMesh(n) = ownedElemCoords(2*n-1)
latMesh(n) = ownedElemCoords(2*n)
end do
elemMaskArray = ESMF_ArrayCreate(Distgrid, maskMesh, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_MeshGet(Emesh, elemMaskArray=elemMaskArray, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call mpp_get_compute_domain(ocean_public%domain, isc, iec, jsc, jec)
n = 0
do j = jsc, jec
jg = j + ocean_grid%jsc - jsc
do i = isc, iec
ig = i + ocean_grid%isc - isc
n = n+1
mask(n) = ocean_grid%mask2dT(ig,jg)
lon(n) = ocean_grid%geolonT(ig,jg)
lat(n) = ocean_grid%geolatT(ig,jg)
end do
end do
eps_omesh = get_eps_omesh(ocean_state)
do n = 1,numOwnedElements
diff_lon = abs(mod(lonMesh(n) - lon(n),360.0))
if (diff_lon > eps_omesh) then
frmt = "('ERROR: Difference between ESMF Mesh and MOM6 domain coords is "//&
"greater than parameter EPS_OMESH. n, lonMesh(n), lon(n), diff_lon, "//&
"EPS_OMESH= ',i8,2(f21.13,3x),2(d21.5))"
write(err_msg, frmt)n,lonMesh(n),lon(n), diff_lon, eps_omesh
call MOM_error(FATAL, err_msg)
end if
diff_lat = abs(latMesh(n) - lat(n))
if (diff_lat > eps_omesh) then
frmt = "('ERROR: Difference between ESMF Mesh and MOM6 domain coords is"//&
"greater than parameter EPS_OMESH. n, latMesh(n), lat(n), diff_lat, "//&
"EPS_OMESH= ',i8,2(f21.13,3x),2(d21.5))"
write(err_msg, frmt)n,latMesh(n),lat(n), diff_lat, eps_omesh
call MOM_error(FATAL, err_msg)
end if
if (abs(maskMesh(n) - mask(n)) > 0) then
frmt = "('ERROR: ESMF mesh and MOM6 domain masks are inconsistent! - "//&
"MOM n, maskMesh(n), mask(n) = ',3(i8,2x))"
write(err_msg, frmt)n,maskMesh(n),mask(n)
call MOM_error(FATAL, err_msg)
end if
end do
deallocate(ownedElemCoords)
deallocate(lonMesh , lon )
deallocate(latMesh , lat )
deallocate(maskMesh, mask)
! realize the import and export fields using the mesh
call MOM_RealizeFields(importState, fldsToOcn_num, fldsToOcn, "Ocn import", mesh=Emesh, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call MOM_RealizeFields(exportState, fldsFrOcn_num, fldsFrOcn, "Ocn export", mesh=Emesh, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else if (geomtype == ESMF_GEOMTYPE_GRID) then
!---------------------------------
! create a MOM6 grid
!---------------------------------
! generate delayout and dist_grid
allocate(deBlockList(2,2,ntiles))
allocate(petMap(ntiles))
allocate(deLabelList(ntiles))
do n = 1, ntiles
deLabelList(n) = n
deBlockList(1,1,n) = xb(n)
deBlockList(1,2,n) = xe(n)
deBlockList(2,1,n) = yb(n)
deBlockList(2,2,n) = ye(n)
petMap(n) = pe(n)
! write(tmpstr,'(a,3i8)') subname//' iglo = ',n,deBlockList(1,1,n),deBlockList(1,2,n)
! call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
! write(tmpstr,'(a,3i8)') subname//' jglo = ',n,deBlockList(2,1,n),deBlockList(2,2,n)
! call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
! write(tmpstr,'(a,2i8)') subname//' pe = ',n,petMap(n)
! call ESMF_LogWrite(trim(tmpstr), ESMF_LOGMSG_INFO)
!--- assume a tile with starting index of 1 has an equivalent wraparound tile on the other side
enddo
delayout = ESMF_DELayoutCreate(petMap, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! rsd this assumes tripole grid, but sometimes in CESM a bipole
! grid is used -- need to introduce conditional logic here
allocate(connectionList(2))
! bipolar boundary condition at top row: nyg
call ESMF_DistGridConnectionSet(connectionList(1), tileIndexA=1, &
tileIndexB=1, positionVector=(/nxg+1, 2*nyg+1/), &
orientationVector=(/-1, -2/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! periodic boundary condition along first dimension
call ESMF_DistGridConnectionSet(connectionList(2), tileIndexA=1, &
tileIndexB=1, positionVector=(/nxg, 0/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
distgrid = ESMF_DistGridCreate(minIndex=(/1,1/), maxIndex=(/nxg,nyg/), &
! indexflag = ESMF_INDEX_DELOCAL, &
deBlockList=deBlockList, &
! deLabelList=deLabelList, &
delayout=delayout, &
connectionList=connectionList, &