Chi-Tech
function_lua_dimA_to_dimB.cc
Go to the documentation of this file.
2
5#include "chi_lua.h"
6
7#include "ChiObjectFactory.h"
8
10{
11
13
15{
17
18 // Inherits input_dimension and output_dimension
19
20 // clang-format off
21 params.SetGeneralDescription("Lua based parsed function");
22 params.SetDocGroup("DocMathFunctions");
23 // clang-format on
24
25 params.AddRequiredParameter<std::string>("lua_function_name",
26 "Name of the lua function");
27
28 return params;
29}
30
32 : FunctionDimAToDimB(params),
33 lua_function_name_(params.GetParamValue<std::string>("lua_function_name"))
34{
35}
36
37std::vector<double>
38LuaDimAToDimB::Evaluate(const std::vector<double>& vals) const
39{
40 const std::string fname = __PRETTY_FUNCTION__;
41 lua_State* L = Chi::console.GetConsoleState();
42 lua_getglobal(L, lua_function_name_.c_str());
43
44 ChiLogicalErrorIf(not lua_isfunction(L, -1),
45 std::string("Attempted to access lua-function, ") +
47 ", but it seems the function could "
48 "not be retrieved.");
49
50 const size_t num_vals = vals.size();
51
53 num_vals != InputDimension(),
54 std::string("Number of inputs do not match. ") +
55 "Attempted to evaluate with " + std::to_string(num_vals) +
56 " parameters but requires " + std::to_string(InputDimension()));
57
58 lua_newtable(L);
59 lua_Integer k=0;
60 for (double val : vals)
61 {
62 lua_pushinteger(L, ++k);
63 lua_pushnumber(L, val);
64 lua_settable(L, -3);
65 }
66
67 std::vector<double> result;
68 // 1 arguments, 1 result (table), 0=original error object
69 if (lua_pcall(L, 1, 1, 0) == 0)
70 {
71 LuaCheckTableValue(fname, L, -1);
72 size_t table_length = lua_rawlen(L, -1);
73 result.reserve(table_length);
74 for (size_t i = 0; i < table_length; ++i)
75 {
76 lua_pushinteger(L, static_cast<lua_Integer>(i) + 1);
77 lua_gettable(L, -2);
78 result.push_back(lua_tonumber(L, -1));
79 lua_pop(L, 1);
80 }
81 }
82 else
83 throw std::logic_error(fname + " attempted to call lua-function, " +
84 lua_function_name_ + ", but the call failed. " +
85 lua_tostring(L, -1));
86
88 result.size() != OutputDimension(),
89 std::string("Number of outputs after the function was ") +
90 "called does not "
91 "match the function specifications. A table is expected with " +
92 std::to_string(OutputDimension()) + " entries.");
93
94 return result;
95}
96
97} // namespace chi_math::functions
#define ChiLogicalErrorIf(condition, message)
#define ChiInvalidArgumentIf(condition, message)
static chi::Console & console
Definition: chi_runtime.h:80
lua_State *& GetConsoleState()
Definition: chi_console.h:130
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
static chi::InputParameters GetInputParameters()
std::vector< double > Evaluate(const std::vector< double > &vals) const override
static chi::InputParameters GetInputParameters()
LuaDimAToDimB(const chi::InputParameters &params)
RegisterChiObject(chi_math::functions, LuaDimAToDimB)