用法示例
bash
./genCylinderblockMesh.sh -h
Usage: ./genCylinderblockMesh.sh [--x0 X] [--y0 Y] [--z0 Z] --H HEIGHT --r RADIUS --nZ NZ [--nR NR] [--nBox NBOX] [--boxFactor BF] [--out PATH]
./genCylinderblockMesh.sh --x0 10 --y0 15 --z0 20 --H 15 --r 2.5 --nZ 30 --nR 5 --nBox 10 --out ./system/blockMeshDict
结果

脚本
bash
#!/usr/bin/env bash
# Generate blockMeshDict for OpenFOAM-2.3.x using arc edges (no geometry/project).
# Parameters:
# --x0, --y0 : cylinder center (default 0, 0)
# --z0 : start z (default 0)
# --H : cylinder height (REQUIRED)
# --r : cylinder radius (REQUIRED)
# --nR : radial divisions in ring blocks (default 4)
# --nBox : divisions of inner square per side (default 10)
# --nZ : axial divisions (REQUIRED)
# --boxFactor: inner square half-length = boxFactor * r (default 0.5)
# --out : output path (default blockMeshDict)
#
# Example:
# ./gen_blockMesh.sh --H 0.4 --r 0.02 --nZ 160 --out system/blockMeshDict
set -euo pipefail
export LC_ALL=C
# defaults
x0=0.0
y0=0.0
z0=0.0
H=""
r=""
nR=4
nBox=10
nZ=""
boxFactor=0.5
out="blockMeshDict"
# parse args
while [[ $# -gt 0 ]]; do
case "$1" in
--x0) x0="$2"; shift 2;;
--y0) y0="$2"; shift 2;;
--z0) z0="$2"; shift 2;;
--H) H="$2"; shift 2;;
--r) r="$2"; shift 2;;
--nR) nR="$2"; shift 2;;
--nBox) nBox="$2"; shift 2;;
--nZ) nZ="$2"; shift 2;;
--boxFactor) boxFactor="$2"; shift 2;;
--out) out="$2"; shift 2;;
-h|--help)
echo "Usage: $0 [--x0 X] [--y0 Y] [--z0 Z] --H HEIGHT --r RADIUS --nZ NZ [--nR NR] [--nBox NBOX] [--boxFactor BF] [--out PATH]"
exit 0;;
*) echo "Unknown arg: $1" >&2; exit 1;;
esac
done
# required checks
if [[ -z "${H}" || -z "${r}" || -z "${nZ}" ]]; then
echo "Error: --H, --r, and --nZ are required. Try -h for help." >&2
exit 1
fi
# compute derived values using awk (portable float math)
read -r zMin zMax box rs2 cx cy <<< "$(
awk -v z0="$z0" -v H="$H" -v r="$r" -v bf="$boxFactor" -v x0="$x0" -v y0="$y0" 'BEGIN{
zMin = z0; zMax = z0 + H;
box = bf * r;
rs2 = r / sqrt(2.0);
printf("%.10f %.10f %.10f %.10f %.10f %.10f", zMin, zMax, box, rs2, x0, y0);
}'
)"
# helper to format a point (center + offset)
pt() {
# $1: dx, $2: dy, $3: z
awk -v cx="$cx" -v cy="$cy" -v dx="$1" -v dy="$2" -v z="$3" 'BEGIN{
printf("(%.10f %.10f %.10f)", cx+dx, cy+dy, z);
}'
}
# helper for arc mid points (center + offset)
mid() {
# $1: mx, $2: my, $3: z
awk -v cx="$cx" -v cy="$cy" -v mx="$1" -v my="$2" -v z="$3" 'BEGIN{
printf("(%.10f %.10f %.10f)", cx+mx, cy+my, z);
}'
}
# ensure output dir
mkdir -p "$(dirname "$out")"
# generate blockMeshDict
cat > "$out" <<EOF
/*--------------------------------*- C++ -*----------------------------------*\\
| ========= | |
| \\\\ / Field | OpenFOAM: The Open Source CFD Toolbox |
| \\\\ / Operation | Version: 2.3.x |
| \\\\ / And | Web: www.OpenFOAM.org |
| \\\\/ Manipulation | |
\\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// Auto-generated: x0=${cx}, y0=${cy}, z in [${zMin}, ${zMax}], r=${r}, box=${box}, nR=${nR}, nBox=${nBox}, nZ=${nZ}
convertToMeters 1;
vertices
(
// Inner square @ zMin
$(pt -$box -$box $zMin) // 0
$(pt $box -$box $zMin) // 1
$(pt -$box $box $zMin) // 2
$(pt $box $box $zMin) // 3
// Outer ring @ zMin (45-degree points)
$(pt -$rs2 -$rs2 $zMin) // 4
$(pt $rs2 -$rs2 $zMin) // 5
$(pt -$rs2 $rs2 $zMin) // 6
$(pt $rs2 $rs2 $zMin) // 7
// Inner square @ zMax
$(pt -$box -$box $zMax) // 8
$(pt $box -$box $zMax) // 9
$(pt -$box $box $zMax) // 10
$(pt $box $box $zMax) // 11
// Outer ring @ zMax
$(pt -$rs2 -$rs2 $zMax) // 12
$(pt $rs2 -$rs2 $zMax) // 13
$(pt -$rs2 $rs2 $zMax) // 14
$(pt $rs2 $rs2 $zMax) // 15
);
blocks
(
// ring blocks + center block
hex ( 4 5 1 0 12 13 9 8) (${nBox} ${nR} ${nZ}) simpleGrading (1 1 1)
hex ( 4 0 2 6 12 8 10 14) (${nR} ${nBox} ${nZ}) simpleGrading (1 1 1)
hex ( 1 5 7 3 9 13 15 11) (${nR} ${nBox} ${nZ}) simpleGrading (1 1 1)
hex ( 2 3 7 6 10 11 15 14) (${nBox} ${nR} ${nZ}) simpleGrading (1 1 1)
hex ( 0 1 3 2 8 9 11 10) (${nBox} ${nBox} ${nZ}) simpleGrading (1 1 1)
);
edges
(
// arcs @ zMin
arc 4 5 $(mid 0.0 -$r $zMin)
arc 5 7 $(mid $r 0.0 $zMin)
arc 6 7 $(mid 0.0 $r $zMin)
arc 4 6 $(mid -$r 0.0 $zMin)
// arcs @ zMax
arc 12 13 $(mid 0.0 -$r $zMax)
arc 13 15 $(mid $r 0.0 $zMax)
arc 12 14 $(mid -$r 0.0 $zMax)
arc 14 15 $(mid 0.0 $r $zMax)
);
boundary
(
INLET
{
type patch;
faces
(
(0 1 3 2)
(0 2 6 4)
(0 1 5 4)
(1 5 7 3)
(2 3 7 6)
);
}
OUTLET
{
type patch;
faces
(
(8 9 11 10)
(8 10 14 12)
(8 9 13 12)
(9 13 15 11)
(10 11 15 14)
);
}
WALL
{
type wall;
faces
(
(4 12 14 6)
(4 5 13 12)
(5 13 15 7)
(6 7 15 14)
);
}
);
// ************************************************************************* //
EOF
echo "Generated: $out"