From ee7732b3189d05b3ab8628d27cd24768536be828 Mon Sep 17 00:00:00 2001 From: Simon Praetorius <simon.praetorius@tu-dresden.de> Date: Thu, 28 Mar 2019 15:33:46 +0100 Subject: [PATCH] add functions backup() and restore() to ProblemStat --- src/amdis/Initfile.hpp | 6 +++ src/amdis/ProblemStat.hpp | 6 +++ src/amdis/ProblemStat.inc.hpp | 52 +++++++++++++++++++++++++ src/amdis/ProblemStatBase.hpp | 6 +++ test/BackupRestoreTest.cpp | 71 +++++++++++++++++++++++++++++++++++ test/CMakeLists.txt | 4 ++ 6 files changed, 145 insertions(+) create mode 100644 test/BackupRestoreTest.cpp diff --git a/src/amdis/Initfile.hpp b/src/amdis/Initfile.hpp index df76a58b..5cbbaea3 100644 --- a/src/amdis/Initfile.hpp +++ b/src/amdis/Initfile.hpp @@ -47,6 +47,12 @@ namespace AMDiS value = pt().get(key, value); } + template <class T> + static void set(std::string const& key, T const& value) + { + pt()[key] = value; + } + /// Returns specified info level static int getMsgInfo() { diff --git a/src/amdis/ProblemStat.hpp b/src/amdis/ProblemStat.hpp index b4578993..dcea28b9 100644 --- a/src/amdis/ProblemStat.hpp +++ b/src/amdis/ProblemStat.hpp @@ -274,6 +274,12 @@ namespace AMDiS /// Writes output files. void writeFiles(AdaptInfo& adaptInfo, bool force = false); + /// Implements \ref ProblemStatBase::backup + void backup(AdaptInfo& adaptInfo) override; + + /// Implements \ref ProblemStatBase::restore + void restore(Flag initFlag) override; + public: // get-methods diff --git a/src/amdis/ProblemStat.inc.hpp b/src/amdis/ProblemStat.inc.hpp index 26f18a5a..2cfa566e 100644 --- a/src/amdis/ProblemStat.inc.hpp +++ b/src/amdis/ProblemStat.inc.hpp @@ -484,4 +484,56 @@ writeFiles(AdaptInfo& adaptInfo, bool force) } +template <class Traits> +void ProblemStat<Traits>:: +backup(AdaptInfo& adaptInfo) +{ + auto param = Parameters::get<std::string>(name_ + "->backup filename"); + std::string filename = param ? *param : name_ + "_" + std::to_string(adaptInfo.timestepNumber()) + ".backup"; + + Dune::BackupRestoreFacility<Grid>::backup(*grid_, filename); + // TODO: backup data + + msg("Problem backuped to file '{}'.", filename); +} + + +template <class Traits> +void ProblemStat<Traits>:: +restore(Flag initFlag) +{ + std::string filename = Parameters::get<std::string>(name_ + "->restore filename").value(); + test_exit(filesystem::exists(filename), "Restore file '{}' not found.", filename); + + // restore grid from file + std::shared_ptr<Grid> grid(Dune::BackupRestoreFacility<Grid>::restore(filename)); + adoptGrid(grid); + + // create fespace + if (initFlag.isSet(INIT_FE_SPACE) || initFlag.isSet(INIT_SYSTEM)) + createGlobalBasis(); + + // create system + if (initFlag.isSet(INIT_SYSTEM)) + createMatricesAndVectors(); + + // create solver + if (linearSolver_) + warning("solver already created\n"); + else if (initFlag.isSet(INIT_SOLVER)) + createSolver(); + + // create marker + if (initFlag.isSet(INIT_MARKER)) + createMarker(); + + // create file writer + if (initFlag.isSet(INIT_FILEWRITER)) + createFileWriter(); + + solution_->compress(); + + // TODO: read data +} + } // end namespace AMDiS diff --git a/src/amdis/ProblemStatBase.hpp b/src/amdis/ProblemStatBase.hpp index 81f4a7cf..df333cff 100644 --- a/src/amdis/ProblemStatBase.hpp +++ b/src/amdis/ProblemStatBase.hpp @@ -111,6 +111,12 @@ namespace AMDiS */ virtual void estimate(AdaptInfo& adaptInfo) = 0; + /// \brief backup the grid and the solution to a file + virtual void backup(AdaptInfo& adaptInfo) = 0; + + /// \brief restore the grid and the solution from a file + virtual void restore(Flag initFlag) = 0; + /// Returns the name of the problem. virtual std::string const& name() const = 0; }; diff --git a/test/BackupRestoreTest.cpp b/test/BackupRestoreTest.cpp new file mode 100644 index 00000000..7471e87b --- /dev/null +++ b/test/BackupRestoreTest.cpp @@ -0,0 +1,71 @@ +#include <amdis/AMDiS.hpp> +#include <amdis/ProblemStat.hpp> + +#include "Tests.hpp" + +using namespace AMDiS; + +template <class Grid, class Factory> +void test(Factory factory) +{ + using Param = TaylorHoodBasis<Grid>; + using Problem = ProblemStat<Param>; + + std::size_t num_elements = 0; + std::size_t num_vertices = 0; + + // backup + { + std::unique_ptr<Grid> grid(factory()); + Problem prob("test", *grid); + prob.initialize(INIT_ALL); + prob.globalRefine(2); + + num_elements = prob.grid()->size(0); + num_vertices = prob.grid()->size(Grid::dimension); + + AdaptInfo adaptInfo("adapt"); + prob.backup(adaptInfo); + } + + // restore + { + Problem prob("test"); + prob.restore(INIT_ALL); + + AMDIS_TEST_EQ(num_elements, prob.grid()->size(0)); + AMDIS_TEST_EQ(num_vertices, prob.grid()->size(Grid::dimension)); + } +} + +template <class Grid> +void test_cube() +{ + using Factory = Dune::StructuredGridFactory<Grid>; + test<Grid>([]() { return Factory::createCubeGrid({0.0,0.0}, {1.0,1.0}, std::array<unsigned int,2>{2,2}); }); +} + +template <class Grid> +void test_simplex() +{ + using Factory = Dune::StructuredGridFactory<Grid>; + test<Grid>([]() { return Factory::createSimplexGrid({0.0,0.0}, {1.0,1.0}, std::array<unsigned int,2>{2,2}); }); +} + +int main(int argc, char** argv) +{ + AMDiS::init(argc, argv); + + std::string filename = "test.backup"; + Parameters::set("test->backup filename", filename); + Parameters::set("test->restore filename", filename); + + test_cube<Dune::YaspGrid<2>>(); + // test_cube<Dune::UGGrid<2>>(); + + // test_simplex<Dune::AlbertaGrid<2,2>>(); + // test_simplex<Dune::UGGrid<2>>(); + + AMDiS::finalize(); + return 0; +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 51040f8f..f5ef52b2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,10 @@ dune_add_test(SOURCES AdaptInfoTest.cpp LINK_LIBRARIES amdis) +dune_add_test(SOURCES BackupRestoreTest.cpp + LINK_LIBRARIES amdis) +add_dune_alberta_flags(GRIDDIM 2 WORLDDIM 2 BackupRestoreTest) + dune_add_test(SOURCES ConceptsTest.cpp LINK_LIBRARIES amdis) -- GitLab