vector field on gnuplot with u and v components

十年热恋 提交于 2019-12-11 09:32:08

问题


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 is right 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.


回答1:


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.



来源:https://stackoverflow.com/questions/54941564/vector-field-on-gnuplot-with-u-and-v-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!