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

Converter from fld to vtk. More...

#include <OutputVtkBase.h>

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

Public Member Functions

 OutputVtkBase (FieldSharedPtr f)
 
virtual ~OutputVtkBase ()
 
- Public Member Functions inherited from Nektar::FieldUtils::OutputFileBase
 OutputFileBase (FieldSharedPtr f)
 
virtual ~OutputFileBase ()
 
- 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
 
void Process (po::variables_map &vm)
 
std::string GetModuleName ()
 
std::string GetModuleDescription ()
 
const ConfigOptionGetConfigOption (const std::string &key) const
 
ModulePriority GetModulePriority ()
 
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 std::string v_GetModuleName () override
 
virtual void v_OutputFromPts (po::variables_map &vm) override
 Write from pts to output file. More...
 
virtual void v_OutputFromExp (po::variables_map &vm) override
 Write from m_exp to output file. More...
 
virtual void v_OutputFromData (po::variables_map &vm) override
 Write from data to output file. More...
 
virtual fs::path v_GetPath (std::string &filename, po::variables_map &vm) override
 
virtual fs::path v_GetFullOutName (std::string &filename, po::variables_map &vm) override
 
std::string PrepareOutput (po::variables_map &vm)
 
- Protected Member Functions inherited from Nektar::FieldUtils::OutputFileBase
virtual void v_Process (po::variables_map &vm) override
 Write fld to output file. More...
 
virtual std::string v_GetModuleDescription () override
 
virtual ModulePriority v_GetModulePriority () override
 
fs::path GetPath (std::string &filename, po::variables_map &vm)
 
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)
 

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 OutputVtkBase.h.

Constructor & Destructor Documentation

◆ OutputVtkBase()

Nektar::FieldUtils::OutputVtkBase::OutputVtkBase ( FieldSharedPtr  f)

◆ ~OutputVtkBase()

Nektar::FieldUtils::OutputVtkBase::~OutputVtkBase ( )
virtual

Definition at line 66 of file OutputVtkBase.cpp.

67 {
68 }

Member Function Documentation

◆ create()

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

Creates an instance of this class.

Definition at line 51 of file OutputVtkBase.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().

◆ PrepareOutput()

std::string Nektar::FieldUtils::OutputVtkBase::PrepareOutput ( po::variables_map &  vm)
protected

Definition at line 433 of file OutputVtkBase.cpp.

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

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

Referenced by v_OutputFromExp(), Nektar::FieldUtils::OutputVtk::v_OutputFromExp(), and v_OutputFromPts().

◆ v_GetFullOutName()

fs::path Nektar::FieldUtils::OutputVtkBase::v_GetFullOutName ( std::string &  filename,
po::variables_map &  vm 
)
overrideprotectedvirtual

Reimplemented from Nektar::FieldUtils::OutputFileBase.

Definition at line 311 of file OutputVtkBase.cpp.

313 {
314  int nprocs = m_f->m_comm->GetSpaceComm()->GetSize();
315 
316  fs::path fulloutname;
317  if (nprocs == 1)
318  {
319  fulloutname = filename;
320  }
321  else
322  {
323  // Guess at filename that might belong to this process.
324  boost::format pad("P%1$07d.%2$s");
325  pad % m_f->m_comm->GetSpaceComm()->GetRank() % "vtu";
326 
327  // Generate full path name
328  fs::path specPath = GetPath(filename, vm);
329  fs::path poutfile(pad.str());
330  fulloutname = specPath / poutfile;
331  }
332  return fulloutname;
333 }

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

◆ v_GetModuleName()

virtual std::string Nektar::FieldUtils::OutputVtkBase::v_GetModuleName ( )
inlineoverrideprotectedvirtual

Reimplemented from Nektar::FieldUtils::OutputFileBase.

Reimplemented in Nektar::FieldUtils::OutputVtk.

Definition at line 61 of file OutputVtkBase.h.

62  {
63  return "OutputVtk";
64  }

◆ v_GetPath()

fs::path Nektar::FieldUtils::OutputVtkBase::v_GetPath ( std::string &  filename,
po::variables_map &  vm 
)
overrideprotectedvirtual

Reimplemented from Nektar::FieldUtils::OutputFileBase.

Definition at line 291 of file OutputVtkBase.cpp.

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

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

Referenced by Nektar::FieldUtils::OutputVtk::WritePVtu().

◆ v_OutputFromData()

void Nektar::FieldUtils::OutputVtkBase::v_OutputFromData ( po::variables_map &  vm)
overrideprotectedvirtual

Write from data to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Reimplemented in Nektar::FieldUtils::OutputVtk.

Definition at line 283 of file OutputVtkBase.cpp.

284 {
285  boost::ignore_unused(vm);
287  "OutputVtk can't write using only FieldData. You may need "
288  "to add a mesh XML file to your input files.");
289 }
#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.

◆ v_OutputFromExp()

void Nektar::FieldUtils::OutputVtkBase::v_OutputFromExp ( po::variables_map &  vm)
overrideprotectedvirtual

Write from m_exp to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Reimplemented in Nektar::FieldUtils::OutputVtk.

Definition at line 235 of file OutputVtkBase.cpp.

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

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

Referenced by Nektar::FieldUtils::OutputVtk::v_OutputFromExp().

◆ v_OutputFromPts()

void Nektar::FieldUtils::OutputVtkBase::v_OutputFromPts ( po::variables_map &  vm)
overrideprotectedvirtual

Write from pts to output file.

Implements Nektar::FieldUtils::OutputFileBase.

Reimplemented in Nektar::FieldUtils::OutputVtk.

Definition at line 70 of file OutputVtkBase.cpp.

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

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().

Referenced by Nektar::FieldUtils::OutputVtk::v_OutputFromPts().

◆ WriteEmptyVtkPiece()

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

Definition at line 349 of file OutputVtkBase.cpp.

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

Referenced by v_OutputFromExp().

◆ WritePVtu()

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

Definition at line 381 of file OutputVtkBase.cpp.

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

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

Referenced by v_OutputFromExp(), and v_OutputFromPts().

◆ WriteVtkFooter()

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

Definition at line 343 of file OutputVtkBase.cpp.

344 {
345  outfile << " </UnstructuredGrid>" << endl;
346  outfile << "</VTKFile>" << endl;
347 }

Referenced by v_OutputFromExp(), and v_OutputFromPts().

◆ WriteVtkHeader()

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

Definition at line 335 of file OutputVtkBase.cpp.

336 {
337  outfile << "<?xml version=\"1.0\"?>" << endl;
338  outfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" "
339  << "byte_order=\"LittleEndian\">" << endl;
340  outfile << " <UnstructuredGrid>" << endl;
341 }

Referenced by v_OutputFromExp(), and v_OutputFromPts().

Member Data Documentation

◆ m_className

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

Definition at line 55 of file OutputVtkBase.h.