def getDOS( Data = "test_cases/ni_0300/sqe.pkl", MT = "test_cases/ni_0300/mqe.pkl", C_ms = numpy.arange(0.0,2.0,0.1), backgroundFrac = 0.90, constantFrac = 0.00, cutoff = 8.5, elasticCutAvg = 3, longE = 40.0, cutRange = (1e-20,1e20), eStop = 60.0 , T = 300.0, M = 58.6934, N = 10, Tol = 1.0e-7, maxIter = 50, interactive = True, viewDirectory = 'tmp', outputDir = 'tmp', ): """ This is the `main` function for finding a DOS and a multiphonon/ multiple scattering correction from experimental scattering. user.py contains the user modifiable parameters. """ # *** LJ *** # check output dirs _checkOutdir( viewDirectory ) _checkOutdir( outputDir ) # copy the magic html file shutil.copy( os.path.join(paths.data, magic_html ), viewDirectory ) #shutil.copy(magic_html, viewDirectory) if interactive: # open a browser viewhtml = os.path.abspath( os.path.join( viewDirectory, magic_html ) ) bthread = BrowserThread( 'file://' + viewhtml ) bthread.start() # record time import time time1 = time.time() # ********** # --- Prep S(Q,E)for calculation ----------------------------------------------- sqe = expSqe(Data,T,M,cutRange=cutRange) # *** LJ *** if not MT: mqe = None else: mqe = expSqe( MT,T,M,cutRange=cutRange) # ********** if mqe: sqe.removeBackground(mqe,backgroundFrac,constantFrac) sqe.cropForCalc(cutoff,longE,eStop,elasticCutAvg) sqe.norm2one() sqe.expand(2.0) sqe0 = expSqe(sqe) sqe.plotSE(viewDirectory) sqe.plotSQE(viewDirectory,lower=1e-30,upper=2.5e-4) sqe.plotMask(viewDirectory) # --- Fitting ------------------------------------------------------------------ C_ms += 1.0 # This is a hack, until the internal rep of C_ms is changed. # ------------------------------------------------------------------------------ res = getCorrectedScatter(sqe,C_ms,N,Tol,maxIter,interactive,vd=viewDirectory) sqeCalc,dosCalc,cmsCalc,res,C_ms,lsqSc,lsqMu,lsqSl,LSQ \ = getBestSol(sqe0,res,C_ms) dosCalc.plotDOS(viewDirectory) # --- Output to file and pickle ------------------------------------------------ cp.dump((sqe0,C_ms,res,lsqSc,lsqMu,lsqSl,LSQ),\ open( os.path.join( outputDir,"all.pkl") ,'wb'),-1) cp.dump((sqe0,sqeCalc,dosCalc,cmsCalc),\ open( os.path.join( outputDir,"sol.pkl") ,'wb'),-1) # *** LJ *** saveDOSasHistogram( dosCalc, os.path.join( outputDir, "doshist.pkl") ) # ********** f = open( os.path.join( outputDir,"C_ms" ),'w') f.write( "C_ms = %lf\n" % (C_ms[numpy.argmin( numpy.array(LSQ)**2 )]-1.0) ) f.close() io.write(dosCalc.e,dosCalc.g, os.path.join( outputDir,"Dos" ) ) io.write(dosCalc.e,dosCalc.gz, os.path.join( outputDir,"Dos.z" ) ) io.write(sqe0.e,sqe0.se, os.path.join( outputDir,"Se.exp" ) ) io.write(sqe0.e,nar(nar(sqeCalc)), os.path.join( outputDir,"Se.clc" ) ) io.write(sqe0.e,nar(nar(sqeCalc[1:])),os.path.join( outputDir,"Multi.clc") ) io.write(sqe0.e,nar(nar(sqeCalc[1:]))/(cmsCalc),\ os.path.join( outputDir,"Mph.clc" ) ) io.write(sqe0.e,(cmsCalc-1.0)*nar(nar(sqeCalc[1:]))/cmsCalc\ ,os.path.join( outputDir,"Msc.clc" ) ) # --- `Interactive` Output ----------------------------------------------------- SQE = expSqe(sqe0.q,sqe0.e,nar(sqeCalc),sqe0.sqerr,sqe0.T,sqe0.M,cutRange=cutRange) plotComp(sqe0,sqeCalc,viewDirectory) plotLSQ(C_ms,lsqSc,lsqMu,lsqSl,LSQ,viewDirectory) plotSQE(SQE,viewDirectory,'sqeCalc.png',title='S(Q,E) Calculated',\ lower=1e-30,upper=2.5e-4) return
G = Gnuplot.Gnuplot() Gd = Gnuplot.Data G('set grid') #------------------------------------------------------------------------------ #---- Setup objects ----------------------------------------------------------- e,g = io.load(dosFile) dE = e[1] - e[0] E = numpy.arange(floor(-maxE/dE)*dE, (ceil(maxE/dE)+1)*dE, dE) #if e[0]==0.0: #dos contains 0 point # E = numpy.concatenate((-1*e[::-1],e[1:])) #else: #dos does not contain 0 point # E = numpy.concatenate((numpy.append(-1*e[::-1],0.0),e)) Q = numpy.arange(minQ, maxQ, dQ) fakeSqe = numpy.outer(Q,E) sqe = expSqe.expSqe(Q,E,fakeSqe,fakeSqe,T,M,cutRange=cutRange) e = numpy.arange(0.0, maxE+dE, dE) dos = numpy.zeros(len(e)) if len(g) <= len(dos): dos[:len(g)] += g else: dos += g[:len(dos)] dos = densityOfStates.densityOfStates(e,dos,cutRange=cutRange) #------------------------------------------------------------------------------ #---- Get multiphonon scattering ---------------------------------------------- ANE = multiphonon.AthroughN(sqe,dos,N) #for i in range(len(ANE)): # print 'ane',ANE[i] SNQ = multiphonon.SthroughN(sqe,dos,N)
def getCorrectedScatter(sqe, Ei, C_ms, N, Tol, maxIter, interactive, vd=False): """ sqe = experimental S(Q,E) in instance of class expSqe C_ms = list of multipliers C_ms[i]*S_2+(Q,E) = S_2+(Q,E) + multiple scattering N = number of terms to evaluate in multiphonon expansion Tol = convergence at iteration N+1 when (g_{N+1}(E) - g_N(E))^2 < Tol interactive = if True, print information while iterating Returns a list `res` of self consistent solutions for the DOS from the scattering, one for each value in C_ms. res[i] = [ S(Q,E), dos, C_ms[i] ] where S(Q,E) = numpy.array{ S_1(Q,E), S_2(Q,E), ..., S_N(Q,E) } """ sqe0 = expSqe(sqe) res = [] i = 0 tot = len(C_ms) cmsStrLen = 0 #----For outputting named files--------- for x in C_ms: if len(str(x)) > cmsStrLen: cmsStrLen = len(str(x)) #----------------------------------------------------------------- for x in C_ms: i += 1 if interactive: print " " print "--- Multiple scattering Factor "+str(i)+" of "+str(tot)\ +" -------------------------------" print "Dos converged to ... " sys.stdout.flush() dos = sqe2dos(sqe0, Ei) dos0 = Dos(dos.e,dos.g,cutRange=sqe.cutRange) another = True iter = 0 while another == True: SQE = getSQE(sqe0,dos,N,x) sqeSears = getSQE(sqe0,dos,N,x) sqe.sqe = sqe0.sqe - sqeSearsGeometryEffects(sqe0,dos,N)[1:] - doubleScatteringSqe() #sqe.sqe = sqe0.sqe - (nar(SQE[1:]) # [1:] is for subtracting multiple contributions; dos = sqe2dos(sqe, Ei) if nar( (dos.gz-dos0.gz)**2.0 ) > Tol and iter < maxIter: print "Penalty = %le" %nar( (dos.gz-dos0.gz)**2 ) sys.stdout.flush() dos0 = Dos(dos.e,dos.g,cutRange=sqe.cutRange) iter += 1 else: if iter == maxIter: print "NOT CONVERGED AFTER %d ITERATIONS" % iter print "Final penalty: %le" % nar( (dos.gz-dos0.gz)**2 ) sys.stdout.flush() another = False res.append( [SQE, dos , x] ) if interactive: plotSQE(expSqe(sqe0.q,sqe0.e,nar(res[-1][0]),sqe0.sqerr,sqe0.T,sqe0.M),\ vd,'sqeCalc.png',title='S(Q,E) Calculated at '+str(x),lower=1e-30,\ upper=2.5e-4) plotComp(sqe0,res[-1][0],vd) res[-1][1].plotDOS(vd) #----For outputting named files--------- #------ This probably doesn't work in windows, b/c "cp" --------- cmsStr = str(x) while len(cmsStr) < cmsStrLen: cmsStr += '0' os.system("cp " + os.path.join(vd,"dos.png") + " " + \ os.path.join(vd,"dos-" + cmsStr + ".png") ) os.system("cp " + os.path.join(vd,"sqeCalc.png") + " " + \ os.path.join(vd,"sqeCalc-" + cmsStr + ".png") ) os.system("cp " + os.path.join(vd,"seComp.png") + " " + \ os.path.join(vd,"seComp-" + cmsStr + ".png") ) #----------------------------------------------------------------- print " --- " print " " return res