Nektar++
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Member Functions | List of all members
Nektar::FieldUtils::OutputVtk Class Reference

Converter from fld to vtk. More...

#include <OutputVtk.h>

Inheritance diagram for Nektar::FieldUtils::OutputVtk:
[legend]

Public Member Functions

 OutputVtk (FieldSharedPtr f)
 
virtual ~OutputVtk ()
 
virtual std::string GetModuleName ()
 
- Public Member Functions inherited from Nektar::FieldUtils::OutputFileBase
 OutputFileBase (FieldSharedPtr f)
 
virtual ~OutputFileBase ()
 
virtual void Process (po::variables_map &vm)
 Write fld to output file. More...
 
virtual std::string GetModuleDescription ()
 
virtual ModulePriority GetModulePriority ()
 
- 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)
 
virtual ~Module ()=default
 
const ConfigOptionGetConfigOption (const std::string &key) const
 
FIELD_UTILS_EXPORT void RegisterConfig (std::string key, std::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 void AddFile (std::string fileType, std::string fileName)
 
FIELD_UTILS_EXPORT void EvaluateTriFieldAtEquiSpacedPts (LocalRegions::ExpansionSharedPtr &exp, const Array< OneD, const NekDouble > &infield, Array< OneD, NekDouble > &outfield)
 

Static Public Member Functions

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

Static Public Attributes

static ModuleKey m_className
 

Protected Member Functions

virtual void OutputFromPts (po::variables_map &vm)
 Write from pts to output file. More...
 
virtual void OutputFromExp (po::variables_map &vm)
 Write from m_exp to output file. More...
 
virtual void OutputFromData (po::variables_map &vm)
 Write from data to output file. More...
 
virtual fs::path GetPath (std::string &filename, po::variables_map &vm)
 
virtual fs::path GetFullOutName (std::string &filename, po::variables_map &vm)
 
- Protected Member Functions inherited from Nektar::FieldUtils::Module
 Module ()
 

Private Member Functions

void WriteVtkHeader (std::ostream &outfile)
 
void WriteVtkFooter (std::ostream &outfile)
 
void WriteEmptyVtkPiece (std::ofstream &outfile)
 
void WritePVtu (po::variables_map &vm)
 
std::string PrepareOutput (po::variables_map &vm)
 

Additional Inherited Members

- Public Attributes inherited from Nektar::FieldUtils::Module
FieldSharedPtr m_f
 Field object. More...
 
- Protected Attributes inherited from Nektar::FieldUtils::OutputFileBase
bool m_requireEquiSpaced
 
- Protected Attributes inherited from Nektar::FieldUtils::OutputModule
std::ofstream m_fldFile
 Output stream. More...
 
- Protected Attributes inherited from Nektar::FieldUtils::Module
std::map< std::string, ConfigOptionm_config
 List of configuration values. More...
 
std::set< std::string > m_allowedFiles
 List of allowed file formats. More...
 

Detailed Description

Converter from fld to vtk.

Definition at line 47 of file OutputVtk.h.

Constructor & Destructor Documentation

◆ OutputVtk()

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

◆ ~OutputVtk()

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

Definition at line 60 of file OutputVtk.cpp.

61 {
62 }

Member Function Documentation

◆ create()

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

Creates an instance of this class.

Definition at line 51 of file OutputVtk.h.

52  {
54  }
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.

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

◆ GetFullOutName()

fs::path Nektar::FieldUtils::OutputVtk::GetFullOutName ( std::string &  filename,
po::variables_map &  vm 
)
protectedvirtual

Implements Nektar::FieldUtils::OutputFileBase.

Definition at line 305 of file OutputVtk.cpp.

307 {
308  int nprocs = m_f->m_comm->GetSize();
309 
310  fs::path fulloutname;
311  if (nprocs == 1)
312  {
313  fulloutname = filename;
314  }
315  else
316  {
317  // Guess at filename that might belong to this process.
318  boost::format pad("P%1$07d.%2$s");
319  pad % m_f->m_comm->GetRank() % "vtu";
320 
321  // Generate full path name
322  fs::path specPath = GetPath(filename, vm);
323  fs::path poutfile(pad.str());
324  fulloutname = specPath / poutfile;
325  }
326  return fulloutname;
327 }
FieldSharedPtr m_f
Field object.
Definition: Module.h:230
virtual fs::path GetPath(std::string &filename, po::variables_map &vm)
Definition: OutputVtk.cpp:284

References CellMLToNektar.pycml::format, GetPath(), and Nektar::FieldUtils::Module::m_f.

Referenced by PrepareOutput().

◆ GetModuleName()

virtual std::string Nektar::FieldUtils::OutputVtk::GetModuleName ( )
inlinevirtual

Reimplemented from Nektar::FieldUtils::OutputFileBase.

Definition at line 60 of file OutputVtk.h.

61  {
62  return "OutputVtk";
63  }

◆ GetPath()

fs::path Nektar::FieldUtils::OutputVtk::GetPath ( std::string &  filename,
po::variables_map &  vm 
)
protectedvirtual

Implements Nektar::FieldUtils::OutputFileBase.

Definition at line 284 of file OutputVtk.cpp.

286 {
287  boost::ignore_unused(vm);
288 
289  int nprocs = m_f->m_comm->GetSize();
290  fs::path specPath;
291  if (nprocs == 1)
292  {
293  specPath = fs::path(filename);
294  }
295  else
296  {
297  // replace .vtu by _vtu
298  int dot = filename.find_last_of('.');
299  string path = filename.substr(0, dot) + "_vtu";
300  specPath = fs::path(path);
301  }
302  return fs::path(specPath);
303 }

References Nektar::FieldUtils::Module::m_f.

Referenced by GetFullOutName(), PrepareOutput(), and WritePVtu().

◆ OutputFromData()

void Nektar::FieldUtils::OutputVtk::OutputFromData ( po::variables_map &  vm)
protectedvirtual

Write from data to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Definition at line 278 of file OutputVtk.cpp.

279 {
280  boost::ignore_unused(vm);
281  NEKERROR(ErrorUtil::efatal, "OutputVtk can't write using only FieldData.");
282 }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209

References Nektar::ErrorUtil::efatal, and NEKERROR.

◆ OutputFromExp()

void Nektar::FieldUtils::OutputVtk::OutputFromExp ( po::variables_map &  vm)
protectedvirtual

Write from m_exp to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Definition at line 230 of file OutputVtk.cpp.

231 {
232  int i,j;
233  // Extract the output filename and extension
234  string filename = PrepareOutput(vm);
235 
236  // Write solution.
237  ofstream outfile(filename.c_str());
238  WriteVtkHeader(outfile);
239  int nfields = m_f->m_variables.size();
240 
241  int nstrips;
242  m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
243 
244  // Homogeneous strip variant
245  for (int s = 0; s < nstrips; ++s)
246  {
247  // For each field write out field data for each expansion.
248  for (i = 0; i < m_f->m_exp[0]->GetNumElmts(); ++i)
249  {
250  m_f->m_exp[0]->WriteVtkPieceHeader(outfile, i, s);
251 
252  // For this expansion write out each field.
253  for (j = 0; j < nfields; ++j)
254  {
255  m_f->m_exp[s * nfields + j]->WriteVtkPieceData(
256  outfile, i, m_f->m_variables[j]);
257  }
258  m_f->m_exp[0]->WriteVtkPieceFooter(outfile, i);
259  }
260  }
261 
262  if (m_f->m_exp[0]->GetNumElmts() == 0)
263  {
264  WriteEmptyVtkPiece(outfile);
265  }
266 
267  WriteVtkFooter(outfile);
268  cout << "Written file: " << filename << endl;
269 
270  // output parallel outline info if necessary
271  if ( (m_f->m_comm->GetRank() == 0) &&
272  (m_f->m_comm->GetSize() != 1))
273  {
274  WritePVtu(vm);
275  }
276 }
void WriteVtkHeader(std::ostream &outfile)
Definition: OutputVtk.cpp:329
void WriteVtkFooter(std::ostream &outfile)
Definition: OutputVtk.cpp:337
void WriteEmptyVtkPiece(std::ofstream &outfile)
Definition: OutputVtk.cpp:343
std::string PrepareOutput(po::variables_map &vm)
Definition: OutputVtk.cpp:427
void WritePVtu(po::variables_map &vm)
Definition: OutputVtk.cpp:375

References Nektar::FieldUtils::Module::m_f, PrepareOutput(), WriteEmptyVtkPiece(), WritePVtu(), WriteVtkFooter(), and WriteVtkHeader().

◆ OutputFromPts()

void Nektar::FieldUtils::OutputVtk::OutputFromPts ( po::variables_map &  vm)
protectedvirtual

Write from pts to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Definition at line 64 of file OutputVtk.cpp.

65 {
66  int i, j;
67  LibUtilities::PtsFieldSharedPtr fPts = m_f->m_fieldPts;
68 
69  // Extract the output filename and extension
70  string filename = PrepareOutput(vm);
71 
72  // Write solution.
73  ofstream outfile(filename.c_str());
74  WriteVtkHeader(outfile);
75  int nfields = 0;
76  int dim = fPts->GetDim();
77 
78  int nvert = 1;
79  int vtktype = 1;
80  switch (fPts->GetPtsType())
81  {
84  {
86  "VTK output needs setting up for ePtsFile and ePtsLine");
87  break;
88  }
90  {
92  "VTK output needs setting up for PtsPlane");
93  break;
94  }
96  {
98  "VTK output needs setting up for PtsBox");
99  break;
100  }
102  {
103  nvert = 2;
104  vtktype = 3;
105  break;
106  }
108  {
109  nvert = 3;
110  vtktype = 5;
111  break;
112  }
114  {
115  nvert = 4;
116  vtktype = 10;
117  break;
118  }
119  default:
120  NEKERROR(ErrorUtil::efatal, "ptsType not supported yet.");
121  }
122 
123  vector<Array<OneD, int> > ptsConn;
124  fPts->GetConnectivity(ptsConn);
125 
126  nfields = fPts->GetNFields();
127 
128  int nPts = fPts->GetNpoints();
129  int numBlocks = 0;
130  for (i = 0; i < ptsConn.size(); ++i)
131  {
132  numBlocks += ptsConn[i].size() / nvert;
133  }
134 
135  // write out pieces of data.
136  outfile << " <Piece NumberOfPoints=\"" << nPts
137  << "\" NumberOfCells=\"" << numBlocks << "\">" << endl;
138  outfile << " <Points>" << endl;
139  outfile << " <DataArray type=\"Float64\" "
140  << "NumberOfComponents=\"" << 3 << "\" format=\"ascii\">"
141  << endl;
142  for (i = 0; i < nPts; ++i)
143  {
144  for (j = 0; j < dim; ++j)
145  {
146  outfile << " " << setprecision(8) << scientific
147  << fPts->GetPointVal(j, i) << " ";
148  }
149  for (j = dim; j < 3; ++j)
150  {
151  // pack to 3D since paraview does not seem to handle 2D
152  outfile << " 0.000000";
153  }
154  outfile << endl;
155  }
156  outfile << " </DataArray>" << endl;
157  outfile << " </Points>" << endl;
158  outfile << " <Cells>" << endl;
159  outfile << " <DataArray type=\"Int32\" "
160  << "Name=\"connectivity\" format=\"ascii\">" << endl;
161 
162  // dump connectivity data if it exists
163  outfile << " ";
164  int cnt = 1;
165  for (i = 0; i < ptsConn.size(); ++i)
166  {
167  for (j = 0; j < ptsConn[i].size(); ++j)
168  {
169  outfile << ptsConn[i][j] << " ";
170  if ((!(cnt % nvert)) && cnt)
171  {
172  outfile << std::endl;
173  outfile << " ";
174  }
175  cnt++;
176  }
177  }
178  outfile << " </DataArray>" << endl;
179  outfile << " <DataArray type=\"Int32\" "
180  << "Name=\"offsets\" format=\"ascii\">" << endl;
181 
182  outfile << " ";
183  for (i = 0; i < numBlocks; ++i)
184  {
185  outfile << i * nvert + nvert << " ";
186  }
187  outfile << endl;
188  outfile << " </DataArray>" << endl;
189  outfile << " <DataArray type=\"UInt8\" "
190  << "Name=\"types\" format=\"ascii\">" << endl;
191  outfile << " ";
192  for (i = 0; i < numBlocks; ++i)
193  {
194  outfile << vtktype << " ";
195  }
196  outfile << endl;
197  outfile << " </DataArray>" << endl;
198  outfile << " </Cells>" << endl;
199  outfile << " <PointData>" << endl;
200 
201  // printing the fields
202  for (j = 0; j < nfields; ++j)
203  {
204  outfile << " <DataArray type=\"Float64\" Name=\""
205  << m_f->m_variables[j] << "\">" << endl;
206  outfile << " ";
207  for (i = 0; i < fPts->GetNpoints(); ++i)
208  {
209  outfile << fPts->GetPointVal(dim + j, i) << " ";
210  }
211  outfile << endl;
212  outfile << " </DataArray>" << endl;
213  }
214 
215  outfile << " </PointData>" << endl;
216  outfile << " </Piece>" << endl;
217 
218  WriteVtkFooter(outfile);
219  cout << "Written file: " << filename << endl;
220 
221  // output parallel outline info if necessary
222  if ( (m_f->m_comm->GetRank() == 0) &&
223  (m_f->m_comm->GetSize() != 1))
224  {
225  WritePVtu(vm);
226  cout << "Written file: " << filename << endl;
227  }
228 }
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:182

References Nektar::ErrorUtil::efatal, Nektar::LibUtilities::ePtsBox, Nektar::LibUtilities::ePtsFile, Nektar::LibUtilities::ePtsLine, Nektar::LibUtilities::ePtsPlane, Nektar::LibUtilities::ePtsSegBlock, Nektar::LibUtilities::ePtsTetBlock, Nektar::LibUtilities::ePtsTriBlock, Nektar::FieldUtils::Module::m_f, NEKERROR, PrepareOutput(), WritePVtu(), WriteVtkFooter(), and WriteVtkHeader().

◆ PrepareOutput()

std::string Nektar::FieldUtils::OutputVtk::PrepareOutput ( po::variables_map &  vm)
private

Definition at line 427 of file OutputVtk.cpp.

428 {
429  // Extract the output filename and extension
430  string filename = m_config["outfile"].as<string>();
431 
432  fs::path specPath = GetPath(filename,vm);
433  fs::path fulloutname = GetFullOutName(filename,vm);
434  filename = LibUtilities::PortablePath(fulloutname);
435 
436  if (m_f->m_comm->GetSize() != 1)
437  {
438  if (m_f->m_comm->TreatAsRankZero())
439  {
440  try
441  {
442  fs::create_directory(specPath);
443  }
444  catch (fs::filesystem_error &e)
445  {
446  ASSERTL0(false, "Filesystem error: " + string(e.what()));
447  }
448  cout << "Writing files to directory: " << specPath << endl;
449  }
450  m_f->m_comm->Block();
451  }
452  else
453  {
454  cout << "Writing: " << specPath << endl;
455  }
456  return filename;
457 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:233
virtual fs::path GetFullOutName(std::string &filename, po::variables_map &vm)
Definition: OutputVtk.cpp:305
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41

References ASSERTL0, GetFullOutName(), GetPath(), Nektar::FieldUtils::Module::m_config, Nektar::FieldUtils::Module::m_f, and Nektar::LibUtilities::PortablePath().

Referenced by OutputFromExp(), and OutputFromPts().

◆ WriteEmptyVtkPiece()

void Nektar::FieldUtils::OutputVtk::WriteEmptyVtkPiece ( std::ofstream &  outfile)
private

Definition at line 343 of file OutputVtk.cpp.

344 {
345  // write out empty piece of data.
346  outfile << " <Piece NumberOfPoints=\"" << 0 << "\" NumberOfCells=\"" << 0
347  << "\">" << endl;
348  outfile << " <Points>" << endl;
349  outfile << " <DataArray type=\"Float64\" "
350  << "NumberOfComponents=\"" << 3 << "\" format=\"ascii\">" << endl;
351  outfile << " </DataArray>" << endl;
352  outfile << " </Points>" << endl;
353  outfile << " <Cells>" << endl;
354  outfile << " <DataArray type=\"Int32\" "
355  << "Name=\"connectivity\" format=\"ascii\">" << endl;
356  outfile << " </DataArray>" << endl;
357  outfile << " <DataArray type=\"Int32\" "
358  << "Name=\"offsets\" format=\"ascii\">" << endl;
359 
360  outfile << " ";
361  outfile << endl;
362  outfile << " </DataArray>" << endl;
363  outfile << " <DataArray type=\"UInt8\" "
364  << "Name=\"types\" format=\"ascii\">" << endl;
365  outfile << " ";
366  outfile << endl;
367  outfile << " </DataArray>" << endl;
368  outfile << " </Cells>" << endl;
369  outfile << " <PointData>" << endl;
370 
371  outfile << " </PointData>" << endl;
372  outfile << " </Piece>" << endl;
373 }

Referenced by OutputFromExp().

◆ WritePVtu()

void Nektar::FieldUtils::OutputVtk::WritePVtu ( po::variables_map &  vm)
private

Definition at line 375 of file OutputVtk.cpp.

376 {
377  string filename = m_config["outfile"].as<string>();
378  int dot = filename.find_last_of('.');
379  string body = filename.substr(0, dot);
380  filename = body + ".pvtu";
381 
382  ofstream outfile(filename.c_str());
383 
384  int nprocs = m_f->m_comm->GetSize();
385  string path = LibUtilities::PortablePath(GetPath(filename,vm));
386 
387  outfile << "<?xml version=\"1.0\"?>" << endl;
388  outfile << "<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" "
389  << "byte_order=\"LittleEndian\">" << endl;
390  outfile << "<PUnstructuredGrid GhostLevel=\"0\">" << endl;
391  outfile << "<PPoints> " << endl;
392  outfile << "<PDataArray type=\"Float64\" NumberOfComponents=\"" << 3
393  << "\"/> " << endl;
394  outfile << "</PPoints>" << endl;
395  outfile << "<PCells>" << endl;
396  outfile << "<PDataArray type=\"Int32\" Name=\"connectivity\" "
397  "NumberOfComponents=\"1\"/>"
398  << endl;
399  outfile << "<PDataArray type=\"Int32\" Name=\"offsets\" "
400  "NumberOfComponents=\"1\"/>"
401  << endl;
402  outfile << "<PDataArray type=\"UInt8\" Name=\"types\" "
403  "NumberOfComponents=\"1\"/>"
404  << endl;
405  outfile << "</PCells>" << endl;
406  outfile << "<PPointData Scalars=\"Material\">" << endl;
407  for (int i = 0; i < m_f->m_variables.size(); ++i)
408  {
409  outfile << "<PDataArray type=\"Float64\" Name=\""
410  << m_f->m_variables[i] << "\"/>" << endl;
411  }
412  outfile << "</PPointData>" << endl;
413 
414  for (int i = 0; i < nprocs; ++i)
415  {
416  boost::format pad("P%1$07d.vtu");
417  pad % i;
418  outfile << "<Piece Source=\"" << path << "/" << pad.str()
419  << "\"/>" << endl;
420  }
421  outfile << "</PUnstructuredGrid>" << endl;
422  outfile << "</VTKFile>" << endl;
423 
424  cout << "Written file: " << filename << endl;
425 }

References CellMLToNektar.pycml::format, GetPath(), Nektar::FieldUtils::Module::m_config, Nektar::FieldUtils::Module::m_f, and Nektar::LibUtilities::PortablePath().

Referenced by OutputFromExp(), and OutputFromPts().

◆ WriteVtkFooter()

void Nektar::FieldUtils::OutputVtk::WriteVtkFooter ( std::ostream &  outfile)
private

Definition at line 337 of file OutputVtk.cpp.

338 {
339  outfile << " </UnstructuredGrid>" << endl;
340  outfile << "</VTKFile>" << endl;
341 }

Referenced by OutputFromExp(), and OutputFromPts().

◆ WriteVtkHeader()

void Nektar::FieldUtils::OutputVtk::WriteVtkHeader ( std::ostream &  outfile)
private

Definition at line 329 of file OutputVtk.cpp.

330 {
331  outfile << "<?xml version=\"1.0\"?>" << endl;
332  outfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" "
333  << "byte_order=\"LittleEndian\">" << endl;
334  outfile << " <UnstructuredGrid>" << endl;
335 }

Referenced by OutputFromExp(), and OutputFromPts().

Member Data Documentation

◆ m_className

ModuleKey Nektar::FieldUtils::OutputVtk::m_className
static
Initial value:
ModuleKey(eOutputModule, "vtu"), OutputVtk::create, "Writes a VTU file.")
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
Definition: OutputVtk.h:51
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:290
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49

Definition at line 55 of file OutputVtk.h.