This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWRFHydro_NUOPC_Cap.F90
1476 lines (1250 loc) · 53.8 KB
/
WRFHydro_NUOPC_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
!>
!! @mainpage NCAR's WRF-Hydro NUOPC Cap
!! @author Daniel Rosen (daniel.rosen@noaa.gov)
!! @author ESMF Support (esmf_support@list.woc.noaa.gov)
!! @date 03/14/2017 WRF-Hydro NUOPC Cap Added to GitHub
!! @date 03/17/2017 Documentation Added
!!
!! @tableofcontents
!!
!! @section Overview Overview
!!
!! The Weather Research and Forecasting Hydrological (WRF-Hydro) model is a
!! hydrometerological forecasting model developed and maintained by the
!! National Center for Atmospheric Research (NCAR). The WRF-Hydro cap wraps
!! the WRF-Hydro model with NUOPC compliant interfaces. The result is a
!! WRF-Hydro model capable of coupling with other models using National
!! Unified Operational Prediction Capability (NUOPC).
!!
!! This page documents the technical design of the specialized NUOPC model and
!! the WRF-Hydro gluecode. For generic NUOPC model documentation please see
!! the NUOPC reference manual: https://www.earthsystemcog.org/projects/nuopc/refmans.
!!
!!
!! @section NuopcSpecialization NUOPC Model Specialized Entry Points
!!
!! This cap specializes the cap configuration, initialization, advertised
!! fields, realized fields, data initialization, clock, run, and finalize.
!!
!! @subsection SetServices Set Services (Register Subroutines)
!!
!! Table summarizing the NUOPC specialized subroutines registered during
!! [SetServices] (@ref WRFHYDRO_NUOPC::SetServices). The "Phase" column says
!! whether the subroutine is called during the initialization, run, or
!! finalize part of the coupled system run.
!!
!! Phase | Cap Subroutine | Description
!! -------|---------------------------------------------------|-------------------------------------------------------------
!! Init | [InitializeP0] (@ref WRFHYDRO_NUOPC::InitializeP0) | Set the Initialize Phase Definition (IPD). Configure model
!! Init | [InitializeP1] (@ref WRFHYDRO_NUOPC::InitializeP1) | Initialize model. Advertize import and export fields
!! Init | [InitializeP3] (@ref WRFHYDRO_NUOPC::InitializeP3) | Realize import and export fields
!! Init | [DataInitialize] (@ref WRFHYDRO_NUOPC::DataInitialize) | Initialize import and export data
!! Init | [SetClock] (@ref WRFHYDRO_NUOPC::SetClock) | Set model clock during initialization
!! Run | [CheckImport] (@ref WRFHYDRO_NUOPC::CheckImport) | Check timestamp on import data.
!! Run | [ModelAdvance] (@ref WRFHYDRO_NUOPC::ModelAdvance) | Advances the model by a timestep
!! Final | [ModelFinalize] (@ref WRFHYDRO_NUOPC::ModelFinalize) | Releases memory
!!
!!
!! @section Initialize Initialize
!!
!! Description of the initialization phases and internal model calls.
!! - [InitializeP0] (@ref WRFHYDRO_NUOPC::InitializeP0)
!! - [InitializeP1] (@ref WRFHYDRO_NUOPC::InitializeP1)
!! - [InitializeP3] (@ref WRFHYDRO_NUOPC::InitializeP3)
!! - [DataInitialize] (@ref WRFHYDRO_NUOPC::DataInitialize)
!! - [SetClock] (@ref WRFHYDRO_NUOPC::SetClock)
!!
!! @subsection InitializeP0 InitializeP0
!!
!! During initialize phase 0 the runtime configuration is read in from model
!! attributes and the initialization phase definition version is set to
!! IPDv03.
!!
!! @subsection InitializeP1 InitializeP1
!!
!! During initialize phase 1 the model is initialized and the import and
!! export fields are advertised in a state labeled with the domain ID.
!!
!! @subsection InitializeP3 InitializeP3
!!
!! During initialize phase 3 import and export fields are realized if they are
!! connected through NUOPC. Realized fields are created on the WRF-Hydro grid.
!!
!! @subsection DataInitialize DataInitialize
!!
!! During data initialize this cap checks the timestamp of all import fields
!! dependent on a coupled model. Once all dependent import fields have been
!! initialized this cap is marked initalized.
!!
!! @subsection SetClock SetClock
!!
!! During set clock the cap creates a new clock using the timestep configured
!! in te WRF-Hydro configuration file. The restart write time step is also
!! created and the restart write time accumulation tracker is reset to zero.
!!
!!
!! @section Run Run
!!
!! Description of the run phase(s) and internal model calls.
!! - [CheckImport] (@ref WRFHYDRO_NUOPC::CheckImport)
!! - [ModelAdvance] (@ref WRFHYDRO_NUOPC::ModelAdvance)
!!
!! @subsection CheckImport CheckImport
!!
!! During check import the import data is checked to verify that it is at
!! the beginning or end of the timestep.
!!
!! @subsection ModelAdvance ModelAdvance
!!
!! Calls WRF-Hydro advance for the configured domain.
!!
!!
!! @section Finalize Finalize
!!
!! Description of the finalize phase and internal model calls.
!! - [ModelFinalize] (@ref WRFHYDRO_NUOPC::ModelFinalize)
!!
!! @subsection ModelFinalize ModelFinalize
!!
!! During model finalize WRF-Hydro finalize subroutines are called and memory
!! allocated during cap initialization is released.
!!
!!
!! @section ModelConfiguration Model Configuration
!!
!! Custom model attributes are used to configure the model.
!!
!! Attribute | Default | Description
!! ------------------|-----------------|-------------------------------------------------------------------------------------
!! Verbosity | VERBOSITY_LV1 | Verbosity levels are defined in WRFHYDRO_NUOPC_Macros.h
!! DomainID | 1 |
!! RestartInterval | NEVER | Determine when to write NUOPC state restart files in seconds
!! ConfigFile | hydro.namelist | Set the WRF-Hydro configuraion file
!! dasConfigFile | namelist.hrldas | Set the WRF-Hydro DAS configuration file
!! WriteGrid | FALSE | Write a NetCDF file for the WRF-Hydro domain
!! WriteImport | FALSE | Write a NetCDF file for the import state before model advance
!! WriteExport | FALSE | Write a NetCDF file for the export state after model advance
!! LogMemory | FALSE | Write memory statistics. (Not Implemented)
!! TestFillImport | FALSE | Fill the import state with ESMF_FieldFill(sincos) for testing
!! TestFillExport | FALSE | Fill the export state with ESMF_FieldFill(sincos) for testing
!!
!!
!! @section ModelFields Model Fields
!!
!! The following tables list the import and export fields.
!!
!! @subsection ImportFields Import Fields
!!
!! Import fields are listed in the import_list parameter.
!!
!! Standard Name | Units | Model Variable | Description | Notes
!! ---------------|--------|-----------------|--------------------------------------------|--------------------------------------
!! dummy_field_1 | Pa | forcing_1 | field description for first import field | |
!! dummy_field_2 | kg | forcing_2 | field description for second import field | |
!! dummy_field_3 | W m-2 | forcing_3 | field description for third import field | field notes
!!
!! @subsection ExportField Export Fields
!!
!! Export fields are listed in the export_list parameter.
!!
!! Standard Name | Units | Model Variable | Description | Notes
!! ---------------|---------|-----------------|-------------------------------------------|---------------------------
!! dummy_field_1 | m | output_1 | field description for first export field | field notes
!! dummy_field_2 | kg | output_2 | field description for second export field | |
!! dummy_field_3 | m s-1 | output_3 | field description for third export field | field notes
!!
!!
!! @section MemoryManagement Memory Management
!!
!! Model configuration is stored in a custom internal state data type. A
!! pointer to the custom internal state data type is stored in the component.
!!
!! The cap allocates new memory for each field. This will be updated so that
!! NUOPC fields directly access the WRF-Hydro field memory.
!!
!! @section IO Input and Output
!!
!! Cap diagnostic output is written to the ESMF PET Logs. Cap diagnostic
!! output can be increased or decreased by setting the Verbosity attribute.
!!
!! NUOPC state restart write files are written depending on the
!! RestartInterval attribute. If set to 0 then NUOPC state restart write files
!! will never be written.
!!
!! WRF-Hydro diagnostics output is written to standard out. To increase the
!! diagnostic output compile WRF-Hydro with -DHYDRO_D.
!!
!! WRF-Hydro writes several output files. Please see the
!! [WRF-Hydro documentation] (https://www.ral.ucar.edu/projects/wrf_hydro).
!!
!! @section Dependencies Dependencies
!!
!! Dependencies
!! - [ESMF v7.0.0+] (https://www.earthsystemcog.org/projects/esmf/)
!! - [NetCDF v4.3.0+] (http://www.unidata.ucar.edu/software/netcdf/docs/)
!! - [NetCDF FORTRAN] (http://www.unidata.ucar.edu/software/netcdf/docs/building_netcdf_fortran.html)
!!
!! @subsection ESMF ESMF
!!
!! See the [ESMF User's Guide]
!! (http://www.earthsystemmodeling.org/esmf_releases/public/last/ESMF_usrdoc).
!!
!! @section BuildingAndInstalling Building and Installing
!!
!! Environment Variables
!! - ESMFMKFILE
!!
!! NUOPC Makefile Targets
!! - nuopc
!! - nuopcinstall
!! - nuopcclean
!!
!! The build system in [Makefile] (@ref Makefile) wraps the WRF-Hydro build
!! system and adds the nuopc, nuopcinstall, and nuopcclean targets. Before
!! building make sure to configure the internal model.
!!
!! To build and install into the current directory run:
!! $ make nuopc
!!
!! To install into an alternative directory run:
!! $ make nuopcinstall DESTDIR=<INSTALL_DIR> INSTDIR=<SUBDIR>
!!
!! To build with debugging information run:
!! $ make nuopc DEBUG=on
!!
!! @section Repository
!! The WRF-Hydro NUOPC cap is maintained in a GitHub repository:
!! https://github.com/NESII/wrfhydro_cap
!!
!! @section References
!!
!! - [WRF-Hydro] (https://www.ral.ucar.edu/projects/wrf_hydro)
!! - [ESPS] (https://www.earthsystemcog.org/projects/esps)
!! - [ESMF] (https://www.earthsystemcog.org/projects/esmf)
!! - [NUOPC] (https://www.earthsystemcog.org/projects/nuopc/)
#define FILENAME "WRFHydro_NUOPC_Cap"
#define MODNAME "WRFHydro_NUOPC"
#include "WRFHydro_NUOPC_Macros.h"
module WRFHydro_NUOPC
use ESMF
use NUOPC
use NUOPC_Model, &
model_routine_SS => SetServices, &
model_label_DataInitialize => label_DataInitialize, &
model_label_SetClock => label_SetClock, &
model_label_CheckImport => label_CheckImport, &
model_label_Advance => label_Advance, &
model_label_Finalize => label_Finalize
use WRFHYDRO_NUOPC_Gluecode
use WRFHydro_ESMF_Extensions
implicit none
private
public SetServices
CHARACTER(LEN=*), PARAMETER :: label_InternalState = 'InternalState'
type type_InternalStateStruct
integer :: did = 1
character :: hgrid = '0'
integer :: verbosity = VERBOSITY_LV1
character(len=64) :: configFile = 'hydro.namelist'
character(len=64) :: dasConfigFile = 'namelist.hrldas'
character(len=256) :: forcingDir = 'WRFHYDRO_FORCING'
logical :: nestToNest = .FALSE.
logical :: lwrite_grid = .TRUE.
logical :: llog_memory = .FALSE.
logical :: ltestfill_imp = .FALSE.
logical :: ltestfill_exp = .FALSE.
integer :: nfields = size(WRFHYDRO_FieldList)
integer :: timeSlice = 0
integer :: timeStepInt = 0
logical :: lwrite_debug = .FALSE.
integer :: debugIntvlInt =0
type(ESMF_TimeInterval) :: debugIntvl
type(ESMF_TimeInterval) :: debugImpAccum
type(ESMF_TimeInterval) :: debugExpAccum
integer :: debugImpSlice = 1
integer :: debugExpSlice = 1
type (ESMF_Clock) :: clock(1)
type (ESMF_TimeInterval) :: stepTimer(1)
type(ESMF_State) :: NStateImp(1)
type(ESMF_State) :: NStateExp(1)
integer :: mode(1) = WRFHYDRO_Unknown
endtype
type type_InternalState
type(type_InternalStateStruct), pointer :: wrap
end type
! some temporary debug variables
character(len=ESMF_MAXSTR) :: logMsg
!-----------------------------------------------------------------------------
contains
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "SetServices"
subroutine SetServices(gcomp, rc)
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
integer :: stat
type(type_InternalState) :: is
#ifdef DEBUG
call ESMF_LogSet(flush=.true., rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! allocate memory for this internal state and set it in the component
allocate(is%wrap, stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg='WRFHYDRO: Allocation of internal state memory failed.', &
file=FILENAME, rcToReturn=rc)) return ! bail out
call ESMF_UserCompSetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! the NUOPC model component will register the generic methods
call NUOPC_CompDerive(gcomp, model_routine_SS, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! switching to IPD versions
call ESMF_GridCompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
userRoutine=InitializeP0, phase=0, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! set entry point for methods that require specific implementation
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p1"/), userRoutine=InitializeP1, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_CompSetEntryPoint(gcomp, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p3"/), userRoutine=InitializeP3, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! attach specializing method(s)
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_DataInitialize, &
specRoutine=DataInitialize, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_CompSpecialize(gcomp, speclabel=model_label_SetClock, &
specRoutine=SetClock, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_MethodRemove(gcomp, label=model_label_CheckImport, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail ou
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_CheckImport, &
specRoutine=CheckImport, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail ou
call NUOPC_CompSpecialize(gcomp, speclabel=model_label_Advance, &
specRoutine=ModelAdvance, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_CompSpecialize(gcomp, specLabel=model_label_Finalize, &
specRoutine=ModelFinalize, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "InitializeP0"
subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! local variables
character(32) :: cname
integer :: stat
logical :: configIsPresent
type(ESMF_Config) :: config
type(NUOPC_FreeFormat) :: attrFF
type(type_InternalState) :: is
character(len=64) :: value
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! check gcomp for config
call ESMF_GridCompGet(gcomp, configIsPresent=configIsPresent, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (configIsPresent) then
! read and ingest free format component attributes
call ESMF_GridCompGet(gcomp, config=config, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
attrFF = NUOPC_FreeFormatCreate(config, &
label=trim(cname)//"_attributes::", relaxedflag=.true., rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_CompAttributeIngest(gcomp, attrFF, addFlag=.true., rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_FreeFormatDestroy(attrFF, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
! Determine Domain ID
call ESMF_AttributeGet(gcomp, name="did", value=value, &
defaultValue="1", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%did = ESMF_UtilString2Int(value, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! Time Step
call ESMF_AttributeGet(gcomp, name="time_step", value=value, defaultValue="0", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
read (value,*,iostat=stat) is%wrap%timeStepInt
if (stat /= 0) then
call ESMF_LogSetError(ESMF_FAILURE, &
msg=METHOD//": Cannot convert "//trim(value)//" to integer.", &
line=__LINE__,file=__FILE__,rcToReturn=rc)
return ! bail out
endif
! Debug Write Interval
call ESMF_AttributeGet(gcomp, name="debug_interval", value=value, defaultValue="default", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%debugIntvlInt = ESMF_UtilString2Int(value, &
specialStringList=(/"default","yearly","hourly","daily"/), &
specialValueList=(/0,31536000,3600,86400/), rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (is%wrap%debugIntvlInt /= 0) then
is%wrap%lwrite_debug=.TRUE.
call ESMF_TimeIntervalSet(is%wrap%debugIntvl, &
s=is%wrap%debugIntvlInt, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_TimeIntervalSet(is%wrap%debugImpAccum, s_r8=0._ESMF_KIND_R8, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_TimeIntervalSet(is%wrap%debugExpAccum, s_r8=0._ESMF_KIND_R8, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
! Determine Verbosity
call ESMF_AttributeGet(gcomp, name="verbosity", value=value, &
defaultValue="default", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%verbosity = ESMF_UtilString2Int(value, &
specialStringList=(/"none","min","max","debug","default"/), &
specialValueList=(/VERBOSITY_LV0,VERBOSITY_LV1,VERBOSITY_LV2, &
VERBOSITY_LV3,VERBOSITY_LV1/), rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! Determine hydro configuration filename
call ESMF_AttributeGet(gcomp, name="config_file", value=value, &
defaultValue="hydro.namelist", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%configFile = value
! Determine DAS configuration filename
call ESMF_AttributeGet(gcomp, name="das_config_file", value=value, &
defaultValue="namelist.hrldas", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%dasConfigFile = value
! Connect Nest to Nest
call ESMF_AttributeGet(gcomp, name="nest_to_nest", value=value, &
defaultValue="false", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%nestToNest = (trim(value)=="true")
! Forcing Directory
call ESMF_AttributeGet(gcomp, name="forcings_directory", value=value, &
defaultValue=is%wrap%forcingDir, &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%forcingDir = trim(value)
! Write coupled grid files
call ESMF_AttributeGet(gcomp, name="write_grid", value=value, &
defaultValue="true", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%lwrite_grid = (trim(value)=="true")
! Log Memory
call ESMF_AttributeGet(gcomp, name="log_memory", value=value, &
defaultValue="false", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%llog_memory = (trim(value)=="true")
! Test fill import fields
call ESMF_AttributeGet(gcomp, name="testfill_imp", value=value, &
defaultValue="false", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%ltestfill_imp = (trim(value)=="true")
! Test fill export fields
call ESMF_AttributeGet(gcomp, name="testfill_exp", value=value, &
defaultValue="false", &
convention="NUOPC", purpose="Instance", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%ltestfill_exp = (trim(value)=="true")
! Switch to IPDv03 by filtering all other phaseMap entries
call NUOPC_CompFilterPhaseMap(gcomp, ESMF_METHOD_INITIALIZE, &
acceptStringList=(/"IPDv03p"/), rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (is%wrap%verbosity >= VERBOSITY_LV1) &
call LogAttributes(trim(cname),gcomp)
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "InitializeP1"
subroutine InitializeP1(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! local variables
character(32) :: cname
type(type_InternalState) :: is
type(ESMF_VM) :: vm
integer :: fIndex
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! initialize wrfhydro
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if(ESMF_STDERRORCHECK(rc)) return ! bail out
call wrfhydro_nuopc_ini(is%wrap%did,vm,clock,is%wrap%forcingDir,rc=rc)
if(ESMF_STDERRORCHECK(rc)) return ! bail out
! get hgrid for domain id
call WRFHYDRO_get_hgrid(is%wrap%did,is%wrap%hgrid,rc=rc)
if(ESMF_STDERRORCHECK(rc)) return ! bail out
! add namespace
if(.NOT.is%wrap%nestToNest) then
is%wrap%NStateImp(1) = importState
is%wrap%NStateExp(1) = exportState
else
call NUOPC_AddNestedState(importState, &
CplSet=trim(is%wrap%hgrid), &
nestedStateName="NestedStateImp_N1", &
nestedState=is%wrap%NStateImp(1), rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_AddNestedState(exportState, &
CplSet=trim(is%wrap%hgrid), &
nestedStateName="NestedStateExp_N1", &
nestedState=is%wrap%NStateExp(1), rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
call WRFHYDRO_FieldDictionaryAdd(rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
!!
!! advertise import and export fields
!!
do fIndex = 1, size(WRFHYDRO_FieldList)
if (WRFHYDRO_FieldList(fIndex)%adImport) then
call NUOPC_Advertise(is%wrap%NStateImp(1), &
standardName=trim(WRFHYDRO_FieldList(fIndex)%stdname), &
name=trim(WRFHYDRO_FieldList(fIndex)%stdname), &
rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
if (WRFHYDRO_FieldList(fIndex)%adExport) then
call NUOPC_Advertise(is%wrap%NStateExp(1), &
standardName=trim(WRFHYDRO_FieldList(fIndex)%stdname), &
name=trim(WRFHYDRO_FieldList(fIndex)%stdname), &
rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
enddo
if (is%wrap%verbosity >= VERBOSITY_LV1) &
call LogAdvertised(trim(cname))
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "InitializeP3"
subroutine InitializeP3(gcomp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! local variables
character(32) :: cname
type(type_InternalState) :: is
type(ESMF_Grid) :: WRFHYDRO_Grid
type(ESMF_Field) :: field
logical :: importConnected, exportConnected
integer :: fIndex
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
WRFHYDRO_Grid = WRFHYDRO_GridCreate(is%wrap%did,rc=rc)
if(ESMF_STDERRORCHECK(rc)) return ! bail out
if (is%wrap%lwrite_grid) then
call WRFHYDRO_ESMF_GridWrite(WRFHYDRO_Grid, &
trim(cname)//'_grid_D'//trim(is%wrap%hgrid)//".nc", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
do fIndex = 1, size(WRFHYDRO_FieldList)
if (WRFHYDRO_FieldList(fIndex)%adImport) then
importConnected = NUOPC_IsConnected(is%wrap%NStateImp(1), &
fieldName=WRFHYDRO_FieldList(fIndex)%stdname)
else
importConnected = .FALSE.
endif
if (importConnected) then
WRFHYDRO_FieldList(fIndex)%realizedImport = .TRUE.
field = WRFHYDRO_FieldCreate(stdName=WRFHYDRO_FieldList(fIndex)%stdname, &
grid=WRFHYDRO_grid, &
did=is%wrap%did, &
rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_Realize(is%wrap%NStateImp(1), field=field, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
elseif(WRFHYDRO_FieldList(fIndex)%adImport) then
call ESMF_StateRemove(is%wrap%NStateImp(1), (/trim(WRFHYDRO_FieldList(fIndex)%stdname)/), &
relaxedflag=.true.,rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
endif
if (WRFHYDRO_FieldList(fIndex)%adExport) then
exportConnected = NUOPC_IsConnected(is%wrap%NStateExp(1), &
fieldName=WRFHYDRO_FieldList(fIndex)%stdname)
else
exportConnected = .FALSE.
endif
if (exportConnected) then
WRFHYDRO_FieldList(fIndex)%realizedExport = .TRUE.
field = WRFHYDRO_FieldCreate(stdName=WRFHYDRO_FieldList(fIndex)%stdname, &
grid=WRFHYDRO_grid, &
did=is%wrap%did, &
rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_Realize(is%wrap%NStateExp(1), field=field,rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
elseif(WRFHYDRO_FieldList(fIndex)%adExport) then
call ESMF_StateRemove(is%wrap%NStateExp(1),(/trim(WRFHYDRO_FieldList(fIndex)%stdname)/), &
relaxedflag=.true.,rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
endif
! TODO: Initialize the value in the pointer to 0 after proper restart is setup
!if(associated(WRFHYDRO_FieldList(fIndex)%farrayPtr) ) WRFHYDRO_FieldList(fIndex)%farrayPtr = 0.0
! remove a not connected Field from State
enddo
! Model has initialized its own field memory so don't fill state.
! call NUOPC_FillState(is%wrap%NStateImp(1),0,rc=rc)
! if (ESMF_STDERRORCHECK(rc)) return
! call NUOPC_FillState(is%wrap%NStateExp(1),0,rc=rc)
! if (ESMF_STDERRORCHECK(rc)) return
is%wrap%mode(1) = WRFHYDRO_RunModeGet(is%wrap%NStateImp(1),rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (is%wrap%verbosity >= VERBOSITY_LV1) &
call LogRealized(trim(cname))
if (is%wrap%verbosity >= VERBOSITY_LV1) &
call LogMode(trim(cname),gcomp)
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "DataInitialize"
subroutine DataInitialize(gcomp, rc)
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
character(32) :: cname
type(type_InternalState) :: is
type(ESMF_Clock) :: modelClock
integer :: iIndex
integer :: itemCount
character(len=64),allocatable :: itemNameList(:)
type(ESMF_StateItem_Flag), allocatable :: itemTypeList(:)
type(ESMF_Field) :: field
integer :: stat
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%timeSlice = is%wrap%timeSlice + 1
! query the Component for its clock
call NUOPC_ModelGet(gcomp, modelClock=modelClock, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! Initialize import and export fields
! No initialization. Fields remain set to initial value
! Fill import fields with test data
if (is%wrap%ltestfill_imp) then
! Not Implemented
endif
! Fill export fields with test data
if (is%wrap%ltestfill_exp) then
! Not Implemented
endif
call ESMF_StateGet(is%wrap%NStateExp(1),itemCount=itemCount, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
allocate( &
itemNameList(itemCount), &
itemTypeList(itemCount), &
stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Allocation of state item list memory failed.", &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
call ESMF_StateGet(is%wrap%NStateExp(1),itemNameList=itemNameList, &
itemTypeList=itemTypeList,rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
do iIndex=1, itemCount
if ( itemTypeList(iIndex) == ESMF_STATEITEM_FIELD) then
call ESMF_StateGet(is%wrap%NStateExp(1),field=field, &
itemName=itemNameList(iIndex),rc=rc)
if (ESMF_STDERRORCHECK(rc)) return
call NUOPC_SetAttribute(field, name="Updated", value="true", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
enddo
deallocate(itemNameList, itemTypeList, stat=stat)
if (ESMF_LogFoundDeallocError(statusToCheck=stat, &
msg="Deallocation of state item list memory failed.", &
line=__LINE__, &
file=__FILE__)) &
return ! bail out
! Write init files if lwrite_debug is on
if (is%wrap%lwrite_debug) then
call NUOPC_Write(is%wrap%NStateImp(1), &
fileNamePrefix="field_"//trim(cname)//"_imp_D"//trim(is%wrap%hgrid)//'_', &
overwrite=.false., timeslice=is%wrap%debugImpSlice, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call NUOPC_Write(is%wrap%NStateExp(1), &
fileNamePrefix="field_"//trim(cname)//"_exp_D"//trim(is%wrap%hgrid)//'_', &
overwrite=.false., timeslice=is%wrap%debugExpSlice, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%debugImpSlice = is%wrap%debugImpSlice + 1
is%wrap%debugExpSlice = is%wrap%debugExpSlice + 1
endif
! set InitializeDataComplete Attribute to "true", indicating to the
! generic code that all inter-model data dependencies are satisfied
call NUOPC_CompAttributeSet(gcomp, name="InitializeDataComplete", value="true", rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! if (is%wrap%verbosity >= VERBOSITY_LV3) &
! call WRFHydro_FieldListLog(label=trim(cname))
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "SetClock"
subroutine SetClock(gcomp, rc)
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
! local variables
character(32) :: cname
type(type_InternalState) :: is
integer :: dt
type(ESMF_Clock) :: modelClock
type(ESMF_TimeInterval) :: timeStep
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query the Component for its clock
call NUOPC_ModelGet(gcomp, modelClock=modelClock, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query the clock for its timestep
call ESMF_ClockGet(modelClock, timeStep=timeStep, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query the timestep for seconds
call ESMF_TimeIntervalGet(timestep,s=dt,rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! override timestep
if (is%wrap%timeStepInt /= 0) then
call ESMF_TimeIntervalSet(timestep, &
s=is%wrap%timeStepInt, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call WRFHYDRO_set_timestep(is%wrap%did,real(is%wrap%timeStepInt),rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_ClockSet(modelClock, timeStep=timeStep, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
else
call WRFHYDRO_set_timestep(is%wrap%did,real(dt),rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
call NUOPC_CompSetClock(gcomp, modelClock, timeStep, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
is%wrap%clock(1) = modelClock
! Reset Timers
call ESMF_TimeIntervalSet(is%wrap%stepTimer(1), &
s_r8=0._ESMF_KIND_R8, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (is%wrap%lwrite_debug) then
call ESMF_TimeIntervalSet(is%wrap%debugImpAccum, &
s_r8=0._ESMF_KIND_R8, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
call ESMF_TimeIntervalSet(is%wrap%debugExpAccum, &
s_r8=0._ESMF_KIND_R8, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
endif
if (is%wrap%verbosity >= VERBOSITY_LV1) &
call LogClock(trim(cname),gcomp)
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------
#undef METHOD
#define METHOD "CheckImport"
subroutine CheckImport(gcomp, rc)
type(ESMF_GridComp) :: gcomp
integer,intent(out) :: rc
! local variables
character(32) :: cname
type(type_InternalState) :: is
integer :: nIndex
character(len=10) :: sStr
type(ESMF_Clock) :: modelClock
type(ESMF_Time) :: modelCurrTime
logical :: allCurrTime
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": entered "//METHOD, ESMF_LOGMSG_INFO)
#endif
rc = ESMF_SUCCESS
! Query component for name
call ESMF_GridCompGet(gcomp, name=cname, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query Component for its internal State
nullify(is%wrap)
call ESMF_UserCompGetInternalState(gcomp, label_InternalState, is, rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! query the Component for its clock
call NUOPC_ModelGet(gcomp, modelClock=modelClock, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! get the curr time out of the clock
call ESMF_ClockGet(modelClock, currTime=modelCurrTime, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
! check that Fields in the importState show correct timestamp
allCurrTime = NUOPC_IsAtTime(is%wrap%NStateImp(1), modelCurrTime, rc=rc)
if (ESMF_STDERRORCHECK(rc)) return ! bail out
if (.not.allCurrTime) then
call ESMF_LogWrite(trim(cname)//": NUOPC INCOMPATIBILITY DETECTED: "// &
"Import Fields not at correct time", &
ESMF_LOGMSG_WARNING)
endif
#ifdef DEBUG
call ESMF_LogWrite(MODNAME//": leaving "//METHOD, ESMF_LOGMSG_INFO)
#endif
end subroutine
!-----------------------------------------------------------------------------