Serializer.h 3.71 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
// ==                                                                        ==
// == 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

25
#include <map>
26
27
28
29
30
31
32
33
34
35
36
#include "FileWriter.h"
#include "Parameters.h"
#include "AdaptInfo.h"
#include "ProblemStatBase.h"

namespace AMDiS {

  template<typename ProblemType>
  class Serializer : public FileWriterInterface
  {
  public:
37
38
39
40
41
    Serializer(ProblemType *prob) 
      : name(""), 
	problem(prob),
	tsModulo(1), 
	timestepNumber(-1)
Thomas Witkowski's avatar
Thomas Witkowski committed
42
    {
43
44
45
46
47
      GET_PARAMETER(0, problem->getName() + "->output->serialization filename", 
		    &name);
      GET_PARAMETER(0, problem->getName() + "->output->write every i-th timestep",
		    "%d", &tsModulo);
      TEST_EXIT(name != "")("no filename\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
48
    }
49

Thomas Witkowski's avatar
Thomas Witkowski committed
50
    virtual ~Serializer() {}
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()");

60
61
62
      timestepNumber++;
      timestepNumber %= tsModulo;
      if ((timestepNumber != 0) && !force)
63
64
65
66
	return;

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

67
      std::ofstream out(name.c_str());
68
      TEST_EXIT(out.is_open())("Cannot open serialization file!\n");
69
      problem->serialize(out);
70
71
72
      adaptInfo->serialize(out);
      out.close();

73
      MSG("problem serialized to %s \n", name.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
74
    }
75
76

  protected:
77
    /// Name of file to which the problem is serialized.
78
    std::string name;
79

80
    /// Pointer to the problem.
81
    ProblemType *problem;
82

83
    /// The problem is serialized every tsModulo-th timestep.
84
    int tsModulo;
85

86
    /// Current timestep number.
87
    int timestepNumber;
88
89
  };

90
  namespace SerUtil {
91

92
93
    template<typename T>
    void serialize(std::ostream &out, T *data)
94
    {
95
96
      out.write(reinterpret_cast<const char*>(data), sizeof(T));
    }   
97

98
99
    template<typename T>
    void deserialize(std::istream &in, T *data)
100
    {
101
102
      in.read(reinterpret_cast<char*>(data), sizeof(T));
    }   
103

104
105
    template<typename T1, typename T2>
    void serialize(std::ostream &out, std::map<T1, T2> &data)
106
    {
107
108
109
110
111
112
113
114
115
116
      int mapSize = data.size();
      serialize(out, &mapSize);

      for (typename std::map<T1,T2>::iterator it = data.begin(); 
	   it != data.end(); ++it) {
	T1 v1 = it->first;
	T2 v2 = it->second;
	serialize(out, &v1);
	serialize(out, &v2);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
117
    }
118
  }
119
120
121

}
#endif