#!/usr/bin/env bash
# Tier-1 item 3 -- move the production endpoint from P1 to P2.
#
# Why: across the five discretisations already in Table S2 the P1 family is still
# drifting (-37.9 / -40.9 / -42.3 %) while the P2 family has settled (-44.64 % on
# the coarse mesh, -44.51 % on the intermediate mesh, 0.13 pp apart across a
# 3.4x change in mesh size). The reported -42.34 % is the least converged of the
# five. `--include-main-p2` already exists upstream, so this is a pure run.
#
# Cost: one added P2 pair on the main mesh. Expect this to dominate the wall
# clock -- budget several hours and prefer projected_minres for the large P2
# system (the augmented direct factorisation is what ran out of memory before).
#
# Usage:
#   ./01_run_main_p2.sh /path/to/project-root /path/to/out-root
set -euo pipefail

PROJECT_ROOT="${1:?usage: 01_run_main_p2.sh <project-root> <out-root>}"
OUT_ROOT="${2:?usage: 01_run_main_p2.sh <project-root> <out-root>}"

SOURCE_DIR="${PROJECT_ROOT}/05_Source_Data"
[ -f "${SOURCE_DIR}/run_reynolds_finan_heterogeneous_two_group.py" ] || SOURCE_DIR="${PROJECT_ROOT}"

mkdir -p "${OUT_ROOT}"

python3 "${SOURCE_DIR}/run_reynolds_finan_heterogeneous_two_group.py" \
    --out-dir "${OUT_ROOT}/p2_production" \
    --primary-mesh intermediate_h0p75 \
    --include-main-mesh \
    --intermediate-p2 \
    --include-main-p2 \
    --rigid-constraint-linear-solver projected_minres \
    --resume

echo
echo "Done. Next:"
echo "  1. python3 05_collect_results.py --runs ${OUT_ROOT}/p2_production ..."
echo "  2. Report the endpoint from the P2 family and quote the full"
echo "     -37.9 to -44.6 % spread as the discretisation range in Table S2."
