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

Last change on this file since 3887 was 3885, checked in by kanani, 5 years ago

restructure/add location/debug messages

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