ctest for thermostats: Langevin, Nose-Hoover and Berendsen
[unres.git] / ctest / matplotlib_fit_hist.py
diff --git a/ctest/matplotlib_fit_hist.py b/ctest/matplotlib_fit_hist.py
new file mode 100755 (executable)
index 0000000..8601325
--- /dev/null
@@ -0,0 +1,47 @@
+#! /usr/bin/env python
+
+import matplotlib
+#matplotlib.use('GTK')
+matplotlib.use('Agg')
+import matplotlib.pyplot as plt
+import numpy as np
+from scipy.optimize import curve_fit
+from math import sqrt
+import sys
+
+def prob_T(x,a):
+    g=np.float128(105.)
+    Tr=np.float128(300.)
+    return a * (  x**((g-2)/2) * np.exp( -g*x/(2*Tr) ) )   
+
+#x,y= np.loadtxt('1L2Y_L_GB000.stat',usecols=(0,10),skiprows=30,unpack=True)
+#x,y= np.loadtxt('1L2Y_NH_GB000.stat',usecols=(0,11),skiprows=10000,unpack=True)
+#x,y= np.loadtxt('1L2Y_B_GB000.stat',usecols=(0,10),skiprows=30,unpack=True)
+if (sys.argv[1] == '1L2Y_NH_GB000.stat'):
+ x,y= np.loadtxt(sys.argv[1],usecols=(0,11),skiprows=10000,unpack=True)
+else: 
+ x,y= np.loadtxt(sys.argv[1],usecols=(0,10),skiprows=10000,unpack=True)
+
+h,bin=np.histogram(y,bins=50,density=True)
+
+plt.bar(bin[:-1], h, width = bin[2]-bin[1])
+plt.xlim(min(bin), max(bin))
+plt.ylim(0,max(h))
+plt.ylabel('probality')
+plt.xlabel('temperature')
+
+center = (bin[:-1] + bin[1:]) / 2
+#print bin
+#print center
+popt, pcov = curve_fit(prob_T, center, h, p0=10E-107)
+xfine = np.linspace(min(bin), max(bin), 100)
+#print popt
+
+chi_squared = np.sum((prob_T(center, *popt)-h)**2)
+print '%15.10f' % chi_squared
+
+#print xfine
+#print prob_T(xfine,popt[0])
+plt.plot(xfine,prob_T(xfine,popt[0]),'-',c='red')
+plt.savefig(sys.argv[1]+'.png')
+#plt.show()