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
......@@ -62,75 +62,19 @@ int NSVE<rnumber>::initialize(void)
}
template <typename rnumber>
int NSVE<rnumber>::main_loop(void)
int NSVE<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->step(dt);
this->iteration = this->fs->iteration;
if (this->fs->iteration % this->niter_out == 0)
{
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();
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->fs->io_checkpoint(false);
this->checkpoint = this->fs->checkpoint;
this->write_iteration();
}
this->fs->step(this->dt);
this->iteration = this->fs->iteration;
return EXIT_SUCCESS;
}
template <typename rnumber>
int NSVE<rnumber>::write_checkpoint(void)
{
this->fs->io_checkpoint(false);
this->checkpoint = this->fs->checkpoint;
this->write_iteration();
return EXIT_SUCCESS;
}
......@@ -147,6 +91,8 @@ int NSVE<rnumber>::finalize(void)
template <typename rnumber>
int NSVE<rnumber>::do_stats()
{
if (!(this->iteration % this->niter_stat == 0))
return EXIT_SUCCESS;
hid_t stat_group;
if (this->myrank == 0)
stat_group = H5Gopen(
......@@ -174,24 +120,6 @@ int NSVE<rnumber>::do_stats()
if (this->myrank == 0)
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;
}
......
......@@ -66,10 +66,11 @@ class NSVE: public direct_numerical_simulation
~NSVE(){}
int initialize(void);
int main_loop(void);
int step(void);
int finalize(void);
int read_parameters(void);
int write_checkpoint(void);
int do_stats(void);
};
......
......@@ -80,96 +80,30 @@ int NSVEp<rnumber>::initialize(void)
}
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->cvelocity->ift();
this->ps->completeLoop(dt);
this->fs->compute_velocity(fs->cvorticity);
this->fs->cvelocity->ift();
this->ps->completeLoop(this->dt);
this->fs->step(this->dt);
this->iteration = this->fs->iteration;
return EXIT_SUCCESS;
}
this->fs->step(dt);
this->iteration = this->fs->iteration;
if (this->fs->iteration % this->niter_out == 0)
{
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();
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->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();
}
template <typename rnumber>
int NSVEp<rnumber>::write_checkpoint(void)
{
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();
return EXIT_SUCCESS;
}
......@@ -188,51 +122,41 @@ int NSVEp<rnumber>::finalize(void)
template <typename rnumber>
int NSVEp<rnumber>::do_stats()
{
hid_t stat_group;
if (this->myrank == 0)
stat_group = H5Gopen(
this->stat_file,
"statistics",
H5P_DEFAULT);
else
stat_group = 0;
fs->compute_velocity(fs->cvorticity);
*tmp_vec_field = fs->cvelocity->get_cdata();
tmp_vec_field->compute_stats(
fs->kk,
stat_group,
"velocity",
fs->iteration / niter_stat,
max_velocity_estimate/sqrt(3));
*tmp_vec_field = fs->cvorticity->get_cdata();
tmp_vec_field->compute_stats(
fs->kk,
stat_group,
"vorticity",
fs->iteration / niter_stat,
max_vorticity_estimate/sqrt(3));
if (this->myrank == 0)
H5Gclose(stat_group);
if (myrank == 0)
// fluid stats go here
if (this->iteration % this->niter_stat == 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);
}
hid_t stat_group;
if (this->myrank == 0)
stat_group = H5Gopen(
this->stat_file,
"statistics",
H5P_DEFAULT);
else
stat_group = 0;
fs->compute_velocity(fs->cvorticity);
*tmp_vec_field = fs->cvelocity->get_cdata();
tmp_vec_field->compute_stats(
fs->kk,
stat_group,
"velocity",
fs->iteration / niter_stat,
max_velocity_estimate/sqrt(3));
*tmp_vec_field = fs->cvorticity->get_cdata();
tmp_vec_field->compute_stats(
fs->kk,
stat_group,
"vorticity",
fs->iteration / niter_stat,
max_vorticity_estimate/sqrt(3));
if (this->myrank == 0)
H5Gclose(stat_group);
}
MPI_Bcast(
&this->stop_code_now,
1,
MPI_C_BOOL,
0,
MPI_COMM_WORLD);
// particle sampling should go here
//if (this->iteration % this->niter_part == 0)
//{
//}
return EXIT_SUCCESS;
}
......
......@@ -76,10 +76,11 @@ class NSVEp: public direct_numerical_simulation
~NSVEp(){}
int initialize(void);
int main_loop(void);
int step(void);
int finalize(void);
int read_parameters(void);
int write_checkpoint(void);
int do_stats(void);
};
......
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include "direct_numerical_simulation.hpp"
int grow_single_dataset(hid_t dset, int tincrement)
......@@ -38,6 +41,7 @@ direct_numerical_simulation::direct_numerical_simulation(
{
MPI_Comm_rank(this->comm, &this->myrank);
MPI_Comm_size(this->comm, &this->nprocs);
this->stop_code_now = false;
}
......@@ -130,3 +134,95 @@ int direct_numerical_simulation::write_iteration(void)
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
const std::string &simulation_name);
virtual ~direct_numerical_simulation(){}
virtual int write_checkpoint(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;
int main_loop(void);
int read_iteration(void);
int write_iteration(void);
int grow_file_datasets(void);
int check_stopping_condition(void);
};
#endif//DIRECT_NUMERICAL_SIMULATION_HPP
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment