Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Neutral_Reader.h
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: Neutral_Reader.h,v 1.4 2008/12/06 08:43:24 mtcampbe Exp $
24 
25 #ifndef NEUTRAL_READER_H
26 #define NEUTRAL_READER_H
27 
28 #include "../Rocsurf/include/surfbasic.h"
29 #include <vector>
30 #include <string>
31 #include <fstream>
32 #include <cstdlib>
33 #include <cstdio>
34 
36 
37 // This class provides an interface for reading in a surface mesh
38 // from the ".smf" or ".obj" file.
40 public:
41  // Constructor from an MPI communicator and a scale factor.
43 
44  // Assuming the window has been created, read in the local panes from
45  // given file and return the number of local panes.
46  int read_mesh( const char *fname, const std::string &wname) {
47  std::ifstream is( fname);
48  if ( is == NULL) {
49  std::cerr << "Error: Could not open file " << fname << std::endl;
50  exit(-1);
51  }
52 
53  // Create the window if not yet exist
54  int h=COM_get_window_handle(wname.c_str());
55  if ( h<=0) COM_new_window( wname.c_str());
56 
57  std::cout << "Reading file " << fname << std::endl;
58 
59  // Search for NUMNP
60  do {
61  get_nextline( is, buf);
62  } while ( !is.eof() && std::string(buf).find( "NUMNP")==std::string::npos);
63 
64  std::cerr << "Buf is now \"" << buf << "\" before getting nn" << std::endl;
65  get_nextline( is, buf);
66  int t1, t2, t3, t4, nn, ne;
67  std::sscanf( buf, "%d %d %d %d %d %d", &nn, &ne, &t1, &t2, &t3, &t4);
68 
69  // Search for NODAL COORDINAES
70  do {
71  get_nextline( is, buf);
72  } while ( !is.eof() && std::string(buf).find( "NODAL")==std::string::npos);
73 
74  std::cout << "Reading " << nn << " nodes and "
75  << ne << " triangles " << std::endl;
76  read_pane_coors( is, wname, nn);
77 
78  // Search for ELEMENTS
79  do {
80  get_nextline( is, buf);
81  } while ( !is.eof() && std::string(buf).find( "ELEMENTS")==std::string::npos);
82  read_pane_elems( is, wname, ne);
83 
84  return 1;
85  }
86 
87 private:
88  void get_nextline( std::istream &is, char *str) {
89  str[0] = '\0';
90  if ( is.eof()) { return; }
91  is.getline( str, MAXLEN);
92  }
93 
94  // Read in coordinates of a pane into a window if local
95  void read_pane_coors( std::istream &is, const std::string &wname, int nn) {
96  COM_set_size( (wname+".nc").c_str(), 1, nn);
97  SURF::Point_3<double> *ps;
98  COM_allocate_array( (wname+".nc").c_str(), 1, &(void*&)ps);
99 
100  for ( int i=0; i<nn; ++i) {
101  get_nextline( is, buf);
102  int t1;
103 
104  std::sscanf( buf, "%d %lf %lf %lf", &t1, &ps[i][0], &ps[i][1], &ps[i][2]);
105  COM_assertion( t1==i+1);
106  }
107  }
108 
109  // Read in element connectivity of a pane into a window if local
110  void read_pane_elems( std::istream &is, const std::string &wname, int ne){
111  // Note: Only supports triangular meshes now.
112  COM_set_size( (wname+".:t3:").c_str(), 1, ne);
113  SURF::Vector_3<int> *es;
114  COM_allocate_array( (wname+".:t3:").c_str(), 1, &(void*&)es);
115 
116  for (int i=0; i<ne; ++i) {
117  int t1, t2, t3;
118 
119  get_nextline( is, buf);
120  std::sscanf( buf, "%d %d %d %d %d %d", &t1, &t2, &t3,
121  &es[i][0], &es[i][1], &es[i][2]);
122  COM_assertion( t1 == i+1 && t2==3 && t3==3);
123  }
124  }
125 
126 private:
127  enum {MAXLEN=255};
128  char buf[MAXLEN+1];
129 };
130 
131 #endif
132 
133 
134 
135 
136 
137 
#define COM_assertion(EX)
Error checking utility similar to the assert macro of the C language.
void COM_set_size(const char *wa_str, int pane_id, int size, int ng=0)
Set sizes of for a specific attribute.
Definition: roccom_c++.h:136
void read_pane_elems(std::istream &is, const std::string &wname, int ne)
int read_mesh(const char *fname, const std::string &wname)
char buf[MAXLEN+1]
void get_nextline(std::istream &is, char *str)
Definition: Rocin.h:64
blockLoc i
Definition: read.cpp:79
void COM_new_window(const char *wname, MPI_Comm c=MPI_COMM_NULL)
Definition: roccom_c++.h:86
void COM_allocate_array(const char *wa_str, int pane_id=0, void **addr=NULL, int strd=0, int cap=0)
Allocate space for an attribute on a specific pane and return the address by setting addr...
Definition: roccom_c++.h:196
int COM_get_window_handle(const char *wname)
Definition: roccom_c++.h:404
void read_pane_coors(std::istream &is, const std::string &wname, int nn)
#define COM_EXTERN_MODULE(moduleName)
Definition: roccom_basic.h:116