File size: 7,326 Bytes
1ca7ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash

# OpenFOAM Simulation Setup Script
# Usage: ./setup_simulation.sh <xxx>
# 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 <subdirectory_name>"
    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"