如何在 gnuplot 中使用 bash 输出作为变量? OpenFOAM 日志示例

如何在 gnuplot 中使用 bash 输出作为变量? OpenFOAM 日志示例

我有一个这样的日志文件:

    Starting time loop

Courant Number mean: 0 max: 0
deltaT = 0.0012
Time = 0.0012

DILUPBiCGStab:  Solving for Ux, Initial residual = 1, Final residual = 4.8276e-08, No Iterations 1
DILUPBiCGStab:  Solving for Uy, Initial residual = 1, Final residual = 2.23172e-07, No Iterations 1
DILUPBiCGStab:  Solving for Uz, Initial residual = 1, Final residual = 2.80701e-08, No Iterations 1
DILUPBiCGStab:  Solving for T, Initial residual = 0.999922, Final residual = 4.3295e-07, No Iterations 1
DICPCG:  Solving for p_rgh, Initial residual = 1, Final residual = 0.00904671, No Iterations 163
time step continuity errors : sum local = 8.39133e-07, global = -8.63793e-09, cumulative = -8.63793e-09
DILUPBiCGStab:  Solving for epsilon, Initial residual = 0.00291157, Final residual = 2.1992e-06, No Iterations 1
DILUPBiCGStab:  Solving for k, Initial residual = 1, Final residual = 0.00101111, No Iterations 1
ExecutionTime = 2.53 s  ClockTime = 3 s

Courant Number mean: 0.00447015 max: 0.356735
deltaT = 0.00143547
Time = 0.00263547

DILUPBiCGStab:  Solving for Ux, Initial residual = 0.334632, Final residual = 3.26106e-05, No Iterations 1
DILUPBiCGStab:  Solving for Uy, Initial residual = 0.325756, Final residual = 1.62512e-05, No Iterations 1
DILUPBiCGStab:  Solving for Uz, Initial residual = 0.379719, Final residual = 2.45476e-05, No Iterations 1
DILUPBiCGStab:  Solving for T, Initial residual = 0.110323, Final residual = 1.51228e-05, No Iterations 1
DICPCG:  Solving for p_rgh, Initial residual = 0.135502, Final residual = 0.00128893, No Iterations 152
time step continuity errors : sum local = 3.78267e-06, global = 5.94272e-08, cumulative = 5.07892e-08
DILUPBiCGStab:  Solving for epsilon, Initial residual = 0.0132143, Final residual = 9.90056e-06, No Iterations 1
DILUPBiCGStab:  Solving for k, Initial residual = 0.27268, Final residual = 0.000279373, No Iterations 1
ExecutionTime = 3.86 s  ClockTime = 4 s

我的 gnuplot 脚本是这样的:

set terminal png
set output 'res.png'
set logscale y
set title "Residuals"
set ylabel 'Residual'
set xlabel 'Time [s]'
dt = 0.001
plot "< cat log | grep 'Solving for Ux' | cut -d' ' -f9 | tr -d ','" using ($0*dt):1 title 'Ux' with lines,\
"< cat log | grep 'Solving for Uy' | cut -d' ' -f9 | tr -d ','" using ($0*dt):1 title 'Uy' with lines,\
"< cat log | grep 'Solving for Uz' | cut -d' ' -f9 | tr -d ','" using ($0*dt):1 title 'Uz' with lines,\
"< cat log | grep 'Solving for T' | cut -d' ' -f9 | tr -d ','" using ($0*dt):1 title 'T' with lines,\
"< cat log | grep 'Solving for p' | cut -d' ' -f9 | tr -d ','" using ($0*dt):1 title 'p' with lines
set terminal x11
set output
replot
pause 1
reread

基本上,它获取“解决 Ux”,然后获取“初始残差”处的第一个数字,然后绘制它。每个块(从 Courant Number 平均值开始到 Clocktime)都是 1 次迭代。实际时间是迭代* deltaT。因此,第一次迭代是 0.0012 秒,第二次迭代是 2*0.00143547 = 0.0028 秒......等等。

如果 deltaT 是常数,那么我可以像上面的脚本一样设置它并在绘图时将其相乘,例如使用 ($0*dt):1。然后从上面,我只是为 dt 设置了一个值。

但是,我的 deltaT 发生了变化,那么我们如何将其贡献给代码呢?我可以使用以下 bash 命令来获取 deltaT 的值:

cat log | grep -v -e 'ClockTime =' -e 'ExecutionTime = ' -e 'Solving for' -e 'time step' -e 'Courant' -e 'Time' | cut -d' ' -f3 | tr -cd '[:digit:].\n'

但我不知道如何将这些设置为变量来绘制。我试过:

dt = "cat log | grep -v -e 'ClockTime =' -e 'ExecutionTime = ' -e 'Solving for' -e 'time step' -e 'Courant' -e 'Time' | cut -d' ' -f3 | tr -cd '[:digit:].\n'"

但无法绘制它。 Gnuplot 给出了类似“管道错误”的信息。

答案1

我们可以稍微简化一下 gnuplot 部分,因为它能够找到第 8 列中的数字,而不需要通过cut|trcat。例如,

plot \
"< <log grep 'Solving for Ux'" using ($0*dt):8 title 'Ux' with lines,\

然后我们可以做一个虚拟图,仅搜索“deltaT”,并setdt()使用行号“$0”(+1,因为行从 0 开始)和第 3 列中的数字调用函数:

plot \
"< <log grep 'deltaT'" using (setdt($0+1,$3)):NaN notitle with lines,\

使用NaN非数字可以避免绘制该数据。

该函数将值保存在数组中,并返回值 0:

array dt[99]
setdt(row,value) = (dt[row] = row*value,0)

我们通过使用行号索引数组来在绘图命令中使用这些值:

plot \
"< <log grep 'deltaT'"         using (setdt($0+1,$3)):NaN notitle with lines,\
"< <log grep 'Solving for Ux'" using (dt[$0+1]):8 title 'Ux' with lines,\
...

相关内容