#pragma TextEncoding = "UTF-8" #pragma rtGlobals=3 // Use modern global access method and strict wave access. Function simDFT(tval,ovpVal,IPwave,mol,alpha,i0,phi,expSpecName,expEnergyName,expFolderPath,fit,rigidShift,thetaList,anchorStep1,anchorStep2,anchorExp1,anchorExp2,stepWid1,stepWid2,stepE1,stepE2,[d,holdAmps,holdWidths,holdPos,NEXAFStype,stepShift,startPre,endPre,startPost,endPost,refine,maskEnergy1,maskEnergy2,pkToRefine,refpw,holdmodTheta]) Variable tval Variable ovpVal Wave IPwave String mol Variable alpha Variable i0 Variable phi String expSpecName //Base name of spectra String expEnergyName //Base name of experimental energy waves String expFolderPath String fit //If yes then try to fit the data, else just model it Variable rigidShift Variable stepShift Variable d String thetaList //Semicolon separated list of theta values to simulate/fit Variable holdAmps Variable holdWidths Variable holdPos String NEXAFStype Variable startPre //At what energy should the preedge start to be defined? Variable endPre //At what energy should the preedge stop to be defined? Variable startPost //At what energy should the postedge start to be defined? Variable endPost //At what energy should the postedge stop to be defined? Variable anchorStep1,anchorStep2 Variable anchorExp1,anchorExp2 Variable stepWid1,stepWid2,stepE1,stepE2 Variable refine Variable maskEnergy1 Variable maskEnergy2 Variable pkToRefine Wave refpw Variable holdmodTheta // ======================/ Define defaults/================================= if(ParamIsDefault(holdAmps)) holdAmps = 0 endif if(ParamIsDefault(holdWidths)) holdWidths = 1 endif if(ParamIsDefault(holdPos)) holdPos = 1 endif if(ParamIsDefault(NEXAFStype)) NEXAFStype = "" endif if(ParamIsDefault(refine)) refine = 0 endif if(ParamIsDefault(holdmodTheta)) holdmodTheta = 1 endif Variable openParams = 4 - holdAmps - holdWidths - holdPos - holdmodTheta Variable timerStart = startMSTimer if(!refine) Wave pw2d = getParams(tval,ovpVal) makeTensor(pw2d) normalizeTensor() prepSimDFTfit(expSpecName,expEnergyName,mol,IPwave,expFolderPath,anchorStep1,anchorStep2,stepWid1,stepWid2,stepE1,stepE2,stepShift = rigidShift) make1DparamWave(IPwave,pw2d,alpha,i0,phi,rigidShift = rigidShift) endif Wave pWave1D,paramNames,allExpSpec,allExpEnergy,allDFTSteps Variable n = numpnts(allExpSpec),nSpec = ItemsInList(thetaList),i if(!refine) Duplicate/O pWave1D,pwCopy else Duplicate/O refpw,refitpWave2 endif Wave valuesPolar = makePolarWave2(thetaList,n) //Make wave containing theta values Wave pwAlpha = fitAlpha2(maskEnergy1,maskEnergy2,pWave1D,allExpEnergy,allExpSpec,nSpec,thetaList,alpha,i0,tval,ovpval,allDFTSteps) pwAlpha[2] = alpha Wave pwI0 = fitI0(maskEnergy1,maskEnergy2,pwAlpha,allExpEnergy,allExpSpec,valuesPolar,allDFTSteps,nSpec,tval,ovpval)//Fit i0 if(!refine) Duplicate/O allExpSpec,res,results1,results2,results3,results4,results5,results6,results,weightWave results = 0;res = 0 endif Variable nPeaks = (numpnts(pWave1D) - 4 - pWave1D[0])/11 Variable pks2Fit = nPeaks String H1 = makeHoldString(holdAmps,holdWidths,holdPos,pWave1D,holdModTheta,1,1) Wave eps1 = makeEpsiltonWave(pWave1D,holdAmps,holdWidths,holdPos,holdModTheta,1,1) Wave/T constraint1 = makeConstraintWave(holdAmps,holdWidths,holdPos,nPeaks,pWave1D,holdmodTheta,1,1) //Trim the waves to be fit. Lower energies can be wonky for(i=n-1;i>=0;i-=1) Variable cEn = allExpEnergy[i] if(cEn < 279.9) DeletePoints i,1,allExpEnergy,allExpSpec,allDFTSteps,res,results1,results2,results3,results4,results5,results6,results,weightWave,valuesPolar endif endfor Wave allExpEnergy,allExpSpec,allDFTSteps,res,results1,results2,results3,results4,results,weightWave,valuesPolar Variable npnts = numpnts(allExpSpec) print " ╠═════════════ Refining To NEXAFS ══════════════╣ " print " ╟───────────────────┬───────────┬───────────────╢ " print " ║ PARAMS │ CHI SQR │ RED CHI SQR ║ " print " ╟───────────────────┼───────────┼───────────────╢ " if(StringMatch(fit,"yes")) weightWave = 0.0622831*allExpSpec Variable V_FitTol = 0.00001,V_FitError = 0 if(refine) fitSinglePeak(refpw,pkToRefine,holdPos,holdWidths,holdAmps,holdmodTheta,tval,ovpVal,alpha,mol) String newpwName = "refine_pwCopy_pk" + num2str(pkToRefine) Wave refitpWave = $newpwName Abort else // Refine Amp // ----------------------------------------------------------------------- Duplicate/O pwI0, pwFit1 Make/O/N=6 rcs2,alphaFit,idFit,gf2,cs2 idFit[0] = 0;idFit[1] = 1;idFit[2] = 2;idFit[3] = 3;idFit[4] = 4;idFit[5] = 5 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit1, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps1 /D=results1 /I=1 /W=weightWave if(V_FitError) V_chiSq = NaN endif Wave pwFit NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,1,0) printf " ║ AMP │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╟───────────────────┼───────────┼───────────────╢ " rcs2[0] = redchiSqExpCl alphaFit[0] = pwFit1[2] cs2[0] = V_chisq Wave M_Covar,W_Sigma,pwMolAdj // Refine Amp & Theta // ----------------------------------------------------------------------- Duplicate/O pwFit1, pwFit2 H1 = makeHoldString(0,1,1,pwFit2,0,1,2) Wave eps2 = makeEpsiltonWave(pwFit2,0,1,1,0,1,2) Wave/T constraint2 = makeConstraintWave(0,1,1,nPeaks,pwFit2,0,1,2) V_FitTol = 0.00001;V_FitError = 0//;V_FitMaxIters=100 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit2, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps2 /D=results2 /I=1 /W=weightWave if(V_FitError)//If something in the fit fails, set V_chiSq to NaN for error handling V_chiSq = NaN endif NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,3,0) printf " ║ AMP/θ │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╟───────────────────┼───────────┼───────────────╢ " rcs2[1] = redchiSqExpCl alphaFit[1] = pwFit2[2] cs2[1] = V_chisq // Refine Amp, Theta & Energy // ----------------------------------------------------------------------- Duplicate/O pwFit2, pwFit3 H1 = makeHoldString(0,1,0,pwFit3,0,1,3) Wave eps3 = makeEpsiltonWave(pwFit3,0,1,0,0,1,3) Wave/T constraint3 = makeConstraintWave(0,1,0,nPeaks,pwFit3,0,1,3) V_FitTol = 0.00001;V_FitError = 0//;V_FitMaxIters=100 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit3, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps3 /D=results3 /I=1 /W=weightWave if(V_FitError)//If something in the fit fails, set V_chiSq to NaN for error handling V_chiSq = NaN endif NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,3,0) printf " ║ AMP/θ/POS │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╟───────────────────┼───────────┼───────────────╢ " rcs2[2] = redchiSqExpCl alphaFit[2] = pwFit3[2] cs2[2] = V_chisq // Refine All // ----------------------------------------------------------------------- Duplicate/O pwFit3, pwFit4 H1 = makeHoldString(0,0,0,pwFit4,0,1,4) Wave eps4 = makeEpsiltonWave(pwFit4,0,0,0,0,1,4) Wave/T constraint4 = makeConstraintWave(0,0,0,nPeaks,pwFit4,0,1,4) V_FitTol = 0.00001;V_FitError = 0//;V_FitMaxIters=100 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit4, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps4 /D=results4 /I=1 /W=weightWave if(V_FitError)//If something in the fit fails, set V_chiSq to NaN for error handling V_chiSq = NaN endif NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,4,0) printf " ║ ALL │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╟───────────────────┼───────────┼───────────────╢ " rcs2[3] = redchiSqExpCl alphaFit[3] = pwFit4[2] cs2[3] = V_chisq // Refine All // ----------------------------------------------------------------------- Duplicate/O pwFit4, pwFit5 H1 = makeHoldString(0,0,0,pwFit5,0,0,5) Wave eps5 = makeEpsiltonWave(pwFit5,0,0,0,0,0,5) Wave/T constraint5 = makeConstraintWave(0,0,0,nPeaks,pwFit5,0,0,5) V_FitTol = 0.00001;V_FitError = 0//;V_FitMaxIters=100 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit5, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps5 /D=results5 /I=1 /W=weightWave if(V_FitError)//If something in the fit fails, set V_chiSq to NaN for error handling V_chiSq = NaN endif NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,5,1) printf " ║ ALL │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╟───────────────────┼───────────┼───────────────╢ " rcs2[4] = redchiSqExpCl alphaFit[4] = pwFit5[2] cs2[4] = V_chisq // Refine All // ----------------------------------------------------------------------- WaveStats/R=[0,4]/Q rcs2 if(V_minloc == 0) Duplicate/O pwFit1,pwFit6 elseif(V_minloc == 1) Duplicate/O pwFit2,pwFit6 elseif(V_minloc == 2) Duplicate/O pwFit3,pwFit6 elseif(V_minloc == 3) Duplicate/O pwFit4,pwFit6 elseif(V_minloc == 4) Duplicate/O pwFit5,pwFit6 endif H1 = makeHoldString(0,0,0,pwFit6,0,0,6) Wave eps6 = makeEpsiltonWave(pwFit6,0,0,0,0,0,6) Wave/T constraint6 = makeConstraintWave(0,0,0,nPeaks,pwFit6,0,0,6) V_FitTol = 0.00001;V_FitError = 0 FuncFit/Q/H=H1/M=2 simDFTfit2,pwFit6, allExpSpec /X={allExpEnergy,valuesPolar,allDFTSteps} /R=res /E=eps6 /D=results6 /I=1 /W=weightWave if(V_FitError) V_chiSq = NaN endif NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl chiSqExpCl = V_chisq redchiSqExpCl = calcRedChiSq(allExpSpec,V_Chisq,nPeaks,5,1) printf " ║ ALL │ %07.1f │ %05.3f ║ \r", V_chisq, redchiSqExpCl print " ╚═══════════════════╧═══════════╧═══════════════╝ " rcs2[5] = redchiSqExpCl alphaFit[5] = pwFit6[2] cs2[5] = V_chisq WaveStats/Q rcs2 if(idFit[V_minloc] == 0) Duplicate/O results1,results Duplicate/O pwFit1,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[0] chiSqExpCl = cs2[0] elseif(idFit[V_minloc] == 1) Duplicate/O results2,results Duplicate/O pwFit2,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[1] chiSqExpCl = cs2[1] elseif(idFit[V_minloc] == 2) Duplicate/O results3,results Duplicate/O pwFit3,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[2] chiSqExpCl = cs2[2] elseif(idFit[V_minloc] == 3) Duplicate/O results4,results Duplicate/O pwFit4,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[3] chiSqExpCl = cs2[3] elseif(idFit[V_minloc] == 4) Duplicate/O results5,results Duplicate/O pwFit5,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[4] chiSqExpCl = cs2[4] elseif(idFit[V_minloc] == 5) Duplicate/O results6,results Duplicate/O pwFit6,pwFinal NVAR redchiSqExpCl = root:redchiSqExpCl NVAR chiSqExpCl = root:chiSqExpCl redchiSqExpCl = rcs2[5] chiSqExpCl = cs2[5] endif //Calculate Goodness of Fit (GF) parameter for subsequent BIC analysis NVAR GFExpCl = root:GFExpCl GFExpCl = 0 Variable num=0,den=0 for(i=0;i tol) if(dif > 0) stepSum2 -= dif elseif(dif < 0) stepSum2 += dif endif dftStepLo_2 = findWaveValAtEergy(allExpEnergy,stepSum2,anchorStep1) dif = dftStepLo_2 - bareStepLo // print dif endif return stepSum2 End Function findWaveValAtEergy(xw,yw,val) Wave xw,yw Variable val Variable p1 = round(BinarySearchInterp(xw,val)) Variable dftLo = yw[p1] return dftLo End Function/WAVE findBAenergy() String currentFolder=GetdataFolder(1) SetDataFolder root:Packages:NXA WAVE/Z mu_energy String newNameEn = currentFolder + "mu_energy" Duplicate/O mu_energy,$newNameEn SetDataFolder currentFolder return mu_energy End Function/WAVE findBAMA(mol) String mol String currentFolder=GetdataFolder(1) SetDataFolder root:Packages:NXA WAVE/Z mu=$(mol+"_mu") String newNameMu = currentFolder + "mu" Duplicate/O mu,$newNameMu If( !WaveExists(mu) ) Abort "Couldn't find the indicated mass absorption wave! Aborting!" endif SetDataFolder currentFolder return mu End Function/WAVE makeLongSpecWave(expSpecName) String expSpecName String listOfExpSpec = SortList(WaveList(expSpecName + "*",";",""),";",24) Make/O/D/N=0 allExpSpec = 0 Concatenate/O/NP listOfExpSpec,allExpSpec return allExpSpec End Function/WAVE makeLongEnWave(expEnName) String expEnName String listOfExpEn = SortList(WaveList(expEnName + "*",";",""),";",24) Make/O/D/N=0 allExpEn = 0 Concatenate/O/NP listOfExpEn,allExpEn return allExpEn End Function/WAVE makeStep(IPcorr,pntsPerSpec,stepWid1,stepWid2,stepE1,stepE2,mol,expSpecName,expEnergyName) Wave IPcorr Variable pntsPerSpec Variable stepWid1,stepWid2,stepE1,stepE2 String mol String expSpecName //Base name of spectra String expEnergyName //Base name of experimental energy waves String listOfExpSpec = SortList(WaveList(expSpecName + "*",";",""),";",24) String listOfExpEnergy = SortList(WaveList(expEnergyName + "*",";",""),";",24) Variable nSpec = ItemsInList(listOfExpSpec) String dummyName = StringFromList(0,listOfExpEnergy) Wave dummyEnergy = $dummyName Variable startPre= 240 //At what energy should the preedge start to be defined? Variable endPre = 284.1 //At what energy should the preedge stop to be defined? Variable startPost = 284.3 //At what energy should the postedge start to be defined? Variable endPost = 360 //At what energy should the postedge stop to be defined? //Determine min and max values for StepEdge WaveStats/Q IPcorr Variable stepWid, stepDecay = 0 //Determine pre and post edges of bare atom absoprtion using a polynomial fit //Find the bare atom waves Wave mu_energy = findBAenergy() Wave mu = findBAMA(mol) FitPoly3(mu,mu_energy,startPre,endPre) WAVE W_Coef Duplicate/d/o W_Coef, PreEdge_Coef FitPoly3(mu,mu_energy,startPost,endPost) Duplicate/d/o W_Coef, PostEdge_Coef Variable StitchLow =poly(PreEdge_Coef,endPre) Variable StitchHigh=poly(PostEdge_Coef,startPost) //Make step edges for each atom Variable nAtoms = numpnts(IPcorr),i Variable dftPreEn2 = round(BinarySearchInterp(dummyEnergy,IPcorr[V_minloc])) Variable dftPosEn2 = round(BinarySearchInterp(dummyEnergy,IPcorr[V_maxloc])) Make/O/N=(pntsPerSpec) stepSum=0 for(i=0;i=pps) j=0 endif endfor //Fill the redimensioned wave with new theta values j=0 for(i=0;i=0","K0<=90","K1>=0"} fitpw[0] = alpha fitpw[1] = i0 fitpw[2] = 0 //Only fit Pi Range for(j=0;j 55) // Debugger //endif for(i=0;i=nColors) j = 0 endif endfor Label left "Mass Absorbance [cm\S2\M/g] \U" Label bottom "Transition Energy [eV]" ModifyGraph mirror=1,nticks=10,minor=1,fStyle=1,fSize=14 if(StringMatch(fitStage,"alphaFit")) TextBox/C/N=text0/A=MC "α="+num2str(alpha)+"°" elseif(StringMatch(fitStage,"i0Fit")) TextBox/C/N=text0/A=MC "i0="+num2str(i0) endif endif End Function/WAVE fitI0(E1,E2,pw,xw,ew,tw,sw,nSpec,tval,ovpval) Wave pw,xw,ew,sw,tw //Parameter wave, concatenated energy wave,concatenated experimental NEXAFS,concatenated sample theta wave Variable E1,E2 //Energy range that we want to fit alpha in Variable nSpec //How many spectra are we fitting? Variable tval,ovpval Wave mw = makeMaskWave(E1,E2,xw) String H = makeHoldStrI0Fit(pw) Wave eps = makeEpsiltonWaveI0Fit(pw) Duplicate/O ew,i0FitResults,res Duplicate/O pw, pwi0Fit Variable V_FitError=0 FuncFit/Q/H=H simDFTfit2,pwi0Fit, ew /X={xw,tw,sw} /R=res /E=eps /D=i0FitResults/M=mw splitSpec(i0FitResults,nSpec,"i0Fit",alpha=round(pwi0Fit[2])) splitSpec(xw,nSpec,"energy") splitSpec(ew,nSpec,"exp") Variable alpha = pwi0Fit[2],i0 = pwi0Fit[1] //plotFitResults("i0Fit",alpha,i0,tval,ovpval) return pwi0Fit End Function organizeTensorWaves(alpha,fit) Variable alpha String fit String iniFolder = GetDataFolder(1) Variable i,j NewDataFolder/O TensorMisc String misc = WaveList("!totalNEXAFS*",";","") Variable nMisc = ItemsInList(misc) for(j=0;j=n-1) break endif if(i==n-1) i=0 j+=1 endif endfor //Remove Normalized Values of Oscillator Strength. Not used for fitting. for(i=11*nPeaks+4+pw[0]-6;i>4 + pw[0];i-=11) DeletePoints i,6,cvMadAdj,corrMat DeletePoints/M=1 i,6,cvMadAdj,corrMat DeletePoints i,6,pw2,pNames2 endfor //Remove Ionization Potentials. Not used for fitting. for(i=pw[0] + 3;i>3;i-=1) DeletePoints i,1,cvMadAdj,corrMat DeletePoints/M=1 i,1,cvMadAdj,corrMat DeletePoints i,1,pw2,pNames2 endfor ////Remove phi and number of Atoms. Not used for fitting. if(d) String corrGName = "CORRELATION_" + replaceString(".",num2str(alpha),"p") DoWindow $corrGName//CORRELATION if(!V_Flag) WaveStats/Q corrMat NewImage/N=$corrGName/K=1 corrMat ModifyImage corrMat ctab= {*,*,RedWhiteBlue256,0} ModifyGraph nticks=10,fStyle=1,gridStyle=2,gridRGB=(0,0,0),margin(left)=25,margin(top)=25,margin(right)=90,fsize=10 Label left "Fit Parameters";DelayUpdate Label top "Fit Parameters" ColorScale/C/N=text0/A=RC/E ctab={-0.2,0.2,RedWhiteBlue256,0},axisRange={V_min,V_max} ColorScale/C/N=text0 "Correlation \\U" endif endif End Function FitPoly3(yw,xw,StartX, EndX) WAVE yw, xw Variable startX, EndX FindLevel/P/Q xw, startX Variable startP=floor(V_LevelX) Findlevel/P/Q xw, endX Variable endP=floor(V_LevelX) CurveFit/W=2/Q poly 3, yw[startP,endP]/X=xw[startP,endP] end Function Lstep(x, x0,width) Variable x, x0, width return 1/2 + 1/pi * atan((x-x0)/(width/2)) end Function Gstep(x,x0,width) Variable x, x0, width Variable c=2*sqrt(2) return 1/2 + 1/2*erf((x-x0)/(width/c)) end Function/S makeHoldString(holdAmps,holdWidths,holdPos,pWave1D,holdModTheta,holdAlpha,step) Variable holdAmps,holdWidths,holdPos,holdModTheta,holdAlpha,step Wave pWave1D Variable i,nPeaks = (numpnts(pwave1d)-pwave1d[0]-4)/11 //Make Hold String for fit. Open amplitudes, hold position and width constant if(holdAlpha) String H = "1111" //H1 = Number of atoms, i0, alpha, phi else H = "1101" endif for(i=1;i<=pWave1D[0];i+=1) H +="1" //For Ionization Potentials from DFT. Used to build the Step Edge. Hold them constant. endfor for(i=0;i<=nPeaks-1;i+=1) //Hold string for peak parameters. Position, Width, Max Amplitude, xxNoRM, xyNorm,xzNorm,yyNorm,yzNorm,zzNorm //Hold cases: if(!holdPos && !holdWidths && !holdAmps && !holdModTheta) H +="00011111110"//"00011111110" elseif(holdPos && !holdWidths && holdAmps && !holdModTheta) H +="10111111110" elseif(holdPos && !holdWidths && !holdAmps && !holdModTheta) H +="10011111110"//"10011111110" elseif(holdPos && holdWidths && !holdAmps && !holdModTheta) H +="11011111110"//"11011111110" elseif(!holdPos && holdWidths && !holdAmps && !holdModTheta) H +="01011111110" elseif(!holdPos && !holdWidths && holdAmps && !holdModTheta) H +="00111111110" elseif(!holdPos && holdWidths && holdAmps && !holdModTheta) H +="01111111110" elseif(holdAmps && holdWidths && holdAmps && !holdModTheta) H +="11111111110" elseif(!holdPos && !holdWidths && !holdAmps && holdModTheta) H +="00011111111"//"00011111110" elseif(holdPos && !holdWidths && holdAmps && holdModTheta) H +="10111111111" elseif(holdPos && !holdWidths && !holdAmps && holdModTheta) H +="10011111111"//"10011111110" elseif(holdPos && holdWidths && !holdAmps && holdModTheta) H +="11011111111"//"11011111110" elseif(!holdPos && holdWidths && !holdAmps && holdModTheta) H +="01011111111" elseif(!holdPos && !holdWidths && holdAmps && holdModTheta) H +="00111111111" elseif(!holdPos && holdWidths && holdAmps && holdModTheta) H +="01111111111" elseif(holdAmps && holdWidths && holdAmps && holdModTheta) H +="11111111111" endif endfor String hName = "holdWave" + num2str(step) Make/O/N=(numpnts(pWave1D)) $hName Wave holdWave = $hName for(i=0;i= 0","K2 <= 90"} for(i=4 + pw[0];i < 4 + pw[0] + 11*(nPeaks);i+=11) pos = pw[i+0] if(pos <= Emax) c = "K" + num2str(i+2) + " >= 0" Redimension/N=(2+j) constraintAlpha constraintAlpha[j+1] = c j+=1 else break endif endfor return constraintAlpha End Function/WAVE makeEpsiltonWave(pWave1D,holdAmps,holdWidths,holdPos,holdModTheta,holdAlpha,step,[tol]) Wave pWave1D Variable holdAmps,holdWidths,holdPos,holdModTheta,holdAlpha,step,tol Variable i,nPeaks = (numpnts(pwave1d)-pwave1d[0]-4)/11,epsPos,epsWid,epsAmp,epsMT //Set up values for epsilon wave String eName = "eps" +num2str(step) Make/O/N=(numpnts(pWave1D)) $eName Wave eps = $eName if(ParamIsDefault(tol)) epsPos = 1e-3 epsWid = 1e-3 epsAmp = 1e-3 epsMT = 1e-3 else epsPos = tol epsWid = tol epsAmp = tol epsMT = tol endif eps[0] = 0 eps[1] = 0 if(holdAlpha) eps[2] = 0 else eps[2] = 1e-2 endif eps[3] = 0 for(i=4;i14) // n=1 // wMult=1.5 // else // // endif //Variable openParams = holdAmps + holdWidths + holdPos String cName = "constraints" + num2str(step) if(!holdAlpha) j=2 aMod=2 String alphaLo = "K2 >= 0" String alphaHi = "K2 <= 90" else j=0 aMod=0 endif if(!holdAlpha) if(nPeaks == 1) Make/O/T/N=(2) $cName Wave/T constraints = $cName else Make/O/T/N=(nPeaks) $cName Wave/T constraints = $cName endif constraints[0] = alphaLo constraints[1] = alphaHi else Make/O/T/N=(nPeaks) $cName Wave/T constraints = $cName endif String lcp,hcp,lcp2,hcp2//Constraint strings for position String lcw,hcw//Constraint strings for width String lca //Constraint strings for amplitude String lcm,hcm //Constraint strings for modTheta if(!holdPos && !holdWidths && !holdAmps &&holdModTheta) Redimension/N=(5*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid wHigh = wMult*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lca = "K" + num2str(i+2) + " > 0" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lcw constraints[j+3] = hcw constraints[j+4] = lca j+=5 endfor elseif(holdPos && !holdWidths && holdAmps &&holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) constraints[j+0] = lcw constraints[j+1] = hcw j+=2 endfor elseif(holdPos && !holdWidths && !holdAmps &&holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lcw constraints[j+1] = hcw constraints[j+2] = lca j+=3 endfor elseif(holdPos && holdWidths && !holdAmps &&holdModTheta) Redimension/N=(1*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lca j+=1 endfor elseif(!holdPos && holdWidths && !holdAmps &&holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lca = "K" + num2str(i+2) + " > 0" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lca j+=3 endfor elseif(!holdPos && !holdWidths && holdAmps &&holdModTheta) Redimension/N=(5*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lca = "K" + num2str(i+1) + " > 0" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lcw constraints[j+3] = hcw constraints[j+4] = lca j+=5 endfor elseif(!holdPos && holdWidths && holdAmps &&holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp j+=2 endfor //////////////////////////////////////////////////////// elseif(!holdPos && !holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(7*nPeaks+aMod) constraints//(5*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lcw constraints[j+3] = hcw constraints[j+4] = lca constraints[j+5] = lcm constraints[j+6] = hcm j+=7 endfor elseif(holdPos && !holdWidths && holdAmps &&!holdModTheta) Redimension/N=(4*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = hcw constraints[j+2] = lcm constraints[j+3] = hcm j+=4 endfor elseif(holdPos && !holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(5*nPeaks+aMod) constraints//(3*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = hcw constraints[j+2] = lca constraints[j+3] = lcm constraints[j+4] = hcm j+=5 endfor elseif(holdPos && holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints//(2*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lca constraints[j+1] = lcm constraints[j+2] = hcm j+=3 endfor elseif(!holdPos && holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(5*nPeaks+aMod) constraints//(4*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lca constraints[j+3] = lcm constraints[j+4] = hcm j+=5 endfor elseif(!holdPos && !holdWidths && holdAmps &&!holdModTheta) Redimension/N=(6*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lcw = "K" + num2str(i+1) + " > 0" hcw = "K" + num2str(i+1) + " <=" + num2str(wHigh) lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lcw constraints[j+3] = lcw constraints[j+4] = lcm constraints[j+5] = hcm j+=6 endfor elseif(!holdPos && holdWidths && holdAmps &&!holdModTheta) Redimension/N=(4*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid // lcp = "K" + num2str(i+0) + " >=" + num2str(pLow) // hcp = "K" + num2str(i+0) + " <=" + num2str(pHigh) lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" if(i==4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pLow) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) elseif(i == 11*(nPeaks-1) + 4 + pWave1D[0]) lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pHigh) else lcp = "K" + num2str(i+0) + " >" + num2str(pWave1D[i-11]) hcp = "K" + num2str(i+0) + " <" + num2str(pWave1D[i+11]) endif hcp2 = "K" + num2str(i+0) + " <=" + num2str(emax) constraints[j+0] = lcp constraints[j+1] = hcp constraints[j+2] = lcm constraints[j+3] = hcm j+=4 endfor elseif(holdAmps && holdWidths && holdAmps &&holdModTheta) print "Constraint wave not built due to all parameters being held constant" endif return constraints End Function/WAVE makeConstraintWave2(holdAmps,holdWidths,holdPos,nPeaks,pWave1D,holdModTheta,holdAlpha,step) Variable holdAmps,holdWidths,holdPos,holdModTheta,holdAlpha,step Variable nPeaks Wave pWave1D Variable i,j,pos,pLow,pHigh,wHigh,wid,n=1,emax=360,aMod,wMult=1.5 // if(nPeaks>14) // n=1 // wMult=1.5 // else // // endif //Variable openParams = holdAmps + holdWidths + holdPos String cName = "constraints" + num2str(step) if(!holdAlpha) j=2 aMod=2 String alphaLo = "K2 >= 0" String alphaHi = "K2 <= 90" else j=0 aMod=0 endif if(!holdAlpha) if(nPeaks == 1) Make/O/T/N=(2) $cName Wave/T constraints = $cName else Make/O/T/N=(nPeaks) $cName Wave/T constraints = $cName endif constraints[0] = alphaLo constraints[1] = alphaHi else Make/O/T/N=(nPeaks) $cName Wave/T constraints = $cName endif String lcp,hcp,lcp2,hcp2//Constraint strings for position String lcw,hcw//Constraint strings for width String lca //Constraint strings for amplitude String lcm,hcm //Constraint strings for modTheta if(!holdPos && !holdWidths && !holdAmps &&holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lcw constraints[j+1] = lca j+=2 endfor elseif(holdPos && !holdWidths && holdAmps &&holdModTheta) Redimension/N=(1*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" constraints[j+0] = lcw j+=1 endfor elseif(holdPos && !holdWidths && !holdAmps &&holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lcw constraints[j+2] = lca j+=2 endfor elseif(holdPos && holdWidths && !holdAmps &&holdModTheta) Redimension/N=(1*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lca j+=1 endfor elseif(!holdPos && holdWidths && !holdAmps &&holdModTheta) Redimension/N=(1*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lca = "K" + num2str(i+2) + " > 0" constraints[j+0] = lca j+=1 endfor elseif(!holdPos && !holdWidths && holdAmps &&holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid lcw = "K" + num2str(i+1) + " > 0" lca = "K" + num2str(i+1) + " > 0" constraints[j+2] = lcw constraints[j+4] = lca j+=2 endfor elseif(!holdPos && holdWidths && holdAmps &&holdModTheta) Redimension/N=(0*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid j+=0 endfor //////////////////////////////////////////////////////// elseif(!holdPos && !holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(4*nPeaks+aMod) constraints//(5*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid lcw = "K" + num2str(i+1) + " > 0" lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = lca constraints[j+2] = lcm constraints[j+3] = hcm j+=4 endfor elseif(holdPos && !holdWidths && holdAmps &&!holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = lcm constraints[j+2] = hcm j+=3 endfor elseif(holdPos && !holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(4*nPeaks+aMod) constraints//(3*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) wid = pWave1D[i+1] wHigh = wMult*wid lcw = "K" + num2str(i+1) + " > 0" lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = lca constraints[j+2] = lcm constraints[j+3] = hcm j+=4 endfor elseif(holdPos && holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints//(2*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lca constraints[j+1] = lcm constraints[j+2] = hcm j+=3 endfor elseif(!holdPos && holdWidths && !holdAmps &&!holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints//(4*nPeaks) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lca = "K" + num2str(i+2) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lca constraints[j+1] = lcm constraints[j+2] = hcm j+=3 endfor elseif(!holdPos && !holdWidths && holdAmps &&!holdModTheta) Redimension/N=(3*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] wHigh = wMult*wid pLow = pos - n*wid pHigh = pos + n*wid lcw = "K" + num2str(i+1) + " > 0" lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcw constraints[j+1] = lcm constraints[j+2] = hcm j+=3 endfor elseif(!holdPos && holdWidths && holdAmps &&!holdModTheta) Redimension/N=(2*nPeaks+aMod) constraints for(i=4 + pWave1D[0];i<11*nPeaks + 4 + pWave1D[0];i+=11) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lcm = "K" + num2str(i+10) + " >= -90" hcm = "K" + num2str(i+10) + " <= 90" constraints[j+0] = lcm constraints[j+1] = hcm j+=2 endfor elseif(holdAmps && holdWidths && holdAmps &&holdModTheta) print "Constraint wave not built due to all parameters being held constant" endif return constraints End Function alphaModelPlotting(fit,allExpEnergy,allExpSpec,allStepEnergy,allDFTSteps,results,totalNEXAFS,nSpec,nPeaks,pwCopy,tval,ovpVal,thetaList,d,NEXAFStype,[alpha]) String fit,thetaList,NEXAFStype Wave allExpEnergy,allExpSpec,allStepEnergy,allDFTSteps,results,totalNEXAFS,pwCopy Variable nSpec,nPeaks,tval,ovpVal,d,alpha if(StringMatch(fit,"yes")) splitSpec(allExpEnergy ,nSpec,"expEnergy" ,alpha=round(pwCopy[2])) splitSpec(allExpSpec ,nSpec,"expSpec" ,alpha=round(pwCopy[2])) splitSpec(allStepEnergy,nSpec,"stepEnergy",alpha=round(pwCopy[2])) splitSpec(allDFTSteps ,nSpec,"dftStep" ,alpha=round(pwCopy[2])) splitSpec(results ,nSpec,"fitresults" ,alpha=round(pwCopy[2])) // if(d) // print "Generating comparison plots from fits." // plotResultsFit2("expSpec;fitresults;dftStep;",pwCopy[2],"expEnergy",thetaList,tval,ovpVal,nPeaks,NEXAFStype) // endif elseif(StringMatch(fit,"no")) splitSpec(allExpEnergy ,nSpec,"expEnergy" ,alpha=alpha) splitSpec(allExpSpec ,nSpec,"expSpec" ,alpha=alpha) splitSpec(allStepEnergy,nSpec,"stepEnergy",alpha=alpha) splitSpec(allDFTSteps ,nSpec,"dftStep" ,alpha=alpha)//,reScale=1) splitSpec(totalNEXAFS ,nSpec,"dftSpec" ,alpha=alpha) if(d) print "Generating comparison plots from modeling." // plotPeaks(nSpec,nPeaks,pwCopy[2],tval,ovpVal) plotResults("dftSpec;",alpha,"expEnergy",thetaList,tval,ovpVal) endif endif End Function findMagicAngle(thetaList) String thetaList Variable nThetas = ItemsInList(thetaList),i for(i=0;i= E1 && en <= E2) maskWave[i] = 1 else maskWave[i] = 0 endif endfor return maskWave End // bare fit function for simDFT Function simpleDFTModel(pw,norm3D,thetaValues) Wave pw //1D Parameter wave. Has peak positions,widths, and amplitudes. has initial guess for alpha and IPs required to build step edge Wave norm3D //Absorption tensor Wave thetaValues Variable i,j=0,k,m //Make the absorption tensor for each transition, normalize it, and place each transition into a layer of a 3D wave Variable nTransitions = DimSize(norm3D,2) Variable pos,wid,amp,alpha,i0,phi Variable p0,pf,nSpec=numpnts(thetaValues),cSpec=1,thetaVal Variable nAtoms = pw[0] Variable targetDP = 10 Make/O/D/N=(3,3) currentTensor = 0 //This is the wave that will contain the DFT tensor model NEXAFS Variable specpnts = 300*nSpec Variable ini = 280,fin=290,res=2000 Wave xWave = makeX(ini,fin,specpnts,nSpec) Wave thw = makeThetaWave(ini,fin,specpnts,nSpec,thetaValues) Variable nPnts = specpnts/nSpec Make/O/D/N=(nTransitions) MA = 0 //Make rotation matrices for rotation of 90,180,and 270 degrees around z-axis Make/O/D/N=(3,3) rotMat0 = {{cos(0*(Pi/180)) ,sin(0*(Pi/180)),0} ,{-sin(0*(Pi/180)) ,cos(0*(Pi/180)),0} ,{0,0,1}} Make/O/D/N=(3,3) rotMat90 = {{cos(90*(Pi/180)) ,sin(90*(Pi/180)),0} ,{-sin(90*(Pi/180)) ,cos(90*(Pi/180)),0} ,{0,0,1}} Make/O/D/N=(3,3) rotMat180 = {{cos(180*(Pi/180)),sin(180*(Pi/180)),0},{-sin(180*(Pi/180)),cos(180*(Pi/180)),0},{0,0,1}} Make/O/D/N=(3,3) rotMat270 = {{cos(270*(Pi/180)),sin(270*(Pi/180)),0},{-sin(270*(Pi/180)),cos(270*(Pi/180)),0},{0,0,1}} for(i=3;i<3+3*nTransitions;i+=3) i0 = pw[0] alpha = pw[1]//90-pw[i+9] phi = pw[2] pos = pw[i + 0] wid = pw[i + 1] amp = pw[i + 2] currentTensor = i0 * amp * norm3D[p][q][j] Make/O/D/N=(3,3) rotMatAlignX = {{1,0,0},{0,cos(alpha*(Pi/180)),sin(alpha*(Pi/180))},{0,-sin(alpha*(Pi/180)),cos(alpha*(Pi/180))}} MatrixOP/O tiltedTensor = rotMatAlignX x currentTensor x rotMatAlignX^t MatrixOP/O RotTensor0 = rotMat0 x tiltedTensor x rotMat0^t MatrixOP/O RotTensor90 = rotMat90 x tiltedTensor x rotMat90^t MatrixOP/O RotTensor180 = rotMat180 x tiltedTensor x rotMat180^t MatrixOP/O RotTensor270 = rotMat270 x tiltedTensor x rotMat270^t MatrixOP/O filmTensor = RotTensor0 + RotTensor90 + RotTensor180 + RotTensor270 for(k=0;k<=2;k+=1) for(m=0;m<=2;m+=1) Variable currComp = filmTensor[k][m] targetDP = round(targetDP) currComp = round(currComp * (10^targetDP)) / (10^targetDP) if(currComp == -0) currComp = 0 endif filmTensor[k][m] = currComp endfor endfor if(j Controls the lower bound for the peak position //hc0 --> Controls the upper bound for the peak position //lc1 --> Controls the lower bound for the peak width (always be greater than 0) //lc2 --> Controls the lower bound on the peak amplitude (Always greater than 0) //lc3 --> Controls the lower bound for the ratio between the tensor elements (always greater than zero) //lc4 --> Controls the lower bound for the tilt angle (always greater than or equal to 0 degrees) //lc5 --> Controls the upper bound on the tilt angle (always less than or equal to 90 degrees) if(!holdPos && !holdWidths && !holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(5*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc1 = "K" + num2str(i+1) + " >= 0" lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc1 constraints[j+3] = lc2 constraints[j+4] = lc3 j+=5 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" constraints[j+0] = lc2 j+=1 cpk+=1 endif endfor elseif(holdPos && !holdWidths && holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc1 = "K" + num2str(i+1) + " >= 0" constraints[j+0] = lc1 j+=1 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdPos && !holdWidths && !holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(3*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc1 = "K" + num2str(i+1) + " >= 0" lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc1 constraints[j+1] = lc2 // constraints[j+2] = lc3 j+=2 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdPos && holdWidths && !holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(2*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && holdWidths && !holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(4*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc2 constraints[j+3] = lc3 j+=4 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && !holdWidths && holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc1 = "K" + num2str(i+1) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc1 constraints[j+3] = lc3 j+=4 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && holdWidths && holdAmps && !holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc3 j+=3 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && !holdWidths && !holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(5*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc1 = "K" + num2str(i+1) + " >= 0" lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc1 constraints[j+3] = lc2 // constraints[j+4] = lc3 j+=4 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdPos && !holdWidths && holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc1 = "K" + num2str(i+1) + " >= 0" constraints[j+0] = lc1 j+=1 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdPos && !holdWidths && !holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(3*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc1 = "K" + num2str(i+1) + " >= 0" lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc1 constraints[j+1] = lc2 // constraints[j+2] = lc3 j+=2 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdPos && holdWidths && !holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(2*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && holdWidths && !holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints//(4*nPeaks) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc2 = "K" + num2str(i+2) + " >= 0" lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc2 // constraints[j+3] = lc3 j+=3 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && !holdWidths && holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) lc1 = "K" + num2str(i+1) + " >= 0" constraints[j+0] = lc0 constraints[j+1] = hc0 constraints[j+2] = lc1 j+=3 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(!holdPos && holdWidths && holdAmps && holdSecAmp) Redimension/N=(nPeaks + totalConstraints) constraints j=0 for(i=4 + pWave1D[0];i < 4 + pWave1D[0] + 11*(nPeaks+1);i+=11) if(cpk == pkID) pos = pWave1D[i+0] wid = pWave1D[i+1] pLow = pos - n*wid pHigh = pos + n*wid lc0 = "K" + num2str(i+0) + " >=" + num2str(pLow) hc0 = "K" + num2str(i+0) + " <=" + num2str(pHigh) constraints[j+0] = lc0 constraints[j+1] = hc0 j+=2 cpk+=1 else lc2 = "K" + num2str(i+2) + " >= 0" // lc3 = "K" + num2str(i+10) + " >= 0" constraints[j+0] = lc2 // constraints[j+1] = lc3 j+=1 cpk+=1 endif endfor elseif(holdAmps && holdWidths && holdAmps && holdSecAmp) print "Constraint wave not built due to all parameters being held constant" endif return constraints End Function/WAVE makeSingleEpsiltonWave(pWave1D,pkID,holdAmps,holdWidths,holdPos,holdSecAmp) Wave pWave1D Variable pkID Variable holdAmps,holdWidths,holdPos,holdSecAmp Variable nPeaks = (numpnts(pWave1D) - 4 - pWave1D[0])/11,cPk=0 Variable i //Set up values for epsilon wave String epsWaveName = "eps_Pk" + num2str(pkID) Make/O/N=(numpnts(pWave1D)) $epsWaveName Wave eps = $epsWaveName Variable epsPos = 1e-7 Variable epsWid = 1e-7 Variable epsAmp = 1e-4 Variable epsProp = 1e-4 eps[0] = 0 eps[1] = 0//5e-9 eps[3] = 0 for(i=4;i vPaMax) paraMax = plPaMax else paraMax = vPaMax endif if(plPeMin < vPeMin) perpMin = plPeMin else perpMin = vPeMin endif if(plPeMax > vPeMax) perpMax = plPeMax else perpMax = vPeMax endif //Compare Total intensities of vector and planar orbitals DoWindow plotVecvsPlanarOrbs if(!V_Flag) Display/N=plotVecvsPlanarOrbs/K=1 vecOrb,plOrb vs tw NewFreeAxis/L para NewFreeAxis/L perp AppendToGraph/W=plotVecvsPlanarOrbs/L=para plParaInt,vecParaInt vs tw AppendToGraph/W=plotVecvsPlanarOrbs/L=perp plPerpInt,vecPerpInt vs tw ModifyGraph mode=4,marker=19,lsize=1.5, freePos(para)=0,freePos(perp)=0 Label left "Total Intensity[a.u.]" Label bottom "θ[°]" Label para "Para Intensity[a.u.]" Label perp "Perp Intensity[a.u.]" SetAxis para paraMin,paraMax SetAxis perp perpMin,perpMax ModifyGraph grid=2,mirror=1,minor=1 ModifyGraph rgb(plOrb)=(1,4,52428),rgb(plParaInt)=(1,4,52428),rgb(plPerpInt)=(1,4,52428) ModifyGraph lblPosMode(left)=1,axisEnab(left)={0.69,1} ModifyGraph lblPosMode(para)=1,axisEnab(para)={0.34,0.66} ModifyGraph lblPosMode(perp)=1,axisEnab(perp)={0,0.32} Legend/C/N=text0/J/A=RB "\\JCα="+num2str(a)+"°\r\\s(plOrb) Planar\r\\s(vecOrb) Vector" else Legend/C/N=text0/J/W=plotVecvsPlanarOrbs "\\JCα="+num2str(a)+"°\r\\s(plOrb) Planar\r\\s(vecOrb) Vector" endif End Function/S detOrbType(tensor) Wave tensor String type if((tensor[0][0]==tensor[1][1]==tensor[2][2])) type = "I" elseif((tensor[0][0]==0) && (tensor[1][1]==0) && (tensor[2][2]!=0)) type = "V" elseif((tensor[0][0]!=0) && (tensor[1][1]!=0) && (tensor[2][2]==0)) type = "P" elseif((tensor[0][0]!=0) && (tensor[1][1]!=0) && (tensor[2][2]!=0)) type = "M" endif print type return type End Function modelTensorInt(pw,thw,tensor) Wave pw //1D Parameter wave. Has peak positions,widths, and amplitudes. has initial guess for alpha and IPs required to build step edge Wave thw //Wave containing the various values of theta Wave tensor Variable i,j=0,k,m,targetDP = 10,n=numpnts(thw) Variable pos,wid,amp,alpha,i0,phi,localalpha,amp2 Duplicate/O tensor,currentTensor Duplicate/O pw,npw Make/O/N=(n) tensorInt,tempMA //Make rotation matrices for rotation of 90,180,and 270 degrees around z-axis Make/O/D/N=(3,3) rotMat0 = {{cos(0*(Pi/180)) ,sin(0*(Pi/180)),0} ,{-sin(0*(Pi/180)) ,cos(0*(Pi/180)),0} ,{0,0,1}} Make/O/D/N=(3,3) rotMat90 = {{cos(90*(Pi/180)) ,sin(90*(Pi/180)),0} ,{-sin(90*(Pi/180)) ,cos(90*(Pi/180)),0} ,{0,0,1}} Make/O/D/N=(3,3) rotMat180 = {{cos(180*(Pi/180)),sin(180*(Pi/180)),0},{-sin(180*(Pi/180)),cos(180*(Pi/180)),0},{0,0,1}} Make/O/D/N=(3,3) rotMat270 = {{cos(270*(Pi/180)),sin(270*(Pi/180)),0},{-sin(270*(Pi/180)),cos(270*(Pi/180)),0},{0,0,1}} String vecType = detOrbType(tensor) //*****POTENTIALLY CHANGE CLUSTERING ALGORITHM**** //Generate clusters based on overall/total OS instead of component OS //How to open up individual tensor elements for fitting: //1. Ratio of in-plane vs out-of-plane components? //2. Need a second parameter to describe amplitude? Possibly related to a local alpha? //*********Will need to change pWave from 10 parameters per peak to 11!******* //3. Visualize tensor elements for each cluster (Tensor Element vs Cluster ID) -->Log plot //4. Additive oscillator strength... How to accomplish this programatically??? alpha = npw[0]*(Pi/180) phi = npw[1]*(Pi/180) pos = npw[2] wid = ABS(npw[3]) amp = ABS(npw[4]) amp2 = npw[5] currentTensor = amp * tensor[p][q] Make/O/N=2 vector vector[0] = sqrt(tensor[0][0]) vector[1] = sqrt(0.5*tensor[2][2]) //vector[2] = tensor[2][2] Wave rv = vecOpsWrap(vector,amp2,"Z") currentTensor[0][0] = abs(rv[0])^2 currentTensor[1][1] = abs(rv[0])^2 currentTensor[2][2] = 2*abs(rv[1])^2 Make/O/N=(3,3) modMolTensor = 0 modMolTensor[0][0] = abs(rv[0])^2 modMolTensor[1][1] = abs(rv[0])^2 modMolTensor[2][2] = 2*abs(rv[1])^2 //print sqrt(currentTensor[0][0]^2+currentTensor[1][1]^2+currentTensor[2][2]^2) Make/O/D/N=(3,3) rotMatAlignX = {{1,0,0},{0,cos(alpha),sin(alpha)},{0,-sin(alpha),cos(alpha)}} //Tilt the tensor by angle alpha MatrixOP/O tiltedTensorX = rotMatAlignX x currentTensor x rotMatAlignX^t //Construct the film tensor by adding the azimuthal rotations MatrixOP/O RotTensor0X = rotMat0 x tiltedTensorX x rotMat0^t MatrixOP/O RotTensor90X = rotMat90 x tiltedTensorX x rotMat90^t MatrixOP/O RotTensor180X = rotMat180 x tiltedTensorX x rotMat180^t MatrixOP/O RotTensor270X = rotMat270 x tiltedTensorX x rotMat270^t MatrixOP/O filmTensor = RotTensor0X + RotTensor90X + RotTensor180X + RotTensor270X Wave filmTensorTrunc = truncateMatrix(targetDP,filmTensor) //print sqrt(filmTensorTrunc[0][0]^2+filmTensorTrunc[1][1]^2+filmTensorTrunc[2][2]^2) //Apply electric field to current tensor to obtain the mass absorbance for(i=0;i ModAmp(amp2) //print mag,mag2,mag3 return rv End Function calcDeterminant(tensor,rotAngle) Wave tensor Variable rotAngle Variable detIni,detFin,As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is,Ae,Be,Ce,De,Ee,Fe,Ge,He,Ie rotAngle = rotAngle * (Pi/180) Make/O/D/N=(3,3) rotMat = {{cos(rotAngle) ,sin(rotAngle),0} ,{-sin(rotAngle) ,cos(rotAngle),0} ,{0,0,1}} MatrixOP/O newTensor = rotMat x tensor x rotMat^t As = tensor[0][0];Bs = tensor[0][1];Cs = tensor[0][2] Ds = tensor[1][0];Es = tensor[1][1];Fs = tensor[1][2] Gs = tensor[2][0];Hs = tensor[2][1];Is = tensor[2][2] detIni = As * (Es*Is - Fs*Hs) - Bs * (Ds*Is - Fs*Gs) + Cs * (Ds*Hs - Es*Gs) Ae = newtensor[0][0];Be = newtensor[0][1];Ce = newtensor[0][2] De = newtensor[1][0];Ee = newtensor[1][1];Fe = newtensor[1][2] Ge = newtensor[2][0];He = newtensor[2][1];Ie = newtensor[2][2] detFin = Ae * (Ee*Ie - Fe*He) - Be * (De*Ie - Fe*Ge) + Ce * (De*He - Ee*Ge) print detIni print detFin End Function calcNormTensor(tensor) Wave tensor Variable i,j for(i=0;i<3;i+=1) Variable normT = (tensor[i][j])^2 if(j==2) break elseif(i<2) j+=1 else i=0 endif endfor print normT End Function/WAVE truncate2D(w,targetDP) Wave w Variable targetDP WaveStats/Q w Variable k,m //If the value of a component is less than tol times the maximum tensor component then set it to 0 for(k=0;k<=2;k+=1) for(m=0;m<=2;m+=1) Variable currComp = w[k][m] targetDP = round(targetDP) currComp = round(currComp * (10^targetDP)) / (10^targetDP) if(currComp == -0) currComp = 0 endif w[k][m] = currComp endfor endfor return w End Function calcRedChiSq(ori,chiSq,nPeaks,openParams,openAlpha) Wave ORI Variable chiSq,nPeaks,openParams,openAlpha Variable n=numpnts(ori),redChiSq=0 redChiSq = chiSq/(n-nPeaks*openParams - openAlpha*1) return redChiSq End Function/WAVE scaleExptoBA(expSpec,expEn,anchorStep1,anchorStep2,mol) Wave expSpec,expEn Variable anchorStep1,anchorStep2 String mol //Find the bare atom waves Wave mu_energy = findBAenergy() Wave mu = findBAMA(mol) //Find energies in mu Variable lo = BinarySearch(mu_energy,anchorStep1) Variable hi = BinarySearch(mu_energy,anchorStep2) WaveStats/R=[lo,hi]/Q mu Variable mins = V_min Duplicate/O expSpec,test //Find the experimental energies used for scaling Variable exp280 = findWaveValAtEergy(expEn,expSpec,anchorStep1) Variable exp360 = findWaveValAtEergy(expEn,expSpec,anchorStep2) //Find the bare atom energies used for scaling Variable bareStepLo = findWaveValAtEergy(mu_energy,mu,anchorStep1) Variable bareStepHi = findWaveValAtEergy(mu_energy,mu,anchorStep2) Variable expScale = (exp360-exp280) / (bareStepHi-bareStepLo) test = test/ expScale + V_min return test End Function MAtoF2F1(maw,ew,rho,mol) Wave maw,ew //Mass absorbance and energy waves Variable rho //Density of molecule String mol//Name of molecule Variable Na=6.0221415e23 //Avogadro's number [at/d.mol] Variable re=2.81794e-13 //classical electron radius [cm] Variable hc=1.97263e-05//eV*cm //1.23984e-4//Product of speed of light and Planck's constant Wave mu = findBAMA(mol) Variable Mw=NumberByKey("Mw",Note(mu))//Molecular weight of molecule Variable zstar=NumberByKey("Zstar",Note(mu))//Zstar for molecule String betaWaveStr=removeending(nameofwave(maw),"_ma")+"_beta" String f2WaveStr=removeending(betaWaveStr,"_beta")+"_f2" Duplicate/O maw,$betaWaveStr,$f2WaveStr Wave betas = $betaWaveStr Wave F2 = $f2WaveStr //Convert mass absorbance to beta and then to f2 betas = maw*hc/(4*pi*ew) F2=(Mw*maw*ew)/(2*re*hc*Na) //Calculate f1 from f2 wave //Wave f1 = NXA_KK_FFT2(ew, F2, zstar) Wave f1 = BAC_KK_FFT(ew, F2, zstar,19)// String deltaWaveStr=removeending(nameofwave(f1),"_f1")+"_delta" Duplicate/O/D f1,$deltaWaveStr Wave delta = $deltaWaveStr Variable n=numpnts(ew),i for(i=0;i expSpecName is "SP" String expEnName //Base name for experimental energy waves. Same instruction as expSpecName //Ensure that there is at least one layer for the 3D waves Variable etaDim = abs((eta - etaFin)/etaDel) if(etaDim==0) etaDim = 1 endif //Make the concatenated NEXAFS wave Wave allExpSpec = makeLongSpecWave(expSpecName) Wave allExpEnergy = makeLongEnWave(expEnName) Variable tol = 1e-3,expScale,pps=round(numpnts(allExpEnergy)/nSpec),k=0 //Find the bare atom waves //Wave mu_energy = findBAenergy() //Wave mu = findBAMA(mol) WaveStats/Q stepSum //Find the bare atom intensity values at the energies used for scaling Variable bareStepLo = findWaveValAtEergy(allExpEnergy,stepSum,anchor1) Variable bareStepHi = findWaveValAtEergy(allExpEnergy,stepSum,anchor2) Duplicate/O allExpSpec,allExpSpec2 //Find the experimental intensity values at the energies used for scaling Variable expLo = findWaveValAtEergy(allExpEnergy,allExpSpec2,anchor1) Variable expHi = findWaveValAtEergy(allExpEnergy,allExpSpec2,anchor2) //Scale the experimental data to the bare atom absorption Variable equalAlpha = 0 //Find points in experiment wave that define pi and sigma manifold energy ranges FindLevel/Q/R=[0,pps-1] allExpEnergy,pi1 Variable piPnt1 = round(V_levelX) FindLevel/Q/R=[0,pps-1] allExpEnergy,pi2 Variable piPnt2 = round(V_levelX) FindLevel/Q/R=[0,pps-1] allExpEnergy,sig1 Variable sigPnt1 = round(V_levelX) FindLevel/Q/R=[0,pps-1] allExpEnergy,sig2 Variable sigPnt2 = round(V_levelX) //Wave that will contain NEXAFS scaling results Make/O/N=(pps,nSpec,etaDim) ScaledEtaNEXAFS = 0 //Make waves that will contain the intensities,energies,and fit uncertainties at pi and sigma manifold Variable nPi = piPnt2-piPnt1, nSig = sigPnt2-sigPnt1,i,j Make/O/N=(nPi,nSpec+1) piIntsAlpha = 0 Make/O/N=(nSig,nSpec+1) sigIntsAlpha = 0 Make/O/N=(nSpec) thetas = 0 Make/O/N=(nPi) alphasPi,energiesPi = 0,sigmasPi = 0,avgAlphaPi = 0 Make/O/N=(nSig) alphasSig = 0,energiesSig = 0,sigmasSig = 0,avgAlphaSig = 0 //3D waves for fit results Make/O/N=(nSpec,nPi,etaDim) piFitResults2D = 0,piExp2D = 0,piResiduals2D = 0 Make/O/N=(3,nPi,etaDim) piPW2D = 0 Make/O/N=(nSpec,nSig,etaDim) SigFitResults2D = 0,SigExp2D = 0,SigResiduals2D = 0 Make/O/N=(3,nSig,etaDim) sigPW2D = 0 Make/O/N=(etaDim) etaWave do //Scale the experimental NEXAFS to the bare atom absorption generated from DFT //---------------------------------------------------------------------------------------- if(method == 1) expScale = expLo/bareStepLo //Try anchoring at just preedge print expLo,bareStepLo,expScale elseif(method == 2) expScale = (expHi-expLo) / (bareStepHi-bareStepLo)//Anchor at post and preedge endif allExpSpec2 = allExpSpec / (eta *expScale) Variable expLo_2 = findWaveValAtEergy(allExpEnergy,allExpSpec2,anchor1) Variable dif = expLo_2 - bareStepLo if(abs(dif) > tol) //print "Difference between experiment and step is larger than tolerance. Check." if(dif > 0) allExpSpec2 -= dif elseif(dif < 0) allExpSpec2 += dif endif expLo_2 = findWaveValAtEergy(allExpEnergy,allExpSpec2,anchor1) if ((expLo_2 - bareStepLo) > tol) Debugger endif //print dif endif //----------------------------------------------------------------------------------------- if(eta==1) break endif //This loop will populate the energies for desired energy pi range for(i=0;i=0","K0<=90","K1>=0"} fitpw[0] = alpha1 fitpw[1] = i0 fitpw[2] = 0 //Only fit Pi Range //Populate this wave with the intensities for each spectrum at current energy for(j=0;j=0","K0<=90","K1>=0"} //Populate this wave with the intensities for each spectrum at current energy for(j=0;j= etaDim) break endif while(equalAlpha == 0 && eta > etaFin) splitSpec(stepSum,nSpec,"STEPWAVE_") //print avgPiAlpha,avgSigAlpha,eta return allExpSpec2 End