source: palm/trunk/SOURCE/pmc_interface_mod.f90 @ 1878

Last change on this file since 1878 was 1878, checked in by hellstea, 8 years ago

New simpler synchronization for nested runs

  • Property svn:keywords set to Id
File size: 150.4 KB
Line 
1MODULE pmc_interface
2
3!--------------------------------------------------------------------------------!
4! This file is part of PALM.
5!
6! PALM is free software: you can redistribute it and/or modify it under the terms
7! of the GNU General Public License as published by the Free Software Foundation,
8! either version 3 of the License, or (at your option) any later version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2016 Leibniz Universitaet Hannover
18!--------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22! Synchronization rewritten, logc-array index order changed for cache optimization
23!
24! Former revisions:
25! -----------------
26! $Id: pmc_interface_mod.f90 1878 2016-04-19 12:30:36Z hellstea $
27!
28! 1850 2016-04-08 13:29:27Z maronga
29! Module renamed
30!
31!
32! 1808 2016-04-05 19:44:00Z raasch
33! MPI module used by default on all machines
34!
35! 1801 2016-04-05 13:12:47Z raasch
36! bugfix for r1797: zero setting of temperature within topography does not work
37! and has been disabled
38!
39! 1797 2016-03-21 16:50:28Z raasch
40! introduction of different datatransfer modes,
41! further formatting cleanup, parameter checks added (including mismatches
42! between root and client model settings),
43! +routine pmci_check_setting_mismatches
44! comm_world_nesting introduced
45!
46! 1791 2016-03-11 10:41:25Z raasch
47! routine pmci_update_new removed,
48! pmc_get_local_model_info renamed pmc_get_model_info, some keywords also
49! renamed,
50! filling up redundant ghost points introduced,
51! some index bound variables renamed,
52! further formatting cleanup
53!
54! 1783 2016-03-06 18:36:17Z raasch
55! calculation of nest top area simplified,
56! interpolation and anterpolation moved to seperate wrapper subroutines
57!
58! 1781 2016-03-03 15:12:23Z raasch
59! _p arrays are set zero within buildings too, t.._m arrays and respective
60! settings within buildings completely removed
61!
62! 1779 2016-03-03 08:01:28Z raasch
63! only the total number of PEs is given for the domains, npe_x and npe_y
64! replaced by npe_total, two unused elements removed from array
65! define_coarse_grid_real,
66! array management changed from linked list to sequential loop
67!
68! 1766 2016-02-29 08:37:15Z raasch
69! modifications to allow for using PALM's pointer version,
70! +new routine pmci_set_swaplevel
71!
72! 1764 2016-02-28 12:45:19Z raasch
73! +cpl_parent_id,
74! cpp-statements for nesting replaced by __parallel statements,
75! errors output with message-subroutine,
76! index bugfixes in pmci_interp_tril_all,
77! some adjustments to PALM style
78!
79! 1762 2016-02-25 12:31:13Z hellstea
80! Initial revision by A. Hellsten
81!
82! Description:
83! ------------
84! Domain nesting interface routines. The low-level inter-domain communication   
85! is conducted by the PMC-library routines.
86!------------------------------------------------------------------------------!
87
88#if defined( __nopointer )
89    USE arrays_3d,                                                             &
90        ONLY:  dzu, dzw, e, e_p, pt, pt_p, q, q_p, u, u_p, v, v_p, w, w_p, zu, &
91               zw, z0
92#else
93   USE arrays_3d,                                                              &
94        ONLY:  dzu, dzw, e, e_p, e_1, e_2, pt, pt_p, pt_1, pt_2, q, q_p, q_1,  &
95               q_2, u, u_p, u_1, u_2, v, v_p, v_1, v_2, w, w_p, w_1, w_2, zu,  &
96               zw, z0
97#endif
98
99    USE control_parameters,                                                    &
100        ONLY:  coupling_char, dt_3d, dz, humidity, message_string,             &
101               nest_bound_l, nest_bound_r, nest_bound_s, nest_bound_n,         &
102               nest_domain, passive_scalar, simulated_time, topography,        &
103               volume_flow
104
105    USE cpulog,                                                                &
106        ONLY:  cpu_log, log_point_s
107
108    USE grid_variables,                                                        &
109        ONLY:  dx, dy
110
111    USE indices,                                                               &
112        ONLY:  nbgp, nx, nxl, nxlg, nxlu, nxr, nxrg, ny, nyn, nyng, nys, nysg, &
113               nysv, nz, nzb, nzb_s_inner, nzb_u_inner, nzb_u_outer,           &
114               nzb_v_inner, nzb_v_outer, nzb_w_inner, nzb_w_outer, nzt
115
116    USE kinds
117
118#if defined( __parallel )
119#if defined( __mpifh )
120    INCLUDE "mpif.h"
121#else
122    USE MPI
123#endif
124
125    USE pegrid,                                                                &
126        ONLY:  collective_wait, comm1dx, comm1dy, comm2d, myid, myidx, myidy,  &
127               numprocs
128
129    USE pmc_client,                                                            &
130        ONLY:  pmc_clientinit, pmc_c_clear_next_array_list,                    &
131               pmc_c_getnextarray, pmc_c_get_2d_index_list, pmc_c_getbuffer,   &
132               pmc_c_putbuffer, pmc_c_setind_and_allocmem,                     &
133               pmc_c_set_dataarray, pmc_set_dataarray_name
134
135    USE pmc_general,                                                           &
136        ONLY:  da_namelen, pmc_max_modell, pmc_status_ok
137
138    USE pmc_handle_communicator,                                               &
139        ONLY:  pmc_get_model_info, pmc_init_model, pmc_is_rootmodel,           &
140               pmc_no_namelist_found, pmc_server_for_client
141
142    USE pmc_mpi_wrapper,                                                       &
143        ONLY:  pmc_bcast, pmc_recv_from_client, pmc_recv_from_server,          &
144               pmc_send_to_client, pmc_send_to_server
145
146    USE pmc_server,                                                            &
147        ONLY:  pmc_serverinit, pmc_s_clear_next_array_list, pmc_s_fillbuffer,  &
148               pmc_s_getdata_from_buffer, pmc_s_getnextarray,                  &
149               pmc_s_setind_and_allocmem, pmc_s_set_active_data_array,         &
150               pmc_s_set_dataarray, pmc_s_set_2d_index_list
151
152#endif
153
154    IMPLICIT NONE
155
156    PRIVATE
157
158!
159!-- Constants
160    INTEGER(iwp), PARAMETER ::  client_to_server = 2   !:
161    INTEGER(iwp), PARAMETER ::  server_to_client = 1   !:
162
163!
164!-- Coupler setup
165    INTEGER(iwp), SAVE      ::  comm_world_nesting     !:
166    INTEGER(iwp), SAVE      ::  cpl_id  = 1            !:
167    CHARACTER(LEN=32), SAVE ::  cpl_name               !:
168    INTEGER(iwp), SAVE      ::  cpl_npe_total          !:
169    INTEGER(iwp), SAVE      ::  cpl_parent_id          !:
170
171!
172!-- Control parameters, will be made input parameters later
173    CHARACTER(LEN=7), SAVE ::  nesting_datatransfer_mode = 'mixed'  !: steering
174                                                         !: parameter for data-
175                                                         !: transfer mode
176    CHARACTER(LEN=7), SAVE ::  nesting_mode = 'two-way'  !: steering parameter
177                                                         !: for 1- or 2-way nesting
178
179    LOGICAL, SAVE ::  nested_run = .FALSE.  !: general switch
180
181    REAL(wp), SAVE ::  anterp_relax_length_l = -1.0_wp   !:
182    REAL(wp), SAVE ::  anterp_relax_length_r = -1.0_wp   !:
183    REAL(wp), SAVE ::  anterp_relax_length_s = -1.0_wp   !:
184    REAL(wp), SAVE ::  anterp_relax_length_n = -1.0_wp   !:
185    REAL(wp), SAVE ::  anterp_relax_length_t = -1.0_wp   !:
186
187!
188!-- Geometry
189    REAL(wp), SAVE                            ::  area_t               !:
190    REAL(wp), SAVE, DIMENSION(:), ALLOCATABLE ::  coord_x              !:
191    REAL(wp), SAVE, DIMENSION(:), ALLOCATABLE ::  coord_y              !:
192    REAL(wp), SAVE                            ::  lower_left_coord_x   !:
193    REAL(wp), SAVE                            ::  lower_left_coord_y   !:
194
195!
196!-- Client coarse data arrays
197    INTEGER(iwp), DIMENSION(5)                  ::  coarse_bound   !:
198
199    REAL(wp), SAVE                              ::  xexl           !:
200    REAL(wp), SAVE                              ::  xexr           !:
201    REAL(wp), SAVE                              ::  yexs           !:
202    REAL(wp), SAVE                              ::  yexn           !:
203    REAL(wp), SAVE, DIMENSION(:,:), ALLOCATABLE ::  tkefactor_l    !:
204    REAL(wp), SAVE, DIMENSION(:,:), ALLOCATABLE ::  tkefactor_n    !:
205    REAL(wp), SAVE, DIMENSION(:,:), ALLOCATABLE ::  tkefactor_r    !:
206    REAL(wp), SAVE, DIMENSION(:,:), ALLOCATABLE ::  tkefactor_s    !:
207    REAL(wp), SAVE, DIMENSION(:,:), ALLOCATABLE ::  tkefactor_t    !:
208
209    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ec   !:
210    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  ptc  !:
211    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  uc   !:
212    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  vc   !:
213    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  wc   !:
214    REAL(wp), SAVE, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  qc   !:
215
216!
217!-- Client interpolation coefficients and client-array indices to be precomputed
218!-- and stored.
219    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  ico    !:
220    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  icu    !:
221    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jco    !:
222    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jcv    !:
223    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kco    !:
224    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kcw    !:
225    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1xo   !:
226    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2xo   !:
227    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1xu   !:
228    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2xu   !:
229    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1yo   !:
230    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2yo   !:
231    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1yv   !:
232    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2yv   !:
233    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1zo   !:
234    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2zo   !:
235    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r1zw   !:
236    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:)     ::  r2zw   !:
237
238!
239!-- Client index arrays and log-ratio arrays for the log-law near-wall
240!-- corrections. These are not truly 3-D arrays but multiple 2-D arrays.
241    INTEGER(iwp), SAVE :: ncorr  !: 4th dimension of the log_ratio-arrays
242    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_u_l   !:
243    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_u_n   !:
244    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_u_r   !:
245    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_u_s   !:
246    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_v_l   !:
247    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_v_n   !:
248    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_v_r   !:
249    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_v_s   !:
250    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_w_l   !:
251    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_w_n   !:
252    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_w_r   !:
253    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:,:,:) ::  logc_w_s   !:
254    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_u_l   !:
255    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_u_n   !:
256    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_u_r   !:
257    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_u_s   !:
258    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_v_l   !:
259    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_v_n   !:
260    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_v_r   !:
261    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_v_s   !:
262    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_w_l   !:
263    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_w_n   !:
264    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_w_r   !:
265    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:,:,:,:)   ::  logc_ratio_w_s   !:
266
267!
268!-- Upper bounds for k in anterpolation.
269    INTEGER(iwp), SAVE ::  kctu   !:
270    INTEGER(iwp), SAVE ::  kctw   !:
271
272!
273!-- Upper bound for k in log-law correction in interpolation.
274    INTEGER(iwp), SAVE ::  nzt_topo_nestbc_l   !:
275    INTEGER(iwp), SAVE ::  nzt_topo_nestbc_n   !:
276    INTEGER(iwp), SAVE ::  nzt_topo_nestbc_r   !:
277    INTEGER(iwp), SAVE ::  nzt_topo_nestbc_s   !:
278
279!
280!-- Number of ghost nodes in coarse-grid arrays for i and j in anterpolation.
281    INTEGER(iwp), SAVE ::  nhll   !:
282    INTEGER(iwp), SAVE ::  nhlr   !:
283    INTEGER(iwp), SAVE ::  nhls   !:
284    INTEGER(iwp), SAVE ::  nhln   !:
285
286!
287!-- Spatial under-relaxation coefficients for anterpolation.
288    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:) ::  frax   !:
289    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:) ::  fray   !:
290    REAL(wp), SAVE, ALLOCATABLE, DIMENSION(:) ::  fraz   !:
291
292!
293!-- Client-array indices to be precomputed and stored for anterpolation.
294    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  iflu   !:
295    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  ifuu   !:
296    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  iflo   !:
297    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  ifuo   !:
298    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jflv   !:
299    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jfuv   !:
300    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jflo   !:
301    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  jfuo   !:
302    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kflw   !:
303    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kfuw   !:
304    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kflo   !:
305    INTEGER(iwp), SAVE, ALLOCATABLE, DIMENSION(:) ::  kfuo   !:
306
307    INTEGER(iwp), DIMENSION(3)          ::  define_coarse_grid_int    !:
308    REAL(wp), DIMENSION(7)              ::  define_coarse_grid_real   !:
309
310    TYPE coarsegrid_def
311       INTEGER(iwp)                        ::  nx
312       INTEGER(iwp)                        ::  ny
313       INTEGER(iwp)                        ::  nz
314       REAL(wp)                            ::  dx
315       REAL(wp)                            ::  dy
316       REAL(wp)                            ::  dz
317       REAL(wp)                            ::  lower_left_coord_x
318       REAL(wp)                            ::  lower_left_coord_y
319       REAL(wp)                            ::  xend
320       REAL(wp)                            ::  yend
321       REAL(wp), DIMENSION(:), ALLOCATABLE ::  coord_x
322       REAL(wp), DIMENSION(:), ALLOCATABLE ::  coord_y
323       REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzu       
324       REAL(wp), DIMENSION(:), ALLOCATABLE ::  dzw       
325       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zu       
326       REAL(wp), DIMENSION(:), ALLOCATABLE ::  zw       
327    END TYPE coarsegrid_def
328                                         
329    TYPE(coarsegrid_def), SAVE ::  cg   !:
330
331
332    INTERFACE pmci_check_setting_mismatches
333       MODULE PROCEDURE pmci_check_setting_mismatches
334    END INTERFACE
335
336    INTERFACE pmci_client_initialize
337       MODULE PROCEDURE pmci_client_initialize
338    END INTERFACE
339
340    INTERFACE pmci_synchronize
341       MODULE PROCEDURE pmci_synchronize
342    END INTERFACE
343
344    INTERFACE pmci_datatrans
345       MODULE PROCEDURE pmci_datatrans
346    END INTERFACE pmci_datatrans
347
348    INTERFACE pmci_ensure_nest_mass_conservation
349       MODULE PROCEDURE pmci_ensure_nest_mass_conservation
350    END INTERFACE
351
352    INTERFACE pmci_init
353       MODULE PROCEDURE pmci_init
354    END INTERFACE
355
356    INTERFACE pmci_modelconfiguration
357       MODULE PROCEDURE pmci_modelconfiguration
358    END INTERFACE
359
360    INTERFACE pmci_server_initialize
361       MODULE PROCEDURE pmci_server_initialize
362    END INTERFACE
363
364    INTERFACE pmci_set_swaplevel
365       MODULE PROCEDURE pmci_set_swaplevel
366    END INTERFACE pmci_set_swaplevel
367
368    PUBLIC anterp_relax_length_l, anterp_relax_length_r,                       &
369           anterp_relax_length_s, anterp_relax_length_n,                       &
370           anterp_relax_length_t, client_to_server, comm_world_nesting,        &
371           cpl_id, nested_run, nesting_datatransfer_mode, nesting_mode,        &
372           server_to_client
373    PUBLIC pmci_client_initialize
374    PUBLIC pmci_datatrans
375    PUBLIC pmci_ensure_nest_mass_conservation
376    PUBLIC pmci_init
377    PUBLIC pmci_modelconfiguration
378    PUBLIC pmci_server_initialize
379    PUBLIC pmci_synchronize
380    PUBLIC pmci_set_swaplevel
381
382
383 CONTAINS
384
385
386 SUBROUTINE pmci_init( world_comm )
387
388    USE control_parameters,                                                  &
389        ONLY:  message_string
390
391    IMPLICIT NONE
392
393    INTEGER, INTENT(OUT) ::  world_comm   !:
394
395#if defined( __parallel )
396
397    INTEGER(iwp)         ::  ierr         !:
398    INTEGER(iwp)         ::  istat        !:
399    INTEGER(iwp)         ::  pmc_status   !:
400
401
402    CALL pmc_init_model( world_comm, nesting_datatransfer_mode, nesting_mode,  &
403                         pmc_status )
404
405    IF ( pmc_status == pmc_no_namelist_found )  THEN
406!
407!--    This is not a nested run
408       world_comm = MPI_COMM_WORLD
409       cpl_id     = 1
410       cpl_name   = ""
411
412       RETURN
413
414    ENDIF
415
416!
417!-- Check steering parameter values
418    IF ( TRIM( nesting_mode ) /= 'one-way'  .AND.                              &
419         TRIM( nesting_mode ) /= 'two-way' )                                   &
420    THEN
421       message_string = 'illegal nesting mode: ' // TRIM( nesting_mode )
422       CALL message( 'pmci_init', 'PA0417', 3, 2, 0, 6, 0 )
423    ENDIF
424
425    IF ( TRIM( nesting_datatransfer_mode ) /= 'cascade'  .AND.                 &
426         TRIM( nesting_datatransfer_mode ) /= 'mixed'    .AND.                 &
427         TRIM( nesting_datatransfer_mode ) /= 'overlap' )                      &
428    THEN
429       message_string = 'illegal nesting datatransfer mode: '                  &
430                        // TRIM( nesting_datatransfer_mode )
431       CALL message( 'pmci_init', 'PA0418', 3, 2, 0, 6, 0 )
432    ENDIF
433
434!
435!-- Set the general steering switch which tells PALM that its a nested run
436    nested_run = .TRUE.
437
438!
439!-- Get some variables required by the pmc-interface (and in some cases in the
440!-- PALM code out of the pmci) out of the pmc-core
441    CALL pmc_get_model_info( comm_world_nesting = comm_world_nesting,          &
442                             cpl_id = cpl_id, cpl_parent_id = cpl_parent_id,   &
443                             cpl_name = cpl_name, npe_total = cpl_npe_total,   &
444                             lower_left_x = lower_left_coord_x,                &
445                             lower_left_y = lower_left_coord_y )
446!
447!-- Set the steering switch which tells the models that they are nested (of
448!-- course the root domain (cpl_id = 1) is not nested)
449    IF ( cpl_id >= 2 )  THEN
450       nest_domain = .TRUE.
451       WRITE( coupling_char, '(A1,I2.2)') '_', cpl_id
452    ENDIF
453
454!
455!-- Message that communicators for nesting are initialized.
456!-- Attention: myid has been set at the end of pmc_init_model in order to
457!-- guarantee that only PE0 of the root domain does the output.
458    CALL location_message( 'finished', .TRUE. )
459!
460!-- Reset myid to its default value
461    myid = 0
462#else
463!
464!-- Nesting cannot be used in serial mode. cpl_id is set to root domain (1)
465!-- because no location messages would be generated otherwise.
466!-- world_comm is given a dummy value to avoid compiler warnings (INTENT(OUT)
467!-- should get an explicit value)
468    cpl_id     = 1
469    nested_run = .FALSE.
470    world_comm = 1
471#endif
472
473 END SUBROUTINE pmci_init
474
475
476
477 SUBROUTINE pmci_modelconfiguration
478
479    IMPLICIT NONE
480
481    CALL location_message( 'setup the nested model configuration', .FALSE. )
482!
483!-- Compute absolute coordinates for all models
484    CALL pmci_setup_coordinates
485!
486!-- Initialize the client (must be called before pmc_setup_server)
487    CALL pmci_setup_client
488!
489!-- Initialize PMC Server
490    CALL pmci_setup_server
491!
492!-- Check for mismatches between settings of master and client variables
493!-- (e.g., all clients have to follow the end_time settings of the root master)
494    CALL pmci_check_setting_mismatches
495
496    CALL location_message( 'finished', .TRUE. )
497
498 END SUBROUTINE pmci_modelconfiguration
499
500
501
502 SUBROUTINE pmci_setup_server
503
504#if defined( __parallel )
505    IMPLICIT NONE
506
507    CHARACTER(LEN=32) ::  myname
508
509    INTEGER(iwp) ::  client_id        !:
510    INTEGER(iwp) ::  ierr             !:
511    INTEGER(iwp) ::  i                !:
512    INTEGER(iwp) ::  j                !:
513    INTEGER(iwp) ::  k                !:
514    INTEGER(iwp) ::  m                !:
515    INTEGER(iwp) ::  nomatch          !:
516    INTEGER(iwp) ::  nx_cl            !:
517    INTEGER(iwp) ::  ny_cl            !:
518    INTEGER(iwp) ::  nz_cl            !:
519
520    INTEGER(iwp), DIMENSION(5) ::  val    !:
521
522    REAL(wp) ::  dx_cl            !:
523    REAL(wp) ::  dy_cl            !:
524    REAL(wp) ::  xez              !:
525    REAL(wp) ::  yez              !:
526
527    REAL(wp), DIMENSION(1) ::  fval             !:
528
529    REAL(wp), DIMENSION(:), ALLOCATABLE ::  cl_coord_x   !:
530    REAL(wp), DIMENSION(:), ALLOCATABLE ::  cl_coord_y   !:
531   
532
533!
534!   Initialize the pmc server
535    CALL pmc_serverinit
536
537!
538!-- Get coordinates from all clients
539    DO  m = 1, SIZE( pmc_server_for_client ) - 1
540
541       client_id = pmc_server_for_client(m)
542       IF ( myid == 0 )  THEN       
543
544          CALL pmc_recv_from_client( client_id, val,  size(val),  0, 123, ierr )
545          CALL pmc_recv_from_client( client_id, fval, size(fval), 0, 124, ierr )
546         
547          nx_cl = val(1)
548          ny_cl = val(2)
549          dx_cl = val(4)
550          dy_cl = val(5)
551
552          nz_cl = nz
553
554!
555!--       Find the highest client level in the coarse grid for the reduced z
556!--       transfer
557          DO  k = 1, nz                 
558             IF ( zw(k) > fval(1) )  THEN
559                nz_cl = k
560                EXIT
561             ENDIF
562          ENDDO
563
564!   
565!--       Get absolute coordinates from the client
566          ALLOCATE( cl_coord_x(-nbgp:nx_cl+nbgp) )
567          ALLOCATE( cl_coord_y(-nbgp:ny_cl+nbgp) )
568         
569          CALL pmc_recv_from_client( client_id, cl_coord_x, SIZE( cl_coord_x ),&
570                                     0, 11, ierr )
571          CALL pmc_recv_from_client( client_id, cl_coord_y, SIZE( cl_coord_y ),&
572                                     0, 12, ierr )
573!          WRITE ( 0, * )  'receive from pmc Client ', client_id, nx_cl, ny_cl
574         
575          define_coarse_grid_real(1) = lower_left_coord_x
576          define_coarse_grid_real(2) = lower_left_coord_y
577          define_coarse_grid_real(3) = dx
578          define_coarse_grid_real(4) = dy
579          define_coarse_grid_real(5) = lower_left_coord_x + ( nx + 1 ) * dx
580          define_coarse_grid_real(6) = lower_left_coord_y + ( ny + 1 ) * dy
581          define_coarse_grid_real(7) = dz
582
583          define_coarse_grid_int(1)  = nx
584          define_coarse_grid_int(2)  = ny
585          define_coarse_grid_int(3)  = nz_cl
586
587!
588!--       Check that the client domain is completely inside the server domain.
589          nomatch = 0
590          xez = ( nbgp + 1 ) * dx 
591          yez = ( nbgp + 1 ) * dy 
592          IF ( ( cl_coord_x(0) < define_coarse_grid_real(1) + xez )       .OR. &
593               ( cl_coord_x(nx_cl+1) > define_coarse_grid_real(5) - xez ) .OR. &
594               ( cl_coord_y(0) < define_coarse_grid_real(2) + yez )       .OR. &
595               ( cl_coord_y(ny_cl+1) > define_coarse_grid_real(6) - yez ) )    &
596          THEN
597             nomatch = 1
598          ENDIF
599
600          DEALLOCATE( cl_coord_x )
601          DEALLOCATE( cl_coord_y )
602
603!
604!--       Send coarse grid information to client
605          CALL pmc_send_to_client( client_id, define_coarse_grid_real,         &
606                                   SIZE( define_coarse_grid_real ), 0, 21,     &
607                                   ierr )
608          CALL pmc_send_to_client( client_id, define_coarse_grid_int,  3, 0,   &
609                                   22, ierr )
610
611!
612!--       Send local grid to client
613          CALL pmc_send_to_client( client_id, coord_x, nx+1+2*nbgp, 0, 24,     &
614                                   ierr )
615          CALL pmc_send_to_client( client_id, coord_y, ny+1+2*nbgp, 0, 25,     &
616                                   ierr )
617
618!
619!--       Also send the dzu-, dzw-, zu- and zw-arrays here
620          CALL pmc_send_to_client( client_id, dzu, nz_cl+1, 0, 26, ierr )
621          CALL pmc_send_to_client( client_id, dzw, nz_cl+1, 0, 27, ierr )
622          CALL pmc_send_to_client( client_id, zu,  nz_cl+2, 0, 28, ierr )
623          CALL pmc_send_to_client( client_id, zw,  nz_cl+2, 0, 29, ierr )
624
625       ENDIF
626
627       CALL MPI_BCAST( nomatch, 1, MPI_INTEGER, 0, comm2d, ierr )
628       IF ( nomatch /= 0 ) THEN
629          WRITE ( message_string, * )  'Error: nested client domain does ',    &
630                                       'not fit into its server domain'
631          CALL message( 'pmc_palm_setup_server', 'PA0XYZ', 1, 2, 0, 6, 0 )
632       ENDIF
633     
634       CALL MPI_BCAST( nz_cl, 1, MPI_INTEGER, 0, comm2d, ierr )
635
636!
637!--    TO_DO: Klaus: please give a comment what is done here
638       CALL pmci_create_index_list
639
640!
641!--    Include couple arrays into server content
642!--    TO_DO: Klaus: please give a more meaningful comment
643       CALL pmc_s_clear_next_array_list
644       DO  WHILE ( pmc_s_getnextarray( client_id, myname ) )
645          CALL pmci_set_array_pointer( myname, client_id = client_id,          &
646                                       nz_cl = nz_cl )
647       ENDDO
648       CALL pmc_s_setind_and_allocmem( client_id )
649    ENDDO
650
651 CONTAINS
652
653
654   SUBROUTINE pmci_create_index_list
655
656       IMPLICIT NONE
657
658       INTEGER(iwp) ::  i                  !:
659       INTEGER(iwp) ::  ic                 !:
660       INTEGER(iwp) ::  ierr               !:
661       INTEGER(iwp) ::  j                  !:
662       INTEGER(iwp) ::  k                  !:
663       INTEGER(iwp) ::  m                  !:
664       INTEGER(iwp) ::  n                  !:
665       INTEGER(iwp) ::  npx                !:
666       INTEGER(iwp) ::  npy                !:
667       INTEGER(iwp) ::  nrx                !:
668       INTEGER(iwp) ::  nry                !:
669       INTEGER(iwp) ::  px                 !:
670       INTEGER(iwp) ::  py                 !:
671       INTEGER(iwp) ::  server_pe          !:
672
673       INTEGER(iwp), DIMENSION(2) ::  scoord             !:
674       INTEGER(iwp), DIMENSION(2) ::  size_of_array      !:
675
676       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE  ::  coarse_bound_all   !:
677       INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE  ::  index_list         !:
678
679       IF ( myid == 0 )  THEN
680!--       TO_DO: Klaus: give more specific comment what size_of_array stands for
681          CALL pmc_recv_from_client( client_id, size_of_array, 2, 0, 40, ierr )
682          ALLOCATE( coarse_bound_all(size_of_array(1),size_of_array(2)) )
683          CALL pmc_recv_from_client( client_id, coarse_bound_all,              &
684                                     SIZE( coarse_bound_all ), 0, 41, ierr )
685
686!
687!--       Compute size of index_list.
688          ic = 0
689          DO  k = 1, size_of_array(2)
690             DO  j = coarse_bound_all(3,k), coarse_bound_all(4,k)
691                DO  i = coarse_bound_all(1,k), coarse_bound_all(2,k)
692                   ic = ic + 1
693                ENDDO
694             ENDDO
695          ENDDO
696
697          ALLOCATE( index_list(6,ic) )
698
699          CALL MPI_COMM_SIZE( comm1dx, npx, ierr )
700          CALL MPI_COMM_SIZE( comm1dy, npy, ierr )
701!
702!--       The +1 in index is because PALM starts with nx=0
703          nrx = nxr - nxl + 1
704          nry = nyn - nys + 1
705          ic  = 0
706!
707!--       Loop over all client PEs
708          DO  k = 1, size_of_array(2)
709!
710!--          Area along y required by actual client PE
711             DO  j = coarse_bound_all(3,k), coarse_bound_all(4,k)
712!
713!--             Area along x required by actual client PE
714                DO  i = coarse_bound_all(1,k), coarse_bound_all(2,k)
715
716                   px = i / nrx
717                   py = j / nry
718                   scoord(1) = px
719                   scoord(2) = py
720                   CALL MPI_CART_RANK( comm2d, scoord, server_pe, ierr )
721                 
722                   ic = ic + 1
723!
724!--                First index in server array
725                   index_list(1,ic) = i - ( px * nrx ) + 1 + nbgp
726!
727!--                Second index in server array
728                   index_list(2,ic) = j - ( py * nry ) + 1 + nbgp
729!
730!--                x index of client coarse grid
731                   index_list(3,ic) = i - coarse_bound_all(1,k) + 1
732!
733!--                y index of client coarse grid
734                   index_list(4,ic) = j - coarse_bound_all(3,k) + 1
735!
736!--                PE number of client
737                   index_list(5,ic) = k - 1
738!
739!--                PE number of server
740                   index_list(6,ic) = server_pe
741
742                ENDDO
743             ENDDO
744          ENDDO
745!
746!--       TO_DO: Klaus: comment what is done here
747          CALL pmc_s_set_2d_index_list( client_id, index_list(:,1:ic) )
748
749       ELSE
750!
751!--       TO_DO: Klaus: comment why this dummy allocation is required
752          ALLOCATE( index_list(6,1) )
753          CALL pmc_s_set_2d_index_list( client_id, index_list )
754       ENDIF
755
756       DEALLOCATE(index_list)
757
758     END SUBROUTINE pmci_create_index_list
759
760#endif
761 END SUBROUTINE pmci_setup_server
762
763
764
765 SUBROUTINE pmci_setup_client
766
767#if defined( __parallel )
768    IMPLICIT NONE
769
770    CHARACTER(LEN=da_namelen) ::  myname     !:
771
772    INTEGER(iwp) ::  i          !:
773    INTEGER(iwp) ::  ierr       !:
774    INTEGER(iwp) ::  icl        !:
775    INTEGER(iwp) ::  icr        !:
776    INTEGER(iwp) ::  j          !:
777    INTEGER(iwp) ::  jcn        !:
778    INTEGER(iwp) ::  jcs        !:
779
780    INTEGER(iwp), DIMENSION(5) ::  val        !:
781   
782    REAL(wp) ::  xcs        !:
783    REAL(wp) ::  xce        !:
784    REAL(wp) ::  ycs        !:
785    REAL(wp) ::  yce        !:
786
787    REAL(wp), DIMENSION(1) ::  fval       !:
788                                             
789!
790!-- TO_DO: describe what is happening in this if-clause
791!-- Root Model does not have Server and is not a client
792    IF ( .NOT. pmc_is_rootmodel() )  THEN
793
794       CALL pmc_clientinit
795!
796!--    Here and only here the arrays are defined, which actualy will be
797!--    exchanged between client and server.
798!--    Please check, if the arrays are in the list of possible exchange arrays
799!--    in subroutines:
800!--    pmci_set_array_pointer (for server arrays)
801!--    pmci_create_client_arrays (for client arrays)
802       CALL pmc_set_dataarray_name( 'coarse', 'u'  ,'fine', 'u',  ierr )
803       CALL pmc_set_dataarray_name( 'coarse', 'v'  ,'fine', 'v',  ierr )
804       CALL pmc_set_dataarray_name( 'coarse', 'w'  ,'fine', 'w',  ierr )
805       CALL pmc_set_dataarray_name( 'coarse', 'e'  ,'fine', 'e',  ierr )
806       CALL pmc_set_dataarray_name( 'coarse', 'pt' ,'fine', 'pt', ierr )
807       IF ( humidity  .OR.  passive_scalar )  THEN
808          CALL pmc_set_dataarray_name( 'coarse', 'q'  ,'fine', 'q',  ierr )
809       ENDIF
810
811!
812!--    Update this list appropritely and also in create_client_arrays and in
813!--    pmci_set_array_pointer.
814!--    If a variable is removed, it only has to be removed from here.
815       CALL pmc_set_dataarray_name( lastentry = .TRUE. )
816
817!
818!--    Send grid to server
819       val(1)  = nx
820       val(2)  = ny
821       val(3)  = nz
822       val(4)  = dx
823       val(5)  = dy
824       fval(1) = zw(nzt+1)
825
826       IF ( myid == 0 )  THEN
827
828          CALL pmc_send_to_server( val, SIZE( val ), 0, 123, ierr )
829          CALL pmc_send_to_server( fval, SIZE( fval ), 0, 124, ierr )
830          CALL pmc_send_to_server( coord_x, nx + 1 + 2 * nbgp, 0, 11, ierr )
831          CALL pmc_send_to_server( coord_y, ny + 1 + 2 * nbgp, 0, 12, ierr )
832
833!
834!--       Receive Coarse grid information.
835!--       TO_DO: find shorter and more meaningful name for  define_coarse_grid_real
836          CALL pmc_recv_from_server( define_coarse_grid_real,                  &
837                                     SIZE(define_coarse_grid_real), 0, 21, ierr )
838          CALL pmc_recv_from_server( define_coarse_grid_int,  3, 0, 22, ierr )
839!
840!--        Debug-printouts - keep them
841!          WRITE(0,*) 'Coarse grid from Server '
842!          WRITE(0,*) 'startx_tot    = ',define_coarse_grid_real(1)
843!          WRITE(0,*) 'starty_tot    = ',define_coarse_grid_real(2)
844!          WRITE(0,*) 'endx_tot      = ',define_coarse_grid_real(5)
845!          WRITE(0,*) 'endy_tot      = ',define_coarse_grid_real(6)
846!          WRITE(0,*) 'dx            = ',define_coarse_grid_real(3)
847!          WRITE(0,*) 'dy            = ',define_coarse_grid_real(4)
848!          WRITE(0,*) 'dz            = ',define_coarse_grid_real(7)
849!          WRITE(0,*) 'nx_coarse     = ',define_coarse_grid_int(1)
850!          WRITE(0,*) 'ny_coarse     = ',define_coarse_grid_int(2)
851!          WRITE(0,*) 'nz_coarse     = ',define_coarse_grid_int(3)
852       ENDIF
853
854       CALL MPI_BCAST( define_coarse_grid_real, SIZE(define_coarse_grid_real), &
855                       MPI_REAL, 0, comm2d, ierr )
856       CALL MPI_BCAST( define_coarse_grid_int, 3, MPI_INTEGER, 0, comm2d, ierr )
857
858       cg%dx = define_coarse_grid_real(3)
859       cg%dy = define_coarse_grid_real(4)
860       cg%dz = define_coarse_grid_real(7)
861       cg%nx = define_coarse_grid_int(1)
862       cg%ny = define_coarse_grid_int(2)
863       cg%nz = define_coarse_grid_int(3)
864
865!
866!--    Get server coordinates on coarse grid
867       ALLOCATE( cg%coord_x(-nbgp:cg%nx+nbgp) )
868       ALLOCATE( cg%coord_y(-nbgp:cg%ny+nbgp) )
869     
870       ALLOCATE( cg%dzu(1:cg%nz+1) )
871       ALLOCATE( cg%dzw(1:cg%nz+1) )
872       ALLOCATE( cg%zu(0:cg%nz+1) )
873       ALLOCATE( cg%zw(0:cg%nz+1) )
874
875!
876!--    Get coarse grid coordinates and vales of the z-direction from server
877       IF ( myid == 0)  THEN
878
879          CALL pmc_recv_from_server( cg%coord_x, cg%nx+1+2*nbgp, 0, 24, ierr )
880          CALL pmc_recv_from_server( cg%coord_y, cg%ny+1+2*nbgp, 0, 25, ierr )
881          CALL pmc_recv_from_server( cg%dzu, cg%nz + 1, 0, 26, ierr )
882          CALL pmc_recv_from_server( cg%dzw, cg%nz + 1, 0, 27, ierr )
883          CALL pmc_recv_from_server( cg%zu, cg%nz + 2, 0, 28, ierr )
884          CALL pmc_recv_from_server( cg%zw, cg%nz + 2, 0, 29, ierr )
885
886       ENDIF
887
888!
889!--    Broadcast this information
890       CALL MPI_BCAST( cg%coord_x, cg%nx+1+2*nbgp, MPI_REAL, 0, comm2d, ierr )
891       CALL MPI_BCAST( cg%coord_y, cg%ny+1+2*nbgp, MPI_REAL, 0, comm2d, ierr )
892       CALL MPI_BCAST( cg%dzu, cg%nz+1, MPI_REAL, 0, comm2d, ierr )
893       CALL MPI_BCAST( cg%dzw, cg%nz+1, MPI_REAL, 0, comm2d, ierr )
894       CALL MPI_BCAST( cg%zu, cg%nz+2,  MPI_REAL, 0, comm2d, ierr )
895       CALL MPI_BCAST( cg%zw, cg%nz+2,  MPI_REAL, 0, comm2d, ierr )
896       
897!
898!--    Find the index bounds for the nest domain in the coarse-grid index space
899       CALL pmci_map_fine_to_coarse_grid
900!
901!--    TO_DO: Klaus give a comment what is happening here
902       CALL pmc_c_get_2d_index_list
903
904!
905!--    Include couple arrays into client content
906!--    TO_DO: Klaus: better explain the above comment (what is client content?)
907       CALL  pmc_c_clear_next_array_list
908       DO  WHILE ( pmc_c_getnextarray( myname ) )
909!--       TO_DO: Klaus, why the c-arrays are still up to cg%nz??
910          CALL pmci_create_client_arrays ( myname, icl, icr, jcs, jcn, cg%nz )
911       ENDDO
912       CALL pmc_c_setind_and_allocmem
913
914!
915!--    Precompute interpolation coefficients and client-array indices
916       CALL pmci_init_interp_tril
917
918!
919!--    Precompute the log-law correction index- and ratio-arrays
920       CALL pmci_init_loglaw_correction 
921
922!
923!--    Define the SGS-TKE scaling factor based on the grid-spacing ratio
924       CALL pmci_init_tkefactor
925
926!
927!--    Two-way coupling.
928!--    Precompute the index arrays and relaxation functions for the
929!--    anterpolation
930       IF ( nesting_mode == 'two-way' )  THEN
931          CALL pmci_init_anterp_tophat
932       ENDIF
933
934!
935!--    Finally, compute the total area of the top-boundary face of the domain.
936!--    This is needed in the pmc_ensure_nest_mass_conservation     
937       area_t = ( nx + 1 ) * (ny + 1 ) * dx * dy
938
939    ENDIF
940
941 CONTAINS
942
943    SUBROUTINE pmci_map_fine_to_coarse_grid
944!
945!--    Determine index bounds of interpolation/anterpolation area in the coarse
946!--    grid index space
947       IMPLICIT NONE
948
949       INTEGER(iwp), DIMENSION(5,numprocs) ::  coarse_bound_all   !:
950       INTEGER(iwp), DIMENSION(2)          ::  size_of_array      !:
951                                             
952       REAL(wp) ::  loffset     !:
953       REAL(wp) ::  noffset     !:
954       REAL(wp) ::  roffset     !:
955       REAL(wp) ::  soffset     !:
956
957!
958!--    If the fine- and coarse grid nodes do not match:
959       loffset = MOD( coord_x(nxl), cg%dx )
960       xexl    = cg%dx + loffset
961!
962!--    This is needed in the anterpolation phase
963       nhll = CEILING( xexl / cg%dx )
964       xcs  = coord_x(nxl) - xexl
965       DO  i = 0, cg%nx
966          IF ( cg%coord_x(i) > xcs )  THEN
967             icl = MAX( -1, i-1 )
968             EXIT
969          ENDIF
970       ENDDO
971!
972!--    If the fine- and coarse grid nodes do not match
973       roffset = MOD( coord_x(nxr+1), cg%dx )
974       xexr    = cg%dx + roffset
975!
976!--    This is needed in the anterpolation phase
977       nhlr = CEILING( xexr / cg%dx )
978       xce  = coord_x(nxr) + xexr
979       DO  i = cg%nx, 0 , -1
980          IF ( cg%coord_x(i) < xce )  THEN
981             icr = MIN( cg%nx+1, i+1 )
982             EXIT
983          ENDIF
984       ENDDO
985!
986!--    If the fine- and coarse grid nodes do not match
987       soffset = MOD( coord_y(nys), cg%dy )
988       yexs    = cg%dy + soffset
989!
990!--    This is needed in the anterpolation phase
991       nhls = CEILING( yexs / cg%dy )
992       ycs  = coord_y(nys) - yexs
993       DO  j = 0, cg%ny
994          IF ( cg%coord_y(j) > ycs )  THEN
995             jcs = MAX( -nbgp, j-1 )
996             EXIT
997          ENDIF
998       ENDDO
999!
1000!--    If the fine- and coarse grid nodes do not match
1001       noffset = MOD( coord_y(nyn+1), cg%dy )
1002       yexn    = cg%dy + noffset
1003!
1004!--    This is needed in the anterpolation phase
1005       nhln = CEILING( yexn / cg%dy )
1006       yce  = coord_y(nyn) + yexn
1007       DO  j = cg%ny, 0, -1
1008          IF ( cg%coord_y(j) < yce )  THEN
1009             jcn = MIN( cg%ny + nbgp, j+1 )
1010             EXIT
1011          ENDIF
1012       ENDDO
1013
1014       coarse_bound(1) = icl
1015       coarse_bound(2) = icr
1016       coarse_bound(3) = jcs
1017       coarse_bound(4) = jcn
1018       coarse_bound(5) = myid
1019!
1020!--    Note that MPI_Gather receives data from all processes in the rank order
1021!--    TO_DO: refer to the line where this fact becomes important
1022       CALL MPI_GATHER( coarse_bound, 5, MPI_INTEGER, coarse_bound_all, 5, &
1023                        MPI_INTEGER, 0, comm2d, ierr )
1024
1025       IF ( myid == 0 )  THEN
1026          size_of_array(1) = SIZE( coarse_bound_all, 1 )
1027          size_of_array(2) = SIZE( coarse_bound_all, 2 )
1028          CALL pmc_send_to_server( size_of_array, 2, 0, 40, ierr )
1029          CALL pmc_send_to_server( coarse_bound_all, SIZE( coarse_bound_all ), &
1030                                   0, 41, ierr )
1031       ENDIF
1032
1033    END SUBROUTINE pmci_map_fine_to_coarse_grid
1034
1035
1036
1037    SUBROUTINE pmci_init_interp_tril
1038!
1039!--    Precomputation of the interpolation coefficients and client-array indices
1040!--    to be used by the interpolation routines interp_tril_lr, interp_tril_ns
1041!--    and interp_tril_t.
1042
1043       IMPLICIT NONE
1044
1045       INTEGER(iwp) ::  i       !:
1046       INTEGER(iwp) ::  i1      !:
1047       INTEGER(iwp) ::  j       !:
1048       INTEGER(iwp) ::  j1      !:
1049       INTEGER(iwp) ::  k       !:
1050       INTEGER(iwp) ::  kc      !:
1051
1052       REAL(wp) ::  xb          !:
1053       REAL(wp) ::  xcsu        !:
1054       REAL(wp) ::  xfso        !:
1055       REAL(wp) ::  xcso        !:
1056       REAL(wp) ::  xfsu        !:
1057       REAL(wp) ::  yb          !:
1058       REAL(wp) ::  ycso        !:
1059       REAL(wp) ::  ycsv        !:
1060       REAL(wp) ::  yfso        !:
1061       REAL(wp) ::  yfsv        !:
1062       REAL(wp) ::  zcso        !:
1063       REAL(wp) ::  zcsw        !:
1064       REAL(wp) ::  zfso        !:
1065       REAL(wp) ::  zfsw        !:
1066     
1067
1068       xb = nxl * dx
1069       yb = nys * dy
1070     
1071       ALLOCATE( icu(nxlg:nxrg) )
1072       ALLOCATE( ico(nxlg:nxrg) )
1073       ALLOCATE( jcv(nysg:nyng) )
1074       ALLOCATE( jco(nysg:nyng) )
1075       ALLOCATE( kcw(nzb:nzt+1) )
1076       ALLOCATE( kco(nzb:nzt+1) )
1077       ALLOCATE( r1xu(nxlg:nxrg) )
1078       ALLOCATE( r2xu(nxlg:nxrg) )
1079       ALLOCATE( r1xo(nxlg:nxrg) )
1080       ALLOCATE( r2xo(nxlg:nxrg) )
1081       ALLOCATE( r1yv(nysg:nyng) )
1082       ALLOCATE( r2yv(nysg:nyng) )
1083       ALLOCATE( r1yo(nysg:nyng) )
1084       ALLOCATE( r2yo(nysg:nyng) )
1085       ALLOCATE( r1zw(nzb:nzt+1) )
1086       ALLOCATE( r2zw(nzb:nzt+1) )
1087       ALLOCATE( r1zo(nzb:nzt+1) )
1088       ALLOCATE( r2zo(nzb:nzt+1) )
1089
1090!
1091!--    Note that the node coordinates xfs... and xcs... are relative to the
1092!--    lower-left-bottom corner of the fc-array, not the actual client domain
1093!--    corner
1094       DO  i = nxlg, nxrg
1095          xfsu    = coord_x(i) - ( lower_left_coord_x + xb - xexl )
1096          xfso    = coord_x(i) + 0.5_wp * dx - ( lower_left_coord_x + xb - xexl )
1097          icu(i)  = icl + FLOOR( xfsu / cg%dx )
1098          ico(i)  = icl + FLOOR( ( xfso - 0.5_wp * cg%dx ) / cg%dx )
1099          xcsu    = ( icu(i) - icl ) * cg%dx
1100          xcso    = ( ico(i) - icl ) * cg%dx + 0.5_wp * cg%dx
1101          r2xu(i) = ( xfsu - xcsu ) / cg%dx
1102          r2xo(i) = ( xfso - xcso ) / cg%dx
1103          r1xu(i) = 1.0_wp - r2xu(i)
1104          r1xo(i) = 1.0_wp - r2xo(i)
1105       ENDDO
1106
1107       DO  j = nysg, nyng
1108          yfsv    = coord_y(j) - ( lower_left_coord_y + yb - yexs )
1109          yfso    = coord_y(j) + 0.5_wp * dy - ( lower_left_coord_y + yb - yexs )
1110          jcv(j)  = jcs + FLOOR( yfsv / cg%dy )
1111          jco(j)  = jcs + FLOOR( ( yfso -0.5_wp * cg%dy ) / cg%dy )
1112          ycsv    = ( jcv(j) - jcs ) * cg%dy
1113          ycso    = ( jco(j) - jcs ) * cg%dy + 0.5_wp * cg%dy
1114          r2yv(j) = ( yfsv - ycsv ) / cg%dy
1115          r2yo(j) = ( yfso - ycso ) / cg%dy
1116          r1yv(j) = 1.0_wp - r2yv(j)
1117          r1yo(j) = 1.0_wp - r2yo(j)
1118       ENDDO
1119
1120       DO  k = nzb, nzt + 1
1121          zfsw = zw(k)
1122          zfso = zu(k)
1123
1124          kc = 0
1125          DO  WHILE ( cg%zw(kc) <= zfsw )
1126             kc = kc + 1
1127          ENDDO
1128          kcw(k) = kc - 1
1129         
1130          kc = 0
1131          DO  WHILE ( cg%zu(kc) <= zfso )
1132             kc = kc + 1
1133          ENDDO
1134          kco(k) = kc - 1
1135
1136          zcsw    = cg%zw(kcw(k))
1137          zcso    = cg%zu(kco(k))
1138          r2zw(k) = ( zfsw - zcsw ) / cg%dzw(kcw(k)+1)
1139          r2zo(k) = ( zfso - zcso ) / cg%dzu(kco(k)+1)
1140          r1zw(k) = 1.0_wp - r2zw(k)
1141          r1zo(k) = 1.0_wp - r2zo(k)
1142       ENDDO
1143     
1144    END SUBROUTINE pmci_init_interp_tril
1145
1146
1147
1148    SUBROUTINE pmci_init_loglaw_correction
1149!
1150!--    Precomputation of the index and log-ratio arrays for the log-law
1151!--    corrections for near-wall nodes after the nest-BC interpolation.
1152!--    These are used by the interpolation routines interp_tril_lr and
1153!--    interp_tril_ns.
1154
1155       IMPLICIT NONE
1156
1157       INTEGER(iwp) ::  direction    !:  Wall normal index: 1=k, 2=j, 3=i.
1158       INTEGER(iwp) ::  i            !:
1159       INTEGER(iwp) ::  icorr        !:
1160       INTEGER(iwp) ::  inc          !:  Wall outward-normal index increment -1
1161                                     !: or 1, for direction=1, inc=1 always
1162       INTEGER(iwp) ::  iw           !:
1163       INTEGER(iwp) ::  j            !:
1164       INTEGER(iwp) ::  jcorr        !:
1165       INTEGER(iwp) ::  jw           !:
1166       INTEGER(iwp) ::  k            !:
1167       INTEGER(iwp) ::  kb           !:
1168       INTEGER(iwp) ::  kcorr        !:
1169       INTEGER(iwp) ::  lc           !:
1170       INTEGER(iwp) ::  ni           !:
1171       INTEGER(iwp) ::  nj           !:
1172       INTEGER(iwp) ::  nk           !:
1173       INTEGER(iwp) ::  nzt_topo_max !:
1174       INTEGER(iwp) ::  wall_index   !:  Index of the wall-node coordinate
1175
1176       REAL(wp), ALLOCATABLE, DIMENSION(:) ::  lcr   !:
1177
1178!
1179!--    First determine the maximum k-index needed for the near-wall corrections.
1180!--    This maximum is individual for each boundary to minimize the storage
1181!--    requirements and to minimize the corresponding loop k-range in the
1182!--    interpolation routines.
1183       nzt_topo_nestbc_l = nzb
1184       IF ( nest_bound_l )  THEN
1185          DO  i = nxl-1, nxl
1186             DO  j = nys, nyn
1187                nzt_topo_nestbc_l = MAX( nzt_topo_nestbc_l, nzb_u_inner(j,i),  &
1188                                         nzb_v_inner(j,i), nzb_w_inner(j,i) )
1189             ENDDO
1190          ENDDO
1191          nzt_topo_nestbc_l = nzt_topo_nestbc_l + 1
1192       ENDIF
1193     
1194       nzt_topo_nestbc_r = nzb
1195       IF ( nest_bound_r )  THEN
1196          i = nxr + 1
1197          DO  j = nys, nyn
1198             nzt_topo_nestbc_r = MAX( nzt_topo_nestbc_r, nzb_u_inner(j,i),     &
1199                                      nzb_v_inner(j,i), nzb_w_inner(j,i) )
1200          ENDDO
1201          nzt_topo_nestbc_r = nzt_topo_nestbc_r + 1
1202       ENDIF
1203
1204       nzt_topo_nestbc_s = nzb
1205       IF ( nest_bound_s )  THEN
1206          DO  j = nys-1, nys
1207             DO  i = nxl, nxr
1208                nzt_topo_nestbc_s = MAX( nzt_topo_nestbc_s, nzb_u_inner(j,i),  &
1209                                         nzb_v_inner(j,i), nzb_w_inner(j,i) )
1210             ENDDO
1211          ENDDO
1212          nzt_topo_nestbc_s = nzt_topo_nestbc_s + 1
1213       ENDIF
1214
1215       nzt_topo_nestbc_n = nzb
1216       IF ( nest_bound_n )  THEN
1217          j = nyn + 1
1218          DO  i = nxl, nxr
1219             nzt_topo_nestbc_n = MAX( nzt_topo_nestbc_n, nzb_u_inner(j,i),     &
1220                                      nzb_v_inner(j,i), nzb_w_inner(j,i) )
1221          ENDDO
1222          nzt_topo_nestbc_n = nzt_topo_nestbc_n + 1
1223       ENDIF
1224
1225!
1226!--    Then determine the maximum number of near-wall nodes per wall point based
1227!--    on the grid-spacing ratios.
1228       nzt_topo_max = MAX( nzt_topo_nestbc_l, nzt_topo_nestbc_r,               &
1229                           nzt_topo_nestbc_s, nzt_topo_nestbc_n )
1230
1231!
1232!--    Note that the outer division must be integer division.
1233       ni = CEILING( cg%dx / dx ) / 2
1234       nj = CEILING( cg%dy / dy ) / 2
1235       nk = 1
1236       DO  k = 1, nzt_topo_max
1237          nk = MAX( nk, CEILING( cg%dzu(k) / dzu(k) ) )
1238       ENDDO
1239       nk = nk / 2   !  Note that this must be integer division.
1240       ncorr =  MAX( ni, nj, nk )
1241
1242       ALLOCATE( lcr(0:ncorr-1) )
1243       lcr = 1.0_wp
1244
1245!
1246!--    First horizontal walls
1247!--    Left boundary
1248       IF ( nest_bound_l )  THEN
1249
1250          ALLOCATE( logc_u_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
1251          ALLOCATE( logc_v_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
1252          ALLOCATE( logc_ratio_u_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,nys:nyn) )
1253          ALLOCATE( logc_ratio_v_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,nys:nyn) )
1254          logc_u_l       = 0
1255          logc_v_l       = 0
1256          logc_ratio_u_l = 1.0_wp
1257          logc_ratio_v_l = 1.0_wp
1258          direction      = 1
1259          inc            = 1
1260
1261          DO  j = nys, nyn
1262!
1263!--          Left boundary for u
1264             i   = 0
1265             kb  = nzb_u_inner(j,i)
1266             k   = kb + 1
1267             wall_index = kb
1268             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1269                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1270             logc_u_l(1,k,j) = lc
1271             logc_ratio_u_l(1,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1272             lcr(0:ncorr-1) = 1.0_wp
1273!
1274!--          Left boundary for v
1275             i   = -1
1276             kb  =  nzb_v_inner(j,i)
1277             k   =  kb + 1
1278             wall_index = kb
1279             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1280                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1281             logc_v_l(1,k,j) = lc
1282             logc_ratio_v_l(1,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1283             lcr(0:ncorr-1) = 1.0_wp
1284
1285          ENDDO
1286
1287       ENDIF
1288
1289!
1290!--    Right boundary
1291       IF ( nest_bound_r )  THEN
1292
1293          ALLOCATE( logc_u_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
1294          ALLOCATE( logc_v_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
1295          ALLOCATE( logc_ratio_u_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,nys:nyn) )
1296          ALLOCATE( logc_ratio_v_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,nys:nyn) )
1297          logc_u_r       = 0
1298          logc_v_r       = 0
1299          logc_ratio_u_r = 1.0_wp
1300          logc_ratio_v_r = 1.0_wp
1301          direction      = 1
1302          inc            = 1
1303          DO  j = nys, nyn
1304!
1305!--          Right boundary for u
1306             i   = nxr + 1
1307             kb  = nzb_u_inner(j,i)
1308             k   = kb + 1
1309             wall_index = kb
1310             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1311                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1312             logc_u_r(1,k,j) = lc
1313             logc_ratio_u_r(1,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1314             lcr(0:ncorr-1) = 1.0_wp
1315!
1316!--          Right boundary for v
1317             i   = nxr + 1
1318             kb  = nzb_v_inner(j,i)
1319             k   = kb + 1
1320             wall_index = kb
1321             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1322                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1323             logc_v_r(1,k,j) = lc
1324             logc_ratio_v_r(1,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1325             lcr(0:ncorr-1) = 1.0_wp
1326
1327          ENDDO
1328
1329       ENDIF
1330
1331!
1332!--    South boundary
1333       IF ( nest_bound_s )  THEN
1334
1335          ALLOCATE( logc_u_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
1336          ALLOCATE( logc_v_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
1337          ALLOCATE( logc_ratio_u_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,nxl:nxr) )
1338          ALLOCATE( logc_ratio_v_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,nxl:nxr) )
1339          logc_u_s       = 0
1340          logc_v_s       = 0
1341          logc_ratio_u_s = 1.0_wp
1342          logc_ratio_v_s = 1.0_wp
1343          direction      = 1
1344          inc            = 1
1345
1346          DO  i = nxl, nxr
1347!
1348!--          South boundary for u
1349             j   = -1
1350             kb  =  nzb_u_inner(j,i)
1351             k   =  kb + 1
1352             wall_index = kb
1353             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1354                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1355             logc_u_s(1,k,i) = lc
1356             logc_ratio_u_s(1,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1357             lcr(0:ncorr-1) = 1.0_wp
1358!
1359!--          South boundary for v
1360             j   = 0
1361             kb  = nzb_v_inner(j,i)
1362             k   = kb + 1
1363             wall_index = kb
1364             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1365                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1366             logc_v_s(1,k,i) = lc
1367             logc_ratio_v_s(1,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1368             lcr(0:ncorr-1) = 1.0_wp
1369
1370          ENDDO
1371
1372       ENDIF
1373
1374!
1375!--    North boundary
1376       IF ( nest_bound_n )  THEN
1377
1378          ALLOCATE( logc_u_n(1:2,nzb:nzt_topo_nestbc_n,nxl:nxr) )
1379          ALLOCATE( logc_v_n(1:2,nzb:nzt_topo_nestbc_n,nxl:nxr) )
1380          ALLOCATE( logc_ratio_u_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,nxl:nxr) )
1381          ALLOCATE( logc_ratio_v_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,nxl:nxr) )
1382          logc_u_n       = 0
1383          logc_v_n       = 0
1384          logc_ratio_u_n = 1.0_wp
1385          logc_ratio_v_n = 1.0_wp
1386          direction      = 1
1387          inc            = 1
1388
1389          DO  i = nxl, nxr
1390!
1391!--          North boundary for u
1392             j   = nyn + 1
1393             kb  = nzb_u_inner(j,i)
1394             k   = kb + 1
1395             wall_index = kb
1396             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1397                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1398             logc_u_n(1,k,i) = lc
1399             logc_ratio_u_n(1,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1400             lcr(0:ncorr-1) = 1.0_wp
1401!
1402!--          North boundary for v
1403             j   = nyn + 1
1404             kb  = nzb_v_inner(j,i)
1405             k   = kb + 1
1406             wall_index = kb
1407             CALL pmci_define_loglaw_correction_parameters( lc, lcr, k, j,     &
1408                                inc, wall_index, z0(j,i), kb, direction, ncorr )
1409             logc_v_n(1,k,i) = lc
1410             logc_ratio_v_n(1,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1411             lcr(0:ncorr-1) = 1.0_wp
1412
1413          ENDDO
1414
1415       ENDIF
1416
1417!       
1418!--    Then vertical walls and corners if necessary
1419       IF ( topography /= 'flat' )  THEN
1420
1421          kb = 0       ! kb is not used when direction > 1
1422!       
1423!--       Left boundary
1424          IF ( nest_bound_l )  THEN
1425
1426             ALLOCATE( logc_w_l(1:2,nzb:nzt_topo_nestbc_l,nys:nyn) )
1427             ALLOCATE( logc_ratio_w_l(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_l,     &
1428                                      nys:nyn) )
1429
1430             logc_w_l       = 0
1431             logc_ratio_w_l = 1.0_wp
1432             direction      = 2
1433             DO  j = nys, nyn
1434                DO  k = nzb, nzt_topo_nestbc_l
1435!
1436!--                Wall for u on the south side, but not on the north side
1437                   i  = 0
1438                   IF ( ( nzb_u_outer(j,i) > nzb_u_outer(j+1,i) ) .AND.        &
1439                        ( nzb_u_outer(j,i) == nzb_u_outer(j-1,i) ) )           &
1440                   THEN
1441                      inc        =  1
1442                      wall_index =  j
1443                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1444                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1445!
1446!--                   The direction of the wall-normal index is stored as the
1447!--                   sign of the logc-element.
1448                      logc_u_l(2,k,j) = inc * lc
1449                      logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1450                      lcr(0:ncorr-1) = 1.0_wp
1451                   ENDIF
1452
1453!
1454!--                Wall for u on the north side, but not on the south side
1455                   i  = 0
1456                   IF ( ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) ) .AND.        &
1457                        ( nzb_u_outer(j,i) == nzb_u_outer(j+1,i) ) )  THEN
1458                      inc        = -1
1459                      wall_index =  j + 1
1460                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1461                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1462!
1463!--                   The direction of the wall-normal index is stored as the
1464!--                   sign of the logc-element.
1465                      logc_u_l(2,k,j) = inc * lc
1466                      logc_ratio_u_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1467                      lcr(0:ncorr-1) = 1.0_wp
1468                   ENDIF
1469
1470!
1471!--                Wall for w on the south side, but not on the north side.
1472                   i  = -1
1473                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j+1,i) )  .AND.       &
1474                        ( nzb_w_outer(j,i) == nzb_w_outer(j-1,i) ) )  THEN
1475                      inc        =  1
1476                      wall_index =  j
1477                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1478                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1479!
1480!--                   The direction of the wall-normal index is stored as the
1481!--                   sign of the logc-element.
1482                      logc_w_l(2,k,j) = inc * lc
1483                      logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1484                      lcr(0:ncorr-1) = 1.0_wp
1485                   ENDIF
1486
1487!
1488!--                Wall for w on the north side, but not on the south side.
1489                   i  = -1
1490                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) )  .AND.       &
1491                        ( nzb_w_outer(j,i) == nzb_w_outer(j+1,i) ) )  THEN
1492                      inc        = -1
1493                      wall_index =  j+1
1494                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1495                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1496!
1497!--                   The direction of the wall-normal index is stored as the
1498!--                   sign of the logc-element.
1499                      logc_w_l(2,k,j) = inc * lc
1500                      logc_ratio_w_l(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1501                      lcr(0:ncorr-1) = 1.0_wp
1502                   ENDIF
1503
1504                ENDDO
1505             ENDDO
1506
1507          ENDIF   !  IF ( nest_bound_l )
1508
1509!       
1510!--       Right boundary
1511          IF ( nest_bound_r )  THEN
1512
1513             ALLOCATE( logc_w_r(1:2,nzb:nzt_topo_nestbc_r,nys:nyn) )
1514             ALLOCATE( logc_ratio_w_r(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_r,     &
1515                                      nys:nyn) )
1516             logc_w_r       = 0
1517             logc_ratio_w_r = 1.0_wp
1518             direction      = 2
1519             i  = nxr + 1
1520
1521             DO  j = nys, nyn
1522                DO  k = nzb, nzt_topo_nestbc_r
1523!
1524!--                Wall for u on the south side, but not on the north side
1525                   IF ( ( nzb_u_outer(j,i) > nzb_u_outer(j+1,i) )  .AND.       &
1526                        ( nzb_u_outer(j,i) == nzb_u_outer(j-1,i) ) )  THEN
1527                      inc        = 1
1528                      wall_index = j
1529                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1530                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1531!
1532!--                   The direction of the wall-normal index is stored as the
1533!--                   sign of the logc-element.
1534                      logc_u_r(2,k,j) = inc * lc
1535                      logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1536                      lcr(0:ncorr-1) = 1.0_wp
1537                   ENDIF
1538
1539!
1540!--                Wall for u on the north side, but not on the south side
1541                   IF ( ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) )  .AND.       &
1542                        ( nzb_u_outer(j,i) == nzb_u_outer(j+1,i) ) )  THEN
1543                      inc        = -1
1544                      wall_index =  j+1
1545                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1546                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1547!
1548!--                   The direction of the wall-normal index is stored as the
1549!--                   sign of the logc-element.
1550                      logc_u_r(2,k,j) = inc * lc
1551                      logc_ratio_u_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1552                      lcr(0:ncorr-1) = 1.0_wp
1553                   ENDIF
1554
1555!
1556!--                Wall for w on the south side, but not on the north side
1557                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j+1,i) )  .AND.       &
1558                        ( nzb_w_outer(j,i) == nzb_w_outer(j-1,i) ) )  THEN
1559                      inc        =  1
1560                      wall_index =  j
1561                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1562                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1563!
1564!--                   The direction of the wall-normal index is stored as the
1565!--                   sign of the logc-element.
1566                      logc_w_r(2,k,j) = inc * lc
1567                      logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1568                      lcr(0:ncorr-1) = 1.0_wp
1569                   ENDIF
1570
1571!
1572!--                Wall for w on the north side, but not on the south side
1573                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) )  .AND.       &
1574                        ( nzb_w_outer(j,i) == nzb_w_outer(j+1,i) ) )  THEN
1575                      inc        = -1
1576                      wall_index =  j+1
1577                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1578                          k, j, inc, wall_index, z0(j,i), kb, direction, ncorr )
1579
1580!
1581!--                   The direction of the wall-normal index is stored as the
1582!--                   sign of the logc-element.
1583                      logc_w_r(2,k,j) = inc * lc
1584                      logc_ratio_w_r(2,0:ncorr-1,k,j) = lcr(0:ncorr-1)
1585                      lcr(0:ncorr-1) = 1.0_wp
1586                   ENDIF
1587
1588                ENDDO
1589             ENDDO
1590
1591          ENDIF   !  IF ( nest_bound_r )
1592
1593!       
1594!--       South boundary
1595          IF ( nest_bound_s )  THEN
1596
1597             ALLOCATE( logc_w_s(1:2,nzb:nzt_topo_nestbc_s,nxl:nxr) )
1598             ALLOCATE( logc_ratio_w_s(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_s,     &
1599                                      nxl:nxr) )
1600             logc_w_s       = 0
1601             logc_ratio_w_s = 1.0_wp
1602             direction      = 3
1603
1604             DO  i = nxl, nxr
1605                DO  k = nzb, nzt_topo_nestbc_s
1606!
1607!--                Wall for v on the left side, but not on the right side
1608                   j  = 0
1609                   IF ( ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) )  .AND.       &
1610                        ( nzb_v_outer(j,i) == nzb_v_outer(j,i-1) ) )  THEN
1611                      inc        =  1
1612                      wall_index =  i
1613                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1614                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1615!
1616!--                   The direction of the wall-normal index is stored as the
1617!--                   sign of the logc-element.
1618                      logc_v_s(2,k,i) = inc * lc
1619                      logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1620                      lcr(0:ncorr-1) = 1.0_wp
1621                   ENDIF
1622
1623!
1624!--                Wall for v on the right side, but not on the left side
1625                   j  = 0
1626                   IF ( ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) )  .AND.       &
1627                        ( nzb_v_outer(j,i) == nzb_v_outer(j,i+1) ) )  THEN
1628                      inc        = -1
1629                      wall_index =  i+1
1630                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1631                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1632!
1633!--                   The direction of the wall-normal index is stored as the
1634!--                   sign of the logc-element.
1635                      logc_v_s(2,k,i) = inc * lc
1636                      logc_ratio_v_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1637                      lcr(0:ncorr-1) = 1.0_wp
1638                   ENDIF
1639
1640!
1641!--                Wall for w on the left side, but not on the right side
1642                   j  = -1
1643                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) )  .AND.       &
1644                        ( nzb_w_outer(j,i) == nzb_w_outer(j,i-1) ) )  THEN
1645                      inc        =  1
1646                      wall_index =  i
1647                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1648                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1649!
1650!--                   The direction of the wall-normal index is stored as the
1651!--                   sign of the logc-element.
1652                      logc_w_s(2,k,i) = inc * lc
1653                      logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1654                      lcr(0:ncorr-1) = 1.0_wp
1655                   ENDIF
1656
1657!
1658!--                Wall for w on the right side, but not on the left side
1659                   j  = -1
1660                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) )  .AND.       &
1661                        ( nzb_w_outer(j,i) == nzb_w_outer(j,i+1) ) )  THEN
1662                      inc        = -1
1663                      wall_index =  i+1
1664                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1665                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1666!
1667!--                   The direction of the wall-normal index is stored as the
1668!--                   sign of the logc-element.
1669                      logc_w_s(2,k,i) = inc * lc
1670                      logc_ratio_w_s(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1671                      lcr(0:ncorr-1) = 1.0_wp
1672                   ENDIF
1673
1674                ENDDO
1675             ENDDO
1676
1677          ENDIF   !  IF (nest_bound_s )
1678
1679!       
1680!--       North boundary
1681          IF ( nest_bound_n )  THEN
1682
1683             ALLOCATE( logc_w_n(1:2,nzb:nzt_topo_nestbc_n, nxl:nxr) )
1684             ALLOCATE( logc_ratio_w_n(1:2,0:ncorr-1,nzb:nzt_topo_nestbc_n,     &
1685                                      nxl:nxr) )
1686             logc_w_n       = 0
1687             logc_ratio_w_n = 1.0_wp
1688             direction      = 3
1689             j  = nyn + 1
1690
1691             DO  i = nxl, nxr
1692                DO  k = nzb, nzt_topo_nestbc_n
1693!
1694!--                Wall for v on the left side, but not on the right side
1695                   IF ( ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) )  .AND.       &
1696                        ( nzb_v_outer(j,i) == nzb_v_outer(j,i-1) ) )  THEN
1697                      inc        = 1
1698                      wall_index = i
1699                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1700                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1701!
1702!--                   The direction of the wall-normal index is stored as the
1703!--                   sign of the logc-element.
1704                      logc_v_n(2,k,i) = inc * lc
1705                      logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1706                      lcr(0:ncorr-1) = 1.0_wp
1707                   ENDIF
1708
1709!
1710!--                Wall for v on the right side, but not on the left side
1711                   IF ( ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) )  .AND.       &
1712                        ( nzb_v_outer(j,i) == nzb_v_outer(j,i+1) ) )  THEN
1713                      inc        = -1
1714                      wall_index =  i + 1
1715                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1716                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1717!
1718!--                   The direction of the wall-normal index is stored as the
1719!--                   sign of the logc-element.
1720                      logc_v_n(2,k,i) = inc * lc
1721                      logc_ratio_v_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1722                      lcr(0:ncorr-1) = 1.0_wp
1723                   ENDIF
1724
1725!
1726!--                Wall for w on the left side, but not on the right side
1727                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) )  .AND.       &
1728                        ( nzb_w_outer(j,i) == nzb_w_outer(j,i-1) ) )  THEN
1729                      inc        = 1
1730                      wall_index = i
1731                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1732                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1733!
1734!--                   The direction of the wall-normal index is stored as the
1735!--                   sign of the logc-element.
1736                      logc_w_n(2,k,i) = inc * lc
1737                      logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1738                      lcr(0:ncorr-1) = 1.0_wp
1739                   ENDIF
1740
1741!
1742!--                Wall for w on the right side, but not on the left side
1743                   IF ( ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) )  .AND.       &
1744                        ( nzb_w_outer(j,i) == nzb_w_outer(j,i+1) ) )  THEN
1745                      inc        = -1
1746                      wall_index =  i+1
1747                      CALL pmci_define_loglaw_correction_parameters( lc, lcr,  &
1748                          k, i, inc, wall_index, z0(j,i), kb, direction, ncorr )
1749!
1750!--                   The direction of the wall-normal index is stored as the
1751!--                   sign of the logc-element.
1752                      logc_w_n(2,k,i) = inc * lc
1753                      logc_ratio_w_n(2,0:ncorr-1,k,i) = lcr(0:ncorr-1)
1754                      lcr(0:ncorr-1) = 1.0_wp
1755                   ENDIF
1756
1757                ENDDO
1758             ENDDO
1759
1760          ENDIF   !  IF ( nest_bound_n )
1761
1762       ENDIF   !  IF ( topography /= 'flat' )
1763
1764    END SUBROUTINE pmci_init_loglaw_correction
1765
1766
1767
1768    SUBROUTINE pmci_define_loglaw_correction_parameters( lc, lcr, k, ij, inc,  &
1769                                        wall_index, z0_l, kb, direction, ncorr )
1770
1771       IMPLICIT NONE
1772
1773       INTEGER(iwp), INTENT(IN)  ::  direction                 !:
1774       INTEGER(iwp), INTENT(IN)  ::  ij                        !:
1775       INTEGER(iwp), INTENT(IN)  ::  inc                       !:
1776       INTEGER(iwp), INTENT(IN)  ::  k                         !:
1777       INTEGER(iwp), INTENT(IN)  ::  kb                        !:
1778       INTEGER(iwp), INTENT(OUT) ::  lc                        !:
1779       INTEGER(iwp), INTENT(IN)  ::  ncorr                     !:
1780       INTEGER(iwp), INTENT(IN)  ::  wall_index                !:
1781
1782       INTEGER(iwp) ::  alcorr       !:
1783       INTEGER(iwp) ::  corr_index   !:
1784       INTEGER(iwp) ::  lcorr        !:
1785
1786       LOGICAL      ::  more         !:
1787
1788       REAL(wp), DIMENSION(0:ncorr-1), INTENT(OUT) ::  lcr     !:
1789       REAL(wp), INTENT(IN)      ::  z0_l                      !:
1790     
1791       REAL(wp)     ::  logvelc1     !:
1792     
1793
1794       SELECT CASE ( direction )
1795
1796          CASE (1)   !  k
1797             more = .TRUE.
1798             lcorr = 0
1799             DO  WHILE ( more .AND. lcorr <= ncorr-1 )
1800                corr_index = k + lcorr
1801                IF ( lcorr == 0 )  THEN
1802                   CALL pmci_find_logc_pivot_k( lc, logvelc1, z0_l, kb )
1803                ENDIF
1804               
1805                IF ( corr_index < lc )  THEN
1806                   lcr(lcorr) = LOG( ( zu(k) - zw(kb) ) / z0_l ) / logvelc1
1807                   more = .TRUE.
1808                ELSE
1809                   lcr(lcorr) = 1.0
1810                   more = .FALSE.
1811                ENDIF
1812                lcorr = lcorr + 1
1813             ENDDO
1814
1815          CASE (2)   !  j
1816             more = .TRUE.
1817             lcorr  = 0
1818             alcorr = 0
1819             DO  WHILE ( more  .AND.  alcorr <= ncorr-1 )
1820                corr_index = ij + lcorr   ! In this case (direction = 2) ij is j
1821                IF ( lcorr == 0 )  THEN
1822                   CALL pmci_find_logc_pivot_j( lc, logvelc1, ij, wall_index,  &
1823                                                z0_l, inc )
1824                ENDIF
1825
1826!
1827!--             The role of inc here is to make the comparison operation "<"
1828!--             valid in both directions
1829                IF ( inc * corr_index < inc * lc )  THEN
1830                   lcr(alcorr) = LOG( ABS( coord_y(corr_index) + 0.5_wp * dy   &
1831                                         - coord_y(wall_index) ) / z0_l )      &
1832                                 / logvelc1
1833                   more = .TRUE.
1834                ELSE
1835                   lcr(alcorr) = 1.0_wp
1836                   more = .FALSE.
1837                ENDIF
1838                lcorr  = lcorr + inc
1839                alcorr = ABS( lcorr )
1840             ENDDO
1841
1842          CASE (3)   !  i
1843             more = .TRUE.
1844             lcorr  = 0
1845             alcorr = 0
1846             DO  WHILE ( more  .AND.  alcorr <= ncorr-1 )
1847                corr_index = ij + lcorr   ! In this case (direction = 3) ij is i
1848                IF ( lcorr == 0 )  THEN
1849                   CALL pmci_find_logc_pivot_i( lc, logvelc1, ij, wall_index,  &
1850                                                z0_l, inc )
1851                ENDIF
1852!
1853!--             The role of inc here is to make the comparison operation "<"
1854!--             valid in both directions
1855                IF ( inc * corr_index < inc * lc )  THEN
1856                   lcr(alcorr) = LOG( ABS( coord_x(corr_index) + 0.5_wp * dx   &
1857                                         - coord_x(wall_index) ) / z0_l )      &
1858                                 / logvelc1
1859                   more = .TRUE.
1860                ELSE
1861                   lcr(alcorr) = 1.0_wp
1862                   more = .FALSE.
1863                ENDIF
1864                lcorr  = lcorr + inc
1865                alcorr = ABS( lcorr )
1866             ENDDO
1867
1868       END SELECT
1869
1870    END SUBROUTINE pmci_define_loglaw_correction_parameters
1871
1872
1873
1874    SUBROUTINE pmci_find_logc_pivot_k( lc, logzc1, z0_l, kb )
1875!
1876!--    Finds the pivot node and te log-law factor for near-wall nodes for
1877!--    which the wall-parallel velocity components will be log-law corrected
1878!--    after interpolation. This subroutine is only for horizontal walls.
1879
1880       IMPLICIT NONE
1881
1882       INTEGER(iwp), INTENT(IN)  ::  kb   !:
1883       INTEGER(iwp), INTENT(OUT) ::  lc   !:
1884
1885       INTEGER(iwp) ::  kbc    !:
1886       INTEGER(iwp) ::  k1     !:
1887
1888       REAL(wp),INTENT(OUT) ::  logzc1     !:
1889       REAL(wp), INTENT(IN) ::  z0_l       !:
1890
1891       REAL(wp) ::  zuc1   !:
1892
1893
1894       kbc = nzb + 1
1895!
1896!--    kbc is the first coarse-grid point above the surface
1897       DO  WHILE ( cg%zu(kbc) < zu(kb) )
1898          kbc = kbc + 1
1899       ENDDO
1900       zuc1  = cg%zu(kbc)
1901       k1    = kb + 1
1902       DO  WHILE ( zu(k1) < zuc1 )  !  Important: must be <, not <=
1903          k1 = k1 + 1
1904       ENDDO
1905       logzc1 = LOG( (zu(k1) - zw(kb) ) / z0_l )
1906       lc = k1
1907
1908    END SUBROUTINE pmci_find_logc_pivot_k
1909
1910
1911
1912    SUBROUTINE pmci_find_logc_pivot_j( lc, logyc1, j, jw, z0_l, inc )
1913!
1914!--    Finds the pivot node and te log-law factor for near-wall nodes for
1915!--    which the wall-parallel velocity components will be log-law corrected
1916!--    after interpolation. This subroutine is only for vertical walls on
1917!--    south/north sides of the node.
1918
1919       IMPLICIT NONE
1920
1921       INTEGER(iwp), INTENT(IN)  ::  inc    !:  increment must be 1 or -1.
1922       INTEGER(iwp), INTENT(IN)  ::  j      !:
1923       INTEGER(iwp), INTENT(IN)  ::  jw     !:
1924       INTEGER(iwp), INTENT(OUT) ::  lc     !:
1925
1926       INTEGER(iwp) ::  j1       !:
1927
1928       REAL(wp), INTENT(IN) ::  z0_l   !:
1929
1930       REAL(wp) ::  logyc1   !:
1931       REAL(wp) ::  yc1      !:
1932
1933!
1934!--    yc1 is the y-coordinate of the first coarse-grid u- and w-nodes out from
1935!--    the wall
1936       yc1  = coord_y(jw) + 0.5_wp * inc * cg%dy
1937!
1938!--    j1 is the first fine-grid index further away from the wall than yc1
1939       j1 = j
1940!
1941!--    Important: must be <, not <=
1942       DO  WHILE ( inc * ( coord_y(j1) + 0.5_wp * dy ) < inc * yc1 )
1943          j1 = j1 + inc
1944       ENDDO
1945
1946       logyc1 = LOG( ABS( coord_y(j1) + 0.5_wp * dy - coord_y(jw) ) / z0_l )
1947       lc = j1
1948
1949    END SUBROUTINE pmci_find_logc_pivot_j
1950
1951
1952
1953    SUBROUTINE pmci_find_logc_pivot_i( lc, logxc1, i, iw, z0_l, inc )
1954!
1955!--    Finds the pivot node and the log-law factor for near-wall nodes for
1956!--    which the wall-parallel velocity components will be log-law corrected
1957!--    after interpolation. This subroutine is only for vertical walls on
1958!--    south/north sides of the node.
1959
1960       IMPLICIT NONE
1961
1962       INTEGER(iwp), INTENT(IN)  ::  i      !:
1963       INTEGER(iwp), INTENT(IN)  ::  inc    !: increment must be 1 or -1.
1964       INTEGER(iwp), INTENT(IN)  ::  iw     !:
1965       INTEGER(iwp), INTENT(OUT) ::  lc     !:
1966
1967       INTEGER(iwp) ::  i1       !:
1968
1969       REAL(wp), INTENT(IN) ::  z0_l   !:
1970
1971       REAL(wp) ::  logxc1   !:
1972       REAL(wp) ::  xc1      !:
1973
1974!
1975!--    xc1 is the x-coordinate of the first coarse-grid v- and w-nodes out from
1976!--    the wall
1977       xc1  = coord_x(iw) + 0.5_wp * inc * cg%dx
1978!
1979!--    i1 is the first fine-grid index futher away from the wall than xc1.
1980       i1 = i
1981!
1982!--    Important: must be <, not <=
1983       DO  WHILE ( inc * ( coord_x(i1) + 0.5_wp *dx ) < inc * xc1 )
1984          i1 = i1 + inc
1985       ENDDO
1986     
1987       logxc1 = LOG( ABS( coord_x(i1) + 0.5_wp*dx - coord_x(iw) ) / z0_l )
1988       lc = i1
1989
1990    END SUBROUTINE pmci_find_logc_pivot_i
1991
1992
1993
1994
1995    SUBROUTINE pmci_init_anterp_tophat
1996!
1997!--    Precomputation of the client-array indices for
1998!--    corresponding coarse-grid array index and the
1999!--    Under-relaxation coefficients to be used by anterp_tophat.
2000
2001       IMPLICIT NONE
2002
2003       INTEGER(iwp) ::  i        !: Fine-grid index
2004       INTEGER(iwp) ::  ii       !: Coarse-grid index
2005       INTEGER(iwp) ::  istart   !:
2006       INTEGER(iwp) ::  j        !: Fine-grid index
2007       INTEGER(iwp) ::  jj       !: Coarse-grid index
2008       INTEGER(iwp) ::  jstart   !:
2009       INTEGER(iwp) ::  k        !: Fine-grid index
2010       INTEGER(iwp) ::  kk       !: Coarse-grid index
2011       INTEGER(iwp) ::  kstart   !:
2012       REAL(wp)     ::  xi       !:
2013       REAL(wp)     ::  eta      !:
2014       REAL(wp)     ::  zeta     !:
2015     
2016!
2017!--    Default values:
2018       IF ( anterp_relax_length_l < 0.0_wp )  THEN
2019          anterp_relax_length_l = 0.1_wp * ( nx + 1 ) * dx
2020       ENDIF
2021       IF ( anterp_relax_length_r < 0.0_wp )  THEN
2022          anterp_relax_length_r = 0.1_wp * ( nx + 1 ) * dx
2023       ENDIF
2024       IF ( anterp_relax_length_s < 0.0_wp )  THEN
2025          anterp_relax_length_s = 0.1_wp * ( ny + 1 ) * dy
2026       ENDIF
2027       IF ( anterp_relax_length_n < 0.0_wp )  THEN
2028          anterp_relax_length_n = 0.1_wp * ( ny + 1 ) * dy
2029       ENDIF
2030       IF ( anterp_relax_length_t < 0.0_wp )  THEN
2031          anterp_relax_length_t = 0.1_wp * zu(nzt)
2032       ENDIF
2033
2034!
2035!--    First determine kctu and kctw that are the coarse-grid upper bounds for
2036!--    index k
2037       kk = 0
2038       DO  WHILE ( cg%zu(kk) < zu(nzt) )
2039          kk = kk + 1
2040       ENDDO
2041       kctu = kk - 1
2042
2043       kk = 0
2044       DO  WHILE ( cg%zw(kk) < zw(nzt-1) )
2045          kk = kk + 1
2046       ENDDO
2047       kctw = kk - 1
2048
2049       ALLOCATE( iflu(icl:icr) )
2050       ALLOCATE( iflo(icl:icr) )
2051       ALLOCATE( ifuu(icl:icr) )
2052       ALLOCATE( ifuo(icl:icr) )
2053       ALLOCATE( jflv(jcs:jcn) )
2054       ALLOCATE( jflo(jcs:jcn) )
2055       ALLOCATE( jfuv(jcs:jcn) )
2056       ALLOCATE( jfuo(jcs:jcn) )
2057       ALLOCATE( kflw(0:kctw) )
2058       ALLOCATE( kflo(0:kctu) )
2059       ALLOCATE( kfuw(0:kctw) )
2060       ALLOCATE( kfuo(0:kctu) )
2061
2062!
2063!--    i-indices of u for each ii-index value
2064       istart = nxlg
2065       DO  ii = icl, icr
2066          i = istart
2067          DO  WHILE ( ( coord_x(i) < cg%coord_x(ii) - 0.5_wp * cg%dx )  .AND.  &
2068                      ( i < nxrg ) )
2069             i = i + 1
2070          ENDDO
2071          iflu(ii) = MIN( MAX( i, nxlg ), nxrg )
2072          DO  WHILE ( ( coord_x(i) < cg%coord_x(ii) + 0.5_wp * cg%dx )  .AND.  &
2073                      ( i < nxrg ) )
2074             i = i + 1
2075          ENDDO
2076          ifuu(ii) = MIN( MAX( i, nxlg ), nxrg )
2077          istart = iflu(ii)
2078       ENDDO
2079
2080!
2081!--    i-indices of others for each ii-index value
2082       istart = nxlg
2083       DO  ii = icl, icr
2084          i = istart
2085          DO  WHILE ( ( coord_x(i) + 0.5_wp * dx < cg%coord_x(ii) )  .AND.     &
2086                      ( i < nxrg ) )
2087             i = i + 1
2088          ENDDO
2089          iflo(ii) = MIN( MAX( i, nxlg ), nxrg )
2090          DO  WHILE ( ( coord_x(i) + 0.5_wp * dx < cg%coord_x(ii) + cg%dx )    &
2091                      .AND.  ( i < nxrg ) )
2092             i = i + 1
2093          ENDDO
2094          ifuo(ii) = MIN(MAX(i,nxlg),nxrg)
2095          istart = iflo(ii)
2096       ENDDO
2097
2098!
2099!--    j-indices of v for each jj-index value
2100       jstart = nysg
2101       DO  jj = jcs, jcn
2102          j = jstart
2103          DO  WHILE ( ( coord_y(j) < cg%coord_y(jj) - 0.5_wp * cg%dy )  .AND.  &
2104                      ( j < nyng ) )
2105             j = j + 1
2106          ENDDO
2107          jflv(jj) = MIN( MAX( j, nysg ), nyng )
2108          DO  WHILE ( ( coord_y(j) < cg%coord_y(jj) + 0.5_wp * cg%dy )  .AND.  &
2109                      ( j < nyng ) )
2110             j = j + 1
2111          ENDDO
2112          jfuv(jj) = MIN( MAX( j, nysg ), nyng )
2113          jstart = jflv(jj)
2114       ENDDO
2115
2116!
2117!--    j-indices of others for each jj-index value
2118       jstart = nysg
2119       DO  jj = jcs, jcn
2120          j = jstart
2121          DO  WHILE ( ( coord_y(j) + 0.5_wp * dy < cg%coord_y(jj) )  .AND.     &
2122                      ( j < nyng ) )
2123             j = j + 1
2124          ENDDO
2125          jflo(jj) = MIN( MAX( j, nysg ), nyng )
2126          DO  WHILE ( ( coord_y(j) + 0.5_wp * dy < cg%coord_y(jj) + cg%dy )    &
2127                      .AND.  ( j < nyng ) )
2128             j = j + 1
2129          ENDDO
2130          jfuo(jj) = MIN( MAX( j, nysg ), nyng )
2131          jstart = jflv(jj)
2132       ENDDO
2133
2134!
2135!--    k-indices of w for each kk-index value
2136       kstart  = 0
2137       kflw(0) = 0
2138       kfuw(0) = 0
2139       DO  kk = 1, kctw
2140          k = kstart
2141          DO  WHILE ( ( zw(k) < cg%zw(kk) - 0.5_wp * cg%dzw(kk) )  .AND.       &
2142                      ( k < nzt ) )
2143             k = k + 1
2144          ENDDO
2145          kflw(kk) = MIN( MAX( k, 1 ), nzt + 1 )
2146          DO  WHILE ( ( zw(k) < cg%zw(kk) + 0.5_wp * cg%dzw(kk+1) )  .AND.     &
2147                      ( k < nzt ) )
2148             k = k + 1
2149          ENDDO
2150          kfuw(kk) = MIN( MAX( k, 1 ), nzt + 1 )
2151          kstart = kflw(kk)
2152       ENDDO
2153
2154!
2155!--    k-indices of others for each kk-index value
2156       kstart  = 0
2157       kflo(0) = 0
2158       kfuo(0) = 0
2159       DO  kk = 1, kctu
2160          k = kstart
2161          DO  WHILE ( ( zu(k) < cg%zu(kk) - 0.5_wp * cg%dzu(kk) )  .AND.       &
2162                      ( k < nzt ) )
2163             k = k + 1
2164          ENDDO
2165          kflo(kk) = MIN( MAX( k, 1 ), nzt + 1 )
2166          DO  WHILE ( ( zu(k) < cg%zu(kk) + 0.5_wp * cg%dzu(kk+1) )  .AND.     &
2167                      ( k < nzt ) )
2168             k = k + 1
2169          ENDDO
2170          kfuo(kk) = MIN( MAX( k-1, 1 ), nzt + 1 )
2171          kstart = kflo(kk)
2172       ENDDO
2173     
2174!
2175!--    Spatial under-relaxation coefficients
2176       ALLOCATE( frax(icl:icr) )
2177
2178       DO  ii = icl, icr
2179          IF ( nest_bound_l )  THEN
2180             xi = ( MAX( 0.0_wp, ( cg%coord_x(ii) - lower_left_coord_x ) ) /   &
2181                    anterp_relax_length_l )**4
2182          ELSEIF ( nest_bound_r )  THEN
2183             xi = ( MAX( 0.0_wp, ( lower_left_coord_x + ( nx + 1 ) * dx -      &
2184                                   cg%coord_x(ii) ) ) /                        &
2185                    anterp_relax_length_r )**4
2186          ELSE
2187             xi = 999999.9_wp
2188          ENDIF
2189          frax(ii) = xi / ( 1.0_wp + xi )
2190       ENDDO
2191
2192       ALLOCATE( fray(jcs:jcn) )
2193
2194       DO  jj = jcs, jcn
2195          IF ( nest_bound_s )  THEN
2196             eta = ( MAX( 0.0_wp, ( cg%coord_y(jj) - lower_left_coord_y ) ) /  &
2197                     anterp_relax_length_s )**4
2198          ELSEIF ( nest_bound_n )  THEN
2199             eta = ( MAX( 0.0_wp, ( lower_left_coord_y + ( ny + 1 ) * dy -     &
2200                                    cg%coord_y(jj)) ) /                        &
2201                     anterp_relax_length_n )**4
2202          ELSE
2203             eta = 999999.9_wp
2204          ENDIF
2205          fray(jj) = eta / ( 1.0_wp + eta )
2206       ENDDO
2207     
2208       ALLOCATE( fraz(0:kctu) )
2209       DO  kk = 0, kctu
2210          zeta = ( ( zu(nzt) - cg%zu(kk) ) / anterp_relax_length_t )**4
2211          fraz(kk) = zeta / ( 1.0_wp + zeta )
2212       ENDDO
2213
2214    END SUBROUTINE pmci_init_anterp_tophat
2215
2216
2217
2218    SUBROUTINE pmci_init_tkefactor
2219
2220!
2221!--    Computes the scaling factor for the SGS TKE from coarse grid to be used
2222!--    as BC for the fine grid. Based on the Kolmogorov energy spectrum
2223!--    for the inertial subrange and assumption of sharp cut-off of the resolved
2224!--    energy spectrum. Near the surface, the reduction of TKE is made
2225!--    smaller than further away from the surface.
2226
2227       IMPLICIT NONE
2228       REAL(wp), PARAMETER ::  cfw = 0.2_wp          !:
2229       REAL(wp), PARAMETER ::  c_tkef = 0.6_wp       !:
2230       REAL(wp)            ::  fw                    !:
2231       REAL(wp), PARAMETER ::  fw0 = 0.9_wp          !:
2232       REAL(wp)            ::  glsf                  !:
2233       REAL(wp)            ::  glsc                  !:
2234       REAL(wp)            ::  height                !:
2235       REAL(wp), PARAMETER ::  p13 = 1.0_wp/3.0_wp   !:
2236       REAL(wp), PARAMETER ::  p23 = 2.0_wp/3.0_wp   !:
2237       INTEGER(iwp)        ::  k                     !:
2238       INTEGER(iwp)        ::  kc                    !:
2239       
2240
2241       IF ( nest_bound_l )  THEN
2242          ALLOCATE( tkefactor_l(nzb:nzt+1,nysg:nyng) )
2243          tkefactor_l = 0.0_wp
2244          i = nxl - 1
2245          DO  j = nysg, nyng
2246             DO  k = nzb_s_inner(j,i) + 1, nzt
2247                kc     = kco(k+1)
2248                glsf   = ( dx * dy * dzu(k) )**p13
2249                glsc   = ( cg%dx * cg%dy *cg%dzu(kc) )**p13
2250                height = zu(k) - zu(nzb_s_inner(j,i))
2251                fw     = EXP( -cfw * height / glsf )
2252                tkefactor_l(k,j) = c_tkef * ( fw0 * fw + ( 1.0_wp - fw ) *     &
2253                                              ( glsf / glsc )**p23 )
2254             ENDDO
2255             tkefactor_l(nzb_s_inner(j,i),j) = c_tkef * fw0
2256          ENDDO
2257       ENDIF
2258
2259       IF ( nest_bound_r )  THEN
2260          ALLOCATE( tkefactor_r(nzb:nzt+1,nysg:nyng) )
2261          tkefactor_r = 0.0_wp
2262          i = nxr + 1
2263          DO  j = nysg, nyng
2264             DO  k = nzb_s_inner(j,i) + 1, nzt
2265                kc     = kco(k+1)
2266                glsf   = ( dx * dy * dzu(k) )**p13
2267                glsc   = ( cg%dx * cg%dy * cg%dzu(kc) )**p13
2268                height = zu(k) - zu(nzb_s_inner(j,i))
2269                fw     = EXP( -cfw * height / glsf )
2270                tkefactor_r(k,j) = c_tkef * ( fw0 * fw + ( 1.0_wp - fw ) *     &
2271                                              ( glsf / glsc )**p23 )
2272             ENDDO
2273             tkefactor_r(nzb_s_inner(j,i),j) = c_tkef * fw0
2274          ENDDO
2275       ENDIF
2276
2277      IF ( nest_bound_s )  THEN
2278          ALLOCATE( tkefactor_s(nzb:nzt+1,nxlg:nxrg) )
2279          tkefactor_s = 0.0_wp
2280          j = nys - 1
2281          DO  i = nxlg, nxrg
2282             DO  k = nzb_s_inner(j,i) + 1, nzt
2283                kc     = kco(k+1)
2284                glsf   = ( dx * dy * dzu(k) )**p13
2285                glsc   = ( cg%dx * cg%dy * cg%dzu(kc) ) ** p13
2286                height = zu(k) - zu(nzb_s_inner(j,i))
2287                fw     = EXP( -cfw*height / glsf )
2288                tkefactor_s(k,i) = c_tkef * ( fw0 * fw + ( 1.0_wp - fw ) *     &
2289                                              ( glsf / glsc )**p23 )
2290             ENDDO
2291             tkefactor_s(nzb_s_inner(j,i),i) = c_tkef * fw0
2292          ENDDO
2293       ENDIF
2294
2295       IF ( nest_bound_n )  THEN
2296          ALLOCATE( tkefactor_n(nzb:nzt+1,nxlg:nxrg) )
2297          tkefactor_n = 0.0_wp
2298          j = nyn + 1
2299          DO  i = nxlg, nxrg
2300             DO  k = nzb_s_inner(j,i)+1, nzt
2301                kc     = kco(k+1)
2302                glsf   = ( dx * dy * dzu(k) )**p13
2303                glsc   = ( cg%dx * cg%dy * cg%dzu(kc) )**p13
2304                height = zu(k) - zu(nzb_s_inner(j,i))
2305                fw     = EXP( -cfw * height / glsf )
2306                tkefactor_n(k,i) = c_tkef * ( fw0 * fw + ( 1.0_wp - fw ) *     &
2307                                              ( glsf / glsc )**p23 )
2308             ENDDO
2309             tkefactor_n(nzb_s_inner(j,i),i) = c_tkef * fw0
2310          ENDDO
2311       ENDIF
2312
2313       ALLOCATE( tkefactor_t(nysg:nyng,nxlg:nxrg) )
2314       k = nzt
2315       DO  i = nxlg, nxrg
2316          DO  j = nysg, nyng
2317             kc     = kco(k+1)
2318             glsf   = ( dx * dy * dzu(k) )**p13
2319             glsc   = ( cg%dx * cg%dy * cg%dzu(kc) )**p13
2320             height = zu(k) - zu(nzb_s_inner(j,i))
2321             fw     = EXP( -cfw * height / glsf )
2322             tkefactor_t(j,i) = c_tkef * ( fw0 * fw + ( 1.0_wp - fw ) *        &
2323                                           ( glsf / glsc )**p23 )
2324          ENDDO
2325       ENDDO
2326     
2327    END SUBROUTINE pmci_init_tkefactor
2328
2329#endif
2330 END SUBROUTINE pmci_setup_client
2331
2332
2333
2334 SUBROUTINE pmci_setup_coordinates
2335
2336#if defined( __parallel )
2337    IMPLICIT NONE
2338
2339    INTEGER(iwp) ::  i   !:
2340    INTEGER(iwp) ::  j   !:
2341
2342!
2343!-- Create coordinate arrays.
2344    ALLOCATE( coord_x(-nbgp:nx+nbgp) )
2345    ALLOCATE( coord_y(-nbgp:ny+nbgp) )
2346     
2347    DO  i = -nbgp, nx + nbgp
2348       coord_x(i) = lower_left_coord_x + i * dx
2349    ENDDO
2350     
2351    DO  j = -nbgp, ny + nbgp
2352       coord_y(j) = lower_left_coord_y + j * dy
2353    ENDDO
2354
2355#endif
2356 END SUBROUTINE pmci_setup_coordinates
2357
2358
2359
2360
2361 SUBROUTINE pmci_set_array_pointer( name, client_id, nz_cl )
2362
2363    IMPLICIT NONE
2364
2365    INTEGER, INTENT(IN)          ::  client_id   !:
2366    INTEGER, INTENT(IN)          ::  nz_cl       !:
2367    CHARACTER(LEN=*), INTENT(IN) ::  name        !:
2368
2369#if defined( __parallel )
2370    INTEGER(iwp) ::  ierr        !:
2371    INTEGER(iwp) ::  istat       !:
2372
2373    REAL(wp), POINTER, DIMENSION(:,:)   ::  p_2d        !:
2374    REAL(wp), POINTER, DIMENSION(:,:)   ::  p_2d_sec    !:
2375    REAL(wp), POINTER, DIMENSION(:,:,:) ::  p_3d        !:
2376    REAL(wp), POINTER, DIMENSION(:,:,:) ::  p_3d_sec    !:
2377
2378
2379    NULLIFY( p_3d )
2380    NULLIFY( p_2d )
2381
2382!
2383!-- List of array names, which can be coupled.
2384!-- In case of 3D please change also the second array for the pointer version
2385    IF ( TRIM(name) == "u"  )  p_3d => u
2386    IF ( TRIM(name) == "v"  )  p_3d => v
2387    IF ( TRIM(name) == "w"  )  p_3d => w
2388    IF ( TRIM(name) == "e"  )  p_3d => e
2389    IF ( TRIM(name) == "pt" )  p_3d => pt
2390    IF ( TRIM(name) == "q"  )  p_3d => q
2391!
2392!-- Next line is just an example for a 2D array (not active for coupling!)
2393!-- Please note, that z0 has to be declared as TARGET array in modules.f90
2394!    IF ( TRIM(name) == "z0" )    p_2d => z0
2395
2396#if defined( __nopointer )
2397    IF ( ASSOCIATED( p_3d ) )  THEN
2398       CALL pmc_s_set_dataarray( client_id, p_3d, nz_cl, nz )
2399    ELSEIF ( ASSOCIATED( p_2d ) )  THEN
2400       CALL pmc_s_set_dataarray( client_id, p_2d )
2401    ELSE
2402!
2403!--    Give only one message for the root domain
2404       IF ( myid == 0  .AND.  cpl_id == 1 )  THEN
2405
2406          message_string = 'pointer for array "' // TRIM( name ) //            &
2407                           '" can''t be associated'
2408          CALL message( 'pmci_set_array_pointer', 'PA0117', 3, 2, 0, 6, 0 )
2409       ELSE
2410!
2411!--       Avoid others to continue
2412          CALL MPI_BARRIER( comm2d, ierr )
2413       ENDIF
2414    ENDIF
2415#else
2416    IF ( TRIM(name) == "u"  )  p_3d_sec => u_2
2417    IF ( TRIM(name) == "v"  )  p_3d_sec => v_2
2418    IF ( TRIM(name) == "w"  )  p_3d_sec => w_2
2419    IF ( TRIM(name) == "e"  )  p_3d_sec => e_2
2420    IF ( TRIM(name) == "pt" )  p_3d_sec => pt_2
2421    IF ( TRIM(name) == "q"  )  p_3d_sec => q_2
2422
2423    IF ( ASSOCIATED( p_3d ) )  THEN
2424       CALL pmc_s_set_dataarray( client_id, p_3d, nz_cl, nz, &
2425                                 array_2 = p_3d_sec )
2426    ELSEIF ( ASSOCIATED( p_2d ) )  THEN
2427       CALL pmc_s_set_dataarray( client_id, p_2d )
2428    ELSE
2429!
2430!--    Give only one message for the root domain
2431       IF ( myid == 0  .AND.  cpl_id == 1 )  THEN
2432
2433          message_string = 'pointer for array "' // TRIM( name ) //            &
2434                           '" can''t be associated'
2435          CALL message( 'pmci_set_array_pointer', 'PA0117', 3, 2, 0, 6, 0 )
2436       ELSE
2437!
2438!--       Avoid others to continue
2439          CALL MPI_BARRIER( comm2d, ierr )
2440       ENDIF
2441
2442   ENDIF
2443#endif
2444
2445#endif
2446 END SUBROUTINE pmci_set_array_pointer
2447
2448
2449
2450 SUBROUTINE pmci_create_client_arrays( name, is, ie, js, je, nzc  )
2451
2452    IMPLICIT NONE
2453
2454    CHARACTER(LEN=*), INTENT(IN) ::  name    !:
2455
2456    INTEGER(iwp), INTENT(IN) ::  ie      !:
2457    INTEGER(iwp), INTENT(IN) ::  is      !:
2458    INTEGER(iwp), INTENT(IN) ::  je      !:
2459    INTEGER(iwp), INTENT(IN) ::  js      !:
2460    INTEGER(iwp), INTENT(IN) ::  nzc     !:  Note that nzc is cg%nz
2461
2462#if defined( __parallel )
2463    INTEGER(iwp) ::  ierr    !:
2464    INTEGER(iwp) ::  istat   !:
2465
2466    REAL(wp), POINTER,DIMENSION(:,:)   ::  p_2d    !:
2467    REAL(wp), POINTER,DIMENSION(:,:,:) ::  p_3d    !:
2468
2469
2470    NULLIFY( p_3d )
2471    NULLIFY( p_2d )
2472
2473!
2474!-- List of array names, which can be coupled
2475    IF ( TRIM( name ) == "u" )  THEN
2476       IF ( .NOT. ALLOCATED( uc ) )  ALLOCATE( uc(0:nzc+1, js:je, is:ie) )
2477       p_3d => uc
2478    ELSEIF ( TRIM( name ) == "v" )  THEN
2479       IF ( .NOT. ALLOCATED( vc ) )  ALLOCATE( vc(0:nzc+1, js:je, is:ie) )
2480       p_3d => vc
2481    ELSEIF ( TRIM( name ) == "w" )  THEN
2482       IF ( .NOT. ALLOCATED( wc ) )  ALLOCATE( wc(0:nzc+1, js:je, is:ie) )
2483       p_3d => wc
2484    ELSEIF ( TRIM( name ) == "e" )  THEN
2485       IF ( .NOT. ALLOCATED( ec ) )  ALLOCATE( ec(0:nzc+1, js:je, is:ie) )
2486       p_3d => ec
2487    ELSEIF ( TRIM( name ) == "pt")  THEN
2488       IF ( .NOT. ALLOCATED( ptc ) ) ALLOCATE( ptc(0:nzc+1, js:je, is:ie) )
2489       p_3d => ptc
2490    ELSEIF ( TRIM( name ) == "q")  THEN
2491       IF ( .NOT. ALLOCATED( qc ) ) ALLOCATE( qc(0:nzc+1, js:je, is:ie) )
2492       p_3d => qc
2493    !ELSEIF (trim(name) == "z0") then
2494       !IF (.not.allocated(z0c))  allocate(z0c(js:je, is:ie))
2495       !p_2d => z0c
2496    ENDIF
2497
2498    IF ( ASSOCIATED( p_3d ) )  THEN
2499       CALL pmc_c_set_dataarray( p_3d )
2500    ELSEIF ( ASSOCIATED( p_2d ) )  THEN
2501       CALL pmc_c_set_dataarray( p_2d )
2502    ELSE
2503!
2504!--    Give only one message for the first client domain
2505       IF ( myid == 0  .AND.  cpl_id == 2 )  THEN
2506
2507          message_string = 'pointer for array "' // TRIM( name ) //            &
2508                           '" can''t be associated'
2509          CALL message( 'pmci_create_client_arrays', 'PA0170', 3, 2, 0, 6, 0 )
2510       ELSE
2511!
2512!--       Prevent others from continuing
2513          CALL MPI_BARRIER( comm2d, ierr )
2514       ENDIF
2515    ENDIF
2516
2517#endif
2518 END SUBROUTINE pmci_create_client_arrays
2519
2520
2521
2522 SUBROUTINE pmci_server_initialize
2523!-- TO_DO: add general explanations about what this subroutine does
2524#if defined( __parallel )
2525    IMPLICIT NONE
2526
2527    INTEGER(iwp) ::  client_id   !:
2528    INTEGER(iwp) ::  m           !:
2529
2530    REAL(wp) ::  waittime    !:
2531
2532
2533    DO  m = 1, SIZE( pmc_server_for_client ) - 1
2534       client_id = pmc_server_for_client(m)
2535       CALL pmc_s_fillbuffer( client_id, waittime=waittime )
2536    ENDDO
2537
2538#endif
2539 END SUBROUTINE pmci_server_initialize
2540
2541
2542
2543 SUBROUTINE pmci_client_initialize
2544!-- TO_DO: add general explanations about what this subroutine does
2545#if defined( __parallel )
2546    IMPLICIT NONE
2547
2548    INTEGER(iwp) ::  i          !:
2549    INTEGER(iwp) ::  icl        !:
2550    INTEGER(iwp) ::  icr        !:
2551    INTEGER(iwp) ::  j          !:
2552    INTEGER(iwp) ::  jcn        !:
2553    INTEGER(iwp) ::  jcs        !:
2554
2555    REAL(wp) ::  waittime   !:
2556
2557!
2558!-- Root id is never a client
2559    IF ( cpl_id > 1 )  THEN
2560
2561!
2562!--    Client domain boundaries in the server index space
2563       icl = coarse_bound(1)
2564       icr = coarse_bound(2)
2565       jcs = coarse_bound(3)
2566       jcn = coarse_bound(4)
2567
2568!
2569!--    Get data from server
2570       CALL pmc_c_getbuffer( waittime = waittime )
2571
2572!
2573!--    The interpolation.
2574       CALL pmci_interp_tril_all ( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,   &
2575                                   r2yo, r1zo, r2zo, nzb_u_inner, 'u' )
2576       CALL pmci_interp_tril_all ( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,   &
2577                                   r2yv, r1zo, r2zo, nzb_v_inner, 'v' )
2578       CALL pmci_interp_tril_all ( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,   &
2579                                   r2yo, r1zw, r2zw, nzb_w_inner, 'w' )
2580       CALL pmci_interp_tril_all ( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,   &
2581                                   r2yo, r1zo, r2zo, nzb_s_inner, 'e' )
2582       CALL pmci_interp_tril_all ( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,   &
2583                                   r2yo, r1zo, r2zo, nzb_s_inner, 's' )
2584       IF ( humidity  .OR.  passive_scalar )  THEN
2585          CALL pmci_interp_tril_all ( q, qc, ico, jco, kco, r1xo, r2xo, r1yo,  &
2586                                      r2yo, r1zo, r2zo, nzb_s_inner, 's' )
2587       ENDIF
2588
2589       IF ( topography /= 'flat' )  THEN
2590!
2591!--       Inside buildings set velocities and TKE back to zero.
2592!--       Other scalars (pt, q, s, km, kh, p, sa, ...) are ignored at present,
2593!--       maybe revise later.
2594          DO   i = nxlg, nxrg
2595             DO   j = nysg, nyng
2596                u(nzb:nzb_u_inner(j,i),j,i)   = 0.0_wp
2597                v(nzb:nzb_v_inner(j,i),j,i)   = 0.0_wp
2598                w(nzb:nzb_w_inner(j,i),j,i)   = 0.0_wp
2599                e(nzb:nzb_s_inner(j,i),j,i)   = 0.0_wp
2600                u_p(nzb:nzb_u_inner(j,i),j,i) = 0.0_wp
2601                v_p(nzb:nzb_v_inner(j,i),j,i) = 0.0_wp
2602                w_p(nzb:nzb_w_inner(j,i),j,i) = 0.0_wp
2603                e_p(nzb:nzb_s_inner(j,i),j,i) = 0.0_wp
2604             ENDDO
2605          ENDDO
2606       ENDIF
2607    ENDIF
2608
2609
2610 CONTAINS
2611
2612
2613    SUBROUTINE pmci_interp_tril_all( f, fc, ic, jc, kc, r1x, r2x, r1y, r2y,    &
2614                                     r1z, r2z, kb, var )
2615!
2616!--    Interpolation of the internal values for the client-domain initialization
2617!--    This subroutine is based on trilinear interpolation.
2618!--    Coding based on interp_tril_lr/sn/t
2619       IMPLICIT NONE
2620
2621       CHARACTER(LEN=1), INTENT(IN) :: var  !:
2622
2623       INTEGER(iwp), DIMENSION(nxlg:nxrg), INTENT(IN)           ::  ic    !:
2624       INTEGER(iwp), DIMENSION(nysg:nyng), INTENT(IN)           ::  jc    !:
2625       INTEGER(iwp), DIMENSION(nzb:nzt+1), INTENT(IN)           ::  kc    !:
2626       INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg), INTENT(IN) ::  kb    !:
2627
2628       INTEGER(iwp) ::  i      !:
2629       INTEGER(iwp) ::  ib     !:
2630       INTEGER(iwp) ::  ie     !:
2631       INTEGER(iwp) ::  j      !:
2632       INTEGER(iwp) ::  jb     !:
2633       INTEGER(iwp) ::  je     !:
2634       INTEGER(iwp) ::  k      !:
2635       INTEGER(iwp) ::  k1     !:
2636       INTEGER(iwp) ::  kbc    !:
2637       INTEGER(iwp) ::  l      !:
2638       INTEGER(iwp) ::  m      !:
2639       INTEGER(iwp) ::  n      !:
2640
2641       REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(INOUT) :: f !:
2642       REAL(wp), DIMENSION(0:cg%nz+1,jcs:jcn,icl:icr), INTENT(IN) :: fc    !:
2643       REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN) :: r1x   !:
2644       REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN) :: r2x   !:
2645       REAL(wp), DIMENSION(nysg:nyng), INTENT(IN) :: r1y   !:
2646       REAL(wp), DIMENSION(nysg:nyng), INTENT(IN) :: r2y   !:
2647       REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN) :: r1z   !:
2648       REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN) :: r2z   !:
2649
2650       REAL(wp) ::  fk         !:
2651       REAL(wp) ::  fkj        !:
2652       REAL(wp) ::  fkjp       !:
2653       REAL(wp) ::  fkp        !:
2654       REAL(wp) ::  fkpj       !:
2655       REAL(wp) ::  fkpjp      !:
2656       REAL(wp) ::  logratio   !:
2657       REAL(wp) ::  logzuc1    !:
2658       REAL(wp) ::  zuc1       !:
2659
2660
2661       ib = nxl
2662       ie = nxr
2663       jb = nys
2664       je = nyn
2665       IF ( nest_bound_l )  THEN
2666          ib = nxl - 1
2667!
2668!--       For u, nxl is a ghost node, but not for the other variables
2669          IF ( var == 'u' )  THEN
2670             ib = nxl
2671          ENDIF
2672       ENDIF
2673       IF ( nest_bound_s )  THEN
2674          jb = nys - 1
2675!
2676!--       For v, nys is a ghost node, but not for the other variables
2677          IF ( var == 'v' )  THEN
2678             jb = nys
2679          ENDIF
2680       ENDIF
2681       IF ( nest_bound_r )  THEN
2682          ie = nxr + 1
2683       ENDIF
2684       IF ( nest_bound_n )  THEN
2685          je = nyn + 1
2686       ENDIF
2687
2688!
2689!--    Trilinear interpolation.
2690       DO  i = ib, ie
2691          DO  j = jb, je
2692             DO  k = kb(j,i), nzt + 1
2693                l = ic(i)
2694                m = jc(j)
2695                n = kc(k)
2696                fkj      = r1x(i) * fc(n,m,l)     + r2x(i) * fc(n,m,l+1)
2697                fkjp     = r1x(i) * fc(n,m+1,l)   + r2x(i) * fc(n,m+1,l+1)
2698                fkpj     = r1x(i) * fc(n+1,m,l)   + r2x(i) * fc(n+1,m,l+1)
2699                fkpjp    = r1x(i) * fc(n+1,m+1,l) + r2x(i) * fc(n+1,m+1,l+1)
2700                fk       = r1y(j) * fkj  + r2y(j) * fkjp
2701                fkp      = r1y(j) * fkpj + r2y(j) * fkpjp
2702                f(k,j,i) = r1z(k) * fk   + r2z(k) * fkp
2703             ENDDO
2704          ENDDO
2705       ENDDO
2706
2707!
2708!--    Correct the interpolated values of u and v in near-wall nodes, i.e. in
2709!--    the nodes below the coarse-grid nodes with k=1. The corrction is only
2710!--    made over horizontal wall surfaces in this phase. For the nest boundary
2711!--    conditions, a corresponding correction is made for all vertical walls,
2712!--    too.
2713       IF ( var == 'u' .OR. var == 'v' )  THEN
2714          DO  i = ib, nxr
2715             DO  j = jb, nyn
2716                kbc = 1
2717!
2718!--             kbc is the first coarse-grid point above the surface
2719                DO  WHILE ( cg%zu(kbc) < zu(kb(j,i)) )
2720                   kbc = kbc + 1
2721                ENDDO
2722                zuc1 = cg%zu(kbc)
2723                k1   = kb(j,i) + 1
2724                DO  WHILE ( zu(k1) < zuc1 )
2725                   k1 = k1 + 1
2726                ENDDO
2727                logzuc1 = LOG( ( zu(k1) - zu(kb(j,i)) ) / z0(j,i) )
2728
2729                k = kb(j,i) + 1
2730                DO  WHILE ( zu(k) < zuc1 )
2731                   logratio = ( LOG( ( zu(k) - zu(kb(j,i)) ) / z0(j,i)) ) / logzuc1
2732                   f(k,j,i) = logratio * f(k1,j,i)
2733                   k  = k + 1
2734                ENDDO
2735                f(kb(j,i),j,i) = 0.0_wp
2736             ENDDO
2737          ENDDO
2738
2739       ELSEIF ( var == 'w' )  THEN
2740
2741          DO  i = ib, nxr
2742              DO  j = jb, nyn
2743                f(kb(j,i),j,i) = 0.0_wp
2744             ENDDO
2745          ENDDO
2746
2747       ENDIF
2748
2749    END SUBROUTINE pmci_interp_tril_all
2750
2751#endif
2752 END SUBROUTINE pmci_client_initialize
2753
2754
2755
2756 SUBROUTINE pmci_check_setting_mismatches
2757!
2758!-- Check for mismatches between settings of master and client variables
2759!-- (e.g., all clients have to follow the end_time settings of the root model).
2760!-- The root model overwrites variables in the other models, so these variables
2761!-- only need to be set once in file PARIN.
2762
2763#if defined( __parallel )
2764
2765    USE control_parameters,                                                    &
2766        ONLY:  dt_restart, end_time, message_string, restart_time, time_restart
2767
2768    IMPLICIT NONE
2769
2770    INTEGER ::  ierr
2771
2772    REAL(wp) ::  dt_restart_root
2773    REAL(wp) ::  end_time_root
2774    REAL(wp) ::  restart_time_root
2775    REAL(wp) ::  time_restart_root
2776
2777!
2778!-- Check the time to be simulated.
2779!-- Here, and in the following, the root process communicates the respective
2780!-- variable to all others, and its value will then be compared with the local
2781!-- values.
2782    IF ( pmc_is_rootmodel() )  end_time_root = end_time
2783    CALL MPI_BCAST( end_time_root, 1, MPI_REAL, 0, comm_world_nesting, ierr )
2784
2785    IF ( .NOT. pmc_is_rootmodel() )  THEN
2786       IF ( end_time /= end_time_root )  THEN
2787          WRITE( message_string, * )  'mismatch between root model and ',      &
2788               'client settings &   end_time(root) = ', end_time_root,         &
2789               ' &   end_time(client) = ', end_time, ' & client value is set', &
2790               ' to root value'
2791          CALL message( 'pmci_check_setting_mismatches', 'PA0419', 0, 1, 0, 6, &
2792                        0 )
2793          end_time = end_time_root
2794       ENDIF
2795    ENDIF
2796
2797!
2798!-- Same for restart time
2799    IF ( pmc_is_rootmodel() )  restart_time_root = restart_time
2800    CALL MPI_BCAST( restart_time_root, 1, MPI_REAL, 0, comm_world_nesting, ierr )
2801
2802    IF ( .NOT. pmc_is_rootmodel() )  THEN
2803       IF ( restart_time /= restart_time_root )  THEN
2804          WRITE( message_string, * )  'mismatch between root model and ',      &
2805               'client settings &   restart_time(root) = ', restart_time_root, &
2806               ' &   restart_time(client) = ', restart_time, ' & client ',     &
2807               'value is set to root value'
2808          CALL message( 'pmci_check_setting_mismatches', 'PA0419', 0, 1, 0, 6, &
2809                        0 )
2810          restart_time = restart_time_root
2811       ENDIF
2812    ENDIF
2813
2814!
2815!-- Same for dt_restart
2816    IF ( pmc_is_rootmodel() )  dt_restart_root = dt_restart
2817    CALL MPI_BCAST( dt_restart_root, 1, MPI_REAL, 0, comm_world_nesting, ierr )
2818
2819    IF ( .NOT. pmc_is_rootmodel() )  THEN
2820       IF ( dt_restart /= dt_restart_root )  THEN
2821          WRITE( message_string, * )  'mismatch between root model and ',      &
2822               'client settings &   dt_restart(root) = ', dt_restart_root,     &
2823               ' &   dt_restart(client) = ', dt_restart, ' & client ',         &
2824               'value is set to root value'
2825          CALL message( 'pmci_check_setting_mismatches', 'PA0419', 0, 1, 0, 6, &
2826                        0 )
2827          dt_restart = dt_restart_root
2828       ENDIF
2829    ENDIF
2830
2831!
2832!-- Same for time_restart
2833    IF ( pmc_is_rootmodel() )  time_restart_root = time_restart
2834    CALL MPI_BCAST( time_restart_root, 1, MPI_REAL, 0, comm_world_nesting, ierr )
2835
2836    IF ( .NOT. pmc_is_rootmodel() )  THEN
2837       IF ( time_restart /= time_restart_root )  THEN
2838          WRITE( message_string, * )  'mismatch between root model and ',      &
2839               'client settings &   time_restart(root) = ', time_restart_root, &
2840               ' &   time_restart(client) = ', time_restart, ' & client ',     &
2841               'value is set to root value'
2842          CALL message( 'pmci_check_setting_mismatches', 'PA0419', 0, 1, 0, 6, &
2843                        0 )
2844          time_restart = time_restart_root
2845       ENDIF
2846    ENDIF
2847
2848#endif
2849
2850 END SUBROUTINE pmci_check_setting_mismatches
2851
2852
2853
2854 SUBROUTINE pmci_ensure_nest_mass_conservation
2855
2856#if defined( __parallel )
2857!
2858!-- Adjust the volume-flow rate through the top boundary so that the net volume
2859!-- flow through all boundaries of the current nest domain becomes zero.
2860    IMPLICIT NONE
2861
2862    INTEGER(iwp) ::  i                          !:
2863    INTEGER(iwp) ::  ierr                       !:
2864    INTEGER(iwp) ::  j                          !:
2865    INTEGER(iwp) ::  k                          !:
2866
2867    REAL(wp) ::  dxdy                            !:
2868    REAL(wp) ::  innor                           !:
2869    REAL(wp) ::  w_lt                            !:
2870    REAL(wp), DIMENSION(1:3) ::  volume_flow_l   !:
2871
2872!
2873!-- Sum up the volume flow through the left/right boundaries
2874    volume_flow(1)   = 0.0_wp
2875    volume_flow_l(1) = 0.0_wp
2876
2877    IF ( nest_bound_l )  THEN
2878       i = 0
2879       innor = dy
2880       DO   j = nys, nyn
2881          DO   k = nzb_u_inner(j,i)+1, nzt
2882             volume_flow_l(1) = volume_flow_l(1) + innor * u(k,j,i) * dzw(k)
2883          ENDDO
2884       ENDDO
2885    ENDIF
2886
2887    IF ( nest_bound_r )  THEN
2888       i = nx + 1
2889       innor = -dy
2890       DO   j = nys, nyn
2891          DO   k = nzb_u_inner(j,i)+1, nzt
2892             volume_flow_l(1) = volume_flow_l(1) + innor * u(k,j,i) * dzw(k)
2893          ENDDO
2894       ENDDO
2895    ENDIF
2896
2897#if defined( __parallel )
2898    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2899    CALL MPI_ALLREDUCE( volume_flow_l(1), volume_flow(1), 1, MPI_REAL, &
2900                        MPI_SUM, comm2d, ierr )
2901#else
2902    volume_flow(1) = volume_flow_l(1)
2903#endif
2904
2905!
2906!-- Sum up the volume flow through the south/north boundaries
2907    volume_flow(2)   = 0.0_wp
2908    volume_flow_l(2) = 0.0_wp
2909
2910    IF ( nest_bound_s )  THEN
2911       j = 0
2912       innor = dx
2913       DO   i = nxl, nxr
2914          DO   k = nzb_v_inner(j,i)+1, nzt
2915             volume_flow_l(2) = volume_flow_l(2) + innor * v(k,j,i) * dzw(k)
2916          ENDDO
2917       ENDDO
2918    ENDIF
2919
2920    IF ( nest_bound_n )  THEN
2921       j = ny + 1
2922       innor = -dx
2923       DO   i = nxl, nxr
2924          DO   k = nzb_v_inner(j,i)+1, nzt
2925             volume_flow_l(2) = volume_flow_l(2) + innor * v(k,j,i) * dzw(k)
2926          ENDDO
2927       ENDDO
2928    ENDIF
2929
2930#if defined( __parallel )
2931    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2932    CALL MPI_ALLREDUCE( volume_flow_l(2), volume_flow(2), 1, MPI_REAL,         &
2933                        MPI_SUM, comm2d, ierr )
2934#else
2935    volume_flow(2) = volume_flow_l(2)
2936#endif
2937
2938!
2939!-- Sum up the volume flow through the top boundary
2940    volume_flow(3)   = 0.0_wp
2941    volume_flow_l(3) = 0.0_wp
2942    dxdy = dx * dy
2943    k = nzt
2944    DO   i = nxl, nxr
2945       DO   j = nys, nyn
2946          volume_flow_l(3) = volume_flow_l(3) - w(k,j,i) * dxdy
2947       ENDDO
2948    ENDDO
2949
2950#if defined( __parallel )
2951    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2952    CALL MPI_ALLREDUCE( volume_flow_l(3), volume_flow(3), 1, MPI_REAL,         &
2953                        MPI_SUM, comm2d, ierr )
2954#else
2955    volume_flow(3) = volume_flow_l(3)
2956#endif
2957
2958!
2959!-- Correct the top-boundary value of w
2960    w_lt = (volume_flow(1) + volume_flow(2) + volume_flow(3)) / area_t
2961    DO   i = nxl, nxr
2962       DO   j = nys, nyn
2963          DO  k = nzt, nzt + 1
2964             w(k,j,i) = w(k,j,i) + w_lt
2965          ENDDO
2966       ENDDO
2967    ENDDO
2968
2969#endif
2970 END SUBROUTINE pmci_ensure_nest_mass_conservation
2971
2972
2973
2974 SUBROUTINE pmci_synchronize
2975
2976#if defined( __parallel )
2977!
2978!-- Unify the time steps for each model and synchronize using
2979!-- MPI_ALLREDUCE with the MPI_MIN operator over all processes using
2980!-- the global communicator MPI_COMM_WORLD.
2981   IMPLICIT NONE
2982
2983   INTEGER(iwp)           :: ierr  !:
2984   REAL(wp), DIMENSION(1) :: dtl   !:
2985   REAL(wp), DIMENSION(1) :: dtg   !:
2986
2987   
2988   dtl(1) = dt_3d 
2989   CALL MPI_ALLREDUCE( dtl, dtg, 1, MPI_REAL, MPI_MIN, MPI_COMM_WORLD, ierr )
2990   dt_3d  = dtg(1)
2991
2992 END SUBROUTINE pmci_synchronize
2993               
2994
2995
2996 SUBROUTINE pmci_set_swaplevel( swaplevel )
2997!
2998!-- After each Runge-Kutta sub-timestep, alternately set buffer one or buffer
2999!-- two active
3000
3001    IMPLICIT NONE
3002
3003    INTEGER(iwp),INTENT(IN) ::  swaplevel  !: swaplevel (1 or 2) of PALM's
3004                                           !: timestep
3005
3006    INTEGER(iwp)            ::  client_id  !:
3007    INTEGER(iwp)            ::  m          !:
3008
3009    DO  m = 1, SIZE( pmc_server_for_client )-1
3010       client_id = pmc_server_for_client(m)
3011       CALL pmc_s_set_active_data_array( client_id, swaplevel )
3012    ENDDO
3013
3014 END SUBROUTINE pmci_set_swaplevel
3015
3016
3017
3018 SUBROUTINE pmci_datatrans( local_nesting_mode )
3019!
3020!-- Althoug nesting_mode is a variable of this model, pass it as an argument to
3021!-- allow for example to force one-way initialization phase
3022
3023    IMPLICIT NONE
3024
3025    INTEGER(iwp)           ::  ierr   !:
3026    INTEGER(iwp)           ::  istat  !:
3027
3028    CHARACTER(LEN=*),INTENT(IN) ::  local_nesting_mode
3029
3030    IF ( local_nesting_mode == 'one-way' )  THEN
3031
3032       CALL pmci_client_datatrans( server_to_client )
3033       CALL pmci_server_datatrans( server_to_client )
3034
3035    ELSE
3036
3037       IF( nesting_datatransfer_mode == 'cascade' )  THEN
3038
3039          CALL pmci_client_datatrans( server_to_client )
3040          CALL pmci_server_datatrans( server_to_client )
3041
3042          CALL pmci_server_datatrans( client_to_server )
3043          CALL pmci_client_datatrans( client_to_server )
3044
3045       ELSEIF( nesting_datatransfer_mode == 'overlap')  THEN
3046
3047          CALL pmci_server_datatrans( server_to_client )
3048          CALL pmci_client_datatrans( server_to_client )
3049
3050          CALL pmci_client_datatrans( client_to_server )
3051          CALL pmci_server_datatrans( client_to_server )
3052
3053       ELSEIF( TRIM( nesting_datatransfer_mode ) == 'mixed' )  THEN
3054
3055          CALL pmci_server_datatrans( server_to_client )
3056          CALL pmci_client_datatrans( server_to_client )
3057
3058          CALL pmci_server_datatrans( client_to_server )
3059          CALL pmci_client_datatrans( client_to_server )
3060
3061       ENDIF
3062
3063    ENDIF
3064
3065 END SUBROUTINE pmci_datatrans
3066
3067
3068
3069
3070 SUBROUTINE pmci_server_datatrans( direction )
3071
3072    IMPLICIT NONE
3073
3074    INTEGER(iwp),INTENT(IN) ::  direction   !:
3075
3076#if defined( __parallel )
3077    INTEGER(iwp) ::  client_id   !:
3078    INTEGER(iwp) ::  i           !:
3079    INTEGER(iwp) ::  j           !:
3080    INTEGER(iwp) ::  ierr        !:
3081    INTEGER(iwp) ::  m           !:
3082
3083    REAL(wp)               ::  waittime    !:
3084    REAL(wp), DIMENSION(1) ::  dtc         !:
3085    REAL(wp), DIMENSION(1) ::  dtl         !:
3086
3087
3088    DO  m = 1, SIZE( PMC_Server_for_Client )-1
3089       client_id = PMC_Server_for_Client(m)
3090       
3091       IF ( direction == server_to_client )  THEN
3092          CALL cpu_log( log_point_s(71), 'pmc server send', 'start' )
3093          CALL pmc_s_fillbuffer( client_id )
3094          CALL cpu_log( log_point_s(71), 'pmc server send', 'stop' )
3095       ELSE
3096!
3097!--       Communication from client to server
3098          CALL cpu_log( log_point_s(72), 'pmc server recv', 'start' )
3099          client_id = pmc_server_for_client(m)
3100          CALL pmc_s_getdata_from_buffer( client_id )
3101          CALL cpu_log( log_point_s(72), 'pmc server recv', 'stop' )
3102
3103!
3104!--       The anterpolated data is now available in u etc
3105          IF ( topography /= 'flat' )  THEN
3106
3107!
3108!--          Inside buildings/topography reset velocities and TKE back to zero.
3109!--          Other scalars (pt, q, s, km, kh, p, sa, ...) are ignored at
3110!--          present, maybe revise later.
3111             DO   i = nxlg, nxrg
3112                DO   j = nysg, nyng
3113                   u(nzb:nzb_u_inner(j,i),j,i)  = 0.0_wp
3114                   v(nzb:nzb_v_inner(j,i),j,i)  = 0.0_wp
3115                   w(nzb:nzb_w_inner(j,i),j,i)  = 0.0_wp
3116                   e(nzb:nzb_s_inner(j,i),j,i)  = 0.0_wp
3117!
3118!--                TO_DO: zero setting of temperature within topography creates
3119!--                       wrong results
3120!                   pt(nzb:nzb_s_inner(j,i),j,i) = 0.0_wp
3121!                   IF ( humidity  .OR.  passive_scalar )  THEN
3122!                      q(nzb:nzb_s_inner(j,i),j,i) = 0.0_wp
3123!                   ENDIF
3124                ENDDO
3125             ENDDO
3126          ENDIF
3127       ENDIF
3128    ENDDO
3129
3130#endif
3131 END SUBROUTINE pmci_server_datatrans
3132
3133
3134
3135 SUBROUTINE pmci_client_datatrans( direction )
3136
3137    IMPLICIT NONE
3138
3139    INTEGER(iwp), INTENT(IN) ::  direction   !:
3140
3141#if defined( __parallel )
3142    INTEGER(iwp) ::  ierr        !:
3143    INTEGER(iwp) ::  icl         !:
3144    INTEGER(iwp) ::  icr         !:
3145    INTEGER(iwp) ::  jcs         !:
3146    INTEGER(iwp) ::  jcn         !:
3147   
3148    REAL(wp), DIMENSION(1) ::  dtl         !:
3149    REAL(wp), DIMENSION(1) ::  dts         !:
3150
3151
3152    dtl = dt_3d
3153    IF ( cpl_id > 1 )  THEN
3154!
3155!--    Client domain boundaries in the server indice space.
3156       icl = coarse_bound(1)
3157       icr = coarse_bound(2)
3158       jcs = coarse_bound(3)
3159       jcn = coarse_bound(4)
3160
3161       IF ( direction == server_to_client )  THEN
3162
3163          CALL cpu_log( log_point_s(73), 'pmc client recv', 'start' )
3164          CALL pmc_c_getbuffer( )
3165          CALL cpu_log( log_point_s(73), 'pmc client recv', 'stop' )
3166
3167          CALL cpu_log( log_point_s(75), 'pmc interpolation', 'start' )
3168          CALL pmci_interpolation
3169          CALL cpu_log( log_point_s(75), 'pmc interpolation', 'stop' )
3170
3171       ELSE
3172!
3173!--       direction == client_to_server
3174          CALL cpu_log( log_point_s(76), 'pmc anterpolation', 'start' )
3175          CALL pmci_anterpolation
3176          CALL cpu_log( log_point_s(76), 'pmc anterpolation', 'stop' )
3177
3178          CALL cpu_log( log_point_s(74), 'pmc client send', 'start' )
3179          CALL pmc_c_putbuffer( )
3180          CALL cpu_log( log_point_s(74), 'pmc client send', 'stop' )
3181
3182       ENDIF
3183    ENDIF
3184
3185 CONTAINS
3186
3187    SUBROUTINE pmci_interpolation
3188
3189!
3190!--    A wrapper routine for all interpolation and extrapolation actions
3191       IMPLICIT NONE
3192
3193!
3194!--    Add IF-condition here: IF not vertical nesting
3195!--    Left border pe:
3196       IF ( nest_bound_l )  THEN
3197          CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,  &
3198                                    r2yo, r1zo, r2zo, nzb_u_inner, logc_u_l,   &
3199                                    logc_ratio_u_l, nzt_topo_nestbc_l, 'l',    &
3200                                    'u' )
3201          CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,  &
3202                                    r2yv, r1zo, r2zo, nzb_v_inner, logc_v_l,   &
3203                                    logc_ratio_v_l, nzt_topo_nestbc_l, 'l',    &
3204                                    'v' )
3205          CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,  &
3206                                    r2yo, r1zw, r2zw, nzb_w_inner, logc_w_l,   &
3207                                    logc_ratio_w_l, nzt_topo_nestbc_l, 'l',    &
3208                                    'w' )
3209          CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,  &
3210                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_l,   &
3211                                    logc_ratio_u_l, nzt_topo_nestbc_l, 'l',    &
3212                                    'e' )
3213          CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,  &
3214                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_l,   &
3215                                    logc_ratio_u_l, nzt_topo_nestbc_l, 'l',    &
3216                                    's' )
3217          IF ( humidity  .OR.  passive_scalar )  THEN
3218             CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, &
3219                                       r2yo, r1zo, r2zo, nzb_s_inner,          &
3220                                       logc_u_l, logc_ratio_u_l,               &
3221                                       nzt_topo_nestbc_l, 'l', 's' )
3222          ENDIF
3223
3224          IF ( nesting_mode == 'one-way' )  THEN
3225             CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'l', 'u' )
3226             CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'l', 'v' )
3227             CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'l', 'w' )
3228             CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'l', 'e' )
3229             CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'l', 's' )
3230             IF ( humidity  .OR.  passive_scalar )  THEN
3231                CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'l', 's' )
3232             ENDIF
3233          ENDIF
3234
3235       ENDIF
3236!
3237!--    Right border pe
3238       IF ( nest_bound_r )  THEN
3239          CALL pmci_interp_tril_lr( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,  &
3240                                    r2yo, r1zo, r2zo, nzb_u_inner, logc_u_r,   &
3241                                    logc_ratio_u_r, nzt_topo_nestbc_r, 'r',    &
3242                                    'u' )
3243          CALL pmci_interp_tril_lr( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,  &
3244                                    r2yv, r1zo, r2zo, nzb_v_inner, logc_v_r,   &
3245                                    logc_ratio_v_r, nzt_topo_nestbc_r, 'r',    &
3246                                    'v' )
3247          CALL pmci_interp_tril_lr( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,  &
3248                                    r2yo, r1zw, r2zw, nzb_w_inner, logc_w_r,   &
3249                                    logc_ratio_w_r, nzt_topo_nestbc_r, 'r',    &
3250                                    'w' )
3251          CALL pmci_interp_tril_lr( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,  &
3252                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_r,   &
3253                                    logc_ratio_u_r, nzt_topo_nestbc_r, 'r',    &
3254                                    'e' )
3255          CALL pmci_interp_tril_lr( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,  &
3256                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_r,   &
3257                                    logc_ratio_u_r, nzt_topo_nestbc_r, 'r',    &
3258                                    's' )
3259          IF ( humidity  .OR.  passive_scalar )  THEN
3260             CALL pmci_interp_tril_lr( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, &
3261                                       r2yo, r1zo, r2zo, nzb_s_inner,          &
3262                                       logc_u_r, logc_ratio_u_r,               &
3263                                       nzt_topo_nestbc_r, 'r', 's' )
3264          ENDIF
3265
3266          IF ( nesting_mode == 'one-way' )  THEN
3267             CALL pmci_extrap_ifoutflow_lr( u, nzb_u_inner, 'r', 'u' )
3268             CALL pmci_extrap_ifoutflow_lr( v, nzb_v_inner, 'r', 'v' )
3269             CALL pmci_extrap_ifoutflow_lr( w, nzb_w_inner, 'r', 'w' )
3270             CALL pmci_extrap_ifoutflow_lr( e, nzb_s_inner, 'r', 'e' )
3271             CALL pmci_extrap_ifoutflow_lr( pt,nzb_s_inner, 'r', 's' )
3272             IF ( humidity  .OR.  passive_scalar )  THEN
3273                CALL pmci_extrap_ifoutflow_lr( q, nzb_s_inner, 'r', 's' )
3274             ENDIF
3275          ENDIF
3276
3277       ENDIF
3278!
3279!--    South border pe
3280       IF ( nest_bound_s )  THEN
3281          CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,  &
3282                                    r2yo, r1zo, r2zo, nzb_u_inner, logc_u_s,   &
3283                                    logc_ratio_u_s, nzt_topo_nestbc_s, 's',    &
3284                                    'u' )
3285          CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,  &
3286                                    r2yv, r1zo, r2zo, nzb_v_inner, logc_v_s,   &
3287                                    logc_ratio_v_s, nzt_topo_nestbc_s, 's',    &
3288                                    'v' )
3289          CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,  &
3290                                    r2yo, r1zw, r2zw, nzb_w_inner, logc_w_s,   &
3291                                    logc_ratio_w_s, nzt_topo_nestbc_s, 's',    &
3292                                    'w' )
3293          CALL pmci_interp_tril_sn( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,  &
3294                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_s,   &
3295                                    logc_ratio_u_s, nzt_topo_nestbc_s, 's',    &
3296                                    'e' )
3297          CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,  &
3298                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_s,   &
3299                                    logc_ratio_u_s, nzt_topo_nestbc_s, 's',    &
3300                                    's' )
3301          IF ( humidity  .OR.  passive_scalar )  THEN
3302             CALL pmci_interp_tril_sn( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, &
3303                                       r2yo, r1zo, r2zo, nzb_s_inner,          &
3304                                       logc_u_s, logc_ratio_u_s,               &
3305                                       nzt_topo_nestbc_s, 's', 's' )
3306          ENDIF
3307
3308          IF ( nesting_mode == 'one-way' )  THEN
3309             CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 's', 'u' )
3310             CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 's', 'v' )
3311             CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 's', 'w' )
3312             CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 's', 'e' )
3313             CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 's', 's' )
3314             IF ( humidity  .OR.  passive_scalar )  THEN
3315                CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 's', 's' )
3316             ENDIF
3317          ENDIF
3318
3319       ENDIF
3320!
3321!--    North border pe
3322       IF ( nest_bound_n )  THEN
3323          CALL pmci_interp_tril_sn( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,  &
3324                                    r2yo, r1zo, r2zo, nzb_u_inner, logc_u_n,   &
3325                                    logc_ratio_u_n, nzt_topo_nestbc_n, 'n',    &
3326                                    'u' )
3327          CALL pmci_interp_tril_sn( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,  &
3328                                    r2yv, r1zo, r2zo, nzb_v_inner, logc_v_n,   &
3329                                    logc_ratio_v_n, nzt_topo_nestbc_n, 'n',    &
3330                                    'v' )
3331          CALL pmci_interp_tril_sn( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,  &
3332                                    r2yo, r1zw, r2zw, nzb_w_inner, logc_w_n,   &
3333                                    logc_ratio_w_n, nzt_topo_nestbc_n, 'n',    &
3334                                    'w' )
3335          CALL pmci_interp_tril_sn( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,  &
3336                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_n,   &
3337                                    logc_ratio_u_n, nzt_topo_nestbc_n, 'n',    &
3338                                    'e' )
3339          CALL pmci_interp_tril_sn( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,  &
3340                                    r2yo, r1zo, r2zo, nzb_s_inner, logc_u_n,   &
3341                                    logc_ratio_u_n, nzt_topo_nestbc_n, 'n',    &
3342                                    's' )
3343          IF ( humidity  .OR.  passive_scalar )  THEN
3344             CALL pmci_interp_tril_sn( q, qc, ico, jco, kco, r1xo, r2xo, r1yo, &
3345                                       r2yo, r1zo, r2zo, nzb_s_inner,          &
3346                                       logc_u_n, logc_ratio_u_n,               &
3347                                       nzt_topo_nestbc_n, 'n', 's' )
3348          ENDIF
3349
3350          IF ( nesting_mode == 'one-way' )  THEN
3351             CALL pmci_extrap_ifoutflow_sn( u, nzb_u_inner, 'n', 'u' )
3352             CALL pmci_extrap_ifoutflow_sn( v, nzb_v_inner, 'n', 'v' )
3353             CALL pmci_extrap_ifoutflow_sn( w, nzb_w_inner, 'n', 'w' )
3354             CALL pmci_extrap_ifoutflow_sn( e, nzb_s_inner, 'n', 'e' )
3355             CALL pmci_extrap_ifoutflow_sn( pt,nzb_s_inner, 'n', 's' )
3356             IF ( humidity  .OR.  passive_scalar )  THEN
3357                CALL pmci_extrap_ifoutflow_sn( q, nzb_s_inner, 'n', 's' )
3358             ENDIF
3359
3360          ENDIF
3361
3362       ENDIF
3363
3364!
3365!--    All PEs are top-border PEs
3366       CALL pmci_interp_tril_t( u,  uc,  icu, jco, kco, r1xu, r2xu, r1yo,      &
3367                                r2yo, r1zo, r2zo, 'u' )
3368       CALL pmci_interp_tril_t( v,  vc,  ico, jcv, kco, r1xo, r2xo, r1yv,      &
3369                                r2yv, r1zo, r2zo, 'v' )
3370       CALL pmci_interp_tril_t( w,  wc,  ico, jco, kcw, r1xo, r2xo, r1yo,      &
3371                                r2yo, r1zw, r2zw, 'w' )
3372       CALL pmci_interp_tril_t( e,  ec,  ico, jco, kco, r1xo, r2xo, r1yo,      &
3373                                r2yo, r1zo, r2zo, 'e' )
3374       CALL pmci_interp_tril_t( pt, ptc, ico, jco, kco, r1xo, r2xo, r1yo,      &
3375                                r2yo, r1zo, r2zo, 's' )
3376       IF ( humidity .OR. passive_scalar )  THEN
3377          CALL pmci_interp_tril_t( q, qc, ico, jco, kco, r1xo, r2xo, r1yo,     &
3378                                   r2yo, r1zo, r2zo, 's' )
3379       ENDIF
3380
3381       IF ( nesting_mode == 'one-way' )  THEN
3382          CALL pmci_extrap_ifoutflow_t( u,  'u' )
3383          CALL pmci_extrap_ifoutflow_t( v,  'v' )
3384          CALL pmci_extrap_ifoutflow_t( w,  'w' )
3385          CALL pmci_extrap_ifoutflow_t( e,  'e' )
3386          CALL pmci_extrap_ifoutflow_t( pt, 's' )
3387          IF ( humidity  .OR.  passive_scalar )  THEN
3388             CALL pmci_extrap_ifoutflow_t( q, 's' )
3389          ENDIF
3390      ENDIF
3391
3392   END SUBROUTINE pmci_interpolation
3393
3394
3395
3396   SUBROUTINE pmci_anterpolation
3397
3398!
3399!--   A wrapper routine for all anterpolation actions.
3400      IMPLICIT NONE
3401
3402      CALL pmci_anterp_tophat( u,  uc,  kctu, iflu, ifuu, jflo, jfuo, kflo,    &
3403                               kfuo, 'u' )
3404      CALL pmci_anterp_tophat( v,  vc,  kctu, iflo, ifuo, jflv, jfuv, kflo,    &
3405                               kfuo, 'v' )
3406      CALL pmci_anterp_tophat( w,  wc,  kctw, iflo, ifuo, jflo, jfuo, kflw,    &
3407                               kfuw, 'w' )
3408      CALL pmci_anterp_tophat( pt, ptc, kctu, iflo, ifuo, jflo, jfuo, kflo,    &
3409                               kfuo, 's' )
3410      IF ( humidity  .OR.  passive_scalar )  THEN
3411         CALL pmci_anterp_tophat( q, qc, kctu, iflo, ifuo, jflo, jfuo, kflo,   &
3412                                  kfuo, 's' )
3413      ENDIF
3414
3415   END SUBROUTINE pmci_anterpolation
3416
3417
3418
3419   SUBROUTINE pmci_interp_tril_lr( f, fc, ic, jc, kc, r1x, r2x, r1y, r2y, r1z, &
3420                                   r2z, kb, logc, logc_ratio, nzt_topo_nestbc, &
3421                                   edge, var )
3422!
3423!--   Interpolation of ghost-node values used as the client-domain boundary
3424!--   conditions. This subroutine handles the left and right boundaries. It is
3425!--   based on trilinear interpolation.
3426
3427      IMPLICIT NONE
3428
3429      REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                      &
3430                                      INTENT(INOUT) ::  f       !:
3431      REAL(wp), DIMENSION(0:cg%nz+1,jcs:jcn,icl:icr),                          &
3432                                      INTENT(IN)    ::  fc      !:
3433      REAL(wp), DIMENSION(1:2,0:ncorr-1,nzb:nzt_topo_nestbc,nys:nyn),          &
3434                                      INTENT(IN)    ::  logc_ratio   !:
3435      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r1x     !:
3436      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r2x     !:
3437      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r1y     !:
3438      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r2y     !:
3439      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r1z     !:
3440      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r2z     !:
3441     
3442      INTEGER(iwp), DIMENSION(nxlg:nxrg), INTENT(IN)           ::  ic     !:
3443      INTEGER(iwp), DIMENSION(nysg:nyng), INTENT(IN)           ::  jc     !:
3444      INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg), INTENT(IN) ::  kb     !:
3445      INTEGER(iwp), DIMENSION(nzb:nzt+1), INTENT(IN)           ::  kc     !:
3446      INTEGER(iwp), DIMENSION(1:2,nzb:nzt_topo_nestbc,nys:nyn),                &
3447                                          INTENT(IN)           ::  logc   !:
3448      INTEGER(iwp) ::  nzt_topo_nestbc   !:
3449
3450      CHARACTER(LEN=1),INTENT(IN) ::  edge   !:
3451      CHARACTER(LEN=1),INTENT(IN) ::  var    !:
3452
3453      INTEGER(iwp) ::  i       !:
3454      INTEGER(iwp) ::  ib      !:
3455      INTEGER(iwp) ::  ibgp    !:
3456      INTEGER(iwp) ::  iw      !:
3457      INTEGER(iwp) ::  j       !:
3458      INTEGER(iwp) ::  jco     !:
3459      INTEGER(iwp) ::  jcorr   !:
3460      INTEGER(iwp) ::  jinc    !:
3461      INTEGER(iwp) ::  jw      !:
3462      INTEGER(iwp) ::  j1      !:
3463      INTEGER(iwp) ::  k       !:
3464      INTEGER(iwp) ::  kco     !:
3465      INTEGER(iwp) ::  kcorr   !:
3466      INTEGER(iwp) ::  k1      !:
3467      INTEGER(iwp) ::  l       !:
3468      INTEGER(iwp) ::  m       !:
3469      INTEGER(iwp) ::  n       !:
3470      INTEGER(iwp) ::  kbc     !:
3471     
3472      REAL(wp) ::  coarse_dx   !:
3473      REAL(wp) ::  coarse_dy   !:
3474      REAL(wp) ::  coarse_dz   !:
3475      REAL(wp) ::  fkj         !:
3476      REAL(wp) ::  fkjp        !:
3477      REAL(wp) ::  fkpj        !:
3478      REAL(wp) ::  fkpjp       !:
3479      REAL(wp) ::  fk          !:
3480      REAL(wp) ::  fkp         !:
3481     
3482!
3483!--   Check which edge is to be handled
3484      IF ( edge == 'l' )  THEN
3485!
3486!--      For u, nxl is a ghost node, but not for the other variables
3487         IF ( var == 'u' )  THEN
3488            i  = nxl
3489            ib = nxl - 1 
3490         ELSE
3491            i  = nxl - 1
3492            ib = nxl - 2
3493         ENDIF
3494      ELSEIF ( edge == 'r' )  THEN
3495         i  = nxr + 1
3496         ib = nxr + 2
3497      ENDIF
3498     
3499      DO  j = nys, nyn+1
3500         DO  k = kb(j,i), nzt+1
3501            l = ic(i)
3502            m = jc(j)
3503            n = kc(k)
3504            fkj      = r1x(i) * fc(n,m,l)     + r2x(i) * fc(n,m,l+1)
3505            fkjp     = r1x(i) * fc(n,m+1,l)   + r2x(i) * fc(n,m+1,l+1)
3506            fkpj     = r1x(i) * fc(n+1,m,l)   + r2x(i) * fc(n+1,m,l+1)
3507            fkpjp    = r1x(i) * fc(n+1,m+1,l) + r2x(i) * fc(n+1,m+1,l+1)
3508            fk       = r1y(j) * fkj  + r2y(j) * fkjp
3509            fkp      = r1y(j) * fkpj + r2y(j) * fkpjp
3510            f(k,j,i) = r1z(k) * fk   + r2z(k) * fkp
3511         ENDDO
3512      ENDDO
3513
3514!
3515!--   Generalized log-law-correction algorithm.
3516!--   Doubly two-dimensional index arrays logc(1:2,:,:) and log-ratio arrays
3517!--   logc_ratio(1:2,0:ncorr-1,:,:) have been precomputed in subroutine
3518!--   pmci_init_loglaw_correction.
3519!
3520!--   Solid surface below the node
3521      IF ( var == 'u' .OR. var == 'v' )  THEN           
3522         DO  j = nys, nyn
3523            k = kb(j,i)+1
3524            IF ( ( logc(1,k,j) /= 0 )  .AND.  ( logc(2,k,j) == 0 ) )  THEN
3525               k1 = logc(1,k,j)
3526               DO  kcorr = 0, ncorr - 1
3527                  kco = k + kcorr
3528                  f(kco,j,i) = logc_ratio(1,kcorr,k,j) * f(k1,j,i)
3529               ENDDO
3530            ENDIF
3531         ENDDO
3532      ENDIF
3533
3534!
3535!--   In case of non-flat topography, also vertical walls and corners need to be
3536!--   treated. Only single and double wall nodes are corrected. Triple and
3537!--   higher-multiple wall nodes are not corrected as the log law would not be
3538!--   valid anyway in such locations.
3539      IF ( topography /= 'flat' )  THEN
3540         IF ( var == 'u' .OR. var == 'w' )  THEN                 
3541
3542!
3543!--         Solid surface only on south/north side of the node                   
3544            DO  j = nys, nyn
3545               DO  k = kb(j,i)+1, nzt_topo_nestbc
3546                  IF ( ( logc(2,k,j) /= 0 )  .AND.  ( logc(1,k,j) == 0 ) )  THEN
3547
3548!
3549!--                  Direction of the wall-normal index is carried in as the
3550!--                  sign of logc
3551                     jinc = SIGN( 1, logc(2,k,j) )
3552                     j1   = ABS( logc(2,k,j) )
3553                     DO  jcorr = 0, ncorr-1
3554                        jco = j + jinc * jcorr
3555                        f(k,jco,i) = logc_ratio(2,jcorr,k,j) * f(k,j1,i)
3556                     ENDDO
3557                  ENDIF
3558               ENDDO
3559            ENDDO
3560         ENDIF
3561
3562!
3563!--      Solid surface on both below and on south/north side of the node           
3564         IF ( var == 'u' )  THEN
3565            DO  j = nys, nyn
3566               k = kb(j,i) + 1
3567               IF ( ( logc(2,k,j) /= 0 )  .AND.  ( logc(1,k,j) /= 0 ) )  THEN
3568                  k1   = logc(1,k,j)                 
3569                  jinc = SIGN( 1, logc(2,k,j) )
3570                  j1   = ABS( logc(2,k,j) )                 
3571                  DO  jcorr = 0, ncorr-1
3572                     jco = j + jinc * jcorr
3573                     DO  kcorr = 0, ncorr-1
3574                        kco = k + kcorr
3575                        f(kco,jco,i) = 0.5_wp * ( logc_ratio(1,kcorr,k,j) *    &
3576                                                  f(k1,j,i)                    &
3577                                                + logc_ratio(2,jcorr,k,j) *    &
3578                                                  f(k,j1,i) )
3579                     ENDDO
3580                  ENDDO
3581               ENDIF
3582            ENDDO
3583         ENDIF
3584
3585      ENDIF  ! ( topography /= 'flat' )
3586
3587!
3588!--   Rescale if f is the TKE.
3589      IF ( var == 'e')  THEN
3590         IF ( edge == 'l' )  THEN
3591            DO  j = nys, nyn + 1
3592               DO  k = kb(j,i), nzt + 1
3593                  f(k,j,i) = tkefactor_l(k,j) * f(k,j,i)
3594               ENDDO
3595            ENDDO
3596         ELSEIF ( edge == 'r' )  THEN           
3597            DO  j = nys, nyn+1
3598               DO  k = kb(j,i), nzt+1
3599                  f(k,j,i) = tkefactor_r(k,j) * f(k,j,i)
3600               ENDDO
3601            ENDDO
3602         ENDIF
3603      ENDIF
3604
3605!
3606!--   Store the boundary values also into the other redundant ghost node layers
3607      IF ( edge == 'l' )  THEN
3608         DO  ibgp = -nbgp, ib
3609            f(0:nzt+1,nysg:nyng,ibgp) = f(0:nzt+1,nysg:nyng,i)
3610         ENDDO
3611      ELSEIF ( edge == 'r' )  THEN
3612         DO  ibgp = ib, nx+nbgp
3613            f(0:nzt+1,nysg:nyng,ibgp) = f(0:nzt+1,nysg:nyng,i)
3614         ENDDO
3615      ENDIF
3616
3617   END SUBROUTINE pmci_interp_tril_lr
3618
3619
3620
3621   SUBROUTINE pmci_interp_tril_sn( f, fc, ic, jc, kc, r1x, r2x, r1y, r2y, r1z, &
3622                                   r2z, kb, logc, logc_ratio,                  &
3623                                   nzt_topo_nestbc, edge, var )
3624
3625!
3626!--   Interpolation of ghost-node values used as the client-domain boundary
3627!--   conditions. This subroutine handles the south and north boundaries.
3628!--   This subroutine is based on trilinear interpolation.
3629
3630      IMPLICIT NONE
3631
3632      REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                      &
3633                                      INTENT(INOUT) ::  f             !:
3634      REAL(wp), DIMENSION(0:cg%nz+1,jcs:jcn,icl:icr),                          &
3635                                      INTENT(IN)    ::  fc            !:
3636      REAL(wp), DIMENSION(1:2,0:ncorr-1,nzb:nzt_topo_nestbc,nxl:nxr),          &
3637                                      INTENT(IN)    ::  logc_ratio    !:
3638      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r1x           !:
3639      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r2x           !:
3640      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r1y           !:
3641      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r2y           !:
3642      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r1z           !:
3643      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r2z           !:
3644     
3645      INTEGER(iwp), DIMENSION(nxlg:nxrg), INTENT(IN)           ::  ic    !:
3646      INTEGER(iwp), DIMENSION(nysg:nyng), INTENT(IN)           ::  jc    !:
3647      INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg), INTENT(IN) ::  kb    !:
3648      INTEGER(iwp), DIMENSION(nzb:nzt+1), INTENT(IN)           ::  kc    !:
3649      INTEGER(iwp), DIMENSION(1:2,nzb:nzt_topo_nestbc,nxl:nxr),                &
3650                                          INTENT(IN)           ::  logc  !:
3651      INTEGER(iwp) ::  nzt_topo_nestbc   !:
3652
3653      CHARACTER(LEN=1), INTENT(IN) ::  edge   !:
3654      CHARACTER(LEN=1), INTENT(IN) ::  var    !:
3655     
3656      INTEGER(iwp) ::  i       !:
3657      INTEGER(iwp) ::  iinc    !:
3658      INTEGER(iwp) ::  icorr   !:
3659      INTEGER(iwp) ::  ico     !:
3660      INTEGER(iwp) ::  i1      !:
3661      INTEGER(iwp) ::  j       !:
3662      INTEGER(iwp) ::  jb      !:
3663      INTEGER(iwp) ::  jbgp    !:
3664      INTEGER(iwp) ::  k       !:
3665      INTEGER(iwp) ::  kcorr   !:
3666      INTEGER(iwp) ::  kco     !:
3667      INTEGER(iwp) ::  k1      !:
3668      INTEGER(iwp) ::  l       !:
3669      INTEGER(iwp) ::  m       !:
3670      INTEGER(iwp) ::  n       !:
3671                           
3672      REAL(wp) ::  coarse_dx   !:
3673      REAL(wp) ::  coarse_dy   !:
3674      REAL(wp) ::  coarse_dz   !:
3675      REAL(wp) ::  fk          !:
3676      REAL(wp) ::  fkj         !:
3677      REAL(wp) ::  fkjp        !:
3678      REAL(wp) ::  fkpj        !:
3679      REAL(wp) ::  fkpjp       !:
3680      REAL(wp) ::  fkp         !:
3681     
3682!
3683!--   Check which edge is to be handled: south or north
3684      IF ( edge == 's' )  THEN
3685!
3686!--      For v, nys is a ghost node, but not for the other variables
3687         IF ( var == 'v' )  THEN
3688            j  = nys
3689            jb = nys - 1 
3690         ELSE
3691            j  = nys - 1
3692            jb = nys - 2
3693         ENDIF
3694      ELSEIF ( edge == 'n' )  THEN
3695         j  = nyn + 1
3696         jb = nyn + 2
3697      ENDIF
3698
3699      DO  i = nxl, nxr+1
3700         DO  k = kb(j,i), nzt+1
3701            l = ic(i)
3702            m = jc(j)
3703            n = kc(k)             
3704            fkj      = r1x(i) * fc(n,m,l)     + r2x(i) * fc(n,m,l+1)
3705            fkjp     = r1x(i) * fc(n,m+1,l)   + r2x(i) * fc(n,m+1,l+1)
3706            fkpj     = r1x(i) * fc(n+1,m,l)   + r2x(i) * fc(n+1,m,l+1)
3707            fkpjp    = r1x(i) * fc(n+1,m+1,l) + r2x(i) * fc(n+1,m+1,l+1)
3708            fk       = r1y(j) * fkj  + r2y(j) * fkjp
3709            fkp      = r1y(j) * fkpj + r2y(j) * fkpjp
3710            f(k,j,i) = r1z(k) * fk   + r2z(k) * fkp
3711         ENDDO
3712      ENDDO
3713
3714!
3715!--   Generalized log-law-correction algorithm.
3716!--   Multiply two-dimensional index arrays logc(1:2,:,:) and log-ratio arrays
3717!--   logc_ratio(1:2,0:ncorr-1,:,:) have been precomputed in subroutine
3718!--   pmci_init_loglaw_correction.
3719!
3720!--   Solid surface below the node
3721      IF ( var == 'u'  .OR.  var == 'v' )  THEN           
3722         DO  i = nxl, nxr
3723            k = kb(j,i) + 1
3724            IF ( ( logc(1,k,i) /= 0 )  .AND.  ( logc(2,k,i) == 0 ) )  THEN
3725               k1 = logc(1,k,i)
3726               DO  kcorr = 0, ncorr-1
3727                  kco = k + kcorr
3728                  f(kco,j,i) = logc_ratio(1,kcorr,k,i) * f(k1,j,i)
3729               ENDDO
3730            ENDIF
3731         ENDDO
3732      ENDIF
3733
3734!
3735!--   In case of non-flat topography, also vertical walls and corners need to be
3736!--   treated. Only single and double wall nodes are corrected.
3737!--   Triple and higher-multiple wall nodes are not corrected as it would be
3738!--   extremely complicated and the log law would not be valid anyway in such
3739!--   locations.
3740      IF ( topography /= 'flat' )  THEN
3741         IF ( var == 'v' .OR. var == 'w' )  THEN
3742            DO  i = nxl, nxr
3743               DO  k = kb(j,i), nzt_topo_nestbc
3744
3745!
3746!--               Solid surface only on left/right side of the node           
3747                  IF ( ( logc(2,k,i) /= 0 )  .AND.  ( logc(1,k,i) == 0 ) )  THEN
3748
3749!
3750!--                  Direction of the wall-normal index is carried in as the
3751!--                  sign of logc
3752                     iinc = SIGN( 1, logc(2,k,i) )
3753                     i1  = ABS( logc(2,k,i) )
3754                     DO  icorr = 0, ncorr-1
3755                        ico = i + iinc * icorr
3756                        f(k,j,ico) = logc_ratio(2,icorr,k,i) * f(k,j,i1)
3757                     ENDDO
3758                  ENDIF
3759               ENDDO
3760            ENDDO
3761         ENDIF
3762
3763!
3764!--      Solid surface on both below and on left/right side of the node           
3765         IF ( var == 'v' )  THEN
3766            DO  i = nxl, nxr
3767               k = kb(j,i) + 1
3768               IF ( ( logc(2,k,i) /= 0 )  .AND.  ( logc(1,k,i) /= 0 ) )  THEN
3769                  k1   = logc(1,k,i)         
3770                  iinc = SIGN( 1, logc(2,k,i) )
3771                  i1   = ABS( logc(2,k,i) )
3772                  DO  icorr = 0, ncorr-1
3773                     ico = i + iinc * icorr
3774                     DO  kcorr = 0, ncorr-1
3775                        kco = k + kcorr
3776                        f(kco,i,ico) = 0.5_wp * ( logc_ratio(1,kcorr,k,i) *    &
3777                                                  f(k1,j,i)  &
3778                                                + logc_ratio(2,icorr,k,i) *    &
3779                                                  f(k,j,i1) )
3780                     ENDDO
3781                  ENDDO
3782               ENDIF
3783            ENDDO
3784         ENDIF
3785         
3786      ENDIF  ! ( topography /= 'flat' )
3787
3788!
3789!--   Rescale if f is the TKE.
3790      IF ( var == 'e')  THEN
3791         IF ( edge == 's' )  THEN
3792            DO  i = nxl, nxr + 1
3793               DO  k = kb(j,i), nzt+1
3794                  f(k,j,i) = tkefactor_s(k,i) * f(k,j,i)
3795               ENDDO
3796            ENDDO
3797         ELSEIF ( edge == 'n' )  THEN
3798            DO  i = nxl, nxr + 1
3799               DO  k = kb(j,i), nzt+1
3800                  f(k,j,i) = tkefactor_n(k,i) * f(k,j,i)
3801               ENDDO
3802            ENDDO
3803         ENDIF
3804      ENDIF
3805
3806!
3807!--   Store the boundary values also into the other redundant ghost node layers
3808      IF ( edge == 's' )  THEN
3809         DO  jbgp = -nbgp, jb
3810            f(0:nzt+1,jbgp,nxlg:nxrg) = f(0:nzt+1,j,nxlg:nxrg)
3811         ENDDO
3812      ELSEIF ( edge == 'n' )  THEN
3813         DO  jbgp = jb, ny+nbgp
3814            f(0:nzt+1,jbgp,nxlg:nxrg) = f(0:nzt+1,j,nxlg:nxrg)
3815         ENDDO
3816      ENDIF
3817
3818   END SUBROUTINE pmci_interp_tril_sn
3819
3820 
3821
3822   SUBROUTINE pmci_interp_tril_t( f, fc, ic, jc, kc, r1x, r2x, r1y, r2y, r1z,  &
3823                                  r2z, var )
3824
3825!
3826!--   Interpolation of ghost-node values used as the client-domain boundary
3827!--   conditions. This subroutine handles the top boundary.
3828!--   This subroutine is based on trilinear interpolation.
3829
3830      IMPLICIT NONE
3831
3832      REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                      &
3833                                      INTENT(INOUT) ::  f     !:
3834      REAL(wp), DIMENSION(0:cg%nz+1,jcs:jcn,icl:icr),                          &
3835                                      INTENT(IN)    ::  fc    !:
3836      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r1x   !:
3837      REAL(wp), DIMENSION(nxlg:nxrg), INTENT(IN)    ::  r2x   !:
3838      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r1y   !:
3839      REAL(wp), DIMENSION(nysg:nyng), INTENT(IN)    ::  r2y   !:
3840      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r1z   !:
3841      REAL(wp), DIMENSION(nzb:nzt+1), INTENT(IN)    ::  r2z   !:
3842     
3843      INTEGER(iwp), DIMENSION(nxlg:nxrg), INTENT(IN) ::  ic    !:
3844      INTEGER(iwp), DIMENSION(nysg:nyng), INTENT(IN) ::  jc    !:
3845      INTEGER(iwp), DIMENSION(nzb:nzt+1), INTENT(IN) ::  kc    !:
3846     
3847      CHARACTER(LEN=1), INTENT(IN) :: var   !:
3848
3849      INTEGER(iwp) ::  i   !:
3850      INTEGER(iwp) ::  j   !:
3851      INTEGER(iwp) ::  k   !:
3852      INTEGER(iwp) ::  l   !:
3853      INTEGER(iwp) ::  m   !:
3854      INTEGER(iwp) ::  n   !:
3855     
3856      REAL(wp) ::  coarse_dx   !:
3857      REAL(wp) ::  coarse_dy   !:
3858      REAL(wp) ::  coarse_dz   !:
3859      REAL(wp) ::  fk          !:
3860      REAL(wp) ::  fkj         !:
3861      REAL(wp) ::  fkjp        !:
3862      REAL(wp) ::  fkpj        !:
3863      REAL(wp) ::  fkpjp       !:
3864      REAL(wp) ::  fkp         !:
3865
3866     
3867      IF ( var == 'w' )  THEN
3868         k  = nzt
3869      ELSE
3870         k  = nzt + 1
3871      ENDIF
3872     
3873      DO  i = nxl-1, nxr+1
3874         DO  j = nys-1, nyn+1
3875            l = ic(i)
3876            m = jc(j)
3877            n = kc(k)             
3878            fkj      = r1x(i) * fc(n,m,l)     + r2x(i) * fc(n,m,l+1)
3879            fkjp     = r1x(i) * fc(n,m+1,l)   + r2x(i) * fc(n,m+1,l+1)
3880            fkpj     = r1x(i) * fc(n+1,m,l)   + r2x(i) * fc(n+1,m,l+1)
3881            fkpjp    = r1x(i) * fc(n+1,m+1,l) + r2x(i) * fc(n+1,m+1,l+1)
3882            fk       = r1y(j) * fkj  + r2y(j) * fkjp
3883            fkp      = r1y(j) * fkpj + r2y(j) * fkpjp
3884            f(k,j,i) = r1z(k) * fk   + r2z(k) * fkp
3885         ENDDO
3886      ENDDO
3887
3888!
3889!--   Just fill up the second ghost-node layer for w.
3890      IF ( var == 'w' )  THEN
3891         f(nzt+1,:,:) = f(nzt,:,:)
3892      ENDIF
3893
3894!
3895!--   Rescale if f is the TKE.
3896!--   It is assumed that the bottom surface never reaches the top boundary of a
3897!--   nest domain.
3898      IF ( var == 'e' )  THEN
3899         DO  i = nxl, nxr
3900            DO  j = nys, nyn
3901               f(k,j,i) = tkefactor_t(j,i) * f(k,j,i)
3902            ENDDO
3903         ENDDO
3904      ENDIF
3905
3906   END SUBROUTINE pmci_interp_tril_t
3907
3908
3909
3910    SUBROUTINE pmci_extrap_ifoutflow_lr( f, kb, edge, var )
3911!
3912!--    After the interpolation of ghost-node values for the client-domain
3913!--    boundary conditions, this subroutine checks if there is a local outflow
3914!--    through the boundary. In that case this subroutine overwrites the
3915!--    interpolated values by values extrapolated from the domain. This
3916!--    subroutine handles the left and right boundaries. However, this operation
3917!--    is only needed in case of one-way coupling.
3918
3919       IMPLICIT NONE
3920
3921       CHARACTER(LEN=1),INTENT(IN) ::  edge   !:
3922       CHARACTER(LEN=1),INTENT(IN) ::  var    !:
3923
3924       INTEGER(iwp) ::  i     !:
3925       INTEGER(iwp) ::  ib    !:
3926       INTEGER(iwp) ::  ibgp  !:
3927       INTEGER(iwp) ::  ied   !:
3928       INTEGER(iwp) ::  j     !:
3929       INTEGER(iwp) ::  k     !:
3930     
3931       INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg), INTENT(IN) ::  kb   !:
3932
3933       REAL(wp) ::  outnor    !:
3934       REAL(wp) ::  vdotnor   !:
3935
3936       REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(INOUT) :: f !:
3937
3938!
3939!--    Check which edge is to be handled: left or right
3940       IF ( edge == 'l' )  THEN
3941          IF ( var == 'u' )  THEN
3942             i   = nxl
3943             ib  = nxl - 1
3944             ied = nxl + 1
3945          ELSE
3946             i   = nxl - 1
3947             ib  = nxl - 2
3948             ied = nxl
3949          ENDIF
3950          outnor = -1.0_wp
3951       ELSEIF ( edge == 'r' )  THEN
3952          i      = nxr + 1
3953          ib     = nxr + 2
3954          ied    = nxr
3955          outnor = 1.0_wp
3956       ENDIF
3957
3958       DO  j = nys, nyn+1
3959          DO  k = kb(j,i), nzt+1
3960             vdotnor = outnor * u(k,j,ied)
3961!
3962!--          Local outflow
3963             IF ( vdotnor > 0.0_wp )  THEN
3964                f(k,j,i) = f(k,j,ied)
3965             ENDIF
3966          ENDDO
3967          IF ( (var == 'u' )  .OR.  (var == 'v' )  .OR.  (var == 'w') )  THEN
3968             f(kb(j,i),j,i) = 0.0_wp
3969          ENDIF
3970       ENDDO
3971
3972!
3973!--    Store the boundary values also into the redundant ghost node layers.
3974       IF ( edge == 'l' )  THEN
3975          DO ibgp = -nbgp, ib
3976             f(0:nzt+1,nysg:nyng,ibgp) = f(0:nzt+1,nysg:nyng,i)
3977          ENDDO
3978       ELSEIF ( edge == 'r' )  THEN
3979          DO ibgp = ib, nx+nbgp
3980             f(0:nzt+1,nysg:nyng,ibgp) = f(0:nzt+1,nysg:nyng,i)
3981          ENDDO
3982       ENDIF
3983
3984    END SUBROUTINE pmci_extrap_ifoutflow_lr
3985
3986
3987
3988    SUBROUTINE pmci_extrap_ifoutflow_sn( f, kb, edge, var )
3989!
3990!--    After  the interpolation of ghost-node values for the client-domain
3991!--    boundary conditions, this subroutine checks if there is a local outflow
3992!--    through the boundary. In that case this subroutine overwrites the
3993!--    interpolated values by values extrapolated from the domain. This
3994!--    subroutine handles the south and north boundaries.
3995
3996       IMPLICIT NONE
3997
3998       CHARACTER(LEN=1), INTENT(IN) ::  edge   !:
3999       CHARACTER(LEN=1), INTENT(IN) ::  var    !:
4000     
4001       INTEGER(iwp) ::  i         !:
4002       INTEGER(iwp) ::  j         !:
4003       INTEGER(iwp) ::  jb        !:
4004       INTEGER(iwp) ::  jbgp      !:
4005       INTEGER(iwp) ::  jed       !:
4006       INTEGER(iwp) ::  k         !:
4007
4008       INTEGER(iwp), DIMENSION(nysg:nyng,nxlg:nxrg), INTENT(IN) ::  kb   !:
4009
4010       REAL(wp)     ::  outnor    !:
4011       REAL(wp)     ::  vdotnor   !:
4012
4013       REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(INOUT) :: f !:
4014
4015!
4016!--    Check which edge is to be handled: left or right
4017       IF ( edge == 's' )  THEN
4018          IF ( var == 'v' )  THEN
4019             j   = nys
4020             jb  = nys - 1
4021             jed = nys + 1
4022          ELSE
4023             j   = nys - 1
4024             jb  = nys - 2
4025             jed = nys
4026          ENDIF
4027          outnor = -1.0_wp
4028       ELSEIF ( edge == 'n' )  THEN
4029          j      = nyn + 1
4030          jb     = nyn + 2
4031          jed    = nyn
4032          outnor = 1.0_wp
4033       ENDIF
4034
4035       DO  i = nxl, nxr+1
4036          DO  k = kb(j,i), nzt+1
4037             vdotnor = outnor * v(k,jed,i)
4038!
4039!--          Local outflow
4040             IF ( vdotnor > 0.0_wp )  THEN
4041                f(k,j,i) = f(k,jed,i)
4042             ENDIF
4043          ENDDO
4044          IF ( (var == 'u' )  .OR.  (var == 'v' )  .OR.  (var == 'w') )  THEN
4045             f(kb(j,i),j,i) = 0.0_wp
4046          ENDIF
4047       ENDDO
4048
4049!
4050!--    Store the boundary values also into the redundant ghost node layers.
4051       IF ( edge == 's' )  THEN
4052          DO  jbgp = -nbgp, jb
4053             f(0:nzt+1,jbgp,nxlg:nxrg) = f(0:nzt+1,j,nxlg:nxrg)
4054          ENDDO
4055       ELSEIF ( edge == 'n' )  THEN
4056          DO  jbgp = jb, ny+nbgp
4057             f(0:nzt+1,jbgp,nxlg:nxrg) = f(0:nzt+1,j,nxlg:nxrg)
4058          ENDDO
4059       ENDIF
4060
4061    END SUBROUTINE pmci_extrap_ifoutflow_sn
4062
4063 
4064
4065    SUBROUTINE pmci_extrap_ifoutflow_t( f, var )
4066!
4067!--    Interpolation of ghost-node values used as the client-domain boundary
4068!--    conditions. This subroutine handles the top boundary. It is based on
4069!--    trilinear interpolation.
4070
4071       IMPLICIT NONE
4072
4073       CHARACTER(LEN=1), INTENT(IN) ::  var   !:
4074     
4075       INTEGER(iwp) ::  i     !:
4076       INTEGER(iwp) ::  j     !:
4077       INTEGER(iwp) ::  k     !:
4078       INTEGER(iwp) ::  ked   !:
4079
4080       REAL(wp) ::  vdotnor   !:
4081
4082       REAL(wp), DIMENSION(nzb:nzt+1,nys-nbgp:nyn+nbgp,nxl-nbgp:nxr+nbgp),     &
4083                 INTENT(INOUT) ::  f   !:
4084     
4085
4086       IF ( var == 'w' )  THEN
4087          k    = nzt
4088          ked  = nzt - 1
4089       ELSE
4090          k    = nzt + 1
4091          ked  = nzt
4092       ENDIF
4093
4094       DO  i = nxl, nxr
4095          DO  j = nys, nyn
4096             vdotnor = w(ked,j,i)
4097!
4098!--          Local outflow
4099             IF ( vdotnor > 0.0_wp )  THEN
4100                f(k,j,i) = f(ked,j,i)
4101             ENDIF
4102          ENDDO
4103       ENDDO
4104
4105!
4106!--    Just fill up the second ghost-node layer for w
4107       IF ( var == 'w' )  THEN
4108          f(nzt+1,:,:) = f(nzt,:,:)
4109       ENDIF
4110
4111    END SUBROUTINE pmci_extrap_ifoutflow_t
4112
4113
4114
4115    SUBROUTINE pmci_anterp_tophat( f, fc, kct, ifl, ifu, jfl, jfu, kfl, kfu,   &
4116                                   var )
4117!
4118!--    Anterpolation of internal-node values to be used as the server-domain
4119!--    values. This subroutine is based on the first-order numerical
4120!--    integration of the fine-grid values contained within the coarse-grid
4121!--    cell.
4122
4123       IMPLICIT NONE
4124
4125       CHARACTER(LEN=1), INTENT(IN) ::  var   !:
4126
4127       INTEGER(iwp) ::  i         !: Fine-grid index
4128       INTEGER(iwp) ::  ii        !: Coarse-grid index
4129       INTEGER(iwp) ::  iclp      !:
4130       INTEGER(iwp) ::  icrm      !:
4131       INTEGER(iwp) ::  ifc       !:
4132       INTEGER(iwp) ::  ijfc      !:
4133       INTEGER(iwp) ::  j         !: Fine-grid index
4134       INTEGER(iwp) ::  jj        !: Coarse-grid index
4135       INTEGER(iwp) ::  jcnm      !:
4136       INTEGER(iwp) ::  jcsp      !:
4137       INTEGER(iwp) ::  k         !: Fine-grid index
4138       INTEGER(iwp) ::  kk        !: Coarse-grid index
4139       INTEGER(iwp) ::  kcb       !:
4140       INTEGER(iwp) ::  nfc       !:
4141
4142       INTEGER(iwp), INTENT(IN) ::  kct   !:
4143
4144       INTEGER(iwp), DIMENSION(icl:icr), INTENT(IN) ::  ifl   !:
4145       INTEGER(iwp), DIMENSION(icl:icr), INTENT(IN) ::  ifu   !:
4146       INTEGER(iwp), DIMENSION(jcs:jcn), INTENT(IN) ::  jfl   !:
4147       INTEGER(iwp), DIMENSION(jcs:jcn), INTENT(IN) ::  jfu   !:
4148       INTEGER(iwp), DIMENSION(0:kct), INTENT(IN)   ::  kfl   !:
4149       INTEGER(iwp), DIMENSION(0:kct), INTENT(IN)   ::  kfu   !:
4150
4151
4152       REAL(wp) ::  cellsum   !:
4153       REAL(wp) ::  f1f       !:
4154       REAL(wp) ::  fra       !:
4155
4156       REAL(wp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(IN) ::  f   !:
4157       REAL(wp), DIMENSION(0:cg%nz+1,jcs:jcn,icl:icr), INTENT(INOUT)  ::  fc  !:
4158 
4159
4160!
4161!--    Initialize the index bounds for anterpolation
4162       iclp = icl 
4163       icrm = icr 
4164       jcsp = jcs 
4165       jcnm = jcn 
4166
4167!
4168!--    Define the index bounds iclp, icrm, jcsp and jcnm.
4169!--    Note that kcb is simply zero and kct enters here as a parameter and it is
4170!--    determined in pmci_init_anterp_tophat
4171       IF ( nest_bound_l )  THEN
4172          IF ( var == 'u' )  THEN
4173             iclp = icl + nhll + 1
4174          ELSE
4175             iclp = icl + nhll
4176          ENDIF
4177       ENDIF
4178       IF ( nest_bound_r )  THEN
4179          icrm = icr - nhlr
4180       ENDIF
4181
4182       IF ( nest_bound_s )  THEN
4183          IF ( var == 'v' )  THEN
4184             jcsp = jcs + nhls + 1
4185          ELSE
4186             jcsp = jcs + nhls
4187          ENDIF
4188       ENDIF
4189       IF ( nest_bound_n )  THEN
4190          jcnm = jcn - nhln
4191       ENDIF
4192       kcb = 0
4193
4194!
4195!--    Note that l,m, and n are coarse-grid indices and i,j, and k are fine-grid
4196!--    indices.
4197       DO  ii = iclp, icrm
4198          ifc = ifu(ii) - ifl(ii) + 1
4199          DO  jj = jcsp, jcnm
4200             ijfc = ifc * ( jfu(jj) - jfl(jj) + 1 )
4201!
4202!--          For simplicity anterpolate within buildings too
4203             DO  kk = kcb, kct
4204                nfc =  ijfc * ( kfu(kk) - kfl(kk) + 1 )
4205                cellsum = 0.0_wp
4206                DO  i = ifl(ii), ifu(ii)
4207                   DO  j = jfl(jj), jfu(jj)
4208                      DO  k = kfl(kk), kfu(kk)
4209                         cellsum = cellsum + f(k,j,i)
4210                      ENDDO
4211                   ENDDO
4212                ENDDO
4213!
4214!--             Spatial under-relaxation.
4215                fra  = frax(ii) * fray(jj) * fraz(kk)
4216!
4217!--             Block out the fine-grid corner patches from the anterpolation
4218                IF ( ( ifl(ii) < nxl ) .OR. ( ifu(ii) > nxr ) )  THEN
4219                   IF ( ( jfl(jj) < nys ) .OR. ( jfu(jj) > nyn ) )  THEN
4220                      fra = 0.0_wp
4221                   ENDIF
4222                ENDIF
4223!
4224!--             TO DO: introduce 3-d coarse grid array for precomputed
4225!--             1/REAL(nfc) values
4226                fc(kk,jj,ii) = ( 1.0_wp - fra ) * fc(kk,jj,ii) +               &
4227                               fra * cellsum / REAL( nfc, KIND = wp )
4228
4229             ENDDO
4230          ENDDO
4231       ENDDO
4232
4233    END SUBROUTINE pmci_anterp_tophat
4234
4235#endif
4236 END SUBROUTINE pmci_client_datatrans
4237
4238END MODULE pmc_interface
Note: See TracBrowser for help on using the repository browser.