33 #ifndef __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA_IMPL_
34 #define __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA_IMPL_
44 #define PROFILE_CALLBACK() PROFILE_FUNC_GROUP("luacallback")
45 #define PROFILE_CALLBACK_BEGIN(name) PROFILE_BEGIN_GROUP(name, "luacallback")
46 #define PROFILE_CALLBACK_END() PROFILE_END()
48 #define PROFILE_CALLBACK()
49 #define PROFILE_CALLBACK_BEGIN(name)
50 #define PROFILE_CALLBACK_END()
64 template <
typename TData,
int dim,
typename TRet>
68 ss <<
"function name(";
69 if(
dim >= 1) ss <<
"x";
70 if(
dim >= 2) ss <<
", y";
71 if(
dim >= 3) ss <<
", z";
72 ss <<
", t, si)\n ... \n return ";
74 ss << lua_traits<TRet>::signature() <<
", ";
75 ss << lua_traits<TData>::signature();
81 template <
typename TData,
int dim,
typename TRet>
91 template <
typename TData,
int dim,
typename TRet>
93 : m_callbackName(luaCallback), m_bFromFactory(false)
102 if(lua_isnil(
m_L, -1)){
118 template <
typename TData,
int dim,
typename TRet>
120 : m_callbackName(
"__anonymous__lua__function__"), m_bFromFactory(false)
138 template <
typename TData,
int dim,
typename TRet>
144 const int level = lua_gettop(L);
152 lua_rawgeti(L, LUA_REGISTRYINDEX, callbackRef);
172 if(lua_pcall(L, argSize, LUA_MULTRET, 0) != 0)
174 "testing callback '" << callName <<
"',"
175 " lua message: "<< lua_tostring(L, -1));
178 const int numResults = lua_gettop(L) - level;
184 if(numResults != retSize){
186 UG_THROW(
name() <<
": Number of return values incorrect "
188 "\nRequired: "<<retSize<<
", passed: "<<numResults
189 <<
". Use signature as follows:\n"
202 "\nUse signature as follows:\n"
213 UG_THROW(
"LuaUserData: Return values type incorrect "
215 "\nUse signature as follows:\n"
224 lua_pop(L, numResults);
230 template <
typename TData,
int dim,
typename TRet>
239 bool bRet = check_callback_returns(L, handle.
ref,
"__lua_function_handle__", bThrow);
245 template <
typename TData,
int dim,
typename TRet>
254 lua_getglobal(L, callName);
257 if(lua_isnil(L, -1)) {
259 UG_THROW(
name() <<
": Cannot find specified lua callback "
260 " with name: "<<callName);
268 int callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
271 bool bRet = check_callback_returns(L, callbackRef, callName, bThrow);
274 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
280 template <
typename TData,
int dim,
typename TRet>
289 for(
int i=0; i<
dim; i++)
294 m_luaComp.call(ret, d);
304 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_callbackRef);
324 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
326 "running callback '" << m_callbackName <<
"',"
327 " lua message: "<< lua_tostring(m_L, -1)<<
".\n"
328 "Use signature as follows:\n"
340 "callback '" << m_callbackName <<
"'.\n"
341 "Use signature as follows:\n"
345 lua_pop(m_L, retSize);
352 template <
typename TData,
int dim,
typename TRet>
356 luaL_unref(m_L, LUA_REGISTRYINDEX, m_callbackRef);
366 template <
typename TData,
int dim,
typename TRet>
371 typedef std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> > Map;
372 typedef typename Map::iterator iterator;
375 iterator iter = m_mData.find(
name);
378 if(iter == m_mData.end())
384 sp->set_created_from_factory(
true);
393 std::pair<LuaUserData<TData,dim,TRet>*,
int*>& data = m_mData[
name];
394 data.first = sp.
get();
408 std::pair<LuaUserData<TData,dim,TRet>*,
int*>& data = iter->second;
413 template <
typename TData,
int dim,
typename TRet>
417 typedef std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> > Map;
418 typedef typename Map::iterator iterator;
421 iterator iter = m_mData.find(
name);
424 if(iter == m_mData.end())
425 UG_THROW(
"LuaUserDataFactory: trying to remove non-registered"
426 " data with name: "<<
name);
433 template <
typename TData,
int dim,
typename TRet>
434 std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> >
441 template <
typename TData,
int dim,
typename TDataIn>
444 : m_numArgs(numArgs), m_bPosTimeNeed(false)
456 template <
typename TData,
int dim,
typename TDataIn>
458 LuaUserFunction(
const char* luaCallback,
size_t numArgs,
bool bPosTimeNeed)
459 : m_numArgs(numArgs), m_bPosTimeNeed(bPosTimeNeed)
467 m_luaComp_Deriv.clear();
472 template <
typename TData,
int dim,
typename TDataIn>
475 : m_numArgs(numArgs), m_bPosTimeNeed(false)
484 UG_LOG(
"WARNING (in LuaUserFunction): LUA2C compiler "
485 "can't be executed for FunctionHandle.\n");
490 template <
typename TData,
int dim,
typename TDataIn>
493 : m_numArgs(numArgs), m_bPosTimeNeed(bPosTimeNeed)
501 m_luaComp_Deriv.clear();
507 template <
typename TData,
int dim,
typename TDataIn>
514 for(
size_t i = 0; i < m_numArgs; ++i){
515 free_deriv_callback_ref(i);
519 template <
typename TData,
int dim,
typename TDataIn>
522 if(m_cbValueRef != LUA_NOREF){
523 luaL_unref(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
524 m_cbValueRef = LUA_NOREF;
528 template <
typename TData,
int dim,
typename TDataIn>
531 if(m_cbDerivRef[arg] != LUA_NOREF){
532 luaL_unref(m_L, LUA_REGISTRYINDEX, m_cbDerivRef[arg]);
533 m_cbDerivRef[arg] = LUA_NOREF;
538 template <
typename TData,
int dim,
typename TDataIn>
542 m_cbValueName = luaCallback;
545 lua_getglobal(m_L, m_cbValueName.c_str());
548 if(lua_isnil(m_L, -1)){
549 UG_THROW(
"LuaUserFunction::set_lua_value_callback(...):"
550 "Specified callback does not exist: " << m_cbValueName);
557 m_cbValueRef = luaL_ref(m_L, LUA_REGISTRYINDEX);
561 m_cbDerivName.resize(numArgs);
562 m_cbDerivRef.resize(numArgs, LUA_NOREF);
565 set_num_input(numArgs);
568 m_luaComp_Deriv.resize(numArgs);
572 template <
typename TData,
int dim,
typename TDataIn>
577 m_cbValueName =
"__anonymous__lua__function__";
583 m_cbValueRef = handle.
ref;
587 m_cbDerivName.resize(numArgs);
588 m_cbDerivRef.resize(numArgs, LUA_NOREF);
591 set_num_input(numArgs);
594 m_luaComp_Deriv.resize(numArgs);
598 template <
typename TData,
int dim,
typename TDataIn>
603 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback: Trying "
604 "to set a derivative for argument " << arg <<
", that "
605 "does not exist. Number of arguments is "<<m_numArgs);
608 m_cbDerivName[arg] = luaCallback;
611 free_deriv_callback_ref(arg);
614 lua_getglobal(m_L, m_cbDerivName[arg].c_str());
617 if(lua_isnil(m_L, -1)){
618 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback(...):"
619 "Specified callback does not exist: " << m_cbDerivName[arg]);
623 m_cbDerivRef[arg] = luaL_ref(m_L, LUA_REGISTRYINDEX);
631 template <
typename TData,
int dim,
typename TDataIn>
636 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback: Trying "
637 "to set a derivative for argument " << arg <<
", that "
638 "does not exist. Number of arguments is "<<m_numArgs);
641 m_cbDerivName[arg] = std::string(
"__anonymous__lua__function__");
644 free_deriv_callback_ref(arg);
647 m_cbDerivRef[arg] = handle.
ref;
658 template <
typename TData,
int dim,
typename TDataIn>
668 va_start(ap2, numArgs);
671 for(
int i = 0; i < numArgs; ++i)
672 d[i] = va_arg(ap2,
double);
678 m_luaComp.name() <<
", " << m_luaComp.num_in() <<
" != " << numArgs <<
" or " << m_luaComp.num_out() <<
" != " <<
lua_traits<TData>::size);
679 m_luaComp.call(ret, d);
689 UG_ASSERT(numArgs == (
int)m_numArgs,
"Number of arguments mismatched.");
692 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
696 va_start(ap, numArgs);
699 for(
int i = 0; i < numArgs; ++i)
702 TDataIn val = va_arg(ap, TDataIn);
718 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
719 UG_THROW(
"LuaUserFunction::operator(...): Error while "
720 "running callback '" << m_cbValueName <<
"',"
721 " lua message: "<< lua_tostring(m_L, -1));
728 UG_CATCH_THROW(
"LuaUserFunction::operator(...): Error while running "
729 "callback '" << m_cbValueName <<
"'");
732 lua_pop(m_L, retSize);
737 template <
typename TData,
int dim,
typename TDataIn>
748 for(
size_t i = 0; i < dataIn.size(); ++i)
751 for(
int i=0; i<
dim; i++)
752 d[i+m_numArgs] = x[i];
753 d[
dim+m_numArgs]=time;
754 d[
dim+m_numArgs+1]=si;
759 m_luaComp.call(ret, d);
770 UG_ASSERT(dataIn.size() == m_numArgs,
"Number of arguments mismatched.");
773 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
776 for(
size_t i = 0; i < dataIn.size(); ++i)
801 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
802 UG_THROW(
"LuaUserFunction::eval_value(...): Error while "
803 "running callback '" << m_cbValueName <<
"',"
804 " lua message: "<< lua_tostring(m_L, -1));
812 "running callback '" << m_cbValueName <<
"'");
815 lua_pop(m_L, retSize);
820 template <
typename TData,
int dim,
typename TDataIn>
832 for(
size_t i=0; i<m_numArgs; i++)
835 for(
int i=0; i<
dim; i++)
836 d[i+m_numArgs] = x[i];
837 d[
dim+m_numArgs]=time;
838 d[
dim+m_numArgs+1]=si;
844 luaComp.
call(ret, d);
854 UG_ASSERT(dataIn.size() == m_numArgs,
"Number of arguments mismatched.");
855 UG_ASSERT(arg < m_numArgs,
"Argument does not exist.");
858 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbDerivRef[arg]);
861 for(
size_t i = 0; i < dataIn.size(); ++i)
886 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
887 UG_THROW(
"LuaUserFunction::eval_deriv: Error while "
888 "running callback '" << m_cbDerivName[arg] <<
"',"
889 " lua message: "<< lua_tostring(m_L, -1) );
897 "running callback '" << m_cbDerivName[arg] <<
"'");
900 lua_pop(m_L, retSize);
905 template <
typename TData,
int dim,
typename TDataIn>
909 number time,
int si)
const
913 std::vector<TDataIn> vDataIn(this->num_input());
916 for(
size_t c = 0; c < vDataIn.size(); ++c)
917 (*m_vpUserData[c])(vDataIn[c], globIP, time, si);
920 eval_value(value, vDataIn, globIP, time, si);
925 template <
typename TData,
int dim,
typename TDataIn>
926 template <
int refDim>
940 std::vector<TDataIn> vDataIn(this->num_input());
943 for(
size_t ip = 0; ip < nip; ++ip)
945 for(
size_t c = 0; c < vDataIn.size(); ++c)
946 (*m_vpUserData[c])(vDataIn[c], vGlobIP[ip], time, si, elem, vCornerCoords, vLocIP[ip], u);
949 eval_value(vValue[ip], vDataIn, vGlobIP[ip], time, si);
954 template <
typename TData,
int dim,
typename TDataIn>
955 template <
int refDim>
967 std::vector<std::vector<TData> > vvvDeriv[],
972 std::vector<TDataIn> vDataIn(this->num_input());
974 for(
size_t ip = 0; ip < nip; ++ip)
977 for(
size_t c = 0; c < vDataIn.size(); ++c)
978 vDataIn[c] = m_vpUserData[c]->value(this->series_id(c,
s), ip);
981 eval_value(vValue[ip], vDataIn, vGlobIP[ip], time, si);
985 if(!bDeriv || this->zero_derivative())
return;
988 this->set_zero(vvvDeriv, nip);
991 for(
size_t c = 0; c < vDataIn.size(); ++c)
994 if(m_cbDerivRef[c] == LUA_NOREF || m_vpUserData[c]->zero_derivative())
continue;
997 for(
size_t ip = 0; ip < nip; ++ip)
1000 for(
size_t i = 0; i < vDataIn.size(); ++i)
1001 vDataIn[i] = m_vpUserData[i]->value(this->series_id(c,
s), ip);
1007 eval_deriv(derivVal, vDataIn, vGlobIP[ip], time, si, c);
1010 for(
size_t fct = 0; fct < this->input_num_fct(c); ++fct)
1013 const size_t commonFct = this->input_common_fct(c, fct);
1016 for(
size_t dof = 0; dof < this->num_sh(fct); ++dof)
1019 mult_add(vvvDeriv[ip][commonFct][dof],
1021 m_vpDependData[c]->deriv(this->series_id(c,
s), ip, fct, dof));
1034 template <
typename TData,
int dim,
typename TDataIn>
1038 m_vpUserData.resize(num);
1039 m_vpDependData.resize(num);
1042 base_type::set_num_input(num);
1045 template <
typename TData,
int dim,
typename TDataIn>
1049 UG_ASSERT(i < m_vpUserData.size(),
"Input not needed");
1050 UG_ASSERT(i < m_vpDependData.size(),
"Input not needed");
1053 if(i >= this->num_input())
1054 UG_THROW(
"LuaUserFunction::set_input: Only " << this->num_input()
1055 <<
" inputs can be set. Use 'set_num_input' to increase"
1056 " the number of needed inputs.");
1059 m_vpUserData[i] = data;
1062 m_vpDependData[i] = data.template cast_dynamic<DependentUserData<TDataIn, dim> >();
1065 base_type::set_input(i, data, data);
1068 template <
typename TData,
int dim,
typename TDataIn>
1071 set_input(i, CreateConstUserData<dim>(val, TDataIn()));
1079 template <
typename TData,
typename TDataIn>
1086 template <
typename TData,
typename TDataIn>
1090 m_cbValueName = luaCallback;
1093 lua_getglobal(m_L, m_cbValueName.c_str());
1096 if(lua_isnil(m_L, -1)){
1097 UG_THROW(
"LuaFunction::set_lua_callback(...):"
1098 "Specified lua callback does not exist: " << m_cbValueName);
1102 m_cbValueRef = luaL_ref(m_L, LUA_REGISTRYINDEX);
1105 m_numArgs = numArgs;
1108 template <
typename TData,
typename TDataIn>
1112 UG_ASSERT(numArgs == (
int)m_numArgs,
"Number of arguments mismatched.");
1115 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
1119 va_start(ap, numArgs);
1122 for(
int i = 0; i < numArgs; ++i)
1125 TDataIn val = va_arg(ap, TDataIn);
1141 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
1142 UG_THROW(
"LuaFunction::operator(...): Error while "
1143 "running callback '" << m_cbValueName <<
"',"
1144 " lua message: "<< lua_tostring(m_L, -1));
1151 UG_CATCH_THROW(
"LuaFunction::operator(...): Error while running "
1152 "callback '" << m_cbValueName <<
"'");
1155 lua_pop(m_L, retSize);
location name
Definition: checkpoint_util.lua:128
Definition: smart_pointer.h:108
T * get()
returns encapsulated pointer
Definition: smart_pointer.h:197
int * refcount_ptr() const
WARNING: this method is DANGEROUS!
Definition: smart_pointer.h:263
Type based UserData.
Definition: user_data.h:501
The base class for all geometric objects, such as vertices, edges, faces, volumes,...
Definition: grid_base_objects.h:157
Definition: local_algebra.h:198
Handle for a lua reference.
Definition: lua_function_handle.h:40
int ref
Definition: lua_function_handle.h:42
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
LuaFunction()
constructor
Definition: lua_user_data_impl.h:1080
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
static void remove(const std::string &name)
removes the user data
Definition: lua_user_data_impl.h:415
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
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
virtual ~LuaUserData()
}
Definition: lua_user_data_impl.h:353
LuaUserFunction(const char *luaCallback, size_t numArgs)
constructor
Definition: lua_user_data_impl.h:443
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
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(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
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
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
A class for fixed size, dense matrices.
Definition: math_matrix.h:52
Definition: lua_compiler.h:50
const std::string & name() const
Definition: lua_compiler.h:86
int num_out() const
Definition: lua_compiler.h:81
bool call(double *ret, const double *in) const
Definition: lua_compiler.cpp:263
function util FileDummy read(...) error("io.open_0 does not support read.") end
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_CATCH_THROW(msg)
Definition: error.h:64
#define UG_THROW(msg)
Definition: error.h:57
#define UG_LOG(msg)
Definition: log.h:367
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
double number
Definition: types.h:124
struct lua_State lua_State
Definition: lua_table_handle.h:40
#define PROFILE_CALLBACK()
Definition: lua_user_data_impl.h:48
#define PROFILE_CALLBACK_END()
Definition: lua_user_data_impl.h:50
#define PROFILE_CALLBACK_BEGIN(name)
Definition: lua_user_data_impl.h:49
string GetLUAScriptFunctionDefined(const char *functionName)
returns file and line of defined script function
Definition: info_commands.cpp:368
lua_State * GetDefaultLuaState()
returns the default lua state
Definition: lua_util.cpp:242
bool useLuaCompiler
Definition: info_commands.cpp:93
bool IsFiniteAndNotTooBig(double d)
Definition: number_util.h:39
SmartPtr< T, FreePolicy > make_sp(T *inst)
returns a SmartPtr for the passed raw pointer
Definition: smart_pointer.h:836
static void mult_add(TData &out, const TData &in1, const TDataIn &s)
computes out += s * in1 (with appropriate '*')
Lua Traits to push/pop on lua stack.
Definition: lua_traits.h:79