Commit d837201f authored by Pavel Burovskiy's avatar Pavel Burovskiy
Browse files

Making expression evaluator a property of session

git-svn-id: https://gforge.sci.utah.edu/svn/nektar/trunk@3911 305cdda6-5ce1-45b3-a98d-dfc68c8b3305
parent 77ea6bba
......@@ -64,7 +64,11 @@ namespace Nektar
* pre-processed expressions.
*
*/
LibUtilities::AnalyticExpressionEvaluator Equation::m_evaluator;
/// \todo refactor this class: change its name and move implementation
/// to this file
// LibUtilities::AnalyticExpressionEvaluator Equation::m_evaluator;
}
}
......
......@@ -42,6 +42,7 @@
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
#include <LibUtilities/LibUtilitiesDeclspec.h>
#include <LibUtilities/BasicUtils/SharedArray.hpp>
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <boost/algorithm/string/trim.hpp>
namespace Nektar
......@@ -53,14 +54,16 @@ namespace Nektar
public:
Equation(const Equation &src):
m_expr (src.m_expr),
m_expr_id (src.m_expr_id)
m_expr (src.m_expr),
m_expr_id (src.m_expr_id),
m_evaluator (src.m_evaluator)
{
}
Equation(const std::string& expr = ""):
m_expr(expr),
m_expr_id(-1)
Equation(const SessionReaderSharedPtr& session, const std::string& expr = ""):
m_expr (expr),
m_expr_id (-1),
m_evaluator (session->GetExpressionEvaluator())
{
boost::algorithm::trim(m_expr);
......@@ -198,7 +201,7 @@ namespace Nektar
std::string GetExpression(void) const
{
return m_expr;
return m_expr;
}
/// Returns time spend on expression evaluation at
......@@ -211,10 +214,8 @@ namespace Nektar
private:
std::string m_expr;
int m_expr_id;
LIB_UTILITIES_EXPORT static LibUtilities::AnalyticExpressionEvaluator m_evaluator;
AnalyticExpressionEvaluator& m_evaluator;
};
typedef boost::shared_ptr<Equation> EquationSharedPtr;
}
}
......
......@@ -859,6 +859,18 @@ namespace Nektar
}
/**
*
*/
AnalyticExpressionEvaluator& SessionReader::GetExpressionEvaluator()
{
return m_exprEvaluator;
}
/**
*
*/
......@@ -1216,15 +1228,14 @@ namespace Nektar
NekDouble value=0.0;
try
{
LibUtilities::Equation expEvaluator(rhs);
value = expEvaluator.Evaluate();
LibUtilities::Equation expession(GetSharedThisPtr(), rhs);
value = expession.Evaluate();
}
catch (const std::runtime_error &)
{
ASSERTL0(false, "Error evaluating parameter expression '" + rhs + "'." );
}
LibUtilities::Equation expEvaluator;
expEvaluator.SetParameter(lhs, value);
m_exprEvaluator.SetParameter(lhs, value);
caseSensitiveParameters[lhs] = value;
boost::to_upper(lhs);
m_parameters[lhs] = value;
......@@ -1233,7 +1244,7 @@ namespace Nektar
parameter = parameter->NextSiblingElement();
}
/*
try
{
// Set ourselves up for evaluation later.
......@@ -1246,6 +1257,7 @@ namespace Nektar
// Attempted to set parameters more than once, but we let
// this go.
}
*/
}
if (m_verbose && m_parameters.size() > 0)
......@@ -1589,7 +1601,7 @@ namespace Nektar
SubstituteExpressions(fcnStr);
// set expression
funcDef.m_expression = MemoryManager<Equation>::AllocateSharedPtr(fcnStr);
funcDef.m_expression = MemoryManager<Equation>::AllocateSharedPtr(GetSharedThisPtr(),fcnStr);
}
// Files are denoted by F
......
......@@ -40,7 +40,8 @@
#include <string>
#include <LibUtilities/Communication/Comm.h>
#include <LibUtilities/BasicUtils/Equation.h>
//#include <LibUtilities/BasicUtils/Equation.h>
#include <LibUtilities/Interpreter/AnalyticExpressionEvaluator.hpp>
#include <LibUtilities/BasicConst/NektarUnivTypeDefs.hpp>
#include <LibUtilities/LibUtilitiesDeclspec.h>
......@@ -86,6 +87,10 @@ namespace Nektar
"Expression",
"File"
};
class Equation;
typedef boost::shared_ptr<Equation> EquationSharedPtr;
struct FunctionVariableDefinition
{
enum FunctionType m_type;
......@@ -240,6 +245,9 @@ namespace Nektar
/// Returns the filename to be loaded for a given variable index.
LIB_UTILITIES_EXPORT std::string GetFunctionFilename(const std::string& name, const unsigned int &var) const;
/// Returns the instance of AnalyticExpressionEvaluator specific to this instance of Session.
LIB_UTILITIES_EXPORT AnalyticExpressionEvaluator& GetExpressionEvaluator();
/* ------ TAGS ------ */
/// Checks if a specified tag is defined.
LIB_UTILITIES_EXPORT bool DefinesTag(const std::string& pName) const;
......@@ -282,6 +290,9 @@ namespace Nektar
GeometricInfoMap m_geometricInfo;
/// Expressions.
ExpressionMap m_expressions;
AnalyticExpressionEvaluator m_exprEvaluator;
/// Functions.
FunctionMap m_functions;
/// Variables.
......
......@@ -84,9 +84,6 @@ namespace Nektar
namespace LibUtilities
{
// class AnalyticExpressionEvaluator;
// typedef boost::shared_ptr<AnalyticExpressionEvaluator> AnalyticExpressionEvaluatorSharedPtr;
/// This class defines evaluator of analytic (symbolic)
/// mathematical expressions. Expressions are allowed to
......
......@@ -98,7 +98,7 @@ namespace Nektar
m_sessionName = m_session->GetSessionName();
/// Read the geometry and the expansion information
m_graph = SpatialDomains::MeshGraph::Read(m_filename);
m_graph = SpatialDomains::MeshGraph::Read(m_session);
m_UseContCoeff = false;
......@@ -584,7 +584,7 @@ namespace Nektar
// Read in spatial data
int nq = m_fields[0]->GetNpoints();
m_spatialParameters = MemoryManager<SpatialDomains::SpatialParameters>
::AllocateSharedPtr(nq);
::AllocateSharedPtr(m_session, nq);
m_spatialParameters->Read(m_filename);
Array<OneD, NekDouble> x(nq), y(nq), z(nq);
......
......@@ -200,7 +200,7 @@ namespace Nektar
for (std::vector<std::string>::iterator varIter = vars.begin();
varIter != vars.end(); ++varIter)
{
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr("00.0"));
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr(m_session,"00.0"));
(*boundaryConditions)[*varIter] = neumannCondition;
}
}
......@@ -254,13 +254,13 @@ namespace Nektar
}
attr = attr->Next();
}
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr(equation, userDefined, filename));
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr(m_session, equation, userDefined, filename));
(*boundaryConditions)[*iter] = neumannCondition;
}
else
{
// This variable's condition is zero.
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr("0"));
BoundaryConditionShPtr neumannCondition(MemoryManager<NeumannBoundaryCondition>::AllocateSharedPtr(m_session, "0"));
(*boundaryConditions)[*iter] = neumannCondition;
}
}
......@@ -273,7 +273,7 @@ namespace Nektar
for (std::vector<std::string>::iterator varIter = vars.begin();
varIter != vars.end(); ++varIter)
{
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr("0"));
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr(m_session, "0"));
(*boundaryConditions)[*varIter] = dirichletCondition;
}
}
......@@ -325,13 +325,13 @@ namespace Nektar
attr = attr->Next();
}
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr(equation, userDefined, filename));
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr(m_session, equation, userDefined, filename));
(*boundaryConditions)[*iter] = dirichletCondition;
}
else
{
// This variable's condition is zero.
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr("0"));
BoundaryConditionShPtr dirichletCondition(MemoryManager<DirichletBoundaryCondition>::AllocateSharedPtr(m_session, "0"));
(*boundaryConditions)[*iter] = dirichletCondition;
}
}
......@@ -344,7 +344,7 @@ namespace Nektar
for (std::vector<std::string>::iterator varIter = vars.begin();
varIter != vars.end(); ++varIter)
{
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr("0", "0"));
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr(m_session, "0", "0"));
(*boundaryConditions)[*varIter] = robinCondition;
}
}
......@@ -417,13 +417,13 @@ namespace Nektar
}
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr(equation1, equation2, userDefined, filename));
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr(m_session, equation1, equation2, userDefined, filename));
(*boundaryConditions)[*iter] = robinCondition;
}
else
{
// This variable's condition is zero.
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr("0", "0"));
BoundaryConditionShPtr robinCondition(MemoryManager<RobinBoundaryCondition>::AllocateSharedPtr(m_session, "0", "0"));
(*boundaryConditions)[*iter] = robinCondition;
}
}
......
......@@ -187,11 +187,12 @@ namespace Nektar
{
DirichletBoundaryCondition(
const LibUtilities::SessionReaderSharedPtr &pSession,
const std::string& eqn,
const std::string& userDefined = std::string("NoUserDefined"),
const std::string& filename=std::string("")):
BoundaryConditionBase(eDirichlet, userDefined),
m_dirichletCondition(eqn),
m_dirichletCondition(pSession, eqn),
m_filename(filename)
{
}
......@@ -203,11 +204,12 @@ namespace Nektar
struct NeumannBoundaryCondition : public BoundaryConditionBase
{
NeumannBoundaryCondition(
const LibUtilities::SessionReaderSharedPtr &pSession,
const std::string& eqn,
const std::string& userDefined = std::string("NoUserDefined"),
const std::string& filename=std::string("")):
BoundaryConditionBase(eNeumann, userDefined),
m_neumannCondition(eqn),
m_neumannCondition(pSession, eqn),
m_filename(filename)
{
}
......@@ -219,13 +221,14 @@ namespace Nektar
struct RobinBoundaryCondition : public BoundaryConditionBase
{
RobinBoundaryCondition(
const LibUtilities::SessionReaderSharedPtr &pSession,
const std::string &a,
const std::string &b,
const std::string &userDefined = std::string("NoUserDefined"),
const std::string& filename=std::string("")):
BoundaryConditionBase(eRobin, userDefined),
m_robinFunction(a),
m_robinPrimitiveCoeff(b),
m_robinFunction(pSession, a),
m_robinPrimitiveCoeff(pSession, b),
m_filename(filename)
{
}
......
......@@ -101,6 +101,7 @@ namespace Nektar
}
/**
*
*/
......@@ -112,6 +113,67 @@ namespace Nektar
/**
*
*/
boost::shared_ptr<MeshGraph> MeshGraph::Read(
const LibUtilities::SessionReaderSharedPtr &pSession)
{
boost::shared_ptr<MeshGraph> returnval;
// read the geometry tag to get the dimension
TiXmlElement* geometry_tag = pSession->GetElement("NEKTAR/GEOMETRY");
TiXmlAttribute *attr = geometry_tag->FirstAttribute();
int meshDim = 0;
int err = 0;
while (attr)
{
std::string attrName(attr->Name());
if (attrName == "DIM")
{
err = attr->QueryIntValue(&meshDim);
ASSERTL1(err==TIXML_SUCCESS, "Unable to read mesh dimension.");
break;
}
else
{
std::string errstr("Unknown attribute: ");
errstr += attrName;
ASSERTL1(false, errstr.c_str());
}
// Get the next attribute.
attr = attr->Next();
}
// instantiate the dimension-specific meshgraph classes
switch(meshDim)
{
case 1:
returnval = MemoryManager<MeshGraph1D>::AllocateSharedPtr(pSession);
break;
case 2:
returnval = MemoryManager<MeshGraph2D>::AllocateSharedPtr(pSession);
break;
case 3:
returnval = MemoryManager<MeshGraph3D>::AllocateSharedPtr(pSession);
break;
default:
std::string err = "Invalid mesh dimension: ";
std::stringstream strstrm;
strstrm << meshDim;
err += strstrm.str();
NEKERROR(ErrorUtil::efatal, err.c_str());
}
return returnval;
}
/* ==== OUTDATED ROUTINE, PLEASE NOT USE ==== */
boost::shared_ptr<MeshGraph> MeshGraph::Read(
const std::string& infilename,
bool pReadExpansions)
......@@ -158,6 +220,7 @@ namespace Nektar
}
/**
*
*/
......@@ -564,9 +627,16 @@ namespace Nektar
ASSERTL0(nStr,"NUMMODES was not defined in EXPANSION section of input");
std::string nummodesStr = nStr;
LibUtilities::Equation nummodesEqn(nummodesStr);
num_modes = (int) nummodesEqn.Evaluate();
// ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
if (m_session)
{
LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
num_modes = (int) nummodesEqn.Evaluate();
}
else
{
num_modes = boost::lexical_cast<int>(nummodesStr);
}
useExpansionType = true;
}
......@@ -772,9 +842,18 @@ namespace Nektar
ASSERTL0(nStr,"NUMMODES-X was not defined in EXPANSION section of input");
std::string nummodesStr = nStr;
LibUtilities::Equation nummodesEqn(nummodesStr);
// ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
if (m_session)
{
LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
num_modes_x = (int) nummodesEqn.Evaluate();
}
else
{
num_modes_x = boost::lexical_cast<int>(nummodesStr);
}
num_modes_x = (int) nummodesEqn.Evaluate();
}
const char * tStr_y = expansion->Attribute("TYPE-Y");
......@@ -793,9 +872,17 @@ namespace Nektar
ASSERTL0(nStr,"NUMMODES-Y was not defined in EXPANSION section of input");
std::string nummodesStr = nStr;
LibUtilities::Equation nummodesEqn(nummodesStr);
// ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
if (m_session)
{
LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
num_modes_y = (int) nummodesEqn.Evaluate();
}
else
{
num_modes_y = boost::lexical_cast<int>(nummodesStr);
}
num_modes_y = (int) nummodesEqn.Evaluate();
}
const char * tStr_z = expansion->Attribute("TYPE-Z");
......@@ -814,9 +901,17 @@ namespace Nektar
ASSERTL0(nStr,"NUMMODES-Z was not defined in EXPANSION section of input");
std::string nummodesStr = nStr;
LibUtilities::Equation nummodesEqn(nummodesStr);
// ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
if (m_session)
{
LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
num_modes_z = (int) nummodesEqn.Evaluate();
}
else
{
num_modes_z = boost::lexical_cast<int>(nummodesStr);
}
num_modes_z = (int) nummodesEqn.Evaluate();
}
CompositeMapIter compVecIter;
......
......@@ -235,10 +235,15 @@ namespace Nektar
/* ---- Mesh Reading routines ---- */
SPATIAL_DOMAINS_EXPORT static boost::shared_ptr<MeshGraph> Read(
const LibUtilities::SessionReaderSharedPtr &pSession);
/// \todo Remove updated routine
SPATIAL_DOMAINS_EXPORT static boost::shared_ptr<MeshGraph> Read(
const std::string& infilename,
bool pReadExpansions = true);
/// Read will read the meshgraph vertices given a filename.
SPATIAL_DOMAINS_EXPORT virtual void ReadGeometry(
const std::string& infilename);
......
......@@ -46,7 +46,8 @@ namespace Nektar
/**
* Private default constructor.
*/
SpatialParameters::SpatialParameters()
SpatialParameters::SpatialParameters():
m_session()
{
}
......@@ -57,11 +58,12 @@ namespace Nektar
* points.
* @param nq Number of data points in the domain.
*/
SpatialParameters::SpatialParameters(const int nq) :
SpatialParameters::SpatialParameters(const LibUtilities::SessionReaderSharedPtr& pSession, const int nq) :
m_nq(nq),
m_spatialMap(),
m_constMap(),
m_analyticMap()
m_analyticMap(),
m_session(pSession)
{
}
......@@ -75,7 +77,8 @@ namespace Nektar
m_nq(src.m_nq),
m_spatialMap(),
m_constMap(src.m_constMap),
m_analyticMap(src.m_analyticMap)
m_analyticMap(src.m_analyticMap),
m_session(src.m_session)
{
SpatialDataMap::const_iterator x;
for (x = src.m_spatialMap.begin(); x != src.m_spatialMap.end(); ++x)
......@@ -227,7 +230,7 @@ namespace Nektar
{
SpatialDataSharedPtr fn(MemoryManager<SpatialData>
::AllocateSharedPtr(m_nq));
LibUtilities::Equation E(q->second);
LibUtilities::Equation E(m_session, q->second);
E.Evaluate(x, y, z, fn->UpdatePhys());
m_spatialMap[q->first] = fn;
......
......@@ -93,7 +93,7 @@ namespace Nektar
{
public:
/// Define a new set of spatially-dependent parameters.
SPATIAL_DOMAINS_EXPORT SpatialParameters(const int nq);
SPATIAL_DOMAINS_EXPORT SpatialParameters(const LibUtilities::SessionReaderSharedPtr& pSession, const int nq);
/// Copies an existing set of spatially-dependent parameters.
SPATIAL_DOMAINS_EXPORT SpatialParameters(const SpatialParameters& src);
......@@ -127,6 +127,10 @@ namespace Nektar
/// List of analytic parameter definitions.
std::map<std::string, std::string> m_analyticMap;
/// Shared pointer to the current session
const LibUtilities::SessionReaderSharedPtr& m_session;
/// Default constructor.
SpatialParameters();
......
......@@ -49,8 +49,6 @@ int main(int argc, char *argv[])
string vDriverModule;
DriverSharedPtr drv;
LibUtilities::Equation eq("");
try
{
// Create session reader.
......@@ -75,7 +73,5 @@ int main(int argc, char *argv[])
cout << "Error: " << eStr << endl;
}
std::cout << "Time spend evaluating: " << eq.GetTime() << std::endl;
return 0;
}
......@@ -87,7 +87,7 @@ namespace Nektar
m_sessionName = m_sessionName.substr(0, m_sessionName.find_last_of("."));
// Read the geometry and the expansion information
m_graph = SpatialDomains::MeshGraph::Read(m_filename);
m_graph = SpatialDomains::MeshGraph::Read(m_session);
m_domainsize = m_graph->GetDomain().size();
m_UseContCoeff = false;
......@@ -272,7 +272,7 @@ namespace Nektar
// Read in spatial data
int nq = m_fields[0]->GetNpoints();
m_spatialParameters = MemoryManager<SpatialDomains::SpatialParameters>::AllocateSharedPtr(nq);
m_spatialParameters = MemoryManager<SpatialDomains::SpatialParameters>::AllocateSharedPtr(m_session, nq);
m_spatialParameters->Read(m_filename);
Array<OneD, NekDouble> x(nq), y(nq), z(nq);
......
......@@ -65,7 +65,7 @@ int main(int argc, char *argv[])
//-------------------------------------------------------------
// Read in mesh from input file
string meshfile(argv[argc-2]);
SpatialDomains::MeshGraphSharedPtr graphShPt = SpatialDomains::MeshGraph::Read(meshfile);
SpatialDomains::MeshGraphSharedPtr graphShPt = SpatialDomains::MeshGraph::Read(vSession);//meshfile);
//------------------------------------------------------------
//-------------------------------------------------------------
......