mirror of
https://codeberg.org/vcbferreira/NuFI_deal.ii
synced 2026-08-12 22:43:17 +02:00
corrected saving and eval of fields over time
This commit is contained in:
+24
-33
@@ -1,44 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
#include <nufi/nufi_solver.h>
|
||||
|
||||
void clear_results_directory(const std::string &dir)
|
||||
{
|
||||
if (!std::filesystem::exists(dir) || !std::filesystem::is_directory(dir))
|
||||
return;
|
||||
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';
|
||||
}
|
||||
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()
|
||||
{
|
||||
#include <omp.h>
|
||||
std::cout << "Threads: " << omp_get_max_threads() << "\n";
|
||||
try
|
||||
{
|
||||
clear_results_directory("results");
|
||||
int main() {
|
||||
#include <omp.h>
|
||||
std::cout << "Threads: " << omp_get_max_threads() << "\n";
|
||||
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;
|
||||
}
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+109
-108
@@ -1,186 +1,187 @@
|
||||
#include "nufi/nufi_solver.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/qvm/mat_access.hpp>
|
||||
#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 <cstddef>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/save_results.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include "nufi/fields.h"
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include "nufi/save_results.h"
|
||||
#include "nufi/stopwatch.h"
|
||||
|
||||
using namespace dealii;
|
||||
|
||||
double NuFISolver::eval_ftilda(unsigned int n,
|
||||
double x,
|
||||
double u,
|
||||
const PoissonProblem<1> &poisson) const
|
||||
{
|
||||
if ( n == 0 ) return f0(x,u);
|
||||
|
||||
double
|
||||
NuFISolver::eval_ftilda(unsigned int n, double x, double u,
|
||||
const PoissonProblem<1> &poisson,
|
||||
const std::vector<Vector<double>> &phi_history) const {
|
||||
if (n == 0)
|
||||
return f0(x, u);
|
||||
|
||||
double Ex;
|
||||
|
||||
// We omit the initial half-step.
|
||||
|
||||
while ( --n )
|
||||
{
|
||||
x = x - Parameters::DT *u;
|
||||
Ex = -eval(x, poisson);
|
||||
u = u + Parameters::DT *Ex;
|
||||
while (--n) {
|
||||
x = x - Parameters::DT * u;
|
||||
Ex = -eval(x, poisson, phi_history[n]);
|
||||
u = u + Parameters::DT * Ex;
|
||||
}
|
||||
|
||||
// The final half-step.
|
||||
x -= Parameters::DT*u;
|
||||
Ex = -eval(x, poisson);
|
||||
u += 0.5*Parameters::DT*Ex;
|
||||
x = x - Parameters::DT * u;
|
||||
Ex = -eval(x, poisson, phi_history[n]);
|
||||
u += 0.5 * Parameters::DT * Ex;
|
||||
|
||||
return f0(x,u);
|
||||
return f0(x, u);
|
||||
}
|
||||
|
||||
double NuFISolver::eval_f(unsigned int n,
|
||||
double x,
|
||||
double u,
|
||||
const PoissonProblem<1> &poisson) const
|
||||
{
|
||||
if ( n == 0 ) return f0(x,u);
|
||||
|
||||
double
|
||||
NuFISolver::eval_f(unsigned int n, double x, double u,
|
||||
const PoissonProblem<1> &poisson,
|
||||
const std::vector<Vector<double>> &phi_history) const {
|
||||
if (n == 0)
|
||||
return f0(x, u);
|
||||
|
||||
double Ex;
|
||||
|
||||
// Initial half-step.
|
||||
Ex = -eval(x, poisson);
|
||||
u += 0.5*Parameters::DT * Ex;
|
||||
Ex = -eval(x, poisson, phi_history[n]);
|
||||
u += 0.5 * Parameters::DT * Ex;
|
||||
|
||||
while ( --n )
|
||||
{
|
||||
x = x - Parameters::DT *u;
|
||||
Ex = -eval(x, poisson);
|
||||
u = u + Parameters::DT *Ex;
|
||||
while (--n) {
|
||||
x = x - Parameters::DT * u;
|
||||
Ex = -eval(x, poisson, phi_history[n]);
|
||||
u = u + Parameters::DT * Ex;
|
||||
}
|
||||
|
||||
// The final half-step.
|
||||
x -= Parameters::DT*u;
|
||||
Ex = -eval(x, poisson);
|
||||
u += 0.5*Parameters::DT*Ex;
|
||||
x = x - Parameters::DT * u;
|
||||
Ex = -eval(x, poisson, phi_history[n]);
|
||||
u += 0.5 * Parameters::DT * Ex;
|
||||
|
||||
return f0(x,u);
|
||||
return f0(x, u);
|
||||
}
|
||||
|
||||
double NuFISolver::eval_rho(const unsigned int n,
|
||||
const double x,
|
||||
const PoissonProblem<1> &poisson,
|
||||
const unsigned int Nv) const
|
||||
{
|
||||
const double dv = (Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
|
||||
double NuFISolver::eval_rho(const unsigned int n, const double x,
|
||||
const PoissonProblem<1> &poisson,
|
||||
const std::vector<Vector<double>> &phi_history,
|
||||
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;
|
||||
|
||||
#pragma omp parallel for reduction (+ : integral)
|
||||
for (unsigned int i = 0; i < Nv; ++i)
|
||||
integral += eval_ftilda(n, x, v_min + i * dv, poisson);
|
||||
|
||||
return 1.0 - integral*dv;
|
||||
if (n == 0) {
|
||||
for (unsigned int i = 0; i < Nv; ++i)
|
||||
integral += f0(x, v_min + i * dv);
|
||||
} else {
|
||||
#pragma omp parallel for reduction(+ : integral)
|
||||
for (unsigned int i = 0; i < Nv; ++i)
|
||||
integral += eval_ftilda(n, x, v_min + i * dv, poisson, phi_history);
|
||||
}
|
||||
return 1.0 - integral * dv;
|
||||
}
|
||||
|
||||
void NuFISolver::run()
|
||||
{
|
||||
void NuFISolver::run() {
|
||||
std::cout << "Building E_sline\n\n";
|
||||
|
||||
using std::abs;
|
||||
using std::max;
|
||||
|
||||
std::unique_ptr<double,decltype(std::free)*> rho { reinterpret_cast<double*>(std::aligned_alloc(64,sizeof(double)*Nx)), std::free };
|
||||
std::unique_ptr<double, decltype(std::free) *> rho{
|
||||
reinterpret_cast<double *>(std::aligned_alloc(64, sizeof(double) * Nx)),
|
||||
std::free};
|
||||
|
||||
std::vector<double> int_E_squared;
|
||||
int_E_squared.reserve(Nt);
|
||||
|
||||
if ( rho == nullptr ) throw std::bad_alloc {};
|
||||
std::vector<Vector<double>> phi_history;
|
||||
|
||||
Gradient grad(x_min, x_max, Nx);
|
||||
if (rho == nullptr)
|
||||
throw std::bad_alloc{};
|
||||
|
||||
double total_time = 0;
|
||||
std::ofstream time_file("results/simulation_time.txt");
|
||||
|
||||
for (unsigned int it = 0; it < Nt; ++it)
|
||||
{
|
||||
stopwatch<double> timer;
|
||||
|
||||
double time_elapsed_before = timer.elapsed();
|
||||
for (unsigned int it = 0; it < Nt; ++it) {
|
||||
stopwatch<double> timer;
|
||||
|
||||
std::cout << "Timestep " << it << " / " << Nt << " (simulation time = "<< it*Parameters::DT << ")"<< std::endl;
|
||||
double time_elapsed_before = timer.elapsed();
|
||||
|
||||
// compute rho
|
||||
std::cout << "Timestep " << it << " / " << Nt
|
||||
<< " (simulation time = " << it * Parameters::DT << ")"
|
||||
<< std::endl;
|
||||
|
||||
double dx = Parameters::CALC_DX;
|
||||
|
||||
#pragma omp parallel for
|
||||
for(size_t i = 0; i<Nx; i++)
|
||||
{
|
||||
double x = Parameters::X_DOMAIN_LEFT + i*dx;
|
||||
double ith_rho = eval_rho(it, x, poisson, Parameters::NV);
|
||||
// compute rho
|
||||
|
||||
AssertThrow(std::isfinite(ith_rho), ExcMessage("NaN detected in rho"));
|
||||
rho.get()[i] = ith_rho;
|
||||
}
|
||||
double dx = Parameters::CALC_DX;
|
||||
|
||||
poisson.set_rhs_function(std::make_unique<ChargeDensity_NuFI<1>>(rho.get(), Nx));
|
||||
poisson.solve_step();
|
||||
#pragma omp parallel for
|
||||
for (size_t i = 0; i < Nx; i++) {
|
||||
double x = Parameters::X_DOMAIN_LEFT + i * dx;
|
||||
double ith_rho = eval_rho(it, x, poisson, phi_history, Parameters::NV);
|
||||
|
||||
std::vector<double> sampled_potential = poisson.sample_electric_potential(x_min, x_max, Nx); // Solution of FE
|
||||
AssertThrow(std::isfinite(ith_rho), ExcMessage("NaN detected in rho"));
|
||||
rho.get()[i] = ith_rho;
|
||||
}
|
||||
|
||||
// These have been tested to be equivalent
|
||||
// ////////////////////////////////////////////////
|
||||
// std::vector<double> E_vals = grad.compute(sampled_potential); // vector grad of FE solution
|
||||
// save_space_vector(E_vals, "electric", it);
|
||||
// std::vector<double> E_vals_deal = poisson.sample_electric_field(x_min, x_max, Nx); // FE grad of soution
|
||||
// save_space_vector(E_vals_deal, "electricdeal", it);
|
||||
// ////////////////////////////////////////////////
|
||||
poisson.set_rhs_function(
|
||||
std::make_unique<ChargeDensity_NuFI<1>>(rho.get(), Nx));
|
||||
poisson.solve_step();
|
||||
|
||||
phi_history.push_back(poisson.get_solution());
|
||||
|
||||
// interpolate and save current field
|
||||
std::vector<double> sampled_potential =
|
||||
poisson.sample_electric_potential(x_min, x_max, Nx); // Solution of FE
|
||||
|
||||
std::vector<double> E_x(Nx,0.0) ;
|
||||
#pragma omp parallel for
|
||||
for(size_t ix=0; ix<Nx; ++ix)
|
||||
{
|
||||
E_x[ix] = -eval(Parameters::X_DOMAIN_LEFT+ix*dx, poisson);
|
||||
double timer_elapsed = timer.elapsed();
|
||||
double step_time = timer_elapsed - time_elapsed_before;
|
||||
total_time += timer_elapsed;
|
||||
|
||||
std::cout << "step made in " << step_time << " seconds\n\n";
|
||||
if (it % Parameters::PLOT_FREQUENCY == 0) {
|
||||
std::cout << "Saving results... ";
|
||||
save_f(*this, it, poisson, phi_history, Parameters::PLOT_NX,
|
||||
Parameters::NV, "results/ftilda_" + std::to_string(it) + ".dat");
|
||||
save_rho(*this, it, poisson, phi_history, Parameters::PLOT_NX,
|
||||
"results/rho_" + std::to_string(it) + ".dat");
|
||||
// save_Efield(it, coeffs.get(), 128, "results/field_" +
|
||||
// std::to_string(it) + ".dat");
|
||||
|
||||
std::vector<double> E_x(Nx, 0.0);
|
||||
#pragma omp parallel for
|
||||
for (size_t ix = 0; ix < Nx; ++ix) {
|
||||
E_x[ix] = -eval(Parameters::X_DOMAIN_LEFT + ix * dx, poisson,
|
||||
phi_history[it]);
|
||||
}
|
||||
|
||||
double timer_elapsed = timer.elapsed();
|
||||
total_time += timer_elapsed;
|
||||
|
||||
std::cout << "step made in "<< timer_elapsed-time_elapsed_before <<" seconds\n\n";
|
||||
if (it % Parameters::PLOT_FREQUENCY == 0)
|
||||
{
|
||||
std::cout << "Saving results... ";
|
||||
save_f(*this, it, poisson, Parameters::PLOT_NX, Parameters::NV, "results/ftilda_" + std::to_string(it) + ".dat");
|
||||
save_rho(*this, it, poisson, Parameters::PLOT_NX, "results/rho_" + std::to_string(it) + ".dat");
|
||||
// save_Efield(it, coeffs.get(), 128, "results/field_" + std::to_string(it) + ".dat");
|
||||
save_space_vector(E_x, "field", it);
|
||||
save_space_vector(E_x, "field", it);
|
||||
|
||||
double int_val = 0.5 * integral_space_vector_squared(poisson);
|
||||
int_E_squared.push_back(int_val);
|
||||
save_space_vector(int_E_squared, "electricint", it);
|
||||
std::cout << "Time since start = "<< total_time<<"\n\n";
|
||||
}
|
||||
double int_val =
|
||||
0.5 * integral_space_vector_squared(poisson, phi_history[it]);
|
||||
int_E_squared.push_back(int_val);
|
||||
save_space_vector(int_E_squared, "electricint", it);
|
||||
std::cout << "Time since start = " << total_time << "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "NuFI simulation finished in "<< total_time <<" seconds.\n";
|
||||
std::cout << "NuFI simulation finished in " << total_time << " seconds.\n";
|
||||
}
|
||||
|
||||
NuFISolver::NuFISolver()
|
||||
: order(Parameters::FE_DEGREE),
|
||||
poisson(order)
|
||||
{
|
||||
NuFISolver::NuFISolver() : order(Parameters::FE_DEGREE), poisson(order) {
|
||||
std::cout << "Initializing dealii Poisson Solver\n";
|
||||
poisson.initialize();
|
||||
}
|
||||
|
||||
+44
-52
@@ -1,22 +1,18 @@
|
||||
#include "nufi/save_results.h"
|
||||
|
||||
#include "nufi/fields.h"
|
||||
#include "nufi/nufi_solver.h"
|
||||
#include "nufi/parameters.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "nufi/nufi_solver.h"
|
||||
#include "nufi/poisson_problem.h"
|
||||
#include "nufi/fields.h"
|
||||
|
||||
|
||||
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_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) {
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
@@ -32,34 +28,30 @@ void save_f( const NuFISolver &solver,
|
||||
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 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;
|
||||
for (unsigned int j = 0; j < Nv_out; ++j) {
|
||||
double v = vmin + (j + 0.5) * dv;
|
||||
|
||||
double val = solver.eval_f(n, x, v, poisson);
|
||||
double val = solver.eval_f(n, x, v, poisson, phi_history);
|
||||
|
||||
file << val;
|
||||
file << val;
|
||||
|
||||
if (j < Nv_out - 1)
|
||||
file << " ";
|
||||
}
|
||||
if (j < Nv_out - 1)
|
||||
file << " ";
|
||||
}
|
||||
|
||||
file << "\n";
|
||||
file << "\n";
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void save_rho(const NuFISolver &solver,
|
||||
unsigned int n,
|
||||
const PoissonProblem<1> &poisson,
|
||||
unsigned int Nx_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) {
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
@@ -69,20 +61,18 @@ void save_rho(const NuFISolver &solver,
|
||||
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, poisson);
|
||||
file << val;
|
||||
file << "\n";
|
||||
for (unsigned int i = 0; i < Nx_out; ++i, xmin += dx) {
|
||||
double val = solver.eval_rho(n, xmin, poisson, phi_history);
|
||||
file << val;
|
||||
file << "\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void save_Efield([[maybe_unused]]unsigned int n,
|
||||
const PoissonProblem<1> &poisson,
|
||||
unsigned int Nx_out,
|
||||
const std::string &filename)
|
||||
{
|
||||
void save_Efield([[maybe_unused]] unsigned int n,
|
||||
const PoissonProblem<1> &poisson,
|
||||
const std::vector<Vector<double>> &phi_history,
|
||||
unsigned int Nx_out, const std::string &filename) {
|
||||
std::ofstream file(filename);
|
||||
|
||||
double xmin = Parameters::X_DOMAIN_LEFT;
|
||||
@@ -94,23 +84,25 @@ void save_Efield([[maybe_unused]]unsigned int n,
|
||||
file << Nx_out << "\n";
|
||||
file << xmin << " " << xmax << "\n";
|
||||
|
||||
for (unsigned int i = 0; i < Nx_out; ++i, xmin += dx)
|
||||
{
|
||||
double val = -eval(xmin, poisson);
|
||||
file << val;
|
||||
file << "\n";
|
||||
for (unsigned int i = 0; i < Nx_out; ++i, xmin += dx) {
|
||||
double val = -eval(xmin, poisson, phi_history[n]);
|
||||
file << val;
|
||||
file << "\n";
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
void save_space_vector(const std::vector<double>& vals, const std::string& filename, size_t it)
|
||||
{
|
||||
std::ofstream file("results/" + filename + "_" + std::to_string(it) + ".dat");
|
||||
void save_space_vector(const std::vector<double> &vals,
|
||||
const std::string &filename, size_t it) {
|
||||
std::ofstream file("results/" + filename + "_" + std::to_string(it) + ".dat");
|
||||
|
||||
if (!file) throw std::runtime_error("failed to start file in results/");
|
||||
if (!file)
|
||||
throw std::runtime_error("failed to start file in results/");
|
||||
|
||||
file << vals.size() << "\n";
|
||||
file << Parameters::X_DOMAIN_LEFT << " " << Parameters::X_DOMAIN_RIGHT << "\n";
|
||||
file << std::fixed << std::setprecision(8);
|
||||
for (double val : vals) file << val << "\n";
|
||||
file << vals.size() << "\n";
|
||||
file << Parameters::X_DOMAIN_LEFT << " " << Parameters::X_DOMAIN_RIGHT
|
||||
<< "\n";
|
||||
file << std::fixed << std::setprecision(8);
|
||||
for (double val : vals)
|
||||
file << val << "\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user