from NL.Math.SplineInterpolation import SplineInterpolation1D
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText

setVerbosity(SilentLog)

# Initialize figure
fig = plt.figure(figsize=(6,5))
ax = fig.add_subplot(111)

# Looping over configurations of different compositions
compositions = [0.05,0.1,0.2,0.3,0.4,0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0]
shift = -5.0
for i,composition in enumerate(compositions):
    hdf5 = 'x%.2f.hdf5' % composition
    print(i, ':', hdf5)

    # Compute RDFs
    configurations = nlread(hdf5, BulkConfiguration)
    histogram = None
    for j in range(2,12):
        configuration = configurations[j]
        # Calculate the radial distribution function
        rd = RadialDistribution(configuration,
                                cutoff_radius=10*Angstrom,
                                resolution=0.01*Angstrom,
                                pair_selection=[Sulfur, Sulfur],
                                )
        hist = rd._histogramValues()
        center = rd._binCenters()
        if histogram is None:
            histogram = hist
        else:
            histogram += hist

    # Mean RDF.
    histogram = numpy.array(histogram)/10.+i*shift

    # Plot RDF
    red = float(i)/len(compositions)
    color = (red,0,0)
    ax.axhline(i*shift, color=color, lw=1, ls='--')
    ax.plot(center, histogram, label='x=%.2f'%composition, color=color, lw=2)
    xy = (0.5,i*shift+1.0)
    if i == 0:
        txt = r'$\mathrm{Li}_{%.2f}\mathrm{S}$' % composition
    else:
        txt = r'$\mathrm{Li}_{%.1f}\mathrm{S}$' % composition
    ax.annotate(txt, xy=xy, xycoords='data')

title = AnchoredText('S-S',loc=1,borderpad=0.5,frameon=False,prop=dict(fontweight="bold",fontsize=16))
ax.add_artist(title)
ax.set_yticks([])
ax.set_yticklabels([])
ax.set_xlabel('Distance (Angstroms)')
ax.set_ylabel('RDF (arb. units)')
plt.tight_layout()
plt.savefig('rdf_ss.png')
plt.show()