# bash script for running fresh melt pond simulation
# THIS FILE RELIES ON:
# DIRECTORY CALLED ra_test EXISTING IN DIR WHERE MULTIRUN.SH IS RUN FROM
# DIRECTORY CALLED snapshots IN THIS DIR
# snapshots MUST CONTAIN clearall.sh SHELL SCRIPT

# code has only been tested on original author's system, and we accept no responsibility for adverse consequences if used on other systems. Use at your own risk. In particular, script below involves deletion of files/directories detected in the source directory

AJWbasedir=$(pwd)

# Gives time to ctrl-c from 'nohup sh multirun.sh &' command ; prevents accidental breaks in code
sleep 30


# Usually variable argument for 2d_melt_ra.py is Rayleigh number
# Put list of Rayleigh numbers in list of $i
for i in 5000000; # Variable argument (Rayleigh Number/Resolution...)
do

    #Warning that new run is being initialised on server
    echo "Initialising run with Ra = $i"

    #Brief Pause
    sleep 10

    #Create names of zip files, uncomment #framedata if you want to save frames

    #framedata="framedata$i.zip"
    snapshotdata="snapdata$i.zip"

    #Runs simulation with a $i as variable argument, set respective variable in python script as int(float(sys.argv[1]))
#    mpiexec -n 16 python3 const_q.py $i 0 0
#    mpiexec -n 16 python3 const_q.py $i snapshots_s1.h5 1
#    mpiexec -n 16 python3 const_q.py $i snapshots_s16.h5 1
#    mpiexec -n 16 python3 2d_melt_ra.py $i snapshots_s14.h5 1
    mpiexec -n 16 python3 2d_melt_ra.py $i 0 0

    #Merges data into one .h5 file - change from snapshots if data outputted to different folder
    mpiexec -n 16 python3 merge.py snapshots

    #Uncomment if you want to plot data on coloured images
    #mpiexec -n 16 python3 plot_2d_series.py snapshots/*.h5

    #Uncomment if you've created images of system with plot_2d_series.py
    #zip -r $framedata frames/
    #echo "Removing frame data"
    #(cd frames; rm write*.png)

    #Copies last h5 file from last run to be loaded into next run. Change name of file depending on run time
    # Next line should contain number of file you wish to copy from directory
    # For this case it was snapshots_s14.h5 but may be a different number if
    # You want to reseed using a different file
    # Comment this line out if you don't want to copy a new h5 file across.
    cp snapshots/snapshots_s16.h5 ./
    #Zips up all .h5 files into directory ra_test
    #Make sure ra_test exists!
    (cd ra_test ; zip $snapshotdata ../snapshots/*.h5)
    cd $AJWbasedir
    #Checks zip file exists before removing files, then clears all .h5 files from snapshots/
    if [ -f "ra_test/$snapshotdata" ]; then
       cd snapshots
       sh clearall.sh
       cd ..
    else
       break
    fi


done




