GemaCoreLib
The GeMA Core library
gmUserFunction.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
23 #ifndef _GEMA_USER_FUNCTION_H_
24 #define _GEMA_USER_FUNCTION_H_
25 
26 #include "gmCoreConfig.h"
27 #include "gmValueInfo.h"
28 #include "gmCell.h"
29 #include "gmInterpolator.h"
30 #include "gmThreadLocalBuffer.h"
31 
32 #include <QString>
33 #include <unit.h>
34 
35 class GmMesh;
36 class GmSimulationData;
37 class GmLogCategory;
38 
39 class GmValueAccessor;
40 class GmCellAccessor;
41 class GmGaussAccessor;
42 
45 
46 class LuaFunction;
47 class LuaEnv;
48 
51 {
54 };
55 
57 
75 typedef bool (GmPhysicsUserFunctions::*GmCUserMethod)(const GmUserFunctionContext* context, double* out);
76 
79 {
80 public:
81 
84  {
86  GmUserFunctionParameter() : _dim(-1), _history(0) {}
87 
89  GmUserFunctionParameter(QString src, Unit u, int dim, int history, GmInterpolatorType interpType, QVariant interpParam)
90  : _src(src), _unit(u), _dim(dim), _history(history), _interpType(interpType), _interpParam(interpParam) {}
91 
94  int _dim;
95  int _history;
98  };
99 
101  ~GmUserFunction();
102 
104  QString id() const { return _id; }
105 
107  GmUserFunctionType type() const { return _type; }
108 
110  const QList<GmUserFunctionParameter>& parameterList() const { return _parameters; }
111 
113  bool isCFunction() const { return _cFuncFactory; }
114 
116  LuaFunction* luaFunction() const { return _luaFunction ? _luaFunction->localData() : NULL; }
117 
119  QString cFunctionName() const { return _cFuncName; }
120 
122  QString cFunctionPlugin() const { return _cFuncPlugin; }
123 
128  GmPluginObjectFactory* cFunctionFactory() const { return _cFuncFactory; }
129 
130  void addParameter(QString src, Unit unit, int dim = -1, int history = -1,
131  GmInterpolatorType interpType = GM_NO_INTERPOLATOR, QVariant interpParam = QVariant());
132 
133  void setLuaMethod(int tid, LuaFunction* func);
134  void setCMethod (QString pluginName, QString methodName, GmPluginObjectFactory* factory);
135 
136 private:
137 
145 };
146 
149 {
150 public:
153 
154  bool setEvaluationContext(const GmValueInfo* resultInfo, GmSimulationData* simData,
155  const GmMesh* mesh, const GmLogCategory& logger, QString& err);
156 
157  const double* eval(int index, const GmVector* coord, int ip, QString& err) const;
158 
160  const GmUserFunction* userFunction() const { return _functionInfo; }
161 
162 private:
163 
166  {
176  };
177 
178  const double* callFunction(int index, const GmVector* coord, int npars, LuaEnv* env, QString& err) const;
179 
180  bool loadCFunction(GmSimulationData* simData, const GmMesh* mesh, QString& err);
181  bool parseParameterType(QString parName, ParamContextType* parType, GmValueInfo** info, QString& err);
182 
183  bool acceptsInterpolatorType(const GmCell* c, GmInterpolatorType type) const;
184 
185  friend class GmUserFunctionContext; // Allows the function context to access the parameter buffer and metadata
186 
190  const GmMesh* _mesh;
194 
197  {
198  public:
199  ParamContext();
200  ~ParamContext();
201 
202  void setContext(ParamContextType type, void* ac, int size, int dim, bool nodeBased,
203  GmInterpolatorType interpType, QVariant interpParam, Unit timeUnit);
204 
205  void evalParameter(int index, const GmVector* coord, int ip, const GmValueAccessor* coordAccessor,
206  const GmSimulationData* simData, const GmMesh* mesh, double* result, bool warnOnInvalidIndex, QString& err) const;
207 
208  void pushParameter(int index, const GmVector* coord, int ip, const GmValueAccessor* coordAccessor,
209  const GmSimulationData* simData, const GmMesh* mesh, LuaEnv* env, bool warnOnInvalidIndex, QString& err) const;
210 
211  GmValueInfo* info() const;
212 
214  void* _accessor;
215  int _dim;
216  int _size;
217  bool _nodeBased;
221  };
222 
225 
229 
232 };
233 
236 {
237 public:
239  int id() const { return _id; }
240 
242  const GmMesh* mesh() const { return _eval->_mesh; }
243 
249  const GmVector* coord() const { return _coord; }
250 
252  const GmUserFunction* functionInfo() const { return _eval->_functionInfo; }
253 
255  const GmValueInfo* resultInfo() const { return _eval->_resultInfo; }
256 
258  int resultSize() const { return _eval->_resultInfo->size(); }
259 
261  int numPars() const { return _eval->_functionInfo->parameterList().size(); }
262 
271  const double* par(int i) const { assert(i >= 0 && i < numPars()); return _eval->_input->localData()[i]; }
272 
274  double scalarPar(int i) const { assert(i >= 0 && i < numPars()); return *_eval->_input->localData()[i]; }
275 
277  int parSize(int i) const { assert(i >= 0 && i < numPars()); return _eval->_parInfo[i]._size; }
278 
279 private:
280  friend class GmUserFunctionEvaluator;
281 
284 
286  void setId(int index) { _id = index; }
287 
292  void setCoord(const GmVector* coord) { _coord = coord; }
293 
294  int _id;
295  const GmVector* _coord;
297 };
298 
299 
300 #endif
301 
302 
QVariant _interpParam
The optional parameters of the interpolation.
Definition: gmUserFunction.h:220
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition: gmValueInfo.h:126
The parameter should return the current time.
Definition: gmUserFunction.h:174
const double * par(int i) const
Returns the it'h input parameter as a vector of doubles.
Definition: gmUserFunction.h:271
The parameter should return the mesh name (Lua functions only)
Definition: gmUserFunction.h:173
void * _accessor
The accessor object used to retrieve the parameter value. Can be NULL, a value, cell or gauss accesso...
Definition: gmUserFunction.h:214
QString _src
The value source for this parameter.
Definition: gmUserFunction.h:92
int numPars() const
Returns the number of input parameters.
Definition: gmUserFunction.h:261
int _ipRuleSet
The integration rule set used by gauss attribute parameters or -1 if there is no such parameter type.
Definition: gmUserFunction.h:224
GmPluginObjectFactory * cFunctionFactory() const
Returns the stored C function factory (that can be used to instance a new GmPhysicsUserFunctions obje...
Definition: gmUserFunction.h:128
bool _nodeBased
Set to true if this is a node based parameter evaluated for a cell.
Definition: gmUserFunction.h:217
Means that the function calculates cell values (attributes or properties)
Definition: gmUserFunction.h:53
int _size
The number of values associated with this parameter (depends on the value returned by the accessor an...
Definition: gmUserFunction.h:216
double scalarPar(int i) const
Returns the it'h input parameter as a single double.
Definition: gmUserFunction.h:274
const GmMesh * _mesh
The mesh that will supply function parameters.
Definition: gmUserFunction.h:190
Unit _unit
The expected unit for the value.
Definition: gmUserFunction.h:93
int parSize(int i) const
Returns the size of the i'th parameter.
Definition: gmUserFunction.h:277
Class responsible for evaluating a UserFunction over a node / cell.
Definition: gmUserFunction.h:148
~ParamContext()
Destructor.
Definition: gmUserFunction.cpp:847
GmInterpolatorType _interpType
The interpolator type used for aggregating node values (if needed) or for interpolating Gauss values ...
Definition: gmUserFunction.h:219
The GmGaussAccessor class is a proxy object to a value accesor implementing a more convenient interfa...
Definition: gmGaussAccessor.h:38
const GmMesh * mesh() const
Returns the mesh that holds the attribute being evaluated.
Definition: gmUserFunction.h:242
ParamContextType _type
The parameter type.
Definition: gmUserFunction.h:213
Declaration of usefull configuration definitions for the Core library.
QString _id
Unique id for this value function.
Definition: gmUserFunction.h:138
bool(GmPhysicsUserFunctions::* GmCUserMethod)(const GmUserFunctionContext *context, double *out)
A pointer to a C function exported from a physics plugin to evaluate the value of some attribute / pr...
Definition: gmUserFunction.h:75
Factory class exported by every plugin object.
Definition: gmPluginObjectFactory.h:36
bool loadCFunction(GmSimulationData *simData, const GmMesh *mesh, QString &err)
Load internal parameters needed to call a C function.
Definition: gmUserFunction.cpp:711
Declaration of the GmValueInfo class.
const GmUserFunction * _functionInfo
Information about the function that will be evaluated.
Definition: gmUserFunction.h:188
Base interface for mesh cells.
Definition: gmCell.h:81
GmInterpolatorType _interpType
How do we interpolate node attributes for element functions?
Definition: gmUserFunction.h:96
GmUserFunctionType _type
The type of function stored.
Definition: gmUserFunction.h:139
The parameter is the result of a gauss attribute accessor.
Definition: gmUserFunction.h:169
void evalParameter(int index, const GmVector *coord, int ip, const GmValueAccessor *coordAccessor, const GmSimulationData *simData, const GmMesh *mesh, double *result, bool warnOnInvalidIndex, QString &err) const
Evaluates the parameter at the position index and fills result with the values using the configured a...
Definition: gmUserFunction.cpp:901
Declaration of the GmTLBuffer class.
QList< GmUserFunctionParameter > _parameters
The list of function parameters.
Definition: gmUserFunction.h:140
Unit _timeUnit
The unit for recovering time for type == CURRENT_TIME/DELTA_TIME_PARAM.
Definition: gmUserFunction.h:218
~GmUserFunctionEvaluator()
Destructor.
Definition: gmUserFunction.cpp:267
ParamContext()
Default constructor.
Definition: gmUserFunction.cpp:834
const GmVector * _coord
Point of aggregation when node attributes are evaluated on behalf of a cell.
Definition: gmUserFunction.h:295
bool _warnOnInvalidIndex
Should we emit the invalid index warning?
Definition: gmUserFunction.h:193
Interface class for accessing and setting values from an "indexable" collection of values.
Definition: gmValueAccessor.h:59
Auxiliar class storing the information needed to collect a parameter value.
Definition: gmUserFunction.h:196
const GmValueInfo * _resultInfo
Information about the expected function result.
Definition: gmUserFunction.h:189
const QList< GmUserFunctionParameter > & parameterList() const
Returns the list of parameters passed to the function.
Definition: gmUserFunction.h:110
Auxiliar class used to store the complete set of simulation data.
Definition: gmSimulationData.h:51
Base interface class for Physics type plugins to export user functions that can be used to provide at...
Definition: gmPhysics.h:158
void setId(int index)
Set the current node / cell id.
Definition: gmUserFunction.h:286
const double * eval(int index, const GmVector *coord, int ip, QString &err) const
Evaluates the function to retrieve data from the node/cell identified by its index.
Definition: gmUserFunction.cpp:590
GmTLS< double ** > * _input
Per thread local buffer used to hold input parameters for C functions.
Definition: gmUserFunction.h:230
const GmValueInfo * resultInfo() const
Information about the returned result.
Definition: gmUserFunction.h:255
int _id
The attribute / cell id.
Definition: gmUserFunction.h:294
ParamContextType
Auxiliar enum to store the type of paramaeter stored in ParamContext.
Definition: gmUserFunction.h:165
GmTLS< LuaFunction * > * _luaFunction
The lua function to be called or NULL for C functions. Has a per thread value with a function per Lua...
Definition: gmUserFunction.h:141
const GmVector * coord() const
Returns the evaluation point used when aggregating node attributes for a cell.
Definition: gmUserFunction.h:249
The parameter is the result of a cell attribute / property accessor.
Definition: gmUserFunction.h:168
No interpolation needed. Not valid to construct an interpolator object but usefull when defining user...
Definition: gmInterpolator.h:132
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition: gmThreadLocalStorage.h:163
void setContext(ParamContextType type, void *ac, int size, int dim, bool nodeBased, GmInterpolatorType interpType, QVariant interpParam, Unit timeUnit)
Sets the parameter context. Takes ownership of the received accessor (NULL for standard parameters th...
Definition: gmUserFunction.cpp:863
const GmUserFunction * userFunction() const
Returns the user function associated with this evaluator.
Definition: gmUserFunction.h:160
bool acceptsInterpolatorType(const GmCell *c, GmInterpolatorType type) const
Checks if the given interpolator type is acceptable for the real cell object type.
Definition: gmUserFunction.cpp:816
int id() const
Returns the id of the node / cell that has an attribute / property beeing evaluated.
Definition: gmUserFunction.h:239
GmInterpolatorType
Available interpolation methods. Not every method is suitable for every kind of interpolator class....
Definition: gmInterpolator.h:62
GmPhysicsUserFunctions * _cObj
The object that contains the C method or NULL for Lua functions.
Definition: gmUserFunction.h:226
int _dim
A "bound" dimension for returning a scalar when _src is multidimensional (or -1 instead)
Definition: gmUserFunction.h:94
GmUserFunctionParameter(QString src, Unit u, int dim, int history, GmInterpolatorType interpType, QVariant interpParam)
Basic constructor.
Definition: gmUserFunction.h:89
int _dim
The access index for vectors/matrices. May be -1 for complete vector/matrix.
Definition: gmUserFunction.h:215
The GmCellAccessor class is a proxy object to a value accesor implementing a more convenient interfac...
Definition: gmCellAccessor.h:66
QString _cFuncName
The name of the C function to be called (or empty for Lua functions)
Definition: gmUserFunction.h:142
const double * callFunction(int index, const GmVector *coord, int npars, LuaEnv *env, QString &err) const
Calls the user function (in Lua or C). Expects parameters to be in place (Lua stack or _input vector)
Definition: gmUserFunction.cpp:644
GmValueInfo * info() const
Returns the info object tied to the parameter accessor.
Definition: gmUserFunction.cpp:884
void setCoord(const GmVector *coord)
Set the evaluation point used when aggregating node attributes for a cell. The coordinate values depe...
Definition: gmUserFunction.h:292
GmUserFunctionType type() const
Returns the type of function stored inside ths object.
Definition: gmUserFunction.h:107
LuaFunction * luaFunction() const
Returns the stored Lua function (associated with the current thread) or NULL for C functions.
Definition: gmUserFunction.h:116
GmCUserMethod _cMethod
The C method or NULL for Lua functions.
Definition: gmUserFunction.h:227
The parameter should return the integration point natural coordinates.
Definition: gmUserFunction.h:171
Means that the function calculates nodal values (attributes)
Definition: gmUserFunction.h:52
The parameter should return the node / cell Id.
Definition: gmUserFunction.h:172
GmTLBuffer< double, true > _result
Per thread local buffer used to store function results.
Definition: gmUserFunction.h:231
QString _cFuncPlugin
The name of the plugin that contains _cFuncName (or empty for Lua functions)
Definition: gmUserFunction.h:143
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
bool _evalNeedsCoord
Set to true if the function evaluation needs user coordinates.
Definition: gmUserFunction.h:192
QString cFunctionName() const
Returns the name of the stored C function or an empty string for Lua functions.
Definition: gmUserFunction.h:119
int resultSize() const
Returns the size of the returned value.
Definition: gmUserFunction.h:258
GmTLS< GmUserFunctionContext * > * _cContext
Per thread context objects passed to C methods or NULL for Lua functions.
Definition: gmUserFunction.h:228
Auxiliar structure for storing function parameter information.
Definition: gmUserFunction.h:83
int size() const
Returns the number of values (doubles) associated to this definition.
Definition: gmValueInfo.h:168
QVariant _interpParam
Optional interpolation parameters.
Definition: gmUserFunction.h:97
Class representing a category with multiple logging levels.
Definition: gmLog.h:58
const GmSimulationData * _simData
The simulation data object used for retrieving time information.
Definition: gmUserFunction.h:187
The parameter is the result of a node attribute / state var / coordinate accessor.
Definition: gmUserFunction.h:167
The parameter should return the difference from the current time to the last one.
Definition: gmUserFunction.h:175
GmUserFunctionEvaluator * _eval
The evaluation context used to retrieve remaining information provided by the context.
Definition: gmUserFunction.h:296
GmUserFunctionParameter()
Default constructor. Needed to allow storing objects in lists.
Definition: gmUserFunction.h:86
QString id() const
Returns the object id.
Definition: gmUserFunction.h:104
int _history
The state accessed by this parameter (0 = current state, 1 = previous, etc..)
Definition: gmUserFunction.h:95
ParamContext * _parInfo
A vector with a parameter info for each function parameter.
Definition: gmUserFunction.h:223
The context received by C methods giving access to parameters data and metadata.
Definition: gmUserFunction.h:235
GmUserFunctionType
Type of a user function.
Definition: gmUserFunction.h:50
bool setEvaluationContext(const GmValueInfo *resultInfo, GmSimulationData *simData, const GmMesh *mesh, const GmLogCategory &logger, QString &err)
Prepares the object to enable function evaluations.
Definition: gmUserFunction.cpp:315
bool isCFunction() const
Returns true if the stored function is a C function.
Definition: gmUserFunction.h:113
Declaration of the GmCell class.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
void pushParameter(int index, const GmVector *coord, int ip, const GmValueAccessor *coordAccessor, const GmSimulationData *simData, const GmMesh *mesh, LuaEnv *env, bool warnOnInvalidIndex, QString &err) const
Evaluates the parameter at the position index and pushes the values in the Lua stack using the config...
Definition: gmUserFunction.cpp:1037
GmUserFunctionContext(GmUserFunctionEvaluator *eval)
Private constructor.
Definition: gmUserFunction.h:283
bool parseParameterType(QString parName, ParamContextType *parType, GmValueInfo **info, QString &err)
Fills parType and info from the parameter name. On errors, fills err and returns false.
Definition: gmUserFunction.cpp:749
Base interface class for Mesh type plugins.
Definition: gmMesh.h:44
QString cFunctionPlugin() const
Returns the name of the stored C function plugin or an empty string for Lua functions.
Definition: gmUserFunction.h:122
const GmValueAccessor * _coordAccessor
An accessor to read coordinate values needed when interpolating values over cells.
Definition: gmUserFunction.h:191
Class used to store the definition of a user function and its parameters.
Definition: gmUserFunction.h:78
The parameter should return the integration point cartesian coordinates.
Definition: gmUserFunction.h:170
GmPluginObjectFactory * _cFuncFactory
The factory used to instance a GmPhysicsUserFunctions if needed (or NULL for Lua functions)
Definition: gmUserFunction.h:144
Declaration of the GmInterpolator and GmInterpolatorObject classes.
GmUserFunctionEvaluator(const GmUserFunction *ufInfo)
Constructor. Gets as parameter the user function definition.
Definition: gmUserFunction.cpp:246
const GmUserFunction * functionInfo() const
Information about the user function parameters.
Definition: gmUserFunction.h:252