2015-06-10 15:09:32 -04:00
|
|
|
/*
|
|
|
|
|
* Pre-processor to generate signed distance function from segmented data
|
|
|
|
|
* segmented data should be stored in a raw binary file as 1-byte integer (type char)
|
|
|
|
|
* will output distance functions for phases
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
2015-08-21 16:56:43 -04:00
|
|
|
#include "common/Array.h"
|
|
|
|
|
#include "common/Domain.h"
|
2015-06-10 15:09:32 -04:00
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
// Initialize MPI
|
|
|
|
|
int rank, nprocs;
|
|
|
|
|
MPI_Init(&argc,&argv);
|
2016-10-28 15:18:00 -04:00
|
|
|
|
|
|
|
|
MPI_Comm comm = MPI_COMM_WORLD;
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Comm_rank(comm,&rank);
|
|
|
|
|
MPI_Comm_size(comm,&nprocs);
|
2016-10-28 15:18:00 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int SOLID=atoi(argv[1]);
|
2016-02-28 17:19:36 -05:00
|
|
|
int NWP=atoi(argv[2]);
|
2016-10-28 15:18:00 -04:00
|
|
|
//char NWP,SOLID;
|
|
|
|
|
//SOLID=argv[1][0];
|
|
|
|
|
//NWP=argv[2][0];
|
2016-02-28 17:19:36 -05:00
|
|
|
if (rank==0){
|
2016-10-28 15:18:00 -04:00
|
|
|
printf("Solid Label: %i \n",SOLID);
|
|
|
|
|
printf("NWP Label: %i \n",NWP);
|
2016-02-28 17:19:36 -05:00
|
|
|
}
|
2016-10-28 15:38:38 -04:00
|
|
|
|
2015-06-10 15:09:32 -04:00
|
|
|
//.......................................................................
|
|
|
|
|
// Reading the domain information file
|
|
|
|
|
//.......................................................................
|
|
|
|
|
int nprocx, nprocy, nprocz, nx, ny, nz, nspheres;
|
|
|
|
|
double Lx, Ly, Lz;
|
|
|
|
|
int Nx,Ny,Nz;
|
|
|
|
|
int i,j,k,n;
|
|
|
|
|
int BC=0;
|
|
|
|
|
char Filename[40];
|
|
|
|
|
int xStart,yStart,zStart;
|
|
|
|
|
// char fluidValue,solidValue;
|
|
|
|
|
|
|
|
|
|
std::vector<char> solidValues;
|
|
|
|
|
std::vector<char> nwpValues;
|
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
|
|
if (rank==0){
|
|
|
|
|
ifstream domain("Domain.in");
|
|
|
|
|
domain >> nprocx;
|
|
|
|
|
domain >> nprocy;
|
|
|
|
|
domain >> nprocz;
|
|
|
|
|
domain >> nx;
|
|
|
|
|
domain >> ny;
|
|
|
|
|
domain >> nz;
|
|
|
|
|
domain >> nspheres;
|
|
|
|
|
domain >> Lx;
|
|
|
|
|
domain >> Ly;
|
|
|
|
|
domain >> Lz;
|
|
|
|
|
|
|
|
|
|
ifstream image("Segmented.in");
|
|
|
|
|
image >> Filename; // Name of data file containing segmented data
|
|
|
|
|
image >> Nx; // size of the binary file
|
|
|
|
|
image >> Ny;
|
|
|
|
|
image >> Nz;
|
|
|
|
|
image >> xStart; // offset for the starting voxel
|
|
|
|
|
image >> yStart;
|
|
|
|
|
image >> zStart;
|
|
|
|
|
|
|
|
|
|
}
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Barrier(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
// Computational domain
|
2015-06-14 19:18:37 -04:00
|
|
|
//.................................................
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Bcast(&nx,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&ny,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&nz,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&nprocx,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&nprocy,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&nprocz,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&nspheres,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&Lx,1,MPI_DOUBLE,0,comm);
|
|
|
|
|
MPI_Bcast(&Ly,1,MPI_DOUBLE,0,comm);
|
|
|
|
|
MPI_Bcast(&Lz,1,MPI_DOUBLE,0,comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
//.................................................
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Bcast(&Ny,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&Ny,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&Nz,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&xStart,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&yStart,1,MPI_INT,0,comm);
|
|
|
|
|
MPI_Bcast(&zStart,1,MPI_INT,0,comm);
|
2015-06-14 19:18:37 -04:00
|
|
|
//.................................................
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Barrier(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
|
|
|
|
|
// Check that the number of processors >= the number of ranks
|
|
|
|
|
if ( rank==0 ) {
|
|
|
|
|
printf("Number of MPI ranks required: %i \n", nprocx*nprocy*nprocz);
|
|
|
|
|
printf("Number of MPI ranks used: %i \n", nprocs);
|
|
|
|
|
printf("Full domain size: %i x %i x %i \n",nx*nprocx,ny*nprocy,nz*nprocz);
|
|
|
|
|
}
|
|
|
|
|
if ( nprocs < nprocx*nprocy*nprocz ){
|
|
|
|
|
ERROR("Insufficient number of processors");
|
|
|
|
|
}
|
2015-08-14 14:59:36 -04:00
|
|
|
char *SegData = NULL;
|
2015-06-10 15:09:32 -04:00
|
|
|
// Rank=0 reads the entire segmented data and distributes to worker processes
|
|
|
|
|
if (rank==0){
|
|
|
|
|
printf("Dimensions of segmented image: %i x %i x %i \n",Nx,Ny,Nz);
|
2015-06-27 17:07:00 -04:00
|
|
|
SegData = new char[Nx*Ny*Nz];
|
2015-06-10 15:09:32 -04:00
|
|
|
FILE *SEGDAT = fopen(Filename,"rb");
|
|
|
|
|
if (SEGDAT==NULL) ERROR("Error reading segmented data");
|
2016-01-10 11:16:54 -05:00
|
|
|
size_t ReadSeg;
|
|
|
|
|
ReadSeg=fread(SegData,1,Nx*Ny*Nz,SEGDAT);
|
2016-01-10 11:30:13 -05:00
|
|
|
if (ReadSeg != size_t(Nx*Ny*Nz)) printf("lbpm_segmented_decomp: Error reading segmented data (rank=%i)\n",rank);
|
2015-06-10 15:09:32 -04:00
|
|
|
fclose(SEGDAT);
|
|
|
|
|
printf("Read segmented data from %s \n",Filename);
|
|
|
|
|
}
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Barrier(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
|
|
|
|
|
// Get the rank info
|
|
|
|
|
int N = (nx+2)*(ny+2)*(nz+2);
|
|
|
|
|
Domain Dm(nx,ny,nz,rank,nprocx,nprocy,nprocz,Lx,Ly,Lz,BC);
|
|
|
|
|
for (k=0;k<nz+2;k++){
|
|
|
|
|
for (j=0;j<ny+2;j++){
|
|
|
|
|
for (i=0;i<nx+2;i++){
|
|
|
|
|
n = k*(nx+2)*(ny+2)+j*(nx+2)+i;
|
|
|
|
|
Dm.id[n] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-04 13:06:36 -04:00
|
|
|
Dm.CommInit(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
|
2015-06-14 19:18:37 -04:00
|
|
|
// number of sites to use for periodic boundary condition transition zone
|
2015-06-14 20:06:01 -04:00
|
|
|
int z_transition_size = (nprocz*nz - (Nz - zStart))/2;
|
|
|
|
|
if (z_transition_size < 0) z_transition_size=0;
|
2015-06-14 19:18:37 -04:00
|
|
|
|
2015-06-10 15:09:32 -04:00
|
|
|
// Set up the sub-domains
|
|
|
|
|
if (rank==0){
|
|
|
|
|
printf("Distributing subdomains across %i processors \n",nprocs);
|
|
|
|
|
printf("Process grid: %i x %i x %i \n",Dm.nprocx,Dm.nprocy,Dm.nprocz);
|
|
|
|
|
printf("Subdomain size: %i \n",N);
|
2015-06-14 19:35:14 -04:00
|
|
|
printf("Size of transition region: %i \n", z_transition_size);
|
2015-06-10 15:09:32 -04:00
|
|
|
char *tmp;
|
|
|
|
|
tmp = new char[N];
|
|
|
|
|
for (int kp=0; kp<nprocz; kp++){
|
|
|
|
|
for (int jp=0; jp<nprocy; jp++){
|
|
|
|
|
for (int ip=0; ip<nprocx; ip++){
|
|
|
|
|
// rank of the process that gets this subdomain
|
|
|
|
|
int rnk = kp*Dm.nprocx*Dm.nprocy + jp*Dm.nprocx + ip;
|
|
|
|
|
// Pack and send the subdomain for rnk
|
|
|
|
|
for (k=0;k<nz+2;k++){
|
|
|
|
|
for (j=0;j<ny+2;j++){
|
|
|
|
|
for (i=0;i<nx+2;i++){
|
|
|
|
|
int x = xStart + ip*nx + i-1;
|
|
|
|
|
int y = yStart + jp*ny + j-1;
|
2015-06-14 19:18:37 -04:00
|
|
|
// int z = zStart + kp*nz + k-1;
|
2015-06-14 20:06:01 -04:00
|
|
|
int z = zStart + kp*nz + k-1 - z_transition_size;
|
|
|
|
|
if (z<zStart) z=zStart;
|
2015-06-14 19:18:37 -04:00
|
|
|
if (!(z<Nz)) z=Nz-1;
|
2015-06-10 15:09:32 -04:00
|
|
|
int nlocal = k*(nx+2)*(ny+2) + j*(nx+2) + i;
|
|
|
|
|
int nglobal = z*Nx*Ny+y*Nx+x;
|
|
|
|
|
tmp[nlocal] = SegData[nglobal];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (rnk==0){
|
|
|
|
|
for (k=0;k<nz+2;k++){
|
|
|
|
|
for (j=0;j<ny+2;j++){
|
|
|
|
|
for (i=0;i<nx+2;i++){
|
|
|
|
|
int nlocal = k*(nx+2)*(ny+2) + j*(nx+2) + i;
|
|
|
|
|
Dm.id[nlocal] = tmp[nlocal];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
printf("Sending data to process %i \n", rnk);
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Send(tmp,N,MPI_CHAR,rnk,15,comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
// Recieve the subdomain from rank = 0
|
|
|
|
|
printf("Ready to recieve data %i at process %i \n", N,rank);
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Recv(Dm.id,N,MPI_CHAR,0,15,comm,MPI_STATUS_IGNORE);
|
2015-06-10 15:09:32 -04:00
|
|
|
}
|
2015-09-04 13:06:36 -04:00
|
|
|
MPI_Barrier(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
|
|
|
|
|
nx+=2; ny+=2; nz+=2;
|
|
|
|
|
N=nx*ny*nz;
|
|
|
|
|
|
2016-02-28 21:03:47 -05:00
|
|
|
if (rank==0) printf("All sub-domains recieved \n");
|
2015-11-24 16:46:12 -05:00
|
|
|
for (k=0;k<nz;k++){
|
2015-06-10 15:09:32 -04:00
|
|
|
for (j=0;j<ny;j++){
|
|
|
|
|
for (i=0;i<nx;i++){
|
2016-02-28 17:19:36 -05:00
|
|
|
n = k*nx*ny+j*nx+i;
|
2016-02-28 17:39:17 -05:00
|
|
|
if (Dm.id[n]==char(SOLID)) Dm.id[n] = 0;
|
2016-10-28 15:18:00 -04:00
|
|
|
else if (Dm.id[n]==char(NWP)) Dm.id[n] = 1;
|
|
|
|
|
else Dm.id[n] = 2;
|
2016-02-28 17:19:36 -05:00
|
|
|
|
2015-06-10 15:09:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-28 21:03:47 -05:00
|
|
|
if (rank==0) printf("Domain set \n");
|
2015-11-24 16:46:12 -05:00
|
|
|
|
2016-02-28 21:03:47 -05:00
|
|
|
int count = 0;
|
2016-02-28 17:39:17 -05:00
|
|
|
int total = 0;
|
|
|
|
|
int countGlobal = 0;
|
|
|
|
|
int totalGlobal = 0;
|
2016-02-28 17:50:44 -05:00
|
|
|
for (k=1;k<nz-1;k++){
|
|
|
|
|
for (j=1;j<ny-1;j++){
|
|
|
|
|
for (i=1;i<nx-1;i++){
|
2016-02-28 21:03:47 -05:00
|
|
|
n=k*nx*ny+j*nx+i;
|
2016-02-28 17:50:44 -05:00
|
|
|
total++;
|
|
|
|
|
if (Dm.id[n] == 0){
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-28 17:39:17 -05:00
|
|
|
}
|
|
|
|
|
MPI_Allreduce(&count,&countGlobal,1,MPI_INT,MPI_SUM,comm);
|
|
|
|
|
MPI_Allreduce(&total,&totalGlobal,1,MPI_INT,MPI_SUM,comm);
|
2016-12-19 10:08:32 -05:00
|
|
|
|
|
|
|
|
|
2016-02-28 17:39:17 -05:00
|
|
|
float porosity = float(totalGlobal-countGlobal)/totalGlobal;
|
|
|
|
|
if (rank==0) printf("Porosity=%f\n",porosity);
|
|
|
|
|
|
2016-12-19 10:08:32 -05:00
|
|
|
if (rank==0){
|
2017-01-18 15:09:40 -05:00
|
|
|
int xstart = xStart; // Is this correct?
|
|
|
|
|
int ystart = yStart;
|
|
|
|
|
int zstart = zStart;
|
2016-12-19 10:08:32 -05:00
|
|
|
//totalGlobal=(Nx-xstart)*(Ny-ystart)*(Nz-zstart);
|
|
|
|
|
countGlobal = 0;
|
|
|
|
|
for (k=zstart; k<zstart+nprocz*(nz-2); k++){
|
|
|
|
|
for (j=ystart; j<ystart+nprocy*(ny-2); j++){
|
|
|
|
|
for (i=xstart; i<xstart+nprocx*(nx-2); i++){
|
|
|
|
|
|
|
|
|
|
n=k*Nx*Ny+j*Nx+i;
|
|
|
|
|
if (n < Nx*Ny*Nz){
|
2017-03-17 10:35:13 -04:00
|
|
|
if (SegData[n] == char(SOLID)){
|
2016-12-19 10:08:32 -05:00
|
|
|
countGlobal++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
float porosity = float(totalGlobal-countGlobal)/totalGlobal;
|
|
|
|
|
printf("Original Porosity=%f\n",porosity);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 15:18:00 -04:00
|
|
|
count = 0;
|
|
|
|
|
total = 0;
|
|
|
|
|
countGlobal = 0;
|
|
|
|
|
totalGlobal = 0;
|
|
|
|
|
for (k=1;k<nz-1;k++){
|
|
|
|
|
for (j=1;j<ny-1;j++){
|
|
|
|
|
for (i=1;i<nx-1;i++){
|
|
|
|
|
n=k*nx*ny+j*nx+i;
|
|
|
|
|
if (Dm.id[n] != 0) total++;
|
|
|
|
|
if (Dm.id[n] == 2) count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MPI_Allreduce(&count,&countGlobal,1,MPI_INT,MPI_SUM,comm);
|
|
|
|
|
MPI_Allreduce(&total,&totalGlobal,1,MPI_INT,MPI_SUM,comm);
|
|
|
|
|
float saturation = float(countGlobal)/totalGlobal;
|
|
|
|
|
if (rank==0) printf("wetting phase saturation=%f\n",saturation);
|
|
|
|
|
|
2016-02-28 17:39:17 -05:00
|
|
|
|
2015-11-24 16:51:27 -05:00
|
|
|
char LocalRankFilename[40];
|
2015-06-10 15:09:32 -04:00
|
|
|
|
|
|
|
|
sprintf(LocalRankFilename,"ID.%05i",rank);
|
|
|
|
|
FILE *ID = fopen(LocalRankFilename,"wb");
|
|
|
|
|
fwrite(Dm.id,1,N,ID);
|
|
|
|
|
// fwrite(Distance.get(),8,Distance.length(),ID);
|
|
|
|
|
fclose(ID);
|
|
|
|
|
|
2016-07-23 10:16:16 -04:00
|
|
|
if (rank==0) printf("Writing symmetric domain reflection\n");
|
|
|
|
|
MPI_Barrier(comm);
|
2015-06-10 15:09:32 -04:00
|
|
|
int symrank,sympz;
|
|
|
|
|
sympz = 2*nprocz - Dm.kproc -1;
|
|
|
|
|
symrank = sympz*nprocx*nprocy + Dm.jproc*nprocx + Dm.iproc;
|
|
|
|
|
|
|
|
|
|
// DoubleArray SymDist(nx,ny,nz);
|
|
|
|
|
char *symid;
|
|
|
|
|
symid = new char [N];
|
|
|
|
|
for (k=0;k<nz;k++){
|
|
|
|
|
for (j=0;j<ny;j++){
|
|
|
|
|
for (i=0;i<nx;i++){
|
|
|
|
|
n=k*nx*ny+j*nx+i;
|
|
|
|
|
int nsym=(nz-k-1)*nx*ny+j*nx+i;
|
|
|
|
|
symid[nsym] = Dm.id[n];
|
|
|
|
|
//SymDist(i,j,nz-k-1)=Distance(i,j,k);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sprintf(LocalRankFilename,"ID.%05i",symrank);
|
|
|
|
|
FILE *SYMID = fopen(LocalRankFilename,"wb");
|
|
|
|
|
// fwrite(SymDist.get(),8,SymDist.length(),SYMDIST);
|
|
|
|
|
fwrite(symid,1,N,SYMID);
|
|
|
|
|
fclose(SYMID);
|
2016-10-28 15:18:00 -04:00
|
|
|
}
|
2016-02-28 21:03:47 -05:00
|
|
|
MPI_Barrier(comm);
|
|
|
|
|
MPI_Finalize();
|
2015-06-10 15:09:32 -04:00
|
|
|
}
|