Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
library/FieldUtils/OutputModules/OutputVtk.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: OutputVtk.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: VTK file format output.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <set>
37 #include <string>
38 using namespace std;
39 
40 #include "OutputVtk.h"
42 #include <boost/format.hpp>
43 #include <iomanip>
44 
45 namespace Nektar
46 {
47 namespace FieldUtils
48 {
49 
50 ModuleKey OutputVtk::m_className = GetModuleFactory().RegisterCreatorFunction(
51  ModuleKey(eOutputModule, "vtu"), OutputVtk::create, "Writes a VTU file.");
52 
53 OutputVtk::OutputVtk(FieldSharedPtr f) : OutputModule(f)
54 {
55  m_requireEquiSpaced = true;
56 }
57 
59 {
60 }
61 
62 void OutputVtk::Process(po::variables_map &vm)
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 }
378 
379 void OutputVtk::WriteEmptyVtkPiece(std::ofstream &outfile)
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 }
410 }
411 }
map< string, ConfigOption > m_config
List of configuration values.
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
STL namespace.
pair< ModuleType, string > ModuleKey
virtual void Process(po::variables_map &vm)
Write fld to output file.
boost::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:178
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:767
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
Abstract base class for output modules.
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215