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 branches/UKMO/dev_r5518_GO6_runoff_O4test/NEMOGCM/NEMO/OPA_SRC/SBC – NEMO

source: branches/UKMO/dev_r5518_GO6_runoff_O4test/NEMOGCM/NEMO/OPA_SRC/SBC/cpl_oasis3.F90 @ 10217

Last change on this file since 10217 was 10217, checked in by frrh, 6 years ago

Save revised working version.
This is safe with both OASIS vn3.0 and 4.0 for 0D, 1D and 2D field exchanges.
It does not cater for bundles.

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