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