// ============================================================================ // == == // == AMDiS - Adaptive multidimensional simulations == // == == // ============================================================================ // == == // == crystal growth group == // == == // == Stiftung caesar == // == Ludwig-Erhard-Allee 2 == // == 53175 Bonn == // == germany == // == == // ============================================================================ // == == // == http://www.caesar.de/cg/AMDiS == // == == // ============================================================================ /** \file Serializer.h */ #ifndef AMDIS_SERIALIZER_H #define AMDIS_SERIALIZER_H #include #include "FileWriter.h" #include "Parameters.h" #include "AdaptInfo.h" #include "ProblemStatBase.h" namespace AMDiS { template class Serializer : public FileWriterInterface { public: Serializer(ProblemType *prob) : name(""), problem(prob), tsModulo(1), timestepNumber(-1) { GET_PARAMETER(0, problem->getName() + "->output->serialization filename", &name); GET_PARAMETER(0, problem->getName() + "->output->write every i-th timestep", "%d", &tsModulo); TEST_EXIT(name != "")("no filename\n"); } virtual ~Serializer() {} virtual void writeFiles(AdaptInfo *adaptInfo, bool force, int level = -1, Flag traverseFlag = Mesh::CALL_LEAF_EL, bool (*writeElem)(ElInfo*) = NULL) { FUNCNAME("Serializer::writeFiles()"); timestepNumber++; timestepNumber %= tsModulo; if ((timestepNumber != 0) && !force) return; TEST_EXIT(adaptInfo)("No AdaptInfo\n"); std::ofstream out(name.c_str()); TEST_EXIT(out.is_open())("Cannot open serialization file!\n"); problem->serialize(out); adaptInfo->serialize(out); out.close(); MSG("problem serialized to %s \n", name.c_str()); } protected: /// Name of file to which the problem is serialized. std::string name; /// Pointer to the problem. ProblemType *problem; /// The problem is serialized every tsModulo-th timestep. int tsModulo; /// Current timestep number. int timestepNumber; }; namespace SerUtil { template void serialize(std::ostream &out, T *data) { out.write(reinterpret_cast(data), sizeof(T)); } template void deserialize(std::istream &in, T *data) { in.read(reinterpret_cast(data), sizeof(T)); } template void serialize(std::ostream &out, std::map &data) { int mapSize = data.size(); serialize(out, &mapSize); for (typename std::map::iterator it = data.begin(); it != data.end(); ++it) { T1 v1 = it->first; T2 v2 = it->second; serialize(out, &v1); serialize(out, &v2); } } } } #endif