set -e

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe
export OMPI_MCA_btl_tcp_if_include=lo
export OMPI_ALLOW_RUN_AS_ROOT=1
export OMPI_ALLOW_RUN_AS_ROOT_CONFIRM=1

# testing on 5 processes is recommended by mpi4py upstream where possible
# and is safe to oversubscribe with OpenMPI
# (but oversubscribing might be a problem  - runs slow - with mpich)
# But the kspsolve demo requires a power of 2, so use 4 processes not 5.
N_MPI=4

# don't try to display plots generated with matplotlib
export MPLBACKEND=Agg

PETSC_DIR=/usr/lib/petsc
echo "Running petsc4py demos using PETSC_DIR=${PETSC_DIR}"

# test demos as installed
cp -r /usr/share/doc/python-petsc4py-doc/examples/demo test-demos
gunzip -r test-demos

for pyver in `py3versions -sv`; do
  echo "Run demos (single processor) with python$pyver"
  make PETSC_DIR=${PETSC_DIR} PYTHON=python$pyver F2PY=f2py$pyver -C test-demos/legacy all

  echo "Run demos (MPI) with python$pyver"
  make PETSC_DIR=${PETSC_DIR} PYTHON=python$pyver F2PY=f2py$pyver MPIEXEC_FLAGS="-n ${N_MPI}" -C test-demos/legacy all-mpi

  echo "Run poisson2d demo with python$pyver"
  python$pyver demo/poisson2d/poisson2d.py
done
