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.
nemogcm.F90 in branches/UKMO/dev_r5107_restart_date/NEMOGCM/NEMO/OOO_SRC – NEMO

source: branches/UKMO/dev_r5107_restart_date/NEMOGCM/NEMO/OOO_SRC/nemogcm.F90 @ 5493

Last change on this file since 5493 was 5493, checked in by dancopsey, 9 years ago

Removed SVN keywords.

File size: 30.6 KB
Line 
1MODULE nemogcm
2   !!======================================================================
3   !!                       ***  MODULE nemogcm   ***
4   !! Ocean system   : NEMO GCM (ocean dynamics, on-line tracers, biochemistry and sea-ice)
5   !!======================================================================
6   !! History :  OPA  ! 1990-10  (C. Levy, G. Madec)  Original code
7   !!            7.0  ! 1991-11  (M. Imbard, C. Levy, G. Madec)
8   !!            7.1  ! 1993-03  (M. Imbard, C. Levy, G. Madec, O. Marti, M. Guyon, A. Lazar,
9   !!                             P. Delecluse, C. Perigaud, G. Caniaux, B. Colot, C. Maes) release 7.1
10   !!             -   ! 1992-06  (L.Terray)  coupling implementation
11   !!             -   ! 1993-11  (M.A. Filiberti) IGLOO sea-ice
12   !!            8.0  ! 1996-03  (M. Imbard, C. Levy, G. Madec, O. Marti, M. Guyon, A. Lazar,
13   !!                             P. Delecluse, L.Terray, M.A. Filiberti, J. Vialar, A.M. Treguier, M. Levy) release 8.0
14   !!            8.1  ! 1997-06  (M. Imbard, G. Madec)
15   !!            8.2  ! 1999-11  (M. Imbard, H. Goosse)  LIM sea-ice model
16   !!                 ! 1999-12  (V. Thierry, A-M. Treguier, M. Imbard, M-A. Foujols)  OPEN-MP
17   !!                 ! 2000-07  (J-M Molines, M. Imbard)  Open Boundary Conditions  (CLIPPER)
18   !!   NEMO     1.0  ! 2002-08  (G. Madec)  F90: Free form and modules
19   !!             -   ! 2004-06  (R. Redler, NEC CCRLE, Germany) add OASIS[3/4] coupled interfaces
20   !!             -   ! 2004-08  (C. Talandier) New trends organization
21   !!             -   ! 2005-06  (C. Ethe) Add the 1D configuration possibility
22   !!             -   ! 2005-11  (V. Garnier) Surface pressure gradient organization
23   !!             -   ! 2006-03  (L. Debreu, C. Mazauric)  Agrif implementation
24   !!             -   ! 2006-04  (G. Madec, R. Benshila)  Step reorganization
25   !!             -   ! 2007-07  (J. Chanut, A. Sellar) Unstructured open boundaries (BDY)
26   !!            3.2  ! 2009-08  (S. Masson)  open/write in the listing file in mpp
27   !!            3.3  ! 2010-05  (K. Mogensen, A. Weaver, M. Martin, D. Lea) Assimilation interface
28   !!             -   ! 2010-10  (C. Ethe, G. Madec) reorganisation of initialisation phase
29   !!            3.3.1! 2011-01  (A. R. Porter, STFC Daresbury) dynamical allocation
30   !!            3.4  ! 2011-11  (C. Harris) decomposition changes for running with CICE
31   !!----------------------------------------------------------------------
32
33   !!----------------------------------------------------------------------
34   !!   nemo_gcm       : solve ocean dynamics, tracer, biogeochemistry and/or sea-ice
35   !!   nemo_init      : initialization of the NEMO system
36   !!   nemo_ctl       : initialisation of the contol print
37   !!   nemo_closefile : close remaining open files
38   !!   nemo_alloc     : dynamical allocation
39   !!   nemo_partition : calculate MPP domain decomposition
40   !!   factorise      : calculate the factors of the no. of MPI processes
41   !!----------------------------------------------------------------------
42   USE step_oce        ! module used in the ocean time stepping module
43   USE domcfg          ! domain configuration               (dom_cfg routine)
44   USE mppini          ! shared/distributed memory setting (mpp_init routine)
45   USE domain          ! domain initialization             (dom_init routine)
46#if defined key_nemocice_decomp
47   USE ice_domain_size, only: nx_global, ny_global
48#endif
49   USE istate          ! initial state setting          (istate_init routine)
50   USE phycst          ! physical constant                  (par_cst routine)
51   USE diaobs          ! Observation diagnostics       (dia_obs_init routine)
52   USE lib_fortran     ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
53   USE step            ! NEMO time-stepping                 (stp     routine)
54   USE icbini          ! handle bergs, initialisation
55   USE icbstp          ! handle bergs, calving, themodynamics and transport
56   USE cpl_oasis3      ! OASIS3 coupling
57   USE lib_mpp         ! distributed memory computing
58#if defined key_iomput
59   USE xios
60#endif
61   USE ooo_data        ! Offline obs_oper data
62   USE ooo_read        ! Offline obs_oper read routines
63   USE ooo_intp        ! Offline obs_oper interpolation
64
65   IMPLICIT NONE
66   PRIVATE
67
68   PUBLIC   nemo_gcm    ! called by nemo.f90
69   PUBLIC   nemo_init   ! needed by AGRIF
70   PUBLIC   nemo_alloc  ! needed by TAM
71
72   CHARACTER(lc) ::   cform_aaa="( /, 'AAAAAAAA', / ) "     ! flag for output listing
73
74   !!----------------------------------------------------------------------
75   !! NEMO/OPA 4.0 , NEMO Consortium (2011)
76   !! $Id$
77   !! Software governed by the CeCILL licence     (NEMOGCM/NEMO_CeCILL.txt)
78   !!----------------------------------------------------------------------
79CONTAINS
80
81   SUBROUTINE nemo_gcm
82         !!----------------------------------------------------------------------
83         !!                    ***  SUBROUTINE offline_obs_oper ***
84         !!
85         !! ** Purpose : To use NEMO components to interpolate model fields
86         !!              to observation space.
87         !!
88         !! ** Method : 1. Initialise NEMO
89         !!             2. Initialise offline obs_oper
90         !!             3. Cycle through match ups
91         !!             4. Write results to file
92         !!
93         !!----------------------------------------------------------------------
94         !! Class 4 output stream switch
95         USE obs_fbm, ONLY: ln_cl4
96         !! Initialise NEMO
97         CALL nemo_init
98         !! Initialise Offline obs_oper data
99         CALL ooo_data_init( ln_cl4 )
100         !! Loop over various model counterparts
101         DO jimatch = 1, cl4_match_len
102            IF (jimatch .GT. 1) THEN
103               !! Initialise obs_oper
104               CALL dia_obs_init
105            END IF
106            !! Interpolate to observation space
107            CALL ooo_interp
108            !! Pipe to output files
109            CALL dia_obs_wri
110            !! Reset the obs_oper between
111            CALL dia_obs_dealloc
112         END DO
113         !! Safely stop MPI
114         IF(lk_mpp) CALL mppstop  ! end mpp communications
115   END SUBROUTINE nemo_gcm
116
117   SUBROUTINE nemo_init
118      !!----------------------------------------------------------------------
119      !!                     ***  ROUTINE nemo_init  ***
120      !!
121      !! ** Purpose :   initialization of the NEMO GCM
122      !!----------------------------------------------------------------------
123      INTEGER ::   ji            ! dummy loop indices
124      INTEGER ::   ilocal_comm   ! local integer
125      CHARACTER(len=80), DIMENSION(16) ::   cltxt
126      !!
127      NAMELIST/namctl/ ln_ctl, nn_print, nn_ictls, nn_ictle,   &
128         &             nn_isplt, nn_jsplt, nn_jctls, nn_jctle,   &
129         &             nn_bench, nn_timing
130      NAMELIST/namcfg/ cp_cfg, cp_cfz, jp_cfg, jpidta, jpjdta, jpkdta, jpiglo, jpjglo, &
131         &             jpizoom, jpjzoom, jperio
132      !!----------------------------------------------------------------------
133      !
134      cltxt = ''
135      !
136      !                             ! Open reference namelist and configuration namelist files
137      CALL ctl_opn( numnam_ref, 'namelist_ref', 'OLD', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE. )
138      CALL ctl_opn( numnam_cfg, 'namelist_cfg', 'OLD', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE. )
139      !
140      REWIND( numnam_ref )              ! Namelist namctl in reference namelist : Control prints & Benchmark
141      READ  ( numnam_ref, namctl, IOSTAT = ios, ERR = 901 )
142901   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namctl in reference namelist', .TRUE. )
143
144      REWIND( numnam_cfg )              ! Namelist namctl in confguration namelist : Control prints & Benchmark
145      READ  ( numnam_cfg, namctl, IOSTAT = ios, ERR = 902 )
146902   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namctl in configuration namelist', .TRUE. )
147
148      !
149      REWIND( numnam_ref )              ! Namelist namcfg in reference namelist : Control prints & Benchmark
150      READ  ( numnam_ref, namcfg, IOSTAT = ios, ERR = 903 )
151903   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namcfg in reference namelist', .TRUE. )
152
153      REWIND( numnam_cfg )              ! Namelist namcfg in confguration namelist : Control prints & Benchmark
154      READ  ( numnam_cfg, namcfg, IOSTAT = ios, ERR = 904 )
155904   IF( ios /= 0 ) CALL ctl_nam ( ios , 'namcfg in configuration namelist', .TRUE. )   
156
157      !                             !--------------------------------------------!
158      !                             !  set communicator & select the local node  !
159      !                             !  NB: mynode also opens output.namelist.dyn !
160      !                             !      on unit number numond on first proc   !
161      !                             !--------------------------------------------!
162#if defined key_iomput
163      IF( Agrif_Root() ) THEN
164         IF( lk_cpl ) THEN
165            CALL cpl_init( ilocal_comm )                               ! nemo local communicator given by oasis
166            CALL xios_initialize( "oceanx",local_comm=ilocal_comm )    ! send nemo communicator to xios
167         ELSE
168            CALL  xios_initialize( "nemo",return_comm=ilocal_comm )    ! nemo local communicator given by xios
169         ENDIF
170      ENDIF
171      ENDIF
172      narea = mynode( cltxt, numnam_ref, numnam_cfg, numond , nstop, ilocal_comm )   ! Nodes selection
173#else
174      IF( lk_cpl ) THEN
175         IF( Agrif_Root() ) THEN
176            CALL cpl_init( ilocal_comm )                               ! nemo local communicator given by oasis
177         ENDIF
178         narea = mynode( cltxt, numnam_ref, numnam_cfg, numond , nstop, ilocal_comm )   ! Nodes selection (control print return in cltxt)
179      ELSE
180         ilocal_comm = 0
181         narea = mynode( cltxt, numnam_ref, numnam_cfg, numond , nstop )                ! Nodes selection (control print return in cltxt)
182      ENDIF
183#endif
184      narea = narea + 1                                     ! mynode return the rank of proc (0 --> jpnij -1 )
185
186      lwm = (narea == 1)                                    ! control of output namelists
187      lwp = (narea == 1) .OR. ln_ctl                        ! control of all listing output print
188
189      IF(lwm) THEN
190         ! write merged namelists from earlier to output namelist now that the
191         ! file has been opened in call to mynode. nammpp has already been
192         ! written in mynode (if lk_mpp_mpi)
193         WRITE( numond, namctl )
194         WRITE( numond, namcfg )
195      ENDIF
196
197      ! If dimensions of processor grid weren't specified in the namelist file
198      ! then we calculate them here now that we have our communicator size
199      IF( (jpni < 1) .OR. (jpnj < 1) )THEN
200#if   defined key_mpp_mpi
201         IF( Agrif_Root() ) CALL nemo_partition(mppsize)
202#else
203         jpni  = 1
204         jpnj  = 1
205         jpnij = jpni*jpnj
206#endif
207      END IF
208
209      ! Calculate domain dimensions given calculated jpni and jpnj
210      ! This used to be done in par_oce.F90 when they were parameters rather
211      ! than variables
212      IF( Agrif_Root() ) THEN
213#if defined key_nemocice_decomp
214         jpi = ( nx_global+2-2*jpreci + (jpni-1) ) / jpni + 2*jpreci ! first  dim.
215         jpj = ( ny_global+2-2*jprecj + (jpnj-1) ) / jpnj + 2*jprecj ! second dim.
216#else
217         jpi = ( jpiglo-2*jpreci + (jpni-1) ) / jpni + 2*jpreci   ! first  dim.
218         jpj = ( jpjglo-2*jprecj + (jpnj-1) ) / jpnj + 2*jprecj   ! second dim.
219#endif
220         jpk = jpkdta                                             ! third dim
221         jpim1 = jpi-1                                            ! inner domain indices
222         jpjm1 = jpj-1                                            !   "           "
223         jpkm1 = jpk-1                                            !   "           "
224         jpij  = jpi*jpj                                          !  jpi x j
225      ENDIF
226
227      IF(lwp) THEN                            ! open listing units
228         !
229         CALL ctl_opn( numout, 'ocean.output', 'REPLACE', 'FORMATTED', 'SEQUENTIAL', -1, 6, .FALSE., narea )
230         !
231         WRITE(numout,*)
232         WRITE(numout,*) '   CNRS - NERC - Met OFFICE - MERCATOR-ocean - INGV - CMCC'
233         WRITE(numout,*) '                       NEMO team'
234         WRITE(numout,*) '            Ocean General Circulation Model'
235         WRITE(numout,*) '                  version 3.4  (2011) '
236         WRITE(numout,*)
237         WRITE(numout,*)
238         DO ji = 1, SIZE(cltxt)
239            IF( TRIM(cltxt(ji)) /= '' )   WRITE(numout,*) cltxt(ji)      ! control print of mynode
240         END DO
241         WRITE(numout,cform_aaa)                                         ! Flag AAAAAAA
242         !
243      ENDIF
244
245      ! Now we know the dimensions of the grid and numout has been set we can
246      ! allocate arrays
247      CALL nemo_alloc()
248
249      !                             !-------------------------------!
250      !                             !  NEMO general initialization  !
251      !                             !-------------------------------!
252
253      CALL nemo_ctl                          ! Control prints & Benchmark
254
255      !                                      ! Domain decomposition
256      IF( jpni*jpnj == jpnij ) THEN   ;   CALL mpp_init      ! standard cutting out
257      ELSE                            ;   CALL mpp_init2     ! eliminate land processors
258      ENDIF
259      !
260      IF( nn_timing == 1 )  CALL timing_init
261      !
262      !                                      ! General initialization
263                            CALL     phy_cst    ! Physical constants
264                            CALL     eos_init   ! Equation of state
265                            CALL     dom_cfg    ! Domain configuration
266                            CALL     dom_init   ! Domain
267
268      IF( ln_nnogather )    CALL nemo_northcomms   ! Initialise the northfold neighbour lists (must be done after the masks are defined)
269
270      IF( ln_ctl        )   CALL prt_ctl_init   ! Print control
271
272                            CALL  istate_init   ! ocean initial state (Dynamics and tracers)
273
274      IF( lk_diaobs     ) THEN                  ! Observation & model comparison
275                            CALL dia_obs_init            ! Initialize observational data
276                            CALL dia_obs( nit000 - 1 )   ! Observation operator for restart
277      ENDIF
278   END SUBROUTINE nemo_init
279
280
281   SUBROUTINE nemo_ctl
282      !!----------------------------------------------------------------------
283      !!                     ***  ROUTINE nemo_ctl  ***
284      !!
285      !! ** Purpose :   control print setting
286      !!
287      !! ** Method  : - print namctl information and check some consistencies
288      !!----------------------------------------------------------------------
289      !
290      IF(lwp) THEN                  ! control print
291         WRITE(numout,*)
292         WRITE(numout,*) 'nemo_ctl: Control prints & Benchmark'
293         WRITE(numout,*) '~~~~~~~ '
294         WRITE(numout,*) '   Namelist namctl'
295         WRITE(numout,*) '      run control (for debugging)     ln_ctl     = ', ln_ctl
296         WRITE(numout,*) '      level of print                  nn_print   = ', nn_print
297         WRITE(numout,*) '      Start i indice for SUM control  nn_ictls   = ', nn_ictls
298         WRITE(numout,*) '      End i indice for SUM control    nn_ictle   = ', nn_ictle
299         WRITE(numout,*) '      Start j indice for SUM control  nn_jctls   = ', nn_jctls
300         WRITE(numout,*) '      End j indice for SUM control    nn_jctle   = ', nn_jctle
301         WRITE(numout,*) '      number of proc. following i     nn_isplt   = ', nn_isplt
302         WRITE(numout,*) '      number of proc. following j     nn_jsplt   = ', nn_jsplt
303         WRITE(numout,*) '      benchmark parameter (0/1)       nn_bench   = ', nn_bench
304         WRITE(numout,*) '      timing activated    (0/1)       nn_timing  = ', nn_timing
305      ENDIF
306      !
307      nprint    = nn_print          ! convert DOCTOR namelist names into OLD names
308      nictls    = nn_ictls
309      nictle    = nn_ictle
310      njctls    = nn_jctls
311      njctle    = nn_jctle
312      isplt     = nn_isplt
313      jsplt     = nn_jsplt
314      nbench    = nn_bench
315      !                             ! Parameter control
316      !
317      IF( ln_ctl ) THEN                 ! sub-domain area indices for the control prints
318         IF( lk_mpp .AND. jpnij > 1 ) THEN
319            isplt = jpni   ;   jsplt = jpnj   ;   ijsplt = jpni*jpnj   ! the domain is forced to the real split domain
320         ELSE
321            IF( isplt == 1 .AND. jsplt == 1  ) THEN
322               CALL ctl_warn( ' - isplt & jsplt are equal to 1',   &
323                  &           ' - the print control will be done over the whole domain' )
324            ENDIF
325            ijsplt = isplt * jsplt            ! total number of processors ijsplt
326         ENDIF
327         IF(lwp) WRITE(numout,*)'          - The total number of processors over which the'
328         IF(lwp) WRITE(numout,*)'            print control will be done is ijsplt : ', ijsplt
329         !
330         !                              ! indices used for the SUM control
331         IF( nictls+nictle+njctls+njctle == 0 )   THEN    ! print control done over the default area
332            lsp_area = .FALSE.
333         ELSE                                             ! print control done over a specific  area
334            lsp_area = .TRUE.
335            IF( nictls < 1 .OR. nictls > jpiglo )   THEN
336               CALL ctl_warn( '          - nictls must be 1<=nictls>=jpiglo, it is forced to 1' )
337               nictls = 1
338            ENDIF
339            IF( nictle < 1 .OR. nictle > jpiglo )   THEN
340               CALL ctl_warn( '          - nictle must be 1<=nictle>=jpiglo, it is forced to jpiglo' )
341               nictle = jpiglo
342            ENDIF
343            IF( njctls < 1 .OR. njctls > jpjglo )   THEN
344               CALL ctl_warn( '          - njctls must be 1<=njctls>=jpjglo, it is forced to 1' )
345               njctls = 1
346            ENDIF
347            IF( njctle < 1 .OR. njctle > jpjglo )   THEN
348               CALL ctl_warn( '          - njctle must be 1<=njctle>=jpjglo, it is forced to jpjglo' )
349               njctle = jpjglo
350            ENDIF
351         ENDIF
352      ENDIF
353      !
354      IF( nbench == 1 ) THEN              ! Benchmark
355         SELECT CASE ( cp_cfg )
356         CASE ( 'gyre' )   ;   CALL ctl_warn( ' The Benchmark is activated ' )
357         CASE DEFAULT      ;   CALL ctl_stop( ' The Benchmark is based on the GYRE configuration:',   &
358            &                                 ' key_gyre must be used or set nbench = 0' )
359         END SELECT
360      ENDIF
361      !
362      IF( lk_c1d .AND. .NOT.lk_iomput )   CALL ctl_stop( 'nemo_ctl: The 1D configuration must be used ',   &
363         &                                               'with the IOM Input/Output manager. '         ,   &
364         &                                               'Compile with key_iomput enabled' )
365      !
366      IF( 1_wp /= SIGN(1._wp,-0._wp)  )   CALL ctl_stop( 'nemo_ctl: The intrinsec SIGN function follows ',  &
367         &                                               'f2003 standard. '                              ,  &
368         &                                               'Compile with key_nosignedzero enabled' )
369      !
370   END SUBROUTINE nemo_ctl
371
372
373   SUBROUTINE nemo_closefile
374      !!----------------------------------------------------------------------
375      !!                     ***  ROUTINE nemo_closefile  ***
376      !!
377      !! ** Purpose :   Close the files
378      !!----------------------------------------------------------------------
379      !
380      IF( lk_mpp )   CALL mppsync
381      !
382      CALL iom_close                                 ! close all input/output files managed by iom_*
383      !
384      IF( numstp      /= -1 )   CLOSE( numstp      )   ! time-step file
385      IF( numsol      /= -1 )   CLOSE( numsol      )   ! solver file
386      IF( numnam      /= -1 )   CLOSE( numnam      )   ! oce namelist
387      IF( numnam_ice  /= -1 )   CLOSE( numnam_ice  )   ! ice namelist
388      IF( numevo_ice  /= -1 )   CLOSE( numevo_ice  )   ! ice variables (temp. evolution)
389      IF( numout      /=  6 )   CLOSE( numout      )   ! standard model output file
390      IF( numdct_vol  /= -1 )   CLOSE( numdct_vol  )   ! volume transports
391      IF( numdct_heat /= -1 )   CLOSE( numdct_heat )   ! heat transports
392      IF( numdct_salt /= -1 )   CLOSE( numdct_salt )   ! salt transports
393
394      !
395      numout = 6                                     ! redefine numout in case it is used after this point...
396      !
397   END SUBROUTINE nemo_closefile
398
399
400   SUBROUTINE nemo_alloc
401      !!----------------------------------------------------------------------
402      !!                     ***  ROUTINE nemo_alloc  ***
403      !!
404      !! ** Purpose :   Allocate all the dynamic arrays of the OPA modules
405      !!
406      !! ** Method  :
407      !!----------------------------------------------------------------------
408      USE diawri    , ONLY: dia_wri_alloc
409      USE dom_oce   , ONLY: dom_oce_alloc
410      !
411      INTEGER :: ierr
412      !!----------------------------------------------------------------------
413      !
414      ierr =        oce_alloc       ()          ! ocean
415      ierr = ierr + dia_wri_alloc   ()
416      ierr = ierr + dom_oce_alloc   ()          ! ocean domain
417      !
418      ierr = ierr + lib_mpp_alloc   (numout)    ! mpp exchanges
419      !
420      IF( lk_mpp    )   CALL mpp_sum( ierr )
421      IF( ierr /= 0 )   CALL ctl_stop( 'STOP', 'nemo_alloc : unable to allocate standard ocean arrays' )
422      !
423   END SUBROUTINE nemo_alloc
424
425
426   SUBROUTINE nemo_partition( num_pes )
427      !!----------------------------------------------------------------------
428      !!                 ***  ROUTINE nemo_partition  ***
429      !!
430      !! ** Purpose :
431      !!
432      !! ** Method  :
433      !!----------------------------------------------------------------------
434      INTEGER, INTENT(in) :: num_pes ! The number of MPI processes we have
435      !
436      INTEGER, PARAMETER :: nfactmax = 20
437      INTEGER :: nfact ! The no. of factors returned
438      INTEGER :: ierr  ! Error flag
439      INTEGER :: ji
440      INTEGER :: idiff, mindiff, imin ! For choosing pair of factors that are closest in value
441      INTEGER, DIMENSION(nfactmax) :: ifact ! Array of factors
442      !!----------------------------------------------------------------------
443
444      ierr = 0
445
446      CALL factorise( ifact, nfactmax, nfact, num_pes, ierr )
447
448      IF( nfact <= 1 ) THEN
449         WRITE (numout, *) 'WARNING: factorisation of number of PEs failed'
450         WRITE (numout, *) '       : using grid of ',num_pes,' x 1'
451         jpnj = 1
452         jpni = num_pes
453      ELSE
454         ! Search through factors for the pair that are closest in value
455         mindiff = 1000000
456         imin    = 1
457         DO ji = 1, nfact-1, 2
458            idiff = ABS( ifact(ji) - ifact(ji+1) )
459            IF( idiff < mindiff ) THEN
460               mindiff = idiff
461               imin = ji
462            ENDIF
463         END DO
464         jpnj = ifact(imin)
465         jpni = ifact(imin + 1)
466      ENDIF
467      !
468      jpnij = jpni*jpnj
469      !
470   END SUBROUTINE nemo_partition
471
472
473   SUBROUTINE factorise( kfax, kmaxfax, knfax, kn, kerr )
474      !!----------------------------------------------------------------------
475      !!                     ***  ROUTINE factorise  ***
476      !!
477      !! ** Purpose :   return the prime factors of n.
478      !!                knfax factors are returned in array kfax which is of
479      !!                maximum dimension kmaxfax.
480      !! ** Method  :
481      !!----------------------------------------------------------------------
482      INTEGER                    , INTENT(in   ) ::   kn, kmaxfax
483      INTEGER                    , INTENT(  out) ::   kerr, knfax
484      INTEGER, DIMENSION(kmaxfax), INTENT(  out) ::   kfax
485      !
486      INTEGER :: ifac, jl, inu
487      INTEGER, PARAMETER :: ntest = 14
488      INTEGER :: ilfax(ntest)
489
490      ! lfax contains the set of allowed factors.
491      data (ilfax(jl),jl=1,ntest) / 16384, 8192, 4096, 2048, 1024, 512, 256,  &
492         &                            128,   64,   32,   16,    8,   4,   2  /
493      !!----------------------------------------------------------------------
494
495      ! Clear the error flag and initialise output vars
496      kerr = 0
497      kfax = 1
498      knfax = 0
499
500      ! Find the factors of n.
501      IF( kn == 1 )   GOTO 20
502
503      ! nu holds the unfactorised part of the number.
504      ! knfax holds the number of factors found.
505      ! l points to the allowed factor list.
506      ! ifac holds the current factor.
507
508      inu   = kn
509      knfax = 0
510
511      DO jl = ntest, 1, -1
512         !
513         ifac = ilfax(jl)
514         IF( ifac > inu )   CYCLE
515
516         ! Test whether the factor will divide.
517
518         IF( MOD(inu,ifac) == 0 ) THEN
519            !
520            knfax = knfax + 1            ! Add the factor to the list
521            IF( knfax > kmaxfax ) THEN
522               kerr = 6
523               write (*,*) 'FACTOR: insufficient space in factor array ', knfax
524               return
525            ENDIF
526            kfax(knfax) = ifac
527            ! Store the other factor that goes with this one
528            knfax = knfax + 1
529            kfax(knfax) = inu / ifac
530            !WRITE (*,*) 'ARPDBG, factors ',knfax-1,' & ',knfax,' are ', kfax(knfax-1),' and ',kfax(knfax)
531         ENDIF
532         !
533      END DO
534
535   20 CONTINUE      ! Label 20 is the exit point from the factor search loop.
536      !
537   END SUBROUTINE factorise
538
539#if defined key_mpp_mpi
540   SUBROUTINE nemo_northcomms
541      !!======================================================================
542      !!                     ***  ROUTINE  nemo_northcomms  ***
543      !! nemo_northcomms    :  Setup for north fold exchanges with explicit peer to peer messaging
544      !!=====================================================================
545      !!----------------------------------------------------------------------
546      !!
547      !! ** Purpose :   Initialization of the northern neighbours lists.
548      !!----------------------------------------------------------------------
549      !!    1.0  ! 2011-10  (A. C. Coward, NOCS & J. Donners, PRACE)
550      !!----------------------------------------------------------------------
551
552      INTEGER ::   ji, jj, jk, ij, jtyp    ! dummy loop indices
553      INTEGER ::   ijpj                    ! number of rows involved in north-fold exchange
554      INTEGER ::   northcomms_alloc        ! allocate return status
555      REAL(wp), ALLOCATABLE, DIMENSION ( :,: ) ::   znnbrs     ! workspace
556      LOGICAL,  ALLOCATABLE, DIMENSION ( : )   ::   lrankset   ! workspace
557
558      IF(lwp) WRITE(numout,*)
559      IF(lwp) WRITE(numout,*) 'nemo_northcomms : Initialization of the northern neighbours lists'
560      IF(lwp) WRITE(numout,*) '~~~~~~~~~~'
561
562      !!----------------------------------------------------------------------
563      ALLOCATE( znnbrs(jpi,jpj), stat = northcomms_alloc )
564      ALLOCATE( lrankset(jpnij), stat = northcomms_alloc )
565      IF( northcomms_alloc /= 0 ) THEN
566         WRITE(numout,cform_war)
567         WRITE(numout,*) 'northcomms_alloc : failed to allocate arrays'
568         CALL ctl_stop( 'STOP', 'nemo_northcomms : unable to allocate temporary arrays' )
569      ENDIF
570      nsndto = 0
571      isendto = -1
572      ijpj   = 4
573      !
574      ! This routine has been called because ln_nnogather has been set true ( nammpp )
575      ! However, these first few exchanges have to use the mpi_allgather method to
576      ! establish the neighbour lists to use in subsequent peer to peer exchanges.
577      ! Consequently, set l_north_nogather to be false here and set it true only after
578      ! the lists have been established.
579      !
580      l_north_nogather = .FALSE.
581      !
582      ! Exchange and store ranks on northern rows
583
584      DO jtyp = 1,4
585
586         lrankset = .FALSE.
587         znnbrs = narea
588         SELECT CASE (jtyp)
589            CASE(1)
590               CALL lbc_lnk( znnbrs, 'T', 1. )      ! Type 1: T,W-points
591            CASE(2)
592               CALL lbc_lnk( znnbrs, 'U', 1. )      ! Type 2: U-point
593            CASE(3)
594               CALL lbc_lnk( znnbrs, 'V', 1. )      ! Type 3: V-point
595            CASE(4)
596               CALL lbc_lnk( znnbrs, 'F', 1. )      ! Type 4: F-point
597         END SELECT
598
599         IF ( njmppt(narea) .EQ. MAXVAL( njmppt ) ) THEN
600            DO jj = nlcj-ijpj+1, nlcj
601               ij = jj - nlcj + ijpj
602               DO ji = 1,jpi
603                  IF ( INT(znnbrs(ji,jj)) .NE. 0 .AND. INT(znnbrs(ji,jj)) .NE. narea ) &
604               &     lrankset(INT(znnbrs(ji,jj))) = .true.
605               END DO
606            END DO
607
608            DO jj = 1,jpnij
609               IF ( lrankset(jj) ) THEN
610                  nsndto(jtyp) = nsndto(jtyp) + 1
611                  IF ( nsndto(jtyp) .GT. jpmaxngh ) THEN
612                     CALL ctl_stop( ' Too many neighbours in nemo_northcomms ', &
613                  &                 ' jpmaxngh will need to be increased ')
614                  ENDIF
615                  isendto(nsndto(jtyp),jtyp) = jj-1   ! narea converted to MPI rank
616               ENDIF
617            END DO
618         ENDIF
619
620      END DO
621
622      !
623      ! Type 5: I-point
624      !
625      ! ICE point exchanges may involve some averaging. The neighbours list is
626      ! built up using two exchanges to ensure that the whole stencil is covered.
627      ! lrankset should not be reset between these 'J' and 'K' point exchanges
628
629      jtyp = 5
630      lrankset = .FALSE.
631      znnbrs = narea
632      CALL lbc_lnk( znnbrs, 'J', 1. ) ! first ice U-V point
633
634      IF ( njmppt(narea) .EQ. MAXVAL( njmppt ) ) THEN
635         DO jj = nlcj-ijpj+1, nlcj
636            ij = jj - nlcj + ijpj
637            DO ji = 1,jpi
638               IF ( INT(znnbrs(ji,jj)) .NE. 0 .AND. INT(znnbrs(ji,jj)) .NE. narea ) &
639            &     lrankset(INT(znnbrs(ji,jj))) = .true.
640         END DO
641        END DO
642      ENDIF
643
644      znnbrs = narea
645      CALL lbc_lnk( znnbrs, 'K', 1. ) ! second ice U-V point
646
647      IF ( njmppt(narea) .EQ. MAXVAL( njmppt )) THEN
648         DO jj = nlcj-ijpj+1, nlcj
649            ij = jj - nlcj + ijpj
650            DO ji = 1,jpi
651               IF ( INT(znnbrs(ji,jj)) .NE. 0 .AND.  INT(znnbrs(ji,jj)) .NE. narea ) &
652            &       lrankset( INT(znnbrs(ji,jj))) = .true.
653            END DO
654         END DO
655
656         DO jj = 1,jpnij
657            IF ( lrankset(jj) ) THEN
658               nsndto(jtyp) = nsndto(jtyp) + 1
659               IF ( nsndto(jtyp) .GT. jpmaxngh ) THEN
660                  CALL ctl_stop( ' Too many neighbours in nemo_northcomms ', &
661               &                 ' jpmaxngh will need to be increased ')
662               ENDIF
663               isendto(nsndto(jtyp),jtyp) = jj-1   ! narea converted to MPI rank
664            ENDIF
665         END DO
666         !
667         ! For northern row areas, set l_north_nogather so that all subsequent exchanges
668         ! can use peer to peer communications at the north fold
669         !
670         l_north_nogather = .TRUE.
671         !
672      ENDIF
673      DEALLOCATE( znnbrs )
674      DEALLOCATE( lrankset )
675
676   END SUBROUTINE nemo_northcomms
677#else
678   SUBROUTINE nemo_northcomms      ! Dummy routine
679      WRITE(*,*) 'nemo_northcomms: You should not have seen this print! error?'
680   END SUBROUTINE nemo_northcomms
681#endif
682   !!======================================================================
683END MODULE nemogcm
Note: See TracBrowser for help on using the repository browser.