2020-12-26 14:00:17 -05:00
|
|
|
#include "analysis/ElectroChemistry.h"
|
|
|
|
|
|
2020-12-26 20:26:27 -05:00
|
|
|
ElectroChemistryAnalyzer::ElectroChemistryAnalyzer(std::shared_ptr <Domain> dm):
|
2021-01-04 21:40:41 -05:00
|
|
|
Dm(dm)
|
2020-12-29 14:04:43 -05:00
|
|
|
{
|
|
|
|
|
|
2020-12-26 14:00:17 -05:00
|
|
|
Nx=dm->Nx; Ny=dm->Ny; Nz=dm->Nz;
|
|
|
|
|
Volume=(Nx-2)*(Ny-2)*(Nz-2)*Dm->nprocx()*Dm->nprocy()*Dm->nprocz()*1.0;
|
|
|
|
|
|
|
|
|
|
ChemicalPotential.resize(Nx,Ny,Nz); ChemicalPotential.fill(0);
|
|
|
|
|
ElectricalPotential.resize(Nx,Ny,Nz); ElectricalPotential.fill(0);
|
|
|
|
|
Pressure.resize(Nx,Ny,Nz); Pressure.fill(0);
|
|
|
|
|
Rho.resize(Nx,Ny,Nz); Rho.fill(0);
|
|
|
|
|
Vel_x.resize(Nx,Ny,Nz); Vel_x.fill(0); // Gradient of the phase indicator field
|
|
|
|
|
Vel_y.resize(Nx,Ny,Nz); Vel_y.fill(0);
|
|
|
|
|
Vel_z.resize(Nx,Ny,Nz); Vel_z.fill(0);
|
|
|
|
|
SDs.resize(Nx,Ny,Nz); SDs.fill(0);
|
|
|
|
|
|
2020-12-26 20:26:27 -05:00
|
|
|
if (Dm->rank()==0){
|
|
|
|
|
bool WriteHeader=false;
|
|
|
|
|
TIMELOG = fopen("electrokinetic.csv","r");
|
|
|
|
|
if (TIMELOG != NULL)
|
|
|
|
|
fclose(TIMELOG);
|
|
|
|
|
else
|
|
|
|
|
WriteHeader=true;
|
|
|
|
|
|
|
|
|
|
TIMELOG = fopen("electrokinetic.csv","a+");
|
|
|
|
|
if (WriteHeader)
|
|
|
|
|
{
|
|
|
|
|
// If timelog is empty, write a short header to list the averages
|
|
|
|
|
//fprintf(TIMELOG,"--------------------------------------------------------------------------------------\n");
|
2020-12-29 14:04:43 -05:00
|
|
|
fprintf(TIMELOG,"TBD TBD\n");
|
2020-12-26 20:26:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ElectroChemistryAnalyzer::~ElectroChemistryAnalyzer(){
|
2020-12-31 11:19:12 -05:00
|
|
|
if (Dm->rank()==0){
|
|
|
|
|
fclose(TIMELOG);
|
|
|
|
|
}
|
2020-12-26 20:26:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ElectroChemistryAnalyzer::SetParams(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 11:19:12 -05:00
|
|
|
void ElectroChemistryAnalyzer::Basic(ScaLBL_IonModel &Ion, ScaLBL_Poisson &Poisson, ScaLBL_StokesModel &Stokes, int timestep){
|
2020-12-29 14:04:43 -05:00
|
|
|
|
2020-12-31 11:19:12 -05:00
|
|
|
int i,j,k;
|
2021-01-14 14:54:21 -05:00
|
|
|
double Vin=0.0;
|
|
|
|
|
double Vout=0.0;
|
2020-12-29 14:04:43 -05:00
|
|
|
Poisson.getElectricPotential(ElectricalPotential);
|
2020-12-31 11:19:12 -05:00
|
|
|
|
|
|
|
|
/* local sub-domain averages */
|
|
|
|
|
double rho_avg_local[Ion.number_ion_species];
|
|
|
|
|
double rho_mu_avg_local[Ion.number_ion_species];
|
|
|
|
|
double rho_mu_fluctuation_local[Ion.number_ion_species];
|
|
|
|
|
double rho_psi_avg_local[Ion.number_ion_species];
|
|
|
|
|
double rho_psi_fluctuation_local[Ion.number_ion_species];
|
|
|
|
|
/* global averages */
|
|
|
|
|
double rho_avg_global[Ion.number_ion_species];
|
|
|
|
|
double rho_mu_avg_global[Ion.number_ion_species];
|
|
|
|
|
double rho_mu_fluctuation_global[Ion.number_ion_species];
|
|
|
|
|
double rho_psi_avg_global[Ion.number_ion_species];
|
|
|
|
|
double rho_psi_fluctuation_global[Ion.number_ion_species];
|
|
|
|
|
|
2020-12-29 14:04:43 -05:00
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
2020-12-31 11:19:12 -05:00
|
|
|
rho_avg_local[ion] = 0.0;
|
|
|
|
|
rho_mu_avg_local[ion] = 0.0;
|
|
|
|
|
rho_psi_avg_local[ion] = 0.0;
|
2020-12-29 14:04:43 -05:00
|
|
|
Ion.getIonConcentration(Rho,ion);
|
2020-12-31 11:19:12 -05:00
|
|
|
/* Compute averages for each ion */
|
|
|
|
|
for (k=1; k<Nz; k++){
|
|
|
|
|
for (j=1; j<Ny; j++){
|
|
|
|
|
for (i=1; i<Nx; i++){
|
|
|
|
|
rho_avg_local[ion] += Rho(i,j,k);
|
|
|
|
|
rho_mu_avg_local[ion] += Rho(i,j,k)*Rho(i,j,k);
|
|
|
|
|
rho_psi_avg_local[ion] += Rho(i,j,k)*ElectricalPotential(i,j,k);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-14 14:54:21 -05:00
|
|
|
rho_avg_global[ion]=Dm->Comm.sumReduce( rho_avg_local[ion]) / Volume;
|
|
|
|
|
rho_mu_avg_global[ion]=Dm->Comm.sumReduce( rho_mu_avg_local[ion]) / Volume;
|
|
|
|
|
rho_psi_avg_global[ion]=Dm->Comm.sumReduce( rho_psi_avg_local[ion]) / Volume;
|
|
|
|
|
|
|
|
|
|
if (rho_avg_global[ion] > 0.0){
|
|
|
|
|
rho_mu_avg_global[ion] /= rho_avg_global[ion];
|
|
|
|
|
rho_psi_avg_global[ion] /= rho_avg_global[ion];
|
|
|
|
|
}
|
2020-12-29 14:04:43 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-31 11:19:12 -05:00
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
|
|
|
|
rho_mu_fluctuation_local[ion] = 0.0;
|
|
|
|
|
rho_psi_fluctuation_local[ion] = 0.0;
|
|
|
|
|
/* Compute averages for each ion */
|
|
|
|
|
for (k=1; k<Nz; k++){
|
|
|
|
|
for (j=1; j<Ny; j++){
|
|
|
|
|
for (i=1; i<Nx; i++){
|
|
|
|
|
rho_mu_fluctuation_local[ion] += (Rho(i,j,k)*Rho(i,j,k) - rho_mu_avg_global[ion]);
|
|
|
|
|
rho_psi_fluctuation_local[ion] += (Rho(i,j,k)*ElectricalPotential(i,j,k) - rho_psi_avg_global[ion]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-04 20:15:31 -05:00
|
|
|
rho_mu_fluctuation_global[ion]=Dm->Comm.sumReduce( rho_mu_fluctuation_local[ion]);
|
|
|
|
|
rho_psi_fluctuation_global[ion]=Dm->Comm.sumReduce( rho_psi_fluctuation_local[ion]);
|
2020-12-31 11:19:12 -05:00
|
|
|
}
|
2020-12-26 20:26:27 -05:00
|
|
|
|
2020-12-31 11:19:12 -05:00
|
|
|
if (Dm->rank()==0){
|
|
|
|
|
fprintf(TIMELOG,"%i ",timestep);
|
|
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_avg_global[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_mu_avg_global[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_psi_avg_global[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_mu_fluctuation_global[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_psi_fluctuation_global[ion]);
|
|
|
|
|
}
|
2021-01-14 14:54:21 -05:00
|
|
|
fprintf(TIMELOG,"%.8g %.8g\n",Vin,Vout);
|
2020-12-31 11:19:12 -05:00
|
|
|
fflush(TIMELOG);
|
|
|
|
|
}
|
|
|
|
|
/* else{
|
|
|
|
|
fprintf(TIMELOG,"%i ",timestep);
|
|
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_avg_local[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_mu_avg_local[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_psi_avg_local[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_mu_fluctuation_local[ion]);
|
|
|
|
|
fprintf(TIMELOG,"%.8g ",rho_psi_fluctuation_local[ion]);
|
|
|
|
|
}
|
|
|
|
|
fflush(TIMELOG);
|
|
|
|
|
} */
|
2020-12-26 14:00:17 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-29 14:04:43 -05:00
|
|
|
void ElectroChemistryAnalyzer::WriteVis( ScaLBL_IonModel &Ion, ScaLBL_Poisson &Poisson, ScaLBL_StokesModel &Stokes, std::shared_ptr<Database> input_db, int timestep){
|
2020-12-26 14:00:17 -05:00
|
|
|
|
2020-12-29 14:04:43 -05:00
|
|
|
auto vis_db = input_db->getDatabase( "Visualization" );
|
2021-01-04 21:40:41 -05:00
|
|
|
char VisName[40];
|
|
|
|
|
|
|
|
|
|
std::vector<IO::MeshDataStruct> visData;
|
|
|
|
|
fillHalo<double> fillData(Dm->Comm,Dm->rank_info,{Dm->Nx-2,Dm->Ny-2,Dm->Nz-2},{1,1,1},0,1);
|
2020-12-29 14:04:43 -05:00
|
|
|
|
|
|
|
|
IO::initialize("","silo","false");
|
|
|
|
|
// Create the MeshDataStruct
|
|
|
|
|
visData.resize(1);
|
|
|
|
|
|
|
|
|
|
visData[0].meshName = "domain";
|
|
|
|
|
visData[0].mesh = std::make_shared<IO::DomainMesh>( Dm->rank_info,Dm->Nx-2,Dm->Ny-2,Dm->Nz-2,Dm->Lx,Dm->Ly,Dm->Lz );
|
|
|
|
|
auto ElectricPotential = std::make_shared<IO::Variable>();
|
2021-01-02 19:21:15 -05:00
|
|
|
std::vector<shared_ptr<IO::Variable>> IonConcentration;
|
|
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
|
|
|
|
IonConcentration.push_back(std::make_shared<IO::Variable>());
|
|
|
|
|
}
|
2020-12-29 14:04:43 -05:00
|
|
|
auto VxVar = std::make_shared<IO::Variable>();
|
|
|
|
|
auto VyVar = std::make_shared<IO::Variable>();
|
|
|
|
|
auto VzVar = std::make_shared<IO::Variable>();
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "save_electric_potential", true )){
|
|
|
|
|
ElectricPotential->name = "ElectricPotential";
|
|
|
|
|
ElectricPotential->type = IO::VariableType::VolumeVariable;
|
|
|
|
|
ElectricPotential->dim = 1;
|
|
|
|
|
ElectricPotential->data.resize(Dm->Nx-2,Dm->Ny-2,Dm->Nz-2);
|
|
|
|
|
visData[0].vars.push_back(ElectricPotential);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "save_concentration", true )){
|
|
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
2021-01-02 19:21:15 -05:00
|
|
|
sprintf(VisName,"IonConcentration_%i",ion+1);
|
|
|
|
|
IonConcentration[ion]->name = VisName;
|
|
|
|
|
IonConcentration[ion]->type = IO::VariableType::VolumeVariable;
|
|
|
|
|
IonConcentration[ion]->dim = 1;
|
|
|
|
|
IonConcentration[ion]->data.resize(Dm->Nx-2,Dm->Ny-2,Dm->Nz-2);
|
|
|
|
|
visData[0].vars.push_back(IonConcentration[ion]);
|
2020-12-29 14:04:43 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-02 19:21:15 -05:00
|
|
|
|
2020-12-29 14:04:43 -05:00
|
|
|
if (vis_db->getWithDefault<bool>( "save_velocity", false )){
|
|
|
|
|
VxVar->name = "Velocity_x";
|
|
|
|
|
VxVar->type = IO::VariableType::VolumeVariable;
|
|
|
|
|
VxVar->dim = 1;
|
|
|
|
|
VxVar->data.resize(Dm->Nx-2,Dm->Ny-2,Dm->Nz-2);
|
|
|
|
|
visData[0].vars.push_back(VxVar);
|
|
|
|
|
VyVar->name = "Velocity_y";
|
|
|
|
|
VyVar->type = IO::VariableType::VolumeVariable;
|
|
|
|
|
VyVar->dim = 1;
|
|
|
|
|
VyVar->data.resize(Dm->Nx-2,Dm->Ny-2,Dm->Nz-2);
|
|
|
|
|
visData[0].vars.push_back(VyVar);
|
|
|
|
|
VzVar->name = "Velocity_z";
|
|
|
|
|
VzVar->type = IO::VariableType::VolumeVariable;
|
|
|
|
|
VzVar->dim = 1;
|
|
|
|
|
VzVar->data.resize(Dm->Nx-2,Dm->Ny-2,Dm->Nz-2);
|
|
|
|
|
visData[0].vars.push_back(VzVar);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "save_electric_potential", true )){
|
|
|
|
|
ASSERT(visData[0].vars[0]->name=="ElectricPotential");
|
|
|
|
|
Poisson.getElectricPotential(ElectricalPotential);
|
|
|
|
|
Array<double>& ElectricPotentialData = visData[0].vars[0]->data;
|
|
|
|
|
fillData.copy(ElectricalPotential,ElectricPotentialData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "save_concentration", true )){
|
|
|
|
|
for (int ion=0; ion<Ion.number_ion_species; ion++){
|
2021-01-02 19:21:15 -05:00
|
|
|
sprintf(VisName,"IonConcentration_%i",ion+1);
|
|
|
|
|
IonConcentration[ion]->name = VisName;
|
|
|
|
|
ASSERT(visData[0].vars[1+ion]->name==VisName);
|
|
|
|
|
Array<double>& IonConcentrationData = visData[0].vars[1+ion]->data;
|
2020-12-29 14:04:43 -05:00
|
|
|
Ion.getIonConcentration(Rho,ion);
|
|
|
|
|
fillData.copy(Rho,IonConcentrationData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "save_velocity", false )){
|
2021-01-02 19:21:15 -05:00
|
|
|
ASSERT(visData[0].vars[1+Ion.number_ion_species+0]->name=="Velocity_x");
|
|
|
|
|
ASSERT(visData[0].vars[1+Ion.number_ion_species+1]->name=="Velocity_y");
|
|
|
|
|
ASSERT(visData[0].vars[1+Ion.number_ion_species+2]->name=="Velocity_z");
|
2020-12-29 14:04:43 -05:00
|
|
|
Stokes.getVelocity(Vel_x,Vel_y,Vel_z);
|
2021-01-02 19:21:15 -05:00
|
|
|
Array<double>& VelxData = visData[0].vars[1+Ion.number_ion_species+0]->data;
|
|
|
|
|
Array<double>& VelyData = visData[0].vars[1+Ion.number_ion_species+1]->data;
|
|
|
|
|
Array<double>& VelzData = visData[0].vars[1+Ion.number_ion_species+2]->data;
|
2020-12-29 14:04:43 -05:00
|
|
|
fillData.copy(Vel_x,VelxData);
|
|
|
|
|
fillData.copy(Vel_y,VelyData);
|
|
|
|
|
fillData.copy(Vel_z,VelzData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (vis_db->getWithDefault<bool>( "write_silo", true ))
|
2021-01-04 21:40:41 -05:00
|
|
|
IO::writeData( timestep, visData, Dm->Comm );
|
2020-12-29 14:04:43 -05:00
|
|
|
|
|
|
|
|
/* if (vis_db->getWithDefault<bool>( "save_8bit_raw", true )){
|
|
|
|
|
char CurrentIDFilename[40];
|
|
|
|
|
sprintf(CurrentIDFilename,"id_t%d.raw",timestep);
|
|
|
|
|
Averages.AggregateLabels(CurrentIDFilename);
|
|
|
|
|
}
|
|
|
|
|
*/
|
2020-12-26 14:00:17 -05:00
|
|
|
}
|