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

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

preliminary changes for radiation conditions

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