#!/bin/bash # OpenFOAM Simulation Setup Script # Usage: ./setup_simulation.sh # Where xxx is the subdirectory name under the outputs path # ./setup_simulation.sh gen_meshes_1002 # ./setup_simulation_single.sh test_noreseed_myvae_100k_opt_diffout_givenshape_drivaernet_oridrag_w_phys_all set -e # Exit on any error # Check if argument is provided if [ $# -ne 1 ]; then echo "Usage: $0 " echo "Example: $0 gen_meshes_1002" exit 1 fi # Input parameter SUBDIR_NAME="$1" # Define paths SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TEMPLATE_DIR="/mnt/cvlab/scratch/cvlab/home/hantzhan/cfd_workflow/template" OUTPUT_BASE="/mnt/cvlab/scratch/cvlab/home/hantzhan/Dora/pytorch_lightning/outputs/Diffusion-Conditional/train_conditional_CLIPasso" OUTPUT_BASE="/mnt/cvlab/scratch/cvlab/home/hantzhan/Dora/pytorch_lightning/outputs/Diffusion-Unconditional" SOURCE_DIR="${OUTPUT_BASE}/${SUBDIR_NAME}" echo "=== OpenFOAM Simulation Setup ===" echo "Subdirectory: ${SUBDIR_NAME}" echo "Source directory: ${SOURCE_DIR}" # Check if source directory exists if [ ! -d "${SOURCE_DIR}" ]; then echo "Error: Source directory does not exist: ${SOURCE_DIR}" exit 1 fi # Check if template directory exists if [ ! -d "${TEMPLATE_DIR}" ]; then echo "Error: Template directory does not exist: ${TEMPLATE_DIR}" echo "Please update TEMPLATE_DIR in the script to point to the correct template location." exit 1 fi # Create main SUBDIR_NAME directory MAIN_DIR="${SCRIPT_DIR}/${SUBDIR_NAME}" echo "Creating main directory: ${MAIN_DIR}" if [ -d "${MAIN_DIR}" ]; then echo "Warning: Main directory already exists: ${MAIN_DIR}" read -p "Do you want to overwrite it? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Exiting without making changes." exit 0 fi echo "Removing existing directory..." rm -rf "${MAIN_DIR}" fi mkdir -p "${MAIN_DIR}" # Find all subdirectories in the source directory echo "Searching for subdirectories in: ${SOURCE_DIR}" SUBDIRS=($(find "${SOURCE_DIR}" -maxdepth 1 -type d ! -path "${SOURCE_DIR}")) if [ ${#SUBDIRS[@]} -eq 0 ]; then echo "Error: No subdirectories found in ${SOURCE_DIR}" exit 1 fi echo "Found ${#SUBDIRS[@]} subdirectory(ies):" for subdir in "${SUBDIRS[@]}"; do echo " $(basename "${subdir}")" done # Process each subdirectory for SUBDIR_PATH in "${SUBDIRS[@]}"; do # Extract the basename of the subdirectory SUBDIR_BASENAME=$(basename "${SUBDIR_PATH}") # Find STL files in this subdirectory (only *416*.stl) echo "" echo "=== Processing subdirectory: ${SUBDIR_BASENAME} ===" echo "Searching for *416*.stl in: ${SUBDIR_PATH}" STL_FILES=($(find "${SUBDIR_PATH}" -name "*416*.stl" -type f)) if [ ${#STL_FILES[@]} -eq 0 ]; then echo "Warning: *416*.stl not found in ${SUBDIR_PATH}, skipping..." continue fi echo "Found *416*.stl in ${SUBDIR_BASENAME}:" for stl_file in "${STL_FILES[@]}"; do echo " $(basename "${stl_file}")" done # Process each STL file in this subdirectory for STL_PATH in "${STL_FILES[@]}"; do # Extract the base name of the STL file (without .stl extension) STL_BASENAME=$(basename "${STL_PATH}" .stl) # Create simulation directory name using subdirectory basename + STL basename (as subdirectory of MAIN_DIR) SIM_DIR="${MAIN_DIR}/${SUBDIR_BASENAME}_${STL_BASENAME}" echo "" echo "--- Processing STL: ${STL_BASENAME} from ${SUBDIR_BASENAME} ---" echo "Creating simulation directory: ${SIM_DIR}" # Check if simulation directory already exists if [ -d "${SIM_DIR}" ]; then echo "Warning: Simulation directory already exists: ${SIM_DIR}" read -p "Do you want to overwrite it? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Skipping ${STL_BASENAME} from ${SUBDIR_BASENAME}" continue fi echo "Removing existing directory..." rm -rf "${SIM_DIR}" fi # Copy template to simulation directory echo "Copying template from: ${TEMPLATE_DIR}" cp -r "${TEMPLATE_DIR}" "${SIM_DIR}" # Create triSurface directory if it doesn't exist TRISURFACE_DIR="${SIM_DIR}/constant/triSurface" # mkdir -p "${TRISURFACE_DIR}" # Copy and rename STL file CAR_STL_PATH="${TRISURFACE_DIR}/car.stl" echo "Copying STL file: ${STL_PATH} -> ${CAR_STL_PATH}" cp "${STL_PATH}" "${CAR_STL_PATH}" # Verify the STL file was copied successfully if [ ! -f "${CAR_STL_PATH}" ]; then echo "Error: Failed to copy STL file to ${CAR_STL_PATH}" continue fi # Call setup_simulation function from sim.py echo "Calling setup_simulation for: ${SIM_DIR}" cd "${SCRIPT_DIR}" # Use Python to call the setup_simulation function python3 -c " import sys sys.path.append('${SCRIPT_DIR}') from sim import setup_simulation import os simdir = '${SIM_DIR}' print(f'Setting up simulation for: {simdir}') try: setup_simulation(simdir) print(f'Successfully set up simulation for: {os.path.basename(simdir)}') except Exception as e: print(f'Error setting up simulation for {os.path.basename(simdir)}: {e}') sys.exit(1) " if [ $? -eq 0 ]; then echo "✓ Successfully set up simulation for: ${STL_BASENAME} from ${SUBDIR_BASENAME}" else echo "✗ Failed to set up simulation for: ${STL_BASENAME} from ${SUBDIR_BASENAME}" continue fi # Print summary information echo "Simulation directory created: ${SIM_DIR}" echo "STL file location: ${CAR_STL_PATH}" # Check if config file was updated CONFIG_FILE="${SIM_DIR}/include/config" if [ -f "${CONFIG_FILE}" ]; then echo "Configuration file updated: ${CONFIG_FILE}" # Extract and display some key parameters echo "Key parameters:" grep -E "Aref|vr[1-4]_" "${CONFIG_FILE}" 2>/dev/null || echo " (Could not extract parameters)" fi done done echo "" echo "=== Setup Complete ===" echo "Created simulation directories for *416*.stl files in subdirectories of: ${SOURCE_DIR}" echo "You can now run OpenFOAM simulations in the created directories." # Optional: List all created simulation directories echo "" echo "Created simulation directories:" for SUBDIR_PATH in "${SUBDIRS[@]}"; do SUBDIR_BASENAME=$(basename "${SUBDIR_PATH}") STL_FILES_IN_SUBDIR=($(find "${SUBDIR_PATH}" -name "*416*.stl" -type f)) for STL_PATH in "${STL_FILES_IN_SUBDIR[@]}"; do STL_BASENAME=$(basename "${STL_PATH}" .stl) SIM_DIR="${MAIN_DIR}/${SUBDIR_BASENAME}_${STL_BASENAME}" if [ -d "${SIM_DIR}" ]; then echo " ${SIM_DIR}" fi done done echo "" echo "Next steps:" echo "1. Navigate to a simulation directory" echo "2. Check the mesh quality: checkMesh" echo "3. Run the simulation: ./Allrun" echo "4. Monitor progress: tail -f log.simpleFoam"