31 #include <momemta/InputTag.h> 32 #include <momemta/ILuaCallback.h> 33 #include <momemta/Logging.h> 34 #include <momemta/ModuleFactory.h> 35 #include <momemta/ModuleRegistry.h> 36 #include <momemta/ParameterSet.h> 38 #include <ExecutionPath.h> 39 #include <lua/LazyTable.h> 40 #include <lua/ParameterSetParser.h> 43 #include <lua/utils.h> 45 void execute_string(std::shared_ptr<lua_State> L,
const std::string& code) {
46 if (luaL_dostring(L.get(), code.c_str())) {
47 std::string error = lua_tostring(L.get(), -1);
56 virtual void onModuleDeclared(
const std::string& type,
const std::string& name)
override {
57 modules.push_back({type, name});
61 integrands.push_back(tag);
65 paths.push_back(path);
73 inputs.push_back(name);
76 std::vector<std::pair<std::string, std::string>> modules;
77 std::vector<InputTag> integrands;
78 std::vector<ExecutionPath> paths;
79 std::size_t n_dimensions;
80 std::vector<std::string> inputs;
85 using lua::LazyTable::LazyTable;
88 virtual void freeze()
override {
89 lua::LazyTable::freeze();
93 TEST_CASE(
"lua parsing utilities",
"[lua]") {
96 auto default_log_level = logging::level::warning;
97 logging::set_level(default_log_level);
100 REQUIRE(luaCallback.modules.empty());
103 auto stack_size = lua_gettop(L.get());
105 SECTION(
"custom functions") {
107 logging::set_level(logging::level::fatal);
108 execute_string(L,
"load_modules('not_existing.so')");
109 logging::set_level(default_log_level);
111 execute_string(L,
"parameter('not_existing')");
115 execute_string(L,
"index1 = add_dimension()");
116 lua_getglobal(L.get(),
"index1");
118 REQUIRE( (momemta::any_cast<InputTag>(value.first)).toString() ==
"cuba::ps_points/1");
119 execute_string(L,
"index2 = add_dimension()");
120 lua_getglobal(L.get(),
"index2");
122 REQUIRE( (momemta::any_cast<InputTag>(value.first)).toString() ==
"cuba::ps_points/2");
125 REQUIRE( luaCallback.n_dimensions == 2 );
127 execute_string(L,
"integrand('integrand1::output', 'integrand2::output')");
128 REQUIRE( luaCallback.integrands.size() == 2 );
129 REQUIRE( luaCallback.integrands.at(1).toString() ==
"integrand2::output" );
132 SECTION(
"defining modules") {
133 execute_string(L,
"BreitWignerGenerator.test = {}");
134 REQUIRE(luaCallback.modules.size() == 1);
135 REQUIRE(luaCallback.modules.back().first ==
"BreitWignerGenerator");
136 REQUIRE(luaCallback.modules.back().second ==
"test");
138 execute_string(L,
"BreitWignerGenerator.test2 = {}");
139 REQUIRE(luaCallback.modules.size() == 2);
140 REQUIRE(luaCallback.modules.back().second ==
"test2");
143 SECTION(
"loading modules") {
144 momemta::ModuleList modules;
147 auto n_modules = modules.size();
149 execute_string(L,
"load_modules('libempty_module.so')");
153 REQUIRE(modules.size() == n_modules + 1);
156 SECTION(
"parsing values") {
158 lua_pushinteger(L.get(), 42);
160 REQUIRE(value.first.type() ==
typeid(int64_t));
161 REQUIRE(momemta::any_cast<int64_t>(value.first) == 42);
162 REQUIRE_FALSE(value.second);
166 lua_pushnumber(L.get(), 38.5);
168 REQUIRE(value.first.type() ==
typeid(double));
169 REQUIRE(momemta::any_cast<double>(value.first) == Approx(38.5));
170 REQUIRE_FALSE(value.second);
174 lua_pushboolean(L.get(),
true);
176 REQUIRE(value.first.type() ==
typeid(bool));
177 REQUIRE(momemta::any_cast<bool>(value.first) ==
true);
178 REQUIRE_FALSE(value.second);
182 lua_pushliteral(L.get(),
"lua is fun");
184 REQUIRE(value.first.type() ==
typeid(std::string));
185 REQUIRE(momemta::any_cast<std::string>(value.first) ==
"lua is fun");
186 REQUIRE_FALSE(value.second);
190 execute_string(L,
"return {0.1, 0.2, 0.3}");
192 REQUIRE(value.first.type() ==
typeid(std::vector<double>));
193 REQUIRE_FALSE(value.second);
195 auto v = momemta::any_cast<std::vector<double>>(value.first);
196 REQUIRE(v.size() == 3);
197 REQUIRE(v[0] == Approx(0.1));
198 REQUIRE(v[1] == Approx(0.2));
199 REQUIRE(v[2] == Approx(0.3));
204 execute_string(L,
"return {0.1, 2, 0.3}");
206 REQUIRE(value.first.type() ==
typeid(std::vector<double>));
207 REQUIRE_FALSE(value.second);
209 auto v = momemta::any_cast<std::vector<double>>(value.first);
210 REQUIRE(v.size() == 3);
211 REQUIRE(v[0] == Approx(0.1));
212 REQUIRE(v[1] == Approx(2));
213 REQUIRE(v[2] == Approx(0.3));
218 execute_string(L,
"return {1, 2, 3}");
220 REQUIRE(value.first.type() ==
typeid(std::vector<int64_t>));
221 REQUIRE_FALSE(value.second);
223 auto v = momemta::any_cast<std::vector<int64_t>>(value.first);
224 REQUIRE(v.size() == 3);
232 execute_string(L,
"return {1, 'string', false}");
237 SECTION(
"parsing lazy values") {
240 execute_string(L,
"parameters = { top_mass = 173. }");
242 SECTION(
"lazy function") {
243 execute_string(L,
"return parameter('top_mass')");
245 REQUIRE(value.second ==
true);
248 auto fct_evaluated = fct();
249 REQUIRE(fct_evaluated.type() ==
typeid(double));
250 REQUIRE(momemta::any_cast<double>(fct_evaluated) == Approx(173.));
254 SECTION(
"lazy function after modification of parameter") {
255 execute_string(L,
"return parameter('top_mass')");
257 REQUIRE(value.second ==
true);
261 lua_getglobal(L.get(),
"parameters");
262 lua_pushnumber(L.get(), 175.);
263 lua_setfield(L.get(), -2,
"top_mass");
267 auto fct_evaluated = fct();
268 REQUIRE(fct_evaluated.type() ==
typeid(double));
269 REQUIRE(momemta::any_cast<double>(fct_evaluated) == Approx(175.));
273 SECTION(
"lazy table field") {
276 SECTION(
"evaluation") {
278 REQUIRE(value.type() ==
typeid(double));
279 REQUIRE(momemta::any_cast<double>(value) == Approx(173.));
286 REQUIRE(value.type() ==
typeid(double));
287 REQUIRE(momemta::any_cast<double>(value) == Approx(175.));
292 SECTION(
"ParameterSet evaluation") {
293 auto def = R
"(test_table = { 297 inputtag = "module::parameter", 298 vector = {0, 1, 2, 3} 301 execute_string(L, def); 303 int type = lua_getglobal(L.get(),
"test_table");
304 REQUIRE(type == LUA_TTABLE);
309 REQUIRE(p.existsAs<int64_t>(
"integer"));
310 REQUIRE(p.get<int64_t>(
"integer") == 1);
312 REQUIRE(p.existsAs<
double>(
"float"));
313 REQUIRE(p.get<
double>(
"float") == Approx(10.));
315 REQUIRE(p.existsAs<std::string>(
"string"));
316 REQUIRE(p.get<std::string>(
"string") ==
"test");
318 auto i =
InputTag(
"module",
"parameter");
319 REQUIRE(p.existsAs<
InputTag>(
"inputtag"));
320 REQUIRE(p.get<
InputTag>(
"inputtag") == i);
322 REQUIRE(p.existsAs<std::vector<int64_t>>(
"vector"));
323 auto v = p.get<std::vector<int64_t>>(
"vector");
324 REQUIRE(v.size() == 4);
333 SECTION(
"LazyParameterSet evaluation") {
334 auto def = R
"(test_table = { 338 inputtag = "module::parameter" 341 execute_string(L, def); 343 int type = lua_getglobal(L.get(),
"test_table");
344 REQUIRE(type == LUA_TTABLE);
352 REQUIRE(f.existsAs<int64_t>(
"integer"));
353 REQUIRE(f.get<int64_t>(
"integer") == 1);
355 REQUIRE(f.existsAs<
double>(
"float"));
356 REQUIRE(f.get<
double>(
"float") == Approx(10.));
358 REQUIRE(f.existsAs<std::string>(
"string"));
359 REQUIRE(f.get<std::string>(
"string") ==
"test");
361 auto i =
InputTag(
"module",
"parameter");
362 REQUIRE(f.existsAs<
InputTag>(
"inputtag"));
363 REQUIRE(f.get<
InputTag>(
"inputtag") == i);
368 p.
set(
"integer", 10);
371 p.
set(
"float",
true);
379 REQUIRE(f.existsAs<int64_t>(
"integer"));
380 REQUIRE(f.get<int64_t>(
"integer") == 10);
382 REQUIRE(f.existsAs<
bool>(
"float"));
383 REQUIRE(f.get<
bool>(
"float") ==
true);
385 REQUIRE(f.existsAs<std::string>(
"string"));
386 REQUIRE(f.get<std::string>(
"string") ==
"test");
388 REQUIRE(f.existsAs<
InputTag>(
"inputtag"));
389 REQUIRE(f.get<
InputTag>(
"inputtag") == i);
391 REQUIRE(f.existsAs<
double>(
"new"));
392 REQUIRE(f.get<
double>(
"new") == Approx(125));
397 SECTION(
"LazyParameterSet with non-existing table") {
399 int type = lua_getglobal(L.get(),
"test_table");
401 REQUIRE(type == LUA_TNIL);
406 type = lua_getglobal(L.get(),
"test_table");
408 REQUIRE(type == LUA_TNIL);
410 p.
set(
"key",
"value");
413 type = lua_getglobal(L.get(),
"test_table");
415 REQUIRE(type == LUA_TTABLE);
419 REQUIRE(p.existsAs<std::string>(
"key"));
420 REQUIRE(p.get<std::string>(
"key") ==
"value");
424 auto def = R
"(path = Path("a", "b", "c"))"; 425 execute_string(L, def); 427 auto type = lua_getglobal(L.get(),
"path");
428 REQUIRE(type == LUA_TUSERDATA);
431 REQUIRE(type_name == LUA_PATH_TYPE_NAME);
434 REQUIRE(path !=
nullptr);
436 REQUIRE(path->elements.size() == 3);
437 REQUIRE(path->elements[0] ==
"a");
438 REQUIRE(path->elements[1] ==
"b");
439 REQUIRE(path->elements[2] ==
"c");
444 SECTION(
"Path to momemta::any") {
445 auto def = R
"(path = Path("a"))"; 446 execute_string(L, def); 448 auto type = lua_getglobal(L.get(),
"path");
449 REQUIRE(type == LUA_TUSERDATA);
457 REQUIRE(stack_size == lua_gettop(L.get()));
Notification callback used for communication between the lua file and MoMEMta.
virtual void onNewPath(const ExecutionPath &path) override
A new path is declared in the configuration file.
A specialization of ParameterSet for lazy loading of lua tables.
std::shared_ptr< lua_State > init_runtime(ILuaCallback *callback)
Initialize the lua runtime.
virtual void addIntegrationDimension() override
A new integration dimension is requested in the configuration file.
virtual void onIntegrandDeclared(const InputTag &tag) override
The integrand was defined in the configuration file.
Lua binding of C++ Path class.
ExecutionPath * path_get(lua_State *L, int index)
Retrieve an instance of Path from the lua stack.
Lazy table field in lua (delayed table access)
std::enable_if< std::is_same< T, bool >::value||std::is_same< T, InputTag >::value >::type set(const std::string &name, const T &value)
Change the value of a given parameter. If the parameter does not exist, it's first created...
static ModuleRegistry & get()
A singleton available at startup.
static void parse(ParameterSet &p, lua_State *L, int index)
Convert a lua table to a ParameterSet.
virtual void onModuleDeclared(const std::string &type, const std::string &name) override
A module is declared in the configuration file.
Generic functions to deal with custom lua types.
A class encapsulating a lua table.
momemta::any get_custom_type_ptr(lua_State *L, int index)
Convert a lua custom table to a momemta::any value.
std::string get_custom_type_name(lua_State *L, int index)
Get the type of a custom table.
void exportList(bool ignore_internal, ModuleList &list) const
std::pair< momemta::any, bool > to_any(lua_State *L, int index)
Convert a lua type to momemta::any.
virtual void onNewInputDeclared(const std::string &name) override
The configuration file declared a new input.
Lazy function in lua (delayed function evaluation)