#!/bin/bash
# ********************
#
# Script to execute a dynamical system's C++ code for a range of input parameters and write the results to a series of data files
# that can be plotted using gnuplot and illustrate the effects of parameters on the model.
#
#
#
# Preconditions to this script
# *****************************************************
#
#  All paths and filenames are hardwired.
#
# Version History
# ***************
#
#  26/07/23  1.0  Author: Julian Shillcock. Simple limit cycle model from Lecture 7.
#  30/08/23  1.1  Modified to use arguments and automatically plot results.
#
# Arguments
# *********
#
#  $1   = Number of steps in the integration
#  $2   = constant in x equation
#  $3   = constant in x equation
#  $4   = constant in y equation
#  $5   = constant in y equation
#  $6   = lower X axis limit = lower y axis limit
#  $7   = upper X axis limit = upper y axis limit
#
#
# ******************************************************************************************
# Now the main body of the script.

if [ $# != 7 ]; then echo "****** Insufficent arguments to script (supply n, a[4], xlow, xhigh), aborting ******"; exit ; fi

# Set the unchanging parameters

prefix=cycle
inputName=${prefix}input
dataName=${prefix}data
executable=$prefix

# Model parameters and number of steps

n=$1

a0=$2
a1=$3
a2=$4
a3=$5

# Axis limits of the plot

xlow=$6
xhigh=$7
ylow=$6
yhigh=$7


# Iterate over a set of initial points giving each one a different filename. It currently only works for integer values though.

# ********************
# Iterate over a set of initial points giving each one a different filename. It currently just appends the points
# to the filename, which works only for legal characters, but . and - are ok.

#xvalues="-5 -2 -1 -0.75 -0.5 -0.1 0.1 0.5 0.75 1 2 5"
#yvalues="-5 -2 -1 -0.75 -0.5 -0.1 0.1 0.5 0.75 1 2 5"

xvalues="-8 -6 -4 -2 -1 -0.8 -0.6 -0.5 -0.4 -0.25 0.25 0.4 0.5 0.6 0.8 1 2 4 6 8"
yvalues="-8 -6 -4 -2 -1 -0.8 -0.6 -0.5 -0.4 -0.25 0.25 0.4 0.5 0.6 0.8 1 2 4 6 8"

# Make a list of all the data files to add the plot script

dataList=""

for x in $xvalues; do
for y in $yvalues; do

if [ -e $inputName ]; then rm $inputName; fi

echo ${dataName}$x$y $n $a0 $a1 $a2 $a3 $x $y > $inputName

$executable < $inputName >> /dev/null

dataList=$dataList" "${dataName}$x$y

done
done


# Plot the results in gnuplot, using the current date/time to identify the plot. We use sed/awk to change the name
# of the plot file and other variables depending on the curve being plotted.

current_time=$(date "+%Y.%m.%d-%H.%M.%S")

# Set the name of the output file

sed "s/prefix/$prefix/g;s/currentTime/$current_time/" $prefix.txt > ${prefix}1.txt

# Set the limits of the plot

sed "s/limits/[$xlow:$xhigh][$ylow:$yhigh]/" ${prefix}1.txt > ${prefix}2.txt

# Add the parameter values as a label in the bottom left corner

sed "s/xlow/$xlow/; s/ylow/$ylow/" ${prefix}2.txt > ${prefix}3.txt

# Insert the names of all the relevant data files into the plot file

for item in $dataList; do
    awk -v name=$item '{ if($1=="dataLine") {print "\""name"\"" " using 1:2 with lines,\\\n dataLine"} else {print}}' ${prefix}3.txt > ${prefix}2.txt
    
    mv ${prefix}2.txt ${prefix}3.txt
done


awk -v name=$item '{ if($1=="dataLine") {print "\""name"\"" " using 1:2 with lines \n"} else {print}}' ${prefix}3.txt > ${prefix}2.txt

# Make a plot with no legend so the picture is clear

cp ${prefix}2.txt ${prefix}3.txt

gnuplot ${prefix}3.txt

# Clean up by deleting intermediate files

rm ${prefix}1.txt
rm ${prefix}2.txt
rm ${prefix}3.txt

rm $inputName
rm ${dataName}*
