xdr_mesh.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 // Local includes
19 #include "libmesh/xdr_mesh.h"
20 #include "libmesh/xdr_mhead.h"
21 #include "libmesh/enum_elem_type.h" // for ElemType
22 
23 namespace libMesh
24 {
25 
26 // ------------------------------------------------------------
27 // XdrMESH members
29 {
30  // Temporary variables to facilitate stream reading
31  const int comm_len= 256;
32  char comment[comm_len];
33 
34  switch (m_type)
35  {
36 
37 #ifdef LIBMESH_HAVE_XDR
38 
39  case (XdrMGF::DECODE):
40  case (XdrMGF::ENCODE):
41  {
42  xdr_int(mp_xdr_handle, &(hd->m_numel));
43  xdr_int(mp_xdr_handle, &(hd->m_numNodes));
44  xdr_int(mp_xdr_handle, &(hd->m_sumWghts));
45  xdr_int(mp_xdr_handle, &(hd->m_numBCs));
46  xdr_int(mp_xdr_handle, &(hd->m_strSize));
47  break;
48  }
49 
50 #endif
51 
52  case (XdrMGF::W_ASCII):
53  {
54  mp_out << hd->m_numel << "\t # Num. Elements\n";
55  mp_out << hd->m_numNodes << "\t # Num. Nodes\n";
56  mp_out << hd->m_sumWghts << "\t # Sum of Element Weights\n";
57  mp_out << hd->m_numBCs << "\t # Num. Boundary Conds.\n";
58  mp_out << hd->m_strSize << "\t # String Size (ignore)\n";
59  break;
60  }
61 
62  case (XdrMGF::R_ASCII):
63  {
64  libmesh_assert (mp_in.good());
65 
66  mp_in >> hd->m_numel ; mp_in.getline(comment, comm_len);
67  mp_in >> hd->m_numNodes ; mp_in.getline(comment, comm_len);
68  mp_in >> hd->m_sumWghts ; mp_in.getline(comment, comm_len);
69  mp_in >> hd->m_numBCs ; mp_in.getline(comment, comm_len);
70  mp_in >> hd->m_strSize ; mp_in.getline(comment, comm_len);
71 
72  libmesh_assert(mp_in.good());
73 
74  break;
75  }
76 
77  default:
78  // Unknown access type
79  libmesh_error();
80 
81  }
82 
83  // Let's write the augmented header information
84  // before we write the title and id string
85 
86  // Both DEAL and LIBM style files have augmented headers.
87  if ((orig_flag == 0) || (orig_flag == 2))
88  {
89 
90  switch (m_type)
91  {
92 
93 #ifdef LIBMESH_HAVE_XDR
94 
95  case (XdrMGF::ENCODE):
96  case (XdrMGF::DECODE):
97  {
98  // this used to be 0. How did that work?
99  unsigned int temp_n_blocks = hd->get_n_blocks();
100  xdr_u_int(mp_xdr_handle, &temp_n_blocks);
101  hd->set_n_blocks(temp_n_blocks);
102 
103  // The number of blocks (i.e. the number of element types)
104  // for any mesh must always
105  // be at least 1.
106  libmesh_assert_not_equal_to (hd->get_n_blocks(), 0);
107  break;
108  }
109 
110 #endif
111 
112  case (XdrMGF::W_ASCII):
113  {
114  mp_out << hd->get_n_blocks() << "\t # Num. Element Blocks.\n";
115  break;
116  }
117 
118  case (XdrMGF::R_ASCII):
119  {
120  libmesh_assert (mp_in.good());
121  unsigned int temp_n_blocks=0;
122  mp_in >> temp_n_blocks;
123  hd->set_n_blocks(temp_n_blocks);
124  mp_in.getline(comment, comm_len);
125  break;
126  }
127 
128  default:
129  // Unknown access type
130  libmesh_error();
131  }
132 
133 
134  std::vector<ElemType> et;
135  hd->get_block_elt_types(et);
136 
137 
138  // Note: If DECODING or READING, allocate space in the vector
139  if ((m_type == DECODE) || (m_type == R_ASCII))
140  et.resize(hd->get_n_blocks());
141 
142 
143  switch (m_type)
144  {
145 
146 #ifdef LIBMESH_HAVE_XDR
147 
148  case (XdrMGF::ENCODE):
149  case (XdrMGF::DECODE):
150  {
151  xdr_vector(mp_xdr_handle,
152  (char *) &et[0],
153  et.size(),
154  sizeof(unsigned int),
155  (xdrproc_t) xdr_u_int);
156  break;
157  }
158 
159 #endif
160 
161  case (XdrMGF::W_ASCII):
162  {
163  for (unsigned int i=0; i<hd->get_n_blocks(); i++)
164  mp_out << et[i] << " ";
165 
166  mp_out << "\t # Element types in each block.\n";
167  break;
168  }
169 
170  case (XdrMGF::R_ASCII):
171  {
172  libmesh_assert (mp_in.good());
173 
174  for (unsigned int i=0; i<hd->get_n_blocks(); i++)
175  {
176  // convoluted way of doing it to
177  // satisfy icc
178  unsigned int type;
179 
180  mp_in >> type ;
181 
182  et[i] = static_cast<ElemType>(type) ;
183  }
184  mp_in.getline(comment, comm_len);
185  break;
186  }
187 
188  default:
189  // Unknown access type
190  libmesh_error();
191  }
192 
193 
194 
195  // Note: If DECODING or READING, you need to set the value
196  // in the header data structure.
197  if ((m_type == DECODE) || (m_type == R_ASCII))
198  hd->set_block_elt_types(et);
199 
200 
201  std::vector<unsigned int> neeb;
202  hd->get_num_elem_each_block(neeb);
203 
204  // If DECODING or READING, allocate space for the vector
205  if ((m_type == DECODE) || (m_type == R_ASCII))
206  neeb.resize( hd->get_n_blocks()*(this->get_num_levels()+1) );
207 
208  switch (m_type)
209  {
210 
211 #ifdef LIBMESH_HAVE_XDR
212 
213  case (XdrMGF::ENCODE):
214  case (XdrMGF::DECODE):
215  {
216  xdr_vector(mp_xdr_handle,
217  (char *) &neeb[0],
218  neeb.size(),
219  sizeof(unsigned int),
220  (xdrproc_t) xdr_u_int);
221  }
222 
223 #endif
224 
225  case (XdrMGF::W_ASCII):
226  {
227  for (unsigned int i=0; i<neeb.size(); i++)
228  mp_out << neeb[i] << " ";
229 
230  mp_out << "\t # Num. of elements in each block at each level.\n";
231  break;
232  }
233 
234  case (XdrMGF::R_ASCII):
235  {
236 
237  // We will treat this line as containing
238  // 1.) The number of elements in each block OR
239  // 2.) The number of elements at each level in each block
240  // Therefore, we don't know a-priori how many ints to read.
241 
242  // Get the full line from the stream up to the newline
243  mp_in.getline(comment, comm_len);
244 
245  // Construct a char buffer to hold the tokens as we
246  // process them, and construct a std::string object and
247  // a std::stringstream object for tokenizing this line.
248  char token[comm_len];
249  std::string s_temp(comment);
250  std::stringstream ss(s_temp);
251 
252  // Resize the neeb vector to zero so we can push back
253  // values onto it. Note that we are using a tokenizer
254  // scheme again here to read the line, but it's not entirely
255  // necessary since we know the size neeb should have.
256  neeb.resize(0);
257 
258  // Process the tokens one at a time
259  while (ss >> token)
260  {
261  // If you reach the hash, the rest of the line is a comment,
262  // so quit reading.
263  if (token[0] == '#')
264  break;
265 
266  // If you reach an alphabetic character, this is an error
267  if (!isdigit(token[0]))
268  {
269  libMesh::err << "Error: Unrecognized character detected."
270  << std::endl;
271  libmesh_error();
272  }
273 
274  // Otherwise, add the value to the neeb vector
275  neeb.push_back( std::atoi(token) );
276  }
277 
278  // Be sure you have the right number of entries in neeb
279  libmesh_assert_equal_to (neeb.size(), (hd->get_n_blocks() * (this->get_num_levels()+1)));
280 
281  break;
282  }
283 
284  default:
285  // Unknown access type
286  libmesh_error();
287  }
288 
289  if ((m_type == DECODE) || (m_type == R_ASCII))
290  hd->set_num_elem_each_block(neeb);
291  }
292 
293 
294  else if (orig_flag == 1) // MGF originator
295  {
296  }
297  else // Unknown Originator!
298  {
299  libmesh_error();
300  }
301 
302 
303 
304 
305  // Write the ID and TITLE strings (can be safely ignored)
306  switch (m_type)
307  {
308 
309 #ifdef LIBMESH_HAVE_XDR
310 
311  case (XdrMGF::ENCODE):
312  case (XdrMGF::DECODE):
313  {
314  char* temp = hd->cpyString(hd->getId());
315  xdr_string(mp_xdr_handle,&temp, ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize));
316  hd->setId(temp);
317  delete [] temp;
318 
319  temp = hd->cpyString(hd->getTitle());
320 
321  xdr_string(mp_xdr_handle,&temp, ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize));
322  hd->setTitle(temp);
323  delete [] temp;
324  break;
325  }
326 
327 #endif
328 
329  case (XdrMGF::W_ASCII):
330  {
331  mp_out << hd->mp_id << '\n';
332  mp_out << hd->mp_title << '\n';
333  break;
334  }
335 
336  case (XdrMGF::R_ASCII):
337  {
338  libmesh_assert (mp_in.good());
339 
340  mp_in.getline(comment, comm_len);
341  hd->setId(comment);
342 
343  libmesh_assert (mp_in.good());
344 
345  mp_in.getline(comment, comm_len);
346  hd->setTitle(comment);
347 
348  break;
349  }
350 
351  default:
352  // Unknown access type
353  libmesh_error();
354  }
355 
356  return 1;
357 }
358 
359 } // namespace libMesh

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

Hosted By:
SourceForge.net Logo