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

Converter from fld to vtk. More...

#include <OutputVtk.h>

Inheritance diagram for Nektar::FieldUtils::OutputVtk:
Inheritance graph
[legend]
Collaboration diagram for Nektar::FieldUtils::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...
 
void WriteEmptyVtkPiece (std::ofstream &outfile)
 
virtual std::string GetModuleName ()
 
- Public Member Functions inherited from Nektar::FieldUtils::OutputModule
 OutputModule (FieldSharedPtr p_f)
 
FIELD_UTILS_EXPORT void OpenStream ()
 Open a file for output. More...
 
- Public Member Functions inherited from Nektar::FieldUtils::Module
FIELD_UTILS_EXPORT Module (FieldSharedPtr p_f)
 
FIELD_UTILS_EXPORT void RegisterConfig (string key, string value)
 Register a configuration option with a module. More...
 
FIELD_UTILS_EXPORT void PrintConfig ()
 Print out all configuration options for a module. More...
 
FIELD_UTILS_EXPORT void SetDefaults ()
 Sets default configuration options for those which have not been set. More...
 
FIELD_UTILS_EXPORT bool GetRequireEquiSpaced (void)
 
FIELD_UTILS_EXPORT void SetRequireEquiSpaced (bool pVal)
 
FIELD_UTILS_EXPORT void EvaluateTriFieldAtEquiSpacedPts (LocalRegions::ExpansionSharedPtr &exp, const Array< OneD, const NekDouble > &infield, Array< OneD, NekDouble > &outfield)
 

Static Public Member Functions

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

Static Public Attributes

static ModuleKey m_className
 

Additional Inherited Members

- Protected Member Functions inherited from Nektar::FieldUtils::Module
 Module ()
 
- Protected Attributes inherited from Nektar::FieldUtils::OutputModule
ofstream m_fldFile
 Output stream. More...
 
- Protected Attributes inherited from Nektar::FieldUtils::Module
FieldSharedPtr m_f
 Field object. More...
 
map< string, ConfigOptionm_config
 List of configuration values. More...
 
bool m_requireEquiSpaced
 

Detailed Description

Converter from fld to vtk.

Definition at line 48 of file library/FieldUtils/OutputModules/OutputVtk.h.

Constructor & Destructor Documentation

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

Definition at line 58 of file library/FieldUtils/OutputModules/OutputVtk.cpp.

59 {
60 }

Member Function Documentation

static boost::shared_ptr<Module> Nektar::FieldUtils::OutputVtk::create ( FieldSharedPtr  f)
inlinestatic

Creates an instance of this class.

Definition at line 52 of file library/FieldUtils/OutputModules/OutputVtk.h.

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

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

Implements Nektar::FieldUtils::Module.

Definition at line 66 of file library/FieldUtils/OutputModules/OutputVtk.h.

67  {
68  return "OutputVtk";
69  }
void Nektar::FieldUtils::OutputVtk::Process ( po::variables_map &  vm)
virtual

Write fld to output file.

Implements Nektar::FieldUtils::Module.

Definition at line 62 of file library/FieldUtils/OutputModules/OutputVtk.cpp.

References ASSERTL0, ASSERTL1, Nektar::LibUtilities::ePtsBox, Nektar::LibUtilities::ePtsFile, Nektar::LibUtilities::ePtsLine, Nektar::LibUtilities::ePtsPlane, Nektar::LibUtilities::ePtsSegBlock, Nektar::LibUtilities::ePtsTetBlock, Nektar::LibUtilities::ePtsTriBlock, CellMLToNektar.pycml::format, Nektar::FieldUtils::Module::m_config, Nektar::FieldUtils::Module::m_f, Nektar::LibUtilities::NullPtsField, Nektar::LibUtilities::PortablePath(), and WriteEmptyVtkPiece().

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

Definition at line 379 of file library/FieldUtils/OutputModules/OutputVtk.cpp.

Referenced by Process().

380 {
381  // write out empty piece of data.
382  outfile << " <Piece NumberOfPoints=\"" << 0 << "\" NumberOfCells=\"" << 0
383  << "\">" << endl;
384  outfile << " <Points>" << endl;
385  outfile << " <DataArray type=\"Float64\" "
386  << "NumberOfComponents=\"" << 3 << "\" format=\"ascii\">" << endl;
387  outfile << " </DataArray>" << endl;
388  outfile << " </Points>" << endl;
389  outfile << " <Cells>" << endl;
390  outfile << " <DataArray type=\"Int32\" "
391  << "Name=\"connectivity\" format=\"ascii\">" << endl;
392  outfile << " </DataArray>" << endl;
393  outfile << " <DataArray type=\"Int32\" "
394  << "Name=\"offsets\" format=\"ascii\">" << endl;
395 
396  outfile << " ";
397  outfile << endl;
398  outfile << " </DataArray>" << endl;
399  outfile << " <DataArray type=\"UInt8\" "
400  << "Name=\"types\" format=\"ascii\">" << endl;
401  outfile << " ";
402  outfile << endl;
403  outfile << " </DataArray>" << endl;
404  outfile << " </Cells>" << endl;
405  outfile << " <PointData>" << endl;
406 
407  outfile << " </PointData>" << endl;
408  outfile << " </Piece>" << endl;
409 }

Member Data Documentation

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

Definition at line 56 of file library/FieldUtils/OutputModules/OutputVtk.h.