SUBROUTINE data_output_mask( av ) !------------------------------------------------------------------------------! ! Current revisions: ! ----------------- ! ! Former revisions: ! ----------------- ! $Id: data_output_mask.f90 772 2011-10-27 11:08:46Z heinze $ ! ! 771 2011-10-27 10:56:21Z heinze ! +lpt ! ! 667 2010-12-23 12:06:00Z suehring/gryschka ! Calls of exchange_horiz are modified. ! ! 564 2010-09-30 13:18:59Z helmke ! start number of mask output files changed to 201, netcdf message identifiers ! of masked output changed, palm message identifiers of masked output changed ! ! 493 2010-03-01 08:30:24Z raasch ! netcdf_format_mask* and format_parallel_io replaced by netcdf_data_format ! ! 475 2010-02-04 02:26:16Z raasch ! Bugfix in serial branch: arguments from array local_pf removed in N90_PUT_VAR ! ! 410 2009-12-04 17:05:40Z letzel ! Initial version ! ! Description: ! ------------ ! Masked data output in NetCDF format for current mask (current value of mid). !------------------------------------------------------------------------------! #if defined( __netcdf ) USE arrays_3d USE averaging USE cloud_parameters USE control_parameters USE cpulog USE grid_variables USE indices USE interfaces USE netcdf USE netcdf_control USE particle_attributes USE pegrid IMPLICIT NONE INTEGER :: av, ngp, file_id, i, if, is, j, k, l, n, psi, s, sender, & ind(6) LOGICAL :: found, resorted REAL :: mean_r, s_r3, s_r4 REAL, DIMENSION(:,:,:), ALLOCATABLE :: local_pf #if defined( __parallel ) REAL, DIMENSION(:,:,:), ALLOCATABLE :: total_pf #endif REAL, DIMENSION(:,:,:), POINTER :: to_be_resorted ! !-- Return, if nothing to output IF ( domask_no(mid,av) == 0 ) RETURN CALL cpu_log (log_point(49),'data_output_mask','start') ! !-- Open output file. IF ( netcdf_output .AND. ( myid == 0 .OR. netcdf_data_format > 2 ) ) & THEN CALL check_open( 200+mid+av*max_masks ) ENDIF ! !-- Allocate total and local output arrays. #if defined( __parallel ) IF ( myid == 0 ) THEN ALLOCATE( total_pf(mask_size(mid,1),mask_size(mid,2),mask_size(mid,3)) ) ENDIF #endif ALLOCATE( local_pf(mask_size_l(mid,1),mask_size_l(mid,2), & mask_size_l(mid,3)) ) ! !-- Update the NetCDF time axis. domask_time_count(mid,av) = domask_time_count(mid,av) + 1 IF ( netcdf_output .AND. ( myid == 0 .OR. netcdf_data_format > 2 ) ) & THEN nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), id_var_time_mask(mid,av), & (/ simulated_time /), & start = (/ domask_time_count(mid,av) /), & count = (/ 1 /) ) CALL handle_netcdf_error( 'data_output_mask', 460 ) ENDIF ! !-- Loop over all variables to be written. if = 1 DO WHILE ( domask(mid,av,if)(1:1) /= ' ' ) ! !-- Reallocate local_pf on PE 0 since its shape changes during MPI exchange IF ( netcdf_data_format < 3 .AND. myid == 0 .AND. if > 1 ) THEN DEALLOCATE( local_pf ) ALLOCATE( local_pf(mask_size_l(mid,1),mask_size_l(mid,2), & mask_size_l(mid,3)) ) ENDIF ! !-- Store the variable chosen. resorted = .FALSE. SELECT CASE ( TRIM( domask(mid,av,if) ) ) CASE ( 'e' ) IF ( av == 0 ) THEN to_be_resorted => e ELSE to_be_resorted => e_av ENDIF CASE ( 'lpt' ) IF ( av == 0 ) THEN to_be_resorted => pt ELSE to_be_resorted => lpt_av ENDIF CASE ( 'p' ) IF ( av == 0 ) THEN to_be_resorted => p ELSE to_be_resorted => p_av ENDIF CASE ( 'pc' ) ! particle concentration (requires ghostpoint exchange) IF ( av == 0 ) THEN tend = prt_count CALL exchange_horiz( tend, nbgp ) DO i = 1, mask_size_l(mid,1) DO j = 1, mask_size_l(mid,2) DO k = 1, mask_size_l(mid,3) local_pf(i,j,k) = tend(mask_k(mid,k), & mask_j(mid,j),mask_i(mid,i)) ENDDO ENDDO ENDDO resorted = .TRUE. ELSE CALL exchange_horiz( pc_av, nbgp ) to_be_resorted => pc_av ENDIF CASE ( 'pr' ) ! mean particle radius IF ( av == 0 ) THEN DO i = nxl, nxr DO j = nys, nyn DO k = nzb, nzt+1 psi = prt_start_index(k,j,i) s_r3 = 0.0 s_r4 = 0.0 DO n = psi, psi+prt_count(k,j,i)-1 s_r3 = s_r3 + particles(n)%radius**3 s_r4 = s_r4 + particles(n)%radius**4 ENDDO IF ( s_r3 /= 0.0 ) THEN mean_r = s_r4 / s_r3 ELSE mean_r = 0.0 ENDIF tend(k,j,i) = mean_r ENDDO ENDDO ENDDO CALL exchange_horiz( tend, nbgp ) DO i = 1, mask_size_l(mid,1) DO j = 1, mask_size_l(mid,2) DO k = 1, mask_size_l(mid,3) local_pf(i,j,k) = tend(mask_k(mid,k), & mask_j(mid,j),mask_i(mid,i)) ENDDO ENDDO ENDDO resorted = .TRUE. ELSE CALL exchange_horiz( pr_av, nbgp ) to_be_resorted => pr_av ENDIF CASE ( 'pt' ) IF ( av == 0 ) THEN IF ( .NOT. cloud_physics ) THEN to_be_resorted => pt ELSE DO i = 1, mask_size_l(mid,1) DO j = 1, mask_size_l(mid,2) DO k = 1, mask_size_l(mid,3) local_pf(i,j,k) = & pt(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) & + l_d_cp * pt_d_t(mask_k(mid,k)) * & ql(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) ENDDO ENDDO ENDDO resorted = .TRUE. ENDIF ELSE to_be_resorted => pt_av ENDIF CASE ( 'q' ) IF ( av == 0 ) THEN to_be_resorted => q ELSE to_be_resorted => q_av ENDIF CASE ( 'ql' ) IF ( av == 0 ) THEN to_be_resorted => ql ELSE to_be_resorted => ql_av ENDIF CASE ( 'ql_c' ) IF ( av == 0 ) THEN to_be_resorted => ql_c ELSE to_be_resorted => ql_c_av ENDIF CASE ( 'ql_v' ) IF ( av == 0 ) THEN to_be_resorted => ql_v ELSE to_be_resorted => ql_v_av ENDIF CASE ( 'ql_vp' ) IF ( av == 0 ) THEN to_be_resorted => ql_vp ELSE to_be_resorted => ql_vp_av ENDIF CASE ( 'qv' ) IF ( av == 0 ) THEN DO i = 1, mask_size_l(mid,1) DO j = 1, mask_size_l(mid,2) DO k = 1, mask_size_l(mid,3) local_pf(i,j,k) = & q(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) - & ql(mask_k(mid,k),mask_j(mid,j),mask_i(mid,i)) ENDDO ENDDO ENDDO resorted = .TRUE. ELSE to_be_resorted => qv_av ENDIF CASE ( 'rho' ) IF ( av == 0 ) THEN to_be_resorted => rho ELSE to_be_resorted => rho_av ENDIF CASE ( 's' ) IF ( av == 0 ) THEN to_be_resorted => q ELSE to_be_resorted => s_av ENDIF CASE ( 'sa' ) IF ( av == 0 ) THEN to_be_resorted => sa ELSE to_be_resorted => sa_av ENDIF CASE ( 'u' ) IF ( av == 0 ) THEN to_be_resorted => u ELSE to_be_resorted => u_av ENDIF CASE ( 'v' ) IF ( av == 0 ) THEN to_be_resorted => v ELSE to_be_resorted => v_av ENDIF CASE ( 'vpt' ) IF ( av == 0 ) THEN to_be_resorted => vpt ELSE to_be_resorted => vpt_av ENDIF CASE ( 'w' ) IF ( av == 0 ) THEN to_be_resorted => w ELSE to_be_resorted => w_av ENDIF CASE DEFAULT ! !-- User defined quantity CALL user_data_output_mask(av, domask(mid,av,if), found, local_pf ) resorted = .TRUE. IF ( .NOT. found ) THEN WRITE ( message_string, * ) 'no output available for: ', & TRIM( domask(mid,av,if) ) CALL message( 'data_output_mask', 'PA0327', 0, 0, 0, 6, 0 ) ENDIF END SELECT ! !-- Resort the array to be output, if not done above IF ( .NOT. resorted ) THEN DO i = 1, mask_size_l(mid,1) DO j = 1, mask_size_l(mid,2) DO k = 1, mask_size_l(mid,3) local_pf(i,j,k) = to_be_resorted(mask_k(mid,k), & mask_j(mid,j),mask_i(mid,i)) ENDDO ENDDO ENDDO ENDIF ! !-- I/O block. I/O methods are implemented !-- (1) for parallel execution !-- a. with NetCDF 4 parallel I/O-enabled library !-- b. with NetCDF 3 library !-- (2) for serial execution. !-- The choice of method depends on the correct setting of preprocessor !-- directives __parallel and __netcdf4 as well as on the parameter !-- netcdf_data_format. #if defined( __parallel ) #if defined( __netcdf4 ) IF ( netcdf_data_format > 2 ) THEN ! !-- (1) a. Parallel I/O using NetCDF 4 (not yet tested) nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), & id_var_domask(mid,av,if), & local_pf, & start = (/ mask_start_l(mid,1), mask_start_l(mid,2), & mask_start_l(mid,3), domask_time_count(mid,av) /), & count = (/ mask_size_l(mid,1), mask_size_l(mid,2), & mask_size_l(mid,3), 1 /) ) CALL handle_netcdf_error( 'data_output_mask', 461 ) ELSE #endif ! !-- (1) b. Conventional I/O only through PE0 !-- PE0 receives partial arrays from all processors of the respective mask !-- and outputs them. Here a barrier has to be set, because otherwise !-- "-MPI- FATAL: Remote protocol queue full" may occur. CALL MPI_BARRIER( comm2d, ierr ) ngp = mask_size_l(mid,1) * mask_size_l(mid,2) * mask_size_l(mid,3) IF ( myid == 0 ) THEN ! !-- Local array can be relocated directly. total_pf( & mask_start_l(mid,1):mask_start_l(mid,1)+mask_size_l(mid,1)-1, & mask_start_l(mid,2):mask_start_l(mid,2)+mask_size_l(mid,2)-1, & mask_start_l(mid,3):mask_start_l(mid,3)+mask_size_l(mid,3)-1 ) & = local_pf ! !-- Receive data from all other PEs. DO n = 1, numprocs-1 ! !-- Receive index limits first, then array. !-- Index limits are received in arbitrary order from the PEs. CALL MPI_RECV( ind(1), 6, MPI_INTEGER, MPI_ANY_SOURCE, 0, & comm2d, status, ierr ) ! !-- Not all PEs have data for the mask IF ( ind(1) /= -9999 ) THEN ngp = ( ind(2)-ind(1)+1 ) * (ind(4)-ind(3)+1 ) * & ( ind(6)-ind(5)+1 ) sender = status(MPI_SOURCE) DEALLOCATE( local_pf ) ALLOCATE(local_pf(ind(1):ind(2),ind(3):ind(4),ind(5):ind(6))) CALL MPI_RECV( local_pf(ind(1),ind(3),ind(5)), ngp, & MPI_REAL, sender, 1, comm2d, status, ierr ) total_pf(ind(1):ind(2),ind(3):ind(4),ind(5):ind(6)) & = local_pf ENDIF ENDDO nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), & id_var_domask(mid,av,if), total_pf, & start = (/ 1, 1, 1, domask_time_count(mid,av) /), & count = (/ mask_size(mid,1), mask_size(mid,2), & mask_size(mid,3), 1 /) ) CALL handle_netcdf_error( 'data_output_mask', 462 ) ELSE ! !-- If at least part of the mask resides on the PE, send the index !-- limits for the target array, otherwise send -9999 to PE0. IF ( mask_size_l(mid,1) > 0 .AND. mask_size_l(mid,2) > 0 .AND. & mask_size_l(mid,3) > 0 ) & THEN ind(1) = mask_start_l(mid,1) ind(2) = mask_start_l(mid,1) + mask_size_l(mid,1) - 1 ind(3) = mask_start_l(mid,2) ind(4) = mask_start_l(mid,2) + mask_size_l(mid,2) - 1 ind(5) = mask_start_l(mid,3) ind(6) = mask_start_l(mid,3) + mask_size_l(mid,3) - 1 ELSE ind(1) = -9999; ind(2) = -9999 ind(3) = -9999; ind(4) = -9999 ind(5) = -9999; ind(6) = -9999 ENDIF CALL MPI_SEND( ind(1), 6, MPI_INTEGER, 0, 0, comm2d, ierr ) ! !-- If applicable, send data to PE0. IF ( ind(1) /= -9999 ) THEN CALL MPI_SEND( local_pf(1,1,1), ngp, MPI_REAL, 0, 1, comm2d, & ierr ) ENDIF ENDIF ! !-- A barrier has to be set, because otherwise some PEs may proceed too !-- fast so that PE0 may receive wrong data on tag 0. CALL MPI_BARRIER( comm2d, ierr ) #if defined( __netcdf4 ) ENDIF #endif #else ! !-- (2) For serial execution of PALM, the single processor (PE0) holds all !-- data and writes them directly to file. nc_stat = NF90_PUT_VAR( id_set_mask(mid,av), & id_var_domask(mid,av,if), & local_pf, & start = (/ 1, 1, 1, domask_time_count(mid,av) /), & count = (/ mask_size_l(mid,1), mask_size_l(mid,2), & mask_size_l(mid,3), 1 /) ) CALL handle_netcdf_error( 'data_output_mask', 463 ) #endif if = if + 1 ENDDO ! !-- Deallocate temporary arrays. DEALLOCATE( local_pf ) #if defined( __parallel ) IF ( myid == 0 ) THEN DEALLOCATE( total_pf ) ENDIF #endif CALL cpu_log (log_point(49),'data_output_mask','stop','nobarrier') #endif END SUBROUTINE data_output_mask