Nektar++
FieldConvert/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"
41 
42 namespace Nektar
43 {
44 namespace Utilities
45 {
46 
47 ModuleKey OutputVtk::m_className =
49  ModuleKey(eOutputModule, "vtu"), OutputVtk::create,
50  "Writes a VTU file.");
51 
52 OutputVtk::OutputVtk(FieldSharedPtr f) : OutputModule(f)
53 {
54  m_requireEquiSpaced = true;
55 }
56 
58 {
59 }
60 
61 void OutputVtk::Process(po::variables_map &vm)
62 {
63  if(!m_f->m_exp.size()) // do nothing if no expansion defined
64  {
65  return;
66  }
67 
68  int i, j;
69  if (m_f->m_verbose)
70  {
71  cout << "OutputVtk: Writing file..." << endl;
72  }
73 
74  // Extract the output filename and extension
75  string filename = m_config["outfile"].as<string>();
76 
77  // amend for parallel output if required
78  if(m_f->m_session->GetComm()->GetSize() != 1)
79  {
80  int dot = filename.find_last_of('.');
81  string ext = filename.substr(dot,filename.length()-dot);
82  string procId = "_P" + boost::lexical_cast<std::string>(
83  m_f->m_session->GetComm()->GetRank());
84  string start = filename.substr(0,dot);
85  filename = start + procId + ext;
86  }
87 
88  // Write solution.
89  ofstream outfile(filename.c_str());
90  m_f->m_exp[0]->WriteVtkHeader(outfile);
91 
92  int nfields, nstrips;
93  if (m_f->m_fielddef.size() == 0)
94  {
95  nfields = 0;
96  }
97  else
98  {
99  nfields = m_f->m_fielddef[0]->m_fields.size();
100  }
101  m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
102 
103  // Homogeneous strip variant
104  for(int s = 0; s < nstrips; ++s)
105  {
106  // For each field write out field data for each expansion.
107  for (i = 0; i < m_f->m_exp[0]->GetNumElmts(); ++i)
108  {
109  m_f->m_exp[0]->WriteVtkPieceHeader(outfile,i,s);
110 
111  // For this expansion write out each field.
112  for (j = 0; j < nfields; ++j)
113  {
114  m_f->m_exp[s*nfields+j]->WriteVtkPieceData(
115  outfile, i, m_f->m_fielddef[0]->m_fields[j]);
116  }
117  m_f->m_exp[0]->WriteVtkPieceFooter(outfile, i);
118  }
119  }
120  m_f->m_exp[0]->WriteVtkFooter(outfile);
121  cout << "Written file: " << filename << endl;
122 }
123 
124 }
125 }
pair< ModuleType, string > ModuleKey
Abstract base class for output modules.
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
FieldSharedPtr m_f
Field object.
virtual void Process()
Write mesh to output file.
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:677
ModuleFactory & GetModuleFactory()
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215