moved NufiSolver::run to main

This commit is contained in:
VCB Ferreira
2026-08-02 23:48:12 +02:00
parent 14856c7ce0
commit cab3af2004
4 changed files with 123 additions and 137 deletions
-6
View File
@@ -11,7 +11,6 @@
#include "nufi/fields.h" // dont remove #include "nufi/fields.h" // dont remove
#include "nufi/grids.h" #include "nufi/grids.h"
#include "nufi/parameters.h" #include "nufi/parameters.h"
#include "nufi/poisson_problem.h"
using namespace dealii; using namespace dealii;
@@ -19,7 +18,6 @@ class NuFISolver {
public: public:
NuFISolver(); NuFISolver();
void run();
std::vector<double> std::vector<double>
eval_rho(unsigned int n, std::vector<double> &x, eval_rho(unsigned int n, std::vector<double> &x,
const std::vector<GridStructure<1>> &grid_struct, const std::vector<GridStructure<1>> &grid_struct,
@@ -48,10 +46,6 @@ private:
double x_min = Parameters::X_DOMAIN_LEFT; double x_min = Parameters::X_DOMAIN_LEFT;
double x_max = Parameters::X_DOMAIN_RIGHT; double x_max = Parameters::X_DOMAIN_RIGHT;
std::vector<double> rho;
unsigned int order; unsigned int order;
PoissonProblem<1> poisson;
}; };
#endif #endif
-1
View File
@@ -81,7 +81,6 @@ public:
unsigned int get_dof_size(); unsigned int get_dof_size();
void set_rhs_function( void set_rhs_function(
std::function<std::vector<double>(const std::vector<Point<dim>> &)> f); std::function<std::vector<double>(const std::vector<Point<dim>> &)> f);
// void set_rhs(const Vector<double> &new_rhs) { rhs = new_rhs; }
const Vector<double> &get_solution() const { return solution; } const Vector<double> &get_solution() const { return solution; }
const MappingQ<dim> &get_mapping() const { return mapping; } const MappingQ<dim> &get_mapping() const { return mapping; }
+121 -3
View File
@@ -1,6 +1,8 @@
#include <filesystem> #include <filesystem>
#include <iostream> #include <iostream>
#include <nufi/nufi_solver.h> #include <nufi/nufi_solver.h>
#include <nufi/poisson_problem.h>
#include <nufi/save_results.h>
#include <omp.h> #include <omp.h>
void clear_results_directory(const std::string &dir) { void clear_results_directory(const std::string &dir) {
@@ -13,16 +15,132 @@ void clear_results_directory(const std::string &dir) {
std::cout << "Deleted: " << entry.path() << '\n'; std::cout << "Deleted: " << entry.path() << '\n';
} }
} }
};
template <int dim> void run() {
unsigned int Nt = std::floor(Parameters::TMAX / Parameters::DT);
PoissonProblem<dim> poisson(dim);
NuFISolver solver;
using std::abs;
using std::max;
std::cout << "Initializing dealii Poisson Solver\n";
poisson.initialize();
std::vector<double> int_E_squared;
int_E_squared.reserve(Nt);
std::vector<double> int_E_squared_times;
int_E_squared_times.reserve(Nt);
std::vector<GridStructure<1>> grid_versions;
std::vector<SolutionSnapshot<1>> phi_history;
std::ofstream time_file(Parameters::PLOT_DIR + "simulation_time.dat");
double total_time = 0;
stopwatch<double> total_timer;
time_file << "it "
<< "step_time "
<< "total_time "
<< "compute_time "
<< "refine_time "
<< "plot_time"
<< "\n";
std::ofstream error_file(Parameters::PLOT_DIR + "error_estimate.dat");
error_file << "# nufi Kelly l2 error estimate\n";
error_file << "# it l2_error_estimate\n";
for (unsigned int it = 0; it < Nt; ++it) {
stopwatch<double> timer;
double time_elapsed_before = timer.elapsed();
double compute_time = 0.0;
double refine_time = 0.0;
double plot_time = 0.0;
std::cout << "Timestep " << it << " / " << Nt
<< " (simulation time = " << it * Parameters::DT << ")"
<< "\n";
// START: diagnostics
std::cout << "cells = " << poisson.get_triangulation().n_active_cells()
<< "\n"
<< " dofs = " << poisson.get_dof_handler().n_dofs() << "\n";
// END: diagnostics
double compute_start = timer.elapsed();
// compute rho
poisson.set_rhs_function([&](const std::vector<Point<1>> &points) {
std::vector<double> x(points.size());
for (size_t i = 0; i < points.size(); ++i)
x[i] = points[i][0];
return solver.eval_rho(it, x, grid_versions, phi_history, Parameters::NV);
});
if (it % Parameters::REFINE_FREQUENCY == 0) {
refine_time = poisson.solve_step(it, grid_versions, true);
compute_time = timer.elapsed() - compute_start - refine_time;
} else {
poisson.solve_step(it, grid_versions, false);
compute_time = timer.elapsed() - compute_start;
} }
update_solution_history(phi_history, poisson,
grid_versions.back().grid_version);
error_file << it << " " << poisson.get_error_estimate() << "\n";
error_file.flush();
double timer_elapsed = timer.elapsed();
double step_time = timer_elapsed - time_elapsed_before;
std::cout << "step made in " << step_time << " seconds\n\n";
if (it % Parameters::PLOT_FREQUENCY == 0) {
double plot_start = timer.elapsed();
std::cout << "Saving results... ";
DiagnosticsSnapshot snap =
compute_diagnostics(solver, it, grid_versions, phi_history,
Parameters::PLOT_NX, Parameters::NV);
save_f(snap, Parameters::PLOT_DIR + "f_" + std::to_string(it) + ".dat");
save_rho(snap,
Parameters::PLOT_DIR + "rho_" + std::to_string(it) + ".dat");
save_Efield(snap,
Parameters::PLOT_DIR + "E_" + std::to_string(it) + ".dat");
int_E_squared.push_back(compute_int_E_squared(snap));
int_E_squared_times.push_back(it * Parameters::DT);
save_time_series(int_E_squared_times, int_E_squared,
Parameters::PLOT_DIR + "int_E_sqr.dat");
plot_time = timer.elapsed() - plot_start;
std::cout << "Results saved in " << plot_start << "[s]" << "\n";
}
total_time = total_timer.elapsed();
std::cout << "Time since start = " << total_time << "\n\n";
time_file << it << " " << step_time << " " << total_time << " "
<< compute_time << " " << refine_time << " " << plot_time << "\n";
time_file.flush();
}
std::cout << "NuFI simulation finished in " << total_time << " seconds.\n";
};
int main() { int main() {
// omp_set_max_active_levels(1); // omp_set_max_active_levels(1);
std::cout << "Threads: " << omp_get_max_threads() << "\n"; std::cout << "Threads: " << omp_get_max_threads() << "\n";
try { try {
clear_results_directory("results");
NuFISolver solver; clear_results_directory("results");
solver.run(); run<1>(); // 1 = space_dim
} catch (const std::exception &exc) { } catch (const std::exception &exc) {
std::cerr << "\nException:\n" << exc.what() << "\n"; std::cerr << "\nException:\n" << exc.what() << "\n";
+1 -126
View File
@@ -183,129 +183,4 @@ NuFISolver::eval_rho_points(unsigned int n, const std::vector<Point<1>> &points,
return NuFISolver::eval_rho(n, point_vector, grid_struct, phi_history, Nv); return NuFISolver::eval_rho(n, point_vector, grid_struct, phi_history, Nv);
} }
void NuFISolver::run() { NuFISolver::NuFISolver() = default;
//====//====//
// Run prep //
//====//====//
using std::abs;
using std::max;
std::vector<double> int_E_squared;
int_E_squared.reserve(Nt);
std::vector<double> int_E_squared_times;
int_E_squared_times.reserve(Nt);
std::vector<GridStructure<1>> grid_versions;
std::vector<SolutionSnapshot<1>> phi_history;
std::ofstream time_file(Parameters::PLOT_DIR + "simulation_time.dat");
double total_time = 0;
stopwatch<double> total_timer;
time_file << "it "
<< "step_time "
<< "total_time "
<< "compute_time "
<< "refine_time "
<< "plot_time"
<< "\n";
std::ofstream error_file(Parameters::PLOT_DIR + "error_estimate.dat");
error_file << "# nufi Kelly l2 error estimate\n";
error_file << "# it l2_error_estimate\n";
//====//====//
// Time loop//
//====//====//
for (unsigned int it = 0; it < Nt; ++it) {
stopwatch<double> timer;
double time_elapsed_before = timer.elapsed();
double compute_time = 0.0;
double refine_time = 0.0;
double plot_time = 0.0;
std::cout << "Timestep " << it << " / " << Nt
<< " (simulation time = " << it * Parameters::DT << ")"
<< "\n";
// START: diagnostics
std::cout << "cells = " << poisson.get_triangulation().n_active_cells()
<< "\n"
<< " dofs = " << poisson.get_dof_handler().n_dofs() << "\n";
// END: diagnostics
double compute_start = timer.elapsed();
// compute rho
poisson.set_rhs_function([&](const std::vector<Point<1>> &points) {
std::vector<double> x(points.size());
for (size_t i = 0; i < points.size(); ++i)
x[i] = points[i][0];
return eval_rho(it, x, grid_versions, phi_history, Parameters::NV);
});
if (it % Parameters::REFINE_FREQUENCY == 0) {
refine_time = poisson.solve_step(it, grid_versions, true);
compute_time = timer.elapsed() - compute_start - refine_time;
} else {
poisson.solve_step(it, grid_versions, false);
compute_time = timer.elapsed() - compute_start;
}
update_solution_history(phi_history, poisson,
grid_versions.back().grid_version);
error_file << it << " " << poisson.get_error_estimate() << "\n";
error_file.flush();
double timer_elapsed = timer.elapsed();
double step_time = timer_elapsed - time_elapsed_before;
std::cout << "step made in " << step_time << " seconds\n\n";
//====//====//
// Plotting //
//====//====//
if (it % Parameters::PLOT_FREQUENCY == 0) {
double plot_start = timer.elapsed();
std::cout << "Saving results... ";
DiagnosticsSnapshot snap =
compute_diagnostics(*this, it, grid_versions, phi_history,
Parameters::PLOT_NX, Parameters::NV);
save_f(snap, Parameters::PLOT_DIR + "f_" + std::to_string(it) + ".dat");
save_rho(snap,
Parameters::PLOT_DIR + "rho_" + std::to_string(it) + ".dat");
save_Efield(snap,
Parameters::PLOT_DIR + "E_" + std::to_string(it) + ".dat");
int_E_squared.push_back(compute_int_E_squared(snap));
int_E_squared_times.push_back(it * Parameters::DT);
save_time_series(int_E_squared_times, int_E_squared,
Parameters::PLOT_DIR + "int_E_sqr.dat");
plot_time = timer.elapsed() - plot_start;
std::cout << "Results saved in " << plot_start << "[s]" << "\n";
}
total_time = total_timer.elapsed();
std::cout << "Time since start = " << total_time << "\n\n";
time_file << it << " " << step_time << " " << total_time << " "
<< compute_time << " " << refine_time << " " << plot_time << "\n";
time_file.flush();
}
std::cout << "NuFI simulation finished in " << total_time << " seconds.\n";
}
NuFISolver::NuFISolver() : order(Parameters::FE_DEGREE), poisson(order) {
std::cout << "Initializing dealii Poisson Solver\n";
poisson.initialize();
}