mirror of
https://codeberg.org/vcbferreira/NuFI_deal.ii
synced 2026-08-12 22:43:17 +02:00
completed nufi, untested
This commit is contained in:
@@ -33,7 +33,6 @@ inline double compute_rho(const double x,
|
|||||||
const double v = Parameters::V_DOMAIN_LEFT + (i + 0.5) * dv;
|
const double v = Parameters::V_DOMAIN_LEFT + (i + 0.5) * dv;
|
||||||
integral += f0(x, v) * dv;
|
integral += f0(x, v) * dv;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1.0 - integral;
|
return 1.0 - integral;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "parameters.hpp"
|
|
||||||
#include "poisson_problem.hpp"
|
|
||||||
|
|
||||||
#include "nufi_solver.hpp"
|
#include "nufi_solver.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
|||||||
+91
-28
@@ -1,6 +1,13 @@
|
|||||||
|
/*
|
||||||
|
Todo:
|
||||||
|
- update Nx between timesteps to account for adaptivity changes because Vector rho needs to be resized
|
||||||
|
*/
|
||||||
|
|
||||||
#ifndef NUFI_SOLVER_HPP
|
#ifndef NUFI_SOLVER_HPP
|
||||||
#define NUFI_SOLVER_HPP
|
#define NUFI_SOLVER_HPP
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
#include <deal.II/base/point.h>
|
#include <deal.II/base/point.h>
|
||||||
#include <deal.II/base/tensor.h>
|
#include <deal.II/base/tensor.h>
|
||||||
#include <deal.II/numerics/fe_field_function.h>
|
#include <deal.II/numerics/fe_field_function.h>
|
||||||
@@ -20,30 +27,28 @@ public:
|
|||||||
NuFISolver();
|
NuFISolver();
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
double eval_rho(size_t n, double x, double u);
|
double eval_rho(unsigned int n, double x, unsigned int Nv = Parameters::NV);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void compute_density();
|
double eval_ftilda(unsigned int n, double x, double u);
|
||||||
void solve_poisson();
|
void solve_poisson(unsigned int n);
|
||||||
void update_distribution();
|
|
||||||
|
|
||||||
double evaluate_E(double x);
|
double evaluate_E(double x);
|
||||||
|
|
||||||
PoissonProblem<1> poisson;
|
|
||||||
|
|
||||||
std::vector<double> coeffs;
|
|
||||||
std::vector<double> rho;
|
std::vector<double> rho;
|
||||||
|
|
||||||
unsigned int Nt;
|
unsigned int Nt = std::floor(Parameters::TMAX/Parameters::DT);
|
||||||
unsigned int Nx;
|
unsigned int Nx;
|
||||||
|
|
||||||
double Lx = Parameters::LX;
|
double Lx = Parameters::LX;
|
||||||
|
|
||||||
unsigned int order;
|
unsigned int order;
|
||||||
|
|
||||||
double dt;
|
double dt = Parameters::DT;
|
||||||
size_t stride_t;
|
|
||||||
|
PoissonProblem<1> poisson;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline double NuFISolver::evaluate_E(double x)
|
inline double NuFISolver::evaluate_E(double x)
|
||||||
@@ -75,12 +80,14 @@ inline double NuFISolver::evaluate_E(double x)
|
|||||||
return E_val;
|
return E_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline double NuFISolver::eval_rho(size_t n,
|
inline double NuFISolver::eval_ftilda(unsigned int n,
|
||||||
double x,
|
double x,
|
||||||
double u)
|
double u)
|
||||||
{
|
{
|
||||||
|
double Lu = std::abs(Parameters::V_DOMAIN_LEFT - Parameters::V_DOMAIN_RIGHT);
|
||||||
|
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return f0(x,u);
|
return f0(x, u);
|
||||||
|
|
||||||
double Ex;
|
double Ex;
|
||||||
|
|
||||||
@@ -101,33 +108,89 @@ inline double NuFISolver::eval_rho(size_t n,
|
|||||||
u += 0.5*dt*Ex; // is this line useless ?
|
u += 0.5*dt*Ex; // is this line useless ?
|
||||||
|
|
||||||
double x_periodic = x - Lx * std::floor(x / Lx);
|
double x_periodic = x - Lx * std::floor(x / Lx);
|
||||||
|
double u_periodic = u - Lu * std::floor(u / Lu);
|
||||||
|
|
||||||
return compute_rho(x_periodic); // compute_rho (from fields.hpp) uses f0
|
return f0(x_periodic, u_periodic);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline double NuFISolver::eval_rho(const unsigned int n,
|
||||||
|
const double x,
|
||||||
|
const unsigned int Nv)
|
||||||
|
{
|
||||||
|
const double dv = (Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
|
||||||
|
|
||||||
|
double integral = 0.0;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1.0 - integral;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChargeDensity_NuFI : public Function<1>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ChargeDensity_NuFI(NuFISolver &solver, size_t n)
|
||||||
|
: solver(solver), n(n) {}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
NuFISolver &solver;
|
||||||
|
size_t n;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
inline void NuFISolver::run()
|
||||||
{
|
{
|
||||||
rho.resize(Nx, 0.0); // initialize density array
|
std::cout << "Starting NuFI solver\n";
|
||||||
|
|
||||||
// Main time-stepping loop
|
for (unsigned int n = 0; n < Nt; ++n)
|
||||||
for (size_t n = 0; n < Nt; ++n)
|
|
||||||
{
|
{
|
||||||
|
std::cout << "Timestep " << n << " / " << Nt << std::endl;
|
||||||
|
|
||||||
// Solve this logic! To use on deal.ii
|
|
||||||
|
|
||||||
// 1. Compute charge density rho from current distribution
|
double dx = Lx / Nx;
|
||||||
compute_density();
|
|
||||||
|
|
||||||
// 2. Solve Poisson's equation to update electric field
|
for (unsigned int i = 0; i < Nx; ++i)
|
||||||
solve_poisson();
|
{
|
||||||
|
double x = (i + 0.5) * dx;
|
||||||
// 3. Update distribution function along characteristics
|
rho[i] = eval_rho(n, x);
|
||||||
update_distribution();
|
|
||||||
|
|
||||||
// Optional: compute ftilda at current step if needed
|
|
||||||
// for demonstration: evaluate ftilda at midpoint x, u = 0
|
|
||||||
// double ft = eval_ftilda(n, 0.5 * poisson.get_Lx(), 0.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
solve_poisson(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "NuFI simulation finished.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline NuFISolver::NuFISolver()
|
||||||
|
: order(Parameters::FE_DEGREE),
|
||||||
|
poisson(order, Parameters::NV)
|
||||||
|
{
|
||||||
|
poisson.initialize();
|
||||||
|
|
||||||
|
Nx = poisson.get_dof_handler().n_dofs();
|
||||||
|
rho.resize(Nx, 0.0);
|
||||||
|
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ namespace Parameters
|
|||||||
|
|
||||||
constexpr double EPS = 0.01;
|
constexpr double EPS = 0.01;
|
||||||
constexpr double WAVE_NR = 0.5;
|
constexpr double WAVE_NR = 0.5;
|
||||||
|
|
||||||
|
constexpr double DT=0.05;
|
||||||
|
constexpr unsigned int TMAX = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+35
-2
@@ -1,6 +1,7 @@
|
|||||||
#ifndef POISSON_PROBLEM_HPP
|
#ifndef POISSON_PROBLEM_HPP
|
||||||
#define POISSON_PROBLEM_HPP
|
#define POISSON_PROBLEM_HPP
|
||||||
|
|
||||||
|
#include <deal.II/base/function.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
|
||||||
#include <deal.II/base/quadrature_lib.h>
|
#include <deal.II/base/quadrature_lib.h>
|
||||||
@@ -43,9 +44,13 @@ class PoissonProblem
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PoissonProblem(unsigned int degree, unsigned int Nv);
|
PoissonProblem(unsigned int degree, unsigned int Nv);
|
||||||
|
|
||||||
|
void initialize();
|
||||||
|
void solve_step();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
void set_Nv(unsigned int new_Nv);
|
void set_Nv(unsigned int new_Nv);
|
||||||
|
void set_rhs_function(const Function<dim> &rhs);
|
||||||
|
|
||||||
const Vector<double> &get_solution() const { return solution; }
|
const Vector<double> &get_solution() const { return solution; }
|
||||||
const DoFHandler<dim> &get_dof_handler() const { return dof_handler; }
|
const DoFHandler<dim> &get_dof_handler() const { return dof_handler; }
|
||||||
@@ -69,6 +74,8 @@ private:
|
|||||||
Vector<double> solution; // phi
|
Vector<double> solution; // phi
|
||||||
Vector<double> system_rhs;
|
Vector<double> system_rhs;
|
||||||
|
|
||||||
|
const Function<dim> *rhs_function;
|
||||||
|
|
||||||
MappingQ<dim> mapping;
|
MappingQ<dim> mapping;
|
||||||
|
|
||||||
unsigned int Nv;
|
unsigned int Nv;
|
||||||
@@ -81,6 +88,12 @@ void PoissonProblem<dim>::set_Nv(unsigned int new_Nv)
|
|||||||
Nv = new_Nv;
|
Nv = new_Nv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <int dim>
|
||||||
|
void PoissonProblem<dim>::set_rhs_function(const Function<dim> &rhs)
|
||||||
|
{
|
||||||
|
rhs_function = &rhs;
|
||||||
|
}
|
||||||
|
|
||||||
template <int dim>
|
template <int dim>
|
||||||
PoissonProblem<dim>::PoissonProblem(unsigned int degree, unsigned int Nv)
|
PoissonProblem<dim>::PoissonProblem(unsigned int degree, unsigned int Nv)
|
||||||
: fe(degree)
|
: fe(degree)
|
||||||
@@ -185,7 +198,7 @@ void PoissonProblem<dim>::assemble_system()
|
|||||||
Vector<double> cell_rhs(dofs_per_cell);
|
Vector<double> cell_rhs(dofs_per_cell);
|
||||||
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
|
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
|
||||||
|
|
||||||
ChargeDensity<dim> rhs_function(Parameters::EPS, Parameters::WAVE_NR, Nv);
|
Assert(rhs_function != nullptr, ExcMessage("RHS function not set"));
|
||||||
|
|
||||||
for (const auto &cell : dof_handler.active_cell_iterators())
|
for (const auto &cell : dof_handler.active_cell_iterators())
|
||||||
{
|
{
|
||||||
@@ -195,7 +208,7 @@ void PoissonProblem<dim>::assemble_system()
|
|||||||
|
|
||||||
for (unsigned int q = 0; q < n_q_points; ++q)
|
for (unsigned int q = 0; q < n_q_points; ++q)
|
||||||
{
|
{
|
||||||
const double rho = rhs_function.value(fe_values.quadrature_point(q));
|
const double rho = rhs_function->value(fe_values.quadrature_point(q));
|
||||||
|
|
||||||
for (unsigned int i = 0; i < dofs_per_cell; ++i)
|
for (unsigned int i = 0; i < dofs_per_cell; ++i)
|
||||||
{
|
{
|
||||||
@@ -286,7 +299,27 @@ void PoissonProblem<dim>::output_results() const
|
|||||||
data_out_E.write_vtk(out2);
|
data_out_E.write_vtk(out2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <int dim>
|
||||||
|
void PoissonProblem<dim>::initialize()
|
||||||
|
{
|
||||||
|
set_Nv(Parameters::NV);
|
||||||
|
|
||||||
|
create_mesh(); // build grid
|
||||||
|
setup_system(); // distribute DoFs and matrices
|
||||||
|
}
|
||||||
|
|
||||||
|
template <int dim>
|
||||||
|
void PoissonProblem<dim>::solve_step()
|
||||||
|
{
|
||||||
|
system_matrix = 0;
|
||||||
|
system_rhs = 0;
|
||||||
|
|
||||||
|
assemble_system();
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// NuFI doesnt use this, kept only for testing.
|
||||||
template <int dim>
|
template <int dim>
|
||||||
void PoissonProblem<dim>::run()
|
void PoissonProblem<dim>::run()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user