diff --git a/src/variable.cpp b/src/variable.cpp index 6766a67a9a..eb1deaae15 100644 --- a/src/variable.cpp +++ b/src/variable.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include 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 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 diff --git a/src/variable.h b/src/variable.h index cb74e71b51..5dc8748936 100644 --- a/src/variable.h +++ b/src/variable.h @@ -122,8 +122,6 @@ class Variable : protected Pointers { Tree **, Tree **, int &, double *, int &); int is_atom_vector(char *); void atom_vector(char *, Tree **, Tree **, int &); - int is_constant(char *); - double constant(char *); int parse_args(char *, char **); void print_var_error(const std::string &, int, const std::string &, int, int global=1); diff --git a/unittest/commands/test_variables.cpp b/unittest/commands/test_variables.cpp index 8c0a24bc0d..cc20ea2b96 100644 --- a/unittest/commands/test_variables.cpp +++ b/unittest/commands/test_variables.cpp @@ -211,14 +211,17 @@ TEST_F(VariableTest, AtomicSystem) TEST_F(VariableTest, Expressions) { + atomic_system(); ASSERT_EQ(variable->nvar, 0); if (!verbose) ::testing::internal::CaptureStdout(); command("variable one index 1"); command("variable two equal 2"); command("variable three equal v_one+v_two"); command("variable four equal PI"); + command("variable five equal version"); + command("variable six equal XXX"); if (!verbose) ::testing::internal::GetCapturedStdout(); - ASSERT_EQ(variable->nvar, 4); + ASSERT_EQ(variable->nvar, 6); int ivar = variable->find("one"); ASSERT_FALSE(variable->equalstyle(ivar)); @@ -229,11 +232,18 @@ TEST_F(VariableTest, Expressions) ASSERT_DOUBLE_EQ(variable->compute_equal(ivar),3.0); ivar = variable->find("four"); ASSERT_DOUBLE_EQ(variable->compute_equal(ivar),MY_PI); + ivar = variable->find("five"); + ASSERT_GE(variable->compute_equal(ivar),20210310); + + TEST_FAILURE(".*ERROR: Variable six: Invalid thermo keyword 'XXX' in variable formula.*", + command("print \"${six}\"");); } TEST_F(VariableTest, Functions) { + atomic_system(); file_vars(); + ASSERT_EQ(variable->nvar, 0); if (!verbose) ::testing::internal::CaptureStdout(); command("variable one index 1");