replace redundant functions to handle constants with (unordered) map
This commit is contained in:
@ -39,6 +39,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
using namespace LAMMPS_NS;
|
using namespace LAMMPS_NS;
|
||||||
@ -76,6 +77,20 @@ enum{SUM,XMIN,XMAX,AVE,TRAP,SLOPE};
|
|||||||
|
|
||||||
#define BIG 1.0e20
|
#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)
|
Variable::Variable(LAMMPS *lmp) : Pointers(lmp)
|
||||||
@ -98,6 +113,10 @@ Variable::Variable(LAMMPS *lmp) : Pointers(lmp)
|
|||||||
randomequal = nullptr;
|
randomequal = nullptr;
|
||||||
randomatom = nullptr;
|
randomatom = nullptr;
|
||||||
|
|
||||||
|
// override initializer since LAMMPS class needs to be instantiated
|
||||||
|
|
||||||
|
constants["version"] = lmp->num_ver;
|
||||||
|
|
||||||
// customize by assigning a precedence level
|
// customize by assigning a precedence level
|
||||||
|
|
||||||
precedence[DONE] = 0;
|
precedence[DONE] = 0;
|
||||||
@ -2093,8 +2112,8 @@ double Variable::evaluate(char *str, Tree **tree, int ivar)
|
|||||||
// constant
|
// constant
|
||||||
// ----------------
|
// ----------------
|
||||||
|
|
||||||
} else if (is_constant(word)) {
|
} else if (constants.find(word) != constants.end()) {
|
||||||
value1 = constant(word);
|
value1 = constants[word];
|
||||||
if (tree) {
|
if (tree) {
|
||||||
Tree *newtree = new Tree();
|
Tree *newtree = new Tree();
|
||||||
newtree->type = VALUE;
|
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
|
parse string for comma-separated args
|
||||||
store copy of each arg in args array
|
store copy of each arg in args array
|
||||||
|
|||||||
@ -122,8 +122,6 @@ class Variable : protected Pointers {
|
|||||||
Tree **, Tree **, int &, double *, int &);
|
Tree **, Tree **, int &, double *, int &);
|
||||||
int is_atom_vector(char *);
|
int is_atom_vector(char *);
|
||||||
void atom_vector(char *, Tree **, Tree **, int &);
|
void atom_vector(char *, Tree **, Tree **, int &);
|
||||||
int is_constant(char *);
|
|
||||||
double constant(char *);
|
|
||||||
int parse_args(char *, char **);
|
int parse_args(char *, char **);
|
||||||
void print_var_error(const std::string &, int, const std::string &,
|
void print_var_error(const std::string &, int, const std::string &,
|
||||||
int, int global=1);
|
int, int global=1);
|
||||||
|
|||||||
@ -211,14 +211,17 @@ TEST_F(VariableTest, AtomicSystem)
|
|||||||
|
|
||||||
TEST_F(VariableTest, Expressions)
|
TEST_F(VariableTest, Expressions)
|
||||||
{
|
{
|
||||||
|
atomic_system();
|
||||||
ASSERT_EQ(variable->nvar, 0);
|
ASSERT_EQ(variable->nvar, 0);
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
command("variable one index 1");
|
command("variable one index 1");
|
||||||
command("variable two equal 2");
|
command("variable two equal 2");
|
||||||
command("variable three equal v_one+v_two");
|
command("variable three equal v_one+v_two");
|
||||||
command("variable four equal PI");
|
command("variable four equal PI");
|
||||||
|
command("variable five equal version");
|
||||||
|
command("variable six equal XXX");
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
ASSERT_EQ(variable->nvar, 4);
|
ASSERT_EQ(variable->nvar, 6);
|
||||||
|
|
||||||
int ivar = variable->find("one");
|
int ivar = variable->find("one");
|
||||||
ASSERT_FALSE(variable->equalstyle(ivar));
|
ASSERT_FALSE(variable->equalstyle(ivar));
|
||||||
@ -229,11 +232,18 @@ TEST_F(VariableTest, Expressions)
|
|||||||
ASSERT_DOUBLE_EQ(variable->compute_equal(ivar),3.0);
|
ASSERT_DOUBLE_EQ(variable->compute_equal(ivar),3.0);
|
||||||
ivar = variable->find("four");
|
ivar = variable->find("four");
|
||||||
ASSERT_DOUBLE_EQ(variable->compute_equal(ivar),MY_PI);
|
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)
|
TEST_F(VariableTest, Functions)
|
||||||
{
|
{
|
||||||
|
atomic_system();
|
||||||
file_vars();
|
file_vars();
|
||||||
|
|
||||||
ASSERT_EQ(variable->nvar, 0);
|
ASSERT_EQ(variable->nvar, 0);
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
command("variable one index 1");
|
command("variable one index 1");
|
||||||
|
|||||||
Reference in New Issue
Block a user