Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Typedefs | Functions
VtkToFld.cpp File Reference
#include <vtkPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>
#include <vtkCellDataToPointData.h>
#include <vtkContourFilter.h>
#include <boost/unordered_set.hpp>
#include <boost/program_options.hpp>
#include <LibUtilities/BasicUtils/SessionReader.h>
#include <LibUtilities/Communication/Comm.h>
#include <SpatialDomains/MeshGraph2D.h>
#include <MultiRegions/ExpList2D.h>
#include <LocalRegions/Expansion2D.h>
Include dependency graph for VtkToFld.cpp:

Go to the source code of this file.

Classes

struct  Vertex
 Represents a vertex in the mesh. More...
 
struct  VertexHash
 Hash function for the Vertex struct used for defining sets. More...
 

Typedefs

typedef boost::shared_ptr< VertexVertexSharedPtr
 
typedef boost::unordered_set
< VertexSharedPtr, VertexHash
VertexSet
 Define a set of Vertices with associated hash function. More...
 

Functions

bool operator== (const VertexSharedPtr &v1, const VertexSharedPtr &v2)
 Define comparison operator for the vertex struct. More...
 
int main (int argc, char *argv[])
 

Typedef Documentation

typedef boost::unordered_set<VertexSharedPtr, VertexHash> VertexSet

Define a set of Vertices with associated hash function.

Definition at line 73 of file VtkToFld.cpp.

typedef boost::shared_ptr<Vertex> VertexSharedPtr

Definition at line 48 of file VtkToFld.cpp.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Main function.

Usage: VtkToFld session.xml input.vtk output.fld [options]

Definition at line 81 of file VtkToFld.cpp.

References ASSERTL0, Nektar::StdRegions::StdExpansion::GetCoords(), Nektar::iterator, Nektar::NekMeshUtils::vert, and Nektar::LibUtilities::FieldIO::Write().

82 {
83  // Set up available options
84  po::options_description desc("Available options");
85  desc.add_options()
86  ("help,h", "Produce this help message.")
87  ("name,n", po::value<string>()->default_value("Intensity"),
88  "Name of field in VTK file to use for intensity.")
89  ("outname,m", po::value<string>()->default_value("intensity"),
90  "Name of field in output FLD file.")
91  ("precision,p", po::value<double>()->default_value(1),
92  "Precision of vertex matching.");
93 
94  po::options_description hidden("Hidden options");
95  hidden.add_options()
96  ("file", po::value<vector<string> >(), "Input filename");
97 
98  po::options_description cmdline_options;
99  cmdline_options.add(desc).add(hidden);
100 
101  po::positional_options_description p;
102  p.add("file", -1);
103 
104  po::variables_map vm;
105 
106  // Parse command-line options
107  try
108  {
109  po::store(po::command_line_parser(argc, argv).
110  options(cmdline_options).positional(p).run(), vm);
111  po::notify(vm);
112  }
113  catch (const std::exception& e)
114  {
115  cerr << e.what() << endl;
116  cerr << desc;
117  return 1;
118  }
119 
120  if ( vm.count("help") || vm.count("file") == 0 ||
121  vm["file"].as<vector<string> >().size() != 3) {
122  cerr << "Usage: VtkToFld session.xml intensity.vtk output.fld [options]"
123  << endl;
124  cerr << desc;
125  return 1;
126  }
127 
128  // Extract command-line argument values
129  std::vector<std::string> vFiles = vm["file"].as<vector<string> >();
130  const string infile = vFiles[1];
131  const string outfile = vFiles[2];
132  const double factor = vm["precision"].as<double>();
133  const string name = vm["name"].as<string>();
134  const string outname = vm["outname"].as<string>();
135 
136  std::vector<std::string> vFilenames;
140 
141  vFilenames.push_back(vFiles[0]);
142  vSession = LibUtilities::SessionReader::CreateInstance(2, argv, vFilenames);
143 
144  try
145  {
146  //----------------------------------------------
147  // Read in mesh from input file
149  AllocateSharedPtr(vSession);
150  //----------------------------------------------
151 
152  //----------------------------------------------
153  // Define Expansion
155  AllocateSharedPtr(vSession,graph2D);
156  //----------------------------------------------
157 
158  //----------------------------------------------
159  // Set up coordinates of mesh
160  int coordim = Exp->GetCoordim(0);
161  int nq = Exp->GetNpoints();
162 
163  Array<OneD, NekDouble> xc0(nq,0.0);
164  Array<OneD, NekDouble> xc1(nq,0.0);
165  Array<OneD, NekDouble> xc2(nq,0.0);
166 
167  switch(coordim)
168  {
169  case 2:
170  Exp->GetCoords(xc0,xc1);
171  break;
172  case 3:
173  Exp->GetCoords(xc0,xc1,xc2);
174  break;
175  default:
176  ASSERTL0(false,"Coordim not valid");
177  break;
178  }
179  //----------------------------------------------
180 
181  vtkPolyDataReader *vtkMeshReader = vtkPolyDataReader::New();
182  vtkMeshReader->SetFileName(infile.c_str());
183  vtkMeshReader->Update();
184 
185  vtkPolyData *vtkMesh = vtkMeshReader->GetOutput();
186  vtkCellDataToPointData* c2p = vtkCellDataToPointData::New();
187 #if VTK_MAJOR_VERSION <= 5
188  c2p->SetInput(vtkMesh);
189 #else
190  c2p->SetInputData(vtkMesh);
191 #endif
192  c2p->PassCellDataOn();
193  c2p->Update();
194  vtkPolyData *vtkDataAtPoints = c2p->GetPolyDataOutput();
195 
196  vtkPoints *vtkPoints = vtkMesh->GetPoints();
197  ASSERTL0(vtkPoints, "ERROR: cannot get points from mesh.");
198 
199  vtkCellArray *vtkPolys = vtkMesh->GetPolys();
200  ASSERTL0(vtkPolys, "ERROR: cannot get polygons from mesh.");
201 
202  vtkPointData *vtkPData = vtkDataAtPoints->GetPointData();
203  ASSERTL0(vtkPolys, "ERROR: cannot get point data from file.");
204 
205  VertexSet points;
206  VertexSet::iterator vIter;
207  double p[3];
208  double val;
209  double x, y, z;
210  int coeff_idx;
211  int i,j,n;
212 
213  if (!vtkDataAtPoints->GetPointData()->HasArray(name.c_str())) {
214  n = vtkDataAtPoints->GetPointData()->GetNumberOfArrays();
215  cerr << "Input file '" << infile
216  << "' does not have a field named '"
217  << name << "'" << endl;
218  cerr << "There are " << n << " arrays in this file." << endl;
219  for (int i = 0; i < n; ++i)
220  {
221  cerr << " "
222  << vtkDataAtPoints->GetPointData()->GetArray(i)->GetName()
223  << endl;
224  }
225  return 1;
226  }
227 
228  // Build up an unordered set of vertices from the VTK file. For each
229  // vertex a hashed value of the coordinates is generated to within a
230  // given tolerance.
231  n = vtkPoints->GetNumberOfPoints();
232  for (i = 0; i < n; ++i)
233  {
234  vtkPoints->GetPoint(i,p);
235  val = vtkPData->GetScalars(name.c_str())->GetTuple1(i);
236  boost::shared_ptr<Vertex> v(new Vertex(p[0],p[1],p[2],val,factor));
237  points.insert(v);
238  }
239 
240  // Now process each vertex of each element in the mesh
242  for (i = 0; i < Exp->GetNumElmts(); ++i)
243  {
244  StdRegions::StdExpansionSharedPtr e = Exp->GetExp(i);
245  for (j = 0; j < e->GetNverts(); ++j)
246  {
247  // Get the index of the coefficient corresponding to this vertex
248  coeff_idx = Exp->GetCoeff_Offset(i) + e->GetVertexMap(j);
249 
250  // Get the coordinates of the vertex
251  vert = e->as<LocalRegions::Expansion2D>()->GetGeom2D()
252  ->GetVertex(j);
253  vert->GetCoords(x,y,z);
254 
255  // Look up the vertex in the VertexSet
256  boost::shared_ptr<Vertex> v(new Vertex(x,y,z,0.0,factor));
257  vIter = points.find(v);
258 
259  // If not found, maybe the tolerance should be reduced?
260  // If found, record the scalar value from the VTK file in the
261  // corresponding coefficient.
262  if (vIter == points.end())
263  {
264  cerr << "Vertex " << i << " not found. Looking for ("
265  << x << ", " << y << ", " << z << ")" << endl;
266  }
267  else
268  {
269  Exp->UpdateCoeffs()[coeff_idx] = (*vIter)->scalar;
270  }
271  }
272  }
273  Exp->SetPhysState(false);
274 
275  //-----------------------------------------------
276  // Write solution to file
277  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
278  = Exp->GetFieldDefinitions();
279  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
280 
281  for(i = 0; i < FieldDef.size(); ++i)
282  {
283  FieldDef[i]->m_fields.push_back(outname);
284  Exp->AppendFieldData(FieldDef[i], FieldData[i]);
285  }
286 
287  LibUtilities::FieldIO vFld(vSession->GetComm());
288  vFld.Write(outfile, FieldDef, FieldData);
289  //-----------------------------------------------
290  }
291  catch (...) {
292  cout << "An error occurred." << endl;
293  }
294 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
boost::unordered_set< VertexSharedPtr, VertexHash > VertexSet
Define a set of Vertices with associated hash function.
Definition: VtkToFld.cpp:73
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
void GetCoords(Array< OneD, NekDouble > &coords_1, Array< OneD, NekDouble > &coords_2=NullNekDouble1DArray, Array< OneD, NekDouble > &coords_3=NullNekDouble1DArray)
this function returns the physical coordinates of the quadrature points of the expansion ...
Definition: StdExpansion.h:676
boost::shared_ptr< ExpList2D > ExpList2DSharedPtr
Shared pointer to an ExpList2D object.
Definition: ExpList2D.h:49
Represents a vertex in the mesh.
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs typedef NekMatrix< LhsDataType, StandardMatrixTag >::iterator iterator
Class for operating on FLD files.
Definition: FieldIO.h:137
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
Write data in FLD format.
Definition: FieldIO.cpp:160
boost::shared_ptr< StdExpansion > StdExpansionSharedPtr
boost::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:442
boost::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:60
bool operator== ( const VertexSharedPtr v1,
const VertexSharedPtr v2 
)

Define comparison operator for the vertex struct.

Definition at line 51 of file VtkToFld.cpp.

52 {
53  return v1->operator==(*v2);
54 }