writing the output in vtk format

I already installed the vtk library and run the vtk examples. before installation of vtk, I had the output in .txt as follow,

1
2
3
4
5
6
7
8
9
10
		
std::ofstream file;
file.open("file.txt");
file << "i " << std::setw(15) << " x(i) " << std::setw(15) << " y(i) " <<  std::setw(15) << " ans[i] " << "\n";
for (size_t i = 0; i < A.size(); i++)
{
file << i << std::setw(15) << Solution[i] << "\t";
file << "\n";
}
file.close(); 


Now I want to write the output in vtk format to use in paraview but I don't know how to write in vtk format. I would be really grateful if you could give me a hint for that.
Last edited on
As you appear to have only two columns of data this would appear to be a line graph. Don't use paraview: just use gnuplot.

Your example also appears to have 4 columns in the header, but only 2 columns of data.
The gnuplot is good; it can plot 3D too.

If you knew to load vtk, you should know where its documentation -- for "vtk format" -- is.
I, for one, had never heard of vtk. Read what the format requires and then code accordingly.


PS. Is this always safe?
1
2
3
4
for (size_t i = 0; i < A.size(); i++)
{
  file << Solution[i];
}
@Lastchance thanks for your answer. You are right about four columns in header and two columns of data. It should be four columns of data as well but I changed the two first column to i just for shortening in this post. You said don't use paraview because it's a line graph, why gnuplot is better for line graph?and when it's better to use paraview?
@keskiverto
Thanks for you answer. No it's not always safe because of four columns of header and two columns of data
Last edited on
Cplusc wrote:
why gnuplot is better for line graph?


gnuplot is a simple, freely-available piece of software designed to produce line graphs from columnar text data. Which is what you seem to have. As @Keskiverto says, it can do some higher-dimension contour plotting and vector plots, but from what you have presented in this thread so far you seem to want to plot data with a single independent variable; i.e. a line graph.

Paraview is designed primarily to visualise 3-d data: e.g. from CFD or finite-element software. Whilst it can extract data along a line in space and plot a graph, that is not its primary use. It would be an absolute sledgehammer just to plot a simple line graph.

I use Paraview to visualise 3-d CFD data, but the data format was Ensight Gold, not VTK, and I wrote the routines to produce those in Fortran, not C++.

An intermediate approach would be to use Python with the Matplotlib routines.

You need to show exactly what you want to plot and what type of plot you want to produce, but from what you have written so far Paraview seems overkill.
Thanks for your explanation. I want 3D plot and I want to get the contur of velocity and pressure in different parts of fluid flows.
No it's not always safe because of four columns of header and two columns of data

Wrong.

The reason it may not be safe is that Solution.size() could be less than A.size(). That has nothing to do with columns.
@Cplusc,
OK, Paraview would be fair enough for that (though it's nothing like the contents of your initial post).

What are the formats of your grid, pressure and velocity fields (i.e. what containers are they stored in)?
I want to write the output in vtk format to use in paraview but I don't know how to write in vtk format. I would be really grateful if you could give me a hint for taht.

*Drops hint*

https://www.cs.unc.edu/~taylorr/Comp715/papers/VTK-file-formats.pdf

*Another hint dropped*

https://www.earthmodels.org/software/vtk-and-paraview/vtk-file-formats

DDG search metalink ("vtk format"):

https://duckduckgo.com/?q=vtk+format&t=ffsb&ia=web
Last edited on
@keskiverto A and solution vector are always the same size. That's why I refered to the number of column in header and data as the only possibility for unsafety. By the way the main object of my question is the vtk format writing but I appreciate for pointing this issue.
@lastchance the Container for pressure and velocity is vector of double but for grid is vector of node, node is the object of class Node which has different member functions to get the coordiante.
Last edited on
@George P thanks for sharing the Links. The ASCII *vtu is exactly what I am looking for.
PLEASE learn to do 'net searches yourself before you ask questions. It took me all of about 3 seconds to get the metalink and find all the links I plopped down.

Or gi'e a hint you already tried searching and came up croppers. Simple stuff like "VTK file format" is almost a no brainer when it comes to web searches.

FYI, I ain't even remotely interested in Paraview or VTK file formats, so have zero reason to do a search. You, OTOH, have interest so have a valid reason to do a search yourself.
@George P
I do know net research very well. For including vtk library and have that working I had found all those links before you share. I didn't say with those links I got my answer, I said ASCII VTU format is what I AM looking for and I said thank you just as an appreciation to your answer. With all respect no insistence for you to answer if you ain't even remotely interested.
Last edited on
Cplusc wrote:
I want 3D plot and I want to get the contur of velocity and pressure in different parts of fluid flows.


@Cplusc, I don't use the VTK format with Paraview - I use Ensight Gold. However, I had a go at coding up a simple VTK ASCII, structured-grid filewriter from one of the links given above.

It seems to work in Paraview - screenshot https://imgur.com/a/UbYHAfq
The flow is trivial (plane Poiseuille flow).

If you already have a "VTK library" providing file write routines then you would be better using that. However, you can play around with the code below (and break it down into subroutines). Note that I don't know what sort of CFD grid you are using: structured or unstructured.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

//======================================================================

void writeVTK( ostream &out,                     // output file stream
               const vector<double> &x,          // x coordinates
               const vector<double> &y,          // y coordinates
               const vector<double> &z,          // z coordinates
               const vector<double> &p,          // pressure
               const vector<double> &u,          // velocity-x
               const vector<double> &v,          // velocity-y
               const vector<double> &w,          // velocity-z
               int NX, int NY, int NZ )          // dimensions x, y, z
{
   int N = NX * NY * NZ;

   out << "# vtk DataFile Version 3.0\n";
   out << "Test file\n";
   out << "ASCII\n";

   out << "DATASET STRUCTURED_GRID\n";
   out << "DIMENSIONS " << ' ' << NX << ' ' << NY << ' ' << NZ << '\n';
   out << "POINTS " << N << " double\n";
   for ( int i = 0; i < N; i++ ) out << x[i] << " " << y[i] << " " << z[i] << '\n';

   out << "POINT_DATA " << N << '\n';
   out << "SCALARS pressure double 1\n";
   out << "LOOKUP_TABLE default\n";
   for ( int i = 0; i < N; i++ ) out << p[i] << '\n';

   out << "VECTORS velocity double\n";
   for ( int i = 0; i < N; i++ ) out << u[i] << " " << v[i] << " " << w[i] << '\n';
}

//======================================================================

int main()
{
   int NX = 5, NY = 10, NZ = 3;
   int N = NX * NY * NZ;

   vector<double> x(N), y(N), z(N);
   vector<double> u(N), v(N), w(N), p(N);

   // Some data (plane Poiseuille flow)
   double pressureGradient = 2.5;
   double depth = 2.0;
   double U0 = 1.0;
   int n = 0;
   for ( int k = 0; k < NZ; k++ )
   {
      for ( int j = 0; j < NY; j++ )
      {
         for ( int i = 0; i < NX; i++ )
         {
            x[n] = i;
            y[n] = depth * j / ( NY - 1 );
            z[n] = k;
            u[n] = U0 * ( y[n] / depth ) * ( 1.0 - y[n] / depth );
            v[n] = w[n] = 0.0;
            p[n] = NX - 1 - pressureGradient * i;
            n++;
         }
      }
   }

   ofstream out( "temp.vtk" );
   writeVTK( out, x, y, z, p, u, v, w, NX, NY, NZ );
}

Thanks for your help. I was reading about Ensight Gold format and I found it more convinet and easy to apply.
Last edited on
Topic archived. No new replies allowed.