MAIN_initialize_state.F90 56.8 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 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 775 776 777 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 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
SUBROUTINE MAIN_initialize_state(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,Number_Of_Shifts,Forcing_Factor_times_Weight_x,Number_Of_Forced_Directions,Forced_Vectors,Initial_Density_Rho0,cix,ciy,ciz,Density_Weight, &
& Mirror_Shift_Direction,GEOMETRY_sphere_cubic_array,GEOMETRY_plates,GEOMETRY_square_duct,GEOMETRY_pipe,local_x_nodes_ghost,local_y_nodes_ghost,local_z_nodes_ghost,global_fluid_count_x,global_fluid_count_y,global_fluid_count_z, &
& local_x_min_real,local_x_max_real,local_y_min_real,local_y_max_real,local_z_min_real,local_z_max_real,global_x_min_real, &
& global_x_max_real,global_x_min_ghost,global_x_max_ghost,global_y_min_real,global_y_max_real,global_y_min_ghost, &
& global_y_max_ghost,global_z_min_real,global_z_max_real,global_z_min_ghost,global_z_max_ghost,divide_x,divide_y,divide_z, &
& local_walls,Resume_Simulation,ROOT,global_product_xyz_nodes_real,cartesian_communicator,IN,OUT,EAST,WEST,UP,DOWN,&
& local_x_nodes_real,local_y_nodes_real,local_z_nodes_real,local_fluid_surface,lattice_dimension,local_product_xyz_nodes_real,&
& global_fluid_surface_count_x,global_fluid_surface_count_y,global_fluid_surface_count_z,&
& global_inv_fluid_surface_count_x,global_inv_fluid_surface_count_y,global_inv_fluid_surface_count_z,&
& all_processes_local_bounds,dx,pipe_radius,&
& global_fluid_count_real,local_product_xyz_nodes_ghost,LBM_Time_Steps_Prev_Completed,Number_Of_LBM_Prev_Run_Digits,&
& global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,&
& global_x_nodes_ghost,global_y_nodes_ghost,global_z_nodes_ghost,&
& Print_Results_MPI_ID,My_MPI_Process_ID_Digits,LBM_previously_converged,wrote_HARD_resume_files,skip_LBM,&
& half_of_moving_shift_directions,f_local,xyz_dir_normal_to_release_plane,&
& global_plane_dim1_min_ghost,global_plane_dim1_max_ghost,&
& global_plane_dim2_min_ghost,global_plane_dim2_max_ghost,&
& global_plane_dim3_min_ghost,global_plane_dim3_max_ghost,&
& global_plane_dim1_min_real,global_plane_dim1_max_real,&
& global_plane_dim2_min_real,global_plane_dim2_max_real,&
& global_plane_dim3_min_real,global_plane_dim3_max_real,&
& global_walls_surf_rel_dim1_min,global_walls_surf_rel_dim1_max,&
& LST_release_plane_position_lu,&
& global_plane_dim1_nodes_ghost,&
& global_plane_dim2_nodes_ghost,&
& global_plane_dim3_nodes_ghost,&
& global_plane_dim1_nodes_real,&
& global_plane_dim2_nodes_real,&
& global_plane_dim3_nodes_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,&
& global_inv_fluid_surf_count_xy,&
& global_inv_fluid_surf_count_yz,&
& global_inv_fluid_surf_count_zx,&
& global_fluid_surface_count_xy,&
& global_fluid_surface_count_yz,&
& global_fluid_surface_count_zx,&
& GEOMETRY_semi_pipe_duct,&
& GEOMETRY_comb,&
& GEOMETRY_fiber_layers,&
& GEOMETRY_narrowing_slit,&
& GEOMETRY_backwards_step,&
& Forcing_Factor_x,&
& write_HARD_resume_files,&
& LST_apply_volume_user_mask,&
& Forcing_Factor_Weights_x,&
& NON_2_VOL_MASK_fluid_count_ratio )

!This subroutine is arranged as follows:
!1)Define some constants to be used
!2)Define parameters characteristic of the lattice to be used
!3)Create solid geometry
!4)Calculate initial density distribution functions

!USE LBM_global_array_pointers_module
USE debug_module
USE precision_detector


		IMPLICIT NONE

		!-----------INTENT(IN)------------------------------------------
		INTEGER,INTENT(IN) :: My_MPI_Process_ID,Number_Of_Processes
		INTEGER,INTENT(IN) :: local_x_min_ghost,local_x_max_ghost
		INTEGER,INTENT(IN) :: local_y_min_ghost,local_y_max_ghost
		INTEGER,INTENT(IN) :: local_z_min_ghost,local_z_max_ghost
		INTEGER,INTENT(IN) :: nx, ny, nz
		INTEGER,INTENT(IN) :: Q,Number_Of_Shifts,Number_Of_Forced_Directions,ROOT,global_product_xyz_nodes_real
		INTEGER,INTENT(IN) :: local_x_nodes_ghost,local_y_nodes_ghost,local_z_nodes_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
		INTEGER,INTENT(IN) :: global_x_min_real,global_x_max_real,global_x_min_ghost,global_x_max_ghost !GLOBAL BOUNDS X
		INTEGER,INTENT(IN) :: global_y_min_real,global_y_max_real,global_y_min_ghost,global_y_max_ghost !GLOBAL BOUNDS Y
		INTEGER,INTENT(IN) :: global_z_min_real,global_z_max_real,global_z_min_ghost,global_z_max_ghost !GLOBAL BOUNDS Z
		INTEGER,INTENT(IN) :: global_x_nodes_real,global_y_nodes_real,global_z_nodes_real
		INTEGER,INTENT(IN) :: global_x_nodes_ghost,global_y_nodes_ghost,global_z_nodes_ghost
		INTEGER,INTENT(IN) :: cartesian_communicator,IN,OUT,EAST,WEST,UP,DOWN,local_product_xyz_nodes_real
		INTEGER,INTENT(IN) :: local_x_nodes_real,local_y_nodes_real,local_z_nodes_real,lattice_dimension
		INTEGER,INTENT(IN) :: local_product_xyz_nodes_ghost
		INTEGER,INTENT(IN) :: LBM_Time_Steps_Prev_Completed
		INTEGER,INTENT(IN) :: half_of_moving_shift_directions
		INTEGER,INTENT(IN) :: xyz_dir_normal_to_release_plane
		!INTEGER,INTENT(IN) :: Number_Of_LBM_Prev_Run_Digits
		INTEGER,INTENT(IN) :: Number_Of_LBM_Prev_Run_Digits,My_MPI_Process_ID_Digits
		INTEGER,INTENT(IN),DIMENSION(6 * Number_Of_Processes) :: all_processes_local_bounds
		REAL,INTENT(IN)    :: Initial_Density_Rho0,dx,pipe_radius
		REAL,INTENT(IN)    :: Forcing_Factor_x
		REAL,INTENT(IN) :: LST_release_plane_position_lu
		LOGICAL*4,INTENT(IN) :: LBM_previously_converged,wrote_HARD_resume_files
		LOGICAL*4,INTENT(IN) :: GEOMETRY_semi_pipe_duct,GEOMETRY_narrowing_slit
		LOGICAL*4,INTENT(IN) :: GEOMETRY_backwards_step
		LOGICAL*4,INTENT(IN) :: GEOMETRY_comb,GEOMETRY_sphere_cubic_array,GEOMETRY_fiber_layers
		LOGICAL*4,INTENT(IN) :: write_HARD_resume_files,LST_apply_volume_user_mask
		
		LOGICAL*4,INTENT(IN) :: GEOMETRY_plates,GEOMETRY_square_duct,GEOMETRY_pipe
		LOGICAL*4,INTENT(IN) :: divide_x,divide_y,divide_z,Resume_Simulation,skip_LBM
		LOGICAL*4,PARAMETER :: debug = GLOBAL_DEBUG
		!LOGICAL*4,PARAMETER :: debug = .true.
		
		!-----------INTENT(OUT)-----------------------------------------
        !INTEGER,INTENT(OUT), DIMENSION(1:Number_Of_Shifts) ::Shift_Polarity, Shift_Direction, Shift_Dimension, Mirror_Shift_Direction
		INTEGER,INTENT(OUT), DIMENSION(1:Q) :: Mirror_Shift_Direction
		INTEGER,INTENT(OUT), DIMENSION(1:Number_Of_Forced_Directions) :: Forced_Vectors
		INTEGER,INTENT(OUT) :: global_fluid_count_real

		INTEGER,INTENT(OUT) :: global_plane_dim1_min_ghost,global_plane_dim1_max_ghost
		INTEGER,INTENT(OUT) :: global_plane_dim2_min_ghost,global_plane_dim2_max_ghost
		INTEGER,INTENT(OUT) :: global_plane_dim3_min_ghost,global_plane_dim3_max_ghost

		INTEGER,INTENT(OUT) :: global_plane_dim1_min_real,global_plane_dim1_max_real
		INTEGER,INTENT(OUT) :: global_plane_dim2_min_real,global_plane_dim2_max_real
		INTEGER,INTENT(OUT) :: global_plane_dim3_min_real,global_plane_dim3_max_real
		INTEGER,INTENT(OUT) :: global_walls_surf_rel_dim1_min,global_walls_surf_rel_dim1_max

		INTEGER,INTENT(OUT) :: global_plane_dim1_nodes_ghost
		INTEGER,INTENT(OUT) :: global_plane_dim2_nodes_ghost
		INTEGER,INTENT(OUT) :: global_plane_dim3_nodes_ghost
		INTEGER,INTENT(OUT) :: global_plane_dim1_nodes_real 
		INTEGER,INTENT(OUT) :: global_plane_dim2_nodes_real 
		INTEGER,INTENT(OUT) :: global_plane_dim3_nodes_real 

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

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

		INTEGER,INTENT(OUT),DIMENSION(global_x_min_real:global_x_max_real,&
		& global_y_min_real:global_y_max_real) :: global_fluid_surface_count_xy
		INTEGER,INTENT(OUT),DIMENSION(global_y_min_real:global_y_max_real,&
		& global_z_min_real:global_z_max_real) :: global_fluid_surface_count_yz
		INTEGER,INTENT(OUT),DIMENSION(global_z_min_real:global_z_max_real,&
		& global_x_min_real:global_x_max_real) :: global_fluid_surface_count_zx

		INTEGER,INTENT(OUT),DIMENSION(global_x_min_real:global_x_max_real,&
		& global_y_min_real:global_y_max_real) :: global_inv_fluid_surf_count_xy
		INTEGER,INTENT(OUT),DIMENSION(global_y_min_real:global_y_max_real,&
		& global_z_min_real:global_z_max_real) :: global_inv_fluid_surf_count_yz
		INTEGER,INTENT(OUT),DIMENSION(global_z_min_real:global_z_max_real,&
		& global_x_min_real:global_x_max_real) :: global_inv_fluid_surf_count_zx

		REAL,INTENT(OUT) :: NON_2_VOL_MASK_fluid_count_ratio

		REAL,INTENT(OUT), DIMENSION(1:Number_Of_Forced_Directions) :: Forcing_Factor_times_Weight_x
		REAL,INTENT(OUT), DIMENSION(1:Number_Of_Forced_Directions) :: Forcing_Factor_Weights_x

		INTEGER,INTENT(OUT), DIMENSION(global_x_min_real:global_x_max_real) :: global_fluid_surface_count_x,&
		& global_inv_fluid_surface_count_x
		INTEGER,INTENT(OUT), DIMENSION(global_y_min_real:global_y_max_real) :: global_fluid_surface_count_y,&
		& global_inv_fluid_surface_count_y
		INTEGER,INTENT(OUT), DIMENSION(global_z_min_real:global_z_max_real) :: global_fluid_surface_count_z,&
		& global_inv_fluid_surface_count_z

		INTEGER, DIMENSION(global_x_min_real:global_x_max_real) :: global_wall_count_x,&
		& global_fluid_count_x
		INTEGER, DIMENSION(global_y_min_real:global_y_max_real) :: global_wall_count_y,&
		& global_fluid_count_y
		INTEGER, DIMENSION(global_z_min_real:global_z_max_real) :: global_wall_count_z,&
		& global_fluid_count_z

		REAL, INTENT(OUT), DIMENSION(0:Q) :: cix, ciy,ciz, Density_Weight
		REAL, INTENT(OUT), 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) :: f_local

		LOGICAL*4,INTENT(OUT),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_walls,local_fluid_surface

		!		REAL, INTENT(OUT),ALLOCATABLE  :: f_local(:,:,:,:)
		!		LOGICAL*4,INTENT(OUT),ALLOCATABLE  :: global_walls(:,:,:),local_fluid_surface(:,:,:)
		!-----------NO INTENT------------------------------------------

		INTEGER,PARAMETER :: x = 1
		INTEGER,PARAMETER :: y = 2
		INTEGER,PARAMETER :: z = 3
		INTEGER,PARAMETER :: Negative =  1
		INTEGER,PARAMETER :: Positive = -1
		INTEGER :: TEMP
		INTEGER :: Vector_Direction_Number,sdir,I,J,K, NEIGHBORS,L,M,N
		INTEGER :: global_walls_count_real
		INTEGER :: my_mpi_real_data_type

		INTEGER :: mpi_abort_error,mpi_error_code
	
!		INTEGER :: global_solid_surface_any_count	

!		REAL :: global_numbr_of_interface_faces


!		REAL, ALLOCATABLE  :: f_global(:,:,:,:)
		
		REAL,PARAMETER :: zero = 0.0
		REAL,PARAMETER :: one = 1.0
		REAL,PARAMETER :: Inverse_3  = 1.0 / 3.0  
		REAL,PARAMETER :: Inverse_6  = 1.0 / 6.0
		REAL,PARAMETER :: Inverse_9  = 1.0 / 9.0
		REAL,PARAMETER :: Inverse_12 = 1.0 / 12.0
		REAL,PARAMETER :: Inverse_18 = 1.0 / 18.0
		REAL,PARAMETER :: Inverse_24 = 1.0 / 24.0 
		REAL,PARAMETER :: Inverse_72 = 1.0 / 72.0

		REAL,PARAMETER :: Two_Nineths = 2.0 / 9.0
		
	
		LOGICAL :: file_3d_reconstruction_exists

		LOGICAL :: write_global_walls
		LOGICAL*4,DIMENSION(:,:,:),ALLOCATABLE :: LBM_global_walls_release_plane

		CHARACTER(LEN = Number_Of_LBM_Prev_Run_Digits) Print_LBM_Prev_Run_Step_Number
		CHARACTER(LEN = My_MPI_Process_ID_Digits) Print_Results_MPI_ID
		CHARACTER(LEN = 103) LBM_Previous_Run_Format_String
	
		CHARACTER FLUID*7
		CHARACTER SOLID*6
!		CHARACTER*2 id


		WRITE (0,*) My_MPI_Process_ID,'entered SUBROUTINE MAIN_initialize_state'
		WRITE (9,*) My_MPI_Process_ID,'entered SUBROUTINE MAIN_initialize_state'


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)




!===========2)ASSIGN DIRECTIONS/FORCING FACTORS AND Density_Rho FRACTIONS PERTINENT TO A PARTICULAR LATTICE===============
IF (Q.EQ.8) THEN  !D2Q9
cix = (/ 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, -1.0, -1.0, 1.0/)
ciy = (/ 0.0, 0.0, 1.0, 0.0, -1.0, 1.0, 1.0, -1.0, -1.0/)
ciz = (/ 0.0, 0.0, 0.0, 0.0,  0.0, 0.0, 0.0,  0.0,  0.0/)
Density_Weight =(/ 0.44444444444, 0.1111111111, 0.1111111111, 0.1111111111, &
        & 0.1111111111, 0.0277777778,0.0277777778,0.0277777778,0.0277777778/)
!Shift_Polarity = (/Positive,Positive,Negative,Negative,Positive,Positive,Positive,Negative,Negative,Negative,Positive,Negative/)
!Shift_Dimension =(/x,y,x,y,x,y,y,x,x,y,x,y/)
!Shift_Direction       =(/1,2,3,4,5,5,6,6,7,7,8,8/)
Mirror_Shift_Direction =(/3,4,1,2,7,8,5,6/)


Forcing_Factor_Weights_x = (/Inverse_3,Inverse_12,Inverse_12,-Inverse_3,-Inverse_12,-Inverse_12/)
Forcing_Factor_times_Weight_x = Forcing_Factor_x * Forcing_Factor_Weights_x 

!Forcing_Factor_times_Weight_x = Forcing_Factor_x * (/Inverse_3,Inverse_12,Inverse_12,-Inverse_3,-Inverse_12,-Inverse_12/)


Forced_Vectors = (/1,5,8,3,6,7/)

ELSEIF(Q.EQ.14) THEN !D3Q15
!cix = (/ 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 1.0,-1.0, 1.0, -1.0, -1.0, 1.0 /)
!ciy = (/ 0., 0., 0., 1., -1., 0., 0., 1., -1., 1.,-1., -1., 1., 1., -1. /)
!ciz = (/ 0., 0., 0., 0., 0., 1., -1., 1., -1., -1.,1., 1., -1., 1., -1. /)

cix(0) = zero; ciy(0) = zero; ciz(0) = zero; 

cix(1) = -one; ciy(1) = zero; ciz(1) = zero; 
cix(2) = zero; ciy(2) = -one; ciz(2) = zero; 
cix(3) = zero; ciy(3) = zero; ciz(3) = -one;
 
cix(4) = -one; ciy(4) = -one; ciz(4) = -one; 
cix(5) = -one; ciy(5) = -one; ciz(5) =  one; 
cix(6) = -one; ciy(6) =  one; ciz(6) = -one; 
cix(7) = -one; ciy(7) =  one; ciz(7) =  one; 

cix(8) = - cix(8 - half_of_moving_shift_directions); 
ciy(8) = - ciy(8 - half_of_moving_shift_directions); 
ciz(8) = - ciz(8 - half_of_moving_shift_directions);

cix(9) = - cix(9 - half_of_moving_shift_directions); 
ciy(9) = - ciy(9 - half_of_moving_shift_directions); 
ciz(9) = - ciz(9 - half_of_moving_shift_directions);

cix(10) = - cix(10 - half_of_moving_shift_directions); 
ciy(10) = - ciy(10 - half_of_moving_shift_directions); 
ciz(10) = - ciz(10 - half_of_moving_shift_directions);

cix(11) = - cix(11 - half_of_moving_shift_directions); 
ciy(11) = - ciy(11 - half_of_moving_shift_directions); 
ciz(11) = - ciz(11 - half_of_moving_shift_directions);

cix(12) = - cix(12 - half_of_moving_shift_directions); 
ciy(12) = - ciy(12 - half_of_moving_shift_directions); 
ciz(12) = - ciz(12 - half_of_moving_shift_directions);

cix(13) = - cix(13 - half_of_moving_shift_directions); 
ciy(13) = - ciy(13 - half_of_moving_shift_directions); 
ciz(13) = - ciz(13 - half_of_moving_shift_directions);

cix(14) = - cix(14 - half_of_moving_shift_directions); 
ciy(14) = - ciy(14 - half_of_moving_shift_directions); 
ciz(14) = - ciz(14 - half_of_moving_shift_directions);


!Density_Weight = (/ 0.2222222222, 0.1111111111, 0.1111111111, 0.1111111111, &
!     &              0.1111111111, 0.1111111111, 0.1111111111, 0.0138888889,   &
!     &              0.0138888889, 0.0138888889, 0.0138888889, 0.0138888889,   &
!     &              0.0138888889, 0.0138888889, 0.0138888889 /)

Density_Weight = (/ Two_Nineths,&
				  & Inverse_9, Inverse_9, Inverse_9, &
				  & Inverse_72,Inverse_72,Inverse_72,Inverse_72, &
				  & Inverse_9, Inverse_9, Inverse_9, &
				  & Inverse_72, Inverse_72,Inverse_72,Inverse_72 /)


!Shift_Direction       = (/1,2,3,4,5,6,7,7,7,8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13,13,14,14,14/)
!Shift_Direction       = (/1,2,3,4,5,6,7,8,9,10,11,12,13,14/)
!Mirror_Shift_Direction = (/2,1,4,3,6,5,8,7,10,9,12,11,14,13/)  !this is the mirror image of the streaming directions used for bounce back BC
Mirror_Shift_Direction = (/8,9,10,11,12,13,14,1,2,3,4,5,6,7/)

!Shift_Polarity = (/Positive,Negative,Positive,Negative,Positive,Negative,Positive,Positive,Positive,Negative, &
! & Negative,Negative,Positive,Positive,Negative,Negative,Negative,Positive,Positive,Negative,Positive,Negative, &
! & Positive,Negative,Negative,Positive,Positive,Positive,Negative,Negative /)
!Shift_Dimension =(/x,x,y,y,z,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z /)

!Forcing_Factor_times_Weight_x = Forcing_Factor_x * (/Inverse_3,-Inverse_3,Inverse_24,-Inverse_24,Inverse_24,-Inverse_24,Inverse_24,-Inverse_24,-Inverse_24,Inverse_24/)

Forcing_Factor_Weights_x = (/-Inverse_3,-Inverse_24,-Inverse_24,-Inverse_24,-Inverse_24,&
	                     & Inverse_3, Inverse_24, Inverse_24, Inverse_24, Inverse_24/)

Forcing_Factor_times_Weight_x = Forcing_Factor_x * Forcing_Factor_Weights_x 

!Forcing_Factor_times_Weight_x = Forcing_Factor_x * (/-Inverse_3,-Inverse_24,-Inverse_24,-Inverse_24,-Inverse_24,&
!	                     & Inverse_3, Inverse_24, Inverse_24, Inverse_24, Inverse_24/)

!Forced_Vectors = (/1,2,7,8,9,10,11,12,13,14/)
Forced_Vectors = (/1,4,5,6,7,8,11,12,13,14/)

ELSEIF(Q.EQ.18) THEN !D3Q19
cix = (/0.0,1.0,0.0,0.0,-1.0,0.0,0.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,0.0,0.0,0.0,0.0 /)
ciy = (/0.0,0.0,1.0,0.0,0.0,-1.0,0.0,1.0,1.0,-1.0,-1.0,0.0,0.0,0.0,0.0,1.0,-1.0,1.0,-1.0 /)
ciz = (/0.0,0.0,0.0,1.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0 /)
Density_Weight = (/0.3333333333,0.0555555556,0.0555555556,0.0555555556,0.0555555556,0.0555555556,0.0555555556,0.0277777778 &
 & ,0.0277777778,0.0277777778,0.0277777778,0.0277777778,0.0277777778,0.0277777778,0.0277777778,0.0277777778,0.0277777778 &
 & ,0.0277777778,0.0277777778/)


!Shift_Direction       = (/1,2,3,4,5,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15,16,16,17,17,18,18/)
Mirror_Shift_Direction = (/4,5,6,1,2,3,10,9,8,7,14,13,12,11,18,17,16,15/)  !this is the mirror image of the streaming directions used for bounce back BC

!Shift_Polarity = (/Positive,Positive,Positive,Negative,Negative,Negative,Positive,Positive,Negative,Positive, &
! & Positive,Negative,Negative,Negative,Positive,Positive,Negative,Positive,Positive,Negative,Negative,Negative, &
! & Positive,Positive,Negative,Positive,Positive,Negative,Negative,Negative/)
!Shift_Dimension =(/x,y,z,x,y,z,x,y,x,y,x,y,x,y,x,z,x,z,x,z,x,z,y,z,y,z,y,z,y,z /)

Forcing_Factor_Weights_x = (/Inverse_6,-Inverse_6,Inverse_12,-Inverse_12,Inverse_12,&
& -Inverse_12,Inverse_12,-Inverse_12,Inverse_12,-Inverse_12/)

Forcing_Factor_times_Weight_x = Forcing_Factor_x * Forcing_Factor_Weights_x
!Forcing_Factor_times_Weight_x = Forcing_Factor_x * (/Inverse_6,-Inverse_6,Inverse_12,-Inverse_12,Inverse_12,-Inverse_12,Inverse_12,-Inverse_12,Inverse_12,-Inverse_12/)
Forced_Vectors = (/1,4,7,8,9,10,11,12,13,14/)


ELSEIF(Q.EQ.26) THEN !D3Q27
cix = (/0.0,1.0,0.0,0.0,-1.0,0.0,0.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,0.0,0.0,0.0,0.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0 /)
ciy = (/0.0,0.0,1.0,0.0,0.0,-1.0,0.0,1.0,1.0,-1.0,-1.0,0.0,0.0,0.0,0.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0 /)
ciz = (/0.0,0.0,0.0,1.0,0.0,0.0,-1.0,0.0,0.0,0.0,0.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0 /)
Density_Weight = (/0.2962962963,0.0740740741,0.0740740741,0.0740740741,0.0740740741,0.0740740741,0.0740740741,0.0185185185, &
& 0.0185185185,0.0185185185,0.0185185185,0.0185185185,0.0185185185,0.0185185185,0.0185185185,0.0185185185,0.0185185185, &
& 0.0185185185,0.0185185185,0.0046296296,0.0046296296,0.0046296296,0.0046296296,0.0046296296,0.0046296296,0.0046296296, &
0.0046296296/)


!Shift_Direction       = (/1,2,3,4,5,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13, &
! & 14,14,15,15,16,16,17,17,18,18,19,19,19,20,20,20,21,21,21,22,22,22, &
! & 23,23,23,24,24,24,25,25,25,26,26,26/)
Mirror_Shift_Direction = (/4,5,6,1,2,3,10,9,8,7,14,13,12,11,18,17,16,15,20,19,22,21,24,23,26,25/)  !this is the mirror image of the streaming directions used for bounce back BC

!Shift_Polarity = (/Positive,Positive,Positive,Negative,Negative,Negative,Positive,Positive,Negative,Positive, &
! & Positive,Negative,Negative,Negative,Positive,Positive,Negative,Positive,Positive,Negative,Negative,Negative, &
! & Positive,Positive,Negative,Positive,Positive,Negative,Negative,Negative, &
! & Positive,Positive,Positive,Negative,Negative,Negative,Positive,Positive,Negative,Negative,Negative, &
! &Positive,Positive,Negative,Positive,Negative,Positive,Negative,Negative,Positive,Positive,Positive,Negative,Negative/)
!Shift_Dimension =(/x,y,z,x,y,z,x,y,x,y,x,y,x,y,x,z,x,z,x,z,x,z,y,z,y,z,y,z, &
! &y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z,x,y,z /)

Forcing_Factor_Weights_x = (/2.0*Inverse_9,-2.0*Inverse_9,Inverse_18,-Inverse_18,Inverse_18,-Inverse_18,Inverse_18, &
 &-Inverse_18,Inverse_18,-Inverse_18,Inverse_72,-Inverse_72,Inverse_72,-Inverse_72,Inverse_72,-Inverse_72,-Inverse_72,&
 & Inverse_72/)

Forcing_Factor_times_Weight_x = Forcing_Factor_x * Forcing_Factor_Weights_x
!Forcing_Factor_times_Weight_x = Forcing_Factor_x * (/2.0*Inverse_9,-2.0*Inverse_9,Inverse_18,-Inverse_18,Inverse_18,-Inverse_18,Inverse_18, &
! &-Inverse_18,Inverse_18,-Inverse_18,Inverse_72,-Inverse_72,Inverse_72,-Inverse_72,Inverse_72,-Inverse_72,-Inverse_72,Inverse_72/)

Forced_Vectors = (/1,4,7,8,9,10,11,12,13,14,19,20,21,22,23,24,25,26/)

	ENDIF !!Q

			IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: Initial_Density_Rho0',Initial_Density_Rho0
			IF(debug) WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: Initial_Density_Rho0',Initial_Density_Rho0
	IF(debug) THEN
	
		OPEN (31, FILE = 'debug/Density_Weight_'//Print_Results_MPI_ID//'.txt', STATUS = 'unknown')
		WRITE(31,*) 'MAIN_initialize_state: Initial_Density_Rho0', Initial_Density_Rho0
		WRITE(31,*)
		WRITE(31,*) 'Vector_Direction_Number:','	Density_Weight:'
		DO Vector_Direction_Number = 0, Q
		 	
			WRITE(31,*) Vector_Direction_Number, Density_Weight(Vector_Direction_Number)   
		
		ENDDO !! Vector_Direction_Number 
		
		CLOSE(31)
	ENDIF !!DEBUG

!=====================================END====================================================

!==============================CREATE SOLID GEOMETRY=========================================

!INITIALIZE ALL NODES TO FLUID
local_walls(:,:,:)=.false.

!IF(My_MPI_Process_ID.EQ.ROOT) THEN

	IF (GEOMETRY_backwards_step) THEN	!MAKE SOLID GEOMETRY_plates IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_backwards_step'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_backwards_step'

		CALL INIT_GEOMETRY_backwards_step(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& 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,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_backwards_step'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_backwards_step'
	
	END IF !!GEOMETRY_backwards_step

	

	IF (GEOMETRY_comb) THEN	!MAKE SOLID GEOMETRY_comb IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_comb'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_comb'

		CALL INIT_GEOMETRY_comb(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& 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,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_comb'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_comb'
	
	END IF !!GEOMETRY_comb

	IF ( GEOMETRY_fiber_layers ) THEN


		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_fiber_layers_MAIN'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_fiber_layers_MAIN'

		CALL INIT_GEOMETRY_fiber_layers_MAIN(&
		& ROOT,&
		& cartesian_communicator,&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& 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,&
		& local_x_min_real,&
		& local_x_max_real,&
		& local_y_min_real,&
		& local_y_max_real,&
		& local_z_min_real,&
		& local_z_max_real,&
		& divide_x,&
		& divide_y,&
		& divide_z,&
		& local_x_nodes_ghost,&
		& local_y_nodes_ghost,&
		& local_z_nodes_ghost,&
		& local_x_nodes_real,&
		& local_y_nodes_real,&
		& local_z_nodes_real,&
		& IN,&
		& OUT,&
		& WEST,&
		& EAST,&
		& UP,&
		& DOWN,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_fiber_layers_MAIN'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_fiber_layers_MAIN'


	ENDIF !! GEOMETRY_fiber_layers

	IF (GEOMETRY_plates) THEN	!MAKE SOLID GEOMETRY_plates IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_slit'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_slit'

		CALL INIT_GEOMETRY_slit(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& 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,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_slit'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_slit'
	
	END IF !!GEOMETRY_plates


	IF (GEOMETRY_narrowing_slit) THEN	!MAKE SOLID GEOMETRY_narrowing_slit IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_narrowing_slit'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_narrowing_slit'

		CALL INIT_GEOMETRY_narrowing_slit(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&		
		& 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,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_narrowing_slit'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_narrowing_slit'
	
	END IF !!GEOMETRY_narrowing_slit



	IF (GEOMETRY_square_duct) THEN	!MAKE SOLID GEOMETRY_square_duct IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_square_duct'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_square_duct'

		CALL INIT_GEOMETRY_square_duct(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& 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,&
		& local_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_square_duct'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_square_duct'
	
	END IF !!GEOMETRY_square_duct

	IF (GEOMETRY_pipe) THEN	!MAKE SOLID GEOMETRY_pipe IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_pipe'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_pipe'

		CALL INIT_GEOMETRY_pipe(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& dx,pipe_radius,&
		& My_MPI_Process_ID,&
		& global_x_nodes_real,&
		& global_y_nodes_real,&
		& global_z_nodes_real,&
		& 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_walls)
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_pipe'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_pipe'
	
	END IF !!GEOMETRY_pipe


	IF (GEOMETRY_semi_pipe_duct) THEN	!MAKE SOLID GEOMETRY_semi_pipe_duct IF REQUESTED BY USER
		
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call GEOMETRY_semi_pipe_duct'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call GEOMETRY_semi_pipe_duct'
		CALL INIT_GEOMETRY_semi_pipe_duct(&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& dx,pipe_radius,&
		& My_MPI_Process_ID,&
		& global_x_nodes_real,&
		& global_y_nodes_real,&
		& global_z_nodes_real,&
		& 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_walls)
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling GEOMETRY_semi_pipe_duct'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling GEOMETRY_semi_pipe_duct'
	
	END IF !!GEOMETRY_pipe

	!MAKE SOLID SPHERES IF REQUESTED BY USER
	IF (GEOMETRY_sphere_cubic_array) THEN
	
		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_cubic_sphere_array'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call INIT_GEOMETRY_cubic_sphere_array'

		CALL INIT_GEOMETRY_cubic_sphere_array(&
		& My_MPI_Process_ID,&
		& ROOT,&
		& cartesian_communicator,&
		& nx, ny, nz,&
		& global_x_min_real,&
		& global_x_max_real,&
		& global_x_min_ghost,&
		& global_x_max_ghost,&
		& global_y_min_real,&
		& global_y_max_real,&
		& global_y_min_ghost,&
		& global_y_max_ghost,&
		& global_z_min_real,&
		& global_z_max_real,&
		& global_z_min_ghost,&
		& global_z_max_ghost,&
		& 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_walls)

		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_cubic_sphere_array'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling INIT_GEOMETRY_cubic_sphere_array'

	ENDIF !(GEOMETRY_sphere_cubic_array.gt.0) 

!ENDIF !!My_MPI_Process_ID.EQ.ROOT

!CALL MPI_Barrier(cartesian_communicator,mpi_error_code)  !NOBODY CAN READ THE FILE BEFORE IT IS WRITTEN(IF ROOT IS TOLD TO WRITE IT)

	
	IF ( GEOMETRY_backwards_step.OR.&
	& GEOMETRY_comb.OR.&
	& GEOMETRY_plates.OR.&
	& GEOMETRY_square_duct.OR.&
	& GEOMETRY_pipe.OR.&
	& GEOMETRY_semi_pipe_duct.OR.&
	& GEOMETRY_sphere_cubic_array.OR.&
	& GEOMETRY_fiber_layers.OR.&
	& GEOMETRY_narrowing_slit ) THEN
		
		write_global_walls = .TRUE.

	ELSE

		write_global_walls = .FALSE.

	ENDIF !!
	
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: &
	& write_global_walls',write_global_walls
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: &
	& write_global_walls',write_global_walls

	!WRITE THE 3D GEOMETRY
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: &
	& about to call global_write_3D_logical_array: local_walls'
		
	IF( write_global_walls ) CALL global_write_3D_logical_array(local_walls,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,&
	& Number_Of_Processes,cartesian_communicator,lattice_dimension,20,'results/global_walls')
		
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: &
	&   finished calling global_write_3D_logical_array: local_walls'




	!DETERMINE WALL OR SURFACE MIN AND MAX THAT CORRESPOND TO SLICES THAT PRECEED AND SUPERSEED THE RELEASE PLANE
	global_walls_surf_rel_dim1_min = FLOOR(LST_release_plane_position_lu)
	global_walls_surf_rel_dim1_max = FLOOR(LST_release_plane_position_lu) + 1


	IF(xyz_dir_normal_to_release_plane.EQ.X) THEN
	!SELECT APPROPRIATE BOUNDS FOR LBM_global_walls_release_plane AND FOR LBM_global_fluid_surf_rel_plane depending on orientation of release plane

		global_plane_dim1_min_ghost = global_x_min_ghost
		global_plane_dim1_max_ghost = global_x_max_ghost

		global_plane_dim2_min_ghost = global_y_min_ghost
		global_plane_dim2_max_ghost = global_y_max_ghost

		global_plane_dim3_min_ghost = global_z_min_ghost
		global_plane_dim3_max_ghost = global_z_max_ghost


		global_plane_dim1_min_real = global_x_min_real
		global_plane_dim1_max_real = global_x_max_real

		global_plane_dim2_min_real = global_y_min_real
		global_plane_dim2_max_real = global_y_max_real

		global_plane_dim3_min_real = global_z_min_real
		global_plane_dim3_max_real = global_z_max_real

		!ACCOUNT FOR PERIODIC BOUNDARY CONDITIONS WHEN DETERMINING BOUNDARIES OF RELEASE PLANE WALLS OR SURFACE

		IF(global_walls_surf_rel_dim1_max.EQ.global_x_min_real) global_walls_surf_rel_dim1_min = global_x_max_real

		IF(global_walls_surf_rel_dim1_min.EQ.global_x_max_real) global_walls_surf_rel_dim1_max = global_x_min_real

		
		global_plane_dim1_nodes_ghost = global_x_nodes_ghost
		global_plane_dim2_nodes_ghost = global_y_nodes_ghost
		global_plane_dim3_nodes_ghost = global_z_nodes_ghost
		global_plane_dim1_nodes_real  = global_x_nodes_real
		global_plane_dim2_nodes_real  = global_y_nodes_real
		global_plane_dim3_nodes_real  = global_z_nodes_real


	ELSEIF(xyz_dir_normal_to_release_plane.EQ.Y) THEN

		global_plane_dim1_min_ghost = global_y_min_ghost
		global_plane_dim1_max_ghost = global_y_max_ghost

		global_plane_dim2_min_ghost = global_x_min_ghost
		global_plane_dim2_max_ghost = global_x_max_ghost

		global_plane_dim3_min_ghost = global_z_min_ghost
		global_plane_dim3_max_ghost = global_z_max_ghost

		global_plane_dim1_min_real = global_y_min_real
		global_plane_dim1_max_real = global_y_max_real

		global_plane_dim2_min_real = global_x_min_real
		global_plane_dim2_max_real = global_x_max_real

		global_plane_dim3_min_real = global_z_min_real
		global_plane_dim3_max_real = global_z_max_real

		!ACCOUNT FOR PERIODIC BOUNDARY CONDITIONS WHEN DETERMINING BOUNDARIES OF RELEASE PLANE WALLS OR SURFACE
		IF(global_walls_surf_rel_dim1_max.EQ.global_y_min_real) global_walls_surf_rel_dim1_min = global_y_max_real

		IF(global_walls_surf_rel_dim1_min.EQ.global_y_max_real) global_walls_surf_rel_dim1_max = global_y_min_real

		global_plane_dim1_nodes_ghost = global_y_nodes_ghost
		global_plane_dim2_nodes_ghost = global_x_nodes_ghost
		global_plane_dim3_nodes_ghost = global_z_nodes_ghost
		global_plane_dim1_nodes_real  = global_y_nodes_real
		global_plane_dim2_nodes_real  = global_x_nodes_real
		global_plane_dim3_nodes_real  = global_z_nodes_real
	
	ELSEIF(xyz_dir_normal_to_release_plane.EQ.Z) THEN


		global_plane_dim1_min_ghost = global_z_min_ghost
		global_plane_dim1_max_ghost = global_z_max_ghost
		
		global_plane_dim2_min_ghost = global_x_min_ghost
		global_plane_dim2_max_ghost = global_x_max_ghost

		global_plane_dim3_min_ghost = global_y_min_ghost
		global_plane_dim3_max_ghost = global_y_max_ghost

		global_plane_dim1_min_real = global_z_min_real
		global_plane_dim1_max_real = global_z_max_real

		global_plane_dim2_min_real = global_x_min_real
		global_plane_dim2_max_real = global_x_max_real

		global_plane_dim3_min_real = global_y_min_real
		global_plane_dim3_max_real = global_y_max_real

		!ACCOUNT FOR PERIODIC BOUNDARY CONDITIONS WHEN DETERMINING BOUNDARIES OF RELEASE PLANE WALLS OR SURFACE
		IF(global_walls_surf_rel_dim1_max.EQ.global_z_min_real) global_walls_surf_rel_dim1_min = global_z_max_real

		IF(global_walls_surf_rel_dim1_min.EQ.global_z_max_real) global_walls_surf_rel_dim1_max = global_z_min_real

		global_plane_dim1_nodes_ghost = global_z_nodes_ghost
		global_plane_dim2_nodes_ghost = global_x_nodes_ghost
		global_plane_dim3_nodes_ghost = global_y_nodes_ghost
		global_plane_dim1_nodes_real  = global_z_nodes_real
		global_plane_dim2_nodes_real  = global_x_nodes_real
		global_plane_dim3_nodes_real  = global_y_nodes_real

	ELSE
	
			PRINT*,'MAIN_initialize_state ERROR: INVALID xyz_dir_normal_to_release_plane SPECIFIED!'
			IF(debug) WRITE (0,*) 'MAIN_initialize_state ERROR: INVALID xyz_dir_normal_to_release_plane SPECIFIED!'
			IF(debug) WRITE (9,*) 'MAIN_initialize_state ERROR: INVALID xyz_dir_normal_to_release_plane SPECIFIED!'
			CALL MPI_Abort(cartesian_communicator,mpi_abort_error,mpi_error_code)
			STOP

	ENDIF

ALLOCATE ( LBM_global_walls_release_plane(0:1,global_plane_dim2_min_ghost:global_plane_dim2_max_ghost,&
& global_plane_dim3_min_ghost:global_plane_dim3_max_ghost) )

 LBM_global_walls_release_plane(:,:,:) = .TRUE.
		
!READ IN LOCAL_WALLS ARRAY FROM A TXT FILE
IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to call local_read_global_logical_array'

CALL local_read_global_logical_array(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_fluid_count_x,global_fluid_count_y,global_fluid_count_z,&
& global_wall_count_x,global_wall_count_y,global_wall_count_z,LBM_global_walls_release_plane,&
& My_MPI_Process_ID,divide_x,divide_y,divide_z,IN,OUT,WEST,EAST,UP,DOWN,cartesian_communicator,&
& 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_x_nodes_real,local_y_nodes_real,local_z_nodes_real,local_x_nodes_ghost,local_y_nodes_ghost,local_z_nodes_ghost,&
& global_x_min_ghost,global_x_max_ghost,global_y_min_ghost,global_y_max_ghost,global_z_min_ghost,global_z_max_ghost,&
& global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,&
& global_x_nodes_ghost,global_y_nodes_ghost,global_z_nodes_ghost,&
& ROOT,global_walls_count_real,global_fluid_count_real,&
& global_plane_dim2_min_ghost,global_plane_dim2_max_ghost,&
& global_plane_dim3_min_ghost,global_plane_dim3_max_ghost,&
& global_walls_surf_rel_dim1_min,global_walls_surf_rel_dim1_max,&
& xyz_dir_normal_to_release_plane,&
& global_plane_dim1_min_real,global_plane_dim1_max_real,&
& global_plane_dim2_min_real,global_plane_dim2_max_real,&
& global_plane_dim3_min_real,global_plane_dim3_max_real,&
& global_plane_dim1_nodes_ghost,&
& global_plane_dim2_nodes_ghost,&
& global_plane_dim3_nodes_ghost,&
& global_plane_dim1_nodes_real,&
& global_plane_dim2_nodes_real,&
& global_plane_dim3_nodes_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,&
& local_walls,12,'global_walls')

IF(debug) WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling local_read_global_logical_array'

DEALLOCATE ( LBM_global_walls_release_plane )  !WE ONLY NEED THIS IN LST

	WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: ABOUT TO CALL MAIN_characterize_geometry'
	WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: ABOUT TO CALL MAIN_characterize_geometry'

	CALL MAIN_characterize_geometry(&
	& My_MPI_Process_ID,Number_Of_Processes,&
	& ROOT,global_product_xyz_nodes_real,&
	& global_plane_dim1_min_ghost,global_plane_dim1_max_ghost,&
	& global_plane_dim2_min_ghost,global_plane_dim2_max_ghost,&
	& global_plane_dim3_min_ghost,global_plane_dim3_max_ghost,&
	& global_plane_dim1_min_real,global_plane_dim1_max_real,&
	& global_plane_dim2_min_real,global_plane_dim2_max_real,&
	& global_plane_dim3_min_real,global_plane_dim3_max_real,&
	& global_walls_surf_rel_dim1_min,global_walls_surf_rel_dim1_max,&
	& global_plane_dim1_nodes_ghost,&
	& global_plane_dim2_nodes_ghost,&
	& global_plane_dim3_nodes_ghost,&
	& global_plane_dim1_nodes_real,&
	& global_plane_dim2_nodes_real,&
	& global_plane_dim3_nodes_real,& 
	& 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,& 
	& 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_min_ghost,global_x_max_ghost,& 
	& global_y_min_ghost,global_y_max_ghost,& 
	& global_z_min_ghost,global_z_max_ghost,&
	& global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,&
	& global_x_nodes_ghost,global_y_nodes_ghost,global_z_nodes_ghost,& 
	& divide_x,divide_y,divide_z,&
	& local_x_nodes_real,local_y_nodes_real,local_z_nodes_real,&
	& local_x_nodes_ghost,local_y_nodes_ghost,local_z_nodes_ghost,&
	& IN,OUT,EAST,WEST,UP,DOWN,&
	& cartesian_communicator,local_product_xyz_nodes_real,&
	& lattice_dimension,local_walls,dx,&
	& all_processes_local_bounds,Q,&
	& global_fluid_surface_count_x,global_inv_fluid_surface_count_x,&
	& global_fluid_surface_count_y,&
	& global_inv_fluid_surface_count_y,&
	& global_fluid_surface_count_z,&
	& global_inv_fluid_surface_count_z,&
	& global_fluid_surface_count_xy,&
	& global_fluid_surface_count_yz,&
	& global_fluid_surface_count_zx,&
	& global_inv_fluid_surf_count_xy,&
	& global_inv_fluid_surf_count_yz,&
	& global_inv_fluid_surf_count_zx,&
	& local_fluid_surface,&
	& xyz_dir_normal_to_release_plane,&
	& global_fluid_count_real,&
	& global_walls_count_real,&
	& LST_apply_volume_user_mask,&
	& NON_2_VOL_MASK_fluid_count_ratio)

	WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling MAIN_characterize_geometry'
	WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished calling MAIN_characterize_geometry'


	!COPY OBTAIN GHOST VALUES FROM REAL VALUES IN CASE THERE IS DIVISION OF MPI DOMAIN
!	IF (divide_x) THEN  !divide the domain up(MPI) in the X-Vector_Direction_Number

!		global_walls(GLOBAL_X_MIN_GHOST,global_y_min_ghost:global_y_max_ghost,global_z_min_ghost:global_z_max_ghost) = &
!		& global_walls(GLOBAL_X_MAX_REAL,global_y_min_ghost:global_y_max_ghost,global_z_min_ghost:global_z_max_ghost)  !make ghost columns
!		global_walls(GLOBAL_X_MAX_GHOST,global_y_min_ghost:global_y_max_ghost,global_z_min_ghost:global_z_max_ghost) = & 
!		& global_walls(GLOBAL_X_MIN_REAL,global_y_min_ghost:global_y_max_ghost,global_z_min_ghost:global_z_max_ghost)

!	ENDIF !divide_x

!	IF (divide_y) THEN  !divide the domain up(MPI) in the Y-Vector_Direction_Number

!		global_walls(global_x_min_ghost:global_x_max_ghost,GLOBAL_Y_MIN_GHOST,global_z_min_ghost:global_z_max_ghost) = &
!		& global_walls(global_x_min_ghost:global_x_max_ghost,GLOBAL_Y_MAX_REAL,global_z_min_ghost:global_z_max_ghost)  !make ghost columns
!		global_walls(global_x_min_ghost:global_x_max_ghost,GLOBAL_Y_MAX_GHOST,global_z_min_ghost:global_z_max_ghost) = &
!		& global_walls(global_x_min_ghost:global_x_max_ghost,GLOBAL_Y_MIN_REAL,global_z_min_ghost:global_z_max_ghost)

!	ENDIF !divide_y

!	IF (divide_z) THEN  !divide the domain up(MPI) in the Z-Vector_Direction_Number

!		global_walls(global_x_min_ghost:global_x_max_ghost,global_y_min_ghost:global_y_max_ghost,GLOBAL_Z_MIN_GHOST) = &
!		& global_walls(global_x_min_ghost:global_x_max_ghost,global_y_min_ghost:global_y_max_ghost,GLOBAL_Z_MAX_REAL)  !make ghost columns
!		global_walls(global_x_min_ghost:global_x_max_ghost,global_y_min_ghost:global_y_max_ghost,GLOBAL_Z_MAX_GHOST) = &
!		& global_walls(global_x_min_ghost:global_x_max_ghost,global_y_min_ghost:global_y_max_ghost,GLOBAL_Z_MIN_REAL)

!	ENDIF !divide_z

!OBTAIN LOCAL CHUNK OF WALLS FROM THE GLOBAL WALLS ARRAY
!local_walls(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_walls(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,local_z_min_ghost:local_z_max_ghost)


!COUNT THE NUMBER OF FLUID NODES AS A FUNCTION OF EACH DIMENSION
!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.local_walls(I,J,K) ) THEN
	
!				global_fluid_count_x(I) = global_fluid_count_x(I) + 1.0
!				global_fluid_count_y(J) = global_fluid_count_y(J) + 1.0
!				global_fluid_count_z(K) = global_fluid_count_z(K) + 1.0
	
!			END IF ! .NOT.local_walls(I,J,K)

!		END DO !I

!	END DO	!J

!END DO !K


IF(debug) THEN  !WRITE local_walls ARRAY FOR DEBUGGING PURPOSES

!	1000  format(I2)
!	write (Print_Results_MPI_ID,1000) My_MPI_Process_ID
	!write solid nodes to solid.dat	in TecPlot/xyz format
	!IF (My_MPI_Process_ID.EQ.0) THEN
	OPEN (22, FILE = 'debug/local_walls_'//Print_Results_MPI_ID//'.txt', STATUS = 'unknown')
	write(22,*) 'zone i=',count (local_walls(local_x_min_ghost:local_x_max_ghost,local_y_min_ghost:local_y_max_ghost,local_z_min_ghost:local_z_max_ghost))
	DO I=local_x_min_ghost,local_x_max_ghost; DO J=local_y_min_ghost,local_y_max_ghost; DO K=local_z_min_ghost,local_z_max_ghost
		IF(local_walls(I,J,K)) WRITE(22,*) I,J,K
	ENDDO; ENDDO; ENDDO
	CLOSE (22)

END IF !!debug

!IF (My_MPI_Process_ID.EQ.ROOT) THEN

!	OPEN (18, FILE = 'results/global_walls.txt', STATUS = 'unknown')

!	DO K=global_z_min_real,global_z_max_real
	
!		DO J=global_y_min_real,global_y_max_real
	
!			DO I=global_x_min_real,global_x_max_real
	
!			    WRITE(18,*) I,J,K,global_walls(I,J,K)
	
!			END DO !k = 1, nz
	
!		END DO !j = 1, ny
	
!	END DO !i = 1, nx

!	CLOSE(18)

!ENDIF !My_MPI_Process_ID.EQ.0



!=======================================END==================================================





!DUMP local_fluid_surface NODES

!OPEN (7, FILE = 'results/local_fluid_surface'//Print_Results_MPI_ID//'.txt', STATUS = 'unknown')
!write(7,*) 'zone i=',global_number_of_surface_nodes
!DO I=global_x_min_real,global_x_max_real; DO J=global_y_min_real,global_y_max_real; DO K=global_z_min_real,global_z_max_real
!	WRITE(7,*) I,J,K,local_fluid_surface(I,J,K)
!ENDDO; ENDDO; ENDDO
!CLOSE (7)

!=======================================END==================================================



!==============================CALCULATE INITIAL OR READ FROM RESUME FILE DENSITY DISTRIBUTIONS f_local======================
IF(.NOT.skip_LBM) THEN

	WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: skip_LBM',skip_LBM
	WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: skip_LBM',skip_LBM


	IF(debug) f_local(:,:,:,:) = 100.0 !THIS IS JUST A LARGE NUMBER FOR DUBIGGING PURPOSES
!	IF(debug) f_temp(:,:,:,:) = 100.0  !THIS IS JUST A LARGE NUMBER FOR DUBIGGING PURPOSES

!WRITE THE ACTUAL RESUME FILE IN BINARY
!OPEN (66, FILE = 'f_binary_resume.dat', STATUS = 'unknown', form='unformatted')
			
	IF(.NOT.Resume_Simulation) THEN !CALCULATE INITIAL f_local 

		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: Resume_Simulation',Resume_Simulation
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: Resume_Simulation',Resume_Simulation

		INQUIRE(FILE = 'results/3d_reconstruction.txt',EXIST = file_3d_reconstruction_exists)

 		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists ABOUT TO BARRIER'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists ABOUT TO BARRIER'
		CALL MPI_Barrier(cartesian_communicator,mpi_error_code)  !THIS IS TO ENSURE THAT NO I/O RACE CONDITIONS OCCUR LATER ON
 		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists FINISHED TO BARRIER'
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists FINISHED TO BARRIER'


		WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists',file_3d_reconstruction_exists
		WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: file_3d_reconstruction_exists',file_3d_reconstruction_exists

		IF(.NOT.file_3d_reconstruction_exists) THEN 

!			IF (My_MPI_Process_ID.EQ.ROOT) 	THEN

!				OPEN (76, FILE = 'results/3d_reconstruction.txt', STATUS = 'new', FORM='formatted')
	
!				!WRITE THE HEADER FOR THE GEOMETRY COORDINATES
!				WRITE(76,*) 'ZONE,i=',global_x_nodes_real,&
!				& 'j=',global_y_nodes_real,&
!				& 'k=',global_z_nodes_real

!				CLOSE(76)
		
!			END IF !!My_MPI_Process_ID.EQ.ANTIROOT

			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL global_write_3D_reconstruction'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL global_write_3D_reconstruction'
			CALL global_write_3D_reconstruction(local_walls,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,&
			& global_x_nodes_real,global_y_nodes_real,global_z_nodes_real,&
			& Number_Of_Processes,cartesian_communicator,lattice_dimension,25,'results/3d_reconstruction')
			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: FINISHED to CALL global_write_3D_reconstruction'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: FINISHED to CALL global_write_3D_reconstruction'

		ENDIF !! .NOT.file_3d_reconstruction_exists
	
		
!		DO Vector_Direction_Number = 0, Q     !EQN 14
			
!			DO K=local_z_min_ghost,local_z_max_ghost
	
!				DO J=local_y_min_ghost,local_y_max_ghost
	
!					DO I=local_x_min_ghost,local_x_max_ghost

					!	IF( .NOT.local_walls(I,J,K) ) THEN
		 	
!				 			f_local(Vector_Direction_Number,I,J,K) = Density_Weight(Vector_Direction_Number)  * &
!							&   Initial_Density_Rho0 
							
							!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) = 0.0

							!f_temp(Vector_Direction_Number,I,J,K) = 0.0

		f_local = 0.0  !THIS WILL ZERO THE WHOLE ARRAY
	
					!	END IF !!.NOT.local_walls(I,J,K)
							
!	
!					END DO !!I
	
!				END DO	!!J
	
!			END DO !!K
		
!		ENDDO !! Vector_Direction_Number 
	
	ELSE !!Resume_Simulation  THEN READ FROM RESUME FILE

		IF(LBM_previously_converged.AND.wrote_HARD_resume_files.AND.write_HARD_resume_files) THEN
		!IF(.false.) THEN

			
			WRITE (LBM_Previous_Run_Format_String(1:2),'(A2)') '  '
			WRITE (LBM_Previous_Run_Format_String(103:103),'(A)') ')'
			WRITE (LBM_Previous_Run_Format_String(3:102),'(I100)') Number_Of_LBM_Prev_Run_Digits
			LBM_Previous_Run_Format_String = ADJUSTL(LBM_Previous_Run_Format_String)
			LBM_Previous_Run_Format_String='(I'//LBM_Previous_Run_Format_String

			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: LBM_Previous_Run_Format_String',LBM_Previous_Run_Format_String
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: LBM_Previous_Run_Format_String',LBM_Previous_Run_Format_String

			!!REMOVE PREVIOUSLY GENERATED f_binary_resume AND f_asci_resume FILES
			WRITE (Print_LBM_Prev_Run_Step_Number,LBM_Previous_Run_Format_String) LBM_Time_Steps_Prev_Completed 
			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: Print_LBM_Prev_Run_Step_Number',Print_LBM_Prev_Run_Step_Number
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: Print_LBM_Prev_Run_Step_Number',Print_LBM_Prev_Run_Step_Number

				IF(debug) THEN
					WRITE (29,*) '================MAIN_initialize_state: pre-local_read_4D_array:NONWALLS==================='
					DO K = local_z_min_real,local_z_max_real	!DO K=1,1
						DO J = local_y_min_real,local_y_max_real	!DO J=1,1
							DO I = local_x_min_real,local_x_max_real	!DO I=1,1
								IF(.NOT.local_walls(I,J,K)) THEN
									DO Vector_Direction_Number = 0, Q
										WRITE (29,*) Vector_Direction_Number,I,J,K,f_local(Vector_Direction_Number,I,J,K)
									ENDDO !! dimension_1_size
								;ENDIF !!.NOT.local_walls(I,J,K)
							ENDDO !!I
						ENDDO !!J
					ENDDO !!K
				ENDIF !!DEBUG

			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL local_read_4D_array'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL local_read_4D_array'
			CALL local_read_4D_array(local_walls,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,IN,OUT,WEST,EAST,UP,DOWN,cartesian_communicator,&
			& 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_x_nodes_ghost,local_y_nodes_ghost,local_z_nodes_ghost,local_x_nodes_real,local_y_nodes_real,local_z_nodes_real,&
			& divide_x,divide_y,divide_z,My_MPI_Process_ID,f_local,0,Q,28+Number_Of_LBM_Prev_Run_Digits,&
			& global_fluid_count_real,ROOT,'backup/f_binary_HARD_resume'//'_'//Print_LBM_Prev_Run_Step_Number)
			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished to CALL local_read_4D_array'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished to CALL local_read_4D_array'


		ELSE
			
			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL LBM_soft_binary_resume'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: about to CALL LBM_soft_binary_resume'
			CALL LBM_soft_binary_resume(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,LBM_Time_Steps_Prev_Completed,Print_Results_MPI_ID,&
			& Number_Of_LBM_Prev_Run_Digits,My_MPI_Process_ID_Digits,My_MPI_Process_ID,Number_Of_Processes,&
			& local_product_xyz_nodes_ghost,f_local)
			WRITE (0,*) My_MPI_Process_ID,'MAIN_initialize_state: finished to CALL LBM_soft_binary_resume'
			WRITE (9,*) My_MPI_Process_ID,'MAIN_initialize_state: finished to CALL LBM_soft_binary_resume'


		ENDIF !!LBM_previously_converged

		IF(debug) THEN

			WRITE (29,*) '================post resume: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

								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 resume: 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

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

				ENDIF !! DEBUG



			
!		OPEN (66, FILE = 'f_binary_resume.dat', STATUS = 'old', form='unformatted')

!		CALL resume_binary_f(f_global,global_x_min_real,global_x_max_real,global_y_min_real,global_y_max_real,&
!		& global_z_min_real,global_z_max_real,Q,My_MPI_Process_ID)
		

!		DO Vector_Direction_Number = 0, Q     !EQN 14
			
!			DO K = local_z_min_ghost, local_z_max_ghost
				
!				N = K
!				IF(K.EQ.0) N = nz
!				IF(K.EQ.nz+1) N = 1
!	
!				DO J = local_y_min_ghost, local_y_max_ghost
					
!					M = J
!					IF(J.EQ.0) M = ny
!					IF(J.EQ.ny+1) M = 1
	
!					DO I = local_x_min_ghost, local_x_max_ghost
						
!						L = I
!						IF(I.EQ.0) L = nx
!						IF(I.EQ.nx+1) L = 1

!						IF( .NOT.local_walls(I,J,K) ) THEN
		 	
!				 			f_local(Vector_Direction_Number,I,J,K) = f_global(Vector_Direction_Number,L,M,N)
	
!						END IF !!.NOT.local_walls(I,J,K)
							
	
!					END DO !!I
	
!				END DO	!!J
	
!			END DO !!K
		
!		ENDDO !! Vector_Direction_Number

!		CLOSE(66)

	END IF !!TO RESUME OR NOT TO RESUME

ENDIF !!skip_LBM

!=======================================END==================================================

   CALL MPI_Barrier(cartesian_communicator,mpi_error_code)  !THIS IS TO ENSURE THAT NO I/O RACE CONDITIONS OCCUR LATER ON
        

	WRITE (0,*) My_MPI_Process_ID,'EXITING SUBROUTINE MAIN_initialize_state'
	WRITE (9,*) My_MPI_Process_ID,'EXITING SUBROUTINE MAIN_initialize_state'

	RETURN
END SUBROUTINE MAIN_initialize_state