Commit 2e17b93c authored by Santiago Ospina's avatar Santiago Ospina

Adds a simulation base class and a unit test for it

Signed-off-by: default avatarSantiago Ospina <saospina@hugo.iwr.uni-heidelberg.de>
parent 2c0f1bc1
#ifndef DUNE_DORIE_BASE_SIMULATION_HH
#define DUNE_DORIE_BASE_SIMULATION_HH
#include "util.hh"
#include <dune/common/exceptions.hh>
#include <dune/common/float_cmp.hh>
namespace Dune{
namespace Dorie{
/**
* @brief Base class for simulations.
*/
class SimulationBase
{
public:
/**
* @brief Constructs the object.
*
* @param[in] output_policy The output policy
* @param[in] adapt_policy The adapt policy
*/
SimulationBase(OutputPolicy output_policy, AdaptivityPolicy adapt_policy)
: _output_policy(output_policy)
, _adapt_policy(adapt_policy)
{}
/**
* @brief Constructs the object.
*/
SimulationBase()
: SimulationBase(OutputPolicy::None,AdaptivityPolicy::None)
{}
/**
* @brief Writes the data.
*/
void virtual writeData();
/**
* @brief Sets the output policy.
*
* @param[in] output_policy The output policy,
*/
void setPolicy(OutputPolicy output_policy) {_output_policy = output_policy;}
/**
* @brief Sets the adaptivity policy.
*
* @param[in] adapt_policy The adaptivity policy.
*/
void setPolicy(AdaptivityPolicy adapt_policy) {_adapt_policy = adapt_policy;}
/**
* @brief Returns the current output policy.
*
* @return The current output policy.
*/
OutputPolicy outputPolicy() {return _output_policy;}
/**
* @brief Returns the current adaptivity policy.
*
* @return The current adaptivity policy.
*/
AdaptivityPolicy adaptivityPolicy() {return _adapt_policy;}
/**
* @brief Mark the grid in order to improve the current model.
*/
virtual void markGrid();
/**
* @brief Adapt the grid together it every dependency of the
* grid (e.g. solution vector and grid function spaces).
*/
virtual void adapt();
/**
* @brief Method that provides the begin time of the model.
*
* @return Begin time of the model.
*/
virtual double beginTime() = 0;
/**
* @brief Method that provides the end time of the model.
*
* @return End time of the model.
*/
virtual double endTime() = 0;
/**
* @brief Method that provides the current time of the model.
*
* @return Current time of the model.
*/
virtual double currentTime() = 0;
/**
* @brief Suggest a time step to the model.
*
* @param[in] suggestion for the internal time step of the model.
*/
virtual void suggestTimestep(double dt) {};
/**
* @brief Performs one steps in direction to endTime()
*/
virtual void step() = 0;
/**
* @brief Runs the model performing steps until currentTime() equals endTime()
*/
virtual void run();
private:
OutputPolicy _output_policy;
AdaptivityPolicy _adapt_policy;
};
void SimulationBase::writeData()
{
if (_output_policy != OutputPolicy::None)
DUNE_THROW(Dune::IOError,"Simulation can't write data!");
}
void SimulationBase::adapt()
{
if (_adapt_policy != AdaptivityPolicy::None)
DUNE_THROW(Dune::NotImplemented,"Simulation can't be adapted!");
else
DUNE_THROW(Dune::InvalidStateException,
"Simulation uses adaptivity policy None. Method adapt() must not be called!");
}
void SimulationBase::markGrid()
{
DUNE_THROW(Dune::NotImplemented,"Simulation can't mark the grid for adaptivity!");
}
void SimulationBase::run()
{
if (outputPolicy() != OutputPolicy::None)
writeData();
while( Dune::FloatCmp::lt(currentTime(),endTime()) )
{
step();
if (adaptivityPolicy() != AdaptivityPolicy::None)
if ( Dune::FloatCmp::lt(currentTime(),endTime()) )
{
markGrid();
adapt();
}
}
}
} // namespace Dorie
} // namespace Dune
#endif // DUNE_DORIE_BASE_SIMULATION_HH
\ No newline at end of file
......@@ -19,6 +19,18 @@
namespace Dune{
namespace Dorie{
/**
* @brief Enum class for output policy. It defines when a simulation class
* should produce an output
*/
enum class OutputPolicy {None,EndOfStep};
/**
* @brief Enum class for output policy. It defines which variable
* is the target in order to mark the grid
*/
enum class AdaptivityPolicy {None,WaterFlux};
/// Return the estimation of entries per matrix row for the spatial GridOperator
/** This supposedly decreases matrix assembly time.
* The values specify the *blocks* per row. DG assembles one block for the
......
dune_add_test(SOURCES simulation_base.cc NAME test_simulation_base)
add_custom_target(build_test_simulation_base
DEPENDS test_simulation_base
)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <dune/dorie/interface/base_simulation.hh>
#include <dune/common/parallel/mpihelper.hh>
class DummyModel : public Dune::Dorie::SimulationBase
{
public:
DummyModel()
: Dune::Dorie::SimulationBase()
, _dt(.1)
, _current_time(beginTime())
{}
double beginTime() override {return 0.;}
double endTime() override {return 1.;}
double currentTime() override {return _current_time;}
void step() override {_current_time+=_dt;}
private:
double _dt;
double _current_time;
};
int main(int argc, char **argv)
{
bool passed = true;
auto& helper = Dune::MPIHelper::instance(argc, argv);
try {
DummyModel model;
model.run();
} catch (Dune::Exception& e) {
std::cout << e.what() << std::endl;
passed &= false;
} catch (...) {
std::cerr << "Exception occurred!" << std::endl;
passed &= false;
}
return !passed;
}
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