replace redundant functions to handle constants with (unordered) map

This commit is contained in:
Axel Kohlmeyer
2021-03-23 17:48:47 -04:00
parent 1efd72eb58
commit 346c36e227
3 changed files with 32 additions and 42 deletions

View File

@ -39,6 +39,7 @@
#include <cmath>
#include <cstring>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace LAMMPS_NS;
@ -76,6 +77,20 @@ enum{SUM,XMIN,XMAX,AVE,TRAP,SLOPE};
#define BIG 1.0e20
// constants for variable expressions. customize by adding new items.
// if needed (cf. 'version') initialize in Variable class constructor.
static std::unordered_map<std::string, double> constants = {
{"PI", MY_PI },
{"version", -1 },
{"yes", 1 },
{"no", 0 },
{"on", 1 },
{"off", 0 },
{"true", 1 },
{"false", 0 }
};
/* ---------------------------------------------------------------------- */
Variable::Variable(LAMMPS *lmp) : Pointers(lmp)
@ -98,6 +113,10 @@ Variable::Variable(LAMMPS *lmp) : Pointers(lmp)
randomequal = nullptr;
randomatom = nullptr;
// override initializer since LAMMPS class needs to be instantiated
constants["version"] = lmp->num_ver;
// customize by assigning a precedence level
precedence[DONE] = 0;
@ -2093,8 +2112,8 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
// constant
// ----------------
} else if (is_constant(word)) {
value1 = constant(word);
} else if (constants.find(word) != constants.end()) {
value1 = constants[word];
if (tree) {
Tree *newtree = new Tree();
newtree->type = VALUE;
@ -4663,43 +4682,6 @@ void Variable::atom_vector(char *word, Tree **tree,
}
}
/* ----------------------------------------------------------------------
check if word matches a constant
return 1 if yes, else 0
customize by adding a constant: PI, version
------------------------------------------------------------------------- */
int Variable::is_constant(char *word)
{
if (strcmp(word,"PI") == 0) return 1;
if (strcmp(word,"version") == 0) return 1;
if (strcmp(word,"yes") == 0) return 1;
if (strcmp(word,"no") == 0) return 1;
if (strcmp(word,"on") == 0) return 1;
if (strcmp(word,"off") == 0) return 1;
if (strcmp(word,"true") == 0) return 1;
if (strcmp(word,"false") == 0) return 1;
return 0;
}
/* ----------------------------------------------------------------------
process a constant in formula
customize by adding a constant: PI, version
------------------------------------------------------------------------- */
double Variable::constant(char *word)
{
if (strcmp(word,"PI") == 0) return MY_PI;
if (strcmp(word,"version") == 0) return lmp->num_ver;
if (strcmp(word,"yes") == 0) return 1.0;
if (strcmp(word,"no") == 0) return 0.0;
if (strcmp(word,"on") == 0) return 1.0;
if (strcmp(word,"off") == 0) return 0.0;
if (strcmp(word,"true") == 0) return 1.0;
if (strcmp(word,"false") == 0) return 0.0;
return 0.0;
}
/* ----------------------------------------------------------------------
parse string for comma-separated args
store copy of each arg in args array