#!/bin/bash
#---------------------------------*- sh -*-------------------------------------
# ==  == ====== ====   ====    |
#                   \\     ||  | Multiphase Code Repository by HZDR
# ======   //   ||  || ===//   | Website: https://doi.org/10.14278/rodare.767
# ||  ||  //    ||  // || \\   | License: GPL-3.0-or-later
# ==  == ====== ====   ==  ==  |
#------------------------------------------------------------------------------
# License
#     This file is part of the Multiphase Code Repository by HZDR.
#
#     Copyright (C) 2025 by Helmholtz-Zentrum Dresden-Rossendorf e.V. (HZDR),
#     Website: https://hzdr.de
#
#     Multiphase Code Repository by HZDR is based on the free software for
#     computational fluid dynamics (CFD) from the OpenFOAM Foundation.
#     Copyright (C) 2025 by OpenFOAM Foundation, Website: https://openfoam.org
#
#     If you are interested in which files are original OpenFOAM Foundation
#     files, which OpenFOAM Foundation files were modified, and which files were
#     newly created, see FILES.md.
#
#     Multiphase Code Repository by HZDR is free software: you can redistribute
#     it and/or modify it under the terms of the GNU General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.
#
#     Multiphase Code Repository by HZDR is distributed in the hope that it will
#     be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
#     Public License for more details.
#
#     You should have received a copy of the GNU General Public License along
#     with the Multiphase Code Repository by HZDR. If not, see
#     <http://www.gnu.org/licenses/>.
#
# Description
#     SLURM script to start a ParaView server on a Slurm Cluster running in an
#     Apptainer container. Find the connection instructions in the Slurm log
#     shortly after the job starts. You may need to wait a minute for the image
#     to download first. The *.OpenFOAM reader plugin can be used, but you first
#     need to `touch <caseName>.OpenFOAM` at the case directory.
#
#------------------------------------------------------------------------------

# Partition
#SBATCH --partition=cpu-skylake

# Stay on a single node
#SBATCH --nodes=1

# Set number of tasks per node
#SBATCH --ntasks-per-node=4

# Set max wallclock time
#SBATCH --time=12:00:00

# Set name of the job
#SBATCH --job-name=pvserver

# Define image, public-facing hostname of the remote, port and possible bind
# mounts (if case resides on a network storage)
IMAGE="oras://registry.hzdr.de/fwdc/multiphase/code/13-s.1:multiphase-code-repository-by-hzdr-amd64.sif"
REMOTE_HOSTNAME=rosi4.fz-rossendorf.de
PARAVIEW_PORT=11111
BIND_MOUNT="/bigdata"

# Report connection details
cat << EOF

######################## Connect with ParaView server #########################"
Open a terminal on your local machine and run

ssh -L ${PARAVIEW_PORT}:$(hostname):${PARAVIEW_PORT} ${USER}@${REMOTE_HOSTNAME}

to establish an ssh tunnel to the compute node running the server. Then, in a
separate terminal, run

apptainer exec ${IMAGE} paraview --url=cs://localhost:${PARAVIEW_PORT}

If you want to run ParaView outside of a container, you need to make sure that
version

$(apptainer exec ${IMAGE} bash -c 'echo ${ParaView_VERSION}')

is installed. On a Linux machine, you can then open a terminal and run

paraview --url=cs://localhost:${PARAVIEW_PORT}

If you are working on a Windows or Macintosh PC, open ParaView, go to
File/Connect... and add a server using Host: localhost and Port: ${PARAVIEW_PORT}.
Then save and connect.

Note that disconnecting from the server or closing the ParaView GUI will kill
this job. If ParaView is killed due to some error in the OpenFOAM setup, you
will find the corresponding error message within this log file.
################################################################################

EOF

# Start server
apptainer exec -B "${BIND_MOUNT}" $IMAGE pvserver --server-port=${PARAVIEW_PORT}
