Parameters loaded from lua file without rebuilding

This commit is contained in:
VCB Ferreira
2026-08-08 13:52:58 +02:00
parent 9d1a656673
commit 475560a3cc
5 changed files with 253 additions and 33 deletions
+5
View File
@@ -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)
+84
View File
@@ -0,0 +1,84 @@
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
#include <variant>
extern "C" {
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
}
using LuaValue = std::variant<int, float, bool, std::string>;
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 <typename T> 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<T>(key);
lua_pop(L, 1);
std::cout << "[LuaConfig] " << key << " = " << value << "\n";
return value;
}
private:
lua_State *L = nullptr;
template <typename T> T extract(const std::string &key) const {
if constexpr (std::is_same_v<T, int>) {
if (!lua_isinteger(L, -1) && !lua_isnumber(L, -1))
throw std::runtime_error("Key '" + key + "' is not a number");
return static_cast<int>(lua_tointeger(L, -1));
} else if constexpr (std::is_same_v<T, float>) {
if (!lua_isnumber(L, -1))
throw std::runtime_error("Key '" + key + "' is not a number");
return static_cast<float>(lua_tonumber(L, -1));
} else if constexpr (std::is_same_v<T, double>) {
if (!lua_isnumber(L, -1))
throw std::runtime_error("Key '" + key + "' is not a number");
return static_cast<double>(lua_tonumber(L, -1));
} else if constexpr (std::is_same_v<T, bool>) {
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<T, std::string>) {
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<T>()");
}
}
};
+74 -33
View File
@@ -1,60 +1,101 @@
#ifndef PARAMETERS_H
#define PARAMETERS_H
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <string>
#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<unsigned int>(config.get<int>("DIMENSION"));
X_DOMAIN_LEFT = config.get<double>("X_DOMAIN_LEFT");
X_DOMAIN_RIGHT = config.get<double>("X_DOMAIN_RIGHT");
LX = config.get<double>("LX");
LX_INV = config.get<double>("LX_INV");
V_DOMAIN_LEFT = config.get<double>("V_DOMAIN_LEFT");
V_DOMAIN_RIGHT = config.get<double>("V_DOMAIN_RIGHT");
NV = static_cast<unsigned int>(config.get<int>("NV"));
DV = config.get<double>("DV");
f0_TYPE = static_cast<size_t>(config.get<int>("f0_TYPE"));
GLOBAL_REFINEMENT =
static_cast<unsigned int>(config.get<int>("GLOBAL_REFINEMENT"));
FE_DEGREE = static_cast<unsigned int>(config.get<int>("FE_DEGREE"));
CONVERGENCE_ITERATIONS =
static_cast<unsigned int>(config.get<int>("CONVERGENCE_ITERATIONS"));
CONVERGENCE_LIMIT = config.get<double>("CONVERGENCE_LIMIT");
REFINE_FREQUENCY =
static_cast<unsigned int>(config.get<int>("REFINE_FREQUENCY"));
REFINEMENT_TOP_FRACTION = config.get<double>("REFINEMENT_TOP_FRACTION");
REFINEMENT_BOTTOM_FRACTION = config.get<double>("REFINEMENT_BOTTOM_FRACTION");
EPS = config.get<double>("EPS");
WAVE_NR = config.get<double>("WAVE_NR");
F0_FACTOR = config.get<double>("F0_FACTOR");
DT = config.get<double>("DT");
TMAX = static_cast<unsigned int>(config.get<int>("TMAX"));
PLOT_FREQUENCY = config.get<int>("PLOT_FREQUENCY");
PLOT_NX = static_cast<size_t>(config.get<int>("PLOT_NX"));
PLOT_DX = config.get<double>("PLOT_DX");
PLOT_DIR = config.get<std::string>("PLOT_DIR");
}
} // namespace Parameters
#endif
#endif // PARAMETERS_H
+86
View File
@@ -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/",
}
+4
View File
@@ -1,6 +1,7 @@
#include <filesystem>
#include <iostream>
#include <nufi/nufi_solver.h>
#include <nufi/parameters.h>
#include <nufi/poisson_problem.h>
#include <nufi/save_results.h>
#include <omp.h>
@@ -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