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.1_icesheet_and_river_coupling/src/OCE/SBC – NEMO

source: NEMO/branches/UKMO/NEMO_4.0.1_icesheet_and_river_coupling/src/OCE/SBC/cpl_oasis3.F90 @ 12580

Last change on this file since 12580 was 12580, checked in by dancopsey, 4 years ago

Add 1D river coupling code from changeset 10269 of GO6 package branch
branches/UKMO/dev_r5518_GO6_package

File size: 36.3 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                  CALL oasis_def_var (ssnd(ji)%nid(jc,jm), zclname, id_part   , (/ 2, 1 /),   &
300                     &                OASIS_Out          , ishape , OASIS_REAL, nerror )
301                  IF ( nerror /= OASIS_Ok ) THEN
302                     WRITE(numout,*) 'Failed to define transient ', ji, jc, jm, " "//TRIM(zclname)
303                     CALL oasis_abort ( ssnd(ji)%nid(jc,jm), 'cpl_define', 'Failure in oasis_def_var' )
304                  ENDIF
305                  IF( ln_ctl .AND. ssnd(ji)%nid(jc,jm) /= -1 ) WRITE(numout,*) "variable defined in the namcouple"
306                  IF( ln_ctl .AND. ssnd(ji)%nid(jc,jm) == -1 ) WRITE(numout,*) "variable NOT defined in the namcouple"
307               END DO
308            END DO
309         ENDIF
310      END DO
311      !
312      ! ... Announce received variables.
313      !
314      srcv(:)%ncplmodel = kcplmodel
315      !
316      DO ji = 1, krcv
317         IF ( srcv(ji)%laction ) THEN
318           
319            IF( srcv(ji)%nct > nmaxcat ) THEN
320               CALL oasis_abort ( ncomp_id, 'cpl_define', 'Number of categories of '//   &
321                  &              TRIM(srcv(ji)%clname)//' is larger than nmaxcat, increase nmaxcat' )
322               RETURN
323            ENDIF
324           
325            DO jc = 1, srcv(ji)%nct
326               DO jm = 1, kcplmodel
327                 
328                  IF ( srcv(ji)%nct .GT. 1 ) THEN
329                     WRITE(cli2,'(i2.2)') jc
330                     zclname = TRIM(srcv(ji)%clname)//'_cat'//cli2
331                  ELSE
332                     zclname = srcv(ji)%clname
333                  ENDIF
334                  IF ( kcplmodel  > 1 ) THEN
335                     WRITE(cli2,'(i2.2)') jm
336                     zclname = 'model'//cli2//'_'//TRIM(zclname)
337                  ENDIF
338#if defined key_agrif
339                  IF( agrif_fixed() /= 0 ) THEN
340                     zclname=TRIM(Agrif_CFixed())//'_'//TRIM(zclname)
341                  END IF
342#endif
343                  IF( ln_ctl ) WRITE(numout,*) "Define", ji, jc, jm, " "//TRIM(zclname), " for ", OASIS_In
344                  flush(numout)
345
346                  ! Define 0D (Greenland or Antarctic ice mass) or 1D (river outflow) coupling fields
347                  IF (srcv(ji)%dimensions <= 1) THEN
348                    var_nodims(1) = 1
349                    var_nodims(2) = 1 ! Modify this value to cater for bundled fields.
350                    IF (nproc == 0) THEN
351
352                       IF (srcv(ji)%dimensions == 0) THEN
353
354                          ! If 0D then set temporary variables to 0D components
355                          id_part_temp = id_part_0d
356                          ishape0d1d(2) = 1
357                       ELSE
358
359                          ! If 1D then set temporary variables to river outflow components
360                          id_part_temp = id_part_rnf_1d
361                          ishape0d1d(2)= nn_cpl_river
362
363                       END IF
364
365                       CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part_temp   , var_nodims,   &
366                                   OASIS_In           , ishape0d1d(1:2) , OASIS_REAL, nerror )
367                    ELSE
368                       ! Dummy call to keep OASIS3-MCT happy.
369                       CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part_0d   , var_nodims,   &
370                                   OASIS_In           , ishape0d1d(1:2) , OASIS_REAL, nerror )
371                    END IF
372                  ELSE
373                    ! It's a "normal" 2D (or pseudo 3D) coupling field.
374                    ! ... Set the field dimension and bundle count
375                    var_nodims(1) = 2
376                    var_nodims(2) = 1 ! Modify this value to cater for bundled fields.
377
378                    CALL oasis_def_var (srcv(ji)%nid(jc,jm), zclname, id_part   , var_nodims,   &
379                       &                OASIS_In           , ishape , OASIS_REAL, nerror )
380                  ENDIF
381                  IF ( nerror /= OASIS_Ok ) THEN
382                     WRITE(numout,*) 'Failed to define transient ', ji, jc, jm, " "//TRIM(zclname)
383                     CALL oasis_abort ( srcv(ji)%nid(jc,jm), 'cpl_define', 'Failure in oasis_def_var' )
384                  ENDIF
385                  IF( ln_ctl .AND. srcv(ji)%nid(jc,jm) /= -1 ) WRITE(numout,*) "variable defined in the namcouple"
386                  IF( ln_ctl .AND. srcv(ji)%nid(jc,jm) == -1 ) WRITE(numout,*) "variable NOT defined in the namcouple"
387
388               END DO
389            END DO
390         ENDIF
391      END DO
392     
393      !------------------------------------------------------------------
394      ! End of definition phase
395      !------------------------------------------------------------------
396     
397      CALL oasis_enddef(nerror)
398      IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
399      !
400      IF ( ltmp_wapatch ) THEN
401         nldi = nldi_save   ;   nlei = nlei_save
402         nldj = nldj_save   ;   nlej = nlej_save
403      ENDIF
404   END SUBROUTINE cpl_define
405   
406   
407   SUBROUTINE cpl_snd( kid, kstep, pdata, kinfo )
408      !!---------------------------------------------------------------------
409      !!              ***  ROUTINE cpl_snd  ***
410      !!
411      !! ** Purpose : - At each coupling time-step,this routine sends fields
412      !!      like sst or ice cover to the coupler or remote application.
413      !!----------------------------------------------------------------------
414      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
415      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
416      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
417      REAL(wp), DIMENSION(:,:,:), INTENT(in   ) ::   pdata
418      !!
419      INTEGER                                   ::   jc,jm     ! local loop index
420      !!--------------------------------------------------------------------
421      ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define
422      IF ( ltmp_wapatch ) THEN
423         nldi_save = nldi   ;   nlei_save = nlei
424         nldj_save = nldj   ;   nlej_save = nlej
425         IF( nimpp           ==      1 ) nldi = 1
426         IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
427         IF( njmpp           ==      1 ) nldj = 1
428         IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
429      ENDIF
430      !
431      ! snd data to OASIS3
432      !
433      DO jc = 1, ssnd(kid)%nct
434         DO jm = 1, ssnd(kid)%ncplmodel
435       
436            IF( ssnd(kid)%nid(jc,jm) /= -1 ) THEN
437               CALL oasis_put ( ssnd(kid)%nid(jc,jm), kstep, pdata(nldi:nlei, nldj:nlej,jc), kinfo )
438               
439               IF ( ln_ctl ) THEN       
440                  IF ( kinfo == OASIS_Sent     .OR. kinfo == OASIS_ToRest .OR.   &
441                     & kinfo == OASIS_SentOut  .OR. kinfo == OASIS_ToRestOut ) THEN
442                     WRITE(numout,*) '****************'
443                     WRITE(numout,*) 'oasis_put: Outgoing ', ssnd(kid)%clname
444                     WRITE(numout,*) 'oasis_put: ivarid ', ssnd(kid)%nid(jc,jm)
445                     WRITE(numout,*) 'oasis_put:  kstep ', kstep
446                     WRITE(numout,*) 'oasis_put:   info ', kinfo
447                     WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
448                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
449                     WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
450                     WRITE(numout,*) '****************'
451                  ENDIF
452               ENDIF
453               
454            ENDIF
455           
456         ENDDO
457      ENDDO
458      IF ( ltmp_wapatch ) THEN
459         nldi = nldi_save   ;   nlei = nlei_save
460         nldj = nldj_save   ;   nlej = nlej_save
461      ENDIF
462      !
463    END SUBROUTINE cpl_snd
464
465
466   SUBROUTINE cpl_rcv( kid, kstep, pdata, pmask, kinfo )
467      !!---------------------------------------------------------------------
468      !!              ***  ROUTINE cpl_rcv  ***
469      !!
470      !! ** Purpose : - At each coupling time-step,this routine receives fields
471      !!      like stresses and fluxes from the coupler or remote application.
472      !!----------------------------------------------------------------------
473      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
474      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
475      REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::   pdata     ! IN to keep the value if nothing is done
476      REAL(wp), DIMENSION(:,:,:), INTENT(in   ) ::   pmask     ! coupling mask
477      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
478      !!
479      INTEGER                                   ::   jc,jm     ! local loop index
480      LOGICAL                                   ::   llaction, llfisrt
481      !!--------------------------------------------------------------------
482      ! patch to restore wraparound rows in cpl_send, cpl_rcv, cpl_define
483      IF ( ltmp_wapatch ) THEN
484         nldi_save = nldi   ;   nlei_save = nlei
485         nldj_save = nldj   ;   nlej_save = nlej
486      ENDIF
487      !
488      ! receive local data from OASIS3 on every process
489      !
490      kinfo = OASIS_idle
491      !
492      DO jc = 1, srcv(kid)%nct
493         IF ( ltmp_wapatch ) THEN
494            IF( nimpp           ==      1 ) nldi = 1
495            IF( nimpp + jpi - 1 == jpiglo ) nlei = jpi
496            IF( njmpp           ==      1 ) nldj = 1
497            IF( njmpp + jpj - 1 == jpjglo ) nlej = jpj
498         ENDIF
499         llfisrt = .TRUE.
500
501         DO jm = 1, srcv(kid)%ncplmodel
502
503            IF( srcv(kid)%nid(jc,jm) /= -1 ) THEN
504
505               CALL oasis_get ( srcv(kid)%nid(jc,jm), kstep, exfld, kinfo )         
506               
507               llaction =  kinfo == OASIS_Recvd   .OR. kinfo == OASIS_FromRest .OR.   &
508                  &        kinfo == OASIS_RecvOut .OR. kinfo == OASIS_FromRestOut
509               
510               IF ( ln_ctl )   WRITE(numout,*) "llaction, kinfo, kstep, ivarid: " , llaction, kinfo, kstep, srcv(kid)%nid(jc,jm)
511               
512               IF ( llaction ) THEN
513                 
514                  kinfo = OASIS_Rcv
515                  IF( llfisrt ) THEN
516                     pdata(nldi:nlei,nldj:nlej,jc) =                                 exfld(:,:) * pmask(nldi:nlei,nldj:nlej,jm)
517                     llfisrt = .FALSE.
518                  ELSE
519                     pdata(nldi:nlei,nldj:nlej,jc) = pdata(nldi:nlei,nldj:nlej,jc) + exfld(:,:) * pmask(nldi:nlei,nldj:nlej,jm)
520                  ENDIF
521                 
522                  IF ( ln_ctl ) THEN       
523                     WRITE(numout,*) '****************'
524                     WRITE(numout,*) 'oasis_get: Incoming ', srcv(kid)%clname
525                     WRITE(numout,*) 'oasis_get: ivarid '  , srcv(kid)%nid(jc,jm)
526                     WRITE(numout,*) 'oasis_get:   kstep', kstep
527                     WRITE(numout,*) 'oasis_get:   info ', kinfo
528                     WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
529                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
530                     WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
531                     WRITE(numout,*) '****************'
532                  ENDIF
533                 
534               ENDIF
535               
536            ENDIF
537           
538         ENDDO
539
540         IF ( ltmp_wapatch ) THEN
541            nldi = nldi_save   ;   nlei = nlei_save
542            nldj = nldj_save   ;   nlej = nlej_save
543         ENDIF
544         !--- Fill the overlap areas and extra hallows (mpp)
545         !--- check periodicity conditions (all cases)
546         IF( .not. llfisrt ) THEN
547            CALL lbc_lnk( 'cpl_oasis3', pdata(:,:,jc), srcv(kid)%clgrid, srcv(kid)%nsgn )   
548         ENDIF
549 
550      ENDDO
551      !
552   END SUBROUTINE cpl_rcv
553
554
555   SUBROUTINE cpl_rcv_1d( kid, kstep, pdata, nitems, kinfo )
556      !!---------------------------------------------------------------------
557      !!              ***  ROUTINE cpl_rcv_1d  ***
558      !!
559      !! ** Purpose : - A special version of cpl_rcv to deal exclusively with
560      !! receipt of 0D or 1D fields.
561      !! The fields are recieved into a 1D array buffer which is simply a
562      !! dynamically sized sized array (which may be of size 1)
563      !! of 0 dimensional fields. This allows us to pass miltiple 0D
564      !! fields via a single put/get operation. 
565      !!----------------------------------------------------------------------
566      INTEGER , INTENT(in   ) ::   nitems      ! Number of 0D items to recieve
567                                               ! during this get operation. i.e.
568                                               ! The size of the 1D array in which
569                                               ! 0D items are passed.   
570      INTEGER , INTENT(in   ) ::   kid         ! ID index of the incoming
571                                               ! data. 
572      INTEGER , INTENT(in   ) ::   kstep       ! ocean time-step in seconds
573      REAL(wp), INTENT(inout) ::   pdata(1:nitems) ! The original value(s), 
574                                                   ! unchanged if nothing is
575                                                   ! received
576      INTEGER , INTENT(  out) ::   kinfo       ! OASIS3 info argument
577      !!
578      REAL(wp) ::   recvfld(1:nitems)          ! Local receive field buffer
579      INTEGER  ::   jc,jm     ! local loop index
580      INTEGER  ::   ierr
581      LOGICAL  ::   llaction
582      INTEGER  ::   MPI_WORKING_PRECISION
583      INTEGER  ::   number_to_print 
584      !!--------------------------------------------------------------------
585      !
586      ! receive local data from OASIS3 on every process
587      !
588      kinfo = OASIS_idle
589      !
590      ! 0D and 1D fields won't have categories or any other form of "pseudo level"
591      ! so we only cater for a single set of values and thus don't bother
592      ! with a loop over the jc index
593      jc = 1
594
595      DO jm = 1, srcv(kid)%ncplmodel
596
597         IF( srcv(kid)%nid(jc,jm) /= -1 ) THEN
598
599            IF ( ( srcv(kid)%dimensions <= 1) .AND. (nproc == 0) ) THEN
600               ! Since there is no concept of data decomposition for zero
601               ! dimension fields, they must only be exchanged through the master PE,
602               ! unlike "normal" 2D field cases where every PE is involved.
603
604               CALL oasis_get ( srcv(kid)%nid(jc,jm), kstep, recvfld, kinfo )   
605               
606               llaction =  kinfo == OASIS_Recvd   .OR. kinfo == OASIS_FromRest .OR.   &
607                           kinfo == OASIS_RecvOut .OR. kinfo == OASIS_FromRestOut
608               
609               IF ( ln_ctl ) WRITE(numout,*) "llaction, kinfo, kstep, ivarid: " , &
610                                     llaction, kinfo, kstep, srcv(kid)%nid(jc,jm)
611               
612               IF ( llaction ) THEN
613                 
614                  kinfo = OASIS_Rcv
615                  pdata(1:nitems) = recvfld(1:nitems) 
616                 
617                  IF ( ln_ctl ) THEN       
618                     number_to_print = 10
619                     IF ( nitems < number_to_print ) number_to_print = nitems
620                     WRITE(numout,*) '****************'
621                     WRITE(numout,*) 'oasis_get: Incoming ', srcv(kid)%clname
622                     WRITE(numout,*) 'oasis_get: ivarid '  , srcv(kid)%nid(jc,jm)
623                     WRITE(numout,*) 'oasis_get:   kstep', kstep
624                     WRITE(numout,*) 'oasis_get:   info ', kinfo
625                     WRITE(numout,*) '     - Minimum Value is ', MINVAL(pdata(:))
626                     WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:))
627                     WRITE(numout,*) '     - Start of data is ', pdata(1:number_to_print)
628                     WRITE(numout,*) '****************'
629                     IF(lflush) CALL flush(numout)
630                  ENDIF
631                 
632               ENDIF
633            ENDIF   
634          ENDIF
635           
636       ENDDO
637       
638       ! Set the precision that we want to broadcast using MPI_BCAST
639       SELECT CASE( wp )
640       CASE( sp ) 
641         MPI_WORKING_PRECISION = MPI_REAL                ! Single precision
642       CASE( dp )
643         MPI_WORKING_PRECISION = MPI_DOUBLE_PRECISION    ! Double precision
644       CASE default
645         CALL oasis_abort( ncomp_id, "cpl_rcv_1d", "Could not find precision for coupling 0d or 1d field" )
646       END SELECT
647
648       ! We have to broadcast (potentially) received values from PE 0 to all
649       ! the others. If no new data has been received we're just
650       ! broadcasting the existing values but there's no more efficient way
651       ! to deal with that w/o NEMO adopting a UM-style test mechanism
652       ! to determine active put/get timesteps.
653       CALL mpi_bcast( pdata, nitems, MPI_WORKING_PRECISION, localRoot, mpi_comm_opa, ierr )
654
655      !
656   END SUBROUTINE cpl_rcv_1d
657
658
659   INTEGER FUNCTION cpl_freq( cdfieldname ) 
660      !!---------------------------------------------------------------------
661      !!              ***  ROUTINE cpl_freq  ***
662      !!
663      !! ** Purpose : - send back the coupling frequency for a particular field
664      !!----------------------------------------------------------------------
665      CHARACTER(len = *), INTENT(in) ::   cdfieldname    ! field name as set in namcouple file
666      !!
667      INTEGER               :: id
668      INTEGER               :: info
669      INTEGER, DIMENSION(1) :: itmp
670      INTEGER               :: ji,jm     ! local loop index
671      INTEGER               :: mop
672      !!----------------------------------------------------------------------
673      cpl_freq = 0   ! defaut definition
674      id = -1        ! defaut definition
675      !
676      DO ji = 1, nsnd
677         IF (ssnd(ji)%laction ) THEN
678            DO jm = 1, ncplmodel
679               IF( ssnd(ji)%nid(1,jm) /= -1 ) THEN
680                  IF( TRIM(cdfieldname) == TRIM(ssnd(ji)%clname) ) THEN
681                     id = ssnd(ji)%nid(1,jm)
682                     mop = OASIS_Out
683                  ENDIF
684               ENDIF
685            ENDDO
686         ENDIF
687      ENDDO
688      DO ji = 1, nrcv
689         IF (srcv(ji)%laction ) THEN
690            DO jm = 1, ncplmodel
691               IF( srcv(ji)%nid(1,jm) /= -1 ) THEN
692                  IF( TRIM(cdfieldname) == TRIM(srcv(ji)%clname) ) THEN
693                     id = srcv(ji)%nid(1,jm)
694                     mop = OASIS_In
695                  ENDIF
696               ENDIF
697            ENDDO
698         ENDIF
699      ENDDO
700      !
701      IF( id /= -1 ) THEN
702#if ! defined key_oa3mct_v1v2
703         CALL oasis_get_freqs(id, mop, 1, itmp, info)
704#else
705         CALL oasis_get_freqs(id,      1, itmp, info)
706#endif
707         cpl_freq = itmp(1)
708      ENDIF
709      !
710   END FUNCTION cpl_freq
711
712
713   SUBROUTINE cpl_finalize
714      !!---------------------------------------------------------------------
715      !!              ***  ROUTINE cpl_finalize  ***
716      !!
717      !! ** Purpose : - Finalizes the coupling. If MPI_init has not been
718      !!      called explicitly before cpl_init it will also close
719      !!      MPI communication.
720      !!----------------------------------------------------------------------
721      !
722      DEALLOCATE( exfld )
723      IF (nstop == 0) THEN
724         CALL oasis_terminate( nerror )         
725      ELSE
726         CALL oasis_abort( ncomp_id, "cpl_finalize", "NEMO ABORT STOP" )
727      ENDIF       
728      !
729   END SUBROUTINE cpl_finalize
730
731#if ! defined key_oasis3
732
733   !!----------------------------------------------------------------------
734   !!   No OASIS Library          OASIS3 Dummy module...
735   !!----------------------------------------------------------------------
736
737   SUBROUTINE oasis_init_comp(k1,cd1,k2)
738      CHARACTER(*), INTENT(in   ) ::  cd1
739      INTEGER     , INTENT(  out) ::  k1,k2
740      k1 = -1 ; k2 = -1
741      WRITE(numout,*) 'oasis_init_comp: Error you sould not be there...', cd1
742   END SUBROUTINE oasis_init_comp
743
744   SUBROUTINE oasis_abort(k1,cd1,cd2)
745      INTEGER     , INTENT(in   ) ::  k1
746      CHARACTER(*), INTENT(in   ) ::  cd1,cd2
747      WRITE(numout,*) 'oasis_abort: Error you sould not be there...', cd1, cd2
748   END SUBROUTINE oasis_abort
749
750   SUBROUTINE oasis_get_localcomm(k1,k2)
751      INTEGER     , INTENT(  out) ::  k1,k2
752      k1 = -1 ; k2 = -1
753      WRITE(numout,*) 'oasis_get_localcomm: Error you sould not be there...'
754   END SUBROUTINE oasis_get_localcomm
755
756   SUBROUTINE oasis_def_partition(k1,k2,k3,k4)
757      INTEGER     , INTENT(  out) ::  k1,k3
758      INTEGER     , INTENT(in   ) ::  k2(5)
759      INTEGER     , INTENT(in   ) ::  k4
760      k1 = k2(1) ; k3 = k2(5)+k4
761      WRITE(numout,*) 'oasis_def_partition: Error you sould not be there...'
762   END SUBROUTINE oasis_def_partition
763
764   SUBROUTINE oasis_def_var(k1,cd1,k2,k3,k4,k5,k6,k7)
765      CHARACTER(*), INTENT(in   ) ::  cd1
766      INTEGER     , INTENT(in   ) ::  k2,k3(2),k4,k5(*),k6
767      INTEGER     , INTENT(  out) ::  k1,k7
768      k1 = -1 ; k7 = -1
769      WRITE(numout,*) 'oasis_def_var: Error you sould not be there...', cd1
770   END SUBROUTINE oasis_def_var
771
772   SUBROUTINE oasis_enddef(k1)
773      INTEGER     , INTENT(  out) ::  k1
774      k1 = -1
775      WRITE(numout,*) 'oasis_enddef: Error you sould not be there...'
776   END SUBROUTINE oasis_enddef
777 
778   SUBROUTINE oasis_put(k1,k2,p1,k3)
779      REAL(wp), DIMENSION(:,:), INTENT(in   ) ::  p1
780      INTEGER                 , INTENT(in   ) ::  k1,k2
781      INTEGER                 , INTENT(  out) ::  k3
782      k3 = -1
783      WRITE(numout,*) 'oasis_put: Error you sould not be there...'
784   END SUBROUTINE oasis_put
785
786   SUBROUTINE oasis_get_1d(k1,k2,p1,k3)
787      REAL(wp), DIMENSION(:)  , INTENT(  out) ::  p1
788      INTEGER                 , INTENT(in   ) ::  k1,k2
789      INTEGER                 , INTENT(  out) ::  k3
790      p1(1) = -1. ; k3 = -1
791      WRITE(numout,*) 'oasis_get_1d: Error you sould not be there...'
792   END SUBROUTINE oasis_get_1d
793
794   SUBROUTINE oasis_get_2d(k1,k2,p1,k3)
795      REAL(wp), DIMENSION(:,:), INTENT(  out) ::  p1
796      INTEGER                 , INTENT(in   ) ::  k1,k2
797      INTEGER                 , INTENT(  out) ::  k3
798      p1(1,1) = -1. ; k3 = -1
799      WRITE(numout,*) 'oasis_get_2d: Error you sould not be there...'
800   END SUBROUTINE oasis_get_2d
801
802   SUBROUTINE oasis_get_freqs(k1,k5,k2,k3,k4)
803      INTEGER              , INTENT(in   ) ::  k1,k2
804      INTEGER, DIMENSION(1), INTENT(  out) ::  k3
805      INTEGER              , INTENT(  out) ::  k4,k5
806      k3(1) = k1 ; k4 = k2 ; k5 = k2
807      WRITE(numout,*) 'oasis_get_freqs: Error you sould not be there...'
808   END SUBROUTINE oasis_get_freqs
809
810   SUBROUTINE oasis_terminate(k1)
811      INTEGER     , INTENT(  out) ::  k1
812      k1 = -1
813      WRITE(numout,*) 'oasis_terminate: Error you sould not be there...'
814   END SUBROUTINE oasis_terminate
815   
816#endif
817
818   !!=====================================================================
819END MODULE cpl_oasis3
Note: See TracBrowser for help on using the repository browser.