mend4x
mend4x

Reputation: 213

vector field on gnuplot with u and v components

I'm solving Navier-Stokes equation for incompressible fluid flow through a square region with obstacle. As an output I get X and Y components of velocity as NxN matrix each. How to plot vector field for it in gnuplot.

I found this answer but I can't understand what values to put for x, y, dx, dy.

Can anyone explain how to use my output to plot vector field?

UPDATE

I tried doing as @LutzL said, but something seems to be wrong with my code. Is everything alright with this code?

int main() {
    ifstream finu("U"), finv("V");
    int N = 41, M = 41;
    auto
            **u = new double *[N],
            **v = new double *[N];
    for (int i = 0; i < N; i++) {
        u[i] = new double[M];
        v[i] = new double[M];
    }
    double
            dx = 1.0 / (N - 1),
            dy = 1.0 / (M - 1);

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            finu >> u[i][j];
            finv >> v[i][j];
        }
    }

    ofstream foutvec("vec");

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            foutvec << dx * i << "\t" << dy * j << "\t" << u[i][j] << "\t" << v[i][j] << endl;
        }
    }
    ofstream plt("graph.plt");
    plt << "set term pngcairo"
           "\nset title 'Navier-Stokes Equation'"
           "\nset output 'vec.png'"
           "\nplot 'vec' w vec";
    plt.close();
    system("gnuplot graph.plt");
    return 0;
}

As an output I get a bit weird field.

Upvotes: 2

Views: 1368

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25972

You need to save your result in a text file with lines

x[i]  y[j]  X[i,j]  Y[i,j]

for all of the pairs i,j. Then use gnuplot with the "traditional" vector field command.

You need only use using if you put additional columns into that file, and the vectors to display are not (simply) the 3rd and 4th columns. One use might be that you compute a scaling factor R[i,j] to display X/R, Y/R. Put that into 5th place

x[i]  y[j]  X[i,j]  Y[i,j]  R[i,j]

and call with using 1:2:($3/$5):($4/$5) to perform the scaling in gnuplot.


In the code in the update and the resulting image, one sees that the vector field is too large to plot. Scale with dt for some reasonable time step, in the gnuplot commands this could be done via

dt = 0.01
plot 'vec' u 1:2:(dt*$3):(dt*$4) w vec

The incomplete plot hints to an incomplete data file on the disk. Flush or close the output stream for the vector data.

Upvotes: 3

Related Questions