Serializer.h 4.01 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
44
45
46
47
	timestepNumber_(-1)
	  {
      GET_PARAMETER(0, problem_->getName() + "->output->serialization filename", 
		    &name_);
      GET_PARAMETER(0, problem_->getName() + "->output->write every i-th timestep",
48
		    "%d", &tsModulo_);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      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_++;
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
76
77
      problem_->serialize(out);
      adaptInfo->serialize(out);
      out.close();

      MSG("problem serialized to %s \n", name_.c_str());
    };

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

    bool isWritingDelayed() { 
      return false; 
    };
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
111
112
      out.write(reinterpret_cast<const char*>(ptrInt), sizeof(int));
    };

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

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


122
    static void deserializeInt(std::istream &in, int* ptrInt) {
123
124
125
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(int));
    };

126
    static void deserializeDouble(std::istream &in, double* ptrInt) {
127
128
129
      in.read(reinterpret_cast<char*>(ptrInt), sizeof(double));
    };

130
    static void deserializeBool(std::istream &in, bool* ptrBool) {
131
132
133
134
135
136
      in.read(reinterpret_cast<char*>(ptrBool), sizeof(bool));
    };

  };
}
#endif