@zhouhuibin
2021-03-17T15:44:34.000000Z
字数 1642
阅读 825
Python
from scipy import optimize as op
import scipy.integrate
import matplotlib.pyplot as plt
import os
import numpy as np
from find_data import load_fulldata
k = 1.38e-23
N0 = 6.02e23
n = 7
datafile = r'D:\zhouhuibin\20210220_Lab\data\Bridgeman Co3Sn2-xInxS2\heat capacity\20201124_In0.82\0.82-HeatCap.dat'
savepath = r'D:\zhouhuibin\20210220_Lab\data\Bridgeman Co3Sn2-xInxS2\heat capacity\20201124_In0.82'
def integrand(t):
#return t
return np.exp(t)*np.power(t,4)/np.power((np.exp(t)-1),2)
def C(T_all,T_D,r):
C = [9*n*k*N0*(T/T_D)**3*scipy.integrate.quad(integrand, 0.0, T_D/T)[0]+r*T for T in T_all]
return C
#vcurve = np.vectorize(curve)
# vcurve = numpy.vectorize(curve, excluded=set([1]))
data = np.array(load_fulldata(datafile,'[Data]'))
T_raw = data[:,0]
C_raw = data[:,1]
popt, pcov = op.curve_fit(C, T_raw, C_raw)
print popt, pcov
y_fit = C(T_raw,popt[0],popt[1])
plt.clf()
plt.cla()
plt.plot(T_raw,C_raw,'bo',T_raw,y_fit,'r-')
plt.savefig(os.path.join(savepath,'fit_test.png'))
plt.show()
def load_fulldata(infilename, key_word):
# infilename should be a string enclosed by ''
ifile = open (infilename,'r') # open datafile
lines= ifile.readlines() #read data
line_no = 0 # define line number
found_string = False
for line in lines: # search for starting mark of data
if key_word in line:
#if 'Tableau' in line:
print ('found in line '+ str(line_no))
found_string = True
break
line_no += 1
if not found_string:
print ('string not found');
if found_string:
numbers = [] # define empty data matrix
for line in lines[line_no+2:]: # iterate and check line from second row after starting mark
line = line.replace(',',' ') # replace ',' with 'tab' key
words = line.split()
numbers_row = []
for i1 in range(len(words)):
number = float(words[i1])
numbers_row.append(number)
numbers.append(numbers_row)
return numbers
ifile.close()