# -----------------------------------------------------------------------------------------------
# Script for running the random initial guesses-related numerical experiments
# -----------------------------------------------------------------------------------------------
# NCIRCLES is the number of balls
# NROUNDSEVALUATED is the total number of random initial guesses to be evaluated
# NROUNDSCALLED is the total number of random initial guesses to be generated 
#  Remark: Chose NROUNDSCALLED > NROUNDSEVALUATED when running your experiments
# SQUARESIZE is the side length of the square
# NPROCESSORS is the number of processors available on your computer to run tests simultaneously
# -----------------------------------------------------------------------------------------------

set -e
path=$(pwd)
echo path= $path

let j=1
let NCIRCLES=3
let NROUNDSCALLED=15
let NROUNDSEVALUATED=10
let SQUARESIZE=2
let NPROCESSORS=64

# Generates NROUNDSCALLED random initial guesses
FreeFem++ -nw eigncircles_initialguessesgenerator.edp -ncircles $NCIRCLES -nroundscalled $NROUNDSCALLED -squaresize $SQUARESIZE

# Solves the eigenvalue problem (Algorithm 1) for each initial guess in parallel
for (( i = 1; i <= $NROUNDSCALLED; i++ ));
do
	nohup FreeFem++ -nw eigncircles_random_Alg1.edp -ncircles $NCIRCLES -nroundi $i -nroundscalled $NROUNDSCALLED -squaresize $SQUARESIZE &
	j=$(( $j + 1 ));
	if [ $( expr  $j % $NPROCESSORS) == 0 ]
	then
		wait
	fi
done

wait $(jobs -p)

# Select the best solution found and draw it
FreeFem++ -nw eigncircles_findbestresult_Alg1.edp -ncircles $NCIRCLES -nroundscalled $NROUNDSCALLED -nroundsevaluated $NROUNDSEVALUATED -squaresize $SQUARESIZE

# Run Algorithms 1 and 2 starting from the initial guess that lead to the best solution in the loop above
FreeFem++ -nw eigncircles_random_Algs1n2.edp -ncircles $NCIRCLES -nroundscalled $NROUNDSCALLED -nroundsevaluated $NROUNDSEVALUATED -squaresize $SQUARESIZE

echo " "
echo ' ======================================'
echo ' =====    Program terminated!    ======'
echo ' ======================================'
echo " "
