Line data Source code
1 : /*
2 : * Copyright (c) 2013-2014: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef LUA_TRAITS_H
34 : #define LUA_TRAITS_H
35 :
36 : #include "common/util/number_util.h"
37 :
38 : namespace ug{
39 : ///////////////////////////////////////////////////////////////////////////////
40 : // Lua Traits
41 : ///////////////////////////////////////////////////////////////////////////////
42 :
43 :
44 : /// Helper to access a return value on the stack.
45 : /** If the value can't be converted to a number, an error is thrown*/
46 0 : inline number ReturnValueToNumber(lua_State* L, int index){
47 0 : if(!lua_isnumber(L, index))
48 0 : UG_THROW("ReturnValueToNumber: Data passed from Lua: "
49 : "Can't convert return value to number!");
50 0 : number a = lua_tonumber(L, index);
51 0 : UG_COND_THROW(IsFiniteAndNotTooBig(a) == false, a << " not finite");
52 0 : return a;
53 :
54 : }
55 :
56 : /// Helper to access a return value on the stack.
57 : /** If the value can't be converted to a boolean, an error is thrown*/
58 0 : inline number ReturnValueToBool(lua_State* L, int index){
59 0 : if(!lua_isboolean(L, index))
60 0 : UG_THROW("ReturnValueToBool: Data passed from Lua: "
61 : "Can't convert return value to boolean!");
62 :
63 0 : return lua_toboolean(L, index);
64 : }
65 :
66 : /// Helper to access a return value on the stack.
67 : /** If the value can't be converted to a integer, an error is thrown*/
68 0 : inline int ReturnValueToInteger(lua_State* L, int index){
69 0 : if(!lua_isnumber(L, index))
70 0 : UG_THROW("ReturnValueToBool: Data passed from Lua: "
71 : "Can't convert return value to integer!");
72 :
73 0 : return lua_tointeger(L, index);
74 : }
75 :
76 :
77 : /// Lua Traits to push/pop on lua stack
78 : template <typename TData>
79 : struct lua_traits;
80 :
81 : template <>
82 : struct lua_traits<void>
83 : {
84 : static const int size = 0;
85 :
86 : static void push(lua_State* L, const bool&){}
87 :
88 : static void read(lua_State* L, bool&, int index = -1){}
89 :
90 : static void do_return(const bool&) {return;}
91 :
92 : static bool check(lua_State* L, int index = -1){return true;}
93 :
94 : static std::string signature()
95 : {
96 : return "void";
97 : }
98 :
99 : static std::string name()
100 : {
101 : return "void";
102 : }
103 : };
104 :
105 : template <>
106 : struct lua_traits<bool>
107 : {
108 : static const int size = 1;
109 :
110 : static void push(lua_State* L, const bool& b)
111 : {
112 : lua_pushboolean(L, b);
113 : }
114 :
115 : static void read(lua_State* L, bool& b, int index = -1)
116 : {
117 0 : b = ReturnValueToBool(L, index);
118 : }
119 :
120 : static bool check(lua_State* L, int index = -1)
121 : {
122 0 : return lua_isboolean(L, index);
123 : }
124 :
125 : static bool do_return(const bool& b) {return b;}
126 :
127 : static std::string signature()
128 : {
129 0 : return "bool";
130 : }
131 :
132 : static std::string name()
133 : {
134 : return "Bool";
135 : }
136 : };
137 :
138 : template <>
139 : struct lua_traits<int>
140 : {
141 : static const int size = 1;
142 :
143 : static void push(lua_State* L, const int& c)
144 : {
145 0 : lua_pushinteger(L, c);
146 0 : }
147 :
148 : static void read(lua_State* L, int& c, int index = -1)
149 : {
150 0 : c = ReturnValueToInteger(L, index);
151 : }
152 :
153 : static bool check(lua_State* L, int index = -1)
154 : {
155 : return lua_isnumber(L, index);
156 : }
157 :
158 : static std::string signature()
159 : {
160 : return "integer";
161 : }
162 :
163 : static std::string name()
164 : {
165 : return "Integer";
166 : }
167 : };
168 :
169 : template <>
170 : struct lua_traits<number>
171 : {
172 : static const int size = 1;
173 :
174 : static void push(lua_State* L, const number& c)
175 : {
176 0 : lua_pushnumber(L, c);
177 : }
178 :
179 : static void read(lua_State* L, number& c, int index = -1)
180 : {
181 0 : c = ReturnValueToNumber(L, index);
182 : }
183 :
184 : static void read(number &c, double ret[1], bool *dummy)
185 : {
186 : c = ret[1];
187 : }
188 : static void read(number &c, double ret[1], void *dummy)
189 : {
190 : c = ret[0];
191 : }
192 :
193 : static bool check(lua_State* L, int index = -1)
194 : {
195 0 : return lua_isnumber(L, index);
196 : }
197 :
198 : static std::string signature()
199 : {
200 0 : return "number";
201 : }
202 :
203 : static std::string name()
204 : {
205 0 : return "Number";
206 : }
207 : };
208 :
209 : template <std::size_t dim>
210 : struct lua_traits< ug::MathVector<dim> >
211 : {
212 : static const int size = dim;
213 :
214 : static void push(lua_State* L, const MathVector<dim>& x)
215 : {
216 0 : for(size_t i = 0; i < dim; ++i)
217 0 : lua_pushnumber(L, x[i]);
218 : }
219 :
220 : static void read(lua_State* L, MathVector<dim>& x, int index = -1)
221 : {
222 0 : for(size_t i = 0; i < dim; ++i){
223 0 : x[dim-1-i] = ReturnValueToNumber(L, index--);
224 : }
225 : }
226 :
227 : static void read(MathVector<dim>& x, const double ret[dim], bool *dummy)
228 : {
229 : for(size_t i=0; i<dim; i++)
230 : x[i] = ret[i+1];
231 : }
232 :
233 : static void read(MathVector<dim>& x, const double ret[dim], void *dummy)
234 : {
235 : for(size_t i=0; i<dim; i++)
236 : x[i] = ret[i];
237 : }
238 :
239 : static bool check(lua_State* L, int index = -1)
240 : {
241 0 : for(size_t i = 0; i < dim; ++i){
242 0 : if(!lua_isnumber(L, index--)) return false;
243 : }
244 : return true;
245 : }
246 :
247 0 : static std::string signature()
248 : {
249 : static char cmp[] = {'x', 'y', 'z'};
250 0 : std::stringstream ss;
251 0 : for(size_t i = 0; i < dim; ++i){
252 0 : if(i != 0) ss << ", ";
253 0 : ss << "v" << cmp[i];
254 : }
255 0 : return ss.str();
256 0 : }
257 :
258 0 : static std::string name()
259 : {
260 0 : std::stringstream ss;
261 0 : ss << "Vector";
262 0 : return ss.str();
263 0 : }
264 : };
265 :
266 :
267 : template <std::size_t dim>
268 : struct lua_traits< MathMatrix<dim, dim> >
269 : {
270 : static const int size = dim*dim;
271 :
272 : static void push(lua_State* L, const MathMatrix<dim, dim>& D)
273 : {
274 : for(size_t i = 0; i < dim; ++i){
275 : for(size_t j = 0; j < dim; ++j){
276 : lua_pushnumber(L, D[i][j]);
277 : }
278 : }
279 :
280 : }
281 :
282 0 : static void read(lua_State* L, MathMatrix<dim, dim>& D, int index = -1)
283 : {
284 0 : for(size_t i = 0; i < dim; ++i){
285 0 : for(size_t j = 0; j < dim; ++j){
286 0 : D[dim-1-i][dim-1-j] = ReturnValueToNumber(L, index--);
287 : }
288 : }
289 0 : }
290 :
291 : static void read(MathMatrix<dim, dim>& D, const double ret[size], bool *dummy)
292 : {
293 : for(size_t i = 0; i < dim; ++i){
294 : for(size_t j = 0; j < dim; ++j){
295 : D[i][j] = ret[i*dim+j+1];
296 : }
297 : }
298 : }
299 : static void read(MathMatrix<dim, dim>& D, const double ret[size], void *dummy)
300 : {
301 : for(size_t i = 0; i < dim; ++i){
302 : for(size_t j = 0; j < dim; ++j){
303 : D[i][j] = ret[i*dim+j];
304 : }
305 : }
306 : }
307 :
308 0 : static bool check(lua_State* L, int index = -1)
309 : {
310 0 : for(size_t i = 0; i < dim; ++i){
311 0 : for(size_t j = 0; j < dim; ++j){
312 0 : if(!lua_isnumber(L, index--)) return false;
313 : }
314 : }
315 : return true;
316 : }
317 :
318 0 : static std::string signature()
319 : {
320 : static char cmp[] = {'x', 'y', 'z'};
321 0 : std::stringstream ss;
322 0 : for(size_t i = 0; i < dim; ++i){
323 0 : for(size_t j = 0; j < dim; ++j){
324 0 : if(i != 0 || j != 0) ss << ", ";
325 0 : ss << "D" << cmp[i] << cmp[j];
326 : }
327 : }
328 0 : return ss.str();
329 0 : }
330 :
331 0 : static std::string name()
332 : {
333 0 : std::stringstream ss;
334 0 : ss << "Matrix";
335 0 : return ss.str();
336 0 : }
337 : };
338 :
339 : /* for debugging callbacks
340 : *
341 : if(compare(out, out2)==false)
342 : {
343 : UG_LOG("\neval_value------------\n" << m_luaC.name << "\n" << out << "\n--\n" << out2 << "\n------------\n");
344 : for(int i=0; i<lua_traits<TData>::size+1; i++)
345 : UG_LOG(ret[i] << "\n");
346 : }
347 :
348 : inline bool compare(double &a, double &b)
349 : {
350 : return a == b;
351 : }
352 :
353 : inline bool compare(MathVector<3, double> &x, MathVector<3, double> &y)
354 : {
355 : for(int i=0; i<3; i++)
356 : if(x[i] != y[i]) return false;
357 : return true;
358 : }
359 :
360 : inline bool compare(MathVector<2, double> &x, MathVector<2, double> &y)
361 : {
362 : for(int i=0; i<2; i++)
363 : if(x[i] != y[i]) return false;
364 : return true;
365 : }
366 :
367 : inline bool compare(MathMatrix<3, 3, double> &A, MathMatrix<3, 3, double> &B)
368 : {
369 : for(int j=0; j<3; j++)
370 : for(int i=0; i<3; i++)
371 : if(A[i][j] != B[i][j])
372 : return false;
373 : return true;
374 :
375 : }
376 : inline bool compare(MathMatrix<2, 2, double> &A, MathMatrix<2, 2, double> &B)
377 : {
378 : for(int j=0; j<2; j++)
379 : for(int i=0; i<2; i++)
380 : if(A[i][j] != B[i][j])
381 : return false;
382 : return true;
383 :
384 : }*/
385 :
386 : }
387 : #endif /* LUA_TRAITS_H */
388 :
389 :
|