################################
# Measuring the speed of sound #
################################


# Import modules
import pencil as pc
import seaborn as sns 
import matplotlib.pyplot as plt 
import pandas as pd 
import numpy as np 
  
# create figure environment:
f, ax = plt.subplots(figsize=(10,5))

# maximum number of VAR files:
numberofvarfiles = len(open('../data/proc0/varN.list').readlines(  ))-1

# collect all density snapshots:
xmaxlnrho = []
t = []

for i in range(0, numberofvarfiles, 1):
    var = pc.read.var(ivar=i,trimall=True, datadir='../data')
    lnrho = var.lnrho[0,0,:]
    maxarglnrho = np.argmax(lnrho)
    xmaxlnrho.append(var.x[maxarglnrho])
    t.append(var.t)
  

# create plot:
plt.axis([0,max(t), -5,5])
plt.scatter(t, xmaxlnrho, label='position of maximum $x_\\mathrm{max}$')

# check velocity:
t_theo = np.linspace(0, 10, 100)
x_theo = lambda time, v_test: v_test*(time-4.8)

# check if velocity = 1 works:
plt.plot(t_theo, x_theo (t_theo, 1), color='pink', linewidth=1.)



# plot derivative of xmax:
v_numeric = np.gradient(xmaxlnrho,t)
plt.plot(t, v_numeric, color='green', label='time derivate of $x_\\mathrm{max}$')


# add labels
plt.xlabel("time $t$")
plt.ylabel("$y =$")


# add legend
plt.legend(loc='upper right', title='$y=$')


f.savefig("xmax_t.pdf" , bbox_inches='tight')
