Serializer.h 3.99 KB
Newer Older
1
// ============================================================================
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// ==                                                                        ==
// == 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 "FileWriter.h"
#include "Parameters.h"
#include "AdaptInfo.h"
#include "ProblemStatBase.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
    Serializer(ProblemType *problem) 
      : name_(""), 
	problem_(problem), 
39
	tsModulo_(1), 
40
	timestepNumber_(-1)
Thomas Witkowski's avatar
Thomas Witkowski committed
41
    {
42
43
44
      GET_PARAMETER(0, problem_->getName() + "->output->serialization filename", 
		    &name_);
      GET_PARAMETER(0, problem_->getName() + "->output->write every i-th timestep",
45
		    "%d", &tsModulo_);
46
      TEST_EXIT(name_ != "")("no filename\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
47
    }
48

Thomas Witkowski's avatar
Thomas Witkowski committed
49
    virtual ~Serializer() {}
50
51
52
53
54
55
56
57
58
59

    virtual void writeFiles(AdaptInfo *adaptInfo, 
			    bool force,
			    int level = -1,
			    Flag traverseFlag = Mesh::CALL_LEAF_EL,
			    bool (*writeElem)(ElInfo*) = NULL) 
    {
      FUNCNAME("Serializer::writeFiles()");

      timestepNumber_++;
60
      timestepNumber_ %= tsModulo_;
61
      if ((timestepNumber_ != 0) && !force)
62
63
64
65
	return;

      TEST_EXIT(adaptInfo)("No AdaptInfo\n");

66
      std::ofstream out(name_.c_str());
67
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
68
69
70
71
72
      problem_->serialize(out);
      adaptInfo->serialize(out);
      out.close();

      MSG("problem serialized to %s \n", name_.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
73
    }
74

Thomas Witkowski's avatar
Thomas Witkowski committed
75
    void writeDelayedFiles() {}
Thomas Witkowski's avatar
Thomas Witkowski committed
76
77
78

    bool isWritingDelayed() { 
      return false; 
Thomas Witkowski's avatar
Thomas Witkowski committed
79
    }
80
81

  protected:
82
    /// Name of file to which the problem is serialized.
83
    std::string name_;
84

85
    /// Pointer to the problem.
86
87
    ProblemType *problem_;

88
    /// The problem is serialized every tsModulo-th timestep.
89
    int tsModulo_;
90

91
    /// Current timestep number.
92
93
94
95
96
97
    int timestepNumber_;
  };


  class SerializerUtil {
  public:
98
    static void serializeInt(std::ostream &out, int* ptrInt) {
99
      out.write(reinterpret_cast<const char*>(ptrInt), sizeof(int));
Thomas Witkowski's avatar
Thomas Witkowski committed
100
    }
101

102
    static void serializeDouble(std::ostream &out, double* ptrInt) {
103
      out.write(reinterpret_cast<const char*>(ptrInt), sizeof(double));
Thomas Witkowski's avatar
Thomas Witkowski committed
104
    }
105

106
    static void serializeBool(std::ostream &out, bool* ptrBool) {
107
108
109
      out.write(reinterpret_cast<const char*>(ptrBool), sizeof(bool));
    }

110
    static void deserializeInt(std::istream &in, int* ptrInt) {
111
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(int));
Thomas Witkowski's avatar
Thomas Witkowski committed
112
    }
113

114
    static void deserializeDouble(std::istream &in, double* ptrInt) {
115
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(double));
Thomas Witkowski's avatar
Thomas Witkowski committed
116
    }
117

118
    static void deserializeBool(std::istream &in, bool* ptrBool) {
119
      in.read(reinterpret_cast<char*>(ptrBool), sizeof(bool));
Thomas Witkowski's avatar
Thomas Witkowski committed
120
    }
121
122
123
124

  };
}
#endif