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

Last change on this file since 2365 was 2365, checked in by kanani, 7 years ago

Vertical nesting implemented (SadiqHuq?)

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