#include #include #include #include #include #include #include #include "common/ScaLBL.h" #include "common/Communication.h" #include "common/TwoPhase.h" #include "common/MPI_Helpers.h" //#define WRITE_SURFACES /* * Simulator for two-phase flow in porous media * James E. McClure 2013-2014 */ using namespace std; //************************************************************************* // Implementation of Steady State Single-Phase LBM for permeability measurement //************************************************************************* inline void PackID(int *list, int count, char *sendbuf, char *ID){ // Fill in the phase ID values from neighboring processors // This packs up the values that need to be sent from one processor to another int idx,n; for (idx=0; idx> tau; // Viscosity parameter // Line 2: External force components (Fx,Fy, Fz) input >> Fx; input >> Fy; input >> Fz; // Line 3: Pressure Boundary conditions input >> Restart; input >> pBC; input >> din; input >> dout; // Line 4: time-stepping criteria input >> timestepMax; // max no. of timesteps input >> interval; // restart interval input >> tol; // error tolerance //............................................................. //....................................................................... // Reading the domain information file //....................................................................... ifstream domain("Domain.in"); domain >> nprocx; domain >> nprocy; domain >> nprocz; domain >> Nx; domain >> Ny; domain >> Nz; domain >> nspheres; domain >> Lx; domain >> Ly; domain >> Lz; //....................................................................... } // ************************************************************** // Broadcast simulation parameters from rank 0 to all other procs MPI_Barrier(MPI_COMM_WORLD); //................................................. MPI_Bcast(&tau,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&pBC,1,MPI_LOGICAL,0,MPI_COMM_WORLD); MPI_Bcast(&Restart,1,MPI_LOGICAL,0,MPI_COMM_WORLD); MPI_Bcast(&din,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&dout,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&Fx,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&Fy,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&Fz,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(×tepMax,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&interval,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&tol,1,MPI_DOUBLE,0,MPI_COMM_WORLD); // Computational domain MPI_Bcast(&Nx,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&Ny,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&Nz,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&nprocx,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&nprocy,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&nprocz,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&nspheres,1,MPI_INT,0,MPI_COMM_WORLD); MPI_Bcast(&Lx,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&Ly,1,MPI_DOUBLE,0,MPI_COMM_WORLD); MPI_Bcast(&Lz,1,MPI_DOUBLE,0,MPI_COMM_WORLD); //................................................. MPI_Barrier(MPI_COMM_WORLD); RESTART_INTERVAL=interval; // ************************************************************** // ************************************************************** double rlxA = 1.f/tau; double rlxB = 8.f*(2.f-rlxA)/(8.f-rlxA); if (nprocs != nprocx*nprocy*nprocz){ printf("nprocx = %i \n",nprocx); printf("nprocy = %i \n",nprocy); printf("nprocz = %i \n",nprocz); INSIST(nprocs == nprocx*nprocy*nprocz,"Fatal error in processor count!"); } if (rank==0){ printf("********************************************************\n"); printf("tau = %f \n", tau); printf("Force(x) = %f \n", Fx); printf("Force(y) = %f \n", Fy); printf("Force(z) = %f \n", Fz); printf("Sub-domain size = %i x %i x %i\n",Nz,Nz,Nz); printf("Parallel domain size = %i x %i x %i\n",nprocx,nprocy,nprocz); printf("********************************************************\n"); } double viscosity=(tau-0.5)/3.0; // Initialized domain and averaging framework for Two-Phase Flow int BC=pBC; Domain Dm(Nx,Ny,Nz,rank,nprocx,nprocy,nprocz,Lx,Ly,Lz,BC); TwoPhase Averages(Dm); InitializeRanks( rank, nprocx, nprocy, nprocz, iproc, jproc, kproc, rank_x, rank_y, rank_z, rank_X, rank_Y, rank_Z, rank_xy, rank_XY, rank_xY, rank_Xy, rank_xz, rank_XZ, rank_xZ, rank_Xz, rank_yz, rank_YZ, rank_yZ, rank_Yz ); MPI_Barrier(MPI_COMM_WORLD); Nz += 2; Nx = Ny = Nz; // Cubic domain int N = Nx*Ny*Nz; int dist_mem_size = N*sizeof(double); //....................................................................... if (rank == 0) printf("Read input media... \n"); //....................................................................... //....................................................................... // Filenames used char LocalRankString[8]; char LocalRankFilename[40]; char LocalRestartFile[40]; char tmpstr[10]; sprintf(LocalRankString,"%05d",rank); sprintf(LocalRankFilename,"%s%s","ID.",LocalRankString); sprintf(LocalRestartFile,"%s%s","Restart.",LocalRankString); // printf("Local File Name = %s \n",LocalRankFilename); // .......... READ THE INPUT FILE ....................................... // char value; char *id; id = new char[N]; int sum = 0; double sum_local; double iVol_global = 1.0/(1.0*(Nx-2)*(Ny-2)*(Nz-2)*nprocs); if (pBC) iVol_global = 1.0/(1.0*(Nx-2)*nprocx*(Ny-2)*nprocy*((Nz-2)*nprocz-6)); double porosity, pore_vol; //........................................................................... if (rank == 0) cout << "Reading in domain from signed distance function..." << endl; //....................................................................... sprintf(LocalRankString,"%05d",rank); // sprintf(LocalRankFilename,"%s%s","ID.",LocalRankString); // WriteLocalSolidID(LocalRankFilename, id, N); sprintf(LocalRankFilename,"%s%s","SignDist.",LocalRankString); ReadBinaryFile(LocalRankFilename, Averages.SDs.get(), N); MPI_Barrier(MPI_COMM_WORLD); if (rank == 0) cout << "Domain set." << endl; //....................................................................... // Assign the phase ID field based on the signed distance //....................................................................... for (k=0;k 0.0){ id[n] = 2; } // compute the porosity (actual interface location used) if (Averages.SDs(n) > 0.0){ sum++; } } } } // Set up kstart, kfinish so that the reservoirs are excluded from averaging int kstart,kfinish; kstart = 1; kfinish = Nz-1; if (pBC && kproc==0) kstart = 4; if (pBC && kproc==nprocz-1) kfinish = Nz-4; // Compute the pore volume sum_local = 0.0; for ( k=kstart;k 0){ sum_local += 1.0; } } } } MPI_Allreduce(&sum_local,&pore_vol,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); // MPI_Allreduce(&sum_local,&porosity,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD); porosity = pore_vol*iVol_global; if (rank==0) printf("Media porosity = %f \n",porosity); //......................................................... // If pressure boundary conditions are applied remove solid if (pBC && kproc == 0){ for (k=0; k<3; k++){ for (j=0;j tol ){ //************************************************************************* // Fused Color Gradient and Collision //************************************************************************* MRT( ID,f_even,f_odd,rlxA,rlxB,Fx,Fy,Fz,Nx,Ny,Nz); //************************************************************************* //************************************************************************* // Pack and send the D3Q19 distributions ScaLBL_Comm.SendD3Q19(f_even, f_odd); //************************************************************************* // Swap the distributions for momentum transport //************************************************************************* SwapD3Q19(ID, f_even, f_odd, Nx, Ny, Nz); //************************************************************************* // Wait for communications to complete and unpack the distributions ScaLBL_Comm.RecvD3Q19(f_even, f_odd); //************************************************************************* if (pBC && kproc == 0) { PressureBC_inlet(f_even,f_odd,din,Nx,Ny,Nz); } if (pBC && kproc == nprocz-1){ PressureBC_outlet(f_even,f_odd,dout,Nx,Ny,Nz,Nx*Ny*(Nz-2)); } //................................................................................... DeviceBarrier(); MPI_Barrier(MPI_COMM_WORLD); // Timestep completed! timestep++; if (timestep%500 == 0){ //........................................................................... // Copy the data for for the analysis timestep //........................................................................... // Copy the phase from the GPU -> CPU //........................................................................... DeviceBarrier(); ComputePressureD3Q19(ID,f_even,f_odd,Pressure,Nx,Ny,Nz); ComputeVelocityD3Q19(ID,f_even,f_odd,Velocity,Nx,Ny,Nz); CopyToHost(Averages.Press.get(),Pressure,N*sizeof(double)); CopyToHost(Averages.Vel_x.get(),&Velocity[0],N*sizeof(double)); CopyToHost(Averages.Vel_y.get(),&Velocity[N],N*sizeof(double)); CopyToHost(Averages.Vel_z.get(),&Velocity[2*N],N*sizeof(double)); // Way more work than necessary -- this is just to get the solid interfacial area!! Averages.Initialize(); Averages.UpdateMeshValues(); Averages.ComputeLocal(); Averages.Reduce(); double vawx = -Averages.vaw_global(0); double vawy = -Averages.vaw_global(1); double vawz = -Averages.vaw_global(2); if (rank==0){ // ************* DIMENSIONLESS FORCHEIMER EQUATION ************************* // Dye, A.L., McClure, J.E., Gray, W.G. and C.T. Miller // Description of Non-Darcy Flows in Porous Medium Systems // Physical Review E 87 (3), 033012 // Fo := density*D32^3*(density*force) / (viscosity^2) // Re := density*D32*velocity / viscosity // Fo = a*Re + b*Re^2 // ************************************************************************* //viscosity = (tau-0.5)*0.333333333333333333; D32 = 6.0*(Dm.Volume-Averages.vol_w_global)/Averages.As_global; printf("Sauter Mean Diameter = %f \n",D32); mag_force = sqrt(Fx*Fx+Fy*Fy+Fz*Fz); Fo = D32*D32*D32*mag_force/viscosity/viscosity; // .... 1-D flow should be aligned with force ... velocity = vawx*Fx/mag_force + vawy*Fy/mag_force + vawz*Fz/mag_force; err1D = fabs(velocity-sqrt(vawx*vawx+vawy*vawy+vawz*vawz))/velocity; //.......... Computation of the Reynolds number Re .............. Re = D32*velocity/viscosity; printf("Force: %.5g,%.5g,%.5g \n",Fx,Fy,Fz); printf("Velocity: %.5g,%.5g,%.5g \n",vawx,vawy,vawz); printf("Relative error for 1D representation: %.5g \n",err1D); printf("Dimensionless force: %5g \n", Fo); printf("Reynolds number: %.5g \n", Re); printf("Dimensionless Permeability (k/D^2): %.5g \n", Re/Fo); } } } //************************************************************************/ DeviceBarrier(); MPI_Barrier(MPI_COMM_WORLD); stoptime = MPI_Wtime(); if (rank==0) printf("-------------------------------------------------------------------\n"); // Compute the walltime per timestep cputime = (stoptime - starttime)/timestep; // Performance obtained from each node double MLUPS = double(Nx*Ny*Nz)/cputime/1000000; if (rank==0) printf("********************************************************\n"); if (rank==0) printf("CPU time = %f \n", cputime); if (rank==0) printf("Lattice update rate (per core)= %f MLUPS \n", MLUPS); MLUPS *= nprocs; if (rank==0) printf("Lattice update rate (total)= %f MLUPS \n", MLUPS); if (rank==0) printf("********************************************************\n"); // **************************************************** MPI_Barrier(MPI_COMM_WORLD); MPI_Finalize(); // **************************************************** }