Serializer.h 4.03 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() 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
79
      return false; 
Thomas Witkowski's avatar
Thomas Witkowski committed
80
    }
81
82

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

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

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

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


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

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

109
110
    static void serializeBool(std::ostream &out, bool* ptrBool) 
    {
111
112
113
      out.write(reinterpret_cast<const char*>(ptrBool), sizeof(bool));
    }

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

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

124
125
    static void deserializeBool(std::istream &in, bool* ptrBool) 
    {
126
      in.read(reinterpret_cast<char*>(ptrBool), sizeof(bool));
Thomas Witkowski's avatar
Thomas Witkowski committed
127
    }
128
129
130
131

  };
}
#endif