33 #ifndef __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA_IMPL_
34 #define __H__UG_BRIDGE__BRIDGES__USER_DATA__USER_DATA_IMPL_
46 #define PROFILE_CALLBACK() PROFILE_FUNC_GROUP("luacallback")
47 #define PROFILE_CALLBACK_BEGIN(name) PROFILE_BEGIN_GROUP(name, "luacallback")
48 #define PROFILE_CALLBACK_END() PROFILE_END()
50 #define PROFILE_CALLBACK()
51 #define PROFILE_CALLBACK_BEGIN(name)
52 #define PROFILE_CALLBACK_END()
66 template <
typename TData,
int dim,
typename TRet>
70 ss <<
"function name(";
71 if(
dim >= 1) ss <<
"x";
72 if(
dim >= 2) ss <<
", y";
73 if(
dim >= 3) ss <<
", z";
74 ss <<
", t, si)\n ... \n return ";
76 ss << lua_traits<TRet>::signature() <<
", ";
77 ss << lua_traits<TData>::signature();
83 template <
typename TData,
int dim,
typename TRet>
93 template <
typename TData,
int dim,
typename TRet>
95 : m_callbackName(luaCallback), m_bFromFactory(false)
104 if(lua_isnil(
m_L, -1)){
120 template <
typename TData,
int dim,
typename TRet>
122 : m_callbackName(
"__anonymous__lua__function__"), m_bFromFactory(false)
140 template <
typename TData,
int dim,
typename TRet>
146 const int level = lua_gettop(L);
154 lua_rawgeti(L, LUA_REGISTRYINDEX, callbackRef);
174 if(lua_pcall(L, argSize, LUA_MULTRET, 0) != 0)
176 "testing callback '" << callName <<
"',"
177 " lua message: "<< lua_tostring(L, -1));
180 const int numResults = lua_gettop(L) - level;
186 if(numResults != retSize){
188 UG_THROW(
name() <<
": Number of return values incorrect "
190 "\nRequired: "<<retSize<<
", passed: "<<numResults
191 <<
". Use signature as follows:\n"
204 "\nUse signature as follows:\n"
215 UG_THROW(
"LuaUserData: Return values type incorrect "
217 "\nUse signature as follows:\n"
226 lua_pop(L, numResults);
232 template <
typename TData,
int dim,
typename TRet>
241 bool bRet = check_callback_returns(L, handle.
ref,
"__lua_function_handle__", bThrow);
247 template <
typename TData,
int dim,
typename TRet>
256 lua_getglobal(L, callName);
259 if(lua_isnil(L, -1)) {
261 UG_THROW(
name() <<
": Cannot find specified lua callback "
262 " with name: "<<callName);
270 int callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
273 bool bRet = check_callback_returns(L, callbackRef, callName, bThrow);
276 luaL_unref(L, LUA_REGISTRYINDEX, callbackRef);
282 template <
typename TData,
int dim,
typename TRet>
291 for(
int i=0; i<
dim; i++)
296 m_luaComp.call(ret, d);
306 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_callbackRef);
326 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
328 "running callback '" << m_callbackName <<
"',"
329 " lua message: "<< lua_tostring(m_L, -1)<<
".\n"
330 "Use signature as follows:\n"
342 "callback '" << m_callbackName <<
"'.\n"
343 "Use signature as follows:\n"
347 lua_pop(m_L, retSize);
354 template <
typename TData,
int dim,
typename TRet>
358 luaL_unref(m_L, LUA_REGISTRYINDEX, m_callbackRef);
368 template <
typename TData,
int dim,
typename TRet>
373 typedef std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> > Map;
374 typedef typename Map::iterator iterator;
377 iterator iter = m_mData.find(
name);
380 if(iter == m_mData.end())
386 sp->set_created_from_factory(
true);
395 std::pair<LuaUserData<TData,dim,TRet>*,
int*>& data = m_mData[
name];
396 data.first = sp.
get();
410 std::pair<LuaUserData<TData,dim,TRet>*,
int*>& data = iter->second;
415 template <
typename TData,
int dim,
typename TRet>
419 typedef std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> > Map;
420 typedef typename Map::iterator iterator;
423 iterator iter = m_mData.find(
name);
426 if(iter == m_mData.end())
427 UG_THROW(
"LuaUserDataFactory: trying to remove non-registered"
428 " data with name: "<<
name);
435 template <
typename TData,
int dim,
typename TRet>
436 std::map<std::string, std::pair<LuaUserData<TData,dim,TRet>*,
int*> >
443 template <
typename TData,
int dim,
typename TDataIn>
446 : m_numArgs(numArgs), m_bPosTimeNeed(false)
458 template <
typename TData,
int dim,
typename TDataIn>
460 LuaUserFunction(
const char* luaCallback,
size_t numArgs,
bool bPosTimeNeed)
461 : m_numArgs(numArgs), m_bPosTimeNeed(bPosTimeNeed)
469 m_luaComp_Deriv.clear();
474 template <
typename TData,
int dim,
typename TDataIn>
477 : m_numArgs(numArgs), m_bPosTimeNeed(false)
486 UG_LOG(
"WARNING (in LuaUserFunction): LUA2C compiler "
487 "can't be executed for FunctionHandle.\n");
492 template <
typename TData,
int dim,
typename TDataIn>
495 : m_numArgs(numArgs), m_bPosTimeNeed(bPosTimeNeed)
503 m_luaComp_Deriv.clear();
509 template <
typename TData,
int dim,
typename TDataIn>
516 for(
size_t i = 0; i < m_numArgs; ++i){
517 free_deriv_callback_ref(i);
521 template <
typename TData,
int dim,
typename TDataIn>
524 if(m_cbValueRef != LUA_NOREF){
525 luaL_unref(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
526 m_cbValueRef = LUA_NOREF;
530 template <
typename TData,
int dim,
typename TDataIn>
533 if(m_cbDerivRef[arg] != LUA_NOREF){
534 luaL_unref(m_L, LUA_REGISTRYINDEX, m_cbDerivRef[arg]);
535 m_cbDerivRef[arg] = LUA_NOREF;
540 template <
typename TData,
int dim,
typename TDataIn>
544 m_cbValueName = luaCallback;
547 lua_getglobal(m_L, m_cbValueName.c_str());
550 if(lua_isnil(m_L, -1)){
551 UG_THROW(
"LuaUserFunction::set_lua_value_callback(...):"
552 "Specified callback does not exist: " << m_cbValueName);
559 m_cbValueRef = luaL_ref(m_L, LUA_REGISTRYINDEX);
563 m_cbDerivName.resize(numArgs);
564 m_cbDerivRef.resize(numArgs, LUA_NOREF);
567 set_num_input(numArgs);
570 m_luaComp_Deriv.resize(numArgs);
574 template <
typename TData,
int dim,
typename TDataIn>
579 m_cbValueName =
"__anonymous__lua__function__";
585 m_cbValueRef = handle.
ref;
589 m_cbDerivName.resize(numArgs);
590 m_cbDerivRef.resize(numArgs, LUA_NOREF);
593 set_num_input(numArgs);
596 m_luaComp_Deriv.resize(numArgs);
600 template <
typename TData,
int dim,
typename TDataIn>
605 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback: Trying "
606 "to set a derivative for argument " << arg <<
", that "
607 "does not exist. Number of arguments is "<<m_numArgs);
610 m_cbDerivName[arg] = luaCallback;
613 free_deriv_callback_ref(arg);
616 lua_getglobal(m_L, m_cbDerivName[arg].c_str());
619 if(lua_isnil(m_L, -1)){
620 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback(...):"
621 "Specified callback does not exist: " << m_cbDerivName[arg]);
625 m_cbDerivRef[arg] = luaL_ref(m_L, LUA_REGISTRYINDEX);
633 template <
typename TData,
int dim,
typename TDataIn>
638 UG_THROW(
"LuaUserFunction::set_lua_deriv_callback: Trying "
639 "to set a derivative for argument " << arg <<
", that "
640 "does not exist. Number of arguments is "<<m_numArgs);
643 m_cbDerivName[arg] = std::string(
"__anonymous__lua__function__");
646 free_deriv_callback_ref(arg);
649 m_cbDerivRef[arg] = handle.
ref;
660 template <
typename TData,
int dim,
typename TDataIn>
670 va_start(ap2, numArgs);
673 for(
int i = 0; i < numArgs; ++i)
674 d[i] = va_arg(ap2,
double);
680 m_luaComp.name() <<
", " << m_luaComp.num_in() <<
" != " << numArgs <<
" or " << m_luaComp.num_out() <<
" != " <<
lua_traits<TData>::size);
681 m_luaComp.call(ret, d);
691 UG_ASSERT(numArgs == (
int)m_numArgs,
"Number of arguments mismatched.");
694 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
698 va_start(ap, numArgs);
701 for(
int i = 0; i < numArgs; ++i)
704 TDataIn val = va_arg(ap, TDataIn);
720 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
721 UG_THROW(
"LuaUserFunction::operator(...): Error while "
722 "running callback '" << m_cbValueName <<
"',"
723 " lua message: "<< lua_tostring(m_L, -1));
730 UG_CATCH_THROW(
"LuaUserFunction::operator(...): Error while running "
731 "callback '" << m_cbValueName <<
"'");
734 lua_pop(m_L, retSize);
739 template <
typename TData,
int dim,
typename TDataIn>
750 for(
size_t i = 0; i < dataIn.size(); ++i)
753 for(
int i=0; i<
dim; i++)
754 d[i+m_numArgs] = x[i];
755 d[
dim+m_numArgs]=time;
756 d[
dim+m_numArgs+1]=si;
761 m_luaComp.call(ret, d);
772 UG_ASSERT(dataIn.size() == m_numArgs,
"Number of arguments mismatched.");
775 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
778 for(
size_t i = 0; i < dataIn.size(); ++i)
803 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
804 UG_THROW(
"LuaUserFunction::eval_value(...): Error while "
805 "running callback '" << m_cbValueName <<
"',"
806 " lua message: "<< lua_tostring(m_L, -1));
814 "running callback '" << m_cbValueName <<
"'");
817 lua_pop(m_L, retSize);
822 template <
typename TData,
int dim,
typename TDataIn>
834 for(
size_t i=0; i<m_numArgs; i++)
837 for(
int i=0; i<
dim; i++)
838 d[i+m_numArgs] = x[i];
839 d[
dim+m_numArgs]=time;
840 d[
dim+m_numArgs+1]=si;
846 luaComp.
call(ret, d);
856 UG_ASSERT(dataIn.size() == m_numArgs,
"Number of arguments mismatched.");
857 UG_ASSERT(arg < m_numArgs,
"Argument does not exist.");
860 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbDerivRef[arg]);
863 for(
size_t i = 0; i < dataIn.size(); ++i)
888 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
889 UG_THROW(
"LuaUserFunction::eval_deriv: Error while "
890 "running callback '" << m_cbDerivName[arg] <<
"',"
891 " lua message: "<< lua_tostring(m_L, -1) );
899 "running callback '" << m_cbDerivName[arg] <<
"'");
902 lua_pop(m_L, retSize);
907 template <
typename TData,
int dim,
typename TDataIn>
911 number time,
int si)
const
915 std::vector<TDataIn> vDataIn(this->num_input());
918 for(
size_t c = 0; c < vDataIn.size(); ++c)
919 (*m_vpUserData[c])(vDataIn[c], globIP, time, si);
922 eval_value(value, vDataIn, globIP, time, si);
927 template <
typename TData,
int dim,
typename TDataIn>
928 template <
int refDim>
942 std::vector<TDataIn> vDataIn(this->num_input());
945 for(
size_t ip = 0; ip < nip; ++ip)
947 for(
size_t c = 0; c < vDataIn.size(); ++c)
948 (*m_vpUserData[c])(vDataIn[c], vGlobIP[ip], time, si, elem, vCornerCoords, vLocIP[ip], u);
951 eval_value(vValue[ip], vDataIn, vGlobIP[ip], time, si);
956 template <
typename TData,
int dim,
typename TDataIn>
957 template <
int refDim>
969 std::vector<std::vector<TData> > vvvDeriv[],
974 std::vector<TDataIn> vDataIn(this->num_input());
976 for(
size_t ip = 0; ip < nip; ++ip)
979 for(
size_t c = 0; c < vDataIn.size(); ++c)
980 vDataIn[c] = m_vpUserData[c]->value(this->series_id(c,
s), ip);
983 eval_value(vValue[ip], vDataIn, vGlobIP[ip], time, si);
987 if(!bDeriv || this->zero_derivative())
return;
990 this->set_zero(vvvDeriv, nip);
993 for(
size_t c = 0; c < vDataIn.size(); ++c)
996 if(m_cbDerivRef[c] == LUA_NOREF || m_vpUserData[c]->zero_derivative())
continue;
999 for(
size_t ip = 0; ip < nip; ++ip)
1002 for(
size_t i = 0; i < vDataIn.size(); ++i)
1003 vDataIn[i] = m_vpUserData[i]->value(this->series_id(c,
s), ip);
1009 eval_deriv(derivVal, vDataIn, vGlobIP[ip], time, si, c);
1012 for(
size_t fct = 0; fct < this->input_num_fct(c); ++fct)
1015 const size_t commonFct = this->input_common_fct(c, fct);
1018 for(
size_t dof = 0; dof < this->num_sh(fct); ++dof)
1021 mult_add(vvvDeriv[ip][commonFct][dof],
1023 m_vpDependData[c]->deriv(this->series_id(c,
s), ip, fct, dof));
1036 template <
typename TData,
int dim,
typename TDataIn>
1040 m_vpUserData.resize(num);
1041 m_vpDependData.resize(num);
1044 base_type::set_num_input(num);
1047 template <
typename TData,
int dim,
typename TDataIn>
1051 UG_ASSERT(i < m_vpUserData.size(),
"Input not needed");
1052 UG_ASSERT(i < m_vpDependData.size(),
"Input not needed");
1055 if(i >= this->num_input())
1056 UG_THROW(
"LuaUserFunction::set_input: Only " << this->num_input()
1057 <<
" inputs can be set. Use 'set_num_input' to increase"
1058 " the number of needed inputs.");
1061 m_vpUserData[i] = data;
1064 m_vpDependData[i] = data.template cast_dynamic<DependentUserData<TDataIn, dim> >();
1067 base_type::set_input(i, data, data);
1070 template <
typename TData,
int dim,
typename TDataIn>
1073 set_input(i, CreateConstUserData<dim>(val, TDataIn()));
1081 template <
typename TData,
typename TDataIn>
1088 template <
typename TData,
typename TDataIn>
1092 m_cbValueName = luaCallback;
1095 lua_getglobal(m_L, m_cbValueName.c_str());
1098 if(lua_isnil(m_L, -1)){
1099 UG_THROW(
"LuaFunction::set_lua_callback(...):"
1100 "Specified lua callback does not exist: " << m_cbValueName);
1104 m_cbValueRef = luaL_ref(m_L, LUA_REGISTRYINDEX);
1107 m_numArgs = numArgs;
1110 template <
typename TData,
typename TDataIn>
1114 UG_ASSERT(numArgs == (
int)m_numArgs,
"Number of arguments mismatched.");
1117 lua_rawgeti(m_L, LUA_REGISTRYINDEX, m_cbValueRef);
1121 va_start(ap, numArgs);
1124 for(
int i = 0; i < numArgs; ++i)
1127 TDataIn val = va_arg(ap, TDataIn);
1143 if(lua_pcall(m_L, argSize, retSize, 0) != 0)
1144 UG_THROW(
"LuaFunction::operator(...): Error while "
1145 "running callback '" << m_cbValueName <<
"',"
1146 " lua message: "<< lua_tostring(m_L, -1));
1153 UG_CATCH_THROW(
"LuaFunction::operator(...): Error while running "
1154 "callback '" << m_cbValueName <<
"'");
1157 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:1111
lua_State * m_L
lua state
Definition: lua_user_data.h:425
LuaFunction()
constructor
Definition: lua_user_data_impl.h:1082
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:1089
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:417
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:370
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:249
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:67
static std::string name()
returns name of UserData
Definition: lua_user_data_impl.h:84
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:94
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:284
virtual ~LuaUserData()
}
Definition: lua_user_data_impl.h:355
LuaUserFunction(const char *luaCallback, size_t numArgs)
constructor
Definition: lua_user_data_impl.h:445
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:740
virtual ~LuaUserFunction()
destructor frees the reference
Definition: lua_user_data_impl.h:510
void free_deriv_callback_ref(size_t arg)
frees callback-references for derivate callbacks
Definition: lua_user_data_impl.h:531
void evaluate(TData &value, const MathVector< dim > &globIP, number time, int si) const
Definition: lua_user_data_impl.h:909
void set_input(size_t i, SmartPtr< CplUserData< TDataIn, dim > > data)
set input value for paramter i
Definition: lua_user_data_impl.h:1049
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:823
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:959
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:522
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:601
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:541
void set_num_input(size_t num)
set number of needed inputs
Definition: lua_user_data_impl.h:1037
virtual void operator()(TData &out, int numArgs,...) const
evaluates the data
Definition: lua_user_data_impl.h:661
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:50
#define PROFILE_CALLBACK_END()
Definition: lua_user_data_impl.h:52
#define PROFILE_CALLBACK_BEGIN(name)
Definition: lua_user_data_impl.h:51
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