-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathice_comp_nuopc.F90
1475 lines (1235 loc) · 61 KB
/
ice_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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module ice_comp_nuopc
!----------------------------------------------------------------------------
! This is the NUOPC cap for CICE
!----------------------------------------------------------------------------
use ESMF
use NUOPC , only : NUOPC_CompDerive, NUOPC_CompSetEntryPoint, NUOPC_CompSpecialize
use NUOPC , only : NUOPC_CompFilterPhaseMap, NUOPC_IsUpdated, NUOPC_IsAtTime
use NUOPC , only : NUOPC_CompAttributeGet, NUOPC_Advertise
use NUOPC , only : NUOPC_SetAttribute, NUOPC_CompAttributeGet, NUOPC_CompAttributeSet
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 : NUOPC_ModelGet, SetVM
use ice_constants , only : ice_init_constants
use ice_shr_methods , only : chkerr, state_setscalar, state_getscalar, state_diagnose, alarmInit
use ice_shr_methods , only : set_component_logging, get_component_instance
use ice_shr_methods , only : state_flddebug
use ice_import_export , only : ice_import, ice_export
use ice_import_export , only : ice_advertise_fields, ice_realize_fields
use ice_domain_size , only : nx_global, ny_global
use ice_domain , only : nblocks, blocks_ice, distrb_info
use ice_blocks , only : block, get_block, nx_block, ny_block, nblocks_x, nblocks_y
use ice_blocks , only : nblocks_tot, get_block_parameter
use ice_distribution , only : ice_distributiongetblockloc
use ice_grid , only : tlon, tlat, hm, tarea, ULON, ULAT
use ice_communicate , only : init_communicate, my_task, master_task, mpi_comm_ice
use ice_calendar , only : force_restart_now, write_ic
use ice_calendar , only : idate, mday, time, mmonth, time2sec, year_init
use ice_calendar , only : msec, dt, calendar, calendar_type, nextsw_cday, istep
use ice_kinds_mod , only : dbl_kind, int_kind, char_len, char_len_long
use ice_scam , only : scmlat, scmlon, single_column
use ice_fileunits , only : nu_diag, nu_diag_set, inst_index, inst_name
use ice_fileunits , only : inst_suffix, release_all_fileunits, flush_fileunit
use ice_restart_shared , only : runid, runtype, restart_dir, restart_file
use ice_history , only : accum_hist
use CICE_InitMod , only : cice_init
use CICE_RunMod , only : cice_run
use ice_exit , only : abort_ice
use icepack_intfc , only : icepack_warnings_flush, icepack_warnings_aborted
use icepack_intfc , only : icepack_init_orbit, icepack_init_parameters, icepack_query_orbit
use icepack_intfc , only : icepack_query_tracer_flags, icepack_query_parameters
use cice_wrapper_mod , only : t_startf, t_stopf, t_barrierf
use cice_wrapper_mod , only : shr_file_getlogunit, shr_file_setlogunit
#ifdef CESMCOUPLED
use shr_const_mod
use shr_orb_mod , only : shr_orb_decl, shr_orb_params, SHR_ORB_UNDEF_REAL, SHR_ORB_UNDEF_INT
#endif
use ice_timers
use ice_prescribed_mod , only : ice_prescribed_init
implicit none
private
public :: SetServices
public :: SetVM
private :: InitializeP0
private :: InitializeAdvertise
private :: InitializeRealize
private :: ModelAdvance
private :: ModelSetRunClock
private :: ModelFinalize
private :: ice_orbital_init ! only valid for cesm
character(len=char_len_long) :: flds_scalar_name = ''
integer :: flds_scalar_num = 0
integer :: flds_scalar_index_nx = 0
integer :: flds_scalar_index_ny = 0
integer :: flds_scalar_index_nextsw_cday = 0
character(len=char_len_long) :: orb_mode ! attribute - orbital mode
integer :: orb_iyear ! attribute - orbital year
integer :: orb_iyear_align ! attribute - associated with model year
real(dbl_kind) :: orb_obliq ! attribute - obliquity in degrees
real(dbl_kind) :: orb_mvelp ! attribute - moving vernal equinox longitude
real(dbl_kind) :: orb_eccen ! attribute and update- orbital eccentricity
character(len=*) , parameter :: orb_fixed_year = 'fixed_year'
character(len=*) , parameter :: orb_variable_year = 'variable_year'
character(len=*) , parameter :: orb_fixed_parameters = 'fixed_parameters'
character(len=*),parameter :: shr_cal_noleap = 'NO_LEAP'
character(len=*),parameter :: shr_cal_gregorian = 'GREGORIAN'
integer :: dbug = 0
integer , parameter :: debug_import = 0 ! internal debug level
integer , parameter :: debug_export = 0 ! internal debug level
character(*), parameter :: modName = "(ice_comp_nuopc)"
character(*), parameter :: u_FILE_u = &
__FILE__
!=======================================================================
contains
!===============================================================================
subroutine SetServices(gcomp, rc)
! Arguments
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! Local variables
character(len=*),parameter :: subname=trim(modName)//':(SetServices) '
!--------------------------------
rc = ESMF_SUCCESS
if (dbug > 5) call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! the NUOPC gcomp 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=(/"IPDv01p1"/), userRoutine=InitializeAdvertise, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv01p3"/), userRoutine=InitializeRealize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! attach specializing method(s)
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=ModelFinalize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (dbug > 5) call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
end subroutine SetServices
!===============================================================================
subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
! Arguments
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
!--------------------------------
rc = ESMF_SUCCESS
! Switch to IPDv01 by filtering all other phaseMap entries
call NUOPC_CompFilterPhaseMap(gcomp, ESMF_METHOD_INITIALIZE, &
acceptStringList=(/"IPDv01p"/), rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end subroutine InitializeP0
!===============================================================================
subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
! Arguments
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! Local variables
character(len=char_len_long) :: cvalue
character(len=char_len_long) :: logmsg
logical :: isPresent, isSet
character(len=*), parameter :: subname=trim(modName)//':(InitializeAdvertise) '
!--------------------------------
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
flds_scalar_name = trim(cvalue)
call ESMF_LogWrite(trim(subname)//' flds_scalar_name = '//trim(flds_scalar_name), ESMF_LOGMSG_INFO)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else
call abort_ice(subname//'Need to set attribute ScalarFieldName')
endif
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldCount", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue, *) flds_scalar_num
write(logmsg,*) flds_scalar_num
call ESMF_LogWrite(trim(subname)//' flds_scalar_num = '//trim(logmsg), ESMF_LOGMSG_INFO)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else
call abort_ice(subname//'Need to set attribute ScalarFieldCount')
endif
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxGridNX", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) flds_scalar_index_nx
write(logmsg,*) flds_scalar_index_nx
call ESMF_LogWrite(trim(subname)//' : flds_scalar_index_nx = '//trim(logmsg), ESMF_LOGMSG_INFO)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else
call abort_ice(subname//'Need to set attribute ScalarFieldIdxGridNX')
endif
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxGridNY", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) flds_scalar_index_ny
write(logmsg,*) flds_scalar_index_ny
call ESMF_LogWrite(trim(subname)//' : flds_scalar_index_ny = '//trim(logmsg), ESMF_LOGMSG_INFO)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
else
call abort_ice(subname//'Need to set attribute ScalarFieldIdxGridNY')
endif
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxNextSwCday", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) flds_scalar_index_nextsw_cday
write(logmsg,*) flds_scalar_index_nextsw_cday
call ESMF_LogWrite(trim(subname)//' : flds_scalar_index_nextsw_cday = '//trim(logmsg), ESMF_LOGMSG_INFO)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
endif
call NUOPC_CompAttributeGet(gcomp, name='dbug_flag', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) dbug
end if
write(logmsg,'(i6)') dbug
call ESMF_LogWrite('CICE_cap: dbug = '//trim(logmsg), ESMF_LOGMSG_INFO)
call ice_advertise_fields(gcomp, importState, exportState, flds_scalar_name, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end subroutine InitializeAdvertise
!===============================================================================
subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
! Arguments
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! Local variables
real(dbl_kind) :: eccen, obliqr, lambm0, mvelpp
type(ESMF_DistGrid) :: distGrid
type(ESMF_Mesh) :: Emesh, EmeshTemp
integer :: spatialDim
integer :: numOwnedElements
real(dbl_kind), pointer :: ownedElemCoords(:)
real(dbl_kind), pointer :: lat(:), latMesh(:)
real(dbl_kind), pointer :: lon(:), lonMesh(:)
integer , allocatable :: gindex_ice(:)
integer , allocatable :: gindex_elim(:)
integer , allocatable :: gindex(:)
integer :: globalID
character(ESMF_MAXSTR) :: cvalue
character(len=char_len) :: tfrz_option
character(ESMF_MAXSTR) :: convCIM, purpComp
type(ESMF_VM) :: vm
type(ESMF_Time) :: currTime ! Current time
type(ESMF_Time) :: startTime ! Start time
type(ESMF_Time) :: stopTime ! Stop time
type(ESMF_Time) :: refTime ! Ref time
type(ESMF_TimeInterval) :: timeStep ! Model timestep
type(ESMF_Calendar) :: esmf_calendar ! esmf calendar
type(ESMF_CalKind_Flag) :: esmf_caltype ! esmf calendar type
integer :: start_ymd ! Start date (YYYYMMDD)
integer :: start_tod ! start time of day (s)
integer :: curr_ymd ! Current date (YYYYMMDD)
integer :: curr_tod ! Current time of day (s)
integer :: stop_ymd ! stop date (YYYYMMDD)
integer :: stop_tod ! stop time of day (sec)
integer :: ref_ymd ! Reference date (YYYYMMDD)
integer :: ref_tod ! reference time of day (s)
integer :: yy,mm,dd ! Temporaries for time query
integer :: iyear ! yyyy
integer :: dtime ! time step
integer :: lmpicom
integer :: shrlogunit ! original log unit
character(len=char_len) :: starttype ! infodata start type
integer :: lsize ! local size of coupling array
logical :: isPresent
logical :: isSet
integer :: localPet
integer :: n,c,g,i,j,m ! indices
integer :: iblk, jblk ! indices
integer :: ig, jg ! indices
integer :: ilo, ihi, jlo, jhi ! beginning and end of physical domain
type(block) :: this_block ! block information for current block
integer :: compid ! component id
character(len=char_len_long) :: tempc1,tempc2
real(dbl_kind) :: diff_lon
integer :: npes
integer :: num_elim_global
integer :: num_elim_local
integer :: num_elim
integer :: num_ice
integer :: num_elim_gcells ! local number of eliminated gridcells
integer :: num_elim_blocks ! local number of eliminated blocks
integer :: num_total_blocks
integer :: my_elim_start, my_elim_end
real(dbl_kind) :: rad_to_deg
integer(int_kind) :: ktherm
logical :: mastertask
character(len=char_len_long) :: diag_filename = 'unset'
character(len=*), parameter :: F00 = "('(ice_comp_nuopc) ',2a,1x,d21.14)"
character(len=*), parameter :: subname=trim(modName)//':(InitializeRealize) '
!--------------------------------
rc = ESMF_SUCCESS
if (dbug > 5) call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
!----------------------------------------------------------------------------
! generate local mpi comm
!----------------------------------------------------------------------------
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, mpiCommunicator=lmpicom, localPet=localPet, PetCount=npes, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!----------------------------------------------------------------------------
! Initialize cice communicators
!----------------------------------------------------------------------------
call init_communicate(lmpicom) ! initial setup for message passing
mastertask = .false.
if (my_task == master_task) mastertask = .true.
!----------------------------------------------------------------------------
! determine instance information
!----------------------------------------------------------------------------
call get_component_instance(gcomp, inst_suffix, inst_index, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
inst_name = "ICE"//trim(inst_suffix)
!----------------------------------------------------------------------------
! start cice timers
!----------------------------------------------------------------------------
call t_startf ('cice_init_total')
!----------------------------------------------------------------------------
! Initialize constants
!----------------------------------------------------------------------------
#ifdef CESMCOUPLED
call ice_init_constants(omega_in=SHR_CONST_OMEGA, radius_in=SHR_CONST_REARTH, &
spval_dbl_in=SHR_CONST_SPVAL)
call icepack_init_parameters( &
secday_in = SHR_CONST_CDAY, &
rhoi_in = SHR_CONST_RHOICE, &
rhow_in = SHR_CONST_RHOSW, &
cp_air_in = SHR_CONST_CPDAIR, &
cp_ice_in = SHR_CONST_CPICE, &
cp_ocn_in = SHR_CONST_CPSW, &
gravit_in = SHR_CONST_G, &
rhofresh_in = SHR_CONST_RHOFW, &
zvir_in = SHR_CONST_ZVIR, &
vonkar_in = SHR_CONST_KARMAN, &
cp_wv_in = SHR_CONST_CPWV, &
stefan_boltzmann_in = SHR_CONST_STEBOL, &
Tffresh_in = SHR_CONST_TKFRZ, &
Lsub_in = SHR_CONST_LATSUB, &
Lvap_in = SHR_CONST_LATVAP, &
!Lfresh_in = SHR_CONST_LATICE, & ! computed in init_parameters as Lsub-Lvap
Timelt_in = SHR_CONST_TKFRZ-SHR_CONST_TKFRZ, &
Tsmelt_in = SHR_CONST_TKFRZ-SHR_CONST_TKFRZ, &
ice_ref_salinity_in = SHR_CONST_ICE_REF_SAL, &
depressT_in = 0.054_dbl_kind, &
Tocnfrz_in = -34.0_dbl_kind*0.054_dbl_kind, &
pi_in = SHR_CONST_PI, &
snowpatch_in = 0.005_dbl_kind, &
dragio_in = 0.00962_dbl_kind)
call icepack_warnings_flush(nu_diag)
if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
file=__FILE__, line=__LINE__)
#endif
!----------------------------------------------------------------------------
! Determine attributes - also needed in realize phase to get grid information
!----------------------------------------------------------------------------
! Get orbital values
! Note that these values are obtained in a call to init_orbit in ice_shortwave.F90
! if CESMCOUPLED is not defined
call ice_orbital_init(gcomp, clock, nu_diag, mastertask, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Determine runtype and possibly nextsw_cday
call NUOPC_CompAttributeGet(gcomp, name='start_type', value=cvalue, isPresent=isPresent, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent) then
read(cvalue,*) starttype
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
call abort_ice( subname//' ERROR: unknown starttype' )
end if
! We assume here that on startup - nextsw_cday is just the current time
! TOOD (mvertens, 2019-03-21): need to get the perpetual run working
if (trim(runtype) /= 'initial') then
! Set nextsw_cday to -1 (this will skip an orbital calculation on initialization
nextsw_cday = -1.0_dbl_kind
else
call ESMF_ClockGet( clock, currTime=currTime, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_TimeGet( currTime, dayOfYear_r8=nextsw_cday, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end if
else
runtype = 'initial' ! determined from the namelist in ice_init if CESMCOUPLED is not defined
end if
! Determine if single column
call NUOPC_CompAttributeGet(gcomp, name='single_column', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) single_column
if (single_column) then
call NUOPC_CompAttributeGet(gcomp, name='scmlon', value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) scmlon
call NUOPC_CompAttributeGet(gcomp, name='scmlat', value=cvalue, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) scmlat
end if
else
single_column = .false.
end if
! Determine runid
call NUOPC_CompAttributeGet(gcomp, name='case_name', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (isPresent .and. isSet) then
read(cvalue,*) runid
else
! read in from the namelist in ice_init.F90 if this is not an attribute passed from the driver
runid = 'unknown'
end if
! Get clock information before call to cice_init
call ESMF_ClockGet( clock, &
currTime=currTime, startTime=startTime, stopTime=stopTime, refTime=RefTime, &
timeStep=timeStep, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_TimeGet( currTime, yy=yy, mm=mm, dd=dd, s=curr_tod, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ice_cal_ymd2date(yy,mm,dd,curr_ymd)
call ESMF_TimeGet( startTime, yy=yy, mm=mm, dd=dd, s=start_tod, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ice_cal_ymd2date(yy,mm,dd,start_ymd)
call ESMF_TimeGet( stopTime, yy=yy, mm=mm, dd=dd, s=stop_tod, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ice_cal_ymd2date(yy,mm,dd,stop_ymd)
call ESMF_TimeGet( refTime, yy=yy, mm=mm, dd=dd, s=ref_tod, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ice_cal_ymd2date(yy,mm,dd,ref_ymd)
call ESMF_TimeIntervalGet( timeStep, s=dtime, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
dt = real(dtime)
call ESMF_TimeGet( currTime, calkindflag=esmf_caltype, rc=rc )
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (esmf_caltype == ESMF_CALKIND_NOLEAP) then
calendar_type = shr_cal_noleap
else if (esmf_caltype == ESMF_CALKIND_GREGORIAN) then
calendar_type = shr_cal_gregorian
else
call abort_ice( subname//'ERROR:: bad calendar for ESMF' )
end if
!----------------------------------------------------------------------------
! Set cice logging
!----------------------------------------------------------------------------
! Note - this must be done AFTER the communicators are set
! Note that sets the nu_diag module variable in ice_fileunits
! Set the nu_diag_set flag so it's not reset later
call shr_file_setLogUnit (shrlogunit)
call NUOPC_CompAttributeGet(gcomp, name="diro", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
diag_filename = trim(cvalue)
end if
call NUOPC_CompAttributeGet(gcomp, name="logfile", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
diag_filename = trim(diag_filename) // '/' // trim(cvalue)
end if
if (trim(diag_filename) /= 'unset') then
call set_component_logging(gcomp, mastertask, nu_diag, shrlogunit, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
nu_diag_set = .true.
end if
!----------------------------------------------------------------------------
! Initialize cice
!----------------------------------------------------------------------------
! Note that cice_init also sets time manager info as well as mpi communicator info,
! including master_task and my_task
call t_startf ('cice_init')
call cice_init
call t_stopf ('cice_init')
!----------------------------------------------------------------------------
! reset shr logging to my log file
!----------------------------------------------------------------------------
call icepack_query_parameters(ktherm_out=ktherm)
call icepack_query_parameters(tfrz_option_out=tfrz_option)
call icepack_warnings_flush(nu_diag)
if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
file=__FILE__, line=__LINE__)
! Now write output to nu_diag - this must happen AFTER call to cice_init
if (mastertask) then
write(nu_diag,F00) trim(subname),' cice init nextsw_cday = ',nextsw_cday
write(nu_diag,*) trim(subname),' tfrz_option = ',trim(tfrz_option)
if (ktherm == 2 .and. trim(tfrz_option) /= 'mushy') then
write(nu_diag,*) trim(subname),' Warning: Using ktherm = 2 and tfrz_option = ', trim(tfrz_option)
endif
write(nu_diag,*) trim(subname),' inst_name = ',trim(inst_name)
write(nu_diag,*) trim(subname),' inst_index = ',inst_index
write(nu_diag,*) trim(subname),' inst_suffix = ',trim(inst_suffix)
endif
!---------------------------------------------------------------------------
! use EClock to reset calendar information on initial start
!---------------------------------------------------------------------------
! - on initial run
! - iyear, month and mday obtained from sync clock
! - time determined from iyear, month and mday
! - istep0 and istep1 are set to 0
! - on restart run
! - istep0, time and time_forc are read from restart file
! - istep1 is set to istep0
! - idate is determined from time via the call to calendar (see below)
if (runtype == 'initial') then
if (ref_ymd /= start_ymd .or. ref_tod /= start_tod) then
if (my_task == master_task) then
write(nu_diag,*) trim(subname),': ref_ymd ',ref_ymd, ' must equal start_ymd ',start_ymd
write(nu_diag,*) trim(subname),': ref_ymd ',ref_tod, ' must equal start_ymd ',start_tod
end if
end if
if (my_task == master_task) then
write(nu_diag,*) trim(subname),' idate from sync clock = ', start_ymd
write(nu_diag,*) trim(subname),' tod from sync clock = ', start_tod
write(nu_diag,*) trim(subname),' resetting idate to match sync clock'
end if
idate = curr_ymd
if (idate < 0) then
if (my_task == master_task) then
write(nu_diag,*) trim(subname),' ERROR curr_ymd,year_init =',curr_ymd,year_init
write(nu_diag,*) trim(subname),' ERROR idate lt zero',idate
end if
call abort_ice(subname//' :: ERROR idate lt zero')
endif
iyear = (idate/10000) ! integer year of basedate
mmonth= (idate-iyear*10000)/100 ! integer month of basedate
mday = idate-iyear*10000-mmonth*100 ! day of month of basedate
if (my_task == master_task) then
write(nu_diag,*) trim(subname),' curr_ymd = ',curr_ymd
write(nu_diag,*) trim(subname),' cice year_init = ',year_init
write(nu_diag,*) trim(subname),' cice start date = ',idate
write(nu_diag,*) trim(subname),' cice start ymds = ',iyear,mmonth,mday,start_tod
write(nu_diag,*) trim(subname),' cice calendar_type = ',trim(calendar_type)
endif
#ifdef CESMCOUPLED
if (calendar_type == "GREGORIAN" .or. &
calendar_type == "Gregorian" .or. &
calendar_type == "gregorian") then
call time2sec(iyear-(year_init-1),mmonth,mday,time)
else
call time2sec(iyear-year_init,mmonth,mday,time)
endif
#endif
time = time+start_tod
end if
call calendar(time) ! update calendar info
if (write_ic) then
call accum_hist(dt) ! write initial conditions
end if
!---------------------------------------------------------------------------
! Determine the global index space needed for the distgrid
!---------------------------------------------------------------------------
! number the local grid to get allocation size for gindex_ice
lsize = 0
do iblk = 1, nblocks
this_block = get_block(blocks_ice(iblk),iblk)
ilo = this_block%ilo
ihi = this_block%ihi
jlo = this_block%jlo
jhi = this_block%jhi
do j = jlo, jhi
do i = ilo, ihi
lsize = lsize + 1
enddo
enddo
enddo
! set global index array
allocate(gindex_ice(lsize))
n = 0
do iblk = 1, nblocks
this_block = get_block(blocks_ice(iblk),iblk)
ilo = this_block%ilo
ihi = this_block%ihi
jlo = this_block%jlo
jhi = this_block%jhi
do j = jlo, jhi
do i = ilo, ihi
n = n+1
ig = this_block%i_glob(i)
jg = this_block%j_glob(j)
gindex_ice(n) = (jg-1)*nx_global + ig
enddo
enddo
enddo
! Determine total number of eliminated blocks globally
globalID = 0
num_elim_global = 0 ! number of eliminated blocks
num_total_blocks = 0
do jblk=1,nblocks_y
do iblk=1,nblocks_x
globalID = globalID + 1
num_total_blocks = num_total_blocks + 1
if (distrb_info%blockLocation(globalID) == 0) then
num_elim_global = num_elim_global + 1
end if
end do
end do
if (num_elim_global > 0) then
! Distribute the eliminated blocks in a round robin fashion amoung processors
num_elim_local = num_elim_global / npes
my_elim_start = num_elim_local*localPet + min(localPet, mod(num_elim_global, npes)) + 1
if (localPet < mod(num_elim_global, npes)) then
num_elim_local = num_elim_local + 1
end if
my_elim_end = my_elim_start + num_elim_local - 1
! Determine the number of eliminated gridcells locally
globalID = 0
num_elim_blocks = 0 ! local number of eliminated blocks
num_elim_gcells = 0
do jblk=1,nblocks_y
do iblk=1,nblocks_x
globalID = globalID + 1
if (distrb_info%blockLocation(globalID) == 0) then
num_elim_blocks = num_elim_blocks + 1
if (num_elim_blocks >= my_elim_start .and. num_elim_blocks <= my_elim_end) then
this_block = get_block(globalID, globalID)
num_elim_gcells = num_elim_gcells + &
(this_block%jhi-this_block%jlo+1) * (this_block%ihi-this_block%ilo+1)
end if
end if
end do
end do
! Determine the global index space of the eliminated gridcells
allocate(gindex_elim(num_elim_gcells))
globalID = 0
num_elim_gcells = 0 ! local number of eliminated gridcells
num_elim_blocks = 0 ! local number of eliminated blocks
do jblk=1,nblocks_y
do iblk=1,nblocks_x
globalID = globalID + 1
if (distrb_info%blockLocation(globalID) == 0) then
this_block = get_block(globalID, globalID)
num_elim_blocks = num_elim_blocks + 1
if (num_elim_blocks >= my_elim_start .and. num_elim_blocks <= my_elim_end) then
do j=this_block%jlo,this_block%jhi
do i=this_block%ilo,this_block%ihi
num_elim_gcells = num_elim_gcells + 1
ig = this_block%i_glob(i)
jg = this_block%j_glob(j)
gindex_elim(num_elim_gcells) = (jg-1)*nx_global + ig
end do
end do
end if
end if
end do
end do
! create a global index that includes both active and eliminated gridcells
num_ice = size(gindex_ice)
num_elim = size(gindex_elim)
allocate(gindex(num_elim + num_ice))
do n = 1,num_ice
gindex(n) = gindex_ice(n)
end do
do n = num_ice+1,num_ice+num_elim
gindex(n) = gindex_elim(n-num_ice)
end do
deallocate(gindex_elim)
else
! No eliminated land blocks
num_ice = size(gindex_ice)
allocate(gindex(num_ice))
do n = 1,num_ice
gindex(n) = gindex_ice(n)
end do
end if
!---------------------------------------------------------------------------
! Create distGrid from global index array
!---------------------------------------------------------------------------
DistGrid = ESMF_DistGridCreate(arbSeqIndexList=gindex, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!---------------------------------------------------------------------------
! Create the CICE mesh
!---------------------------------------------------------------------------
! read in the mesh
call NUOPC_CompAttributeGet(gcomp, name='mesh_ice', 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 (my_task == master_task) then
write(nu_diag,*)'mesh file for cice domain is ',trim(cvalue)
end if
! recreate the mesh using the above distGrid
EMesh = ESMF_MeshCreate(EMeshTemp, elementDistgrid=Distgrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! obtain mesh lats and lons
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), latMesh(numOwnedElements))
call ESMF_MeshGet(Emesh, ownedElemCoords=ownedElemCoords)
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
call icepack_query_parameters(rad_to_deg_out=rad_to_deg)
call icepack_warnings_flush(nu_diag)
if (icepack_warnings_aborted()) call abort_ice(error_message=subname, &
file=__FILE__, line=__LINE__)
! obtain internally generated cice lats and lons for error checks
allocate(lon(lsize))
allocate(lat(lsize))
n = 0
do iblk = 1, nblocks
this_block = get_block(blocks_ice(iblk),iblk)
ilo = this_block%ilo
ihi = this_block%ihi
jlo = this_block%jlo
jhi = this_block%jhi
do j = jlo, jhi
do i = ilo, ihi
n = n+1
lon(n) = tlon(i,j,iblk)*rad_to_deg
lat(n) = tlat(i,j,iblk)*rad_to_deg
enddo
enddo
enddo
! error check differences between internally generated lons and those read in
do n = 1,lsize
diff_lon = abs(lonMesh(n) - lon(n))
if ( (diff_lon > 1.e2 .and. abs(diff_lon - 360_dbl_kind) > 1.e-1) .or.&
(diff_lon > 1.e-3 .and. diff_lon < 1._dbl_kind) ) then
!write(6,100)n,lonMesh(n),lon(n), diff_lon
100 format('ERROR: CICE n, lonmesh(n), lon(n), diff_lon = ',i6,2(f21.13,3x),d21.5)
!call abort_ice()
end if
if (abs(latMesh(n) - lat(n)) > 1.e-1) then
!write(6,101)n,latMesh(n),lat(n), abs(latMesh(n)-lat(n))
101 format('ERROR: CICE n, latmesh(n), lat(n), diff_lat = ',i6,2(f21.13,3x),d21.5)
!call abort_ice()
end if
end do
! deallocate memory
deallocate(ownedElemCoords)
deallocate(lon, lonMesh)
deallocate(lat, latMesh)
!-----------------------------------------------------------------
! Realize the actively coupled fields
!-----------------------------------------------------------------
call ice_realize_fields(gcomp, mesh=Emesh, &
flds_scalar_name=flds_scalar_name, flds_scalar_num=flds_scalar_num, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!-----------------------------------------------------------------
! Prescribed ice initialization - first get compid
!-----------------------------------------------------------------
call NUOPC_CompAttributeGet(gcomp, name='MCTID', value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
read(cvalue,*) compid ! convert from string to integer
else
compid = 0
end if
call ice_prescribed_init(lmpicom, compid, gindex_ice)
!-----------------------------------------------------------------
! Create cice export state
!-----------------------------------------------------------------
call ice_export (exportstate, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call State_SetScalar(dble(nx_global), flds_scalar_index_nx, exportState, &
flds_scalar_name, flds_scalar_num, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call State_SetScalar(dble(ny_global), flds_scalar_index_ny, exportState, &
flds_scalar_name, flds_scalar_num, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! TODO (mvertens, 2018-12-21): fill in iceberg_prognostic as .false.
if (debug_export > 0 .and. my_task==master_task) then
call State_fldDebug(exportState, flds_scalar_name, 'cice_export:', &
idate, msec, nu_diag, rc=rc)
end if
!--------------------------------
! diagnostics
!--------------------------------
if (dbug > 0) then
call state_diagnose(exportState,subname//':ES',rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
endif
if (dbug > 5) call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
call t_stopf ('cice_init_total')
deallocate(gindex_ice)
deallocate(gindex)
call flush_fileunit(nu_diag)
end subroutine InitializeRealize
!===============================================================================
subroutine ModelAdvance(gcomp, rc)
!---------------------------------------------------------------------------
! Run CICE
!---------------------------------------------------------------------------
! Arguments
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! Local variables
type(ESMF_Clock) :: clock
type(ESMF_Alarm) :: alarm
type(ESMF_Time) :: startTime
type(ESMF_Time) :: currTime
type(ESMF_Time) :: nextTime
type(ESMF_TimeInterval) :: timeStep
type(ESMF_State) :: importState, exportState
character(ESMF_MAXSTR) :: cvalue
real(dbl_kind) :: eccen, obliqr, lambm0, mvelpp
integer :: shrlogunit ! original log unit
integer :: k,n ! index
logical :: stop_now ! .true. ==> stop at the end of this run phase
integer :: ymd ! Current date (YYYYMMDD)
integer :: tod ! Current time of day (sec)
integer :: curr_ymd ! Current date (YYYYMMDD)
integer :: curr_tod ! Current time of day (s)
integer :: yy,mm,dd ! year, month, day, time of day
integer :: ymd_sync ! Sync date (YYYYMMDD)
integer :: yr_sync ! Sync current year
integer :: mon_sync ! Sync current month
integer :: day_sync ! Sync current day
integer :: tod_sync ! Sync current time of day (sec)
character(char_len_long) :: restart_date
character(char_len_long) :: restart_filename
logical :: isPresent, isSet
character(*) , parameter :: F00 = "('(ice_comp_nuopc) ',2a,i8,d21.14)"
character(len=*),parameter :: subname=trim(modName)//':(ModelAdvance) '
character(char_len_long) :: msgString
!--------------------------------
rc = ESMF_SUCCESS
if (dbug > 5) call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
! query the Component for its clock, importState and exportState
call ESMF_GridCompGet(gcomp, clock=clock, importState=importState, &
exportState=exportState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_ClockPrint(clock, options="currTime", &
preString="------>Advancing ICE from: ", unit=msgString, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(subname//trim(msgString), ESMF_LOGMSG_INFO)
call ESMF_ClockGet(clock, startTime=startTime, currTime=currTime, &
timeStep=timeStep, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_TimePrint(currTime + timeStep, &
preString="--------------------------------> to: ", unit=msgString, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO)
!--------------------------------
! Turn on timers
!--------------------------------
call ice_timer_start(timer_total) ! time entire run
call t_barrierf('cice_run_total_BARRIER',mpi_comm_ice)
call t_startf ('cice_run_total')
!--------------------------------
! Reset shr logging to my log file
!--------------------------------
call shr_file_getLogUnit (shrlogunit)
call shr_file_setLogUnit (nu_diag)
!--------------------------------
! Query the Component for its clock, importState and exportState
!--------------------------------
call NUOPC_ModelGet(gcomp, modelClock=clock, importState=importState, exportState=exportState, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
!--------------------------------
! Determine time of next atmospheric shortwave calculation
!--------------------------------
call NUOPC_CompAttributeGet(gcomp, name="ScalarFieldIdxNextSwCday", value=cvalue, isPresent=isPresent, isSet=isSet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (isPresent .and. isSet) then
call State_GetScalar(importState, flds_scalar_index_nextsw_cday, nextsw_cday, &
flds_scalar_name, flds_scalar_num, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return