LBPM/models/ColorModel.cpp

889 lines
31 KiB
C++
Raw Normal View History

2018-05-16 13:40:38 -05:00
/*
color lattice boltzmann model
*/
2018-05-16 13:50:40 -05:00
#include "models/ColorModel.h"
#include "analysis/distance.h"
2018-12-05 12:09:00 -06:00
#include "analysis/morphology.h"
2018-05-16 13:40:38 -05:00
2018-05-18 16:24:17 -05:00
ScaLBL_ColorModel::ScaLBL_ColorModel(int RANK, int NP, MPI_Comm COMM):
2018-05-19 13:02:45 -05:00
rank(RANK), nprocs(NP), Restart(0),timestep(0),timestepMax(0),tauA(0),tauB(0),rhoA(0),rhoB(0),alpha(0),beta(0),
Fx(0),Fy(0),Fz(0),flux(0),din(0),dout(0),inletA(0),inletB(0),outletA(0),outletB(0),
Nx(0),Ny(0),Nz(0),N(0),Np(0),nprocx(0),nprocy(0),nprocz(0),BoundaryCondition(0),Lx(0),Ly(0),Lz(0),comm(COMM)
2018-05-16 14:08:47 -05:00
{
REVERSE_FLOW_DIRECTION = false;
2018-05-16 13:54:16 -05:00
}
ScaLBL_ColorModel::~ScaLBL_ColorModel(){
2018-05-19 13:02:45 -05:00
2018-05-16 13:54:16 -05:00
}
2018-06-27 15:31:03 -05:00
/*void ScaLBL_ColorModel::WriteCheckpoint(const char *FILENAME, const double *cPhi, const double *cfq, int Np)
{
int q,n;
double value;
ofstream File(FILENAME,ios::binary);
for (n=0; n<Np; n++){
// Write the two density values
value = cPhi[n];
File.write((char*) &value, sizeof(value));
// Write the even distributions
for (q=0; q<19; q++){
value = cfq[q*Np+n];
File.write((char*) &value, sizeof(value));
}
}
File.close();
}
void ScaLBL_ColorModel::ReadCheckpoint(char *FILENAME, double *cPhi, double *cfq, int Np)
{
int q=0, n=0;
double value=0;
ifstream File(FILENAME,ios::binary);
for (n=0; n<Np; n++){
File.read((char*) &value, sizeof(value));
cPhi[n] = value;
// Read the distributions
for (q=0; q<19; q++){
File.read((char*) &value, sizeof(value));
cfq[q*Np+n] = value;
}
}
File.close();
}
2018-07-01 16:13:27 -05:00
*/
2018-06-27 15:31:03 -05:00
2018-05-16 14:08:47 -05:00
void ScaLBL_ColorModel::ReadParams(string filename){
2018-05-16 13:40:38 -05:00
// read the input database
2018-05-19 13:02:45 -05:00
db = std::make_shared<Database>( filename );
domain_db = db->getDatabase( "Domain" );
color_db = db->getDatabase( "Color" );
analysis_db = db->getDatabase( "Analysis" );
// Color Model parameters
timestepMax = color_db->getScalar<int>( "timestepMax" );
tauA = color_db->getScalar<double>( "tauA" );
tauB = color_db->getScalar<double>( "tauB" );
rhoA = color_db->getScalar<double>( "rhoA" );
rhoB = color_db->getScalar<double>( "rhoB" );
Fx = color_db->getVector<double>( "F" )[0];
Fy = color_db->getVector<double>( "F" )[1];
Fz = color_db->getVector<double>( "F" )[2];
alpha = color_db->getScalar<double>( "alpha" );
beta = color_db->getScalar<double>( "beta" );
Restart = color_db->getScalar<bool>( "Restart" );
din = color_db->getScalar<double>( "din" );
dout = color_db->getScalar<double>( "dout" );
flux = color_db->getScalar<double>( "flux" );
inletA=1.f;
inletB=0.f;
outletA=0.f;
outletB=1.f;
2018-07-01 16:13:27 -05:00
2018-05-19 13:02:45 -05:00
// Read domain parameters
auto L = domain_db->getVector<double>( "L" );
auto size = domain_db->getVector<int>( "n" );
auto nproc = domain_db->getVector<int>( "nproc" );
BoundaryCondition = domain_db->getScalar<int>( "BC" );
Nx = size[0];
Ny = size[1];
Nz = size[2];
Lx = L[0];
Ly = L[1];
Lz = L[2];
nprocx = nproc[0];
nprocy = nproc[1];
nprocz = nproc[2];
if (BoundaryCondition==4) flux *= rhoA; // mass flux must adjust for density (see formulation for details)
2018-05-19 13:02:45 -05:00
2018-05-19 16:28:39 -05:00
}
void ScaLBL_ColorModel::SetDomain(){
2018-05-19 13:02:45 -05:00
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
Nx+=2; Ny+=2; Nz += 2;
N = Nx*Ny*Nz;
2019-03-29 05:56:24 -05:00
id = new signed char [N];
2018-05-19 13:02:45 -05:00
for (int i=0; i<Nx*Ny*Nz; i++) Dm->id[i] = 1; // initialize this way
2019-03-19 15:36:02 -05:00
//Averages = std::shared_ptr<TwoPhase> ( new TwoPhase(Dm) ); // TwoPhase analysis object
Averages = std::shared_ptr<SubPhase> ( new SubPhase(Dm) ); // TwoPhase analysis object
2018-05-19 13:02:45 -05:00
MPI_Barrier(comm);
Dm->CommInit();
MPI_Barrier(comm);
2018-06-13 09:09:55 -05:00
rank = Dm->rank();
2018-05-16 13:40:38 -05:00
}
void ScaLBL_ColorModel::ReadInput(){
2018-07-01 16:13:27 -05:00
size_t readID;
Mask->ReadIDs();
for (int i=0; i<Nx*Ny*Nz; i++) id[i] = Mask->id[i]; // save what was read
sprintf(LocalRankString,"%05d",rank);
sprintf(LocalRankFilename,"%s%s","ID.",LocalRankString);
sprintf(LocalRestartFile,"%s%s","Restart.",LocalRankString);
// Generate the signed distance map
// Initialize the domain and communication
Array<char> id_solid(Nx,Ny,Nz);
int count = 0;
// Solve for the position of the solid phase
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 the solid phase
if (Mask->id[n] > 0) id_solid(i,j,k) = 1;
else id_solid(i,j,k) = 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
Averages->SDs(i,j,k) = 2.0*double(id_solid(i,j,k))-1.0;
}
}
}
// MeanFilter(Averages->SDs);
if (rank==0) printf("Initialized solid phase -- Converting to Signed Distance function \n");
CalcDist(Averages->SDs,id_solid,*Mask);
2018-07-01 16:13:27 -05:00
if (rank == 0) cout << "Domain set." << endl;
2018-10-24 12:26:37 -05:00
2019-03-19 15:36:02 -05:00
Averages->SetParams(rhoA,rhoB,tauA,tauB,Fx,Fy,Fz,alpha,beta);
2018-05-16 13:40:38 -05:00
}
2018-06-27 15:31:03 -05:00
2018-05-19 06:01:09 -05:00
void ScaLBL_ColorModel::AssignComponentLabels(double *phase)
2018-05-18 19:53:49 -05:00
{
size_t NLABELS=0;
2019-03-29 04:23:49 -05:00
signed char VALUE=0;
2018-05-18 19:53:49 -05:00
double AFFINITY=0.f;
2018-07-01 16:13:27 -05:00
2019-03-29 04:23:49 -05:00
auto LabelList = color_db->getVector<int>( "ComponentLabels" );
2018-06-13 07:10:06 -05:00
auto AffinityList = color_db->getVector<double>( "ComponentAffinity" );
2018-07-01 16:13:27 -05:00
2018-06-13 07:10:06 -05:00
NLABELS=LabelList.size();
if (NLABELS != AffinityList.size()){
ERROR("Error: ComponentLabels and ComponentAffinity must be the same length! \n");
2018-05-18 19:53:49 -05:00
}
2018-07-01 16:13:27 -05:00
2018-12-06 06:29:36 -06:00
double label_count[NLABELS];
double label_count_global[NLABELS];
2018-05-18 19:53:49 -05:00
// Assign the labels
for (int idx=0; idx<NLABELS; idx++) label_count[idx]=0;
2018-05-18 19:53:49 -05:00
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;
2018-07-01 16:13:27 -05:00
VALUE=id[n];
2018-05-18 19:53:49 -05:00
// Assign the affinity from the paired list
for (unsigned int idx=0; idx < NLABELS; idx++){
//printf("idx=%i, value=%i, %i, \n",idx, VALUE,LabelList[idx]);
2018-05-18 19:53:49 -05:00
if (VALUE == LabelList[idx]){
AFFINITY=AffinityList[idx];
2018-12-06 06:29:36 -06:00
label_count[idx] += 1.0;
2018-05-18 19:53:49 -05:00
idx = NLABELS;
2018-07-01 16:13:27 -05:00
Mask->id[n] = 0; // set mask to zero since this is an immobile component
2018-05-18 19:53:49 -05:00
}
}
2018-07-02 12:32:25 -05:00
// fluid labels are reserved
if (VALUE == 1) AFFINITY=1.0;
else if (VALUE == 2) AFFINITY=-1.0;
2018-05-18 19:53:49 -05:00
phase[n] = AFFINITY;
}
}
}
2018-07-01 16:13:27 -05:00
// Set Dm to match Mask
for (int i=0; i<Nx*Ny*Nz; i++) Dm->id[i] = Mask->id[i];
2018-12-06 06:29:36 -06:00
for (int idx=0; idx<NLABELS; idx++) label_count_global[idx]=sumReduce( Dm->Comm, label_count[idx]);
if (rank==0){
2019-03-24 19:48:34 -05:00
printf("Component labels: %lu \n",NLABELS);
for (unsigned int idx=0; idx<NLABELS; idx++){
VALUE=LabelList[idx];
AFFINITY=AffinityList[idx];
2019-01-21 12:26:28 -06:00
double volume_fraction = double(label_count_global[idx])/double((Nx-2)*(Ny-2)*(Nz-2)*nprocs);
2019-03-29 04:23:49 -05:00
printf(" label=%d, affinity=%f, volume fraction==%f\n",VALUE,AFFINITY,volume_fraction);
}
}
2018-05-18 19:53:49 -05:00
}
2018-05-16 13:40:38 -05:00
void ScaLBL_ColorModel::Create(){
/*
* This function creates the variables needed to run a LBM
*/
2018-05-18 19:53:49 -05:00
//.........................................................
// don't perform computations at the eight corners
2018-05-21 09:13:23 -05:00
//id[0] = id[Nx-1] = id[(Ny-1)*Nx] = id[(Ny-1)*Nx + Nx-1] = 0;
//id[(Nz-1)*Nx*Ny] = id[(Nz-1)*Nx*Ny+Nx-1] = id[(Nz-1)*Nx*Ny+(Ny-1)*Nx] = id[(Nz-1)*Nx*Ny+(Ny-1)*Nx + Nx-1] = 0;
2018-07-01 16:13:27 -05:00
2018-05-18 19:53:49 -05:00
//.........................................................
// Initialize communication structures in averaging domain
2018-05-19 16:28:39 -05:00
for (int i=0; i<Nx*Ny*Nz; i++) Dm->id[i] = Mask->id[i];
2018-05-19 06:01:09 -05:00
Mask->CommInit();
2018-05-19 06:14:37 -05:00
Np=Mask->PoreCount();
2018-05-18 19:53:49 -05:00
//...........................................................................
if (rank==0) printf ("Create ScaLBL_Communicator \n");
// Create a communicator for the device (will use optimized layout)
// ScaLBL_Communicator ScaLBL_Comm(Mask); // original
ScaLBL_Comm = std::shared_ptr<ScaLBL_Communicator>(new ScaLBL_Communicator(Mask));
2018-07-02 12:32:25 -05:00
ScaLBL_Comm_Regular = std::shared_ptr<ScaLBL_Communicator>(new ScaLBL_Communicator(Mask));
2018-05-16 14:15:01 -05:00
2018-05-18 19:53:49 -05:00
int Npad=(Np/16 + 2)*16;
2018-06-13 09:09:55 -05:00
if (rank==0) printf ("Set up memory efficient layout, %i | %i | %i \n", Np, Npad, N);
2018-05-18 19:53:49 -05:00
Map.resize(Nx,Ny,Nz); Map.fill(-2);
auto neighborList= new int[18*Npad];
Np = ScaLBL_Comm->MemoryOptimizedLayoutAA(Map,neighborList,Mask->id,Np);
MPI_Barrier(comm);
2018-05-16 13:40:38 -05:00
2018-05-18 19:53:49 -05:00
//...........................................................................
// MAIN VARIABLES ALLOCATED HERE
//...........................................................................
// LBM variables
if (rank==0) printf ("Allocating distributions \n");
//......................device distributions.................................
2018-06-13 09:09:55 -05:00
dist_mem_size = Np*sizeof(double);
neighborSize=18*(Np*sizeof(int));
2018-05-18 19:53:49 -05:00
//...........................................................................
ScaLBL_AllocateDeviceMemory((void **) &NeighborList, neighborSize);
ScaLBL_AllocateDeviceMemory((void **) &dvcMap, sizeof(int)*Np);
ScaLBL_AllocateDeviceMemory((void **) &fq, 19*dist_mem_size);
ScaLBL_AllocateDeviceMemory((void **) &Aq, 7*dist_mem_size);
ScaLBL_AllocateDeviceMemory((void **) &Bq, 7*dist_mem_size);
ScaLBL_AllocateDeviceMemory((void **) &Den, 2*dist_mem_size);
2018-07-02 12:32:25 -05:00
ScaLBL_AllocateDeviceMemory((void **) &Phi, sizeof(double)*Nx*Ny*Nz);
2018-05-18 19:53:49 -05:00
ScaLBL_AllocateDeviceMemory((void **) &Pressure, sizeof(double)*Np);
ScaLBL_AllocateDeviceMemory((void **) &Velocity, 3*sizeof(double)*Np);
2018-07-02 12:32:25 -05:00
ScaLBL_AllocateDeviceMemory((void **) &ColorGrad, 3*sizeof(double)*Np);
2018-05-18 19:53:49 -05:00
//...........................................................................
// Update GPU data structures
2018-07-02 12:32:25 -05:00
if (rank==0) printf ("Setting up device map and neighbor list \n");
fflush(stdout);
2018-05-18 19:53:49 -05:00
int *TmpMap;
TmpMap=new int[Np];
for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){
int idx=Map(i,j,k);
if (!(idx < 0))
TmpMap[idx] = k*Nx*Ny+j*Nx+i;
}
}
}
2018-07-02 12:32:25 -05:00
// check that TmpMap is valid
2018-09-19 20:30:30 -05:00
for (int idx=0; idx<ScaLBL_Comm->LastExterior(); idx++){
int n = TmpMap[idx];
if (n > Nx*Ny*Nz){
printf("Bad value! idx=%i \n");
TmpMap[idx] = Nx*Ny*Nz-1;
}
}
for (int idx=ScaLBL_Comm->FirstInterior(); idx<ScaLBL_Comm->LastInterior(); idx++){
2018-07-02 12:32:25 -05:00
int n = TmpMap[idx];
if (n > Nx*Ny*Nz){
printf("Bad value! idx=%i \n");
TmpMap[idx] = Nx*Ny*Nz-1;
}
}
2018-05-18 19:53:49 -05:00
ScaLBL_CopyToDevice(dvcMap, TmpMap, sizeof(int)*Np);
ScaLBL_DeviceBarrier();
delete [] TmpMap;
2018-07-02 12:32:25 -05:00
// copy the neighbor list
ScaLBL_CopyToDevice(NeighborList, neighborList, neighborSize);
// initialize phi based on PhaseLabel (include solid component labels)
double *PhaseLabel;
PhaseLabel = new double[N];
AssignComponentLabels(PhaseLabel);
ScaLBL_CopyToDevice(Phi, PhaseLabel, N*sizeof(double));
2018-05-16 13:40:38 -05:00
}
2018-05-18 19:53:49 -05:00
/********************************************************
* AssignComponentLabels *
********************************************************/
2018-05-16 13:40:38 -05:00
2018-05-19 16:28:39 -05:00
void ScaLBL_ColorModel::Initialize(){
2018-09-04 06:46:09 -05:00
if (rank==0) printf ("Initializing distributions \n");
ScaLBL_D3Q19_Init(fq, Np);
2018-05-19 16:28:39 -05:00
/*
* This function initializes model
*/
2018-06-27 15:31:03 -05:00
if (Restart == true){
if (rank==0){
printf("Reading restart file! \n");
ifstream restart("Restart.txt");
if (restart.is_open()){
restart >> timestep;
printf("Restarting from timestep =%i \n",timestep);
}
else{
printf("WARNING:No Restart.txt file, setting timestep=0 \n");
timestep=0;
}
}
MPI_Bcast(&timestep,1,MPI_INT,0,comm);
// Read in the restart file to CPU buffers
2018-09-05 00:00:02 -05:00
int *TmpMap;
TmpMap = new int[Np];
2018-09-28 08:34:49 -05:00
double *cPhi, *cDist, *cDen;
2018-09-05 15:40:48 -05:00
cPhi = new double[N];
2018-09-28 08:34:49 -05:00
cDen = new double[2*Np];
2018-09-05 00:00:02 -05:00
cDist = new double[19*Np];
2018-09-28 08:34:49 -05:00
ScaLBL_CopyToHost(TmpMap, dvcMap, Np*sizeof(int));
2018-10-22 13:31:17 -05:00
ScaLBL_CopyToHost(cPhi, Phi, N*sizeof(double));
2018-09-28 08:34:49 -05:00
ifstream File(LocalRestartFile,ios::binary);
2018-09-04 23:19:11 -05:00
int idx;
2018-09-04 21:36:22 -05:00
double value,va,vb;
2018-09-28 08:34:49 -05:00
for (int n=0; n<Np; n++){
2018-09-27 21:50:57 -05:00
File.read((char*) &va, sizeof(va));
File.read((char*) &vb, sizeof(vb));
2018-09-28 08:34:49 -05:00
cDen[n] = va;
cDen[Np+n] = vb;
}
for (int n=0; n<Np; n++){
// Read the distributions
for (int q=0; q<19; q++){
File.read((char*) &value, sizeof(value));
cDist[q*Np+n] = value;
}
}
File.close();
for (int n=0; n<ScaLBL_Comm->LastExterior(); n++){
va = cDen[n];
vb = cDen[Np + n];
2018-09-27 21:50:57 -05:00
value = (va-vb)/(va+vb);
idx = TmpMap[n];
if (!(idx < 0) && idx<N)
cPhi[idx] = value;
}
2018-09-28 13:26:36 -05:00
for (int n=ScaLBL_Comm->FirstInterior(); n<ScaLBL_Comm->LastInterior(); n++){
va = cDen[n];
vb = cDen[Np + n];
value = (va-vb)/(va+vb);
idx = TmpMap[n];
if (!(idx < 0) && idx<N)
cPhi[idx] = value;
2018-09-04 21:10:29 -05:00
}
2018-09-28 08:34:49 -05:00
2018-06-27 15:31:03 -05:00
// Copy the restart data to the GPU
2018-09-28 08:34:49 -05:00
ScaLBL_CopyToDevice(Den,cDen,2*Np*sizeof(double));
2018-06-27 15:31:03 -05:00
ScaLBL_CopyToDevice(fq,cDist,19*Np*sizeof(double));
2018-09-05 15:40:48 -05:00
ScaLBL_CopyToDevice(Phi,cPhi,N*sizeof(double));
2018-06-27 15:31:03 -05:00
ScaLBL_DeviceBarrier();
2018-09-04 23:19:11 -05:00
2018-06-27 15:31:03 -05:00
MPI_Barrier(comm);
}
2018-07-02 12:32:25 -05:00
if (rank==0) printf ("Initializing phase field \n");
2018-07-28 15:01:24 -05:00
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);
2018-07-02 12:32:25 -05:00
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);
}
}
2019-03-29 05:07:31 -05:00
ScaLBL_CopyToHost(Averages->Phi.data(),Phi,N*sizeof(double));
2018-05-16 13:40:38 -05:00
}
void ScaLBL_ColorModel::Run(){
2018-07-01 16:13:27 -05:00
int nprocs=nprocx*nprocy*nprocz;
const RankInfoStruct rank_info(rank,nprocx,nprocy,nprocz);
2018-10-23 14:38:01 -05:00
bool SET_CAPILLARY_NUMBER = false;
bool MORPH_ADAPT = false;
2018-11-09 09:57:31 -06:00
bool USE_MORPH = false;
2019-03-08 10:58:40 -06:00
int analysis_interval = 1000; // number of timesteps in between in situ analysis
2019-03-08 10:57:10 -06:00
int MAX_MORPH_TIMESTEPS = 50000; // maximum number of LBM timesteps to spend in morphological adaptation routine
2019-03-26 10:17:56 -05:00
int MAX_STEADY_TIMESTEPS = 200000;
int RAMP_TIMESTEPS = 0;//50000; // number of timesteps to run initially (to get a reasonable velocity field before other pieces kick in)
2019-03-08 10:57:10 -06:00
int morph_interval = 1000000;
2019-03-08 10:58:40 -06:00
int CURRENT_MORPH_TIMESTEPS=0; // counter for number of timesteps spent in morphological adaptation routine (reset each time)
2019-03-22 20:29:38 -05:00
int CURRENT_STEADY_TIMESTEPS=0; // counter for number of timesteps spent in morphological adaptation routine (reset each time)
int morph_timesteps = 0;
double morph_delta = 0.0;
2019-03-08 10:57:10 -06:00
double capillary_number = 0.0;
double tolerance = 0.01;
2018-11-09 15:16:36 -06:00
double Ca_previous = 0.f;
double delta_volume = 0.0;
double delta_volume_target = 0.0;
2019-03-15 16:03:22 -05:00
double RESIDUAL_ENDPOINT_THRESHOLD = 0.04;
2019-03-15 16:03:22 -05:00
if (color_db->keyExists( "residual_endpoint_threshold" )){
RESIDUAL_ENDPOINT_THRESHOLD = color_db->getScalar<double>( "residual_endpoint_threshold" );
}
2018-10-24 10:10:45 -05:00
if (color_db->keyExists( "capillary_number" )){
capillary_number = color_db->getScalar<double>( "capillary_number" );
SET_CAPILLARY_NUMBER=true;
}
if (BoundaryCondition != 0 && SET_CAPILLARY_NUMBER==true){
if (rank == 0) printf("WARINING: capillary number target only supported for BC = 0 \n");
SET_CAPILLARY_NUMBER=false;
}
2018-10-23 14:38:01 -05:00
if (analysis_db->keyExists( "morph_delta" )){
2018-10-23 22:59:38 -05:00
morph_delta = analysis_db->getScalar<double>( "morph_delta" );
2018-10-23 14:38:01 -05:00
}
if (analysis_db->keyExists( "morph_interval" )){
2018-10-23 22:59:38 -05:00
morph_interval = analysis_db->getScalar<int>( "morph_interval" );
2018-11-21 21:06:20 -06:00
USE_MORPH = true;
2018-10-23 14:38:01 -05:00
}
2018-11-09 09:57:31 -06:00
if (analysis_db->keyExists( "tolerance" )){
tolerance = analysis_db->getScalar<double>( "tolerance" );
}
2019-03-08 10:57:10 -06:00
if (analysis_db->keyExists( "analysis_interval" )){
analysis_interval = analysis_db->getScalar<int>( "analysis_interval" );
2018-10-23 14:38:01 -05:00
}
2019-03-26 10:17:56 -05:00
if (analysis_db->keyExists( "max_steady_timesteps" )){
MAX_STEADY_TIMESTEPS = analysis_db->getScalar<int>( "max_steady_timesteps" );
}
if (analysis_db->keyExists( "max_morph_timesteps" )){
MAX_MORPH_TIMESTEPS = analysis_db->getScalar<int>( "max_morph_timesteps" );
}
2018-07-02 12:32:25 -05:00
if (rank==0){
printf("********************************************************\n");
printf("No. of timesteps: %i \n", timestepMax);
fflush(stdout);
}
2018-07-01 16:13:27 -05:00
//.......create and start timer............
double starttime,stoptime,cputime;
ScaLBL_DeviceBarrier();
MPI_Barrier(comm);
starttime = MPI_Wtime();
//.........................................
2019-03-19 15:36:02 -05:00
2018-07-02 12:32:25 -05:00
//************ MAIN ITERATION LOOP ***************************************/
2018-07-01 16:13:27 -05:00
PROFILE_START("Loop");
2018-07-02 12:32:25 -05:00
//std::shared_ptr<Database> analysis_db;
2018-07-06 08:21:29 -05:00
bool Regular = false;
runAnalysis analysis( analysis_db, rank_info, ScaLBL_Comm, Dm, Np, Regular, beta, Map );
2018-07-06 11:38:49 -05:00
//analysis.createThreads( analysis_method, 4 );
2018-07-01 16:13:27 -05:00
while (timestep < timestepMax ) {
//if ( rank==0 ) { printf("Running timestep %i (%i MB)\n",timestep+1,(int)(Utilities::getMemoryUsage()/1048576)); }
PROFILE_START("Update");
// *************ODD TIMESTEP*************
timestep++;
// Compute the Phase indicator field
// Read for Aq, Bq happens in this routine (requires communication)
ScaLBL_Comm->BiSendD3Q7AA(Aq,Bq); //READ FROM NORMAL
2018-07-28 15:01:24 -05:00
ScaLBL_D3Q7_AAodd_PhaseField(NeighborList, dvcMap, Aq, Bq, Den, Phi, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np);
2018-07-01 16:13:27 -05:00
ScaLBL_Comm->BiRecvD3Q7AA(Aq,Bq); //WRITE INTO OPPOSITE
2018-09-22 16:15:00 -05:00
ScaLBL_DeviceBarrier();
2018-07-28 15:01:24 -05:00
ScaLBL_D3Q7_AAodd_PhaseField(NeighborList, dvcMap, Aq, Bq, Den, Phi, 0, ScaLBL_Comm->LastExterior(), Np);
2018-07-02 12:32:25 -05:00
2018-07-01 16:13:27 -05:00
// Perform the collision operation
ScaLBL_Comm->SendD3Q19AA(fq); //READ FROM NORMAL
2018-09-22 15:00:32 -05:00
if (BoundaryCondition > 0){
ScaLBL_Comm->Color_BC_z(dvcMap, Phi, Den, inletA, inletB);
ScaLBL_Comm->Color_BC_Z(dvcMap, Phi, Den, outletA, outletB);
}
2018-07-02 12:32:25 -05:00
// Halo exchange for phase field
ScaLBL_Comm_Regular->SendHalo(Phi);
ScaLBL_D3Q19_AAodd_Color(NeighborList, dvcMap, fq, Aq, Bq, Den, Phi, Velocity, rhoA, rhoB, tauA, tauB,
2018-07-28 15:01:24 -05:00
alpha, beta, Fx, Fy, Fz, Nx, Nx*Ny, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np);
2018-07-02 12:32:25 -05:00
ScaLBL_Comm_Regular->RecvHalo(Phi);
2018-07-01 16:13:27 -05:00
ScaLBL_Comm->RecvD3Q19AA(fq); //WRITE INTO OPPOSITE
2018-09-22 16:15:00 -05:00
ScaLBL_DeviceBarrier();
2018-07-01 16:13:27 -05:00
// Set BCs
if (BoundaryCondition == 3){
ScaLBL_Comm->D3Q19_Pressure_BC_z(NeighborList, fq, din, timestep);
ScaLBL_Comm->D3Q19_Pressure_BC_Z(NeighborList, fq, dout, timestep);
}
if (BoundaryCondition == 4){
din = ScaLBL_Comm->D3Q19_Flux_BC_z(NeighborList, fq, flux, timestep);
ScaLBL_Comm->D3Q19_Pressure_BC_Z(NeighborList, fq, dout, timestep);
}
2018-07-02 12:32:25 -05:00
ScaLBL_D3Q19_AAodd_Color(NeighborList, dvcMap, fq, Aq, Bq, Den, Phi, Velocity, rhoA, rhoB, tauA, tauB,
2018-07-28 15:01:24 -05:00
alpha, beta, Fx, Fy, Fz, Nx, Nx*Ny, 0, ScaLBL_Comm->LastExterior(), Np);
2018-07-01 16:13:27 -05:00
ScaLBL_DeviceBarrier(); MPI_Barrier(comm);
// *************EVEN TIMESTEP*************
timestep++;
// Compute the Phase indicator field
ScaLBL_Comm->BiSendD3Q7AA(Aq,Bq); //READ FROM NORMAL
2018-07-28 15:01:24 -05:00
ScaLBL_D3Q7_AAeven_PhaseField(dvcMap, Aq, Bq, Den, Phi, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np);
2018-07-01 16:13:27 -05:00
ScaLBL_Comm->BiRecvD3Q7AA(Aq,Bq); //WRITE INTO OPPOSITE
2018-09-22 16:15:00 -05:00
ScaLBL_DeviceBarrier();
2018-07-28 15:01:24 -05:00
ScaLBL_D3Q7_AAeven_PhaseField(dvcMap, Aq, Bq, Den, Phi, 0, ScaLBL_Comm->LastExterior(), Np);
2018-07-01 16:13:27 -05:00
// Perform the collision operation
ScaLBL_Comm->SendD3Q19AA(fq); //READ FORM NORMAL
2018-07-02 12:32:25 -05:00
// Halo exchange for phase field
2018-09-22 15:00:32 -05:00
if (BoundaryCondition > 0){
ScaLBL_Comm->Color_BC_z(dvcMap, Phi, Den, inletA, inletB);
ScaLBL_Comm->Color_BC_Z(dvcMap, Phi, Den, outletA, outletB);
}
2018-07-02 12:32:25 -05:00
ScaLBL_Comm_Regular->SendHalo(Phi);
ScaLBL_D3Q19_AAeven_Color(dvcMap, fq, Aq, Bq, Den, Phi, Velocity, rhoA, rhoB, tauA, tauB,
2018-07-28 15:01:24 -05:00
alpha, beta, Fx, Fy, Fz, Nx, Nx*Ny, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np);
2018-07-02 12:32:25 -05:00
ScaLBL_Comm_Regular->RecvHalo(Phi);
2018-07-01 16:13:27 -05:00
ScaLBL_Comm->RecvD3Q19AA(fq); //WRITE INTO OPPOSITE
2018-09-22 16:15:00 -05:00
ScaLBL_DeviceBarrier();
2018-07-01 16:13:27 -05:00
// Set boundary conditions
if (BoundaryCondition == 3){
ScaLBL_Comm->D3Q19_Pressure_BC_z(NeighborList, fq, din, timestep);
ScaLBL_Comm->D3Q19_Pressure_BC_Z(NeighborList, fq, dout, timestep);
}
else if (BoundaryCondition == 4){
din = ScaLBL_Comm->D3Q19_Flux_BC_z(NeighborList, fq, flux, timestep);
ScaLBL_Comm->D3Q19_Pressure_BC_Z(NeighborList, fq, dout, timestep);
}
2018-07-02 12:32:25 -05:00
ScaLBL_D3Q19_AAeven_Color(dvcMap, fq, Aq, Bq, Den, Phi, Velocity, rhoA, rhoB, tauA, tauB,
2018-07-28 15:01:24 -05:00
alpha, beta, Fx, Fy, Fz, Nx, Nx*Ny, 0, ScaLBL_Comm->LastExterior(), Np);
2018-07-01 16:13:27 -05:00
ScaLBL_DeviceBarrier(); MPI_Barrier(comm);
//************************************************************************
2018-07-02 12:32:25 -05:00
2018-07-01 16:13:27 -05:00
MPI_Barrier(comm);
PROFILE_STOP("Update");
2018-12-18 16:58:15 -06:00
2018-07-01 16:13:27 -05:00
// Run the analysis
2019-03-19 15:36:02 -05:00
//analysis.run( timestep, *Averages, Phi, Pressure, Velocity, fq, Den );
2019-03-20 20:44:50 -05:00
analysis.basic( timestep, *Averages, Phi, Pressure, Velocity, fq, Den );
2019-03-19 15:36:02 -05:00
if (rank==0 && timestep%analysis_interval == 0 && BoundaryCondition > 0){
printf("....inlet pressure=%f \n",din);
}
2018-11-04 12:18:56 -06:00
// allow initial ramp-up to get closer to steady state
2019-03-08 10:57:10 -06:00
if (timestep > RAMP_TIMESTEPS && timestep%analysis_interval == 0 && USE_MORPH){
2018-12-17 18:32:19 -06:00
analysis.finish();
2019-03-22 20:29:38 -05:00
CURRENT_STEADY_TIMESTEPS += analysis_interval;
2018-11-09 09:57:31 -06:00
2019-03-22 20:29:38 -05:00
if ( morph_timesteps > morph_interval ){
2019-03-21 06:14:08 -05:00
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;
2018-11-06 15:06:27 -06:00
double muA = rhoA*(tauA-0.5)/3.f;
double muB = rhoB*(tauB-0.5)/3.f;
2019-03-26 10:18:43 -05:00
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);
2018-12-18 07:44:17 -06:00
double Ca = fabs(volA*muA*flow_rate_A + volB*muB*flow_rate_B)/(5.796*alpha*double((Nx-2)*(Ny-2)*(Nz-2)*nprocs));
2018-11-06 15:06:27 -06:00
2018-11-09 09:57:31 -06:00
double force_magnitude = sqrt(Fx*Fx + Fy*Fy + Fz*Fz);
2019-03-26 10:17:56 -05:00
if (fabs((Ca - Ca_previous)/Ca) < tolerance || CURRENT_STEADY_TIMESTEPS > MAX_STEADY_TIMESTEPS){
2018-11-09 09:57:31 -06:00
MORPH_ADAPT = true;
2018-12-24 06:40:54 -06:00
CURRENT_MORPH_TIMESTEPS=0;
2019-03-20 20:38:25 -05:00
delta_volume_target = (volA + volB)*morph_delta; // set target volume change
2019-03-20 20:44:50 -05:00
Averages->Full();
2019-03-20 21:25:34 -05:00
Averages->Write(timestep);
analysis.WriteVisData( timestep, *Averages, Phi, Pressure, Velocity, fq, Den );
analysis.finish();
2019-03-20 21:25:34 -05:00
2018-11-10 06:05:13 -06:00
if (rank==0){
printf("** WRITE STEADY POINT *** ");
printf("Ca = %f, (previous = %f) \n",Ca,Ca_previous);
2018-12-17 17:47:44 -06:00
volA /= double((Nx-2)*(Ny-2)*(Nz-2)*nprocs);
volB /= double((Nx-2)*(Ny-2)*(Nz-2)*nprocs);
2018-11-10 14:18:09 -06:00
FILE * kr_log_file = fopen("relperm.csv","a");
2019-03-22 20:29:38 -05:00
fprintf(kr_log_file,"%i %.5g %.5g %.5g %.5g %.5g %.5g ",CURRENT_STEADY_TIMESTEPS,muA,muB,5.796*alpha,Fx,Fy,Fz);
2018-11-24 07:00:57 -06:00
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);
2018-11-10 14:18:09 -06:00
fclose(kr_log_file);
printf(" Measured capillary number %f \n ",Ca);
}
2018-11-09 09:57:31 -06:00
if (SET_CAPILLARY_NUMBER ){
Fx *= capillary_number / Ca;
Fy *= capillary_number / Ca;
Fz *= capillary_number / Ca;
if (force_magnitude > 1e-3){
Fx *= 1e-3/force_magnitude; // impose ceiling for stability
Fy *= 1e-3/force_magnitude;
Fz *= 1e-3/force_magnitude;
}
if (force_magnitude < 1e-7){
2018-11-09 09:57:31 -06:00
Fx *= 1e-6/force_magnitude; // impose floor
Fy *= 1e-6/force_magnitude;
Fz *= 1e-6/force_magnitude;
}
2018-11-10 12:21:26 -06:00
if (rank == 0) printf(" -- adjust force by factor %f \n ",capillary_number / Ca);
2019-03-19 15:36:02 -05:00
Averages->SetParams(rhoA,rhoB,tauA,tauB,Fx,Fy,Fz,alpha,beta);
2018-11-06 15:06:27 -06:00
}
2019-03-24 03:33:59 -05:00
// flow reversal criteria based on fractional flow rate
if (delta_volume_target < 0.0 &&
volA*flow_rate_A/(volA*flow_rate_A+volB*flow_rate_B) < RESIDUAL_ENDPOINT_THRESHOLD){
REVERSE_FLOW_DIRECTION = true;
2019-03-24 03:33:59 -05:00
}
else if (delta_volume_target > 0.0 &&
volB*flow_rate_B/(volA*flow_rate_A+volB*flow_rate_B) < RESIDUAL_ENDPOINT_THRESHOLD){
REVERSE_FLOW_DIRECTION = true;
}
if ( REVERSE_FLOW_DIRECTION ){
2019-03-24 03:33:59 -05:00
delta_volume_target *= (-1.0);
REVERSE_FLOW_DIRECTION = false;
2019-03-24 03:33:59 -05:00
}
}
else{
2018-11-09 09:57:31 -06:00
if (rank==0){
printf("** Continue to simulate steady *** \n ");
2018-11-09 15:16:36 -06:00
printf("Ca = %f, (previous = %f) \n",Ca,Ca_previous);
}
2018-11-09 21:53:17 -06:00
morph_timesteps=0;
}
2018-11-09 15:54:32 -06:00
Ca_previous = Ca;
}
2018-11-04 12:18:56 -06:00
if (MORPH_ADAPT ){
2018-12-24 06:40:54 -06:00
CURRENT_MORPH_TIMESTEPS += analysis_interval;
2018-12-19 17:55:01 -06:00
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
2018-12-23 13:13:15 -06:00
delta_volume += MorphInit(beta,delta_volume_target-delta_volume);
if ( (delta_volume - delta_volume_target)/delta_volume_target > 0.0 ){
MORPH_ADAPT = false;
delta_volume = 0.0;
2019-03-22 20:29:38 -05:00
CURRENT_STEADY_TIMESTEPS=0;
2018-12-23 13:13:15 -06:00
}
2018-12-24 06:40:54 -06:00
else if (CURRENT_MORPH_TIMESTEPS > MAX_MORPH_TIMESTEPS) {
2019-03-26 06:14:16 -05:00
delta_volume = 0.0;
2018-12-23 13:19:35 -06:00
MORPH_ADAPT = false;
2019-03-22 20:29:38 -05:00
CURRENT_STEADY_TIMESTEPS=0;
2018-12-23 13:19:35 -06:00
}
if ( REVERSE_FLOW_DIRECTION ){
2019-03-26 06:14:16 -05:00
if (rank==0) printf("*****REVERSE FLOW DIRECTION***** \n");
delta_volume = 0.0;
// flow direction will reverse after next steady point
MORPH_ADAPT = false;
CURRENT_STEADY_TIMESTEPS=0;
}
2018-10-24 11:58:47 -05:00
MPI_Barrier(comm);
}
2018-11-06 15:06:27 -06:00
morph_timesteps += analysis_interval;
2018-10-22 13:52:37 -05:00
}
2018-07-01 16:13:27 -05:00
}
2018-07-06 08:21:29 -05:00
analysis.finish();
2018-07-01 16:13:27 -05:00
PROFILE_STOP("Loop");
PROFILE_SAVE("lbpm_color_simulator",1);
//************************************************************************
ScaLBL_DeviceBarrier();
MPI_Barrier(comm);
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(Np)/cputime/1000000;
2018-07-02 12:32:25 -05:00
2018-07-01 16:13:27 -05:00
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");
// ************************************************************************
2018-05-16 13:40:38 -05:00
}
2018-05-18 16:24:17 -05:00
2018-12-05 12:09:00 -06:00
double ScaLBL_ColorModel::MorphInit(const double beta, const double target_delta_volume){
2018-10-22 15:51:38 -05:00
const RankInfoStruct rank_info(rank,nprocx,nprocy,nprocz);
2018-10-22 14:44:44 -05:00
2018-10-22 13:31:17 -05:00
double vF = 0.f;
double vS = 0.f;
2018-12-05 12:09:00 -06:00
double delta_volume;
2018-10-22 15:51:38 -05:00
2018-10-22 13:31:17 -05:00
DoubleArray phase(Nx,Ny,Nz);
IntArray phase_label(Nx,Ny,Nz);;
DoubleArray phase_distance(Nx,Ny,Nz);
2018-10-22 13:49:34 -05:00
Array<char> phase_id(Nx,Ny,Nz);
2018-12-18 16:46:25 -06:00
fillHalo<double> fillDouble(Dm->Comm,Dm->rank_info,{Nx-2,Ny-2,Nz-2},{1,1,1},0,1);
2018-10-22 13:31:17 -05:00
// Basic algorithm to
// 1. Copy phase field to CPU
2018-10-22 13:49:34 -05:00
ScaLBL_CopyToHost(phase.data(), Phi, N*sizeof(double));
2018-10-22 13:31:17 -05:00
double count,count_global,volume_initial,volume_final,volume_connected;
count = 0.f;
2018-12-17 17:47:44 -06:00
for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){
2018-10-22 23:50:07 -05:00
if (phase(i,j,k) > 0.f && Averages->SDs(i,j,k) > 0.f) count+=1.f;
}
}
}
2018-12-18 07:44:17 -06:00
volume_initial = sumReduce( Dm->Comm, count);
2018-12-18 16:46:25 -06:00
/*
sprintf(LocalRankFilename,"phi_initial.%05i.raw",rank);
FILE *INPUT = fopen(LocalRankFilename,"wb");
fwrite(phase.data(),8,N,INPUT);
fclose(INPUT);
*/
2018-10-22 13:31:17 -05:00
// 2. Identify connected components of phase field -> phase_label
2018-10-22 15:51:38 -05:00
BlobIDstruct new_index;
ComputeGlobalBlobIDs(Nx-2,Ny-2,Nz-2,rank_info,phase,Averages->SDs,vF,vS,phase_label,comm);
MPI_Barrier(comm);
2018-10-22 13:31:17 -05:00
// only operate on component "0"
count = 0.0;
2018-10-22 13:31:17 -05:00
for (int k=0; k<Nz; k++){
for (int j=0; j<Ny; j++){
for (int i=0; i<Nx; i++){
int label = phase_label(i,j,k);
if (label == 0 ){
phase_id(i,j,k) = 0;
count += 1.0;
}
else
phase_id(i,j,k) = 1;
2018-10-22 13:31:17 -05:00
}
}
}
volume_connected = sumReduce( Dm->Comm, count);
2018-10-22 13:31:17 -05:00
// 3. Generate a distance map to the largest object -> phase_distance
2018-10-22 13:49:34 -05:00
CalcDist(phase_distance,phase_id,*Dm);
2018-10-22 23:50:07 -05:00
2018-10-22 13:49:34 -05:00
double temp,value;
double factor=0.5/beta;
2018-10-22 13:31:17 -05:00
for (int k=0; k<Nz; k++){
for (int j=0; j<Ny; j++){
for (int i=0; i<Nx; i++){
2018-10-22 23:50:07 -05:00
if (phase_distance(i,j,k) < 3.f ){
value = phase(i,j,k);
if (value > 1.f) value=1.f;
if (value < -1.f) value=-1.f;
// temp -- distance based on analytical form McClure, Prins et al, Comp. Phys. Comm.
temp = -factor*log((1.0+value)/(1.0-value));
/// use this approximation close to the object
if (fabs(value) < 0.8 && Averages->SDs(i,j,k) > 1.f ){
phase_distance(i,j,k) = temp;
}
2018-12-18 16:46:25 -06:00
// erase the original object
phase(i,j,k) = -1.0;
2018-10-22 22:31:53 -05:00
}
2018-10-22 13:31:17 -05:00
}
}
}
2018-10-22 23:50:07 -05:00
2019-03-27 05:38:39 -05:00
if (volume_connected < 0.02*volume_initial && target_delta_volume < 0.0){
2019-03-26 06:14:16 -05:00
// if connected volume is less than 2% just delete the whole thing
2019-03-26 10:21:25 -05:00
if (rank==0) printf("Connected region has shrunk to less than 2 %% of total fluid volume \n");
REVERSE_FLOW_DIRECTION = true;
2018-10-22 13:31:17 -05:00
}
2019-03-26 06:14:16 -05:00
if (rank==0) printf("MorphGrow with target volume fraction change %f \n", target_delta_volume/volume_initial);
double target_delta_volume_incremental = target_delta_volume;
if (fabs(target_delta_volume) > 0.01*volume_initial)
target_delta_volume_incremental = 0.01*volume_initial*target_delta_volume/fabs(target_delta_volume);
delta_volume = MorphGrow(Averages->SDs,phase_distance,phase_id,Averages->Dm, target_delta_volume_incremental);
for (int k=0; k<Nz; k++){
for (int j=0; j<Ny; j++){
for (int i=0; i<Nx; i++){
if (phase_distance(i,j,k) < 0.0 ) phase_id(i,j,k) = 0;
else phase_id(i,j,k) = 1;
//if (phase_distance(i,j,k) < 0.0 ) phase(i,j,k) = 1.0;
2018-12-05 17:06:36 -06:00
}
2019-03-26 06:14:16 -05:00
}
}
CalcDist(phase_distance,phase_id,*Dm); // re-calculate distance
// 5. Update phase indicator field based on new distnace
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;
double d = phase_distance(i,j,k);
if (Averages->SDs(i,j,k) > 0.f){
if (d < 3.f){
//phase(i,j,k) = -1.0;
phase(i,j,k) = (2.f*(exp(-2.f*beta*d))/(1.f+exp(-2.f*beta*d))-1.f);
2018-10-22 15:51:38 -05:00
}
2019-03-26 06:14:16 -05:00
}
}
2018-10-22 13:31:17 -05:00
}
}
2019-03-26 06:14:16 -05:00
fillDouble.fill(phase);
2018-10-22 14:44:44 -05:00
count = 0.f;
2018-12-17 17:47:44 -06:00
for (int k=1; k<Nz-1; k++){
for (int j=1; j<Ny-1; j++){
for (int i=1; i<Nx-1; i++){
2018-10-22 23:50:07 -05:00
if (phase(i,j,k) > 0.f && Averages->SDs(i,j,k) > 0.f) count+=1.f;
}
}
}
2018-12-18 07:44:17 -06:00
volume_final= sumReduce( Dm->Comm, count);
2018-12-05 12:09:00 -06:00
delta_volume = (volume_final-volume_initial);
2018-11-02 13:17:17 -05:00
if (rank == 0) printf("MorphInit: change fluid volume fraction by %f \n", delta_volume/volume_initial);
2018-12-18 16:46:25 -06:00
if (rank == 0) printf(" new saturation = %f \n", volume_final/(0.238323*double((Nx-2)*(Ny-2)*(Nz-2)*nprocs)));
2018-10-22 13:31:17 -05:00
// 6. copy back to the device
//if (rank==0) printf("MorphInit: copy data back to device\n");
2018-10-22 13:49:34 -05:00
ScaLBL_CopyToDevice(Phi,phase.data(),N*sizeof(double));
2018-12-18 16:46:25 -06:00
/*
sprintf(LocalRankFilename,"dist_final.%05i.raw",rank);
FILE *DIST = fopen(LocalRankFilename,"wb");
fwrite(phase_distance.data(),8,N,DIST);
fclose(DIST);
sprintf(LocalRankFilename,"phi_final.%05i.raw",rank);
FILE *PHI = fopen(LocalRankFilename,"wb");
fwrite(phase.data(),8,N,PHI);
fclose(PHI);
*/
2018-10-22 13:31:17 -05:00
// 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 delta_volume;
2018-10-22 13:31:17 -05:00
}
2018-05-18 16:24:17 -05:00
void ScaLBL_ColorModel::WriteDebug(){
// Copy back final phase indicator field and convert to regular layout
DoubleArray PhaseField(Nx,Ny,Nz);
2018-07-02 12:32:25 -05:00
//ScaLBL_Comm->RegularLayout(Map,Phi,PhaseField);
ScaLBL_CopyToHost(PhaseField.data(), Phi, sizeof(double)*N);
2018-05-18 16:24:17 -05:00
FILE *OUTFILE;
sprintf(LocalRankFilename,"Phase.%05i.raw",rank);
OUTFILE = fopen(LocalRankFilename,"wb");
fwrite(PhaseField.data(),8,N,OUTFILE);
fclose(OUTFILE);
2018-05-19 05:39:44 -05:00
}