fix memory leak and avoid uninitialized data access

This commit is contained in:
Axel Kohlmeyer
2022-09-22 13:12:15 -04:00
parent 4f0caca9d8
commit 96f04237c1

View File

@ -140,6 +140,8 @@ FixChargeRegulation::FixChargeRegulation(LAMMPS *lmp, int narg, char **arg) :
nsalt_successes = 0;
}
/* ---------------------------------------------------------------------- */
FixChargeRegulation::~FixChargeRegulation() {
memory->destroy(ptype_ID);
@ -153,14 +155,23 @@ FixChargeRegulation::~FixChargeRegulation() {
int igroupall = group->find("all");
neighbor->exclusion_group_group_delete(exclusion_group, igroupall);
}
if (groupstrings) {
for (int i = 0; i < ngroups; ++i) delete[] groupstrings[i];
memory->destroy(groupstrings);
}
}
/* ---------------------------------------------------------------------- */
int FixChargeRegulation::setmask() {
int mask = 0;
mask |= PRE_EXCHANGE;
return mask;
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::init() {
triclinic = domain->triclinic;
@ -246,6 +257,8 @@ void FixChargeRegulation::init() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::pre_exchange() {
if (next_reneighbor != update->ntimestep) return;
@ -372,6 +385,8 @@ void FixChargeRegulation::pre_exchange() {
next_reneighbor = update->ntimestep + nevery;
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::forward_acid() {
double energy_before = energy_stored;
@ -429,17 +444,16 @@ void FixChargeRegulation::forward_acid() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::backward_acid() {
double energy_before = energy_stored;
double factor;
int mask_tmp;
double dummyp[3];
double pos[3];
pos[0] = 0;
pos[1] = 0;
pos[2] = 0; // acid/base particle position
double pos_all[3];
double dummyp[3] = {0.0, 0.0, 0.0};
double pos[3] = {0.0, 0.0, 0.0}; // acid/base particle position
double pos_all[3] = {0.0, 0.0, 0.0};
int m1 = -1, m2 = -1;
m1 = get_random_particle(acid_type, -1, 0, dummyp);
@ -507,16 +521,15 @@ void FixChargeRegulation::backward_acid() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::forward_base() {
double energy_before = energy_stored;
double factor;
double dummyp[3];
double pos[3];
pos[0] = 0;
pos[1] = 0;
pos[2] = 0; // acid/base particle position
double pos_all[3];
double dummyp[3] = {0.0, 0.0, 0.0};
double pos[3] = {0.0, 0.0, 0.0}; // acid/base particle position
double pos_all[3] = {0.0, 0.0, 0.0};
int m1 = -1, m2 = -1;
m1 = get_random_particle(base_type, 0, 0, dummyp);
@ -567,17 +580,16 @@ void FixChargeRegulation::forward_base() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::backward_base() {
double energy_before = energy_stored;
double factor;
double dummyp[3];
double dummyp[3] = {0.0, 0.0, 0.0};
double pos[3] = {0.0, 0.0, 0.0}; // acid/base particle position
double pos_all[3] = {0.0, 0.0, 0.0};
int mask_tmp;
double pos[3];
pos[0] = 0;
pos[1] = 0;
pos[2] = 0; // acid/base particle position
double pos_all[3];
int m1 = -1, m2 = -1;
m1 = get_random_particle(base_type, 1, 0, dummyp);
@ -644,11 +656,13 @@ void FixChargeRegulation::backward_base() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::forward_ions() {
double energy_before = energy_stored;
double factor;
double dummyp[3];
double dummyp[3] = {0.0, 0.0, 0.0};
int m1 = -1, m2 = -1;
factor = volume_rx * volume_rx * c10pI_plus * c10pI_minus /
((1 + ncation) * (1 + nanion));
@ -679,13 +693,14 @@ void FixChargeRegulation::forward_ions() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::backward_ions() {
double energy_before = energy_stored;
double factor;
int mask1_tmp = 0, mask2_tmp = 0;
double dummyp[3];
double dummyp[3] = {0.0, 0.0, 0.0};
int m1 = -1, m2 = -1;
m1 = get_random_particle(cation_type, +1, 0, dummyp);
@ -761,11 +776,13 @@ void FixChargeRegulation::backward_ions() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::forward_ions_multival() {
double energy_before = energy_stored;
double factor = 1;
double dummyp[3];
double dummyp[3] = {0.0, 0.0, 0.0};
// particle ID array for all ions to be inserted
auto mm = std::unique_ptr<int[]>(new int[salt_charge_ratio + 1]);
@ -819,11 +836,13 @@ void FixChargeRegulation::forward_ions_multival() {
}
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::backward_ions_multival() {
double energy_before = energy_stored;
double factor = 1;
double dummyp[3]; // dummy particle
double dummyp[3] = {0.0, 0.0, 0.0}; // dummy particle
// particle ID array for all deleted ions
auto mm = std::unique_ptr<int[]>(new int[salt_charge_ratio + 1]);
// charge array for all deleted ions
@ -940,6 +959,8 @@ void FixChargeRegulation::backward_ions_multival() {
}
}
/* ---------------------------------------------------------------------- */
int FixChargeRegulation::insert_particle(int ptype, double charge, double rd, double *target) {
// insert a particle of type (ptype) with charge (charge) within distance (rd) of (target)
@ -1010,6 +1031,8 @@ int FixChargeRegulation::insert_particle(int ptype, double charge, double rd, do
return m;
}
/* ---------------------------------------------------------------------- */
int FixChargeRegulation::get_random_particle(int ptype, double charge, double rd, double *target) {
// returns a randomly chosen particle of type (ptype) with charge (charge)
@ -1074,6 +1097,8 @@ int FixChargeRegulation::get_random_particle(int ptype, double charge, double rd
return -1;
}
/* ---------------------------------------------------------------------- */
double FixChargeRegulation::energy_full() {
if (triclinic) domain->x2lamda(atom->nlocal);
domain->pbc();
@ -1133,6 +1158,8 @@ double FixChargeRegulation::energy_full() {
return total_energy;
}
/* ---------------------------------------------------------------------- */
int FixChargeRegulation::particle_number_xrd(int ptype, double charge, double rd, double *target) {
int count = 0;
@ -1162,6 +1189,8 @@ int FixChargeRegulation::particle_number_xrd(int ptype, double charge, double rd
return count_sum;
}
/* ---------------------------------------------------------------------- */
int FixChargeRegulation::particle_number(int ptype, double charge) {
int count = 0;
@ -1174,6 +1203,8 @@ int FixChargeRegulation::particle_number(int ptype, double charge) {
return count_sum;
}
/* ---------------------------------------------------------------------- */
double FixChargeRegulation::compute_vector(int n) {
if (n == 0) {
return nacid_attempts + nbase_attempts + nsalt_attempts;
@ -1250,6 +1281,8 @@ void FixChargeRegulation::restart(char *buf)
error->all(FLERR,"Must not reset timestep when restarting fix gcmc");
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::setThermoTemperaturePointer() {
int ifix = -1;
ifix = modify->find_fix(idftemp);
@ -1263,6 +1296,8 @@ void FixChargeRegulation::setThermoTemperaturePointer() {
}
/* ---------------------------------------------------------------------- */
void FixChargeRegulation::assign_tags() {
// Assign tags to ions with zero tags
if (atom->tag_enable) {