modernize lookup of fixes

This commit is contained in:
Axel Kohlmeyer
2023-04-02 17:46:42 -04:00
parent eb13217498
commit 09deafd6d4
7 changed files with 55 additions and 78 deletions

View File

@ -324,7 +324,7 @@ FixColvars::FixColvars(LAMMPS *lmp, int narg, char **arg) :
if (!out_name) out_name = utils::strdup("out"); if (!out_name) out_name = utils::strdup("out");
/* initialize various state variables. */ /* initialize various state variables. */
tstat_id = -1; tstat_fix = nullptr;
energy = 0.0; energy = 0.0;
nlevels_respa = 0; nlevels_respa = 0;
init_flag = 0; init_flag = 0;
@ -432,12 +432,13 @@ void FixColvars::one_time_init()
double t_target = 0.0; double t_target = 0.0;
if (tmp_name) { if (tmp_name) {
if (strcmp(tmp_name,"NULL") == 0) { if (strcmp(tmp_name,"NULL") == 0) {
tstat_id = -1; tstat_fix = nullptr;
} else { } else {
tstat_id = modify->find_fix(tmp_name); tstat_fix = modify->get_fix_by_id(tmp_name);
if (tstat_id < 0) error->one(FLERR,"Could not find tstat fix ID"); if (!tstat_fix) error->one(FLERR, "Could not find thermostat fix ID {}", tmp_name);
double *tt = (double*)modify->fix[tstat_id]->extract("t_target",tmp); double *tt = (double*) tstat_fix->extract("t_target", tmp);
if (tt) t_target = *tt; if (tt) t_target = *tt;
else error->one(FLERR, "Fix ID {} is not a thermostat fix", tmp_name);
} }
} }
@ -675,13 +676,13 @@ void FixColvars::post_force(int /*vflag*/)
if (proxy->want_exit()) if (proxy->want_exit())
error->one(FLERR,"Run aborted on request from colvars module.\n"); error->one(FLERR,"Run aborted on request from colvars module.\n");
if (tstat_id < 0) { if (!tstat_fix) {
proxy->set_temperature(0.0); proxy->set_temperature(0.0);
} else { } else {
int tmp; int tmp;
// get thermostat target temperature from corresponding fix, // get thermostat target temperature from corresponding fix,
// if the fix supports extraction. // if the fix supports extraction.
double *tt = (double *) modify->fix[tstat_id]->extract("t_target",tmp); double *tt = (double *) tstat_fix->extract("t_target", tmp);
if (tt) if (tt)
proxy->set_temperature(*tt); proxy->set_temperature(*tt);
else else

View File

@ -68,7 +68,7 @@ class FixColvars : public Fix {
char *out_name; // prefix string for all output files char *out_name; // prefix string for all output files
char *tmp_name; // name of thermostat fix. char *tmp_name; // name of thermostat fix.
int rng_seed; // seed to initialize random number generator int rng_seed; // seed to initialize random number generator
int tstat_id; // id of the thermostat fix Fix *tstat_fix; // pointer to thermostat fix
double energy; // biasing energy of the fix double energy; // biasing energy of the fix
int me; // my MPI rank in this "world". int me; // my MPI rank in this "world".

View File

@ -188,11 +188,11 @@ void ComputeHMA::init_list(int /* id */, NeighList *ptr)
void ComputeHMA::setup() void ComputeHMA::setup()
{ {
int dummy=0; int dummy=0;
int ifix = modify->find_fix(id_temp); Fix *ifix = modify->get_fix_by_id(id_temp);
if (ifix < 0) error->all(FLERR,"Could not find compute hma temperature ID"); if (!ifix) error->all(FLERR,"Could not find compute hma temperature fix ID {}", id_temp);
auto temperat = (double *) modify->fix[ifix]->extract("t_target",dummy); auto temperat = (double *) ifix->extract("t_target",dummy);
if (temperat==nullptr) error->all(FLERR,"Could not find compute hma temperature ID"); if (temperat == nullptr) error->all(FLERR,"Fix ID {} is not a thermostat {}", id_temp);
finaltemp = * temperat; finaltemp = *temperat;
// set fix which stores original atom coords // set fix which stores original atom coords

View File

@ -71,28 +71,22 @@ FixController::FixController(LAMMPS *lmp, int narg, char **arg) :
// error check // error check
if (pvwhich == ArgInfo::COMPUTE) { if (pvwhich == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(pvID); Compute *c = modify->get_compute_by_id(pvID);
if (icompute < 0) if (!c) error->all(FLERR,"Compute ID {} for fix controller does not exist", pvID);
error->all(FLERR,"Compute ID for fix controller does not exist");
Compute *c = modify->compute[icompute];
int flag = 0; int flag = 0;
if (c->scalar_flag && pvindex == 0) flag = 1; if (c->scalar_flag && pvindex == 0) flag = 1;
else if (c->vector_flag && pvindex > 0) flag = 1; else if (c->vector_flag && pvindex > 0) flag = 1;
if (!flag) error->all(FLERR,"Fix controller compute does not " if (!flag)
"calculate a global scalar or vector"); error->all(FLERR,"Fix controller compute does not calculate a global scalar or vector");
if (pvindex && pvindex > c->size_vector) if (pvindex && pvindex > c->size_vector)
error->all(FLERR,"Fix controller compute vector is " error->all(FLERR,"Fix controller compute vector is accessed out-of-range");
"accessed out-of-range");
} else if (pvwhich == ArgInfo::FIX) { } else if (pvwhich == ArgInfo::FIX) {
int ifix = modify->find_fix(pvID); Fix *f = modify->get_fix_by_id(pvID);
if (ifix < 0) if (!f) error->all(FLERR,"Fix ID {} for fix controller does not exist", pvID);
error->all(FLERR,"Fix ID for fix controller does not exist");
Fix *f = modify->fix[ifix];
int flag = 0; int flag = 0;
if (f->scalar_flag && pvindex == 0) flag = 1; if (f->scalar_flag && pvindex == 0) flag = 1;
else if (f->vector_flag && pvindex > 0) flag = 1; else if (f->vector_flag && pvindex > 0) flag = 1;
if (!flag) error->all(FLERR,"Fix controller fix does not " if (!flag) error->all(FLERR,"Fix controller fix does not calculate a global scalar or vector");
"calculate a global scalar or vector");
if (pvindex && pvindex > f->size_vector) if (pvindex && pvindex > f->size_vector)
error->all(FLERR,"Fix controller fix vector is accessed out-of-range"); error->all(FLERR,"Fix controller fix vector is accessed out-of-range");
} else if (pvwhich == ArgInfo::VARIABLE) { } else if (pvwhich == ArgInfo::VARIABLE) {
@ -135,25 +129,20 @@ int FixController::setmask()
void FixController::init() void FixController::init()
{ {
if (pvwhich == ArgInfo::COMPUTE) { if (pvwhich == ArgInfo::COMPUTE) {
int icompute = modify->find_compute(pvID); pcompute = modify->get_compute_by_id(pvID);
if (icompute < 0) if (!pcompute) error->all(FLERR,"Compute ID {} for fix controller does not exist", pvID);
error->all(FLERR,"Compute ID for fix controller does not exist");
pcompute = modify->compute[icompute];
} else if (pvwhich == ArgInfo::FIX) { } else if (pvwhich == ArgInfo::FIX) {
int ifix = modify->find_fix(pvID); pfix = modify->get_fix_by_id(pvID);
if (ifix < 0) error->all(FLERR,"Fix ID for fix controller does not exist"); if (!pfix) error->all(FLERR,"Fix ID {} for fix controller does not exist", pvID);
pfix = modify->fix[ifix];
} else if (pvwhich == ArgInfo::VARIABLE) { } else if (pvwhich == ArgInfo::VARIABLE) {
pvar = input->variable->find(pvID); pvar = input->variable->find(pvID);
if (pvar < 0) if (pvar < 0) error->all(FLERR,"Variable name for fix controller does not exist");
error->all(FLERR,"Variable name for fix controller does not exist");
} }
cvar = input->variable->find(cvID); cvar = input->variable->find(cvID);
if (cvar < 0) if (cvar < 0) error->all(FLERR,"Variable name for fix controller does not exist");
error->all(FLERR,"Variable name for fix controller does not exist");
// set sampling time // set sampling time

View File

@ -1294,16 +1294,13 @@ void FixChargeRegulation::restart(char *buf)
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
void FixChargeRegulation::setThermoTemperaturePointer() { void FixChargeRegulation::setThermoTemperaturePointer() {
int ifix = -1; Fix *ifix = modify->get_fix_by_id(idftemp);
ifix = modify->find_fix(idftemp); if (!ifix)
if (ifix == -1) { error->all(FLERR, "fix charge/regulation could not find thermostat fix id {}", idftemp);
error->all(FLERR, "fix charge/regulation regulation could not find "
"a temperature fix id provided by tempfixid\n");
}
Fix *temperature_fix = modify->fix[ifix];
int dim;
target_temperature_tcp = (double *) temperature_fix->extract("t_target", dim);
int dim;
target_temperature_tcp = (double *) ifix->extract("t_target", dim);
if (!target_temperature_tcp) error->all(FLERR, "Fix id {} does not control temperature", idftemp);
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

@ -565,9 +565,8 @@ void FixGCMC::init()
fixrigid = nullptr; fixrigid = nullptr;
if (rigidflag) { if (rigidflag) {
int ifix = modify->find_fix(idrigid); fixrigid = modify->get_fix_by_id(idrigid);
if (ifix < 0) error->all(FLERR,"Fix gcmc rigid fix does not exist"); if (!fixrigid) error->all(FLERR,"Fix gcmc rigid fix ID {} does not exist", idrigid);
fixrigid = modify->fix[ifix];
int tmp; int tmp;
if (&onemols[imol] != (Molecule **) fixrigid->extract("onemol",tmp)) if (&onemols[imol] != (Molecule **) fixrigid->extract("onemol",tmp))
error->all(FLERR, "Fix gcmc and fix rigid/small not using same molecule template ID"); error->all(FLERR, "Fix gcmc and fix rigid/small not using same molecule template ID");
@ -578,9 +577,8 @@ void FixGCMC::init()
fixshake = nullptr; fixshake = nullptr;
if (shakeflag) { if (shakeflag) {
int ifix = modify->find_fix(idshake); fixshake = modify->get_fix_by_id(idshake);
if (ifix < 0) error->all(FLERR,"Fix gcmc shake fix does not exist"); if (!fixshake) error->all(FLERR,"Fix gcmc shake fix ID {} does not exist", idshake);
fixshake = modify->fix[ifix];
int tmp; int tmp;
if (&onemols[imol] != (Molecule **) fixshake->extract("onemol",tmp)) if (&onemols[imol] != (Molecule **) fixshake->extract("onemol",tmp))
error->all(FLERR,"Fix gcmc and fix shake not using same molecule template ID"); error->all(FLERR,"Fix gcmc and fix shake not using same molecule template ID");

View File

@ -1,4 +1,3 @@
// clang-format off
/* ------------------------------------------------------------------------- /* -------------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories https://www.lammps.org/, Sandia National Laboratories
@ -17,42 +16,37 @@
OpenMP based threading support for LAMMPS OpenMP based threading support for LAMMPS
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include <cstring>
#include "atom.h"
#include "comm.h"
#include "error.h"
#include "force.h"
#include "modify.h"
#include "neighbor.h"
#include "thr_omp.h" #include "thr_omp.h"
#include "pair.h"
#include "bond.h"
#include "angle.h" #include "angle.h"
#include "dihedral.h" #include "atom.h"
#include "improper.h" #include "bond.h"
#include "comm.h"
#include "compute.h" #include "compute.h"
#include "dihedral.h"
#include "error.h"
#include "force.h"
#include "improper.h"
#include "math_const.h" #include "math_const.h"
#include "modify.h"
#include "neighbor.h"
#include "pair.h"
#include <cstring>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using namespace MathConst; using MathConst::THIRD;
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
ThrOMP::ThrOMP(LAMMPS *ptr, int style) ThrOMP::ThrOMP(LAMMPS *ptr, int style) : lmp(ptr), fix(nullptr), thr_style(style), thr_error(0)
: lmp(ptr), fix(nullptr), thr_style(style), thr_error(0)
{ {
// register fix omp with this class // register fix omp with this class
int ifix = lmp->modify->find_fix("package_omp"); fix = static_cast<FixOMP *>(lmp->modify->get_fix_by_id("package_omp"));
if (ifix < 0) if (!fix) lmp->error->all(FLERR, "The 'package omp' command is required for /omp styles");
lmp->error->all(FLERR,"The 'package omp' command is required for /omp styles");
fix = static_cast<FixOMP *>(lmp->modify->fix[ifix]);
} }
// clang-format off
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------
Hook up per thread per atom arrays into the tally infrastructure Hook up per thread per atom arrays into the tally infrastructure
---------------------------------------------------------------------- */ ---------------------------------------------------------------------- */
@ -154,7 +148,6 @@ void ThrOMP::ev_setup_thr(int eflag, int vflag, int nall, double *eatom,
memset(&(thr->cvatom_imprp[0][0]),0,nall*9*sizeof(double)); memset(&(thr->cvatom_imprp[0][0]),0,nall*9*sizeof(double));
} }
} }
// nothing to do for THR_KSPACE // nothing to do for THR_KSPACE
} }
@ -1312,7 +1305,6 @@ void ThrOMP::ev_tally_thr(Dihedral * const dihed, const int i1, const int i2,
if (i4 < nlocal) v_tally9(thr->cvatom_dihed[i4],v4); if (i4 < nlocal) v_tally9(thr->cvatom_dihed[i4],v4);
} }
} }
} }
/* ---------------------------------------------------------------------- /* ----------------------------------------------------------------------