GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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#include "gmAggregator.h"
32
33#include <QString>
34#include <unit.h>
35
36class GmMesh;
38class GmLogCategory;
39
40class GmValueAccessor;
41class GmCellAccessor;
42class GmGaussAccessor;
43
46
48
49class LuaFunction;
50class LuaEnv;
51
58
60
78typedef bool (GmPhysicsUserFunctions::*GmCUserMethod)(const GmUserFunctionContext* context, double* out);
79
82{
83public:
84
87 {
89 GmUserFunctionParameter() : _dim(-1), _history(0) {}
90
92 GmUserFunctionParameter(QString src, Unit u, int dim, int history, GmInterpolatorType interpType, QVariant interpParam, GmAggregationType aggType)
93 : _src(src), _unit(u), _dim(dim), _history(history), _interpType(interpType), _interpParam(interpParam), _aggType(aggType) {}
94
97 int _dim;
101 GmAggregationType _aggType;
102 };
103
106
108 QString id() const { return _id; }
109
111 GmUserFunctionType type() const { return _type; }
112
113 QString discSetName() const { return _dsName; }
114
115 const GmDiscontinuitySet* discSet() const { return _discSet; }
116
118 const QVector<GmUserFunctionParameter>& parameterList() const { return _parameters; }
119
121 bool isCFunction() const { return _cFuncFactory; }
122
124 LuaFunction* luaFunction() const { return _luaFunction ? _luaFunction->localData() : NULL; }
125
127 QString cFunctionName() const { return _cFuncName; }
128
130 QString cFunctionPlugin() const { return _cFuncPlugin; }
131
136 GmPluginObjectFactory* cFunctionFactory() const { return _cFuncFactory; }
137
138 void addParameter(QString src, Unit unit, int dim = -1, int history = -1,
139 GmInterpolatorType interpType = GM_NO_INTERPOLATOR, QVariant interpParam = QVariant(), GmAggregationType aggType = GM_NO_AGGREGATION);
140
141 void setLuaMethod(int tid, LuaFunction* func);
142 void setCMethod (QString pluginName, QString methodName, GmPluginObjectFactory* factory);
143 void setDiscSet(GmDiscontinuitySet* discSet);
144
145private:
146
151 QString _dsName; //DiscontinuitySet name
152 GmDiscontinuitySet* _discSet;
156};
157
160{
161public:
164
165 bool setEvaluationContext(const GmValueInfo* resultInfo, GmSimulationData* simData,
166 const GmMesh* mesh, const GmLogCategory& logger, QString& err);
167
168 const double* eval(int index, const GmVector* coord, int ip, QString& err) const;
169
171 const GmUserFunction* userFunction() const { return _functionInfo; }
172
173private:
174
189
190 const double* callFunction(int index, const GmVector* coord, int npars, LuaEnv* env, QString& err) const;
191
192 bool loadCFunction(GmSimulationData* simData, const GmMesh* mesh, QString& err);
193 bool parseParameterType(QString parName, ParamContextType* parType, GmValueInfo** info, QString& err);
194
195 bool acceptsInterpolatorType(const GmCell* c, GmInterpolatorType type) const;
196
197 friend class GmUserFunctionContext; // Allows the function context to access the parameter buffer and metadata
198
202 const GmMesh* _mesh;
207
210 {
211 public:
212 ParamContext();
214
215 void setContext(ParamContextType type, void* ac, int size, int dim, bool nodeBased,
216 GmInterpolatorType interpType, QVariant interpParam, GmAggregationType aggType, Unit timeUnit);
217
218 void evalParameter(int index, const GmVector* coord, int ip, const GmValueAccessor* coordAccessor,
219 const GmSimulationData* simData, const GmMesh* mesh, const GmDiscontinuitySet* discSet,
220 double* result, bool warnOnInvalidIndex, QString& err) const;
221
222 void pushParameter(int index, const GmVector* coord, int ip, const GmValueAccessor* coordAccessor,
223 const GmSimulationData* simData, const GmMesh* mesh, const GmDiscontinuitySet* discSet,
224 LuaEnv* env, bool warnOnInvalidIndex, QString& err) const;
225
226 GmValueInfo* info() const;
227
229 void* _accessor;
230 int _dim;
231 int _size;
236 GmAggregationType _aggType;
237 };
238
242
246
249};
250
253{
254public:
256 int id() const { return _id; }
257
259 const GmMesh* mesh() const { return _eval->_mesh; }
260
266 const GmVector* coord() const { return _coord; }
267
269 const GmUserFunction* functionInfo() const { return _eval->_functionInfo; }
270
272 const GmValueInfo* resultInfo() const { return _eval->_resultInfo; }
273
275 int resultSize() const { return _eval->_resultInfo->size(); }
276
278 int numPars() const { return _eval->_functionInfo->parameterList().size(); }
279
288 const double* par(int i) const { assert(i >= 0 && i < numPars()); return _eval->_input->localData()[i]; }
289
291 double scalarPar(int i) const { assert(i >= 0 && i < numPars()); return *_eval->_input->localData()[i]; }
292
294 int parSize(int i) const { assert(i >= 0 && i < numPars()); return _eval->_parInfo[i]._size; }
295
296private:
298
301
303 void setId(int index) { _id = index; }
304
309 void setCoord(const GmVector* coord) { _coord = coord; }
310
311 int _id;
314};
315
316
317#endif
318
319
The GmCellAccessor class is a proxy object to a value accesor implementing a more convenient interfac...
Definition gmCellAccessor.h:67
Base interface for mesh cells.
Definition gmCell.h:88
Base interface for providing discontinuity geometry information for spatial indices.
Definition gmDiscontinuitySet.h:59
The GmGaussAccessor class is a proxy object to a value accessor implementing a more convenient interf...
Definition gmGaussAccessor.h:39
Class representing a category with multiple logging levels.
Definition gmLog.h:58
Base interface class for Mesh type plugins.
Definition gmMesh.h:48
Base interface class for Physics type plugins to export user functions that can be used to provide at...
Definition gmPhysics.h:177
Factory class exported by every plugin object.
Definition gmPluginObjectFactory.h:37
Auxiliar class used to store the complete set of simulation data.
Definition gmSimulationData.h:55
A class similar to GmTLS that creates a buffer for each possible thread in the GmThreadManager,...
Definition gmThreadLocalBuffer.h:106
A class that works together with GmThreadManager to provide thread local storage.
Definition gmThreadLocalStorage.h:132
T & localData(int tid)
Returns the given thread local data as a modifiable reference.
Definition gmThreadLocalStorage.h:163
The context received by C methods giving access to parameters data and metadata.
Definition gmUserFunction.h:253
const GmUserFunction * functionInfo() const
Information about the user function parameters.
Definition gmUserFunction.h:269
GmUserFunctionEvaluator * _eval
The evaluation context used to retrieve remaining information provided by the context.
Definition gmUserFunction.h:313
int id() const
Returns the id of the node / cell that has an attribute / property being evaluated.
Definition gmUserFunction.h:256
const GmVector * _coord
Point of aggregation when node attributes are evaluated on behalf of a cell.
Definition gmUserFunction.h:312
int parSize(int i) const
Returns the size of the i'th parameter.
Definition gmUserFunction.h:294
int _id
The attribute / cell id.
Definition gmUserFunction.h:311
GmUserFunctionContext(GmUserFunctionEvaluator *eval)
Private constructor. Id and coord are only known and updated just before a call.
Definition gmUserFunction.h:300
const GmVector * coord() const
Returns the evaluation point used when aggregating node attributes for a cell.
Definition gmUserFunction.h:266
int numPars() const
Returns the number of input parameters.
Definition gmUserFunction.h:278
const GmValueInfo * resultInfo() const
Information about the returned result.
Definition gmUserFunction.h:272
void setCoord(const GmVector *coord)
Set the evaluation point used when aggregating node attributes for a cell. The coordinate values depe...
Definition gmUserFunction.h:309
int resultSize() const
Returns the size of the returned value.
Definition gmUserFunction.h:275
const GmMesh * mesh() const
Returns the mesh that holds the attribute being evaluated.
Definition gmUserFunction.h:259
const double * par(int i) const
Returns the it'h input parameter as a vector of doubles.
Definition gmUserFunction.h:288
double scalarPar(int i) const
Returns the it'h input parameter as a single double.
Definition gmUserFunction.h:291
void setId(int index)
Set the current node / cell id.
Definition gmUserFunction.h:303
Auxiliar class storing the information needed to collect a parameter value.
Definition gmUserFunction.h:210
QVariant _interpParam
The optional parameters of the interpolation.
Definition gmUserFunction.h:235
int _size
The number of values associated with this parameter (depends on the value returned by the accessor an...
Definition gmUserFunction.h:231
GmInterpolatorType _interpType
The interpolator type used for aggregating node values (if needed) or for interpolating Gauss values ...
Definition gmUserFunction.h:234
void evalParameter(int index, const GmVector *coord, int ip, const GmValueAccessor *coordAccessor, const GmSimulationData *simData, const GmMesh *mesh, const GmDiscontinuitySet *discSet, 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:964
Unit _timeUnit
The unit for recovering time for type == CURRENT_TIME/DELTA_TIME_PARAM.
Definition gmUserFunction.h:233
GmAggregationType _aggType
The aggregation type used for aggregating node values (if needed)
Definition gmUserFunction.h:236
GmValueInfo * info() const
Returns the info object tied to the parameter accessor.
Definition gmUserFunction.cpp:943
void pushParameter(int index, const GmVector *coord, int ip, const GmValueAccessor *coordAccessor, const GmSimulationData *simData, const GmMesh *mesh, const GmDiscontinuitySet *discSet, 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:1140
int _dim
The access index for vectors/matrices. May be -1 for complete vector/matrix.
Definition gmUserFunction.h:230
ParamContext()
Default constructor.
Definition gmUserFunction.cpp:892
void * _accessor
The accessor object used to retrieve the parameter value. Can be NULL, a value, cell or gauss accesso...
Definition gmUserFunction.h:229
ParamContextType _type
The parameter type.
Definition gmUserFunction.h:228
~ParamContext()
Destructor.
Definition gmUserFunction.cpp:905
bool _nodeBased
Set to true if this is a node based parameter evaluated for a cell.
Definition gmUserFunction.h:232
void setContext(ParamContextType type, void *ac, int size, int dim, bool nodeBased, GmInterpolatorType interpType, QVariant interpParam, GmAggregationType aggType, Unit timeUnit)
Sets the parameter context. Takes ownership of the received accessor (NULL for standard parameters th...
Definition gmUserFunction.cpp:921
Class responsible for evaluating a UserFunction over a node / cell.
Definition gmUserFunction.h:160
GmTLBuffer< double, true > _result
Per thread local buffer used to store function results.
Definition gmUserFunction.h:248
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:697
GmCUserMethod _cMethod
The C method or NULL for Lua functions.
Definition gmUserFunction.h:244
GmUserFunctionEvaluator(const GmUserFunction *ufInfo)
Constructor. Gets as parameter the user function definition.
Definition gmUserFunction.cpp:256
const GmUserFunction * _functionInfo
Information about the function that will be evaluated.
Definition gmUserFunction.h:200
ParamContextType
Auxiliar enum to store the type of paramaeter stored in ParamContext.
Definition gmUserFunction.h:177
@ DELTA_TIME_PARAM
The parameter should return the difference from the current time to the last one.
Definition gmUserFunction.h:187
@ GAUSS_AC_PARAM
The parameter is the result of a gauss attribute accessor.
Definition gmUserFunction.h:180
@ CELL_TYPE_PARAM
The parameter should return the cell type (Lua functions only)
Definition gmUserFunction.h:185
@ IPOINT_PARAM
The parameter should return the integration point cartesian coordinates.
Definition gmUserFunction.h:181
@ VALUE_AC_PARAM
The parameter is the result of a node attribute / state var / coordinate accessor.
Definition gmUserFunction.h:178
@ CELL_AC_PARAM
The parameter is the result of a cell attribute / property accessor.
Definition gmUserFunction.h:179
@ NIPOINT_PARAM
The parameter should return the integration point natural coordinates.
Definition gmUserFunction.h:182
@ ID_PARAM
The parameter should return the node / cell Id.
Definition gmUserFunction.h:183
@ MESH_ID_PARAM
The parameter should return the mesh name (Lua functions only)
Definition gmUserFunction.h:184
@ CURRENT_TIME_PARAM
The parameter should return the current time.
Definition gmUserFunction.h:186
GmTLS< double ** > * _input
Per thread local buffer used to hold input parameters for C functions.
Definition gmUserFunction.h:247
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:805
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:874
int _evalProgressItem
The item used to measure user function call times.
Definition gmUserFunction.h:241
bool _warnOnInvalidIndex
Should we emit the invalid index warning?
Definition gmUserFunction.h:205
const GmMesh * _mesh
The mesh that will supply function parameters.
Definition gmUserFunction.h:202
const GmValueAccessor * _coordAccessor
An accessor to read coordinate values needed when interpolating values over cells.
Definition gmUserFunction.h:203
GmPhysicsUserFunctions * _cObj
The object that contains the C method or NULL for Lua functions.
Definition gmUserFunction.h:243
GmTLS< GmUserFunctionContext * > * _cContext
Per thread context objects passed to C methods or NULL for Lua functions.
Definition gmUserFunction.h:245
ParamContext * _parInfo
A vector with a parameter info for each function parameter.
Definition gmUserFunction.h:239
~GmUserFunctionEvaluator()
Destructor.
Definition gmUserFunction.cpp:278
bool _isAggregationOnly
True if all parameters are aggregation.
Definition gmUserFunction.h:206
const GmValueInfo * _resultInfo
Information about the expected function result.
Definition gmUserFunction.h:201
bool _evalNeedsCoord
Set to true if the function evaluation needs user coordinates.
Definition gmUserFunction.h:204
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:326
int _ipRuleSet
The integration rule set used by gauss attribute parameters or -1 if there is no such parameter type.
Definition gmUserFunction.h:240
const GmUserFunction * userFunction() const
Returns the user function associated with this evaluator.
Definition gmUserFunction.h:171
const GmSimulationData * _simData
The simulation data object used for retrieving time information.
Definition gmUserFunction.h:199
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:625
bool loadCFunction(GmSimulationData *simData, const GmMesh *mesh, QString &err)
Load internal parameters needed to call a C function.
Definition gmUserFunction.cpp:767
Class used to store the definition of a user function and its parameters.
Definition gmUserFunction.h:82
QVector< GmUserFunctionParameter > _parameters
The list of function parameters.
Definition gmUserFunction.h:149
QString cFunctionName() const
Returns the name of the stored C function or an empty string for Lua functions.
Definition gmUserFunction.h:127
QString _cFuncPlugin
The name of the plugin that contains _cFuncName (or empty for Lua functions)
Definition gmUserFunction.h:154
GmUserFunctionType _type
The type of function stored.
Definition gmUserFunction.h:148
bool isCFunction() const
Returns true if the stored function is a C function.
Definition gmUserFunction.h:121
GmPluginObjectFactory * cFunctionFactory() const
Returns the stored C function factory (that can be used to instance a new GmPhysicsUserFunctions obje...
Definition gmUserFunction.h:136
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:150
QString _cFuncName
The name of the C function to be called (or empty for Lua functions)
Definition gmUserFunction.h:153
QString id() const
Returns the object id.
Definition gmUserFunction.h:108
QString _id
Unique id for this value function.
Definition gmUserFunction.h:147
LuaFunction * luaFunction() const
Returns the stored Lua function (associated with the current thread) or NULL for C functions.
Definition gmUserFunction.h:124
GmUserFunctionType type() const
Returns the type of function stored inside ths object.
Definition gmUserFunction.h:111
QString cFunctionPlugin() const
Returns the name of the stored C function plugin or an empty string for Lua functions.
Definition gmUserFunction.h:130
GmPluginObjectFactory * _cFuncFactory
The factory used to instance a GmPhysicsUserFunctions if needed (or NULL for Lua functions)
Definition gmUserFunction.h:155
const QVector< GmUserFunctionParameter > & parameterList() const
Returns the list of parameters passed to the function.
Definition gmUserFunction.h:118
Interface class for accessing and setting values from an "indexable" collection of values.
Definition gmValueAccessor.h:60
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition gmValueInfo.h:132
int size() const
Returns the number of values (doubles) associated to this definition.
Definition gmValueInfo.h:178
Declaration of the GmAggregator class.
Declaration of the GmCell class.
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Declaration of the GmInterpolator and GmInterpolatorObject classes.
GmInterpolatorType
Available interpolation methods. Not every method is suitable for every kind of interpolator class....
Definition gmInterpolator.h:64
@ GM_NO_INTERPOLATOR
No interpolation needed. Not valid to construct an interpolator object but useful when defining user ...
Definition gmInterpolator.h:133
Declaration of the GmTLBuffer class.
GmUserFunctionType
Type of a user function.
Definition gmUserFunction.h:54
@ GM_NODE_USER_FUNCTION
Means that the function calculates nodal values (attributes)
Definition gmUserFunction.h:55
@ GM_CELL_USER_FUNCTION
Means that the function calculates cell values (attributes or properties)
Definition gmUserFunction.h:56
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:78
Declaration of the GmValueInfo class.
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition gmVector.h:34
Auxiliar structure for storing function parameter information.
Definition gmUserFunction.h:87
int _dim
A "bound" dimension for returning a scalar when _src is multidimensional (or -1 instead)
Definition gmUserFunction.h:97
GmUserFunctionParameter(QString src, Unit u, int dim, int history, GmInterpolatorType interpType, QVariant interpParam, GmAggregationType aggType)
Basic constructor.
Definition gmUserFunction.h:92
GmUserFunctionParameter()
Default constructor. Needed to allow storing objects in lists.
Definition gmUserFunction.h:89
QString _src
The value source for this parameter.
Definition gmUserFunction.h:95
GmInterpolatorType _interpType
How do we interpolate node attributes for element functions?
Definition gmUserFunction.h:99
Unit _unit
The expected unit for the value.
Definition gmUserFunction.h:96
QVariant _interpParam
Optional interpolation parameters.
Definition gmUserFunction.h:100
int _history
The state accessed by this parameter (0 = current state, 1 = previous, etc..)
Definition gmUserFunction.h:98