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

Last change on this file since 622 was 622, checked in by raasch, 13 years ago

New:
---

Optional barriers included in order to speed up collective operations
MPI_ALLTOALL and MPI_ALLREDUCE. This feature is controlled with new initial
parameter collective_wait. Default is .FALSE, but .TRUE. on SGI-type
systems. (advec_particles, advec_s_bc, buoyancy, check_for_restart,
cpu_statistics, data_output_2d, data_output_ptseries, flow_statistics,
global_min_max, inflow_turbulence, init_3d_model, init_particles, init_pegrid,
init_slope, parin, pres, poismg, set_particle_attributes, timestep,
read_var_list, user_statistics, write_compressed, write_var_list)

Adjustments for Kyushu Univ. (lcrte, ibmku). Concerning hybrid
(MPI/openMP) runs, the number of openMP threads per MPI tasks can now
be given as an argument to mrun-option -O. (mbuild, mrun, subjob)

Changed:


Initialization of the module command changed for SGI-ICE/lcsgi (mbuild, subjob)

Errors:


  • Property svn:keywords set to Id
File size: 34.4 KB
Line 
1 SUBROUTINE init_pegrid
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! -----------------
6! optional barriers included in order to speed up collective operations
7! ATTENTION: nnz_x undefined problem still has to be solved!!!!!!!!
8! TEST OUTPUT (TO BE REMOVED) logging mpi2 ierr values
9!
10! Former revisions:
11! -----------------
12! $Id: init_pegrid.f90 622 2010-12-10 08:08:13Z raasch $
13! 438 2010-02-01 04:32:43Z raasch
14! 2d-decomposition is default for Cray-XT machines
15!
16! 274 2009-03-26 15:11:21Z heinze
17! Output of messages replaced by message handling routine.
18!
19! 206 2008-10-13 14:59:11Z raasch
20! Implementation of a MPI-1 coupling: added __parallel within the __mpi2 part
21! 2d-decomposition is default on SGI-ICE systems
22!
23! 197 2008-09-16 15:29:03Z raasch
24! multigrid levels are limited by subdomains if mg_switch_to_pe0_level = -1,
25! nz is used instead nnz for calculating mg-levels
26! Collect on PE0 horizontal index bounds from all other PEs,
27! broadcast the id of the inflow PE (using the respective communicator)
28!
29! 114 2007-10-10 00:03:15Z raasch
30! Allocation of wall flag arrays for multigrid solver
31!
32! 108 2007-08-24 15:10:38Z letzel
33! Intercommunicator (comm_inter) and derived data type (type_xy) for
34! coupled model runs created, assign coupling_mode_remote,
35! indices nxlu and nysv are calculated (needed for non-cyclic boundary
36! conditions)
37!
38! 82 2007-04-16 15:40:52Z raasch
39! Cpp-directive lcmuk changed to intel_openmp_bug, setting of host on lcmuk by
40! cpp-directive removed
41!
42! 75 2007-03-22 09:54:05Z raasch
43! uxrp, vynp eliminated,
44! dirichlet/neumann changed to dirichlet/radiation, etc.,
45! poisfft_init is only called if fft-solver is switched on
46!
47! RCS Log replace by Id keyword, revision history cleaned up
48!
49! Revision 1.28  2006/04/26 13:23:32  raasch
50! lcmuk does not understand the !$ comment so a cpp-directive is required
51!
52! Revision 1.1  1997/07/24 11:15:09  raasch
53! Initial revision
54!
55!
56! Description:
57! ------------
58! Determination of the virtual processor topology (if not prescribed by the
59! user)and computation of the grid point number and array bounds of the local
60! domains.
61!------------------------------------------------------------------------------!
62
63    USE control_parameters
64    USE fft_xy
65    USE grid_variables
66    USE indices
67    USE pegrid
68    USE poisfft_mod
69    USE poisfft_hybrid_mod
70    USE statistics
71    USE transpose_indices
72
73
74    IMPLICIT NONE
75
76    INTEGER ::  gathered_size, i, id_inflow_l, id_recycling_l, ind(5), j, k, &
77                maximum_grid_level_l, mg_switch_to_pe0_level_l, mg_levels_x, &
78                mg_levels_y, mg_levels_z, nnx_y, nnx_z, nny_x, nny_z, nnz_x, &
79                nnz_y, numproc_sqr, nx_total, nxl_l, nxr_l, nyn_l, nys_l,    &
80                nzb_l, nzt_l, omp_get_num_threads, subdomain_size
81
82    INTEGER, DIMENSION(:), ALLOCATABLE ::  ind_all, nxlf, nxrf, nynf, nysf
83
84    LOGICAL ::  found
85
86!
87!-- Get the number of OpenMP threads
88    !$OMP PARALLEL
89#if defined( __intel_openmp_bug )
90    threads_per_task = omp_get_num_threads()
91#else
92!$  threads_per_task = omp_get_num_threads()
93#endif
94    !$OMP END PARALLEL
95
96
97#if defined( __parallel )
98!
99!-- Determine the processor topology or check it, if prescribed by the user
100    IF ( npex == -1  .AND.  npey == -1 )  THEN
101
102!
103!--    Automatic determination of the topology
104!--    The default on SMP- and cluster-hosts is a 1d-decomposition along x
105       IF ( host(1:3) == 'ibm'  .OR.  host(1:3) == 'nec'      .OR. &
106            ( host(1:2) == 'lc'  .AND.  host(3:5) /= 'sgi'  .AND.  &
107              host(3:4) /= 'xt' )                             .OR. &
108             host(1:3) == 'dec' )  THEN
109
110          pdims(1) = numprocs
111          pdims(2) = 1
112
113       ELSE
114
115          numproc_sqr = SQRT( REAL( numprocs ) )
116          pdims(1)    = MAX( numproc_sqr , 1 )
117          DO  WHILE ( MOD( numprocs , pdims(1) ) /= 0 )
118             pdims(1) = pdims(1) - 1
119          ENDDO
120          pdims(2) = numprocs / pdims(1)
121
122       ENDIF
123
124    ELSEIF ( npex /= -1  .AND.  npey /= -1 )  THEN
125
126!
127!--    Prescribed by user. Number of processors on the prescribed topology
128!--    must be equal to the number of PEs available to the job
129       IF ( ( npex * npey ) /= numprocs )  THEN
130          WRITE( message_string, * ) 'number of PEs of the prescribed ',      & 
131                 'topology (', npex*npey,') does not match & the number of ', & 
132                 'PEs available to the job (', numprocs, ')'
133          CALL message( 'init_pegrid', 'PA0221', 1, 2, 0, 6, 0 )
134       ENDIF
135       pdims(1) = npex
136       pdims(2) = npey
137
138    ELSE
139!
140!--    If the processor topology is prescribed by the user, the number of
141!--    PEs must be given in both directions
142       message_string = 'if the processor topology is prescribed by the, ' //  &
143                   ' user& both values of "npex" and "npey" must be given ' // &
144                   'in the &NAMELIST-parameter file'
145       CALL message( 'init_pegrid', 'PA0222', 1, 2, 0, 6, 0 )
146
147    ENDIF
148
149!
150!-- The hybrid solver can only be used in case of a 1d-decomposition along x
151    IF ( pdims(2) /= 1  .AND.  psolver == 'poisfft_hybrid' )  THEN
152       message_string = 'psolver = "poisfft_hybrid" can only be' // &
153                        '& used in case of a 1d-decomposition along x'
154       CALL message( 'init_pegrid', 'PA0223', 1, 2, 0, 6, 0 )
155    ENDIF
156
157!
158!-- For communication speedup, set barriers in front of collective
159!-- communications by default on SGI-type systems
160    IF ( host(3:5) == 'sgi' )  collective_wait = .TRUE.
161
162!
163!-- If necessary, set horizontal boundary conditions to non-cyclic
164    IF ( bc_lr /= 'cyclic' )  cyclic(1) = .FALSE.
165    IF ( bc_ns /= 'cyclic' )  cyclic(2) = .FALSE.
166
167!
168!-- Create the virtual processor grid
169    CALL MPI_CART_CREATE( comm_palm, ndim, pdims, cyclic, reorder, &
170                          comm2d, ierr )
171    CALL MPI_COMM_RANK( comm2d, myid, ierr )
172    WRITE (myid_char,'(''_'',I4.4)')  myid
173
174    CALL MPI_CART_COORDS( comm2d, myid, ndim, pcoord, ierr )
175    CALL MPI_CART_SHIFT( comm2d, 0, 1, pleft, pright, ierr )
176    CALL MPI_CART_SHIFT( comm2d, 1, 1, psouth, pnorth, ierr )
177
178!
179!-- Determine sub-topologies for transpositions
180!-- Transposition from z to x:
181    remain_dims(1) = .TRUE.
182    remain_dims(2) = .FALSE.
183    CALL MPI_CART_SUB( comm2d, remain_dims, comm1dx, ierr )
184    CALL MPI_COMM_RANK( comm1dx, myidx, ierr )
185!
186!-- Transposition from x to y
187    remain_dims(1) = .FALSE.
188    remain_dims(2) = .TRUE.
189    CALL MPI_CART_SUB( comm2d, remain_dims, comm1dy, ierr )
190    CALL MPI_COMM_RANK( comm1dy, myidy, ierr )
191
192
193!
194!-- Find a grid (used for array d) which will match the transposition demands
195    IF ( grid_matching == 'strict' )  THEN
196
197       nxa = nx;  nya = ny;  nza = nz
198
199    ELSE
200
201       found = .FALSE.
202   xn: DO  nxa = nx, 2*nx
203!
204!--       Meet conditions for nx
205          IF ( MOD( nxa+1, pdims(1) ) /= 0 .OR. &
206               MOD( nxa+1, pdims(2) ) /= 0 )  CYCLE xn
207
208      yn: DO  nya = ny, 2*ny
209!
210!--          Meet conditions for ny
211             IF ( MOD( nya+1, pdims(2) ) /= 0 .OR. &
212                  MOD( nya+1, pdims(1) ) /= 0 )  CYCLE yn
213
214
215         zn: DO  nza = nz, 2*nz
216!
217!--             Meet conditions for nz
218                IF ( ( MOD( nza, pdims(1) ) /= 0  .AND.  pdims(1) /= 1  .AND. &
219                       pdims(2) /= 1 )  .OR.                                  &
220                     ( MOD( nza, pdims(2) ) /= 0  .AND.  dt_dosp /= 9999999.9 &
221                     ) )  THEN
222                   CYCLE zn
223                ELSE
224                   found = .TRUE.
225                   EXIT xn
226                ENDIF
227
228             ENDDO zn
229
230          ENDDO yn
231
232       ENDDO xn
233
234       IF ( .NOT. found )  THEN
235          message_string = 'no matching grid for transpositions found'
236          CALL message( 'init_pegrid', 'PA0224', 1, 2, 0, 6, 0 )
237       ENDIF
238
239    ENDIF
240
241!
242!-- Calculate array bounds in x-direction for every PE.
243!-- The last PE along x may get less grid points than the others
244    ALLOCATE( nxlf(0:pdims(1)-1), nxrf(0:pdims(1)-1), nynf(0:pdims(2)-1), &
245              nysf(0:pdims(2)-1), nnx_pe(0:pdims(1)-1), nny_pe(0:pdims(2)-1) )
246
247    IF ( MOD( nxa+1 , pdims(1) ) /= 0 )  THEN
248       WRITE( message_string, * ) 'x-direction: gridpoint number (',nx+1,') ',&
249                               'is not an& integral divisor of the number ',  &
250                               'processors (', pdims(1),')'
251       CALL message( 'init_pegrid', 'PA0225', 1, 2, 0, 6, 0 )
252    ELSE
253       nnx  = ( nxa + 1 ) / pdims(1)
254       IF ( nnx*pdims(1) - ( nx + 1) > nnx )  THEN
255          WRITE( message_string, * ) 'x-direction: nx does not match the',    & 
256                       'requirements given by the number of PEs &used',       &
257                       '& please use nx = ', nx - ( pdims(1) - ( nnx*pdims(1) &
258                                   - ( nx + 1 ) ) ), ' instead of nx =', nx
259          CALL message( 'init_pegrid', 'PA0226', 1, 2, 0, 6, 0 )
260       ENDIF
261    ENDIF   
262
263!
264!-- Left and right array bounds, number of gridpoints
265    DO  i = 0, pdims(1)-1
266       nxlf(i)   = i * nnx
267       nxrf(i)   = ( i + 1 ) * nnx - 1
268       nnx_pe(i) = MIN( nx, nxrf(i) ) - nxlf(i) + 1
269    ENDDO
270
271!
272!-- Calculate array bounds in y-direction for every PE.
273    IF ( MOD( nya+1 , pdims(2) ) /= 0 )  THEN
274       WRITE( message_string, * ) 'y-direction: gridpoint number (',ny+1,') ', &
275                           'is not an& integral divisor of the number of',     &
276                           'processors (', pdims(2),')'
277       CALL message( 'init_pegrid', 'PA0227', 1, 2, 0, 6, 0 )
278    ELSE
279       nny  = ( nya + 1 ) / pdims(2)
280       IF ( nny*pdims(2) - ( ny + 1) > nny )  THEN
281          WRITE( message_string, * ) 'y-direction: ny does not match the',    &
282                       'requirements given by the number of PEs &used ',      &
283                       '& please use ny = ', ny - ( pdims(2) - ( nnx*pdims(2) &
284                                     - ( ny + 1 ) ) ), ' instead of ny =', ny
285          CALL message( 'init_pegrid', 'PA0228', 1, 2, 0, 6, 0 )
286       ENDIF
287    ENDIF   
288
289!
290!-- South and north array bounds
291    DO  j = 0, pdims(2)-1
292       nysf(j)   = j * nny
293       nynf(j)   = ( j + 1 ) * nny - 1
294       nny_pe(j) = MIN( ny, nynf(j) ) - nysf(j) + 1
295    ENDDO
296
297!
298!-- Local array bounds of the respective PEs
299    nxl  = nxlf(pcoord(1))
300    nxra = nxrf(pcoord(1))
301    nxr  = MIN( nx, nxra )
302    nys  = nysf(pcoord(2))
303    nyna = nynf(pcoord(2))
304    nyn  = MIN( ny, nyna )
305    nzb  = 0
306    nzta = nza
307    nzt  = MIN( nz, nzta )
308    nnz  = nza
309
310!
311!-- Calculate array bounds and gridpoint numbers for the transposed arrays
312!-- (needed in the pressure solver)
313!-- For the transposed arrays, cyclic boundaries as well as top and bottom
314!-- boundaries are omitted, because they are obstructive to the transposition
315
316!
317!-- 1. transposition  z --> x
318!-- This transposition is not neccessary in case of a 1d-decomposition along x,
319!-- except that the uptream-spline method is switched on
320    IF ( pdims(2) /= 1  .OR.  momentum_advec == 'ups-scheme'  .OR. &
321         scalar_advec == 'ups-scheme' )  THEN
322
323       IF ( pdims(2) == 1  .AND. ( momentum_advec == 'ups-scheme'  .OR. &
324            scalar_advec == 'ups-scheme' ) )  THEN
325          message_string = '1d-decomposition along x ' // &
326                           'chosen but nz restrictions may occur' // &
327                           '& since ups-scheme is activated'
328          CALL message( 'init_pegrid', 'PA0229', 0, 1, 0, 6, 0 )
329       ENDIF
330       nys_x  = nys
331       nyn_xa = nyna
332       nyn_x  = nyn
333       nny_x  = nny
334       IF ( MOD( nza , pdims(1) ) /= 0 )  THEN
335          WRITE( message_string, * ) 'transposition z --> x:',                &
336                       '&nz=',nz,' is not an integral divisior of pdims(1)=', &
337                                                                   pdims(1)
338          CALL message( 'init_pegrid', 'PA0230', 1, 2, 0, 6, 0 )
339       ENDIF
340       nnz_x  = nza / pdims(1)
341       nzb_x  = 1 + myidx * nnz_x
342       nzt_xa = ( myidx + 1 ) * nnz_x
343       nzt_x  = MIN( nzt, nzt_xa )
344
345       sendrecvcount_zx = nnx * nny * nnz_x
346
347    ELSE
348!
349!---   Setting of dummy values because otherwise variables are undefined in
350!---   the next step  x --> y
351!---   WARNING: This case has still to be clarified!!!!!!!!!!!!
352       nnz_x  = 1
353       nzb_x  = 1
354       nzt_xa = 1
355       nzt_x  = 1
356       nny_x  = nny
357
358    ENDIF
359
360!
361!-- 2. transposition  x --> y
362    nnz_y  = nnz_x
363    nzb_y  = nzb_x
364    nzt_ya = nzt_xa
365    nzt_y  = nzt_x
366    IF ( MOD( nxa+1 , pdims(2) ) /= 0 )  THEN
367       WRITE( message_string, * ) 'transposition x --> y:',                &
368                         '&nx+1=',nx+1,' is not an integral divisor of ',&
369                         'pdims(2)=',pdims(2)
370       CALL message( 'init_pegrid', 'PA0231', 1, 2, 0, 6, 0 )
371    ENDIF
372    nnx_y = (nxa+1) / pdims(2)
373    nxl_y = myidy * nnx_y
374    nxr_ya = ( myidy + 1 ) * nnx_y - 1
375    nxr_y  = MIN( nx, nxr_ya )
376
377    sendrecvcount_xy = nnx_y * nny_x * nnz_y
378
379!
380!-- 3. transposition  y --> z  (ELSE:  x --> y  in case of 1D-decomposition
381!-- along x)
382    IF ( pdims(2) /= 1  .OR.  momentum_advec == 'ups-scheme'  .OR. &
383         scalar_advec == 'ups-scheme' )  THEN
384!
385!--    y --> z
386!--    This transposition is not neccessary in case of a 1d-decomposition
387!--    along x, except that the uptream-spline method is switched on
388       nnx_z  = nnx_y
389       nxl_z  = nxl_y
390       nxr_za = nxr_ya
391       nxr_z  = nxr_y
392       IF ( MOD( nya+1 , pdims(1) ) /= 0 )  THEN
393          WRITE( message_string, * ) 'transposition y --> z:',            &
394                            '& ny+1=',ny+1,' is not an integral divisor of ',&
395                            'pdims(1)=',pdims(1)
396          CALL message( 'init_pegrid', 'PA0232', 1, 2, 0, 6, 0 )
397       ENDIF
398       nny_z  = (nya+1) / pdims(1)
399       nys_z  = myidx * nny_z
400       nyn_za = ( myidx + 1 ) * nny_z - 1
401       nyn_z  = MIN( ny, nyn_za )
402
403       sendrecvcount_yz = nnx_y * nny_z * nnz_y
404
405    ELSE
406!
407!--    x --> y. This condition must be fulfilled for a 1D-decomposition along x
408       IF ( MOD( nya+1 , pdims(1) ) /= 0 )  THEN
409          WRITE( message_string, * ) 'transposition x --> y:',               &
410                            '& ny+1=',ny+1,' is not an integral divisor of ',&
411                            'pdims(1)=',pdims(1)
412          CALL message( 'init_pegrid', 'PA0233', 1, 2, 0, 6, 0 )
413       ENDIF
414
415    ENDIF
416
417!
418!-- Indices for direct transpositions z --> y (used for calculating spectra)
419    IF ( dt_dosp /= 9999999.9 )  THEN
420       IF ( MOD( nza, pdims(2) ) /= 0 )  THEN
421          WRITE( message_string, * ) 'direct transposition z --> y (needed ', &
422                    'for spectra):& nz=',nz,' is not an integral divisor of ',&
423                    'pdims(2)=',pdims(2)
424          CALL message( 'init_pegrid', 'PA0234', 1, 2, 0, 6, 0 )
425       ELSE
426          nxl_yd  = nxl
427          nxr_yda = nxra
428          nxr_yd  = nxr
429          nzb_yd  = 1 + myidy * ( nza / pdims(2) )
430          nzt_yda = ( myidy + 1 ) * ( nza / pdims(2) )
431          nzt_yd  = MIN( nzt, nzt_yda )
432
433          sendrecvcount_zyd = nnx * nny * ( nza / pdims(2) )
434       ENDIF
435    ENDIF
436
437!
438!-- Indices for direct transpositions y --> x (they are only possible in case
439!-- of a 1d-decomposition along x)
440    IF ( pdims(2) == 1 )  THEN
441       nny_x  = nny / pdims(1)
442       nys_x  = myid * nny_x
443       nyn_xa = ( myid + 1 ) * nny_x - 1
444       nyn_x  = MIN( ny, nyn_xa )
445       nzb_x  = 1
446       nzt_xa = nza
447       nzt_x  = nz
448       sendrecvcount_xy = nnx * nny_x * nza
449    ENDIF
450
451!
452!-- Indices for direct transpositions x --> y (they are only possible in case
453!-- of a 1d-decomposition along y)
454    IF ( pdims(1) == 1 )  THEN
455       nnx_y  = nnx / pdims(2)
456       nxl_y  = myid * nnx_y
457       nxr_ya = ( myid + 1 ) * nnx_y - 1
458       nxr_y  = MIN( nx, nxr_ya )
459       nzb_y  = 1
460       nzt_ya = nza
461       nzt_y  = nz
462       sendrecvcount_xy = nnx_y * nny * nza
463    ENDIF
464
465!
466!-- Arrays for storing the array bounds are needed any more
467    DEALLOCATE( nxlf , nxrf , nynf , nysf )
468
469!
470!-- Collect index bounds from other PEs (to be written to restart file later)
471    ALLOCATE( hor_index_bounds(4,0:numprocs-1) )
472
473    IF ( myid == 0 )  THEN
474
475       hor_index_bounds(1,0) = nxl
476       hor_index_bounds(2,0) = nxr
477       hor_index_bounds(3,0) = nys
478       hor_index_bounds(4,0) = nyn
479
480!
481!--    Receive data from all other PEs
482       DO  i = 1, numprocs-1
483          CALL MPI_RECV( ibuf, 4, MPI_INTEGER, i, MPI_ANY_TAG, comm2d, status, &
484                         ierr )
485          hor_index_bounds(:,i) = ibuf(1:4)
486       ENDDO
487
488    ELSE
489!
490!--    Send index bounds to PE0
491       ibuf(1) = nxl
492       ibuf(2) = nxr
493       ibuf(3) = nys
494       ibuf(4) = nyn
495       CALL MPI_SEND( ibuf, 4, MPI_INTEGER, 0, myid, comm2d, ierr )
496
497    ENDIF
498
499#if defined( __print )
500!
501!-- Control output
502    IF ( myid == 0 )  THEN
503       PRINT*, '*** processor topology ***'
504       PRINT*, ' '
505       PRINT*, 'myid   pcoord    left right  south north  idx idy   nxl: nxr',&
506               &'   nys: nyn'
507       PRINT*, '------------------------------------------------------------',&
508               &'-----------'
509       WRITE (*,1000)  0, pcoord(1), pcoord(2), pleft, pright, psouth, pnorth, &
510                       myidx, myidy, nxl, nxr, nys, nyn
5111000   FORMAT (I4,2X,'(',I3,',',I3,')',3X,I4,2X,I4,3X,I4,2X,I4,2X,I3,1X,I3, &
512               2(2X,I4,':',I4))
513
514!
515!--    Receive data from the other PEs
516       DO  i = 1,numprocs-1
517          CALL MPI_RECV( ibuf, 12, MPI_INTEGER, i, MPI_ANY_TAG, comm2d, status, &
518                         ierr )
519          WRITE (*,1000)  i, ( ibuf(j) , j = 1,12 )
520       ENDDO
521    ELSE
522
523!
524!--    Send data to PE0
525       ibuf(1) = pcoord(1); ibuf(2) = pcoord(2); ibuf(3) = pleft
526       ibuf(4) = pright; ibuf(5) = psouth; ibuf(6) = pnorth; ibuf(7) = myidx
527       ibuf(8) = myidy; ibuf(9) = nxl; ibuf(10) = nxr; ibuf(11) = nys
528       ibuf(12) = nyn
529       CALL MPI_SEND( ibuf, 12, MPI_INTEGER, 0, myid, comm2d, ierr )       
530    ENDIF
531#endif
532
533#if defined( __parallel )
534#if defined( __mpi2 )
535!
536!-- In case of coupled runs, get the port name on PE0 of the atmosphere model
537!-- and pass it to PE0 of the ocean model
538    IF ( myid == 0 )  THEN
539
540       IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
541
542          CALL MPI_OPEN_PORT( MPI_INFO_NULL, port_name, ierr )
543!
544!--       TEST OUTPUT (TO BE REMOVED)
545          WRITE(9,*)  TRIM( coupling_mode ),  &
546               ', ierr after MPI_OPEN_PORT: ', ierr
547          CALL LOCAL_FLUSH( 9 )
548
549          CALL MPI_PUBLISH_NAME( 'palm_coupler', MPI_INFO_NULL, port_name, &
550                                 ierr )
551!
552!--       TEST OUTPUT (TO BE REMOVED)
553          WRITE(9,*)  TRIM( coupling_mode ),  &
554               ', ierr after MPI_PUBLISH_NAME: ', ierr
555          CALL LOCAL_FLUSH( 9 )
556
557!
558!--       Write a flag file for the ocean model and the other atmosphere
559!--       processes.
560!--       There seems to be a bug in MPICH2 which causes hanging processes
561!--       in case that execution of LOOKUP_NAME is continued too early
562!--       (i.e. before the port has been created)
563          OPEN( 90, FILE='COUPLING_PORT_OPENED', FORM='FORMATTED' )
564          WRITE ( 90, '(''TRUE'')' )
565          CLOSE ( 90 )
566
567       ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
568
569!
570!--       Continue only if the atmosphere model has created the port.
571!--       There seems to be a bug in MPICH2 which causes hanging processes
572!--       in case that execution of LOOKUP_NAME is continued too early
573!--       (i.e. before the port has been created)
574          INQUIRE( FILE='COUPLING_PORT_OPENED', EXIST=found )
575          DO WHILE ( .NOT. found )
576             INQUIRE( FILE='COUPLING_PORT_OPENED', EXIST=found )
577          ENDDO
578
579          CALL MPI_LOOKUP_NAME( 'palm_coupler', MPI_INFO_NULL, port_name, ierr )
580!
581!--       TEST OUTPUT (TO BE REMOVED)
582          WRITE(9,*)  TRIM( coupling_mode ),  &
583               ', ierr after MPI_LOOKUP_NAME: ', ierr
584          CALL LOCAL_FLUSH( 9 )
585
586
587       ENDIF
588
589    ENDIF
590
591!
592!-- In case of coupled runs, establish the connection between the atmosphere
593!-- and the ocean model and define the intercommunicator (comm_inter)
594    CALL MPI_BARRIER( comm2d, ierr )
595    IF ( coupling_mode == 'atmosphere_to_ocean' )  THEN
596
597       PRINT*, '... before COMM_ACCEPT'
598       CALL MPI_COMM_ACCEPT( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &
599                             comm_inter, ierr )
600       PRINT*, '--- ierr = ', ierr
601       PRINT*, '--- comm_inter atmosphere = ', comm_inter
602
603       coupling_mode_remote = 'ocean_to_atmosphere'
604
605    ELSEIF ( coupling_mode == 'ocean_to_atmosphere' )  THEN
606
607       IF ( myid == 0 )  PRINT*, '*** read: ', port_name, '  ierr = ', ierr
608       PRINT*, '... before COMM_CONNECT'
609       CALL MPI_COMM_CONNECT( port_name, MPI_INFO_NULL, 0, MPI_COMM_WORLD, &
610                              comm_inter, ierr )
611       PRINT*, '--- ierr = ', ierr
612       PRINT*, '--- comm_inter ocean      = ', comm_inter
613
614       coupling_mode_remote = 'atmosphere_to_ocean'
615
616    ENDIF
617#endif
618
619!
620!-- In case of coupled runs, create a new MPI derived datatype for the
621!-- exchange of surface (xy) data .
622!-- Gridpoint number for the exchange of ghost points (xy-plane)
623    ngp_xy  = ( nxr - nxl + 3 ) * ( nyn - nys + 3 )
624
625!
626!-- Define a new MPI derived datatype for the exchange of ghost points in
627!-- y-direction for 2D-arrays (line)
628    CALL MPI_TYPE_VECTOR( ngp_xy, 1, nzt-nzb+2, MPI_REAL, type_xy, ierr )
629    CALL MPI_TYPE_COMMIT( type_xy, ierr )
630#endif
631
632#else
633
634!
635!-- Array bounds when running on a single PE (respectively a non-parallel
636!-- machine)
637    nxl  = 0
638    nxr  = nx
639    nxra = nx
640    nnx  = nxr - nxl + 1
641    nys  = 0
642    nyn  = ny
643    nyna = ny
644    nny  = nyn - nys + 1
645    nzb  = 0
646    nzt  = nz
647    nzta = nz
648    nnz  = nz
649
650    ALLOCATE( hor_index_bounds(4,0:0) )
651    hor_index_bounds(1,0) = nxl
652    hor_index_bounds(2,0) = nxr
653    hor_index_bounds(3,0) = nys
654    hor_index_bounds(4,0) = nyn
655
656!
657!-- Array bounds for the pressure solver (in the parallel code, these bounds
658!-- are the ones for the transposed arrays)
659    nys_x  = nys
660    nyn_x  = nyn
661    nyn_xa = nyn
662    nzb_x  = nzb + 1
663    nzt_x  = nzt
664    nzt_xa = nzt
665
666    nxl_y  = nxl
667    nxr_y  = nxr
668    nxr_ya = nxr
669    nzb_y  = nzb + 1
670    nzt_y  = nzt
671    nzt_ya = nzt
672
673    nxl_z  = nxl
674    nxr_z  = nxr
675    nxr_za = nxr
676    nys_z  = nys
677    nyn_z  = nyn
678    nyn_za = nyn
679
680#endif
681
682!
683!-- Calculate number of grid levels necessary for the multigrid poisson solver
684!-- as well as the gridpoint indices on each level
685    IF ( psolver == 'multigrid' )  THEN
686
687!
688!--    First calculate number of possible grid levels for the subdomains
689       mg_levels_x = 1
690       mg_levels_y = 1
691       mg_levels_z = 1
692
693       i = nnx
694       DO WHILE ( MOD( i, 2 ) == 0  .AND.  i /= 2 )
695          i = i / 2
696          mg_levels_x = mg_levels_x + 1
697       ENDDO
698
699       j = nny
700       DO WHILE ( MOD( j, 2 ) == 0  .AND.  j /= 2 )
701          j = j / 2
702          mg_levels_y = mg_levels_y + 1
703       ENDDO
704
705       k = nz    ! do not use nnz because it might be > nz due to transposition
706                 ! requirements
707       DO WHILE ( MOD( k, 2 ) == 0  .AND.  k /= 2 )
708          k = k / 2
709          mg_levels_z = mg_levels_z + 1
710       ENDDO
711
712       maximum_grid_level = MIN( mg_levels_x, mg_levels_y, mg_levels_z )
713
714!
715!--    Find out, if the total domain allows more levels. These additional
716!--    levels are processed on PE0 only.
717       IF ( numprocs > 1  .AND.  mg_switch_to_pe0_level /= -1 )  THEN
718          IF ( mg_levels_z > MIN( mg_levels_x, mg_levels_y ) )  THEN
719             mg_switch_to_pe0_level_l = maximum_grid_level
720
721             mg_levels_x = 1
722             mg_levels_y = 1
723
724             i = nx+1
725             DO WHILE ( MOD( i, 2 ) == 0  .AND.  i /= 2 )
726                i = i / 2
727                mg_levels_x = mg_levels_x + 1
728             ENDDO
729
730             j = ny+1
731             DO WHILE ( MOD( j, 2 ) == 0  .AND.  j /= 2 )
732                j = j / 2
733                mg_levels_y = mg_levels_y + 1
734             ENDDO
735
736             maximum_grid_level_l = MIN( mg_levels_x, mg_levels_y, mg_levels_z )
737
738             IF ( maximum_grid_level_l > mg_switch_to_pe0_level_l )  THEN
739                mg_switch_to_pe0_level_l = maximum_grid_level_l - &
740                                           mg_switch_to_pe0_level_l + 1
741             ELSE
742                mg_switch_to_pe0_level_l = 0
743             ENDIF
744          ELSE
745             mg_switch_to_pe0_level_l = 0
746             maximum_grid_level_l = maximum_grid_level
747          ENDIF
748
749!
750!--       Use switch level calculated above only if it is not pre-defined
751!--       by user
752          IF ( mg_switch_to_pe0_level == 0 )  THEN
753
754             IF ( mg_switch_to_pe0_level_l /= 0 )  THEN
755                mg_switch_to_pe0_level = mg_switch_to_pe0_level_l
756                maximum_grid_level     = maximum_grid_level_l
757             ENDIF
758
759          ELSE
760!
761!--          Check pre-defined value and reset to default, if neccessary
762             IF ( mg_switch_to_pe0_level < mg_switch_to_pe0_level_l  .OR.  &
763                  mg_switch_to_pe0_level >= maximum_grid_level_l )  THEN
764                message_string = 'mg_switch_to_pe0_level ' // &
765                                 'out of range and reset to default (=0)'
766                CALL message( 'init_pegrid', 'PA0235', 0, 1, 0, 6, 0 )
767                mg_switch_to_pe0_level = 0
768             ELSE
769!
770!--             Use the largest number of possible levels anyway and recalculate
771!--             the switch level to this largest number of possible values
772                maximum_grid_level = maximum_grid_level_l
773
774             ENDIF
775          ENDIF
776
777       ENDIF
778
779       ALLOCATE( grid_level_count(maximum_grid_level),                   &
780                 nxl_mg(maximum_grid_level), nxr_mg(maximum_grid_level), &
781                 nyn_mg(maximum_grid_level), nys_mg(maximum_grid_level), &
782                 nzt_mg(maximum_grid_level) )
783
784       grid_level_count = 0
785       nxl_l = nxl; nxr_l = nxr; nys_l = nys; nyn_l = nyn; nzt_l = nzt
786
787       DO  i = maximum_grid_level, 1 , -1
788
789          IF ( i == mg_switch_to_pe0_level )  THEN
790#if defined( __parallel )
791!
792!--          Save the grid size of the subdomain at the switch level, because
793!--          it is needed in poismg.
794!--          Array bounds of the local subdomain grids are gathered on PE0
795             ind(1) = nxl_l; ind(2) = nxr_l
796             ind(3) = nys_l; ind(4) = nyn_l
797             ind(5) = nzt_l
798             ALLOCATE( ind_all(5*numprocs), mg_loc_ind(5,0:numprocs-1) )
799             CALL MPI_ALLGATHER( ind, 5, MPI_INTEGER, ind_all, 5, &
800                                 MPI_INTEGER, comm2d, ierr )
801             DO  j = 0, numprocs-1
802                DO  k = 1, 5
803                   mg_loc_ind(k,j) = ind_all(k+j*5)
804                ENDDO
805             ENDDO
806             DEALLOCATE( ind_all )
807!
808!--          Calculate the grid size of the total domain gathered on PE0
809             nxr_l = ( nxr_l-nxl_l+1 ) * pdims(1) - 1
810             nxl_l = 0
811             nyn_l = ( nyn_l-nys_l+1 ) * pdims(2) - 1
812             nys_l = 0
813!
814!--          The size of this gathered array must not be larger than the
815!--          array tend, which is used in the multigrid scheme as a temporary
816!--          array
817             subdomain_size = ( nxr - nxl + 3 )     * ( nyn - nys + 3 )     * &
818                              ( nzt - nzb + 2 )
819             gathered_size  = ( nxr_l - nxl_l + 3 ) * ( nyn_l - nys_l + 3 ) * &
820                              ( nzt_l - nzb + 2 )
821
822             IF ( gathered_size > subdomain_size )  THEN
823                message_string = 'not enough memory for storing ' // &
824                                 'gathered multigrid data on PE0'
825                CALL message( 'init_pegrid', 'PA0236', 1, 2, 0, 6, 0 )
826             ENDIF
827#else
828             message_string = 'multigrid gather/scatter impossible ' // &
829                          'in non parallel mode'
830             CALL message( 'init_pegrid', 'PA0237', 1, 2, 0, 6, 0 )
831#endif
832          ENDIF
833
834          nxl_mg(i) = nxl_l
835          nxr_mg(i) = nxr_l
836          nys_mg(i) = nys_l
837          nyn_mg(i) = nyn_l
838          nzt_mg(i) = nzt_l
839
840          nxl_l = nxl_l / 2 
841          nxr_l = nxr_l / 2
842          nys_l = nys_l / 2 
843          nyn_l = nyn_l / 2 
844          nzt_l = nzt_l / 2 
845       ENDDO
846
847    ELSE
848
849       maximum_grid_level = 1
850
851    ENDIF
852
853    grid_level = maximum_grid_level
854
855#if defined( __parallel )
856!
857!-- Gridpoint number for the exchange of ghost points (y-line for 2D-arrays)
858    ngp_y  = nyn - nys + 1
859
860!
861!-- Define a new MPI derived datatype for the exchange of ghost points in
862!-- y-direction for 2D-arrays (line)
863    CALL MPI_TYPE_VECTOR( nxr-nxl+3, 1, ngp_y+2, MPI_REAL, type_x, ierr )
864    CALL MPI_TYPE_COMMIT( type_x, ierr )
865    CALL MPI_TYPE_VECTOR( nxr-nxl+3, 1, ngp_y+2, MPI_INTEGER, type_x_int, ierr )
866    CALL MPI_TYPE_COMMIT( type_x_int, ierr )
867
868!
869!-- Calculate gridpoint numbers for the exchange of ghost points along x
870!-- (yz-plane for 3D-arrays) and define MPI derived data type(s) for the
871!-- exchange of ghost points in y-direction (xz-plane).
872!-- Do these calculations for the model grid and (if necessary) also
873!-- for the coarser grid levels used in the multigrid method
874    ALLOCATE ( ngp_yz(maximum_grid_level), type_xz(maximum_grid_level) )
875
876    nxl_l = nxl; nxr_l = nxr; nys_l = nys; nyn_l = nyn; nzb_l = nzb; nzt_l = nzt
877         
878    DO i = maximum_grid_level, 1 , -1
879       ngp_yz(i) = (nzt_l - nzb_l + 2) * (nyn_l - nys_l + 3)
880
881       CALL MPI_TYPE_VECTOR( nxr_l-nxl_l+3, nzt_l-nzb_l+2, ngp_yz(i), &
882                             MPI_REAL, type_xz(i), ierr )
883       CALL MPI_TYPE_COMMIT( type_xz(i), ierr )
884
885       nxl_l = nxl_l / 2 
886       nxr_l = nxr_l / 2
887       nys_l = nys_l / 2 
888       nyn_l = nyn_l / 2 
889       nzt_l = nzt_l / 2 
890    ENDDO
891#endif
892
893#if defined( __parallel )
894!
895!-- Setting of flags for inflow/outflow conditions in case of non-cyclic
896!-- horizontal boundary conditions.
897    IF ( pleft == MPI_PROC_NULL )  THEN
898       IF ( bc_lr == 'dirichlet/radiation' )  THEN
899          inflow_l  = .TRUE.
900       ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
901          outflow_l = .TRUE.
902       ENDIF
903    ENDIF
904
905    IF ( pright == MPI_PROC_NULL )  THEN
906       IF ( bc_lr == 'dirichlet/radiation' )  THEN
907          outflow_r = .TRUE.
908       ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
909          inflow_r  = .TRUE.
910       ENDIF
911    ENDIF
912
913    IF ( psouth == MPI_PROC_NULL )  THEN
914       IF ( bc_ns == 'dirichlet/radiation' )  THEN
915          outflow_s = .TRUE.
916       ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
917          inflow_s  = .TRUE.
918       ENDIF
919    ENDIF
920
921    IF ( pnorth == MPI_PROC_NULL )  THEN
922       IF ( bc_ns == 'dirichlet/radiation' )  THEN
923          inflow_n  = .TRUE.
924       ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
925          outflow_n = .TRUE.
926       ENDIF
927    ENDIF
928
929!
930!-- Broadcast the id of the inflow PE
931    IF ( inflow_l )  THEN
932       id_inflow_l = myidx
933    ELSE
934       id_inflow_l = 0
935    ENDIF
936    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
937    CALL MPI_ALLREDUCE( id_inflow_l, id_inflow, 1, MPI_INTEGER, MPI_SUM, &
938                        comm1dx, ierr )
939
940!
941!-- Broadcast the id of the recycling plane
942!-- WARNING: needs to be adjusted in case of inflows other than from left side!
943    IF ( ( recycling_width / dx ) >= nxl  .AND. &
944         ( recycling_width / dx ) <= nxr )  THEN
945       id_recycling_l = myidx
946    ELSE
947       id_recycling_l = 0
948    ENDIF
949    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
950    CALL MPI_ALLREDUCE( id_recycling_l, id_recycling, 1, MPI_INTEGER, MPI_SUM, &
951                        comm1dx, ierr )
952
953#else
954    IF ( bc_lr == 'dirichlet/radiation' )  THEN
955       inflow_l  = .TRUE.
956       outflow_r = .TRUE.
957    ELSEIF ( bc_lr == 'radiation/dirichlet' )  THEN
958       outflow_l = .TRUE.
959       inflow_r  = .TRUE.
960    ENDIF
961
962    IF ( bc_ns == 'dirichlet/radiation' )  THEN
963       inflow_n  = .TRUE.
964       outflow_s = .TRUE.
965    ELSEIF ( bc_ns == 'radiation/dirichlet' )  THEN
966       outflow_n = .TRUE.
967       inflow_s  = .TRUE.
968    ENDIF
969#endif
970!
971!-- At the outflow, u or v, respectively, have to be calculated for one more
972!-- grid point.
973    IF ( outflow_l )  THEN
974       nxlu = nxl + 1
975    ELSE
976       nxlu = nxl
977    ENDIF
978    IF ( outflow_s )  THEN
979       nysv = nys + 1
980    ELSE
981       nysv = nys
982    ENDIF
983
984    IF ( psolver == 'poisfft_hybrid' )  THEN
985       CALL poisfft_hybrid_ini
986    ELSEIF ( psolver == 'poisfft' )  THEN
987       CALL poisfft_init
988    ENDIF
989
990!
991!-- Allocate wall flag arrays used in the multigrid solver
992    IF ( psolver == 'multigrid' )  THEN
993
994       DO  i = maximum_grid_level, 1, -1
995
996           SELECT CASE ( i )
997
998              CASE ( 1 )
999                 ALLOCATE( wall_flags_1(nzb:nzt_mg(i)+1,         &
1000                                        nys_mg(i)-1:nyn_mg(i)+1, &
1001                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1002
1003              CASE ( 2 )
1004                 ALLOCATE( wall_flags_2(nzb:nzt_mg(i)+1,         &
1005                                        nys_mg(i)-1:nyn_mg(i)+1, &
1006                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1007
1008              CASE ( 3 )
1009                 ALLOCATE( wall_flags_3(nzb:nzt_mg(i)+1,         &
1010                                        nys_mg(i)-1:nyn_mg(i)+1, &
1011                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1012
1013              CASE ( 4 )
1014                 ALLOCATE( wall_flags_4(nzb:nzt_mg(i)+1,         &
1015                                        nys_mg(i)-1:nyn_mg(i)+1, &
1016                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1017
1018              CASE ( 5 )
1019                 ALLOCATE( wall_flags_5(nzb:nzt_mg(i)+1,         &
1020                                        nys_mg(i)-1:nyn_mg(i)+1, &
1021                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1022
1023              CASE ( 6 )
1024                 ALLOCATE( wall_flags_6(nzb:nzt_mg(i)+1,         &
1025                                        nys_mg(i)-1:nyn_mg(i)+1, &
1026                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1027
1028              CASE ( 7 )
1029                 ALLOCATE( wall_flags_7(nzb:nzt_mg(i)+1,         &
1030                                        nys_mg(i)-1:nyn_mg(i)+1, &
1031                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1032
1033              CASE ( 8 )
1034                 ALLOCATE( wall_flags_8(nzb:nzt_mg(i)+1,         &
1035                                        nys_mg(i)-1:nyn_mg(i)+1, &
1036                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1037
1038              CASE ( 9 )
1039                 ALLOCATE( wall_flags_9(nzb:nzt_mg(i)+1,         &
1040                                        nys_mg(i)-1:nyn_mg(i)+1, &
1041                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1042
1043              CASE ( 10 )
1044                 ALLOCATE( wall_flags_10(nzb:nzt_mg(i)+1,        &
1045                                        nys_mg(i)-1:nyn_mg(i)+1, &
1046                                        nxl_mg(i)-1:nxr_mg(i)+1) )
1047
1048              CASE DEFAULT
1049                 message_string = 'more than 10 multigrid levels'
1050                 CALL message( 'init_pegrid', 'PA0238', 1, 2, 0, 6, 0 )
1051
1052          END SELECT
1053
1054       ENDDO
1055
1056    ENDIF
1057
1058 END SUBROUTINE init_pegrid
Note: See TracBrowser for help on using the repository browser.