Files
LRT_OpenMC/scripts/openmc_run.sh
T
Stafie Alex PSI de70d59ffd first files
2025-10-08 13:11:16 +02:00

113 lines
3.5 KiB
Bash

#!/bin/bash
#
# ================================================================
# OpenMC + Python Runner (Apptainer-based)
# ================================================================
# Description:
# This script runs either:
# (1) OpenMC directly inside an Apptainer container, or
# (2) a Python script using the same container environment.
#
# Usage:
# ./run_openmc.sh → Run OpenMC in the container
# ./run_openmc.sh script.py args → Run Python script with args
# ./run_openmc.sh -h | --help → Show help
#
# Behavior:
# - The current working directory is mounted into /workspace inside the container.
# - The OpenMC library directory is mounted into /libs.
# - The output log is saved in the same directory as the run:
# * openmc_run.log for OpenMC runs
# * <scriptname>.log for Python runs
#
# Example:
# ./run_openmc.sh # Run OpenMC normally
# ./run_openmc.sh input.py # API can be called inside the script
#
# ================================================================
# === Config ===
# ================================================================
# NOTE: Update SIF and LIB_DIR paths as needed !!!!!!!!!!!!!!!!!!!
# ================================================================
SIF="/afs/psi.ch/project/stars/workspace/RND/SB-RND-ACT-004-25/SC41/openmc/openmc_apptainer/openmc.sif"
LIB_DIR="/afs/psi.ch/project/stars/workspace/RND/SB-RND-ACT-004-25/SC41/openmc/lib_mine"
HOST_DIR="$(pwd)"
CONTAINER_DIR="/workspace"
# === Help ===
show_help() {
cat << EOF
Usage: $(basename "$0") [script.py [args...]]
Run OpenMC or a Python script inside an Apptainer container.
Options:
-h, --help Show this help message and exit
Modes:
• No arguments → Runs OpenMC inside the container
• script.py [args...] → Runs the given Python script using py
Examples:
$(basename "$0") # Run OpenMC normally
$(basename "$0") run_case.py # Run Python script
Environment:
Container: ${SIF}
Host dir : ${HOST_DIR}
Lib dir : ${LIB_DIR}
EOF
}
# === Handle Help Flag ===
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi
# === Mode ===
if [ -z "$1" ]; then
MODE="openmc"
else
MODE="python"
PYTHON_SCRIPT="$1"
shift
fi
# === Log File ===
if [ "$MODE" = "openmc" ]; then
LOG_FILE="${HOST_DIR}/openmc_run.log"
else
if [ ! -f "${HOST_DIR}/${PYTHON_SCRIPT}" ]; then
echo "Error: Python script '${PYTHON_SCRIPT}' not found in ${HOST_DIR}."
exit 1
fi
LOG_FILE="${HOST_DIR}/${PYTHON_SCRIPT%.*}.log"
fi
echo "---------------------------------------------------------------"
echo " Running in container: ${SIF}"
echo " Host directory : ${HOST_DIR}"
echo " Container directory : ${CONTAINER_DIR}"
echo " Library mount : ${LIB_DIR} -> /libs"
echo " Log file : ${LOG_FILE}"
echo "---------------------------------------------------------------"
# === Exec===
if [ "$MODE" = "openmc" ]; then
echo ">>> Starting OpenMC run..."
apptainer exec \
--bind "${HOST_DIR}:${CONTAINER_DIR}" \
--bind "${LIB_DIR}:/libs" \
--pwd "${CONTAINER_DIR}" "${SIF}" \
openmc | tee "${LOG_FILE}"
else
echo ">>> Running Python script: ${PYTHON_SCRIPT}"
apptainer exec \
--bind "${HOST_DIR}:${CONTAINER_DIR}" \
--bind "${LIB_DIR}:/libs" \
--pwd "${CONTAINER_DIR}" "${SIF}" \
python3.11 "${CONTAINER_DIR}/${PYTHON_SCRIPT}" "$@" | tee "${LOG_FILE}"
fi