mesh_data_xdr_support.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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  // we should 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  {
129  libMesh::err << "ERROR: File does not contain complex-valued data!"
130  << std::endl;
131  libmesh_error();
132  }
133 #elif LIBMESH_USE_REAL_NUMBERS
134  if (vtype != "REAL")
135  {
136  libMesh::err << "ERROR: File does not contain real-valued data!"
137  << std::endl;
138  libmesh_error();
139  }
140 #else
141  /*
142  * What number type is this?
143  */
144  libmesh_error();
145 #endif
146  }
147 
148 
149 
155  unsigned int n_node = 0;
156  io.data (n_node);
157 
158 
164  unsigned int n_elem = 0;
165  io.data (n_elem);
166 
167 #ifdef DEBUG
168  unsigned int previous_values_size = 0;
169 #endif
170 
171  for (unsigned int n_cnt=0; n_cnt < n_node; n_cnt++)
172  {
179  unsigned int f_id = 0;
180  io.data (f_id);
181 
182  const Node* node = foreign_id_to_node(f_id);
183 
184 
191  {
192  std::vector<Number> values;
193  io.data (values);
194 
195 
196 #ifdef DEBUG
197  /*
198  * make sure the size of the values vectors
199  * are identical for all nodes
200  */
201  if (n_cnt == 0)
202  previous_values_size = values.size();
203  else
204  {
205  if (previous_values_size != values.size())
206  {
207  libMesh::err << "ERROR: Size mismatch for n_cnt = " << n_cnt << std::endl;
208  libmesh_error();
209  }
210  }
211 #endif
212 
213 
217  _node_data.insert (std::make_pair(node, values));
218  }
219  }
220 
221 
222 
223 #ifdef DEBUG
224  previous_values_size = 0;
225 #endif
226 
227  for (unsigned int n_cnt=0; n_cnt < n_elem; n_cnt++)
228  {
234  unsigned int f_id = 0;
235  io.data (f_id);
236 
237  const Elem* elem = foreign_id_to_elem(f_id);
238 
239 
246  {
247  std::vector<Number> values;
248  io.data (values);
249 
250 
251 #ifdef DEBUG
252  /*
253  * make sure the size of the values vectors
254  * are identical for all elements
255  */
256  if (n_cnt == 0)
257  previous_values_size = values.size();
258  else
259  {
260  if (previous_values_size != values.size())
261  {
262  libMesh::err << "ERROR: Size mismatch for n_cnt = " << n_cnt << std::endl;
263  libmesh_error();
264  }
265  }
266 #endif
267 
268 
273  if (elem->processor_id() == proc_id)
274  _elem_data.insert (std::make_pair(elem, values));
275  }
276  }
277 
278 
279  /*
280  * finished reading. Now ready for use, provided
281  * there was any data contained in the file.
282  */
283  libmesh_assert ((this->_node_data.size() != 0) || (this->_elem_data.size() != 0));
284 
285  this->_node_data_closed = true;
286  this->_elem_data_closed = true;
287 }
288 
289 
290 
291 
292 
293 
294 void MeshData::write_xdr (const std::string& name,
295  const XdrMODE mode)
296 {
335  /*
336  * make sure the id maps are ready
337  * and that we have data to write
338  */
341 
344 
345 
346  Xdr io(name, mode);
347 
348 
349  // all processors write the data in the same format
350  //const unsigned int proc_id = _mesh.processor_id();
351 
357  {
358  std::string desc = this->_data_descriptor;
359  io.data (desc, "# Data description");
360  }
361 
362 
363 
369  {
370 #ifdef LIBMESH_USE_COMPLEX_NUMBERS
371  std::string desc = "COMPLEX";
372 #elif LIBMESH_USE_REAL_NUMBERS
373  std::string desc = "REAL";
374 #else
375 better_you_choke_this...
376 #endif
377  io.data (desc, "# type of values");
378  }
379 
380 
381 
387  {
388  unsigned int n_node = this->_node_data.size();
389  io.data (n_node, "# No. of nodes for which data is stored");
390  }
391 
392 
398  {
399  unsigned int n_elem = this->_elem_data.size();
400  io.data (n_elem, "# No. of elements for which data is stored");
401  }
402 
403 
404 
405 
406  std::map<const Node*,
407  std::vector<Number> >::const_iterator nit = _node_data.begin ();
408 
409  for (; nit != _node_data.end(); ++nit)
410  {
411  const Node* node = (*nit).first;
412 
418  {
419  unsigned int f_id = node_to_foreign_id (node);
420  io.data (f_id, "# Foreign node id");
421  }
422 
423 
429  {
430  /*
431  * since we are iterating over our @e own
432  * map, this libmesh_assert should never break...
433  */
434  libmesh_assert (this->has_data(node));
435 
436  const std::vector<Number>& values = this->get_data(node);
437 
438  /*
439  * copy the data to a local buf, since
440  * the Xdr class needs write access, even
441  * when only reading data
442  */
443  std::vector<Number> buf = values;
444  io.data (buf, "# Values");
445  }
446  }
447 
448 
449 
450 
451 
452 
453 
454  std::map<const Elem*,
455  std::vector<Number> >::const_iterator eit = _elem_data.begin ();
456 
457  for (; eit != _elem_data.end(); ++eit)
458  {
459  const Elem* elem = (*eit).first;
460 
466  {
467  unsigned int f_id = elem_to_foreign_id (elem);
468  io.data (f_id, "# Foreign element id");
469  }
470 
471 
477  {
478  /*
479  * since we are iterating over our @e own
480  * map, this libmesh_assert should never break...
481  */
482  libmesh_assert (this->has_data(elem));
483 
484  const std::vector<Number>& values = this->get_data(elem);
485 
486  /*
487  * copy the data to a local buf, since
488  * the Xdr class needs write access, even
489  * when only reading data
490  */
491  std::vector<Number> buf = values;
492  io.data (buf, "# Values");
493  }
494  }
495 }
496 
497 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo