Merge branch 'morphLBM' of github.com:JamesEMcClure/LBPM-WIA into morphLBM

This commit is contained in:
James McClure 2019-05-01 10:47:35 -04:00
commit fa0fd7cba8
18 changed files with 464 additions and 488 deletions

View File

@ -250,10 +250,11 @@ void SubPhase::Basic(){
double total_flow_rate = water_flow_rate + not_water_flow_rate; double total_flow_rate = water_flow_rate + not_water_flow_rate;
double fractional_flow= water_flow_rate / total_flow_rate; double fractional_flow= water_flow_rate / total_flow_rate;
double krn = nu_n*not_water_flow_rate / force_mag ; double h = Dm->voxel_length;
double krw = nu_w*water_flow_rate / force_mag; double krn = h*h*nu_n*not_water_flow_rate / force_mag ;
double krw = h*h*nu_w*water_flow_rate / force_mag;
//printf(" water saturation = %f, fractional flow =%f \n",saturation,fractional_flow); //printf(" water saturation = %f, fractional flow =%f \n",saturation,fractional_flow);
fprintf(TIMELOG,"%.5g %.5g %.5g %.5g %.5g %.5g %.5g\n",saturation,krw,krn,water_flow_rate,not_water_flow_rate, gwb.p, gnb.p); fprintf(TIMELOG,"%.5g %.5g %.5g %.5g %.5g %.5g %.5g\n",saturation,krw,krn,h*h*h*water_flow_rate,h*h*h*not_water_flow_rate, gwb.p, gnb.p);
fflush(TIMELOG); fflush(TIMELOG);
} }

View File

@ -25,7 +25,7 @@ inline void UnpackID(int *list, int count, signed char *recvbuf, signed char *ID
} }
//*************************************************************************************** //***************************************************************************************
double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction){ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction, signed char ErodeLabel, signed char NewLabel){
// SignDist is the distance to the object that you want to constaing the morphological opening // SignDist is the distance to the object that you want to constaing the morphological opening
// VoidFraction is the the empty space where the object inst // VoidFraction is the the empty space where the object inst
// id is a labeled map // id is a labeled map
@ -54,14 +54,13 @@ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain>
n = k*nx*ny+j*nx+i; n = k*nx*ny+j*nx+i;
// extract maximum distance for critical radius // extract maximum distance for critical radius
if ( SignDist(i,j,k) > maxdist) maxdist=SignDist(i,j,k); if ( SignDist(i,j,k) > maxdist) maxdist=SignDist(i,j,k);
if ( SignDist(i,j,k) > 0.0 ){ if ( id[n] == ErodeLabel){
count += 1.0; count += 1.0;
id[n] = 2; //id[n] = 2;
} }
} }
} }
} }
MPI_Barrier(Dm->Comm); MPI_Barrier(Dm->Comm);
// total Global is the number of nodes in the pore-space // total Global is the number of nodes in the pore-space
@ -71,6 +70,7 @@ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain>
double volume_fraction=totalGlobal/volume; double volume_fraction=totalGlobal/volume;
if (rank==0) printf("Volume fraction for morphological opening: %f \n",volume_fraction); if (rank==0) printf("Volume fraction for morphological opening: %f \n",volume_fraction);
if (rank==0) printf("Maximum pore size: %f \n",maxdistGlobal); if (rank==0) printf("Maximum pore size: %f \n",maxdistGlobal);
final_void_fraction = volume_fraction; //initialize
// Communication buffers // Communication buffers
signed char *sendID_x, *sendID_y, *sendID_z, *sendID_X, *sendID_Y, *sendID_Z; signed char *sendID_x, *sendID_y, *sendID_z, *sendID_X, *sendID_Y, *sendID_Z;
@ -140,11 +140,11 @@ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain>
double GlobalNumber = 1.f; double GlobalNumber = 1.f;
int imin,jmin,kmin,imax,jmax,kmax; int imin,jmin,kmin,imax,jmax,kmax;
if (ErodeLabel == 1){
VoidFraction = 1.0 - VoidFraction;
}
double Rcrit_new = maxdistGlobal; double Rcrit_new = maxdistGlobal;
//if (argc>2){
// Rcrit_new = strtod(argv[2],NULL);
// if (rank==0) printf("Max. distance =%f, Initial critical radius = %f \n",maxdistGlobal,Rcrit_new);
//}
while (void_fraction_new > VoidFraction) while (void_fraction_new > VoidFraction)
{ {
@ -173,9 +173,9 @@ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain>
for (ii=imin; ii<imax; ii++){ for (ii=imin; ii<imax; ii++){
int nn = kk*nx*ny+jj*nx+ii; int nn = kk*nx*ny+jj*nx+ii;
double dsq = double((ii-i)*(ii-i)+(jj-j)*(jj-j)+(kk-k)*(kk-k)); double dsq = double((ii-i)*(ii-i)+(jj-j)*(jj-j)+(kk-k)*(kk-k));
if (id[nn] == 2 && dsq <= Rcrit_new*Rcrit_new){ if (id[nn] == ErodeLabel && dsq <= Rcrit_new*Rcrit_new){
LocalNumber+=1.0; LocalNumber+=1.0;
id[nn]=1; id[nn]=NewLabel;
} }
} }
} }
@ -270,7 +270,7 @@ double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain>
for (int j=1; j<Ny-1; j++){ for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){ for (int i=1; i<Nx-1; i++){
n=k*Nx*Ny+j*Nx+i; n=k*Nx*Ny+j*Nx+i;
if (id[n] == 2){ if (id[n] == ErodeLabel){
count+=1.0; count+=1.0;
} }
} }
@ -452,6 +452,10 @@ double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain
//} //}
MPI_Barrier(Dm->Comm); MPI_Barrier(Dm->Comm);
FILE *DRAIN = fopen("morphdrain.csv","w");
fprintf(DRAIN,"sw radius\n");
while (void_fraction_new > VoidFraction && Rcrit_new > 0.5) while (void_fraction_new > VoidFraction && Rcrit_new > 0.5)
{ {
void_fraction_diff_old = void_fraction_diff_new; void_fraction_diff_old = void_fraction_diff_new;
@ -462,9 +466,9 @@ double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain
if (Window == 0) Window = 1; // If Window = 0 at the begining, after the following process will have sw=1.0 if (Window == 0) Window = 1; // If Window = 0 at the begining, after the following process will have sw=1.0
// and sw<Sw will be immediately broken // and sw<Sw will be immediately broken
double LocalNumber=0.f; double LocalNumber=0.f;
for(int k=0; k<Nz; k++){ for(int k=1; k<Nz-1; k++){
for(int j=0; j<Ny; j++){ for(int j=1; j<Ny-1; j++){
for(int i=0; i<Nx; i++){ for(int i=1; i<Nx-1; i++){
n = k*nx*ny + j*nx+i; n = k*nx*ny + j*nx+i;
if (SignDist(i,j,k) > Rcrit_new){ if (SignDist(i,j,k) > Rcrit_new){
// loop over the window and update // loop over the window and update
@ -479,7 +483,7 @@ double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain
for (ii=imin; ii<imax; ii++){ for (ii=imin; ii<imax; ii++){
int nn = kk*nx*ny+jj*nx+ii; int nn = kk*nx*ny+jj*nx+ii;
double dsq = double((ii-i)*(ii-i)+(jj-j)*(jj-j)+(kk-k)*(kk-k)); double dsq = double((ii-i)*(ii-i)+(jj-j)*(jj-j)+(kk-k)*(kk-k));
if (id[nn] == 2 && dsq <= Rcrit_new*Rcrit_new){ if (id[nn] == 2 && dsq <= (Rcrit_new+1)*(Rcrit_new+1)){
LocalNumber+=1.0; LocalNumber+=1.0;
id[nn]=1; id[nn]=1;
} }
@ -653,6 +657,8 @@ double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain
void_fraction_new = countGlobal/totalGlobal; void_fraction_new = countGlobal/totalGlobal;
void_fraction_diff_new = abs(void_fraction_new-VoidFraction); void_fraction_diff_new = abs(void_fraction_new-VoidFraction);
if (rank==0){ if (rank==0){
fprintf(DRAIN,"%f ",void_fraction_new);
fprintf(DRAIN,"%f\n",Rcrit_new);
printf(" %f ",void_fraction_new); printf(" %f ",void_fraction_new);
printf(" %f\n",Rcrit_new); printf(" %f\n",Rcrit_new);
} }

View File

@ -3,6 +3,6 @@
#include "common/Domain.h" #include "common/Domain.h"
#include "analysis/runAnalysis.h" #include "analysis/runAnalysis.h"
double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction); double MorphOpen(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction, signed char ErodeLabel, signed char ReplaceLabel);
double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction); double MorphDrain(DoubleArray &SignDist, signed char *id, std::shared_ptr<Domain> Dm, double VoidFraction);
double MorphGrow(DoubleArray &BoundaryDist, DoubleArray &Dist, Array<char> &id, std::shared_ptr<Domain> Dm, double TargetVol); double MorphGrow(DoubleArray &BoundaryDist, DoubleArray &Dist, Array<char> &id, std::shared_ptr<Domain> Dm, double TargetVol);

View File

@ -0,0 +1,49 @@
Color {
tauA = 0.7;
tauB = 0.7;
rhoA = 1.0;
rhoB = 1.0;
alpha = 1e-3;
beta = 0.95;
F = 0, 0, 0
Restart = false
pBC = 0
din = 1.0
dout = 1.0
timestepMax = 3000
interval = 1000
tol = 1e-5;
das = 0.1
dbs = 0.9
flux = 0.0
ComponentLabels = 0, -1 // labels for immobile components
ComponentAffinity = -1.0, 0.5 // wetting condition for immobile components (-1 = water wet / +1 oil wet)
}
Domain {
Filename = "CornerFlow.raw"
nproc = 1, 1, 2 // Number of processors (Npx,Npy,Npz)
n = 64, 64, 64 // Size of local domain (Nx,Ny,Nz)
N = 64, 64, 128 // size of the input image
n_spheres = 1 // Number of spheres
L = 1, 1, 1 // Length of domain (x,y,z)
BC = 0 // Boundary condition type
ReadType = "8bit"
ReadValues = 0, 1, 2 // labels within the original input image
WriteValues = 0, 1, 2 // labels to write (if they aren't the same)
ComponentLabels = 0 // labels that are immobile
HistoryLabels = -1 // new labels to assign to each immobile component based on fluid history
Sw = 0.3 // target saturation for morphological routines
}
Analysis {
blobid_interval = 1000 // Frequency to perform blob identification
analysis_interval = 1000 // Frequency to perform analysis
restart_interval = 1000 // Frequency to write restart data
visualization_interval = 2000 // Frequency to write visualization data
restart_file = "Restart" // Filename to use for restart file (will append rank)
N_threads = 4 // Number of threads to use
load_balance = "independent" // Load balance method to use: "none", "default", "independent"
}

View File

@ -1,2 +0,0 @@
1
32 32 32

View File

@ -1,6 +0,0 @@
1.0 1.0
1.0 1.0
1.0e-3 0.95
0.0 0.0 1.0e-5
0 0 10.0 1.0
10000 2000 1e-5

View File

@ -1,3 +0,0 @@
2 2 2
320 320 320
1.0 1.0 1.0

View File

@ -89,7 +89,7 @@ __global__ void sum_kernel_block(double *sum, double *input, int n)
__inline__ __device__ __inline__ __device__
double warpReduceSum(double val) { double warpReduceSum(double val) {
for (int offset = warpSize/2; offset > 0; offset /= 2) for (int offset = warpSize/2; offset > 0; offset /= 2)
val += __shfl_down(val, offset); val += __shfl_down_sync(0xFFFFFFFF, val, offset, 32);
return val; return val;
} }

View File

@ -9,6 +9,7 @@ extern "C" int ScaLBL_SetDevice(int rank){
//int device = local_rank % n_devices; //int device = local_rank % n_devices;
int device = rank % n_devices; int device = rank % n_devices;
cudaSetDevice(device); cudaSetDevice(device);
if (rank < n_devices) printf("MPI rank=%i will use GPU ID %i / %i \n",rank,device,n_devices);
return device; return device;
} }

View File

@ -4,6 +4,8 @@ color lattice boltzmann model
#include "models/ColorModel.h" #include "models/ColorModel.h"
#include "analysis/distance.h" #include "analysis/distance.h"
#include "analysis/morphology.h" #include "analysis/morphology.h"
#include <stdlib.h>
#include <time.h>
ScaLBL_ColorModel::ScaLBL_ColorModel(int RANK, int NP, MPI_Comm COMM): ScaLBL_ColorModel::ScaLBL_ColorModel(int RANK, int NP, MPI_Comm COMM):
rank(RANK), nprocs(NP), Restart(0),timestep(0),timestepMax(0),tauA(0),tauB(0),rhoA(0),rhoB(0),alpha(0),beta(0), rank(RANK), nprocs(NP), Restart(0),timestep(0),timestepMax(0),tauA(0),tauB(0),rhoA(0),rhoB(0),alpha(0),beta(0),
@ -126,7 +128,13 @@ void ScaLBL_ColorModel::ReadParams(string filename){
void ScaLBL_ColorModel::SetDomain(){ void ScaLBL_ColorModel::SetDomain(){
Dm = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // full domain for analysis Dm = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // full domain for analysis
Mask = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // mask domain removes immobile phases Mask = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // mask domain removes immobile phases
Nx+=2; Ny+=2; Nz += 2; // domain parameters
Nx = Dm->Nx;
Ny = Dm->Ny;
Nz = Dm->Nz;
Lx = Dm->Lx;
Ly = Dm->Ly;
Lz = Dm->Lz;
N = Nx*Ny*Nz; N = Nx*Ny*Nz;
id = new signed char [N]; id = new signed char [N];
for (int i=0; i<Nx*Ny*Nz; i++) Dm->id[i] = 1; // initialize this way for (int i=0; i<Nx*Ny*Nz; i++) Dm->id[i] = 1; // initialize this way
@ -137,12 +145,6 @@ void ScaLBL_ColorModel::SetDomain(){
MPI_Barrier(comm); MPI_Barrier(comm);
// Read domain parameters // Read domain parameters
rank = Dm->rank(); rank = Dm->rank();
Nx = Dm->Nx;
Ny = Dm->Ny;
Nz = Dm->Nz;
Lx = Dm->Lx;
Ly = Dm->Ly;
Lz = Dm->Lz;
nprocx = Dm->nprocx(); nprocx = Dm->nprocx();
nprocy = Dm->nprocy(); nprocy = Dm->nprocy();
nprocz = Dm->nprocz(); nprocz = Dm->nprocz();
@ -448,6 +450,7 @@ void ScaLBL_ColorModel::Run(){
bool SET_CAPILLARY_NUMBER = false; bool SET_CAPILLARY_NUMBER = false;
bool MORPH_ADAPT = false; bool MORPH_ADAPT = false;
bool USE_MORPH = false; bool USE_MORPH = false;
bool USE_SEED = false;
int analysis_interval = 1000; // number of timesteps in between in situ analysis int analysis_interval = 1000; // number of timesteps in between in situ analysis
int MAX_MORPH_TIMESTEPS = 50000; // maximum number of LBM timesteps to spend in morphological adaptation routine int MAX_MORPH_TIMESTEPS = 50000; // maximum number of LBM timesteps to spend in morphological adaptation routine
int MIN_STEADY_TIMESTEPS = 100000; int MIN_STEADY_TIMESTEPS = 100000;
@ -458,9 +461,11 @@ void ScaLBL_ColorModel::Run(){
int CURRENT_STEADY_TIMESTEPS=0; // counter for number of timesteps spent in morphological adaptation routine (reset each time) int CURRENT_STEADY_TIMESTEPS=0; // counter for number of timesteps spent in morphological adaptation routine (reset each time)
int morph_timesteps = 0; int morph_timesteps = 0;
double morph_delta = 0.0; double morph_delta = 0.0;
double seed_water = 0.0;
double capillary_number = 0.0; double capillary_number = 0.0;
double tolerance = 0.01; double tolerance = 0.01;
double Ca_previous = 0.f; double Ca_previous = 0.f;
double initial_volume = 0.0;
double delta_volume = 0.0; double delta_volume = 0.0;
double delta_volume_target = 0.0; double delta_volume_target = 0.0;
double RESIDUAL_ENDPOINT_THRESHOLD = 0.04; double RESIDUAL_ENDPOINT_THRESHOLD = 0.04;
@ -476,6 +481,10 @@ void ScaLBL_ColorModel::Run(){
if (rank == 0) printf("WARINING: capillary number target only supported for BC = 0 \n"); if (rank == 0) printf("WARINING: capillary number target only supported for BC = 0 \n");
SET_CAPILLARY_NUMBER=false; SET_CAPILLARY_NUMBER=false;
} }
if (analysis_db->keyExists( "seed_water" )){
seed_water = analysis_db->getScalar<double>( "seed_water" );
USE_SEED = true;
}
if (analysis_db->keyExists( "morph_delta" )){ if (analysis_db->keyExists( "morph_delta" )){
morph_delta = analysis_db->getScalar<double>( "morph_delta" ); morph_delta = analysis_db->getScalar<double>( "morph_delta" );
} }
@ -608,28 +617,35 @@ void ScaLBL_ColorModel::Run(){
analysis.finish(); analysis.finish();
CURRENT_STEADY_TIMESTEPS += analysis_interval; CURRENT_STEADY_TIMESTEPS += analysis_interval;
double volB = Averages->gwb.V;
double volA = Averages->gnb.V;
volA /= Dm->Volume;
volB /= Dm->Volume;;
double vA_x = Averages->gnb.Px/Averages->gnb.M;
double vA_y = Averages->gnb.Py/Averages->gnb.M;
double vA_z = Averages->gnb.Pz/Averages->gnb.M;
double vB_x = Averages->gwb.Px/Averages->gwb.M;
double vB_y = Averages->gwb.Py/Averages->gwb.M;
double vB_z = Averages->gwb.Pz/Averages->gwb.M;
double muA = rhoA*(tauA-0.5)/3.f;
double muB = rhoB*(tauB-0.5)/3.f;
double force_mag = sqrt(Fx*Fx+Fy*Fy+Fz*Fz);
double dir_x = Fx/force_mag;
double dir_y = Fy/force_mag;
double dir_z = Fz/force_mag;
if (force_mag == 0.0){
// default to z direction
dir_x = 0.0;
dir_y = 0.0;
dir_z = 1.0;
force_mag = 1.0;
}
double current_saturation = volB/(volA+volB);
double flow_rate_A = volA*(vA_x*dir_x + vA_y*dir_y + vA_z*dir_z);
double flow_rate_B = volB*(vB_x*dir_x + vB_y*dir_y + vB_z*dir_z);
double Ca = fabs(muA*flow_rate_A + muB*flow_rate_B)/(5.796*alpha);
if ( morph_timesteps > morph_interval ){ if ( morph_timesteps > morph_interval ){
double volB = Averages->gwb.V;
double volA = Averages->gnb.V;
double vA_x = Averages->gnb.Px/Averages->gnb.M;
double vA_y = Averages->gnb.Py/Averages->gnb.M;
double vA_z = Averages->gnb.Pz/Averages->gnb.M;
double vB_x = Averages->gwb.Px/Averages->gnb.M;
double vB_y = Averages->gwb.Py/Averages->gnb.M;
double vB_z = Averages->gwb.Pz/Averages->gnb.M;
double muA = rhoA*(tauA-0.5)/3.f;
double muB = rhoB*(tauB-0.5)/3.f;
double force_mag = sqrt(Fx*Fx+Fy*Fy+Fz*Fz);
double dir_x = Fx/force_mag;
double dir_y = Fy/force_mag;
double dir_z = Fz/force_mag;
double flow_rate_A = (vA_x*dir_x + vA_y*dir_y + vA_z*dir_z);
double flow_rate_B = (vB_x*dir_x + vB_y*dir_y + vB_z*dir_z);
double current_saturation = volB/(volA+volB);
double Ca = fabs(volA*muA*flow_rate_A + volB*muB*flow_rate_B)/(5.796*alpha*double((Nx-2)*(Ny-2)*(Nz-2)*nprocs));
double force_magnitude = sqrt(Fx*Fx + Fy*Fy + Fz*Fz);
bool isSteady = false; bool isSteady = false;
if ( (fabs((Ca - Ca_previous)/Ca) < tolerance && CURRENT_STEADY_TIMESTEPS > MIN_STEADY_TIMESTEPS)) if ( (fabs((Ca - Ca_previous)/Ca) < tolerance && CURRENT_STEADY_TIMESTEPS > MIN_STEADY_TIMESTEPS))
@ -649,11 +665,27 @@ void ScaLBL_ColorModel::Run(){
if (rank==0){ if (rank==0){
printf("** WRITE STEADY POINT *** "); printf("** WRITE STEADY POINT *** ");
printf("Ca = %f, (previous = %f) \n",Ca,Ca_previous); printf("Ca = %f, (previous = %f) \n",Ca,Ca_previous);
volA /= double((Nx-2)*(Ny-2)*(Nz-2)*nprocs); double pA = Averages->gnb.p;
volB /= double((Nx-2)*(Ny-2)*(Nz-2)*nprocs); double pB = Averages->gwb.p;
FILE * kr_log_file = fopen("relperm.csv","a");
fprintf(kr_log_file,"%i %.5g %.5g %.5g %.5g %.5g %.5g ",CURRENT_STEADY_TIMESTEPS,muA,muB,5.796*alpha,Fx,Fy,Fz); double h = Dm->voxel_length;
fprintf(kr_log_file,"%.5g %.5g %.5g %.5g %.5g %.5g %.5g %.5g\n",volA,volB,vA_x,vA_y,vA_z,vB_x,vB_y,vB_z); double kAeff = h*h*muA*flow_rate_A/(rhoA*force_mag);
double kBeff = h*h*muB*flow_rate_B/(rhoB*force_mag);
double pAB = (pA-pB)/(h*5.796*alpha);
double viscous_pressure_drop = (rhoA*volA + rhoB*volB)*force_mag;
double Mobility = muA/muB;
bool WriteHeader=false;
FILE * kr_log_file = fopen("relperm.csv","r");
if (kr_log_file != NULL)
fclose(kr_log_file);
else
WriteHeader=true;
kr_log_file = fopen("relperm.csv","a");
if (WriteHeader)
fprintf(kr_log_file,"timesteps sat.water eff.perm.oil eff.perm.water cap.pressure pressure.drop Ca M\n",CURRENT_STEADY_TIMESTEPS,current_saturation,kAeff,kBeff,pAB,viscous_pressure_drop,Ca,Mobility);
fprintf(kr_log_file,"%i %.5g %.5g %.5g %.5g %.5g %.5g %.5g\n",CURRENT_STEADY_TIMESTEPS,current_saturation,kAeff,kBeff,pAB,viscous_pressure_drop,Ca,Mobility);
fclose(kr_log_file); fclose(kr_log_file);
printf(" Measured capillary number %f \n ",Ca); printf(" Measured capillary number %f \n ",Ca);
@ -664,15 +696,15 @@ void ScaLBL_ColorModel::Run(){
Fy *= capillary_number / Ca; Fy *= capillary_number / Ca;
Fz *= capillary_number / Ca; Fz *= capillary_number / Ca;
if (force_magnitude > 1e-3){ if (force_mag > 1e-3){
Fx *= 1e-3/force_magnitude; // impose ceiling for stability Fx *= 1e-3/force_mag; // impose ceiling for stability
Fy *= 1e-3/force_magnitude; Fy *= 1e-3/force_mag;
Fz *= 1e-3/force_magnitude; Fz *= 1e-3/force_mag;
} }
if (force_magnitude < 1e-7){ if (force_mag < 1e-7){
Fx *= 1e-7/force_magnitude; // impose floor Fx *= 1e-7/force_mag; // impose floor
Fy *= 1e-7/force_magnitude; Fy *= 1e-7/force_mag;
Fz *= 1e-7/force_magnitude; Fz *= 1e-7/force_mag;
} }
if (rank == 0) printf(" -- adjust force by factor %f \n ",capillary_number / Ca); if (rank == 0) printf(" -- adjust force by factor %f \n ",capillary_number / Ca);
Averages->SetParams(rhoA,rhoB,tauA,tauB,Fx,Fy,Fz,alpha,beta); Averages->SetParams(rhoA,rhoB,tauA,tauB,Fx,Fy,Fz,alpha,beta);
@ -687,18 +719,29 @@ void ScaLBL_ColorModel::Run(){
morph_timesteps=0; morph_timesteps=0;
Ca_previous = Ca; Ca_previous = Ca;
} }
if (MORPH_ADAPT ){ if (MORPH_ADAPT ){
CURRENT_MORPH_TIMESTEPS += analysis_interval; CURRENT_MORPH_TIMESTEPS += analysis_interval;
if (rank==0) printf("***Morphological step with target volume change %f ***\n", delta_volume_target); if (USE_SEED){
//double delta_volume_target = volB - (volA + volB)*TARGET_SATURATION; // change in volume to A delta_volume = volA - initial_volume;
delta_volume += MorphInit(beta,delta_volume_target-delta_volume); CURRENT_MORPH_TIMESTEPS += analysis_interval;
double massChange = SeedPhaseField(seed_water);
if (rank==0) printf("***Seed water in oil %f, mass change %f ***\n", seed_water, massChange);
}
else{
if (rank==0) printf("***Morphological step with target volume change %f ***\n", delta_volume_target);
//double delta_volume_target = volB - (volA + volB)*TARGET_SATURATION; // change in volume to A
delta_volume += MorphInit(beta,delta_volume_target-delta_volume);
}
if ( (delta_volume - delta_volume_target)/delta_volume_target > 0.0 ){ if ( (delta_volume - delta_volume_target)/delta_volume_target > 0.0 ){
MORPH_ADAPT = false; MORPH_ADAPT = false;
initial_volume = volA;
delta_volume = 0.0; delta_volume = 0.0;
CURRENT_STEADY_TIMESTEPS=0; CURRENT_STEADY_TIMESTEPS=0;
} }
else if (CURRENT_MORPH_TIMESTEPS > MAX_MORPH_TIMESTEPS) { else if (CURRENT_MORPH_TIMESTEPS > MAX_MORPH_TIMESTEPS) {
delta_volume = 0.0; delta_volume = 0.0;
initial_volume = volA;
MORPH_ADAPT = false; MORPH_ADAPT = false;
CURRENT_STEADY_TIMESTEPS=0; CURRENT_STEADY_TIMESTEPS=0;
} }
@ -711,7 +754,6 @@ void ScaLBL_ColorModel::Run(){
//morph_delta *= (-1.0); //morph_delta *= (-1.0);
REVERSE_FLOW_DIRECTION = false; REVERSE_FLOW_DIRECTION = false;
} }
MPI_Barrier(comm); MPI_Barrier(comm);
} }
morph_timesteps += analysis_interval; morph_timesteps += analysis_interval;
@ -739,6 +781,60 @@ void ScaLBL_ColorModel::Run(){
// ************************************************************************ // ************************************************************************
} }
double ScaLBL_ColorModel::SeedPhaseField(const double seed_water_in_oil){
srand(time(NULL));
double mass_loss =0.f;
double count =0.f;
DoubleArray phase(Nx,Ny,Nz);
ScaLBL_CopyToHost(phase.data(), Phi, N*sizeof(double));
for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){
double random_value = double(rand())/ RAND_MAX;
if (Averages->SDs(i,j,k) < 0.f){
// skip
}
else if (phase(i,j,k) > 0.f ){
phase(i,j,k) -= random_value*seed_water_in_oil;
mass_loss += random_value*seed_water_in_oil;
count++;
}
else {
}
}
}
}
count= sumReduce( Dm->Comm, count);
mass_loss= sumReduce( Dm->Comm, mass_loss);
if (rank == 0) printf("Remove mass %f from %f voxels \n",mass_loss,count);
ScaLBL_CopyToDevice(Phi,phase.data(),N*sizeof(double));
FILE *OUTFILE;
sprintf(LocalRankFilename,"Phase.%05i.raw",rank);
OUTFILE = fopen(LocalRankFilename,"wb");
fwrite(phase.data(),8,N,OUTFILE);
fclose(OUTFILE);
// 7. Re-initialize phase field and density
ScaLBL_PhaseField_Init(dvcMap, Phi, Den, Aq, Bq, 0, ScaLBL_Comm->LastExterior(), Np);
ScaLBL_PhaseField_Init(dvcMap, Phi, Den, Aq, Bq, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np);
if (BoundaryCondition >0 ){
if (Dm->kproc()==0){
ScaLBL_SetSlice_z(Phi,1.0,Nx,Ny,Nz,0);
ScaLBL_SetSlice_z(Phi,1.0,Nx,Ny,Nz,1);
ScaLBL_SetSlice_z(Phi,1.0,Nx,Ny,Nz,2);
}
if (Dm->kproc() == nprocz-1){
ScaLBL_SetSlice_z(Phi,-1.0,Nx,Ny,Nz,Nz-1);
ScaLBL_SetSlice_z(Phi,-1.0,Nx,Ny,Nz,Nz-2);
ScaLBL_SetSlice_z(Phi,-1.0,Nx,Ny,Nz,Nz-3);
}
}
return(mass_loss);
}
double ScaLBL_ColorModel::MorphInit(const double beta, const double target_delta_volume){ double ScaLBL_ColorModel::MorphInit(const double beta, const double target_delta_volume){
const RankInfoStruct rank_info(rank,nprocx,nprocy,nprocz); const RankInfoStruct rank_info(rank,nprocx,nprocy,nprocz);
@ -753,6 +849,7 @@ double ScaLBL_ColorModel::MorphInit(const double beta, const double target_delta
Array<char> phase_id(Nx,Ny,Nz); Array<char> phase_id(Nx,Ny,Nz);
fillHalo<double> fillDouble(Dm->Comm,Dm->rank_info,{Nx-2,Ny-2,Nz-2},{1,1,1},0,1); fillHalo<double> fillDouble(Dm->Comm,Dm->rank_info,{Nx-2,Ny-2,Nz-2},{1,1,1},0,1);
// Basic algorithm to // Basic algorithm to
// 1. Copy phase field to CPU // 1. Copy phase field to CPU
ScaLBL_CopyToHost(phase.data(), Phi, N*sizeof(double)); ScaLBL_CopyToHost(phase.data(), Phi, N*sizeof(double));
@ -881,7 +978,9 @@ double ScaLBL_ColorModel::MorphInit(const double beta, const double target_delta
for (int k=1; k<Nz-1; k++){ for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){ for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){ for (int i=1; i<Nx-1; i++){
if (phase(i,j,k) > 0.f && Averages->SDs(i,j,k) > 0.f) count+=1.f; if (phase(i,j,k) > 0.f && Averages->SDs(i,j,k) > 0.f){
count+=1.f;
}
} }
} }
} }

View File

@ -80,6 +80,7 @@ private:
void LoadParams(std::shared_ptr<Database> db0); void LoadParams(std::shared_ptr<Database> db0);
void AssignComponentLabels(double *phase); void AssignComponentLabels(double *phase);
double MorphInit(const double beta, const double morph_delta); double MorphInit(const double beta, const double morph_delta);
double SeedPhaseField(const double seed_water_in_oil);
}; };

View File

@ -22,27 +22,27 @@ void ScaLBL_MRTModel::ReadParams(string filename){
mrt_db = db->getDatabase( "MRT" ); mrt_db = db->getDatabase( "MRT" );
// Color Model parameters // Color Model parameters
if (color_db->keyExists( "timestepMax" )){ if (mrt_db->keyExists( "timestepMax" )){
timestepMax = mrt_db->getScalar<int>( "timestepMax" ); timestepMax = mrt_db->getScalar<int>( "timestepMax" );
} }
if (color_db->keyExists( "tau" )){ if (mrt_db->keyExists( "tau" )){
tau = mrt_db->getScalar<double>( "tau" ); tau = mrt_db->getScalar<double>( "tau" );
} }
if (color_db->keyExists( "F" )){ if (mrt_db->keyExists( "F" )){
Fx = mrt_db->getVector<double>( "F" )[0]; Fx = mrt_db->getVector<double>( "F" )[0];
Fy = mrt_db->getVector<double>( "F" )[1]; Fy = mrt_db->getVector<double>( "F" )[1];
Fz = mrt_db->getVector<double>( "F" )[2]; Fz = mrt_db->getVector<double>( "F" )[2];
} }
if (color_db->keyExists( "Restart" )){ if (mrt_db->keyExists( "Restart" )){
Restart = mrt_db->getScalar<bool>( "Restart" ); Restart = mrt_db->getScalar<bool>( "Restart" );
} }
if (color_db->keyExists( "din" )){ if (mrt_db->keyExists( "din" )){
din = mrt_db->getScalar<double>( "din" ); din = mrt_db->getScalar<double>( "din" );
} }
if (color_db->keyExists( "dout" )){ if (mrt_db->keyExists( "dout" )){
dout = mrt_db->getScalar<double>( "dout" ); dout = mrt_db->getScalar<double>( "dout" );
} }
if (color_db->keyExists( "flux" )){ if (mrt_db->keyExists( "flux" )){
flux = mrt_db->getScalar<double>( "flux" ); flux = mrt_db->getScalar<double>( "flux" );
} }
@ -56,7 +56,15 @@ void ScaLBL_MRTModel::ReadParams(string filename){
void ScaLBL_MRTModel::SetDomain(){ void ScaLBL_MRTModel::SetDomain(){
Dm = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // full domain for analysis Dm = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // full domain for analysis
Mask = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // mask domain removes immobile phases Mask = std::shared_ptr<Domain>(new Domain(domain_db,comm)); // mask domain removes immobile phases
Nx+=2; Ny+=2; Nz += 2;
// domain parameters
Nx = Dm->Nx;
Ny = Dm->Ny;
Nz = Dm->Nz;
Lx = Dm->Lx;
Ly = Dm->Ly;
Lz = Dm->Lz;
N = Nx*Ny*Nz; N = Nx*Ny*Nz;
Distance.resize(Nx,Ny,Nz); Distance.resize(Nx,Ny,Nz);
Velocity_x.resize(Nx,Ny,Nz); Velocity_x.resize(Nx,Ny,Nz);
@ -68,14 +76,8 @@ void ScaLBL_MRTModel::SetDomain(){
MPI_Barrier(comm); MPI_Barrier(comm);
Dm->CommInit(); Dm->CommInit();
MPI_Barrier(comm); MPI_Barrier(comm);
// Read domain parameters
rank = Dm->rank(); rank = Dm->rank();
Nx = Dm->Nx;
Ny = Dm->Ny;
Nz = Dm->Nz;
Lx = Dm->Lx;
Ly = Dm->Ly;
Lz = Dm->Lz;
nprocx = Dm->nprocx(); nprocx = Dm->nprocx();
nprocy = Dm->nprocy(); nprocy = Dm->nprocy();
nprocz = Dm->nprocz(); nprocz = Dm->nprocz();
@ -262,7 +264,7 @@ void ScaLBL_MRTModel::Run(){
printf(" %f\n",absperm); printf(" %f\n",absperm);
FILE * log_file = fopen("Permeability.csv","a"); FILE * log_file = fopen("Permeability.csv","a");
fprintf(log_file,"%i %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g\n",timestep, Fx, Fy, Fz, mu, fprintf(log_file,"%i %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g %.8g\n",timestep, Fx, Fy, Fz, mu,
Vs,As,Hs,Xs,vax,vay,vaz, absperm); h*h*h*Vs,h*h*As,h*Hs,Xs,vax,vay,vaz, absperm);
fclose(log_file); fclose(log_file);
} }
} }

View File

@ -11,7 +11,7 @@ ADD_LBPM_EXECUTABLE( lbpm_dfh_simulator )
ADD_LBPM_EXECUTABLE( lbpm_refine_pp ) ADD_LBPM_EXECUTABLE( lbpm_refine_pp )
ADD_LBPM_EXECUTABLE( lbpm_morphdrain_pp ) ADD_LBPM_EXECUTABLE( lbpm_morphdrain_pp )
ADD_LBPM_EXECUTABLE( lbpm_morphopen_pp ) ADD_LBPM_EXECUTABLE( lbpm_morphopen_pp )
#ADD_LBPM_EXECUTABLE( lbpm_morph_pp ) ADD_LBPM_EXECUTABLE( lbpm_morph_pp )
#ADD_LBPM_EXECUTABLE( lbpm_segmented_pp ) #ADD_LBPM_EXECUTABLE( lbpm_segmented_pp )
#ADD_LBPM_EXECUTABLE( lbpm_block_pp ) #ADD_LBPM_EXECUTABLE( lbpm_block_pp )
#ADD_LBPM_EXECUTABLE( lbpm_segmented_decomp ) #ADD_LBPM_EXECUTABLE( lbpm_segmented_decomp )

View File

@ -39,6 +39,11 @@ int main(int argc, char **argv)
printf("Running Color LBM \n"); printf("Running Color LBM \n");
printf("********************************************************\n"); printf("********************************************************\n");
} }
// Initialize compute device
int device=ScaLBL_SetDevice(rank);
ScaLBL_DeviceBarrier();
MPI_Barrier(comm);
PROFILE_ENABLE(1); PROFILE_ENABLE(1);
//PROFILE_ENABLE_TRACE(); //PROFILE_ENABLE_TRACE();
//PROFILE_ENABLE_MEMORY(); //PROFILE_ENABLE_MEMORY();

View File

@ -11,423 +11,234 @@
#include <sstream> #include <sstream>
#include "common/Array.h" #include "common/Array.h"
#include "common/Domain.h" #include "common/Domain.h"
#include "analysis/distance.h"
#include "analysis/morphology.h"
//************************************************************************* //*************************************************************************
// Morpohological drainage pre-processor // Morpohologica pre-processor
// Generate states on primary drainage using morphological approach // Initialize phase distribution using morphological approach
// Signed distance function is used to determine fluid configuration // Signed distance function is used to determine fluid configuration
//************************************************************************* //*************************************************************************
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<count; idx++){
n = list[idx];
sendbuf[idx] = ID[n];
}
}
//***************************************************************************************
inline void UnpackID(int *list, int count, char *recvbuf, char *ID){
// Fill in the phase ID values from neighboring processors
// This unpacks the values once they have been recieved from neighbors
int idx,n;
for (idx=0; idx<count; idx++){
n = list[idx];
ID[n] = recvbuf[idx];
}
}
//***************************************************************************************
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
// Initialize MPI // Initialize MPI
int rank, nprocs; int rank, nprocs;
MPI_Init(&argc,&argv); MPI_Init(&argc,&argv);
MPI_Comm comm = MPI_COMM_WORLD; MPI_Comm comm = MPI_COMM_WORLD;
MPI_Comm_rank(comm,&rank); MPI_Comm_rank(comm,&rank);
MPI_Comm_size(comm,&nprocs); MPI_Comm_size(comm,&nprocs);
{
//.......................................................................
// Reading the domain information file
//.......................................................................
int nprocx, nprocy, nprocz, nx, ny, nz, nspheres;
double Lx, Ly, Lz;
int i,j,k,n;
int BC=0;
// char fluidValue,solidValue;
int MAXTIME=1000;
int READ_FROM_BLOCK=0;
//double Rcrit; char LocalRankString[8];
double Rcrit=strtod(argv[1],NULL); char LocalRankFilename[40];
if (rank==0){
printf("Critical radius %f (voxels)\n",Rcrit);
//printf("Target saturation %f \n",SW);
}
// }
//....................................................................... string filename;
// Reading the domain information file double Rcrit_new, SW;
//....................................................................... if (argc > 1){
int nprocx, nprocy, nprocz, nx, ny, nz, nspheres; filename=argv[1];
double Lx, Ly, Lz; Rcrit_new=0.f;
int i,j,k,n; //SW=strtod(argv[2],NULL);
int BC=0;
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;
}
MPI_Barrier(comm);
// Computational domain
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);
//.................................................
MPI_Barrier(comm);
// 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");
}
char LocalRankFilename[40];
int BoundaryCondition=1;
Domain Dm(nx,ny,nz,rank,nprocx,nprocy,nprocz,Lx,Ly,Lz,BoundaryCondition);
nx+=2; ny+=2; nz+=2;
int N = nx*ny*nz;
char *id;
id = new char[N];
// Define communication sub-domain -- everywhere
for (int k=0; k<nz; k++){
for (int j=0; j<ny; j++){
for (int i=0; i<nx; i++){
n = k*nx*ny+j*nx+i;
Dm.id[n] = 1;
}
} }
} else ERROR("No input database provided\n");
/*// Exclude the maximum / minimum z rows // read the input database
if (rank/(nprocx*nprocy)==0){ auto db = std::make_shared<Database>( filename );
int k=0; auto domain_db = db->getDatabase( "Domain" );
for (int j=0; j<ny; j++){
for (int i=0; i<nx; i++){
n = k*nx*ny+j*nx+i;
Dm.id[n] = 0;
}
}
}
if (rank/(nprocx*nprocy)==nprocz-1){
int k=nz-1;
for (int j=0; j<ny; j++){
for (int i=0; i<nx; i++){
n = k*nx*ny+j*nx+i;
Dm.id[n] = 0;
}
}
}
*/
Dm.CommInit(comm);
DoubleArray SignDist(nx,ny,nz); // Read domain parameters
// Read the signed distance from file auto L = domain_db->getVector<double>( "L" );
sprintf(LocalRankFilename,"SignDist.%05i",rank); auto size = domain_db->getVector<int>( "n" );
FILE *DIST = fopen(LocalRankFilename,"rb"); auto nproc = domain_db->getVector<int>( "nproc" );
size_t ReadSignDist; auto ReadValues = domain_db->getVector<int>( "ReadValues" );
ReadSignDist=fread(SignDist.data(),8,N,DIST); auto WriteValues = domain_db->getVector<int>( "WriteValues" );
if (ReadSignDist != size_t(N)) printf("lbpm_morphdrain_pp: Error reading signed distance function (rank=%i)\n",rank); SW = domain_db->getScalar<double>("Sw");
fclose(DIST);
sprintf(LocalRankFilename,"ID.%05i",rank); // Generate the NWP configuration
size_t readID; //if (rank==0) printf("Initializing morphological distribution with critical radius %f \n", Rcrit);
FILE *IDFILE = fopen(LocalRankFilename,"rb"); if (rank==0) printf("Performing morphological imbibition with target saturation %f \n", SW);
if (IDFILE==NULL) ERROR("Error opening file: ID.xxxxx"); // GenerateResidual(id,nx,ny,nz,Saturation);
readID=fread(id,1,N,IDFILE);
if (readID != size_t(N)) printf("lbpm_segmented_pp: Error reading ID (rank=%i) \n",rank);
fclose(IDFILE);
nx = size[0];
ny = size[1];
nz = size[2];
nprocx = nproc[0];
nprocy = nproc[1];
nprocz = nproc[2];
Dm.CommInit(comm); int N = (nx+2)*(ny+2)*(nz+2);
int iproc = Dm.iproc();
int jproc = Dm.jproc();
int kproc = Dm.kproc();
// Generate the NWP configuration std::shared_ptr<Domain> Dm (new Domain(domain_db,comm));
//if (rank==0) printf("Performing morphological drainage with critical radius %f \n", Rcrit); // std::shared_ptr<Domain> Dm (new Domain(nx,ny,nz,rank,nprocx,nprocy,nprocz,Lx,Ly,Lz,BC));
//if (rank==0) printf("Performing morphological drainage with target saturation %f \n", SW); for (n=0; n<N; n++) Dm->id[n]=1;
// GenerateResidual(id,nx,ny,nz,Saturation); Dm->CommInit();
// Communication buffers signed char *id;
char *sendID_x, *sendID_y, *sendID_z, *sendID_X, *sendID_Y, *sendID_Z; signed char *id_connected;
char *sendID_xy, *sendID_yz, *sendID_xz, *sendID_Xy, *sendID_Yz, *sendID_xZ; id = new signed char [N];
char *sendID_xY, *sendID_yZ, *sendID_Xz, *sendID_XY, *sendID_YZ, *sendID_XZ; id_connected = new signed char [N];
char *recvID_x, *recvID_y, *recvID_z, *recvID_X, *recvID_Y, *recvID_Z; sprintf(LocalRankFilename,"ID.%05i",rank);
char *recvID_xy, *recvID_yz, *recvID_xz, *recvID_Xy, *recvID_Yz, *recvID_xZ; size_t readID;
char *recvID_xY, *recvID_yZ, *recvID_Xz, *recvID_XY, *recvID_YZ, *recvID_XZ; FILE *IDFILE = fopen(LocalRankFilename,"rb");
// send buffers if (IDFILE==NULL) ERROR("Error opening file: ID.xxxxx");
sendID_x = new char [Dm.sendCount_x]; readID=fread(id,1,N,IDFILE);
sendID_y = new char [Dm.sendCount_y]; if (readID != size_t(N)) printf("lbpm_morph_pp: Error reading ID (rank=%i) \n",rank);
sendID_z = new char [Dm.sendCount_z]; fclose(IDFILE);
sendID_X = new char [Dm.sendCount_X];
sendID_Y = new char [Dm.sendCount_Y];
sendID_Z = new char [Dm.sendCount_Z];
sendID_xy = new char [Dm.sendCount_xy];
sendID_yz = new char [Dm.sendCount_yz];
sendID_xz = new char [Dm.sendCount_xz];
sendID_Xy = new char [Dm.sendCount_Xy];
sendID_Yz = new char [Dm.sendCount_Yz];
sendID_xZ = new char [Dm.sendCount_xZ];
sendID_xY = new char [Dm.sendCount_xY];
sendID_yZ = new char [Dm.sendCount_yZ];
sendID_Xz = new char [Dm.sendCount_Xz];
sendID_XY = new char [Dm.sendCount_XY];
sendID_YZ = new char [Dm.sendCount_YZ];
sendID_XZ = new char [Dm.sendCount_XZ];
//......................................................................................
// recv buffers
recvID_x = new char [Dm.recvCount_x];
recvID_y = new char [Dm.recvCount_y];
recvID_z = new char [Dm.recvCount_z];
recvID_X = new char [Dm.recvCount_X];
recvID_Y = new char [Dm.recvCount_Y];
recvID_Z = new char [Dm.recvCount_Z];
recvID_xy = new char [Dm.recvCount_xy];
recvID_yz = new char [Dm.recvCount_yz];
recvID_xz = new char [Dm.recvCount_xz];
recvID_Xy = new char [Dm.recvCount_Xy];
recvID_xZ = new char [Dm.recvCount_xZ];
recvID_xY = new char [Dm.recvCount_xY];
recvID_yZ = new char [Dm.recvCount_yZ];
recvID_Yz = new char [Dm.recvCount_Yz];
recvID_Xz = new char [Dm.recvCount_Xz];
recvID_XY = new char [Dm.recvCount_XY];
recvID_YZ = new char [Dm.recvCount_YZ];
recvID_XZ = new char [Dm.recvCount_XZ];
//......................................................................................
int sendtag,recvtag;
sendtag = recvtag = 7;
int x,y,z; nx+=2; ny+=2; nz+=2;
int ii,jj,kk; // Generate the signed distance map
int Nx = nx; // Initialize the domain and communication
int Ny = ny; Array<char> id_solid(nx,ny,nz);
int Nz = nz; Array<int> phase_label(nx,ny,nz);
double GlobalNumber = 1.f; DoubleArray SignDist(nx,ny,nz);
DoubleArray phase(nx,ny,nz);
double count,countGlobal,totalGlobal,count_wet_global; for (int k=0; k<nz; k++){
count = 0.f; for (int j=0; j<ny; j++){
double count_wet= 0.f; for (int i=0; i<nx; i++){
for (int k=0; k<nz; k++){ n=k*nx*ny+j*nx+i;
for (int j=0; j<ny; j++){ if (id[n] == 1){
for (int i=0; i<nx; i++){ phase(i,j,k) = 1.0;
n = k*nx*ny+j*nx+i;
if (SignDist(i,j,k) < 0.0) id[n] = 0;
else{
// initially saturated with wetting phase
//id[n] = 2;
count+=1.0;
if (id[n] == 2) count_wet+=1.0;
}
}
}
}
// total Global is the number of nodes in the pore-space
MPI_Allreduce(&count,&totalGlobal,1,MPI_DOUBLE,MPI_SUM,comm);
MPI_Allreduce(&count_wet,&count_wet_global,1,MPI_DOUBLE,MPI_SUM,comm);
double porosity=totalGlobal/(double(nprocx*nprocy*nprocz)*double(nx-2)*double(ny-2)*double(nz-2));
if (rank==0) printf("Media Porosity: %f \n",porosity);
if (rank==0) printf("Initial saturation: %f \n",count_wet_global/totalGlobal);
if (rank==0) printf("Starting morhpological drainage with critical radius = %f \n",Rcrit);
int imin,jmin,kmin,imax,jmax,kmax;
int Window=round(Rcrit);
GlobalNumber = 1.0;
while (GlobalNumber > 0){
// Layer the inlet with NWP
if (kproc == 0){
for(j=0; j<Ny; j++){
for(i=0; i<Nx; i++){
n = j*nx+i;
// n = nx*ny + j*nx+i;
if (id[n] > 0) id[n]=1;
}
}
}
// Layer the outlet with WP
if (kproc == nprocz-1){
for(j=0; j<Ny; j++){
for(i=0; i<Nx; i++){
n = (nz-1)*nx*ny+j*nx+i;
// n = nx*ny + j*nx+i;
if (id[n] > 0) id[n]=2;
}
}
}
if (rank==0) printf("GlobalNumber=%f \n",GlobalNumber);
double LocalNumber=GlobalNumber=0.f;
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;
if (id[n] == 1 && SignDist(i,j,k) > Rcrit){
// loop over the window and update
imin=max(1,i-Window);
jmin=max(1,j-Window);
kmin=max(1,k-Window);
imax=min(Nx-1,i+Window);
jmax=min(Ny-1,j+Window);
kmax=min(Nz-1,k+Window);
for (kk=kmin; kk<kmax; kk++){
for (jj=jmin; jj<jmax; jj++){
for (ii=imin; ii<imax; ii++){
int nn = kk*nx*ny+jj*nx+ii;
double dsq = double((ii-i)*(ii-i)+(jj-j)*(jj-j)+(kk-k)*(kk-k));
if (id[nn] == 2 && dsq <= Rcrit*Rcrit){
LocalNumber+=1.0;
id[nn]=1;
}
}
}
}
} }
// move on else
phase(i,j,k) = -1.0;
} }
} }
} }
// Solve for the position of the solid phase
// Pack and send the updated ID values for (int k=0;k<nz;k++){
PackID(Dm.sendList_x, Dm.sendCount_x ,sendID_x, id); for (int j=0;j<ny;j++){
PackID(Dm.sendList_X, Dm.sendCount_X ,sendID_X, id); for (int i=0;i<nx;i++){
PackID(Dm.sendList_y, Dm.sendCount_y ,sendID_y, id); int n = k*nx*ny+j*nx+i;
PackID(Dm.sendList_Y, Dm.sendCount_Y ,sendID_Y, id); // Initialize the solid phase
PackID(Dm.sendList_z, Dm.sendCount_z ,sendID_z, id); if (id[n] > 0) id_solid(i,j,k) = 1;
PackID(Dm.sendList_Z, Dm.sendCount_Z ,sendID_Z, id); else id_solid(i,j,k) = 0;
PackID(Dm.sendList_xy, Dm.sendCount_xy ,sendID_xy, id);
PackID(Dm.sendList_Xy, Dm.sendCount_Xy ,sendID_Xy, id);
PackID(Dm.sendList_xY, Dm.sendCount_xY ,sendID_xY, id);
PackID(Dm.sendList_XY, Dm.sendCount_XY ,sendID_XY, id);
PackID(Dm.sendList_xz, Dm.sendCount_xz ,sendID_xz, id);
PackID(Dm.sendList_Xz, Dm.sendCount_Xz ,sendID_Xz, id);
PackID(Dm.sendList_xZ, Dm.sendCount_xZ ,sendID_xZ, id);
PackID(Dm.sendList_XZ, Dm.sendCount_XZ ,sendID_XZ, id);
PackID(Dm.sendList_yz, Dm.sendCount_yz ,sendID_yz, id);
PackID(Dm.sendList_Yz, Dm.sendCount_Yz ,sendID_Yz, id);
PackID(Dm.sendList_yZ, Dm.sendCount_yZ ,sendID_yZ, id);
PackID(Dm.sendList_YZ, Dm.sendCount_YZ ,sendID_YZ, id);
//......................................................................................
MPI_Sendrecv(sendID_x,Dm.sendCount_x,MPI_CHAR,Dm.rank_x(),sendtag,
recvID_X,Dm.recvCount_X,MPI_CHAR,Dm.rank_X(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_X,Dm.sendCount_X,MPI_CHAR,Dm.rank_X(),sendtag,
recvID_x,Dm.recvCount_x,MPI_CHAR,Dm.rank_x(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_y,Dm.sendCount_y,MPI_CHAR,Dm.rank_y(),sendtag,
recvID_Y,Dm.recvCount_Y,MPI_CHAR,Dm.rank_Y(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_Y,Dm.sendCount_Y,MPI_CHAR,Dm.rank_Y(),sendtag,
recvID_y,Dm.recvCount_y,MPI_CHAR,Dm.rank_y(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_z,Dm.sendCount_z,MPI_CHAR,Dm.rank_z(),sendtag,
recvID_Z,Dm.recvCount_Z,MPI_CHAR,Dm.rank_Z(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_Z,Dm.sendCount_Z,MPI_CHAR,Dm.rank_Z(),sendtag,
recvID_z,Dm.recvCount_z,MPI_CHAR,Dm.rank_z(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_xy,Dm.sendCount_xy,MPI_CHAR,Dm.rank_xy(),sendtag,
recvID_XY,Dm.recvCount_XY,MPI_CHAR,Dm.rank_XY(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_XY,Dm.sendCount_XY,MPI_CHAR,Dm.rank_XY(),sendtag,
recvID_xy,Dm.recvCount_xy,MPI_CHAR,Dm.rank_xy(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_Xy,Dm.sendCount_Xy,MPI_CHAR,Dm.rank_Xy(),sendtag,
recvID_xY,Dm.recvCount_xY,MPI_CHAR,Dm.rank_xY(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_xY,Dm.sendCount_xY,MPI_CHAR,Dm.rank_xY(),sendtag,
recvID_Xy,Dm.recvCount_Xy,MPI_CHAR,Dm.rank_Xy(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_xz,Dm.sendCount_xz,MPI_CHAR,Dm.rank_xz(),sendtag,
recvID_XZ,Dm.recvCount_XZ,MPI_CHAR,Dm.rank_XZ(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_XZ,Dm.sendCount_XZ,MPI_CHAR,Dm.rank_XZ(),sendtag,
recvID_xz,Dm.recvCount_xz,MPI_CHAR,Dm.rank_xz(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_Xz,Dm.sendCount_Xz,MPI_CHAR,Dm.rank_Xz(),sendtag,
recvID_xZ,Dm.recvCount_xZ,MPI_CHAR,Dm.rank_xZ(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_xZ,Dm.sendCount_xZ,MPI_CHAR,Dm.rank_xZ(),sendtag,
recvID_Xz,Dm.recvCount_Xz,MPI_CHAR,Dm.rank_Xz(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_yz,Dm.sendCount_yz,MPI_CHAR,Dm.rank_yz(),sendtag,
recvID_YZ,Dm.recvCount_YZ,MPI_CHAR,Dm.rank_YZ(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_YZ,Dm.sendCount_YZ,MPI_CHAR,Dm.rank_YZ(),sendtag,
recvID_yz,Dm.recvCount_yz,MPI_CHAR,Dm.rank_yz(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_Yz,Dm.sendCount_Yz,MPI_CHAR,Dm.rank_Yz(),sendtag,
recvID_yZ,Dm.recvCount_yZ,MPI_CHAR,Dm.rank_yZ(),recvtag,comm,MPI_STATUS_IGNORE);
MPI_Sendrecv(sendID_yZ,Dm.sendCount_yZ,MPI_CHAR,Dm.rank_yZ(),sendtag,
recvID_Yz,Dm.recvCount_Yz,MPI_CHAR,Dm.rank_Yz(),recvtag,comm,MPI_STATUS_IGNORE);
UnpackID(Dm.recvList_x, Dm.recvCount_x ,recvID_x, id);
UnpackID(Dm.recvList_X, Dm.recvCount_X ,recvID_X, id);
UnpackID(Dm.recvList_y, Dm.recvCount_y ,recvID_y, id);
UnpackID(Dm.recvList_Y, Dm.recvCount_Y ,recvID_Y, id);
UnpackID(Dm.recvList_z, Dm.recvCount_z ,recvID_z, id);
UnpackID(Dm.recvList_Z, Dm.recvCount_Z ,recvID_Z, id);
UnpackID(Dm.recvList_xy, Dm.recvCount_xy ,recvID_xy, id);
UnpackID(Dm.recvList_Xy, Dm.recvCount_Xy ,recvID_Xy, id);
UnpackID(Dm.recvList_xY, Dm.recvCount_xY ,recvID_xY, id);
UnpackID(Dm.recvList_XY, Dm.recvCount_XY ,recvID_XY, id);
UnpackID(Dm.recvList_xz, Dm.recvCount_xz ,recvID_xz, id);
UnpackID(Dm.recvList_Xz, Dm.recvCount_Xz ,recvID_Xz, id);
UnpackID(Dm.recvList_xZ, Dm.recvCount_xZ ,recvID_xZ, id);
UnpackID(Dm.recvList_XZ, Dm.recvCount_XZ ,recvID_XZ, id);
UnpackID(Dm.recvList_yz, Dm.recvCount_yz ,recvID_yz, id);
UnpackID(Dm.recvList_Yz, Dm.recvCount_Yz ,recvID_Yz, id);
UnpackID(Dm.recvList_yZ, Dm.recvCount_yZ ,recvID_yZ, id);
UnpackID(Dm.recvList_YZ, Dm.recvCount_YZ ,recvID_YZ, id);
//......................................................................................
MPI_Allreduce(&LocalNumber,&GlobalNumber,1,MPI_DOUBLE,MPI_SUM,comm);
}
count = 1.f;
for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){
n=k*Nx*Ny+j*Nx+i;
if (id[n] == 2){
count+=1.0;
} }
} }
} }
// Initialize the signed distance function
for (int k=0;k<nz;k++){
for (int j=0;j<ny;j++){
for (int i=0;i<nx;i++){
int n = k*nx*ny+j*nx+i;
// Initialize distance to +/- 1
SignDist(i,j,k) = 2.0*double(id_solid(i,j,k))-1.0;
}
}
}
if (rank==0) printf("Initialized solid phase -- Converting to Signed Distance function \n");
CalcDist(SignDist,id_solid,*Dm);
MPI_Barrier(comm);
// Extract only the connected part of NWP
BlobIDstruct new_index;
double vF=0.0; double vS=0.0;
ComputeGlobalBlobIDs(nx-2,ny-2,nz-2,Dm->rank_info,phase,SignDist,vF,vS,phase_label,Dm->Comm);
MPI_Barrier(Dm->Comm);
int count_connected=0;
int count_porespace=0;
int count_water=0;
for (int k=1; k<nz-1; k++){
for (int j=1; j<ny-1; j++){
for (int i=1; i<nx-1; i++){
n=k*nx*ny+j*nx+i;
// only apply opening to connected component
if ( phase_label(i,j,k) == 0){
count_connected++;
}
if (id[n] > 0){
count_porespace++;
}
if (id[n] == 2){
count_water++;
}
}
}
}
count_connected=sumReduce( Dm->Comm, count_connected);
count_porespace=sumReduce( Dm->Comm, count_porespace);
count_water=sumReduce( Dm->Comm, count_water);
for (int k=0; k<nz; k++){
for (int j=0; j<ny; j++){
for (int i=0; i<nx; i++){
n=k*nx*ny+j*nx+i;
// only apply opening to connected component
if ( phase_label(i,j,k) == 0){
id_solid(i,j,k) = 1;
id_connected[n] = 2;
id[n] = 2;
}
else{
id_solid(i,j,k) = 0;
id_connected[n] = 0;
}
}
}
}
CalcDist(SignDist,id_solid,*Dm);
// target water increase in voxels, normalized by connected volume
double St = (SW*count_porespace - count_water)/count_porespace;
signed char water=2;
signed char notwater=1;
// Run the morphological opening
if (St > 0.0)
MorphOpen(SignDist, id_connected, Dm, St, water, notwater);
else {
if(rank==0) printf("Initial condition satisfies condition for saturation target \n");
}
// re-label
for (int k=0; k<nz; k++){
for (int j=0; j<ny; j++){
for (int i=0; i<nx; i++){
n=k*nx*ny+j*nx+i;
// only apply opening to connected component
if ( id_connected[n] == 1){
id[n] = 1;
}
}
}
}
count_water=0;
for (int k=1; k<nz-1; k++){
for (int j=1; j<ny-1; j++){
for (int i=1; i<nx-1; i++){
n=k*nx*ny+j*nx+i;
if (id[n] == 2){
count_water++;
}
}
}
}
count_water=sumReduce( Dm->Comm, count_water);
SW = double(count_water) / count_porespace;
if(rank==0) printf("Final saturation: %f \n", SW);
if (rank==0) printf("Writing ID file \n");
sprintf(LocalRankFilename,"ID.%05i",rank);
FILE *ID = fopen(LocalRankFilename,"wb");
fwrite(id,1,N,ID);
fclose(ID);
} }
MPI_Allreduce(&count,&countGlobal,1,MPI_DOUBLE,MPI_SUM,comm);
double sw = countGlobal/totalGlobal;
if (rank==0){
printf("Final saturation=%f\n",sw);
}
sprintf(LocalRankFilename,"ID.%05i",rank);
FILE *ID = fopen(LocalRankFilename,"wb");
fwrite(id,1,N,ID);
fclose(ID);
MPI_Barrier(comm); MPI_Barrier(comm);
MPI_Finalize(); MPI_Finalize();

View File

@ -173,7 +173,7 @@ int main(int argc, char **argv)
signed char NEWVALUE=HistoryLabels[idx]; signed char NEWVALUE=HistoryLabels[idx];
if (LOCVAL == VALUE){ if (LOCVAL == VALUE){
idx = NLABELS; idx = NLABELS;
if (SignDist(i,j,k) < 1.0){ if (SignDist(i,j,k) < 2.0){
id[n] = NEWVALUE; id[n] = NEWVALUE;
} }
} }

View File

@ -62,7 +62,14 @@ int main(int argc, char **argv)
auto ReadValues = domain_db->getVector<int>( "ReadValues" ); auto ReadValues = domain_db->getVector<int>( "ReadValues" );
auto WriteValues = domain_db->getVector<int>( "WriteValues" ); auto WriteValues = domain_db->getVector<int>( "WriteValues" );
SW = domain_db->getScalar<double>("Sw"); SW = domain_db->getScalar<double>("Sw");
signed char ErodeLabel=2;
signed char OpenLabel=1;
if (domain_db->keyExists( "OpenLabel" )){
OpenLabel = domain_db->getScalar<int>("OpenLabel");
}
if (domain_db->keyExists( "ErodeLabel" )){
ErodeLabel = domain_db->getScalar<int>("ErodeLabel");
}
// Generate the NWP configuration // Generate the NWP configuration
//if (rank==0) printf("Initializing morphological distribution with critical radius %f \n", Rcrit); //if (rank==0) printf("Initializing morphological distribution with critical radius %f \n", Rcrit);
if (rank==0) printf("Performing morphological opening with target saturation %f \n", SW); if (rank==0) printf("Performing morphological opening with target saturation %f \n", SW);
@ -126,7 +133,7 @@ int main(int argc, char **argv)
MPI_Barrier(comm); MPI_Barrier(comm);
// Run the morphological opening // Run the morphological opening
MorphOpen(SignDist, id, Dm, SW); MorphOpen(SignDist, id, Dm, SW, ErodeLabel, OpenLabel);
// calculate distance to non-wetting fluid // calculate distance to non-wetting fluid
if (domain_db->keyExists( "HistoryLabels" )){ if (domain_db->keyExists( "HistoryLabels" )){
@ -173,7 +180,7 @@ int main(int argc, char **argv)
signed char NEWVALUE=HistoryLabels[idx]; signed char NEWVALUE=HistoryLabels[idx];
if (LOCVAL == VALUE){ if (LOCVAL == VALUE){
idx = NLABELS; idx = NLABELS;
if (SignDist(i,j,k) < 1.0){ if (SignDist(i,j,k) < 2.0){
id[n] = NEWVALUE; id[n] = NEWVALUE;
} }
} }

View File

@ -42,6 +42,11 @@ int main(int argc, char **argv)
printf("Running Single Phase Permeability Calculation \n"); printf("Running Single Phase Permeability Calculation \n");
printf("********************************************************\n"); printf("********************************************************\n");
} }
// Initialize compute device
int device=ScaLBL_SetDevice(rank);
ScaLBL_DeviceBarrier();
MPI_Barrier(comm);
ScaLBL_MRTModel MRT(rank,nprocs,comm); ScaLBL_MRTModel MRT(rank,nprocs,comm);
auto filename = argv[1]; auto filename = argv[1];