www.mooseframework.org
DataIO.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "DataIO.h"
16 #include "MooseMesh.h"
17 #include "ColumnMajorMatrix.h"
18 
19 #include "libmesh/numeric_vector.h"
20 #include "libmesh/dense_matrix.h"
21 #include "libmesh/elem.h"
22 
23 template <>
24 void
25 dataStore(std::ostream & stream, Real & v, void * /*context*/)
26 {
27  stream.write((char *)&v, sizeof(v));
28 }
29 
30 template <>
31 void
32 dataStore(std::ostream & stream, std::string & v, void * /*context*/)
33 {
34  // Write the size of the string
35  unsigned int size = v.size();
36  stream.write((char *)&size, sizeof(size));
37 
38  // Write the string (Do not store the null byte)
39  stream.write(v.c_str(), sizeof(char) * size);
40 }
41 
42 template <>
43 void
44 dataStore(std::ostream & stream, NumericVector<Real> & v, void * /*context*/)
45 {
46  v.close();
47 
48  numeric_index_type size = v.local_size();
49 
50  for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
51  {
52  Real r = v(i);
53  stream.write((char *)&r, sizeof(r));
54  }
55 }
56 
57 template <>
58 void
59 dataStore(std::ostream & stream, DenseVector<Real> & v, void * /*context*/)
60 {
61  unsigned int m = v.size();
62  stream.write((char *)&m, sizeof(m));
63  for (unsigned int i = 0; i < v.size(); i++)
64  {
65  Real r = v(i);
66  stream.write((char *)&r, sizeof(r));
67  }
68 }
69 
70 template <>
71 void
72 dataStore(std::ostream & stream, DenseMatrix<Real> & v, void * /*context*/)
73 {
74  unsigned int m = v.m();
75  unsigned int n = v.n();
76  stream.write((char *)&m, sizeof(m));
77  stream.write((char *)&n, sizeof(n));
78  for (unsigned int i = 0; i < v.m(); i++)
79  for (unsigned int j = 0; j < v.n(); j++)
80  {
81  Real r = v(i, j);
82  stream.write((char *)&r, sizeof(r));
83  }
84 }
85 
86 template <>
87 void
88 dataStore(std::ostream & stream, ColumnMajorMatrix & v, void * /*context*/)
89 {
90  for (unsigned int i = 0; i < v.m(); i++)
91  for (unsigned int j = 0; j < v.n(); j++)
92  {
93  Real r = v(i, j);
94  stream.write((char *)&r, sizeof(r));
95  }
96 }
97 
98 template <>
99 void
100 dataStore(std::ostream & stream, RealTensorValue & v, void * /*context*/)
101 {
102  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
103  for (unsigned int j = 0; i < LIBMESH_DIM; i++)
104  stream.write((char *)&v(i, j), sizeof(v(i, j)));
105 }
106 
107 template <>
108 void
109 dataStore(std::ostream & stream, RealVectorValue & v, void * /*context*/)
110 {
111  // Obviously if someone loads data with different LIBMESH_DIM than was used for saving them, it
112  // won't work.
113  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
114  stream.write((char *)&v(i), sizeof(v(i)));
115 }
116 
117 template <>
118 void
119 dataStore(std::ostream & stream, const Elem *& e, void * context)
120 {
121  // TODO: Write out the unique ID of this elem
122  dof_id_type id = libMesh::DofObject::invalid_id;
123 
124  if (e)
125  {
126  id = e->id();
127  if (id == libMesh::DofObject::invalid_id)
128  mooseError("Can't output Elems with invalid ids!");
129  }
130 
131  storeHelper(stream, id, context);
132 }
133 
134 template <>
135 void
136 dataStore(std::ostream & stream, const Node *& n, void * context)
137 {
138  // TODO: Write out the unique ID of this node
139  dof_id_type id = libMesh::DofObject::invalid_id;
140 
141  if (n)
142  {
143  id = n->id();
144  if (id == libMesh::DofObject::invalid_id)
145  mooseError("Can't output Nodes with invalid ids!");
146  }
147 
148  storeHelper(stream, id, context);
149 }
150 
151 template <>
152 void
153 dataStore(std::ostream & stream, Elem *& e, void * context)
154 {
155  // TODO: Write out the unique ID of this elem
156  dof_id_type id = libMesh::DofObject::invalid_id;
157 
158  if (e)
159  {
160  id = e->id();
161  if (id == libMesh::DofObject::invalid_id)
162  mooseError("Can't output Elems with invalid ids!");
163  }
164 
165  storeHelper(stream, id, context);
166 }
167 
168 template <>
169 void
170 dataStore(std::ostream & stream, Node *& n, void * context)
171 {
172  // TODO: Write out the unique ID of this node
173  dof_id_type id = libMesh::DofObject::invalid_id;
174 
175  if (n)
176  {
177  id = n->id();
178  if (id == libMesh::DofObject::invalid_id)
179  mooseError("Can't output Nodes with invalid ids!");
180  }
181 
182  storeHelper(stream, id, context);
183 }
184 
185 template <>
186 void
187 dataStore(std::ostream & stream, std::stringstream & s, void * /* context */)
188 {
189  const std::string & s_str = s.str();
190 
191  size_t s_size = s_str.size();
192  stream.write((char *)&s_size, sizeof(s_size));
193 
194  stream.write(s_str.c_str(), sizeof(char) * (s_str.size()));
195 }
196 
197 template <>
198 void
199 dataStore(std::ostream & stream, std::stringstream *& s, void * context)
200 {
201  dataStore(stream, *s, context);
202 }
203 
204 // global load functions
205 
206 template <>
207 void
208 dataLoad(std::istream & stream, Real & v, void * /*context*/)
209 {
210  stream.read((char *)&v, sizeof(v));
211 }
212 
213 template <>
214 void
215 dataLoad(std::istream & stream, std::string & v, void * /*context*/)
216 {
217  // Read the size of the string
218  unsigned int size = 0;
219  stream.read((char *)&size, sizeof(size));
220 
221  // Resize the string data
222  v.resize(size);
223 
224  // Read the string
225  stream.read(&v[0], sizeof(char) * size);
226 }
227 
228 template <>
229 void
230 dataLoad(std::istream & stream, NumericVector<Real> & v, void * /*context*/)
231 {
232  numeric_index_type size = v.local_size();
233 
234  for (numeric_index_type i = v.first_local_index(); i < v.first_local_index() + size; i++)
235  {
236  Real r = 0;
237  stream.read((char *)&r, sizeof(r));
238  v.set(i, r);
239  }
240 
241  v.close();
242 }
243 
244 template <>
245 void
246 dataLoad(std::istream & stream, DenseVector<Real> & v, void * /*context*/)
247 {
248  unsigned int n = 0;
249  stream.read((char *)&n, sizeof(n));
250  v.resize(n);
251  for (unsigned int i = 0; i < n; i++)
252  {
253  Real r = 0;
254  stream.read((char *)&r, sizeof(r));
255  v(i) = r;
256  }
257 }
258 
259 template <>
260 void
261 dataLoad(std::istream & stream, DenseMatrix<Real> & v, void * /*context*/)
262 {
263  unsigned int nr = 0, nc = 0;
264  stream.read((char *)&nr, sizeof(nr));
265  stream.read((char *)&nc, sizeof(nc));
266  v.resize(nr, nc);
267  for (unsigned int i = 0; i < v.m(); i++)
268  for (unsigned int j = 0; j < v.n(); j++)
269  {
270  Real r = 0;
271  stream.read((char *)&r, sizeof(r));
272  v(i, j) = r;
273  }
274 }
275 
276 template <>
277 void
278 dataLoad(std::istream & stream, ColumnMajorMatrix & v, void * /*context*/)
279 {
280  for (unsigned int i = 0; i < v.m(); i++)
281  for (unsigned int j = 0; j < v.n(); j++)
282  {
283  Real r = 0;
284  stream.read((char *)&r, sizeof(r));
285  v(i, j) = r;
286  }
287 }
288 
289 template <>
290 void
291 dataLoad(std::istream & stream, RealTensorValue & v, void * /*context*/)
292 {
293  // Obviously if someone loads data with different LIBMESH_DIM than was used for saving them, it
294  // won't work.
295  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
296  for (unsigned int j = 0; i < LIBMESH_DIM; i++)
297  {
298  Real r = 0;
299  stream.read((char *)&r, sizeof(r));
300  v(i, j) = r;
301  }
302 }
303 
304 template <>
305 void
306 dataLoad(std::istream & stream, RealVectorValue & v, void * /*context*/)
307 {
308  // Obviously if someone loads data with different LIBMESH_DIM than was used for saving them, it
309  // won't work.
310  for (unsigned int i = 0; i < LIBMESH_DIM; i++)
311  {
312  Real r = 0;
313  stream.read((char *)&r, sizeof(r));
314  v(i) = r;
315  }
316 }
317 
318 template <>
319 void
320 dataLoad(std::istream & stream, const Elem *& e, void * context)
321 {
322  if (!context)
323  mooseError("Can only load Elem objects using a MooseMesh context!");
324 
325  MooseMesh * mesh = static_cast<MooseMesh *>(context);
326 
327  // TODO: Write out the unique ID of this element
328  dof_id_type id = libMesh::DofObject::invalid_id;
329 
330  loadHelper(stream, id, context);
331 
332  if (id != libMesh::DofObject::invalid_id)
333  e = mesh->elemPtr(id);
334  else
335  e = NULL;
336 }
337 
338 template <>
339 void
340 dataLoad(std::istream & stream, const Node *& n, void * context)
341 {
342  if (!context)
343  mooseError("Can only load Node objects using a MooseMesh context!");
344 
345  MooseMesh * mesh = static_cast<MooseMesh *>(context);
346 
347  // TODO: Write out the unique ID of this nodeent
348  dof_id_type id = libMesh::DofObject::invalid_id;
349 
350  loadHelper(stream, id, context);
351 
352  if (id != libMesh::DofObject::invalid_id)
353  n = mesh->nodePtr(id);
354  else
355  n = NULL;
356 }
357 
358 template <>
359 void
360 dataLoad(std::istream & stream, Elem *& e, void * context)
361 {
362  if (!context)
363  mooseError("Can only load Elem objects using a MooseMesh context!");
364 
365  MooseMesh * mesh = static_cast<MooseMesh *>(context);
366 
367  // TODO: Write out the unique ID of this element
368  dof_id_type id = libMesh::DofObject::invalid_id;
369 
370  loadHelper(stream, id, context);
371 
372  if (id != libMesh::DofObject::invalid_id)
373  e = mesh->elemPtr(id);
374  else
375  e = NULL;
376 }
377 
378 template <>
379 void
380 dataLoad(std::istream & stream, Node *& n, void * context)
381 {
382  if (!context)
383  mooseError("Can only load Node objects using a MooseMesh context!");
384 
385  MooseMesh * mesh = static_cast<MooseMesh *>(context);
386 
387  // TODO: Write out the unique ID of this nodeent
388  dof_id_type id = libMesh::DofObject::invalid_id;
389 
390  loadHelper(stream, id, context);
391 
392  if (id != libMesh::DofObject::invalid_id)
393  n = mesh->nodePtr(id);
394  else
395  n = NULL;
396 }
397 
398 template <>
399 void
400 dataLoad(std::istream & stream, std::stringstream & s, void * /* context */)
401 {
402  size_t s_size = 0;
403 
404  stream.read((char *)&s_size, sizeof(s_size));
405 
406  char * s_s = new char[s_size];
407 
408  stream.read(s_s, s_size);
409 
410  s.write(s_s, s_size);
411  delete[] s_s;
412 }
413 
414 template <>
415 void
416 dataLoad(std::istream & stream, std::stringstream *& s, void * context)
417 {
418  dataLoad(stream, *s, context);
419 }
unsigned int n() const
Returns the number of rows.
VectorValue< Real > RealVectorValue
Definition: Assembly.h:40
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:2115
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
virtual const Node * nodePtr(const dof_id_type i) const
Definition: MooseMesh.C:455
This class defines a Tensor that can change its shape.
void storeHelper(std::ostream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:528
PetscInt m
void dataLoad(std::istream &stream, Real &v, void *)
Definition: DataIO.C:208
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:74
PetscInt n
TensorValue< Real > RealTensorValue
Definition: Assembly.h:45
void loadHelper(std::istream &stream, P &data, void *context)
Scalar helper routine.
Definition: DataIO.h:592
void dataStore(std::ostream &stream, Real &v, void *)
Definition: DataIO.C:25
unsigned int m() const
Returns the number of columns.