mesh_data_xdr_support.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2016 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // C++ includes
21 #include <fstream>
22 
23 // Local includes
24 #include "libmesh/mesh_data.h"
25 #include "libmesh/mesh_base.h"
26 #include "libmesh/xdr_cxx.h"
27 #include "libmesh/elem.h"
28 
29 namespace libMesh
30 {
31 
32 
33 //------------------------------------------------------
34 // MeshData functions
35 void MeshData::read_xdr (const std::string & name,
36  const XdrMODE mode)
37 {
77  // Better be active or in compatibility mode
79 
80 
81  // make sure the id maps are ready
84 
85 
89  this->clear();
90 
91 
92  Xdr io(name, mode);
93 
94 
95  /*
96  * all processors read the data in the same format,
97  * but only the processor that owns the element stores
98  * element-associated data. For nodes, i haven't come
99  * up with such asmart idea, yet... :-P
100  */
101  const unsigned int proc_id = _mesh.processor_id();
102 
103 
104 
110  {
111  std::string desc = "";
112  io.data (desc);
113  this->_data_descriptor = desc;
114  }
115 
116 
117 
123  {
124  std::string vtype="";
125  io.data (vtype);
126 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
127  if (vtype != "COMPLEX")
128  libmesh_error_msg("ERROR: File does not contain complex-valued data!");
129 
130 #elif LIBMESH_USE_REAL_NUMBERS
131  if (vtype != "REAL")
132  libmesh_error_msg("ERROR: File does not contain real-valued data!");
133 
134 #else
135  /*
136  * What number type is this?
137  */
138  libmesh_error_msg("Must be using either real or complex numbers!");
139 #endif
140  }
141 
142 
143 
149  unsigned int n_node = 0;
150  io.data (n_node);
151 
152 
158  unsigned int n_elem = 0;
159  io.data (n_elem);
160 
161 #ifdef DEBUG
162  std::size_t previous_values_size = 0;
163 #endif
164 
165  for (unsigned int n_cnt=0; n_cnt < n_node; n_cnt++)
166  {
173  unsigned int f_id = 0;
174  io.data (f_id);
175 
176  const Node * node = foreign_id_to_node(f_id);
177 
178 
185  {
186  std::vector<Number> values;
187  io.data (values);
188 
189 
190 #ifdef DEBUG
191  /*
192  * make sure the size of the values vectors
193  * are identical for all nodes
194  */
195  if (n_cnt == 0)
196  previous_values_size = values.size();
197  else
198  {
199  if (previous_values_size != values.size())
200  libmesh_error_msg("ERROR: Size mismatch for n_cnt = " << n_cnt);
201  }
202 #endif
203 
204 
208  _node_data.insert (std::make_pair(node, values));
209  }
210  }
211 
212 
213 
214 #ifdef DEBUG
215  previous_values_size = 0;
216 #endif
217 
218  for (unsigned int n_cnt=0; n_cnt < n_elem; n_cnt++)
219  {
225  unsigned int f_id = 0;
226  io.data (f_id);
227 
228  const Elem * elem = foreign_id_to_elem(f_id);
229 
230 
237  {
238  std::vector<Number> values;
239  io.data (values);
240 
241 
242 #ifdef DEBUG
243  /*
244  * make sure the size of the values vectors
245  * are identical for all elements
246  */
247  if (n_cnt == 0)
248  previous_values_size = values.size();
249  else
250  {
251  if (previous_values_size != values.size())
252  libmesh_error_msg("ERROR: Size mismatch for n_cnt = " << n_cnt);
253  }
254 #endif
255 
256 
261  if (elem->processor_id() == proc_id)
262  _elem_data.insert (std::make_pair(elem, values));
263  }
264  }
265 
266 
267  /*
268  * finished reading. Now ready for use, provided
269  * there was any data contained in the file.
270  */
271  libmesh_assert ((this->_node_data.size() != 0) || (this->_elem_data.size() != 0));
272 
273  this->_node_data_closed = true;
274  this->_elem_data_closed = true;
275 }
276 
277 
278 
279 
280 
281 
282 void MeshData::write_xdr (const std::string & name,
283  const XdrMODE mode)
284 {
323  /*
324  * make sure the id maps are ready
325  * and that we have data to write
326  */
329 
332 
333 
334  Xdr io(name, mode);
335 
336 
337  // all processors write the data in the same format
338  //const unsigned int proc_id = _mesh.processor_id();
339 
345  {
346  std::string desc = this->_data_descriptor;
347  io.data (desc, "# Data description");
348  }
349 
350 
351 
357  {
358 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
359  std::string desc = "COMPLEX";
360 #elif LIBMESH_USE_REAL_NUMBERS
361  std::string desc = "REAL";
362 #else
363  better_you_choke_this...
364 #endif
365  io.data (desc, "# type of values");
366  }
367 
368 
369 
375  {
376  unsigned int n_node =
377  cast_int<unsigned int>(this->_node_data.size());
378  io.data (n_node, "# No. of nodes for which data is stored");
379  }
380 
381 
387  {
388  unsigned int n_elem =
389  cast_int<unsigned int>(this->_elem_data.size());
390  io.data (n_elem, "# No. of elements for which data is stored");
391  }
392 
393 
394 
395 
396  std::map<const Node *,
397  std::vector<Number> >::const_iterator nit = _node_data.begin ();
398 
399  for (; nit != _node_data.end(); ++nit)
400  {
401  const Node * node = (*nit).first;
402 
408  {
409  unsigned int f_id = node_to_foreign_id (node);
410  io.data (f_id, "# Foreign node id");
411  }
412 
413 
419  {
420  /*
421  * since we are iterating over our @e own
422  * map, this libmesh_assert should never break...
423  */
424  libmesh_assert (this->has_data(node));
425 
426  const std::vector<Number> & values = this->get_data(node);
427 
428  /*
429  * copy the data to a local buf, since
430  * the Xdr class needs write access, even
431  * when only reading data
432  */
433  std::vector<Number> buf = values;
434  io.data (buf, "# Values");
435  }
436  }
437 
438 
439 
440 
441 
442 
443 
444  std::map<const Elem *,
445  std::vector<Number> >::const_iterator eit = _elem_data.begin ();
446 
447  for (; eit != _elem_data.end(); ++eit)
448  {
449  const Elem * elem = (*eit).first;
450 
456  {
457  unsigned int f_id = elem_to_foreign_id (elem);
458  io.data (f_id, "# Foreign element id");
459  }
460 
461 
467  {
468  /*
469  * since we are iterating over our @e own
470  * map, this libmesh_assert should never break...
471  */
472  libmesh_assert (this->has_data(elem));
473 
474  const std::vector<Number> & values = this->get_data(elem);
475 
476  /*
477  * copy the data to a local buf, since
478  * the Xdr class needs write access, even
479  * when only reading data
480  */
481  std::vector<Number> buf = values;
482  io.data (buf, "# Values");
483  }
484  }
485 }
486 
487 } // namespace libMesh
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
bool _node_id_map_closed
Definition: mesh_data.h:575
void data(T &a, const char *comment="")
Definition: xdr_cxx.C:706
const std::vector< Number > & get_data(const Node *node) const
Definition: mesh_data.h:862
A geometric point in (x,y,z) space associated with a DOF.
Definition: node.h:54
bool has_data(const Node *node) const
Definition: mesh_data.h:848
libmesh_assert(remote_elem)
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Definition: mesh_tools.C:669
The base class for all geometric element types.
Definition: elem.h:92
void write_xdr(const std::string &name, const XdrMODE mode=WRITE)
bool _elem_id_map_closed
Definition: mesh_data.h:593
unsigned int node_to_foreign_id(const Node *n) const
Definition: mesh_data.C:399
bool _compatibility_mode
Definition: mesh_data.h:614
const Elem * foreign_id_to_elem(const unsigned int fid) const
Definition: mesh_data.C:434
bool _elem_data_closed
Definition: mesh_data.h:599
void read_xdr(const std::string &name, const XdrMODE mode=READ)
std::string _data_descriptor
Definition: mesh_data.h:515
C++ interface for the XDR (eXternal Data Representation) format.
Definition: xdr_cxx.h:68
unsigned int elem_to_foreign_id(const Elem *n) const
Definition: mesh_data.C:463
const MeshBase & _mesh
Definition: mesh_data.h:509
const Node * foreign_id_to_node(const unsigned int fid) const
Definition: mesh_data.C:369
std::map< const Node *, std::vector< Number > > _node_data
Definition: mesh_data.h:525
std::map< const Elem *, std::vector< Number > > _elem_data
Definition: mesh_data.h:549
processor_id_type processor_id() const
processor_id_type processor_id() const
Definition: dof_object.h:686
bool _node_data_closed
Definition: mesh_data.h:581