Allrun
Allrun
#!/bin/sh
cd ${0%/*} || exit 1 # run from this directory
./replace
# Source tutorial run functions
. $WM_PROJECT_DIR/bin/tools/RunFunctions
application=`getApplication`
runApplication $application
# -----------------------------------------------------------------end-of-file
replace
パラメータスイープされ生成されたairFoil2D/0/U
のファイルから値を抜き出し、x方向の速度, y方向の速度を抜き出して、正弦, 余弦の値を計算しています。またその結果をcontrolDict
ファイル中の変数を置換し与えています。
replace
#!/bin/bash
vel=($(grep "internalField " 0/U | awk -F "[()]" '{print $2}'))
u=${vel[0]}
v=${vel[1]}
n=`echo "sqrt($u*$u + $v*$v)" | bc -l | awk '{printf "%f", $0}'`
nj=`echo "$v/$n" |bc -l | awk '{printf "%f", $0}'`
ni=`echo "$u/$n" |bc -l | awk '{printf "%f", $0}'`
#echo $ni, $nj, $n
sed -i 's/liftDir.*/liftDir ( -'$nj' '$ni' 0 );/' system/controlDict
sed -i 's/dragDir.*/dragDir ( '$ni' '$nj' 0 );/' system/controlDict
sed -i 's/magUInf.*/magUInf '$n';/' system/controlDict
controlDict
解説
function{...} 部分がオリジナルから追記したコードです。
extract.py
"controlDict" source code
controlDict
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 2.0.1 |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
location "system";
object controlDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
application simpleFoam;
startFrom latestTime;
startTime 0;
stopAt endTime;
endTime 500;
deltaT 1;
writeControl timeStep;
writeInterval 50;
purgeWrite 0;
writeFormat ascii;
writePrecision 6;
writeCompression off;
timeFormat general;
timePrecision 6;
runTimeModifiable true;
functions
{
forces
{
type forceCoeffs;
functionObjectLibs ( "libforces.so" );
outputControl timeStep;
outputInterval 1;
patches
(
wall
);
pName p;
UName U;
rhoName rhoInf; // Indicates incompressible
log true;
rhoInf 1; // Redundant for incompressible
liftDir ( -0.13921358 0.990262380 0 );
dragDir ( 0.99026238 0.13921358 0 );
CofR (0 0 0);
pitchAxis (0 0 1);
magUInf 26.0032094173;
lRef 1;
Aref 1;
}
}
// ************************************************************************* //
U.inp_template
解説
x_velocity
, y_velocity
は Rescaleの並列設定の画面で設定した変数と連動
します。string("0.00")
は、表示フォーマットを決めています。
詳細はこちらをご参照ください。
"U.inp_template" source code
U.inp_template
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 2.0.1 |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class volVectorField;
object U;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 1 -1 0 0 0 0];
internalField uniform (${x_velocity?string("0.00")} ${y_velocity?string("0.00")} 0);
boundaryField
{
inlet
{
type freestream;
freestreamValue uniform (${x_velocity?string("0.00")} ${y_velocity?string("0.00")} 0);
}
outlet
{
type freestream;
freestreamValue uniform (${x_velocity?string("0.00")} ${y_velocity?string("0.00")} 0);
}
wall
{
type fixedValue;
value uniform (0 0 0);
}
frontAndBack
{
type empty;
}
}
// ************************************************************************* //
extract.py
解説
ポスト用スクリプトのポイントは標準出力に下記フォーマットで抽出した値を表示させることです。詳細はこちらを参照してください。
bashのケース
printf "%s\t%s\n" "variable" "$stringValue"
pythonのケース
print 'variable' + '\t' + stringValue
"extract.py" source code
extract.py
#!/usr/bin/env python
# extract.py
import argparse
import collections
import re
import sys
# parse args
parser = argparse.ArgumentParser(description='Extract the last specified'
' value of a variable or variables from an '
'OpenFoam output file.')
parser.add_argument('output_file', metavar='output-file',
type=argparse.FileType('r'),
help='Output file from which to extract variable values.')
parser.add_argument('labels', metavar='variable-label', type=str, nargs='+',
help='Variable labels to match for extracting values.')
args = parser.parse_args()
labels = args.labels
output_file = args.output_file
# setup ordered dict to store results
seed = [(label, '*** not found ***') for label in labels]
results = collections.OrderedDict(seed)
# setup regular expressions for label/value matching
# exponential notation
e = '-?(?:\\d+\\.?\\d*|\\.\\d+)[eEdD](?:\\+|-)?\\d+'
# floating point
f = '-?\\d+\\.\\d*|-?\\.\\d+'
# integer
i = '-?\\d+'
# numeric field
value = e + '|' + f + '|' + i
# label
label = '\\w+(?::\\w+)*'
# regular expression for standard output format
regex = re.compile('^\s*(' + label + ')\s{1,}=\s(' + value + ')$')
# extract the results from the output file
try:
for line in output_file:
m = regex.match(line)
# store if a match is found and the label is a target
# overwrite existing values
if m and m.group(1) in results:
results[m.group(1)] = m.group(2)
finally:
output_file.close()
# send to stdout
for k, v in results.items():
print '%s\t%s' % (k, v)