Commit 3ab449bd authored by Lukas Riedel's avatar Lukas Riedel

Use Boost Test for simulation base unit test

Rewrite the test into three test cases with the DummyModel as fixture.
parent 7d37cefa
#define BOOST_TEST_MODULE simulation base test
#include <boost/test/unit_test.hpp>
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config.h"
#endif
#include <cassert>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/common/float_cmp.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/dorie/common/simulation_base.hh>
/// Dummy model for testing.
/** Makes sure that all functions of SimulationBase are properly overridden
* and called.
*/
class DummyModel : public Dune::Dorie::SimulationBase
/// Fixture for all test cases
struct DummyFixture
{
public:
DummyModel(Dune::MPIHelper& helper)
: Dune::Dorie::SimulationBase("dummy",
"debug",
helper,
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
{
this->_log->info("Current time: {}", _current_time);
_data_was_written = true;
}
private:
double _dt;
double _current_time;
public:
double _end_time;
bool _grid_was_adapted;
mutable bool _data_was_written;
/// Initialize the DUNE MPI Helper from the command line arguments
Dune::MPIHelper& helper = Dune::MPIHelper::instance(
boost::unit_test::framework::master_test_suite().argc,
boost::unit_test::framework::master_test_suite().argv
);
/// Dummy model for testing only. Implements all pure virtual methods.
class DummyModel : public Dune::Dorie::SimulationBase
{
private:
using Base = Dune::Dorie::SimulationBase;
public:
DummyModel (Dune::MPIHelper& helper) :
Base("dummy",
"debug",
helper,
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
{
this->_log->info("Current time: {}", _current_time);
_data_was_written = true;
}
private:
double _dt;
double _current_time;
public:
double _end_time;
bool _grid_was_adapted;
mutable bool _data_was_written;
} model = DummyModel(helper);
};
int main(int argc, char **argv)
/// Create a new suite using the fixture in every test case
BOOST_FIXTURE_TEST_SUITE(suite, DummyFixture);
/// Test if the model is correctly initialized
BOOST_AUTO_TEST_CASE (initialize)
{
BOOST_TEST(model._end_time == 1.0);
BOOST_TEST(model._grid_was_adapted == false);
BOOST_TEST(model._data_was_written == false);
BOOST_TEST(model.begin_time() == 0.0);
BOOST_TEST(model.current_time() == 0.0);
const bool output_policy =
(model.output_policy() == Dune::Dorie::OutputPolicy::None);
BOOST_TEST(output_policy);
const bool adaptivity_policy =
(model.adaptivity_policy() == Dune::Dorie::AdaptivityPolicy::None);
BOOST_TEST(adaptivity_policy);
}
/// Test if policies are correctly set
BOOST_AUTO_TEST_CASE (policies)
{
auto& helper = Dune::MPIHelper::instance(argc, argv);
const auto adapt_policy = Dune::Dorie::AdaptivityPolicy::WaterFlux;
model.set_policy(adapt_policy);
bool policy_correct = (model.adaptivity_policy() == adapt_policy);
BOOST_TEST(policy_correct);
const auto output_policy = Dune::Dorie::OutputPolicy::EndOfRichardsStep;
model.set_policy(output_policy);
policy_correct = (model.output_policy() == output_policy);
BOOST_TEST(policy_correct);
}
try {
DummyModel model(helper);
/// Test if the model correctly performs the run() algorithm
BOOST_AUTO_TEST_CASE (run)
{
// perform a step
model.step();
BOOST_TEST(Dune::FloatCmp::eq(model.current_time(),
model.begin_time() + 0.1));
// run the model without adaptivity
// run multiple steps
model.run();
assert(not model._grid_was_adapted);
assert(not model._data_was_written);
assert(Dune::FloatCmp::ge(model.current_time(), model.end_time()));
BOOST_TEST(Dune::FloatCmp::eq(model.current_time(),
model.end_time()));
BOOST_TEST(not model._grid_was_adapted);
BOOST_TEST(not model._data_was_written);
// now set adaptivity
// set new policies
model.set_policy(Dune::Dorie::AdaptivityPolicy::WaterFlux);
model.set_policy(Dune::Dorie::OutputPolicy::EndOfRichardsStep);
model._end_time += 1.0;
// run multiple steps, check if policies are applied
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;
BOOST_TEST(Dune::FloatCmp::eq(model.current_time(),
model.end_time()));
BOOST_TEST(model._grid_was_adapted);
BOOST_TEST(model._data_was_written);
}
BOOST_AUTO_TEST_SUITE_END()
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