具有u和v分量的gnuplot上的向量字段 [英] vector field on gnuplot with u and v components

查看:113
本文介绍了具有u和v分量的gnuplot上的向量字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在求解Navier-Stokes方程,用于不可压缩的流体流过带有障碍物的正方形区域.作为输出,我分别得到速度的XY分量作为NxN矩阵.如何在gnuplot中绘制矢量场.

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?

更新

我尝试按照@LutzL的指示进行操作,但是我的代码似乎有问题.这段代码一切都还好吗?

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.

推荐答案

您需要将结果保存在带有行的文本文件中

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

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

对于所有对i,j

.然后将gnuplot与传统"矢量字段命令一起使用.

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

如果您在该文件中添加了其他列,则仅需要使用using,并且要显示的向量不是(仅是)第3列和第4列.一种用途是计算比例因子R[i,j]以显示X/R, Y/R.把它放在第五位

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]

并用using 1:2:($3/$5):($4/$5)调用以在gnuplot中执行缩放.

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

更新中的代码和生成的图像中,人们看到矢量场太大而无法绘制.在适当的时间步长上用dt进行缩放,在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.

这篇关于具有u和v分量的gnuplot上的向量字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆