import matplotlib as mpl
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
from matplotlib import cm
import re
import os
import json
import numpy as np
import pandas as pd
import json
import sys
from vtk import vtkUnstructuredGridReader
from vtk.util import numpy_support as VN
from vtk.util.numpy_support import vtk_to_numpy, numpy_to_vtk
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
from matplotlib import cm
from multiprocessing import Pool

plt.style.use("seaborn-paper")
plt.rc('font', family='serif', serif='Times')
# plt.rc('text', usetex=True)
plt.rc('xtick', labelsize=8)
plt.rc('ytick', labelsize=8)
plt.rc('axes', labelsize=8)
width = 3.487
height = width / 1.618
scale = 1
fig = plt.figure(figsize=(scale*width,1.3*scale*height),dpi=200)

analytic_f = [ 0.0  , 0.25 , 0.5  , 0.75 , 1.0  , 2.0  , 3.0  , 4.0  , 5.0  , 6.0  , 7.0  , 8.0  , 9.0  , 10.0 ]
analytic_v = [ 0    , 0.083, 0.162, 0.235, 0.302, 0.494, 0.603, 0.670, 0.714, 0.744, 0.767, 0.785, 0.799, 0.811]

top_dir = "./data/"
output_regex = re.compile(".*\.csv")
output_list = list(filter(output_regex.match,os.listdir(top_dir)))
output_list.sort()
for i,out in enumerate(output_list):
    print("{}: {}".format(i,out))
plt.plot(analytic_v,analytic_f,label="Analytic",c="black",zorder =-1)
L = 10
E = 12e6
P=-10e3
ms_list = ["o","x","^","4"] 
for ms,ddir in zip(ms_list,output_list):
    output_dir = "{}/{}".format(top_dir,ddir)
    df = pd.read_csv(output_dir)
    lab = "{} lstps={}".format(*ddir.split(".")[0].split("_"))
    plt.scatter(df["disp"]/L,df["load"],label=lab,marker=ms)
    print(df)

plt.legend()
plt.xlabel("Normalised displacement")
plt.ylabel("Normalised load")
plt.tight_layout()
plt.savefig("load-disp.pdf")
plt.show()
