# %%
# Mesh splitting
# --------------
#
# This script partitions a benchmark mesh if it has not already been split.
# It is normally launched automatically by benchmark.sh rather than run directly.
#
# The partitioning is only performed if at least one expected partition file is
# missing for the current MPI size.

import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("-m", dest="scale", type=str, help="The scale")
scale = parser.parse_args().scale

from slim4 import slim2d, mpi

already_exists = os.path.exists(f"meshes/mesh{scale}_split/{mpi.size}/{mpi.rank}.msh")
if mpi.min_idx(already_exists) == 0:
    slim2d.partition_mesh(
        f"meshes/mesh{scale}.msh", f"meshes/mesh{scale}_split/{mpi.size}/${{rank}}.msh"
    )
else:
    if mpi.rank == 0:
        print(f"mesh{scale}.msh already partitioned in {mpi.size} pieces")
