diff --git a/src/MPIIO/dump_custom_mpiio.cpp b/src/MPIIO/dump_custom_mpiio.cpp index f07c1e19d6..678ee3dadd 100644 --- a/src/MPIIO/dump_custom_mpiio.cpp +++ b/src/MPIIO/dump_custom_mpiio.cpp @@ -231,10 +231,12 @@ void DumpCustomMPIIO::init_style() // lo priority = line, medium priority = int/float, hi priority = column auto words = utils::split_words(format); - if ((int) words.size() < nfield) error->all(FLERR, "Dump_modify format line is too short"); + if ((int) words.size() < nfield) + error->all(FLERR, "Dump_modify format line is too short: {}", format); int i = 0; for (const auto &word : words) { + if (i >= nfield) break; delete[] vformat[i]; if (format_column_user[i]) diff --git a/src/dump_custom.cpp b/src/dump_custom.cpp index c93e83e491..1e7ad367ec 100644 --- a/src/dump_custom.cpp +++ b/src/dump_custom.cpp @@ -262,10 +262,11 @@ void DumpCustom::init_style() auto words = utils::split_words(format); if ((int) words.size() < nfield) - error->all(FLERR,"Dump_modify format line is too short"); + error->all(FLERR,"Dump_modify format line is too short: {}", format); int i=0; for (const auto &word : words) { + if (i >= nfield) break; delete[] vformat[i]; if (format_column_user[i]) diff --git a/src/dump_local.cpp b/src/dump_local.cpp index 5fad243e74..bf26890b05 100644 --- a/src/dump_local.cpp +++ b/src/dump_local.cpp @@ -185,10 +185,11 @@ void DumpLocal::init_style() auto words = utils::split_words(format); if ((int) words.size() < size_one) - error->all(FLERR,"Dump_modify format line is too short"); + error->all(FLERR,"Dump_modify format line is too short: {}", format); int i=0; for (const auto &word : words) { + if (i >= size_one) break; delete[] vformat[i]; if (format_column_user[i]) diff --git a/src/fix_langevin.cpp b/src/fix_langevin.cpp index 98b8f5274a..84a628eefe 100644 --- a/src/fix_langevin.cpp +++ b/src/fix_langevin.cpp @@ -1,4 +1,3 @@ -// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -44,21 +43,20 @@ using namespace LAMMPS_NS; using namespace FixConst; -enum{NOBIAS,BIAS}; -enum{CONSTANT,EQUAL,ATOM}; +enum { NOBIAS, BIAS }; +enum { CONSTANT, EQUAL, ATOM }; -#define SINERTIA 0.4 // moment of inertia prefactor for sphere -#define EINERTIA 0.2 // moment of inertia prefactor for ellipsoid +#define SINERTIA 0.4 // moment of inertia prefactor for sphere +#define EINERTIA 0.2 // moment of inertia prefactor for ellipsoid /* ---------------------------------------------------------------------- */ FixLangevin::FixLangevin(LAMMPS *lmp, int narg, char **arg) : - Fix(lmp, narg, arg), - gjfflag(0), gfactor1(nullptr), gfactor2(nullptr), ratio(nullptr), tstr(nullptr), - flangevin(nullptr), tforce(nullptr), franprev(nullptr), - lv(nullptr), id_temp(nullptr), random(nullptr) + Fix(lmp, narg, arg), gjfflag(0), gfactor1(nullptr), gfactor2(nullptr), ratio(nullptr), + tstr(nullptr), flangevin(nullptr), tforce(nullptr), franprev(nullptr), lv(nullptr), + id_temp(nullptr), random(nullptr) { - if (narg < 7) error->all(FLERR,"Illegal fix langevin command"); + if (narg < 7) error->all(FLERR, "Illegal fix langevin command"); dynamic_group_allow = 1; scalar_flag = 1; @@ -67,30 +65,30 @@ FixLangevin::FixLangevin(LAMMPS *lmp, int narg, char **arg) : ecouple_flag = 1; nevery = 1; - if (utils::strmatch(arg[3],"^v_")) { - tstr = utils::strdup(arg[3]+2); + if (utils::strmatch(arg[3], "^v_")) { + tstr = utils::strdup(arg[3] + 2); } else { - t_start = utils::numeric(FLERR,arg[3],false,lmp); + t_start = utils::numeric(FLERR, arg[3], false, lmp); t_target = t_start; tstyle = CONSTANT; } - t_stop = utils::numeric(FLERR,arg[4],false,lmp); - t_period = utils::numeric(FLERR,arg[5],false,lmp); - seed = utils::inumeric(FLERR,arg[6],false,lmp); + t_stop = utils::numeric(FLERR, arg[4], false, lmp); + t_period = utils::numeric(FLERR, arg[5], false, lmp); + seed = utils::inumeric(FLERR, arg[6], false, lmp); - if (t_period <= 0.0) error->all(FLERR,"Fix langevin period must be > 0.0"); - if (seed <= 0) error->all(FLERR,"Illegal fix langevin command"); + if (t_period <= 0.0) error->all(FLERR, "Fix langevin period must be > 0.0"); + if (seed <= 0) error->all(FLERR, "Illegal fix langevin command"); // initialize Marsaglia RNG with processor-unique seed - random = new RanMars(lmp,seed + comm->me); + random = new RanMars(lmp, seed + comm->me); // allocate per-type arrays for force prefactors - gfactor1 = new double[atom->ntypes+1]; - gfactor2 = new double[atom->ntypes+1]; - ratio = new double[atom->ntypes+1]; + gfactor1 = new double[atom->ntypes + 1]; + gfactor2 = new double[atom->ntypes + 1]; + ratio = new double[atom->ntypes + 1]; // optional args @@ -105,48 +103,48 @@ FixLangevin::FixLangevin(LAMMPS *lmp, int narg, char **arg) : int iarg = 7; while (iarg < narg) { - if (strcmp(arg[iarg],"angmom") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix langevin command"); - if (strcmp(arg[iarg+1],"no") == 0) ascale = 0.0; - else ascale = utils::numeric(FLERR,arg[iarg+1],false,lmp); + if (strcmp(arg[iarg], "angmom") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal fix langevin command"); + if (strcmp(arg[iarg + 1], "no") == 0) + ascale = 0.0; + else + ascale = utils::numeric(FLERR, arg[iarg + 1], false, lmp); iarg += 2; - } else if (strcmp(arg[iarg],"gjf") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix langevin command"); - if (strcmp(arg[iarg+1],"no") == 0) { + } else if (strcmp(arg[iarg], "gjf") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal fix langevin command"); + if (strcmp(arg[iarg + 1], "no") == 0) { gjfflag = 0; osflag = 0; - } - else if (strcmp(arg[iarg+1],"vfull") == 0) { + } else if (strcmp(arg[iarg + 1], "vfull") == 0) { gjfflag = 1; osflag = 1; - } - else if (strcmp(arg[iarg+1],"vhalf") == 0) { + } else if (strcmp(arg[iarg + 1], "vhalf") == 0) { gjfflag = 1; osflag = 0; - } - else error->all(FLERR,"Illegal fix langevin command"); + } else + error->all(FLERR, "Illegal fix langevin command"); iarg += 2; - } else if (strcmp(arg[iarg],"omega") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix langevin command"); - oflag = utils::logical(FLERR,arg[iarg+1],false,lmp); + } else if (strcmp(arg[iarg], "omega") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal fix langevin command"); + oflag = utils::logical(FLERR, arg[iarg + 1], false, lmp); iarg += 2; - } else if (strcmp(arg[iarg],"scale") == 0) { - if (iarg+3 > narg) error->all(FLERR,"Illegal fix langevin command"); - int itype = utils::inumeric(FLERR,arg[iarg+1],false,lmp); - double scale = utils::numeric(FLERR,arg[iarg+2],false,lmp); - if (itype <= 0 || itype > atom->ntypes) - error->all(FLERR,"Illegal fix langevin command"); + } else if (strcmp(arg[iarg], "scale") == 0) { + if (iarg + 3 > narg) error->all(FLERR, "Illegal fix langevin command"); + int itype = utils::inumeric(FLERR, arg[iarg + 1], false, lmp); + double scale = utils::numeric(FLERR, arg[iarg + 2], false, lmp); + if (itype <= 0 || itype > atom->ntypes) error->all(FLERR, "Illegal fix langevin command"); ratio[itype] = scale; iarg += 3; - } else if (strcmp(arg[iarg],"tally") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix langevin command"); - tallyflag = utils::logical(FLERR,arg[iarg+1],false,lmp); + } else if (strcmp(arg[iarg], "tally") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal fix langevin command"); + tallyflag = utils::logical(FLERR, arg[iarg + 1], false, lmp); iarg += 2; - } else if (strcmp(arg[iarg],"zero") == 0) { - if (iarg+2 > narg) error->all(FLERR,"Illegal fix langevin command"); - zeroflag = utils::logical(FLERR,arg[iarg+1],false,lmp); + } else if (strcmp(arg[iarg], "zero") == 0) { + if (iarg + 2 > narg) error->all(FLERR, "Illegal fix langevin command"); + zeroflag = utils::logical(FLERR, arg[iarg + 1], false, lmp); iarg += 2; - } else error->all(FLERR,"Illegal fix langevin command"); + } else + error->all(FLERR, "Illegal fix langevin command"); } // set temperature = nullptr, user can override via fix_modify if wants bias @@ -175,7 +173,7 @@ FixLangevin::FixLangevin(LAMMPS *lmp, int narg, char **arg) : FixLangevin::grow_arrays(atom->nmax); atom->add_callback(Atom::GROW); - // initialize franprev to zero + // initialize franprev to zero int nlocal = atom->nlocal; for (int i = 0; i < nlocal; i++) { @@ -194,18 +192,18 @@ FixLangevin::FixLangevin(LAMMPS *lmp, int narg, char **arg) : FixLangevin::~FixLangevin() { delete random; - delete [] tstr; - delete [] gfactor1; - delete [] gfactor2; - delete [] ratio; - delete [] id_temp; + delete[] tstr; + delete[] gfactor1; + delete[] gfactor2; + delete[] ratio; + delete[] id_temp; memory->destroy(flangevin); memory->destroy(tforce); if (gjfflag) { memory->destroy(franprev); memory->destroy(lv); - atom->delete_callback(id,Atom::GROW); + atom->delete_callback(id, Atom::GROW); } } @@ -226,34 +224,38 @@ int FixLangevin::setmask() void FixLangevin::init() { if (gjfflag) { - if (t_period*2 == update->dt) - error->all(FLERR,"Fix langevin gjf cannot have t_period equal to dt/2"); + if (t_period * 2 == update->dt) + error->all(FLERR, "Fix langevin gjf cannot have t_period equal to dt/2"); // warn if any integrate fix comes after this one int before = 1; int flag = 0; for (int i = 0; i < modify->nfix; i++) { - if (strcmp(id,modify->fix[i]->id) == 0) before = 0; - else if ((modify->fmask[i] && utils::strmatch(modify->fix[i]->style,"^nve")) && before) flag = 1; + auto ifix = modify->get_fix_by_index(i); + if (strcmp(id, ifix->id) == 0) + before = 0; + else if ((modify->fmask[i] && utils::strmatch(ifix->style, "^nve")) && before) + flag = 1; } - if (flag) - error->all(FLERR,"Fix langevin gjf should come before fix nve"); + if (flag) error->all(FLERR, "Fix langevin gjf should come before fix nve"); } if (oflag && !atom->sphere_flag) - error->all(FLERR,"Fix langevin omega requires atom style sphere"); + error->all(FLERR, "Fix langevin omega requires atom style sphere"); if (ascale && !atom->ellipsoid_flag) - error->all(FLERR,"Fix langevin angmom requires atom style ellipsoid"); + error->all(FLERR, "Fix langevin angmom requires atom style ellipsoid"); // check variable if (tstr) { tvar = input->variable->find(tstr); - if (tvar < 0) - error->all(FLERR,"Variable name for fix langevin does not exist"); - if (input->variable->equalstyle(tvar)) tstyle = EQUAL; - else if (input->variable->atomstyle(tvar)) tstyle = ATOM; - else error->all(FLERR,"Variable for fix langevin is invalid style"); + if (tvar < 0) error->all(FLERR, "Variable name {} for fix langevin does not exist", tstr); + if (input->variable->equalstyle(tvar)) + tstyle = EQUAL; + else if (input->variable->atomstyle(tvar)) + tstyle = ATOM; + else + error->all(FLERR, "Variable {} for fix langevin is invalid style", tstr); } // if oflag or ascale set, check that all group particles are finite-size @@ -265,14 +267,12 @@ void FixLangevin::init() for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) - if (radius[i] == 0.0) - error->one(FLERR,"Fix langevin omega requires extended particles"); + if (radius[i] == 0.0) error->one(FLERR, "Fix langevin omega requires extended particles"); } if (ascale) { - avec = dynamic_cast( atom->style_match("ellipsoid")); - if (!avec) - error->all(FLERR,"Fix langevin angmom requires atom style ellipsoid"); + avec = dynamic_cast(atom->style_match("ellipsoid")); + if (!avec) error->all(FLERR, "Fix langevin angmom requires atom style ellipsoid"); int *ellipsoid = atom->ellipsoid; int *mask = atom->mask; @@ -280,8 +280,7 @@ void FixLangevin::init() for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) - if (ellipsoid[i] < 0) - error->one(FLERR,"Fix langevin angmom requires extended particles"); + if (ellipsoid[i] < 0) error->one(FLERR, "Fix langevin angmom requires extended particles"); } // set force prefactors @@ -289,30 +288,30 @@ void FixLangevin::init() if (!atom->rmass) { for (int i = 1; i <= atom->ntypes; i++) { gfactor1[i] = -atom->mass[i] / t_period / force->ftm2v; + gfactor2[i] = sqrt(atom->mass[i]) / force->ftm2v; if (gjfflag) - gfactor2[i] = sqrt(atom->mass[i]) * - sqrt(2.0*force->boltz/t_period/update->dt/force->mvv2e) / - force->ftm2v; + gfactor2[i] *= sqrt(2.0 * force->boltz / t_period / update->dt / force->mvv2e); else - gfactor2[i] = sqrt(atom->mass[i]) * - sqrt(24.0*force->boltz/t_period/update->dt/force->mvv2e) / - force->ftm2v; - gfactor1[i] *= 1.0/ratio[i]; - gfactor2[i] *= 1.0/sqrt(ratio[i]); + gfactor2[i] *= sqrt(24.0 * force->boltz / t_period / update->dt / force->mvv2e); + gfactor1[i] *= 1.0 / ratio[i]; + gfactor2[i] *= 1.0 / sqrt(ratio[i]); } } - if (temperature && temperature->tempbias) tbiasflag = BIAS; - else tbiasflag = NOBIAS; + if (temperature && temperature->tempbias) + tbiasflag = BIAS; + else + tbiasflag = NOBIAS; - if (utils::strmatch(update->integrate_style,"^respa")) - nlevels_respa = (dynamic_cast( update->integrate))->nlevels; + if (utils::strmatch(update->integrate_style, "^respa")) { + nlevels_respa = (static_cast(update->integrate))->nlevels; + if (gjfflag) error->all(FLERR, "Fix langevin gjf and run style respa are not compatible"); + } - if (utils::strmatch(update->integrate_style,"^respa") && gjfflag) - error->all(FLERR,"Fix langevin gjf and respa are not compatible"); - - if (gjfflag) gjfa = (1.0-update->dt/2.0/t_period)/(1.0+update->dt/2.0/t_period); - if (gjfflag) gjfsib = sqrt(1.0+update->dt/2.0/t_period); + if (gjfflag) { + gjfa = (1.0 - update->dt / 2.0 / t_period) / (1.0 + update->dt / 2.0 / t_period); + gjfsib = sqrt(1.0 + update->dt / 2.0 / t_period); + } } /* ---------------------------------------------------------------------- */ @@ -336,13 +335,11 @@ void FixLangevin::setup(int vflag) v[i][0] -= dtfm * f[i][0]; v[i][1] -= dtfm * f[i][1]; v[i][2] -= dtfm * f[i][2]; - if (tbiasflag) - temperature->remove_bias(i,v[i]); - v[i][0] /= gjfa*gjfsib*gjfsib; - v[i][1] /= gjfa*gjfsib*gjfsib; - v[i][2] /= gjfa*gjfsib*gjfsib; - if (tbiasflag) - temperature->restore_bias(i,v[i]); + if (tbiasflag) temperature->remove_bias(i, v[i]); + v[i][0] /= gjfa * gjfsib * gjfsib; + v[i][1] /= gjfa * gjfsib * gjfsib; + v[i][2] /= gjfa * gjfsib * gjfsib; + if (tbiasflag) temperature->restore_bias(i, v[i]); } } else { @@ -352,22 +349,21 @@ void FixLangevin::setup(int vflag) v[i][0] -= dtfm * f[i][0]; v[i][1] -= dtfm * f[i][1]; v[i][2] -= dtfm * f[i][2]; - if (tbiasflag) - temperature->remove_bias(i,v[i]); - v[i][0] /= gjfa*gjfsib*gjfsib; - v[i][1] /= gjfa*gjfsib*gjfsib; - v[i][2] /= gjfa*gjfsib*gjfsib; - if (tbiasflag) - temperature->restore_bias(i,v[i]); + if (tbiasflag) temperature->remove_bias(i, v[i]); + v[i][0] /= gjfa * gjfsib * gjfsib; + v[i][1] /= gjfa * gjfsib * gjfsib; + v[i][2] /= gjfa * gjfsib * gjfsib; + if (tbiasflag) temperature->restore_bias(i, v[i]); } } } - if (utils::strmatch(update->integrate_style,"^verlet")) + if (utils::strmatch(update->integrate_style, "^verlet")) post_force(vflag); else { - (dynamic_cast( update->integrate))->copy_flevel_f(nlevels_respa-1); - post_force_respa(vflag,nlevels_respa-1,0); - (dynamic_cast( update->integrate))->copy_f_flevel(nlevels_respa-1); + auto respa = static_cast(update->integrate); + respa->copy_flevel_f(nlevels_respa - 1); + post_force_respa(vflag, nlevels_respa - 1, 0); + respa->copy_f_flevel(nlevels_respa - 1); } if (gjfflag) { double dtfm; @@ -390,7 +386,7 @@ void FixLangevin::setup(int vflag) lv[i][1] = v[i][1]; lv[i][2] = v[i][2]; } -// + // } else { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { @@ -427,6 +423,7 @@ void FixLangevin::initial_integrate(int /* vflag */) } /* ---------------------------------------------------------------------- */ +// clang-format off void FixLangevin::post_force(int /*vflag*/) { @@ -575,8 +572,7 @@ void FixLangevin::post_force_respa(int vflag, int ilevel, int /*iloop*/) modify forces using one of the many Langevin styles ------------------------------------------------------------------------- */ -template < int Tp_TSTYLEATOM, int Tp_GJF, int Tp_TALLY, - int Tp_BIAS, int Tp_RMASS, int Tp_ZERO > +template void FixLangevin::post_force_templated() { double gamma1,gamma2; @@ -988,6 +984,7 @@ void FixLangevin::end_of_step() energy += energy_onestep*update->dt; } +// clang-format on /* ---------------------------------------------------------------------- */ void FixLangevin::reset_target(double t_new) @@ -1001,20 +998,17 @@ void FixLangevin::reset_dt() { if (atom->mass) { for (int i = 1; i <= atom->ntypes; i++) { + gfactor2[i] = sqrt(atom->mass[i]) / force->ftm2v; if (gjfflag) - gfactor2[i] = sqrt(atom->mass[i]) * - sqrt(2.0*force->boltz/t_period/update->dt/force->mvv2e) / - force->ftm2v; + gfactor2[i] *= sqrt(2.0 * force->boltz / t_period / update->dt / force->mvv2e); else - gfactor2[i] = sqrt(atom->mass[i]) * - sqrt(24.0*force->boltz/t_period/update->dt/force->mvv2e) / - force->ftm2v; - gfactor2[i] *= 1.0/sqrt(ratio[i]); + gfactor2[i] *= sqrt(24.0 * force->boltz / t_period / update->dt / force->mvv2e); + gfactor2[i] *= 1.0 / sqrt(ratio[i]); } } if (gjfflag) { - gjfa = (1.0-update->dt/2.0/t_period)/(1.0+update->dt/2.0/t_period); - gjfsib = sqrt(1.0+update->dt/2.0/t_period); + gjfa = (1.0 - update->dt / 2.0 / t_period) / (1.0 + update->dt / 2.0 / t_period); + gjfsib = sqrt(1.0 + update->dt / 2.0 / t_period); } } @@ -1022,21 +1016,19 @@ void FixLangevin::reset_dt() int FixLangevin::modify_param(int narg, char **arg) { - if (strcmp(arg[0],"temp") == 0) { - if (narg < 2) error->all(FLERR,"Illegal fix_modify command"); - delete [] id_temp; + if (strcmp(arg[0], "temp") == 0) { + if (narg < 2) utils::missing_cmd_args(FLERR, "fix_modify", error); + delete[] id_temp; id_temp = utils::strdup(arg[1]); - - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Could not find fix_modify temperature ID"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR, "Could not find fix_modify temperature compute ID: {}", id_temp); if (temperature->tempflag == 0) - error->all(FLERR, - "Fix_modify temperature ID does not compute temperature"); + error->all(FLERR, "Fix_modify temperature compute {} does not compute temperature", id_temp); if (temperature->igroup != igroup && comm->me == 0) - error->warning(FLERR,"Group for fix_modify temp != fix group"); + error->warning(FLERR, "Group for fix_modify temp != fix group: {} vs {}", + group->names[igroup], group->names[temperature->igroup]); return 2; } return 0; @@ -1059,29 +1051,27 @@ double FixLangevin::compute_scalar() if (!gjfflag) { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) - energy_onestep += flangevin[i][0]*v[i][0] + flangevin[i][1]*v[i][1] + - flangevin[i][2]*v[i][2]; - energy = 0.5*energy_onestep*update->dt; + energy_onestep += + flangevin[i][0] * v[i][0] + flangevin[i][1] * v[i][1] + flangevin[i][2] * v[i][2]; + energy = 0.5 * energy_onestep * update->dt; } else { for (int i = 0; i < nlocal; i++) if (mask[i] & groupbit) { - if (tbiasflag) - temperature->remove_bias(i, lv[i]); - energy_onestep += flangevin[i][0]*lv[i][0] + flangevin[i][1]*lv[i][1] + - flangevin[i][2]*lv[i][2]; - if (tbiasflag) - temperature->restore_bias(i, lv[i]); + if (tbiasflag) temperature->remove_bias(i, lv[i]); + energy_onestep += + flangevin[i][0] * lv[i][0] + flangevin[i][1] * lv[i][1] + flangevin[i][2] * lv[i][2]; + if (tbiasflag) temperature->restore_bias(i, lv[i]); } - energy = -0.5*energy_onestep*update->dt; + energy = -0.5 * energy_onestep * update->dt; } } // convert midstep energy back to previous fullstep energy - double energy_me = energy - 0.5*energy_onestep*update->dt; + double energy_me = energy - 0.5 * energy_onestep * update->dt; double energy_all; - MPI_Allreduce(&energy_me,&energy_all,1,MPI_DOUBLE,MPI_SUM,world); + MPI_Allreduce(&energy_me, &energy_all, 1, MPI_DOUBLE, MPI_SUM, world); return -energy_all; } @@ -1092,9 +1082,7 @@ double FixLangevin::compute_scalar() void *FixLangevin::extract(const char *str, int &dim) { dim = 0; - if (strcmp(str,"t_target") == 0) { - return &t_target; - } + if (strcmp(str, "t_target") == 0) { return &t_target; } return nullptr; } @@ -1105,9 +1093,9 @@ void *FixLangevin::extract(const char *str, int &dim) double FixLangevin::memory_usage() { double bytes = 0.0; - if (gjfflag) bytes += (double)atom->nmax*6 * sizeof(double); - if (tallyflag || osflag) bytes += (double)atom->nmax*3 * sizeof(double); - if (tforce) bytes += (double)atom->nmax * sizeof(double); + if (gjfflag) bytes += (double) atom->nmax * 6 * sizeof(double); + if (tallyflag || osflag) bytes += (double) atom->nmax * 3 * sizeof(double); + if (tforce) bytes += (double) atom->nmax * sizeof(double); return bytes; } @@ -1117,8 +1105,8 @@ double FixLangevin::memory_usage() void FixLangevin::grow_arrays(int nmax) { - memory->grow(franprev,nmax,3,"fix_langevin:franprev"); - memory->grow(lv,nmax,3,"fix_langevin:lv"); + memory->grow(franprev, nmax, 3, "fix_langevin:franprev"); + memory->grow(lv, nmax, 3, "fix_langevin:lv"); } /* ---------------------------------------------------------------------- diff --git a/src/fix_press_berendsen.cpp b/src/fix_press_berendsen.cpp index f80acce8c4..67802654ba 100644 --- a/src/fix_press_berendsen.cpp +++ b/src/fix_press_berendsen.cpp @@ -21,6 +21,7 @@ #include "error.h" #include "fix_deform.h" #include "force.h" +#include "group.h" #include "kspace.h" #include "modify.h" #include "update.h" @@ -240,14 +241,14 @@ FixPressBerendsen::FixPressBerendsen(LAMMPS *lmp, int narg, char **arg) : FixPressBerendsen::~FixPressBerendsen() { - delete [] rfix; + delete[] rfix; // delete temperature and pressure if fix created them if (tflag) modify->delete_compute(id_temp); if (pflag) modify->delete_compute(id_press); - delete [] id_temp; - delete [] id_press; + delete[] id_temp; + delete[] id_press; } /* ---------------------------------------------------------------------- */ @@ -268,9 +269,9 @@ void FixPressBerendsen::init() // insure no conflict with fix deform - for (int i = 0; i < modify->nfix; i++) - if (strcmp(modify->fix[i]->style,"deform") == 0) { - int *dimflag = (dynamic_cast( modify->fix[i]))->dimflag; + for (const auto &ifix : modify->get_fix_list()) + if (strcmp(ifix->style, "^deform") == 0) { + int *dimflag = static_cast(ifix)->dimflag; if ((p_flag[0] && dimflag[0]) || (p_flag[1] && dimflag[1]) || (p_flag[2] && dimflag[2])) error->all(FLERR,"Cannot use fix press/berendsen and " @@ -279,18 +280,16 @@ void FixPressBerendsen::init() // set temperature and pressure ptrs - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Temperature ID for fix press/berendsen does not exist"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR, "Temperature compute ID {} for fix press/berendsen does not exist", id_temp); if (temperature->tempbias) which = BIAS; else which = NOBIAS; - icompute = modify->find_compute(id_press); - if (icompute < 0) - error->all(FLERR,"Pressure ID for fix press/berendsen does not exist"); - pressure = modify->compute[icompute]; + pressure = modify->get_compute_by_id(id_press); + if (!pressure) + error->all(FLERR, "Pressure compute ID {} for fix press/berendsen does not exist", id_press); // Kspace setting @@ -300,13 +299,13 @@ void FixPressBerendsen::init() // detect if any rigid fixes exist so rigid bodies move when box is remapped // rfix[] = indices to each fix rigid - delete [] rfix; + delete[] rfix; nrigid = 0; rfix = nullptr; for (int i = 0; i < modify->nfix; i++) if (modify->fix[i]->rigid_flag) nrigid++; - if (nrigid) { + if (nrigid > 0) { rfix = new int[nrigid]; nrigid = 0; for (int i = 0; i < modify->nfix; i++) @@ -460,24 +459,25 @@ int FixPressBerendsen::modify_param(int narg, char **arg) modify->delete_compute(id_temp); tflag = 0; } - delete [] id_temp; + delete[] id_temp; id_temp = utils::strdup(arg[1]); - int icompute = modify->find_compute(arg[1]); - if (icompute < 0) error->all(FLERR,"Could not find fix_modify temperature ID"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(arg[1]); + if (!temperature) + error->all(FLERR,"Could not find fix_modify temperature compute ID: ", arg[1]); if (temperature->tempflag == 0) - error->all(FLERR,"Fix_modify temperature ID does not compute temperature"); + error->all(FLERR,"Fix_modify temperature compute {} does not compute temperature", arg[1]); if (temperature->igroup != 0 && comm->me == 0) - error->warning(FLERR,"Temperature for NPT is not for group all"); + error->warning(FLERR,"Temperature compute {} for fix {} is not for group all: {}", + arg[1], style, group->names[temperature->igroup]); // reset id_temp of pressure to new temperature ID - icompute = modify->find_compute(id_press); - if (icompute < 0) - error->all(FLERR,"Pressure ID for fix press/berendsen does not exist"); - modify->compute[icompute]->reset_extra_compute_fix(id_temp); + auto icompute = modify->get_compute_by_id(id_press); + if (!icompute) + error->all(FLERR,"Pressure compute ID {} for fix {} does not exist", id_press, style); + icompute->reset_extra_compute_fix(id_temp); return 2; @@ -487,15 +487,13 @@ int FixPressBerendsen::modify_param(int narg, char **arg) modify->delete_compute(id_press); pflag = 0; } - delete [] id_press; + delete[] id_press; id_press = utils::strdup(arg[1]); - int icompute = modify->find_compute(arg[1]); - if (icompute < 0) error->all(FLERR,"Could not find fix_modify pressure ID"); - pressure = modify->compute[icompute]; - + pressure = modify->get_compute_by_id(arg[1]); + if (pressure) error->all(FLERR,"Could not find fix_modify pressure compute ID: {}", arg[1]); if (pressure->pressflag == 0) - error->all(FLERR,"Fix_modify pressure ID does not compute pressure"); + error->all(FLERR,"Fix_modify pressure compute {} does not compute pressure", arg[1]); return 2; } return 0; diff --git a/src/fix_temp_berendsen.cpp b/src/fix_temp_berendsen.cpp index 26c14e842d..07ad556943 100644 --- a/src/fix_temp_berendsen.cpp +++ b/src/fix_temp_berendsen.cpp @@ -40,7 +40,8 @@ FixTempBerendsen::FixTempBerendsen(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg), tstr(nullptr), id_temp(nullptr), tflag(0) { - if (narg != 6) error->all(FLERR,"Illegal fix temp/berendsen command"); + if (narg != 6) + error->all(FLERR,"Illegal fix {} command: expected 6 arguments but found {}", style, narg); // Berendsen thermostat should be applied every step @@ -68,7 +69,7 @@ FixTempBerendsen::FixTempBerendsen(LAMMPS *lmp, int narg, char **arg) : // error checks if (t_period <= 0.0) - error->all(FLERR,"Fix temp/berendsen period must be > 0.0"); + error->all(FLERR,"Fix temp/berendsen Tdamp period must be > 0.0"); // create a new compute temp style // id = fix-ID + temp, compute group = fix group @@ -84,12 +85,12 @@ FixTempBerendsen::FixTempBerendsen(LAMMPS *lmp, int narg, char **arg) : FixTempBerendsen::~FixTempBerendsen() { - delete [] tstr; + delete[] tstr; // delete temperature if fix created it if (tflag) modify->delete_compute(id_temp); - delete [] id_temp; + delete[] id_temp; } /* ---------------------------------------------------------------------- */ @@ -110,18 +111,17 @@ void FixTempBerendsen::init() if (tstr) { tvar = input->variable->find(tstr); if (tvar < 0) - error->all(FLERR,"Variable name for fix temp/berendsen does not exist"); + error->all(FLERR,"Variable name {} for fix temp/berendsen does not exist", tstr); if (input->variable->equalstyle(tvar)) tstyle = EQUAL; - else error->all(FLERR,"Variable for fix temp/berendsen is invalid style"); + else error->all(FLERR,"Variable {} for fix temp/berendsen is invalid style", tstr); } - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Temperature ID for fix temp/berendsen does not exist"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR,"Temperature compute ID {} for fix {} does not exist", id_temp, style); if (modify->check_rigid_group_overlap(groupbit)) - error->warning(FLERR,"Cannot thermostat atoms in rigid bodies"); + error->warning(FLERR,"Cannot thermostat atoms in rigid bodies with fix {}", style); if (temperature->tempbias) which = BIAS; else which = NOBIAS; @@ -139,8 +139,7 @@ void FixTempBerendsen::end_of_step() if (tdof < 1) return; if (t_current == 0.0) - error->all(FLERR, - "Computed temperature for fix temp/berendsen cannot be 0.0"); + error->all(FLERR, "Computed current temperature for fix temp/berendsen must not be 0.0"); double delta = update->ntimestep - update->beginstep; if (delta != 0.0) delta /= update->endstep - update->beginstep; @@ -154,8 +153,8 @@ void FixTempBerendsen::end_of_step() modify->clearstep_compute(); t_target = input->variable->compute_equal(tvar); if (t_target < 0.0) - error->one(FLERR, - "Fix temp/berendsen variable returned negative temperature"); + error->one(FLERR, "Fix temp/berendsen variable {} returned negative temperature", + input->variable->names[tvar]); modify->addstep_compute(update->ntimestep + nevery); } @@ -198,24 +197,23 @@ void FixTempBerendsen::end_of_step() int FixTempBerendsen::modify_param(int narg, char **arg) { if (strcmp(arg[0],"temp") == 0) { - if (narg < 2) error->all(FLERR,"Illegal fix_modify command"); + if (narg < 2) utils::missing_cmd_args(FLERR, "fix_modify", error); if (tflag) { modify->delete_compute(id_temp); tflag = 0; } - delete [] id_temp; + delete[] id_temp; id_temp = utils::strdup(arg[1]); - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Could not find fix_modify temperature ID"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR,"Could not find fix_modify temperature compute {}", id_temp); if (temperature->tempflag == 0) - error->all(FLERR, - "Fix_modify temperature ID does not compute temperature"); + error->all(FLERR, "Fix_modify temperature compute {} does not compute temperature", id_temp); if (temperature->igroup != igroup && comm->me == 0) - error->warning(FLERR,"Group for fix_modify temp != fix group"); + error->warning(FLERR, "Group for fix_modify temp != fix group: {} vs {}", + group->names[igroup], group->names[temperature->igroup]); return 2; } return 0; diff --git a/src/fix_temp_rescale.cpp b/src/fix_temp_rescale.cpp index f4f462c968..e248dfe92c 100644 --- a/src/fix_temp_rescale.cpp +++ b/src/fix_temp_rescale.cpp @@ -43,7 +43,7 @@ FixTempRescale::FixTempRescale(LAMMPS *lmp, int narg, char **arg) : if (narg < 8) utils::missing_cmd_args(FLERR, "fix temp/rescale", error); nevery = utils::inumeric(FLERR,arg[3],false,lmp); - if (nevery <= 0) error->all(FLERR, "Invalid fix temp/rescale argument: {}", nevery); + if (nevery <= 0) error->all(FLERR, "Invalid fix temp/rescale every argument: {}", nevery); restart_global = 1; scalar_flag = 1; @@ -66,6 +66,10 @@ FixTempRescale::FixTempRescale(LAMMPS *lmp, int narg, char **arg) : t_window = utils::numeric(FLERR,arg[6],false,lmp); fraction = utils::numeric(FLERR,arg[7],false,lmp); + if (t_stop < 0) error->all(FLERR, "Invalid fix temp/rescale Tstop argument: {}", t_stop); + if (t_window < 0) error->all(FLERR, "Invalid fix temp/rescale window argument: {}", t_window); + if (fraction <= 0) error->all(FLERR, "Invalid fix temp/rescale fraction argument: {}", fraction); + // create a new compute temp // id = fix-ID + temp, compute group = fix group @@ -80,12 +84,12 @@ FixTempRescale::FixTempRescale(LAMMPS *lmp, int narg, char **arg) : FixTempRescale::~FixTempRescale() { - delete [] tstr; + delete[] tstr; // delete temperature if fix created it if (tflag) modify->delete_compute(id_temp); - delete [] id_temp; + delete[] id_temp; } /* ---------------------------------------------------------------------- */ @@ -106,15 +110,14 @@ void FixTempRescale::init() if (tstr) { tvar = input->variable->find(tstr); if (tvar < 0) - error->all(FLERR,"Variable {} for fix temp/rescale does not exist", tstr); + error->all(FLERR,"Variable name {} for fix temp/rescale does not exist", tstr); if (input->variable->equalstyle(tvar)) tstyle = EQUAL; else error->all(FLERR,"Variable {} for fix temp/rescale is invalid style", tstr); } - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Temperature ID for fix temp/rescale does not exist"); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR,"Temperature ID {} for fix temp/rescale does not exist", id_temp); if (temperature->tempbias) which = BIAS; else which = NOBIAS; @@ -147,8 +150,7 @@ void FixTempRescale::end_of_step() modify->clearstep_compute(); t_target = input->variable->compute_equal(tvar); if (t_target < 0.0) - error->one(FLERR, - "Fix temp/rescale variable returned negative temperature"); + error->one(FLERR, "Fix temp/rescale variable returned negative temperature"); modify->addstep_compute(update->ntimestep + nevery); } @@ -200,19 +202,18 @@ int FixTempRescale::modify_param(int narg, char **arg) modify->delete_compute(id_temp); tflag = 0; } - delete [] id_temp; + delete[] id_temp; id_temp = utils::strdup(arg[1]); - int icompute = modify->find_compute(id_temp); - if (icompute < 0) - error->all(FLERR,"Could not find fix_modify temperature ID {}", arg[1]); - temperature = modify->compute[icompute]; + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) + error->all(FLERR,"Could not find fix_modify temperature compute {}", id_temp); if (temperature->tempflag == 0) - error->all(FLERR, - "Fix_modify temperature ID does not compute temperature"); + error->all(FLERR, "Fix_modify temperature compute {} does not compute temperature", id_temp); if (temperature->igroup != igroup && comm->me == 0) - error->warning(FLERR,"Group for fix_modify temp != fix group"); + error->warning(FLERR, "Group for fix_modify temp != fix group: {} vs {}", + group->names[igroup], group->names[temperature->igroup]); return 2; } return 0; diff --git a/src/fix_vector.cpp b/src/fix_vector.cpp index e916b8e9fe..38169bfcff 100644 --- a/src/fix_vector.cpp +++ b/src/fix_vector.cpp @@ -1,4 +1,3 @@ -// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories @@ -26,41 +25,34 @@ using namespace LAMMPS_NS; using namespace FixConst; -enum{ONE,RUNNING,WINDOW}; -enum{SCALAR,VECTOR}; - +enum { ONE, RUNNING, WINDOW }; +enum { SCALAR, VECTOR }; /* ---------------------------------------------------------------------- */ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) : - Fix(lmp, narg, arg), - nvalues(0), which(nullptr), argindex(nullptr), value2index(nullptr), ids(nullptr), vector(nullptr), array(nullptr) + Fix(lmp, narg, arg), vector(nullptr), array(nullptr) { - if (narg < 5) error->all(FLERR,"Illegal fix vector command"); + if (narg < 5) utils::missing_cmd_args(FLERR, "fix vector", error); - nevery = utils::inumeric(FLERR,arg[3],false,lmp); - if (nevery <= 0) error->all(FLERR,"Illegal fix vector command"); + nevery = utils::inumeric(FLERR, arg[3], false, lmp); + if (nevery <= 0) error->all(FLERR, "Invalid fix vector every argument: {}", nevery); - nvalues = narg-4; - which = new int[nvalues]; - argindex = new int[nvalues]; - value2index = new int[nvalues]; - ids = new char*[nvalues]; - - nvalues = 0; + values.clear(); for (int iarg = 4; iarg < narg; iarg++) { ArgInfo argi(arg[iarg]); - which[nvalues] = argi.get_type(); - argindex[nvalues] = argi.get_index1(); - ids[nvalues] = argi.copy_name(); + value_t val; + val.which = argi.get_type(); + val.argindex = argi.get_index1(); + val.id = argi.get_name(); + val.val.c = nullptr; - if ((argi.get_type() == ArgInfo::UNKNOWN) - || (argi.get_type() == ArgInfo::NONE) - || (argi.get_dim() > 1)) - error->all(FLERR,"Illegal fix vector command"); + if ((argi.get_type() == ArgInfo::UNKNOWN) || (argi.get_type() == ArgInfo::NONE) || + (argi.get_dim() > 1)) + error->all(FLERR, "Invalid fix vector argument: {}", arg[iarg]); - nvalues++; + values.push_back(val); } // setup and error check @@ -68,59 +60,70 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) : // this fix produces either a global vector or array // intensive/extensive flags set by compute,fix,variable that produces value - int value,finalvalue; - for (int i = 0; i < nvalues; i++) { - if (which[i] == ArgInfo::COMPUTE) { - auto icompute = modify->get_compute_by_id(ids[i]); - if (!icompute) error->all(FLERR,"Compute ID {} for fix vector does not exist",ids[i]); - if (argindex[i] == 0 && icompute->scalar_flag == 0) - error->all(FLERR,"Fix vector compute {} does not calculate a scalar",ids[i]); - if (argindex[i] && icompute->vector_flag == 0) - error->all(FLERR,"Fix vector compute {} does not calculate a vector",ids[i]); - if (argindex[i] && argindex[i] > icompute->size_vector) - error->all(FLERR,"Fix vector compute {} vector is accessed out-of-range",ids[i]); + int value, finalvalue; + bool first = true; + for (auto &val : values) { + if (val.which == ArgInfo::COMPUTE) { + auto icompute = modify->get_compute_by_id(val.id); + if (!icompute) error->all(FLERR, "Compute ID {} for fix vector does not exist", val.id); + if (val.argindex == 0 && icompute->scalar_flag == 0) + error->all(FLERR, "Fix vector compute {} does not calculate a scalar", val.id); + if (val.argindex && icompute->vector_flag == 0) + error->all(FLERR, "Fix vector compute {} does not calculate a vector", val.id); + if (val.argindex && (val.argindex > icompute->size_vector)) + error->all(FLERR, "Fix vector compute {} vector is accessed out-of-range", val.id); - if (argindex[i] == 0) value = icompute->extscalar; - else if (icompute->extvector >= 0) value = icompute->extvector; - else value = icompute->extlist[argindex[i]-1]; + if (val.argindex == 0) + value = icompute->extscalar; + else if (icompute->extvector >= 0) + value = icompute->extvector; + else + value = icompute->extlist[val.argindex - 1]; + val.val.c = icompute; - } else if (which[i] == ArgInfo::FIX) { - auto ifix = modify->get_fix_by_id(ids[i]); - if (!ifix) error->all(FLERR,"Fix ID {} for fix vector does not exist",ids[i]); - if (argindex[i] == 0 && ifix->scalar_flag == 0) - error->all(FLERR,"Fix vector fix {} does not calculate a scalar",ids[i]); - if (argindex[i] && ifix->vector_flag == 0) - error->all(FLERR,"Fix vector fix {} does not calculate a vector",ids[i]); - if (argindex[i] && argindex[i] > ifix->size_vector) - error->all(FLERR,"Fix vector fix {} vector is accessed out-of-range",ids[i]); + } else if (val.which == ArgInfo::FIX) { + auto ifix = modify->get_fix_by_id(val.id); + if (!ifix) error->all(FLERR, "Fix ID {} for fix vector does not exist", val.id); + if (val.argindex == 0 && ifix->scalar_flag == 0) + error->all(FLERR, "Fix vector fix {} does not calculate a scalar", val.id); + if (val.argindex && ifix->vector_flag == 0) + error->all(FLERR, "Fix vector fix {} does not calculate a vector", val.id); + if (val.argindex && val.argindex > ifix->size_vector) + error->all(FLERR, "Fix vector fix {} vector is accessed out-of-range", val.id); if (nevery % ifix->global_freq) - error->all(FLERR,"Fix for fix {} vector not computed at compatible time",ids[i]); + error->all(FLERR, "Fix for fix {} vector not computed at compatible time", val.id); - if (argindex[i] == 0) value = ifix->extvector; - else value = ifix->extarray; + if (val.argindex == 0) + value = ifix->extvector; + else + value = ifix->extarray; + val.val.f = ifix; - } else if (which[i] == ArgInfo::VARIABLE) { - int ivariable = input->variable->find(ids[i]); + } else if (val.which == ArgInfo::VARIABLE) { + int ivariable = input->variable->find(val.id.c_str()); if (ivariable < 0) - error->all(FLERR,"Variable name {} for fix vector does not exist",ids[i]); - if (argindex[i] == 0 && input->variable->equalstyle(ivariable) == 0) - error->all(FLERR,"Fix vector variable {} is not equal-style variable",ids[i]); - if (argindex[i] && input->variable->vectorstyle(ivariable) == 0) - error->all(FLERR,"Fix vector variable {} is not vector-style variable",ids[i]); + error->all(FLERR, "Variable name {} for fix vector does not exist", val.id); + if (val.argindex == 0 && input->variable->equalstyle(ivariable) == 0) + error->all(FLERR, "Fix vector variable {} is not equal-style variable", val.id); + if (val.argindex && input->variable->vectorstyle(ivariable) == 0) + error->all(FLERR, "Fix vector variable {} is not vector-style variable", val.id); value = 0; + val.val.v = ivariable; } - if (i == 0) finalvalue = value; - else if (value != finalvalue) - error->all(FLERR,"Fix vector cannot set output array intensive/extensive from these inputs"); + if (first) { + finalvalue = value; + first = false; + } else if (value != finalvalue) + error->all(FLERR, "Fix vector cannot set output array intensive/extensive from these inputs"); } - if (nvalues == 1) { + if (values.size() == 1) { vector_flag = 1; extvector = finalvalue; } else { array_flag = 1; - size_array_cols = nvalues; + size_array_cols = values.size(); extarray = finalvalue; } @@ -132,15 +135,17 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) : vector = nullptr; array = nullptr; ncount = ncountmax = 0; - if (nvalues == 1) size_vector = 0; - else size_array_rows = 0; + if (values.size() == 1) + size_vector = 0; + else + size_array_rows = 0; // nextstep = next step on which end_of_step does something // add nextstep to all computes that store invocation times // since don't know a priori which are invoked by this fix // once in end_of_step() can set timestep for ones actually invoked - nextstep = (update->ntimestep/nevery)*nevery; + nextstep = (update->ntimestep / nevery) * nevery; if (nextstep < update->ntimestep) nextstep += nevery; modify->addstep_compute_all(nextstep); @@ -153,12 +158,7 @@ FixVector::FixVector(LAMMPS *lmp, int narg, char **arg) : FixVector::~FixVector() { - delete [] which; - delete [] argindex; - delete [] value2index; - for (int i = 0; i < nvalues; i++) delete [] ids[i]; - delete [] ids; - + values.clear(); memory->destroy(vector); memory->destroy(array); } @@ -178,22 +178,19 @@ void FixVector::init() { // set current indices for all computes,fixes,variables - for (int i = 0; i < nvalues; i++) { - if (which[i] == ArgInfo::COMPUTE) { - int icompute = modify->find_compute(ids[i]); - if (icompute < 0) error->all(FLERR,"Compute ID {} for fix vector does not exist",id[i]); - value2index[i] = icompute; + for (auto &val : values) { + if (val.which == ArgInfo::COMPUTE) { + val.val.c = modify->get_compute_by_id(val.id); + if (!val.val.c) error->all(FLERR, "Compute ID {} for fix vector does not exist", val.id); - } else if (which[i] == ArgInfo::FIX) { - int ifix = modify->find_fix(ids[i]); - if (ifix < 0) error->all(FLERR,"Fix ID {} for fix vector does not exist",id[i]); - value2index[i] = ifix; + } else if (val.which == ArgInfo::FIX) { + val.val.f = modify->get_fix_by_id(val.id); + if (!val.val.f) error->all(FLERR, "Fix ID {} for fix vector does not exist", val.id); - } else if (which[i] == ArgInfo::VARIABLE) { - int ivariable = input->variable->find(ids[i]); - if (ivariable < 0) - error->all(FLERR,"Variable name for fix vector does not exist"); - value2index[i] = ivariable; + } else if (val.which == ArgInfo::VARIABLE) { + int ivariable = input->variable->find(val.id.c_str()); + if (ivariable < 0) error->all(FLERR, "Variable name for fix vector does not exist"); + val.val.v = ivariable; } } @@ -201,11 +198,13 @@ void FixVector::init() // use endstep to allow for subsequent runs with "pre no" // nsize = # of entries from initialstep to finalstep - bigint finalstep = update->endstep/nevery * nevery; + bigint finalstep = update->endstep / nevery * nevery; if (finalstep > update->endstep) finalstep -= nevery; - ncountmax = (finalstep-initialstep)/nevery + 1; - if (nvalues == 1) memory->grow(vector,ncountmax,"vector:vector"); - else memory->grow(array,ncountmax,nvalues,"vector:array"); + ncountmax = (finalstep - initialstep) / nevery + 1; + if (values.size() == 1) + memory->grow(vector, ncountmax, "vector:vector"); + else + memory->grow(array, ncountmax, values.size(), "vector:array"); } /* ---------------------------------------------------------------------- @@ -224,61 +223,64 @@ void FixVector::end_of_step() // skip if not step which requires doing something if (update->ntimestep != nextstep) return; - if (ncount == ncountmax) - error->all(FLERR,"Overflow of allocated fix vector storage"); + if (ncount == ncountmax) error->all(FLERR, "Overflow of allocated fix vector storage"); // accumulate results of computes,fixes,variables to local copy // compute/fix/variable may invoke computes so wrap with clear/add double *result; - if (nvalues == 1) result = &vector[ncount]; - else result = array[ncount]; + if (values.size() == 1) + result = &vector[ncount]; + else + result = array[ncount]; modify->clearstep_compute(); - for (int i = 0; i < nvalues; i++) { - int m = value2index[i]; + int i = 0; + for (auto &val : values) { // invoke compute if not previously invoked - if (which[i] == ArgInfo::COMPUTE) { - auto compute = modify->get_compute_by_index(m); + if (val.which == ArgInfo::COMPUTE) { - if (argindex[i] == 0) { - if (!(compute->invoked_flag & Compute::INVOKED_SCALAR)) { - compute->compute_scalar(); - compute->invoked_flag |= Compute::INVOKED_SCALAR; + if (val.argindex == 0) { + if (!(val.val.c->invoked_flag & Compute::INVOKED_SCALAR)) { + val.val.c->compute_scalar(); + val.val.c->invoked_flag |= Compute::INVOKED_SCALAR; } - result[i] = compute->scalar; + result[i] = val.val.c->scalar; } else { - if (!(compute->invoked_flag & Compute::INVOKED_VECTOR)) { - compute->compute_vector(); - compute->invoked_flag |= Compute::INVOKED_VECTOR; + if (!(val.val.c->invoked_flag & Compute::INVOKED_VECTOR)) { + val.val.c->compute_vector(); + val.val.c->invoked_flag |= Compute::INVOKED_VECTOR; } - result[i] = compute->vector[argindex[i]-1]; + result[i] = val.val.c->vector[val.argindex - 1]; } - // access fix fields, guaranteed to be ready + // access fix fields, guaranteed to be ready - } else if (which[i] == ArgInfo::FIX) { - if (argindex[i] == 0) - result[i] = modify->get_fix_by_index(m)->compute_scalar(); + } else if (val.which == ArgInfo::FIX) { + if (val.argindex == 0) + result[i] = val.val.f->compute_scalar(); else - result[i] = modify->get_fix_by_index(m)->compute_vector(argindex[i]-1); + result[i] = val.val.f->compute_vector(val.argindex - 1); - // evaluate equal-style or vector-style variable + // evaluate equal-style or vector-style variable - } else if (which[i] == ArgInfo::VARIABLE) { - if (argindex[i] == 0) - result[i] = input->variable->compute_equal(m); + } else if (val.which == ArgInfo::VARIABLE) { + if (val.argindex == 0) + result[i] = input->variable->compute_equal(val.val.v); else { double *varvec; - int nvec = input->variable->compute_vector(m,&varvec); - int index = argindex[i]; - if (nvec < index) result[i] = 0.0; - else result[i] = varvec[index-1]; + int nvec = input->variable->compute_vector(val.val.v, &varvec); + int index = val.argindex; + if (nvec < index) + result[i] = 0.0; + else + result[i] = varvec[index - 1]; } } + ++i; } // trigger computes on next needed step @@ -289,8 +291,10 @@ void FixVector::end_of_step() // update size of vector or array ncount++; - if (nvalues == 1) size_vector++; - else size_array_rows++; + if (values.size() == 1) + size_vector++; + else + size_array_rows++; } /* ---------------------------------------------------------------------- diff --git a/src/fix_vector.h b/src/fix_vector.h index 6d0db294c6..3c2f69b21b 100644 --- a/src/fix_vector.h +++ b/src/fix_vector.h @@ -36,9 +36,17 @@ class FixVector : public Fix { double compute_array(int, int) override; private: - int nvalues; - int *which, *argindex, *value2index; - char **ids; + struct value_t { + int which; + int argindex; + std::string id; + union { + class Compute *c; + class Fix *f; + int v; + } val; + }; + std::vector values; bigint nextstep, initialstep; @@ -47,8 +55,6 @@ class FixVector : public Fix { double *vector; double **array; }; - } // namespace LAMMPS_NS - #endif #endif