LBM_equilibrate_rhovel.F90 41.9 KB
Newer Older
Femi Kadri's avatar
Femi Kadri committed
1 2 3 4 5 6 7 8 9 10 11 12 13
        SUBROUTINE LBM_equilibrate_rhovel(My_MPI_Process_ID,Number_Of_Processes,local_x_min_ghost,local_x_max_ghost,local_y_min_ghost,local_y_max_ghost, &
& local_z_min_ghost,local_z_max_ghost,nx, ny, nz,Q,dt,dx,Forcing_Factor_times_Weight_x,Number_Of_Forced_Directions,Forced_Vectors,f_local,Relaxation_Time_Tau,local_walls, &
& Kinematic_Viscosity,Initial_Density_Rho0,LBM_Step_Number,nsteps,cix,ciy,ciz,Density_Weight,SLIT,Constant_1,Inverse_Square_Root_2, &
& Constant_A, Constant_B, Constant_C,Lattice_Speed_Of_Sound,Inverse_3,Inverse_6,Inverse_8,Inverse_12,Inverse_16,Inverse_24,&
& Inverse_48,Inverse_64,local_x_min_real,local_x_max_real,local_y_min_real,local_y_max_real,local_z_min_real,local_z_max_real,&
& LBM_Print_Results_Incriment,global_product_xyz_nodes_real,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,ROOT,cartesian_communicator,global_fluid_count_x,global_fluid_count_y,global_fluid_count_z,superficial_area,&
& Inv_Relaxation_Time_Tau,Convergence_Percent_Tollerance,global_Ux_max_old,global_Ux_min_old,global_Ux_avg_old,&
& global_Uy_max_old,global_Uy_min_old,global_Uy_avg_old,global_Uz_max_old,global_Uz_min_old,global_Uz_avg_old,&
& dx2,dx3,inverse_dx3,dx_inverse_dt,inv_global_fluid_count_real,Dbl_LBM_Print_Results_Incriment,LBM_currently_converged,&
& LBM_Time_Steps_Prev_Completed,Total_Number_Of_LBM_Steps,local_Ux_max_old,local_Ux_min_old,local_Ux_avg_old,&
& local_Uy_max_old,local_Uy_min_old,local_Uy_avg_old,local_Uz_max_old,local_Uz_min_old,local_Uz_avg_old,&
Femi Kadri's avatar
Femi Kadri committed
14
& Inv_LBM_Print_Results_Incriment,local_Density_Rho_avg,inv_local_non_wall_count_ghost,f_temp,&
Femi Kadri's avatar
Femi Kadri committed
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
& global_fluid_count_xy,global_fluid_count_yz,global_fluid_count_zx,&
& my_mpi_real_data_type) 



USE debug_module
!USE precision_detector

		IMPLICIT NONE

		!-----------INTENT(IN)------------------------------------------
		INTEGER, INTENT(IN) :: My_MPI_Process_ID,Number_Of_Processes,nx, ny, nz, Q, LBM_Step_Number,nsteps,Number_Of_Forced_Directions
		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), DIMENSION(1:Number_Of_Forced_Directions) :: Forced_Vectors
		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) :: LBM_Print_Results_Incriment,global_product_xyz_nodes_real
		INTEGER,INTENT(IN) :: global_x_min_real,global_x_max_real,global_y_min_real,global_y_max_real,global_z_min_real,global_z_max_real
 		INTEGER,INTENT(IN) :: global_x_nodes_real,global_y_nodes_real,global_z_nodes_real
		INTEGER,INTENT(IN),DIMENSION(global_x_min_real:global_x_max_real) :: global_fluid_count_x
		INTEGER,INTENT(IN),DIMENSION(global_y_min_real:global_y_max_real) :: global_fluid_count_y
		INTEGER,INTENT(IN),DIMENSION(global_z_min_real:global_z_max_real) :: global_fluid_count_z
		INTEGER,INTENT(IN) :: ROOT,cartesian_communicator
		INTEGER,INTENT(IN) :: Dbl_LBM_Print_Results_Incriment
		INTEGER,INTENT(IN) :: LBM_Time_Steps_Prev_Completed,Total_Number_Of_LBM_Steps
		INTEGER,INTENT(IN) :: my_mpi_real_data_type 

		REAL,INTENT(IN) :: inv_local_non_wall_count_ghost
		REAL,INTENT(IN), DIMENSION(0:Q) :: cix, ciy,ciz, Density_Weight
		REAL,INTENT(IN), DIMENSION(1:Number_Of_Forced_Directions) :: Forcing_Factor_times_Weight_x
		REAL,INTENT(IN)   :: superficial_area,Inv_Relaxation_Time_Tau
		REAL,INTENT(IN)   :: Convergence_Percent_Tollerance
		REAL,INTENT(IN) :: inv_global_fluid_count_real,Inv_LBM_Print_Results_Incriment
		REAL,INTENT(IN) :: Inverse_3,Inverse_6,Inverse_8,Inverse_12,Inverse_16,Inverse_24,Inverse_48,Inverse_64
		REAL,INTENT(IN) :: Lattice_Speed_Of_Sound,Constant_1,Inverse_Square_Root_2
		REAL,INTENT(IN) :: Constant_A,Constant_B,Constant_C
		REAL,INTENT(IN) :: dx2,dx3,inverse_dx3,dx_inverse_dt
		REAL,INTENT(IN) :: Relaxation_Time_Tau,Kinematic_Viscosity
Femi Kadri's avatar
Femi Kadri committed
52 53 54 55 56 57 58 59
		REAL,INTENT(IN) :: Initial_Density_Rho0,dt,dx

		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
Femi Kadri's avatar
Femi Kadri committed
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

		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         
		LOGICAL*4,INTENT(IN) :: SLIT
		!LOGICAL*4,PARAMETER :: debug = .true.
		LOGICAL*4,PARAMETER :: debug = GLOBAL_DEBUG

		!-----------INTENT(INOUT)-----------------------------------------
		REAL,INTENT(INOUT) :: local_Density_Rho_avg
		LOGICAL,INTENT(INOUT) :: LBM_currently_converged
		REAL, DIMENSION(0:Q,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(INOUT) :: f_local,f_temp

		REAL,INTENT(INOUT) :: global_Ux_max_old,global_Ux_min_old,global_Ux_avg_old
		REAL,INTENT(INOUT) :: global_Uy_max_old,global_Uy_min_old,global_Uy_avg_old
		REAL,INTENT(INOUT) :: global_Uz_max_old,global_Uz_min_old,global_Uz_avg_old


		REAL,INTENT(INOUT) :: local_Ux_max_old,local_Ux_min_old,local_Ux_avg_old
		REAL,INTENT(INOUT) :: local_Uy_max_old,local_Uy_min_old,local_Uy_avg_old
		REAL,INTENT(INOUT) :: local_Uz_max_old,local_Uz_min_old,local_Uz_avg_old

		!-----------INTENT(OUT)-----------------------------------------
 		!-----------NO INTENT------------------------------------------ 
        INTEGER :: Vector_Direction_Number, I, J, K, Forced_Direction_Number, temp
		INTEGER :: local_x_nodes_ghost, local_y_nodes_ghost, local_z_nodes_ghost
		INTEGER :: mpi_error_code,mpi_abort_error
		INTEGER :: NodesPerProc,COUNTER,sdir

		INTEGER,PARAMETER :: local_Ux_max_send_count = 1
		INTEGER,PARAMETER :: local_Ux_min_send_count = 1
		INTEGER,PARAMETER :: local_Ux_avg_send_count = 1

		INTEGER,PARAMETER :: local_Uy_max_send_count = 1
		INTEGER,PARAMETER :: local_Uy_min_send_count = 1
		INTEGER,PARAMETER :: local_Uy_avg_send_count = 1

		INTEGER,PARAMETER :: local_Uz_max_send_count = 1
		INTEGER,PARAMETER :: local_Uz_min_send_count = 1
		INTEGER,PARAMETER :: local_Uz_avg_send_count = 1
		INTEGER :: LBM_Current_Step

		INTEGER,PARAMETER :: memory_success = 0
		INTEGER :: memory_status

		INTEGER,PARAMETER :: LBM_converged_send_count = 1 

!       REAL, ALLOCATABLE :: Constant_2(:,:,:),Ux(:,:,:), Uy(:,:,:),Uz(:,:,:), Density_Rho(:,:,:)
!		REAL, 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) :: Constant_2,Ux, Uy,Uz, Density_Rho
		REAL :: Constant_2,Ux,Uy,Uz, Density_Rho,Density_Gradient

		REAL :: local_Density_Rho_sum

		REAL :: local_Ux_max,global_Ux_max
		REAL :: local_Ux_min,global_Ux_min
		REAL :: local_Ux_avg,global_Ux_avg

		REAL :: local_Uy_max,global_Uy_max
		REAL :: local_Uy_min,global_Uy_min
		REAL :: local_Uy_avg,global_Uy_avg

		REAL :: local_Uz_max,global_Uz_max
		REAL :: local_Uz_min,global_Uz_min
		REAL :: local_Uz_avg,global_Uz_avg
!		REAL, DIMENSION(0:Number_Of_Processes-1) :: local_Ux_max
!		REAL, DIMENSION(NX*NY*NZ) :: UxGather

		REAL :: delta_u, C_Usq, G,Inverse_Density_Rho
		REAL :: LBM_Percent_Convergence_Ux_max,LBM_Percent_Convergence_Ux_min,LBM_Percent_Convergence_Ux_avg
		REAL :: LBM_Percent_Convergence_Uy_max,LBM_Percent_Convergence_Uy_min,LBM_Percent_Convergence_Uy_avg
		REAL :: LBM_Percent_Convergence_Uz_max,LBM_Percent_Convergence_Uz_min,LBM_Percent_Convergence_Uz_avg

		!REAL,DIMENSION(:,:,:),ALLOCATABLE :: local_density_rho,local_velocity_x,local_velocity_y,local_velocity_z
		REAL,DIMENSION(:,:,:),ALLOCATABLE :: local_velocity_x
Femi Kadri's avatar
Femi Kadri committed
135
		LOGICAL :: ISNAN
Femi Kadri's avatar
Femi Kadri committed
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
        

INCLUDE "mpif.h"

IF(debug) WRITE (0,*) My_MPI_Process_ID,"entered SUBROUTINE LBM_equilibrate_rhovel LBM_Step_Number",LBM_Step_Number
IF(debug) WRITE (9,*) My_MPI_Process_ID,"entered SUBROUTINE LBM_equilibrate_rhovel LBM_Step_Number",LBM_Step_Number

!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)

local_Density_Rho_sum = 0.0

!print*,'Inv_Relaxation_Time_Tau',Inv_Relaxation_Time_Tau

!local_x_nodes_ghost = (local_x_max_ghost - local_x_min_ghost) - 1 !MINUS TWO GHOST COLUMNS
!local_y_nodes_ghost = (local_y_max_ghost - local_y_min_ghost) + 1
!local_z_nodes_ghost = (local_z_max_ghost - local_z_min_ghost) + 1
!NodesPerProc = local_x_nodes_ghost * local_y_nodes_ghost * local_z_nodes_ghost
!delta_U = Relaxation_Time_Tau * 8.0 * Kinematic_Viscosity * umax / ( float (ny-2))**2



!THESE CONSTANTS ARE INITIALIZED IN THE MAIN PROGRAM IN ORDER TO AVOID REDUNDANT CALCULATIONS
!Lattice_Speed_Of_Sound = sqrt(Lattice_Speed_Of_Sound_Squared)  !square root of characteristic 'speed of sound' of lattice
!Constant_1 = (1.0 - (1.0 / Relaxation_Time_Tau)) !; print*,Constant_1
!Constant_A = 1.0 / Lattice_Speed_Of_Sound_Squared
!Constant_B = 1.0 / ( 2.0 * Lattice_Speed_Of_Sound_Squared * Lattice_Speed_Of_Sound_Squared )
!Constant_C = - 1.0 / (2.0 * Lattice_Speed_Of_Sound_Squared )

LBM_Current_Step = LBM_Step_Number + LBM_Time_Steps_Prev_Completed


	IF( MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.&
	& LBM_Current_Step.EQ.2 ) THEN !IF LBM_Step_Number IS A MULTIPLE OF LBM_Print_Results_Incriment

!		ALLOCATE( local_density_rho(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
!		& local_z_min_ghost:local_z_max_ghost),STAT=memory_status )

!		IF (memory_status /= memory_success) THEN
!			WRITE (0,*) My_MPI_Process_ID, &
!		&     "ERROR: can't allocate local_density_rho of dimensions ", &
!		&     local_x_min_ghost, ":", local_x_max_ghost, ",", &
!		&     local_y_min_ghost, ":", local_y_max_ghost, ",", &
!		&     local_z_min_ghost, ":", local_z_max_ghost, ")"
!			CALL MPI_Abort(cartesian_communicator,mpi_abort_error,mpi_error_code)
!		END IF !! (memory_status /= memory_success)

		ALLOCATE( local_velocity_x(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
		& local_z_min_ghost:local_z_max_ghost),STAT=memory_status )

		IF (memory_status /= memory_success) THEN
			WRITE (0,*) My_MPI_Process_ID, &
		&     "ERROR: can't allocate local_velocity_x of dimensions ", &
		&     local_x_min_ghost, ":", local_x_max_ghost, ",", &
		&     local_y_min_ghost, ":", local_y_max_ghost, ",", &
		&     local_z_min_ghost, ":", local_z_max_ghost, ")"
			CALL MPI_Abort(cartesian_communicator,mpi_abort_error,mpi_error_code)
		END IF !! (memory_status /= memory_success)

		local_velocity_x(:,:,:)  = 0.0

	ENDIF !!MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.LBM_Current_Step.EQ.2

!		ALLOCATE( local_velocity_y(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
!		& local_z_min_ghost:local_z_max_ghost),STAT=memory_status )
!
!		IF (memory_status /= memory_success) THEN
!			WRITE (0,*) My_MPI_Process_ID, &
!		&     "ERROR: can't allocate local_velocity_y of dimensions ", &
!		&     local_x_min_ghost, ":", local_x_max_ghost, ",", &
!		&     local_y_min_ghost, ":", local_y_max_ghost, ",", &
!		&     local_z_min_ghost, ":", local_z_max_ghost, ")"
!			CALL MPI_Abort(cartesian_communicator,mpi_abort_error,mpi_error_code)
!		END IF !! (memory_status /= memory_success)

!		ALLOCATE( local_velocity_z(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,&
!		& local_z_min_ghost:local_z_max_ghost),STAT=memory_status )

!		IF (memory_status /= memory_success) THEN
!			WRITE (0,*) My_MPI_Process_ID, &
!		&     "ERROR: can't allocate local_velocity_z of dimensions ", &
!		&     local_x_min_ghost, ":", local_x_max_ghost, ",", &
!		&     local_y_min_ghost, ":", local_y_max_ghost, ",", &
!		&     local_z_min_ghost, ":", local_z_max_ghost, ")"
!			CALL MPI_Abort(cartesian_communicator,mpi_abort_error,mpi_error_code)
!		END IF !! (memory_status /= memory_success)

!		IF( MOD(LBM_Step_Number,LBM_Print_Results_Incriment).EQ.0.OR.(LBM_Step_Number.EQ.1.AND.LBM_Current_Step.GT.2) ) THEN
		
!			local_density_rho(:,:,:) = 0.0

!			local_velocity_x(:,:,:)  = 0.0
!			local_velocity_y(:,:,:)  = 0.0
!			local_velocity_z(:,:,:)  = 0.0

!		ENDIF !!MOD(LBM_Step_Number,LBM_Print_Results_Incriment).EQ.0

		local_Ux_max = 0.0
		global_Ux_max = 0.0

		local_Ux_min = 99999.99  !some large number used for initial value
		global_Ux_min = 99999.99 !some large number used for initial value

		local_Ux_avg = 0.0 
		global_Ux_avg = 0.0 

		local_Uy_max = 0.0
		global_Uy_max = 0.0

		local_Uy_min = 99999.99  !some large number used for initial value
		global_Uy_min = 99999.99 !some large number used for initial value

		local_Uy_avg = 0.0 
		global_Uy_avg = 0.0 

		local_Uz_max = 0.0
		global_Uz_max = 0.0

		local_Uz_min = 99999.99  !some large number used for initial value
		global_Uz_min = 99999.99 !some large number used for initial value

		local_Uz_avg = 0.0 
		global_Uz_avg = 0.0 

	


DO K = local_z_min_ghost,local_z_max_ghost	!DO K=1,1

	DO J = local_y_min_ghost,local_y_max_ghost	!DO J=1,1

		DO I = local_x_min_ghost,local_x_max_ghost	!DO I=1,1

			Density_Gradient = 0.0
			Ux  = 0.0
			Uy  = 0.0
			Uz  = 0.0

			IF(.NOT.local_walls(I,J,K)) THEN
			
			
				!~~~~~~~~~~~~~VELOCITY&DENSITY (EQN14-15)~~~~~~~~~~~~		

				DO Vector_Direction_Number = 0, Q
					!IF(debug) WRITE (29,*) Vector_Direction_Number,I,J,K,f_local(Vector_Direction_Number,I,J,K)
		
					Density_Gradient = Density_Gradient + f_local(Vector_Direction_Number,I,J,K)
					Ux  = Ux + Cix(Vector_Direction_Number) * f_local(Vector_Direction_Number,I,J,K)
					Uy  = Uy + Ciy(Vector_Direction_Number) * f_local(Vector_Direction_Number,I,J,K)
					Uz  = Uz + Ciz(Vector_Direction_Number) * f_local(Vector_Direction_Number,I,J,K)
			
				ENDDO !! Q

				Density_Rho = Density_Gradient + Initial_Density_Rho0 
				
				local_Density_Rho_sum = local_Density_Rho_sum + Density_Rho

				Inverse_Density_Rho = 1.0 / Density_Rho

				Ux = Ux * Inverse_Density_Rho
				Uy = Uy * Inverse_Density_Rho
				Uz = Uz * Inverse_Density_Rho

				!IF(debug.and.LBM_Step_Number.eq.2) WRITE (69,*) LBM_Step_Number,"LBM_equilibrate_rhovel: Ux",Ux

			
			!~~~~~~~~~~~~~END VELOCITY&DENSITY~~~~~~~~~~~~~~~~~~~		

				Constant_2 = (Density_Rho * Inv_Relaxation_Time_Tau) !define a constant to save time on redundant divison
				
				!			Ux=Ux + delta_U

				!======= THE "REST" VECTOR (Q=0) EQN 16 - 17 =========================
				
				C_Usq = Constant_C * ( Ux * Ux + Uy * Uy + Uz * Uz )  !DEFINE VELOCITY SQUARED TO SAVE TIME ON REDUNANT COMPUTATION
				
				!f_local(0,I,J,K) = Constant_1 * f_local(0,I,J,K) + Density_Weight(0) * Constant_2 * (1.0 + C_Usq )
				
				!this is modified to avoid roundoff error Phys. Rev. E 48, 4823 (1993) and J. Comput. Phys., 190, 351-370
Femi Kadri's avatar
Femi Kadri committed
314
				f_local(0,I,J,K) = Constant_1 * f_local(0,I,J,K) + Density_Weight(0) * Constant_2 * ( C_Usq ) + &
Femi Kadri's avatar
Femi Kadri committed
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 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
				& Density_Weight(0) * Density_Gradient * Inv_Relaxation_Time_Tau
				f_temp(0,I,J,K) = f_local(0,I,J,K)
			
				!======= END - THE "REST" VECTOR (Q=0) ===================

				DO Vector_Direction_Number = 1, Q  !CYCLE THROUGH THE "NON-REST" VECTOR DIRECTIONS (1 TO Q) EQN 16 - 17
       							
					G = (Cix(Vector_Direction_Number) * Ux + Ciy(Vector_Direction_Number) * Uy + &
					& Ciz(Vector_Direction_Number) * Uz )  !DEFINE DOT PRODUCT TO SAVE TIME ON REDUNDANT COMPUTATION

					!f_local(Vector_Direction_Number,I,J,K) = Constant_1 * f_local(Vector_Direction_Number,I,J,K) + &
					!& Density_Weight(Vector_Direction_Number) * Constant_2 * (1.0 + Constant_A * G + Constant_B * G * G + &
					!& C_Usq )

					!this is modified to avoid roundoff error Phys. Rev. E 48, 4823 (1993) and J. Comput. Phys., 190, 351-370
					f_local(Vector_Direction_Number,I,J,K) = Constant_1 * f_local(Vector_Direction_Number,I,J,K) + &
					& Density_Weight(Vector_Direction_Number) * Constant_2 * ( Constant_A * G + Constant_B * G * G + &
					& C_Usq ) + Density_Weight(Vector_Direction_Number) * Density_Gradient * Inv_Relaxation_Time_Tau


				END DO !! Vector_Direction_Number = 1, Q


				!~~~~~~~~~~~~~~~~FORCING ~~~~~~~~~~~~~~~~~~~
				DO Forced_Direction_Number = 1, Number_Of_Forced_Directions
			
					f_local(Forced_Vectors(Forced_Direction_Number),I,J,K) = &
					&  f_local(Forced_Vectors(Forced_Direction_Number),I,J,K) + &
					& Forcing_Factor_times_Weight_x(Forced_Direction_Number)

				END DO
				!~~~~~~~~~~~~~~~~END FORCING ~~~~~~~~~~~~~~~~~~~

				!			Ux=Ux - delta_U


					IF (K.GE.local_z_min_real.AND.K.LE.local_z_max_real) THEN

						IF (J.GE.local_y_min_real.AND.J.LE.local_y_max_real) THEN
				
							IF (I.GE.local_x_min_real.AND.I.LE.local_x_max_real) THEN
						
								Ux = Ux * (dx_inverse_dt)   !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
								Uy = Uy * (dx_inverse_dt)   !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
								Uz = Uz * (dx_inverse_dt)   !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]

								IF(Ux.GT.local_Ux_max) local_Ux_max = Ux ! * (dx_inverse_dt)
								IF(Ux.LT.local_Ux_min) local_Ux_min = Ux ! * (dx_inverse_dt)
								local_Ux_avg = local_Ux_avg + Ux ! * (dx_inverse_dt)

								IF(Uy.GT.local_Uy_max) local_Uy_max = Uy ! * (dx_inverse_dt)
								IF(Uy.LT.local_Uy_min) local_Uy_min = Uy ! * (dx_inverse_dt)
								local_Uy_avg = local_Uy_avg + Uy ! * (dx_inverse_dt)

								IF(Uz.GT.local_Uz_max) local_Uz_max = Uz ! * (dx_inverse_dt)
								IF(Uz.LT.local_Uz_min) local_Uz_min = Uz ! * (dx_inverse_dt)
								local_Uz_avg = local_Uz_avg + Uz ! * (dx_inverse_dt)

								!IF( MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.&
								!& (LBM_Current_Step.EQ.2) ) THEN
								!	local_density_rho(I,J,K) = Density_Rho * inverse_dx3  !UNIT CONVERSION BACK TO PHYSICAL UNITS [g/cm3]				

								IF( MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.&
								& LBM_Current_Step.EQ.2 ) THEN

									local_velocity_x(I,J,K)  = Ux !* (dx_inverse_dt) !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
								!	local_velocity_y(I,J,K)  = Uy !* (dx_inverse_dt) !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
								!	local_velocity_z(I,J,K)  = Uz !* (dx_inverse_dt) !UNIT CONVERSION BACK TO PHYSICAL UNITS [cm/s]
								
								ENDIF !!MOD(LBM_Step_Number,LBM_Print_Results_Incriment).EQ.0.OR.&
			
							ENDIF !!I.GE.local_x_min_real.AND.I.LE.local_x_max_real
				
						ENDIF !!J.GE.local_y_min_real.AND.J.LE.local_y_max_real
				 
					ENDIF !!K.GE.local_z_min_real.AND.K.LE.local_z_max_real

				
	


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

		END DO !I

	END DO	!J

END DO !K
	
	!accumulate local_U X,Y AND Z; MIN, MAX AND  AVG VALUES over LBM_Print_Results_Incriment

	local_Density_Rho_avg = local_Density_Rho_sum * inv_local_non_wall_count_ghost

	local_Ux_max_old = local_Ux_max_old + local_Ux_max
	local_Ux_min_old = local_Ux_min_old + local_Ux_min
	local_Ux_avg_old = local_Ux_avg_old + local_Ux_avg
	local_Uy_max_old = local_Uy_max_old + local_Uy_max
	local_Uy_min_old = local_Uy_min_old + local_Uy_min
	local_Uy_avg_old = local_Uy_avg_old + local_Uy_avg
	local_Uz_max_old = local_Uz_max_old + local_Uz_max
	local_Uz_min_old = local_Uz_min_old + local_Uz_min
	local_Uz_avg_old = local_Uz_avg_old + local_Uz_avg



IF(debug) THEN

WRITE (29,*) '================post equil:NONWALLS==================='

DO K = local_z_min_ghost,local_z_max_ghost	!DO K=1,1

	DO J = local_y_min_ghost,local_y_max_ghost	!DO J=1,1

		DO I = local_x_min_ghost,local_x_max_ghost	!DO I=1,1

				WRITE(29,*) I,J,K,local_walls(I,J,K)

			IF(.NOT.local_walls(I,J,K)) THEN
			
			
				!~~~~~~~~~~~~~VELOCITY&DENSITY (EQN14-15)~~~~~~~~~~~~		

				DO Vector_Direction_Number = 0, Q

					IF(debug) WRITE (29,*) Vector_Direction_Number,I,J,K,f_local(Vector_Direction_Number,I,J,K)
		
				ENDDO !! Q
				;endif
enddo;enddo;enddo


WRITE (67,*) '================post equil: WALLS==================='

DO K = local_z_min_ghost,local_z_max_ghost	!DO K=1,1

	DO J = local_y_min_ghost,local_y_max_ghost	!DO J=1,1

		DO I = local_x_min_ghost,local_x_max_ghost	!DO I=1,1

			WRITE(29,*) I,J,K,local_walls(I,J,K)

			IF(local_walls(I,J,K)) THEN
			
			
				!~~~~~~~~~~~~~VELOCITY&DENSITY (EQN14-15)~~~~~~~~~~~~		

				DO Vector_Direction_Number = 0, Q

					IF(debug) WRITE (67,*) Vector_Direction_Number,I,J,K,f_local(Vector_Direction_Number,I,J,K)
		
				ENDDO !! Q
				;endif
enddo;enddo;enddo

ENDIF !! DEBUG

	

	IF( MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.&
	& LBM_Current_Step.EQ.2) THEN !IF LBM_Step_Number IS A MULTIPLE OF LBM_Print_Results_Incriment
	
		!average local_U X,Y AND Z; MIN, MAX AND  AVG VALUES over LBM_Print_Results_Incriment
		local_Ux_max_old = local_Ux_max_old * Inv_LBM_Print_Results_Incriment
		local_Ux_min_old = local_Ux_min_old * Inv_LBM_Print_Results_Incriment
		local_Ux_avg_old = local_Ux_avg_old * Inv_LBM_Print_Results_Incriment
		local_Uy_max_old = local_Uy_max_old * Inv_LBM_Print_Results_Incriment
		local_Uy_min_old = local_Uy_min_old * Inv_LBM_Print_Results_Incriment
		local_Uy_avg_old = local_Uy_avg_old * Inv_LBM_Print_Results_Incriment
		local_Uz_max_old = local_Uz_max_old * Inv_LBM_Print_Results_Incriment
		local_Uz_min_old = local_Uz_min_old * Inv_LBM_Print_Results_Incriment
		local_Uz_avg_old = local_Uz_avg_old * Inv_LBM_Print_Results_Incriment

			
			!====================================== COMMUNICATE Ux VALUES ===========================
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_max_old",local_Ux_max_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_min_old",local_Ux_min_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_avg_old",local_Ux_avg_old

			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_max_old",local_Ux_max_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_min_old",local_Ux_min_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Ux_avg_old",local_Ux_avg_old

			

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_max, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Ux_max_old,global_Ux_max,local_Ux_max_send_count,my_mpi_real_data_type,MPI_MAX,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (9,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:xferred global_Ux_max",global_Ux_max
			IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:xferred global_Ux_max",global_Ux_max

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_max, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_min, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Ux_min_old,global_Ux_min,local_Ux_min_send_count,my_mpi_real_data_type,MPI_MIN,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_min, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Ux_avg, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Ux_avg_old,global_Ux_avg,local_Ux_avg_send_count,my_mpi_real_data_type,MPI_SUM,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Ux_avg, step",LBM_Step_Number

			!====================================== COMMUNICATE Uy VALUES ===========================
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_max_old",local_Uy_max_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_min_old",local_Uy_min_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_avg_old",local_Uy_avg_old

			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_max_old",local_Uy_max_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_min_old",local_Uy_min_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uy_avg_old",local_Uy_avg_old


			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_max, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uy_max_old,global_Uy_max,local_Uy_max_send_count,my_mpi_real_data_type,MPI_MAX,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_max, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_min, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uy_min_old,global_Uy_min,local_Uy_min_send_count,my_mpi_real_data_type,MPI_MIN,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_min, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uy_avg, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uy_avg_old,global_Uy_avg,local_Uy_avg_send_count,my_mpi_real_data_type,MPI_SUM,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uy_avg, step",LBM_Step_Number
			
			!====================================== COMMUNICATE Uz VALUES ===========================
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_max_old",local_Uz_max_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_min_old",local_Uz_min_old
			IF(debug) WRITE (0,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_avg_old",local_Uz_avg_old

			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_max_old",local_Uz_max_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_min_old",local_Uz_min_old
			IF(debug) WRITE (9,*) LBM_Step_Number,"LBM_equilibrate_rhovel:before local_Uz_avg_old",local_Uz_avg_old

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_max, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uz_max_old,global_Uz_max,local_Uz_max_send_count,my_mpi_real_data_type,MPI_MAX,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_max, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_max, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_min, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uz_min_old,global_Uz_min,local_Uz_min_send_count,my_mpi_real_data_type,MPI_MIN,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_min, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_min, step",LBM_Step_Number

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"about to CALL MPI_Reduce: global_Uz_avg, step",LBM_Step_Number
		
			CALL MPI_Reduce(local_Uz_avg_old,global_Uz_avg,local_Uz_avg_send_count,my_mpi_real_data_type,MPI_SUM,ROOT,&
			& cartesian_communicator,mpi_error_code)

			IF(debug) WRITE (0,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_avg, step",LBM_Step_Number
			IF(debug) WRITE (9,*) My_MPI_Process_ID,"finished CALLing MPI_Reduce: global_Uz_avg, step",LBM_Step_Number
			!FINISHED COMMUNICATING local_U X,Y AND Z; MIN, MAX AND  AVG VALUES BETWEEN PROCESSORS

			!ZERO OLD VALUES FOR THE NEXT INCRIMENT

				local_Ux_max_old = 0.0
				local_Ux_min_old = 0.0
				local_Ux_avg_old = 0.0

				local_Uy_max_old = 0.0
				local_Uy_min_old = 0.0
				local_Uy_avg_old = 0.0

				local_Uz_max_old = 0.0
				local_Uz_min_old = 0.0
				local_Uz_avg_old = 0.0
			
			

!				IF(debug) PRINT*,LBM_Step_Number,'global_Ux_avg',global_Ux_avg,global_Ux_avg * inv_global_fluid_count_real
				


				
!				IF(debug) PRINT*,'inv_global_fluid_count_real',inv_global_fluid_count_real,1.0/inv_global_fluid_count_real
				IF(My_MPI_Process_ID.EQ.ROOT) THEN			
			
					global_Ux_avg = global_Ux_avg * inv_global_fluid_count_real
					IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Ux_avg",global_Ux_avg
					global_Uy_avg = global_Uy_avg * inv_global_fluid_count_real
					IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uy_avg",global_Uy_avg
					global_Uz_avg = global_Uz_avg * inv_global_fluid_count_real
					IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uz_avg",global_Uz_avg

				ENDIF !!My_MPI_Process_ID.EQ.ROOT

				IF( LBM_Current_Step.GT.2.AND.(MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0) ) THEN !IGNORE THE ZEROETH AND FIRST LBM_Print_Results_Incriment, BECAUSE THEY ARE MEANINGLESS
					
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to CALL global_write_3D_avg: local_velocity_x'
	
!					CALL global_write_3D_avg(local_velocity_x,My_MPI_Process_ID,local_x_min_ghost,local_x_max_ghost,local_y_min_ghost,local_y_max_ghost,&
!					& local_z_min_ghost,local_z_max_ghost,global_product_xyz_nodes_real,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,local_z_min_real,local_z_max_real,&
!					& local_y_min_real,local_y_max_real,local_x_min_real,local_x_max_real,local_walls,ROOT,&
!					& cartesian_communicator,.FALSE.,.TRUE.,dx2,superficial_area,35,'results_LBM/global_velocity_field_x')
		
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'finished CALLing global_write_3D_avg: local_velocity_x'
	

					IF(My_MPI_Process_ID.EQ.ROOT) THEN

						IF(global_Ux_max_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Ux_max = 100.0 * ( global_Ux_max - global_Ux_max_old ) / global_Ux_max_old
				
						ELSE

							LBM_Percent_Convergence_Ux_max = 100.0

						ENDIF !!global_Ux_max_old.GT.0
				
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Ux_max",LBM_Percent_Convergence_Ux_max
						
						IF(global_Ux_min_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
					
							LBM_Percent_Convergence_Ux_min = 100.0 * ( global_Ux_min - global_Ux_min_old ) / global_Ux_min_old
						
						ELSE
						
							LBM_Percent_Convergence_Ux_min = 100.0
						
						ENDIF !!global_Ux_min_old.GT.0	
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Ux_min",LBM_Percent_Convergence_Ux_min
						
						IF(global_Ux_avg_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Ux_avg = 100.0 * ( global_Ux_avg - global_Ux_avg_old ) / global_Ux_avg_old
						
						ELSE
						
							LBM_Percent_Convergence_Ux_avg = 100.0
						
						ENDIF !!global_Ux_avg_old.GT.0
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Ux_avg",LBM_Percent_Convergence_Ux_avg

						IF(global_Uy_max_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Uy_max = 100.0 * ( global_Uy_max - global_Uy_max_old ) / global_Uy_max_old
						
						ELSE

							LBM_Percent_Convergence_Uy_max = 100.0

						ENDIF !!global_Uy_max_old.GT.0
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uy_max",LBM_Percent_Convergence_Uy_max
						
						IF(global_Uy_min_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
						
							LBM_Percent_Convergence_Uy_min = 100.0 * ( global_Uy_min - global_Uy_min_old ) / global_Uy_min_old
						
						ELSE

							LBM_Percent_Convergence_Uy_min = 100.0

						ENDIF !!global_Uy_min_old.GT.0
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uy_min",LBM_Percent_Convergence_Uy_min
						
						IF(global_Uy_avg_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
								
							LBM_Percent_Convergence_Uy_avg = 100.0 * ( global_Uy_avg - global_Uy_avg_old ) / global_Uy_avg_old
						
						ELSE

							LBM_Percent_Convergence_Uy_avg = 100.0

						ENDIF !!global_Uy_avg_old.GT.0

						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uy_avg",LBM_Percent_Convergence_Uy_avg
	
						IF(global_Uz_max_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Uz_max = 100.0 * ( global_Uz_max - global_Uz_max_old ) / global_Uz_max_old
						
						ELSE 
							
							LBM_Percent_Convergence_Uz_max = 100.0

						ENDIF !!global_Uz_max_old.GT.0

						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uz_max",LBM_Percent_Convergence_Uz_max
						
						IF(global_Uz_min_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Uz_min = 100.0 * ( global_Uz_min - global_Uz_min_old ) / global_Uz_min_old
						
						ELSE 
							
							LBM_Percent_Convergence_Uz_min = 100.0

						ENDIF !!global_Uz_min_old.GT.0
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uz_min",LBM_Percent_Convergence_Uz_min
						
						IF(global_Uz_avg_old.GT.0) THEN !TO AVOID DIVISION BY ZERO
							
							LBM_Percent_Convergence_Uz_avg = 100.0 * ( global_Uz_avg - global_Uz_avg_old ) / global_Uz_avg_old
						
						ELSE
							
							LBM_Percent_Convergence_Uz_avg = 100.0

						ENDIF !!global_Uz_avg_old.GT.0
						
						IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: LBM_Percent_Convergence_Uz_avg",LBM_Percent_Convergence_Uz_avg

						WRITE(10,'(I6.5,3E19.10)') LBM_Current_Step,global_Ux_max_old,global_Ux_max,LBM_Percent_Convergence_Ux_max  !ROOT WRITES TO FILE = 'results_LBM/global_Ux_max.txt'
						WRITE(11,'(I6.5,3E19.10)') LBM_Current_Step,global_Ux_min_old,global_Ux_min,LBM_Percent_Convergence_Ux_min  !ROOT WRITES TO FILE = 'results_LBM/global_Ux_min.txt'
						WRITE(12,'(I6.5,3E19.10)') LBM_Current_Step,global_Ux_avg_old,global_Ux_avg,LBM_Percent_Convergence_Ux_avg  !ROOT WRITES TO FILE = 'results_LBM/global_Ux_avg.txt'
	
						WRITE(14,'(I6.5,3E19.10)') LBM_Current_Step,global_Uy_max_old,global_Uy_max,LBM_Percent_Convergence_Uy_max  !ROOT WRITES TO FILE = 'results_LBM/global_Uy_max.txt'
						WRITE(15,'(I6.5,3E19.10)') LBM_Current_Step,global_Uy_min_old,global_Uy_min,LBM_Percent_Convergence_Uy_min  !ROOT WRITES TO FILE = 'results_LBM/global_Uy_min.txt'
						WRITE(16,'(I6.5,3E19.10)') LBM_Current_Step,global_Uy_avg_old,global_Uy_avg,LBM_Percent_Convergence_Uy_avg  !ROOT WRITES TO FILE = 'results_LBM/global_Uy_avg.txt'


						WRITE(17,'(I6.5,3E19.10)') LBM_Current_Step,global_Uz_max_old,global_Uz_max,LBM_Percent_Convergence_Uz_max  !ROOT WRITES TO FILE = 'results_LBM/global_Uz_max.txt'
						WRITE(18,'(I6.5,3E19.10)') LBM_Current_Step,global_Uz_min_old,global_Uz_min,LBM_Percent_Convergence_Uz_min  !ROOT WRITES TO FILE = 'results_LBM/global_Uz_min.txt'
						WRITE(20,'(I6.5,3E19.10)') LBM_Current_Step,global_Uz_avg_old,global_Uz_avg,LBM_Percent_Convergence_Uz_avg  !ROOT WRITES TO FILE = 'results_LBM/global_Uz_avg.txt'


						!CHECK FOR DIVERGENCE (INFINITY) AND STOP IF OCCURED
Femi Kadri's avatar
Femi Kadri committed
775 776 777
						IF (  ISNAN(global_Ux_max).OR.ISNAN(global_Ux_avg).OR.ISNAN(global_Ux_min) &
						& .OR.ISNAN(global_Uy_max).OR.ISNAN(global_Uy_avg).OR.ISNAN(global_Uy_min) &
						& .OR.ISNAN(global_Uz_max).OR.ISNAN(global_Uz_avg).OR.ISNAN(global_Uz_min) ) THEN 
Femi Kadri's avatar
Femi Kadri committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
							
							PRINT*,      'LBM_equilibrate_rhovel ERROR:  SIMULATION DIVERGED (TRY CHANGING TAU OR REDUCING Forcing_Factor_x, SUCH THAT Re <10)' 
							WRITE (0,*)  'LBM_equilibrate_rhovel ERROR:  SIMULATION DIVERGED (TRY CHANGING TAU OR REDUCING Forcing_Factor_x, SUCH THAT Re <10)' 
							WRITE (9,*)  'LBM_equilibrate_rhovel ERROR:  SIMULATION DIVERGED (TRY CHANGING TAU OR REDUCING Forcing_Factor_x, SUCH THAT Re <10)' 
							WRITE (29,*) 'LBM_equilibrate_rhovel ERROR:  SIMULATION DIVERGED (TRY CHANGING TAU OR REDUCING Forcing_Factor_x, SUCH THAT Re <10)' 

							STOP
							
						ENDIF 

						IF(  MAX( ABS(LBM_Percent_Convergence_Ux_max),ABS(LBM_Percent_Convergence_Ux_avg) )&
						& .LE.Convergence_Percent_Tollerance  ) THEN
	!					& ABS(LBM_Percent_Convergence_Uy_max),ABS(LBM_Percent_Convergence_Uy_min),ABS(LBM_Percent_Convergence_Uy_avg),&
	!					& ABS(LBM_Percent_Convergence_Uz_max),ABS(LBM_Percent_Convergence_Uz_min),ABS(LBM_Percent_Convergence_Uz_avg)  ).LE.Convergence_Percent_Tollerance  ) THEN
					
							LBM_currently_converged = .TRUE.
							IF(debug) WRITE (0,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: about to OPEN (21, FILE = LBM_previously_converged.txt'
							IF(debug) WRITE (9,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: about to OPEN (21, FILE = LBM_previously_converged.txt'
							PRINT*,'LBM_currently_converged',LBM_currently_converged
							OPEN (21, FILE = 'LBM_previously_converged.txt',ACCESS = 'SEQUENTIAL', STATUS = 'unknown')
							WRITE (21,*) .TRUE.  !!LET THE NEXT RUN KNOW THAT THE PREVIOUS RUN HAS CONVERGED
							CLOSE (21) !FILE = 'LBM_previously_converged.txt', STATUS = 'unknown')
							IF(debug) WRITE (0,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: CLOSED (21, FILE = LBM_previously_converged.txt'
							IF(debug) WRITE (9,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: CLOSED (21, FILE = LBM_previously_converged.txt'
						ENDIF !! (...).LE.Convergence_Percent_Tollerance

						!MAKE A FILE FOR A QUICK DETECTION OF CONVERSION BY AN EXTERNAL SCRIPT
						IF(LBM_currently_converged) THEN
						
							OPEN (13, FILE = 'LBM_currently_converged.dat', STATUS = 'unknown')
							CLOSE (13)

						ENDIF !!	 LBM_currently_converged 

					ENDIF !!My_MPI_Process_ID.EQ.ROOT
					
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: about to CALL MPI_Bcast LBM_Step_Number',LBM_Step_Number
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: about to CALL MPI_Bcast LBM_Step_Number',LBM_Step_Number
					CALL MPI_Bcast ( LBM_currently_converged,LBM_converged_send_count,MPI_LOGICAL,ROOT,cartesian_communicator,mpi_error_code )
					IF(debug) WRITE (0,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: finished to CALL MPI_Bcast LBM_Step_Number',LBM_Step_Number
					IF(debug) WRITE (9,*) My_MPI_Process_ID,'LBM_equilibrate_rhovel: finished to CALL MPI_Bcast LBM_Step_Number',LBM_Step_Number

				ENDIF !!LBM_Current_Step.GT.2.AND.(MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0)




			IF(My_MPI_Process_ID.EQ.ROOT) THEN


				IF(debug) WRITE (9,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:before global_Ux_max_old",global_Ux_max
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:before global_Ux_max_old",global_Ux_max

				IF(debug) WRITE (9,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:before global_Ux_max_old",global_Ux_max_old
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:before global_Ux_max_old",global_Ux_max_old
			
				global_Ux_max_old = global_Ux_max
				IF(debug) WRITE (9,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:after global_Ux_max_old",global_Ux_max
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:after global_Ux_max_old",global_Ux_max

				IF(debug) WRITE (9,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:after global_Ux_max_old",global_Ux_max_old
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel:after global_Ux_max_old",global_Ux_max_old
		
				global_Ux_min_old = global_Ux_min
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Ux_min_old",global_Ux_min_old



				global_Ux_avg_old = global_Ux_avg
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Ux_avg_old",global_Ux_avg_old

				global_Uy_max_old = global_Uy_max
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uy_max_old",global_Uy_max_old
				global_Uy_min_old = global_Uy_min
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uy_min_old",global_Uy_min_old
				global_Uy_avg_old = global_Uy_avg
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uy_avg_old",global_Uy_avg_old

				global_Uz_max_old = global_Uz_max
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uz_max_old",global_Uz_max_old
				global_Uz_min_old = global_Uz_min
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uz_min_old",global_Uz_min_old
				global_Uz_avg_old = global_Uz_avg
				IF(debug) WRITE (0,*) LBM_Step_Number,"ROOT LBM_equilibrate_rhovel: global_Uz_avg_old",global_Uz_avg_old

			ENDIF !!My_MPI_Process_ID.EQ.ROOT


!		DEALLOCATE( local_density_rho )
		DEALLOCATE( local_velocity_x )
!		DEALLOCATE( local_velocity_y )
!		DEALLOCATE( local_velocity_z )

	ENDIF !!MOD(LBM_Current_Step,LBM_Print_Results_Incriment).EQ.0.OR.(LBM_Current_Step.EQ.2)

IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to END SUBROUTINE LBM_equilibrate_rhovel'
IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to END SUBROUTINE LBM_equilibrate_rhovel'
        RETURN
        END SUBROUTINE LBM_equilibrate_rhovel