Commit fedee2f2 authored by Lukas Riedel's avatar Lukas Riedel

Merge branch '94-base-class-for-simulations' into 'master'

Resolve "Base class for simulations"

Closes #94

See merge request !87
parents 542d8016 4717dcec
......@@ -6,6 +6,10 @@
### Added
* DORiE now depends on [`yaml-cpp`](https://github.com/jbeder/yaml-cpp/), a
library for reading YAML files. The version required is >=5.2.0.
* Added an abstract base class
[`SimulationBase`](dune/dorie/interface/base_simulation.hh)
that models the concept of simulation steps so that they
can be later coupled with other simulations.
* New classes representing parameterizations. Every parameterization must now
derive from `RichardsParameterization`, and return function objects
representing parameterization functions.
......
#ifndef DUNE_DORIE_SIMULATION_BASE_HH
#define DUNE_DORIE_SIMULATION_BASE_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 SimulationBase.
*
* @param[in] output_policy The output policy.
* Defaults to OutputPolicy::EndOfStep.
* @param[in] adapt_policy The adapt policy.
* Defaults to AdaptiviyPolicy::None.
*/
SimulationBase(
OutputPolicy output_policy=OutputPolicy::EndOfStep,
AdaptivityPolicy adapt_policy=AdaptivityPolicy::None
)
: _output_policy(output_policy)
, _adapt_policy(adapt_policy)
{}
/**
* @brief Sets the output policy.
*
* @param[in] output_policy The output policy,
*/
void set_policy(OutputPolicy output_policy) {_output_policy = output_policy;}
/**
* @brief Sets the adaptivity policy.
*
* @param[in] adapt_policy The adaptivity policy.
*/
void set_policy(AdaptivityPolicy adapt_policy)
{
_adapt_policy = adapt_policy;
}
/**
* @brief Returns the current output policy.
*
* @return The current output policy.
*/
OutputPolicy output_policy() const {return _output_policy;}
/**
* @brief Returns the current adaptivity policy.
*
* @return The current adaptivity policy.
*/
AdaptivityPolicy adaptivity_policy() const {return _adapt_policy;}
/**
* @brief Writes the data.
*/
void virtual write_data() const
{
if (_output_policy != OutputPolicy::None)
DUNE_THROW(Dune::IOError,"Simulation can't write data!");
}
/**
* @brief Mark the grid in order to improve the current model.
*/
virtual void mark_grid()
{
DUNE_THROW(Dune::NotImplemented,
"Simulation can't mark the grid for adaptivity!");
}
/**
* @brief Operations before adaptation of the grid
*/
virtual void pre_adapt_grid() {};
/**
* @brief Adapt the grid together it every dependency of the
* grid (e.g. solution vector and grid function spaces).
*/
virtual void adapt_grid()
{
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!");
}
/**
* @brief Operations after adaptation of the grid
*/
virtual void post_adapt_grid() {};
/**
* @brief Method that provides the begin time of the model.
*
* @return Begin time of the model.
*/
virtual double begin_time() const = 0;
/**
* @brief Method that provides the end time of the model.
*
* @return End time of the model.
*/
virtual double end_time() const = 0;
/**
* @brief Method that provides the current time of the model.
*
* @return Current time of the model.
*/
virtual double current_time() const = 0;
/**
* @brief Suggest a time step to the model.
*
* @param[in] suggestion for the internal time step of the model.
*/
virtual void suggest_timestep(double dt) = 0;
/**
* @brief Performs one steps in direction to end_time(). The time-step
* should never result on a bigger step than the one suggested
* in suggest_timestep().
*/
virtual void step() = 0;
/**
* @brief Runs the model performing steps until current_time() equals
* end_time()
*/
virtual void run()
{
if (output_policy() != OutputPolicy::None)
write_data();
while( Dune::FloatCmp::lt( current_time(), end_time()) )
{
step();
if (adaptivity_policy() != AdaptivityPolicy::None)
if ( Dune::FloatCmp::lt( current_time(), end_time()) )
{
mark_grid();
pre_adapt_grid();
adapt_grid();
post_adapt_grid();
}
}
}
private:
OutputPolicy _output_policy;
AdaptivityPolicy _adapt_policy;
};
} // namespace Dorie
} // namespace Dune
#endif // DUNE_DORIE_SIMULATION_BASE_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
......
dorie_add_unit_test(SOURCES test-simulation-base.cc NAME test-simulation-base)
\ No newline at end of file
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cassert>
#include <dune/dorie/interface/simulation_base.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/common/float_cmp.hh>
/// Dummy model for testing.
/** Makes sure that all functions of SimulationBase are properly overridden
* and called.
*/
class DummyModel : public Dune::Dorie::SimulationBase
{
public:
DummyModel()
: Dune::Dorie::SimulationBase(Dune::Dorie::OutputPolicy::None,
Dune::Dorie::AdaptivityPolicy::None)
, _dt(.1)
, _current_time(begin_time())
, _end_time(1.0)
, _grid_was_adapted(false)
, _data_was_written(false)
{}
double begin_time() const override { return 0.; }
double end_time() const override { return _end_time; }
double current_time() const override { return _current_time; }
void suggest_timestep(double dt) override { _dt = std::min(dt,_dt); }
void step() override { _current_time+=_dt; }
/// Do nothing.
void mark_grid () override {};
/// Adapt the grid in our minds.
void adapt_grid () override
{
_grid_was_adapted = true;
}
void write_data() const override
{
std::cout << "current time: " << _current_time << std::endl;
_data_was_written = true;
}
private:
double _dt;
double _current_time;
public:
double _end_time;
bool _grid_was_adapted;
mutable bool _data_was_written;
};
int main(int argc, char **argv)
{
Dune::MPIHelper::instance(argc, argv);
try {
DummyModel model;
// run the model without adaptivity
model.run();
assert(not model._grid_was_adapted);
assert(not model._data_was_written);
assert(Dune::FloatCmp::ge(model.current_time(), model.end_time()));
// now set adaptivity
model.set_policy(Dune::Dorie::AdaptivityPolicy::WaterFlux);
model.set_policy(Dune::Dorie::OutputPolicy::EndOfStep);
model._end_time += 1.0;
model.run();
assert(model._grid_was_adapted);
assert(model._data_was_written);
assert(Dune::FloatCmp::ge(model.current_time(), model.end_time()));
} catch (Dune::Exception& e) {
std::cout << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "Exception occurred!" << std::endl;
return 1;
}
return 0;
}
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