From 475560a3cc2ed9c510d516686ffdb599c4a88b2a Mon Sep 17 00:00:00 2001 From: VCB Ferreira Date: Sat, 8 Aug 2026 13:52:58 +0200 Subject: [PATCH] Parameters loaded from lua file without rebuilding --- CMakeLists.txt | 5 +++ nufi/lua_config.h | 84 ++++++++++++++++++++++++++++++++++++ nufi/parameters.h | 107 ++++++++++++++++++++++++++++++++-------------- parameters.lua | 86 +++++++++++++++++++++++++++++++++++++ src/main.cc | 4 ++ 5 files changed, 253 insertions(+), 33 deletions(-) create mode 100644 nufi/lua_config.h create mode 100644 parameters.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index 75c53f9..a0e4ee3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,9 @@ deal_ii_initialize_cached_variables() find_package(OpenMP REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(LUA REQUIRED lua5.4) + # ------------------------- add_library(nufi_lib @@ -31,12 +34,14 @@ add_library(nufi_lib target_include_directories(nufi_lib PUBLIC ${CMAKE_SOURCE_DIR} + ${LUA_INCLUDE_DIRS} ) deal_ii_setup_target(nufi_lib) target_link_libraries(nufi_lib OpenMP::OpenMP_CXX + ${LUA_LIBRARIES} ) target_compile_options(nufi_lib PRIVATE -O3 -g -fno-omit-frame-pointer) diff --git a/nufi/lua_config.h b/nufi/lua_config.h new file mode 100644 index 0000000..e9837a1 --- /dev/null +++ b/nufi/lua_config.h @@ -0,0 +1,84 @@ +#pragma once + +#include +#include +#include +#include + +extern "C" { +#include +#include +#include +} + +using LuaValue = std::variant; + +class LuaConfig { +public: + LuaConfig(const std::string &filePath, const std::string &tableName) { + L = luaL_newstate(); + luaL_openlibs(L); + + if (luaL_dofile(L, filePath.c_str()) != LUA_OK) { + std::string err = lua_tostring(L, -1); + lua_close(L); + throw std::runtime_error("Error loading Lua file: " + err); + } + + lua_getglobal(L, tableName.c_str()); + if (!lua_istable(L, -1)) { + lua_close(L); + throw std::runtime_error("'" + tableName + + "' is not a table (or missing)"); + } + } + + ~LuaConfig() { + if (L) + lua_close(L); + } + + LuaConfig(const LuaConfig &) = delete; + LuaConfig &operator=(const LuaConfig &) = delete; + + template T get(const std::string &key) const { + lua_getfield(L, -1, key.c_str()); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + throw std::runtime_error("Missing key: " + key); + } + T value = extract(key); + lua_pop(L, 1); + std::cout << "[LuaConfig] " << key << " = " << value << "\n"; + return value; + } + +private: + lua_State *L = nullptr; + + template T extract(const std::string &key) const { + if constexpr (std::is_same_v) { + if (!lua_isinteger(L, -1) && !lua_isnumber(L, -1)) + throw std::runtime_error("Key '" + key + "' is not a number"); + return static_cast(lua_tointeger(L, -1)); + } else if constexpr (std::is_same_v) { + if (!lua_isnumber(L, -1)) + throw std::runtime_error("Key '" + key + "' is not a number"); + return static_cast(lua_tonumber(L, -1)); + } else if constexpr (std::is_same_v) { + if (!lua_isnumber(L, -1)) + throw std::runtime_error("Key '" + key + "' is not a number"); + return static_cast(lua_tonumber(L, -1)); + } else if constexpr (std::is_same_v) { + if (!lua_isboolean(L, -1)) + throw std::runtime_error("Key '" + key + "' is not a boolean"); + return lua_toboolean(L, -1) != 0; + } else if constexpr (std::is_same_v) { + if (!lua_isstring(L, -1)) + throw std::runtime_error("Key '" + key + "' is not a string"); + return std::string(lua_tostring(L, -1)); + } else { + static_assert(!sizeof(T *), "Unsupported type for LuaConfig::get()"); + } + } +}; diff --git a/nufi/parameters.h b/nufi/parameters.h index 23761e0..b164db7 100644 --- a/nufi/parameters.h +++ b/nufi/parameters.h @@ -1,60 +1,101 @@ #ifndef PARAMETERS_H #define PARAMETERS_H -#include #include -#include #include +#include "lua_config.h" + namespace Parameters { -constexpr unsigned int DIMENSION = 1; -constexpr double X_DOMAIN_LEFT = 0.0; -constexpr double X_DOMAIN_RIGHT = 4 * M_PI; -constexpr double LX = std::abs(X_DOMAIN_RIGHT - X_DOMAIN_LEFT); -constexpr double LX_INV = 1 / LX; +inline unsigned int DIMENSION; -constexpr double V_DOMAIN_LEFT = -10.; -constexpr double V_DOMAIN_RIGHT = 10.; +inline double X_DOMAIN_LEFT; +inline double X_DOMAIN_RIGHT; +inline double LX; +inline double LX_INV; -constexpr unsigned int NV = 256; -constexpr double DV = std::abs(V_DOMAIN_RIGHT - V_DOMAIN_LEFT) / NV; +inline double V_DOMAIN_LEFT; +inline double V_DOMAIN_RIGHT; + +inline unsigned int NV; +inline double DV; // f0_TYPE: // 0 -> twos-stream // 1 -> landau-damping // 2 -> maxwellian // 3 -> bump-on-tail -constexpr size_t f0_TYPE = 1; +inline size_t f0_TYPE; // deal.ii options -constexpr unsigned int GLOBAL_REFINEMENT = 8; -constexpr unsigned int FE_DEGREE = 3; -constexpr unsigned int CONVERGENCE_ITERATIONS = 10000; -constexpr double CONVERGENCE_LIMIT = 1e-8; +inline unsigned int GLOBAL_REFINEMENT; +inline unsigned int FE_DEGREE; +inline unsigned int CONVERGENCE_ITERATIONS; +inline double CONVERGENCE_LIMIT; // Adaptive refinement options -constexpr unsigned int REFINE_FREQUENCY = 50; -constexpr double REFINEMENT_TOP_FRACTION = 0.8; -constexpr double REFINEMENT_BOTTOM_FRACTION = 0.1; +inline unsigned int REFINE_FREQUENCY; +inline double REFINEMENT_TOP_FRACTION; +inline double REFINEMENT_BOTTOM_FRACTION; -// Gauge options -constexpr double GAUGE_DOMAIN_LEFT = 3.2; -constexpr double GAUGE_DOMAIN_RIGHT = 3.8; - -constexpr double EPS = 0.01; -constexpr double WAVE_NR = 0.5; -constexpr double F0_FACTOR = 0.39894228040143267793994; // 1/sqrt(2pi) +inline double EPS; +inline double WAVE_NR; +inline double F0_FACTOR; // 1/sqrt(2pi) // NUFI options -constexpr double DT = 1. / 10.; -constexpr unsigned int TMAX = 100; +inline double DT; +inline unsigned int TMAX; // Plotting options -constexpr int PLOT_FREQUENCY = 10; -constexpr size_t PLOT_NX = 512; -constexpr double PLOT_DX = LX / PLOT_NX; -const std::string PLOT_DIR = "results/"; +inline int PLOT_FREQUENCY; +inline size_t PLOT_NX; +inline double PLOT_DX; +inline std::string PLOT_DIR; + +inline void load_lua_config(const std::string &luaFilePath) { + LuaConfig config(luaFilePath, "parameters"); + + DIMENSION = static_cast(config.get("DIMENSION")); + + X_DOMAIN_LEFT = config.get("X_DOMAIN_LEFT"); + X_DOMAIN_RIGHT = config.get("X_DOMAIN_RIGHT"); + LX = config.get("LX"); + LX_INV = config.get("LX_INV"); + + V_DOMAIN_LEFT = config.get("V_DOMAIN_LEFT"); + V_DOMAIN_RIGHT = config.get("V_DOMAIN_RIGHT"); + + NV = static_cast(config.get("NV")); + DV = config.get("DV"); + + f0_TYPE = static_cast(config.get("f0_TYPE")); + + GLOBAL_REFINEMENT = + static_cast(config.get("GLOBAL_REFINEMENT")); + FE_DEGREE = static_cast(config.get("FE_DEGREE")); + CONVERGENCE_ITERATIONS = + static_cast(config.get("CONVERGENCE_ITERATIONS")); + CONVERGENCE_LIMIT = config.get("CONVERGENCE_LIMIT"); + + REFINE_FREQUENCY = + static_cast(config.get("REFINE_FREQUENCY")); + REFINEMENT_TOP_FRACTION = config.get("REFINEMENT_TOP_FRACTION"); + REFINEMENT_BOTTOM_FRACTION = config.get("REFINEMENT_BOTTOM_FRACTION"); + + EPS = config.get("EPS"); + WAVE_NR = config.get("WAVE_NR"); + F0_FACTOR = config.get("F0_FACTOR"); + + DT = config.get("DT"); + TMAX = static_cast(config.get("TMAX")); + + PLOT_FREQUENCY = config.get("PLOT_FREQUENCY"); + PLOT_NX = static_cast(config.get("PLOT_NX")); + PLOT_DX = config.get("PLOT_DX"); + PLOT_DIR = config.get("PLOT_DIR"); +} + } // namespace Parameters -#endif +#endif // PARAMETERS_H diff --git a/parameters.lua b/parameters.lua new file mode 100644 index 0000000..9cd5e18 --- /dev/null +++ b/parameters.lua @@ -0,0 +1,86 @@ +local DIMENSION = 1 + +local X_DOMAIN_LEFT = 0.0 +local X_DOMAIN_RIGHT = 4 * math.pi + +local V_DOMAIN_LEFT = -10.0 +local V_DOMAIN_RIGHT = 10.0 + +local NV = 256 + +-- f0_TYPE: +-- 0 -> twos-stream +-- 1 -> landau-damping +-- 2 -> maxwellian +-- 3 -> bump-on-tail +local f0_TYPE = 1 + + +-- deal.ii options +local GLOBAL_REFINEMENT = 8 +local FE_DEGREE = 3 +local CONVERGENCE_ITERATIONS = 5000 +local CONVERGENCE_LIMIT = 1e-7 + + +-- Adaptive refinement options +local REFINE_FREQUENCY = 50 +local REFINEMENT_TOP_FRACTION = .8 +local REFINEMENT_BOTTOM_FRACTION = .1 + +local EPS = .01 +local WAVE_NR = .5 +local F0_FACTOR = 0.39894228040143267793994 + + +-- NUFI options +local DT = 1. / 10. +local TMAX = 100 + +-- Plotting options +local PLOT_FREQUENCY = 20 + +local LX = math.abs(X_DOMAIN_RIGHT - X_DOMAIN_LEFT) +local PLOT_NX = 512 + +parameters = { + DIMENSION = DIMENSION, + + X_DOMAIN_LEFT = X_DOMAIN_LEFT, + X_DOMAIN_RIGHT = X_DOMAIN_RIGHT, + LX = LX, + LX_INV = 1.0 / LX, + + V_DOMAIN_LEFT = V_DOMAIN_LEFT, + V_DOMAIN_RIGHT = V_DOMAIN_RIGHT, + + NV = NV, + DV = math.abs(V_DOMAIN_RIGHT - V_DOMAIN_LEFT) / NV, + + f0_TYPE = f0_TYPE, + + -- deal.ii options + GLOBAL_REFINEMENT = GLOBAL_REFINEMENT, + FE_DEGREE = FE_DEGREE, + CONVERGENCE_ITERATIONS = CONVERGENCE_ITERATIONS, + CONVERGENCE_LIMIT = CONVERGENCE_LIMIT, + + -- Adaptive refinement options + REFINE_FREQUENCY = REFINE_FREQUENCY, + REFINEMENT_TOP_FRACTION = REFINEMENT_TOP_FRACTION, + REFINEMENT_BOTTOM_FRACTION = REFINEMENT_BOTTOM_FRACTION, + + EPS = EPS, + WAVE_NR = WAVE_NR, + F0_FACTOR = F0_FACTOR, -- 1/sqrt(2pi) + + -- NUFI options + DT = DT, + TMAX = TMAX, + + -- Plotting options + PLOT_FREQUENCY = PLOT_FREQUENCY, + PLOT_NX = PLOT_NX, + PLOT_DX = LX / PLOT_NX, + PLOT_DIR = "results/", +} diff --git a/src/main.cc b/src/main.cc index 8b60171..20ce56f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -139,6 +140,9 @@ int main() { std::cout << "Threads: " << omp_get_max_threads() << "\n"; try { + std::cout << "Loading parameters from lua config" << "\n"; + Parameters::load_lua_config("parameters.lua"); + clear_results_directory("results"); run<1>(); // 1 = space_dim