global_write_3D_velocity_VOLMASK.F90 22.3 KB
Newer Older
Femi Kadri's avatar
Femi Kadri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
SUBROUTINE global_write_3D_velocity_VOLMASK(My_MPI_Process_ID,ROOT,local_x_min_ghost,local_x_max_ghost,local_y_min_ghost,&
& local_y_max_ghost,local_z_min_ghost,local_z_max_ghost,local_x_min_real,local_x_max_real,local_y_min_real,&
& local_y_max_real,local_z_min_real,local_z_max_real,local_product_xyz_nodes_real,all_processes_local_bounds,&
& local_velocity_x,local_velocity_y,local_velocity_z,Number_Of_Processes,cartesian_communicator,lattice_dimension,&
& dx,dt,global_x_min_real,global_x_max_real,global_y_min_real,global_y_max_real,global_z_min_real,global_z_max_real,&
& pipe_radius,global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,local_walls,local_non_wall_count_real,&
& all_local_non_wall_counts_real,&
& global_fluid_count_xy,&
& global_fluid_count_yz,&
& global_fluid_count_zx,&
& global_wall_count_xy,&
& global_wall_count_yz,&
& global_wall_count_zx,&
& LST_local_vol_of_int_user_mask)


USE debug_module
USE precision_detector

	!THIS SUBROUTINE WILL GATHER VELOCITY IN ALL THREE DIMENSIONS FROM ALL THE MPI PROCESSES TO ROOT, WHO IN TURN WILL WRITE THEM TO A FILE IN ORDER RECEIVED
	IMPLICIT NONE
	INCLUDE "mpif.h"

	INTEGER,INTENT(IN) :: My_MPI_Process_ID,ROOT,Number_Of_Processes,cartesian_communicator
	INTEGER,INTENT(IN) :: local_x_min_ghost,local_x_max_ghost,local_y_min_ghost,local_y_max_ghost,local_z_min_ghost,local_z_max_ghost
	INTEGER,INTENT(IN) :: local_x_min_real,local_x_max_real,local_y_min_real,local_y_max_real,local_z_min_real,local_z_max_real
	INTEGER,INTENT(IN) :: local_product_xyz_nodes_real,lattice_dimension
	INTEGER,INTENT(IN)  :: global_x_min_real,global_x_max_real !GLOBAL BOUNDS X
	INTEGER,INTENT(IN)  :: global_y_min_real,global_y_max_real !GLOBAL BOUNDS Y
	INTEGER,INTENT(IN)  :: global_z_min_real,global_z_max_real !GLOBAL BOUNDS Z
	INTEGER,INTENT(IN) :: global_x_nodes_real,global_y_nodes_real,global_z_nodes_real
	INTEGER,INTENT(IN) :: local_non_wall_count_real
	INTEGER,INTENT(IN),DIMENSION(0:Number_Of_Processes-1) :: all_local_non_wall_counts_real
	INTEGER,INTENT(IN),DIMENSION(6 * Number_Of_Processes) :: all_processes_local_bounds

	INTEGER,INTENT(IN),DIMENSION(global_x_min_real:global_x_max_real,&
	& global_y_min_real:global_y_max_real) :: global_wall_count_xy
	INTEGER,INTENT(IN),DIMENSION(global_y_min_real:global_y_max_real,&
	& global_z_min_real:global_z_max_real) :: global_wall_count_yz
	INTEGER,INTENT(IN),DIMENSION(global_z_min_real:global_z_max_real,&
	& global_x_min_real:global_x_max_real) :: global_wall_count_zx

	INTEGER,INTENT(IN),DIMENSION(global_x_min_real:global_x_max_real,&
	& global_y_min_real:global_y_max_real) :: global_fluid_count_xy
	INTEGER,INTENT(IN),DIMENSION(global_y_min_real:global_y_max_real,&
	& global_z_min_real:global_z_max_real) :: global_fluid_count_yz
	INTEGER,INTENT(IN),DIMENSION(global_z_min_real:global_z_max_real,&
	& global_x_min_real:global_x_max_real) :: global_fluid_count_zx

	REAL,INTENT(IN) :: dx,dt,pipe_radius
	REAL,INTENT(IN),DIMENSION(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
	& local_z_min_ghost:local_z_max_ghost) :: local_velocity_x,local_velocity_y,local_velocity_z
	LOGICAL*4,DIMENSION(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
	& local_z_min_ghost:local_z_max_ghost),INTENT(IN) :: local_walls,LST_local_vol_of_int_user_mask
	
	LOGICAL*4,PARAMETER :: debug = GLOBAL_DEBUG
	!LOGICAL*4,PARAMETER :: debug = .true.
	INTEGER :: I,J,K,index,SOURCE,mpi_error_code,real_buffer_send_size,MPI_Process_Number,destination
	INTEGER :: integer_buffer_send_size,logical_buffer_send_size

	INTEGER,PARAMETER :: x = 1
	INTEGER,PARAMETER :: y = 2
	INTEGER,PARAMETER :: z = 3
	INTEGER,PARAMETER :: tag_integer_communication = 4567
	INTEGER,PARAMETER :: tag_real_communication = 7654
	INTEGER,PARAMETER :: tag_logical_WALLS_communication = 4756
	INTEGER,PARAMETER :: tag_logical_MASK_communication = 6475
	INTEGER :: counter

	INTEGER :: my_mpi_real_data_type
	INTEGER :: recv_size_real_buffer,recv_size_logical_buffer
	INTEGER :: recv_size_integer_buffer

!	INTEGER,DIMENSION(0:Number_Of_Processes-1) :: root_receive_counts
	INTEGER,ALLOCATABLE,DIMENSION(:) :: local_integer_send_buffer,local_integer_recv_buffer
	REAL,ALLOCATABLE,DIMENSION(:) :: local_real_send_buffer,local_real_recv_buffer
	REAL,ALLOCATABLE,DIMENSION(:,:) :: global_velocity_average_yz	
	LOGICAL,ALLOCATABLE,DIMENSION(:) :: local_logical_WALLS_send_buffer,local_logical_WALLS_recv_buffer
	LOGICAL,ALLOCATABLE,DIMENSION(:) :: local_logical_MASK_send_buffer,local_logical_MASK_recv_buffer
	REAL :: dt_inverse_dx,total_distance_from_center,z_distance_from_center,y_distance_from_center,center_y,center_z
	REAL :: velocity_magnitude
	REAL :: dx_in_microns,I_in_microns,J_in_microns,K_in_microns
	INTEGER :: status (MPI_STATUS_SIZE)

!	REAL, DIMENSION(:),ALLOCATABLE :: global_velocity_average_x
!	REAL, DIMENSION(:),ALLOCATABLE :: global_velocity_average_y
!	REAL, DIMENSION(:),ALLOCATABLE :: global_velocity_average_z


	my_mpi_real_data_type = get_my_mpi_real_data_type ()  !FUNNEL OUT THE RESULT OF PRECISION TESTING (this automatically sets the MPI REAL DATA PRECISION TYPE)

	dt_inverse_dx = dt / dx

	dx_in_microns = dx * 10000

	IF (My_MPI_Process_ID.EQ.ROOT) THEN
		
!		ALLOCATE( global_velocity_average_x(global_x_min_ghost:global_x_max_ghost) )
!		ALLOCATE( global_velocity_average_y(global_y_min_ghost:global_y_max_ghost) )
!		ALLOCATE( global_velocity_average_z(global_z_min_ghost:global_z_max_ghost) )

		ALLOCATE( global_velocity_average_yz(global_y_min_real:global_y_max_real,global_z_min_real:global_z_max_real) )
	
		global_velocity_average_yz(:,:) = 0.0


!		OPEN (73,FILE = 'backup/global_velocity_field_lattice_units.dat', STATUS = 'unknown', form='unformatted') !LATTICE UNITS
!		OPEN (74,FILE = 'backup/global_velocity_field_lattice_units.txt', STATUS = 'unknown', form='formatted') !LATTICE UNITS
		OPEN (77,FILE = 'results_LBM/TECPLOT_CONVERGED_global_velocity_field_VOLMASK.txt', STATUS = 'unknown')  !PHYSICAL UNITS [cm/s]
		WRITE(77,'(A132)') 'VARIABLES = "X [microns]", "Y [microns]", "Z [microns]", "Velocity X [cm/s]", "Velocity Y [cm/s]", "Velocity Z [cm/s]", "Velocity Magnitude [cm/s]"'
		WRITE(77,*) 'ZONE,i=',global_x_nodes_real,',j=',global_y_nodes_real,',k=',global_z_nodes_real,',F=POINT,STRANDID=0'
	
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to write values from ROOT'
	
		DO K = local_z_min_real,local_z_max_real	!PACK THE DATA IN THE SEND BUFFERS

			K_in_microns = REAL(K) * dx_in_microns

			DO J = local_y_min_real,local_y_max_real

				J_in_microns = REAL(J) * dx_in_microns

				DO I = local_x_min_real,local_x_max_real

					I_in_microns = REAL(I) * dx_in_microns

					IF(.NOT.local_walls(I,J,K)) THEN !WRITE ONLY THE FLUID VALUES
					
!						WRITE(73) x,I,J,K,local_velocity_x(I,J,K) * dt_inverse_dx  !convert to lattice units for backup
!						WRITE(74,*) x,I,J,K,local_velocity_x(I,J,K) * dt_inverse_dx  !convert to lattice units for backup

!						WRITE(73) y,I,J,K,local_velocity_y(I,J,K) * dt_inverse_dx
!						WRITE(74,*) y,I,J,K,local_velocity_y(I,J,K) * dt_inverse_dx

!						WRITE(73) z,I,J,K,local_velocity_z(I,J,K) * dt_inverse_dx
!						WRITE(74,*) z,I,J,K,local_velocity_z(I,J,K) * dt_inverse_dx

					
						global_velocity_average_yz(J,K) = global_velocity_average_yz(J,K) + local_velocity_x(I,J,K)

!						global_velocity_average_x(I) = global_velocity_average_x(I) + local_velocity_x(I,J,K)*(dx/dt)
!						global_velocity_average_x(I) = global_velocity_average_x(I) + local_velocity_y(I,J,K)*(dx/dt)
!						global_velocity_average_x(I) = global_velocity_average_x(I) + local_velocity_z(I,J,K)*(dx/dt)

					ENDIF !!.NOT.local_walls(I,J,K)

					velocity_magnitude = SQRT( (local_velocity_x(I,J,K) * local_velocity_x(I,J,K)) + &
					& (local_velocity_y(I,J,K) * local_velocity_y(I,J,K)) + &
					& (local_velocity_z(I,J,K) * local_velocity_z(I,J,K))  )


!					WRITE(77,'(7E19.10)') I_in_microns,J_in_microns,K_in_microns,local_velocity_x(I,J,K),&
!					& local_velocity_y(I,J,K),local_velocity_z(I,J,K),velocity_magnitude !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]


					IF(LST_local_vol_of_int_user_mask(I,J,K)) THEN !WRITE ONLY THE VELOCITIES WITHIN THE MASK

						WRITE(77,'(3I6.5,4E19.10)') I,J,K,local_velocity_x(I,J,K),&
						& local_velocity_y(I,J,K),local_velocity_z(I,J,K),velocity_magnitude !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
					
					ELSE !.NOT.LST_local_vol_of_int_user_mask(I,J,K)

						WRITE(77,'(3I6.5,4E19.10)') I,J,K,REAL(0),&
						& REAL(0),REAL(0),REAL(0) !ZERO BECAUSE OUTSIDE OF THE LOGICAL MASK
				
					ENDIF !!LST_local_vol_of_int_user_mask(I,J,K)


				END DO !I

			END DO	!J

		END DO !K
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished writing values from ROOT'

	ENDIF !! My_MPI_Process_ID.EQ.ROOT
	
	!COMMUNICATE DATA TO ROOT FROM ALL NON-ROOT PROCESSES(PACK IT INTO BUFFERS FIRST) AND THEN HAVE ROOT WRITE IT TO A FILE
	IF (My_MPI_Process_ID.NE.ROOT) THEN  !MAKE ALL THE NON-ROOT PROCESSES SEND THEIR BUFFERS TO ROOT

		!real_buffer_send_size = local_non_wall_count_real * lattice_dimension
		real_buffer_send_size = local_product_xyz_nodes_real * lattice_dimension
		integer_buffer_send_size =  local_product_xyz_nodes_real * lattice_dimension
		logical_buffer_send_size =  local_product_xyz_nodes_real

		ALLOCATE( local_integer_send_buffer(integer_buffer_send_size) )	
		ALLOCATE ( local_real_send_buffer(real_buffer_send_size) )
		ALLOCATE ( local_logical_WALLS_send_buffer(logical_buffer_send_size) )
		ALLOCATE ( local_logical_MASK_send_buffer(logical_buffer_send_size) )
		
			

		index = 1		

		counter = 1
					

		DO K = local_z_min_real,local_z_max_real	!PACK THE DATA IN THE SEND BUFFERS

			DO J = local_y_min_real,local_y_max_real

				DO I = local_x_min_real,local_x_max_real

!					IF(.NOT.local_walls(I,J,K)) THEN !WRITE ONLY THE FLUID VALUES

						!STORE LOCAL WALL VALUE
						local_logical_WALLS_send_buffer(counter) = local_walls(I,J,K)
						local_logical_MASK_send_buffer(counter) = LST_local_vol_of_int_user_mask(I,J,K)

						!STORE COORDINATES
						local_integer_send_buffer(index) = I 
						local_integer_send_buffer(index + 1) = J
						local_integer_send_buffer(index + 2) = K
			
						!STORE VELOCITIES
						local_real_send_buffer(index) = local_velocity_x(I,J,K)
						local_real_send_buffer(index + 1) = local_velocity_y(I,J,K)
						local_real_send_buffer(index + 2) = local_velocity_z(I,J,K)

						index = index + lattice_dimension

						counter = counter + 1
					
!					ENDIF !!.NOT.local_walls(I,J,K)

				END DO !I

			END DO	!J

		END DO !K

		destination = ROOT  !send buffers to ROOT

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to send local_integer_send_buffer'
		CALL MPI_Send(local_integer_send_buffer,integer_buffer_send_size,MPI_INTEGER,&
		& destination,tag_integer_communication,cartesian_communicator,mpi_error_code)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished sending local_integer_send_buffer'

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to send local_real_send_buffer'
		CALL MPI_Send(local_real_send_buffer,real_buffer_send_size,my_mpi_real_data_type,&
		& destination,tag_real_communication,cartesian_communicator,mpi_error_code)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished sending local_real_send_buffer'

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to send local_logical_WALLS_send_buffer'
		CALL MPI_Send(local_logical_WALLS_send_buffer,logical_buffer_send_size,MPI_LOGICAL,&
		& destination,tag_logical_WALLS_communication,cartesian_communicator,mpi_error_code)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished sending local_logical_WALLS_send_buffer'

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to send local_logical_MASK_send_buffer'
		CALL MPI_Send(local_logical_MASK_send_buffer,logical_buffer_send_size,MPI_LOGICAL,&
		& destination,tag_logical_MASK_communication,cartesian_communicator,mpi_error_code)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished sending local_logical_MASK_send_buffer'



		DEALLOCATE( local_integer_send_buffer )	
		DEALLOCATE( local_real_send_buffer    )
		DEALLOCATE( local_logical_WALLS_send_buffer )
		DEALLOCATE( local_logical_MASK_send_buffer )

	ELSE  !!My_MPI_Process_ID.EQ.ROOT (SO THIS HAPPENS ONLY ON THE ROOT PROCESS)

		DO MPI_Process_Number = 0, Number_Of_Processes - 1  !CYCLE OVER ALL PROCESSES ON THE ROOT PROCESS
		
			IF(MPI_Process_Number.NE.ROOT) THEN !THIS IS TO ACCOUNT FOR THE CASE IF ROOT IS NOT ZERO, WE COULD HAVE JUST STARTED THE LOOP AT 1 INSTEAD

				SOURCE = MPI_Process_Number !ASSIGN CURRENT PROCESS AS "SOURCE" FROM WHICH INFORMATION IS RECEIVED
				
				!recv_size_real_buffer = all_local_non_wall_counts_real(MPI_Process_Number) * lattice_dimension

				!root_receive_counts(MPI_Process_Number) = lattice_dimension *( all_processes_local_bounds(2 + MPI_Process_Number * 6) - all_processes_local_bounds(1 + MPI_Process_Number * 6) + 1 ) * &
				!& ( all_processes_local_bounds(4 + MPI_Process_Number * 6) - all_processes_local_bounds(3 + MPI_Process_Number * 6) + 1 ) * &
				!& ( all_processes_local_bounds(6 + MPI_Process_Number * 6) - all_processes_local_bounds(5 + MPI_Process_Number * 6) + 1 )
			

				recv_size_logical_buffer = ( all_processes_local_bounds(2 + MPI_Process_Number * 6) - all_processes_local_bounds(1 + MPI_Process_Number * 6) + 1 ) * &
				& ( all_processes_local_bounds(4 + MPI_Process_Number * 6) - all_processes_local_bounds(3 + MPI_Process_Number * 6) + 1 ) * &
				& ( all_processes_local_bounds(6 + MPI_Process_Number * 6) - all_processes_local_bounds(5 + MPI_Process_Number * 6) + 1 )

				recv_size_real_buffer = lattice_dimension * recv_size_logical_buffer

				recv_size_integer_buffer = recv_size_real_buffer
			
				ALLOCATE( local_integer_recv_buffer(recv_size_integer_buffer)    )	
				ALLOCATE( local_real_recv_buffer(recv_size_real_buffer)          )	
				ALLOCATE( local_logical_WALLS_recv_buffer(recv_size_logical_buffer)    )
				ALLOCATE( local_logical_MASK_recv_buffer(recv_size_logical_buffer)    )
					


				!ROOT RECEIVES FROM ALL THE NON-ROOT PROCESSES THEIR BUFFERS
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to recv local_integer_recv_buffer'
				CALL MPI_Recv( local_integer_recv_buffer,recv_size_integer_buffer,&
				& MPI_INTEGER,SOURCE,tag_integer_communication, cartesian_communicator,&
				& status,mpi_error_code)
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished recving local_integer_recv_buffer'

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to recv local_real_recv_buffer'
				CALL MPI_Recv( local_real_recv_buffer,recv_size_real_buffer,&
				& my_mpi_real_data_type,SOURCE,tag_real_communication,&
				& cartesian_communicator,status,mpi_error_code )
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished recving local_real_recv_buffer'
			

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to recv local_logical_WALLS_recv_buffer'
				CALL MPI_Recv( local_logical_WALLS_recv_buffer,recv_size_logical_buffer,&
				& MPI_LOGICAL,SOURCE,tag_logical_WALLS_communication,&
				& cartesian_communicator,status,mpi_error_code )
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished recving local_logical_WALLS_recv_buffer'

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to recv local_logical_MASK_recv_buffer'
				CALL MPI_Recv( local_logical_MASK_recv_buffer,recv_size_logical_buffer,&
				& MPI_LOGICAL,SOURCE,tag_logical_MASK_communication,&
				& cartesian_communicator,status,mpi_error_code )
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished recving local_logical_MASK_recv_buffer'




				!ROOT WRITES DATA RECIEVED FROM NON-ROOT PROCESSES TO A FILE
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to write data from process',MPI_Process_Number
				
				counter = 1


				DO index = 1,recv_size_real_buffer,lattice_dimension
					
					!WRITE ONLY FLUID NODES TO THE RESUME FILES
					IF(	.NOT.local_logical_WALLS_recv_buffer(counter) ) THEN

!						WRITE(73) x,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index) * dt_inverse_dx
!						WRITE(74,*) x,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index) * dt_inverse_dx


!						WRITE(73) y,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index + 1) * dt_inverse_dx
!						WRITE(74,*) y,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index + 1) * dt_inverse_dx

!						WRITE(73) z,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index + 2) * dt_inverse_dx  !lattice units
!						WRITE(74,*) z,local_integer_recv_buffer(index),local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2),&
!						& local_real_recv_buffer(index + 2) * dt_inverse_dx  !lattice units

						global_velocity_average_yz(local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2)) = &
						& global_velocity_average_yz(local_integer_recv_buffer(index + 1),local_integer_recv_buffer(index + 2)) + local_real_recv_buffer(index)

					ENDIF !! .NOT.local_logical_WALLS_recv_buffer(counter)

					velocity_magnitude = SQRT( (local_real_recv_buffer(index) * local_real_recv_buffer(index)) + &
					& (local_real_recv_buffer(index + 1) * local_real_recv_buffer(index + 1)) + &
					& (local_real_recv_buffer(index + 2) * local_real_recv_buffer(index + 2))  )


!					WRITE(77,'(7E19.10)') &
!					& REAL(local_integer_recv_buffer(index)) * dx_in_microns,&
!					& REAL(local_integer_recv_buffer(index + 1)) * dx_in_microns,&
!					& REAL(local_integer_recv_buffer(index + 2)) * dx_in_microns,&
!					& local_real_recv_buffer(index),&
!					& local_real_recv_buffer(index + 1),&
!					& local_real_recv_buffer(index + 2),&
!					& velocity_magnitude  !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]

					IF(local_logical_MASK_recv_buffer(counter)) THEN !WRITE ONLY THE VELOCITIES WITHIN THE MASK

						WRITE(77,'(3I6.5,4E19.10)') &
						& local_integer_recv_buffer(index),& !* dx_in_microns,&
						& local_integer_recv_buffer(index + 1),& !* dx_in_microns,&
						& local_integer_recv_buffer(index + 2),& !* dx_in_microns,&
						& local_real_recv_buffer(index),&
						& local_real_recv_buffer(index + 1),&
						& local_real_recv_buffer(index + 2),&
						& velocity_magnitude  !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
					
					ELSE !.NOT.local_logical_MASK_recv_buffer(I,J,K)

						WRITE(77,'(3I6.5,4E19.10)') I,J,K,REAL(0),&
						& REAL(0),REAL(0),REAL(0) !ZERO BECAUSE OUTSIDE OF THE LOGICAL MASK
				
					ENDIF !!LST_local_vol_of_int_user_mask(I,J,K)
	
					counter = counter + 1
		
				END DO !index
				
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished writing data from process',MPI_Process_Number
			
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: about to deallocate buffers for process',MPI_Process_Number

				DEALLOCATE( local_integer_recv_buffer )	
				DEALLOCATE( local_real_recv_buffer    )
				DEALLOCATE( local_logical_WALLS_recv_buffer )
				DEALLOCATE( local_logical_MASK_recv_buffer )

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_3D_velocity_VOLMASK: finished deallocating buffers for process',MPI_Process_Number
	
			ENDIF !!MPI_Process_Number.NE.ROOT
		
		END DO !!MPI_Process_Number

		!ROOT WRITES ITS OWN VALUES
	
	ENDIF !!My_MPI_Process_ID IS ROOT OR NON-ROOT

	!ROOT CLOSES THE FILE
	IF (My_MPI_Process_ID.EQ.ROOT) THEN

		OPEN (80, FILE = 'results_LBM/CONVERGED_global_velocity_x_radius.txt', STATUS = 'unknown', form='formatted')  !FILE CONTAINING X-ARRAY TO BE AVERAGED

		center_y = REAL(global_y_min_real) + REAL(global_y_max_real-global_y_min_real)/2.0
		center_z = REAL(global_z_min_real) + REAL(global_z_max_real-global_z_min_real)/2.0


		DO K = global_z_min_real,global_z_max_real	
			
			z_distance_from_center = REAL(K) - center_z

			DO J = global_y_min_real,global_y_max_real

				IF(global_velocity_average_yz(J,K).GT.0.AND.global_fluid_count_yz(J,K).EQ.0) THEN
					PRINT*,'J',J,'K',K
					PRINT*,'global_velocity_average_yz(J,K)',global_velocity_average_yz(J,K)
					PRINT*,'global_fluid_count_yz(J,K)',global_fluid_count_yz(J,K)
					STOP
				ENDIF !!

				IF(global_velocity_average_yz(J,K).GT.0) global_velocity_average_yz(J,K) = &
				& global_velocity_average_yz(J,K) / global_fluid_count_yz(J,K)

				y_distance_from_center = REAL(J) - center_y

				total_distance_from_center = SQRT(z_distance_from_center * z_distance_from_center + y_distance_from_center * y_distance_from_center)
				
				IF(total_distance_from_center.LE.pipe_radius) THEN  !CHECK IF SMALLER THAN RADIUS, THEN WRITE (EVERYTHING LARGER THAN pipe_radius IS WALLS)
					
					WRITE(80,*) total_distance_from_center * dx,global_velocity_average_yz(J,K)
					!WRITE(80,*) j,k,total_distance_from_center,global_velocity_average_yz(J,K)

				END IF !!total_distance_from_center.LE.pipe_radius

			END DO	!J

		END DO !K
		
		CLOSE(77) !!OPEN (77,FILE = 'results_LBM/global_velocity_field.txt', STATUS = 'unknown')  !PHYSICAL UNITS [cm/s]

!		CLOSE(73) !!OPEN (73,FILE = 'backup/global_velocity_field_lattice_units.dat', STATUS = 'unknown', form='unformatted') !LATTICE UNITS

!		CLOSE(74) !!OPEN (74,FILE = 'backup/global_velocity_field_lattice_units.txt', STATUS = 'unknown', form='formatted') !LATTICE UNITS


		CLOSE(80)  !FILE = 'results_LBM/CONVERGED_global_velocity_x_radius.txt', STATUS = 'unknown', form='formatted')  !FILE CONTAINING X-ARRAY TO BE AVERAGED

!		DEALLOCATE( global_velocity_average_x )
!		DEALLOCATE( global_velocity_average_y )
!		DEALLOCATE( global_velocity_average_z )
	


		DEALLOCATE ( global_velocity_average_yz )


	ENDIF !!My_MPI_Process_ID.EQ.ROOT

call MPI_Barrier(cartesian_communicator,mpi_error_code)

RETURN
END SUBROUTINE global_write_3D_velocity_VOLMASK