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

Last change on this file since 106 was 106, checked in by raasch, 17 years ago

preliminary update of bugfixes and extensions for non-cyclic BCs

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