OpenFOAM

Description

According to the page of OpenFOAM, OpenFOAM is the free, open-source CFD (Computational Fluid Dynamics) software developed primarily by OpenCFD Ltd since 2004. It has a vast range of features for solving complex fluid problems involving chemical reactions, turbulence, heat transfer, acoustics, solid mechanics, and electromagnetism.

Available Versions

  • openfoam/4.1

  • openfoam/5.x (default)

  • openfoam/7

  • openfoam/8

  • openfoam/1912

  • openfoam/2012

  • openfoam/2112

Loading the Module

# Load OpenFOAM
module load openfoam/2012

# Verify installation
foamSystemCheck
foamVersion

Serial Job Submission

submit_openfoam_serial.sh
#!/bin/bash
#SBATCH -J openfoam_serial
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 24:00:00
#SBATCH --mem=8G

export INPUT="cavity.zip"
export OUTPUT="cavity_results.zip"

module load openfoam/2012

job-nanny ./run_serial.sh
run_serial.sh
#!/bin/bash

# Decompress case
unzip cavity.zip
cd cavity

# Run
blockMesh
icoFoam

# Compress results
cd ..
zip -r cavity_results.zip cavity/[0-9]* cavity/constant cavity/system

Parallel Job Submission

submit_openfoam_mpi.sh
#!/bin/bash
#SBATCH -J openfoam_mpi
#SBATCH -N 2
#SBATCH --ntasks-per-node=28
#SBATCH -t 48:00:00
#SBATCH --mem-per-cpu=2G

export INPUT="motorBike.zip"
export OUTPUT="motorBike_results.zip"

module load openfoam/2012

job-nanny ./run_parallel.sh
run_parallel.sh
#!/bin/bash

# Decompress
unzip motorBike.zip
cd motorBike

# Decompose domain
decomposePar -copyZero

# Run in parallel
mpirun -np $SLURM_NTASKS simpleFoam -parallel

# Reconstruct results
reconstructPar

# Compress results
cd ..
zip -r motorBike_results.zip motorBike/[0-9]* motorBike/constant motorBike/system

Example: Cavity Case (icoFoam)

prepare_cavity.sh
#!/bin/bash
#SBATCH -J prepare_cavity
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 00:30:00
#SBATCH --mem=2G

module load openfoam/2012

# Copy tutorial case
cp -r $FOAM_TUTORIALS/incompressible/icoFoam/cavity/cavity .
cd cavity

# Generate mesh
blockMesh

# Run
icoFoam

# Visualize results (for post-processing)
# paraFoam

Case Configuration Files

system/controlDict

application     icoFoam;
startFrom       startTime;
startTime       0;
stopAt          endTime;
endTime         0.5;
deltaT          0.005;
writeControl    timeStep;
writeInterval   20;
purgeWrite      0;
writeFormat     ascii;
writePrecision  6;
writeCompression off;
timeFormat      general;
timePrecision   6;
runTimeModifiable yes;

system/fvSchemes

ddtSchemes
{
    default         Euler;
}

gradSchemes
{
    default         Gauss linear;
}

divSchemes
{
    default         none;
    div(phi,U)      Gauss linear;
}

laplacianSchemes
{
    default         Gauss linear corrected;
}

interpolationSchemes
{
    default         linear;
}

snGradSchemes
{
    default         corrected;
}

system/fvSolution

solvers
{
    p
    {
        solver          PCG;
        preconditioner  DIC;
        tolerance       1e-06;
        relTol          0.05;
    }

    U
    {
        solver          smoothSolver;
        smoother        symGaussSeidel;
        tolerance       1e-05;
        relTol          0;
    }
}

PISO
{
    nCorrectors     2;
    nNonOrthogonalCorrectors 0;
    pRefCell        0;
    pRefValue       0;
}

constant/transportProperties

nu              [0 2 -1 0 0 0 0] 0.01;

constant/polyMesh/blockMeshDict

vertices
(
    (0 0 0)
    (1 0 0)
    (1 1 0)
    (0 1 0)
    (0 0 0.1)
    (1 0 0.1)
    (1 1 0.1)
    (0 1 0.1)
);

blocks
(
    hex (0 1 2 3 4 5 6 7) (20 20 1) simpleGrading (1 1 1)
);

edges
(
);

boundary
(
    movingWall
    {
        type wall;
        faces
        (
            (3 7 6 2)
        );
    }
    fixedWalls
    {
        type wall;
        faces
        (
            (0 4 7 3)
            (2 6 5 1)
            (1 5 4 0)
            (4 5 6 7)
            (0 3 2 1)
        );
    }
);

mergePatchPairs
(
);

Job Array for Multiple Cases

submit_openfoam_array.sh
#!/bin/bash
#SBATCH -J openfoam_array
#SBATCH --array=1-5
#SBATCH -N 2
#SBATCH --ntasks-per-node=28
#SBATCH -t 48:00:00
#SBATCH --mem-per-cpu=2G

CASES=(
    "cavity"
    "motorBike"
    "damBreak"
    "pitzDaily"
    "airFoil"
)

CASE=${CASES[$SLURM_ARRAY_TASK_ID-1]}
export INPUT="${CASE}.zip"
export OUTPUT="${CASE}_results/"

module load openfoam/2012

mkdir -p ${CASE}_results
cd ${CASE}_results
unzip ../${CASE}.zip
cd ${CASE}

# Decompose for parallel execution
decomposePar -copyZero

# Run
mpirun -np $SLURM_NTASKS simpleFoam -parallel

# Reconstruct
reconstructPar

cd ../..

Post-processing

postprocess_openfoam.sh
#!/bin/bash
#SBATCH -J openfoam_post
#SBATCH -N 1
#SBATCH -n 1
#SBATCH -t 02:00:00
#SBATCH --mem=4G

export INPUT="cavity"
export OUTPUT="postprocessing/"

module load openfoam/2012

cd cavity

# Extract forces
forces

# Calculate wall coefficients
wallShearStress

# Extract slices
sample

# Generate plots with Python
cat > plot.py << 'EOF'
import numpy as np
import matplotlib.pyplot as plt

# Read velocity data
data = np.loadtxt('postProcessing/sample/0/U.xy')
x = data[:,0]
Ux = data[:,1]

plt.figure()
plt.plot(x, Ux)
plt.xlabel('x (m)')
plt.ylabel('Ux (m/s)')
plt.title('Velocity profile')
plt.grid(True)
plt.savefig('velocity_profile.png')
EOF

python plot.py

References

See also