added stop watch

This commit is contained in:
Vasco C. B. Ferreira
2026-04-09 18:02:02 +02:00
parent ee8a7dbaf5
commit 6eb1e6faa1
3 changed files with 48 additions and 4 deletions
BIN
View File
Binary file not shown.
+37
View File
@@ -0,0 +1,37 @@
#ifndef STOPWATCH_H
#define STOPWATCH_H
#include <chrono>
template <typename real>
class stopwatch
{
public:
void reset();
real elapsed();
private:
using clock = std::chrono::high_resolution_clock;
clock::time_point t0 { clock::now() };
};
template <typename real> inline
void stopwatch<real>::reset()
{
t0 = clock::now();
}
template <typename real> inline
real stopwatch<real>::elapsed()
{
using seconds = std::chrono::duration<real,std::ratio<1,1>>;
auto tnow = clock::now();
auto duration = std::chrono::duration_cast<seconds>( tnow - t0 );
return duration.count();
}
#endif // STOPWATCH_H
+11 -4
View File
@@ -17,6 +17,7 @@
#include "nufi/save_results.h" #include "nufi/save_results.h"
#include "nufi/poisson_problem.h" #include "nufi/poisson_problem.h"
#include "nufi/fields.h" #include "nufi/fields.h"
#include "nufi/stopwatch.h"
using namespace dealii; using namespace dealii;
@@ -87,8 +88,12 @@ void NuFISolver::run()
Gradient grad(x_min, x_max, Nx); Gradient grad(x_min, x_max, Nx);
double total_time = 0;
for (unsigned int it = 0; it < Nt; ++it) for (unsigned int it = 0; it < Nt; ++it)
{ {
stopwatch<double> timer;
std::cout << "Timestep " << it << " / " << Nt << std::endl << std::endl; std::cout << "Timestep " << it << " / " << Nt << std::endl << std::endl;
// compute rho // compute rho
@@ -127,9 +132,12 @@ void NuFISolver::run()
E_x[ix] = -eval<1>(Parameters::X_DOMAIN_LEFT+ix*dx, current_coeffs); E_x[ix] = -eval<1>(Parameters::X_DOMAIN_LEFT+ix*dx, current_coeffs);
} }
double timer_elapsed = timer.elapsed();
total_time += timer_elapsed;
if (it % Parameters::PLOT_FREQUENCY == 0) if (it % Parameters::PLOT_FREQUENCY == 0)
{ {
std::cout << "Saving results... \n\n"; std::cout << "Saving results... ";
save_ftilda(*this, it, coeffs.get(), 128, 128, "results/ftilda_" + std::to_string(it) + ".dat"); 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_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"); // save_Efield(it, coeffs.get(), 128, "results/field_" + std::to_string(it) + ".dat");
@@ -138,12 +146,11 @@ void NuFISolver::run()
double int_val = 0.5 * integral_space_vector_squared(current_coeffs); double int_val = 0.5 * integral_space_vector_squared(current_coeffs);
int_E_squared.push_back(int_val); int_E_squared.push_back(int_val);
save_space_vector(int_E_squared, "electricint", it); save_space_vector(int_E_squared, "electricint", it);
std::cout << "Time since start = "<< total_time<<"\n\n";
} }
} }
std::cout << "NuFI simulation finished.\n"; std::cout << "NuFI simulation finished in "<< total_time <<" seconds.\n";
} }
NuFISolver::NuFISolver() NuFISolver::NuFISolver()