source: palm/trunk/SOURCE/pmc_interface.f90 @ 1781

Last change on this file since 1781 was 1781, checked in by raasch, 8 years ago

some pmc calls completely removed, some moved from time_integration to main program, t_..m arrays removed from pmc

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