#!/bin/bash # --- Configuration --- SOURCE_TEMPLATE_DIR="template" SETUP_FILE="setup.json" TMP_DIR="tmp" LOG_FILE="simulation_log.csv" MULTIPLIER=1.5 INITIAL_VALUE=2 # --- Check Dependencies and Source Folder --- if ! command -v jq &> /dev/null; then echo "❌ Error: 'jq' command not found. Please install it." >&2 exit 1 fi if [[ ! -d "$SOURCE_TEMPLATE_DIR" ]]; then echo "❌ Error: Source template directory '$SOURCE_TEMPLATE_DIR' not found." >&2 exit 1 fi # Initialize the log file with a header if [[ ! -f "$LOG_FILE" ]]; then echo "X,Y,Z,Total,Status" > "$LOG_FILE" fi # --- Main Logic Loop (Step 10: Repeat until NOK) --- # Initialize dimensions to the initial value X_DIM=100 Y_DIM=100 Z_DIM=100 while true; do # Calculate new X dimension: X_DIM * 1.5, then truncate result to integer X_DIM=$(echo "scale=0; $X_DIM * $MULTIPLIER / 1" | bc) # Calculate new Y dimension: Y_DIM * 1.5, then truncate result to integer Y_DIM=$(echo "scale=0; $Y_DIM * $MULTIPLIER / 1" | bc) # Calculate new Z dimension: Z_DIM * 1.5, then truncate result to integer Z_DIM=$(echo "scale=0; $Z_DIM * $MULTIPLIER / 1" | bc) #X_DIM=$((X_DIM * MULTIPLIER)) #Y_DIM=$((Y_DIM * MULTIPLIER)) #Z_DIM=$((Z_DIM * MULTIPLIER)) TOTAL_CELLS=$((X_DIM * Y_DIM * Z_DIM)) echo "===================================================" echo "🔄 Running iteration with X=${X_DIM}, Y=${Y_DIM}, Z=${Z_DIM}" # --- Step 1: Copy and Cleanup --- rm -rf "$TMP_DIR" cp -r "$SOURCE_TEMPLATE_DIR" "$TMP_DIR" if [[ $? -ne 0 ]]; then echo "❌ FATAL: Failed to copy template folder. Exiting." >&2 exit 1 fi # Delete all files/folders in 'tmp' except for 'setup.json' find "$TMP_DIR" -mindepth 1 ! -name "$SETUP_FILE" -exec rm -rf {} + CURRENT_SETUP="$TMP_DIR/$SETUP_FILE" # --- Steps 2 & 3: Read, Multiply, and Record New Values --- # Use jq to read and multiply the current dimensions # and update the setup.json file in a single step NEW_JSON=$(jq \ --arg x "$X_DIM" --arg y "$Y_DIM" --arg z "$Z_DIM" \ '.numberCellsXDirection.value = ($x | tonumber) | .numberCellsYDirection.value = ($y | tonumber) | .numberCellsZDirection.value = ($z | tonumber)' \ "$CURRENT_SETUP") # Update the file with the new dimensions echo "$NEW_JSON" > "$CURRENT_SETUP" # Generate the new folder name NEW_FOLDER_NAME="${X_DIM}_${Y_DIM}_${Z_DIM}" # --- Step 4: Rename the 'tmp' folder --- mv "$TMP_DIR" "$NEW_FOLDER_NAME" if [[ $? -ne 0 ]]; then echo "❌ FATAL: Failed to rename '$TMP_DIR' to '$NEW_FOLDER_NAME'. Exiting." >&2 exit 1 fi echo "✅ Folder renamed to '$NEW_FOLDER_NAME'." # --- Step 5: Modify UUID key in the renamed folder's setup.json --- # Use jq to update the geometryData/UUID key jq --arg name "$NEW_FOLDER_NAME" \ '.geometryData.UUID = $name' \ "$NEW_FOLDER_NAME/$SETUP_FILE" > temp_setup.json && \ mv temp_setup.json "$NEW_FOLDER_NAME/$SETUP_FILE" echo "✅ setup.json UUID updated." # --- Step 6: Go to directory and run wkt command --- cd "$NEW_FOLDER_NAME" || exit 1 # Change directory, exit on failure echo "➡️ Running wkt command..." # Use the name of the folder for the command path WKT_COMMAND="wkt geometry --create $SETUP_FILE" # NOTE: The command syntax has been adjusted to assume $SETUP_FILE is relative to the current directory $WKT_COMMAND WKT_STATUS=$? if [[ $WKT_STATUS -eq 0 ]]; then echo "✅ wkt command successful. Proceeding to mesh generation." # --- Step 7: Run generateMesh --- ./generateMesh MESH_STATUS=$? # --- Steps 8 & 9: Log Results and Check for Loop Condition --- if [[ $MESH_STATUS -eq 0 ]]; then LOG_STATUS="OK" echo "✅ generateMesh successful. Logging result and continuing." else LOG_STATUS="NOK" echo "❌ generateMesh FAILED (NOK). Logging result and stopping loop." # The dimensions used in this failed run are logged, but the loop stops. # No update to dimensions is needed. fi else LOG_STATUS="NOK_WKT" echo "❌ wkt command FAILED. Logging result and stopping loop." MESH_STATUS=1 # Set a non-zero status to trigger the NOK branch below fi # Go back to the parent directory (Step 8/9 part 1) cd .. # Append to the log file (Step 8/9 part 2) echo "${X_DIM},${Y_DIM},${Z_DIM},${TOTAL_CELLS},${LOG_STATUS}" >> "$LOG_FILE" # Check if the loop should stop (Step 10 condition) if [[ $MESH_STATUS -ne 0 ]] || [[ $WKT_STATUS -ne 0 ]]; then echo "🛑 First failure recorded (Status: ${LOG_STATUS}). Script finished." break fi done echo "===================================================" echo "Script execution complete. Results logged in $LOG_FILE."