#!/bin/bash

# Run python demos
PYVER=$(py3versions -sv)

set -e

# pyvista is not yet available in debian but if it is installed locally
# then demo tests should be run in off-screen mode
export PYVISTA_OFF_SCREEN=True

DEB_HOST_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_BITS=$(dpkg-architecture -qDEB_HOST_ARCH_BITS)

# Tests real build by default.
# Access the complex build by setting env variable USE_DOLFINX_COMPLEX

if [ "x${USE_DOLFINX_COMPLEX}" != "x" ]; then
    TESTDIR_SUFFIX="-complex"
    TEST_DESCRIPTION="for complex build "
    export PETSC_DIR=/usr/lib/petscdir/petsc-complex
    # process ufl forms for complex test: second argument 1 indicates complex
    UFL_TYPE=1
else
    TESTDIR_SUFFIX="-real"
    export PETSC_DIR=/usr/lib/petsc
fi
export SLEPC_DIR=`grep wPETSC_DIR ${PETSC_DIR}/lib/petsc/conf/petscvariables | awk '{print $3}' | sed "s/petsc/slepc/g"`

# MPI tests are set up to run on no more than 3 processes
NPROC=$( nproc )
if [[ $NPROC > 2 ]]; then
  N_MPI=3
else
  N_MPI=2
fi
# and only 2 processes for unit tests
N_MPI_UNITTEST=2

export PRTE_MCA_plm_ssh_agent=/bin/false
export PRTE_MCA_rmaps_default_mapping_policy=":oversubscribe"
export OMPI_MCA_btl_base_warn_component_unused=0

echo "== running python demos ${TEST_DESCRIPTION}=="
cd python/demo

# help CI tests run a little faster
export DOLFINX_JIT_CFLAGS="-g0 -O2"

declare -a SKIP_TEST_LIST

SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} cahn gmsh)

# skip slow demos
SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} hood demo_hdg)

# static-condensation-elasticity.py (mixed-elasticity-sc) uses numba
if ! dpkg-query -s python3-numba >/dev/null 2>&1; then
   SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} condensation)
fi

# demo_lagrange_variants triggers a segfault interfering with PETSC
# when trying to plot elements.
# Not clear why. Possibly a bug in libtcl used by matplotlib
SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} demo_lagrange_variants)

# complex-number demo demo_axis.py and demo_pml.py fails on several architectures,
# generally flakey so skip on all arches
# see https://github.com/FEniCS/dolfinx/issues/2686
SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} demo_axis demo_pml)

# riscv64 times out running demos, so skip the slowest of them
if [ "$arch" = "riscv64" ]; then
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} tnt-elements navier-stokes half_loaded_waveguide)
fi

TEST_KEYWORDS=""
for t in ${SKIP_TEST_LIST[@]}; do
    if [ "x${TEST_KEYWORDS}" = "x" ]; then
        TEST_KEYWORDS=$t
    else
        TEST_KEYWORDS="${TEST_KEYWORDS} or $t"
    fi
done
if [ "x${TEST_KEYWORDS}" != "x" ]; then
    TEST_KEYWORDS="not ( ${TEST_KEYWORDS} )"
fi
echo "skipping tests with SKIP_TEST_LIST=${SKIP_TEST_LIST[@]}"

for pyver in $PYVER; do
  echo "=== python $pyver demo test (serial) ${TEST_DESCRIPTION}==="
  python$pyver -m pytest -v --durations=20 -k "${TEST_KEYWORDS}" test.py
done

declare -a MPI_SKIP_TEST_LIST
if [ "x${DEB_HOST_ARCH_BITS}" = "x32" ] ; then
  MPI_SKIP_TEST_LIST=(${MPI_SKIP_TEST_LIST[@]} helmholtz condensation poisson)
fi
MPI_TEST_KEYWORDS=""
for t in ${SKIP_TEST_LIST[@]} ${MPI_SKIP_TEST_LIST[@]}; do
    if [ "x${MPI_TEST_KEYWORDS}" = "x" ]; then
        MPI_TEST_KEYWORDS=$t
    else
        MPI_TEST_KEYWORDS="${MPI_TEST_KEYWORDS} or $t"
    fi
done
if [ "x${MPI_TEST_KEYWORDS}" != "x" ]; then
    MPI_TEST_KEYWORDS="not ( ${MPI_TEST_KEYWORDS} )"
fi
echo "also skipping MPI tests with MPI_SKIP_TEST_LIST=${MPI_SKIP_TEST_LIST[@]}"

# "-m mpi" refers to a custom pytest marker (not python module)
# and interferes with the pytest-mpi plugin, if installed.
# So explicitly switch off pytest-mpi (-p no:mpi, distinct from -m mpi)
# see https://github.com/FEniCS/dolfinx/issues/2254
for pyver in $PYVER; do
  echo "=== python demo test (MPI using ${N_MPI} processors out of $NPROC) ${TEST_DESCRIPTION}==="
  python$pyver -m pytest -v -p no:mpi -m mpi --durations=20 -k "${MPI_TEST_KEYWORDS}" test.py --mpiexec=mpiexec --num-proc=${N_MPI}
done
