add labelmap function to variable command

This commit is contained in:
Jacob Gissinger
2021-01-23 17:28:11 -05:00
parent 67c2352015
commit c7215b54be
2 changed files with 70 additions and 2 deletions

View File

@ -24,6 +24,7 @@
#include "info.h"
#include "input.h"
#include "lmppython.h"
#include "label_map.h"
#include "math_const.h"
#include "memory.h"
#include "modify.h"
@ -2067,7 +2068,7 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
} else {
// ----------------
// math or group or special function
// math or group or special or labelmap function
// ----------------
if (str[i] == '(') {
@ -2081,7 +2082,9 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
argstack,nargstack,ivar));
else if (special_function(word,contents,tree,treestack,ntreestack,
argstack,nargstack,ivar));
else print_var_error(FLERR,fmt::format("Invalid math/group/special "
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 '{}()'in variable "
"formula", word),ivar);
delete [] contents;
@ -4508,6 +4511,69 @@ int Variable::special_function(char *word, char *contents, Tree **tree,
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->lmap->typelabel,atom->ntypes);
} else if (strcmp(word,"blabel") == 0) {
value = atom->lmap->find(typestr,atom->lmap->btypelabel,atom->nbondtypes);
} else if (strcmp(word,"alabel") == 0) {
value = atom->lmap->find(typestr,atom->lmap->atypelabel,atom->nangletypes);
} else if (strcmp(word,"dlabel") == 0) {
value = atom->lmap->find(typestr,atom->lmap->dtypelabel,atom->ndihedraltypes);
} else if (strcmp(word,"ilabel") == 0) {
value = atom->lmap->find(typestr,atom->lmap->itypelabel,atom->nimpropertypes);
}
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

View File

@ -118,6 +118,8 @@ class Variable : protected Pointers {
int 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 *);