Merge branch 'feature/read_options_from_lua_table' into feature/rho_E_buffered_diagnostics

This commit is contained in:
VCB Ferreira
2026-08-08 14:09:24 +02:00
6 changed files with 257 additions and 55 deletions
+5
View File
@@ -21,6 +21,9 @@ deal_ii_initialize_cached_variables()
find_package(OpenMP REQUIRED) find_package(OpenMP REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LUA REQUIRED lua5.4)
# ------------------------- # -------------------------
add_library(nufi_lib add_library(nufi_lib
@@ -31,12 +34,14 @@ add_library(nufi_lib
target_include_directories(nufi_lib PUBLIC target_include_directories(nufi_lib PUBLIC
${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}
${LUA_INCLUDE_DIRS}
) )
deal_ii_setup_target(nufi_lib) deal_ii_setup_target(nufi_lib)
target_link_libraries(nufi_lib target_link_libraries(nufi_lib
OpenMP::OpenMP_CXX OpenMP::OpenMP_CXX
${LUA_LIBRARIES}
) )
target_compile_options(nufi_lib PRIVATE -O3 -g -fno-omit-frame-pointer) 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 #ifndef PARAMETERS_H
#define PARAMETERS_H #define PARAMETERS_H
#include <cmath>
#include <cstddef> #include <cstddef>
#include <cstdlib>
#include <string> #include <string>
#include "lua_config.h"
namespace Parameters { namespace Parameters {
constexpr unsigned int DIMENSION = 1;
constexpr double X_DOMAIN_LEFT = 0.0; inline unsigned int DIMENSION;
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;
constexpr double V_DOMAIN_LEFT = -10.; inline double X_DOMAIN_LEFT;
constexpr double V_DOMAIN_RIGHT = 10.; inline double X_DOMAIN_RIGHT;
inline double LX;
inline double LX_INV;
constexpr unsigned int NV = 128; inline double V_DOMAIN_LEFT;
constexpr double DV = std::abs(V_DOMAIN_RIGHT - V_DOMAIN_LEFT) / NV; inline double V_DOMAIN_RIGHT;
inline unsigned int NV;
inline double DV;
// f0_TYPE: // f0_TYPE:
// 0 -> twos-stream // 0 -> twos-stream
// 1 -> landau-damping // 1 -> landau-damping
// 2 -> maxwellian // 2 -> maxwellian
// 3 -> bump-on-tail // 3 -> bump-on-tail
constexpr size_t f0_TYPE = 1; inline size_t f0_TYPE;
// deal.ii options // deal.ii options
constexpr unsigned int GLOBAL_REFINEMENT = 8; inline unsigned int GLOBAL_REFINEMENT;
constexpr unsigned int FE_DEGREE = 3; inline unsigned int FE_DEGREE;
constexpr unsigned int CONVERGENCE_ITERATIONS = 5000; inline unsigned int CONVERGENCE_ITERATIONS;
constexpr double CONVERGENCE_LIMIT = 1e-8; inline double CONVERGENCE_LIMIT;
// Adaptive refinement options // Adaptive refinement options
constexpr unsigned int REFINE_FREQUENCY = 30; inline unsigned int REFINE_FREQUENCY;
constexpr double REFINEMENT_TOP_FRACTION = 0.8; inline double REFINEMENT_TOP_FRACTION;
constexpr double REFINEMENT_BOTTOM_FRACTION = 0.1; inline double REFINEMENT_BOTTOM_FRACTION;
// Gauge options inline double EPS;
constexpr double GAUGE_DOMAIN_LEFT = 3.2; inline double WAVE_NR;
constexpr double GAUGE_DOMAIN_RIGHT = 3.8; inline double F0_FACTOR; // 1/sqrt(2pi)
constexpr double EPS = 0.01;
constexpr double WAVE_NR = 0.5;
constexpr double F0_FACTOR = 0.39894228040143267793994; // 1/sqrt(2pi)
// NUFI options // NUFI options
constexpr double DT = 1. / 10.; inline double DT;
constexpr unsigned int TMAX = 100; inline unsigned int TMAX;
// Plotting options // Plotting options
constexpr int PLOT_FREQUENCY = 10; inline int PLOT_FREQUENCY;
constexpr size_t PLOT_NX = 512; inline size_t PLOT_NX;
constexpr double PLOT_DX = LX / PLOT_NX; inline double PLOT_DX;
const std::string PLOT_DIR = "results/"; 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 } // namespace Parameters
#endif #endif // PARAMETERS_H
+4 -22
View File
@@ -458,29 +458,11 @@ template <int dim> void PoissonProblem<dim>::solve(size_t it) {
system_rhs.l2_norm()); system_rhs.l2_norm());
SolverCG<Vector<double>> solver(solver_control); SolverCG<Vector<double>> solver(solver_control);
solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity()); PreconditionJacobi<SparseMatrix<double>> preconditioner;
constraints.distribute(solution); preconditioner.initialize(system_matrix);
// std::ofstream out("results/phi_after_solve_" + std::to_string(it) + solver.solve(system_matrix, solution, system_rhs, preconditioner);
// ".dat"); std::vector<std::pair<double, double>> data; constraints.distribute(solution);
//
// const auto support =
// DoFTools::map_dofs_to_support_points(mapping, dof_handler);
//
// for (const auto &[dof, p] : support) {
// data.emplace_back(p[0], solution[dof]);
// }
//
// std::sort(data.begin(), data.end());
//
// for (const auto &[x, value] : data) {
// out << x << " " << value << "\n";
// }
//
// std::vector<double> E_x =
// sample_electric_field(Parameters::X_DOMAIN_LEFT,
// Parameters::X_DOMAIN_RIGHT, Parameters::PLOT_NX);
// save_space_vector(E_x, "E_x_after_solve", it);
} }
template <int dim> void PoissonProblem<dim>::initialize() { template <int dim> void PoissonProblem<dim>::initialize() {
+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 <filesystem>
#include <iostream> #include <iostream>
#include <nufi/nufi_solver.h> #include <nufi/nufi_solver.h>
#include <nufi/parameters.h>
#include <nufi/poisson_problem.h> #include <nufi/poisson_problem.h>
#include <nufi/save_results.h> #include <nufi/save_results.h>
#include <omp.h> #include <omp.h>
@@ -131,6 +132,9 @@ int main() {
std::cout << "Threads: " << omp_get_max_threads() << "\n"; std::cout << "Threads: " << omp_get_max_threads() << "\n";
try { try {
std::cout << "Loading parameters from lua config" << "\n";
Parameters::load_lua_config("parameters.lua");
clear_results_directory("results"); clear_results_directory("results");
run<1>(); // 1 = space_dim run<1>(); // 1 = space_dim