8#include <dune/grid/common/backuprestore.hh>
9#include <dune/grid/common/gridfactory.hh>
17 template <
class Gr
idView>
20 enum { dim = GridView::dimension };
21 enum { dow = GridView::dimensionworld };
23 using ct =
typename GridView::ctype;
26 Writer(GridView
const& gv)
30 indexMap_.resize(gridView_.size(dim));
31 auto const& indexSet = gridView_.indexSet();
32 for (
auto const& vertex : vertices(gridView_))
33 indexMap_[indexSet.index(vertex)] = std::int64_t(num++);
36 void writeVertices(std::ostream& out)
const
38 for (
auto const& vertex : vertices(gridView_)) {
39 auto v = vertex.geometry().center();
40 out.write((
char*)&v, dow*
sizeof(ct));
44 void writeElements(std::ostream& out)
const
46 auto const& indexSet = gridView_.indexSet();
48 std::vector<std::int64_t> connectivity;
49 connectivity.reserve(8);
50 for (
auto const& e : elements(gridView_)) {
51 unsigned int id = e.type().id();
52 out.write((
char*)&
id,
sizeof(
unsigned int));
55 for (
unsigned int j = 0; j < e.subEntities(dim); ++j)
56 connectivity.emplace_back(indexMap_[indexSet.subIndex(e,j,dim)]);
58 out.write((
char*)connectivity.data(), connectivity.size()*
sizeof(std::int64_t));
64 std::vector<std::int64_t> indexMap_;
69 enum { dim = Grid::dimension };
70 enum { dow = Grid::dimensionworld };
72 using ct =
typename Grid::ctype;
73 using Factory = Dune::GridFactory<Grid>;
74 using GlobalCoordinates = Dune::FieldVector<ct,dow>;
77 Reader(Factory& factory, std::istream& in)
80 in.read((
char*)&numElements_,
sizeof(std::int64_t));
81 in.read((
char*)&numVertices_,
sizeof(std::int64_t));
84 void readVertices(std::istream& in)
const
87 for (std::int64_t i = 0; i < numVertices_; ++i) {
88 in.read((
char*)&p[0], dow*
sizeof(ct));
89 factory_.insertVertex(p);
93 void readElements(std::istream& in)
const
95 std::vector<std::int64_t> connectivity(8);
96 std::vector<unsigned int> vertices;
99 for (std::int64_t i = 0; i < numElements_; ++i) {
101 in.read((
char*)&
id,
sizeof(
unsigned int));
103 Dune::GeometryType type(
id,dim);
104 auto refElem = Dune::referenceElement<ct,dim>(type);
106 in.read((
char*)connectivity.data(), refElem.size(dim)*
sizeof(std::int64_t));
108 std::copy_n(connectivity.begin(),refElem.size(dim),std::back_inserter(vertices));
110 factory_.insertElement(type, vertices);
116 std::int64_t numElements_ = 0;
117 std::int64_t numVertices_ = 0;
122 template <
class Gr
idView>
123 static void backup(GridView
const& gv, std::string
const& filename)
125 std::ofstream out(filename, std::ios::binary);
130 template <
class Gr
idView>
131 static void backup(GridView
const& gv, std::ostream& out)
133 std::int32_t dim = GridView::dimension;
134 std::int32_t dow = GridView::dimensionworld;
135 std::int64_t num_elements = gv.size(0);
136 std::int64_t num_vertices = gv.size(dim);
138 out.write((
char*)&dim,
sizeof(std::int32_t));
139 out.write((
char*)&dow,
sizeof(std::int32_t));
140 out.write((
char*)&num_elements,
sizeof(std::int64_t));
141 out.write((
char*)&num_vertices,
sizeof(std::int64_t));
143 Writer<GridView> writer(gv);
144 writer.writeVertices(out);
145 writer.writeElements(out);
149 static Grid*
restore(std::string
const& filename)
151 std::ifstream in(filename, std::ios::binary);
158 Dune::GridFactory<Grid> factory;
160 std::int32_t dim = 0, dow = 0;
162 in.read((
char*)&dim,
sizeof(std::int32_t));
163 in.read((
char*)&dow,
sizeof(std::int32_t));
165 assert(Grid::dimension == dim);
166 assert(Grid::dimensionworld == dow);
168 Reader reader(factory, in);
169 reader.readVertices(in);
170 reader.readElements(in);
172 std::unique_ptr<Grid> ptr(factory.createGrid());
173 return ptr.release();
Definition: BackupRestore.hpp:16
static Grid * restore(std::istream &in)
read a hierarchic grid from a stream
Definition: BackupRestore.hpp:156
static void backup(GridView const &gv, std::ostream &out)
write a hierarchic grid into a stream
Definition: BackupRestore.hpp:131
static void backup(GridView const &gv, std::string const &filename)
Write a hierarchic grid to disk.
Definition: BackupRestore.hpp:123
static Grid * restore(std::string const &filename)
read a hierarchic grid from disk
Definition: BackupRestore.hpp:149