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

Last change on this file since 2179 was 2179, checked in by hellstea, 7 years ago

last commit documented

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