global_write_histogram.F90 31.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
SUBROUTINE global_write_histogram(local_3d_array,local_logical_array,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,&
& dx, &
& global_x_min_real,global_x_max_real,global_y_min_real,global_y_max_real,global_z_min_real,global_z_max_real,&
& global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,pipe_radius,&
& Number_Of_Processes,cartesian_communicator,lattice_dimension,Write_inverse_of_logical_array,&
& filename_number_of_letters,filename,global_3d_array_min,global_3d_array_max,Total_Number_of_Histogram_Bins,&
& Secondary_Histogram_BinFraction,global_3d_array_average,global_non_wall_count_real,Inv_global_non_wall_count_real)



USE debug_module
USE precision_detector

	!THIS SUBROUTINE WILL GATHER A 3D ARRAY 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
	INTEGER,INTENT(IN) :: 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
	INTEGER,INTENT(IN) :: local_z_min_real,local_z_max_real
	INTEGER,INTENT(IN) :: global_x_min_real,global_x_max_real,global_y_min_real,global_y_max_real
	INTEGER,INTENT(IN) :: global_z_min_real,global_z_max_real
	INTEGER,INTENT(IN) :: local_product_xyz_nodes_real,lattice_dimension,filename_number_of_letters
	INTEGER,INTENT(IN) :: global_x_nodes_real,global_y_nodes_real,global_z_nodes_real
	INTEGER,INTENT(IN) :: Total_Number_of_Histogram_Bins,global_non_wall_count_real
	INTEGER,INTENT(IN),DIMENSION(6 * Number_Of_Processes) :: all_processes_local_bounds
	REAL,INTENT(IN) :: dx,pipe_radius,Inv_global_non_wall_count_real
	REAL,INTENT(IN) :: Secondary_Histogram_BinFraction
	REAL,INTENT(IN) :: global_3d_array_min,global_3d_array_max,global_3d_array_average
	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_3d_array
	LOGICAL*4,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_logical_array 
	LOGICAL*4,INTENT(IN) :: Write_inverse_of_logical_array
Femi Kadri's avatar
Femi Kadri committed
39 40
	!LOGICAL*4,PARAMETER :: debug = GLOBAL_DEBUG
	LOGICAL*4,PARAMETER :: debug = .true.
Femi Kadri's avatar
Femi Kadri committed
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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	INTEGER :: I,J,K,index,SOURCE,mpi_error_code,MPI_Process_Number,counter
	INTEGER :: send_size_real,send_size_logical !,send_size_integer
	INTEGER :: destination
!	INTEGER,PARAMETER :: tag_integer_communication = 689
	INTEGER,PARAMETER :: tag_real_communication = 976
	INTEGER,PARAMETER :: tag_logical_communication = 769
	INTEGER,DIMENSION(0:Number_Of_Processes-1) :: root_receive_counts_real,&
	& root_receive_counts_logical !,root_receive_counts_integer
!	INTEGER,ALLOCATABLE,DIMENSION(:) :: local_integer_send_buffer,local_integer_recv_buffer
	REAL :: center_y,center_z,y_distance_from_center,z_distance_from_center,total_distance_from_center
	REAL :: sum_squares_3d_array_deviations
	REAL,ALLOCATABLE,DIMENSION(:) :: local_real_send_buffer,local_real_recv_buffer
	LOGICAL,ALLOCATABLE,DIMENSION(:) :: local_logical_send_buffer,local_logical_recv_buffer
	
	INTEGER :: histogram_bin_number,HISTOGRAM_BIN_POINTER
	INTEGER :: my_mpi_real_data_type
!	INTEGER, PARAMETER :: Total_Number_of_Histogram_Bins = 50000
	REAL :: standard_deviation_of_3d_array	
	REAL,ALLOCATABLE,DIMENSION(:) :: Histogram_Bin_Range_Values ! THIS DEFINES THE BOUNDS FOR THE HISTOGRAM BINS(ALL BUT THE MIN AND THE MAX BOUND)
	INTEGER,ALLOCATABLE, DIMENSION(:) :: Historam_Bin_Bucket  !THIS IS WHERE THE NUMBER OF OCCURANCES PER BIN IS COLLECTED
!	REAL :: local_3d_array_min,local_3d_array_max
	REAL :: Histogram_Bin_Size,Inverse_Histogram_Bin_Size
	LOGICAL*4 :: write_histogram

	REAL,DIMENSION(:),ALLOCATABLE :: global_average_x
	REAL,DIMENSION(:),ALLOCATABLE :: global_average_y
	REAL,DIMENSION(:),ALLOCATABLE :: global_average_z

	REAL,ALLOCATABLE,DIMENSION(:,:) :: global_array_average_yz


	INTEGER :: status (MPI_STATUS_SIZE)

	CHARACTER(LEN = filename_number_of_letters) filename
	CHARACTER(LEN = 3) Secondary_Histogram_Bin_Percent

IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to SUBROUTINE global_write_histogram'
IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to SUBROUTINE global_write_histogram'

	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)

	sum_squares_3d_array_deviations = 0.0

IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: Secondary_Histogram_BinFraction',Secondary_Histogram_BinFraction
IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: Secondary_Histogram_BinFraction',Secondary_Histogram_BinFraction

IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: INT(Secondary_Histogram_BinFraction*100)',INT(Secondary_Histogram_BinFraction*100)

	WRITE (Secondary_Histogram_Bin_Percent,'(I3)') INT(Secondary_Histogram_BinFraction*100)

IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: Secondary_Histogram_Bin_Percent',Secondary_Histogram_Bin_Percent
IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: Secondary_Histogram_Bin_Percent',Secondary_Histogram_Bin_Percent

!Secondary_Histogram_BinFraction
!	local_3d_array_max = MAXVAL(local_3d_array)  !OBTAIN THE LOCAL MAXIMUM
!	local_3d_array_min = MINVAL(local_3d_array)  !OBTAIN THE LOCAL MINIMUM

!	WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_3d_array_max '//filename,local_3d_array_max
!	WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_3d_array_min '//filename,local_3d_array_min


	!FIND THE global MIN AND THE MAX VALUES OF THE VELOCITY FIELD IN EACH OF THE THREE DIMENSIONS
!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to CALL MPI_Allreduce:local_3d_array_min'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: about to CALL MPI_Allreduce:local_3d_array_min'

!	CALL MPI_Allreduce(local_3d_array_min,global_3d_array_min,1,my_mpi_real_data_type,MPI_MIN,cartesian_communicator,mpi_error_code)

!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: finished to CALL MPI_Allreduce:local_3d_array_min'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: finished to CALL MPI_Allreduce:local_3d_array_min'


	!FIND THE global MIN AND THE MAX VALUES OF THE VELOCITY FIELD IN EACH OF THE THREE DIMENSIONS
!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to CALL MPI_Allreduce:local_3d_array_max'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: about to CALL MPI_Allreduce:local_3d_array_max'

!	CALL MPI_Allreduce(local_3d_array_max,global_3d_array_max,1,my_mpi_real_data_type,MPI_MAX,cartesian_communicator,mpi_error_code)

!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: finished to CALL MPI_Allreduce:local_3d_array_max'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: finished to CALL MPI_Allreduce:local_3d_array_max'
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: global_3d_array_max '//filename,global_3d_array_max
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: global_3d_array_min '//filename,global_3d_array_min

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_min_real '//filename,local_x_min_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_min_real '//filename,local_x_min_real
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_max_real '//filename,local_x_max_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_max_real '//filename,local_x_max_real
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_min_real '//filename,local_y_min_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_min_real '//filename,local_y_min_real
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_max_real '//filename,local_y_max_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_max_real '//filename,local_y_max_real
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_min_real '//filename,local_z_min_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_min_real '//filename,local_z_min_real
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_max_real '//filename,local_z_max_real
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_max_real '//filename,local_z_max_real


	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_min_ghost '//filename,local_x_min_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_min_ghost '//filename,local_x_min_ghost
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_max_ghost '//filename,local_x_max_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_x_max_ghost '//filename,local_x_max_ghost
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_min_ghost '//filename,local_y_min_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_min_ghost '//filename,local_y_min_ghost
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_max_ghost '//filename,local_y_max_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_y_max_ghost '//filename,local_y_max_ghost
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_min_ghost '//filename,local_z_min_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_min_ghost '//filename,local_z_min_ghost
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_max_ghost '//filename,local_z_max_ghost
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: local_z_max_ghost '//filename,local_z_max_ghost



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

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to write array values from ROOT'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to write array values from ROOT'
	
!		ALLOCATE ( global_average_x(global_x_min_real:global_x_max_real)  )
!		ALLOCATE ( global_average_y(global_y_min_real:global_y_max_real)  )
!		ALLOCATE ( global_average_z(global_z_min_real:global_z_max_real)  )

!		ALLOCATE( global_array_average_yz(global_y_min_real:global_y_max_real,global_z_min_real:global_z_max_real) )

		ALLOCATE ( Histogram_Bin_Range_Values(1:Total_Number_of_Histogram_Bins) )
		ALLOCATE ( Historam_Bin_Bucket(1:Total_Number_of_Histogram_Bins) )
		
		Histogram_Bin_Size = (global_3d_array_max - global_3d_array_min) / (Total_Number_of_Histogram_Bins - 1)  !CALCULATE THE SIZE OF EACH HISTOGRAM BIN
		
		IF(Histogram_Bin_Size.EQ.0) THEN  !CHECK TO SEE IF VALUES ARE NON-ZERO, DON'T WRITE HISTOGRAM IF THEY ARE
			write_histogram = .FALSE.
		ELSE 
			write_histogram = .TRUE.
		ENDIF !! Histogram_Bin_Size.EQ.0


		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: Histogram_Bin_Size',Histogram_Bin_Size


		IF( write_histogram ) THEN
	
			Inverse_Histogram_Bin_Size = 1.0 / Histogram_Bin_Size
			IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: Inverse_Histogram_Bin_Size',Inverse_Histogram_Bin_Size
			
			DO histogram_bin_number = 1, Total_Number_of_Histogram_Bins
			
				IF(histogram_bin_number.EQ.1) THEN

					Histogram_Bin_Range_Values(histogram_bin_number) = global_3d_array_min + Histogram_Bin_Size

				ELSE
				
					Histogram_Bin_Range_Values(histogram_bin_number) = Histogram_Bin_Range_Values(histogram_bin_number - 1) + Histogram_Bin_Size

				ENDIF !! histogram_bin_number.EQ.1

			END DO !! histogram_bin_number = 1, Total_Number_of_Histogram_Bins

		ENDIF !! write_histogram

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to zero variables'
	
!				global_array_average_yz(:,:) = 0.0
!				global_average_x(:) = 0.0
!				global_average_y(:) = 0.0
!				global_average_z(:) = 0.0
				Historam_Bin_Bucket(:) = 0 

!		OPEN (77, FILE = filename//'.txt', STATUS = 'unknown', FORM='formatted')  
!		WRITE(77,*) 'ZONE,i=',global_x_nodes_real,',j=',global_y_nodes_real,',k=',global_z_nodes_real
		
		!ROOT WRITES ITS OWN VALUES
		
		DO K = local_z_min_real,local_z_max_real	

			DO J = local_y_min_real,local_y_max_real

				DO I = local_x_min_real,local_x_max_real
			
				IF(.NOT.Write_inverse_of_logical_array) THEN  !IF YOU don't WANT TO WRITE THE INVERSE OF THE local_logical_array

					IF( local_logical_array(I,J,K).AND.write_histogram ) THEN
					
!						WRITE(77,'(3I6.5,E19.10)') I,J,K,local_3d_array(I,J,K)
					
!						global_average_x(I) = global_average_x(I) + local_3d_array(I,J,K)
!						global_average_y(J) = global_average_y(J) + local_3d_array(I,J,K)
!						global_average_z(K) = global_average_z(K) + local_3d_array(I,J,K)
						
						sum_squares_3d_array_deviations = sum_squares_3d_array_deviations + &
						& ( global_3d_array_average - local_3d_array(I,J,K) ) * ( global_3d_array_average - local_3d_array(I,J,K) )

!						global_array_average_yz(J,K) = global_array_average_yz(J,K) + local_3d_array(I,J,K)

						IF(local_3d_array(I,J,K).LE.global_3d_array_max) THEN  !CHECK TO MAKE SURE THAT THE VALUE BELONGS TO NEW BINNING RANGE
							
							HISTOGRAM_BIN_POINTER = INT( ( local_3d_array(I,J,K) - global_3d_array_min ) * Inverse_Histogram_Bin_Size ) + 1
						
							Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1

						ENDIF !!local_3d_array(I,J,K).LE.global_3d_array_max
!					ELSE
						!WRITE(77,*) I,J,K,0.0,'ROOT',local_logical_array(I,J,K) !,'Write_inverse_of_logical_array',Write_inverse_of_logical_array
!						WRITE(77,'(3I6.5,E19.10)') I,J,K,0.0
!						IF(HISTOGRAM_BIN_POINTER.GE.1.AND.HISTOGRAM_BIN_POINTER.LE.Total_Number_of_Histogram_Bins) THEN
!							Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1
!						ELSEIF(HISTOGRAM_BIN_POINTER.EQ.Total_Number_of_Histogram_Bins) THEN
!						ELSE
!							PRINT*, 'HISTOGRAM ERROR - SHOULDNT HAPPEN!!!'
!							STOP
!						ENDIF !!

						!IF (local_3d_array(I,J,K).LT.Histogram_Bin_Range_Values(HISTOGRAM_BIN_POINTER)) THEN
							
						!	Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1
						
						!END IF

						!IF (local_3d_array(I,J,K).GE.Histogram_Bin_Range_Values(Total_Number_of_Histogram_Bins))  Historam_Bin_Bucket(Total_Number_of_Histogram_Bins) = Historam_Bin_Bucket(Total_Number_of_Histogram_Bins) + 1

					END IF !!local_logical_array(I,J,K).AND.write_histogram

				ELSE !!Write_inverse_of_logical_array !IF YOU WANT TO WRITE THE INVERSE OF THE local_logical_array

					IF( .NOT.local_logical_array(I,J,K).AND.write_histogram ) THEN
					
!						WRITE(77,'(3I6.5,E19.10)') I,J,K,local_3d_array(I,J,K)
					
!						global_average_x(I) = global_average_x(I) + local_3d_array(I,J,K)
!						global_average_y(J) = global_average_y(J) + local_3d_array(I,J,K)
!						global_average_z(K) = global_average_z(K) + local_3d_array(I,J,K)
						sum_squares_3d_array_deviations = sum_squares_3d_array_deviations + &
						& ( global_3d_array_average - local_3d_array(I,J,K) ) * ( global_3d_array_average - local_3d_array(I,J,K) )


						IF(local_3d_array(I,J,K).LE.global_3d_array_max) THEN  !CHECK TO MAKE SURE THAT THE VALUE BELONGS TO NEW BINNING RANGE

							HISTOGRAM_BIN_POINTER = INT( ( local_3d_array(I,J,K) - global_3d_array_min ) * Inverse_Histogram_Bin_Size ) + 1

							Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1

						ENDIF !!local_3d_array(I,J,K).LE.global_3d_array_max
!					ELSE
!						WRITE(77,'(3I6.5,E19.10)') I,J,K,0.0 !,'ROOT:inv',local_logical_array(I,J,K) !,'Write_inverse_of_logical_array',Write_inverse_of_logical_array

					END IF !!local_logical_array(I,J,K)


				ENDIF !!Write_inverse_of_logical_array.AND.write_histogram

				END DO !I

			END DO	!J

		END DO !K 

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished writing array values from ROOT'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished writing array 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
	
!		send_size_integer = local_product_xyz_nodes_real*lattice_dimension		
		send_size_real = local_product_xyz_nodes_real
		send_size_logical = local_product_xyz_nodes_real
!		IF(debug) WRITE (0,*) My_MPI_Process_ID,'send_size_integer',send_size_integer,'send_size_real',send_size_real
!		IF(debug) WRITE (9,*) My_MPI_Process_ID,'send_size_integer',send_size_integer,'send_size_real',send_size_real
!		ALLOCATE( local_integer_send_buffer(send_size_integer) )	
		ALLOCATE ( local_real_send_buffer(send_size_real) )
		ALLOCATE ( local_logical_send_buffer(send_size_logical) )

		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

					!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(counter) = local_3d_array(I,J,K)
					local_logical_send_buffer(counter) = local_logical_array(I,J,K)
					
					counter = counter + 1
!					index = index + lattice_dimension

				END DO !I

			END DO	!J

		END DO !K

		destination = ROOT !SEND BUFFERS TO DESTINATION

!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to send global_write_histogram integer array'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to send global_write_histogram integer array'
!	CALL MPI_Send(local_integer_send_buffer,send_size_integer,MPI_INTEGER,destination,tag_integer_communication,cartesian_communicator,mpi_error_code)
!	IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished sending global_write_histogram integer array'
!	IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished sending global_write_histogram integer array'

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to send global_write_histogram real array'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to send global_write_histogram real array'
	CALL MPI_Send(local_real_send_buffer,send_size_real,my_mpi_real_data_type,destination,tag_real_communication,cartesian_communicator,mpi_error_code)
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished sending global_write_histogram real array'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished sending global_write_histogram real array'

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to send global_write_histogram real array'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to send global_write_histogram real array'
	CALL MPI_Send(local_logical_send_buffer,send_size_logical,MPI_LOGICAL,destination,tag_logical_communication,cartesian_communicator,mpi_error_code)
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished sending global_write_histogram real array'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished sending global_write_histogram real array'


!				DEALLOCATE( local_integer_send_buffer )	
				DEALLOCATE ( local_real_send_buffer )
				DEALLOCATE ( local_logical_send_buffer )

	ELSE  !!My_MPI_Process_ID.EQ.ROOT (SO THIS HAPPENS ONLY ON THE ROOT PROCESS)
		
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'Number_Of_Processes - 1',Number_Of_Processes - 1
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'Number_Of_Processes - 1',Number_Of_Processes - 1
		DO MPI_Process_Number = 0, Number_Of_Processes - 1  !CYCLE OVER ALL PROCESSES ON THE ROOT PROCESS
		
			IF(debug) WRITE (0,*) My_MPI_Process_ID,'ROOT',ROOT,'Cycling over MPI_Process_Number',MPI_Process_Number,'of',Number_Of_Processes - 1,'processes'
			IF(debug) WRITE (9,*) My_MPI_Process_ID,'ROOT',ROOT,'Cycling over MPI_Process_Number',MPI_Process_Number,'of',Number_Of_Processes - 1,'processes'
			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
				
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'THIS SHOULD BE ROOT CYCLING OVER NONROOT MPI_Process_Number',MPI_Process_Number
				IF(debug) WRITE (9,*) My_MPI_Process_ID,'THIS SHOULD BE ROOT CYCLING OVER NONROOT MPI_Process_Number',MPI_Process_Number
				
				SOURCE = MPI_Process_Number !ASSIGN CURRENT PROCESS AS "SOURCE" FROM WHICH INFORMATION IS RECEIVED
				
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'ROOT is preparing to recv from SOURCE',SOURCE
				IF(debug) WRITE (9,*) My_MPI_Process_ID,'ROOT is preparing to recv from SOURCE',SOURCE

				root_receive_counts_real(MPI_Process_Number) = ( 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 )
			
				root_receive_counts_logical(MPI_Process_Number) = root_receive_counts_real(MPI_Process_Number)
				
!				root_receive_counts_integer(MPI_Process_Number) = lattice_dimension * root_receive_counts_real(MPI_Process_Number)
			
				IF(debug) WRITE (0,*),'SOURCE',SOURCE,'root_receive_counts_real',root_receive_counts_real(MPI_Process_Number) !,'root_receive_counts_integer',root_receive_counts_integer(MPI_Process_Number)
				IF(debug) WRITE (9,*),'SOURCE',SOURCE,'root_receive_counts_real',root_receive_counts_real(MPI_Process_Number) !,'root_receive_counts_integer',root_receive_counts_integer(MPI_Process_Number)
!				ALLOCATE( local_integer_recv_buffer(root_receive_counts_integer(MPI_Process_Number)) )	
				ALLOCATE ( local_real_recv_buffer(root_receive_counts_real(MPI_Process_Number)) )	
				ALLOCATE ( local_logical_recv_buffer(root_receive_counts_logical(MPI_Process_Number)) )
				
!				local_integer_recv_buffer(:)=0
!				local_real_recv_buffer(:)=0

				!ROOT RECEIVES FROM ALL THE NON-ROOT PROCESSES THEIR BUFFERS
!					IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to recv global_write_histogram integer array'
!					IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to recv global_write_histogram integer array'
!				CALL MPI_Recv( local_integer_recv_buffer,root_receive_counts_integer(MPI_Process_Number),MPI_INTEGER,SOURCE,tag_integer_communication, cartesian_communicator,status,mpi_error_code)
!					IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished recv global_write_histogram integer array'
!					IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished recv global_write_histogram integer array'
			
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to recv global_write_histogram real array'
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to recv global_write_histogram real array'
				CALL MPI_Recv( local_real_recv_buffer,root_receive_counts_real(MPI_Process_Number),my_mpi_real_data_type,SOURCE,tag_real_communication, cartesian_communicator,status,mpi_error_code )
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished recv global_write_histogram real array'
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished recv global_write_histogram real array'

					IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to recv global_write_histogram real array'
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to recv global_write_histogram real array'
				CALL MPI_Recv( local_logical_recv_buffer,root_receive_counts_logical(MPI_Process_Number),MPI_LOGICAL,SOURCE,tag_logical_communication, cartesian_communicator,status,mpi_error_code )
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished recv global_write_histogram real array'
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished recv global_write_histogram real array'

			
				!ROOT WRITES DATA RECIEVED FROM NON-ROOT PROCESSES TO A FILE
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to write 3D_array from process',MPI_Process_Number
				IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to write 3D_array from process',MPI_Process_Number
				COUNTER = 1
				DO index = 1,root_receive_counts_real(MPI_Process_Number)
	

					IF(.NOT.Write_inverse_of_logical_array) THEN !IF YOU don't WANT TO WRITE THE INVERSE OF THE local_logical_array

						IF(local_logical_recv_buffer(COUNTER).AND.write_histogram)	THEN

!							I = local_integer_recv_buffer(index)
!							J = local_integer_recv_buffer(index + 1)
!							K = local_integer_recv_buffer(index + 2)
					
!							WRITE(77,'(3I6.5,E19.10)') I,J,K,local_real_recv_buffer(COUNTER)

!							global_average_x(I) = global_average_x(I) + local_real_recv_buffer(COUNTER)
!							global_average_y(J) = global_average_y(J) + local_real_recv_buffer(COUNTER)
!							global_average_z(K) = global_average_z(K) + local_real_recv_buffer(COUNTER)

!							global_array_average_yz(J,K) = global_array_average_yz(J,K) + local_real_recv_buffer(COUNTER)
							sum_squares_3d_array_deviations = sum_squares_3d_array_deviations + &
							& ( global_3d_array_average - local_real_recv_buffer(COUNTER) ) * ( global_3d_array_average - local_real_recv_buffer(COUNTER) )

							
							IF(local_real_recv_buffer(COUNTER).LE.global_3d_array_max) THEN  !CHECK TO MAKE SURE THAT THE VALUE BELONGS TO NEW BINNING RANGE

								HISTOGRAM_BIN_POINTER = INT( ( local_real_recv_buffer(COUNTER) - global_3d_array_min ) * Inverse_Histogram_Bin_Size ) + 1

								Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1

							ENDIF !!local_3d_array(I,J,K).LE.global_3d_array_max

!						ELSE

!							I = local_integer_recv_buffer(index)
!							J = local_integer_recv_buffer(index + 1)
!							K = local_integer_recv_buffer(index + 2)
						
!							WRITE(77,'(3I6.5,E19.10)') I,J,K,0.0
							!WRITE(77,*) I,J,K,0.0,'non-ROOT',SOURCE


						END IF !! local_logical_recv_buffer(COUNTER).AND.write_histogram
					
					ELSE !!Write_inverse_of_logical_array  !IF YOU WANT TO WRITE THE INVERSE OF THE local_logical_array
					
						IF(.NOT.local_logical_recv_buffer(COUNTER).AND.write_histogram)	THEN

!							I = local_integer_recv_buffer(index)
!							J = local_integer_recv_buffer(index + 1)
!							K = local_integer_recv_buffer(index + 2)
					
!							WRITE(77,'(3I6.5,E19.10)') I,J,K,local_real_recv_buffer(COUNTER)

!							global_average_x(I) = global_average_x(I) + local_real_recv_buffer(COUNTER)
!							global_average_y(J) = global_average_y(J) + local_real_recv_buffer(COUNTER)
!							global_average_z(K) = global_average_z(K) + local_real_recv_buffer(COUNTER)

!							global_array_average_yz(J,K) = global_array_average_yz(J,K) + local_real_recv_buffer(COUNTER)

							sum_squares_3d_array_deviations = sum_squares_3d_array_deviations + &
							& ( global_3d_array_average - local_real_recv_buffer(COUNTER) ) * ( global_3d_array_average - local_real_recv_buffer(COUNTER) )

							
							IF(local_real_recv_buffer(COUNTER).LE.global_3d_array_max) THEN  !CHECK TO MAKE SURE THAT THE VALUE BELONGS TO NEW BINNING RANGE

								HISTOGRAM_BIN_POINTER = INT( ( local_real_recv_buffer(COUNTER) - global_3d_array_min ) * Inverse_Histogram_Bin_Size ) + 1

								Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) = Historam_Bin_Bucket(HISTOGRAM_BIN_POINTER) + 1
							
							ENDIF !!local_3d_array(I,J,K).LE.global_3d_array_max


!						ELSE

!							I = local_integer_recv_buffer(index)
!							J = local_integer_recv_buffer(index + 1)
!							K = local_integer_recv_buffer(index + 2)
						
!							WRITE(77,'(3I6.5,E19.10)') I,J,K,0.0
							!WRITE(77,*) I,J,K,0.0,'non-ROOT:inv',SOURCE

						END IF !! local_logical_recv_buffer(COUNTER)

					ENDIF !!Write_inverse_of_logical_array.AND.write_histogram

					COUNTER = COUNTER + 1
	
				END DO !index
				IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished writing 3D_array from process',MPI_Process_Number
				IF(debug) WRITE (9,*) My_MPI_Process_ID,'finished writing 3D_array from process',MPI_Process_Number
			
!				DEALLOCATE( local_integer_recv_buffer )	
				DEALLOCATE ( local_real_recv_buffer )
				DEALLOCATE ( local_logical_recv_buffer )

				IF(debug) WRITE (0,*) My_MPI_Process_ID,'deallocated 3D_array buffers from process',MPI_Process_Number
				IF(debug) WRITE (9,*) My_MPI_Process_ID,'deallocated 3D_array buffers from process',MPI_Process_Number
	
			ENDIF !!MPI_Process_Number.NE.ROOT
		
		END DO !!MPI_Process_Number
		
		!ROOT CLOSES OPEN (77, FILE = filename//'.txt', STATUS = 'unknown', FORM='formatted')  
		!CLOSE (77)   

	ENDIF !!My_MPI_Process_ID IS ROOT OR NON-ROOT




	
	IF (My_MPI_Process_ID.EQ.ROOT) THEN

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: sum_squares_3d_array_deviations',sum_squares_3d_array_deviations
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: sum_squares_3d_array_deviations',sum_squares_3d_array_deviations

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: global_non_wall_count_real',global_non_wall_count_real
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: global_non_wall_count_real',global_non_wall_count_real

		!CALCULATE STANDARD DEVIATION:
		standard_deviation_of_3d_array = SQRT( sum_squares_3d_array_deviations / REAL(global_non_wall_count_real) )
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: standard_deviation_of_3d_array',standard_deviation_of_3d_array
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'global_write_histogram: standard_deviation_of_3d_array',standard_deviation_of_3d_array
		!======================WRITE HISTOGRAM==========================
		PRINT*,filename//' standard_deviation_of_3d_array = ',standard_deviation_of_3d_array,'[g/cm s2]'

	IF( write_histogram )	THEN 

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to WRITE HISTOGRAM'
		OPEN (66, FILE = filename//'_histogram('//Secondary_Histogram_Bin_Percent//'%).txt', STATUS = 'unknown', form='formatted')  !FILE CONTAINING HISTOGRAM OF 

		DO histogram_bin_number = 1, Total_Number_of_Histogram_Bins
		
			IF(histogram_bin_number.GT.1) THEN 
				
				WRITE(66,*) Histogram_Bin_Range_Values(histogram_bin_number - 1),&
				& Histogram_Bin_Range_Values(histogram_bin_number),&
				& Historam_Bin_Bucket(histogram_bin_number) * Inv_global_non_wall_count_real

			ELSEIF(histogram_bin_number.EQ.1) THEN
				
				WRITE(66,*) global_3d_array_min,Histogram_Bin_Range_Values(histogram_bin_number),&
				& Historam_Bin_Bucket(histogram_bin_number) * Inv_global_non_wall_count_real

			ELSE

				PRINT*, 'global_write_histogram: HISTOGRAM ERROR - SHOULDNT HAPPEN!!!'
				STOP

			ENDIF !!histogram_bin_number.LE.Total_Number_of_Histogram_Bins

		END DO !!histogram_bin_number = 1, Total_Number_of_Histogram_Bins
		
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to CLOSE(66)'

		CLOSE(66)

		DEALLOCATE ( Histogram_Bin_Range_Values )
		DEALLOCATE ( Historam_Bin_Bucket )

	END IF !! write_histogram
		!======================END WRITE HISTOGRAM========================

	END IF !! My_MPI_Process_ID.EQ.ROOT
	!==========================END WRITE THE AVERAGES IF ROOT=======================================



IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to call barrier in SUBROUTINE global_write_histogram'
call MPI_Barrier(cartesian_communicator,mpi_error_code)

IF(debug) WRITE (0,*) My_MPI_Process_ID,'global_write_histogram: about to RETURN FROM SUBROUTINE global_write_histogram'
RETURN
Femi Kadri's avatar
Femi Kadri committed
612
END SUBROUTINE global_write_histogram