Serializer.h 6.28 KB
Newer Older
1
// ============================================================================
2
3
4
5
6
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
17
18
19
20
21
22
23
24
// ==                                                                        ==
// ============================================================================

/** \file Serializer.h */

#ifndef AMDIS_SERIALIZER_H
#define AMDIS_SERIALIZER_H

25
#include <map>
26
27
#include "boost/lexical_cast.hpp"

28
#include "io/FileWriter.h"
29
30
31
32
33
34
35
36
37
#include "Parameters.h"
#include "AdaptInfo.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
38
39
40
41
42
    Serializer(ProblemType *prob) 
      : name(""), 
	problem(prob),
	tsModulo(1), 
	timestepNumber(-1)
Thomas Witkowski's avatar
Thomas Witkowski committed
43
    {
44
45
46
47
      FUNCNAME("Serializer::Serializer()");

      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");
50
51

#if HAVE_PARALLEL_DOMAIN_AMDIS
52
      name += ".p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank());
53
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
54
    }
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    Serializer(ProblemType *prob, std::string filename, int writeEveryIth)
      : name(filename),
	problem(prob),
	tsModulo(writeEveryIth),
	timestepNumber(-1)
    {
      FUNCNAME("Serializer::Serializer()");

      TEST_EXIT(name != "")("No filename!\n");

#if HAVE_PARALLEL_DOMAIN_AMDIS
      name += ".p" + boost::lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank());
#endif      
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
73
    virtual ~Serializer() {}
74
75
76
77
78
79
80
81
82

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

83
84
85
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
86
87
88
89
	return;

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

90
      std::ofstream out(name.c_str());
91
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
92
      problem->serialize(out);
93
94
95
      adaptInfo->serialize(out);
      out.close();

96
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
97
    }
98
99

  protected:
100
    /// Name of file to which the problem is serialized.
101
    std::string name;
102

103
    /// Pointer to the problem.
104
    ProblemType *problem;
105

106
    /// The problem is serialized every tsModulo-th timestep.
107
    int tsModulo;
108

109
    /// Current timestep number.
110
    int timestepNumber;
111
112
  };

113
  namespace SerUtil {
114

115
    template<typename T>
116
    void serialize(std::ostream& out, T& data)
117
    {
118
      out.write(reinterpret_cast<const char*>(&data), sizeof(T));
119
    }   
120

121
    template<typename T>
122
    void deserialize(std::istream& in, T& data)
123
    {
124
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
125
    }   
126

127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    void serialize(std::ostream& out, DofEdge& data);

    void deserialize(std::istream& in, DofEdge& data);



    void serialize(std::ostream& out, DofFace& data);

    void deserialize(std::istream& in, DofFace& data);



    template<typename T, typename U>
    void serialize(std::ostream& out, std::pair<T, U>& data)
    {
      serialize(out, data.first);
      serialize(out, data.second);
    }

    template<typename T, typename U>
    void deserialize(std::istream& in, std::pair<T, U>& data)
    {
      deserialize(in, data.first);
      deserialize(in, data.second);
    }



157
    template<typename T>
158
    void serialize(std::ostream& out, std::vector<T>& data)
159
160
    {
      int vecSize = data.size();
161
      serialize(out, vecSize);
162
163
164
      for (typename std::vector<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
165
	serialize(out, v);
166
167
168
169
      }
    }

    template<typename T>
170
    void deserialize(std::istream& in, std::vector<T>& data)
171
    {
172
173
      data.clear();

174
      int vecSize = 0;
175
      deserialize(in, vecSize);
176
177
178
179
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
180
	deserialize(in, v);
181
182
183
184
	data[i] = v;
      }
    }

185
186


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    template<typename T>
    void serialize(std::ostream& out, std::set<T>& data)
    {
      int setSize = data.size();
      serialize(out, setSize);
      for (typename std::set<T>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T v = *it;
	serialize(out, v);
      }
    }

    template<typename T>
    void deserialize(std::istream& in, std::set<T>& data)
    {
202
203
      data.clear();

204
205
206
207
208
209
210
211
212
      int setSize = 0;
      deserialize(in, setSize);

      for (int i = 0; i < setSize; i++) {
	T v;
	deserialize(in, v);
	data.insert(v);
      }
    }
213

214
215


216
    template<typename T1, typename T2>
217
    void serialize(std::ostream& out, std::map<T1, T2>& data)
218
    {
219
      int mapSize = data.size();
220
      serialize(out, mapSize);
221
222
223
224
225

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
226
227
	serialize(out, v1);
	serialize(out, v2);
228
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
229
    }
230
231

    template<typename T1, typename T2>
232
    void deserialize(std::istream& in, std::map<T1, T2>& data)
233
    {
234
235
      data.clear();

236
      int mapSize = 0;
237
      deserialize(in, mapSize);
238
239
240
241

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
242
243
	deserialize(in, v1);
	deserialize(in, v2);
244
245
246
247
	data[v1] = v2;
      }
    }

248
  }
249
250
251

}
#endif