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/2014/dev_4728_CNRS04_coupled_interface/NEMOGCM/NEMO/OPA_SRC/SBC – NEMO

source: branches/2014/dev_4728_CNRS04_coupled_interface/NEMOGCM/NEMO/OPA_SRC/SBC/cpl_oasis3.F90 @ 4857

Last change on this file since 4857 was 4857, checked in by smasson, 9 years ago

dev_4728_CNRS04_coupled_interface: cleaning of the coupling interface for OASIS3-MCT

  • Property svn:keywords set to Id
File size: 19.2 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
18   !!----------------------------------------------------------------------
19   !!   cpl_init     : initialization of coupled mode communication
20   !!   cpl_define   : definition of grid and fields
21   !!   cpl_snd     : snd out fields in coupled mode
22   !!   cpl_rcv     : receive fields in coupled mode
23   !!   cpl_finalize : finalize the coupled mode communication
24   !!----------------------------------------------------------------------
25#if defined key_oasis3
26   USE mod_oasis                    ! OASIS3-MCT module
27#endif
28   USE par_oce                      ! ocean parameters
29   USE dom_oce                      ! ocean space and time domain
30   USE in_out_manager               ! I/O manager
31   USE lbclnk                       ! ocean lateral boundary conditions (or mpp link)
32
33   IMPLICIT NONE
34   PRIVATE
35
36   PUBLIC   cpl_init
37   PUBLIC   cpl_define
38   PUBLIC   cpl_snd
39   PUBLIC   cpl_rcv
40   PUBLIC   cpl_freq
41   PUBLIC   cpl_finalize
42
43   INTEGER, PUBLIC            ::   OASIS_Rcv  = 1    !: return code if received field
44   INTEGER, PUBLIC            ::   OASIS_idle = 0    !: return code if nothing done by oasis
45   INTEGER                    ::   ncomp_id          ! id returned by oasis_init_comp
46   INTEGER                    ::   nerror            ! return error code
47#if defined key_oasis3
48   LOGICAL, PUBLIC, PARAMETER ::   lk_cpl = .TRUE.   !: coupled flag
49#else
50   LOGICAL, PUBLIC, PARAMETER ::   lk_cpl = .FALSE.  !: coupled flag
51   ! OASIS Variables not used. defined only for compilation purpose
52   INTEGER                    ::   OASIS_Out         = -1
53   INTEGER                    ::   OASIS_REAL        = -1
54   INTEGER                    ::   OASIS_Ok          = -1
55   INTEGER                    ::   OASIS_In          = -1
56   INTEGER                    ::   OASIS_Sent        = -1
57   INTEGER                    ::   OASIS_SentOut     = -1
58   INTEGER                    ::   OASIS_ToRest      = -1
59   INTEGER                    ::   OASIS_ToRestOut   = -1
60   INTEGER                    ::   OASIS_Recvd       = -1
61   INTEGER                    ::   OASIS_RecvOut     = -1
62   INTEGER                    ::   OASIS_FromRest    = -1
63   INTEGER                    ::   OASIS_FromRestOut = -1
64#endif
65
66   INTEGER, PARAMETER ::   nmaxfld=40    ! Maximum number of coupling fields
67   
68   TYPE, PUBLIC ::   FLD_CPL               !: Type for coupling field information
69      LOGICAL               ::   laction   ! To be coupled or not
70      CHARACTER(len = 8)    ::   clname    ! Name of the coupling field   
71      CHARACTER(len = 1)    ::   clgrid    ! Grid type 
72      REAL(wp)              ::   nsgn      ! Control of the sign change
73      INTEGER, DIMENSION(9) ::   nid       ! Id of the field (no more than 9 categories)
74      INTEGER               ::   nct       ! Number of categories in field
75   END TYPE FLD_CPL
76
77   TYPE(FLD_CPL), DIMENSION(nmaxfld), PUBLIC ::   srcv, ssnd   !: Coupling fields
78
79   REAL(wp), DIMENSION(:,:), ALLOCATABLE ::   exfld   ! Temporary buffer for receiving
80
81   !!----------------------------------------------------------------------
82   !! NEMO/OPA 3.3 , NEMO Consortium (2010)
83   !! $Id$
84   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
85   !!----------------------------------------------------------------------
86CONTAINS
87
88   SUBROUTINE cpl_init( kl_comm )
89      !!-------------------------------------------------------------------
90      !!             ***  ROUTINE cpl_init  ***
91      !!
92      !! ** Purpose :   Initialize coupled mode communication for ocean
93      !!    exchange between AGCM, OGCM and COUPLER. (OASIS3 software)
94      !!
95      !! ** Method  :   OASIS3 MPI communication
96      !!--------------------------------------------------------------------
97      INTEGER, INTENT(out) ::   kl_comm   ! local communicator of the model
98      !!--------------------------------------------------------------------
99
100      ! WARNING: No write in numout in this routine
101      !============================================
102
103      !------------------------------------------------------------------
104      ! 1st Initialize the OASIS system for the application
105      !------------------------------------------------------------------
106      CALL oasis_init_comp ( ncomp_id, 'oceanx', nerror )
107      IF ( nerror /= OASIS_Ok ) &
108         CALL oasis_abort (ncomp_id, 'cpl_init', 'Failure in oasis_init_comp')
109
110      !------------------------------------------------------------------
111      ! 3rd Get an MPI communicator for OPA local communication
112      !------------------------------------------------------------------
113
114      CALL oasis_get_localcomm ( kl_comm, nerror )
115      IF ( nerror /= OASIS_Ok ) &
116         CALL oasis_abort (ncomp_id, 'cpl_init','Failure in oasis_get_localcomm' )
117      !
118   END SUBROUTINE cpl_init
119
120
121   SUBROUTINE cpl_define( krcv, ksnd )
122      !!-------------------------------------------------------------------
123      !!             ***  ROUTINE cpl_define  ***
124      !!
125      !! ** Purpose :   Define grid and field information for ocean
126      !!    exchange between AGCM, OGCM and COUPLER. (OASIS3 software)
127      !!
128      !! ** Method  :   OASIS3 MPI communication
129      !!--------------------------------------------------------------------
130      INTEGER, INTENT(in) ::   krcv, ksnd     ! Number of received and sent coupling fields
131      !
132      INTEGER :: id_part
133      INTEGER :: paral(5)       ! OASIS3 box partition
134      INTEGER :: ishape(2,2)    ! shape of arrays passed to PSMILe
135      INTEGER :: ji,jc          ! local loop indicees
136      CHARACTER(LEN=8) :: zclname
137      !!--------------------------------------------------------------------
138
139      IF(lwp) WRITE(numout,*)
140      IF(lwp) WRITE(numout,*) 'cpl_define : initialization in coupled ocean/atmosphere case'
141      IF(lwp) WRITE(numout,*) '~~~~~~~~~~~~~~~~~'
142      IF(lwp) WRITE(numout,*)
143
144      !
145      ! ... Define the shape for the area that excludes the halo
146      !     For serial configuration (key_mpp_mpi not being active)
147      !     nl* is set to the global values 1 and jp*glo.
148      !
149      ishape(:,1) = (/ 1, nlei-nldi+1 /)
150      ishape(:,2) = (/ 1, nlej-nldj+1 /)
151      !
152      ! ... Allocate memory for data exchange
153      !
154      ALLOCATE(exfld(nlei-nldi+1, nlej-nldj+1), stat = nerror)
155      IF( nerror > 0 ) THEN
156         CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in allocating exfld')   ;   RETURN
157      ENDIF
158      !
159      ! -----------------------------------------------------------------
160      ! ... Define the partition
161      ! -----------------------------------------------------------------
162     
163      paral(1) = 2                                              ! box partitioning
164      paral(2) = jpiglo * (nldj-1+njmpp-1) + (nldi-1+nimpp-1)   ! NEMO lower left corner global offset   
165      paral(3) = nlei-nldi+1                                    ! local extent in i
166      paral(4) = nlej-nldj+1                                    ! local extent in j
167      paral(5) = jpiglo                                         ! global extent in x
168     
169      IF( ln_ctl ) THEN
170         WRITE(numout,*) ' multiexchg: paral (1:5)', paral
171         WRITE(numout,*) ' multiexchg: jpi, jpj =', jpi, jpj
172         WRITE(numout,*) ' multiexchg: nldi, nlei, nimpp =', nldi, nlei, nimpp
173         WRITE(numout,*) ' multiexchg: nldj, nlej, njmpp =', nldj, nlej, njmpp
174      ENDIF
175     
176      CALL oasis_def_partition ( id_part, paral, nerror )
177      !
178      ! ... Announce send variables.
179      !
180      DO ji = 1, ksnd
181         IF ( ssnd(ji)%laction ) THEN
182            DO jc = 1, ssnd(ji)%nct
183               IF ( ssnd(ji)%nct .gt. 1 ) THEN
184                  WRITE(zclname,'( a7, i1)') ssnd(ji)%clname,jc
185               ELSE
186                  zclname=ssnd(ji)%clname
187               ENDIF
188               WRITE(numout,*) "Define",ji,jc,zclname," for",OASIS_Out
189               CALL oasis_def_var (ssnd(ji)%nid(jc), zclname, id_part, (/ 2, 0/),   &
190                    OASIS_Out, ishape, OASIS_REAL, nerror)
191               IF ( nerror /= OASIS_Ok ) THEN
192                  WRITE(numout,*) 'Failed to define transient ', ji, TRIM(zclname)
193                  CALL oasis_abort ( ssnd(ji)%nid(jc), 'cpl_define', 'Failure in oasis_def_var')
194               ENDIF
195            END DO
196         ENDIF
197      END DO
198      !
199      ! ... Announce received variables.
200      !
201      DO ji = 1, krcv
202         IF ( srcv(ji)%laction ) THEN
203            DO jc = 1, srcv(ji)%nct
204               IF ( srcv(ji)%nct .gt. 1 ) THEN
205                  WRITE(zclname,'( a7, i1)') srcv(ji)%clname,jc
206               ELSE
207                  zclname=srcv(ji)%clname
208               ENDIF
209               WRITE(numout,*) "Define",ji,jc,zclname," for",OASIS_In
210               CALL oasis_def_var ( srcv(ji)%nid(jc), zclname, id_part, (/ 2, 0/),   &
211                    &                      OASIS_In    , ishape   , OASIS_REAL, nerror)
212               IF ( nerror /= OASIS_Ok ) THEN
213                  WRITE(numout,*) 'Failed to define transient ', ji, TRIM(zclname)
214                  CALL oasis_abort ( srcv(ji)%nid(jc), 'cpl_define', 'Failure in oasis_def_var')
215               ENDIF
216            END DO
217         ENDIF
218      END DO
219     
220      !------------------------------------------------------------------
221      ! End of definition phase
222      !------------------------------------------------------------------
223     
224      CALL oasis_enddef(nerror)
225      IF( nerror /= OASIS_Ok )   CALL oasis_abort ( ncomp_id, 'cpl_define', 'Failure in oasis_enddef')
226      !
227   END SUBROUTINE cpl_define
228   
229   
230   SUBROUTINE cpl_snd( kid, kstep, pdata, kinfo )
231      !!---------------------------------------------------------------------
232      !!              ***  ROUTINE cpl_snd  ***
233      !!
234      !! ** Purpose : - At each coupling time-step,this routine sends fields
235      !!      like sst or ice cover to the coupler or remote application.
236      !!----------------------------------------------------------------------
237      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
238      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
239      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
240      REAL(wp), DIMENSION(:,:,:), INTENT(in   ) ::   pdata
241      !!
242      INTEGER                                   ::   jc        ! local loop index
243      !!--------------------------------------------------------------------
244      !
245      ! snd data to OASIS3
246      !
247      DO jc = 1, ssnd(kid)%nct
248
249         CALL oasis_put ( ssnd(kid)%nid(jc), kstep, pdata(nldi:nlei, nldj:nlej,jc), kinfo )
250         
251         IF ( ln_ctl ) THEN       
252            IF ( kinfo == OASIS_Sent     .OR. kinfo == OASIS_ToRest .OR.   &
253                 & kinfo == OASIS_SentOut  .OR. kinfo == OASIS_ToRestOut ) THEN
254               WRITE(numout,*) '****************'
255               WRITE(numout,*) 'oasis_put: Outgoing ', ssnd(kid)%clname
256               WRITE(numout,*) 'oasis_put: ivarid ', ssnd(kid)%nid(jc)
257               WRITE(numout,*) 'oasis_put:  kstep ', kstep
258               WRITE(numout,*) 'oasis_put:   info ', kinfo
259               WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
260               WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
261               WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
262               WRITE(numout,*) '****************'
263            ENDIF
264         ENDIF
265
266      ENDDO
267      !
268    END SUBROUTINE cpl_snd
269
270
271   SUBROUTINE cpl_rcv( kid, kstep, pdata, kinfo )
272      !!---------------------------------------------------------------------
273      !!              ***  ROUTINE cpl_rcv  ***
274      !!
275      !! ** Purpose : - At each coupling time-step,this routine receives fields
276      !!      like stresses and fluxes from the coupler or remote application.
277      !!----------------------------------------------------------------------
278      INTEGER                   , INTENT(in   ) ::   kid       ! variable index in the array
279      INTEGER                   , INTENT(in   ) ::   kstep     ! ocean time-step in seconds
280      REAL(wp), DIMENSION(:,:,:), INTENT(inout) ::   pdata     ! IN to keep the value if nothing is done
281      INTEGER                   , INTENT(  out) ::   kinfo     ! OASIS3 info argument
282      !!
283      INTEGER                                   ::   jc        ! local loop index
284      LOGICAL                                   ::   llaction
285      !!--------------------------------------------------------------------
286      !
287      ! receive local data from OASIS3 on every process
288      !
289      DO jc = 1, srcv(kid)%nct
290
291         CALL oasis_get ( srcv(kid)%nid(jc), kstep, exfld, kinfo )         
292         
293         llaction = .false.
294         IF( kinfo == OASIS_Recvd   .OR. kinfo == OASIS_FromRest .OR.   &
295              kinfo == OASIS_RecvOut .OR. kinfo == OASIS_FromRestOut )   llaction = .TRUE.
296         
297         IF ( ln_ctl )   WRITE(numout,*) "llaction, kinfo, kstep, ivarid: " , llaction, kinfo, kstep, srcv(kid)%nid(jc)
298         
299         IF ( llaction ) THEN
300           
301            kinfo = OASIS_Rcv
302            pdata(nldi:nlei, nldj:nlej,jc) = exfld(:,:)
303           
304            !--- Fill the overlap areas and extra hallows (mpp)
305            !--- check periodicity conditions (all cases)
306            CALL lbc_lnk( pdata(:,:,jc), srcv(kid)%clgrid, srcv(kid)%nsgn )   
307           
308            IF ( ln_ctl ) THEN       
309               WRITE(numout,*) '****************'
310               WRITE(numout,*) 'oasis_get: Incoming ', srcv(kid)%clname
311               WRITE(numout,*) 'oasis_get: ivarid '  , srcv(kid)%nid(jc)
312               WRITE(numout,*) 'oasis_get:   kstep', kstep
313               WRITE(numout,*) 'oasis_get:   info ', kinfo
314               WRITE(numout,*) '     - Minimum value is ', MINVAL(pdata(:,:,jc))
315               WRITE(numout,*) '     - Maximum value is ', MAXVAL(pdata(:,:,jc))
316               WRITE(numout,*) '     -     Sum value is ', SUM(pdata(:,:,jc))
317               WRITE(numout,*) '****************'
318            ENDIF
319           
320         ELSE
321            kinfo = OASIS_idle     
322         ENDIF
323         
324      ENDDO
325      !
326   END SUBROUTINE cpl_rcv
327
328
329   INTEGER FUNCTION cpl_freq( kid ) 
330      !!---------------------------------------------------------------------
331      !!              ***  ROUTINE cpl_freq  ***
332      !!
333      !! ** Purpose : - send back the coupling frequency for a particular field
334      !!----------------------------------------------------------------------
335      INTEGER,INTENT(in) ::   kid   ! variable index
336      !!
337      INTEGER :: info
338      !!----------------------------------------------------------------------
339      CALL oasis_get_freqs(kid, 1, cpl_freq, info)
340      !
341   END FUNCTION cpl_freq
342
343
344   SUBROUTINE cpl_finalize
345      !!---------------------------------------------------------------------
346      !!              ***  ROUTINE cpl_finalize  ***
347      !!
348      !! ** Purpose : - Finalizes the coupling. If MPI_init has not been
349      !!      called explicitly before cpl_init it will also close
350      !!      MPI communication.
351      !!----------------------------------------------------------------------
352      !
353      DEALLOCATE( exfld )
354      IF (nstop == 0) THEN
355         CALL oasis_terminate( nerror )         
356      ELSE
357         CALL oasis_abort( ncomp_id, "cpl_finalize", "NEMO ABORT STOP" )
358      ENDIF       
359      !
360   END SUBROUTINE cpl_finalize
361
362#if ! defined key_oasis3
363
364   !!----------------------------------------------------------------------
365   !!   No OASIS Library          OASIS3 Dummy module...
366   !!----------------------------------------------------------------------
367
368   SUBROUTINE oasis_init_comp(k1,cd1,k2)
369      CHARACTER(*), INTENT(in   ) ::  cd1
370      INTEGER     , INTENT(  out) ::  k1,k2
371      k1 = -1 ; k2 = -1
372      WRITE(numout,*) 'oasis_init_comp: Error you sould not be there...', cd1
373   END SUBROUTINE oasis_init_comp
374
375   SUBROUTINE oasis_abort(k1,cd1,cd2)
376      INTEGER     , INTENT(in   ) ::  k1
377      CHARACTER(*), INTENT(in   ) ::  cd1,cd2
378      WRITE(numout,*) 'oasis_abort: Error you sould not be there...', cd1, cd2
379   END SUBROUTINE oasis_abort
380
381   SUBROUTINE oasis_get_localcomm(k1,k2)
382      INTEGER     , INTENT(  out) ::  k1,k2
383      k1 = -1 ; k2 = -1
384      WRITE(numout,*) 'oasis_get_localcomm: Error you sould not be there...'
385   END SUBROUTINE oasis_get_localcomm
386
387   SUBROUTINE oasis_def_partition(k1,k2,k3)
388      INTEGER     , INTENT(  out) ::  k1,k3
389      INTEGER     , INTENT(in   ) ::  k2(5)
390      k1 = k2(1) ; k3 = k2(5)
391      WRITE(numout,*) 'oasis_def_partition: Error you sould not be there...'
392   END SUBROUTINE oasis_def_partition
393
394   SUBROUTINE oasis_def_var(k1,cd1,k2,k3,k4,k5,k6,k7)
395      CHARACTER(*), INTENT(in   ) ::  cd1
396      INTEGER     , INTENT(in   ) ::  k2,k3(2),k4,k5(2,2),k6
397      INTEGER     , INTENT(  out) ::  k1,k7
398      k1 = -1 ; k7 = -1
399      WRITE(numout,*) 'oasis_def_var: Error you sould not be there...', cd1
400   END SUBROUTINE oasis_def_var
401
402   SUBROUTINE oasis_enddef(k1)
403      INTEGER     , INTENT(  out) ::  k1
404      k1 = -1
405      WRITE(numout,*) 'oasis_enddef: Error you sould not be there...'
406   END SUBROUTINE oasis_enddef
407 
408   SUBROUTINE oasis_put(k1,k2,p1,k3)
409      REAL(wp), DIMENSION(:,:), INTENT(in   ) ::  p1
410      INTEGER                 , INTENT(in   ) ::  k1,k2
411      INTEGER                 , INTENT(  out) ::  k3
412      k3 = -1
413      WRITE(numout,*) 'oasis_put: Error you sould not be there...'
414   END SUBROUTINE oasis_put
415
416   SUBROUTINE oasis_get(k1,k2,p1,k3)
417      REAL(wp), DIMENSION(:,:), INTENT(  out) ::  p1
418      INTEGER                 , INTENT(in   ) ::  k1,k2
419      INTEGER                 , INTENT(  out) ::  k3
420      p1(1,1) = -1. ; k3 = -1
421      WRITE(numout,*) 'oasis_get: Error you sould not be there...'
422   END SUBROUTINE oasis_get
423
424   SUBROUTINE oasis_get_freqs(k1,k2,k3,k4)
425      INTEGER     , INTENT(in   ) ::  k1,k2
426      INTEGER     , INTENT(  out) ::  k3,k4
427      k3 = k1 ; k4 = k2
428      WRITE(numout,*) 'oasis_get_freqs: Error you sould not be there...'
429   END SUBROUTINE oasis_get_freqs
430
431   SUBROUTINE oasis_terminate(k1)
432      INTEGER     , INTENT(  out) ::  k1
433      k1 = -1
434      WRITE(numout,*) 'oasis_terminate: Error you sould not be there...'
435   END SUBROUTINE oasis_terminate
436   
437#endif
438
439   !!=====================================================================
440END MODULE cpl_oasis3
Note: See TracBrowser for help on using the repository browser.