mirror of
https://codeberg.org/vcbferreira/NuFI_deal.ii
synced 2026-08-12 22:43:17 +02:00
reorganized project for cleaner filesystem
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
#include <nufi/nufi_solver.h>
|
||||
|
||||
void clear_results_directory(const std::string &dir)
|
||||
{
|
||||
if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir))
|
||||
return;
|
||||
|
||||
for (const auto &entry : std::filesystem::directory_iterator(dir))
|
||||
{
|
||||
if (std::filesystem::is_regular_file(entry))
|
||||
{
|
||||
std::filesystem::remove(entry.path());
|
||||
std::cout << "Deleted: " << entry.path() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
clear_results_directory("results");
|
||||
|
||||
NuFISolver solver;
|
||||
solver.run();
|
||||
}
|
||||
catch (const std::exception &exc)
|
||||
{
|
||||
std::cerr << "\nException:\n" << exc.what() << "\n";
|
||||
return 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::cerr << "\nUnknown exception!\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
#include "nufi/nufi_solver.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <deal.II/base/point.h>
|
||||
#include <deal.II/base/tensor.h>
|
||||
#include <deal.II/numerics/fe_field_function.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <cstddef>
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/save_results.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include "nufi/fields.h"
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
double NuFISolver::eval_ftilda(unsigned int n,
|
||||
double x,
|
||||
double u,
|
||||
const double *E_coeffs) const
|
||||
{
|
||||
if ( n == 0 ) return f0(x,u);
|
||||
|
||||
const size_t stride_x = 1;
|
||||
const size_t stride_t = stride_x*(Parameters::SPLINE_NX + Parameters::SPLINE_ORDER - 1);
|
||||
|
||||
double Ex;
|
||||
const double *c;
|
||||
|
||||
// We omit the initial half-step.
|
||||
|
||||
while ( --n )
|
||||
{
|
||||
x = x - Parameters::DT *u;
|
||||
c = E_coeffs + n*stride_t;
|
||||
Ex = -eval<1>(x, c);
|
||||
u = u + Parameters::DT *Ex;
|
||||
}
|
||||
|
||||
// The final half-step.
|
||||
x -= Parameters::DT*u;
|
||||
c = E_coeffs + n*stride_t;
|
||||
Ex = -eval<1>(x, c);
|
||||
u += 0.5*Parameters::DT*Ex;
|
||||
|
||||
return f0(x,u);
|
||||
}
|
||||
|
||||
double NuFISolver::eval_rho(const unsigned int n,
|
||||
const double x,
|
||||
const double *E_coeffs,
|
||||
const unsigned int Nv) const
|
||||
{
|
||||
const double dv = (Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
|
||||
const double v_min = Parameters::V_DOMAIN_LEFT;
|
||||
|
||||
double integral = 0.0;
|
||||
for (unsigned int i = 0; i < Nv; ++i)
|
||||
integral += eval_ftilda(n, x, v_min + i * dv, E_coeffs) * dv;
|
||||
|
||||
return 1.0 - integral;
|
||||
}
|
||||
|
||||
void NuFISolver::run()
|
||||
{
|
||||
std::cout << "Building E_sline\n\n";
|
||||
|
||||
using std::abs;
|
||||
using std::max;
|
||||
|
||||
const size_t stride_t = Nx + order - 1;
|
||||
|
||||
std::unique_ptr<double[]> coeffs { new double[ Nt*stride_t ] {} };
|
||||
std::unique_ptr<double,decltype(std::free)*> rho { reinterpret_cast<double*>(std::aligned_alloc(64,sizeof(double)*Nx)), std::free };
|
||||
|
||||
if ( rho == nullptr ) throw std::bad_alloc {};
|
||||
|
||||
for (unsigned int it = 0; it < Nt; ++it)
|
||||
{
|
||||
std::cout << "Timestep " << it << " / " << Nt << std::endl << std::endl;
|
||||
|
||||
// compute rho
|
||||
|
||||
double dx = Parameters::SPLINE_DX;
|
||||
double x = Parameters::X_DOMAIN_LEFT;
|
||||
|
||||
for(size_t i = 0; i<Nx; i++, x+=dx)
|
||||
{
|
||||
double ith_rho = eval_rho(it, x, coeffs.get(), Parameters::NV);
|
||||
AssertThrow(std::isfinite(ith_rho), ExcMessage("NaN detected in rho"));
|
||||
rho.get()[i] = ith_rho;
|
||||
}
|
||||
|
||||
// bool rho_written = false;
|
||||
//
|
||||
// if (!rho_written)
|
||||
// {
|
||||
// std::ofstream rho_file("rho_initial.dat");
|
||||
//
|
||||
// if (!rho_file)
|
||||
// throw std::runtime_error("Could not open rho_initial.dat");
|
||||
//
|
||||
// rho_file.precision(16);
|
||||
// rho_file << std::scientific;
|
||||
//
|
||||
// for (size_t i = 0; i < Nx; ++i)
|
||||
// rho_file << rho.get()[i] << "\n";
|
||||
//
|
||||
// rho_written = true;
|
||||
// }
|
||||
|
||||
poisson.set_rhs_function(std::make_unique<ChargeDensity_NuFI<1>>(rho.get(), Parameters::SPLINE_NX));
|
||||
poisson.solve_step();
|
||||
|
||||
if (it % Parameters::PLOT_FREQUENCY == 0)
|
||||
{
|
||||
std::cout << "Saving results... \n\n";
|
||||
save_ftilda(*this, it, coeffs.get(), 128, 128, "results/ftilda_" + std::to_string(it) + ".dat");
|
||||
save_rho(*this, it, coeffs.get(), 128, "results/rho_" + std::to_string(it) + ".dat");
|
||||
save_Efield(it, coeffs.get(), 128, "results/field_" + std::to_string(it) + ".dat");
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "NuFI simulation finished.\n";
|
||||
}
|
||||
|
||||
NuFISolver::NuFISolver()
|
||||
: order(Parameters::FE_DEGREE),
|
||||
poisson(order)
|
||||
{
|
||||
std::cout << "Initializing dealii Poisson Solver\n";
|
||||
poisson.initialize();
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#include "nufi/save_results.h"
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include "nufi/nufi_solver.h"
|
||||
|
||||
|
||||
void save_ftilda( const NuFISolver &solver,
|
||||
unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
unsigned int Nv_out,
|
||||
const std::string &filename)
|
||||
{
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
double xmax = Parameters::X_DOMAIN_RIGHT;
|
||||
|
||||
double vmin = Parameters::V_DOMAIN_LEFT;
|
||||
double vmax = Parameters::V_DOMAIN_RIGHT;
|
||||
|
||||
double dx = (xmax - xmin) / Nx_out;
|
||||
double dv = (vmax - vmin) / Nv_out;
|
||||
|
||||
file << Nx_out << " " << Nv_out << "\n";
|
||||
file << xmin << " " << xmax << "\n";
|
||||
file << vmin << " " << vmax << "\n";
|
||||
|
||||
for (unsigned int i = 0; i < Nx_out; ++i)
|
||||
{
|
||||
double x = xmin + (i + 0.5)*dx;
|
||||
|
||||
for (unsigned int j = 0; j < Nv_out; ++j)
|
||||
{
|
||||
double v = vmin + (j + 0.5)*dv;
|
||||
|
||||
double val = solver.eval_ftilda(n, x, v, E_coeffs);
|
||||
|
||||
file << val;
|
||||
|
||||
if (j < Nv_out - 1)
|
||||
file << " ";
|
||||
}
|
||||
|
||||
file << "\n";
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void save_rho(const NuFISolver &solver,
|
||||
unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
const std::string &filename)
|
||||
{
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
double xmax = Parameters::X_DOMAIN_RIGHT;
|
||||
double dx = (xmax - xmin) / Nx_out;
|
||||
|
||||
file << Nx_out << "\n";
|
||||
file << xmin << " " << xmax << "\n";
|
||||
|
||||
for (unsigned int i = 0; i < Nx_out; ++i, xmin += dx)
|
||||
{
|
||||
double val = solver.eval_rho(n, xmin, E_coeffs);
|
||||
file << val;
|
||||
file << "\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void save_Efield(unsigned int n,
|
||||
const double *E_coeffs,
|
||||
unsigned int Nx_out,
|
||||
const std::string &filename)
|
||||
{
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
double xmax = Parameters::X_DOMAIN_RIGHT;
|
||||
double dx = (xmax - xmin) / Nx_out;
|
||||
|
||||
// select from E_coeffs
|
||||
const size_t stride_x = 1;
|
||||
const size_t stride_t = stride_x*(Parameters::SPLINE_NX + Parameters::SPLINE_ORDER - 1);
|
||||
const double *c;
|
||||
c = E_coeffs + n*stride_t;
|
||||
|
||||
file << Nx_out << "\n";
|
||||
file << xmin << " " << xmax << "\n";
|
||||
|
||||
for (unsigned int i = 0; i < Nx_out; ++i, xmin += dx)
|
||||
{
|
||||
double val = -eval<1>(xmin, c);
|
||||
file << val;
|
||||
file << "\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
Reference in New Issue
Block a user