# %%
# 3D Benchmark
# ============
#
# This example benchmarks SLIM 3D in an idealized domain using realistic
# numerical parameters. The benchmark workflow is split across several files:
#
#   1. ``benchmark.sh`` (this file) : launcher script for the full benchmark sweep
#   2. ``mesh.geo`` : idealized mesh geometry
#   3. ``meshsplit.py``: mesh extrusion and partitioning
#   4. ``benchmark.py`` : simulation setup, timing, and SQLite output
#
# Results from benchmark.py are written to benchmarks.sqlite.
#
#
# Launcher script
# ---------------
#
# This script generates a family of meshes with different horizontal
# resolutions, partitions each mesh, and runs the benchmark for several vertical
# resolutions.
#
# It can be run concurrently by multiple jobs, provided that each job uses a
# different number of MPI ranks. All benchmark results are collected in the same
# SQLite database.

set -euo pipefail

# Change this to your device name, e.g. "A100", "MI250X", "CPU", etc.
devname="DEVICE_NAME"

# Select the command to run the Python scripts. Uncomment the one you want to use.
cmd=(python)
# cmd=(srun python)
# cmd=(mpirun -np 8 python)

mkdir -p meshes
for scale in 1.600e+01 1.345e+01 1.131e+01 9.514e+00 8.000e+00 6.727e+00 5.657e+00 4.757e+00 4.000e+00 3.364e+00 2.828e+00 2.378e+00 2.000e+00 1.682e+00 1.414e+00 1.189e+00 1.000e+00 8.409e-01 7.071e-01 5.946e-01 5.000e-01 4.204e-01 3.536e-01 2.973e-01 2.500e-01 2.102e-01 1.768e-01 1.487e-01 1.250e-01 1.051e-01 8.839e-02 7.433e-02 6.250e-02 5.256e-02 4.419e-02 3.716e-02 3.125e-02 2.628e-02; do
    outfile="meshes/mesh${scale}.msh"
    if [[ ! -f "$outfile" ]]; then
        echo "Generating $outfile"
        gmsh -2 mesh.geo -clscale "$scale" -o "$outfile" > /dev/null
    fi

    "${cmd[@]}" meshsplit.py -m "$scale"
    for nz in 16 32; do # vertical resolutions to test
        echo "Running scale=$scale, nz=$nz, devname=$devname"
        "${cmd[@]}" benchmark.py -nz "$nz" -m "$scale" -devname "$devname"
    done
done
