Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeshVTK.C
Go to the documentation of this file.
1 
2 #include "MeshVTK.H"
3 
4 namespace Mesh {
5  /*
6  * @brief Writes VTK files for the corresponding meshes.
7  * @param meshes List of meshes in the current window.
8  */
9  void writeVtkFiles( std::vector< Mesh::UnstructuredMesh > &meshes )
10  {
11 
12  // TODO: Generalize this function for any type of mesh.
13  for( int m=0; m < meshes.size( ); ++m )
14  {
15  std::ostringstream oss; oss.clear( );
16  oss << Program.outputfile << "." << Program.paneIds[ m ] << ".vtk";
17 
18  std::ofstream ofs;
19  ofs.open( oss.str( ).c_str( ) );
20 
21  if( !ofs.is_open( ) )
22  {
23  std::cerr << "Cannot write VTK file: " << oss.str( ) << std::endl;
24  std::cerr << "File: " << __FILE__ << std::endl;
25  std::cerr << "Line: " << __LINE__ << std::endl;
26  assert( false );
27  }
28 
29  ofs << "# vtk DataFile Version 3.0\n";
30  ofs << oss.str( ) << std::endl;
31  ofs << "ASCII\n";
32  ofs << "DATASET UNSTRUCTURED_GRID\n";
33  ofs << "POINTS " << meshes[ m ].nc.Size( ) << " double\n";
34 
35  for( int i=1; i <= meshes[ m ].nc.Size( ); ++i )
36  {
37  ofs << meshes[ m ].nc.x( i ) << " ";
38  ofs << meshes[ m ].nc.y( i ) << " ";
39  ofs << meshes[ m ].nc.z( i ) << "\n";
40  }
41 
42  ofs << "CELLS " << meshes[ m ].con.Nelem( ) << " " << meshes[ m ].con.Nelem( )*9 << "\n";
43  for( int e=1; e <= meshes[ m ].con.Nelem( ); ++e )
44  {
45  ofs << "8 ";
46  for( int j=1; j <= 8; ++j )
47  ofs << meshes[ m ].con.Node( e, j )-1<< " ";
48  ofs << std::endl;
49  }
50 
51  ofs << "CELL_TYPES " << meshes[ m ].con.Nelem( ) << std::endl;
52  for( int e=1; e <= meshes[ m ].con.Nelem( ); ++ e )
53  ofs << "12\n";
54 
55  ofs << "POINT_DATA " << Program.mask[ m ].size( ) << std::endl;
56  ofs << "SCALARS SharedNodes double\nLOOKUP_TABLE default\n";
57  for( int i=0; i < Program.mask[ m ].size( ); ++i )
58  {
59  if( Program.mask[ m ][ i ] )
60  ofs << "1\n";
61  else
62  ofs << "0\n";
63  }
64  ofs.close( );
65  }
66  }
73  void printVtk( std::vector< double > &vlist, std::vector< std::vector< int > > &elist )
74  {
75  std::ofstream ofs;
76  ofs.open( std::string( "global_mesh.vtk" ).c_str( ) );
77 
78  if( !ofs.is_open( ) )
79  {
80  std::cerr << "Cannot write global VTK file! " << std::endl;
81  std::cerr << "File: " << __FILE__ << std::endl;
82  std::cerr << "Line: " << __LINE__ << std::endl;
83  assert( false );
84  }
85 
86  ofs << "# vtk DataFile Version 3.0\n";
87  ofs << "Global Mesh Output" << std::endl;
88  ofs << "ASCII\n";
89  ofs << "DATASET UNSTRUCTURED_GRID\n";
90  ofs << "POINTS " << vlist.size( )/3 << " double\n";
91 
92  for( int i=0; i < vlist.size( )/3; ++i )
93  {
94  ofs << vlist[ i*3 ] << " ";
95  ofs << vlist[ i*3+1 ] << " ";
96  ofs << vlist[ i*3+2 ] << "\n";
97  }
98 
99  int maxnodes = 0; /* the maximum number of nodes per element */
100  for( int i=0; i < elist.size( ); ++i )
101  {
102  if( maxnodes < elist[ i ].size( ) )
103  maxnodes = elist[ i ].size( );
104  }
105 
106  ofs << "CELLS " << elist.size( ) << " " << elist.size( )*( maxnodes+1) << "\n";
107  for( int i=0; i < elist.size( ); ++i )
108  {
109  ofs << elist[ i ].size( ) << " ";
110  for( int j=0; j < elist[ i ].size( ); ++j )
111  ofs << elist[ i ][ j ]<< " ";
112  ofs << std::endl;
113  }
114 
115  ofs << "CELL_TYPES " << elist.size( ) << std::endl;
116  for( int i=0; i < elist.size( ); ++i )
117  {
118 
119  if( elist[ i ].size( ) == 8 )
120  ofs << "12\n";
121  else if( elist[ i ].size( ) == 4 )
122  ofs << "10\n";
123  else {
124  std::cerr << "Undefined element type!\n";
125  std::cerr << "File: " << __FILE__ << std::endl;
126  std::cerr << "Line: " << __LINE__ << std::endl;
127  }
128 
129  }
130  ofs.close( );
131  }
143  void writeVtkData( NodalCoordinates &nc,Connectivity &con,const std::string &filename,std::vector<double> &soln)
144  {
145 
146  std::cout << "Writing VTK data...";
147  std::cout.flush( );
148 
149  std::ofstream ofs;
150  ofs.open( filename.c_str());
151 
152  if( !ofs.is_open( ) )
153  {
154  std::cerr << "Cannot write VTK file: " << filename << std::endl;
155  std::cerr << "File: " << __FILE__ << std::endl;
156  std::cerr << "Line: " << __LINE__ << std::endl;
157  assert( false );
158  }
159 
160  unsigned int nnodes = nc.NNodes();
161  /* STEP 1: Write the header */
162  ofs << "# vtk DataFile Version 3.0\n";
163  ofs << "Global Mesh Output" << std::endl;
164  ofs << "ASCII\n";
165  ofs << "DATASET UNSTRUCTURED_GRID\n";
166  ofs << "POINTS " << nnodes << " double\n";
167 
168  /* STEP 2: Write the nodes */
169  for( int i=1; i < nnodes; ++i )
170  {
171  ofs << GeoPrim::C3Point(&(nc[i])) << std:endl;
172  }
173 
174  /* STEP 3: Write the max element size */
175  int maxnodes = 0; /* the maximum number of nodes per element */
176  unsigned int nelem = con.size();
177  con.SyncSizes();
178  for( int i=0; i < nelem; ++i )
179  {
180  unsigned int esize = con[i].size();
181  if(maxnodes < esize)
182  maxnodes = esize;
183  }
184 
185  /* STEP 4: Write the element connectivity */
186  ofs << "CELLS " << nelem << " " << nelem*( maxnodes+1) << "\n";
187  for( int i=0; i < nelem; ++i )
188  {
189  ofs << con.Esize(i) << " ";
190  for( int j=0; j < con.Esize(i); ++j )
191  ofs << con[ i ][ j ]-1 << " ";
192  ofs << std::endl;
193  }
194 
195  /* STEP 5: Write the cell types */
196  ofs << "CELL_TYPES " << nelem << std::endl;
197  for( int i=0; i < nelem; ++i )
198  {
199 
200  if( con.Esize(i) == 8 )
201  ofs << "12\n";
202  else if( con.Esize(i) == 4 )
203  ofs << "10\n";
204  else {
205  std::cerr << "Undefined element type!\n";
206  std::cerr << "File: " << __FILE__ << std::endl;
207  std::cerr << "Line: " << __LINE__ << std::endl;
208  }
209 
210  }
211 
212  /* STEP 6: Write the solution data attached to the points */
213  bool wroteHeader = false;
214  std::vector<std::string> Name(5);
215  Name[0] = "rho";
216  Name[1] = "rho-u";
217  Name[2] = "rho-v";
218  Name[3] = "rho-w";
219  Name[4] = "rho-E";
220  if(!soln.empty()){
221  for( int i=0; i < 5; ++i )
222  {
223  if( !wroteHeader )
224  {
225  ofs << "CELL_DATA " << nelem << std::endl;
226  wroteHeader = true;
227  }
228  ofs << "SCALARS " << Name[i] << " double" << std::endl;
229  ofs << "LOOKUP_TABLE default\n";
230  for( int j=0; j < nelem; ++j )
231  {
232  ofs << soln[ i*nelem + j ] << " ";
233  }
234  ofs << std::endl;
235  }
236  }
237  ofs.close( );
238  std::cout << "[DONE]\n";
239  }
240 }
std::vector< Mesh::UnstructuredMesh > meshes
The list of grids to write.
Definition: hdf2pltV2.C:61
void writeVtkFiles(std::vector< Mesh::UnstructuredMesh > &m)
Writes the set of meshes into separate VTK files that can be visualized with Paraview.
General connectivity object.
Definition: Mesh.H:334
A struct that holds all the global program data.
Class Mesh is the main class that holds all information to describe the current state of the mesh...
Definition: Mesh.hpp:19
IndexType Esize(IndexType n) const
Definition: Mesh.H:365
blockLoc i
Definition: read.cpp:79
int NNodes() const
Definition: Mesh.H:278
void writeVtkData()
Writes the merged grid and its data in VTK UNSTRUCTURED_GRID file format.
Definition: hdf2pltV2.C:645
void SyncSizes()
Definition: Mesh.C:281
j indices j
Definition: Indexing.h:6
C3Vector C3Point
void printVtk(std::vector< double > &vlist, std::vector< std::vector< int > > &elist)
This method prints the global grid in VTK file format.