mirror of
https://codeberg.org/vcbferreira/NuFI_deal.ii
synced 2026-08-12 22:43:17 +02:00
Merge branch 'feature/read_options_from_lua_table' into feature/rho_E_buffered_diagnostics
This commit is contained in:
@@ -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
@@ -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 = 128;
|
||||
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 = 5000;
|
||||
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 = 30;
|
||||
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
|
||||
|
||||
+4
-22
@@ -458,29 +458,11 @@ template <int dim> void PoissonProblem<dim>::solve(size_t it) {
|
||||
system_rhs.l2_norm());
|
||||
SolverCG<Vector<double>> solver(solver_control);
|
||||
|
||||
solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity());
|
||||
constraints.distribute(solution);
|
||||
PreconditionJacobi<SparseMatrix<double>> preconditioner;
|
||||
preconditioner.initialize(system_matrix);
|
||||
|
||||
// std::ofstream out("results/phi_after_solve_" + std::to_string(it) +
|
||||
// ".dat"); std::vector<std::pair<double, double>> data;
|
||||
//
|
||||
// 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);
|
||||
solver.solve(system_matrix, solution, system_rhs, preconditioner);
|
||||
constraints.distribute(solution);
|
||||
}
|
||||
|
||||
template <int dim> void PoissonProblem<dim>::initialize() {
|
||||
|
||||
Reference in New Issue
Block a user