Serializer.h 5.77 KB
Newer Older
1
// ============================================================================
2
3
4
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6
7
// ==                                                                        ==
// ============================================================================
8
9
10
11
12
13
14
15
16
17
18
19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20
21
22
23
24
25

/** \file Serializer.h */

#ifndef AMDIS_SERIALIZER_H
#define AMDIS_SERIALIZER_H

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

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

namespace AMDiS {

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

      GET_PARAMETER(0, problem->getName() + "->output->serialization filename", &name);
      GET_PARAMETER(0, problem->getName() + "->output->write every i-th timestep", 
49
		    "%d", &tsModulo);
50
      TEST_EXIT(name != "")("No filename!\n");
51
52

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

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

    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
74
    virtual ~Serializer() {}
75
76
77
78
79
80
81
82
83

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

84
85
86
      TEST_EXIT(tsModulo > 0)
	("Parameter 'write every ith timestep' must be larger than zero!\n");

87
88
89
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
90
91
92
93
	return;

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

94
      std::ofstream out(name.c_str());
95
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
96
      problem->serialize(out);
97
98
99
      adaptInfo->serialize(out);
      out.close();

100
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
101
    }
102
103

  protected:
104
    /// Name of file to which the problem is serialized.
105
    std::string name;
106

107
    /// Pointer to the problem.
108
    ProblemType *problem;
109

110
    /// The problem is serialized every tsModulo-th timestep.
111
    int tsModulo;
112

113
    /// Current timestep number.
114
    int timestepNumber;
115
116
  };

117
  namespace SerUtil {
118

119
    template<typename T>
120
    void serialize(std::ostream& out, T& data)
121
    {
122
      out.write(reinterpret_cast<const char*>(&data), sizeof(T));
123
    }   
124

125
    template<typename T>
126
    void deserialize(std::istream& in, T& data)
127
    {
128
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
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
157
158
159
160

    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);
    }



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

    template<typename T>
174
    void deserialize(std::istream& in, std::vector<T>& data)
175
    {
176
177
      data.clear();

178
      int vecSize = 0;
179
      deserialize(in, vecSize);
180
181
182
183
      data.resize(vecSize);

      for (int i = 0; i < vecSize; i++) {
	T v;
184
	deserialize(in, v);
185
186
187
188
	data[i] = v;
      }
    }

189
190


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    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)
    {
206
207
      data.clear();

208
209
210
211
212
213
214
215
216
      int setSize = 0;
      deserialize(in, setSize);

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

218
219


220
    template<typename T1, typename T2>
221
    void serialize(std::ostream& out, std::map<T1, T2>& data)
222
    {
223
      int mapSize = data.size();
224
      serialize(out, mapSize);
225
226
227
228
229

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

    template<typename T1, typename T2>
236
    void deserialize(std::istream& in, std::map<T1, T2>& data)
237
    {
238
239
      data.clear();

240
      int mapSize = 0;
241
      deserialize(in, mapSize);
242
243
244
245

      for (int i = 0; i < mapSize; i++) {
	T1 v1;
	T2 v2;
246
247
	deserialize(in, v1);
	deserialize(in, v2);
248
249
250
251
	data[v1] = v2;
      }
    }

252
  }
253
254
255

}
#endif