source: palm/trunk/SOURCE/init_pegrid.f90 @ 3057

Last change on this file since 3057 was 3057, checked in by raasch, 6 years ago

bugfix: check that nz is even in case that optimized multigrid is used

  • Property svn:keywords set to Id
File size: 51.8 KB
Line 
1!> @file init_pegrid.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2018 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: init_pegrid.f90 3057 2018-06-05 09:03:41Z raasch $
27! bugfix: check that nz is even in case that optimized multigrid is used
28!
29! 3049 2018-05-29 13:52:36Z Giersch
30! Error messages revised
31!
32! 3045 2018-05-28 07:55:41Z Giersch
33! Error messages revised
34!
35! 2938 2018-03-27 15:52:42Z suehring
36! - No checks for domain decomposition in case of turbulence generator
37!  (is done in stg module)
38! - Introduce ids to indicate lateral processors for turbulence generator
39!
40! 2936 2018-03-27 14:49:27Z suehring
41! Variable use_synthetic_turbulence_generator has been abbreviated
42!
43! 2718 2018-01-02 08:49:38Z maronga
44! Corrected "Former revisions" section
45!
46! 2696 2017-12-14 17:12:51Z kanani
47! Change in file header (GPL part)
48! 3D-Integer exchange on multigrid level (MS)
49! Forcing implemented (MS)
50!
51! 2600 2017-11-01 14:11:20Z raasch
52! calculation of block-I/O quantitites removed (is now done in parin)
53!
54! 2516 2017-10-04 11:03:04Z suehring
55! Remove tabs
56!
57! 2514 2017-10-04 09:52:37Z suehring
58! Redundant preprocessor directives removed
59!
60! 2372 2017-08-25 12:37:32Z sward
61! Shifted cyclic boundary conditions implemented
62!
63! 2365 2017-08-21 14:59:59Z kanani
64! Vertical nesting implemented (SadiqHuq)
65!
66! 2300 2017-06-29 13:31:14Z raasch
67! host-specific settings removed
68!
69! 2298 2017-06-29 09:28:18Z raasch
70! MPI2 related parts removed
71!
72! 2271 2017-06-09 12:34:55Z sward
73! Error message changed
74!
75! 2259 2017-06-08 09:09:11Z gronemeier
76! Implemented synthetic turbulence generator
77!
78! 2238 2017-05-31 16:49:16Z suehring
79! Remove unnecessary module load of pmc_interface
80!
81! 2231 2017-05-30 16:44:33Z suehring
82!
83! 2200 2017-04-11 11:37:51Z suehring
84! monotonic_adjustment removed
85!
86! 2197 2017-03-24 02:25:00Z raasch
87! bugfix: do not allow odd values for nz at the coarsest grid level in case of
88! optimized multigrid solver
89!
90! 2180 2017-03-17 13:33:05Z hellstea
91! Checks to ensure (2178) that pdims match the grid dimensions in the
92! automatic determination of pdims are canceled as unnecessary
93!
94! 2178 2017-03-17 11:07:39Z hellstea
95! Checks to ensure that pdims match the grid dimensions are added in the
96! automatic determination of pdims
97!
98! 2050 2016-11-08 15:00:55Z gronemeier
99! Implement turbulent outflow condition
100!
101! 2000 2016-08-20 18:09:15Z knoop
102! Forced header and separation lines into 80 columns
103!
104! 1968 2016-07-18 12:01:49Z suehring
105! Extent MPI-datatypes for exchange of 2D-INTEGER arrays on coarser multigrid
106! level 
107!
108! 1964 2016-07-14 15:35:18Z hellstea
109! Bugfix: erroneous setting of nest_bound_l/r/s/n = .TRUE. for vertical nesting mode removed.
110!
111! 1923 2016-05-31 16:37:07Z boeske
112! Initial version of purely vertical nesting introduced.
113!
114! 1922 2016-05-31 16:36:08Z boeske
115! Bugfix: array transposition checks restricted to cases if a fourier
116! transform is used , removed unused variable nnx_z
117!
118! 1833 2016-04-07 14:23:03Z raasch
119! spectra related variables moved to spectra_mod
120!
121! 1815 2016-04-06 13:49:59Z raasch
122! cpp-directives for intel openmp bug removed
123!
124! 1804 2016-04-05 16:30:18Z maronga
125! Removed code for parameter file check (__check)
126!
127! 1779 2016-03-03 08:01:28Z raasch
128! changes regarding nested domain removed: virtual PE grid will be automatically
129! calculated for nested runs too
130!
131! 1764 2016-02-28 12:45:19Z raasch
132! cpp-statements for nesting removed
133!
134! 1762 2016-02-25 12:31:13Z hellstea
135! Introduction of nested domain feature
136!
137! 1682 2015-10-07 23:56:08Z knoop
138! Code annotations made doxygen readable
139!
140! 1677 2015-10-02 13:25:23Z boeske
141! New MPI-data types for exchange of 3D integer arrays.
142!
143! 1575 2015-03-27 09:56:27Z raasch
144! adjustments for psolver-queries, calculation of ngp_xz added
145!
146! 1565 2015-03-09 20:59:31Z suehring
147! Refine if-clause for setting nbgp.
148!
149! 1557 2015-03-05 16:43:04Z suehring
150! Adjustment for monotonic limiter
151!
152! 1468 2014-09-24 14:06:57Z maronga
153! Adapted for use on up to 6-digit processor cores
154!
155! 1435 2014-07-21 10:37:02Z keck
156! bugfix: added missing parameter coupling_mode_remote to ONLY-attribute
157!
158! 1402 2014-05-09 14:25:13Z raasch
159! location messages modified
160!
161! 1384 2014-05-02 14:31:06Z raasch
162! location messages added
163!
164! 1353 2014-04-08 15:21:23Z heinze
165! REAL constants provided with KIND-attribute
166!
167! 1322 2014-03-20 16:38:49Z raasch
168! REAL functions provided with KIND-attribute
169!
170! 1320 2014-03-20 08:40:49Z raasch
171! ONLY-attribute added to USE-statements,
172! kind-parameters added to all INTEGER and REAL declaration statements,
173! kinds are defined in new module kinds,
174! revision history before 2012 removed,
175! comment fields (!:) to be used for variable explanations added to
176! all variable declaration statements
177!
178! 1304 2014-03-12 10:29:42Z raasch
179! bugfix: single core MPI runs missed some settings of transpose indices
180!
181! 1212 2013-08-15 08:46:27Z raasch
182! error message for poisfft_hybrid removed
183!
184! 1159 2013-05-21 11:58:22Z fricke
185! dirichlet/neumann and neumann/dirichlet removed
186!
187! 1139 2013-04-18 07:25:03Z raasch
188! bugfix for calculating the id of the PE carrying the recycling plane
189!
190! 1111 2013-03-08 23:54:10Z raasch
191! initialization of poisfft moved to module poisfft
192!
193! 1092 2013-02-02 11:24:22Z raasch
194! unused variables removed
195!
196! 1056 2012-11-16 15:28:04Z raasch
197! Indices for arrays n.._mg start from zero due to definition of arrays f2 and
198! p2 as automatic arrays in recursive subroutine next_mg_level
199!
200! 1041 2012-11-06 02:36:29Z raasch
201! a 2d virtual processor topology is used by default for all machines
202!
203! 1036 2012-10-22 13:43:42Z raasch
204! code put under GPL (PALM 3.9)
205!
206! 1003 2012-09-14 14:35:53Z raasch
207! subdomains must have identical size (grid matching = "match" removed)
208!
209! 1001 2012-09-13 14:08:46Z raasch
210! all actions concerning upstream-spline-method removed
211!
212! 978 2012-08-09 08:28:32Z fricke
213! dirichlet/neumann and neumann/dirichlet added
214! nxlu and nysv are also calculated for inflow boundary
215!
216! 809 2012-01-30 13:32:58Z maronga
217! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
218!
219! 807 2012-01-25 11:53:51Z maronga
220! New cpp directive "__check" implemented which is used by check_namelist_files
221!
222! Revision 1.1  1997/07/24 11:15:09  raasch
223! Initial revision
224!
225!
226! Description:
227! ------------
228!> Determination of the virtual processor topology (if not prescribed by the
229!> user)and computation of the grid point number and array bounds of the local
230!> domains.
231!> @todo: remove MPI-data types for 2D exchange on coarse multigrid level (not
232!>        used any more)
233!------------------------------------------------------------------------------!
234 SUBROUTINE init_pegrid
235 
236
237    USE control_parameters,                                                    &
238        ONLY:  bc_lr, bc_ns, coupling_mode, coupling_mode_remote,              &
239               coupling_topology, force_bound_l, force_bound_n, force_bound_r, &
240               force_bound_s, gathered_size, grid_level,                       &
241               grid_level_count, inflow_l, inflow_n, inflow_r, inflow_s,       &
242               maximum_grid_level, message_string,                             &
243               mg_switch_to_pe0_level, momentum_advec, nest_bound_l,           &
244               nest_bound_n, nest_bound_r, nest_bound_s, nest_domain, neutral, &
245               psolver, outflow_l, outflow_n, outflow_r, outflow_s,            &
246               outflow_source_plane, recycling_width, scalar_advec,            &
247               subdomain_size, turbulent_outflow, y_shift
248
249    USE grid_variables,                                                        &
250        ONLY:  dx
251       
252    USE indices,                                                               &
253        ONLY:  mg_loc_ind, nbgp, nnx, nny, nnz, nx, nx_a, nx_o, nxl, nxl_mg,   &
254               nxlu, nxr, nxr_mg, ny, ny_a, ny_o, nyn, nyn_mg, nys, nys_mg,    &
255               nysv, nz, nzb, nzt, nzt_mg, wall_flags_1, wall_flags_2,         &
256               wall_flags_3, wall_flags_4, wall_flags_5, wall_flags_6,         &
257               wall_flags_7, wall_flags_8, wall_flags_9, wall_flags_10
258
259    USE kinds
260     
261    USE pegrid
262     
263    USE spectra_mod,                                                           &
264        ONLY:  calculate_spectra, dt_dosp
265
266    USE synthetic_turbulence_generator_mod,                                    &
267        ONLY:  id_stg_left, id_stg_north, id_stg_right, id_stg_south,          &
268               use_syn_turb_gen
269
270    USE transpose_indices,                                                     &
271        ONLY:  nxl_y, nxl_yd, nxl_z, nxr_y, nxr_yd, nxr_z, nyn_x, nyn_z, nys_x,&
272               nys_z, nzb_x, nzb_y, nzb_yd, nzt_x, nzt_yd, nzt_y
273
274    USE vertical_nesting_mod,                                                  &
275        ONLY:  vnested, vnest_init_pegrid_domain, vnest_init_pegrid_rank
276
277    IMPLICIT NONE
278
279    INTEGER(iwp) ::  i                        !<
280    INTEGER(iwp) ::  id_inflow_l              !<
281    INTEGER(iwp) ::  id_outflow_l             !< local value of id_outflow
282    INTEGER(iwp) ::  id_outflow_source_l      !< local value of id_outflow_source
283    INTEGER(iwp) ::  id_recycling_l           !<
284    INTEGER(iwp) ::  id_stg_left_l            !< left lateral boundary local core id in case of turbulence generator 
285    INTEGER(iwp) ::  id_stg_north_l           !< north lateral boundary local core id in case of turbulence generator 
286    INTEGER(iwp) ::  id_stg_right_l           !< right lateral boundary local core id in case of turbulence generator 
287    INTEGER(iwp) ::  id_stg_south_l           !< south lateral boundary local core id in case of turbulence generator 
288    INTEGER(iwp) ::  ind(5)                   !<
289    INTEGER(iwp) ::  j                        !<
290    INTEGER(iwp) ::  k                        !<
291    INTEGER(iwp) ::  maximum_grid_level_l     !<
292    INTEGER(iwp) ::  mg_levels_x              !<
293    INTEGER(iwp) ::  mg_levels_y              !<
294    INTEGER(iwp) ::  mg_levels_z              !<
295    INTEGER(iwp) ::  mg_switch_to_pe0_level_l !<
296    INTEGER(iwp) ::  nnx_y                    !<
297    INTEGER(iwp) ::  nnx_z                    !<
298    INTEGER(iwp) ::  nny_x                    !<
299    INTEGER(iwp) ::  nny_z                    !<
300    INTEGER(iwp) ::  nnz_x                    !<
301    INTEGER(iwp) ::  nnz_y                    !<
302    INTEGER(iwp) ::  numproc_sqr              !<
303    INTEGER(iwp) ::  nxl_l                    !<
304    INTEGER(iwp) ::  nxr_l                    !<
305    INTEGER(iwp) ::  nyn_l                    !<
306    INTEGER(iwp) ::  nys_l                    !<
307    INTEGER(iwp) ::  nzb_l                    !<
308    INTEGER(iwp) ::  nzt_l                    !<
309    INTEGER(iwp) ::  omp_get_num_threads      !<
310
311    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ind_all !<
312    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxlf    !<
313    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nxrf    !<
314    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nynf    !<
315    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  nysf    !<
316
317    INTEGER(iwp), DIMENSION(2) ::  pdims_remote         !<
318    INTEGER(iwp)               ::  lcoord(2)            !< PE coordinates of left neighbor along x and y
319    INTEGER(iwp)               ::  rcoord(2)            !< PE coordinates of right neighbor along x and y
320
321!
322!-- Get the number of OpenMP threads
323    !$OMP PARALLEL
324!$  threads_per_task = omp_get_num_threads()
325    !$OMP END PARALLEL
326
327
328#if defined( __parallel )
329
330    CALL location_message( 'creating virtual PE grids + MPI derived data types', &
331                           .FALSE. )
332
333!
334!-- Determine the processor topology or check it, if prescribed by the user
335    IF ( npex == -1  .AND.  npey == -1 )  THEN
336
337!
338!--    Automatic determination of the topology
339       numproc_sqr = SQRT( REAL( numprocs, KIND=wp ) )
340       pdims(1)    = MAX( numproc_sqr , 1 )
341       DO  WHILE ( MOD( numprocs , pdims(1) ) /= 0 )
342          pdims(1) = pdims(1) - 1
343       ENDDO
344       pdims(2) = numprocs / pdims(1)
345
346    ELSEIF ( npex /= -1  .AND.  npey /= -1 )  THEN
347
348!
349!--    Prescribed by user. Number of processors on the prescribed topology
350!--    must be equal to the number of PEs available to the job
351       IF ( ( npex * npey ) /= numprocs )  THEN
352          WRITE( message_string, * ) 'number of PEs of the prescribed ',       &
353              'topology (', npex*npey,') does not match & the number of ',     &
354              'PEs available to the job (', numprocs, ')'
355          CALL message( 'init_pegrid', 'PA0221', 1, 2, 0, 6, 0 )
356       ENDIF
357       pdims(1) = npex
358       pdims(2) = npey
359
360    ELSE
361!
362!--    If the processor topology is prescribed by the user, the number of
363!--    PEs must be given in both directions
364       message_string = 'if the processor topology is prescribed by th' //     &
365                'e user & both values of "npex" and "npey" must be given' //   &
366                ' in the &NAMELIST-parameter file'
367       CALL message( 'init_pegrid', 'PA0222', 1, 2, 0, 6, 0 )
368
369    ENDIF
370
371!
372!-- If necessary, set horizontal boundary conditions to non-cyclic
373    IF ( bc_lr /= 'cyclic' )  cyclic(1) = .FALSE.
374    IF ( bc_ns /= 'cyclic' )  cyclic(2) = .FALSE.
375
376
377!
378!-- Create the virtual processor grid
379    CALL MPI_CART_CREATE( comm_palm, ndim, pdims, cyclic, reorder, &
380                          comm2d, ierr )
381    CALL MPI_COMM_RANK( comm2d, myid, ierr )
382    WRITE (myid_char,'(''_'',I6.6)')  myid
383
384    CALL MPI_CART_COORDS( comm2d, myid, ndim, pcoord, ierr )
385    CALL MPI_CART_SHIFT( comm2d, 0, 1, pleft, pright, ierr )
386    CALL MPI_CART_SHIFT( comm2d, 1, 1, psouth, pnorth, ierr )
387!
388!-- In case of cyclic boundary conditions, a y-shift at the boundaries in
389!-- x-direction can be introduced via parameter y_shift. The shift is done
390!-- by modifying the processor grid in such a way that processors located
391!-- at the x-boundary communicate across it to processors with y-coordinate
392!-- shifted by y_shift relative to their own. This feature can not be used
393!-- in combination with an fft pressure solver. It has been implemented to
394!-- counter the effect of streak structures in case of cyclic boundary
395!-- conditions. For a description of these see Munters
396!-- (2016; dx.doi.org/10.1063/1.4941912)
397!--
398!-- Get coordinates of left and right neighbor on PE grid
399    IF ( y_shift /= 0 ) THEN
400
401       IF ( bc_lr /= 'cyclic'  .OR.  bc_ns /= 'cyclic' )  THEN
402          message_string = 'y_shift /= 0 is only allowed for cyclic ' //       &
403                           'boundary conditions in both directions '
404          CALL message( 'check_parameters', 'PA0467', 1, 2, 0, 6, 0 )
405       ENDIF
406       IF ( TRIM( psolver ) /= 'multigrid' .AND.                               &
407            TRIM( psolver ) /= 'multigrid_noopt')                              &
408       THEN
409          message_string = 'y_shift /= 0 requires a multigrid pressure solver '
410          CALL message( 'check_parameters', 'PA0468', 1, 2, 0, 6, 0 )
411       ENDIF
412
413       CALL MPI_CART_COORDS( comm2d, pright, ndim, rcoord, ierr )
414       CALL MPI_CART_COORDS( comm2d, pleft, ndim, lcoord, ierr )
415
416!
417!--    If the x(y)-coordinate of the right (left) neighbor is smaller (greater)
418!--    than that of the calling process, then the calling process is located on
419!--    the right (left) boundary of the processor grid. In that case,
420!--    the y-coordinate of that neighbor is increased (decreased) by y_shift.
421!--    The rank of the process with that coordinate is then inquired and the
422!--    neighbor rank for MPI_SENDRECV, pright (pleft) is set to it.
423!--    In this way, the calling process receives a new right (left) neighbor
424!--    for all future MPI_SENDRECV calls. That neighbor has a y-coordinate
425!--    of y+(-)y_shift, where y is the original right (left) neighbor's
426!--    y-coordinate. The modulo-operation ensures that if the neighbor's
427!--    y-coordinate exceeds the grid-boundary, it will be relocated to
428!--    the opposite part of the grid cyclicly.
429       IF ( rcoord(1) < pcoord(1) ) THEN
430          rcoord(2) = MODULO( rcoord(2) + y_shift, pdims(2) )
431          CALL MPI_CART_RANK( comm2d, rcoord, pright, ierr )
432       ENDIF
433
434       IF ( lcoord(1) > pcoord(1) ) THEN
435          lcoord(2) = MODULO( lcoord(2) - y_shift, pdims(2) )
436          CALL MPI_CART_RANK( comm2d, lcoord, pleft, ierr )
437       ENDIF
438    ENDIF
439!
440!-- Vertical nesting: store four lists that identify partner ranks to exchange
441!-- data
442    IF ( vnested )  CALL vnest_init_pegrid_rank
443
444!
445!-- Determine sub-topologies for transpositions
446!-- Transposition from z to x:
447    remain_dims(1) = .TRUE.
448    remain_dims(2) = .FALSE.
449    CALL MPI_CART_SUB( comm2d, remain_dims, comm1dx, ierr )
450    CALL MPI_COMM_RANK( comm1dx, myidx, ierr )
451!
452!-- Transposition from x to y
453    remain_dims(1) = .FALSE.
454    remain_dims(2) = .TRUE.
455    CALL MPI_CART_SUB( comm2d, remain_dims, comm1dy, ierr )
456    CALL MPI_COMM_RANK( comm1dy, myidy, ierr )
457
458
459!
460!-- Calculate array bounds along x-direction for every PE.
461    ALLOCATE( nxlf(0:pdims(1)-1), nxrf(0:pdims(1)-1), nynf(0:pdims(2)-1),      &
462              nysf(0:pdims(2)-1) )
463
464    IF ( MOD( nx+1 , pdims(1) ) /= 0 )  THEN
465       WRITE( message_string, * ) 'x-direction: gridpoint number (',nx+1,') ', &
466                               'is not an& integral divisor of the number ',    &
467                               'of processors (', pdims(1),')'
468       CALL message( 'init_pegrid', 'PA0225', 1, 2, 0, 6, 0 )
469    ELSE
470       nnx  = ( nx + 1 ) / pdims(1)
471    ENDIF   
472
473!
474!-- Left and right array bounds, number of gridpoints
475    DO  i = 0, pdims(1)-1
476       nxlf(i)   = i * nnx
477       nxrf(i)   = ( i + 1 ) * nnx - 1
478    ENDDO
479
480!
481!-- Calculate array bounds in y-direction for every PE.
482    IF ( MOD( ny+1 , pdims(2) ) /= 0 )  THEN
483       WRITE( message_string, * ) 'y-direction: gridpoint number (',ny+1,') ', &
484                           'is not an& integral divisor of the number of',      &
485                           'processors (', pdims(2),')'
486       CALL message( 'init_pegrid', 'PA0227', 1, 2, 0, 6, 0 )
487    ELSE
488       nny  = ( ny + 1 ) / pdims(2)
489    ENDIF   
490
491!
492!-- South and north array bounds
493    DO  j = 0, pdims(2)-1
494       nysf(j)   = j * nny
495       nynf(j)   = ( j + 1 ) * nny - 1
496    ENDDO
497
498!
499!-- Local array bounds of the respective PEs
500    nxl = nxlf(pcoord(1))
501    nxr = nxrf(pcoord(1))
502    nys = nysf(pcoord(2))
503    nyn = nynf(pcoord(2))
504    nzb = 0
505    nzt = nz
506    nnz = nz
507
508!
509!-- Set switches to define if the PE is situated at the border of the virtual
510!-- processor grid
511    IF ( nxl == 0 )   left_border_pe  = .TRUE.
512    IF ( nxr == nx )  right_border_pe = .TRUE.
513    IF ( nys == 0 )   south_border_pe = .TRUE.
514    IF ( nyn == ny )  north_border_pe = .TRUE.
515
516!
517!-- Calculate array bounds and gridpoint numbers for the transposed arrays
518!-- (needed in the pressure solver)
519!-- For the transposed arrays, cyclic boundaries as well as top and bottom
520!-- boundaries are omitted, because they are obstructive to the transposition
521
522!
523!-- 1. transposition  z --> x
524!-- This transposition is not neccessary in case of a 1d-decomposition along x
525    IF ( psolver == 'poisfft'  .OR.  calculate_spectra )  THEN
526
527       IF ( pdims(2) /= 1 )  THEN
528          IF ( MOD( nz , pdims(1) ) /= 0 )  THEN
529             WRITE( message_string, * ) 'transposition z --> x:',              &
530                       '& nz=',nz,' is not an integral divisior of pdims(1)=', &
531                                                                   pdims(1)
532             CALL message( 'init_pegrid', 'PA0230', 1, 2, 0, 6, 0 )
533          ENDIF
534       ENDIF
535
536       nys_x = nys
537       nyn_x = nyn
538       nny_x = nny
539       nnz_x = nz / pdims(1)
540       nzb_x = 1 + myidx * nnz_x
541       nzt_x = ( myidx + 1 ) * nnz_x
542       sendrecvcount_zx = nnx * nny * nnz_x
543
544    ENDIF
545
546
547    IF ( psolver == 'poisfft' )  THEN 
548!
549!--    2. transposition  x --> y
550       IF ( MOD( nx+1 , pdims(2) ) /= 0 )  THEN
551          WRITE( message_string, * ) 'transposition x --> y:',                 &
552                            '& nx+1=',nx+1,' is not an integral divisor of ',  &
553                            'pdims(2)=',pdims(2)
554          CALL message( 'init_pegrid', 'PA0231', 1, 2, 0, 6, 0 )
555       ENDIF
556
557       nnz_y = nnz_x
558       nzb_y = nzb_x
559       nzt_y = nzt_x
560       nnx_y = (nx+1) / pdims(2)
561       nxl_y = myidy * nnx_y
562       nxr_y = ( myidy + 1 ) * nnx_y - 1
563       sendrecvcount_xy = nnx_y * nny_x * nnz_y
564!
565!--    3. transposition  y --> z 
566!--    (ELSE:  x --> y  in case of 1D-decomposition along x)
567       nxl_z = nxl_y
568       nxr_z = nxr_y
569       nny_z = (ny+1) / pdims(1)
570       nys_z = myidx * nny_z
571       nyn_z = ( myidx + 1 ) * nny_z - 1
572       sendrecvcount_yz = nnx_y * nny_z * nnz_y
573
574       IF ( pdims(2) /= 1 )  THEN
575!
576!--       y --> z
577!--       This transposition is not neccessary in case of a 1d-decomposition
578!--       along x, except that the uptream-spline method is switched on
579          IF ( MOD( ny+1 , pdims(1) ) /= 0 )  THEN
580             WRITE( message_string, * ) 'transposition y --> z:',              &
581                               '& ny+1=',ny+1,' is not an integral divisor of',&
582                               ' pdims(1)=',pdims(1)
583             CALL message( 'init_pegrid', 'PA0232', 1, 2, 0, 6, 0 )
584          ENDIF
585
586       ELSE
587!
588!--       x --> y
589!--       This condition must be fulfilled for a 1D-decomposition along x
590          IF ( MOD( ny+1 , pdims(1) ) /= 0 )  THEN
591             WRITE( message_string, * ) 'transposition x --> y:',              &
592                               '& ny+1=',ny+1,' is not an integral divisor of',&
593                               ' pdims(1)=',pdims(1)
594             CALL message( 'init_pegrid', 'PA0233', 1, 2, 0, 6, 0 )
595          ENDIF
596
597       ENDIF
598
599    ENDIF
600
601!
602!-- Indices for direct transpositions z --> y (used for calculating spectra)
603    IF ( calculate_spectra )  THEN
604       IF ( MOD( nz, pdims(2) ) /= 0 )  THEN
605          WRITE( message_string, * ) 'direct transposition z --> y (needed ',  &
606                    'for spectra): nz=',nz,' is not an integral divisor of ',  &
607                    'pdims(2)=',pdims(2)
608          CALL message( 'init_pegrid', 'PA0234', 1, 2, 0, 6, 0 )
609       ELSE
610          nxl_yd = nxl
611          nxr_yd = nxr
612          nzb_yd = 1 + myidy * ( nz / pdims(2) )
613          nzt_yd = ( myidy + 1 ) * ( nz / pdims(2) )
614          sendrecvcount_zyd = nnx * nny * ( nz / pdims(2) )
615       ENDIF
616    ENDIF
617
618    IF ( psolver == 'poisfft'  .OR.  calculate_spectra )  THEN
619!
620!--    Indices for direct transpositions y --> x
621!--    (they are only possible in case of a 1d-decomposition along x)
622       IF ( pdims(2) == 1 )  THEN
623          nny_x = nny / pdims(1)
624          nys_x = myid * nny_x
625          nyn_x = ( myid + 1 ) * nny_x - 1
626          nzb_x = 1
627          nzt_x = nz
628          sendrecvcount_xy = nnx * nny_x * nz
629       ENDIF
630
631    ENDIF
632
633    IF ( psolver == 'poisfft' )  THEN
634!
635!--    Indices for direct transpositions x --> y
636!--    (they are only possible in case of a 1d-decomposition along y)
637       IF ( pdims(1) == 1 )  THEN
638          nnx_y = nnx / pdims(2)
639          nxl_y = myid * nnx_y
640          nxr_y = ( myid + 1 ) * nnx_y - 1
641          nzb_y = 1
642          nzt_y = nz
643          sendrecvcount_xy = nnx_y * nny * nz
644       ENDIF
645
646    ENDIF
647
648!
649!-- Arrays for storing the array bounds are needed any more
650    DEALLOCATE( nxlf , nxrf , nynf , nysf )
651
652
653!
654!-- Collect index bounds from other PEs (to be written to restart file later)
655    ALLOCATE( hor_index_bounds(4,0:numprocs-1) )
656
657    IF ( myid == 0 )  THEN
658
659       hor_index_bounds(1,0) = nxl
660       hor_index_bounds(2,0) = nxr
661       hor_index_bounds(3,0) = nys
662       hor_index_bounds(4,0) = nyn
663
664!
665!--    Receive data from all other PEs
666       DO  i = 1, numprocs-1
667          CALL MPI_RECV( ibuf, 4, MPI_INTEGER, i, MPI_ANY_TAG, comm2d, status, &
668                         ierr )
669          hor_index_bounds(:,i) = ibuf(1:4)
670       ENDDO
671
672    ELSE
673!
674!--    Send index bounds to PE0
675       ibuf(1) = nxl
676       ibuf(2) = nxr
677       ibuf(3) = nys
678       ibuf(4) = nyn
679       CALL MPI_SEND( ibuf, 4, MPI_INTEGER, 0, myid, comm2d, ierr )
680
681    ENDIF
682
683
684#if defined( __print )
685!
686!-- Control output
687    IF ( myid == 0 )  THEN
688       PRINT*, '*** processor topology ***'
689       PRINT*, ' '
690       PRINT*, 'myid   pcoord    left right  south north  idx idy   nxl: nxr',&
691               &'   nys: nyn'
692       PRINT*, '------------------------------------------------------------',&
693               &'-----------'
694       WRITE (*,1000)  0, pcoord(1), pcoord(2), pleft, pright, psouth, pnorth, &
695                       myidx, myidy, nxl, nxr, nys, nyn
6961000   FORMAT (I4,2X,'(',I3,',',I3,')',3X,I4,2X,I4,3X,I4,2X,I4,2X,I3,1X,I3, &
697               2(2X,I4,':',I4))
698
699!
700!--    Receive data from the other PEs
701       DO  i = 1,numprocs-1
702          CALL MPI_RECV( ibuf, 12, MPI_INTEGER, i, MPI_ANY_TAG, comm2d, status, &
703                         ierr )
704          WRITE (*,1000)  i, ( ibuf(j) , j = 1,12 )
705       ENDDO
706    ELSE
707
708!
709!--    Send data to PE0
710       ibuf(1) = pcoord(1); ibuf(2) = pcoord(2); ibuf(3) = pleft
711       ibuf(4) = pright; ibuf(5) = psouth; ibuf(6) = pnorth; ibuf(7) = myidx
712       ibuf(8) = myidy; ibuf(9) = nxl; ibuf(10) = nxr; ibuf(11) = nys
713       ibuf(12) = nyn
714       CALL MPI_SEND( ibuf, 12, MPI_INTEGER, 0, myid, comm2d, ierr )       
715    ENDIF
716#endif
717
718!
719!-- Determine the number of ghost point layers
720    IF ( ( scalar_advec == 'ws-scheme' .AND. .NOT. neutral ) .OR.             &
721         momentum_advec == 'ws-scheme' )  THEN
722       nbgp = 3
723    ELSE
724       nbgp = 1
725    ENDIF 
726
727!
728!-- Create a new MPI derived datatype for the exchange of surface (xy) data,
729!-- which is needed for coupled atmosphere-ocean runs.
730!-- First, calculate number of grid points of an xy-plane.
731    ngp_xy  = ( nxr - nxl + 1 + 2 * nbgp ) * ( nyn - nys + 1 + 2 * nbgp )
732    CALL MPI_TYPE_VECTOR( ngp_xy, 1, nzt-nzb+2, MPI_REAL, type_xy, ierr )
733    CALL MPI_TYPE_COMMIT( type_xy, ierr )
734
735    IF ( TRIM( coupling_mode ) /= 'uncoupled' .AND. .NOT. vnested )  THEN
736   
737!
738!--    Pass the number of grid points of the atmosphere model to
739!--    the ocean model and vice versa
740       IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
741
742          nx_a = nx
743          ny_a = ny
744
745          IF ( myid == 0 )  THEN
746
747             CALL MPI_SEND( nx_a, 1, MPI_INTEGER, numprocs, 1, comm_inter,  &
748                            ierr )
749             CALL MPI_SEND( ny_a, 1, MPI_INTEGER, numprocs, 2, comm_inter,  &
750                            ierr )
751             CALL MPI_SEND( pdims, 2, MPI_INTEGER, numprocs, 3, comm_inter, &
752                            ierr )
753             CALL MPI_RECV( nx_o, 1, MPI_INTEGER, numprocs, 4, comm_inter,  &
754                            status, ierr )
755             CALL MPI_RECV( ny_o, 1, MPI_INTEGER, numprocs, 5, comm_inter,  &
756                            status, ierr )
757             CALL MPI_RECV( pdims_remote, 2, MPI_INTEGER, numprocs, 6,      &
758                            comm_inter, status, ierr )
759          ENDIF
760
761          CALL MPI_BCAST( nx_o, 1, MPI_INTEGER, 0, comm2d, ierr )
762          CALL MPI_BCAST( ny_o, 1, MPI_INTEGER, 0, comm2d, ierr ) 
763          CALL MPI_BCAST( pdims_remote, 2, MPI_INTEGER, 0, comm2d, ierr )
764       
765       ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
766
767          nx_o = nx
768          ny_o = ny 
769
770          IF ( myid == 0 ) THEN
771
772             CALL MPI_RECV( nx_a, 1, MPI_INTEGER, 0, 1, comm_inter, status, &
773                            ierr )
774             CALL MPI_RECV( ny_a, 1, MPI_INTEGER, 0, 2, comm_inter, status, &
775                            ierr )
776             CALL MPI_RECV( pdims_remote, 2, MPI_INTEGER, 0, 3, comm_inter, &
777                            status, ierr )
778             CALL MPI_SEND( nx_o, 1, MPI_INTEGER, 0, 4, comm_inter, ierr )
779             CALL MPI_SEND( ny_o, 1, MPI_INTEGER, 0, 5, comm_inter, ierr )
780             CALL MPI_SEND( pdims, 2, MPI_INTEGER, 0, 6, comm_inter, ierr )
781          ENDIF
782
783          CALL MPI_BCAST( nx_a, 1, MPI_INTEGER, 0, comm2d, ierr)
784          CALL MPI_BCAST( ny_a, 1, MPI_INTEGER, 0, comm2d, ierr) 
785          CALL MPI_BCAST( pdims_remote, 2, MPI_INTEGER, 0, comm2d, ierr) 
786
787       ENDIF
788 
789       ngp_a = ( nx_a+1 + 2 * nbgp ) * ( ny_a+1 + 2 * nbgp )
790       ngp_o = ( nx_o+1 + 2 * nbgp ) * ( ny_o+1 + 2 * nbgp )
791
792!
793!--    Determine if the horizontal grid and the number of PEs in ocean and
794!--    atmosphere is same or not
795       IF ( nx_o == nx_a  .AND.  ny_o == ny_a  .AND.  &
796            pdims(1) == pdims_remote(1) .AND. pdims(2) == pdims_remote(2) ) &
797       THEN
798          coupling_topology = 0
799       ELSE
800          coupling_topology = 1
801       ENDIF 
802
803!
804!--    Determine the target PEs for the exchange between ocean and
805!--    atmosphere (comm2d)
806       IF ( coupling_topology == 0 )  THEN
807!
808!--       In case of identical topologies, every atmosphere PE has exactly one
809!--       ocean PE counterpart and vice versa
810          IF ( TRIM( coupling_mode ) == 'atmosphere_to_ocean' ) THEN
811             target_id = myid + numprocs
812          ELSE
813             target_id = myid 
814          ENDIF
815
816       ELSE
817!
818!--       In case of nonequivalent topology in ocean and atmosphere only for
819!--       PE0 in ocean and PE0 in atmosphere a target_id is needed, since
820!--       data echxchange between ocean and atmosphere will be done only
821!--       between these PEs.   
822          IF ( myid == 0 )  THEN
823
824             IF ( TRIM( coupling_mode ) == 'atmosphere_to_ocean' )  THEN
825                target_id = numprocs 
826             ELSE
827                target_id = 0
828             ENDIF
829
830          ENDIF
831
832       ENDIF
833
834    ENDIF
835
836!
837!-- Store partner grid point co-ordinates as lists.
838!-- Create custom MPI vector datatypes for contiguous data transfer
839    IF ( vnested )  CALL vnest_init_pegrid_domain
840
841#else
842
843!
844!-- Array bounds when running on a single PE (respectively a non-parallel
845!-- machine)
846    nxl = 0
847    nxr = nx
848    nnx = nxr - nxl + 1
849    nys = 0
850    nyn = ny
851    nny = nyn - nys + 1
852    nzb = 0
853    nzt = nz
854    nnz = nz
855
856    ALLOCATE( hor_index_bounds(4,0:0) )
857    hor_index_bounds(1,0) = nxl
858    hor_index_bounds(2,0) = nxr
859    hor_index_bounds(3,0) = nys
860    hor_index_bounds(4,0) = nyn
861
862!
863!-- Array bounds for the pressure solver (in the parallel code, these bounds
864!-- are the ones for the transposed arrays)
865    nys_x = nys
866    nyn_x = nyn
867    nzb_x = nzb + 1
868    nzt_x = nzt
869
870    nxl_y = nxl
871    nxr_y = nxr
872    nzb_y = nzb + 1
873    nzt_y = nzt
874
875    nxl_z = nxl
876    nxr_z = nxr
877    nys_z = nys
878    nyn_z = nyn
879
880#endif
881
882!
883!-- Calculate number of grid levels necessary for the multigrid poisson solver
884!-- as well as the gridpoint indices on each level
885    IF ( psolver(1:9) == 'multigrid' )  THEN
886
887!
888!--    First calculate number of possible grid levels for the subdomains
889       mg_levels_x = 1
890       mg_levels_y = 1
891       mg_levels_z = 1
892
893       i = nnx
894       DO WHILE ( MOD( i, 2 ) == 0  .AND.  i /= 2 )
895          i = i / 2
896          mg_levels_x = mg_levels_x + 1
897       ENDDO
898
899       j = nny
900       DO WHILE ( MOD( j, 2 ) == 0  .AND.  j /= 2 )
901          j = j / 2
902          mg_levels_y = mg_levels_y + 1
903       ENDDO
904
905       k = nz    ! do not use nnz because it might be > nz due to transposition
906                 ! requirements
907       DO WHILE ( MOD( k, 2 ) == 0  .AND.  k /= 2 )
908          k = k / 2
909          mg_levels_z = mg_levels_z + 1
910       ENDDO
911!
912!--    The optimized MG-solver does not allow odd values for nz at the coarsest
913!--    grid level
914       IF ( TRIM( psolver ) /= 'multigrid_noopt' )  THEN
915          IF ( MOD( k, 2 ) /= 0 )  mg_levels_z = mg_levels_z - 1
916!
917!--       An odd value of nz does not work. The finest level must have an even
918!--       value.
919          IF (  mg_levels_z == 0 )  THEN
920             message_string = 'optimized multigrid method requires nz to be even'
921             CALL message( 'init_pegrid', 'PA0028', 1, 2, 0, 6, 0 )
922          ENDIF
923       ENDIF
924
925       maximum_grid_level = MIN( mg_levels_x, mg_levels_y, mg_levels_z )
926
927!
928!--    Find out, if the total domain allows more levels. These additional
929!--    levels are identically processed on all PEs.
930       IF ( numprocs > 1  .AND.  mg_switch_to_pe0_level /= -1 )  THEN
931
932          IF ( mg_levels_z > MIN( mg_levels_x, mg_levels_y ) )  THEN
933
934             mg_switch_to_pe0_level_l = maximum_grid_level
935
936             mg_levels_x = 1
937             mg_levels_y = 1
938
939             i = nx+1
940             DO WHILE ( MOD( i, 2 ) == 0  .AND.  i /= 2 )
941                i = i / 2
942                mg_levels_x = mg_levels_x + 1
943             ENDDO
944
945             j = ny+1
946             DO WHILE ( MOD( j, 2 ) == 0  .AND.  j /= 2 )
947                j = j / 2
948                mg_levels_y = mg_levels_y + 1
949             ENDDO
950
951             maximum_grid_level_l = MIN( mg_levels_x, mg_levels_y, mg_levels_z )
952
953             IF ( maximum_grid_level_l > mg_switch_to_pe0_level_l )  THEN
954                mg_switch_to_pe0_level_l = maximum_grid_level_l - &
955                                           mg_switch_to_pe0_level_l + 1
956             ELSE
957                mg_switch_to_pe0_level_l = 0
958             ENDIF
959
960          ELSE
961
962             mg_switch_to_pe0_level_l = 0
963             maximum_grid_level_l = maximum_grid_level
964
965          ENDIF
966
967!
968!--       Use switch level calculated above only if it is not pre-defined
969!--       by user
970          IF ( mg_switch_to_pe0_level == 0 )  THEN
971             IF ( mg_switch_to_pe0_level_l /= 0 )  THEN
972                mg_switch_to_pe0_level = mg_switch_to_pe0_level_l
973                maximum_grid_level     = maximum_grid_level_l
974             ENDIF
975
976          ELSE
977!
978!--          Check pre-defined value and reset to default, if neccessary
979             IF ( mg_switch_to_pe0_level < mg_switch_to_pe0_level_l  .OR.      &
980                  mg_switch_to_pe0_level >= maximum_grid_level_l )  THEN
981                message_string = 'mg_switch_to_pe0_level ' //                  &
982                                 'out of range and reset to 0'
983                CALL message( 'init_pegrid', 'PA0235', 0, 1, 0, 6, 0 )
984                mg_switch_to_pe0_level = 0
985             ELSE
986!
987!--             Use the largest number of possible levels anyway and recalculate
988!--             the switch level to this largest number of possible values
989                maximum_grid_level = maximum_grid_level_l
990
991             ENDIF
992
993          ENDIF
994
995       ENDIF
996
997       ALLOCATE( grid_level_count(maximum_grid_level),                       &
998                 nxl_mg(0:maximum_grid_level), nxr_mg(0:maximum_grid_level), &
999                 nyn_mg(0:maximum_grid_level), nys_mg(0:maximum_grid_level), &
1000                 nzt_mg(0:maximum_grid_level) )
1001
1002       grid_level_count = 0
1003!
1004!--    Index zero required as dummy due to definition of arrays f2 and p2 in
1005!--    recursive subroutine next_mg_level
1006       nxl_mg(0) = 0; nxr_mg(0) = 0; nyn_mg(0) = 0; nys_mg(0) = 0; nzt_mg(0) = 0
1007
1008       nxl_l = nxl; nxr_l = nxr; nys_l = nys; nyn_l = nyn; nzt_l = nzt
1009
1010       DO  i = maximum_grid_level, 1 , -1
1011
1012          IF ( i == mg_switch_to_pe0_level )  THEN
1013#if defined( __parallel )
1014!
1015!--          Save the grid size of the subdomain at the switch level, because
1016!--          it is needed in poismg.
1017             ind(1) = nxl_l; ind(2) = nxr_l
1018             ind(3) = nys_l; ind(4) = nyn_l
1019             ind(5) = nzt_l
1020             ALLOCATE( ind_all(5*numprocs), mg_loc_ind(5,0:numprocs-1) )
1021             CALL MPI_ALLGATHER( ind, 5, MPI_INTEGER, ind_all, 5, &
1022                                 MPI_INTEGER, comm2d, ierr )
1023             DO  j = 0, numprocs-1
1024                DO  k = 1, 5
1025                   mg_loc_ind(k,j) = ind_all(k+j*5)
1026                ENDDO
1027             ENDDO
1028             DEALLOCATE( ind_all )
1029!
1030!--          Calculate the grid size of the total domain
1031             nxr_l = ( nxr_l-nxl_l+1 ) * pdims(1) - 1
1032             nxl_l = 0
1033             nyn_l = ( nyn_l-nys_l+1 ) * pdims(2) - 1
1034             nys_l = 0
1035!
1036!--          The size of this gathered array must not be larger than the
1037!--          array tend, which is used in the multigrid scheme as a temporary
1038!--          array. Therefore the subdomain size of an PE is calculated and
1039!--          the size of the gathered grid. These values are used in 
1040!--          routines pres and poismg
1041             subdomain_size = ( nxr - nxl + 2 * nbgp + 1 ) * &
1042                              ( nyn - nys + 2 * nbgp + 1 ) * ( nzt - nzb + 2 )
1043             gathered_size  = ( nxr_l - nxl_l + 3 ) * ( nyn_l - nys_l + 3 ) *  &
1044                              ( nzt_l - nzb + 2 )
1045
1046#else
1047             message_string = 'multigrid gather/scatter impossible ' //        &
1048                          'in non parallel mode'
1049             CALL message( 'init_pegrid', 'PA0237', 1, 2, 0, 6, 0 )
1050#endif
1051          ENDIF
1052
1053          nxl_mg(i) = nxl_l
1054          nxr_mg(i) = nxr_l
1055          nys_mg(i) = nys_l
1056          nyn_mg(i) = nyn_l
1057          nzt_mg(i) = nzt_l
1058
1059          nxl_l = nxl_l / 2 
1060          nxr_l = nxr_l / 2
1061          nys_l = nys_l / 2 
1062          nyn_l = nyn_l / 2 
1063          nzt_l = nzt_l / 2 
1064
1065       ENDDO
1066
1067!
1068!--    Temporary problem: Currently calculation of maxerror in routine poismg crashes
1069!--    if grid data are collected on PE0 already on the finest grid level.
1070!--    To be solved later.
1071       IF ( maximum_grid_level == mg_switch_to_pe0_level )  THEN
1072          message_string = 'grid coarsening on subdomain level cannot be performed'
1073          CALL message( 'poismg', 'PA0236', 1, 2, 0, 6, 0 )
1074       ENDIF
1075
1076    ELSE
1077
1078       maximum_grid_level = 0
1079
1080    ENDIF
1081
1082!
1083!-- Default level 0 tells exchange_horiz that all ghost planes have to be
1084!-- exchanged. grid_level is adjusted in poismg, where only one ghost plane
1085!-- is required.
1086    grid_level = 0
1087
1088#if defined( __parallel )
1089!
1090!-- Gridpoint number for the exchange of ghost points (y-line for 2D-arrays)
1091    ngp_y  = nyn - nys + 1 + 2 * nbgp
1092
1093!
1094!-- Define new MPI derived datatypes for the exchange of ghost points in
1095!-- x- and y-direction for 2D-arrays (line)
1096    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp, ngp_y, MPI_REAL, type_x,     &
1097                          ierr )
1098    CALL MPI_TYPE_COMMIT( type_x, ierr )
1099
1100    CALL MPI_TYPE_VECTOR( nbgp, ngp_y, ngp_y, MPI_REAL, type_y, ierr )
1101    CALL MPI_TYPE_COMMIT( type_y, ierr )
1102!
1103!-- Define new MPI derived datatypes for the exchange of ghost points in
1104!-- x- and y-direction for 2D-INTEGER arrays (line) - on normal grid
1105    ALLOCATE( type_x_int(0:maximum_grid_level),                                &
1106              type_y_int(0:maximum_grid_level) )
1107
1108    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp, ngp_y, MPI_INTEGER,          &
1109                          type_x_int(0), ierr )
1110    CALL MPI_TYPE_COMMIT( type_x_int(0), ierr )
1111
1112    CALL MPI_TYPE_VECTOR( nbgp, ngp_y, ngp_y, MPI_INTEGER, type_y_int(0), ierr )
1113    CALL MPI_TYPE_COMMIT( type_y_int(0), ierr )
1114!
1115!-- Calculate gridpoint numbers for the exchange of ghost points along x
1116!-- (yz-plane for 3D-arrays) and define MPI derived data type(s) for the
1117!-- exchange of ghost points in y-direction (xz-plane).
1118!-- Do these calculations for the model grid and (if necessary) also
1119!-- for the coarser grid levels used in the multigrid method
1120    ALLOCATE ( ngp_xz(0:maximum_grid_level),                                   &
1121               ngp_xz_int(0:maximum_grid_level),                               &
1122               ngp_yz(0:maximum_grid_level),                                   &
1123               ngp_yz_int(0:maximum_grid_level),                               &
1124               type_xz(0:maximum_grid_level),                                  &
1125               type_xz_int(0:maximum_grid_level),                              &
1126               type_yz(0:maximum_grid_level),                                  &
1127               type_yz_int(0:maximum_grid_level) )
1128
1129    nxl_l = nxl; nxr_l = nxr; nys_l = nys; nyn_l = nyn; nzb_l = nzb; nzt_l = nzt
1130
1131!
1132!-- Discern between the model grid, which needs nbgp ghost points and
1133!-- grid levels for the multigrid scheme. In the latter case only one
1134!-- ghost point is necessary.
1135!-- First definition of MPI-datatypes for exchange of ghost layers on normal
1136!-- grid. The following loop is needed for data exchange in poismg.f90.
1137!
1138!-- Determine number of grid points of yz-layer for exchange
1139    ngp_yz(0) = (nzt - nzb + 2) * (nyn - nys + 1 + 2 * nbgp)
1140
1141!
1142!-- Define an MPI-datatype for the exchange of left/right boundaries.
1143!-- Although data are contiguous in physical memory (which does not
1144!-- necessarily require an MPI-derived datatype), the data exchange between
1145!-- left and right PE's using the MPI-derived type is 10% faster than without.
1146    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp*(nzt-nzb+2), ngp_yz(0), &
1147                          MPI_REAL, type_xz(0), ierr )
1148    CALL MPI_TYPE_COMMIT( type_xz(0), ierr )
1149
1150    CALL MPI_TYPE_VECTOR( nbgp, ngp_yz(0), ngp_yz(0), MPI_REAL, type_yz(0), &
1151                          ierr ) 
1152    CALL MPI_TYPE_COMMIT( type_yz(0), ierr )
1153
1154!
1155!-- Define data types for exchange of 3D Integer arrays.
1156    ngp_yz_int(0) = (nzt - nzb + 2) * (nyn - nys + 1 + 2 * nbgp)
1157
1158    CALL MPI_TYPE_VECTOR( nxr-nxl+1+2*nbgp, nbgp*(nzt-nzb+2), ngp_yz_int(0),   &
1159                          MPI_INTEGER, type_xz_int(0), ierr )
1160    CALL MPI_TYPE_COMMIT( type_xz_int(0), ierr )
1161
1162    CALL MPI_TYPE_VECTOR( nbgp, ngp_yz_int(0), ngp_yz_int(0), MPI_INTEGER,     &
1163                          type_yz_int(0), ierr )
1164    CALL MPI_TYPE_COMMIT( type_yz_int(0), ierr )
1165
1166!
1167!-- Definition of MPI-datatypes for multigrid method (coarser level grids)
1168    IF ( psolver(1:9) == 'multigrid' )  THEN
1169!   
1170!--    Definition of MPI-datatyoe as above, but only 1 ghost level is used
1171       DO  i = maximum_grid_level, 1 , -1
1172!
1173!--       For 3D-exchange on different multigrid level, one ghost point for
1174!--       REAL arrays, two ghost points for INTEGER arrays
1175          ngp_xz(i) = (nzt_l - nzb_l + 2) * (nxr_l - nxl_l + 3)
1176          ngp_yz(i) = (nzt_l - nzb_l + 2) * (nyn_l - nys_l + 3)
1177
1178          ngp_xz_int(i) = (nzt_l - nzb_l + 2) * (nxr_l - nxl_l + 3)
1179          ngp_yz_int(i) = (nzt_l - nzb_l + 2) * (nyn_l - nys_l + 3)
1180!
1181!--       MPI data type for REAL arrays, for xz-layers
1182          CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+3, nzt_l-nzb_l+2, ngp_yz(i),       &
1183                                MPI_REAL, type_xz(i), ierr )
1184          CALL MPI_TYPE_COMMIT( type_xz(i), ierr )
1185
1186!
1187!--       MPI data type for INTEGER arrays, for xz-layers
1188          CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+3, nzt_l-nzb_l+2, ngp_yz_int(i),   &
1189                                MPI_INTEGER, type_xz_int(i), ierr )
1190          CALL MPI_TYPE_COMMIT( type_xz_int(i), ierr )
1191
1192!
1193!--       MPI data type for REAL arrays, for yz-layers
1194          CALL MPI_TYPE_VECTOR( 1, ngp_yz(i), ngp_yz(i), MPI_REAL, type_yz(i), &
1195                                ierr )
1196          CALL MPI_TYPE_COMMIT( type_yz(i), ierr )
1197!
1198!--       MPI data type for INTEGER arrays, for yz-layers
1199          CALL MPI_TYPE_VECTOR( 1, ngp_yz_int(i), ngp_yz_int(i), MPI_INTEGER,  &
1200                                type_yz_int(i), ierr )
1201          CALL MPI_TYPE_COMMIT( type_yz_int(i), ierr )
1202
1203
1204!--       For 2D-exchange of INTEGER arrays on coarser grid level, where 2 ghost
1205!--       points need to be exchanged.
1206          CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+5, 2, nyn_l-nys_l+5, MPI_INTEGER,          &
1207                                type_x_int(i), ierr )
1208          CALL MPI_TYPE_COMMIT( type_x_int(i), ierr )
1209
1210
1211          CALL MPI_TYPE_VECTOR( 2, nyn_l-nys_l+5, nyn_l-nys_l+5, MPI_INTEGER,          &
1212                                type_y_int(i), ierr )
1213          CALL MPI_TYPE_COMMIT( type_y_int(i), ierr )
1214
1215          nxl_l = nxl_l / 2
1216          nxr_l = nxr_l / 2
1217          nys_l = nys_l / 2
1218          nyn_l = nyn_l / 2
1219          nzt_l = nzt_l / 2
1220
1221       ENDDO
1222
1223    ENDIF
1224
1225#endif
1226
1227#if defined( __parallel )
1228!
1229!-- Setting of flags for inflow/outflow/nesting conditions.
1230    IF ( pleft == MPI_PROC_NULL )  THEN
1231       IF ( bc_lr == 'dirichlet/radiation' )  THEN
1232          inflow_l  = .TRUE.
1233       ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
1234          outflow_l = .TRUE.
1235       ELSEIF ( bc_lr == 'nested' )  THEN
1236          nest_bound_l = .TRUE.
1237       ELSEIF ( bc_lr == 'forcing' )  THEN
1238          force_bound_l = .TRUE.
1239       ENDIF
1240    ENDIF
1241 
1242    IF ( pright == MPI_PROC_NULL )  THEN
1243       IF ( bc_lr == 'dirichlet/radiation' )  THEN
1244          outflow_r = .TRUE.
1245       ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
1246          inflow_r  = .TRUE.
1247       ELSEIF ( bc_lr == 'nested' )  THEN
1248          nest_bound_r = .TRUE.
1249       ELSEIF ( bc_lr == 'forcing' )  THEN
1250          force_bound_r = .TRUE.
1251       ENDIF
1252    ENDIF
1253
1254    IF ( psouth == MPI_PROC_NULL )  THEN
1255       IF ( bc_ns == 'dirichlet/radiation' )  THEN
1256          outflow_s = .TRUE.
1257       ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
1258          inflow_s  = .TRUE.
1259       ELSEIF ( bc_ns == 'nested' )  THEN
1260          nest_bound_s = .TRUE.
1261       ELSEIF ( bc_ns == 'forcing' )  THEN
1262          force_bound_s = .TRUE.
1263       ENDIF
1264    ENDIF
1265
1266    IF ( pnorth == MPI_PROC_NULL )  THEN
1267       IF ( bc_ns == 'dirichlet/radiation' )  THEN
1268          inflow_n  = .TRUE.
1269       ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
1270          outflow_n = .TRUE.
1271       ELSEIF ( bc_ns == 'nested' )  THEN
1272          nest_bound_n = .TRUE.
1273       ELSEIF ( bc_ns == 'forcing' )  THEN
1274          force_bound_n = .TRUE.
1275       ENDIF
1276    ENDIF
1277!
1278!-- In case of synthetic turbulence geneartor determine ids.
1279!-- Please note, if no forcing or nesting is applied, the generator is applied
1280!-- only at the left lateral boundary.
1281    IF ( use_syn_turb_gen )  THEN
1282       IF ( force_bound_l  .OR.  nest_bound_l  .OR.  inflow_l )  THEN
1283          id_stg_left_l = myidx
1284       ELSE
1285          id_stg_left_l = 0
1286       ENDIF
1287       IF ( force_bound_r  .OR.  nest_bound_r )  THEN
1288          id_stg_right_l = myidx
1289       ELSE
1290          id_stg_right_l = 0
1291       ENDIF
1292       IF ( force_bound_s  .OR.  nest_bound_s )  THEN
1293          id_stg_south_l = myidy
1294       ELSE
1295          id_stg_south_l = 0
1296       ENDIF
1297       IF ( force_bound_n  .OR.  nest_bound_n )  THEN
1298          id_stg_north_l = myidy
1299       ELSE
1300          id_stg_north_l = 0
1301       ENDIF
1302
1303       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1304       CALL MPI_ALLREDUCE( id_stg_left_l, id_stg_left,   1, MPI_INTEGER,       &
1305                           MPI_SUM, comm1dx, ierr )
1306
1307       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1308       CALL MPI_ALLREDUCE( id_stg_right_l, id_stg_right, 1, MPI_INTEGER,       &
1309                           MPI_SUM, comm1dx, ierr )
1310
1311       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1312       CALL MPI_ALLREDUCE( id_stg_south_l, id_stg_south, 1, MPI_INTEGER,       &
1313                           MPI_SUM, comm1dy, ierr )
1314
1315       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1316       CALL MPI_ALLREDUCE( id_stg_north_l, id_stg_north, 1, MPI_INTEGER,       &
1317                           MPI_SUM, comm1dy, ierr )
1318
1319    ENDIF 
1320 
1321!
1322!-- Broadcast the id of the inflow PE
1323    IF ( inflow_l )  THEN
1324       id_inflow_l = myidx
1325    ELSE
1326       id_inflow_l = 0
1327    ENDIF
1328    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1329    CALL MPI_ALLREDUCE( id_inflow_l, id_inflow, 1, MPI_INTEGER, MPI_SUM, &
1330                        comm1dx, ierr )
1331
1332!
1333!-- Broadcast the id of the recycling plane
1334!-- WARNING: needs to be adjusted in case of inflows other than from left side!
1335    IF ( NINT( recycling_width / dx ) >= nxl  .AND. &
1336         NINT( recycling_width / dx ) <= nxr )  THEN
1337       id_recycling_l = myidx
1338    ELSE
1339       id_recycling_l = 0
1340    ENDIF
1341    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1342    CALL MPI_ALLREDUCE( id_recycling_l, id_recycling, 1, MPI_INTEGER, MPI_SUM, &
1343                        comm1dx, ierr )
1344
1345!
1346!-- Broadcast the id of the outflow PE and outflow-source plane
1347    IF ( turbulent_outflow )  THEN
1348
1349       IF ( outflow_r )  THEN
1350          id_outflow_l = myidx
1351       ELSE
1352          id_outflow_l = 0
1353       ENDIF
1354       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1355       CALL MPI_ALLREDUCE( id_outflow_l, id_outflow, 1, MPI_INTEGER, MPI_SUM, &
1356                           comm1dx, ierr )
1357
1358       IF ( NINT( outflow_source_plane / dx ) >= nxl  .AND. &
1359            NINT( outflow_source_plane / dx ) <= nxr )  THEN
1360          id_outflow_source_l = myidx
1361       ELSE
1362          id_outflow_source_l = 0
1363       ENDIF
1364       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1365       CALL MPI_ALLREDUCE( id_outflow_source_l, id_outflow_source, 1, &
1366                           MPI_INTEGER, MPI_SUM, comm1dx, ierr )
1367
1368    ENDIF
1369
1370    CALL location_message( 'finished', .TRUE. )
1371
1372#else
1373    IF ( bc_lr == 'dirichlet/radiation' )  THEN
1374       inflow_l  = .TRUE.
1375       outflow_r = .TRUE.
1376    ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
1377       outflow_l = .TRUE.
1378       inflow_r  = .TRUE.
1379    ENDIF
1380
1381    IF ( bc_ns == 'dirichlet/radiation' )  THEN
1382       inflow_n  = .TRUE.
1383       outflow_s = .TRUE.
1384    ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
1385       outflow_n = .TRUE.
1386       inflow_s  = .TRUE.
1387    ENDIF
1388#endif
1389
1390!
1391!-- At the inflow or outflow, u or v, respectively, have to be calculated for
1392!-- one more grid point.
1393    IF ( inflow_l .OR. outflow_l .OR. nest_bound_l .OR. force_bound_l )  THEN
1394       nxlu = nxl + 1
1395    ELSE
1396       nxlu = nxl
1397    ENDIF
1398    IF ( inflow_s .OR. outflow_s .OR. nest_bound_s .OR. force_bound_s )  THEN
1399       nysv = nys + 1
1400    ELSE
1401       nysv = nys
1402    ENDIF
1403
1404!
1405!-- Allocate wall flag arrays used in the multigrid solver
1406    IF ( psolver(1:9) == 'multigrid' )  THEN
1407
1408       DO  i = maximum_grid_level, 1, -1
1409
1410           SELECT CASE ( i )
1411
1412              CASE ( 1 )
1413                 ALLOCATE( wall_flags_1(nzb:nzt_mg(i)+1,         &
1414                                        nys_mg(i)-1:nyn_mg(i)+1, &
1415                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1416
1417              CASE ( 2 )
1418                 ALLOCATE( wall_flags_2(nzb:nzt_mg(i)+1,         &
1419                                        nys_mg(i)-1:nyn_mg(i)+1, &
1420                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1421
1422              CASE ( 3 )
1423                 ALLOCATE( wall_flags_3(nzb:nzt_mg(i)+1,         &
1424                                        nys_mg(i)-1:nyn_mg(i)+1, &
1425                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1426
1427              CASE ( 4 )
1428                 ALLOCATE( wall_flags_4(nzb:nzt_mg(i)+1,         &
1429                                        nys_mg(i)-1:nyn_mg(i)+1, &
1430                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1431
1432              CASE ( 5 )
1433                 ALLOCATE( wall_flags_5(nzb:nzt_mg(i)+1,         &
1434                                        nys_mg(i)-1:nyn_mg(i)+1, &
1435                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1436
1437              CASE ( 6 )
1438                 ALLOCATE( wall_flags_6(nzb:nzt_mg(i)+1,         &
1439                                        nys_mg(i)-1:nyn_mg(i)+1, &
1440                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1441
1442              CASE ( 7 )
1443                 ALLOCATE( wall_flags_7(nzb:nzt_mg(i)+1,         &
1444                                        nys_mg(i)-1:nyn_mg(i)+1, &
1445                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1446
1447              CASE ( 8 )
1448                 ALLOCATE( wall_flags_8(nzb:nzt_mg(i)+1,         &
1449                                        nys_mg(i)-1:nyn_mg(i)+1, &
1450                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1451
1452              CASE ( 9 )
1453                 ALLOCATE( wall_flags_9(nzb:nzt_mg(i)+1,         &
1454                                        nys_mg(i)-1:nyn_mg(i)+1, &
1455                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1456
1457              CASE ( 10 )
1458                 ALLOCATE( wall_flags_10(nzb:nzt_mg(i)+1,        &
1459                                        nys_mg(i)-1:nyn_mg(i)+1, &
1460                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1461
1462              CASE DEFAULT
1463                 message_string = 'more than 10 multigrid levels'
1464                 CALL message( 'init_pegrid', 'PA0238', 1, 2, 0, 6, 0 )
1465
1466          END SELECT
1467
1468       ENDDO
1469
1470    ENDIF
1471
1472 END SUBROUTINE init_pegrid
Note: See TracBrowser for help on using the repository browser.