ExprFunctions: modernize code
use unique_ptr
This commit is contained in:
parent
367e3fbe6d
commit
574bf77266
|
@ -91,7 +91,7 @@ int EvalFunc::numError = 0;
|
|||
|
||||
|
||||
EvalFunc::EvalFunc (const char* function, const char* x, Real eps)
|
||||
: gradient(nullptr), dx(eps)
|
||||
: dx(eps)
|
||||
{
|
||||
try {
|
||||
#ifdef USE_OPENMP
|
||||
|
@ -105,51 +105,31 @@ EvalFunc::EvalFunc (const char* function, const char* x, Real eps)
|
|||
expr.resize(nalloc);
|
||||
arg.resize(nalloc);
|
||||
for (size_t i = 0; i < nalloc; ++i) {
|
||||
expr[i] = new Expression;
|
||||
f[i] = new FunctionList;
|
||||
v[i] = new ValueList;
|
||||
expr[i] = std::make_unique<Expression>();
|
||||
f[i] = std::make_unique<FunctionList>();
|
||||
v[i] = std::make_unique<ValueList>();
|
||||
f[i]->AddDefaultFunctions();
|
||||
v[i]->AddDefaultValues();
|
||||
v[i]->Add(x,0.0,false);
|
||||
expr[i]->SetFunctionList(f[i]);
|
||||
expr[i]->SetValueList(v[i]);
|
||||
expr[i]->SetFunctionList(f[i].get());
|
||||
expr[i]->SetValueList(v[i].get());
|
||||
expr[i]->Parse(function);
|
||||
arg[i] = v[i]->GetAddress(x);
|
||||
}
|
||||
}
|
||||
catch (ExprEval::Exception& e) {
|
||||
this->cleanup();
|
||||
ExprException(e,"parsing",function);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
EvalFunc::~EvalFunc ()
|
||||
{
|
||||
this->cleanup();
|
||||
}
|
||||
|
||||
|
||||
void EvalFunc::cleanup ()
|
||||
{
|
||||
for (Expression* it : expr)
|
||||
delete it;
|
||||
for (FunctionList* it : f)
|
||||
delete it;
|
||||
for (ValueList* it : v)
|
||||
delete it;
|
||||
delete gradient;
|
||||
expr.clear();
|
||||
f.clear();
|
||||
v.clear();
|
||||
arg.clear();
|
||||
}
|
||||
EvalFunc::~EvalFunc () = default;
|
||||
|
||||
|
||||
void EvalFunc::addDerivative (const std::string& function, const char* x)
|
||||
{
|
||||
if (!gradient)
|
||||
gradient = new EvalFunc(function.c_str(),x);
|
||||
gradient = std::make_unique<EvalFunc>(function.c_str(),x);
|
||||
}
|
||||
|
||||
|
||||
|
@ -185,7 +165,7 @@ Real EvalFunc::deriv (Real x) const
|
|||
|
||||
|
||||
EvalFunction::EvalFunction (const char* function, Real epsX, Real epsT)
|
||||
: gradient{}, dgradient{}, dx(epsX), dt(epsT)
|
||||
: dx(epsX), dt(epsT)
|
||||
{
|
||||
try {
|
||||
#ifdef USE_OPENMP
|
||||
|
@ -199,17 +179,17 @@ EvalFunction::EvalFunction (const char* function, Real epsX, Real epsT)
|
|||
expr.resize(nalloc);
|
||||
arg.resize(nalloc);
|
||||
for (size_t i = 0; i < nalloc; ++i) {
|
||||
expr[i] = new Expression;
|
||||
f[i] = new FunctionList;
|
||||
v[i] = new ValueList;
|
||||
expr[i] = std::make_unique<Expression>();
|
||||
f[i] = std::make_unique<FunctionList>();
|
||||
v[i] = std::make_unique<ValueList>();
|
||||
f[i]->AddDefaultFunctions();
|
||||
v[i]->AddDefaultValues();
|
||||
v[i]->Add("x",0.0,false);
|
||||
v[i]->Add("y",0.0,false);
|
||||
v[i]->Add("z",0.0,false);
|
||||
v[i]->Add("t",0.0,false);
|
||||
expr[i]->SetFunctionList(f[i]);
|
||||
expr[i]->SetValueList(v[i]);
|
||||
expr[i]->SetFunctionList(f[i].get());
|
||||
expr[i]->SetValueList(v[i].get());
|
||||
expr[i]->Parse(function);
|
||||
arg[i].x = v[i]->GetAddress("x");
|
||||
arg[i].y = v[i]->GetAddress("y");
|
||||
|
@ -218,7 +198,6 @@ EvalFunction::EvalFunction (const char* function, Real epsX, Real epsT)
|
|||
}
|
||||
}
|
||||
catch (ExprEval::Exception& e) {
|
||||
this->cleanup();
|
||||
ExprException(e,"parsing",function);
|
||||
}
|
||||
|
||||
|
@ -234,31 +213,7 @@ EvalFunction::EvalFunction (const char* function, Real epsX, Real epsT)
|
|||
}
|
||||
|
||||
|
||||
EvalFunction::~EvalFunction ()
|
||||
{
|
||||
this->cleanup();
|
||||
}
|
||||
|
||||
|
||||
void EvalFunction::cleanup ()
|
||||
{
|
||||
for (Expression* it : expr)
|
||||
delete it;
|
||||
for (FunctionList* it : f)
|
||||
delete it;
|
||||
for (ValueList* it : v)
|
||||
delete it;
|
||||
for (EvalFunction* it : gradient)
|
||||
delete it;
|
||||
for (EvalFunction* it : dgradient)
|
||||
delete it;
|
||||
expr.clear();
|
||||
f.clear();
|
||||
v.clear();
|
||||
arg.clear();
|
||||
gradient.fill(nullptr);
|
||||
dgradient.fill(nullptr);
|
||||
}
|
||||
EvalFunction::~EvalFunction () = default;
|
||||
|
||||
|
||||
/*!
|
||||
|
@ -289,12 +244,12 @@ void EvalFunction::addDerivative (const std::string& function,
|
|||
if (d1 > 0 && d1 <= 4 && d2 < 1) // A first derivative is specified
|
||||
{
|
||||
if (!gradient[--d1])
|
||||
gradient[d1] = new EvalFunction((variables+function).c_str());
|
||||
gradient[d1] = std::make_unique<EvalFunction>((variables+function).c_str());
|
||||
}
|
||||
else if ((d1 = voigtIdx(d1,d2)) >= 0) // A second derivative is specified
|
||||
{
|
||||
if (!dgradient[d1])
|
||||
dgradient[d1] = new EvalFunction((variables+function).c_str());
|
||||
dgradient[d1] = std::make_unique<EvalFunction>((variables+function).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -378,7 +333,7 @@ Real EvalFunction::dderiv (const Vec3& X, int d1, int d2) const
|
|||
|
||||
void EvalFunction::setParam (const std::string& name, double value)
|
||||
{
|
||||
for (ValueList* v1 : v) {
|
||||
for (std::unique_ptr<ValueList>& v1 : v) {
|
||||
double* address = v1->GetAddress(name);
|
||||
if (!address)
|
||||
v1->Add(name,value,false);
|
||||
|
@ -421,15 +376,11 @@ EvalFunctions::EvalFunctions (const std::string& functions,
|
|||
{
|
||||
std::vector<std::string> components = splitComps(functions,variables);
|
||||
for (const std::string& comp : components)
|
||||
p.push_back(new EvalFunction(comp.c_str()));
|
||||
p.emplace_back(std::make_unique<EvalFunction>(comp.c_str()));
|
||||
}
|
||||
|
||||
|
||||
EvalFunctions::~EvalFunctions ()
|
||||
{
|
||||
for (EvalFunction* it : p)
|
||||
delete it;
|
||||
}
|
||||
EvalFunctions::~EvalFunctions () = default;
|
||||
|
||||
|
||||
void EvalFunctions::addDerivative (const std::string& functions,
|
||||
|
|
|
@ -16,9 +16,11 @@
|
|||
|
||||
#include "Function.h"
|
||||
#include "TensorFunction.h"
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
||||
namespace ExprEval {
|
||||
template<class ArgType> class Expression;
|
||||
|
@ -36,13 +38,13 @@ class EvalFunc : public ScalarFunc
|
|||
using Expression = ExprEval::Expression<Real>; //!< Type alias for expression tree
|
||||
using FunctionList = ExprEval::FunctionList<Real>; //!< Type alias for function list
|
||||
using ValueList = ExprEval::ValueList<Real>; //!< Type alias for value list
|
||||
std::vector<Expression*> expr; //!< Roots of the expression tree
|
||||
std::vector<FunctionList*> f; //!< Lists of functions
|
||||
std::vector<ValueList*> v; //!< Lists of variables and constants
|
||||
std::vector<std::unique_ptr<Expression>> expr; //!< Roots of the expression tree
|
||||
std::vector<std::unique_ptr<FunctionList>> f; //!< Lists of functions
|
||||
std::vector<std::unique_ptr<ValueList>> v; //!< Lists of variables and constants
|
||||
|
||||
std::vector<Real*> arg; //!< Function argument values
|
||||
|
||||
EvalFunc* gradient; //!< First derivative expression
|
||||
std::unique_ptr<EvalFunc> gradient; //!< First derivative expression
|
||||
|
||||
Real dx; //!< Domain increment for calculation of numerical derivative
|
||||
|
||||
|
@ -52,7 +54,9 @@ public:
|
|||
//! \brief The constructor parses the expression string.
|
||||
explicit EvalFunc(const char* function, const char* x = "x",
|
||||
Real eps = Real(1.0e-8));
|
||||
//! \brief The destructor frees the dynamically allocated objects.
|
||||
//! \brief Defaulted destructor.
|
||||
//! \details The implementation needs to be in compile unit so we have the
|
||||
//! definition for the types of the unique_ptr's.
|
||||
virtual ~EvalFunc();
|
||||
|
||||
//! \brief Adds an expression function for a first derivative.
|
||||
|
@ -71,9 +75,6 @@ protected:
|
|||
EvalFunc& operator=(const EvalFunc&) = delete;
|
||||
//! \brief Evaluates the function expression.
|
||||
virtual Real evaluate(const Real& x) const;
|
||||
|
||||
//! \brief Cleans up the allocated data.
|
||||
void cleanup();
|
||||
};
|
||||
|
||||
|
||||
|
@ -86,9 +87,9 @@ class EvalFunction : public RealFunc
|
|||
using Expression = ExprEval::Expression<Real>; //!< Type alias for expression tree
|
||||
using FunctionList = ExprEval::FunctionList<Real>; //!< Type alias for function list
|
||||
using ValueList = ExprEval::ValueList<Real>; //!< Type alias for value list
|
||||
std::vector<Expression*> expr; //!< Roots of the expression tree
|
||||
std::vector<FunctionList*> f; //!< Lists of functions
|
||||
std::vector<ValueList*> v; //!< Lists of variables and constants
|
||||
std::vector<std::unique_ptr<Expression>> expr; //!< Roots of the expression tree
|
||||
std::vector<std::unique_ptr<FunctionList>> f; //!< Lists of functions
|
||||
std::vector<std::unique_ptr<ValueList>> v; //!< Lists of variables and constants
|
||||
|
||||
//! \brief A struct representing a spatial function argument.
|
||||
struct Arg
|
||||
|
@ -101,8 +102,8 @@ class EvalFunction : public RealFunc
|
|||
|
||||
std::vector<Arg> arg; //!< Function argument values
|
||||
|
||||
std::array<EvalFunction*,4> gradient; //!< First derivative expressions
|
||||
std::array<EvalFunction*,6> dgradient; //!< Second derivative expressions
|
||||
std::array<std::unique_ptr<EvalFunction>,4> gradient; //!< First derivative expressions
|
||||
std::array<std::unique_ptr<EvalFunction>,6> dgradient; //!< Second derivative expressions
|
||||
|
||||
bool IAmConstant; //!< Indicates whether the time coordinate is given or not
|
||||
|
||||
|
@ -113,7 +114,9 @@ public:
|
|||
//! \brief The constructor parses the expression string.
|
||||
explicit EvalFunction(const char* function,
|
||||
Real epsX = Real(1.0e-8), Real epsT = Real(1.0e-12));
|
||||
//! \brief The destructor frees the dynamically allocated objects.
|
||||
//! \brief Defaulted destructor.
|
||||
//! \details The implementation needs to be in compile unit so we have the
|
||||
//! definition for the types of the unique_ptr's.
|
||||
virtual ~EvalFunction();
|
||||
|
||||
//! \brief Adds an expression function for a first or second derivative.
|
||||
|
@ -138,9 +141,6 @@ protected:
|
|||
EvalFunction& operator=(const EvalFunction&) = delete;
|
||||
//! \brief Evaluates the function expression.
|
||||
virtual Real evaluate(const Vec3& X) const;
|
||||
|
||||
//! \brief Cleans up the allocated data.
|
||||
void cleanup();
|
||||
};
|
||||
|
||||
|
||||
|
@ -153,7 +153,9 @@ class EvalFunctions
|
|||
protected:
|
||||
//! \brief The constructor parses the expression string for each component.
|
||||
EvalFunctions(const std::string& functions, const std::string& variables);
|
||||
//! \brief The destructor frees the dynamically allocated function components.
|
||||
//! \brief Defaulted destructor.
|
||||
//! \details The implementation needs to be in compile unit so we have the
|
||||
//! definition for the types of the unique_ptr's.
|
||||
virtual ~EvalFunctions();
|
||||
|
||||
public:
|
||||
|
@ -162,7 +164,7 @@ public:
|
|||
const std::string& variables, int d1, int d2 = 0);
|
||||
|
||||
protected:
|
||||
std::vector<EvalFunction*> p; //!< Array of component expressions
|
||||
std::vector<std::unique_ptr<EvalFunction>> p; //!< Array of component expressions
|
||||
};
|
||||
|
||||
|
||||
|
@ -188,7 +190,7 @@ public:
|
|||
//! \brief Returns whether the function is time-independent or not.
|
||||
virtual bool isConstant() const
|
||||
{
|
||||
for (EvalFunction* func : p)
|
||||
for (const std::unique_ptr<EvalFunction>& func : p)
|
||||
if (!func->isConstant())
|
||||
return false;
|
||||
return true;
|
||||
|
@ -205,7 +207,7 @@ public:
|
|||
//! \brief Set an additional parameter in the function.
|
||||
void setParam(const std::string& name, double value)
|
||||
{
|
||||
for (EvalFunction* func : p)
|
||||
for (std::unique_ptr<EvalFunction>& func : p)
|
||||
func->setParam(name, value);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user