Serializer.h 5.67 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
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
87
88
89
90
	return;

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

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

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

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

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

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

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

114
  namespace SerUtil {
115

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

122
    template<typename T>
123
    void deserialize(std::istream& in, T& data)
124
    {
125
      in.read(reinterpret_cast<char*>(&data), sizeof(T));
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
157

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



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

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

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

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

186
187


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

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

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

215
216


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

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

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

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

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

249
  }
250
251
252

}
#endif