getm_esmf.F90 139 KB
Newer Older
Knut's avatar
Knut committed
1
#include "cppdefs.h"
Knut's avatar
Knut committed
2
#define _GETM_NUOPC_
Knut's avatar
Knut committed
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
!-----------------------------------------------------------------------
!BOP
!
! !MODULE:  getm_esmf - routines to integrate GETM into ESMF
!
! !INTERFACE:
   module getm_esmf
!
! !DESCRIPTION:
!  Example for GriddedComponent can be found in
!  ESMFDIR/src/Superstructure/Component/examples/ESMF_GCompEx.F90.
!
! !USES:
   use esmf
   use NUOPC

   IMPLICIT NONE
   private
!
! !PUBLIC DATA MEMBERS:
   public SetServices ! (NUOPC requires this name), must be public
   !public SetVM       ! optional
   public do_getm_esmf
!
! !PRIVATE DATA MEMBERS:
   type type_getmInternalStateStruct
      sequence
   end type

   type type_getmInternalState
      sequence
      type(type_getmInternalStateStruct),pointer :: wrap
   end type

!  Note (KK): __FILE__ includes full path, thus too long for log
   character(len=*),parameter :: FILENAME="getm_esmf.F90"

   character(len=*),parameter :: name_depth  = &
      "water_depth_at_soil_surface"
   character(len=*),parameter :: name_eps3D  = &
      "dissipation_of_tke_in_water"
   character(len=*),parameter :: name_epsbot = &
      "dissipation_of_tke_at_soil_surface"
   character(len=*),parameter :: name_h3D    = &
      "layer_height_in_water"
   character(len=*),parameter :: name_hbot   = &
      "layer_height_at_soil_surface"
   character(len=*),parameter :: name_NN3D   = &
      "buoyancy_frequency_squared_in_water"
   character(len=*),parameter :: name_nuh3D  = &
      "turbulent_diffusivity_of_heat_in_water"
   character(len=*),parameter :: name_num3D  = &
      "turbulent_diffusivity_of_momentum_in_water"
   character(len=*),parameter :: name_numbot = &
      "turbulent_diffusivity_of_momentum_at_soil_surface"
   character(len=*),parameter :: name_S3D    = &
      "salinity_in_water"
60 61
   character(len=*),parameter :: name_slp    = &
      "air_pressure_at_sea_level"
Knut's avatar
Knut committed
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
   character(len=*),parameter :: name_SS3D   = &
      "shear_frequency_squared_in_water"
   character(len=*),parameter :: name_swr    = &
      "surface_downwelling_photosynthetic_radiative_flux"
   character(len=*),parameter :: name_T3D    = &
      "temperature_in_water"
   character(len=*),parameter :: name_taubmax= &
      "maximum_bottom_stress"
   character(len=*),parameter :: name_Tbot   = &
      "temperature_at_soil_surface"
   character(len=*),parameter :: name_tke3D  = &
      "turbulent_kinetic_energy_in_water"
   character(len=*),parameter :: name_tkebot = &
      "turbulent_kinetic_energy_at_soil_surface"
   character(len=*),parameter :: name_U2D    = &
      "depth_averaged_x_velocity_in_water"
   character(len=*),parameter :: name_U3D    = &
      "x_velocity_in_water"
   character(len=*),parameter :: name_Ubot   = &
      "x_velocity_at_soil_surface"
   character(len=*),parameter :: name_V2D    = &
      "depth_averaged_y_velocity_in_water"
   character(len=*),parameter :: name_V3D    = &
      "y_velocity_in_water"
   character(len=*),parameter :: name_Vbot   = &
      "y_velocity_at_soil_surface"
   character(len=*),parameter :: name_waveDir= &
      "wave_direction"
   character(len=*),parameter :: name_waveH  = &
      "wave_height"
   character(len=*),parameter :: name_waveK  = &
      "wave_number"
   character(len=*),parameter :: name_waveT  = &
      "wave_period"
   character(len=*),parameter :: name_windU  = &
      "wind_x_velocity_at_10m"
   character(len=*),parameter :: name_windV  = &
      "wind_y_velocity_at_10m"
!
! !REVISION HISTORY:
!  Original author(s): Knut Klingbeil
!
!EOP
!-----------------------------------------------------------------------

    contains

!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: do_getm_esmf
!
! !INTERFACE:
   subroutine do_getm_esmf()
!
! !DESCRIPTION:
!
! !USES:
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT PARAMETERS:
!
! !OUTPUT PARAMETERS:
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   type(ESMF_Clock)        :: mainClock,getmClock
   type(ESMF_GridComp)     :: getmComp
   type(ESMF_State)        :: importState,exportState
   type(ESMF_TimeInterval) :: runDuration
   integer                 :: phase,phase0,phaseCount
   logical                 :: phaseZeroFlag
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'do_getm_esmf() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_Initialize(defaultLogFileName='getmESMF_LogFile',         &
                        defaultCalKind=ESMF_CALKIND_GREGORIAN)
   call ESMF_LogSet(flush=.true.,                                      &
                    logmsgList=(/ESMF_LOGMSG_WARNING,ESMF_LOGMSG_ERROR/))

   !mainClock = ESMF_ClockCreate(...)

!  Note (KK): contextflag=ESMF_CONTEXT_OWN_VM (default) is required for
!             an individual petList!
!             By default ESMF_GridComp[SetServices|Initialize|Run|Finalize]()
!             are skipped for PETs not in the petList provided
!             to ESMF_GridCompCreate().
   getmComp = ESMF_GridCompCreate(contextflag=ESMF_CONTEXT_PARENT_VM,name="getm")

   importState = ESMF_StateCreate(stateintent=ESMF_STATEINTENT_IMPORT, &
                                  name="getmImportState")
   exportState = ESMF_StateCreate(stateintent=ESMF_STATEINTENT_EXPORT, &
                                  name="getmExportState")

   call ESMF_GridCompSetServices(getmComp,SetServices)

!  Note (KK): check whether a NUOPC-Zero phase is available, inquire
!             InitializePhaseMap (see e.g. NUOPC_Comp.F90::NUOPC_SearchPhaseMap())
   call ESMF_GridCompGetEPPhaseCount(getmComp,ESMF_METHOD_INITIALIZE,  &
                                     phaseCount,phaseZeroFlag)
   phase0=1
   if (phaseZeroFlag) phase0=0

   call toc(TIM_ESMF)

   do phase=phase0,phaseCount
!     Note (KK): The parent component (i.e. this routine) can provide a
!                clock with [start|stop]Time to the getmComp, either by
!                creating the getmComp with this clock or by providing
!                a clock to the InitService.
      call ESMF_GridCompInitialize(getmComp,importState=importState,   &
                                   exportState=exportState,phase=phase)
   end do

   call tic(TIM_ESMF)

!  Note (KK): The InitService of the GETM component created a clock.
!             For PETs not part of GETM's petList, this check is mandatory
!             and will return False!
   !call ESMF_GridCompGet(getmComp,clockIsPresent=clockIsPresent)
   !if (clockIsPresent) then
!     Note (KK): Usually the already created mainClock has only to be
!                adapted (don't forget to update currTime!). However,
!                here the mainClock has not even been created yet.
      call ESMF_GridCompGet(getmComp,clock=getmClock)
      !call ESMF_ClockGet(getmClock,startTime=startTime, &
      !                   stopTime=stopTime,runDuration=runDuration)
      !call ESMF_ClockSet(mainClock,startTime=startTime, &
      !                   stopTime=stopTime,timeStep=runDuration, &
      !                   currTime=startTime)
      mainClock = ESMF_ClockCreate(getmClock)
      call ESMF_ClockGet(mainClock,runDuration=runDuration)
      call ESMF_ClockSet(mainClock,name="mainClock",timeStep=runDuration)
   !end if ! if(clockIsPresent)

   call toc(TIM_ESMF)

   call ESMF_GridCompRun(getmComp,importState=importState,             &
                         exportState=exportState,clock=mainClock)
   call ESMF_GridCompFinalize(getmComp,importState=importState,        &
                              exportState=exportState,clock=mainClock)

   call tic(TIM_ESMF)

   call ESMF_GridCompDestroy(getmComp)

!  Note (KK): Usually mainClock is created by all PETs of the actual VM.
!             However, here it was only created by the PETs in GETM's
!             petList. In case that not all PETs are in GETM's petList,
!             a check of ESMF_GridCompIsPetLocal(getmComp) would be
!             mandatory!
   call ESMF_ClockDestroy(mainClock)

   call ESMF_Finalize()

   call toc(TIM_ESMF)

#ifdef DEBUG
   write(debug,*) 'Leaving do_getm_esmf()'
   write(debug,*)
#endif
   return

   end subroutine do_getm_esmf
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: SetServices - register GriddedComponent GETM
!
! !INTERFACE:
   subroutine SetServices(getmComp,rc)
!
! !DESCRIPTION:
!  Register user-code subroutines.
!  Set the entry points for standard ESMF Component methods
!  ESMF_GridComp[Initialize|Run|Finalize](), called by toplevel component.
!  The toplevel component requires this sub for its mandatory call to
!  ESMF_GridCompSetServices(getmComp,userRoutine=SetServices).
!  For interface see ESMFDIR/src/Superstructure/Component/src/ESMF_GridComp.F90.
!  For the NUOPC Layer this routine must be named SetServices().
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompSetServices().
!
! !USES:
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   logical                    :: abort
   character(len=ESMF_MAXSTR) :: name="getm"
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'SetServices() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)

   call ESMF_LogWrite(trim(name)//"::SetServices...",ESMF_LOGMSG_TRACE)

!  Optional keyword argument "phase" (default: 1) promotes multi-phase user-code.

   call ESMF_GridCompSetEntryPoint(getmComp,ESMF_METHOD_INITIALIZE,    &
                                   userRoutine=InitializeP0,phase=0,   &
                                   rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridCompSetEntryPoint(getmComp,ESMF_METHOD_INITIALIZE,    &
                                   userRoutine=InitializeP1,phase=1,   &
                                   rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridCompSetEntryPoint(getmComp,ESMF_METHOD_INITIALIZE,    &
                                   userRoutine=InitializeP2,phase=2,   &
                                   rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridCompSetEntryPoint(getmComp,ESMF_METHOD_RUN,           &
                                   userRoutine=RunP1,                  &
                                   rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridCompSetEntryPoint(getmComp,ESMF_METHOD_FINALIZE,      &
                                   userRoutine=FinalizeP1,             &
                                   rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  Optional registration of additional routines for checkpoint and
!  restart functions (ESMF_METHOD_[WRITE|READ]RESTART).
!  ...

   call toc(TIM_ESMF)

#ifdef DEBUG
   write(debug,*) 'Leaving SetServices()'
   write(debug,*)
#endif
   return

   end subroutine SetServices
!EOC
!-----------------------------------------------------------------------
#if 0
!BOP
!
! !ROUTINE: SetVM -
!
! !INTERFACE:
   subroutine SetVM(getmComp,rc)
!
! !DESCRIPTION:
!  The toplevel component requires this sub for the optional call to
!  ESMF_GridCompSetVM(getmComp,userRoutine=SetVM), which has to
!  be done *before* the call to ESMF_GridCompSetServices()!
!  For interface see ESMFDIR/src/Superstructure/Component/src/ESMF_GridComp.F90.
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompSetVM().
!
! !USES:
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   type(ESMF_VM) :: gVM
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'SetVM() # ',Ncall
#endif

   call ESMF_VMGetGlobal(gVM,rc=rc)
!  Calls to ESMF_VMGet, ESMF_GridCompSetVMMaxPEs,
!  ESMF_GridCompSetVM[Min|Max]Threads to modify VM of component

#ifdef DEBUG
   write(debug,*) 'Leaving SetVM()'
   write(debug,*)
#endif
   return

   end subroutine SetVM
!EOC
#endif
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: InitializeP0 -
!
! !INTERFACE:
   subroutine InitializeP0(getmComp,importState,exportState,clock,rc)
!
! !DESCRIPTION:
!  NUOPC initialises all components in various phases. By default NUOPC
!  assumes IPDv00 and thus requires userRoutines for init phases 1 and 2
!  (other phases are added/executed by NUOPC). If the userCode provides
!  different phases, this needs to be communicated to NUOPC via
!  InitializePhaseMap in an init phase 0.
!
!                                               | IPDv00 | IPDv01 | IPDv02
!  =============================================|========|========|========
!   add InitializePhaseMap attribute            | 0 (*?) | 0 (*?) | 0 (*?)
!  =============================================|========|========|========
!   advertise import and export fields          | p1 (!) | p1     | p1
!  =============================================|========|========|========
!   realize import and export fields (allocate) | p2 (!) | p3     | p3
!  =============================================|========|========|========
!   check field' Connected status               | p3     | p4     | p4
!   set internal clock (*)                      |        |        |
!  =============================================|========|========|========
!   initialize fields (*)                       | p4     | p5     | p5
!  =============================================|========|========|========
!
!  (!) has to be done by the user, (*) optional by the user
!
!  Note: states and clock are uninitialized if the toplevel component
!        did not provide corresponding arguments to
!        ESMF_GridCompInitialize(getmComp).
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompInitialize().
!
! !USES:
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
   type(ESMF_State)    :: importState,exportState ! may be uninitialized
   type(ESMF_Clock)    :: clock                   ! may be uninitialized
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   logical                                   :: abort
   character(len=ESMF_MAXSTR)                :: name="getm"
   character(len=NUOPC_PhaseMapStringLength) :: InitializePhaseMap(2)
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'InitializeP0() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)

   call ESMF_LogWrite(trim(name)//"::InitializeP0...",ESMF_LOGMSG_TRACE)

   InitializePhaseMap(1) = "IPDv00p1=1"
   InitializePhaseMap(2) = "IPDv00p2=2"

Knut's avatar
Knut committed
464
#ifdef _GETM_NUOPC_
Knut's avatar
Knut committed
465 466 467 468 469 470
   call NUOPC_CompAttributeAdd(getmComp,(/"InitializePhaseMap"/),rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call NUOPC_CompAttributeSet(getmComp,"InitializePhaseMap",          &
                               InitializePhaseMap,rc=rc)
Knut's avatar
Knut committed
471 472
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
473
#else
Knut's avatar
Knut committed
474 475 476 477 478 479
!  Note (KK): NUOPC attributes are purpose="Instance"
   call ESMF_AttributeAdd(getmComp,convention="NUOPC",purpose="General", &
                          attrList=(/"InitializePhaseMap"/),rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

Knut's avatar
Knut committed
480 481 482 483 484
   call ESMF_AttributeSet(getmComp,name="InitializePhaseMap",          &
                          valueList=InitializePhaseMap,                &
                          convention="NUOPC",purpose="General",rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
485
#endif
Knut's avatar
Knut committed
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

   call toc(TIM_ESMF)

#ifdef DEBUG
   write(debug,*) 'Leaving InitializeP0()'
   write(debug,*)
#endif
   return

   end subroutine InitializeP0
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: InitializeP1 -
!
! !INTERFACE:
   subroutine InitializeP1(getmComp,importState,exportState,clock,rc)
!
! !DESCRIPTION:
!  Note: states and clock are uninitialized if the toplevel component
!        did not provide corresponding arguments to
!        ESMF_GridCompInitialize(getmComp).
!        Status can be checked with [State|Clock]IsCreated().
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompInitialize().
!
! !USES:
#ifdef GETM_PARALLEL
   use mpi
   use halo_mpi   , only: comm_getm
#endif
   use time       , only: init_time,start,timestep
Knut's avatar
Knut committed
519
   use initialise , only: init_initialise,do_initialise,dryrun
Knut's avatar
Knut committed
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
   use integration, only: MinN,MaxN
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
   type(ESMF_State)    :: importState,exportState ! may be uninitialized
   type(ESMF_Clock)    :: clock                   ! may be uninitialized
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   type(ESMF_Clock)           :: getmClock
   type(ESMF_Time)            :: getmRefTime,getmStartTime,getmStopTime
   type(ESMF_TimeInterval)    :: getmTimeStep
   type(ESMF_VM)              :: vm
   type(type_getmInternalState)               :: gis
   type(type_getmInternalStateStruct),pointer :: isd=>NULL()
   integer                    :: localrc,comm,length,getmRunTimeStepCount
   logical                    :: abort,clockIsPresent
   character(len=ESMF_MAXSTR) :: name="getm"
   character(len=8)           :: datestr
   character(len=10)          :: timestr
   character(len=19)          :: TimeStrISOFrac,start_external,stop_external
#ifdef GETM_PARALLEL
    character(len=MPI_MAX_ERROR_STRING) :: mpierrmsg
#endif
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'InitializeP1() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,                           &
                         clockIsPresent=clockIsPresent,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_LogWrite(trim(name)//"::InitializeP1...",ESMF_LOGMSG_TRACE)

!  Set Internal State
   allocate(isd)
   gis%wrap => isd
   call ESMF_GridCompSetInternalState(getmComp,gis,rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  Optional Creation and Initialization of child components
!  (Create, SetServices, Initialize)
!  ...

!  Check whether toplevel component called ESMF_GridCompCreate() with clock.
   if (clockIsPresent) then
      call ESMF_GridCompGet(getmComp,clock=getmClock,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      clockIsPresent = ESMF_ClockIsCreated(getmClock,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
   end if
   if (.not. clockIsPresent) then
      clockIsPresent = ESMF_ClockIsCreated(clock,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      if (clockIsPresent) then
         getmClock = ESMF_ClockCreate(clock,rc=rc)
         abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

         call ESMF_ClockSet(getmClock,name=trim(name)//"Clock",rc=rc)
         abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

         call ESMF_GridCompSet(getmComp,clock=getmClock,rc=rc)
         abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
      end if
   end if


!  This is where the model specific setup code goes
!  (allocation, open files, initial conditions).

#ifdef GETM_PARALLEL
   call ESMF_GridCompGet(getmComp,vm=vm,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_VMGet(vm,mpiCommunicator=comm,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call MPI_COMM_DUP(comm,comm_getm,rc)
   if (rc .ne. MPI_SUCCESS) then
!     need depends on specified mpi error handler (i.e. not MPI_ERRORS_ARE_FATAL)
      call MPI_ERROR_STRING(rc,mpierrmsg,length,localrc)
      call ESMF_LogWrite(trim(mpierrmsg),                              &
                         ESMF_LOGMSG_ERROR,line=__LINE__,file=FILENAME)
      call ESMF_Finalize(endflag=ESMF_END_ABORT)
   end if
#endif

   call date_and_time(datestr,timestr)


   if (clockIsPresent) then

!     Use startTime and stopTime from already initialised getmClock.
      call ESMF_ClockGet(getmClock,startTime=getmStartTime,            &
                         stopTime=getmStopTime,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call ESMF_TimeGet(getmStartTime,timeStringISOFrac=start_external,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call ESMF_TimeGet(getmStopTime,timeStringISOFrac=stop_external,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call toc(TIM_ESMF)

      call init_initialise(datestr,timestr)
      call init_time(MinN,MaxN,start_external=start_external, &
                     stop_external=stop_external)
      call do_initialise()

      call tic(TIM_ESMF)

!     use internal GETM time step
      call ESMF_TimeIntervalSet(getmTimeStep,                          &
                                s_r8=real(timestep,kind=ESMF_KIND_R8), &
                                rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call ESMF_ClockSet(getmClock,timeStep=getmTimeStep,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   else

      call toc(TIM_ESMF)

      ! set up clock based on internal GETM specifications
677 678 679
      call init_initialise(datestr,timestr)
      call init_time(MinN,MaxN)
      call do_initialise()
Knut's avatar
Knut committed
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

      call tic(TIM_ESMF)

!     reference time
      TimeStrISOFrac=start(1:10)//"T"//start(12:19)
      !call ESMF_TimeSet(refTime,timeStringISOFrac=TimeStrISOFrac)
      call TimeStringISOFrac2ESMFtime(TimeStrISOFrac,getmRefTime)

!     time step
      call ESMF_TimeIntervalSet(getmTimeStep,                          &
                                s_r8=real(timestep,kind=ESMF_KIND_R8), &
                                rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      getmStartTime = getmRefTime + (MinN-1)*getmTimeStep
      getmRunTimeStepCount = MaxN - MinN + 1

!     create clock based on internal GETM specifications
      getmClock = ESMF_ClockCreate(getmTimeStep,getmStartTime,            &
                                   runTimeStepCount=getmRunTimeStepCount, &
                                   refTime=getmRefTime,                   &
                                   name='getmClock',rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call ESMF_GridCompSet(getmComp,clock=getmClock,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   end if

Knut's avatar
Knut committed
712
   call getmComp_init_grid(getmComp)
713
   call NUOPC_FieldDictionarySetAutoAdd(.true.)
Knut's avatar
Knut committed
714 715
   call init_importStateP1(getmComp,importState)
   call init_exportStateP1(getmComp,exportState)
Knut's avatar
Knut committed
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

   call toc(TIM_ESMF)

#ifdef DEBUG
   write(debug,*) 'Leaving InitializeP1()'
   write(debug,*)
#endif
   return

   end subroutine InitializeP1
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: InitializeP2 -
!
! !INTERFACE:
   subroutine InitializeP2(getmComp,importState,exportState,clock,rc)
!
! !DESCRIPTION:
!  Here neccessary import and export fields are allocated.
!
!  Note: states and clock are uninitialized if the toplevel component
!        did not provide corresponding arguments to
!        ESMF_GridCompInitialize(getmComp).
!        Status can be checked with [State|Clock]IsCreated().
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompInitialize().
!
! !USES:
Knut's avatar
Knut committed
746
   use initialise, only: finalise_initialise,dryrun
Knut's avatar
Knut committed
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
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
   type(ESMF_State)    :: importState,exportState ! may be uninitialized
   type(ESMF_Clock)    :: clock                   ! may be uninitialized
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   logical                    :: abort
   character(len=ESMF_MAXSTR) :: name="getm"
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'InitializeP2() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)

   call ESMF_LogWrite(trim(name)//"::InitializeP2...",ESMF_LOGMSG_TRACE)

   call init_exportStateP2(getmComp,exportState)

!  If the initial Export state needs to be filled, do it here.
   call getmComp_update_grid(getmComp)
   call update_exportState(getmComp,exportState)

   call toc(TIM_ESMF)

788 789
   call finalise_initialise()

Knut's avatar
Knut committed
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
   if (.not.dryrun) then
      STDERR LINE
      LEVEL1 'integrating....'
      STDERR LINE
   end if

   rc = ESMF_SUCCESS

#ifdef DEBUG
   write(debug,*) 'Leaving InitializeP2()'
   write(debug,*)
#endif
   return

   end subroutine InitializeP2
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: RunP1 -
!
! !INTERFACE:
   subroutine RunP1(getmComp,importState,exportState,clock,rc)
!
! !DESCRIPTION:
!  Note: states and clock are uninitialized if the toplevel component
!        did not provide corresponding arguments to
!        ESMF_GridCompRun(getmComp).
!        Status can be checked with [State|Clock]IsCreated().
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompRun().
!
! !USES:
   use initialise , only: runtype,dryrun
   use integration, only: time_step,MinN,MaxN
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
   type(ESMF_State)    :: importState,exportState ! may be uninitialized
   type(ESMF_Clock)    :: clock                   ! may be uninitialized
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   type(ESMF_Clock)           :: getmClock
   type(ESMF_Time)            :: getmTime,nextTime
   type(ESMF_TimeInterval)    :: getmTimeStep
   integer(ESMF_KIND_I8)      :: advanceCount
   integer(kind=kind(MinN))   :: n
   logical                    :: abort
   character(len=ESMF_MAXSTR) :: name="getm"
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'RunP1() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,clock=getmClock,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_LogWrite(trim(name)//"::RunP1...",ESMF_LOGMSG_TRACE)

!  call ESMF_StateGet(), etc to get fields, bundles, arrays from import state
   call read_importState(getmComp,importState)

   call ESMF_ClockGet(getmClock,timeStep=getmTimeStep,currtime=getmTime, &
                      advanceCount=advanceCount,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  use clock to do determine time of calling routine
   call ESMF_ClockGetNextTime(clock,nextTime,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   do while (getmTime + 0.5d0*getmTimeStep < nextTime )

      n = MinN + int(advanceCount,kind=kind(MinN))
      if (n .gt. MaxN ) then
         call ESMF_LogWrite('already reached MaxN',                    &
                            ESMF_LOGMSG_WARNING,line=__LINE__,file=FILENAME)
         !call ESMF_Finalize(endflag=ESMF_END_ABORT)
         rc = ESMF_RC_NOT_SET
         return
      end if

      call toc(TIM_ESMF)

!     optional Run of child components
!     ...

!     This is where the model specific computation goes.
      if (.not.dryrun) call time_step(runtype,n)

      call tic(TIM_ESMF)

      call ESMF_ClockAdvance(getmClock,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

      call ESMF_ClockGet(getmClock,currtime=getmTime,                  &
                         advanceCount=advanceCount,rc=rc)
      abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
      if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   end do

!  Fill export state here using ESMF_StateAdd(), etc
   call getmComp_update_grid(getmComp)
   call update_exportState(getmComp,exportState)

   call toc(TIM_ESMF)

#ifdef DEBUG
   write(debug,*) 'Leaving RunP1()'
   write(debug,*)
#endif
   return

   end subroutine RunP1
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: FinalizeP1 -
!
! !INTERFACE:
   subroutine FinalizeP1(getmComp,importState,exportState,clock,rc)
!
! !DESCRIPTION:
!  Note: states and clock are uninitialized if the toplevel component
!        did not provide corresponding arguments to
!        ESMF_GridCompFinalize(getmComp).
!        Status can be checked with [State|Clock]IsCreated().
!  The toplevel component can inquire rc via optional keyword argument
!  userRc to ESMF_GridCompFinalize().
!
! !USES:
   use initialise , only: runtype,dryrun
   use integration, only: MaxN
   use getm_timers, only: write_getm_timers
   use getm_timers, only: tic,toc,TIM_ESMF
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
   type(ESMF_State)    :: importState,exportState ! may be uninitialized
   type(ESMF_Clock)    :: clock                   ! may be uninitialized
!
! !OUTPUT PARAMETERS:
   integer,intent(out) :: rc
!
! !REVISION HISTORY:
!
! !LOCAL VARIABLES
   type(ESMF_Clock)           :: getmClock
   type(ESMF_Grid)            :: getmGrid
   type(ESMF_DistGrid)        :: getmDistGrid3D
   type(type_getmInternalState)               :: gis
   type(type_getmInternalStateStruct),pointer :: isd
   logical                    :: abort,clockIsPresent,GridIsPresent
   character(len=ESMF_MAXSTR) :: name="getm"
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'FinalizeP1() # ',Ncall
#endif

   call tic(TIM_ESMF)

   call ESMF_GridCompGet(getmComp,name=name,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)

   call ESMF_LogWrite(trim(name)//"::FinalizeP1...",ESMF_LOGMSG_TRACE)

!  optional Finalize and Destruction of child components
!  ...

!  Add whatever code here needed (deallocation,close files,flush results)
   call toc(TIM_ESMF)
   call clean_up(dryrun,runtype,MaxN)
#ifndef NO_TIMERS
   STDERR LINE
   call write_getm_timers
#endif
   call tic(TIM_ESMF)

!  Get Internal State
   call ESMF_GridCompGetInternalState(getmComp,gis,rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (.not. abort) then
!     Deallocate private data block
      isd => gis%wrap
      !deallocate(isd%waveH)
      deallocate(isd)
   end if

   call ESMF_GridCompGet(getmComp,clockIsPresent=clockIsPresent,       &
                                  gridIsPresent=GridIsPresent,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (.not. abort) then
      if (GridIsPresent) then
         call ESMF_GridCompGet(getmComp,grid=getmGrid,rc=rc)
         abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         if (.not. abort) then
            call ESMF_GridGet(getmGrid,distgrid=getmDistGrid3D,rc=rc)
            abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
            if (.not. abort) then
               !call ESMF_DistGridDestroy(getmDistGrid2D,rc=rc)
               !abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
               call ESMF_DistGridDestroy(getmDistGrid3D,rc=rc)
               abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
            end if
            !call ESMF_GridGetCoord(getmGrid,coordDim=...,staggerloc=...,array=array)
            !call ESMF_ArrayDestroy(array)
            call ESMF_GridDestroy(getmGrid,rc=rc)
            abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         end if
      end if
      if (clockIsPresent) then
         call ESMF_GridCompGet(getmComp,clock=getmClock,rc=rc)
         abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         if (.not. abort) then
            call ESMF_ClockDestroy(getmClock,rc=rc)
            abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
         end if
      end if
   end if

   call toc(TIM_ESMF)

   rc = ESMF_SUCCESS

#ifdef DEBUG
   write(debug,*) 'Leaving FinalizeP1()'
   write(debug,*)
#endif
   return

   end subroutine FinalizeP1
!EOC
!-----------------------------------------------------------------------
!BOP
!
! !ROUTINE: getmComp_init_grid - Creates Grid
!
! !INTERFACE:
Knut's avatar
Knut committed
1053
   subroutine getmComp_init_grid(getmComp)
Knut's avatar
Knut committed
1054 1055 1056 1057 1058 1059 1060
!
! !DESCRIPTION:
!
! !USES:
   use initialise, only: runtype
   use domain    , only: ioff,joff,imin,jmin,imax,jmax,kmax
   use domain    , only: grid_type,have_lonlat
Knut's avatar
Knut committed
1061
   use domain    , only: az,ax,areac_getm=>areac
Knut's avatar
Knut committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
   use domain    , only: xcord,ycord,xc,yc,lonc,latc
   use domain    , only: xxcord,yxcord,xx,yx,lonx,latx
#ifndef NO_3D
   use variables_3d, only: zwn,zcn
#endif
   IMPLICIT NONE
!
! !INPUT/OUTPUT PARAMETERS:
   type(ESMF_GridComp) :: getmComp
!
! !REVISION HISTORY:
!  Original Author(s): Knut Klingbeil
!
! !LOCAL VARIABLES
   type(ESMF_Array),target  :: xcArray2D,ycArray2D,xxArray2D,yxArray2D
   type(ESMF_Array),target  :: xcArray3D,ycArray3D,xxArray3D,yxArray3D
   type(ESMF_Array),target  :: loncArray2D,latcArray2D,lonxArray2D,latxArray2D
   type(ESMF_Array),target  :: loncArray3D,latcArray3D,lonxArray3D,latxArray3D
   type(ESMF_Array),pointer :: coordArrayXC2D=>NULL()
   type(ESMF_Array),pointer :: coordArrayXC3D=>NULL()
   type(ESMF_Array),pointer :: coordArrayYC2D=>NULL()
   type(ESMF_Array),pointer :: coordArrayYC3D=>NULL()
   type(ESMF_Array),pointer :: coordArrayXX2D=>NULL()
   type(ESMF_Array),pointer :: coordArrayXX3D=>NULL()
   type(ESMF_Array),pointer :: coordArrayYX2D=>NULL()
   type(ESMF_Array),pointer :: coordArrayYX3D=>NULL()
   type(ESMF_Array)         :: array
   type(ESMF_CoordSys_Flag) :: coordSys
Knut's avatar
Knut committed
1090
   type(ESMF_DistGrid)      :: getmDistGrid2D,getmDistGrid3D,distgrid
Knut's avatar
Knut committed
1091
   type(ESMF_Grid)          :: getmGrid3D,getmGrid2D
Knut's avatar
Knut committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
   type(ESMF_StaggerLoc)    :: staggerloc
   type(ESMF_VM)            :: vm
!  Note (KK): ESMF_ARRAY's are deep classes, that persist after return.
!             (even without save attribute), same for allocated pointers.
   real(ESMF_KIND_R8),pointer :: xc1D  (:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: yc1D  (:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: xx1D  (:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: yx1D  (:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: xc2D  (:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: yc2D  (:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: xx2D  (:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: yx2D  (:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: lonc1D(:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: latc1D(:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: lonx1D(:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: latx1D(:)    =>NULL()
   real(ESMF_KIND_R8),pointer :: lonc2D(:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: latc2D(:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: lonx2D(:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: latx2D(:,:)  =>NULL()
   real(ESMF_KIND_R8),pointer :: zw    (:,:,:)=>NULL()
   real(ESMF_KIND_R8),pointer :: zc    (:,:,:)=>NULL()
   real(ESMF_KIND_R8),pointer :: zx    (:,:,:)=>NULL()
   real(ESMF_KIND_R8),pointer :: areaC (:,:)
   real(ESMF_KIND_R8),pointer :: p2dr  (:,:)
   real(ESMF_KIND_R8),dimension(:,:,:),allocatable,target :: areaW3D
   integer(ESMF_KIND_I4),pointer :: maskC(:,:)
   integer(ESMF_KIND_I4),pointer :: maskX(:,:)
   integer(ESMF_KIND_I4),dimension(:,:,:),allocatable,target :: maskC3D
   integer(ESMF_KIND_I4),dimension(:,:,:),allocatable,target :: maskX3D
   integer(ESMF_KIND_I4),dimension(:),allocatable,target :: alledges
   integer(ESMF_KIND_I4),dimension(4),target             :: myedges
   integer(ESMF_KIND_I4),pointer                         :: p2di(:,:)
   REALTYPE                 :: getmreal
   integer                  :: rc,pet,i0,j0,ilen,jlen,klen,k
   integer                  :: petCount
   integer,dimension(3)     :: coordDimCount
   integer,dimension(3,3)   :: coordDimMap
   integer,dimension(:,:,:),allocatable                  :: deBlockList
   logical                    :: abort,noKindMatch
   character(len=ESMF_MAXSTR) :: name="getm"
!
!EOP
!-----------------------------------------------------------------------
!BOC
#ifdef DEBUG
   integer, save :: Ncall = 0
   Ncall = Ncall+1
   write(debug,*) 'getmComp_init_grid() # ',Ncall
#endif

   call ESMF_GridCompGet(getmComp,name=name,vm=vm,petCount=petCount,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   myedges = (/ ioff , joff , imax , jmax /)
   allocate(alledges(4*petCount))

!  syncflag=ESMF_SYNC_BLOCKING (default)
   call ESMF_VMAllGather(vm,myedges,alledges,4,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   if (runtype .eq. 1) then
      klen = 1
   else
      klen = kmax
   end if

   allocate(deBlockList(3,2,petCount))
   do pet = 0,petCount-1
      i0   = 1 + alledges(1+4*pet)
      j0   = 1 + alledges(2+4*pet)
      ilen =     alledges(3+4*pet)
      jlen =     alledges(4+4*pet)
      deBlockList(:,1,1+pet) = (/ i0        , j0        , 1        /)
      deBlockList(:,2,1+pet) = (/ i0+ilen-1 , j0+jlen-1 , 1+klen-1 /)
   end do

!  indexflag=ESMF_INDEX_DELOCAL (default) starting at 1
!  (for ESMF_INDEX_USER [grid|stagger]MemLBound can be set)
#if 1
!  Single-tile DistGrid (1 subdomain = 1 DE)
!  internal call to ESMF_DistGridCreateDB()
   getmDistGrid2D = ESMF_DistGridCreate(minval(deBlockList(1:2,1,:),2), &
                                        maxval(deBlockList(1:2,2,:),2), &
                                        int(deBlockList(1:2,:,:)),rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
   getmDistGrid3D = ESMF_DistGridCreate(minval(deBlockList(:,1,:),2), &
                                        maxval(deBlockList(:,2,:),2), &
                                        deBlockList,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
#else
!  Multi-tile DistGrid (1 subdomain = 1 tile = 1 DE) by specification of
!  [min|max]IndexPTile.
!  Note (KK): int() intrinsic routines are needed, because ESMF does not
!             accept subarrays as arguments
!  internal call to ESMF_DistGridCreateRDT()
   getmDistGrid2D = ESMF_DistGridCreate(int(deBlockList(1:2,1,:)), &
                                        int(deBlockList(1:2,2,:)),rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
   getmDistGrid3D = ESMF_DistGridCreate(int(deBlockList(:,1,:)), &
                                        int(deBlockList(:,2,:)),rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
#endif

   noKindMatch = ( kind(getmreal) .ne. ESMF_KIND_R8 )

!  KK-TODO: get 2d farrayPtr from created persistent deep 3D Array
   allocate(maskC3D(E2DFIELD,0:klen))
   allocate(maskX3D(E2DFIELD,0:klen))
   allocate(areaW3D(E2DFIELD,0:klen))

   p2di => maskC3D(:,:,klen) ; maskC(imin-HALO:,jmin-HALO:) => p2di
   p2di => maskX3D(:,:,klen) ; maskX(imin-HALO:,jmin-HALO:) => p2di
   p2dr => areaW3D(:,:,klen) ; areaC(imin-HALO:,jmin-HALO:) => p2dr

   maskC = az
   maskX = ax

#ifdef SLICE_MODEL
   maskC(:,jmax/2+1) = 0
   maskX(:,jmax/2+1) = 0
#endif

Knut's avatar
Knut committed
1221
   areaC = areac_getm
Knut's avatar
Knut committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

   do k=0,klen
      maskC3D(:,:,k) = maskC
      maskX3D(:,:,k) = maskX
      areaW3D(:,:,k) = areaC
   end do
   maskC3D(:,:,0) = 0

   allocate(zx(E2DXFIELD,0:klen))
   if (noKindMatch .or. runtype.eq.1) then
      allocate(zw(E2DFIELD,0:klen))
      allocate(zc(E2DFIELD,0:klen))
   else
#ifndef NO_3D
      zw => zwn
      zc => zcn
#endif
   end if

Knut's avatar
Knut committed
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
   select case (grid_type)

!        coordDimMap: for each grid dimension i map array dimension j
!                     to grid dimension coordDimMap(i,j)
!                     i=1,dimCount ; j=1,coordDimCount(i)

      case(1)

         coordSys = ESMF_COORDSYS_CART
         coordDimCount = (/ 1 , 1 , 3 /)     ! rectilinear horizontal coordinates
         coordDimMap = reshape( (/1,2,1,0,0,2,0,0,3/) , (/3,3/) )

      case(2)

         coordSys = ESMF_COORDSYS_SPH_DEG    ! (default)
         coordDimCount = (/ 1 , 1 , 3 /)     ! rectilinear horizontal coordinates
         coordDimMap = reshape( (/1,2,1,0,0,2,0,0,3/) , (/3,3/) )

      case(3)

         coordSys = ESMF_COORDSYS_CART
         coordDimCount = (/ 2 , 2 , 3 /)
         coordDimMap = reshape( (/1,1,1,2,2,2,0,0,3/) , (/3,3/) ) ! (default)

      case(4)

         coordSys = ESMF_COORDSYS_SPH_DEG                         ! (default)
         coordDimCount = (/ 2 , 2 , 3 /)
         coordDimMap = reshape( (/1,1,1,2,2,2,0,0,3/) , (/3,3/) ) ! (default)

   end select

!  Note (KK): gridAlign specifies which corner point in a grid cell
!             shares the center indices [ default=(/-1,...,-1/) ].
!             gridEdge[L|U]Width only affect DE's at the edge of tiles
!             (thus it matters whether a single- or multi-tile DistGrid
!              was created). If gridEdgeWidth's are not set, they are set
!             automatically based on gridAlign.
!             For single staggered grid items, default specification for
!             gridAlign and gridEdgesWidth's can be overwritten by
!             staggerAlign and staggerEdgeWidth's.
!  internal call to ESMF_GridCreateFrmDistGrid()
   getmGrid2D = ESMF_GridCreate(getmDistGrid2D,name=trim(name)//"Grid2D", &
#if 0
! bug in ESMF
                                gridAlign=(/1,1/),                        &
#else
                                gridEdgeLWidth=(/1,1/),                   &
#endif
                                coordSys=coordSys,                        &
                                coordDimCount=int(coordDimCount(1:2)),    &
                                coordDimMap=int(coordDimMap(1:2,1:2)),    &
                                rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   getmGrid3D = ESMF_GridCreate(getmDistGrid3D,name=trim(name)//"Grid3D", &
#if 0
! bug in ESMF
                                gridAlign=(/1,1,1/),                      &
#else
                                gridEdgeLWidth=(/1,1,1/),                 &
#endif

                                coordSys=coordSys,                        &
                                coordDimCount=coordDimCount,              &
                                coordDimMap=coordDimMap,                  &
                                rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

Knut's avatar
Knut committed
1312 1313 1314 1315 1316 1317 1318
   select case (grid_type)

      case(1)

         if (noKindMatch) then
            allocate(xc1D(   _IRANGE_HALO_)) ; xc1D = xcord
            allocate(yc1D(   _JRANGE_HALO_)) ; yc1D = ycord
1319 1320
            allocate(xx1D(-1+_IRANGE_HALO_)) ; xx1D = xxcord
            allocate(yx1D(-1+_JRANGE_HALO_)) ; yx1D = yxcord
Knut's avatar
Knut committed
1321 1322 1323 1324 1325 1326 1327 1328 1329
         else
            xc1D => xcord
            yc1D => ycord
            xx1D => xxcord
            yx1D => yxcord
         end if

!        put pointers into persistent deep Arrays
         call createCoordArrays1D(xc1d,yc1d,xx1d,yx1d,                     &
Knut's avatar
Knut committed
1330
                                  getmGrid2D,getmGrid3D,                   &
Knut's avatar
Knut committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
                                  xcArray2D,ycArray2D,xxArray2D,yxArray2D, &
                                  xcArray3D,ycArray3D,xxArray3D,yxArray3D)
         call setCoordUnits("m","m",                                   &
                            xcArray2D,ycArray2D,xxArray2D,yxArray2D,   &
                            xcArray3D,ycArray3D,xxArray3D,yxArray3D)

         coordArrayXC2D => xcArray2D ; coordArrayYC2D => ycArray2D
         coordArrayXX2D => xxArray2D ; coordArrayYX2D => yxArray2D
         coordArrayXC3D => xcArray3D ; coordArrayYC3D => ycArray3D
         coordArrayXX3D => xxArray3D ; coordArrayYX3D => yxArray3D

      case(2)

         if (noKindMatch) then
            allocate(lonc1D(   _IRANGE_HALO_)) ; lonc1D = xcord
            allocate(latc1D(   _JRANGE_HALO_)) ; latc1D = ycord
1347 1348
            allocate(lonx1D(-1+_IRANGE_HALO_)) ; lonx1D = xxcord
            allocate(latx1D(-1+_JRANGE_HALO_)) ; latx1D = yxcord
Knut's avatar
Knut committed
1349 1350 1351 1352 1353 1354 1355 1356 1357
         else
            lonc1D => xcord
            latc1D => ycord
            lonx1D => xxcord
            latx1D => yxcord
         end if

!        put pointers into persistent deep Arrays
         call createCoordArrays1D(lonc1d,latc1d,lonx1d,latx1d,                     &
Knut's avatar
Knut committed
1358
                                  getmGrid2D,getmGrid3D,                           &
Knut's avatar
Knut committed
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
                                  loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                                  loncArray3D,latcArray3D,lonxArray3D,latxArray3D)
         call setCoordUnits("degrees_east","degrees_north",                  &
                            loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                            loncArray3D,latcArray3D,lonxArray3D,latxArray3D)

         coordArrayXC2D => loncArray2D ; coordArrayYC2D => latcArray2D
         coordArrayXX2D => lonxArray2D ; coordArrayYX2D => latxArray2D
         coordArrayXC3D => loncArray3D ; coordArrayYC3D => latcArray3D
         coordArrayXX3D => lonxArray3D ; coordArrayYX3D => latxArray3D

      case(3)

         if (noKindMatch) then
            allocate(xx2D(E2DXFIELD)) ; xx2D = xx
            allocate(yx2D(E2DXFIELD)) ; yx2D = yx
            allocate(xc2D(E2DFIELD )) ; xc2D = xc
            allocate(yc2D(E2DFIELD )) ; yc2D = yc
            if (have_lonlat) then
               allocate(lonx2D(E2DXFIELD)) ; lonx2D = lonx
               allocate(latx2D(E2DXFIELD)) ; latx2D = latx
               allocate(lonc2D(E2DFIELD )) ; lonc2D = lonc
               allocate(latc2D(E2DFIELD )) ; latc2D = latc
            end if
         else
            xx2D => xx
            yx2D => yx
            xc2D => xc
            yc2D => yc
            if (have_lonlat) then
               lonx2D => lonx
               latx2D => latx
               lonc2D => lonc
               latc2D => latc
            end if
         end if

!        put pointers into persistent deep Arrays
         call createCoordArrays2D(xc2d,yc2d,xx2d,yx2d,                     &
Knut's avatar
Knut committed
1398
                                  getmGrid2D,getmGrid3D,                   &
Knut's avatar
Knut committed
1399 1400 1401 1402 1403 1404 1405 1406
                                  xcArray2D,ycArray2D,xxArray2D,yxArray2D, &
                                  xcArray3D,ycArray3D,xxArray3D,yxArray3D)
         call setCoordUnits("m","m",                                   &
                            xcArray2D,ycArray2D,xxArray2D,yxArray2D,   &
                            xcArray3D,ycArray3D,xxArray3D,yxArray3D)

         if (have_lonlat) then
            call createCoordArrays2D(lonc2d,latc2d,lonx2d,latx2d,                     &
Knut's avatar
Knut committed
1407
                                     getmGrid2D,getmGrid3D,                           &
Knut's avatar
Knut committed
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
                                     loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                                     loncArray3D,latcArray3D,lonxArray3D,latxArray3D)
            call setCoordUnits("degrees_east","degrees_north",                  &
                               loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                               loncArray3D,latcArray3D,lonxArray3D,latxArray3D)
         end if

         coordArrayXC2D => xcArray2D ; coordArrayYC2D => ycArray2D
         coordArrayXX2D => xxArray2D ; coordArrayYX2D => yxArray2D
         coordArrayXC3D => xcArray3D ; coordArrayYC3D => ycArray3D
         coordArrayXX3D => xxArray3D ; coordArrayYX3D => yxArray3D

         if (have_lonlat) then
            coordArrayXC2D => loncArray2D ; coordArrayYC2D => latcArray2D
            coordArrayXX2D => lonxArray2D ; coordArrayYX2D => latxArray2D
            coordArrayXC3D => loncArray3D ; coordArrayYC3D => latcArray3D
            coordArrayXX3D => lonxArray3D ; coordArrayYX3D => latxArray3D
         end if

      case(4)

         if (noKindMatch) then
            allocate(lonx2D(E2DXFIELD)) ; lonx2D = lonx
            allocate(latx2D(E2DXFIELD)) ; latx2D = latx
            allocate(lonc2D(E2DFIELD )) ; lonc2D = lonc
            allocate(latc2D(E2DFIELD )) ; latc2D = latc
         else
            lonx2D => lonx
            latx2D => latx
            lonc2D => lonc
            latc2D => latc
         end if

!        put pointers into persistent deep Arrays
         call createCoordArrays2D(lonc2d,latc2d,lonx2d,latx2d,                     &
Knut's avatar
Knut committed
1443
                                  getmGrid2D,getmGrid3D,                           &
Knut's avatar
Knut committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
                                  loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                                  loncArray3D,latcArray3D,lonxArray3D,latxArray3D)
         call setCoordUnits("degrees_east","degrees_north",                  &
                            loncArray2D,latcArray2D,lonxArray2D,latxArray2D, &
                            loncArray3D,latcArray3D,lonxArray3D,latxArray3D)

         coordArrayXC2D => loncArray2D ; coordArrayYC2D => latcArray2D
         coordArrayXX2D => lonxArray2D ; coordArrayYX2D => latxArray2D
         coordArrayXC3D => loncArray3D ; coordArrayYC3D => latcArray3D
         coordArrayXX3D => lonxArray3D ; coordArrayYX3D => latxArray3D

   end select

!  2D grid

Knut's avatar
Knut committed
1459
   staggerloc = ESMF_STAGGERLOC_CENTER ! (default)
Knut's avatar
Knut committed
1460 1461 1462
   call ESMF_GridGet(getmGrid2D, staggerloc, distgrid=distgrid, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

   call ESMF_GridSetCoord(getmGrid2D,1,array=coordArrayXC2D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetCoord(getmGrid2D,2,array=coordArrayYC2D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  2D center mask
Knut's avatar
Knut committed
1475
   array = ESMF_ArrayCreate(distgrid,maskC,                            &
Knut's avatar
Knut committed
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            datacopyflag=ESMF_DATACOPY_VALUE,          &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  KK-TODO: add attribute with un-/mask value
   call ESMF_GridSetItem(getmGrid2D,ESMF_GRIDITEM_MASK,array=array,    &
                         staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  2D center area
Knut's avatar
Knut committed
1489
   array = ESMF_ArrayCreate(distgrid,areaC,                            &
Knut's avatar
Knut committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            datacopyflag = ESMF_DATACOPY_VALUE,        &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_AttributeSet(array,'units','m**2',rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetItem(getmGrid2D,ESMF_GRIDITEM_AREA,array=array,    &
                         staggerloc=StaggerLoc, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

Knut's avatar
Knut committed
1505
   staggerloc = ESMF_STAGGERLOC_CORNER
Knut's avatar
Knut committed
1506 1507 1508
   call ESMF_GridGet(getmGrid2D, staggerloc, distgrid=distgrid, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520

   call ESMF_GridSetCoord(getmGrid2D,1,array=coordArrayXX2D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetCoord(getmGrid2D,2,array=coordArrayYX2D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  2D corner mask
Knut's avatar
Knut committed
1521
   array = ESMF_ArrayCreate(distgrid,maskX,                            &
Knut's avatar
Knut committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            datacopyflag=ESMF_DATACOPY_VALUE,          &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  KK-TODO: add attribute with un-/mask value
   call ESMF_GridSetItem(getmGrid2D,ESMF_GRIDITEM_MASK,array=array,    &
                         staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

Knut's avatar
Knut committed
1534 1535 1536 1537 1538
#if 0
call ESMF_OutputScripGridFile('GETM_Grid_Update.nc', getmGrid2D, rc)
abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
#endif
Knut's avatar
Knut committed
1539 1540 1541

!  3D grid

Knut's avatar
Knut committed
1542
   staggerloc = ESMF_STAGGERLOC_CENTER_VCENTER ! (=CENTER, default for 3D)
Knut's avatar
Knut committed
1543 1544 1545
   call ESMF_GridGet(getmGrid3D, staggerloc, distgrid=distgrid, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

   call ESMF_GridSetCoord(getmGrid3D,1,array=coordArrayXC3D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetCoord(getmGrid3D,2,array=coordArrayYC3D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  3D center position
Knut's avatar
Knut committed
1558
   array = ESMF_ArrayCreate(distgrid,zc,                               &
Knut's avatar
Knut committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            totalLWidth=(/HALO,HALO,1/),               &
                            totalUWidth=(/HALO,HALO,0/),               &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_AttributeSet(array,'units','m',rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetCoord(getmGrid3D,3,array=array,                    &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  3D center mask
!  KK-TODO: get rid of k=0 layer (implemented by MOSSCO commit 5e04bd4)
Knut's avatar
Knut committed
1577
   array = ESMF_ArrayCreate(distgrid,maskC3D,                          &
Knut's avatar
Knut committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            datacopyflag=ESMF_DATACOPY_VALUE,          &
                            totalLWidth=(/HALO,HALO,1/),               &
                            totalUWidth=(/HALO,HALO,0/),               &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  KK-TODO: add attribute with un-/mask value
   call ESMF_GridSetItem(getmGrid3D,ESMF_GRIDITEM_MASK,array=array,    &
                         staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  3D center area
   !array = ESMF_ArrayCreate(getmDistGrid3D,areaC,indexflag=ESMF_INDEX_DELOCAL,rc=rc)
!  KK-TODO: k=0 layer needed because of areaW3D (needed by MOSSCO)
Knut's avatar
Knut committed
1595
   array = ESMF_ArrayCreate(distgrid,areaW3D,                          &
Knut's avatar
Knut committed
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
                            indexflag=ESMF_INDEX_DELOCAL,              &
                            datacopyflag = ESMF_DATACOPY_VALUE,        &
                            totalLWidth=(/HALO,HALO,1/),               &
                            totalUWidth=(/HALO,HALO,0/),               &
                            rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_AttributeSet(array,'units','m**2',rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetItem(getmGrid3D,ESMF_GRIDITEM_AREA,array=array,    &
                         staggerloc=staggerloc, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

Knut's avatar
Knut committed
1613
   staggerloc = ESMF_STAGGERLOC_CORNER_VFACE
Knut's avatar
Knut committed
1614 1615 1616
   call ESMF_GridGet(getmGrid3D, staggerloc, distgrid=distgrid, rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)
Knut's avatar
Knut committed
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628

   call ESMF_GridSetCoord(getmGrid3D,1,array=coordArrayXX3D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

   call ESMF_GridSetCoord(getmGrid3D,2,array=coordArrayYX3D,           &
                          staggerloc=staggerloc,rc=rc)
   abort = ESMF_LogFoundError(rc,line=__LINE__,file=FILENAME)
   if (abort) call ESMF_Finalize(endflag=ESMF_END_ABORT)

!  3D corner position
Knut's avatar
Knut committed
1629 1630 1631 1632 </