rename variable special function to retrieve types for labels to label2type()

This commit is contained in:
Axel Kohlmeyer
2022-09-06 18:19:27 -04:00
parent 3b6a06fcf5
commit 1d986280ce
3 changed files with 46 additions and 82 deletions

View File

@ -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