From c424e1d9847150fe6a7147994ed5bff7e80b5ea2 Mon Sep 17 00:00:00 2001 From: James McClure Date: Fri, 6 May 2022 16:21:37 -0400 Subject: [PATCH] cleanup with help from valgrind --- common/Membrane.cpp | 79 +-- common/ScaLBL.cpp | 509 ++++++++++---------- models/IonModel.cpp | 31 +- models/PoissonSolver.cpp | 9 + tests/lbpm_nernst_planck_cell_simulator.cpp | 4 +- 5 files changed, 340 insertions(+), 292 deletions(-) diff --git a/common/Membrane.cpp b/common/Membrane.cpp index 98fe25a8..90c4d999 100644 --- a/common/Membrane.cpp +++ b/common/Membrane.cpp @@ -263,7 +263,7 @@ Membrane::~Membrane() { delete [] membraneLinks; delete [] membraneTag; delete [] membraneDist; - + ScaLBL_FreeDeviceMemory( coefficient_x ); ScaLBL_FreeDeviceMemory( coefficient_X ); ScaLBL_FreeDeviceMemory( coefficient_y ); @@ -274,6 +274,7 @@ Membrane::~Membrane() { ScaLBL_FreeDeviceMemory( NeighborList ); ScaLBL_FreeDeviceMemory( MembraneLinks ); ScaLBL_FreeDeviceMemory( MembraneCoef ); + ScaLBL_FreeDeviceMemory( MembraneDistance ); ScaLBL_FreeDeviceMemory( sendbuf_x ); ScaLBL_FreeDeviceMemory( sendbuf_X ); @@ -730,7 +731,6 @@ int Membrane::Create(std::shared_ptr Dm, DoubleArray &Distance, IntArra } if (rank == 0) printf(" Create device data structures... \n"); - /* Create device copies of data structures */ ScaLBL_AllocateDeviceMemory((void **)&MembraneLinks, 2*mlink*sizeof(int)); ScaLBL_AllocateDeviceMemory((void **)&MembraneCoef, 2*mlink*sizeof(double)); @@ -995,6 +995,7 @@ int Membrane::Create(std::shared_ptr Dm, DoubleArray &Distance, IntArra ScaLBL_AllocateZeroCopy((void **) &coefficient_Z, (recvCount_Z - linkCount_Z[0])*sizeof(double)); //...................................................................................... + delete [] neighborList; delete [] TempBuffer; return mlink; } @@ -1355,40 +1356,46 @@ void Membrane::AssignCoefficients(int *Map, double *Psi, double Threshold, double MassFractionIn, double MassFractionOut, double ThresholdMassFractionIn, double ThresholdMassFractionOut){ /* Assign mass transfer coefficients to the membrane data structure */ - - - ScaLBL_D3Q7_Membrane_AssignLinkCoef(MembraneLinks, Map, MembraneDistance, Psi, MembraneCoef, - Threshold, MassFractionIn, MassFractionOut, ThresholdMassFractionIn, ThresholdMassFractionOut, - membraneLinkCount, Nx, Ny, Nz, Np); - ScaLBL_D3Q7_Membrane_AssignLinkCoef_halo(-1,0,0,Map,MembraneDistance,Psi,Threshold, - MassFractionIn,MassFractionOut,ThresholdMassFractionIn,ThresholdMassFractionOut, - dvcRecvDist_X,dvcRecvLinks_X,coefficient_X,0,linkCount_X[0],recvCount_X, - Np,Nx,Ny,Nz); + if (membraneLinkCount > 0) + ScaLBL_D3Q7_Membrane_AssignLinkCoef(MembraneLinks, Map, MembraneDistance, Psi, MembraneCoef, + Threshold, MassFractionIn, MassFractionOut, ThresholdMassFractionIn, ThresholdMassFractionOut, + membraneLinkCount, Nx, Ny, Nz, Np); + + if (linkCount_X[0] < recvCount_X) + ScaLBL_D3Q7_Membrane_AssignLinkCoef_halo(-1,0,0,Map,MembraneDistance,Psi,Threshold, + MassFractionIn,MassFractionOut,ThresholdMassFractionIn,ThresholdMassFractionOut, + dvcRecvDist_X,dvcRecvLinks_X,coefficient_X,0,linkCount_X[0],recvCount_X, + Np,Nx,Ny,Nz); + + if (linkCount_x[0] < recvCount_x) + ScaLBL_D3Q7_Membrane_AssignLinkCoef_halo(1,0,0,Map,MembraneDistance,Psi,Threshold, + MassFractionIn,MassFractionOut,ThresholdMassFractionIn,ThresholdMassFractionOut, + dvcRecvDist_x,dvcRecvLinks_x,coefficient_x,0,linkCount_x[0],recvCount_x, + Np,Nx,Ny,Nz); + + if (linkCount_Y[0] < recvCount_Y) + ScaLBL_D3Q7_Membrane_AssignLinkCoef_halo(0,-1,0,Map,MembraneDistance,Psi,Threshold, + MassFractionIn,MassFractionOut,ThresholdMassFractionIn,ThresholdMassFractionOut, + dvcRecvDist_Y,dvcRecvLinks_Y,coefficient_Y,0,linkCount_Y[0],recvCount_Y, + Np,Nx,Ny,Nz); + + if (linkCount_y[0] 0){ - int *bb_dist_tmp = new int [local_count]; - int *bb_interactions_tmp = new int [local_count]; - ScaLBL_AllocateDeviceMemory((void **) &bb_dist, sizeof(int)*local_count); - ScaLBL_AllocateDeviceMemory((void **) &bb_interactions, sizeof(int)*local_count); - int *fluid_boundary_tmp; - double *lattice_weight_tmp; - float *lattice_cx_tmp; - float *lattice_cy_tmp; - float *lattice_cz_tmp; - /* allocate memory for bounce-back sites */ - fluid_boundary_tmp = new int [local_count]; - lattice_weight_tmp = new double [local_count]; - lattice_cx_tmp = new float [local_count]; - lattice_cy_tmp = new float [local_count]; - lattice_cz_tmp = new float [local_count]; - ScaLBL_AllocateDeviceMemory((void **) &fluid_boundary, sizeof(int)*local_count); - ScaLBL_AllocateDeviceMemory((void **) &lattice_weight, sizeof(double)*local_count); - ScaLBL_AllocateDeviceMemory((void **) &lattice_cx, sizeof(float)*local_count); - ScaLBL_AllocateDeviceMemory((void **) &lattice_cy, sizeof(float)*local_count); - ScaLBL_AllocateDeviceMemory((void **) &lattice_cz, sizeof(float)*local_count); + int *bb_dist_tmp = new int [local_count]; + int *bb_interactions_tmp = new int [local_count]; + ScaLBL_AllocateDeviceMemory((void **) &bb_dist, sizeof(int)*local_count); + ScaLBL_AllocateDeviceMemory((void **) &bb_interactions, sizeof(int)*local_count); + int *fluid_boundary_tmp; + double *lattice_weight_tmp; + float *lattice_cx_tmp; + float *lattice_cy_tmp; + float *lattice_cz_tmp; + /* allocate memory for bounce-back sites */ + fluid_boundary_tmp = new int [local_count]; + lattice_weight_tmp = new double [local_count]; + lattice_cx_tmp = new float [local_count]; + lattice_cy_tmp = new float [local_count]; + lattice_cz_tmp = new float [local_count]; + ScaLBL_AllocateDeviceMemory((void **) &fluid_boundary, sizeof(int)*local_count); + ScaLBL_AllocateDeviceMemory((void **) &lattice_weight, sizeof(double)*local_count); + ScaLBL_AllocateDeviceMemory((void **) &lattice_cx, sizeof(float)*local_count); + ScaLBL_AllocateDeviceMemory((void **) &lattice_cy, sizeof(float)*local_count); + ScaLBL_AllocateDeviceMemory((void **) &lattice_cz, sizeof(float)*local_count); - local_count=0; - for (k=1;k &num_iter) { @@ -50,6 +63,8 @@ void ScaLBL_IonModel::ReadParams(string filename, vector &num_iter) { Ez_dummy = 0.0; //for debugging, unit [V/m] //--------------------------------------------------------------------------// + BoundaryConditionSolid = 0; + // Read domain parameters if (domain_db->keyExists("voxel_length")) { //default unit: um/lu h = domain_db->getScalar("voxel_length"); @@ -685,7 +700,6 @@ void ScaLBL_IonModel::SetDomain() { } void ScaLBL_IonModel::SetMembrane() { - size_t NLABELS = 0; membrane_db = db->getDatabase("Membrane"); @@ -693,7 +707,7 @@ void ScaLBL_IonModel::SetMembrane() { auto MembraneLabels = membrane_db->getVector("MembraneLabels"); IonMembrane = std::shared_ptr(new Membrane(Dm, NeighborList, Np)); - + size_t NLABELS = MembraneLabels.size(); signed char LABEL = 0; double *label_count; double *label_count_global; @@ -1020,7 +1034,6 @@ void ScaLBL_IonModel::Create() { } ScaLBL_CopyToDevice(dvcMap, TmpMap, sizeof(int) * Np); ScaLBL_Comm->Barrier(); - delete[] TmpMap; ScaLBL_CopyToDevice(NeighborList, neighborList, neighborSize); comm.barrier(); @@ -1042,6 +1055,13 @@ void ScaLBL_IonModel::Create() { ScaLBL_Comm->Barrier(); delete[] IonSolid_host; } + else { + IonSolid = NULL; + } + + + delete[] TmpMap; + delete[] neighborList; } void ScaLBL_IonModel::Initialize() { @@ -1444,6 +1464,7 @@ void ScaLBL_IonModel::RunMembrane(double *Velocity, double *ElectricField, doubl //auto t1 = std::chrono::system_clock::now(); for (size_t ic = 0; ic < number_ion_species; ic++) { + /* set the mass transfer coefficients for the membrane */ IonMembrane->AssignCoefficients(dvcMap, Psi, ThresholdVoltage[ic],MassFractionIn[ic], MassFractionOut[ic],ThresholdMassFractionIn[ic],ThresholdMassFractionOut[ic]); @@ -1525,8 +1546,6 @@ void ScaLBL_IonModel::RunMembrane(double *Velocity, double *ElectricField, doubl //Compute charge density for Poisson equation for (size_t ic = 0; ic < number_ion_species; ic++) { int Valence = IonValence[ic]; - if (rank==0) printf("compute charge density for ion %i, Valence =%i \n", ic,Valence); - ScaLBL_D3Q7_Ion_ChargeDensity(Ci, ChargeDensity, Valence, ic, ScaLBL_Comm->FirstInterior(), ScaLBL_Comm->LastInterior(), Np); diff --git a/models/PoissonSolver.cpp b/models/PoissonSolver.cpp index da73e9fc..55d8d2a2 100644 --- a/models/PoissonSolver.cpp +++ b/models/PoissonSolver.cpp @@ -32,6 +32,14 @@ ScaLBL_Poisson::ScaLBL_Poisson(int RANK, int NP, const Utilities::MPI& COMM): } ScaLBL_Poisson::~ScaLBL_Poisson() { + ScaLBL_FreeDeviceMemory(NeighborList); + ScaLBL_FreeDeviceMemory(dvcMap); + ScaLBL_FreeDeviceMemory(Psi); + ScaLBL_FreeDeviceMemory(Psi_BCLabel); + ScaLBL_FreeDeviceMemory(ElectricField); + ScaLBL_FreeDeviceMemory(ResidualError); + ScaLBL_FreeDeviceMemory(fq); + if ( TIMELOG ) fclose( TIMELOG ); } @@ -416,6 +424,7 @@ void ScaLBL_Poisson::Create(){ //ScaLBL_Comm->Barrier(); //Initialize solid boundary for electric potential + // DON'T USE WITH CELLULAR SYSTEM (NO SOLID -- NEED Membrane SOLUTION) ScaLBL_Comm->SetupBounceBackList(Map, Mask->id.data(), Np); comm.barrier(); } diff --git a/tests/lbpm_nernst_planck_cell_simulator.cpp b/tests/lbpm_nernst_planck_cell_simulator.cpp index e59db802..488da489 100644 --- a/tests/lbpm_nernst_planck_cell_simulator.cpp +++ b/tests/lbpm_nernst_planck_cell_simulator.cpp @@ -23,7 +23,9 @@ using namespace std; int main(int argc, char **argv) { // Initialize MPI and error handlers - Utilities::startup( argc, argv ); + //Utilities::startup( argc, argv ); + Utilities::startup( argc, argv, true ); + Utilities::MPI comm( MPI_COMM_WORLD ); int rank = comm.getRank(); int nprocs = comm.getSize();