#!/bin/bash
# ********************
#
# Script to execute a 2D non-linear dynamics 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 limit cycle in the model.
#
#
#
# Preconditions to this script
# *****************************************************
#
#  All paths and filenames are hardwired.
#
# Version History
# ***************
#
#  01/09/23  1.0  Author: Julian Shillcock. Runs the rk.cpp code that integrates the linear system dX/dt = A X in lecture 4.
#                  The matrix A is passed in as command line arguments.
#
# Arguments
# *********
#
#  $1      = Number of steps in the integration
#  $2 - $5 = matrix of coefficients, a, b, c, d
#  $6, $7  = lower/upper X axis limits
#  $8, $9  = lower/upper Y axis limits
#
# ******************************************************************************************

if [ $# != 9 ]; then echo "****** Insufficent arguments to script (supply n, A[], xlow, xhigh, ylow, yhigh), aborting ******"; exit ; fi

# Set the unchanging parameters

prefix=rk
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=$8
yhigh=$9

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

xvalues="-9 -7 -5 -3 -1  1 3 5 7 9"
yvalues="-9 -7 -5 -3 -1  1 3 5 7 9"

# 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/; s/aValue/$a0/; s/bValue/$a1/; s/cValue/$a2/; s/dValue/$a3/;" ${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

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

# Produce the graph as a jpg.

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}*
