I am new in paraview and working on CFD. I want to extract data from C++ and visualize in paraview. For this I need some code samples which extract data from C++ in the .vtk file.
Does anyone have some experience in this, please share or any other possible suggestions for how to extract data from C++ to paraview for visualization?
Thank you so much in advance!
Hi Mathieu Westphal,
Thank you so much. Can you share with me information about vtkWriter?
I am using the code which works for 2D but not 3D, can you check this?
Code is like this:
void Mesh::Write_ascii_paraview(std::string const &fname, std::vector<double> const &v) const
assert(Nnodes() == static_cast<int>(v.size())); // fits vector length to mesh information?
ofstream fout(fname); // open file ASCII mode
if ( !fout.is_open() )
cout << "\nFile " << fname << " has not been opened.\n\n" ;
assert( fout.is_open() && "File not opened." );
string const DELIMETER(" "); // define the same delimiter as in matlab/ascii_read*.m
//int const OFFSET(o); // C-indexing in output
fout << "# vtk DataFile Version 2.0" << endl;
fout << "HEAT EQUATION" << endl;
fout << "ASCII" << endl;
fout << "DATASET POLYDATA" << endl;
fout << "POINTS "<< v.size()<<" float"<<endl;
assert( Nnodes()*Ndims() == static_cast<int>(_xc.size()));
for (int k = 0, kj = 0; k < Nnodes(); ++k)
for (int j = 0; j < Ndims(); ++j, ++kj)
fout << _xc[kj] << DELIMETER;
fout << v[k] << endl;
fout << "POLYGONS "<< Nelems() << ' ' << Nelems()*4 << endl;
assert( Nelems()*NverticesElements() == static_cast<int>(_ia.size()));
for (int k = 0, kj = 0; k < Nelems(); ++k)
fout << 4 << DELIMETER; // triangular patches
for (int j = 0; j < NverticesElements()-1; ++j, ++kj)
fout << _ia[kj] << DELIMETER;
fout << _ia[kj];
fout << endl;
void Mesh::Visualize_paraview(vector<double> const &v) const
//const string exec_m("open -a paraview"); // paraview
const string exec_m("paraview"); // paraview
const string fname("uv.vtk");
int ierror = system(exec_m.c_str()); // call external command
if (ierror != 0)
cout << endl << "Check path to paraview on your system" << endl;
cout << endl;
I’m suggesting to use vtk directly to write the file. You are writing the file manually, this is not the same thing.
I suggest looking at the vtk examples site: https://kitware.github.io/vtk-examples/site/
In your case, you could combine the VTP writer example, https://kitware.github.io/vtk-examples/site/Cxx/IO/WriteVTP/
with one that sets point data on a polydata, like https://kitware.github.io/vtk-examples/site/Cxx/VTKConcepts/Scalars/
If you look at the bottom of the example, it shows how to include and link vtk to your program using CMake.