Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RFC_Window_base_IO_binary.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // $Id: RFC_Window_base_IO_binary.C,v 1.5 2008/12/06 08:43:28 mtcampbe Exp $
24 
25 #include "RFC_Window_base.h"
26 #include <cstdio>
27 #include <cstdlib>
28 #include <fstream>
29 #include <iostream>
30 #include <cassert>
31 
33 
34 inline void
35 write(std::ostream& os, const int t) {
36  os.write((char*)&t, sizeof(int));
37 }
38 
39 inline void
40 read(std::istream& is, int& t) {
41  is.read((char*)&t, sizeof(int));
42 }
43 
44 inline void
45 swap_endian( short int &t) {
46  char *c = (char*)&t;
47  std::swap( c[0],c[1]);
48 }
49 
50 inline void
51 swap_endian( int &t) {
52  char *c = (char*)&t;
53  std::swap( c[0],c[3]); std::swap( c[1],c[2]);
54 }
55 
56 inline void
57 swap_endian( float &t) {
58  char *c = (char*)&t;
59  std::swap( c[0],c[3]); std::swap( c[1],c[2]);
60 }
61 
62 inline void
63 swap_endian( double &t) {
64  char *c = (char*)&t;
65  std::swap( c[0],c[7]); std::swap( c[1],c[6]);
66  std::swap( c[2],c[5]); std::swap( c[3],c[4]);
67 }
68 
69 // Write the subdivision in binary format.
70 void RFC_Pane_base::
71 write_binary( std::ostream &os) const {
72  RFC_assertion( sizeof(int)==4);
73  RFC_assertion( sizeof(Real)==8);
74 
75  write( os, int(1)); // For detecting big or small endian
76  Real three = 3.; // Version number (New version changed on 04/11/04)
77  os.write( (char *)&three, sizeof(Real));
78 
79  // Write out color and the id of the pane.
80  write( os, _color); write( os, id());
81 
82  // #nodes and #faces
83  write( os, size_of_nodes()); write( os, size_of_faces());
84 
85  // Nodal coordinates of the pane
86  const Real *p = _base->coordinates();
87  os.write( (const char*)p, 3*_base->size_of_nodes()*sizeof(Real));
88 
89  if ( _base->is_structured()) {
90  write( os, 1);
91  write( os, 2); write( os, 1);
92  write( os, _base->size_i()); write( os, _base->size_j());
93  }
94  else {
95  std::vector< const COM::Connectivity*> elems;
96  _base->elements( elems);
97  write( os, elems.size());
98  // Output sizes of the faces
99  for ( std::vector<const COM::Connectivity*>::const_iterator
100  it = elems.begin(); it != elems.end(); ++it) {
101  write( os, (*it)->size_of_nodes_pe());
102  write( os, (*it)->size_of_elements());
103 
104  const int *e = (*it)->pointer();
105  const int nn = (*it)->size_of_nodes_pe();
106  os.write( (const char*)e, (*it)->size_of_elements()*nn*sizeof(int));
107  }
108  }
109 
110  // Pane connectivity of input mesh
111  write( os, _b2v_table.size());
112  for ( B2v_table::const_iterator
113  it=_b2v_table.begin(), iend=_b2v_table.end(); it != iend; ++it) {
114  const B2v &b2v = it->second;
115 
116  int n=b2v.size();
117  write( os, it->first); write( os, n);
118  os.write( (const char*)&b2v[0], n*sizeof(b2v[0]));
119  }
120  write ( os, _v2b_table.size());
121  for ( V2b_table::const_iterator
122  it = _v2b_table.begin(), iend=_v2b_table.end(); it!=iend; ++it) {
123  write( os, it->first);
124  write( os, it->second.first);
125  write( os, it->second.second);
126  }
127 
128  write(os, _subnode_parents.size()); write(os, _subfaces.size());
129 
130  // Output subnodes.
131  {
132  int n=_subnode_parents.size();
133  RFC_assertion( sizeof(_subnode_parents[0])==8);
134  os.write( (const char*)&_subnode_parents[0],
135  n*sizeof(_subnode_parents[0]));
136  os.write( (const char*)&_subnode_nat_coors[0],
137  n*sizeof(_subnode_nat_coors[0]));
138  os.write( (const char*)&_subnode_counterparts[0],
139  n*sizeof(_subnode_counterparts[0]));
140  }
141 
142  // Output subfaces.
143  {
144  int n = _subfaces.size();
145  os.write( (const char*)&_subfaces[0],
146  n*sizeof(_subfaces[0]));
147  os.write( (const char*)&_subface_parents[0],
148  n*sizeof(_subface_parents[0]));
149  os.write( (const char*)&_subface_counterparts[0],
150  n*sizeof(_subface_counterparts[0]));
151  }
152 
153  // Offsets of sub-faces.
154  RFC_assertion( int(_subface_offsets.size()) == int(size_of_faces())+1);
155  os.write( (const char*)&_subface_offsets[0],
156  sizeof(int)*_subface_offsets.size());
157 
158  write( os, _subnode_parents.size()+_subfaces.size()); // Checksum
159 }
160 
161 // Read in RFC_Pane_base in binary format.
162 void RFC_Pane_base::
163 read_binary( std::istream &is, std::vector <int> *b2v_all, COM::Pane *pn_in) {
164 
165  COM_Pane_friend *pn = (COM_Pane_friend*)pn_in;
166  RFC_assertion( sizeof(int)==4);
167  RFC_assertion( sizeof(Real)==8);
168 
169  int t1, t2;
170 
171  read( is, t1); // For detecting big or small endian
172  bool need_swap = (t1 != 1);
173  if ( need_swap) swap_endian( t1);
174  RFC_assertion( t1 == 1);
175  Real version;
176  is.read( (char*)&version, sizeof(Real));
177  if ( need_swap) swap_endian( version);
178  RFC_assertion( version == 3.);
179 
180  // Read in the color and the id of the pane.
181  read( is, t1); read( is, t2);
182  if ( need_swap) { swap_endian( t1); swap_endian( t2); }
183 
184  // #nodes and #faces
185  read( is, t1); read( is, t2);
186  if ( need_swap) { swap_endian( t1); swap_endian( t2); }
187  if ( pn != NULL) pn->set_size( pn->attribute(COM::COM_NC), t1, 0);
188  else
189  RFC_assertion( t1 == size_of_nodes() && t2 == size_of_faces());
190  _subface_offsets.resize( t2);
191 
192  // Nodal coordinates of the pane
193  {
194  int n=3*_base->size_of_nodes();
195  std::vector< Real> buf( n);
196  is.read( (char*)&buf.front(), n*sizeof(Real));
197  }
198 
199  // Read in number of element types.
200  int ntypes, is_structured=false;
201  read( is, ntypes); if ( need_swap) { swap_endian( ntypes); }
202  for ( int i=0; i<ntypes; ++i) {
203  read( is, t1); read( is, t2);
204  if ( need_swap) { swap_endian( t1); swap_endian( t2); }
205 
206  if ( t1 == 2) { // Structured mesh
207  is_structured=true;
208  RFC_assertion( t2==1);
209  int dims[2];
210  read( is, dims[0]); read( is, dims[1]);
211  if ( need_swap) { swap_endian( dims[0]); swap_endian( dims[1]); }
212  if ( pn != NULL) {
213  int *t = &dims[0];
214  COM::Connectivity *conn = pn->connectivity( ":st2:");
215  pn->reinit_conn( conn, COM::Pane::OP_SET, &t, 0, 0);
216  }
217  }
218  else { // Unstructured mesh
219  int n=t1*t2;
220  if ( pn) {
221  int *buf=NULL;
222  std::string elem;
223  // Create a new Connectivity object in the Pane
224  switch (t1) {
225  case 3: elem=":t3:"; break;
226  case 4: elem=":q4:"; break;
227  case 6: elem=":t6:"; break;
228  case 8: elem=":q8:"; break;
229  case 9: elem=":q9:"; break;
230  default: RFC_assertion(false);
231  }
232  // Insert a connectivity
233  COM::Connectivity *conn=pn->connectivity( elem);
234  pn->set_size( conn, t2, 0);
235  pn->reinit_conn( conn, COM::Pane::OP_RESIZE, &buf, 0, 0);
236 
237  is.read( (char*)buf, n*sizeof(int));
238  if ( need_swap) for ( int j=0; j<n; ++j) swap_endian( buf[j]);
239  }
240  else {
241  int t;
242  for ( int i=0; i<n; ++i) is.read( (char*)&t, sizeof(int));
243  }
244  }
245  }
246 
247  // Pane connectivity of input mesh
248  read( is, t1); t2 = size_of_nodes();
249  if ( need_swap) { swap_endian( t1); }
250  if ( b2v_all) b2v_all->reserve(t1);
251  for ( int i=0; i<t1; ++i) {
252  int pane_id, n;
253  read( is, pane_id); read( is, n);
254  if ( need_swap) { swap_endian( pane_id); swap_endian( n); }
255  B2v &b2v = _b2v_table[pane_id];
256  b2v.resize( n);
257  is.read( (char*)&b2v[0], n*sizeof(b2v[0]));
258  if ( need_swap) for ( int j=0; j<n; ++j) swap_endian( b2v[j]);
259  RFC_assertion_code( for ( int j=0; j<n; ++j)
260  RFC_assertion( b2v[j]>=1 && b2v[j]<=t2));
261  if ( b2v_all) {
262  b2v_all->push_back( pane_id); b2v_all->push_back( n);
263  b2v_all->insert( b2v_all->end(), b2v.begin(), b2v.end());
264  }
265  }
266 
267  read( is, t1); if ( need_swap) { swap_endian( t1); }
268  for ( int i=0; i<t1; ++i) {
269  int n1,n2,n3;
270  read( is, n1); if ( need_swap) { swap_endian( n1); }
271  read( is, n2); if ( need_swap) { swap_endian( n2); }
272  read( is, n3); if ( need_swap) { swap_endian( n3); }
273 
274  _v2b_table[n1] = std::make_pair( n2, n3);
275  }
276 
277  read( is, t1); read( is, t2);
278  if ( need_swap) { swap_endian( t1); swap_endian( t2); }
279  _subnode_parents.resize( t1);
280  _subnode_nat_coors.resize( t1);
281  _subnode_counterparts.resize( t1);
282  _subfaces.resize( t2);
283  _subface_parents.resize( t2);
284  _subface_counterparts.resize( t2);
285 
286  // Read in the subnodes.
287  int n=_subnode_parents.size();
288  RFC_assertion( sizeof(_subnode_parents[0])==8);
289  is.read( (char*)&_subnode_parents[0], n*sizeof(_subnode_parents[0]));
290  if ( need_swap)
291  for ( int j=0; j<n; ++j) {
292  swap_endian( _subnode_parents[j].face_id);
293  swap_endian( _subnode_parents[j].edge_id);
294  }
295 
296  is.read( (char*)&_subnode_nat_coors[0],
297  n*sizeof(_subnode_nat_coors[0]));
298  if ( need_swap)
299  for ( int j=0; j<n; ++j) {
302  }
303 
304  is.read( (char*)&_subnode_counterparts[0],
305  n*sizeof(_subnode_counterparts[0]));
306  if ( need_swap)
307  for ( int j=0; j<n; ++j) {
310  }
311 
312  n = t2;
313  // Read in the subfaces.
314  is.read( (char*)&_subfaces[0], n*sizeof(_subfaces[0]));
315  if ( need_swap)
316  for ( int j=0; j<n; ++j) {
317  swap_endian( _subfaces[j][0]);
318  swap_endian( _subfaces[j][1]);
319  swap_endian( _subfaces[j][2]);
320  }
321  is.read( (char*)&_subface_parents[0], n*sizeof(_subface_parents[0]));
322  if ( need_swap)
323  for ( int j=0; j<n; ++j) {
325  }
326  is.read( (char*)&_subface_counterparts[0],
327  t2*sizeof(_subface_counterparts[0]));
328  if ( need_swap)
329  for ( int j=0; j<n; ++j) {
332  }
333 
334  // Read in the offset of sub-faces.
335  _subface_offsets.resize( size_of_faces()+1);
336  n = _subface_offsets.size();
337  is.read( (char*)&_subface_offsets[0], sizeof(int)*n);
338  if ( need_swap) for ( int j=0; j<n; ++j) swap_endian( _subface_offsets[j]);
339 
341  int(_subfaces.size()));
342 
343  read( is, t1);
344  if ( need_swap) { swap_endian( t1); }
345  RFC_assertion( t1 ==int(_subnode_parents.size()+_subfaces.size())); // Checksum
346 
347  comp_nat_coors();
348 }
349 
350 
352 
353 
354 
355 
356 
357 
void swap(int &a, int &b)
Definition: buildface.cpp:88
void write_binary(std::ostream &os) const
void comp_nat_coors()
Compute the natural coordinates.
Base * _base
Reference to its base object.
int size_of_faces() const
The total number of faces in the pane.
std::vector< Three_tuple< int > > _subfaces
std::vector< Edge_ID > _subnode_parents
Edge ids of parents.
#define RFC_END_NAME_SPACE
Definition: rfc_basic.h:29
void read_binary(std::istream &is, std::vector< int > *b2v_all=NULL, COM::Pane *p=NULL)
**********************************************************************Rocstar Simulation Suite Illinois Rocstar LLC All rights reserved ****Illinois Rocstar LLC IL **www illinoisrocstar com **sales illinoisrocstar com WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **Arising OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE **********************************************************************INTERFACE SUBROUTINE knode iend
void write(std::ostream &os, const T &t, const io_Read_write &)
Definition: io.h:96
int size_of_nodes() const
The total number of nodes in the pane.
blockLoc i
Definition: read.cpp:79
#define RFC_BEGIN_NAME_SPACE
Definition: rfc_basic.h:28
const NT & n
B2v_table _b2v_table
std::vector< Node_ID > _subnode_counterparts
Ids of counterparts of subnodes.
#define RFC_assertion_code
Definition: rfc_basic.h:68
j indices j
Definition: Indexing.h:6
int _color
Is a node on border?
std::vector< int > _subface_parents
Face ids of the parents of the subfaces.
std::vector< int > _subface_offsets
Offsets of first subfaces contained in a face.
void read(std::istream &is, T &t, const io_Read_write &)
Definition: io.h:132
V2b_table _v2b_table
std::vector< Point_2S > _subnode_nat_coors
Natual coordinates in the parent face.
void swap_endian(short int &t)
std::vector< int > B2v
From local boundary ids to node ids.
#define RFC_assertion
Definition: rfc_basic.h:65
std::vector< Face_ID > _subface_counterparts
Ids of counterparts of faces.