Chi-Tech
lbs_bndry_func_lua.cc
Go to the documentation of this file.
2
3#include "chi_lua.h"
4
5#include "chi_runtime.h"
6#include "chi_log.h"
8
9//###################################################################
10/**Customized boundary function by calling a lua routine.*/
12Evaluate(size_t cell_global_id,
13 int cell_material_id,
14 unsigned int face_index,
15 unsigned int face_node_index,
16 const chi_mesh::Vector3& face_node_location,
17 const chi_mesh::Vector3& face_node_normal,
18 const std::vector<int>& quadrature_angle_indices,
19 const std::vector<chi_mesh::Vector3>& quadrature_angle_vectors,
20 const std::vector<std::pair<double, double>>& quadrature_phi_theta_angles,
21 const std::vector<int>& group_indices,
22 double time)
23{
24 const std::string fname = "LinearBoltzmann::BoundaryFunctionToLua";
25 //======================================== Utility lambdas
26 auto PushVector3AsTable = [](lua_State* L, const chi_mesh::Vector3& vec)
27 {
28 lua_newtable(L);
29
30 lua_pushstring(L, "x");
31 lua_pushnumber(L, vec.x);
32 lua_settable(L, -3);
33
34 lua_pushstring(L, "y");
35 lua_pushnumber(L, vec.y);
36 lua_settable(L, -3);
37
38 lua_pushstring(L, "z");
39 lua_pushnumber(L, vec.z);
40 lua_settable(L, -3);
41 };
42
43 auto PushVecIntAsTable = [](lua_State* L, const std::vector<int>& vec)
44 {
45 lua_newtable(L);
46
47 for (int i=0; i<static_cast<int>(vec.size()); ++i)
48 {
49 lua_pushinteger(L, i+1);
50 lua_pushinteger(L, static_cast<lua_Integer>(vec[i]));
51 lua_settable(L, -3);
52 }
53 };
54
55 auto PushPhiThetaPairTable = [](lua_State* L, const std::pair<double, double>& phi_theta)
56 {
57 lua_newtable(L);
58
59 lua_pushstring(L, "phi");
60 lua_pushnumber(L, phi_theta.first);
61 lua_settable(L, -3);
62
63 lua_pushstring(L, "theta");
64 lua_pushnumber(L, phi_theta.second);
65 lua_settable(L, -3);
66 };
67
68 //======================================== Get lua function
69 lua_State* L = Chi::console.GetConsoleState();
70 lua_getglobal(L, m_lua_function_name.c_str());
71
72 //======================================== Error check lua function
73 if (not lua_isfunction(L, -1))
74 throw std::logic_error(fname + " attempted to access lua-function, " +
75 m_lua_function_name + ", but it seems the function"
76 " could not be retrieved.");
77
78 //======================================== Push arguments
79 lua_pushinteger(L, static_cast<lua_Integer>(cell_global_id));
80 lua_pushinteger(L, static_cast<lua_Integer>(cell_material_id));
81
82 PushVector3AsTable(L, face_node_location);
83 PushVector3AsTable(L, face_node_normal);
84
85 PushVecIntAsTable(L, quadrature_angle_indices);
86
87 {
88 lua_newtable(L);
89 int n=0;
90 for (auto& omega : quadrature_angle_vectors)
91 {
92 lua_pushinteger(L, n+1);
93 PushVector3AsTable(L, omega);
94 lua_settable(L, -3);
95 ++n;
96 }
97 }//push omegas
98
99 {
100 lua_newtable(L);
101 int n=0;
102 for (auto& phi_theta : quadrature_phi_theta_angles)
103 {
104 lua_pushinteger(L, n+1);
105 PushPhiThetaPairTable(L, phi_theta);
106 lua_settable(L, -3);
107 ++n;
108 }
109 }//push phi_theta_pairs
110
111 PushVecIntAsTable(L, group_indices);
112
113 lua_pushnumber(L, time);
114
115 std::vector<double> psi;
116 //9 arguments, 1 result (table), 0=original error object
117 if (lua_pcall(L,9,1,0) == 0)
118 {
119 LuaCheckTableValue(fname, L, -1);
120 size_t table_length = lua_rawlen(L, -1);
121 psi.reserve(table_length);
122 for (size_t i=0; i<table_length; ++i)
123 {
124 lua_pushinteger(L, static_cast<lua_Integer>(i)+1);
125 lua_gettable(L, -2);
126 psi.push_back(lua_tonumber(L,-1));
127 lua_pop(L, 1);
128 }
129 }
130 else
131 throw std::logic_error(fname + " attempted to call lua-function, " +
132 m_lua_function_name + ", but the call failed.");
133
134 lua_pop(L,1); //pop the table, or error code
135
136 //======================================== Error check psi vector
137 size_t num_angles = quadrature_angle_indices.size();
138 size_t num_groups = group_indices.size();
139
140 if (psi.size() != (num_angles*num_groups))
141 throw std::logic_error(fname + " the returned vector from lua-function, " +
142 m_lua_function_name + ", did not produce the required size vector. " +
143 "The size must equal num_angles*num_groups, " +
144 std::to_string(num_angles*num_groups) + ", but the size is " +
145 std::to_string(psi.size()) + ".");
146
147 return psi;
148}
static chi::Console & console
Definition: chi_runtime.h:80
lua_State *& GetConsoleState()
Definition: chi_console.h:130
const std::string m_lua_function_name
std::vector< double > Evaluate(size_t cell_global_id, int cell_material_id, unsigned int face_index, unsigned int face_node_index, const chi_mesh::Vector3 &face_node_location, const chi_mesh::Vector3 &face_node_normal, const std::vector< int > &quadrature_angle_indices, const std::vector< chi_mesh::Vector3 > &quadrature_angle_vectors, const std::vector< std::pair< double, double > > &quadrature_phi_theta_angles, const std::vector< int > &group_indices, double time) override