#!/bin/bash

PUEUE=true
NORMAL_MANY_PARTICLE_SIMULATION=true
OSCILLATING_FIELD_SIMULATION=true

echo "Removing old results..."
mkdir -p results
rm results/*.dat
rm results/*.txt

echo "Compiling binaries..."
rm -f two_particles many_particles
g++ -O3 -larmadillo -I ./include src/* two_particles.cpp -o two_particles
g++ -O3 -larmadillo -I ./include src/* many_particles.cpp -o many_particles
echo "Finished compiling."

if $PUEUE; then
    echo "Using pueue to generate results in parallel..."
    systemctl --user start pueued
    pueue parallel 8
    pueue clean
    COMMAND_PREFIX="pueue add "
else
    echo "Generating results sequentially..."
    COMMAND_PREFIX=""
fi

for N in 4000 8000 16000 32000
do
    for interactions in "" "--disable-interactions"
    do
        for n in 1 2
        do
            if [ "$n" -eq 1 ] && [ "$interactions" == "" ]; then
                # Skip the case of one particle with interactions, as it is meaningless
                continue
            fi
            $COMMAND_PREFIX ./two_particles -N $N -n $n $interactions
            $COMMAND_PREFIX ./two_particles -N $N -n $n $interactions -E
            $COMMAND_PREFIX ./two_particles -N $N -n $n $interactions -V
            $COMMAND_PREFIX ./two_particles -N $N -n $n $interactions -B
        done
    done
    # The analytical solution is only valid for one particle (second particle has v_z) without interactions
    $COMMAND_PREFIX ./two_particles -N $N -n 1 --disable-interactions -A
done

if $NORMAL_MANY_PARTICLE_SIMULATION; then
    for N in 4000 8000 16000 32000
    do
        for interactions in "" "--disable-interactions"
        do
            for n in 10 50 100
            do
                $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions
                $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -E
                $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -V
                $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -B
            done
        done
    done
fi

if $OSCILLATING_FIELD_SIMULATION; then
    N=40000
    n=100
    reduced_output="--reduced-output"
    interactions="--disable-interactions"
    for amplitude in 0.1 0.4 0.7
    do
        for frequency in $(seq 0.2 0.02 2.5)
        do
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -O -f $amplitude -w $frequency $reduced_output
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -V -O -f $amplitude -w $frequency $reduced_output
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -B -O -f $amplitude -w $frequency $reduced_output
        done
    done
    interactions=""
    for amplitude in 0.1 0.4 0.7
    do
        for frequency in $(seq 1.0 0.005 1.8)
        do
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -O -f $amplitude -w $frequency $reduced_output
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -V -O -f $amplitude -w $frequency $reduced_output
            $COMMAND_PREFIX ./many_particles -N $N -t -n $n $interactions -B -O -f $amplitude -w $frequency $reduced_output
        done
    done
fi

if $PUEUE; then
    echo "All tasks added to pueue."

    echo "Use 'pueue status' to check the status of the tasks."

    pueue status
fi
