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

Last change on this file since 2259 was 2259, checked in by gronemeier, 7 years ago

Implemented synthetic turbulence generator

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