From 5d2e3b3ecbf67bb4de29457ea4dfb6c0b28364bd Mon Sep 17 00:00:00 2001 From: Jacob Gissinger Date: Mon, 25 Jan 2021 20:08:25 -0500 Subject: [PATCH] direct support for coeff commands pair_coeff, bond_coeff, angle_coeff, dihedral_coeff, improper_coeff --- src/input.cpp | 32 ++++++++++++++++++++++++++++++++ src/input.h | 1 + src/label_map.cpp | 47 +++++++++++++++++++++++++++++++++++------------ src/label_map.h | 3 ++- src/molecule.cpp | 10 +++++----- src/variable.cpp | 10 +++++----- 6 files changed, 80 insertions(+), 23 deletions(-) diff --git a/src/input.cpp b/src/input.cpp index e409111090..1974c2b4f5 100755 --- a/src/input.cpp +++ b/src/input.cpp @@ -659,6 +659,32 @@ void Input::substitute(char *&str, char *&str2, int &max, int &max2, int flag) strcpy(str,str2); } +/* ---------------------------------------------------------------------- + substitute type labels with numeric value + reallocate str to hold expanded version if necessary + if type already starts with a number, do not modify +------------------------------------------------------------------------- */ + +void Input::readtype(char *&str, int mode) +{ + if (isdigit(str[0])) return; + if (!atom->labelmapflag) error->all(FLERR,fmt::format("Invalid type {}",str)); + + int type,max,max2; + std::string labelstr; + char typechar[256]; + + labelstr = str; + type = atom->lmap->find(labelstr,mode); + if (type == -1) error->all(FLERR,fmt::format("Invalid type {}",str)); + sprintf(typechar,"%d",type); + + max = strlen(str) + 1; + max2 = strlen(typechar) + 1; + if (max2 > max) reallocate(str,max,max2); + strcpy(str,typechar); +} + /* ---------------------------------------------------------------------- return number of triple quotes in line ------------------------------------------------------------------------- */ @@ -1303,6 +1329,7 @@ void Input::angle_coeff() error->all(FLERR,"Angle_coeff command before angle_style is defined"); if (atom->avec->angles_allow == 0) error->all(FLERR,"Angle_coeff command when no angles allowed"); + readtype(arg[0],atom->lmap->ANGLE); force->angle->coeff(narg,arg); } @@ -1344,6 +1371,7 @@ void Input::bond_coeff() error->all(FLERR,"Bond_coeff command before bond_style is defined"); if (atom->avec->bonds_allow == 0) error->all(FLERR,"Bond_coeff command when no bonds allowed"); + readtype(arg[0],atom->lmap->BOND); force->bond->coeff(narg,arg); } @@ -1447,6 +1475,7 @@ void Input::dihedral_coeff() error->all(FLERR,"Dihedral_coeff command before dihedral_style is defined"); if (atom->avec->dihedrals_allow == 0) error->all(FLERR,"Dihedral_coeff command when no dihedrals allowed"); + readtype(arg[0],atom->lmap->DIHEDRAL); force->dihedral->coeff(narg,arg); } @@ -1524,6 +1553,7 @@ void Input::improper_coeff() error->all(FLERR,"Improper_coeff command before improper_style is defined"); if (atom->avec->impropers_allow == 0) error->all(FLERR,"Improper_coeff command when no impropers allowed"); + readtype(arg[0],atom->lmap->IMPROPER); force->improper->coeff(narg,arg); } @@ -1701,6 +1731,8 @@ void Input::pair_coeff() error->all(FLERR,"Pair_coeff command before simulation box is defined"); if (force->pair == nullptr) error->all(FLERR,"Pair_coeff command before pair_style is defined"); + readtype(arg[0],atom->lmap->ATOM); + readtype(arg[1],atom->lmap->ATOM); force->pair->coeff(narg,arg); } diff --git a/src/input.h b/src/input.h index e275981d32..a5a4b2b341 100755 --- a/src/input.h +++ b/src/input.h @@ -38,6 +38,7 @@ class Input : protected Pointers { char *one(const std::string&); // process a single command void substitute(char *&, char *&, int &, int &, int); // substitute for variables in a string + void readtype(char *&, int); // substitute type label with numeric type void write_echo(const std::string &); // send text to active echo file pointers protected: diff --git a/src/label_map.cpp b/src/label_map.cpp index 8ea9ae94e6..b23af8f97f 100755 --- a/src/label_map.cpp +++ b/src/label_map.cpp @@ -148,28 +148,28 @@ void LabelMap::create_lmap2lmap(LabelMap *lmap2, int mode) { if (mode == ATOM) for (int i = 0; i < natomtypes; i++) - lmap2lmap.atom[i] = find(typelabel[i],lmap2->typelabel, - lmap2->natomtypes); + lmap2lmap.atom[i] = search(typelabel[i],lmap2->typelabel, + lmap2->natomtypes); if (mode == BOND) for (int i = 0; i < nbondtypes; i++) - lmap2lmap.bond[i] = find(btypelabel[i],lmap2->btypelabel, - lmap2->nbondtypes); + lmap2lmap.bond[i] = search(btypelabel[i],lmap2->btypelabel, + lmap2->nbondtypes); if (mode == ANGLE) for (int i = 0; i < nangletypes; i++) - lmap2lmap.angle[i] = find(atypelabel[i],lmap2->atypelabel, - lmap2->nangletypes); + lmap2lmap.angle[i] = search(atypelabel[i],lmap2->atypelabel, + lmap2->nangletypes); if (mode == DIHEDRAL) for (int i = 0; i < ndihedraltypes; i++) - lmap2lmap.dihedral[i] = find(dtypelabel[i],lmap2->dtypelabel, - lmap2->ndihedraltypes); + lmap2lmap.dihedral[i] = search(dtypelabel[i],lmap2->dtypelabel, + lmap2->ndihedraltypes); if (mode == IMPROPER) for (int i = 0; i < nimpropertypes; i++) - lmap2lmap.improper[i] = find(itypelabel[i],lmap2->itypelabel, - lmap2->nimpropertypes); + lmap2lmap.improper[i] = search(itypelabel[i],lmap2->itypelabel, + lmap2->nimpropertypes); } /* ---------------------------------------------------------------------- @@ -199,11 +199,34 @@ int LabelMap::find_or_create(std::string mylabel, std::vector &labe } /* ---------------------------------------------------------------------- - find integer type given a type label + return numeric type given a type label return -1 if type not yet defined ------------------------------------------------------------------------- */ -int LabelMap::find(std::string mylabel, std::vector labels, int ntypes) +int LabelMap::find(std::string mylabel, int mode) +{ + if (mode == ATOM) + return search(mylabel,typelabel,natomtypes); + + if (mode == BOND) + return search(mylabel,btypelabel,nbondtypes); + + if (mode == ANGLE) + return search(mylabel,atypelabel,nangletypes); + + if (mode == DIHEDRAL) + return search(mylabel,dtypelabel,ndihedraltypes); + + if (mode == IMPROPER) + return search(mylabel,itypelabel,nimpropertypes); +} + +/* ---------------------------------------------------------------------- + get index+1 given vector of strings + return -1 if type not yet defined +------------------------------------------------------------------------- */ + +int LabelMap::search(std::string mylabel, std::vector labels, int ntypes) { for (int i = 0; i < ntypes; i++) if (labels[i] == mylabel) return i+1; diff --git a/src/label_map.h b/src/label_map.h index 978923252e..11c3f19443 100755 --- a/src/label_map.h +++ b/src/label_map.h @@ -46,7 +46,8 @@ class LabelMap : protected Pointers { void merge_lmap(class LabelMap *, int); // copy another lmap into this one void create_lmap2lmap(class LabelMap *, int); // index mapping between two lmaps int find_or_create(std::string, std::vector &, int); // look up type or create new type - int find(std::string, std::vector, int); // look up type index + int find(std::string, int); // find numeric type of type label + int search(std::string, std::vector, int); // look up type index // input/output for atom class label map diff --git a/src/molecule.cpp b/src/molecule.cpp index 7556239aed..4c4a3bfba3 100755 --- a/src/molecule.cpp +++ b/src/molecule.cpp @@ -729,7 +729,7 @@ void Molecule::types(char *line) typestr = values.next_string(); if (!isdigit(typestr[0])) { if (!atom->labelmapflag) error->one(FLERR,"Invalid Types section in molecule file"); - type[iatom] = atom->lmap->find(typestr,atom->lmap->typelabel,atom->ntypes); + type[iatom] = atom->lmap->find(typestr,atom->lmap->ATOM); if (type[iatom] == -1) error->one(FLERR,"Invalid Types section in molecule file"); } else type[iatom] = utils::inumeric(FLERR,typestr.c_str(),false,lmp); type[iatom] += toffset; @@ -942,7 +942,7 @@ void Molecule::bonds(int flag, char *line) typestr = values.next_string(); if (!isdigit(typestr[0])) { if (!atom->labelmapflag) error->one(FLERR,"Invalid Bonds section in molecule file"); - itype = atom->lmap->find(typestr,atom->lmap->btypelabel,atom->nbondtypes); + itype = atom->lmap->find(typestr,atom->lmap->BOND); if (itype == -1) error->one(FLERR,"Invalid Bonds section in molecule file"); } else itype = utils::inumeric(FLERR,typestr.c_str(),false,lmp); atom1 = values.next_tagint(); @@ -1016,7 +1016,7 @@ void Molecule::angles(int flag, char *line) typestr = values.next_string(); if (!isdigit(typestr[0])) { if (!atom->labelmapflag) error->one(FLERR,"Invalid Angles section in molecule file"); - itype = atom->lmap->find(typestr,atom->lmap->atypelabel,atom->nangletypes); + itype = atom->lmap->find(typestr,atom->lmap->ANGLE); if (itype == -1) error->one(FLERR,"Invalid Angles section in molecule file"); } else itype = utils::inumeric(FLERR,typestr.c_str(),false,lmp); atom1 = values.next_tagint(); @@ -1106,7 +1106,7 @@ void Molecule::dihedrals(int flag, char *line) typestr = values.next_string(); if (!isdigit(typestr[0])) { if (!atom->labelmapflag) error->one(FLERR,"Invalid Dihedrals section in molecule file"); - itype = atom->lmap->find(typestr,atom->lmap->dtypelabel,atom->ndihedraltypes); + itype = atom->lmap->find(typestr,atom->lmap->DIHEDRAL); if (itype == -1) error->one(FLERR,"Invalid Dihedrals section in molecule file"); } else itype = utils::inumeric(FLERR,typestr.c_str(),false,lmp); atom1 = values.next_tagint(); @@ -1211,7 +1211,7 @@ void Molecule::impropers(int flag, char *line) typestr = values.next_string(); if (!isdigit(typestr[0])) { if (!atom->labelmapflag) error->one(FLERR,"Invalid Impropers section in molecule file"); - itype = atom->lmap->find(typestr,atom->lmap->itypelabel,atom->nimpropertypes); + itype = atom->lmap->find(typestr,atom->lmap->IMPROPER); if (itype == -1) error->one(FLERR,"Invalid Impropers section in molecule file"); } else itype = utils::inumeric(FLERR,typestr.c_str(),false,lmp); atom1 = values.next_tagint(); diff --git a/src/variable.cpp b/src/variable.cpp index e63940e768..fdd60de1a0 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -4541,19 +4541,19 @@ int Variable::labelmap_function(char *word, char *contents, Tree **tree, std::string typestr = contents; if (strcmp(word,"label") == 0) { - value = atom->lmap->find(typestr,atom->lmap->typelabel,atom->ntypes); + value = atom->lmap->find(typestr,atom->lmap->ATOM); } else if (strcmp(word,"blabel") == 0) { - value = atom->lmap->find(typestr,atom->lmap->btypelabel,atom->nbondtypes); + value = atom->lmap->find(typestr,atom->lmap->BOND); } else if (strcmp(word,"alabel") == 0) { - value = atom->lmap->find(typestr,atom->lmap->atypelabel,atom->nangletypes); + value = atom->lmap->find(typestr,atom->lmap->ANGLE); } else if (strcmp(word,"dlabel") == 0) { - value = atom->lmap->find(typestr,atom->lmap->dtypelabel,atom->ndihedraltypes); + value = atom->lmap->find(typestr,atom->lmap->DIHEDRAL); } else if (strcmp(word,"ilabel") == 0) { - value = atom->lmap->find(typestr,atom->lmap->itypelabel,atom->nimpropertypes); + value = atom->lmap->find(typestr,atom->lmap->IMPROPER); } if (value == -1)