xdr_soln.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_soln.h"
20 #include "libmesh/xdr_shead.h"
21 
22 // System includes
23 #include <string.h>
24 
25 namespace libMesh
26 {
27 
28 
29 
30 // ------------------------------------------------------------
31 // XdrSOLN members
33 {
34 
35  switch (m_type)
36  {
37 
38 #ifdef LIBMESH_HAVE_XDR
39 
40  case (XdrMGF::ENCODE):
41  case (XdrMGF::DECODE):
42  {
43 
44  xdr_int(mp_xdr_handle, &(hd->m_wrtVar));
45  xdr_int(mp_xdr_handle, &(hd->m_numvar));
46  xdr_int(mp_xdr_handle, &(hd->m_numNodes));
47  xdr_int(mp_xdr_handle, &(hd->m_meshCnt));
48  xdr_int(mp_xdr_handle, &(hd->m_kstep));
49  xdr_int(mp_xdr_handle, &(hd->m_strSize));
50  xdr_REAL(mp_xdr_handle, &(hd->m_time));
51 
52  m_wrtVar=hd->m_wrtVar;
53 
54  char * temp = const_cast<char *>(hd->getId());
55  xdr_string(mp_xdr_handle,&(temp),
56  ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize));
57  hd->setId(temp);
58 
59  temp = const_cast<char *>(hd->getTitle());
60  xdr_string(mp_xdr_handle,&(temp),
61  ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize));
62  hd->setTitle(temp);
63 
64  temp = const_cast<char *>(hd->getUserTitle());
65  xdr_string(mp_xdr_handle,&(temp),
66  ((m_type == XdrMGF::ENCODE) ? std::strlen(temp) : hd->m_strSize));
67  hd->setUserTitle(temp);
68 
69 
70  char * tempTitle = new char[hd->m_strSize*m_wrtVar];
71 
72 
73  if (m_type == XdrMGF::DECODE)
74  {
75  xdr_string(mp_xdr_handle, &tempTitle, hd->m_strSize*m_wrtVar);
76  std::size_t olen= std::strlen(tempTitle);
77  char * top = tempTitle;
78  for (int ivar = 0; ivar < m_wrtVar; ++ivar)
79  {
80  char * p = strchr(tempTitle,' ');
81  *p = '\0';
82  std::size_t tempSize = std::strlen(tempTitle) ;
83  tempTitle+=tempSize+1;
84  }
85  tempTitle = top;
86  hd->mp_varTitle = new char[olen];
87  std::memcpy(hd->mp_varTitle,tempTitle,olen*sizeof(char));
88  }
89  else if (m_type == XdrMGF::ENCODE)
90  {
91  char * p = hd->mp_varTitle;
92  char * top = tempTitle;
93  for (int ivar = 0; ivar < m_wrtVar; ++ivar)
94  {
95  std::size_t tempSize = std::strlen(p) + 1;
96  std::memcpy(tempTitle,p,tempSize*sizeof(char));
97  tempSize = std::strlen(tempTitle);
98  tempTitle[tempSize] = ' ';
99  tempTitle += tempSize+1;
100  p += tempSize+1;
101  }
102  tempTitle = top;
103  xdr_string(mp_xdr_handle, &tempTitle, hd->m_strSize*m_wrtVar);
104  }
105  delete [] tempTitle;
106 
107  return 0;
108  }
109 #endif
110 
111 
112  case (XdrMGF::R_ASCII):
113  {
114  // Temporary variables to facilitate stream reading
115  const int comm_len= 80;
116  char comment[comm_len];
117 
118  libmesh_assert (mp_in.good());
119 
120  mp_in >> hd->m_numNodes ; mp_in.getline(comment, comm_len);
121  mp_in >> hd->m_wrtVar ; mp_in.getline(comment, comm_len);
122  mp_in >> hd->m_strSize ; mp_in.getline(comment, comm_len);
123  mp_in >> hd->m_time ; mp_in.getline(comment, comm_len);
124 
125  mp_in.getline(comment, comm_len);
126  hd->setId(comment);
127 
128  mp_in.getline(comment, comm_len);
129  hd->setTitle(comment);
130 
131  mp_in.getline(comment, comm_len);
132  hd->setUserTitle(comment);
133 
134  m_wrtVar = hd->m_wrtVar;
135 
136  // Read the variable names
137  {
138  std::string var_name;
139  char * titles = new char[hd->m_wrtVar*hd->m_strSize];
140  unsigned int c=0;
141 
142  for (int var=0; var < hd->m_wrtVar; var++)
143  {
144  mp_in >> var_name;
145 
146  for (unsigned int l=0; l<var_name.size(); l++)
147  titles[c++] = var_name[l];
148 
149  titles[c++] = '\0';
150  }
151 
152  mp_in.getline(comment, comm_len);
153 
154  hd->setVarTitle(titles, c);
155 
156  delete [] titles;
157  }
158 
159 
160  return 0;
161  }
162 
163 
164  case (XdrMGF::W_ASCII):
165  {
166  mp_out << hd->m_numNodes << "\t # Num. Nodes\n";
167  mp_out << hd->m_wrtVar << "\t # Num. of Vars\n";
168  mp_out << hd->m_strSize << "\t # String Size (ignore)\n";
169  mp_out << hd->m_time << "\t # Current Time\n";
170  mp_out << hd->mp_id << '\n';
171  mp_out << hd->mp_title << '\n';
172  mp_out << hd->mp_userTitle << '\n';
173 
174  // write the variable names
175  {
176  const char * p = hd->getVarTitle();
177 
178  for (int var=0; var<hd->m_wrtVar ; var++)
179  {
180  mp_out << p << " ";
181  p += std::strlen(p)+1;
182  }
183  mp_out << "\t # Variable Names\n";
184  }
185 
186  m_wrtVar = hd->m_wrtVar;
187 
188  return 0;
189  }
190 
191 
192 
193  default:
194  // Unknown access type
195  libmesh_error_msg("Unknown m_type" << m_type);
196  }
197 
198  return 1;
199 }
200 
201 } // namespace libMesh
const char * getVarTitle() const
Definition: xdr_shead.h:130
libmesh_assert(remote_elem)
int header(XdrSHEAD *hd)
Definition: xdr_soln.C:32
const char * getUserTitle() const
Definition: xdr_shead.h:118
const char * getTitle() const
Definition: xdr_head.h:67
std::ifstream mp_in
Definition: xdr_mgf.h:228
const char * getId() const
Definition: xdr_head.h:57
void setUserTitle(const char *title)
Definition: xdr_shead.h:113
char * mp_userTitle
Definition: xdr_head.h:178
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
char * mp_varTitle
Definition: xdr_head.h:183
void setTitle(const char *title)
Definition: xdr_head.h:62
xdr_Real m_time
Definition: xdr_head.h:188
void setVarTitle(const char *titles, int len)
Definition: xdr_shead.h:124
char * mp_title
Definition: xdr_head.h:173
XDR * mp_xdr_handle
Definition: xdr_mgf.h:210