Chi-Tech
lbs_adj_response_function.cc
Go to the documentation of this file.
2
3#include "chi_lua.h"
4
5#include "chi_runtime.h"
7#include "chi_log.h"
8
9//###################################################################
10/** Calls the lua function associated with the response function and
11 * returns a multigroup vector of the source values.*/
13 GetMGResponse(const chi_mesh::Cell &cell, const size_t num_groups) const
14{
15 const std::string fname = __FUNCTION__;
16
17 std::vector<double> response(num_groups, 0.0);
18
19 //======================================== Utility lambdas
20 auto PushVector3AsTable = [](lua_State* L, const chi_mesh::Vector3& vec)
21 {
22 lua_newtable(L);
23
24 lua_pushstring(L, "x");
25 lua_pushnumber(L, vec.x);
26 lua_settable(L, -3);
27
28 lua_pushstring(L, "y");
29 lua_pushnumber(L, vec.y);
30 lua_settable(L, -3);
31
32 lua_pushstring(L, "z");
33 lua_pushnumber(L, vec.z);
34 lua_settable(L, -3);
35 };
36
37 //============================================= Check response function given
38 // Return default if none provided
39 if (lua_functional.empty())
40 {
41 response.assign(num_groups, 1.0);
42 return response;
43 }
44
45 //============================================= Load lua function
46 lua_State* L = Chi::console.GetConsoleState();
47 lua_getglobal(L, lua_functional.c_str());
48
49
50 //============================================= Error check lua function
51 if (not lua_isfunction(L, -1))
52 throw std::logic_error(fname + " attempted to access lua-function, " +
53 lua_functional + ", but it seems the function"
54 " could not be retrieved.");
55
56 //============================================= Push arguments
57 PushVector3AsTable(L, cell.centroid_);
58 lua_pushinteger(L, cell.material_id_); //4 arguments on stack
59
60 //============================================= Call lua function
61 //2 arguments, 1 result (table), 0=original error object
62 std::vector<double> lua_return;
63 if (lua_pcall(L,2,1,0) == 0)
64 {
65 LuaCheckTableValue(fname, L, -1);
66 const size_t table_length = lua_rawlen(L, -1);
67 lua_return.reserve(table_length);
68 for (size_t i=0; i<table_length; ++i)
69 {
70 lua_pushinteger(L, static_cast<lua_Integer>(i)+1);
71 lua_gettable(L, -2);
72 lua_return.push_back(lua_tonumber(L,-1));
73 lua_pop(L, 1);
74 }
75 }
76 else
77 throw std::logic_error(fname + " attempted to call lua-function, " +
78 lua_functional + ", but the call failed.");
79
80 lua_pop(L,1); //pop the table, or error code
81
82 //============================================= Check return value
83 if (lua_return.size() > response.size())
84 throw std::logic_error(fname + " Call lua-function, " +
85 lua_functional + ", returned a vector of size " +
86 std::to_string(response.size()) +
87 " which is greater than the number of groups " +
88 std::to_string(num_groups) + ".");
89
90 for (size_t g=0; g<num_groups; ++g)
91 response[g] = lua_return[g];
92
93 return response;
94}
static chi::Console & console
Definition: chi_runtime.h:80
lua_State *& GetConsoleState()
Definition: chi_console.h:130
int material_id_
Definition: cell.h:79
Vertex centroid_
Definition: cell.h:78
std::vector< double > GetMGResponse(const chi_mesh::Cell &cell, size_t num_groups) const