Notify callbacks when a timestep has completed

Client code can do additional processing for the output of each
timestep.
This commit is contained in:
Roland Kaufmann 2013-05-14 11:36:13 +02:00
parent f240e79880
commit 37906058f5
2 changed files with 47 additions and 0 deletions

View File

@ -49,6 +49,8 @@
#include <boost/filesystem.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/function.hpp>
#include <boost/signal.hpp>
#include <numeric>
#include <fstream>
@ -99,6 +101,9 @@ namespace Opm
boost::scoped_ptr<TransportSolverTwophaseInterface> tsolver_;
// Misc. data
std::vector<int> allcells_;
// list of hooks that are notified when a timestep completes
boost::signal0 <void> timestep_completed;
};
@ -128,6 +133,11 @@ namespace Opm
return pimpl_->run(timer, state, well_state);
}
// connect the hook to the signal in the implementation class
void SimulatorIncompTwophase::connect_timestep_impl (boost::function0 <void> hook) {
pimpl_->timestep_completed.connect (hook);
}
static void reportVolumes(std::ostream &os, double satvol[2], double tot_porevol_init,
double tot_injected[2], double tot_produced[2],
double injected[2], double produced[2],
@ -585,6 +595,9 @@ namespace Opm
if (output_) {
sreport.reportParam(tstep_os);
}
// notify all clients that we are done with the timestep
timestep_completed ();
}
if (output_) {

View File

@ -21,6 +21,8 @@
#define OPM_SIMULATORINCOMPTWOPHASE_HEADER_INCLUDED
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <functional> // bind
#include <vector>
struct UnstructuredGrid;
@ -88,10 +90,42 @@ namespace Opm
TwophaseState& state,
WellState& well_state);
/// Register a callback for notifications about completed timestep.
/// The specified method of the object is called when the simulator
/// has completed a timestep and the state objects are (possibly)
/// changed.
/// If you want to know the current timestep, the callback must
/// also monitor the timer object which was passed to run().
/// \tparam T Type of the callback object.
/// \tparam callback Address of a member function of T which will
/// be invoked when a timestep completes.
/// \param[in] t Object which will receive notifications. The
/// lifetime of the object must span over the
/// duration of the simulation, and it is your
/// responsibility to ensure that.
/// \example
/// \code{.cpp}
/// struct Foo {
/// void bar () { cout << "Called!" << endl; }
/// };
///
/// SimulatorIncompTwophase sim (...);
/// Foo f;
/// sim.connect_timestep <Foo, &Foo::bar> (f);
/// sim.run (...);
/// \endcode
template <typename T, void (T::*callback)()>
void connect_timestep (T& t) {
connect_timestep_impl (boost::function0<void> (std::bind (callback, t)));
}
private:
class Impl;
// Using shared_ptr instead of scoped_ptr since scoped_ptr requires complete type for Impl.
boost::shared_ptr<Impl> pimpl_;
// implementation which is not templated, and can be in library
void connect_timestep_impl (boost::function0<void> hook);
};
} // namespace Opm