diff --git a/doc/src/Developer_utils.rst b/doc/src/Developer_utils.rst index e1a5f7741d..622988908a 100644 --- a/doc/src/Developer_utils.rst +++ b/doc/src/Developer_utils.rst @@ -205,6 +205,9 @@ Argument processing .. doxygenfunction:: expand_args :project: progguide +.. doxygenfunction:: gridid_parse + :project: progguide + Convenience functions ^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/compute.h b/src/compute.h index fa48d125f8..d1c6cdb77a 100644 --- a/src/compute.h +++ b/src/compute.h @@ -132,9 +132,9 @@ class Compute : protected Pointers { virtual void reset_grid(){}; - virtual int get_grid_by_name(char *, int &) { return -1; }; + virtual int get_grid_by_name(const std::string &, int &) { return -1; }; virtual void *get_grid_by_index(int) { return nullptr; }; - virtual int get_griddata_by_name(int, char *, int &) { return -1; }; + virtual int get_griddata_by_name(int, const std::string &, int &) { return -1; }; virtual void *get_griddata_by_index(int) { return nullptr; }; virtual void dof_remove_pre() {} diff --git a/src/compute_property_grid.cpp b/src/compute_property_grid.cpp index a028517d71..510c2e134e 100644 --- a/src/compute_property_grid.cpp +++ b/src/compute_property_grid.cpp @@ -32,9 +32,8 @@ enum { UNSCALED, SCALED }; /* ---------------------------------------------------------------------- */ ComputePropertyGrid::ComputePropertyGrid(LAMMPS *lmp, int narg, char **arg) : - Compute(lmp, narg, arg), pack_choice(nullptr), - grid2d(nullptr), grid3d(nullptr), - vec2d(nullptr), array2d(nullptr), vec3d(nullptr), array3d(nullptr) + Compute(lmp, narg, arg), grid2d(nullptr), grid3d(nullptr), vec2d(nullptr), vec3d(nullptr), + array2d(nullptr), array3d(nullptr), pack_choice(nullptr) { if (narg < 7) error->all(FLERR, "Illegal compute property/grid command"); @@ -164,9 +163,9 @@ void ComputePropertyGrid::reset_grid() return -1 if grid name not found ------------------------------------------------------------------------- */ -int ComputePropertyGrid::get_grid_by_name(char *name, int &dim) +int ComputePropertyGrid::get_grid_by_name(const std::string &name, int &dim) { - if (strcmp(name,"grid") == 0) { + if (name == "grid") { dim = dimension; return 0; } @@ -200,9 +199,9 @@ void *ComputePropertyGrid::get_grid_by_index(int index) return -1 if data name not found ------------------------------------------------------------------------- */ -int ComputePropertyGrid::get_griddata_by_name(int igrid, char *name, int &ncol) +int ComputePropertyGrid::get_griddata_by_name(int igrid, const std::string &name, int &ncol) { - if (igrid == 0 && strcmp(name,"data") == 0) { + if ((igrid == 0) && (name == "data")) { if (nvalues == 1) ncol = 0; else ncol = nvalues; return 0; diff --git a/src/compute_property_grid.h b/src/compute_property_grid.h index 929346ad7a..9083293636 100644 --- a/src/compute_property_grid.h +++ b/src/compute_property_grid.h @@ -33,9 +33,9 @@ class ComputePropertyGrid : public Compute { void reset_grid() override; - int get_grid_by_name(char *, int &) override; + int get_grid_by_name(const std::string &, int &) override; void *get_grid_by_index(int) override; - int get_griddata_by_name(int, char *, int &) override; + int get_griddata_by_name(int, const std::string &, int &) override; void *get_griddata_by_index(int) override; double memory_usage() override; diff --git a/src/dump_grid.cpp b/src/dump_grid.cpp index e0fbb95990..de61e281ab 100644 --- a/src/dump_grid.cpp +++ b/src/dump_grid.cpp @@ -44,8 +44,7 @@ DumpGrid::DumpGrid(LAMMPS *lmp, int narg, char **arg) : Dump(lmp, narg, arg), idregion(nullptr), earg(nullptr), vtype(nullptr), vformat(nullptr), columns(nullptr), columns_default(nullptr), field2index(nullptr), field2grid(nullptr), field2data(nullptr), - argindex(nullptr), id_compute(nullptr), compute(nullptr), - id_fix(nullptr), fix(nullptr), pack_choice(nullptr) + argindex(nullptr), id_compute(nullptr), id_fix(nullptr), pack_choice(nullptr) { if (narg == 5) error->all(FLERR,"No dump grid arguments specified"); @@ -160,11 +159,9 @@ DumpGrid::~DumpGrid() for (int i = 0; i < ncompute; i++) delete[] id_compute[i]; memory->sfree(id_compute); - delete[] compute; for (int i = 0; i < nfix; i++) delete[] id_fix[i]; memory->sfree(id_fix); - delete[] fix; if (vformat) { for (int i = 0; i < nfield; i++) delete[] vformat[i]; @@ -668,12 +665,9 @@ int DumpGrid::parse_fields(int narg, char **arg) for (int iarg = 0; iarg < narg; iarg++) { - int n,flag,cols; ArgInfo argi(arg[iarg], ArgInfo::COMPUTE | ArgInfo::FIX); argindex[iarg] = argi.get_index1(); auto name = argi.get_name(); - Compute *icompute = nullptr; - Fix *ifix = nullptr; switch (argi.get_type()) { @@ -691,50 +685,38 @@ int DumpGrid::parse_fields(int narg, char **arg) field2source[iarg] = COMPUTE; // split name = idcompute:gname:dname into 3 strings + + auto words = utils::gridid_parse(FLERR,name,error); + const auto &idcompute = words[0]; + const auto &gname = words[1]; + const auto &dname = words[2]; - char *idcompute,*gname,*dname; - utils::grid_parse(FLERR,name,idcompute,gname,dname,error); - - icompute = modify->get_compute_by_id(idcompute); - if (!icompute) - error->all(FLERR,"Could not find dump grid compute ID: {}",idcompute); + auto icompute = modify->get_compute_by_id(idcompute); + if (!icompute) error->all(FLERR,"Could not find dump grid compute ID: {}",idcompute); if (icompute->pergrid_flag == 0) - error->all(FLERR,"Dump grid compute {} does not compute per-grid info", - idcompute); + error->all(FLERR,"Dump grid compute {} does not compute per-grid info",idcompute); int dim; int igrid = icompute->get_grid_by_name(gname,dim); if (igrid < 0) - error->all(FLERR,"Dump grid compute {} does not recognize grid name {}", - idcompute,gname); + error->all(FLERR,"Dump grid compute {} does not recognize grid name {}",idcompute,gname); int ncol; int idata = icompute->get_griddata_by_name(igrid,dname,ncol); if (idata < 0) - error->all(FLERR, - "Dump grid compute {} does not recognize data name {}", - idcompute,dname); + error->all(FLERR,"Dump grid compute {} does not recognize data name {}",idcompute,dname); if (argi.get_dim() == 0 && ncol) - error->all(FLERR,"Dump grid compute {} data {} is not per-grid vector", - idcompute,dname); + error->all(FLERR,"Dump grid compute {} data {} is not per-grid vector",idcompute,dname); if (argi.get_dim() && ncol == 0) - error->all(FLERR,"Dump grid compute {} data {} is not per-grid array", - idcompute,dname); + error->all(FLERR,"Dump grid compute {} data {} is not per-grid array",idcompute,dname); if (argi.get_dim() && argi.get_index1() > ncol) - error->all(FLERR, - "Dump grid compute {} array {} is accessed out-of-range", - idcompute,dname); + error->all(FLERR,"Dump grid compute {} array {} is accessed out-of-range",idcompute,dname); - - field2index[iarg] = add_compute(idcompute); + field2index[iarg] = add_compute(idcompute,icompute); field2grid[iarg] = igrid; field2data[iarg] = idata; - delete [] idcompute; - delete [] gname; - delete [] dname; - } break; // fix value = f_ID @@ -748,47 +730,39 @@ int DumpGrid::parse_fields(int narg, char **arg) // split name = idfix:gname:dname into 3 strings - char *idfix,*gname,*dname; - utils::grid_parse(FLERR,name,idfix,gname,dname,error); + auto words = utils::gridid_parse(FLERR,name,error); + const auto &idfix = words[0]; + const auto &gname = words[1]; + const auto &dname = words[2]; - ifix = modify->get_fix_by_id(idfix); + auto ifix = modify->get_fix_by_id(idfix); if (!ifix) error->all(FLERR,"Could not find dump grid fix ID: {}",idfix); if (ifix->pergrid_flag == 0) - error->all(FLERR,"Dump grid fix {} does not compute per-grid info", - idfix); + error->all(FLERR,"Dump grid fix {} does not compute per-grid info",idfix); if (update->ntimestep % ifix->pergrid_freq) - error->all(FLERR,"Fix for dump grid not computed at compatible time"); + error->all(FLERR,"Fix ID {} for dump grid not computed at compatible time",idfix); int dim; int igrid = ifix->get_grid_by_name(gname,dim); if (igrid < 0) - error->all(FLERR,"Dump grid fix {} does not recognize grid name {}", - idfix,gname); + error->all(FLERR,"Dump grid fix {} does not recognize grid name {}",idfix,gname); int ncol; int idata = ifix->get_griddata_by_name(igrid,dname,ncol); if (idata < 0) - error->all(FLERR,"Dump grid fix {} does not recognize data name {}", - idfix,dname); + error->all(FLERR,"Dump grid fix {} does not recognize data name {}",idfix,dname); if (argi.get_dim() == 0 && ncol) - error->all(FLERR,"Dump grid fix {} data {} is not per-grid vector", - idfix,dname); + error->all(FLERR,"Dump grid fix {} data {} is not per-grid vector",idfix,dname); if (argi.get_dim() > 0 && ncol == 0) - error->all(FLERR,"Dump grid fix {} data {} is not per-grid array", - idfix,dname); + error->all(FLERR,"Dump grid fix {} data {} is not per-grid array",idfix,dname); if (argi.get_dim() > 0 && argi.get_index1() > ncol) - error->all(FLERR,"Dump grid fix {} array {} is accessed out-of-range", - idfix,dname); + error->all(FLERR,"Dump grid fix {} array {} is accessed out-of-range",idfix,dname); - field2index[iarg] = add_fix(idfix); + field2index[iarg] = add_fix(idfix,ifix); field2grid[iarg] = igrid; field2data[iarg] = idata; - delete [] idfix; - delete [] gname; - delete [] dname; - } break; // no match @@ -808,19 +782,17 @@ int DumpGrid::parse_fields(int narg, char **arg) if already in list, do not add, just return index, else add to list ------------------------------------------------------------------------- */ -int DumpGrid::add_compute(const char *id) +int DumpGrid::add_compute(const std::string &id, Compute *cptr) { int icompute; for (icompute = 0; icompute < ncompute; icompute++) - if (strcmp(id,id_compute[icompute]) == 0) break; + if (id == id_compute[icompute]) break; if (icompute < ncompute) return icompute; - id_compute = (char **) - memory->srealloc(id_compute,(ncompute+1)*sizeof(char *),"dump:id_compute"); - delete[] compute; - compute = new Compute*[ncompute+1]; - + id_compute = (char **) memory->srealloc(id_compute,(ncompute+1)*sizeof(char *),"dump:id_compute"); id_compute[ncompute] = utils::strdup(id); + compute.push_back(cptr); + ncompute++; return ncompute-1; } @@ -831,19 +803,17 @@ int DumpGrid::add_compute(const char *id) if already in list, do not add, just return index, else add to list ------------------------------------------------------------------------- */ -int DumpGrid::add_fix(const char *id) +int DumpGrid::add_fix(const std::string &id, Fix *fptr) { int ifix; for (ifix = 0; ifix < nfix; ifix++) - if (strcmp(id,id_fix[ifix]) == 0) break; + if (id == id_fix[ifix]) break; if (ifix < nfix) return ifix; - id_fix = (char **) - memory->srealloc(id_fix,(nfix+1)*sizeof(char *),"dump:id_fix"); - delete[] fix; - fix = new Fix*[nfix+1]; - + id_fix = (char **) memory->srealloc(id_fix,(nfix+1)*sizeof(char *),"dump:id_fix"); id_fix[nfix] = utils::strdup(id); + fix.push_back(fptr); + nfix++; return nfix-1; } diff --git a/src/dump_grid.h b/src/dump_grid.h index edf21688b9..ac3d13b3c9 100644 --- a/src/dump_grid.h +++ b/src/dump_grid.h @@ -49,30 +49,30 @@ class DumpGrid : public Dump { int dimension; - int nxgrid,nygrid,nzgrid; // global grid size + int nxgrid, nygrid, nzgrid; // global grid size - int nfield; // # of keywords listed by user - int ioptional; // index of start of optional args + int nfield; // # of keywords listed by user + int ioptional; // index of start of optional args - // per field info - int *field2index; // which compute/fix - int *field2source; // COMPUTE or FIX - int *field2grid; // index of grid within compute/fix - int *field2data; // index of data within compute/fix - int *argindex; // index into compute,fix,custom per-atom data - // 0 for per-atom vector, 1-N for cols of per-atom array + // per field info + int *field2index; // which compute/fix + int *field2source; // COMPUTE or FIX + int *field2grid; // index of grid within compute/fix + int *field2data; // index of data within compute/fix + int *argindex; // index into compute,fix,custom per-atom data + // 0 for per-atom vector, 1-N for cols of per-atom array - int ncompute; // # of Computes accessed by dump - char **id_compute; // their IDs - class Compute **compute; // list of ptrs to the Computes + int ncompute; // # of Computes accessed by dump + char **id_compute; // their IDs + std::vector compute; // list of ptrs to the Computes - int nfix; // # of Fixes used by dump - char **id_fix; // their IDs - class Fix **fix; // list of ptrs to the Fixes + int nfix; // # of Fixes used by dump + char **id_fix; // their IDs + std::vector fix; // list of ptrs to the Fixes - int nxlo_in,nxhi_in; // bounds of this proc's portion of grids - int nylo_in,nyhi_in; - int nzlo_in,nzhi_in; + int nxlo_in, nxhi_in; // bounds of this proc's portion of grids + int nylo_in, nyhi_in; + int nzlo_in, nzhi_in; // private methods @@ -85,8 +85,8 @@ class DumpGrid : public Dump { double memory_usage() override; int parse_fields(int, char **); - int add_compute(const char *); - int add_fix(const char *); + int add_compute(const std::string &, class Compute *); + int add_fix(const std::string &, class Fix *); int modify_param(int, char **) override; void header_format_binary(); diff --git a/src/fix.h b/src/fix.h index 8171f2ee31..7312a52d0e 100644 --- a/src/fix.h +++ b/src/fix.h @@ -220,9 +220,9 @@ class Fix : protected Pointers { virtual void pack_gather_grid(int, void *){}; virtual void unpack_gather_grid(int, void *, void *, int, int, int, int, int, int){}; - virtual int get_grid_by_name(char *, int &) { return -1; }; + virtual int get_grid_by_name(const std::string &, int &) { return -1; }; virtual void *get_grid_by_index(int) { return nullptr; }; - virtual int get_griddata_by_name(int, char *, int &) { return -1; }; + virtual int get_griddata_by_name(int, const std::string &, int &) { return -1; }; virtual void *get_griddata_by_index(int) { return nullptr; }; virtual double compute_scalar() { return 0.0; } diff --git a/src/fix_ave_grid.cpp b/src/fix_ave_grid.cpp index 72c2b8dcb6..6d04a766e1 100644 --- a/src/fix_ave_grid.cpp +++ b/src/fix_ave_grid.cpp @@ -53,7 +53,7 @@ FixAveGrid::FixAveGrid(LAMMPS *lmp, int narg, char **arg) : value2index(nullptr), value2grid(nullptr), value2data(nullptr), grid2d(nullptr), grid3d(nullptr), grid_buf1(nullptr), grid_buf2(nullptr), - vec2d(nullptr), array2d(nullptr), vec3d(nullptr), array3d(nullptr), + vec2d(nullptr), vec3d(nullptr), array2d(nullptr), array3d(nullptr), count2d(nullptr), count3d(nullptr) { if (narg < 10) error->all(FLERR,"Illegal fix ave/grid command"); @@ -232,7 +232,7 @@ FixAveGrid::FixAveGrid(LAMMPS *lmp, int narg, char **arg) : // if wildcard expansion occurred, free earg memory from exapnd_args() if (expand) { - for (int i = 0; i < nvalues; i++) delete [] earg[i]; + for (int i = 0; i < nvalues; i++) delete[] earg[i]; memory->sfree(earg); } @@ -322,106 +322,78 @@ FixAveGrid::FixAveGrid(LAMMPS *lmp, int narg, char **arg) : for (int i = 0; i < nvalues; i++) { if (which[i] == ArgInfo::COMPUTE) { - char *idcompute,*gname,*dname; - utils::grid_parse(FLERR,ids[i],idcompute,gname,dname,error); - delete [] ids[i]; - ids[i] = new char[strlen(idcompute)+1]; - strcpy(ids[i],idcompute); + auto words = utils::gridid_parse(FLERR,ids[i],error); + const auto &idcompute = words[0]; + const auto &gname = words[1]; + const auto &dname = words[2]; - Compute *icompute = modify->get_compute_by_id(idcompute); - if (!icompute) - error->all(FLERR,"Could not find fix ave/grid compute ID: {}", - idcompute); + delete[] ids[i]; + ids[i] = utils::strdup(idcompute); + + auto icompute = modify->get_compute_by_id(idcompute); + if (!icompute) error->all(FLERR,"Could not find fix ave/grid compute ID: {}",idcompute); if (icompute->pergrid_flag == 0) - error->all(FLERR, - "Fix ave/grid compute {} does not compute per-grid info", - idcompute); + error->all(FLERR,"Fix ave/grid compute {} does not compute per-grid info",idcompute); int dim; int igrid = icompute->get_grid_by_name(gname,dim); if (igrid < 0) - error->all(FLERR, - "Fix ave/grid compute {} does not recognize grid name {}", + error->all(FLERR,"Fix ave/grid compute {} does not recognize grid name {}", idcompute,gname); int ncol; int idata = icompute->get_griddata_by_name(igrid,dname,ncol); if (idata < 0) - error->all(FLERR, - "Fix ave/grid compute {} does not recognize data name {}", + error->all(FLERR,"Fix ave/grid compute {} does not recognize data name {}", idcompute,dname); if (argindex[i] == 0 && ncol) - error->all(FLERR, - "Fix ave/grid compute {} data {} is not per-grid vector", - idcompute,dname); + error->all(FLERR,"Fix ave/grid compute {} data {} is not per-grid vector",idcompute,dname); if (argindex[i] && ncol == 0) - error->all(FLERR, - "Fix ave/grid compute {} data {} is not per-grid array", - idcompute,dname); + error->all(FLERR,"Fix ave/grid compute {} data {} is not per-grid array",idcompute,dname); if (argindex[i] && argindex[i] > ncol) - error->all(FLERR, - "Fix ave/grid compute {} array {} is accessed out-of-range", + error->all(FLERR,"Fix ave/grid compute {} array {} is accessed out-of-range", idcompute,dname); value2grid[i] = igrid; value2data[i] = idata; - delete [] idcompute; - delete [] gname; - delete [] dname; - } else if (which[i] == ArgInfo::FIX) { - char *idfix,*gname,*dname; - utils::grid_parse(FLERR,ids[i],idfix,gname,dname,error); - delete [] ids[i]; - ids[i] = new char[strlen(idfix)+1]; - strcpy(ids[i],idfix); + auto words = utils::gridid_parse(FLERR,ids[i],error); + const auto &idfix = words[0]; + const auto &gname = words[1]; + const auto &dname = words[2]; + + delete[] ids[i]; + ids[i] = utils::strdup(idfix); Fix *ifix = modify->get_fix_by_id(idfix); - if (!ifix) error->all(FLERR,"Could not find fix ave/grid fix ID: {}", - idfix); + if (!ifix) error->all(FLERR,"Could not find fix ave/grid fix ID: {}",idfix); if (ifix->pergrid_flag == 0) - error->all(FLERR,"Fix ave/grid fix {} does not compute per-grid info", - idfix); + error->all(FLERR,"Fix ave/grid fix {} does not compute per-grid info",idfix); if (nevery % ifix->pergrid_freq) - error->all(FLERR, - "Fix for fix grid/atom not computed at compatible time"); + error->all(FLERR, "Fix ID {} for fix grid/atom not computed at compatible time",idfix); int dim; int igrid = ifix->get_grid_by_name(gname,dim); if (igrid < 0) - error->all(FLERR, - "Fix ave/grid compute {} does not recognize grid name {}", - idfix,gname); + error->all(FLERR,"Fix ave/grid fix {} does not recognize grid name {}",idfix,gname); int ncol; int idata = ifix->get_griddata_by_name(igrid,dname,ncol); if (idata < 0) - error->all(FLERR, - "Fix ave/grid compute {} does not recognize data name {}", - idfix,dname); + error->all(FLERR,"Fix ave/grid fix {} does not recognize data name {}",idfix,dname); if (argindex[i] == 0 && ncol) - error->all(FLERR, - "Fix ave/grid compute {} data {} is not per-grid vector", - idfix,dname); + error->all(FLERR, "Fix ave/grid fix {} data {} is not per-grid vector",idfix,dname); if (argindex[i] && ncol == 0) - error->all(FLERR, - "Fix ave/grid compute {} data {} is not per-grid array", - idfix,dname); + error->all(FLERR,"Fix ave/grid fix {} data {} is not per-grid array",idfix,dname); if (argindex[i] && argindex[i] > ncol) - error->all(FLERR, - "Fix ave/grid compute {} array {} is accessed out-of-range", - idfix,dname); + error->all(FLERR,"Fix ave/grid fix {} array {} is accessed out-of-range",idfix,dname); value2grid[i] = igrid; value2data[i] = idata; - - delete [] idfix; - delete [] gname; - delete [] dname; } } } @@ -516,13 +488,13 @@ FixAveGrid::FixAveGrid(LAMMPS *lmp, int narg, char **arg) : FixAveGrid::~FixAveGrid() { - delete [] which; - delete [] argindex; - for (int m = 0; m < nvalues; m++) delete [] ids[m]; - delete [] ids; - delete [] value2index; - delete [] value2grid; - delete [] value2data; + delete[] which; + delete[] argindex; + for (int m = 0; m < nvalues; m++) delete[] ids[m]; + delete[] ids; + delete[] value2index; + delete[] value2grid; + delete[] value2data; delete grid2d; delete grid3d; @@ -715,7 +687,7 @@ void FixAveGrid::end_of_step() double mv2d = force->mv2d; double boltz = force->boltz; - double count,invcount,norm; + double count,norm; double repeat = nrepeat; double invrepeat = 1.0/nrepeat; @@ -751,7 +723,6 @@ void FixAveGrid::end_of_step() for (ix = nxlo_in; ix <= nxhi_in; ix++) { count = count2d[iy][ix]; if (count) { - invcount = 1.0/count; for (m = 0; m <= nvalues; m++) { if (which[m] == ArgInfo::DENSITY_NUMBER) norm = 1.0 / (binvol * repeat); @@ -792,7 +763,6 @@ void FixAveGrid::end_of_step() for (ix = nxlo_in; ix <= nxhi_in; ix++) { count = count3d[iz][iy][ix]; if (count) { - invcount = 1.0/count; for (m = 0; m <= nvalues; m++) { if (which[m] == ArgInfo::DENSITY_NUMBER) norm = 1.0 / (binvol * repeat); @@ -850,7 +820,7 @@ void FixAveGrid::end_of_step() void FixAveGrid::atom2grid() { - int i,j,k,m,n,ix,iy,iz; + int i,j,m,n,ix,iy,iz; // bin[i][dim] = indices of bin each atom is in // not set if group mask does not match @@ -1424,9 +1394,9 @@ void FixAveGrid::reset_grid() return -1 if grid name not found ------------------------------------------------------------------------- */ -int FixAveGrid::get_grid_by_name(char *name, int &dim) +int FixAveGrid::get_grid_by_name(const std::string &name, int &dim) { - if (strcmp(name,"grid") == 0) { + if (name == "grid") { dim = dimension; return 0; } @@ -1460,9 +1430,9 @@ void *FixAveGrid::get_grid_by_index(int index) return -1 if data name not found ------------------------------------------------------------------------- */ -int FixAveGrid::get_griddata_by_name(int igrid, char *name, int &ncol) +int FixAveGrid::get_griddata_by_name(int igrid, const std::string &name, int &ncol) { - if (igrid == 0 && strcmp(name,"data") == 0) { + if ((igrid == 0) && (name == "data")) { if (nvalues == 1) ncol = 0; else ncol = nvalues; return 0; @@ -1470,7 +1440,7 @@ int FixAveGrid::get_griddata_by_name(int igrid, char *name, int &ncol) // count is only produced for ATOM mode - if (modeatom && igrid == 0 && strcmp(name,"count") == 0) { + if (modeatom && (igrid == 0) && (name == "count")) { ncol = 0; return 1; } diff --git a/src/fix_ave_grid.h b/src/fix_ave_grid.h index 5216b7e503..9445884b41 100644 --- a/src/fix_ave_grid.h +++ b/src/fix_ave_grid.h @@ -38,9 +38,9 @@ class FixAveGrid : public Fix { void reset_grid() override; - int get_grid_by_name(char *, int &) override; + int get_grid_by_name(const std::string &, int &) override; void *get_grid_by_index(int) override; - int get_griddata_by_name(int, char *, int &) override; + int get_griddata_by_name(int, const std::string &, int &) override; void *get_griddata_by_index(int) override; double memory_usage() override; diff --git a/src/utils.cpp b/src/utils.cpp index cbcb5c1a23..e68900d7e5 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -768,26 +768,21 @@ int utils::expand_args(const char *file, int line, int narg, char **arg, int mod /* ---------------------------------------------------------------------- Parse grid reference into id:gridname:dataname - return ptrs to 3 substrings + return vector of 3 substrings ------------------------------------------------------------------------- */ -void utils::grid_parse(const char *file, int line, const std::string &name, - char *&id, char *&gridname, char *&dataname, Error *error) +std::vector utils::gridid_parse(const char *file, int line, const std::string &name, + Error *error) { - char *copy = strdup(name); + auto words = Tokenizer(name, ":").as_vector(); + if (words.size() != 3) { + if (error) + error->all(FLERR, "Grid ID {} does not contain two ':' characters", name); + else + return {"", "", ""}; + } - char *ptr1 = strchr(copy,':'); - if (!ptr1) - error->all(FLERR,"Grid reference {} does not contain 2 ':' chars",name); - *ptr1 = '\0'; - char *ptr2 = strchr(ptr1+1,':'); - if (!ptr2) - error->all(FLERR,"Grid reference {} does not contain 2 ':' chars",name); - *ptr2 = '\0'; - - id = strdup(copy); - gridname = strdup(ptr1+1); - dataname = strdup(ptr2+1); + return words; } /* ---------------------------------------------------------------------- diff --git a/src/utils.h b/src/utils.h index 1e0fbf779f..3d8cb3d2de 100644 --- a/src/utils.h +++ b/src/utils.h @@ -359,6 +359,19 @@ namespace utils { int expand_args(const char *file, int line, int narg, char **arg, int mode, char **&earg, LAMMPS *lmp); + /*! Parse grid reference into 3 sub-strings + * + * Format of grid ID reference = id:gridname:dataname + * Return vector with the 3 sub-strings + * + * \param name = grid reference + * \param id = ptr to 1st substring + * \param gridname = ptr to 2nd substring + * \param dataname = ptr to 3rd substring */ + + std::vector gridid_parse(const char *file, int line, const std::string &name, + Error *error); + /*! Make C-style copy of string in new storage * * This allocates a storage buffer and copies the C-style or @@ -368,19 +381,6 @@ namespace utils { * \param text string that should be copied * \return new buffer with copy of string */ - void grid_parse(const char *file, int line, const std::string &name, - char *&id, char *&gridname, char *&dataname, Error *error); - - /*! Parse grid reference into 3 sub-strings - * - * Format of grid reference = id:gridname:dataname - * Return ptrs to the 3 sub-strings - * - * \param name = grid reference - * \param id = ptr to 1st substring - * \param gridname = ptr to 2nd substring - * \param dataname = ptr to 3rd substring */ - char *strdup(const std::string &text); /*! Convert string to lowercase diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index decd7d6379..74b1ac3de1 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -785,6 +785,34 @@ TEST(Utils, boundsbig_case3) ASSERT_EQ(nhi, -1); } +TEST(Utils, gridid_parse) +{ + auto words = utils::gridid_parse(FLERR, "c_1:full:density", nullptr); + ASSERT_THAT(words[0], StrEq("c_1")); + ASSERT_THAT(words[1], StrEq("full")); + ASSERT_THAT(words[2], StrEq("density")); + + words = utils::gridid_parse(FLERR, "c_1_full_density", nullptr); + ASSERT_THAT(words[0], StrEq("")); + ASSERT_THAT(words[1], StrEq("")); + ASSERT_THAT(words[0], StrEq("")); + + words = utils::gridid_parse(FLERR, "c_1:full:", nullptr); + ASSERT_THAT(words[0], StrEq("c_1")); + ASSERT_THAT(words[1], StrEq("full")); + ASSERT_THAT(words[0], StrEq("")); + + words = utils::gridid_parse(FLERR, ":full:density", nullptr); + ASSERT_THAT(words[0], StrEq("")); + ASSERT_THAT(words[1], StrEq("full")); + ASSERT_THAT(words[0], StrEq("density")); + + words = utils::gridid_parse(FLERR, "c_1:full", nullptr); + ASSERT_THAT(words[0], StrEq("")); + ASSERT_THAT(words[1], StrEq("")); + ASSERT_THAT(words[0], StrEq("")); +} + TEST(Utils, errorurl) { auto errmesg = utils::errorurl(10);