我正在尝试使用一个名为 ConstVolt_0flow 的 OpenFOAM 案例,建议我编写一个 bash 命令文件,该文件会定期切换 ConstVolt_0flow/system/Air/fvScheme 中的条目:
gradSchemes { 默认高斯线性; }
到
gradSchemes { 默认高斯立方; }
openFoam 是 v2.4。由于我没有编写任何与 linux 相关的东西的经验,我无法判断 chatgpt 编写的代码是否正确:
#!/bin/bash
# Path to the fvScheme file
fvSchemeFile="system/Air/fvScheme"
# Check the current scheme
currentScheme=$(grep -Po '(?<=default\s+Gauss\s+)\w+' "$fvSchemeFile")
# Switch the scheme
if [ "$currentScheme" == "linear" ]; then
sed -i 's/default\s\+Gauss\s\+linear/default Gauss cubic/' "$fvSchemeFile"
else
sed -i 's/default\s\+Gauss\s\+cubic/default Gauss linear/' "$fvSchemeFile"
fi
echo "Switched gradSchemes in $fvSchemeFile to Gauss $currentScheme"
答案1
那么,如何通过交换两个完整的文件来更简单的解决方案呢?
ConstVolt_0flow/system/Air/fvScheme
使用以下文件版本创建gradSchemes { default Gauss linear; }
ConstVolt_0flow/system/Air/fvScheme.new
使用以下文件版本创建gradSchemes { default Gauss cubic; }
- 创建执行交换的文件
./doswap.sh
:
#!/bin/bash
set -e
cp 'ConstVolt_0flow/system/Air/fvScheme' 'ConstVolt_0flow/system/Air/fvScheme.tmp'
mv 'ConstVolt_0flow/system/Air/fvScheme.new' 'ConstVolt_0flow/system/Air/fvScheme'
mv 'ConstVolt_0flow/system/Air/fvScheme.tmp' 'ConstVolt_0flow/system/Air/fvScheme.new'
# Note: you may need to add a line here to restart your service
# However, if this "OpenFOAM" simulation software should not be restarted
# then it might be off topic for Ask Ubuntu
echo "Switched gradSchemes in fvScheme to other Gauss"
- 创建
./looper.sh
脚本:
#!/bin/bash
while true; do
./doswap.sh
sleep 1m # This is the period. It is set to 1 minute currently. You can adjust it
# Or better, use systemd or some daemon to call ./doswap.sh instead of this
done
chmod +x doswap.sh looper.sh
(或右键单击属性,并将两个文件都设置为可执行文件)- 通过在终端中运行来启动所有内容
./looper.sh
(或者只需双击它,但双击可能不会显示终端输出) - 按 CTRL+C 停止