ug4
lua_user_data.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 
34 #ifndef __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA__
35 #define __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA__
36 
37 #include <stdarg.h>
38 #include <string>
39 #include "registry/registry.h"
40 
41 
42 
43 
44 #ifndef USE_LUAJIT
45 extern "C" {
46 #include "externals/lua/lua.h"
47 }
48 #else
49 #include <lua.hpp>
50 #endif
51 
52 
53 
54 #include "lua_util.h"
55 
56 #include "common/common.h"
57 #include "common/math/ugmath.h"
62 #include "lua_traits.h"
63 
65 
66 #ifdef USE_LUA2C
68 #endif
69 
70 namespace ug
71 {
72 
74 // LuaUserData
76 
78 bool CheckLuaCallbackName(const char* name);
79 
80 // predeclaration
81 template <typename TData, int dim, typename TRet = void>
82 class LuaUserDataFactory;
83 
85 
93 template <typename TData, int dim, typename TRet = void>
95  : public StdGlobPosData<LuaUserData<TData, dim, TRet>, TData, dim, TRet>
96 {
98  friend class LuaUserDataFactory<TData, dim, TRet>;
99 
100  public:
102 
110  LuaUserData(const char* luaCallback);
113 
115  virtual ~LuaUserData();
116 
118  static std::string signature();
119 
121  static std::string name();
122 
124  static bool check_callback_returns(const char* callName,
125  const bool bThrow = false);
126 
128  static bool check_callback_returns(LuaFunctionHandle handle,
129  const bool bThrow = false);
130 
132  static bool check_callback_returns(lua_State* L, int callbackRef, const char* callName,
133  const bool bThrow = false);
134 
136  inline TRet evaluate(TData& D, const MathVector<dim>& x, number time, int si) const;
137 
138  protected:
140  void set_created_from_factory(bool bFromFactory) {m_bFromFactory = bFromFactory;}
141 
142  protected:
144  std::string m_callbackName;
145 
148 
149  #ifdef USE_LUA2C
151  bridge::LUACompiler m_luaComp;
152  #endif
155 
158 };
159 
161 // LuaUserDataFactory
163 
165 
178 template <typename TData, int dim, typename TRet>
180 {
181  friend class LuaUserData<TData,dim,TRet>;
182 
183  protected:
186 
187  // disallow copy
190 
193  {
195  return inst;
196  }
197 
200  const std::string& name);
201 
203  static void remove(const std::string& name);
204 
206  static std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*, int*> > m_mData;
207 
208  public:
223  static SmartPtr<LuaUserData<TData,dim,TRet> > create(const std::string& name)
224  {
225  return instance().provide_or_create(name);
226  }
227 };
228 
230 // LuaUserFunction
232 
234 
239 template <typename TData, int dim, typename TDataIn>
241  : public StdDataLinker<LuaUserFunction<TData, dim, TDataIn>, TData, dim>
242 {
243  public:
244  // type of base class
246  using base_type::set_input;
247 
248  public:
255  LuaUserFunction(const char* luaCallback, size_t numArgs);
256  LuaUserFunction(const char* luaCallback, size_t numArgs, bool bPosTimeNeed);
257  LuaUserFunction(LuaFunctionHandle handle, size_t numArgs);
258  LuaUserFunction(LuaFunctionHandle handle, size_t numArgs, bool bPosTimeNeed);
260 
262  virtual ~LuaUserFunction();
263 
269  void set_deriv(size_t arg, const char* luaCallback);
270  void set_deriv(size_t arg, LuaFunctionHandle handle);
271 
272 
274  void set_num_input(size_t num);
275 
281  void set_input(size_t i, SmartPtr<CplUserData<TDataIn, dim> > data);
282  void set_input(size_t i, number val);
284 
286  { set_input(i, input); set_deriv(i, deriv); }
287 
289  virtual void operator() (TData& out, int numArgs, ...) const;
290 
291  inline void evaluate (TData& value,
292  const MathVector<dim>& globIP,
293  number time, int si) const;
294 
295  template <int refDim>
296  inline void evaluate(TData vValue[],
297  const MathVector<dim> vGlobIP[],
298  number time, int si,
299  GridObject* elem,
300  const MathVector<dim> vCornerCoords[],
301  const MathVector<refDim> vLocIP[],
302  const size_t nip,
303  LocalVector* u,
304  const MathMatrix<refDim, dim>* vJT = NULL) const;
305 
306  template <int refDim>
307  void eval_and_deriv(TData vValue[],
308  const MathVector<dim> vGlobIP[],
309  number time, int si,
310  GridObject* elem,
311  const MathVector<dim> vCornerCoords[],
312  const MathVector<refDim> vLocIP[],
313  const size_t nip,
314  LocalVector* u,
315  bool bDeriv,
316  int s,
317  std::vector<std::vector<TData> > vvvDeriv[],
318  const MathMatrix<refDim, dim>* vJT = NULL);
319 
320  protected:
322  void set_lua_value_callback(const char* luaCallback, size_t numArgs);
323  void set_lua_value_callback(LuaFunctionHandle handle, size_t numArgs);
324 
326  void free_callback_ref();
327 
329  void free_deriv_callback_ref(size_t arg);
330 
332  void eval_value(TData& out, const std::vector<TDataIn>& dataIn,
333  const MathVector<dim>& x, number time, int si) const;
334 
336  void eval_deriv(TData& out, const std::vector<TDataIn>& dataIn,
337  const MathVector<dim>& x, number time, int si, size_t arg) const;
338 
339  protected:
341  std::string m_cbValueName;
342  std::vector<std::string> m_cbDerivName;
343 
346  std::vector<int> m_cbDerivRef;
347 
350 
352  size_t m_numArgs;
353 
356 
358  #ifdef USE_LUA2C
359  bridge::LUACompiler m_luaComp;
360  std::vector<bridge::LUACompiler > m_luaComp_Deriv;
361  #endif
362 
363  protected:
365  std::vector<SmartPtr<CplUserData<TDataIn, dim> > > m_vpUserData;
366 
368  std::vector<SmartPtr<DependentUserData<TDataIn, dim> > > m_vpDependData;
369 
370 };
371 
372 
375 {
376  public:
378 
379  void set_lua_callback(const char* luaCallback);
380 
381  number operator() ( int numArgs, ... ) const;
382 
383  protected:
384  std::string m_callbackName;
387 };
388 
389 
391 // LuaFunction
393 
398 template <typename TData, typename TDataIn>
399 class LuaFunction : public IFunction<TData, TDataIn>
400 {
401  public:
403  LuaFunction();
404  virtual ~LuaFunction() {};
405 
407 
412  void set_lua_callback(const char* luaCallback, size_t numArgs);
413 
415  virtual void operator() (TData& out, int numArgs, ...);
416 
417  protected:
419  std::string m_cbValueName;
420 
423 
426 
428  size_t m_numArgs;
429 };
430 
431 
432 
433 namespace bridge{
434 
435 void RegisterLuaUserData(Registry& reg, std::string grp);
436 
437 } // end namepace bridge
438 } // end namespace ug
439 
440 #include "lua_user_data_impl.h"
441 
442 #endif /* __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA__ */
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
Type based UserData.
Definition: user_data.h:501
const TData & value(size_t s, size_t ip) const
returns the value at ip
Definition: user_data.h:512
The base class for all geometric objects, such as vertices, edges, faces, volumes,...
Definition: grid_base_objects.h:157
number time() const
get the current evaluation time
Definition: user_data.h:285
Definition: user_function.h:40
Definition: local_algebra.h:198
Handle for a lua reference.
Definition: lua_function_handle.h:40
Definition: lua_user_data.h:400
int m_cbValueRef
reference to lua function
Definition: lua_user_data.h:422
virtual void operator()(TData &out, int numArgs,...)
evaluates the data
Definition: lua_user_data_impl.h:1109
lua_State * m_L
lua state
Definition: lua_user_data.h:425
std::string m_cbValueName
callback name as string
Definition: lua_user_data.h:419
LuaFunction()
constructor
Definition: lua_user_data_impl.h:1080
size_t m_numArgs
number of arguments to use
Definition: lua_user_data.h:428
virtual ~LuaFunction()
Definition: lua_user_data.h:404
void set_lua_callback(const char *luaCallback, size_t numArgs)
sets the Lua function used to compute the data
Definition: lua_user_data_impl.h:1087
Factory providing LuaUserData.
Definition: lua_user_data.h:180
LuaUserDataFactory(const LuaUserDataFactory &)
LuaUserDataFactory()
private constructor, since singleton
Definition: lua_user_data.h:185
static void remove(const std::string &name)
removes the user data
Definition: lua_user_data_impl.h:415
static LuaUserDataFactory< TData, dim, TRet > & instance()
singleton provider
Definition: lua_user_data.h:192
static SmartPtr< LuaUserData< TData, dim, TRet > > create(const std::string &name)
Definition: lua_user_data.h:223
static std::map< std::string, std::pair< LuaUserData< TData, dim, TRet > *, int * > > m_mData
storage of already created data
Definition: lua_user_data.h:206
static SmartPtr< LuaUserData< TData, dim, TRet > > provide_or_create(const std::string &name)
returns new Data if not already created, already existing else
Definition: lua_user_data_impl.h:368
LuaUserDataFactory & operator=(const LuaUserDataFactory &)
provides data specified in the lua script
Definition: lua_user_data.h:96
static bool check_callback_returns(const char *callName, const bool bThrow=false)
returns true if callback has correct return values
Definition: lua_user_data_impl.h:247
lua_State * m_L
lua state
Definition: lua_user_data.h:157
int m_callbackRef
reference to lua function
Definition: lua_user_data.h:147
static std::string signature()
returns string of required callback signature
Definition: lua_user_data_impl.h:65
static std::string name()
returns name of UserData
Definition: lua_user_data_impl.h:82
std::string m_callbackName
callback name as string
Definition: lua_user_data.h:144
LuaUserData(const char *luaCallback)
Constructor.
Definition: lua_user_data_impl.h:92
TRet evaluate(TData &D, const MathVector< dim > &x, number time, int si) const
evaluates the data at a given point and time
Definition: lua_user_data_impl.h:282
void set_created_from_factory(bool bFromFactory)
sets that LuaUserData is created by LuaUserDataFactory
Definition: lua_user_data.h:140
bool m_bFromFactory
flag, indicating if created from factory
Definition: lua_user_data.h:154
virtual ~LuaUserData()
}
Definition: lua_user_data_impl.h:353
maps several data values to an output data value using a lua callback
Definition: lua_user_data.h:242
LuaUserFunction(const char *luaCallback, size_t numArgs)
constructor
Definition: lua_user_data_impl.h:443
std::vector< SmartPtr< CplUserData< TDataIn, dim > > > m_vpUserData
LUACompiler types for compiled LUA code.
Definition: lua_user_data.h:365
void eval_value(TData &out, const std::vector< TDataIn > &dataIn, const MathVector< dim > &x, number time, int si) const
evaluates the data at a given point and time
Definition: lua_user_data_impl.h:738
virtual ~LuaUserFunction()
destructor frees the reference
Definition: lua_user_data_impl.h:508
std::vector< SmartPtr< DependentUserData< TDataIn, dim > > > m_vpDependData
data input casted to dependend data
Definition: lua_user_data.h:368
void free_deriv_callback_ref(size_t arg)
frees callback-references for derivate callbacks
Definition: lua_user_data_impl.h:529
void evaluate(TData &value, const MathVector< dim > &globIP, number time, int si) const
Definition: lua_user_data_impl.h:907
void set_input_and_deriv(size_t i, SmartPtr< CplUserData< TDataIn, dim > > input, LuaFunctionHandle deriv)
Definition: lua_user_data.h:285
void set_input(size_t i, SmartPtr< CplUserData< TDataIn, dim > > data)
set input value for paramter i
Definition: lua_user_data_impl.h:1047
std::vector< int > m_cbDerivRef
Definition: lua_user_data.h:346
void eval_deriv(TData &out, const std::vector< TDataIn > &dataIn, const MathVector< dim > &x, number time, int si, size_t arg) const
evaluates the data at a given point and time
Definition: lua_user_data_impl.h:821
void eval_and_deriv(TData vValue[], const MathVector< dim > vGlobIP[], number time, int si, GridObject *elem, const MathVector< dim > vCornerCoords[], const MathVector< refDim > vLocIP[], const size_t nip, LocalVector *u, bool bDeriv, int s, std::vector< std::vector< TData > > vvvDeriv[], const MathMatrix< refDim, dim > *vJT=NULL)
Definition: lua_user_data_impl.h:957
lua_State * m_L
lua state
Definition: lua_user_data.h:349
size_t m_numArgs
number of arguments to use
Definition: lua_user_data.h:352
std::string m_cbValueName
callback name as string
Definition: lua_user_data.h:341
void free_callback_ref()
frees the callback-reference, if a callback was set.
Definition: lua_user_data_impl.h:520
int m_cbValueRef
reference to lua function
Definition: lua_user_data.h:345
StdDataLinker< LuaUserFunction< TData, dim, TDataIn >, TData, dim > base_type
Definition: lua_user_data.h:245
bool m_bPosTimeNeed
flag for position and time data
Definition: lua_user_data.h:355
void set_deriv(size_t arg, const char *luaCallback)
sets the Lua function used to compute the derivative
Definition: lua_user_data_impl.h:599
std::vector< std::string > m_cbDerivName
Definition: lua_user_data.h:342
void set_lua_value_callback(const char *luaCallback, size_t numArgs)
sets the Lua function used to compute the data
Definition: lua_user_data_impl.h:539
void set_num_input(size_t num)
set number of needed inputs
Definition: lua_user_data_impl.h:1035
virtual void operator()(TData &out, int numArgs,...) const
evaluates the data
Definition: lua_user_data_impl.h:659
this class maps a scalar value an output scalar value using a lua callback
Definition: lua_user_data.h:375
void set_lua_callback(const char *luaCallback)
Definition: lua_user_data.cpp:70
number operator()(int numArgs,...) const
Definition: lua_user_data.cpp:85
std::string m_callbackName
Definition: lua_user_data.h:384
LuaUserNumberNumberFunction()
Definition: lua_user_data.cpp:64
lua_State * m_L
Definition: lua_user_data.h:386
int m_callbackRef
Definition: lua_user_data.h:385
A class for fixed size, dense matrices.
Definition: math_matrix.h:52
combines several UserDatas to a new UserData of a specified type
Definition: linker.h:54
virtual void set_input(size_t i, SmartPtr< ICplUserData< dim > > input, SmartPtr< UserDataInfo > info)
sets an input
Definition: linker.h:114
Definition: std_glob_pos_data.h:55
Definition: lua_compiler.h:50
Registry for functions and classes that are exported to scripts and visualizations.
Definition: registry.h:138
static const int dim
double number
Definition: types.h:124
static int input(void)
struct lua_State lua_State
Definition: lua_table_handle.h:40
void RegisterLuaUserData(Registry &reg, string grp)
Definition: lua_user_data.cpp:284
the ug namespace
bool CheckLuaCallbackName(const char *name)
returns true if callback exists
Definition: lua_user_data.cpp:50