linux / MSVC 2012中的管道异常行为



我有一个粒子系统,我基本上跟踪它的坐标。到目前为止,我将所有坐标输出到文件中,然后使用gnuplot脚本绘制它们,并使用ffmpeg从png中创建视频。为了跳过所有的文件I/O,我首先尝试了OpenGL,但无济于事,所以我想建立一个管道到gnuplot。

我在MSVC 2012中这样做:

FILE *gnuplotpipe = _popen(GNUPLOT_NAME, "w");
setvbuf(gnuplotpipe, NULL, _IONBF, NULL);
//setbuf(gnuplotpipe, NULL);
fprintf(gnuplotpipe,"set term pngn");
fprintf(gnuplotpipe,"set size 720, 480n");
fprintf(gnuplotpipe,"set size ratio -1n");
fprintf(gnuplotpipe,"unset keyn");
fprintf(gnuplotpipe,"set xrange [-%d:%d]n", RANGE+100,RANGE+100);
fprintf(gnuplotpipe,"set yrange [-%d:%d]n", RANGE+100,RANGE+100);
for(time-steps)
    //do stuff
    fprintf(gnuplotpipe,"plot '-'n");
    for(all particles)
          // calculate coordinates
          fprintf(gnuplotpipe,"%f %fn", particle[i]->x, particle[i]->y);
    end for
    fprintf(gnuplotpipe, "en");
    //fflush(gnuplotpipe);
end for
_pcloce(gnuplotpipe);

,在linux中,gnuplot -persist代替pgnuplot -persist, popenpclose, setbuf代替setvbuf。这已经经过了大量的试验和错误。

在linux中是相当一致的。我可以看到plot '-'命令在控制台的每一个时间步,然后是难以理解的文本。没有打开。在Windows中,这是惊人的,因为有时它会工作,我可以看到gnuplot"MS Windows"窗口上的数据流(我的意思不是控制台输出,这本身就是惊人的,因为我没有使用wgnuplot_pipes.exe)。没有绘图。窗户关上了。其他时候,对于几个时间步,它将绘制,但在不同的窗口为每个时间步。有时,它会崩溃,或者它会工作,但发送难以理解的文本。

我大部分时间都在处理100个粒子。我可以有多达一万个时间步长。我不知道这算不算多。我怀疑一个截止或在窗口的东西,虽然我设置缓冲区为NULL。我在Win7 x64中的MSVC和Linux Mint x64中的Qt在VirtualBox中工作。我使用gnuplot 4.7 (win)和4.6.0.8 (linux)。我在SO中看过非常广泛,我发现的唯一问题是-persist并没有真正在窗口中持续存在。

只是为了让问题没有得到回答…这是你的过程的python版本,使用x11终端。它会弹出一个带有傻傻的动画的窗口。

看,如果其他一些驱动程序试图为每个帧打开一个窗口,我一点也不会感到惊讶

import subprocess
f=subprocess.Popen(['/usr/bin/gnuplot','-persist'],
                      stdin=subprocess.PIPE,shell=False)
f.stdin.write('set term x11 n')
f.stdin.write('set yrange [-2:2]n')
import math
i=0
d=1
count=0
while count<1000 :
 count+=1
 if abs(i)==50 : d *= -1
 i+=d 
 f.stdin.write('plot '-' with linespoints pt 4 ps 2 n')
 for j in range(0,500):
  f.stdin.write('%f %fn'%(float(j),math.sin(float(j)/float(abs(i)+1))))
 f.stdin.write('en')

最新更新