move shared functionality to utility function added to Lepton library

This commit is contained in:
Axel Kohlmeyer
2022-12-22 05:37:59 -05:00
parent 3bb6e1ab19
commit 4cbe8b353b
4 changed files with 45 additions and 13 deletions

View File

@ -32,6 +32,8 @@
* USE OR OTHER DEALINGS IN THE SOFTWARE. *
* -------------------------------------------------------------------------- */
#include <string>
#include "lepton/CompiledExpression.h"
#include "lepton/CustomFunction.h"
#include "lepton/ExpressionProgram.h"
@ -40,4 +42,10 @@
#include "lepton/ParsedExpression.h"
#include "lepton/Parser.h"
// utility functions
namespace LMP_Lepton
{
/// remove whitespace and quotes from expression string
std::string condense(const std::string &);
}
#endif /*LMP_LEPTON_H_*/

31
lib/lepton/src/Utils.cpp Normal file
View File

@ -0,0 +1,31 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
Contributing author: Axel Kohlmeyer (Temple U)
------------------------------------------------------------------------- */
#include "LMP_Lepton.h"
#include <cctype>
/// remove whitespace and quotes from expression string
std::string LMP_Lepton::condense(const std::string & in)
{
std::string out;
for (const auto &c : in)
if (!isspace(c) && (c != '"') && (c != '\'')) out.push_back(c);
return out;
}

View File

@ -24,9 +24,7 @@
#include "memory.h"
#include "neighbor.h"
#include <cctype>
#include <cmath>
#include <cstring>
#include "LMP_Lepton.h"
@ -170,10 +168,7 @@ void BondLepton::coeff(int narg, char **arg)
// remove whitespace and quotes from expression string and then
// check if the expression can be parsed and evaluated without error
std::string exp_one;
for (const auto &c : std::string(arg[2]))
if (!isspace(c) && (c != '"') && (c != '\'')) exp_one.push_back(c);
std::string exp_one = LMP_Lepton::condense(arg[2]);
try {
auto parsed = LMP_Lepton::Parser::parse(exp_one);
auto bondpot = parsed.createCompiledExpression();
@ -317,6 +312,9 @@ double BondLepton::single(int type, double rsq, int /*i*/, int /*j*/, double &ff
void *BondLepton::extract(const char *str, int &dim)
{
dim = 1;
if (strcmp(str, "r0") == 0) return (void *) r0;
if (str) {
std::string keyword(str);
if (keyword == "r0") return (void *) r0;
}
return nullptr;
}

View File

@ -25,9 +25,7 @@
#include "neigh_list.h"
#include "update.h"
#include <cctype>
#include <cmath>
#include <cstring>
#include "LMP_Lepton.h"
@ -209,10 +207,7 @@ void PairLepton::coeff(int narg, char **arg)
// remove whitespace and quotes from expression string and then
// check if the expression can be parsed and evaluated without error
std::string exp_one;
for (const auto &c : std::string(arg[2]))
if (!isspace(c) && (c != '"') && (c != '\'')) exp_one.push_back(c);
std::string exp_one = LMP_Lepton::condense(arg[2]);
try {
auto parsed = LMP_Lepton::Parser::parse(exp_one);
auto pairpot = parsed.createCompiledExpression();