#!/bin/bash

# Check if the mesh file (polyMesh) exists
if [ ! -f constant/polyMesh/boundary ]; then
    echo "Error: The polyMesh folder not found!"
    exit 1
fi

# Check if decomposeParDict exists, and if not, create it
if [ ! -f system/decomposeParDict ]; then
    decomposePar -force
fi

# Decompose the case
decomposePar

# Run the simulation in parallel and use pyFoamPlotRunner to generate plots
pyFoamPlotRunner.py mpirun -np 5 sonicFoam -parallel

# Reconstruct the case after simulation completion
reconstructPar

# Remove processor directories
rm -rf processor*

# Create the 'pyfoam' directory if it doesn't exist
mkdir -p pyfoam

# Move files starting with 'PyFoam' to the 'pyfoam' directory
mv PyFoam* pyfoam/ 2>/dev/null

# Move the 'Gnuplotting.analyzed' folder to the 'pyfoam' directory
mv Gnu* pyfoam/ 2>/dev/null

# Run parafoam
paraFoam
