source: palm/trunk/SOURCE/data_output_mask.f90 @ 1321

Last change on this file since 1321 was 1321, checked in by raasch, 10 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 19.6 KB
Line 
1 SUBROUTINE data_output_mask( av )
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!
23!
24! Former revisions:
25! -----------------
26! $Id: data_output_mask.f90 1321 2014-03-20 09:40:40Z raasch $
27!
28! 1320 2014-03-20 08:40:49Z raasch
29! ONLY-attribute added to USE-statements,
30! kind-parameters added to all INTEGER and REAL declaration statements,
31! kinds are defined in new module kinds,
32! revision history before 2012 removed,
33! comment fields (!:) to be used for variable explanations added to
34! all variable declaration statements
35!
36! 1318 2014-03-17 13:35:16Z raasch
37! barrier argument removed from cpu_log,
38! module interfaces removed
39!
40! 1092 2013-02-02 11:24:22Z raasch
41! unused variables removed
42!
43! 1036 2012-10-22 13:43:42Z raasch
44! code put under GPL (PALM 3.9)
45!
46! 1031 2012-10-19 14:35:30Z raasch
47! netCDF4 without parallel file support implemented
48!
49! 1007 2012-09-19 14:30:36Z franke
50! Bugfix: calculation of pr must depend on the particle weighting factor,
51! missing calculation of ql_vp added
52!
53! 410 2009-12-04 17:05:40Z letzel
54! Initial version
55!
56! Description:
57! ------------
58! Masked data output in netCDF format for current mask (current value of mid).
59!------------------------------------------------------------------------------!
60
61#if defined( __netcdf )
62    USE arrays_3d,                                                             &
63        ONLY:  e, p, pt, q, ql, ql_c, ql_v, rho, sa, tend, u, v, vpt, w
64   
65    USE averaging,                                                             &
66        ONLY:  e_av, lpt_av, p_av, pc_av, pr_av, pt_av, q_av, ql_av, ql_c_av,  &
67               ql_v_av, ql_vp_av, qv_av, rho_av, s_av, sa_av, u_av, v_av,      &
68               vpt_av, w_av 
69   
70    USE cloud_parameters,                                                      &
71        ONLY:  l_d_cp, pt_d_t
72   
73    USE control_parameters,                                                    &
74        ONLY:  cloud_physics, domask, domask_no, domask_time_count, mask_i,    &
75               mask_j, mask_k, mask_size, mask_size_l, mask_start_l,           &
76               max_masks, message_string, mid, netcdf_data_format,             &
77               netcdf_output, nz_do3d, simulated_time
78   
79    USE cpulog,                                                                &
80        ONLY:  cpu_log, log_point
81   
82    USE indices,                                                               &
83        ONLY:  nbgp, nxl, nxr, nyn, nys, nzb, nzt
84       
85    USE kinds
86   
87    USE netcdf
88   
89    USE netcdf_control
90   
91    USE particle_attributes,                                                   &
92        ONLY:  particles, prt_count, prt_start_index
93   
94    USE pegrid
95
96    IMPLICIT NONE
97
98    INTEGER(iwp) ::  av       !:
99    INTEGER(iwp) ::  ngp      !:
100    INTEGER(iwp) ::  i        !:
101    INTEGER(iwp) ::  if       !:
102    INTEGER(iwp) ::  j        !:
103    INTEGER(iwp) ::  k        !:
104    INTEGER(iwp) ::  n        !:
105    INTEGER(iwp) ::  psi      !:
106    INTEGER(iwp) ::  sender   !:
107    INTEGER(iwp) ::  ind(6)   !:
108   
109    LOGICAL ::  found         !:
110    LOGICAL ::  resorted      !:
111   
112    REAL(wp) ::  mean_r       !:
113    REAL(wp) ::  s_r3         !:
114    REAL(wp) ::  s_r4         !:
115   
116    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  local_pf    !:
117#if defined( __parallel )
118    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  total_pf    !:
119#endif
120    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !:
121
122!
123!-- Return, if nothing to output
124    IF ( domask_no(mid,av) == 0 )  RETURN
125
126    CALL cpu_log (log_point(49),'data_output_mask','start')
127
128!
129!-- Open output file.
130    IF ( netcdf_output  .AND.  ( myid == 0  .OR.  netcdf_data_format > 4 ) ) &
131    THEN
132       CALL check_open( 200+mid+av*max_masks )
133    ENDIF 
134
135!
136!-- Allocate total and local output arrays.
137#if defined( __parallel )
138    IF ( myid == 0 )  THEN
139       ALLOCATE( total_pf(mask_size(mid,1),mask_size(mid,2),mask_size(mid,3)) )
140    ENDIF
141#endif
142    ALLOCATE( local_pf(mask_size_l(mid,1),mask_size_l(mid,2), &
143                       mask_size_l(mid,3)) )
144
145!
146!-- Update the netCDF time axis.
147    domask_time_count(mid,av) = domask_time_count(mid,av) + 1
148    IF ( netcdf_output  .AND.  ( myid == 0  .OR.  netcdf_data_format > 4 ) ) &
149    THEN
150       nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_time_mask(mid,av), &
151                               (/ simulated_time /),                          &
152                               start = (/ domask_time_count(mid,av) /),       &
153                               count = (/ 1 /) )
154       CALL handle_netcdf_error( 'data_output_mask', 460 )
155    ENDIF
156
157!
158!-- Loop over all variables to be written.
159    if = 1
160
161    DO  WHILE ( domask(mid,av,if)(1:1) /= ' ' )
162!
163!--    Reallocate local_pf on PE 0 since its shape changes during MPI exchange
164       IF ( netcdf_data_format < 5   .AND.  myid == 0  .AND.  if > 1 )  THEN
165          DEALLOCATE( local_pf )
166          ALLOCATE( local_pf(mask_size_l(mid,1),mask_size_l(mid,2), &
167                             mask_size_l(mid,3)) )
168       ENDIF
169!
170!--    Store the variable chosen.
171       resorted = .FALSE.
172       SELECT CASE ( TRIM( domask(mid,av,if) ) )
173
174          CASE ( 'e' )
175             IF ( av == 0 )  THEN
176                to_be_resorted => e
177             ELSE
178                to_be_resorted => e_av
179             ENDIF
180
181          CASE ( 'lpt' )
182             IF ( av == 0 )  THEN
183                to_be_resorted => pt
184             ELSE
185                to_be_resorted => lpt_av
186             ENDIF
187
188          CASE ( 'p' )
189             IF ( av == 0 )  THEN
190                to_be_resorted => p
191             ELSE
192                to_be_resorted => p_av
193             ENDIF
194
195          CASE ( 'pc' )  ! particle concentration (requires ghostpoint exchange)
196             IF ( av == 0 )  THEN
197                tend = prt_count
198                CALL exchange_horiz( tend, nbgp )
199                DO  i = 1, mask_size_l(mid,1)
200                   DO  j = 1, mask_size_l(mid,2)
201                      DO  k = 1, mask_size_l(mid,3)
202                         local_pf(i,j,k) =  tend(mask_k(mid,k), &
203                                   mask_j(mid,j),mask_i(mid,i))
204                      ENDDO
205                   ENDDO
206                ENDDO
207                resorted = .TRUE.
208             ELSE
209                CALL exchange_horiz( pc_av, nbgp )
210                to_be_resorted => pc_av
211             ENDIF
212
213          CASE ( 'pr' )  ! mean particle radius
214             IF ( av == 0 )  THEN
215                DO  i = nxl, nxr
216                   DO  j = nys, nyn
217                      DO  k = nzb, nzt+1
218                         psi = prt_start_index(k,j,i)
219                         s_r3 = 0.0
220                         s_r4 = 0.0
221                         DO  n = psi, psi+prt_count(k,j,i)-1
222                            s_r3 = s_r3 + particles(n)%radius**3 * &
223                                          particles(n)%weight_factor
224                            s_r4 = s_r4 + particles(n)%radius**4 * &
225                                          particles(n)%weight_factor
226                         ENDDO
227                         IF ( s_r3 /= 0.0 )  THEN
228                            mean_r = s_r4 / s_r3
229                         ELSE
230                            mean_r = 0.0
231                         ENDIF
232                         tend(k,j,i) = mean_r
233                      ENDDO
234                   ENDDO
235                ENDDO
236                CALL exchange_horiz( tend, nbgp )
237                DO  i = 1, mask_size_l(mid,1)
238                   DO  j = 1, mask_size_l(mid,2)
239                      DO  k = 1, mask_size_l(mid,3)
240                         local_pf(i,j,k) =  tend(mask_k(mid,k), &
241                                   mask_j(mid,j),mask_i(mid,i))
242                      ENDDO
243                   ENDDO
244                ENDDO
245                resorted = .TRUE.
246             ELSE
247                CALL exchange_horiz( pr_av, nbgp )
248                to_be_resorted => pr_av
249             ENDIF
250
251          CASE ( 'pt' )
252             IF ( av == 0 )  THEN
253                IF ( .NOT. cloud_physics ) THEN
254                   to_be_resorted => pt
255                ELSE
256                   DO  i = 1, mask_size_l(mid,1)
257                      DO  j = 1, mask_size_l(mid,2)
258                         DO  k = 1, mask_size_l(mid,3)
259                            local_pf(i,j,k) =  &
260                                 pt(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) &
261                                 + l_d_cp * pt_d_t(mask_k(mid,k)) * &
262                                   ql(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
263                         ENDDO
264                      ENDDO
265                   ENDDO
266                   resorted = .TRUE.
267                ENDIF
268             ELSE
269                to_be_resorted => pt_av
270             ENDIF
271
272          CASE ( 'q' )
273             IF ( av == 0 )  THEN
274                to_be_resorted => q
275             ELSE
276                to_be_resorted => q_av
277             ENDIF
278
279          CASE ( 'ql' )
280             IF ( av == 0 )  THEN
281                to_be_resorted => ql
282             ELSE
283                to_be_resorted => ql_av
284             ENDIF
285
286          CASE ( 'ql_c' )
287             IF ( av == 0 )  THEN
288                to_be_resorted => ql_c
289             ELSE
290                to_be_resorted => ql_c_av
291             ENDIF
292
293          CASE ( 'ql_v' )
294             IF ( av == 0 )  THEN
295                to_be_resorted => ql_v
296             ELSE
297                to_be_resorted => ql_v_av
298             ENDIF
299
300          CASE ( 'ql_vp' )
301             IF ( av == 0 )  THEN
302                DO  i = nxl, nxr
303                   DO  j = nys, nyn
304                      DO  k = nzb, nz_do3d
305                         psi = prt_start_index(k,j,i)
306                         DO  n = psi, psi+prt_count(k,j,i)-1
307                            tend(k,j,i) = tend(k,j,i) + &
308                                          particles(n)%weight_factor / &
309                                          prt_count(k,j,i)
310                         ENDDO
311                      ENDDO
312                   ENDDO
313                ENDDO
314                CALL exchange_horiz( tend, nbgp )
315                DO  i = 1, mask_size_l(mid,1)
316                   DO  j = 1, mask_size_l(mid,2)
317                      DO  k = 1, mask_size_l(mid,3)
318                         local_pf(i,j,k) =  tend(mask_k(mid,k), &
319                                   mask_j(mid,j),mask_i(mid,i))
320                      ENDDO
321                   ENDDO
322                ENDDO
323                resorted = .TRUE.
324             ELSE
325                CALL exchange_horiz( ql_vp_av, nbgp )
326                to_be_resorted => ql_vp_av
327             ENDIF
328
329          CASE ( 'qv' )
330             IF ( av == 0 )  THEN
331                DO  i = 1, mask_size_l(mid,1)
332                   DO  j = 1, mask_size_l(mid,2)
333                      DO  k = 1, mask_size_l(mid,3)
334                         local_pf(i,j,k) =  &
335                              q(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) -  &
336                              ql(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i))
337                      ENDDO
338                   ENDDO
339                ENDDO
340                resorted = .TRUE.
341             ELSE
342                to_be_resorted => qv_av
343             ENDIF
344
345          CASE ( 'rho' )
346             IF ( av == 0 )  THEN
347                to_be_resorted => rho
348             ELSE
349                to_be_resorted => rho_av
350             ENDIF
351
352          CASE ( 's' )
353             IF ( av == 0 )  THEN
354                to_be_resorted => q
355             ELSE
356                to_be_resorted => s_av
357             ENDIF
358
359          CASE ( 'sa' )
360             IF ( av == 0 )  THEN
361                to_be_resorted => sa
362             ELSE
363                to_be_resorted => sa_av
364             ENDIF
365
366          CASE ( 'u' )
367             IF ( av == 0 )  THEN
368                to_be_resorted => u
369             ELSE
370                to_be_resorted => u_av
371             ENDIF
372
373          CASE ( 'v' )
374             IF ( av == 0 )  THEN
375                to_be_resorted => v
376             ELSE
377                to_be_resorted => v_av
378             ENDIF
379
380          CASE ( 'vpt' )
381             IF ( av == 0 )  THEN
382                to_be_resorted => vpt
383             ELSE
384                to_be_resorted => vpt_av
385             ENDIF
386
387          CASE ( 'w' )
388             IF ( av == 0 )  THEN
389                to_be_resorted => w
390             ELSE
391                to_be_resorted => w_av
392             ENDIF
393
394          CASE DEFAULT
395!
396!--          User defined quantity
397             CALL user_data_output_mask(av, domask(mid,av,if), found, local_pf )
398             resorted = .TRUE.
399
400             IF ( .NOT. found )  THEN
401                WRITE ( message_string, * ) 'no output available for: ', &
402                                            TRIM( domask(mid,av,if) )
403                CALL message( 'data_output_mask', 'PA0327', 0, 0, 0, 6, 0 )
404             ENDIF
405
406       END SELECT
407
408!
409!--    Resort the array to be output, if not done above
410       IF ( .NOT. resorted )  THEN
411          DO  i = 1, mask_size_l(mid,1)
412             DO  j = 1, mask_size_l(mid,2)
413                DO  k = 1, mask_size_l(mid,3)
414                   local_pf(i,j,k) =  to_be_resorted(mask_k(mid,k), &
415                                      mask_j(mid,j),mask_i(mid,i))
416                ENDDO
417             ENDDO
418          ENDDO
419       ENDIF
420
421!
422!--    I/O block. I/O methods are implemented
423!--    (1) for parallel execution
424!--     a. with netCDF 4 parallel I/O-enabled library
425!--     b. with netCDF 3 library
426!--    (2) for serial execution.
427!--    The choice of method depends on the correct setting of preprocessor
428!--    directives __parallel and __netcdf4_parallel as well as on the parameter
429!--    netcdf_data_format.
430#if defined( __parallel )
431#if defined( __netcdf4_parallel )
432       IF ( netcdf_data_format > 4 )  THEN
433!
434!--       (1) a. Parallel I/O using netCDF 4 (not yet tested)
435          nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),  &
436               id_var_domask(mid,av,if),  &
437               local_pf,  &
438               start = (/ mask_start_l(mid,1), mask_start_l(mid,2),  &
439                          mask_start_l(mid,3), domask_time_count(mid,av) /),  &
440               count = (/ mask_size_l(mid,1), mask_size_l(mid,2),  &
441                          mask_size_l(mid,3), 1 /) )
442          CALL handle_netcdf_error( 'data_output_mask', 461 )
443       ELSE
444#endif
445!
446!--       (1) b. Conventional I/O only through PE0
447!--       PE0 receives partial arrays from all processors of the respective mask
448!--       and outputs them. Here a barrier has to be set, because otherwise
449!--       "-MPI- FATAL: Remote protocol queue full" may occur.
450          CALL MPI_BARRIER( comm2d, ierr )
451
452          ngp = mask_size_l(mid,1) * mask_size_l(mid,2) * mask_size_l(mid,3)
453          IF ( myid == 0 )  THEN
454!
455!--          Local array can be relocated directly.
456             total_pf( &
457               mask_start_l(mid,1):mask_start_l(mid,1)+mask_size_l(mid,1)-1, &
458               mask_start_l(mid,2):mask_start_l(mid,2)+mask_size_l(mid,2)-1, &
459               mask_start_l(mid,3):mask_start_l(mid,3)+mask_size_l(mid,3)-1 ) &
460               = local_pf
461!
462!--          Receive data from all other PEs.
463             DO  n = 1, numprocs-1
464!
465!--             Receive index limits first, then array.
466!--             Index limits are received in arbitrary order from the PEs.
467                CALL MPI_RECV( ind(1), 6, MPI_INTEGER, MPI_ANY_SOURCE, 0,  &
468                     comm2d, status, ierr )
469!
470!--             Not all PEs have data for the mask
471                IF ( ind(1) /= -9999 )  THEN
472                   ngp = ( ind(2)-ind(1)+1 ) * (ind(4)-ind(3)+1 ) *  &
473                         ( ind(6)-ind(5)+1 )
474                   sender = status(MPI_SOURCE)
475                   DEALLOCATE( local_pf )
476                   ALLOCATE(local_pf(ind(1):ind(2),ind(3):ind(4),ind(5):ind(6)))
477                   CALL MPI_RECV( local_pf(ind(1),ind(3),ind(5)), ngp,  &
478                        MPI_REAL, sender, 1, comm2d, status, ierr )
479                   total_pf(ind(1):ind(2),ind(3):ind(4),ind(5):ind(6)) &
480                        = local_pf
481                ENDIF
482             ENDDO
483
484             nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),  &
485                  id_var_domask(mid,av,if), total_pf, &
486                  start = (/ 1, 1, 1, domask_time_count(mid,av) /), &
487                  count = (/ mask_size(mid,1), mask_size(mid,2), &
488                             mask_size(mid,3), 1 /) )
489             CALL handle_netcdf_error( 'data_output_mask', 462 )
490
491          ELSE
492!
493!--          If at least part of the mask resides on the PE, send the index
494!--          limits for the target array, otherwise send -9999 to PE0.
495             IF ( mask_size_l(mid,1) > 0 .AND.  mask_size_l(mid,2) > 0 .AND. &
496                  mask_size_l(mid,3) > 0  ) &
497                  THEN
498                ind(1) = mask_start_l(mid,1)
499                ind(2) = mask_start_l(mid,1) + mask_size_l(mid,1) - 1
500                ind(3) = mask_start_l(mid,2)
501                ind(4) = mask_start_l(mid,2) + mask_size_l(mid,2) - 1
502                ind(5) = mask_start_l(mid,3)
503                ind(6) = mask_start_l(mid,3) + mask_size_l(mid,3) - 1
504             ELSE
505                ind(1) = -9999; ind(2) = -9999
506                ind(3) = -9999; ind(4) = -9999
507                ind(5) = -9999; ind(6) = -9999
508             ENDIF
509             CALL MPI_SEND( ind(1), 6, MPI_INTEGER, 0, 0, comm2d, ierr )
510!
511!--          If applicable, send data to PE0.
512             IF ( ind(1) /= -9999 )  THEN
513                CALL MPI_SEND( local_pf(1,1,1), ngp, MPI_REAL, 0, 1, comm2d, &
514                     ierr )
515             ENDIF
516          ENDIF
517!
518!--       A barrier has to be set, because otherwise some PEs may proceed too
519!--       fast so that PE0 may receive wrong data on tag 0.
520          CALL MPI_BARRIER( comm2d, ierr )
521#if defined( __netcdf4_parallel )
522       ENDIF
523#endif
524#else
525!
526!--    (2) For serial execution of PALM, the single processor (PE0) holds all
527!--    data and writes them directly to file.
528       nc_stat = NF90_PUT_VAR( id_set_mask(mid,av),  &
529            id_var_domask(mid,av,if),       &
530            local_pf, &
531            start = (/ 1, 1, 1, domask_time_count(mid,av) /), &
532            count = (/ mask_size_l(mid,1), mask_size_l(mid,2), &
533                       mask_size_l(mid,3), 1 /) )
534       CALL handle_netcdf_error( 'data_output_mask', 463 )
535#endif
536
537       if = if + 1
538
539    ENDDO
540
541!
542!-- Deallocate temporary arrays.
543    DEALLOCATE( local_pf )
544#if defined( __parallel )
545    IF ( myid == 0 )  THEN
546       DEALLOCATE( total_pf )
547    ENDIF
548#endif
549
550
551    CALL cpu_log( log_point(49), 'data_output_mask', 'stop' )
552#endif
553
554 END SUBROUTINE data_output_mask
Note: See TracBrowser for help on using the repository browser.