# ----------- Define scan ranges -----------

# Use space-separated floating-point/integer values
SCATTERING_LENGTH_RANGE="[75.00 76.09 77.18 78.27 79.36 80.00 81.04 82.08 83.12 84.17 85.21 86.25 87.29]"
POLAR_ANGLE_RANGE="[40.0]"
AZIMUTHAL_ANGLE_RANGE="[0.0]"
NUM_ATOMS_LIST="[50000 54545 59091 63636 68182 72727 77273 81818 86364 90909 95455]"
CHUNK_SIZE=1

# ----------- Count total combinations for SLURM array -----------

# Strip brackets for Bash processing (for counting only)
scatteringLengths=($(echo "$SCATTERING_LENGTH_RANGE" | tr -d '[],')) 
polarAngles=($(echo "$POLAR_ANGLE_RANGE" | tr -d '[],')) 
azimuthalAngles=($(echo "$AZIMUTHAL_ANGLE_RANGE" | tr -d '[],')) 
numAtoms=($(echo "$NUM_ATOMS_LIST" | tr -d '[],')) 

totalJobs=$((${#scatteringLengths[@]} * ${#polarAngles[@]} * ${#azimuthalAngles[@]} * ${#numAtoms[@]}))
numArrayJobs=$(( (totalJobs + CHUNK_SIZE - 1) / CHUNK_SIZE ))

echo "Total number of jobs: $totalJobs"
echo "Number of SLURM array jobs: $numArrayJobs"
echo "Running SLURM job array from 1 to ${numArrayJobs}"

# ----------- Submit SLURM array job -----------

sbatch --export=SCATTERING_LENGTH_RANGE="$SCATTERING_LENGTH_RANGE",POLAR_ANGLE_RANGE="$POLAR_ANGLE_RANGE",AZIMUTHAL_ANGLE_RANGE="$AZIMUTHAL_ANGLE_RANGE",NUM_ATOMS_LIST="$NUM_ATOMS_LIST",CHUNK_SIZE=$CHUNK_SIZE << EOF
#!/bin/bash
#SBATCH --partition=gpu-single
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=8
#SBATCH --gres=gpu:1
#SBATCH --mem=32G
#SBATCH --time=06:00:00
#SBATCH --job-name=simulation
#SBATCH --error=simulation_%A_%a.err
#SBATCH --output=simulation_%A_%a.out
#SBATCH --array=1-${numArrayJobs}

# Load MATLAB
module load math/matlab/R2023a

echo "Initiating Job..."

# Run MATLAB wrapper with this batch index
matlab -nodisplay -nosplash -r "Scripts.run_hybrid_worker_wrapper(\$SLURM_ARRAY_TASK_ID)"

echo "Job terminated successfully"

exit
EOF