From 6873ebb17a9371f215bf75da47e60ceca68aea3d Mon Sep 17 00:00:00 2001 From: Fernando Posada Date: Tue, 9 Aug 2022 14:01:49 -0400 Subject: [PATCH] LAMMPS code clinic 2022 project 2: Replacing find_compute with get_compute_by_id where possible. --- src/compute_angmom_chunk.cpp | 7 ++----- src/compute_chunk_spread_atom.cpp | 28 +++++++++++++--------------- src/fix_nh.cpp | 22 +++++++++------------- 3 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/compute_angmom_chunk.cpp b/src/compute_angmom_chunk.cpp index 226e0cd646..a44e577de0 100644 --- a/src/compute_angmom_chunk.cpp +++ b/src/compute_angmom_chunk.cpp @@ -185,11 +185,8 @@ void ComputeAngmomChunk::lock_enable() void ComputeAngmomChunk::lock_disable() { - int icompute = modify->find_compute(idchunk); - if (icompute >= 0) { - cchunk = dynamic_cast(modify->compute[icompute]); - cchunk->lockcount--; - } + cchunk = dynamic_cast(modify->get_compute_by_id(idchunk)); + if(cchunk) cchunk->lockcount--; } /* ---------------------------------------------------------------------- diff --git a/src/compute_chunk_spread_atom.cpp b/src/compute_chunk_spread_atom.cpp index 3781e83aa7..a68174e058 100644 --- a/src/compute_chunk_spread_atom.cpp +++ b/src/compute_chunk_spread_atom.cpp @@ -89,21 +89,21 @@ ComputeChunkSpreadAtom(LAMMPS *lmp, int narg, char **arg) : 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 compute chunk/spread/atom does not exist"); + auto icompute = modify->get_compute_by_id(ids[i]); + if (!icompute) + error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", ids[i]); - if (!utils::strmatch(modify->compute[icompute]->style,"/chunk$")) + if (!utils::strmatch(icompute->style,"/chunk$")) error->all(FLERR,"Compute for compute chunk/spread/atom " "does not calculate per-chunk values"); if (argindex[i] == 0) { - if (!modify->compute[icompute]->vector_flag) + if (!icompute->vector_flag) error->all(FLERR,"Compute chunk/spread/atom compute does not calculate global vector"); } else { - if (!modify->compute[icompute]->array_flag) + if (!icompute->array_flag) error->all(FLERR,"Compute chunk/spread/atom compute does not calculate global array"); - if (argindex[i] > modify->compute[icompute]->size_array_cols) + if (argindex[i] > icompute->size_array_cols) error->all(FLERR,"Compute chunk/spread/atom compute array is accessed out-of-range"); } @@ -164,14 +164,13 @@ void ComputeChunkSpreadAtom::init() if (which[m] == ArgInfo::COMPUTE) { int icompute = modify->find_compute(ids[m]); if (icompute < 0) - error->all(FLERR,"Compute ID for compute chunk/spread/atom " - "does not exist"); + error->all(FLERR,"Compute ID {} for compute chunk/spread/atom does not exist", ids[m]); value2index[m] = icompute; } else if (which[m] == ArgInfo::FIX) { int ifix = modify->find_fix(ids[m]); if (ifix < 0) - error->all(FLERR,"Fix ID for compute chunk/spread/atom does not exist"); + error->all(FLERR,"Fix ID {} for compute chunk/spread/atom does not exist", ids[m]); value2index[m] = ifix; } } @@ -181,12 +180,11 @@ void ComputeChunkSpreadAtom::init() void ComputeChunkSpreadAtom::init_chunk() { - int icompute = modify->find_compute(idchunk); - if (icompute < 0) - error->all(FLERR,"Chunk/atom compute does not exist for compute chunk/spread/atom"); - cchunk = dynamic_cast( modify->compute[icompute]); + cchunk = dynamic_cast( modify->get_compute_by_id(idchunk)); + if (!cchunk) + error->all(FLERR,"Chunk/atom compute does not exist for compute chunk/spread/atom {}", idchunk); if (strcmp(cchunk->style,"chunk/atom") != 0) - error->all(FLERR,"Compute chunk/spread/atom does not use chunk/atom compute"); + error->all(FLERR,"Compute chunk/spread/atom {} does not use chunk/atom compute", idchunk); } /* ---------------------------------------------------------------------- */ diff --git a/src/fix_nh.cpp b/src/fix_nh.cpp index d62b13d858..ecabfde86b 100644 --- a/src/fix_nh.cpp +++ b/src/fix_nh.cpp @@ -658,19 +658,17 @@ void FixNH::init() // set temperature and pressure ptrs - auto icompute = modify->get_compute_by_id(id_temp); - if (!icompute) + temperature = modify->get_compute_by_id(id_temp); + if (!temperature) error->all(FLERR,"Temperature ID {} for fix nvt/npt does not exist", id_temp); - temperature = icompute; if (temperature->tempbias) which = BIAS; else which = NOBIAS; if (pstat_flag) { - icompute = modify->get_compute_by_id(id_press); - if (!icompute) + pressure = modify->get_compute_by_id(id_press); + if (!pressure) error->all(FLERR,"Pressure ID {} for fix npt/nph does not exist", id_press); - pressure = icompute; } // set timesteps and frequencies @@ -1411,10 +1409,9 @@ int FixNH::modify_param(int narg, char **arg) delete [] id_temp; id_temp = utils::strdup(arg[1]); - auto icompute = modify->get_compute_by_id(arg[1]); - if (!icompute) + temperature = modify->get_compute_by_id(arg[1]); + if (!temperature) error->all(FLERR,"Could not find fix_modify temperature ID {}", arg[1]); - temperature = icompute; if (temperature->tempflag == 0) error->all(FLERR, @@ -1425,7 +1422,7 @@ int FixNH::modify_param(int narg, char **arg) // reset id_temp of pressure to new temperature ID if (pstat_flag) { - icompute = modify->get_compute_by_id(id_press); + auto icompute = modify->get_compute_by_id(id_press); if (!icompute) error->all(FLERR,"Pressure ID {} for fix modify does not exist", id_press); icompute->reset_extra_compute_fix(id_temp); @@ -1443,9 +1440,8 @@ int FixNH::modify_param(int narg, char **arg) delete [] id_press; id_press = utils::strdup(arg[1]); - auto icompute = modify->get_compute_by_id(arg[1]); - if (!icompute) error->all(FLERR,"Could not find fix_modify pressure ID {}", arg[1]); - pressure = icompute; + pressure = modify->get_compute_by_id(arg[1]); + if (!pressure) error->all(FLERR,"Could not find fix_modify pressure ID {}", arg[1]); if (pressure->pressflag == 0) error->all(FLERR,"Fix_modify pressure ID does not compute pressure");