Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
Nektar::Utilities::OutputVtk Class Reference

Converter from fld to vtk. More...

#include <OutputVtk.h>

Inheritance diagram for Nektar::Utilities::OutputVtk:
Inheritance graph
[legend]
Collaboration diagram for Nektar::Utilities::OutputVtk:
Collaboration graph
[legend]

Public Member Functions

 OutputVtk (FieldSharedPtr f)
 
virtual ~OutputVtk ()
 
virtual void Process (po::variables_map &vm)
 Write fld to output file. More...
 
virtual std::string GetModuleName ()
 
 OutputVtk (MeshSharedPtr m)
 
virtual ~OutputVtk ()
 
virtual void Process ()
 Write mesh to output file. More...
 
- Public Member Functions inherited from Nektar::Utilities::OutputModule
 OutputModule (FieldSharedPtr p_f)
 
void OpenStream ()
 Open a file for output. More...
 
 OutputModule (MeshSharedPtr p_m)
 
void OpenStream ()
 
- Public Member Functions inherited from Nektar::Utilities::Module
 Module (FieldSharedPtr p_f)
 
void RegisterConfig (string key, string value)
 Register a configuration option with a module. More...
 
void PrintConfig ()
 Print out all configuration options for a module. More...
 
void SetDefaults ()
 Sets default configuration options for those which have not been set. More...
 
bool GetRequireEquiSpaced (void)
 
void SetRequireEquiSpaced (bool pVal)
 
void EvaluateTriFieldAtEquiSpacedPts (LocalRegions::ExpansionSharedPtr &exp, const Array< OneD, const NekDouble > &infield, Array< OneD, NekDouble > &outfield)
 
 Module (MeshSharedPtr p_m)
 
void RegisterConfig (std::string key, std::string value)
 
void PrintConfig ()
 
void SetDefaults ()
 
MeshSharedPtr GetMesh ()
 
virtual void ProcessVertices ()
 Extract element vertices. More...
 
virtual void ProcessEdges (bool ReprocessEdges=true)
 Extract element edges. More...
 
virtual void ProcessFaces (bool ReprocessFaces=true)
 Extract element faces. More...
 
virtual void ProcessElements ()
 Generate element IDs. More...
 
virtual void ProcessComposites ()
 Generate composites. More...
 
virtual void ClearElementLinks ()
 

Static Public Member Functions

static boost::shared_ptr< Modulecreate (FieldSharedPtr f)
 Creates an instance of this class. More...
 
static boost::shared_ptr< Modulecreate (MeshSharedPtr m)
 Creates an instance of this class. More...
 

Static Public Attributes

static ModuleKey m_className
 
static ModuleKey className
 

Additional Inherited Members

- Protected Member Functions inherited from Nektar::Utilities::Module
 Module ()
 
void ReorderPrisms (PerMap &perFaces)
 Reorder node IDs so that prisms and tetrahedra are aligned correctly. More...
 
void PrismLines (int prism, PerMap &perFaces, std::set< int > &prismsDone, std::vector< ElementSharedPtr > &line)
 
- Protected Attributes inherited from Nektar::Utilities::OutputModule
ofstream m_fldFile
 Output stream. More...
 
std::ofstream m_mshFile
 Output stream. More...
 
- Protected Attributes inherited from Nektar::Utilities::Module
FieldSharedPtr m_f
 Field object. More...
 
map< string, ConfigOptionm_config
 List of configuration values. More...
 
bool m_requireEquiSpaced
 
MeshSharedPtr m_mesh
 Mesh object. More...
 
std::map< std::string,
ConfigOption
m_config
 List of configuration values. More...
 

Detailed Description

Converter from fld to vtk.

Converter for Gmsh files.

Definition at line 48 of file FieldConvert/OutputModules/OutputVtk.h.

Constructor & Destructor Documentation

Nektar::Utilities::OutputVtk::OutputVtk ( FieldSharedPtr  f)
Nektar::Utilities::OutputVtk::~OutputVtk ( )
virtual

Definition at line 60 of file FieldConvert/OutputModules/OutputVtk.cpp.

61 {
62 }
Nektar::Utilities::OutputVtk::OutputVtk ( MeshSharedPtr  m)

Definition at line 56 of file NekMesh/OutputModules/OutputVtk.cpp.

56  : OutputModule(m)
57 {
58 }
virtual Nektar::Utilities::OutputVtk::~OutputVtk ( )
virtual

Member Function Documentation

static boost::shared_ptr<Module> Nektar::Utilities::OutputVtk::create ( MeshSharedPtr  m)
inlinestatic

Creates an instance of this class.

Definition at line 51 of file NekMesh/OutputModules/OutputVtk.h.

References Nektar::MemoryManager< DataType >::AllocateSharedPtr().

52  {
54  }
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
static boost::shared_ptr<Module> Nektar::Utilities::OutputVtk::create ( FieldSharedPtr  f)
inlinestatic

Creates an instance of this class.

Definition at line 52 of file FieldConvert/OutputModules/OutputVtk.h.

References Nektar::MemoryManager< DataType >::AllocateSharedPtr().

52  {
54  }
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
virtual std::string Nektar::Utilities::OutputVtk::GetModuleName ( )
inlinevirtual

Implements Nektar::Utilities::Module.

Definition at line 63 of file FieldConvert/OutputModules/OutputVtk.h.

64  {
65  return "OutputVtk";
66  }
void Nektar::Utilities::OutputVtk::Process ( po::variables_map &  vm)
virtual

Write fld to output file.

Implements Nektar::Utilities::Module.

Definition at line 64 of file FieldConvert/OutputModules/OutputVtk.cpp.

References ASSERTL0, ASSERTL1, Nektar::LibUtilities::ePtsFile, Nektar::LibUtilities::ePtsLine, Nektar::LibUtilities::ePtsPlane, Nektar::LibUtilities::ePtsTetBlock, Nektar::LibUtilities::ePtsTriBlock, Nektar::Utilities::Module::m_config, Nektar::Utilities::Module::m_f, Nektar::LibUtilities::NullPtsField, and Nektar::LibUtilities::PortablePath().

65 {
66  LibUtilities::PtsFieldSharedPtr fPts = m_f->m_fieldPts;
67 
68  // Do nothing if no expansion defined
69  if (fPts == LibUtilities::NullPtsField && !m_f->m_exp.size())
70  {
71  return;
72  }
73 
74  int i, j;
75  if (m_f->m_verbose)
76  {
77  if(m_f->m_comm->TreatAsRankZero())
78  {
79  cout << "OutputVtk: Writing file..." << endl;
80  }
81  }
82 
83  // Extract the output filename and extension
84  string filename = m_config["outfile"].as<string>();
85  string path;
86 
87  // amend for parallel output if required
88  if(m_f->m_session->GetComm()->GetSize() != 1)
89  {
90  int dot = filename.find_last_of('.');
91  string ext = filename.substr(dot,filename.length()-dot);
92  string start = filename.substr(0,dot);
93  path = start + "_vtu";
94 
95  boost::format pad("P%1$07d.vtu");
96  pad % m_f->m_session->GetComm()->GetRank();
97  filename = pad.str();
98 
99  fs::path poutfile(filename.c_str());
100  fs::path specPath(path.c_str());
101 
102  if(m_f->m_comm->TreatAsRankZero())
103  {
104  try
105  {
106  fs::create_directory(specPath);
107  }
108  catch (fs::filesystem_error& e)
109  {
110  ASSERTL0(false, "Filesystem error: " + string(e.what()));
111  }
112  cout << "Writing files to directory: " << specPath << endl;
113  }
114 
115  fs::path fulloutname = specPath / poutfile;
116  filename = LibUtilities::PortablePath(fulloutname);
117  m_f->m_comm->Block();
118  }
119  else
120  {
121  fs::path specPath(filename.c_str());
122  cout << "Writing: " << specPath << endl;
123  filename = LibUtilities::PortablePath(specPath);
124  }
125 
126  // Write solution.
127  ofstream outfile(filename.c_str());
128  m_f->m_exp[0]->WriteVtkHeader(outfile);
129  int nfields = 0;
130  int dim = 0;
131 
132  vector<string> fieldname;
133  if(fPts == LibUtilities::NullPtsField) // standard output in collapsed coordinates
134  {
135  dim = m_f->m_exp[0]->GetExp(0)->GetCoordim();
136 
137  int nstrips;
138  if (m_f->m_fielddef.size() == 0)
139  {
140  nfields = 0;
141  }
142  else
143  {
144  nfields = m_f->m_fielddef[0]->m_fields.size();
145  }
146  m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
147 
148  // Homogeneous strip variant
149  for(int s = 0; s < nstrips; ++s)
150  {
151  // For each field write out field data for each expansion.
152  for (i = 0; i < m_f->m_exp[0]->GetNumElmts(); ++i)
153  {
154  m_f->m_exp[0]->WriteVtkPieceHeader(outfile,i,s);
155 
156  // For this expansion write out each field.
157  for (j = 0; j < nfields; ++j)
158  {
159  m_f->m_exp[s*nfields+j]->WriteVtkPieceData(
160  outfile, i, m_f->m_fielddef[0]->m_fields[j]);
161  }
162  m_f->m_exp[0]->WriteVtkPieceFooter(outfile, i);
163  }
164  }
165 
166  // save field names for parallel output
167  for(i = 0; i < nfields; ++i)
168  {
169  fieldname.push_back(m_f->m_fielddef[0]->m_fields[i]);
170  }
171  }
172  else // write out data stored in fPts (for example if equispaced output is called).
173  {
174  int i = 0;
175  int j = 0;
176 
177  dim = fPts->GetDim();
178 
179  if(fPts->GetNpoints() == 0)
180  {
181  return;
182  }
183 
184  int nvert, vtktype;
185  switch(fPts->GetPtsType())
186  {
189  {
190  ASSERTL0(false,"VTK output needs settig up for PtsFile or Pts Line");
191  break;
192  }
194  {
195  ASSERTL0(false,"VTK output needs settig up for PtsPlane");
196  break;
197  }
199  {
200  nvert = 3;
201  vtktype = 5;
202  break;
203  }
205  {
206  nvert = 4;
207  vtktype = 10;
208  break;
209  }
210  default:
211  ASSERTL0(false, "ptsType not supported yet.");
212  }
213 
214  vector<Array<OneD, int> > ptsConn;
215  fPts->GetConnectivity(ptsConn);
216 
217  nfields = fPts->GetNFields();
218 
219  int nPts = fPts->GetNpoints();
220  int numBlocks = 0;
221  for(i = 0; i < ptsConn.size(); ++i)
222  {
223  numBlocks += ptsConn[i].num_elements()/nvert;
224  }
225 
226  // write out pieces of data.
227  outfile << " <Piece NumberOfPoints=\""
228  << nPts << "\" NumberOfCells=\""
229  << numBlocks << "\">" << endl;
230  outfile << " <Points>" << endl;
231  outfile << " <DataArray type=\"Float64\" "
232  << "NumberOfComponents=\""<<3<<"\" format=\"ascii\">" << endl;
233  for(i = 0; i < nPts; ++i)
234  {
235  for(j = 0; j < dim; ++j)
236  {
237  outfile << " " << setprecision(8) << scientific
238  << fPts->GetPointVal(j, i) << " ";
239  }
240  for(j = dim; j < 3; ++j) // pack to 3D since paraview does not seem to handle 2D
241  {
242  outfile << " 0.000000" ;
243  }
244  outfile << endl;
245  }
246  outfile << " </DataArray>" << endl;
247  outfile << " </Points>" << endl;
248  outfile << " <Cells>" << endl;
249  outfile << " <DataArray type=\"Int32\" "
250  << "Name=\"connectivity\" format=\"ascii\">" << endl;
251 
252  // dump connectivity data if it exists
253  outfile << " ";
254  int cnt = 1;
255  for(i = 0; i < ptsConn.size();++i)
256  {
257  for(j = 0; j < ptsConn[i].num_elements(); ++j)
258  {
259  outfile << ptsConn[i][j] << " ";
260  if( (!(cnt % nvert)) && cnt )
261  {
262  outfile << std::endl;
263  outfile << " ";
264  }
265  cnt ++;
266  }
267  }
268  outfile << " </DataArray>" << endl;
269  outfile << " <DataArray type=\"Int32\" "
270  << "Name=\"offsets\" format=\"ascii\">" << endl;
271 
272  outfile << " ";
273  for (i = 0; i < numBlocks; ++i)
274  {
275  outfile << i*nvert+nvert << " ";
276  }
277  outfile << endl;
278  outfile << " </DataArray>" << endl;
279  outfile << " <DataArray type=\"UInt8\" "
280  << "Name=\"types\" format=\"ascii\">" << endl;
281  outfile << " ";
282  for (i = 0; i < numBlocks; ++i)
283  {
284  outfile << vtktype <<" ";
285  }
286  outfile << endl;
287  outfile << " </DataArray>" << endl;
288  outfile << " </Cells>" << endl;
289  outfile << " <PointData>" << endl;
290 
291  // printing the fields
292  for(j = 0; j < nfields; ++j)
293  {
294  fieldname.push_back(fPts->GetFieldName(j));
295  outfile << " <DataArray type=\"Float64\" Name=\""
296  << fPts->GetFieldName(j) << "\">" << endl;
297  outfile << " ";
298  for(i = 0; i < fPts->GetNpoints(); ++i)
299  {
300  outfile << fPts->GetPointVal(dim+j, i) << " ";
301  }
302  outfile << endl;
303  outfile << " </DataArray>" << endl;
304  }
305 
306  outfile << " </PointData>" << endl;
307  outfile << " </Piece>" << endl;
308  }
309 
310  m_f->m_exp[0]->WriteVtkFooter(outfile);
311  cout << "Written file: " << filename << endl;
312 
313  // output parallel outline info if necessary
314  if(m_f->m_comm->GetRank() == 0)
315  {
316  ASSERTL1(fieldname.size() == nfields, "fieldname not the same size as nfields");
317  int nprocs = m_f->m_comm->GetSize();
318  if(nprocs != 1)
319  {
320  filename = m_config["outfile"].as<string>();
321  int dot = filename.find_last_of('.');
322  string body = filename.substr(0,dot);
323  filename = body + ".pvtu";
324 
325  ofstream outfile(filename.c_str());
326 
327  outfile << "<?xml version=\"1.0\"?>" << endl;
328  outfile << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" "
329  << "byte_order=\"LittleEndian\">" << endl;
330  outfile << "<PUnstructuredGrid GhostLevel=\"0\">" << endl;
331  outfile << "<PPoints> " << endl;
332  outfile << "<PDataArray type=\"Float64\" NumberOfComponents=\""
333  << 3 << "\"/> " << endl;
334  outfile << "</PPoints>" << endl;
335  outfile << "<PCells>" << endl;
336  outfile << "<PDataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\"/>" << endl;
337  outfile << "<PDataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\"/>" << endl;
338  outfile << "<PDataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\"/>" << endl;
339  outfile << "</PCells>" << endl;
340  outfile << "<PPointData Scalars=\"Material\">" << endl;
341  for(int i = 0; i < nfields; ++i)
342  {
343  outfile << "<PDataArray type=\"Float64\" Name=\"" <<
344  fieldname[i] << "\"/>" << endl;
345  }
346  outfile << "</PPointData>" << endl;
347 
348  for(int i = 0; i < nprocs; ++i)
349  {
350  boost::format pad("P%1$07d.vtu");
351  pad % i;
352  outfile << "<Piece Source=\"" << path << "/" << pad.str() << "\"/>" <<endl;
353  }
354  outfile << "</PUnstructuredGrid>" << endl;
355  outfile << "</VTKFile>" << endl;
356  cout << "Written file: " << filename << endl;
357  }
358  }
359 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:188
map< string, ConfigOption > m_config
List of configuration values.
FieldSharedPtr m_f
Field object.
boost::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:262
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41
static PtsFieldSharedPtr NullPtsField
Definition: PtsField.h:263
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
Definition: ErrorUtil.hpp:218
void Nektar::Utilities::OutputVtk::Process ( )
virtual

Write mesh to output file.

Implements Nektar::Utilities::Module.

Definition at line 64 of file NekMesh/OutputModules/OutputVtk.cpp.

References Nektar::iterator, Nektar::Utilities::Module::m_config, and Nektar::Utilities::Module::m_mesh.

65 {
66  if (m_mesh->m_verbose)
67  {
68  cout << "OutputVtk: Writing file..." << endl;
69  }
70 
71  vtkPolyData *vtkMesh = vtkPolyData::New();
72  vtkPoints *vtkPoints = vtkPoints::New();
73  vtkCellArray *vtkPolys = vtkCellArray::New();
74 
76 
77  std::set<NodeSharedPtr> tmp(m_mesh->m_vertexSet.begin(),
78  m_mesh->m_vertexSet.end());
79 
80  for (it = tmp.begin(); it != tmp.end(); ++it)
81  {
82  NodeSharedPtr n = *it;
83  vtkPoints->InsertPoint(n->m_id, n->m_x, n->m_y, n->m_z);
84  }
85 
86  vtkIdType p[8];
87  vector<ElementSharedPtr> &elmt = m_mesh->m_element[m_mesh->m_expDim];
88  for (int i = 0; i < elmt.size(); ++i)
89  {
90  int vertexCount = elmt[i]->GetVertexCount();
91  for (int j = 0; j < vertexCount; ++j)
92  {
93  p[j] = elmt[i]->GetVertex(j)->m_id;
94  }
95  vtkPolys->InsertNextCell(vertexCount, &p[0]);
96  }
97 
98  vtkMesh->SetPoints(vtkPoints);
99  vtkMesh->SetPolys(vtkPolys);
100 
101  // Write out the new mesh
102  vtkPolyDataWriter *vtkMeshWriter = vtkPolyDataWriter::New();
103  vtkMeshWriter->SetFileName(m_config["outfile"].as<string>().c_str());
104 #if VTK_MAJOR_VERSION <= 5
105  vtkMeshWriter->SetInput(vtkMesh);
106 #else
107  vtkMeshWriter->SetInputData(vtkMesh);
108 #endif
109  vtkMeshWriter->Update();
110 }
map< string, ConfigOption > m_config
List of configuration values.
MeshSharedPtr m_mesh
Mesh object.
boost::shared_ptr< Node > NodeSharedPtr
Definition: Node.h:50
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator

Member Data Documentation

ModuleKey Nektar::Utilities::OutputVtk::className
static
Initial value:

Definition at line 55 of file NekMesh/OutputModules/OutputVtk.h.

ModuleKey Nektar::Utilities::OutputVtk::m_className
static
Initial value:

Definition at line 55 of file FieldConvert/OutputModules/OutputVtk.h.