#!/bin/bash
PUEUE=true

echo "Generating plots..."
rm -rf python/plots

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

export SHOW_PLOTS=0

# Create simple trajectory plots for the case of few particles
for file in results/two_particles_*_positions.dat
    do
    $COMMAND_PREFIX uv run python/plot_trajectories.py plot-z-evolution "$file"
    $COMMAND_PREFIX uv run python/plot_trajectories.py plot-xy-trajectory "$file"

    velocity_file="${file/positions/velocities}"
    for axis in x y z
    do
        $COMMAND_PREFIX uv run python/plot_trajectories.py phase-space-plot --axis "$axis" "$file" "$velocity_file"
    done
done

# Create error plots for a single particle
for analytical_file in results/two_particles_1_no_interactions_*_analytical_positions.dat
do
    euler_file="${analytical_file/analytical/euler}"
    verlet_file="${analytical_file/analytical/velocity_verlet}"
    boris_file="${analytical_file/analytical/boris}"
    rk4_file="${analytical_file/analytical/rk4}"
    $COMMAND_PREFIX uv run python/plot_trajectories.py plot-error "$euler_file" "$verlet_file" "$boris_file" "$rk4_file" "$analytical_file"
done

# Create resonance plots for the many particle case
for folder in results/*
do
    subfolder="$folder/many_particles_100_particles_40000_steps_enabled_interactions_oscillating_potential"
    if [ ! -d "$subfolder" ]; then
        continue
    fi
    second_subfolder="$folder/many_particles_100_particles_40000_steps_disabled_interactions_oscillating_potential"
    if [ ! -d "$second_subfolder" ]; then
        continue
    fi
    $COMMAND_PREFIX uv run python/plot_resonances.py "$subfolder" "$second_subfolder"
done

# Create system energy plots for the many particle case
$COMMAND_PREFIX uv run python/plot_energies.py results/*/many_particles_100_particles_32000_steps_enabled_interactions_total_energies.dat python/plots/many_particles_100_particles_32000_steps_enabled_interactions_total_energies.pdf
for solver in results/*/
do
    name=$(basename "$solver")
    $COMMAND_PREFIX uv run python/plot_energies.py "$solver"many_particles_100_particles_*_steps_enabled_interactions_total_energies.dat python/plots/"$name"_many_particles_100_particles_enabled_interactions_total_energies.pdf
done
