New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
cpl_oasis3.F90 in NEMO/branches/UKMO/NEMO_4.0_new_runoff_coupling/src/OCE/SBC – NEMO

source: NEMO/branches/UKMO/NEMO_4.0_new_runoff_coupling/src/OCE/SBC/cpl_oasis3.F90 @ 11509

Last change on this file since 11509 was 11509, checked in by dancopsey, 5 years ago

Merge in all the 1D river runoff code as I comitted to the GO6 package branch (skipping anything to do with Antarctic and Greenland icesheet mass).

File size: 36.5 KB
Line 
1MODULE cpl_oasis3
2   !!======================================================================
3   !!                    ***  MODULE cpl_oasis  ***
4   !! Coupled O/A : coupled ocean-atmosphere case using OASIS3-MCT
5   !!=====================================================================
6   !! History :  1.0  !  2004-06  (R. Redler, NEC Laboratories Europe, Germany) Original code
7   !!             -   !  2004-11  (R. Redler, NEC Laboratories Europe; N. Keenlyside, W. Park, IFM-GEOMAR, Germany) revision
8   !!             -   !  2004-11  (V. Gayler, MPI M&D) Grid writing
9   !!            2.0  !  2005-08  (R. Redler, W. Park) frld initialization, paral(2) revision
10   !!             -   !  2005-09  (R. Redler) extended to allow for communication over root only
11   !!             -   !  2006-01  (W. Park) modification of physical part
12   !!             -   !  2006-02  (R. Redler, W. Park) buffer array fix for root exchange
13   !!            3.4  !  2011-11  (C. Harris) Changes to allow mutiple category fields
14   !!            3.6  !  2014-11  (S. Masson) OASIS3-MCT
15   !!----------------------------------------------------------------------
16   
17   !!----------------------------------------------------------------------
18   !!   'key_oasis3'                    coupled Ocean/Atmosphere via OASIS3-MCT
19   !!   'key_oa3mct_v3'                 to be added for OASIS3-MCT version 3
20   !!----------------------------------------------------------------------
21   !!   cpl_init     : initialization of coupled mode communication
22   !!   cpl_define   : definition of grid and fields
23   !!   cpl_snd      : snd out fields in coupled mode
24   !!   cpl_rcv      : receive fields in coupled mode
25   !!   cpl_finalize : finalize the coupled mode communication
26   !!----------------------------------------------------------------------
27#if defined key_oasis3
28   USE mod_oasis                    ! OASIS3-MCT module
29#endif
30   USE par_oce                      ! ocean parameters
31   USE cpl_rnf_1d, ONLY: nn_cpl_river   ! Variables used in 1D river outflow
32   USE dom_oce                      ! ocean space and time domain
33   USE in_out_manager               ! I/O manager
34   USE lbclnk                       ! ocean lateral boundary conditions (or mpp link)
35
36   IMPLICIT NONE
37   PRIVATE
38
39#if ! defined key_oasis3
40   ! Dummy interface to oasis_get if not using oasis
41   INTERFACE oasis_get 
42      MODULE PROCEDURE oasis_get_1d, oasis_get_2d 
43   END INTERFACE 
44#endif
45
46   PUBLIC   cpl_init
47   PUBLIC   cpl_define
48   PUBLIC   cpl_snd
49   PUBLIC   cpl_rcv
50   PUBLIC   cpl_rcv_1d
51   PUBLIC   cpl_freq
52   PUBLIC   cpl_finalize
53
54   INTEGER, PUBLIC            ::   OASIS_Rcv  = 1    !: return code if received field
55   INTEGER, PUBLIC            ::   OASIS_idle = 0    !: return code if nothing done by oasis
56   INTEGER                    ::   ncomp_id          ! id returned by oasis_init_comp
57   INTEGER                    ::   nerror            ! return error code
58#if ! defined key_oasis3
59   ! OASIS Variables not used. defined only for compilation purpose
60   INTEGER                    ::   OASIS_Out         = -1
61   INTEGER                    ::   OASIS_REAL        = -1
62   INTEGER                    ::   OASIS_Ok          = -1
63   INTEGER                    ::   OASIS_In          = -1
64   INTEGER                    ::   OASIS_Sent        = -1
65   INTEGER                    ::   OASIS_SentOut     = -1
66   INTEGER                    ::   OASIS_ToRest      = -1
67   INTEGER                    ::   OASIS_ToRestOut   = -1
68   INTEGER                    ::   OASIS_Recvd       = -1
69   INTEGER                    ::   OASIS_RecvOut     = -1
70   INTEGER                    ::   OASIS_FromRest    = -1
71   INTEGER                    ::   OASIS_FromRestOut = -1
72#endif
73
74   INTEGER                    ::   nrcv         ! total number of fields received
75   INTEGER                    ::   nsnd         ! total number of fields sent
76   INTEGER                    ::   ncplmodel    ! Maximum number of models to/from which NEMO is potentialy sending/receiving data
77   INTEGER, PUBLIC, PARAMETER ::   nmaxfld=60   ! Maximum number of coupling fields
78   INTEGER, PUBLIC, PARAMETER ::   nmaxcat=5    ! Maximum number of coupling fields
79   INTEGER, PUBLIC, PARAMETER ::   nmaxcpl=5    ! Maximum number of coupling fields
80   LOGICAL, PARAMETER         ::   ltmp_wapatch = .TRUE.   ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define 
81   INTEGER                    ::   nldi_save, nlei_save
82   INTEGER                    ::   nldj_save, nlej_save
83   
84   TYPE, PUBLIC ::   FLD_CPL               !: Type for coupling field information
85      LOGICAL               ::   laction   ! To be coupled or not
86      CHARACTER(len = 8)    ::   clname    ! Name of the coupling field   
87      CHARACTER(len = 1)    ::   clgrid    ! Grid type 
88      REAL(wp)              ::   nsgn      ! Control of the sign change
89      INTEGER, DIMENSION(nmaxcat,nmaxcpl) ::   nid   ! Id of the field (no more than 9 categories and 9 extrena models)
90      INTEGER               ::   nct       ! Number of categories in field
91      INTEGER               ::   ncplmodel ! Maximum number of models to/from which this variable may be sent/received
92      INTEGER               ::   dimensions ! Number of dimensions of coupling field
93   END TYPE FLD_CPL
94
95   TYPE(FLD_CPL), DIMENSION(nmaxfld), PUBLIC ::   srcv, ssnd   !: Coupling fields
96
97   REAL(wp), DIMENSION(:,:), ALLOCATABLE ::   exfld   ! Temporary buffer for receiving
98
99   !!----------------------------------------------------------------------
100   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
101   !! $Id$
102   !! Software governed by the CeCILL license (see ./LICENSE)
103   !!----------------------------------------------------------------------
104CONTAINS
105
106   SUBROUTINE cpl_init( cd_modname, kl_comm )
107      !!-------------------------------------------------------------------
108      !!             ***  ROUTINE cpl_init  ***
109      !!
110      !! ** Purpose :   Initialize coupled mode communication for ocean
111      !!    exchange between AGCM, OGCM and COUPLER. (OASIS3 software)
112      !!
113      !! ** Method  :   OASIS3 MPI communication
114      !!--------------------------------------------------------------------
115      CHARACTER(len = *), INTENT(in   ) ::   cd_modname   ! model name as set in namcouple file
116      INTEGER           , INTENT(  out) ::   kl_comm      ! local communicator of the model
117      !!--------------------------------------------------------------------
118
119      ! WARNING: No write in numout in this routine
120      !============================================
121
122      !------------------------------------------------------------------
123      ! 1st Initialize the OASIS system for the application
124      !------------------------------------------------------------------
125      CALL oasis_init_comp ( ncomp_id, TRIM(cd_modname), nerror )
126      IF ( nerror /= OASIS_Ok ) &
127         CALL oasis_abort (ncomp_id, 'cpl_init', 'Failure in oasis_init_comp')
128
129      !------------------------------------------------------------------
130      ! 3rd Get an MPI communicator for OPA local communication
131      !------------------------------------------------------------------
132
133      CALL oasis_get_localcomm ( kl_comm, nerror )
134      IF ( nerror /= OASIS_Ok ) &
135         CALL oasis_abort (ncomp_id, 'cpl_init','Failure in oasis_get_localcomm' )
136      !
137   END SUBROUTINE cpl_init
138
139
140   SUBROUTINE cpl_define( krcv, ksnd, kcplmodel )
141      !!-------------------------------------------------------------------
142      !!             ***  ROUTINE cpl_define  ***
143      !!
144      !! ** Purpose :   Define grid and field information for ocean
145      !!    exchange between AGCM, OGCM and COUPLER. (OASIS3 software)
146      !!
147      !! ** Method  :   OASIS3 MPI communication
148      !!--------------------------------------------------------------------
149      INTEGER, INTENT(in) ::   krcv, ksnd     ! Number of received and sent coupling fields
150      INTEGER, INTENT(in) ::   kcplmodel      ! Maximum number of models to/from which NEMO is potentialy sending/receiving data
151      !
152      INTEGER :: id_part
153      INTEGER :: id_part_0d     ! Partition for 0d fields
154      INTEGER :: id_part_rnf_1d ! Partition for 1d river outflow fields
155      INTEGER :: id_part_temp   ! Temperary partition used to choose either 0d or 1d partitions
156      INTEGER :: paral(5)       ! OASIS3 box partition
157      INTEGER :: ishape(4)      ! shape of 2D arrays passed to PSMILe
158      INTEGER :: ishape0d1d(2)  ! Shape of 0D or 1D arrays passed to PSMILe.
159      INTEGER :: var_nodims(2)  ! Number of coupling field dimensions.
160                                ! var_nodims(1) is redundant from OASIS3-MCT vn4.0 onwards
161                                ! but retained for backward compatibility.
162                                ! var_nodims(2) is the number of fields in a bundle
163                                ! or 1 for unbundled fields (bundles are not yet catered for
164                                ! in NEMO hence we default to 1). 
165      INTEGER :: ji,jc,jm       ! local loop indicees
166      CHARACTER(LEN=64) :: zclname
167      CHARACTER(LEN=2) :: cli2
168      !!--------------------------------------------------------------------
169
170      ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define
171      IF ( ltmp_wapatch ) THEN
172         nldi_save = nldi   ;   nlei_save = nlei
173         nldj_save = nldj   ;   nlej_save = nlej
174         IF( nimpp           ==      1 ) nldi = 1
175         IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
176         IF( njmpp           ==      1 ) nldj = 1
177         IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
178      ENDIF
179      IF(lwp) WRITE(numout,*)
180      IF(lwp) WRITE(numout,*) 'cpl_define : initialization in coupled ocean/atmosphere case'
181      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~~~~~~'
182      IF(lwp) WRITE(numout,*)
183
184      ncplmodel = kcplmodel
185      IF( kcplmodel > nmaxcpl ) THEN
186         CALL oasis_abort ( ncomp_id, 'cpl_define', 'ncplmodel is larger than nmaxcpl, increase nmaxcpl')   ;   RETURN
187      ENDIF
188
189      nrcv = krcv
190      IF( nrcv > nmaxfld ) THEN
191         CALL oasis_abort ( ncomp_id, 'cpl_define', 'nrcv is larger than nmaxfld, increase nmaxfld')   ;   RETURN
192      ENDIF
193
194      nsnd = ksnd
195      IF( nsnd > nmaxfld ) THEN
196         CALL oasis_abort ( ncomp_id, 'cpl_define', 'nsnd is larger than nmaxfld, increase nmaxfld')   ;   RETURN
197      ENDIF
198      !
199      ! ... Define the shape for the area that excludes the halo
200      !     For serial configuration (key_mpp_mpi not being active)
201      !     nl* is set to the global values 1 and jp*glo.
202      !
203      ishape(1) = 1
204      ishape(2) = nlei-nldi+1
205      ishape(3) = 1
206      ishape(4) = nlej-nldj+1
207
208      ishape0d1d(1) = 0 
209      ishape0d1d(2) = 0 
210      !
211      ! ... Allocate memory for data exchange
212      !
213      ALLOCATE(exfld(nlei-nldi+1, nlej-nldj+1), stat = nerror)
214      IF( nerror > 0 ) THEN
215         CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in allocating exfld')   ;   RETURN
216      ENDIF
217      !
218      ! -----------------------------------------------------------------
219      ! ... Define the partition
220      ! -----------------------------------------------------------------
221     
222      paral(1) = 2                                              ! box partitioning
223      paral(2) = jpiglo * (nldj-1+njmpp-1) + (nldi-1+nimpp-1)   ! NEMO lower left corner global offset   
224      paral(3) = nlei-nldi+1                                    ! local extent in i
225      paral(4) = nlej-nldj+1                                    ! local extent in j
226      paral(5) = jpiglo                                         ! global extent in x
227     
228      IF( ln_ctl ) THEN
229         WRITE(numout,*) ' multiexchg: paral (1:5)', paral
230         WRITE(numout,*) ' multiexchg: jpi, jpj =', jpi, jpj
231         WRITE(numout,*) ' multiexchg: nldi, nlei, nimpp =', nldi, nlei, nimpp
232         WRITE(numout,*) ' multiexchg: nldj, nlej, njmpp =', nldj, nlej, njmpp
233      ENDIF
234   
235      CALL oasis_def_partition ( id_part, paral, nerror, jpiglo*jpjglo )
236
237      ! A special partition is needed for 0D fields
238
239      paral(1) = 0                                       ! serial partitioning
240      paral(2) = 0   
241      IF ( nproc == 0) THEN
242         paral(3) = 1                   ! Size of array to couple (scalar)
243      ELSE
244         paral(3) = 0                   ! Dummy size for PE's not involved
245      END IF
246      paral(4) = 0 
247      paral(5) = 0 
248         
249      CALL oasis_def_partition ( id_part_0d, paral, nerror, 1 ) 
250 
251      ! Another partition is needed for 1D river routing fields
252       
253      paral(1) = 0                                       ! serial partitioning
254      paral(2) = 0   
255      IF ( nproc == 0) THEN
256         paral(3) = nn_cpl_river                   ! Size of array to couple (vector)
257      ELSE
258         paral(3) = 0                   ! Dummy size for PE's not involved
259      END IF
260      paral(4) = 0 
261      paral(5) = 0 
262 
263 
264      CALL oasis_def_partition ( id_part_rnf_1d, paral, nerror, nn_cpl_river ) 
265
266      !
267      ! ... Announce send variables.
268      !
269      ssnd(:)%ncplmodel = kcplmodel
270      !
271      DO ji = 1, ksnd
272         IF ( ssnd(ji)%laction ) THEN
273
274            IF( ssnd(ji)%nct > nmaxcat ) THEN
275               CALL oasis_abort ( ncomp_id, 'cpl_define', 'Number of categories of '//   &
276                  &              TRIM(ssnd(ji)%clname)//' is larger than nmaxcat, increase nmaxcat' )
277               RETURN
278            ENDIF
279           
280            DO jc = 1, ssnd(ji)%nct
281               DO jm = 1, kcplmodel
282
283                  IF ( ssnd(ji)%nct .GT. 1 ) THEN
284                     WRITE(cli2,'(i2.2)') jc
285                     zclname = TRIM(ssnd(ji)%clname)//'_cat'//cli2
286                  ELSE
287                     zclname = ssnd(ji)%clname
288                  ENDIF
289                  IF ( kcplmodel  > 1 ) THEN
290                     WRITE(cli2,'(i2.2)') jm
291                     zclname = 'model'//cli2//'_'//TRIM(zclname)
292                  ENDIF
293#if defined key_agrif
294                  IF( agrif_fixed() /= 0 ) THEN
295                     zclname=TRIM(Agrif_CFixed())//'_'//TRIM(zclname)
296                  END IF
297#endif
298                  IF( ln_ctl ) WRITE(numout,*) "Define", ji, jc, jm, " "//TRIM(zclname), " for ", OASIS_Out
299
300                  !
301                  ! ... Set the field dimension and bundle count
302                  var_nodims(1) = 2
303                  var_nodims(2) = 1 ! Modify this value to cater for bundled fields.   
304
305                  CALL oasis_def_var (ssnd(ji)%nid(jc,jm), zclname, id_part   , var_nodims,   &
306                     &                OASIS_Out          , ishape , OASIS_REAL, nerror )
307                  IF ( nerror /= OASIS_Ok ) THEN
308                     WRITE(numout,*) 'Failed to define transient ', ji, jc, jm, " "//TRIM(zclname)
309                     CALL oasis_abort ( ssnd(ji)%nid(jc,jm), 'cpl_define', 'Failure in oasis_def_var' )
310                  ENDIF
311                  IF( ln_ctl .AND. ssnd(ji)%nid(jc,jm) /= -1 ) WRITE(numout,*) "variable defined in the namcouple"
312                  IF( ln_ctl .AND. ssnd(ji)%nid(jc,jm) == -1 ) WRITE(numout,*) "variable NOT defined in the namcouple"
313               END DO
314            END DO
315         ENDIF
316      END DO
317      !
318      ! ... Announce received variables.
319      !
320      srcv(:)%ncplmodel = kcplmodel
321      !
322      DO ji = 1, krcv
323         IF ( srcv(ji)%laction ) THEN
324           
325            IF( srcv(ji)%nct > nmaxcat ) THEN
326               CALL oasis_abort ( ncomp_id, 'cpl_define', 'Number of categories of '//   &
327                  &              TRIM(srcv(ji)%clname)//' is larger than nmaxcat, increase nmaxcat' )
328               RETURN
329            ENDIF
330           
331            DO jc = 1, srcv(ji)%nct
332               DO jm = 1, kcplmodel
333                 
334                  IF ( srcv(ji)%nct .GT. 1 ) THEN
335                     WRITE(cli2,'(i2.2)') jc
336                     zclname = TRIM(srcv(ji)%clname)//'_cat'//cli2
337                  ELSE
338                     zclname = srcv(ji)%clname
339                  ENDIF
340                  IF ( kcplmodel  > 1 ) THEN
341                     WRITE(cli2,'(i2.2)') jm
342                     zclname = 'model'//cli2//'_'//TRIM(zclname)
343                  ENDIF
344#if defined key_agrif
345                  IF( agrif_fixed() /= 0 ) THEN
346                     zclname=TRIM(Agrif_CFixed())//'_'//TRIM(zclname)
347                  END IF
348#endif
349                  IF( ln_ctl ) WRITE(numout,*) "Define", ji, jc, jm, " "//TRIM(zclname), " for ", OASIS_In
350                  flush(numout)
351
352                  ! Define 0D (Greenland or Antarctic ice mass) or 1D (river outflow) coupling fields
353                  IF (srcv(ji)%dimensions <= 1) THEN
354                    var_nodims(1) = 1
355                    var_nodims(2) = 1 ! Modify this value to cater for bundled fields.
356                    IF (nproc == 0) THEN
357
358                       IF (srcv(ji)%dimensions == 0) THEN
359
360                          ! If 0D then set temporary variables to 0D components
361                          id_part_temp = id_part_0d
362                          ishape0d1d(2) = 1
363                       ELSE
364
365                          ! If 1D then set temporary variables to river outflow components
366                          id_part_temp = id_part_rnf_1d
367                          ishape0d1d(2)= nn_cpl_river
368
369                       END IF
370
371                       CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part_temp   , var_nodims,   &
372                                   OASIS_In           , ishape0d1d(1:2) , OASIS_REAL, nerror )
373                    ELSE
374                       ! Dummy call to keep OASIS3-MCT happy.
375                       CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part_0d   , var_nodims,   &
376                                   OASIS_In           , ishape0d1d(1:2) , OASIS_REAL, nerror )
377                    END IF
378                  ELSE
379                    ! It's a "normal" 2D (or pseudo 3D) coupling field.
380                    ! ... Set the field dimension and bundle count
381                    var_nodims(1) = 2
382                    var_nodims(2) = 1 ! Modify this value to cater for bundled fields.     
383
384                    CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part   , var_nodims,   &
385                                         OASIS_In           , ishape , OASIS_REAL, nerror )
386                  ENDIF
387
388                  IF ( nerror /= OASIS_Ok ) THEN
389                     WRITE(numout,*) 'Failed to define transient ', ji, jc, jm, " "//TRIM(zclname)
390                     CALL oasis_abort ( srcv(ji)%nid(jc,jm), 'cpl_define', 'Failure in oasis_def_var' )
391                  ENDIF
392                  IF( ln_ctl .AND. srcv(ji)%nid(jc,jm) /= -1 ) WRITE(numout,*) "variable defined in the namcouple"
393                  IF( ln_ctl .AND. srcv(ji)%nid(jc,jm) == -1 ) WRITE(numout,*) "variable NOT defined in the namcouple"
394
395               END DO
396            END DO
397         ENDIF
398      END DO
399     
400      !------------------------------------------------------------------
401      ! End of definition phase
402      !------------------------------------------------------------------
403     
404      CALL oasis_enddef(nerror)
405      IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
406      !
407      IF ( ltmp_wapatch ) THEN
408         nldi = nldi_save   ;   nlei = nlei_save
409         nldj = nldj_save   ;   nlej = nlej_save
410      ENDIF
411   END SUBROUTINE cpl_define
412   
413   
414   SUBROUTINE cpl_snd( kid, kstep, pdata, kinfo )
415      !!---------------------------------------------------------------------
416      !!              ***  ROUTINE cpl_snd  ***
417      !!
418      !! ** Purpose : - At each coupling time-step,this routine sends fields
419      !!      like sst or ice cover to the coupler or remote application.
420      !!----------------------------------------------------------------------
421      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
422      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
423      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
424      REAL(wp), DIMENSION(:,:,:), INTENT(in   ) ::   pdata
425      !!
426      INTEGER                                   ::   jc,jm     ! local loop index
427      !!--------------------------------------------------------------------
428      ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define
429      IF ( ltmp_wapatch ) THEN
430         nldi_save = nldi   ;   nlei_save = nlei
431         nldj_save = nldj   ;   nlej_save = nlej
432         IF( nimpp           ==      1 ) nldi = 1
433         IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
434         IF( njmpp           ==      1 ) nldj = 1
435         IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
436      ENDIF
437      !
438      ! snd data to OASIS3
439      !
440      DO jc = 1, ssnd(kid)%nct
441         DO jm = 1, ssnd(kid)%ncplmodel
442       
443            IF( ssnd(kid)%nid(jc,jm) /= -1 ) THEN
444               CALL oasis_put ( ssnd(kid)%nid(jc,jm), kstep, pdata(nldi:nlei, nldj:nlej,jc), kinfo )
445               
446               IF ( ln_ctl ) THEN       
447                  IF ( kinfo == OASIS_Sent     .OR. kinfo == OASIS_ToRest .OR.   &
448                     & kinfo == OASIS_SentOut  .OR. kinfo == OASIS_ToRestOut ) THEN
449                     WRITE(numout,*) '****************'
450                     WRITE(numout,*) 'oasis_put: Outgoing ', ssnd(kid)%clname
451                     WRITE(numout,*) 'oasis_put: ivarid ', ssnd(kid)%nid(jc,jm)
452                     WRITE(numout,*) 'oasis_put:  kstep ', kstep
453                     WRITE(numout,*) 'oasis_put:   info ', kinfo
454                     WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
455                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
456                     WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
457                     WRITE(numout,*) '****************'
458                  ENDIF
459               ENDIF
460               
461            ENDIF
462           
463         ENDDO
464      ENDDO
465      IF ( ltmp_wapatch ) THEN
466         nldi = nldi_save   ;   nlei = nlei_save
467         nldj = nldj_save   ;   nlej = nlej_save
468      ENDIF
469      !
470    END SUBROUTINE cpl_snd
471
472
473   SUBROUTINE cpl_rcv( kid, kstep, pdata, pmask, kinfo )
474      !!---------------------------------------------------------------------
475      !!              ***  ROUTINE cpl_rcv  ***
476      !!
477      !! ** Purpose : - At each coupling time-step,this routine receives fields
478      !!      like stresses and fluxes from the coupler or remote application.
479      !!----------------------------------------------------------------------
480      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
481      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
482      REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::   pdata     ! IN to keep the value if nothing is done
483      REAL(wp), DIMENSION(:,:,:), INTENT(in   ) ::   pmask     ! coupling mask
484      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
485      !!
486      INTEGER                                   ::   jc,jm     ! local loop index
487      LOGICAL                                   ::   llaction, llfisrt
488      !!--------------------------------------------------------------------
489      ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define
490      IF ( ltmp_wapatch ) THEN
491         nldi_save = nldi   ;   nlei_save = nlei
492         nldj_save = nldj   ;   nlej_save = nlej
493      ENDIF
494      !
495      ! receive local data from OASIS3 on every process
496      !
497      kinfo = OASIS_idle
498      !
499      DO jc = 1, srcv(kid)%nct
500         IF ( ltmp_wapatch ) THEN
501            IF( nimpp           ==      1 ) nldi = 1
502            IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
503            IF( njmpp           ==      1 ) nldj = 1
504            IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
505         ENDIF
506         llfisrt = .TRUE.
507
508         DO jm = 1, srcv(kid)%ncplmodel
509
510            IF( srcv(kid)%nid(jc,jm) /= -1 ) THEN
511
512               CALL oasis_get ( srcv(kid)%nid(jc,jm), kstep, exfld, kinfo )         
513               
514               llaction =  kinfo == OASIS_Recvd   .OR. kinfo == OASIS_FromRest .OR.   &
515                  &        kinfo == OASIS_RecvOut .OR. kinfo == OASIS_FromRestOut
516               
517               IF ( ln_ctl )   WRITE(numout,*) "llaction, kinfo, kstep, ivarid: " , llaction, kinfo, kstep, srcv(kid)%nid(jc,jm)
518               
519               IF ( llaction ) THEN
520                 
521                  kinfo = OASIS_Rcv
522                  IF( llfisrt ) THEN
523                     pdata(nldi:nlei,nldj:nlej,jc) =                                 exfld(:,:) * pmask(nldi:nlei,nldj:nlej,jm)
524                     llfisrt = .FALSE.
525                  ELSE
526                     pdata(nldi:nlei,nldj:nlej,jc) = pdata(nldi:nlei,nldj:nlej,jc) + exfld(:,:) * pmask(nldi:nlei,nldj:nlej,jm)
527                  ENDIF
528                 
529                  IF ( ln_ctl ) THEN       
530                     WRITE(numout,*) '****************'
531                     WRITE(numout,*) 'oasis_get: Incoming ', srcv(kid)%clname
532                     WRITE(numout,*) 'oasis_get: ivarid '  , srcv(kid)%nid(jc,jm)
533                     WRITE(numout,*) 'oasis_get:   kstep', kstep
534                     WRITE(numout,*) 'oasis_get:   info ', kinfo
535                     WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
536                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
537                     WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
538                     WRITE(numout,*) '****************'
539                  ENDIF
540                 
541               ENDIF
542               
543            ENDIF
544           
545         ENDDO
546
547         IF ( ltmp_wapatch ) THEN
548            nldi = nldi_save   ;   nlei = nlei_save
549            nldj = nldj_save   ;   nlej = nlej_save
550         ENDIF
551         !--- Fill the overlap areas and extra hallows (mpp)
552         !--- check periodicity conditions (all cases)
553         IF( .not. llfisrt ) THEN
554            CALL lbc_lnk( 'cpl_oasis3', pdata(:,:,jc), srcv(kid)%clgrid, srcv(kid)%nsgn )   
555         ENDIF
556 
557      ENDDO
558      !
559   END SUBROUTINE cpl_rcv
560
561
562   SUBROUTINE cpl_rcv_1d( kid, kstep, pdata, nitems, kinfo )
563      !!---------------------------------------------------------------------
564      !!              ***  ROUTINE cpl_rcv_1d  ***
565      !!
566      !! ** Purpose : - A special version of cpl_rcv to deal exclusively with
567      !! receipt of 0D or 1D fields.
568      !! The fields are recieved into a 1D array buffer which is simply a
569      !! dynamically sized sized array (which may be of size 1)
570      !! of 0 dimensional fields. This allows us to pass miltiple 0D
571      !! fields via a single put/get operation. 
572      !!----------------------------------------------------------------------
573      INTEGER , INTENT(in   ) ::   nitems      ! Number of 0D items to recieve
574                                               ! during this get operation. i.e.
575                                               ! The size of the 1D array in which
576                                               ! 0D items are passed.   
577      INTEGER , INTENT(in   ) ::   kid         ! ID index of the incoming
578                                               ! data. 
579      INTEGER , INTENT(in   ) ::   kstep       ! ocean time-step in seconds
580      REAL(wp), INTENT(inout) ::   pdata(1:nitems) ! The original value(s), 
581                                                   ! unchanged if nothing is
582                                                   ! received
583      INTEGER , INTENT(  out) ::   kinfo       ! OASIS3 info argument
584      !!
585      REAL(wp) ::   recvfld(1:nitems)          ! Local receive field buffer
586      INTEGER  ::   jc,jm     ! local loop index
587      INTEGER  ::   ierr
588      LOGICAL  ::   llaction
589      INTEGER  ::   MPI_WORKING_PRECISION
590      INTEGER  ::   number_to_print 
591      !!--------------------------------------------------------------------
592      !
593      ! receive local data from OASIS3 on every process
594      !
595      kinfo = OASIS_idle
596      !
597      ! 0D and 1D fields won't have categories or any other form of "pseudo level"
598      ! so we only cater for a single set of values and thus don't bother
599      ! with a loop over the jc index
600      jc = 1
601
602      DO jm = 1, srcv(kid)%ncplmodel
603
604         IF( srcv(kid)%nid(jc,jm) /= -1 ) THEN
605
606            IF ( ( srcv(kid)%dimensions <= 1) .AND. (nproc == 0) ) THEN
607               ! Since there is no concept of data decomposition for zero
608               ! dimension fields, they must only be exchanged through the master PE,
609               ! unlike "normal" 2D field cases where every PE is involved.
610
611               CALL oasis_get ( srcv(kid)%nid(jc,jm), kstep, recvfld, kinfo )   
612               
613               llaction =  kinfo == OASIS_Recvd   .OR. kinfo == OASIS_FromRest .OR.   &
614                           kinfo == OASIS_RecvOut .OR. kinfo == OASIS_FromRestOut
615               
616               IF ( ln_ctl ) WRITE(numout,*) "llaction, kinfo, kstep, ivarid: " , &
617                                     llaction, kinfo, kstep, srcv(kid)%nid(jc,jm)
618               
619               IF ( llaction ) THEN
620                 
621                  kinfo = OASIS_Rcv
622                  pdata(1:nitems) = recvfld(1:nitems) 
623                 
624                  IF ( ln_ctl ) THEN       
625                     number_to_print = 10
626                     IF ( nitems < number_to_print ) number_to_print = nitems
627                     WRITE(numout,*) '****************'
628                     WRITE(numout,*) 'oasis_get: Incoming ', srcv(kid)%clname
629                     WRITE(numout,*) 'oasis_get: ivarid '  , srcv(kid)%nid(jc,jm)
630                     WRITE(numout,*) 'oasis_get:   kstep', kstep
631                     WRITE(numout,*) 'oasis_get:   info ', kinfo
632                     WRITE(numout,*) '     - Minimum Value is ', MINVAL(pdata(:))
633                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:))
634                     WRITE(numout,*) '     - Start of data is ', pdata(1:number_to_print)
635                     WRITE(numout,*) '****************'
636                  ENDIF
637                 
638               ENDIF
639            ENDIF   
640          ENDIF
641           
642       ENDDO
643       
644       ! Set the precision that we want to broadcast using MPI_BCAST
645       SELECT CASE( wp )
646       CASE( sp ) 
647         MPI_WORKING_PRECISION = MPI_REAL                ! Single precision
648       CASE( dp )
649         MPI_WORKING_PRECISION = MPI_DOUBLE_PRECISION    ! Double precision
650       CASE default
651         CALL oasis_abort( ncomp_id, "cpl_rcv_1d", "Could not find precision for coupling 0d or 1d field" )
652       END SELECT
653
654       ! We have to broadcast (potentially) received values from PE 0 to all
655       ! the others. If no new data has been received we're just
656       ! broadcasting the existing values but there's no more efficient way
657       ! to deal with that w/o NEMO adopting a UM-style test mechanism
658       ! to determine active put/get timesteps.
659       CALL mpi_bcast( pdata, nitems, MPI_WORKING_PRECISION, localRoot, mpi_comm_opa, ierr )
660
661      !
662   END SUBROUTINE cpl_rcv_1d
663
664
665   INTEGER FUNCTION cpl_freq( cdfieldname ) 
666      !!---------------------------------------------------------------------
667      !!              ***  ROUTINE cpl_freq  ***
668      !!
669      !! ** Purpose : - send back the coupling frequency for a particular field
670      !!----------------------------------------------------------------------
671      CHARACTER(len = *), INTENT(in) ::   cdfieldname    ! field name as set in namcouple file
672      !!
673      INTEGER               :: id
674      INTEGER               :: info
675      INTEGER, DIMENSION(1) :: itmp
676      INTEGER               :: ji,jm     ! local loop index
677      INTEGER               :: mop
678      !!----------------------------------------------------------------------
679      cpl_freq = 0   ! defaut definition
680      id = -1        ! defaut definition
681      !
682      DO ji = 1, nsnd
683         IF (ssnd(ji)%laction ) THEN
684            DO jm = 1, ncplmodel
685               IF( ssnd(ji)%nid(1,jm) /= -1 ) THEN
686                  IF( TRIM(cdfieldname) == TRIM(ssnd(ji)%clname) ) THEN
687                     id = ssnd(ji)%nid(1,jm)
688                     mop = OASIS_Out
689                  ENDIF
690               ENDIF
691            ENDDO
692         ENDIF
693      ENDDO
694      DO ji = 1, nrcv
695         IF (srcv(ji)%laction ) THEN
696            DO jm = 1, ncplmodel
697               IF( srcv(ji)%nid(1,jm) /= -1 ) THEN
698                  IF( TRIM(cdfieldname) == TRIM(srcv(ji)%clname) ) THEN
699                     id = srcv(ji)%nid(1,jm)
700                     mop = OASIS_In
701                  ENDIF
702               ENDIF
703            ENDDO
704         ENDIF
705      ENDDO
706      !
707      IF( id /= -1 ) THEN
708#if ! defined key_oa3mct_v1v2
709         CALL oasis_get_freqs(id, mop, 1, itmp, info)
710#else
711         CALL oasis_get_freqs(id,      1, itmp, info)
712#endif
713         cpl_freq = itmp(1)
714      ENDIF
715      !
716   END FUNCTION cpl_freq
717
718
719   SUBROUTINE cpl_finalize
720      !!---------------------------------------------------------------------
721      !!              ***  ROUTINE cpl_finalize  ***
722      !!
723      !! ** Purpose : - Finalizes the coupling. If MPI_init has not been
724      !!      called explicitly before cpl_init it will also close
725      !!      MPI communication.
726      !!----------------------------------------------------------------------
727      !
728      DEALLOCATE( exfld )
729      IF (nstop == 0) THEN
730         CALL oasis_terminate( nerror )         
731      ELSE
732         CALL oasis_abort( ncomp_id, "cpl_finalize", "NEMO ABORT STOP" )
733      ENDIF       
734      !
735   END SUBROUTINE cpl_finalize
736
737#if ! defined key_oasis3
738
739   !!----------------------------------------------------------------------
740   !!   No OASIS Library          OASIS3 Dummy module...
741   !!----------------------------------------------------------------------
742
743   SUBROUTINE oasis_init_comp(k1,cd1,k2)
744      CHARACTER(*), INTENT(in   ) ::  cd1
745      INTEGER     , INTENT(  out) ::  k1,k2
746      k1 = -1 ; k2 = -1
747      WRITE(numout,*) 'oasis_init_comp: Error you sould not be there...', cd1
748   END SUBROUTINE oasis_init_comp
749
750   SUBROUTINE oasis_abort(k1,cd1,cd2)
751      INTEGER     , INTENT(in   ) ::  k1
752      CHARACTER(*), INTENT(in   ) ::  cd1,cd2
753      WRITE(numout,*) 'oasis_abort: Error you sould not be there...', cd1, cd2
754   END SUBROUTINE oasis_abort
755
756   SUBROUTINE oasis_get_localcomm(k1,k2)
757      INTEGER     , INTENT(  out) ::  k1,k2
758      k1 = -1 ; k2 = -1
759      WRITE(numout,*) 'oasis_get_localcomm: Error you sould not be there...'
760   END SUBROUTINE oasis_get_localcomm
761
762   SUBROUTINE oasis_def_partition(k1,k2,k3,k4)
763      INTEGER     , INTENT(  out) ::  k1,k3
764      INTEGER     , INTENT(in   ) ::  k2(5)
765      INTEGER     , INTENT(in   ) ::  k4
766      k1 = k2(1) ; k3 = k2(5)+k4
767      WRITE(numout,*) 'oasis_def_partition: Error you sould not be there...'
768   END SUBROUTINE oasis_def_partition
769
770   SUBROUTINE oasis_def_var(k1,cd1,k2,k3,k4,k5,k6,k7)
771      CHARACTER(*), INTENT(in   ) ::  cd1
772      INTEGER     , INTENT(in   ) ::  k2,k3(2),k4,k5(*),k6
773      INTEGER     , INTENT(  out) ::  k1,k7
774      k1 = -1 ; k7 = -1
775      WRITE(numout,*) 'oasis_def_var: Error you sould not be there...', cd1
776   END SUBROUTINE oasis_def_var
777
778   SUBROUTINE oasis_enddef(k1)
779      INTEGER     , INTENT(  out) ::  k1
780      k1 = -1
781      WRITE(numout,*) 'oasis_enddef: Error you sould not be there...'
782   END SUBROUTINE oasis_enddef
783 
784   SUBROUTINE oasis_put(k1,k2,p1,k3)
785      REAL(wp), DIMENSION(:,:), INTENT(in   ) ::  p1
786      INTEGER                 , INTENT(in   ) ::  k1,k2
787      INTEGER                 , INTENT(  out) ::  k3
788      k3 = -1
789      WRITE(numout,*) 'oasis_put: Error you sould not be there...'
790   END SUBROUTINE oasis_put
791
792   SUBROUTINE oasis_get_1d(k1,k2,p1,k3)
793      REAL(wp), DIMENSION(:)  , INTENT(  out) ::  p1
794      INTEGER                 , INTENT(in   ) ::  k1,k2
795      INTEGER                 , INTENT(  out) ::  k3
796      p1(1) = -1. ; k3 = -1
797      WRITE(numout,*) 'oasis_get_1d: Error you sould not be there...'
798   END SUBROUTINE oasis_get_1d
799
800   SUBROUTINE oasis_get_2d(k1,k2,p1,k3)
801      REAL(wp), DIMENSION(:,:), INTENT(  out) ::  p1
802      INTEGER                 , INTENT(in   ) ::  k1,k2
803      INTEGER                 , INTENT(  out) ::  k3
804      p1(1,1) = -1. ; k3 = -1
805      WRITE(numout,*) 'oasis_get_2d: Error you sould not be there...'
806   END SUBROUTINE oasis_get_2d
807
808   SUBROUTINE oasis_get_freqs(k1,k5,k2,k3,k4)
809      INTEGER              , INTENT(in   ) ::  k1,k2
810      INTEGER, DIMENSION(1), INTENT(  out) ::  k3
811      INTEGER              , INTENT(  out) ::  k4,k5
812      k3(1) = k1 ; k4 = k2 ; k5 = k2
813      WRITE(numout,*) 'oasis_get_freqs: Error you sould not be there...'
814   END SUBROUTINE oasis_get_freqs
815
816   SUBROUTINE oasis_terminate(k1)
817      INTEGER     , INTENT(  out) ::  k1
818      k1 = -1
819      WRITE(numout,*) 'oasis_terminate: Error you sould not be there...'
820   END SUBROUTINE oasis_terminate
821   
822#endif
823
824   !!=====================================================================
825END MODULE cpl_oasis3
Note: See TracBrowser for help on using the repository browser.