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 75 of file VtkToFld.cpp.

typedef boost::shared_ptr<Vertex> VertexSharedPtr

Definition at line 50 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 83 of file VtkToFld.cpp.

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

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

54 {
55  return v1->operator==(*v2);
56 }