corrected saving and eval of fields over time

This commit is contained in:
Vasco C. B. Ferreira
2026-06-26 11:40:10 +02:00
parent 26d0637987
commit 006bd19fe2
10 changed files with 373 additions and 403 deletions
+94 -84
View File
@@ -9,52 +9,54 @@
using namespace dealii;
inline std::vector<int> Indices_of_points(const std::vector<double> &points, double x_min, double x_max, double dx, int grid_type=0)
{
// grid type:
// 0 => uniform
// 1 => non uniform (TODO)
if (dx <= 0.0) {
throw std::invalid_argument("dx must be positive");
}
if (x_max <= x_min) {
throw std::invalid_argument("x_max must be > x_min");
}
std::vector<int> indices;
indices.reserve(points.size());
switch (grid_type) {
case 0:
{
const double L = x_max - x_min;
const int N = std::floor(L/dx);
for (double x : points) //GPT loop, to check
{
x-= x_min;
x = x - L * std::floor(x/L);
int i = static_cast<int>(std::floor(x / dx));
// safety: handle rare edge case due to floating precision
if (i == N) i = 0;
indices.push_back(i);
}
}
case 1:
{
throw std::invalid_argument("Case for non uniform grid is not completed");
}
default:
throw std::invalid_argument("Invalid grid_type argument");
}
return indices;
}
// inline std::vector<int> Indices_of_points(const std::vector<double> &points,
// double x_min, double x_max, double dx, int grid_type=0)
// {
// // grid type:
// // 0 => uniform
// // 1 => non uniform (TODO)
//
// if (dx <= 0.0) {
// throw std::invalid_argument("dx must be positive");
// }
// if (x_max <= x_min) {
// throw std::invalid_argument("x_max must be > x_min");
// }
//
// std::vector<int> indices;
// indices.reserve(points.size());
//
// switch (grid_type) {
// case 0:
// {
// const double L = x_max - x_min;
// const int N = std::floor(L/dx);
//
//
// for (double x : points) //GPT loop, to check
// {
// x-= x_min;
// x = x - L * std::floor(x/L);
//
// int i = static_cast<int>(std::floor(x / dx));
//
// // safety: handle rare edge case due to floating precision
// if (i == N) i = 0;
//
// indices.push_back(i);
// }
// }
// case 1:
// {
// throw std::invalid_argument("Case for non uniform grid is not
// completed");
// }
// default:
// throw std::invalid_argument("Invalid grid_type argument");
//
// }
// return indices;
// }
inline double f0(const double x, const double v,
const double eps = Parameters::EPS,
@@ -81,11 +83,18 @@ inline double compute_rho(const double x,
return 1.0 - integral;
}
double eval(double x, const PoissonProblem<1> &poisson) noexcept {
return poisson.evaluate_potential(Point<1>(x));
inline double eval(double x, const PoissonProblem<1> &poisson,
const Vector<double> &solution) noexcept {
x -= Parameters::X_DOMAIN_LEFT;
x = x - Parameters::LX * std::floor(x * Parameters::LX_INV);
return eval_point<1>(poisson.get_mapping(), poisson.get_dof_handler(),
solution, Point<1>(x));
}
inline double integral_space_vector(const PoissonProblem<1> &poisson,
const Vector<double> &solution,
double dx = Parameters::PLOT_DX,
size_t Nx = Parameters::PLOT_NX) {
double integral = 0.0;
@@ -93,12 +102,13 @@ inline double integral_space_vector(const PoissonProblem<1> &poisson,
#pragma omp parallel for reduction(+ : integral)
for (size_t i = 0; i < Nx; ++i) {
double x = xmin + i * dx;
integral += eval(x, poisson);
integral += eval(x, poisson, solution);
}
return integral * dx;
};
inline double integral_space_vector_squared(const PoissonProblem<1> &poisson,
const Vector<double> &solution,
double dx = Parameters::PLOT_DX,
size_t Nx = Parameters::PLOT_NX) {
double integral = 0.0;
@@ -106,46 +116,46 @@ inline double integral_space_vector_squared(const PoissonProblem<1> &poisson,
#pragma omp parallel for reduction(+ : integral)
for (size_t i = 0; i < Nx; ++i) {
double x = xmin + i * dx;
double val = eval(x, poisson);
double val = eval(x, poisson, solution);
integral += val * val;
}
return integral * dx;
};
class Gradient {
public:
Gradient(double xmin, double xmax, unsigned int Nx)
: xmin_(xmin), xmax_(xmax), Nx_(Nx) {
if (xmax_ <= xmin_) {
throw std::invalid_argument("xmax must be greater than xmin");
}
}
std::vector<double> compute(const std::vector<double> &values) const {
size_t n = values.size();
if (n < 2) {
throw std::invalid_argument("Need at least 2 points");
}
std::vector<double> grad(n);
double dx = (xmax_ - xmin_) / (n - 1);
// periodic boundaries
grad[0] = -(values[1] - values[n - 1]) / (2.0 * dx);
grad[n - 1] = -(values[0] - values[n - 2]) / (2.0 * dx);
for (size_t i = 1; i < n - 1; ++i) {
grad[i] = -(values[i + 1] - values[i - 1]) / (2.0 * dx);
}
return grad;
}
private:
double xmin_;
double xmax_;
[[maybe_unused]] unsigned int Nx_;
};
// class Gradient {
// public:
// Gradient(double xmin, double xmax, unsigned int Nx)
// : xmin_(xmin), xmax_(xmax), Nx_(Nx) {
// if (xmax_ <= xmin_) {
// throw std::invalid_argument("xmax must be greater than xmin");
// }
// }
//
// std::vector<double> compute(const std::vector<double> &values) const {
// size_t n = values.size();
// if (n < 2) {
// throw std::invalid_argument("Need at least 2 points");
// }
//
// std::vector<double> grad(n);
//
// double dx = (xmax_ - xmin_) / (n - 1);
// // periodic boundaries
// grad[0] = -(values[1] - values[n - 1]) / (2.0 * dx);
// grad[n - 1] = -(values[0] - values[n - 2]) / (2.0 * dx);
//
// for (size_t i = 1; i < n - 1; ++i) {
// grad[i] = -(values[i + 1] - values[i - 1]) / (2.0 * dx);
// }
//
// return grad;
// }
//
// private:
// double xmin_;
// double xmax_;
// [[maybe_unused]] unsigned int Nx_;
// };
template <int dim>
class ChargeDensity : public Function<dim> // only uses f0
+31 -31
View File
@@ -2,32 +2,35 @@
#define NUFI_SOLVER_H
#include <boost/qvm/mat_access.hpp>
#include <vector>
#include <cmath>
#include <deal.II/base/point.h>
#include <deal.II/base/tensor.h>
#include <deal.II/numerics/fe_field_function.h>
#include <vector>
#include "nufi/fields.h" //dont remove
#include "nufi/parameters.h"
#include "nufi/poisson_problem.h"
#include "nufi/fields.h" //dont remove
using namespace dealii;
class NuFISolver
{
class NuFISolver {
public:
NuFISolver();
void run();
double eval_rho(unsigned int n, double x, const PoissonProblem<1> &poisson, unsigned int Nv = Parameters::NV) const;
double eval_ftilda(unsigned int n, double x, double u, const PoissonProblem<1> &poisson) const;
double eval_f(unsigned int n, double x, double u, const PoissonProblem<1> &poisson) const;
double eval_rho(unsigned int n, double x, const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history,
unsigned int Nv = Parameters::NV) const;
double eval_ftilda(unsigned int n, double x, double u,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history) const;
double eval_f(unsigned int n, double x, double u,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history) const;
private:
unsigned int Nt = std::floor(Parameters::TMAX/Parameters::DT);
unsigned int Nt = std::floor(Parameters::TMAX / Parameters::DT);
unsigned int Nx = Parameters::CALC_NX;
double Lx = Parameters::LX;
@@ -40,36 +43,33 @@ private:
unsigned int order;
PoissonProblem<1> poisson;
};
template<unsigned int dim>
class ChargeDensity_NuFI : public Function<dim>
{
public:
ChargeDensity_NuFI(const double *rho_values, unsigned int Nx)
template <unsigned int dim> class ChargeDensity_NuFI : public Function<dim> {
public:
ChargeDensity_NuFI(const double *rho_values, unsigned int Nx)
: Function<dim>(), rho(rho_values), Nx(Nx) {}
virtual double value(const Point<dim> &p,
[[maybe_unused]] const unsigned int component = 0) const override
{
const double x = p[0];
virtual double
value(const Point<dim> &p,
[[maybe_unused]] const unsigned int component = 0) const override {
const double x = p[0];
// Map x -> grid index
const double L = Parameters::LX;
const double dx = L / (Nx-1);
// Map x -> grid index
const double L = Parameters::LX;
const double dx = L / (Nx - 1);
int i = static_cast<int>(std::floor((x - Parameters::X_DOMAIN_LEFT) / dx));
int i = static_cast<int>(std::floor((x - Parameters::X_DOMAIN_LEFT) / dx));
// periodic wrap
i = (i % Nx + Nx) % Nx;
// periodic wrap
i = (i % Nx + Nx) % Nx;
return rho[i];
}
return rho[i];
}
private:
const double *rho;
const unsigned int Nx;
private:
const double *rho;
const unsigned int Nx;
};
#endif
+28 -29
View File
@@ -4,42 +4,41 @@
#include <cmath>
#include <cstdlib>
namespace Parameters
{
constexpr unsigned int DIMENSION = 1;
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;
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;
constexpr size_t CALC_NX = 256;
constexpr double CALC_DX = LX/CALC_NX;
constexpr size_t CALC_NX = 128;
constexpr double CALC_DX = LX / CALC_NX;
constexpr double V_DOMAIN_LEFT = -10.;
constexpr double V_DOMAIN_RIGHT = 10.;
constexpr double V_DOMAIN_LEFT = -10.;
constexpr double V_DOMAIN_RIGHT = 10.;
constexpr unsigned int NV = 256;
constexpr double DV = std::abs(V_DOMAIN_RIGHT - V_DOMAIN_LEFT)/NV;
constexpr unsigned int NV = 128;
constexpr double DV = std::abs(V_DOMAIN_RIGHT - V_DOMAIN_LEFT) / NV;
// 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;
// deal.ii options
constexpr unsigned int GLOBAL_REFINEMENT = 6;
constexpr unsigned int FE_DEGREE = 2;
constexpr unsigned int CONVERGENCE_ITERATIONS = 5000;
constexpr double CONVERGENCE_LIMIT = 1e-8;
constexpr double EPS = 0.01;
constexpr double WAVE_NR = 0.5;
constexpr double F0_FACTOR = 0.39894228040143267793994; // 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
constexpr double DT=1./4.;
constexpr unsigned int TMAX = 50;
// NUFI options
constexpr double DT = 1. / 10.;
constexpr unsigned int TMAX = 100;
//Plotting options
constexpr int PLOT_FREQUENCY = 20;
constexpr size_t PLOT_NX = 256;
constexpr double PLOT_DX = LX/PLOT_NX;
}
// Plotting options
constexpr int PLOT_FREQUENCY = 10;
constexpr size_t PLOT_NX = CALC_NX;
constexpr double PLOT_DX = LX / PLOT_NX;
} // namespace Parameters
#endif
+15 -27
View File
@@ -12,6 +12,7 @@
#include <deal.II/base/tensor.h>
#include <deal.II/base/utilities.h>
#include <deal.II/fe/mapping_q.h>
#include <deal.II/lac/affine_constraints.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/full_matrix.h>
@@ -61,17 +62,13 @@ public:
const Vector<double> &get_solution() const { return solution; }
const DoFHandler<dim> &get_dof_handler() const { return dof_handler; }
const MappingQ<dim> &get_mapping() const { return mapping; }
std::vector<double> sample_electric_field(double x_min, double x_max,
unsigned int Nx);
std::vector<double> sample_electric_potential(double x_min, double x_max,
unsigned int Nx);
double evaluate_potential(const Point<dim> &p) const
{
return fe_field_function->value(p);
}
private:
void create_mesh();
void setup_system();
@@ -94,7 +91,7 @@ private:
MappingQ<dim> mapping;
std::unique_ptr<Functions::FEFieldFunction<dim>> fe_field_function;
// std::unique_ptr<Functions::FEFieldFunction<dim>> fe_field_function;
};
// Utilities
@@ -177,8 +174,8 @@ PoissonProblem<dim>::sample_electric_potential(double x_min, double x_max,
// template <int dim> std::vector<double> eval_solution_on_points(
// const std::vector<Vector<double>> &solutions,
// const unsigned int n,
// const std::vector<Point<dim>> &points, // need to be in [x_min, x_max]. I think....
// const std::vector<unsigned int> &cell_indices,
// const std::vector<Point<dim>> &points, // need to be in [x_min,
// x_max]. I think.... const std::vector<unsigned int> &cell_indices,
// const DoFHandler<dim> &dof_handler,
// const MappingQ<dim> &mapping)
// {
@@ -219,7 +216,8 @@ PoissonProblem<dim>::sample_electric_potential(double x_min, double x_max,
// for (unsigned int id : point_ids)
// cell_points.push_back(points[id]);
//
// std::vector<types::global_dof_index> indices(dof_handler.get_fe().n_dofs_per_cell());
// std::vector<types::global_dof_index>
// indices(dof_handler.get_fe().n_dofs_per_cell());
// cell->get_dof_indices(indices);
//
// for (unsigned int i=0;i<indices.size();++i)
@@ -240,13 +238,8 @@ PoissonProblem<dim>::sample_electric_potential(double x_min, double x_max,
template <int dim>
double eval_point(const Mapping<dim> &mapping,
const DoFHandler<dim> &dof_handler,
const Vector<double> &solution,
const Point<dim> &point)
{
return VectorTools::point_value<dim>(mapping,
dof_handler,
solution,
point);
const Vector<double> &solution, const Point<dim> &point) {
return VectorTools::point_value<dim>(mapping, dof_handler, solution, point);
}
// dealii Poisson
@@ -288,7 +281,7 @@ template <int dim> void PoissonProblem<dim>::setup_system() {
gauge_dof = i;
break;
}
}
Assert(gauge_dof != numbers::invalid_dof_index,
ExcMessage("No unconstrained DoF found for gauge fixing."));
@@ -307,11 +300,9 @@ template <int dim> void PoissonProblem<dim>::setup_system() {
solution.reinit(dof_handler.n_dofs());
system_rhs.reinit(dof_handler.n_dofs());
fe_field_function =
std::make_unique<Functions::FEFieldFunction<dim>>(
dof_handler, solution, mapping);
}
// fe_field_function =
// std::make_unique<Functions::FEFieldFunction<dim>>(
// dof_handler, solution, mapping);
}
/* (Mine)
template <int dim>
@@ -393,6 +384,8 @@ void PoissonProblem<dim>::assemble_system()
// Paul's, mine's above
template <int dim> void PoissonProblem<dim>::assemble_system() {
Assert(system_matrix.m() == dof_handler.n_dofs(),
ExcMessage("Matrix not initialized correctly"));
system_matrix = 0;
system_rhs = 0;
@@ -446,11 +439,6 @@ template <int dim> void PoissonProblem<dim>::solve() {
// solver.solve(system_matrix, solution, system_rhs, preconditioner);
solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity());
constraints.distribute(solution);
fe_field_function =
std::make_unique<Functions::FEFieldFunction<dim>>(
dof_handler, solution, mapping);
}
template <int dim> void PoissonProblem<dim>::initialize() {
+16 -19
View File
@@ -1,29 +1,26 @@
#ifndef SAVE_RESULTS_H
#define SAVE_RESULTS_H
#include <string>
#include "nufi/nufi_solver.h"
#include "nufi/poisson_problem.h"
#include <deal.II/lac/vector.h>
#include <string>
void save_f(const NuFISolver &solver, unsigned int n,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history, unsigned int Nx_out,
unsigned int Nv_out, const std::string &filename);
void save_f( const NuFISolver &solver,
unsigned int n,
const PoissonProblem<1> &poisson,
unsigned int Nx_out,
unsigned int Nv_out,
const std::string &filename);
void save_rho(const NuFISolver &solver, unsigned int n,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history,
unsigned int Nx_out, const std::string &filename);
void save_rho(const NuFISolver &solver,
unsigned int n,
const PoissonProblem<1> &poisson,
unsigned int Nx_out,
const std::string &filename);
void save_Efield(unsigned int n, const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history,
unsigned int Nx_out, const std::string &filename);
void save_Efield(unsigned int n,
const PoissonProblem<1> &poisson,
unsigned int Nx_out,
const std::string &filename);
void save_space_vector(const std::vector<double> &vals,
const std::string &filename, size_t it);
void save_space_vector(const std::vector<double>& vals, const std::string& filename, size_t it);
#endif
#endif
+12 -20
View File
@@ -3,35 +3,27 @@
#include <chrono>
template <typename real>
class stopwatch
{
template <typename real> class stopwatch {
public:
void reset();
real elapsed();
void reset();
real elapsed();
private:
using clock = std::chrono::high_resolution_clock;
clock::time_point t0 { clock::now() };
using clock = std::chrono::high_resolution_clock;
clock::time_point t0{clock::now()};
};
template <typename real> inline
void stopwatch<real>::reset()
{
t0 = clock::now();
template <typename real> inline void stopwatch<real>::reset() {
t0 = clock::now();
}
template <typename real> inline
real stopwatch<real>::elapsed()
{
using seconds = std::chrono::duration<real,std::ratio<1,1>>;
template <typename real> inline real stopwatch<real>::elapsed() {
using seconds = std::chrono::duration<real, std::ratio<1, 1>>;
auto tnow = clock::now();
auto duration = std::chrono::duration_cast<seconds>( tnow - t0 );
auto tnow = clock::now();
auto duration = std::chrono::duration_cast<seconds>(tnow - t0);
return duration.count();
return duration.count();
}
#endif // STOPWATCH_H