nufi online, todo: plotting

This commit is contained in:
Vasco C. B. Ferreira
2026-03-10 14:58:07 +01:00
parent bf7ad1e595
commit 080d10851a
6 changed files with 274 additions and 100 deletions
+65 -81
View File
@@ -1,8 +1,3 @@
/*
Todo:
- update Nx between timesteps to account for adaptivity changes because Vector rho needs to be resized
*/
#ifndef NUFI_SOLVER_HPP
#define NUFI_SOLVER_HPP
@@ -12,12 +7,14 @@ Todo:
#include <deal.II/base/tensor.h>
#include <deal.II/numerics/fe_field_function.h>
#include <iostream>
#include <vector>
#include <cstddef>
#include "parameters.hpp"
#include "poisson_problem.hpp"
#include "fields.hpp" // holds f0(x,v), and compute_rho(x)
#include "spline_field.hpp"
using namespace dealii;
@@ -27,19 +24,16 @@ public:
NuFISolver();
void run();
double eval_rho(unsigned int n, double x, unsigned int Nv = Parameters::NV);
double eval_rho(unsigned int n, double x, const UniformSpline1D<double,4>& E_spline, unsigned int Nv = Parameters::NV);
private:
double eval_ftilda(unsigned int n, double x, double u);
void solve_poisson(unsigned int n);
double eval_ftilda(unsigned int n, double x, double u, const UniformSpline1D<double, 4>& E_spline);
double evaluate_E(double x);
std::vector<double> rho;
unsigned int Nt = std::floor(Parameters::TMAX/Parameters::DT);
unsigned int Nx;
unsigned int Nx = Parameters::SPLINE_NX;
double Lx = Parameters::LX;
@@ -51,63 +45,29 @@ private:
};
inline double NuFISolver::evaluate_E(double x)
{
// Wrap x into the periodic domain
double x_periodic = x - Lx * std::floor(x / Lx);
Point<1> p(x_periodic);
Functions::FEFieldFunction<1> E_field(
poisson.get_dof_handler(),
poisson.get_solution()
);
double E_val = 0.0;
try
{
// Evaluate the electric field at point p
// If your solution represents phi, take negative gradient
Tensor<1,1> grad = E_field.gradient(p);
E_val = -grad[0]; // -∂φ/∂x
}
catch (const VectorTools::ExcPointNotAvailableHere &)
{
// This happens if p lies in an artificial cell in parallel
AssertThrow(false, ExcMessage("Point not available on this process."));
}
return E_val;
}
inline double NuFISolver::eval_ftilda(unsigned int n,
double x,
double u)
double u,
const UniformSpline1D<double, 4>& E_spline)
{
double Lu = std::abs(Parameters::V_DOMAIN_LEFT - Parameters::V_DOMAIN_RIGHT);
if (n == 0)
return f0(x, u);
double Ex;
// Initial half-step.
Ex = evaluate_E(x);
u += 0.5*dt*Ex;
u += 0.5*dt*E_spline.eval(x);
while ( --n )
{
x -= dt*u;
Ex = evaluate_E(x);
u += dt*Ex;
u += dt*E_spline.eval(x);
}
// Final half-step.
x -= dt*u;
Ex = evaluate_E(x);
u += 0.5*dt*Ex; // is this line useless ?
u += 0.5*dt*E_spline.eval(x);
double x_periodic = x - Lx * std::floor(x / Lx);
double u_periodic = u - Lu * std::floor(u / Lu);
@@ -116,6 +76,7 @@ inline double NuFISolver::eval_ftilda(unsigned int n,
inline double NuFISolver::eval_rho(const unsigned int n,
const double x,
const UniformSpline1D<double, 4>& E_spline,
const unsigned int Nv)
{
const double dv = (Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
@@ -124,62 +85,85 @@ inline double NuFISolver::eval_rho(const unsigned int n,
for (unsigned int i = 0; i < Nv; ++i)
{
const double v = Parameters::V_DOMAIN_LEFT + (i + 0.5) * dv;
integral += eval_ftilda(n, x, v) * dv;
AssertThrow(std::isfinite(E_spline.eval(x)), ExcMessage("NaN detected in E_spline.eval(x) inside NuFISolver::eval_rho integral loop"));
integral += eval_ftilda(n, x, v, E_spline) * dv;
}
return 1.0 - integral;
}
class ChargeDensity_NuFI : public Function<1>
{
public:
ChargeDensity_NuFI(NuFISolver &solver, size_t n)
: solver(solver), n(n) {}
ChargeDensity_NuFI(NuFISolver &solver, size_t n, const UniformSpline1D<double,4> &E_spline)
: solver(solver), n(n), E_spline(E_spline) {}
virtual double value(const Point<1> &p,
[[maybe_unused]] const unsigned int component = 0) const override
{
double x = p[0];
return solver.eval_rho(n, x);
// u not used anymore
return solver.eval_rho(n, x, E_spline);
}
private:
NuFISolver &solver;
size_t n;
const UniformSpline1D<double, 4> &E_spline;
};
inline void NuFISolver::solve_poisson(unsigned int n)
{
ChargeDensity_NuFI rho_function(*this, n);
poisson.set_rhs_function(rho_function);
poisson.solve_step();
}
inline void NuFISolver::run()
{
std::cout << "Starting NuFI solver\n";
std::cout << "Start of NuFISolver::run()\n";
// init E_spline
for (unsigned int n = 0; n < Nt; ++n)
unsigned int Nx = Parameters::SPLINE_NX;
// Nx grid points
double dx = Lx / (Nx-1);
std::vector<double> E_grid(Nx);
//set initial E points
for (unsigned int i=0; i<Nx; ++i)
{
std::cout << "Timestep " << n << " / " << Nt << std::endl;
[[maybe_unused]] double x = Parameters::X_DOMAIN_LEFT + i*dx;
E_grid[i] = 1;
}
UniformSpline1D<double,4> E_spline(E_grid, Parameters::X_DOMAIN_LEFT, Parameters::X_DOMAIN_RIGHT);
for (unsigned int it = 0; it < Nt; ++it)
{
std::cout << "Timestep " << it << " / " << Nt << std::endl;
// Step 1: Evaluate rho^n(x) using current E_spline
std::cout << "Start of eval_rho loop\n";
std::vector<double> rho(Nx);
for (unsigned int i = 0; i < (Nx); ++i)
{
double x = (i + 0.5) * dx;
rho[i] = eval_rho(it, x, E_spline, Parameters::NV);
}
std::cout << "End of eval_rho loop\n";
ChargeDensity_NuFI rho_function(*this, it, E_spline);
poisson.set_rhs_function(rho_function);
for (unsigned int i=0; i< rho.size(); ++i) // check for bad rho[i]
{
AssertThrow(std::isfinite(rho[i]), ExcMessage("NaN detected in rho"));
}
poisson.solve_step();
double dx = Lx / Nx;
std::cout << "Start of eval_rho step with Nx = "<< Nx<< "\n";
for (unsigned int i = 0; i < Nx; ++i)
{
double x = (i + 0.5) * dx;
rho[i] = eval_rho(n, x);
}
std::cout << "End of eval_rho step\n";
solve_poisson(n);
E_grid = poisson.sample_electric_field(poisson, Nx, 0.0, Lx);
// Step 4: Build spline for E^{n+1} (used in next time step)
E_spline = UniformSpline1D<double, 4>(E_grid, 0.0, Lx);
}
std::cout << "NuFI simulation finished.\n";
@@ -187,7 +171,7 @@ inline void NuFISolver::run()
inline NuFISolver::NuFISolver()
: order(Parameters::FE_DEGREE),
poisson(order, Parameters::NV)
poisson(order)
{
std::cout << "Initializing Poisson\n";
poisson.initialize();