ug4
serialization.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__LIB_GRID__SERIALIZATION__
34 #define __H__LIB_GRID__SERIALIZATION__
35 
36 #include <iostream>
39 #include "common/serialization.h"
43 #include "lib_grid/multi_grid.h"
47 // #include "lib_grid/refinement/projectors/refinement_projector.h"
48 // #include "lib_grid/refinement/projectors/projection_handler.h"
49 
50 namespace ug
51 {
52 
61 // Utilities
62 
84 template <class TGeomObj>
86 {
87  public:
89 
91 
93  virtual void write_info(BinaryBuffer& out) const {};
95  virtual void write_data(BinaryBuffer& out, TGeomObj* o) const = 0;
96 
98  virtual void read_info(BinaryBuffer& in) {};
100  virtual void read_data(BinaryBuffer& in, TGeomObj* o) = 0;
101 
103  virtual void deserialization_starts() {}
104 
106  virtual void deserialization_done() {}
107 };
108 
113 
118 
138 {
139  public:
140  virtual ~GridDataSerializer() {}
141 
143 
145  virtual void write_info(BinaryBuffer& out) const {}
146 
148  virtual void read_info(BinaryBuffer& in) {}
149 
150  virtual void write_data(BinaryBuffer& out, Vertex* o) const {}
151  virtual void write_data(BinaryBuffer& out, Edge* o) const {}
152  virtual void write_data(BinaryBuffer& out, Face* o) const {}
153  virtual void write_data(BinaryBuffer& out, Volume* o) const {}
154 
155  virtual void read_data(BinaryBuffer& in, Vertex* o) {}
156  virtual void read_data(BinaryBuffer& in, Edge* o) {}
157  virtual void read_data(BinaryBuffer& in, Face* o) {}
158  virtual void read_data(BinaryBuffer& in, Volume* o) {}
159 
161  virtual void deserialization_starts() {}
162 
164  virtual void deserialization_done() {}
165 };
166 
168 
169 
171 
186 {
187  public:
189 
191 
192  void add(SPVertexDataSerializer cb);
193  void add(SPEdgeDataSerializer cb);
194  void add(SPFaceDataSerializer cb);
195  void add(SPVolumeDataSerializer cb);
196  void add(SPGridDataSerializer cb);
201  void write_infos(BinaryBuffer& out) const;
202 
205  inline void serialize(BinaryBuffer& out, Vertex* vrt) const;
206  inline void serialize(BinaryBuffer& out, Edge* edge) const;
207  inline void serialize(BinaryBuffer& out, Face* face) const;
208  inline void serialize(BinaryBuffer& out, Volume* vol) const;
212 
214  template <class TIterator>
215  void serialize(BinaryBuffer& out, TIterator begin, TIterator end) const;
216 
218  void serialize(BinaryBuffer& out, GridObjectCollection goc) const;
219 
221  void read_infos(BinaryBuffer& in);
222 
225  inline void deserialize(BinaryBuffer& in, Vertex* vrt);
226  inline void deserialize(BinaryBuffer& in, Edge* edge);
227  inline void deserialize(BinaryBuffer& in, Face* face);
228  inline void deserialize(BinaryBuffer& in, Volume* vol);
232 
234  template <class TIterator>
235  void deserialize(BinaryBuffer& in, TIterator begin, TIterator end);
236 
239 
242  void deserialization_starts();
243 
246  void deserialization_done();
247 
248  private:
250  template<class TGeomObj, class TSerializers>
251  void serialize(BinaryBuffer& out, TGeomObj* o,
252  TSerializers& serializers) const;
253 
255  template<class TGeomObj, class TDeserializers>
256  void deserialize(BinaryBuffer& in, TGeomObj* o,
257  TDeserializers& deserializers);
258 
259  template<class TSerializers>
260  void write_info(BinaryBuffer& out, TSerializers& serializers) const;
261 
262  template<class TSerializers>
263  void read_info(BinaryBuffer& in, TSerializers& serializers);
264 
265  template<class TSerializers>
266  void deserialization_starts(TSerializers& serializers);
267 
268  template<class TSerializers>
269  void deserialization_done(TSerializers& serializers);
270 
271  private:
272  std::vector<SPVertexDataSerializer> m_vrtSerializers;
273  std::vector<SPEdgeDataSerializer> m_edgeSerializers;
274  std::vector<SPFaceDataSerializer> m_faceSerializers;
275  std::vector<SPVolumeDataSerializer> m_volSerializers;
276  std::vector<SPGridDataSerializer> m_gridSerializers;
277 };
278 
279 
282 
287 template <class TGeomObj, class TAttachment>
289  public GeomObjDataSerializer<TGeomObj>
290 {
291  public:
293  create(Grid& g, TAttachment a)
295 
296  GeomObjAttachmentSerializer(Grid& g, TAttachment a) :
297  m_aa(g, a, true) {}
298 
300 
301  virtual void write_data(BinaryBuffer& out, TGeomObj* o) const
302  {Serialize(out, m_aa[o]);}
303 
304  virtual void read_data(BinaryBuffer& in, TGeomObj* o)
305  {Deserialize(in, m_aa[o]);}
306 
307  private:
309 };
310 
312 {
313  public:
316 
318 
320  virtual void write_info(BinaryBuffer& out) const;
321 
323  virtual void read_info(BinaryBuffer& in);
324 
325  virtual void write_data(BinaryBuffer& out, Vertex* o) const;
326  virtual void write_data(BinaryBuffer& out, Edge* o) const;
327  virtual void write_data(BinaryBuffer& out, Face* o) const;
328  virtual void write_data(BinaryBuffer& out, Volume* o) const;
329 
330  virtual void read_data(BinaryBuffer& in, Vertex* o);
331  virtual void read_data(BinaryBuffer& in, Edge* o);
332  virtual void read_data(BinaryBuffer& in, Face* o);
333  virtual void read_data(BinaryBuffer& in, Volume* o);
334 
335  private:
337 };
338 
341 // GRID
342 
345 
368  AInt& aIntVRT, BinaryBuffer& out);
369 
372 bool SerializeGridElements(Grid& grid, BinaryBuffer& out);
373 
376 
386  BinaryBuffer& out);
387 
390 
395  bool readGridHeader = true);
396 
397 
400 // MULTI-GRID
401 
403 // SerializeMultiGridElements
405 
444  BinaryBuffer& out,
446 
448 // SerializeMultiGridElements
450 
464  BinaryBuffer& out);
465 
467 // SerializeMultiGridElements
470  BinaryBuffer& out);
471 
474 
489  std::vector<Vertex*>* pvVrts = NULL,
490  std::vector<Edge*>* pvEdges = NULL,
491  std::vector<Face*>* pvFaces = NULL,
492  std::vector<Volume*>* pvVols = NULL,
494 
495 
496 
499 // ATTACHMENTS
500 
502 // copies attached values to a binary stream.
509 template <class TElem, class TAttachment>
510 bool SerializeAttachment(Grid& grid, TAttachment& attachment,
511  BinaryBuffer& out);
512 
515 
521 template <class TElem, class TAttachment>
522 bool SerializeAttachment(Grid& grid, TAttachment& attachment,
523  typename geometry_traits<TElem>::iterator iterBegin,
524  typename geometry_traits<TElem>::iterator iterEnd,
525  BinaryBuffer& out);
526 
529 
535 template <class TElem, class TAttachment>
536 bool DeserializeAttachment(Grid& grid, TAttachment& attachment,
537  BinaryBuffer& in);
538 
541 
547 template <class TElem, class TAttachment>
548 bool DeserializeAttachment(Grid& grid, TAttachment& attachment,
549  typename geometry_traits<TElem>::iterator iterBegin,
550  typename geometry_traits<TElem>::iterator iterEnd,
551  BinaryBuffer& in);
552 
553 
556 // SUBSET-HANDLER
557 
562  BinaryBuffer& out);
563 
567  BinaryBuffer& out);
568 
571 
582  BinaryBuffer& in,
583  bool readPropertyMap = true);
584 
585 
588 
598  BinaryBuffer& in,
599  bool readPropertyMap = true);
600 
601 
604 // SELECTOR
605 
608 bool SerializeSelector(Grid& grid, ISelector& sel,
610  BinaryBuffer& out);
611 
614 bool SerializeSelector(Grid& grid, ISelector& sel,
615  BinaryBuffer& out);
616 
619 
628 bool DeserializeSelector(Grid& grid, ISelector& sel,
630  BinaryBuffer& in);
631 
632 
635 
644 bool DeserializeSelector(Grid& grid, ISelector& sel,
645  BinaryBuffer& in);
646 
647 
648 // void SerializeProjector(BinaryBuffer& out, RefinementProjector& proj);
649 // void SerializeProjectionHandler(BinaryBuffer& out, ProjectionHandler& ph);
650 // SPRefinementProjector DeserializeProjector(BinaryBuffer& in);
651 // void DeserializeProjectionHandler(BinaryBuffer& in, ProjectionHandler& ph);
652  // end of doxygen defgroup command
654 
655 }
656 
658 // include implementation
659 #include "serialization_impl.hpp"
660 
661 #endif
Definition: smart_pointer.h:108
A Buffer for binary data.
Definition: binary_buffer.h:56
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
Serialization callback for grid attachments.
Definition: serialization.h:290
Grid::AttachmentAccessor< TGeomObj, TAttachment > m_aa
Definition: serialization.h:308
static SmartPtr< GeomObjDataSerializer< TGeomObj > > create(Grid &g, TAttachment a)
Definition: serialization.h:293
virtual ~GeomObjAttachmentSerializer()
Definition: serialization.h:299
GeomObjAttachmentSerializer(Grid &g, TAttachment a)
Definition: serialization.h:296
virtual void write_data(BinaryBuffer &out, TGeomObj *o) const
write data associated with the given object. Pure virtual.
Definition: serialization.h:301
virtual void read_data(BinaryBuffer &in, TGeomObj *o)
read data associated with the given object. Pure virtual.
Definition: serialization.h:304
Interface for handling serialization and deserialization of data associated with geometric objects.
Definition: serialization.h:86
virtual ~GeomObjDataSerializer()
Definition: serialization.h:88
virtual void read_data(BinaryBuffer &in, TGeomObj *o)=0
read data associated with the given object. Pure virtual.
virtual void write_data(BinaryBuffer &out, TGeomObj *o) const =0
write data associated with the given object. Pure virtual.
virtual void write_info(BinaryBuffer &out) const
can be used to write arbitrary info to the file.
Definition: serialization.h:93
virtual void deserialization_done()
this method will be called after read_info has been called for all geometric objects.
Definition: serialization.h:106
virtual void deserialization_starts()
this method is called after read_info has been called for all geometric objects.
Definition: serialization.h:103
virtual void read_info(BinaryBuffer &in)
Read the info written during write_info here. Default: empty implementation.
Definition: serialization.h:98
Serialization of data associated with grid elements.
Definition: serialization.h:186
void read_infos(BinaryBuffer &in)
calls read_info on all registered serializers
Definition: serialization.cpp:119
void deserialization_starts()
Definition: serialization.cpp:163
void read_info(BinaryBuffer &in, TSerializers &serializers)
Definition: serialization.cpp:104
void deserialize(BinaryBuffer &in, Vertex *vrt)
Deserializes data associated with the given object.
Definition: serialization_impl.hpp:93
std::vector< SPVolumeDataSerializer > m_volSerializers
Definition: serialization.h:275
void write_infos(BinaryBuffer &out) const
calls write_info on all registered serializers
Definition: serialization.cpp:110
void add(SPVertexDataSerializer cb)
Adds a callback class for serialization and deserialization.
Definition: serialization.cpp:69
std::vector< SPFaceDataSerializer > m_faceSerializers
Definition: serialization.h:274
~GridDataSerializationHandler()
Definition: serialization.h:188
std::vector< SPGridDataSerializer > m_gridSerializers
Definition: serialization.h:276
void write_info(BinaryBuffer &out, TSerializers &serializers) const
Definition: serialization.cpp:96
std::vector< SPEdgeDataSerializer > m_edgeSerializers
Definition: serialization.h:273
void deserialization_done()
Definition: serialization.cpp:181
void serialize(BinaryBuffer &out, Vertex *vrt) const
Serializes data associated with the given object.
Definition: serialization_impl.hpp:44
std::vector< SPVertexDataSerializer > m_vrtSerializers
Definition: serialization.h:272
Interface for handling serialization and deserialization of data associated with all geometric object...
Definition: serialization.h:138
virtual void read_data(BinaryBuffer &in, Vertex *o)
Definition: serialization.h:155
virtual void write_data(BinaryBuffer &out, Vertex *o) const
Definition: serialization.h:150
virtual void deserialization_done()
this method is called after read_info has been called for all geometric objects.
Definition: serialization.h:164
virtual void deserialization_starts()
this method is called after read_info has been called for all geometric objects.
Definition: serialization.h:161
virtual void write_data(BinaryBuffer &out, Face *o) const
Definition: serialization.h:152
virtual void read_data(BinaryBuffer &in, Edge *o)
Definition: serialization.h:156
virtual void write_data(BinaryBuffer &out, Edge *o) const
Definition: serialization.h:151
virtual void read_data(BinaryBuffer &in, Face *o)
Definition: serialization.h:157
virtual void read_info(BinaryBuffer &in)
Read the info written during write_info here. Default: empty implementation.
Definition: serialization.h:148
virtual void write_data(BinaryBuffer &out, Volume *o) const
Definition: serialization.h:153
virtual void write_info(BinaryBuffer &out) const
can be used to write arbitrary info to the file.
Definition: serialization.h:145
virtual void read_data(BinaryBuffer &in, Volume *o)
Definition: serialization.h:158
virtual ~GridDataSerializer()
Definition: serialization.h:140
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
a helper class that holds a collection of possibly unconnected geometric-objects.
Definition: grid_object_collection.h:96
base-implementation of a selector
Definition: selector_interface.h:126
Definition: subset_handler_interface.h:223
Accesses attachements in different element types at the same time.
Definition: attachment_util.h:56
Definition: multi_grid.h:72
Definition: serialization.h:312
virtual void write_info(BinaryBuffer &out) const
writes subset-infos to the stream (subset names and colors)
Definition: serialization.cpp:198
virtual void read_data(BinaryBuffer &in, Vertex *o)
Definition: serialization.cpp:250
virtual void read_info(BinaryBuffer &in)
Read the info written during write_info here. Default: empty implementation.
Definition: serialization.cpp:211
ISubsetHandler & m_sh
Definition: serialization.h:336
SubsetHandlerSerializer(ISubsetHandler &sh)
Definition: serialization.cpp:192
virtual void write_data(BinaryBuffer &out, Vertex *o) const
Definition: serialization.cpp:226
static SPGridDataSerializer create(ISubsetHandler &sh)
Definition: serialization.h:314
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
Definition: grid_base_object_traits.h:68
GeomObjDataSerializer< Edge > EdgeDataSerializer
Definition: serialization.h:110
bool DeserializeSubsetHandler(Grid &grid, ISubsetHandler &sh, GridObjectCollection goc, BinaryBuffer &in, bool readPropertyMap)
assigns subset-indices to all elements in the goc from a stream.
Definition: serialization.cpp:2504
GeomObjDataSerializer< Face > FaceDataSerializer
Definition: serialization.h:111
GeomObjDataSerializer< Vertex > VertexDataSerializer
Definition: serialization.h:109
bool SerializeSubsetHandler(Grid &grid, ISubsetHandler &sh, GridObjectCollection goc, BinaryBuffer &out)
writes the subset-indices of all elements in the goc to a stream.
Definition: serialization.cpp:2417
bool SerializeAttachment(Grid &grid, TAttachment &attachment, BinaryBuffer &out)
Definition: serialization_impl.hpp:144
GeomObjDataSerializer< Volume > VolumeDataSerializer
Definition: serialization.h:112
bool SerializeSelector(Grid &grid, ISelector &sel, GridObjectCollection goc, BinaryBuffer &out)
writes the subset-indices of all elements in the goc to a stream.
Definition: serialization.cpp:2603
SmartPtr< GridDataSerializer > SPGridDataSerializer
Definition: serialization.h:167
SmartPtr< VolumeDataSerializer > SPVolumeDataSerializer
Definition: serialization.h:117
SmartPtr< VertexDataSerializer > SPVertexDataSerializer
Definition: serialization.h:114
SmartPtr< FaceDataSerializer > SPFaceDataSerializer
Definition: serialization.h:116
SmartPtr< EdgeDataSerializer > SPEdgeDataSerializer
Definition: serialization.h:115
bool DeserializeAttachment(Grid &grid, TAttachment &attachment, BinaryBuffer &in)
copies attached values from a binary stream
Definition: serialization_impl.hpp:192
bool DeserializeSelector(Grid &grid, ISelector &sel, GridObjectCollection goc, BinaryBuffer &in)
assigns subset-indices to all elements in the goc from a stream.
Definition: serialization.cpp:2666
bool SerializeGridElements(Grid &grid, BinaryBuffer &out)
Writes all grid elements into a binary-stream.
Definition: serialization.cpp:412
bool DeserializeGridElements(Grid &grid, BinaryBuffer &in, bool readGridHeader)
Creates grid elements from a binary stream.
Definition: serialization.cpp:652
bool SerializeMultiGridElements(MultiGrid &mg, GridObjectCollection mgoc, MultiElementAttachmentAccessor< AInt > &aaInt, BinaryBuffer &out, MultiElementAttachmentAccessor< AGeomObjID > *paaID)
writes a part of the elements of a MultiGrid to a binary stream.
Definition: serialization.cpp:873
bool DeserializeMultiGridElements(MultiGrid &mg, BinaryBuffer &in, std::vector< Vertex * > *pvVrts, std::vector< Edge * > *pvEdges, std::vector< Face * > *pvFaces, std::vector< Volume * > *pvVols, MultiElementAttachmentAccessor< AGeomObjID > *paaID)
Creates multi-grid elements from a binary stream.
Definition: serialization.cpp:1428
the ug namespace
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition: restart_bridge.cpp:112
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition: restart_bridge.cpp:103