Serializer.h 4 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
39
40
41

// ==                                                                        ==
// == 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 "MemoryManager.h"
#include "Parameters.h"
#include "AdaptInfo.h"
#include "ProblemStatBase.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
    MEMORY_MANAGED(Serializer);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
52
    virtual ~Serializer() {}
53
54
55
56
57
58
59
60
61
62

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

      timestepNumber_++;
63
      timestepNumber_ %= tsModulo_;
64
65
66
67
68
69
      if ((timestepNumber_ != 0) && !force) {
	return;
      }

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

70
      std::ofstream out(name_.c_str());
71
72
73
74
75
      problem_->serialize(out);
      adaptInfo->serialize(out);
      out.close();

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

Thomas Witkowski's avatar
Thomas Witkowski committed
78
    void writeDelayedFiles() {}
Thomas Witkowski's avatar
Thomas Witkowski committed
79
80
81

    bool isWritingDelayed() { 
      return false; 
Thomas Witkowski's avatar
Thomas Witkowski committed
82
    }
83
84
85
86
87

  protected:
    /** \brief
     * Name of file to which the problem is serialized.
     */
88
    std::string name_;
89
90
91
92
93
94
95
96
97

    /** \brief
     * Pointer to the problem.
     */
    ProblemType *problem_;

    /** \brief
     * The problem is serialized every tsModulo-th timestep.
     */
98
    int tsModulo_;
99
100
101
102
103
104
105
106
107
108

    /** \brief
     * Current timestep number.
     */
    int timestepNumber_;
  };


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

113
    static void serializeDouble(std::ostream &out, double* ptrInt) {
114
      out.write(reinterpret_cast<const char*>(ptrInt), sizeof(double));
Thomas Witkowski's avatar
Thomas Witkowski committed
115
    }
116

117
    static void serializeBool(std::ostream &out, bool* ptrBool) {
118
119
120
      out.write(reinterpret_cast<const char*>(ptrBool), sizeof(bool));
    }

121
    static void deserializeInt(std::istream &in, int* ptrInt) {
122
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(int));
Thomas Witkowski's avatar
Thomas Witkowski committed
123
    }
124

125
    static void deserializeDouble(std::istream &in, double* ptrInt) {
126
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(double));
Thomas Witkowski's avatar
Thomas Witkowski committed
127
    }
128

129
    static void deserializeBool(std::istream &in, bool* ptrBool) {
130
      in.read(reinterpret_cast<char*>(ptrBool), sizeof(bool));
Thomas Witkowski's avatar
Thomas Witkowski committed
131
    }
132
133
134
135

  };
}
#endif