mesh_data_xdr_support.C
Go to the documentation of this file.00001 // The libMesh Finite Element Library. 00002 // Copyright (C) 2002-2012 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 00003 00004 // This library is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public 00006 // License as published by the Free Software Foundation; either 00007 // version 2.1 of the License, or (at your option) any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // Lesser General Public License for more details. 00013 00014 // You should have received a copy of the GNU Lesser General Public 00015 // License along with this library; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 00019 00020 // C++ includes 00021 #include <fstream> 00022 00023 // Local includes 00024 #include "libmesh/mesh_data.h" 00025 #include "libmesh/mesh_base.h" 00026 #include "libmesh/xdr_cxx.h" 00027 #include "libmesh/elem.h" 00028 00029 namespace libMesh 00030 { 00031 00032 00033 //------------------------------------------------------ 00034 // MeshData functions 00035 void MeshData::read_xdr (const std::string& name, 00036 const XdrMODE mode) 00037 { 00077 // we should better be active or in compatibility mode 00078 libmesh_assert (_active || _compatibility_mode); 00079 00080 00081 // make sure the id maps are ready 00082 libmesh_assert (_node_id_map_closed); 00083 libmesh_assert (_elem_id_map_closed); 00084 00085 00089 this->clear(); 00090 00091 00092 Xdr io(name, mode); 00093 00094 00095 /* 00096 * all processors read the data in the same format, 00097 * but only the processor that owns the element stores 00098 * element-associated data. For nodes, i haven't come 00099 * up with such asmart idea, yet... :-P 00100 */ 00101 const unsigned int proc_id = _mesh.processor_id(); 00102 00103 00104 00110 { 00111 std::string desc = ""; 00112 io.data (desc); 00113 this->_data_descriptor = desc; 00114 } 00115 00116 00117 00123 { 00124 std::string vtype=""; 00125 io.data (vtype); 00126 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 00127 if (vtype != "COMPLEX") 00128 { 00129 libMesh::err << "ERROR: File does not contain complex-valued data!" 00130 << std::endl; 00131 libmesh_error(); 00132 } 00133 #elif LIBMESH_USE_REAL_NUMBERS 00134 if (vtype != "REAL") 00135 { 00136 libMesh::err << "ERROR: File does not contain real-valued data!" 00137 << std::endl; 00138 libmesh_error(); 00139 } 00140 #else 00141 /* 00142 * What number type is this? 00143 */ 00144 libmesh_error(); 00145 #endif 00146 } 00147 00148 00149 00155 unsigned int n_node = 0; 00156 io.data (n_node); 00157 00158 00164 unsigned int n_elem = 0; 00165 io.data (n_elem); 00166 00167 #ifdef DEBUG 00168 unsigned int previous_values_size = 0; 00169 #endif 00170 00171 for (unsigned int n_cnt=0; n_cnt < n_node; n_cnt++) 00172 { 00179 unsigned int f_id = 0; 00180 io.data (f_id); 00181 00182 const Node* node = foreign_id_to_node(f_id); 00183 00184 00191 { 00192 std::vector<Number> values; 00193 io.data (values); 00194 00195 00196 #ifdef DEBUG 00197 /* 00198 * make sure the size of the values vectors 00199 * are identical for all nodes 00200 */ 00201 if (n_cnt == 0) 00202 previous_values_size = values.size(); 00203 else 00204 { 00205 if (previous_values_size != values.size()) 00206 { 00207 libMesh::err << "ERROR: Size mismatch for n_cnt = " << n_cnt << std::endl; 00208 libmesh_error(); 00209 } 00210 } 00211 #endif 00212 00213 00217 _node_data.insert (std::make_pair(node, values)); 00218 } 00219 } 00220 00221 00222 00223 #ifdef DEBUG 00224 previous_values_size = 0; 00225 #endif 00226 00227 for (unsigned int n_cnt=0; n_cnt < n_elem; n_cnt++) 00228 { 00234 unsigned int f_id = 0; 00235 io.data (f_id); 00236 00237 const Elem* elem = foreign_id_to_elem(f_id); 00238 00239 00246 { 00247 std::vector<Number> values; 00248 io.data (values); 00249 00250 00251 #ifdef DEBUG 00252 /* 00253 * make sure the size of the values vectors 00254 * are identical for all elements 00255 */ 00256 if (n_cnt == 0) 00257 previous_values_size = values.size(); 00258 else 00259 { 00260 if (previous_values_size != values.size()) 00261 { 00262 libMesh::err << "ERROR: Size mismatch for n_cnt = " << n_cnt << std::endl; 00263 libmesh_error(); 00264 } 00265 } 00266 #endif 00267 00268 00273 if (elem->processor_id() == proc_id) 00274 _elem_data.insert (std::make_pair(elem, values)); 00275 } 00276 } 00277 00278 00279 /* 00280 * finished reading. Now ready for use, provided 00281 * there was any data contained in the file. 00282 */ 00283 libmesh_assert ((this->_node_data.size() != 0) || (this->_elem_data.size() != 0)); 00284 00285 this->_node_data_closed = true; 00286 this->_elem_data_closed = true; 00287 } 00288 00289 00290 00291 00292 00293 00294 void MeshData::write_xdr (const std::string& name, 00295 const XdrMODE mode) 00296 { 00335 /* 00336 * make sure the id maps are ready 00337 * and that we have data to write 00338 */ 00339 libmesh_assert (_node_id_map_closed); 00340 libmesh_assert (_elem_id_map_closed); 00341 00342 libmesh_assert (_node_data_closed); 00343 libmesh_assert (_elem_data_closed); 00344 00345 00346 Xdr io(name, mode); 00347 00348 00349 // all processors write the data in the same format 00350 //const unsigned int proc_id = _mesh.processor_id(); 00351 00357 { 00358 std::string desc = this->_data_descriptor; 00359 io.data (desc, "# Data description"); 00360 } 00361 00362 00363 00369 { 00370 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 00371 std::string desc = "COMPLEX"; 00372 #elif LIBMESH_USE_REAL_NUMBERS 00373 std::string desc = "REAL"; 00374 #else 00375 better_you_choke_this... 00376 #endif 00377 io.data (desc, "# type of values"); 00378 } 00379 00380 00381 00387 { 00388 unsigned int n_node = this->_node_data.size(); 00389 io.data (n_node, "# No. of nodes for which data is stored"); 00390 } 00391 00392 00398 { 00399 unsigned int n_elem = this->_elem_data.size(); 00400 io.data (n_elem, "# No. of elements for which data is stored"); 00401 } 00402 00403 00404 00405 00406 std::map<const Node*, 00407 std::vector<Number> >::const_iterator nit = _node_data.begin (); 00408 00409 for (; nit != _node_data.end(); ++nit) 00410 { 00411 const Node* node = (*nit).first; 00412 00418 { 00419 unsigned int f_id = node_to_foreign_id (node); 00420 io.data (f_id, "# Foreign node id"); 00421 } 00422 00423 00429 { 00430 /* 00431 * since we are iterating over our @e own 00432 * map, this libmesh_assert should never break... 00433 */ 00434 libmesh_assert (this->has_data(node)); 00435 00436 const std::vector<Number>& values = this->get_data(node); 00437 00438 /* 00439 * copy the data to a local buf, since 00440 * the Xdr class needs write access, even 00441 * when only reading data 00442 */ 00443 std::vector<Number> buf = values; 00444 io.data (buf, "# Values"); 00445 } 00446 } 00447 00448 00449 00450 00451 00452 00453 00454 std::map<const Elem*, 00455 std::vector<Number> >::const_iterator eit = _elem_data.begin (); 00456 00457 for (; eit != _elem_data.end(); ++eit) 00458 { 00459 const Elem* elem = (*eit).first; 00460 00466 { 00467 unsigned int f_id = elem_to_foreign_id (elem); 00468 io.data (f_id, "# Foreign element id"); 00469 } 00470 00471 00477 { 00478 /* 00479 * since we are iterating over our @e own 00480 * map, this libmesh_assert should never break... 00481 */ 00482 libmesh_assert (this->has_data(elem)); 00483 00484 const std::vector<Number>& values = this->get_data(elem); 00485 00486 /* 00487 * copy the data to a local buf, since 00488 * the Xdr class needs write access, even 00489 * when only reading data 00490 */ 00491 std::vector<Number> buf = values; 00492 io.data (buf, "# Values"); 00493 } 00494 } 00495 } 00496 00497 } // namespace libMesh 00498 00499
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:47 UTC
Hosted By: