def convolution(simulated, resolution, expdata, convolved, dak=None, norm2one=False): """Convolve a simulated S(Q,E) with a resolution file Arguments: simulated: Nexus file containing S(Q,E) from a simulation resolution: Nexus file containing the resolution. This will be used to produce a elastic line. convolved: Output Nexus file containing the convolution of the simulated S(Q,E) with the model beamline. expdata: Optional, experimental nexus file. Convolved will be binned as expdata. Returns: workspace for the convolution """ from mantid.simpleapi import (LoadNexus, Rebin, ConvertToHistogram, NormaliseToUnity, SaveNexus, SaveAscii, AddSampleLog) wss=LoadNexus(Filename=simulated,OutputWorkspace='simulated') width=wss.readX(0)[1]-wss.readX(0)[0] # rebin resolution as simulated wsr=LoadNexus(Filename=resolution,OutputWorkspace='resolution') #symmetrize the domain of the resolution function. Otherwise the #convolution results in a function with its peak shifted from the origin min=wsr.readX(0)[0] max=wsr.readX(0)[-1] delta=min+max if delta<0: wsr=Rebin(wsr, Params=(-max,width,max)) elif delta>0: wsr=Rebin(wsr, Params=(min,width,-min)) else: wsr=Rebin(wsr, Params=(min,width,max)) # convolve now, overwriting simulateds for i in range(wss.getNumberHistograms()): v=wsr.readY(i) w=wss.readY(i) x=camm_convolve(w,v,mode='same') wss.setY(i,x) wse=LoadNexus(Filename=expdata,OutputWorkspace='expdata') width=wse.readX(0)[1]-wse.readX(0)[0] # rebin simulated as expdata Rebin(InputWorkspace='simulated', Params=(wse.readX(0)[0],width,wse.readX(0)[-1]), OutputWorkspace='convolved') ConvertToHistogram(InputWorkspace='convolved', OutputWorkspace='convolved') # remember that output from sassena are point-data if norm2one: wsc=NormaliseToUnity(InputWorkspace='convolved', OutputWorkspace='convolved') AddSampleLog(Workspace='convolved',LogName='NormaliseToUnity',LogText=str(float(norm2one)),LogType='Number') if dak: dakota_vals = getParams(dak) # read in Dakota params file AddSampleLog(Workspace='convolved',LogName='FF1',LogText=str(dakota_vals["FF1"]),LogType='Number') from mantid.simpleapi import mtd SaveNexus(InputWorkspace='convolved', Filename=convolved) return
def modelB_EC_C(model, resolution, convolved, qvalues, assembled, expdata=None, costfile=None): """Assemble the Background, Elastic line and Convolution of the resolution with the simulated S(Q,E) This is a hard-coded model consisting of a linear background, and elastic line, and a convolution: b0+b1*E + +EC(Q)*e0*exp(-e1*Q^2)*Elastic(E) + c0*Resolution(E)xSimulated(Q,E) We load Resolution(E)xSimulated(Q,E) as Convolved(Q,E) EC(Q) is a fit to the Q-dependence of the integrated intensity of the empty can EC(Q) = 2.174495971 - 2.065826056*Q + 0.845367259*Q^2 Arguments: model: beamline model file is a single line, e.g, b0=1.3211; b1=0.00 e0=0.99; e1=1.9; c0=2.3 resolution: Nexus file containing the resolution. This will be used to produce a elastic line. convolved: Nexus file containing the convolution of the simulated S(Q,E) with the resolution. qvalues: single-column file containing list of Q-values assembled: output Nexus file containing the assembled S(Q,E) of the beamline model and the simulated S(Q,E) expdata: Optional, experimental nexus file. If passed, output convolved will be binned as expdata. costfile: Optional, file to store cost. If passed, the cost of comparing convolved and expdata will be saved. Returns: workspace containing the assembled S(Q,E) """ import numpy from mantid.simpleapi import (LoadNexus, ScaleX, ConvertToPointData, SaveNexus, DakotaChiSquared) EC = lambda Q: 2.174495971 - 2.065826056*Q + 0.845367259*Q*Q Q=[float(q) for q in open(qvalues,'r').read().split('\n')] p={} for pair in open(model,'r').readline().split(';'): key,val=pair.split('=') p[key.strip()]=float(val.strip()) wsr=LoadNexus(Filename=resolution,OutputWorkspace='resolution') wsr=ConvertToPointData(wsr) E=wsr.readX(0) wse=ScaleX(InputWorkspace=wsr, OutputWorkspace='elastic',factor=-1) # elastic line wsc=LoadNexus(Filename=convolved,OutputWorkspace='convolved') for i in range(wsc.getNumberHistograms()): elastic=wse.readY(i) # elastic spectrum at a given Q convolved=wsc.readY(i) # convolved spectrum at a given Q wsc.setY(i, (p['b0']+p['b1']*E) + (EC(Q[i])*p['e0']*numpy.exp(-p['e1']*Q[i])*elastic) + (p['c0']*convolved) ) # overwrite spectrum SaveNexus(InputWorkspace=wsc, Filename=assembled) if expdata and costfile: DakotaChiSquared(DataFile=assembled,CalculatedFile=expdata,OutputFile=costfile) return wsc