new paradigm, grids saved when changed old solutions not interpolated to new grids

This commit is contained in:
Vasco C. B. Ferreira
2026-07-09 19:23:15 +02:00
parent 47d7540961
commit 43eec84c40
9 changed files with 219 additions and 154 deletions
+47 -27
View File
@@ -17,6 +17,7 @@
#include <vector>
#include "nufi/fields.h"
#include "nufi/grids.h"
#include "nufi/parameters.h"
#include "nufi/poisson_problem.h"
#include "nufi/save_results.h"
@@ -24,10 +25,10 @@
using namespace dealii;
std::vector<double>
NuFISolver::eval_ftilda(unsigned int n, std::vector<double> &X, double u,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history) const {
std::vector<double> NuFISolver::eval_ftilda(
unsigned int n, std::vector<double> &X, double u,
const std::vector<GridStructure<1>> &grid_struct,
const std::vector<SolutionSnapshot<1>> &phi_history) const {
size_t x_size = X.size();
@@ -48,7 +49,8 @@ NuFISolver::eval_ftilda(unsigned int n, std::vector<double> &X, double u,
for (size_t i = 0; i < x_size; ++i)
X[i] = X[i] - Parameters::DT * U[i];
tmp = eval(X, poisson, phi_history[n]); // call eval only once
tmp = eval(X, grid_struct[phi_history[n].grid_version],
phi_history[n].solution); // call eval only once
for (size_t i = 0; i < x_size; ++i) {
Ex[i] = -tmp[i];
@@ -60,7 +62,8 @@ NuFISolver::eval_ftilda(unsigned int n, std::vector<double> &X, double u,
for (size_t i = 0; i < x_size; ++i)
X[i] = X[i] - Parameters::DT * U[i];
tmp = eval(X, poisson, phi_history[n]); // call eval only once
tmp = eval(X, grid_struct[phi_history[n].grid_version],
phi_history[n].solution); // call eval only once
for (size_t i = 0; i < x_size; ++i) {
Ex[i] = -tmp[i];
@@ -74,8 +77,8 @@ NuFISolver::eval_ftilda(unsigned int n, std::vector<double> &X, double u,
std::vector<double>
NuFISolver::eval_f(unsigned int n, std::vector<double> &X, double u,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history) const {
const std::vector<GridStructure<1>> &grid_struct,
const std::vector<SolutionSnapshot<1>> &phi_history) const {
size_t x_size = X.size();
@@ -92,7 +95,8 @@ NuFISolver::eval_f(unsigned int n, std::vector<double> &X, double u,
std::vector<double> tmp(x_size);
// Initial half-step.
tmp = eval(X, poisson, phi_history[n]); // call eval only once
tmp = eval(X, grid_struct[phi_history[n].grid_version],
phi_history[n].solution); // call eval only once
for (size_t i = 0; i < x_size; ++i) {
Ex[i] = -tmp[i];
U[i] = U[i] + 0.5 * Parameters::DT * Ex[i];
@@ -102,7 +106,8 @@ NuFISolver::eval_f(unsigned int n, std::vector<double> &X, double u,
for (size_t i = 0; i < x_size; ++i)
X[i] = X[i] - Parameters::DT * U[i];
tmp = eval(X, poisson, phi_history[n]); // call eval only once
tmp = eval(X, grid_struct[phi_history[n].grid_version],
phi_history[n].solution); // call eval only once
for (size_t i = 0; i < x_size; ++i) {
Ex[i] = -tmp[i];
U[i] = U[i] + Parameters::DT * Ex[i];
@@ -113,7 +118,8 @@ NuFISolver::eval_f(unsigned int n, std::vector<double> &X, double u,
for (size_t i = 0; i < x_size; ++i)
X[i] = X[i] - Parameters::DT * U[i];
tmp = eval(X, poisson, phi_history[n]); // call eval only once
tmp = eval(X, grid_struct[phi_history[n].grid_version],
phi_history[n].solution); // call eval only once
for (size_t i = 0; i < x_size; ++i) {
Ex[i] = -tmp[i];
U[i] = U[i] + 0.5 * Parameters::DT * Ex[i];
@@ -127,20 +133,21 @@ NuFISolver::eval_f(unsigned int n, std::vector<double> &X, double u,
std::vector<double>
NuFISolver::eval_rho(unsigned int n, std::vector<double> &X,
const PoissonProblem<1> &poisson,
const std::vector<Vector<double>> &phi_history,
const std::vector<GridStructure<1>> &grid_struct,
const std::vector<SolutionSnapshot<1>> &phi_history,
const unsigned int Nv) const {
size_t x_size = X.size();
const double dv =
(Parameters::V_DOMAIN_RIGHT - Parameters::V_DOMAIN_LEFT) / Nv;
const double v_min = Parameters::V_DOMAIN_LEFT + 0.5 * dv;
std::vector<double> integral(x_size, 0.0);
std::vector<double> tmp_int(x_size);
for (unsigned int i = 0; i < Nv; ++i) {
tmp_int = eval_ftilda(n, X, v_min + i * dv, poisson,
tmp_int = eval_ftilda(n, X, v_min + i * dv, grid_struct,
phi_history); // used eval_ftilda once per i
for (size_t ii = 0; ii < x_size; ++ii)
integral[ii] += tmp_int[ii];
@@ -164,16 +171,21 @@ void NuFISolver::run() {
reinterpret_cast<double *>(std::aligned_alloc(64, sizeof(double) * Nx)),
std::free};
if (rho == nullptr)
throw std::bad_alloc{};
std::vector<double> int_E_squared;
int_E_squared.reserve(Nt);
std::vector<Vector<double>> phi_history;
std::vector<GridStructure<1>> grid_versions;
std::vector<SolutionSnapshot<1>> phi_history;
update_grid_versions(grid_versions, poisson);
update_solution_history(phi_history, poisson,
grid_versions.back().grid_version);
std::vector<double> x_eval(Parameters::CALC_NX);
if (rho == nullptr)
throw std::bad_alloc{};
std::ofstream time_file("results/simulation_time.dat");
double total_time = 0;
@@ -224,7 +236,7 @@ void NuFISolver::run() {
// compute rho
std::vector<double> x_eval = make_x_eval(poisson.get_dof_size());
std::vector<double> rho_values =
eval_rho(it, x_eval, poisson, phi_history, Parameters::NV);
eval_rho(it, x_eval, grid_versions, phi_history, Parameters::NV);
Vector<double> rhs(x_eval.size());
for (unsigned int i = 0; i < rhs.size(); ++i)
@@ -233,18 +245,23 @@ void NuFISolver::run() {
poisson.set_rhs(rhs);
poisson.solve_step();
phi_history.push_back(poisson.get_solution());
compute_time = timer.elapsed() - compute_start;
if (it % Parameters::REFINE_FREQUENCY == 0 && it != 0) {
double refine_start = timer.elapsed();
poisson.coarse_and_refine_grid(it, phi_history);
poisson.coarse_and_refine_grid(it);
update_grid_versions(grid_versions, poisson);
refine_time = timer.elapsed() - refine_start;
std::cout << "Refinement step done in "
<< std::to_string(std::round(std::floor(refine_time))) << "[s]"
<< "\n";
}
update_solution_history(phi_history, poisson,
grid_versions.back().grid_version);
double timer_elapsed = timer.elapsed();
double step_time = timer_elapsed - time_elapsed_before;
@@ -253,30 +270,33 @@ void NuFISolver::run() {
if (it % Parameters::PLOT_FREQUENCY == 0) {
double plot_start = timer.elapsed();
std::cout << "Saving results... ";
save_f(*this, it, poisson, phi_history, Parameters::PLOT_NX,
save_f(*this, it, grid_versions, phi_history, Parameters::PLOT_NX,
Parameters::NV, "results/ftilda_" + std::to_string(it) + ".dat");
save_rho(*this, it, poisson, phi_history, Parameters::PLOT_NX,
save_rho(*this, it, grid_versions, 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> x_eval_Ex = make_x_eval(Parameters::PLOT_NX);
std::vector<double> tmp_rho(x_eval_Ex.size());
tmp_rho = eval(x_eval_Ex, poisson, phi_history[it]);
tmp_rho = eval(x_eval_Ex, grid_versions[phi_history[it].grid_version],
phi_history[it].solution);
std::vector<double> E_x(Parameters::PLOT_NX);
for (size_t i = 0; i < Parameters::PLOT_NX; ++i)
E_x[i] = -tmp_rho[i];
save_space_vector(E_x, "field", it);
double int_val =
0.5 * integral_space_vector_squared(poisson, phi_history[it]);
double int_val = 0.5 * integral_space_vector_squared(
grid_versions[phi_history[it].grid_version],
phi_history[it].solution);
int_E_squared.push_back(int_val);
save_space_vector(int_E_squared, "electricint", it);
std::cout << "Time since start = " << total_time << "\n\n";
plot_time = timer.elapsed() - plot_start;
}
total_time = timer.elapsed();
time_file << it << " " << step_time << " " << total_time << " "
<< compute_time << " " << refine_time << " " << plot_time << "\n";