Skip to content
Snippets Groups Projects
Commit 8c388633 authored by Chichi Lalescu's avatar Chichi Lalescu
Browse files

define main_loop in direct_numerical_simulation

this means i defined a virtual "step" method, as well as "do_stats" and
"write_checkpoint".
I think this makes it even more clear what job is assigned to which
class.
parent 4004e18c
Branches
Tags
1 merge request!21Bugfix/nansampling
...@@ -62,75 +62,19 @@ int NSVE<rnumber>::initialize(void) ...@@ -62,75 +62,19 @@ int NSVE<rnumber>::initialize(void)
} }
template <typename rnumber> template <typename rnumber>
int NSVE<rnumber>::main_loop(void) int NSVE<rnumber>::step(void)
{ {
clock_t time0, time1; this->fs->step(this->dt);
double time_difference, local_time_difference;
time0 = clock();
bool stop_code_now = false;
int max_iter = (this->iteration + this->niter_todo -
(this->iteration % this->niter_todo));
for (; this->iteration < max_iter;)
{
#ifdef USE_TIMINGOUTPUT
const std::string loopLabel = ("code::main_start::loop-" +
std::to_string(this->iteration));
TIMEZONE(loopLabel.c_str());
#endif
if (this->iteration % this->niter_stat == 0)
this->do_stats();
this->fs->step(dt);
this->iteration = this->fs->iteration; this->iteration = this->fs->iteration;
if (this->fs->iteration % this->niter_out == 0) return EXIT_SUCCESS;
{
this->fs->io_checkpoint(false);
this->checkpoint = this->fs->checkpoint;
this->write_iteration();
}
if (stop_code_now)
break;
time1 = clock();
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
time0 = time1;
} }
if (this->iteration % this->niter_stat == 0)
this->do_stats(); template <typename rnumber>
time1 = clock(); int NSVE<rnumber>::write_checkpoint(void)
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->iteration % this->niter_out != 0)
{ {
this->fs->io_checkpoint(false); this->fs->io_checkpoint(false);
this->checkpoint = this->fs->checkpoint; this->checkpoint = this->fs->checkpoint;
this->write_iteration(); this->write_iteration();
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -147,6 +91,8 @@ int NSVE<rnumber>::finalize(void) ...@@ -147,6 +91,8 @@ int NSVE<rnumber>::finalize(void)
template <typename rnumber> template <typename rnumber>
int NSVE<rnumber>::do_stats() int NSVE<rnumber>::do_stats()
{ {
if (!(this->iteration % this->niter_stat == 0))
return EXIT_SUCCESS;
hid_t stat_group; hid_t stat_group;
if (this->myrank == 0) if (this->myrank == 0)
stat_group = H5Gopen( stat_group = H5Gopen(
...@@ -174,24 +120,6 @@ int NSVE<rnumber>::do_stats() ...@@ -174,24 +120,6 @@ int NSVE<rnumber>::do_stats()
if (this->myrank == 0) if (this->myrank == 0)
H5Gclose(stat_group); H5Gclose(stat_group);
if (myrank == 0)
{
std::string fname = (
std::string("stop_") +
std::string(simname));
{
struct stat file_buffer;
this->stop_code_now = (
stat(fname.c_str(), &file_buffer) == 0);
}
}
MPI_Bcast(
&this->stop_code_now,
1,
MPI_C_BOOL,
0,
MPI_COMM_WORLD);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
......
...@@ -66,10 +66,11 @@ class NSVE: public direct_numerical_simulation ...@@ -66,10 +66,11 @@ class NSVE: public direct_numerical_simulation
~NSVE(){} ~NSVE(){}
int initialize(void); int initialize(void);
int main_loop(void); int step(void);
int finalize(void); int finalize(void);
int read_parameters(void); int read_parameters(void);
int write_checkpoint(void);
int do_stats(void); int do_stats(void);
}; };
......
...@@ -80,83 +80,18 @@ int NSVEp<rnumber>::initialize(void) ...@@ -80,83 +80,18 @@ int NSVEp<rnumber>::initialize(void)
} }
template <typename rnumber> template <typename rnumber>
int NSVEp<rnumber>::main_loop(void) int NSVEp<rnumber>::step(void)
{ {
clock_t time0, time1;
double time_difference, local_time_difference;
time0 = clock();
bool stop_code_now = false;
int max_iter = (this->iteration + this->niter_todo -
(this->iteration % this->niter_todo));
for (; this->iteration < max_iter;)
{
#ifdef USE_TIMINGOUTPUT
const std::string loopLabel = ("code::main_start::loop-" +
std::to_string(this->iteration));
TIMEZONE(loopLabel.c_str());
#endif
if (this->iteration % this->niter_stat == 0)
this->do_stats();
this->fs->compute_velocity(fs->cvorticity); this->fs->compute_velocity(fs->cvorticity);
this->fs->cvelocity->ift(); this->fs->cvelocity->ift();
this->ps->completeLoop(dt); this->ps->completeLoop(this->dt);
this->fs->step(this->dt);
this->fs->step(dt);
this->iteration = this->fs->iteration; this->iteration = this->fs->iteration;
if (this->fs->iteration % this->niter_out == 0) return EXIT_SUCCESS;
{
this->fs->io_checkpoint(false);
this->particles_output_writer_mpi->open_file(fs->get_current_fname());
this->particles_output_writer_mpi->save(
this->ps->getParticlesPositions(),
this->ps->getParticlesRhs(),
this->ps->getParticlesIndexes(),
this->ps->getLocalNbParticles(),
this->fs->iteration);
this->particles_output_writer_mpi->close_file();
this->checkpoint = this->fs->checkpoint;
this->write_iteration();
}
if (stop_code_now)
break;
time1 = clock();
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
time0 = time1;
} }
if (this->iteration % this->niter_stat == 0)
this->do_stats(); template <typename rnumber>
time1 = clock(); int NSVEp<rnumber>::write_checkpoint(void)
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration << " took " << time_difference/nprocs << " seconds" << std::endl;
if (this->iteration % this->niter_out != 0)
{ {
this->fs->io_checkpoint(false); this->fs->io_checkpoint(false);
this->particles_output_writer_mpi->open_file(fs->get_current_fname()); this->particles_output_writer_mpi->open_file(fs->get_current_fname());
...@@ -169,7 +104,6 @@ int NSVEp<rnumber>::main_loop(void) ...@@ -169,7 +104,6 @@ int NSVEp<rnumber>::main_loop(void)
this->particles_output_writer_mpi->close_file(); this->particles_output_writer_mpi->close_file();
this->checkpoint = this->fs->checkpoint; this->checkpoint = this->fs->checkpoint;
this->write_iteration(); this->write_iteration();
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
...@@ -187,6 +121,9 @@ int NSVEp<rnumber>::finalize(void) ...@@ -187,6 +121,9 @@ int NSVEp<rnumber>::finalize(void)
template <typename rnumber> template <typename rnumber>
int NSVEp<rnumber>::do_stats() int NSVEp<rnumber>::do_stats()
{
// fluid stats go here
if (this->iteration % this->niter_stat == 0)
{ {
hid_t stat_group; hid_t stat_group;
if (this->myrank == 0) if (this->myrank == 0)
...@@ -215,24 +152,11 @@ int NSVEp<rnumber>::do_stats() ...@@ -215,24 +152,11 @@ int NSVEp<rnumber>::do_stats()
if (this->myrank == 0) if (this->myrank == 0)
H5Gclose(stat_group); H5Gclose(stat_group);
if (myrank == 0)
{
std::string fname = (
std::string("stop_") +
std::string(simname));
{
struct stat file_buffer;
this->stop_code_now = (
stat(fname.c_str(), &file_buffer) == 0);
}
} }
MPI_Bcast( // particle sampling should go here
&this->stop_code_now, //if (this->iteration % this->niter_part == 0)
1, //{
MPI_C_BOOL, //}
0,
MPI_COMM_WORLD);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
......
...@@ -76,10 +76,11 @@ class NSVEp: public direct_numerical_simulation ...@@ -76,10 +76,11 @@ class NSVEp: public direct_numerical_simulation
~NSVEp(){} ~NSVEp(){}
int initialize(void); int initialize(void);
int main_loop(void); int step(void);
int finalize(void); int finalize(void);
int read_parameters(void); int read_parameters(void);
int write_checkpoint(void);
int do_stats(void); int do_stats(void);
}; };
......
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include "direct_numerical_simulation.hpp" #include "direct_numerical_simulation.hpp"
int grow_single_dataset(hid_t dset, int tincrement) int grow_single_dataset(hid_t dset, int tincrement)
...@@ -38,6 +41,7 @@ direct_numerical_simulation::direct_numerical_simulation( ...@@ -38,6 +41,7 @@ direct_numerical_simulation::direct_numerical_simulation(
{ {
MPI_Comm_rank(this->comm, &this->myrank); MPI_Comm_rank(this->comm, &this->myrank);
MPI_Comm_size(this->comm, &this->nprocs); MPI_Comm_size(this->comm, &this->nprocs);
this->stop_code_now = false;
} }
...@@ -130,3 +134,95 @@ int direct_numerical_simulation::write_iteration(void) ...@@ -130,3 +134,95 @@ int direct_numerical_simulation::write_iteration(void)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
int direct_numerical_simulation::main_loop(void)
{
clock_t time0, time1;
double time_difference, local_time_difference;
time0 = clock();
int max_iter = (this->iteration + this->niter_todo -
(this->iteration % this->niter_todo));
for (; this->iteration < max_iter;)
{
#ifdef USE_TIMINGOUTPUT
const std::string loopLabel = ("code::main_start::loop-" +
std::to_string(this->iteration));
TIMEZONE(loopLabel.c_str());
#endif
this->do_stats();
this->step();
if (this->iteration % this->niter_out == 0)
this->write_checkpoint();
this->check_stopping_condition();
if (this->stop_code_now)
break;
time1 = clock();
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration <<
" took " << time_difference/nprocs <<
" seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration <<
" took " << time_difference/nprocs <<
" seconds" << std::endl;
time0 = time1;
}
this->do_stats();
time1 = clock();
local_time_difference = ((
(unsigned int)(time1 - time0)) /
((double)CLOCKS_PER_SEC));
time_difference = 0.0;
MPI_Allreduce(
&local_time_difference,
&time_difference,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
if (this->myrank == 0)
std::cout << "iteration " << iteration <<
" took " << time_difference/nprocs <<
" seconds" << std::endl;
if (this->myrank == 0)
std::cerr << "iteration " << iteration <<
" took " << time_difference/nprocs <<
" seconds" << std::endl;
if (this->iteration % this->niter_out != 0)
this->write_checkpoint();
return EXIT_SUCCESS;
}
int direct_numerical_simulation::check_stopping_condition(void)
{
if (myrank == 0)
{
std::string fname = (
std::string("stop_") +
std::string(this->simname));
{
struct stat file_buffer;
this->stop_code_now = (
stat(fname.c_str(), &file_buffer) == 0);
}
}
MPI_Bcast(
&this->stop_code_now,
1,
MPI_C_BOOL,
0,
MPI_COMM_WORLD);
return EXIT_SUCCESS;
}
...@@ -68,13 +68,17 @@ class direct_numerical_simulation ...@@ -68,13 +68,17 @@ class direct_numerical_simulation
const std::string &simulation_name); const std::string &simulation_name);
virtual ~direct_numerical_simulation(){} virtual ~direct_numerical_simulation(){}
virtual int write_checkpoint(void) = 0;
virtual int initialize(void) = 0; virtual int initialize(void) = 0;
virtual int main_loop(void) = 0; virtual int step(void) = 0;
virtual int do_stats(void) = 0;
virtual int finalize(void) = 0; virtual int finalize(void) = 0;
int main_loop(void);
int read_iteration(void); int read_iteration(void);
int write_iteration(void); int write_iteration(void);
int grow_file_datasets(void); int grow_file_datasets(void);
int check_stopping_condition(void);
}; };
#endif//DIRECT_NUMERICAL_SIMULATION_HPP #endif//DIRECT_NUMERICAL_SIMULATION_HPP
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment