diff --git a/doc/src/variable.rst b/doc/src/variable.rst index dea52047cd..94f1ecd358 100644 --- a/doc/src/variable.rst +++ b/doc/src/variable.rst @@ -66,8 +66,7 @@ Syntax bound(group,dir,region), gyration(group,region), ke(group,reigon), angmom(group,dim,region), torque(group,dim,region), inertia(group,dimdim,region), omega(group,dim,region) - special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name) - labelmap functions = label(typelabel), blabel(typelabel), alabel(typelabel), dlabel(typelabel), ilabel(typelabel) + special functions = sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x), is_file(name), is_os(name), extract_setting(name), label2type(kind,label) feature functions = is_active(category,feature), is_available(category,feature), is_defined(category,id) atom value = id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i], q[i] atom vector = id, mass, type, mol, x, y, z, vx, vy, vz, fx, fy, fz, q @@ -506,9 +505,7 @@ references, and references to other variables. +--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Region functions | count(ID,IDR), mass(ID,IDR), charge(ID,IDR), xcm(ID,dim,IDR), vcm(ID,dim,IDR), fcm(ID,dim,IDR), bound(ID,dir,IDR), gyration(ID,IDR), ke(ID,IDR), angmom(ID,dim,IDR), torque(ID,dim,IDR), inertia(ID,dimdim,IDR), omega(ID,dim,IDR) | +--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ -| Special functions | sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x) | -+--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ -| Labelmap functions | label(typelabel), blabel(typelabel), alabel(typelabel), dlabel(typelabel), ilabel(typelabel) | +| Special functions | sum(x), min(x), max(x), ave(x), trap(x), slope(x), gmask(x), rmask(x), grmask(x,y), next(x), label2type(kind,label) | +--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Atom values | id[i], mass[i], type[i], mol[i], x[i], y[i], z[i], vx[i], vy[i], vz[i], fx[i], fy[i], fz[i], q[i] | +--------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ @@ -965,18 +962,11 @@ types, bond types and so on. For the full list of available keywords *name* and their meaning, see the documentation for extract_setting() via the link in this paragraph. ----------- - -Labelmap Functions ------------------- - -Labelmap functions convert type labels into numeric types, using label +The label2type() function converts type labels into numeric types, using label maps created by the :doc:`labelmap ` or :doc:`read_data ` -commands. Their argument must be a valid type label, and they return -the corresponding integer numeric type. The argument for the *label()*\ , -*blabel()*\ , *alabel()*\ , *dlabel()*\ , and *ilabel()* functions, -must be an atom, bond, angle, dihedral, or improper type label, -respectively. +commands. The first argument must be label map kind (atom, bond, angle, +dihedral, or improper) and the second argument the label. Iwill return +the corresponding integer numeric type. ---------- diff --git a/src/variable.cpp b/src/variable.cpp index a2a38c1a30..52d77023c1 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -1969,7 +1969,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) } else { // ---------------- - // math or group or special or labelmap function + // math or group or special function // ---------------- if (str[i] == '(') { @@ -1980,8 +1980,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar) if (math_function(word,contents,tree,treestack,ntreestack,argstack,nargstack,ivar)); else if (group_function(word,contents,tree,treestack,ntreestack,argstack,nargstack,ivar)); else if (special_function(word,contents,tree,treestack,ntreestack,argstack,nargstack,ivar)); - else if (labelmap_function(word,contents,tree,treestack,ntreestack,argstack,nargstack,ivar)); - else print_var_error(FLERR,fmt::format("Invalid math/group/special/labelmap function '{}()' " + else print_var_error(FLERR,fmt::format("Invalid math/group/special function '{}()' " "in variable formula", word),ivar); delete[] contents; @@ -3948,7 +3947,7 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t strcmp(word,"trap") != 0 && strcmp(word,"slope") != 0 && strcmp(word,"gmask") != 0 && strcmp(word,"rmask") != 0 && strcmp(word,"grmask") != 0 && strcmp(word,"next") != 0 && strcmp(word,"is_active") != 0 && strcmp(word,"is_defined") != 0 && strcmp(word,"is_available") != 0 && strcmp(word,"is_file") != 0 && - strcmp(word,"is_os") != 0 && strcmp(word,"extract_setting") != 0) + strcmp(word,"is_os") != 0 && strcmp(word,"extract_setting") != 0 && strcmp(word,"label2type") != 0) return 0; // parse contents for comma-separated args @@ -4376,6 +4375,43 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t newtree->value = value; treestack[ntreestack++] = newtree; } else argstack[nargstack++] = value; + + } else if (strcmp(word,"label2type") == 0) { + if (narg != 2) print_var_error(FLERR,"Invalid label2type() function syntax in variable formula",ivar); + + if (!atom->labelmapflag) + print_var_error(FLERR,"Cannot use label2type() function without a labelmap",ivar); + + int value; + std::string kind = args[0]; + std::string typestr = utils::trim(utils::utf8_subst(args[1])); + + if (kind == "atom") { + value = atom->lmap->find(typestr,Atom::ATOM); + } else if (kind == "bond") { + value = atom->lmap->find(typestr,Atom::BOND); + } else if (kind == "angle") { + value = atom->lmap->find(typestr,Atom::ANGLE); + } else if (kind == "dihedral") { + value = atom->lmap->find(typestr,Atom::DIHEDRAL); + } else if (kind == "improper") { + value = atom->lmap->find(typestr,Atom::IMPROPER); + } + + if (value == -1) + print_var_error(FLERR, fmt::format("Invalid {} type label {} in variable formula", + kind, typestr), ivar); + + // save value in tree or on argstack + + if (tree) { + Tree *newtree = new Tree(); + newtree->type = VALUE; + newtree->value = value; + newtree->first = newtree->second = nullptr; + newtree->nextra = 0; + treestack[ntreestack++] = newtree; + } else argstack[nargstack++] = value; } // delete stored args @@ -4385,67 +4421,6 @@ int Variable::special_function(char *word, char *contents, Tree **tree, Tree **t return 1; } -/* ---------------------------------------------------------------------- - process a type label function in formula - push result (numeric type) onto tree or arg stack - word = interaction type - contents = str between parentheses (one type label) - return 0 if not a match, 1 if successfully processed - customize by adding a label map function: - label(typelabel),blabel(typelabel),alabel(typelabel), - dlabel(typelabel),ilabel(typelabel) -------------------------------------------------------------------------- */ - -int Variable::labelmap_function(char *word, char *contents, Tree **tree, - Tree **treestack, int &ntreestack, - double *argstack, int &nargstack, int ivar) -{ - // word not a match to any label map function - - if (strcmp(word,"label") && strcmp(word,"blabel") && - strcmp(word,"alabel") && strcmp(word,"dlabel") && - strcmp(word,"ilabel")) - return 0; - - if (!atom->labelmapflag) - print_var_error(FLERR,"Invalid type label in variable formula",ivar); - - int value; - std::string typestr(contents); - - if (strcmp(word,"label") == 0) { - value = atom->lmap->find(typestr,Atom::ATOM); - - } else if (strcmp(word,"blabel") == 0) { - value = atom->lmap->find(typestr,Atom::BOND); - - } else if (strcmp(word,"alabel") == 0) { - value = atom->lmap->find(typestr,Atom::ANGLE); - - } else if (strcmp(word,"dlabel") == 0) { - value = atom->lmap->find(typestr,Atom::DIHEDRAL); - - } else if (strcmp(word,"ilabel") == 0) { - value = atom->lmap->find(typestr,Atom::IMPROPER); - } - - if (value == -1) - print_var_error(FLERR,"Invalid type label in variable formula",ivar); - - // save value in tree or on argstack - - if (tree) { - Tree *newtree = new Tree(); - newtree->type = VALUE; - newtree->value = value; - newtree->first = newtree->second = nullptr; - newtree->nextra = 0; - treestack[ntreestack++] = newtree; - } else argstack[nargstack++] = value; - - return 1; -} - /* ---------------------------------------------------------------------- extract a global value from a per-atom quantity in a formula flag = 0 -> word is an atom vector diff --git a/src/variable.h b/src/variable.h index ae74c121e8..1e05fd3904 100644 --- a/src/variable.h +++ b/src/variable.h @@ -140,7 +140,6 @@ class Variable : protected Pointers { int group_function(char *, char *, Tree **, Tree **, int &, double *, int &, int); Region *region_function(char *, int); int special_function(char *, char *, Tree **, Tree **, int &, double *, int &, int); - int labelmap_function(char *, char *, Tree **, Tree **, int &, double *, int &, int); void peratom2global(int, char *, double *, int, tagint, Tree **, Tree **, int &, double *, int &); int is_atom_vector(char *); void atom_vector(char *, Tree **, Tree **, int &);