Exemplo n.º 1
0
class rbsrw(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_newsrw()
        self.ui.setupUi(self)
        self.up = UP()
        self.beam = SRWLPartBeam()
        self.precis = Precis()
        #load initial values from excel
        self.workbook = open_workbook(resource.filename('SRWinitialvalues.xls'))
        self.thin(self.ui.deparg.currentIndex())
        #set srw initial values
        column = self.workbook.sheet_by_name('thin undulator').col(0)
        units = self.workbook.sheet_by_name('thin undulator').col(1)
        units =self.unitstr(units)
        self.GetUndParams(DialogU(self,units,column))
        column = self.workbook.sheet_by_name('thin beam').col(0)
        units = self.workbook.sheet_by_name('thin beam').col(1)
        units =self.unitstr(units)
        self.GetBeamParams(DialogB(self,units,column))
        column = self.workbook.sheet_by_name('thin precision').col(0)
        units = self.workbook.sheet_by_name('thin precision').col(1)
        units =self.unitstr(units)
        self.GetPrecision(DialogP(self,units,column))
        #removed exy option
        self.ui.deparg.removeItem(6)

        #connections
        self.ui.undulator.clicked.connect(self.makeund)
        self.ui.beam.clicked.connect(self.makebeam)
        self.ui.precision.clicked.connect(self.setprec)
        self.ui.deparg.currentIndexChanged.connect(self.thin)
        self.ui.sim.clicked.connect(self.srwbuttonThin)
        self.ui.analyze.clicked.connect(self.AnalyticA)
        #indicators
        self.ui.status.setText('Initiated')
        self.ui.analytic.setText('No calculations performed...As of Yet')


    def AnalyticA(self):
        (Kx,Ky,lam_rn,e_phn)=IDWaveLengthPhotonEnergy(self.up.undPer,self.up.Bx,self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (UP.Kx=0.934*UP.By*UP.undPer, UP.K, RAD.ephn, UP.WorU)=
        #1. derived Kx from UP.By and UP.undPer
        #2. Introduced new class RAD.ephn = radiation class, variable phot energy of nth harmonics
        #3. UP.WorU=flag indicating wiggler or undulator situation
        #Inputs: (harmB.n, UP.undPer, UP.Bx, UP.By, self.beam.partStatMom1.gamma)
        stri='# Ky:'+'{:.3f}'.format(Kx)+\
        ' # Kx:'+'{:.3f}'.format(Ky)+'\n'+\
        '# Wavelength, m           Phot. energy, eV'+'\n'+\
        '1st harmonic '+'{:.3e}'.format(lam_rn)+' '+'{:.3f}'.format(e_phn)+'\n'+\
        '3rd harmonic '+'{:.3e}'.format(lam_rn/3.0)+' '+'{:.3f}'.format(e_phn*3.0)+'\n'+\
        '5th harmonic '+'{:.3e}'.format(lam_rn/5.0)+' '+'{:.3f}'.format(e_phn*5.0)+'\n'

        E_c=CriticalEnergyWiggler(self.up.By,self.up.Bx,self.beam.partStatMom1.gamma)
        #Outputs: (RAD.Ecrit,UPWorU) where RAD.Ecrit is critical energy of Wiggler Radiation
        #Inputs: (UP.Bx, self.beam.partStatMom1.gamma,UP.Kx)
        stra=stri+'# Critical energy:'+'{:.3e}'.format(E_c)+', eV'+'\n'+\
        '-----------------------------------'+'\n'

        (P_W, L_id)=RadiatedPowerPlanarWiggler(self.up.undPer,self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowW,UP.L) where RAD.PowW is radiated power of Wiggler Radiation, UP.L=length of ID
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        #RadiatedPowerPlanarWiggler(lam_u,Bx,N_u,Gam,I_b):

        (RadSpotSize,RadSpotDivergence)=UndulatorSourceSizeDivergence(lam_rn,L_id)
        stre=stra+'# Rad spot size: '+'{:.3e}'.format(RadSpotSize)+', m'+'\n'
        strf=stre+'# Rad divergence: '+'{:.3e}'.format(RadSpotDivergence)+', rad'+'\n'+\
        '-----------------------------------'+'\n'

        strb=strf+'# Length of ID:'+'{:.3f}'.format(L_id)+', m'+'\n' + \
        '# Radiated power:'+'{:.3e}'.format(P_W)+', W'+'\n'

        P_Wdc=CentralPowerDensityPlanarWiggler(self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowCPD) where RAD.PowCPD is radiated central cone power density of Wiggler Radiation
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        strc=strb+'# Central Power Density: '+'{:.3e}'.format(P_Wdc)+', W/mrad2'+'\n'

        SpectralFluxValue=SpectralFLux(self.up.numPer,self.beam.partStatMom1.gamma,1,self.beam.Iavg,Kx)
        strd=strc+'# Spectral flux: '+'{:.3e}'.format(SpectralFluxValue)+', phot/(sec mrad 0.1% BW)'+'\n'

        RadBrightness=SpectralCenBrightness(self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        strw=strd+'# Spectral Central Brightness: '+'{:.3e}'.format(RadBrightness)+', phot/(sec mrad2 0.1% BW)'+'\n'+\
        '-----------------------------------'+'\n'

        self.ui.analytic.setText(strw)

    def GetUndParams(self, dialog):
        units = dialog.u
        self.up.numPer = convertUnitsStringToNumber(dialog.ui.numper.text(),units[0])
        self.up.undPer = convertUnitsStringToNumber(dialog.ui.undper.text(),units[1])
        self.up.Bx = convertUnitsStringToNumber(dialog.ui.bx.text(),units[2])
        self.up.By = convertUnitsStringToNumber(dialog.ui.by.text(),units[3])
        self.up.phBx = convertUnitsStringToNumber(dialog.ui.phbx.text(),units[4])
        self.up.phBy = convertUnitsStringToNumber(dialog.ui.phby.text(),units[5])
        self.up.sBx = convertUnitsStringToNumber(dialog.ui.sbx.text(),units[6])
        self.up.sBy = convertUnitsStringToNumber(dialog.ui.sby.text(),units[7])
        self.up.xcID = convertUnitsStringToNumber(dialog.ui.xcid.text(),units[8])
        self.up.ycID = convertUnitsStringToNumber(dialog.ui.ycid.text(),units[9])
        self.up.zcID = convertUnitsStringToNumber(dialog.ui.zcid.text(),units[10])

    def ShowUndParams(self, dialog):
        units = dialog.u
        dialog.ui.numper.setText(displayWithUnitsNumber(self.up.numPer,units[0]))
        dialog.ui.undper.setText(displayWithUnitsNumber(self.up.undPer,units[1]))
        dialog.ui.bx.setText(displayWithUnitsNumber(self.up.Bx,units[2]))
        dialog.ui.by.setText(displayWithUnitsNumber(self.up.By,units[3]))
        dialog.ui.phbx.setText(displayWithUnitsNumber(self.up.phBx,units[4]))
        dialog.ui.phby.setText(displayWithUnitsNumber(self.up.phBy,units[5]))
        dialog.ui.sbx.setText(displayWithUnitsNumber(self.up.sBx,units[6]))
        dialog.ui.sby.setText(displayWithUnitsNumber(self.up.sBy,units[7]))
        dialog.ui.xcid.setText(displayWithUnitsNumber(self.up.xcID,units[8]))
        dialog.ui.ycid.setText(displayWithUnitsNumber(self.up.ycID,units[9]))
        dialog.ui.zcid.setText(displayWithUnitsNumber(self.up.zcID,units[10]))


    def GetBeamParams(self,dialog):
        units = dialog.u
#        (P_W, L_id)=RadiatedPowerPlanarWiggler(self.up.undPer,self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
#        self.beam.partStatMom1.z=-L_id
# z momentum has to equal length of insertion device which equals the length of the undulator??
        self.beam.Iavg = convertUnitsStringToNumber(dialog.ui.iavg.text(),units[0])
        self.beam.partStatMom1.x = convertUnitsStringToNumber(dialog.ui.partstatmom1x.text(),units[1])
        self.beam.partStatMom1.y = convertUnitsStringToNumber(dialog.ui.partstatmom1y.text(),units[2])
        self.beam.partStatMom1.z = convertUnitsStringToNumber(dialog.ui.partstatmom1z.text(),units[3])
        self.beam.partStatMom1.xp = convertUnitsStringToNumber(dialog.ui.partstatmom1xp.text(),units[4])
        self.beam.partStatMom1.yp = convertUnitsStringToNumber(dialog.ui.partstatmom1yp.text(),units[5])
        self.beam.partStatMom1.gamma = convertUnitsStringToNumber(dialog.ui.partstatmom1gamma.text(),units[6])


    def ShowBeamParams(self, dialog):
        units = dialog.u
        dialog.ui.iavg.setText(displayWithUnitsNumber(self.beam.Iavg,units[0]))
        dialog.ui.partstatmom1x.setText(displayWithUnitsNumber(self.beam.partStatMom1.x,units[1]))
        dialog.ui.partstatmom1y.setText(displayWithUnitsNumber(self.beam.partStatMom1.y,units[2]))
        dialog.ui.partstatmom1z.setText(displayWithUnitsNumber(self.beam.partStatMom1.z,units[3]))
        dialog.ui.partstatmom1xp.setText(displayWithUnitsNumber(self.beam.partStatMom1.xp,units[4]))
        dialog.ui.partstatmom1yp.setText(displayWithUnitsNumber(self.beam.partStatMom1.yp,units[5]))
        dialog.ui.partstatmom1gamma.setText(displayWithUnitsNumber(self.beam.partStatMom1.gamma,units[6]))

    def WfrSetUpE(self,wfrE):
        #wfrE = SRWLWfr() this is the waveform class
        Nenergy = int(float(self.ui.tableWidget.item(0,0).text()))#float?
        Nx = int(float(self.ui.tableWidget.item(1,0).text()))
        Ny = int(float(self.ui.tableWidget.item(2,0).text()))
        wfrE.allocate(Nenergy,Nx,Ny)
        wfrE.mesh.zStart = float(self.ui.tableWidget.item(3,0).text())
        wfrE.mesh.eStart = float(self.ui.tableWidget.item(4,0).text())
        wfrE.mesh.eFin = float(self.ui.tableWidget.item(5,0).text())
        wfrE.mesh.xStart = float(self.ui.tableWidget.item(6,0).text())
        wfrE.mesh.xFin = float(self.ui.tableWidget.item(8,0).text())
        wfrE.mesh.yStart = float(self.ui.tableWidget.item(7,0).text())
        wfrE.mesh.yFin = float(self.ui.tableWidget.item(9,0).text())

    def GetPrecision(self,dialog):
        units = dialog.u
        self.precis.meth = dialog.ui.meth.currentIndex()
        self.precis.relPrec = convertUnitsStringToNumber(dialog.ui.relprec.text(),units[1])
        self.precis.zStartInteg = convertUnitsStringToNumber(dialog.ui.zstartint.text(),units[2])
        self.precis.zEndInteg = convertUnitsStringToNumber(dialog.ui.zendint.text(),units[3])
        self.precis.npTraj = convertUnitsStringToNumber(dialog.ui.nptraj.text(),units[4])
        self.precis.useTermin = dialog.ui.usetermin.currentIndex()
        self.precis.sampFactNxNyForProp = convertUnitsStringToNumber(dialog.ui.sampfactnxny.text(),units[6])

    def ShowPrecision(self,dialog):
        units = dialog.u
        dialog.ui.meth.setCurrentIndex(self.precis.meth)
        dialog.ui.relprec.setText(displayWithUnitsNumber(self.precis.relPrec,units[1]))
        dialog.ui.zstartint.setText(displayWithUnitsNumber(self.precis.zStartInteg,units[2]))
        dialog.ui.zendint.setText(displayWithUnitsNumber(self.precis.zEndInteg,units[3]))
        dialog.ui.nptraj.setText(displayWithUnitsNumber(self.precis.npTraj,units[4]))
        dialog.ui.usetermin.setCurrentIndex(self.precis.useTermin)
        dialog.ui.sampfactnxny.setText(displayWithUnitsNumber(self.precis.sampFactNxNyForProp,units[6]))


    def srwbuttonThin(self):
        if 'srwl' not in globals():
            msg = ' !Warning --'
            msg += 'SRW not installed on this system.'
            self.ui.status.setText(msg)
            raise Exception(msg)
        #UP = self.UndParams()
        und = SRWLMagFldU([SRWLMagFldH(1, 'v', self.up.By, self.up.phBy, self.up.sBy, 1), SRWLMagFldH(1, 'h', self.up.Bx, self.up.phBx, self.up.sBx, 1)], self.up.undPer, self.up.numPer)
        magFldCnt = SRWLMagFldC([und], array('d', [self.up.xcID]), array('d', [self.up.ycID]), array('d', [self.up.zcID]))
        #elecBeam = SRWLPartBeam()
        #self.BeamParams(elecBeam)

        #self.AnalyticA()

        #precis = self.Precision()
        arPrecPar = [self.precis.meth, self.precis.relPrec, self.precis.zStartInteg, self.precis.zEndInteg, self.precis.npTraj, self.precis.useTermin, self.precis.sampFactNxNyForProp]

        wfrE = SRWLWfr()
        self.WfrSetUpE(wfrE)
        wfrE.partBeam = self.beam

        wfrXY = SRWLWfr()
        self.WfrSetUpE(wfrXY)
        wfrXY.partBeam = self.beam

        mesh=deepcopy(wfrE.mesh)
        wfrIn=deepcopy(wfrE)

        Polar = self.ui.polar.currentIndex()
        Intens = self.ui.intensity.currentIndex()
        DependArg = self.ui.deparg.currentIndex()
#       print (Polar, Intens, DependArg)

        if DependArg == 0:
            #after setting the text call self.ui.status.repaint() to have it immediately show otherwise it will wait till it exits the block to draw
            str1='* Performing Electric Field (spectrum vs photon energy) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrE, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n'
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrE.mesh.ne)
            srwl.CalcIntFromElecField(arI1, wfrE, Polar, Intens, DependArg, wfrE.mesh.eStart, 0, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            #time.sleep(1)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrE.mesh.eStart, wfrE.mesh.eFin, wfrE.mesh.ne],
            ['Photon energy, eV','Spectral intensity, ph/s/0.1%BW','Intensity vs photon energy'])

        elif DependArg == 1:
            str1='* Performing Electric Field (intensity vs x-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrXY.mesh.nx)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, 0, wfrXY.mesh.xStart, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrXY.mesh.xStart, wfrXY.mesh.xFin, wfrXY.mesh.nx],
            ['Horizontal Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs x-coordinate'])

        elif DependArg == 2:
            str1='* Performing Electric Field (intensity vs y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, 0, wfrXY.mesh.yStart, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrXY.mesh.yStart, wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Vertical Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs y-coordinate'])

        elif DependArg == 3:
            str1='* Performing Electric Field (intensity vs x- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.nx*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.xStart, 1*wfrXY.mesh.xFin, wfrXY.mesh.nx],
            [1*wfrXY.mesh.yStart, 1*wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Horizontal Position [m]', 'Vertical Position [m]', 'Intensity at ' + str(wfrXY.mesh.eStart) + ' eV'])

        elif DependArg == 4:
            str1='* Performing Electric Field (intensity vs energy- and x-coordinate) calculation ... \n \n '
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.ne*wfrXY.mesh.nx)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.eStart, 1*wfrXY.mesh.eFin, wfrXY.mesh.ne],
            [1*wfrXY.mesh.xStart, 1*wfrXY.mesh.xFin, wfrXY.mesh.nx],
            ['Energy [eV]', 'Horizontal Position [m]', 'Intensity integrated from ' + str(wfrXY.mesh.yStart) + ' to ' + str(wfrXY.mesh.yFin) + ' ,m in y-coordinate'])

        elif DependArg == 5:
            str1='* Performing Electric Field (intensity vs energy- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.ne*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.eStart, 1*wfrXY.mesh.eFin, wfrXY.mesh.ne],
            [1*wfrXY.mesh.yStart, 1*wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Energy [eV]', 'Vertical Position [m]', 'Intensity integrated from ' + str(wfrXY.mesh.xStart) + ' to ' + str(wfrXY.mesh.xFin)+ ' ,m in x-coordinate'])
        else:
            print 'Error'

        uti_plot_show()

    def thin(self,i):
        thinsheet = self.workbook.sheet_by_name('thin table')

        for n,c in enumerate(thinsheet.col(i)):
            self.ui.tableWidget.setItem(n,0,QtGui.QTableWidgetItem(str(c.value)))

    def unitstr(self,units):
        for n,u in enumerate(units):
            units[n]=str(u.value)

        return units

    def makeund(self):
        units = self.workbook.sheet_by_name('thin undulator').col(1)
        units = self.unitstr(units)
        dialog = DialogU(self,units)
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        units = self.workbook.sheet_by_name('thin beam').col(1)
        units = self.unitstr(units)
        dialog = DialogB(self,units)
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        units = self.workbook.sheet_by_name('thin precision').col(1)
        units = self.unitstr(units)
        dialog = DialogP(self,units)
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)
Exemplo n.º 2
0
class rbsrw(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_newsrw()
        self.ui.setupUi(self)
        #self.srwdictionary = dict()
        self.up = UP()
        self.beam = SRWLPartBeam()
        self.precis = Precis()
        self.thin(self.ui.deparg.currentIndex())
        #dialog boxes
        #self.dialogb = DialogB()
        #self.dialogp = DialogP()
        #set srw initial values
        self.GetUndParams(DialogU())
        self.GetBeamParams(DialogB())
        self.GetPrecision(DialogP())

        #connections
        self.ui.undulator.clicked.connect(self.makeund)
        self.ui.beam.clicked.connect(self.makebeam)
        self.ui.precision.clicked.connect(self.setprec)
        self.ui.deparg.currentIndexChanged.connect(self.thin)
        self.ui.sim.clicked.connect(self.srwbuttonThin)
        #self.ui.plot.clicked.connect(self.check)
        #indicators
        self.ui.status.setText('Initiated')
        self.ui.analytic.setText('No calculations performed...Yet')


    def AnalyticA(self):
        (Kx,Ky,lam_rn,e_phn)=IDWaveLengthPhotonEnergy(self.up.undPer,self.up.Bx,self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (UP.Kx=0.934*UP.By*UP.undPer, UP.K, RAD.ephn, UP.WorU)=
        #1. derived Kx from UP.By and UP.undPer
        #2. Introduced new class RAD.ephn = radiation class, variable phot energy of nth harmonics
        #3. UP.WorU=flag indicating wiggler or undulator situation
        #Inputs: (harmB.n, UP.undPer, UP.Bx, UP.By, self.beam.partStatMom1.gamma)
        stri='# Ky:'+'{:.3f}'.format(Kx)+\
        ' # Kx:'+'{:.3f}'.format(Ky)+'\n'+\
        '# Wavelength, m           Phot. energy, eV'+'\n'+\
        '1st harmonic '+'{:.3e}'.format(lam_rn)+' '+'{:.3f}'.format(e_phn)+'\n'+\
        '3rd harmonic '+'{:.3e}'.format(lam_rn/3.0)+' '+'{:.3f}'.format(e_phn*3.0)+'\n'+\
        '5th harmonic '+'{:.3e}'.format(lam_rn/5.0)+' '+'{:.3f}'.format(e_phn*5.0)+'\n'

        E_c=CriticalEnergyWiggler(self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (RAD.Ecrit,UPWorU) where RAD.Ecrit is critical energy of Wiggler Radiation
        #Inputs: (UP.Bx, self.beam.partStatMom1.gamma,UP.Kx)
        stra=stri+'# Critical energy:'+'{:.3e}'.format(E_c)+', eV'+'\n'+\
        '-----------------------------------'+'\n'

        (P_W, L_id)=RadiatedPowerPlanarWiggler(self.up.undPer,self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowW,UP.L) where RAD.PowW is radiated power of Wiggler Radiation, UP.L=length of ID
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        #RadiatedPowerPlanarWiggler(lam_u,Bx,N_u,Gam,I_b):

        (RadSpotSize,RadSpotDivergence)=UndulatorSourceSizeDivergence(lam_rn,L_id)
        stre=stra+'# Rad spot size: '+'{:.3e}'.format(RadSpotSize)+', m'+'\n'
        strf=stre+'# Rad divergence: '+'{:.3e}'.format(RadSpotDivergence)+', rad'+'\n'+\
        '-----------------------------------'+'\n'

        strb=strf+'# Length of ID:'+'{:.3f}'.format(L_id)+', m'+'\n' + \
        '# Radiated power:'+'{:.3e}'.format(P_W)+', W'+'\n'

        P_Wdc=CentralPowerDensityPlanarWiggler(self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowCPD) where RAD.PowCPD is radiated central cone power density of Wiggler Radiation
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        strc=strb+'# Central Power Density: '+'{:.3e}'.format(P_Wdc)+', W/mrad2'+'\n'

        SpectralFluxValue=SpectralFLux(self.up.numPer,self.beam.partStatMom1.gamma,1,self.beam.Iavg,Kx)
        strd=strc+'# Spectral flux: '+'{:.3e}'.format(SpectralFluxValue)+', phot/(sec mrad 0.1% BW)'+'\n'

        RadBrightness=SpectralCenBrightness(self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        strw=strd+'# Spectral Central Brightness: '+'{:.3e}'.format(RadBrightness)+', phot/(sec mrad2 0.1% BW)'+'\n'+\
        '-----------------------------------'+'\n'

        self.ui.analytic.setText(strw)

    def GetUndParams(self, dialog):
        self.up.numPer = float(dialog.ui.numper.text())
        self.up.undPer = float(dialog.ui.undper.text())
        self.up.Bx = float(dialog.ui.bx.text())
        self.up.By = float(dialog.ui.by.text())
        self.up.phBx = float(dialog.ui.phbx.text())
        self.up.phBy = float(dialog.ui.phby.text())
        self.up.sBx = float(dialog.ui.sbx.text())
        self.up.sBy = float(dialog.ui.sby.text())
        self.up.xcID = float(dialog.ui.xcid.text())
        self.up.ycID = float(dialog.ui.ycid.text())
        self.up.zcID = float(dialog.ui.zcid.text())
        self.up.n = int(dialog.ui.n.text())

    def ShowUndParams(self, dialog):
        dialog.ui.numper.setText(str(self.up.numPer))
        dialog.ui.undper.setText(str(self.up.undPer))
        dialog.ui.bx.setText(str(self.up.Bx))
        dialog.ui.by.setText(str(self.up.By))
        dialog.ui.phbx.setText(str(self.up.phBx))
        dialog.ui.phby.setText(str(self.up.phBy))
        dialog.ui.sbx.setText(str(self.up.sBx))
        dialog.ui.sby.setText(str(self.up.sBy))
        dialog.ui.xcid.setText(str(self.up.xcID))
        dialog.ui.ycid.setText(str(self.up.ycID))
        dialog.ui.zcid.setText(str(self.up.zcID))
        dialog.ui.n.setText(str(self.up.n))


    def GetBeamParams(self,dialog):
        #this is the self.beam class
        self.beam.Iavg = float(dialog.ui.iavg.text())
        self.beam.partStatMom1.x = float(dialog.ui.partstatmom1x.text())
        self.beam.partStatMom1.y = float(dialog.ui.partstatmom1y.text())
        self.beam.partStatMom1.z = float(dialog.ui.partstatmom1z.text())
        self.beam.partStatMom1.xp = float(dialog.ui.partstatmom1xp.text())
        self.beam.partStatMom1.yp = float(dialog.ui.partstatmom1yp.text())
        self.beam.partStatMom1.gamma = float(dialog.ui.partstatmom1gamma.text())
        '''
        sigEperE = 0.00089 #relative RMS energy spread
        sigX = 33.33e-06 #horizontal RMS size of e-beam [m]
        sigXp = 16.5e-06 #horizontal RMS angular divergence [rad]
        sigY = 2.912e-06 #vertical RMS size of e-beam [m]
        sigYp = 2.7472e-06 #vertical RMS angular divergence [rad]
        '''
        sigEperE = float(dialog.ui.sige.text())
        sigX = float(dialog.ui.sigx.text())
        sigXp = float(dialog.ui.sigxp.text())
        sigY = float(dialog.ui.sigy.text())
        sigYp = float(dialog.ui.sigyp.text())
        #2nd order stat. moments:
        self.beam.arStatMom2[0] = sigX*sigX #<(x-<x>)^2>
        self.beam.arStatMom2[1] = 0 #<(x-<x>)(x'-<x'>)>
        self.beam.arStatMom2[2] = sigXp*sigXp #<(x'-<x'>)^2>
        self.beam.arStatMom2[3] = sigY*sigY #<(y-<y>)^2>
        self.beam.arStatMom2[4] = 0 #<(y-<y>)(y'-<y'>)>
        self.beam.arStatMom2[5] = sigYp*sigYp #<(y'-<y'>)^2>
        self.beam.arStatMom2[10] = sigEperE*sigEperE #<(E-<E>)^2>/<E>^2

    def ShowBeamParams(self, dialog):
        dialog.ui.iavg.setText(str(self.beam.Iavg))
        dialog.ui.partstatmom1x.setText(str(self.beam.partStatMom1.x))
        dialog.ui.partstatmom1y.setText(str(self.beam.partStatMom1.y))
        dialog.ui.partstatmom1z.setText(str(self.beam.partStatMom1.z))
        dialog.ui.partstatmom1xp.setText(str(self.beam.partStatMom1.xp))
        dialog.ui.partstatmom1yp.setText(str(self.beam.partStatMom1.yp))
        dialog.ui.partstatmom1gamma.setText(str(self.beam.partStatMom1.gamma))
        dialog.ui.sige.setText(str(sqrt(self.beam.arStatMom2[10])))
        dialog.ui.sigx.setText(str(sqrt(self.beam.arStatMom2[0])))
        dialog.ui.sigy.setText(str(sqrt(self.beam.arStatMom2[3])))
        dialog.ui.sigxp.setText(str(sqrt(self.beam.arStatMom2[2])))
        dialog.ui.sigyp.setText(str(sqrt(self.beam.arStatMom2[5])))

    def WfrSetUpE(self,wfrE):
        #wfrE = SRWLWfr() this is the waveform class
        Nenergy = int(self.ui.tableWidget.item(0,0).text())#float?
        Nx = int(self.ui.tableWidget.item(1,0).text())
        Ny = int(self.ui.tableWidget.item(2,0).text())
        wfrE.allocate(Nenergy,Nx,Ny)
        wfrE.mesh.zStart = float(self.ui.tableWidget.item(3,0).text())
        wfrE.mesh.eStart = float(self.ui.tableWidget.item(4,0).text())
        wfrE.mesh.eFin = float(self.ui.tableWidget.item(5,0).text())
        wfrE.mesh.xStart = float(self.ui.tableWidget.item(6,0).text())
        wfrE.mesh.xFin = float(self.ui.tableWidget.item(8,0).text())
        wfrE.mesh.yStart = float(self.ui.tableWidget.item(7,0).text())
        wfrE.mesh.yFin = float(self.ui.tableWidget.item(9,0).text())

    def GetPrecision(self,dialog):
        self.precis.meth = dialog.ui.meth.currentIndex()
        self.precis.relPrec = float(dialog.ui.relprec.text())
        self.precis.zStartInteg = float(dialog.ui.zstartint.text())
        self.precis.zEndInteg = float(dialog.ui.zendint.text())
        self.precis.npTraj = float(dialog.ui.nptraj.text())
        self.precis.useTermin = dialog.ui.usetermin.currentIndex()
        self.precis.sampFactNxNyForProp = float(dialog.ui.sampfactnxny.text())

    def ShowPrecision(self,dialog):
        dialog.ui.meth.setCurrentIndex(self.precis.meth)
        dialog.ui.relprec.setText(str(self.precis.relPrec))
        dialog.ui.zstartint.setText(str(self.precis.zStartInteg))
        dialog.ui.zendint.setText(str(self.precis.zEndInteg))
        dialog.ui.nptraj.setText(str(self.precis.npTraj))
        dialog.ui.usetermin.setCurrentIndex(self.precis.useTermin)
        dialog.ui.sampfactnxny.setText(str(self.precis.sampFactNxNyForProp))


    def srwbuttonThin(self):
        if 'srwl' not in globals():
            msg = ' !Warning --'
            msg += 'SRW not installed on this system.'
            self.ui.status.setText(msg)
            raise Exception(msg)
        #UP = self.UndParams()
        und = SRWLMagFldU([SRWLMagFldH(1, 'v', self.up.By, self.up.phBy, self.up.sBy, 1), SRWLMagFldH(1, 'h', self.up.Bx, self.up.phBx, self.up.sBx, 1)], self.up.undPer, self.up.numPer)
        magFldCnt = SRWLMagFldC([und], array('d', [self.up.xcID]), array('d', [self.up.ycID]), array('d', [self.up.zcID]))
        #elecBeam = SRWLPartBeam()
        #self.BeamParams(elecBeam)

        self.AnalyticA()

        #precis = self.Precision()
        arPrecPar = [self.precis.meth, self.precis.relPrec, self.precis.zStartInteg, self.precis.zEndInteg, self.precis.npTraj, self.precis.useTermin, self.precis.sampFactNxNyForProp]

        wfrE = SRWLWfr()
        self.WfrSetUpE(wfrE)
        wfrE.partBeam = self.beam

        wfrXY = SRWLWfr()
        self.WfrSetUpE(wfrXY)
        wfrXY.partBeam = self.beam

        mesh=deepcopy(wfrE.mesh)
        wfrIn=deepcopy(wfrE)

        Polar = self.ui.polar.currentIndex()
        Intens = self.ui.intensity.currentIndex()
        DependArg = self.ui.deparg.currentIndex()
#       print (Polar, Intens, DependArg)

        if DependArg == 0:
            #after setting the text call self.ui.status.repaint() to have it immediately show otherwise it will wait till it exits the block to draw
            str1='* Performing Electric Field (spectrum vs photon energy) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrE, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n'
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrE.mesh.ne)
            srwl.CalcIntFromElecField(arI1, wfrE, Polar, Intens, DependArg, wfrE.mesh.eStart, 0, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            #time.sleep(1)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrE.mesh.eStart, wfrE.mesh.eFin, wfrE.mesh.ne],
            ['Photon energy, eV','Spectral intensity, ph/s/0.1%BW','Intensity vs photon energy'])

        elif DependArg == 1:
            str1='* Performing Electric Field (intensity vs x-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrXY.mesh.nx)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, 0, wfrXY.mesh.xStart, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrXY.mesh.xStart, wfrXY.mesh.xFin, wfrXY.mesh.nx],
            ['Horizontal Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs x-coordinate'])

        elif DependArg == 2:
            str1='* Performing Electric Field (intensity vs y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f',[0]*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, 0, wfrXY.mesh.yStart, 0)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(arI1, [wfrXY.mesh.yStart, wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Vertical Position [m]','Spectral intensity, ph/s/0.1%BW','Intensity vs y-coordinate'])

        elif DependArg == 3:
            str1='* Performing Electric Field (intensity vs x- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.nx*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.xStart, 1*wfrXY.mesh.xFin, wfrXY.mesh.nx],
            [1*wfrXY.mesh.yStart, 1*wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Horizontal Position [m]', 'Vertical Position [m]', 'Intensity at ' + str(wfrXY.mesh.eStart) + ' eV'])

        elif DependArg == 4:
            str1='* Performing Electric Field (intensity vs energy- and x-coordinate) calculation ... \n \n '
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.ne*wfrXY.mesh.nx)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.eStart, 1*wfrXY.mesh.eFin, wfrXY.mesh.ne],
            [1*wfrXY.mesh.xStart, 1*wfrXY.mesh.xFin, wfrXY.mesh.nx],
            ['Energy [eV]', 'Horizontal Position [m]', 'Intensity integrated from ' + str(wfrXY.mesh.yStart) + ' to ' + str(wfrXY.mesh.yFin) + ' ,m in y-coordinate'])

        elif DependArg == 5:
            str1='* Performing Electric Field (intensity vs energy- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcElecFieldSR(wfrXY, 0, magFldCnt, arPrecPar)
            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()
            arI1 = array('f', [0]*wfrXY.mesh.ne*wfrXY.mesh.ny)
            srwl.CalcIntFromElecField(arI1, wfrXY, Polar, Intens, DependArg, wfrXY.mesh.eStart, wfrXY.mesh.xStart, wfrXY.mesh.yStart)
            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot2d(arI1, [1*wfrXY.mesh.eStart, 1*wfrXY.mesh.eFin, wfrXY.mesh.ne],
            [1*wfrXY.mesh.yStart, 1*wfrXY.mesh.yFin, wfrXY.mesh.ny],
            ['Energy [eV]', 'Vertical Position [m]', 'Intensity integrated from ' + str(wfrXY.mesh.xStart) + ' to ' + str(wfrXY.mesh.xFin)+ ' ,m in x-coordinate'])
        else:
            print 'Error'

        uti_plot_show()

    def thin(self,i):
        thintable = [[10000,1,1,20,10,3000,0,0,0,0],
                     [1,100,3,20,395,395,-0.0025,0,0.0025,0],
                     [1,3,100,20,395,395,0,-0.0025,0,0.0025],
                     [1,100,100,20,395,395,-0.0025,-0.0025,0.0025,0.0025],
                     [1000,100,3,20,10,3000,-0.0025,-0.0025,0.0025,0.0025],
                     [1000,3,100,20,10,3000,-0.0025,-0.0025,0.0025,0.0025],
                     [1000,30,30,20,10,3000,0,0,0,0]]

        for n,x in enumerate(thintable[i]):
            self.ui.tableWidget.setItem(n,0,QtGui.QTableWidgetItem(str(x)))

    def makeund(self):
        dialog = DialogU()
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        dialog = DialogB()
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        dialog = DialogP()
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)
Exemplo n.º 3
0
class rbsrw(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_newsrw()
        self.ui.setupUi(self)
        self.up = UP()
        self.beam = SRWLPartBeam()
        #self.precis = Precis()
        self.arPrecF = [0]*5
        self.arPrecP = [0]*5
        self.thick(self.ui.deparg.currentIndex())

        #set srw initial values
        self.GetUndParams(DialogU())
        self.GetBeamParams(DialogB())
        self.GetPrecision(DialogP())

        #connections
        self.ui.undulator.clicked.connect(self.makeund)
        self.ui.beam.clicked.connect(self.makebeam)
        self.ui.precision.clicked.connect(self.setprec)
        self.ui.deparg.currentIndexChanged.connect(self.thick)
        self.ui.sim.clicked.connect(self.srwbuttonThick)
        self.ui.plot.clicked.connect(self.check)
        #indicators
        self.ui.status.setText('Initiated')
        self.ui.analytic.setText('No calculations performed...Yet')

    def AnalyticA(self):
        n_har=1 #harmB.n harmonic number from SRWTestCases(2)\UndC
        (Kx,Ky,lam_rn,e_phn,w_or_id)=IDWaveLengthPhotonEnergy(n_har,self.up.undPer,self.up.Bx,self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (UP.Kx=0.934*UP.By*UP.undPer, UP.K, RAD.ephn, UP.WorU)=
        #1. derived Kx from UP.By and UP.undPer
        #2. Introduced new class RAD.ephn = radiation class, variable phot energy of nth harmonics
        #3. UP.WorU=flag indicating wiggler or undulator situation
        #Inputs: (harmB.n, UP.undPer, UP.Bx, UP.By, self.beam.partStatMom1.gamma)
        stri='# K vertical:'+'{:.3f}'.format(Kx)+'\n'+\
        '# K horizontal:'+'{:.3f}'.format(Ky)+'\n'+\
        '# Wavelength, m         Photon energy, eV'+'\n'+\
        '1st harmonic '+'{:.3e}'.format(lam_rn)+' '+'{:.3e}'.format(e_phn)+'\n'+\
        '3rd harmonic '+'{:.3e}'.format(lam_rn/3.0)+' '+'{:.3e}'.format(e_phn*3.0)+'\n'+\
        '5th harmonic '+'{:.3e}'.format(lam_rn/5.0)+' '+'{:.3e}'.format(e_phn*5.0)+'\n'

        (E_c,w_or_id)=CriticalEnergyWiggler(self.up.Bx,self.beam.partStatMom1.gamma,Kx)
        #Outputs: (RAD.Ecrit,UPWorU) where RAD.Ecrit is critical energy of Wiggler Radiation
        #Inputs: (UP.Bx, self.beam.partStatMom1.gamma,UP.Kx)
        stra=stri+'# If wiggler: critical energy:'+'{:.3e}'.format(E_c)+', eV'+'\n'

        (P_W, L_id)=RadiatedPowerPlanarWiggler(self.up.undPer,self.up.Bx,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowW,UP.L) where RAD.PowW is radiated power of Wiggler Radiation, UP.L=length of ID
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        strb=stra+'# Length of ID:'+'{:.3e}'.format(L_id)+', m'+'\n' + \
        '# Radiated power:'+'{:.3e}'.format(P_W)+', W'+'\n'

        P_Wdc=CentralPowerDensityPlanarWiggler(self.up.Bx,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowCPD) where RAD.PowCPD is radiated central cone power density of Wiggler Radiation
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        strc=strb+'# Central Power Density: '+'{:.3e}'.format(P_Wdc)+', W/mrad2'+'\n'

        self.ui.analytic.setText(strc)

    def UndParamsThick(self):
        #vertical harmonic magnetic field
        harmB = SRWLMagFldH() #magnetic field harmonic
        harmB.n = self.up.n #harmonic number
        if self.up.By is None:
            harmB.B = self.up.Bx #magnetic field amplitude [T]
            harmB.h_or_v = 'h'   #magnetic field plane: horzontal ('h')
        else:
            harmB.B = self.up.By #magnetic field amplitude[T]
            harmB.h_or_v = 'v'   #magnetic field plane: vertical ('v')

        und = SRWLMagFldU([harmB])
        und.per = self.up.undPer #period length [m]
        und.nPer = self.up.numPer #number of periods (will be rounded to integer)
        magFldCnt = SRWLMagFldC([und], array('d', [0]), array('d', [0]), array('d', [0])) #Container of all magnetic field elements
        return (und, magFldCnt)

    def GetUndParams(self, dialog):
        self.up.numPer = float(dialog.ui.numper.text())
        self.up.undPer = float(dialog.ui.undper.text())
        self.up.n = int(dialog.ui.n.text())

        if dialog.ui.vh.isChecked():
            self.up.By = float(dialog.ui.b.text())
            self.up.Bx = None
        else:
            self.up.Bx = float(dialog.ui.b.text())
            self.up.By = None


    def ShowUndParams(self, dialog):
        dialog.ui.numper.setText(str(self.up.numPer))
        dialog.ui.undper.setText(str(self.up.undPer))
        if self.up.By is None:
            dialog.ui.b.setText(str(self.up.Bx))
            dialog.ui.vh.setChecked(False)
        else:
            dialog.ui.b.setText(str(self.up.By))
            dialog.ui.vh.setChecked(True)
        dialog.ui.n.setText(str(self.up.n))

    def GetBeamParams(self,dialog):
        #this is the beam class
        self.beam.Iavg = float(dialog.ui.iavg.text())
        self.beam.partStatMom1.x = float(dialog.ui.partstatmom1x.text())
        self.beam.partStatMom1.y = float(dialog.ui.partstatmom1y.text())
        self.beam.partStatMom1.z = float(dialog.ui.partstatmom1z.text())
        self.beam.partStatMom1.xp = float(dialog.ui.partstatmom1xp.text())
        self.beam.partStatMom1.yp = float(dialog.ui.partstatmom1yp.text())
        self.beam.partStatMom1.gamma = float(dialog.ui.partstatmom1gamma.text())
        '''
        sigEperE = 0.00089 #relative RMS energy spread
        sigX = 33.33e-06 #horizontal RMS size of e-beam [m]
        sigXp = 16.5e-06 #horizontal RMS angular divergence [rad]
        sigY = 2.912e-06 #vertical RMS size of e-beam [m]
        sigYp = 2.7472e-06 #vertical RMS angular divergence [rad]
        '''
        sigEperE = float(dialog.ui.sige.text())
        sigX = float(dialog.ui.sigx.text())
        sigXp = float(dialog.ui.sigxp.text())
        sigY = float(dialog.ui.sigy.text())
        sigYp = float(dialog.ui.sigyp.text())
        #2nd order stat. moments:
        self.beam.arStatMom2[0] = sigX*sigX #<(x-<x>)^2>
        self.beam.arStatMom2[1] = 0 #<(x-<x>)(x'-<x'>)>
        self.beam.arStatMom2[2] = sigXp*sigXp #<(x'-<x'>)^2>
        self.beam.arStatMom2[3] = sigY*sigY #<(y-<y>)^2>
        self.beam.arStatMom2[4] = 0 #<(y-<y>)(y'-<y'>)>
        self.beam.arStatMom2[5] = sigYp*sigYp #<(y'-<y'>)^2>
        self.beam.arStatMom2[10] = sigEperE*sigEperE #<(E-<E>)^2>/<E>^2

    def ShowBeamParams(self, dialog):
        dialog.ui.iavg.setText(str(self.beam.Iavg))
        dialog.ui.partstatmom1x.setText(str(self.beam.partStatMom1.x))
        dialog.ui.partstatmom1y.setText(str(self.beam.partStatMom1.y))
        dialog.ui.partstatmom1z.setText(str(self.beam.partStatMom1.z))
        dialog.ui.partstatmom1xp.setText(str(self.beam.partStatMom1.xp))
        dialog.ui.partstatmom1yp.setText(str(self.beam.partStatMom1.yp))
        dialog.ui.partstatmom1gamma.setText(str(self.beam.partStatMom1.gamma))
        dialog.ui.sige.setText(str(sqrt(self.beam.arStatMom2[10])))
        dialog.ui.sigx.setText(str(sqrt(self.beam.arStatMom2[0])))
        dialog.ui.sigy.setText(str(sqrt(self.beam.arStatMom2[3])))
        dialog.ui.sigxp.setText(str(sqrt(self.beam.arStatMom2[2])))
        dialog.ui.sigyp.setText(str(sqrt(self.beam.arStatMom2[5])))

    def WfrSetUpE(self,wfrE):
        #wfrE = SRWLWfr() this is the waveform class
        Nenergy = int(self.ui.tableWidget.item(0,0).text())#float?
        Nx = int(self.ui.tableWidget.item(1,0).text())
        Ny = int(self.ui.tableWidget.item(2,0).text())
        wfrE.allocate(Nenergy,Nx,Ny)
        wfrE.mesh.zStart = float(self.ui.tableWidget.item(3,0).text())
        wfrE.mesh.eStart = float(self.ui.tableWidget.item(4,0).text())
        wfrE.mesh.eFin = float(self.ui.tableWidget.item(5,0).text())
        wfrE.mesh.xStart = float(self.ui.tableWidget.item(6,0).text())
        wfrE.mesh.xFin = float(self.ui.tableWidget.item(8,0).text())
        wfrE.mesh.yStart = float(self.ui.tableWidget.item(7,0).text())
        wfrE.mesh.yFin = float(self.ui.tableWidget.item(9,0).text())

    def GetPrecision(self,dialog):
        #for spectral flux vs photon energy
        self.arPrecF[0] = float(dialog.ui.harma.text()) #initial UR harmonic to take into account
        self.arPrecF[1] = float(dialog.ui.harmb.text()) #final UR harmonic to take into account
        self.arPrecF[2] = float(dialog.ui.lip.text()) #longitudinal integration precision parameter
        self.arPrecF[3] = float(dialog.ui.aip.text()) #azimuthal integration precision parameter
        self.arPrecF[4] = dialog.ui.flux.currentIndex()+1 #calculate flux (1) or flux per unit surface (2)

        #for power density
        self.arPrecP[0] = float(dialog.ui.prefact.text()) #precision factor
        self.arPrecP[1] = dialog.ui.field.currentIndex()+1 #power density computation method (1- "near field", 2- "far field")
        self.arPrecP[2] = float(dialog.ui.ilp.text()) #initial longitudinal position (effective if self.arPrecP[2] < self.arPrecP[3])
        self.arPrecP[3] = float(dialog.ui.flp.text()) #final longitudinal position (effective if self.arPrecP[2] < self.arPrecP[3])
        self.arPrecP[4] = int(dialog.ui.np.text()) #number of points for (intermediate) trajectory calculation
        #return (self.arPrecF, self.arPrecP)

    def ShowPrecision(self,dialog):
        dialog.ui.harma.setText(str(self.arPrecF[0]))
        dialog.ui.harmb.setText(str(self.arPrecF[1]))
        dialog.ui.lip.setText(str(self.arPrecF[2]))
        dialog.ui.aip.setText(str(self.arPrecF[3]))
        dialog.ui.flux.setCurrentIndex(self.arPrecF[4]-1)

        dialog.ui.prefact.setText(str(self.arPrecP[0]))
        dialog.ui.field.setCurrentIndex(self.arPrecP[1]-1)
        dialog.ui.ilp.setText(str(self.arPrecP[2]))
        dialog.ui.flp.setText(str(self.arPrecP[3]))
        dialog.ui.np.setText(str(self.arPrecP[4]))

    def makeund(self):
        dialog = DialogU()
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        dialog = DialogB()
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        dialog = DialogP()
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)

    def srwbuttonThick(self):
        if 'srwl' not in globals():
            msg = ' !Warning --'
            msg += 'SRW not installed on this system.'
            self.ui.status.setText(msg)
            raise Exception(msg)

        (und,magFldCnt)=self.UndParamsThick()

        #self.beam = SRWLPartBeam()
        #self.BeamParams(self.beam)

        #self.AnalyticA(self.beam)

        #(self.arPrecF, self.arPrecP)=self.PrecisionThick()

        stkF = SRWLStokes() #for spectrum
        self.WfrSetUpE(stkF)
        stkP = SRWLStokes() #for power density
        self.WfrSetUpE(stkP)

        Polar = self.ui.polar.currentIndex()
        Intens = self.ui.intensity.currentIndex()
        DependArg = self.ui.deparg.currentIndex()
        print (Polar, Intens, DependArg)

        if DependArg == 0:
            #after setting the text call self.ui.status.repaint() to have it immediately show otherwise it will wait till it exits the block to draw
            str1='* Performing Electric Field (spectrum vs photon energy) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcStokesUR(stkF, self.beam, und, self.arPrecF) #####

            str2='* Extracting Intensity from calculated Electric Field ... \n \n'
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(stkF.arS, [stkF.mesh.eStart, stkF.mesh.eFin, stkF.mesh.ne], ['Photon Energy [eV]', 'Flux [ph/s/.1%bw]', 'Flux through Finite Aperture'])

        elif DependArg == 1:
            str1='* Performing Power Density calculation (from field) vs x-coordinate calculation ... \n \n'
            self.ui.status.setText(str1)
            print DependArg
            print(stkP)
            print(self.beam)
            print(und)
            print(self.arPrecP)
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)
            print 'yes'

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshX = [1000*stkP.mesh.xStart, 1000*stkP.mesh.xFin, stkP.mesh.nx]
            powDenVsX = array('f', [0]*stkP.mesh.nx)
            for i in range(stkP.mesh.nx): powDenVsX[i] = stkP.arS[stkP.mesh.nx*int(stkP.mesh.ny*0.5) + i]
            print plotMeshX
            uti_plot1d(powDenVsX, plotMeshX, ['Horizontal Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(horizontal cut at y = 0)'])


        elif DependArg == 2:
            str1='* Performing Power Density calculation (from field) vs x-coordinate calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            print DependArg
            print(stkP)
            print(self.beam)
            print(und)
            print(self.arPrecP)
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)
            print 'yes'

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshY = [1000*stkP.mesh.yStart, 1000*stkP.mesh.yFin, stkP.mesh.ny]
            powDenVsY = array('f', [0]*stkP.mesh.ny)
            for i in range(stkP.mesh.ny): powDenVsY[i] = stkP.arS[int(stkP.mesh.nx*0.5) + i*stkP.mesh.ny]
            uti_plot1d(powDenVsY, plotMeshY, ['Vertical Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(vertical cut at x = 0)'])

        elif DependArg == 3:
            str1='* Performing Electric Field (intensity vs x- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshX = [1000*stkP.mesh.xStart, 1000*stkP.mesh.xFin, stkP.mesh.nx]
            plotMeshY = [1000*stkP.mesh.yStart, 1000*stkP.mesh.yFin, stkP.mesh.ny]
            uti_plot2d(stkP.arS, plotMeshX, plotMeshY, ['Horizontal Position [mm]', 'Vertical Position [mm]', 'Power Density'])

        elif DependArg == 4:
            str1='* Performing Electric Field (intensity vs energy- and x-coordinate) calculation ... \n \n '
            self.ui.status.setText(str1)
            self.ui.status.repaint()

            str2='* Un der construction ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()

        elif DependArg == 5:
            str1='* Performing Electric Field (intensity vs energy- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()

            str2='* Un der construction ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()

        else:
            print 'Error'

        uti_plot_show()


    def thick(self,i):
        thicktable = [[10000,1,1,20,10,3000,-0.002,-0.002,0.002,0.002],
                     [1,100,3,20,685,685,-0.002,-0.002,0.002,0.002],
                     [1,3,100,20,685,685,-0.002,-0.002,0.002,0.002],
                     [1,100,100,20,685,685,-0.002,-0.002,0.002,0.002],
                     [1000,100,3,20,10,3000,-0.002,-0.002,0.002,0.002],
                     [1000,3,100,20,10,3000,-0.002,-0.002,0.002,0.002],
                     [1000,30,30,20,10,3000,-0.002,-0.002,0.002,0.002]]

        for n,x in enumerate(thicktable[i]):
            self.ui.tableWidget.setItem(n,0,QtGui.QTableWidgetItem(str(x)))

    def makeund(self):
        dialog = DialogU()
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        dialog = DialogB()
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        dialog = DialogP()
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)

    def check(self):
        print self.self.arPrecF
Exemplo n.º 4
0
class rbsrw(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_newsrw()
        self.ui.setupUi(self)
        self.up = UP()
        self.beam = SRWLPartBeam()
        #self.precis = Precis()
        self.arPrecF = [0]*5
        self.arPrecP = [0]*5
        #load initial values from excel
        self.workbook = open_workbook(resource.filename('SRWinitialvalues.xls'))
        self.thick(self.ui.deparg.currentIndex())
        #disable/remove broken simulation argument
        self.ui.deparg.removeItem(6)
        self.ui.deparg.removeItem(5)
        self.ui.deparg.removeItem(4)
        self.ui.intensity.hide()
        self.ui.formLayout_4.labelForField(self.ui.intensity).hide()
        self.ui.polar.hide()
        self.ui.formLayout_4.labelForField(self.ui.polar).hide()

        #set srw initial values
        column = self.workbook.sheet_by_name('thick undulator').col(0)
        units = self.workbook.sheet_by_name('thick undulator').col(1)
        units = self.unitstr(units)
        self.GetUndParams(DialogU(self,units,column))
        column = self.workbook.sheet_by_name('thick beam').col(0)
        units = self.workbook.sheet_by_name('thick beam').col(1)
        units = self.unitstr(units)
        self.GetBeamParams(DialogB(self,units,column))
        column = self.workbook.sheet_by_name('thick precision').col(0)
        units = self.workbook.sheet_by_name('thick precision').col(1)
        print column
        units = self.unitstr(units)
        self.GetPrecision(DialogP(self,units,column))

        #connections
        self.ui.undulator.clicked.connect(self.makeund)
        self.ui.beam.clicked.connect(self.makebeam)
        self.ui.precision.clicked.connect(self.setprec)
        self.ui.deparg.currentIndexChanged.connect(self.thick)
        self.ui.sim.clicked.connect(self.srwbuttonThick)
        self.ui.analyze.clicked.connect(self.AnalyticA)
        #indicators
        self.ui.status.setText('Initiated')
        self.ui.analytic.setText('No calculations performed...As of Yet')

    def AnalyticA(self):
#        (Kx,Ky,lam_rn,e_phn)=IDWaveLengthPhotonEnergy(self.up.undPer,self.up.Bx,self.up.By,self.beam.partStatMom1.gamma)
        (Kx,Ky,lam_rn,e_phn)=IDWaveLengthPhotonEnergy(self.up.undPer,0,self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (UP.Kx=0.934*UP.By*UP.undPer, UP.K, RAD.ephn, UP.WorU)=
        #1. derived Kx from UP.By and UP.undPer
        #2. Introduced new class RAD.ephn = radiation class, variable phot energy of nth harmonics
        #3. UP.WorU=flag indicating wiggler or undulator situation
        #Inputs: (harmB.n, UP.undPer, UP.Bx, UP.By, self.beam.partStatMom1.gamma)
        stri='# Ky:'+'{:.3f}'.format(Kx)+\
        ' # Kx:'+'{:.3f}'.format(Ky)+'\n'+\
        '# Wavelength, m           Phot. energy, eV'+'\n'+\
        '1st harmonic '+'{:.3e}'.format(lam_rn)+' '+'{:.3f}'.format(e_phn)+'\n'+\
        '3rd harmonic '+'{:.3e}'.format(lam_rn/3.0)+' '+'{:.3f}'.format(e_phn*3.0)+'\n'+\
        '5th harmonic '+'{:.3e}'.format(lam_rn/5.0)+' '+'{:.3f}'.format(e_phn*5.0)+'\n'

        E_c=CriticalEnergyWiggler(self.up.By,self.beam.partStatMom1.gamma)
        #Outputs: (RAD.Ecrit,UPWorU) where RAD.Ecrit is critical energy of Wiggler Radiation
        #Inputs: (UP.Bx, self.beam.partStatMom1.gamma,UP.Kx)
        stra=stri+'# Critical energy:'+'{:.3e}'.format(E_c)+', eV'+'\n'+\
        '-----------------------------------'+'\n'

        (P_W, L_id)=RadiatedPowerPlanarWiggler(self.up.undPer,self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowW,UP.L) where RAD.PowW is radiated power of Wiggler Radiation, UP.L=length of ID
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        #RadiatedPowerPlanarWiggler(lam_u,Bx,N_u,Gam,I_b):

        (RadSpotSize,RadSpotDivergence)=UndulatorSourceSizeDivergence(lam_rn,L_id)
        stre=stra+'# Rad spot size: '+'{:.3e}'.format(RadSpotSize)+', m'+'\n'
        strf=stre+'# Rad divergence: '+'{:.3e}'.format(RadSpotDivergence)+', rad'+'\n'+\
        '-----------------------------------'+'\n'

        strb=strf+'# Length of ID:'+'{:.3f}'.format(L_id)+', m'+'\n' + \
        '# Radiated power:'+'{:.3e}'.format(P_W)+', W'+'\n'

        P_Wdc=CentralPowerDensityPlanarWiggler(self.up.By,self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        #Outputs: (RAD.PowCPD) where RAD.PowCPD is radiated central cone power density of Wiggler Radiation
        #Inputs: (UP.undPer,UP.Bx,UP.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg) standart SRW class variables
        strc=strb+'# Central Power Density: '+'{:.3e}'.format(P_Wdc)+', W/mrad2'+'\n'

        SpectralFluxValue=SpectralFLux(self.up.numPer,self.beam.partStatMom1.gamma,1,self.beam.Iavg,Kx)
        strd=strc+'# Spectral flux: '+'{:.3e}'.format(SpectralFluxValue)+', phot/(sec mrad 0.1% BW)'+'\n'

        RadBrightness=SpectralCenBrightness(self.up.numPer,self.beam.partStatMom1.gamma,self.beam.Iavg)
        strw=strd+'# Spectral Central Brightness: '+'{:.3e}'.format(RadBrightness)+', phot/(sec mrad2 0.1% BW)'+'\n'+\
        '-----------------------------------'+'\n'

        self.ui.analytic.setText(strw)

    def UndParamsThick(self):
        #vertical harmonic magnetic field
        harmB = SRWLMagFldH() #magnetic field harmonic
        harmB.n = self.up.n #harmonic number
        if self.up.By is None:
            harmB.B = self.up.Bx #magnetic field amplitude [T]
            harmB.h_or_v = 'h'   #magnetic field plane: horzontal ('h')
        else:
            harmB.B = self.up.By #magnetic field amplitude[T]
            harmB.h_or_v = 'v'   #magnetic field plane: vertical ('v')

        und = SRWLMagFldU([harmB])
        und.per = self.up.undPer #period length [m]
        und.nPer = self.up.numPer #number of periods (will be rounded to integer)
        magFldCnt = SRWLMagFldC([und], array('d', [0]), array('d', [0]), array('d', [0])) #Container of all magnetic field elements
        magFldCnt.arZc[0] = zcID
        
        part.z = zcID - 0.5*magFldCnt.MagFld[0].rz
        partTraj = SRWLPrtTrj()
        partTraj.partInitCond = part
        partTraj.allocate(npTraj, True)
        partTraj.ctStart = 0 
        partTraj.ctEnd = magFldCnt.MagFld[0].rz
        print('   Performing calculation ... ')
        partTraj = srwl.CalcPartTraj(partTraj, magFldCnt, arPrecPar)
        print('done')
        return (und, magFldCnt)

    def GetUndParams(self, dialog):
        units = dialog.u
        self.up.numPer = convertUnitsStringToNumber(dialog.ui.numper.text(),units[0])
        self.up.undPer = convertUnitsStringToNumber(dialog.ui.undper.text(),units[1])
        self.up.n = int(float(dialog.ui.n.text()))

        if dialog.ui.vh.isChecked():
            self.up.By = convertUnitsStringToNumber(dialog.ui.b.text(),units[2])
            self.up.Bx = 0
        else:
            self.up.Bx = convertUnitsStringToNumber(dialog.ui.b.text(),units[2])
            self.up.By = 0


    def ShowUndParams(self, dialog):
        units = dialog.u
        dialog.ui.numper.setText(displayWithUnitsNumber(self.up.numPer,units[0]))
        dialog.ui.undper.setText(displayWithUnitsNumber(self.up.undPer,units[1]))
        if self.up.By is 0:
            dialog.ui.b.setText(displayWithUnitsNumber(self.up.Bx,units[2]))
            dialog.ui.vh.setChecked(False)
        else:
            dialog.ui.b.setText(displayWithUnitsNumber(self.up.By,units[2]))
            dialog.ui.vh.setChecked(True)
        dialog.ui.n.setText(str(self.up.n))

    def GetBeamParams(self,dialog):
        units = dialog.u
        #this is the beam class
        self.beam.Iavg = convertUnitsStringToNumber(dialog.ui.iavg.text(),units[0])
        self.beam.partStatMom1.x = convertUnitsStringToNumber(dialog.ui.partstatmom1x.text(),units[1])
        self.beam.partStatMom1.y = convertUnitsStringToNumber(dialog.ui.partstatmom1y.text(),units[2])
        self.beam.partStatMom1.z = convertUnitsStringToNumber(dialog.ui.partstatmom1z.text(),units[3])
        self.beam.partStatMom1.xp = convertUnitsStringToNumber(dialog.ui.partstatmom1xp.text(),units[4])
        self.beam.partStatMom1.yp = convertUnitsStringToNumber(dialog.ui.partstatmom1yp.text(),units[5])
        self.beam.partStatMom1.gamma = convertUnitsStringToNumber(dialog.ui.partstatmom1gamma.text(),units[6])
        '''
        sigEperE = 0.00089 #relative RMS energy spread
        sigX = 33.33e-06 #horizontal RMS size of e-beam [m]
        sigXp = 16.5e-06 #horizontal RMS angular divergence [rad]
        sigY = 2.912e-06 #vertical RMS size of e-beam [m]
        sigYp = 2.7472e-06 #vertical RMS angular divergence [rad]
        '''
        sigEperE = convertUnitsStringToNumber(dialog.ui.sige.text(),units[7])
        sigX = convertUnitsStringToNumber(dialog.ui.sigx.text(),units[8])
        sigXp = convertUnitsStringToNumber(dialog.ui.sigxp.text(),units[9])
        sigY = convertUnitsStringToNumber(dialog.ui.sigy.text(),units[10])
        sigYp = convertUnitsStringToNumber(dialog.ui.sigyp.text(),units[11])
        #2nd order stat. moments:
        self.beam.arStatMom2[0] = sigX*sigX #<(x-<x>)^2>
        self.beam.arStatMom2[1] = 0 #<(x-<x>)(x'-<x'>)>
        self.beam.arStatMom2[2] = sigXp*sigXp #<(x'-<x'>)^2>
        self.beam.arStatMom2[3] = sigY*sigY #<(y-<y>)^2>
        self.beam.arStatMom2[4] = 0 #<(y-<y>)(y'-<y'>)>
        self.beam.arStatMom2[5] = sigYp*sigYp #<(y'-<y'>)^2>
        self.beam.arStatMom2[10] = sigEperE*sigEperE #<(E-<E>)^2>/<E>^2

    def ShowBeamParams(self, dialog):
        units = dialog.u
        dialog.ui.iavg.setText(displayWithUnitsNumber(self.beam.Iavg,units[0]))
        dialog.ui.partstatmom1x.setText(displayWithUnitsNumber(self.beam.partStatMom1.x,units[1]))
        dialog.ui.partstatmom1y.setText(displayWithUnitsNumber(self.beam.partStatMom1.y,units[2]))
        dialog.ui.partstatmom1z.setText(displayWithUnitsNumber(self.beam.partStatMom1.z,units[3]))
        dialog.ui.partstatmom1xp.setText(displayWithUnitsNumber(self.beam.partStatMom1.xp,units[4]))
        dialog.ui.partstatmom1yp.setText(displayWithUnitsNumber(self.beam.partStatMom1.yp,units[5]))
        dialog.ui.partstatmom1gamma.setText(displayWithUnitsNumber(self.beam.partStatMom1.gamma,units[6]))
        dialog.ui.sige.setText(displayWithUnitsNumber(sqrt(self.beam.arStatMom2[10]),units[7]))
        dialog.ui.sigx.setText(displayWithUnitsNumber(sqrt(self.beam.arStatMom2[0]),units[8]))
        dialog.ui.sigy.setText(displayWithUnitsNumber(sqrt(self.beam.arStatMom2[3]),units[9]))
        dialog.ui.sigxp.setText(displayWithUnitsNumber(sqrt(self.beam.arStatMom2[2]),units[10]))
        dialog.ui.sigyp.setText(displayWithUnitsNumber(sqrt(self.beam.arStatMom2[5]),units[11]))

    def WfrSetUpE(self,wfrE):
        #wfrE = SRWLWfr() this is the waveform class
#        Nenergy = float(self.ui.tableWidget.item(0,0).text())#float?
        Nenergy = int(convertUnitsStringToNumber(self.ui.tableWidget.item(0,0).text(),''))
#        Nx = int(self.ui.tableWidget.item(1,0).text())
        Nx = int(convertUnitsStringToNumber(self.ui.tableWidget.item(1,0).text(),''))
#        Ny = int(self.ui.tableWidget.item(2,0).text())
        Ny = int(convertUnitsStringToNumber(self.ui.tableWidget.item(2,0).text(),''))
        wfrE.allocate(Nenergy,Nx,Ny)
        wfrE.mesh.zStart = float(self.ui.tableWidget.item(3,0).text())
        wfrE.mesh.eStart = float(self.ui.tableWidget.item(4,0).text())
        wfrE.mesh.eFin = float(self.ui.tableWidget.item(5,0).text())
        wfrE.mesh.xStart = float(self.ui.tableWidget.item(6,0).text())
        wfrE.mesh.xFin = float(self.ui.tableWidget.item(8,0).text())
        wfrE.mesh.yStart = float(self.ui.tableWidget.item(7,0).text())
        wfrE.mesh.yFin = float(self.ui.tableWidget.item(9,0).text())

    def GetPrecision(self,dialog):
        units = dialog.u
        #for spectral flux vs photon energy
        self.arPrecF[0] = float(dialog.ui.harma.text()) #initial UR harmonic to take into account
        self.arPrecF[1] = float(dialog.ui.harmb.text()) #final UR harmonic to take into account
        self.arPrecF[2] = float(dialog.ui.lip.text()) #longitudinal integration precision parameter
        self.arPrecF[3] = float(dialog.ui.aip.text()) #azimuthal integration precision parameter
        self.arPrecF[4] = dialog.ui.flux.currentIndex()+1 #calculate flux (1) or flux per unit surface (2)

        #for power density
        self.arPrecP[0] = float(dialog.ui.prefact.text()) #precision factor
        self.arPrecP[1] = dialog.ui.field.currentIndex()+1 #power density computation method (1- "near field", 2- "far field")
        self.arPrecP[2] = float(dialog.ui.ilp.text()) #initial longitudinal position (effective if self.arPrecP[2] < self.arPrecP[3])
        self.arPrecP[3] = float(dialog.ui.flp.text()) #final longitudinal position (effective if self.arPrecP[2] < self.arPrecP[3])
        self.arPrecP[4] = int(float(dialog.ui.np.text())) #number of points for (intermediate) trajectory calculation
        #return (self.arPrecF, self.arPrecP)

    def ShowPrecision(self,dialog):
        dialog.ui.harma.setText(str(self.arPrecF[0]))
        dialog.ui.harmb.setText(str(self.arPrecF[1]))
        dialog.ui.lip.setText(str(self.arPrecF[2]))
        dialog.ui.aip.setText(str(self.arPrecF[3]))
        dialog.ui.flux.setCurrentIndex(self.arPrecF[4]-1)

        dialog.ui.prefact.setText(str(self.arPrecP[0]))
        dialog.ui.field.setCurrentIndex(self.arPrecP[1]-1)
        dialog.ui.ilp.setText(str(self.arPrecP[2]))
        dialog.ui.flp.setText(str(self.arPrecP[3]))
        dialog.ui.np.setText(str(self.arPrecP[4]))

    def unitstr(self,units):
        for n,u in enumerate(units):
            units[n]=str(u.value)

        return units

    def makeund(self):
        units = self.workbook.sheet_by_name('thick undulator').col(1)
        units = self.unitstr(units)
        dialog = DialogU(self,units)
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        units = self.workbook.sheet_by_name('thick undulator').col(1)
        units = self.unitstr(units)
        dialog = DialogB(self,units)
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        dialog = DialogP()
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)

    def srwbuttonThick(self):
        if 'srwl' not in globals():
            msg = ' !Warning --'
            msg += 'SRW not installed on this system.'
            self.ui.status.setText(msg)
            raise Exception(msg)

        (und,magFldCnt)=self.UndParamsThick()

        #self.beam = SRWLPartBeam()
        #self.BeamParams(self.beam)

        #self.AnalyticA(self.beam)
        #self.AnalyticA()

        #(self.arPrecF, self.arPrecP)=self.PrecisionThick()

        stkF = SRWLStokes() #for spectrum
        self.WfrSetUpE(stkF)
        stkP = SRWLStokes() #for power density
        self.WfrSetUpE(stkP)

        Polar = self.ui.polar.currentIndex()
        Intens = self.ui.intensity.currentIndex()
        DependArg = self.ui.deparg.currentIndex()
        print (Polar, Intens, DependArg)

        if DependArg == 0:
            #after setting the text call self.ui.status.repaint() to have it immediately show otherwise it will wait till it exits the block to draw
            str1='* Performing Electric Field (spectrum vs photon energy) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcStokesUR(stkF, self.beam, und, self.arPrecF) #####

            str2='* Extracting Intensity from calculated Electric Field ... \n \n'
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            uti_plot1d(stkF.arS, [stkF.mesh.eStart, stkF.mesh.eFin, stkF.mesh.ne], ['Photon Energy [eV]', 'Flux [ph/s/.1%bw]', 'Flux through Finite Aperture'])

        elif DependArg == 1:
            str1='* Performing Power Density calculation (from field) vs x-coordinate calculation ... \n \n'
            self.ui.status.setText(str1)
            print DependArg
            print(stkP)
            print(self.beam)
            print(und)
            print(self.arPrecP)
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)
            print 'yes'

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshX = [1000*stkP.mesh.xStart, 1000*stkP.mesh.xFin, stkP.mesh.nx]
            powDenVsX = array('f', [0]*stkP.mesh.nx)
            for i in range(stkP.mesh.nx): powDenVsX[i] = stkP.arS[stkP.mesh.nx*int(stkP.mesh.ny*0.5) + i]
            print plotMeshX
            uti_plot1d(powDenVsX, plotMeshX, ['Horizontal Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(horizontal cut at y = 0)'])


        elif DependArg == 2:
            str1='* Performing Power Density calculation (from field) vs x-coordinate calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
#            print DependArg
#            print(stkP)
#            print(self.beam)
#            print(und)
#            print(self.arPrecP)
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)
#            print 'yes'

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshY = [1000*stkP.mesh.yStart, 1000*stkP.mesh.yFin, stkP.mesh.ny]
            powDenVsY = array('f', [0]*stkP.mesh.ny)
#            for i in range(stkP.mesh.ny): powDenVsY[i] = stkP.arS[int(stkP.mesh.nx*0.5) + i*stkP.mesh.ny]
            for i in range(stkP.mesh.ny): powDenVsY[i] = stkP.arS[stkP.mesh.ny*int(stkP.mesh.nx*0.5) + i]
            uti_plot1d(powDenVsY, plotMeshY, ['Vertical Position [mm]', 'Power Density [W/mm^2]', 'Power Density\n(vertical cut at x = 0)'])

        elif DependArg == 3:
            str1='* Performing Electric Field (intensity vs x- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()
            srwl.CalcPowDenSR(stkP, self.beam, 0, magFldCnt, self.arPrecP)

            str2='* Extracting Intensity from calculated Electric Field ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()
            plotMeshX = [1000*stkP.mesh.xStart, 1000*stkP.mesh.xFin, stkP.mesh.nx]
            plotMeshY = [1000*stkP.mesh.yStart, 1000*stkP.mesh.yFin, stkP.mesh.ny]
            uti_plot2d(stkP.arS, plotMeshX, plotMeshY, ['Horizontal Position [mm]', 'Vertical Position [mm]', 'Power Density'])

        elif DependArg == 4:
            str1='* Performing Electric Field (intensity vs energy- and x-coordinate) calculation ... \n \n '
            self.ui.status.setText(str1)
            self.ui.status.repaint()

            str2='* Un der construction ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()

        elif DependArg == 5:
            str1='* Performing Electric Field (intensity vs energy- and y-coordinate) calculation ... \n \n'
            self.ui.status.setText(str1)
            self.ui.status.repaint()

            str2='* Un der construction ... \n \n '
            self.ui.status.setText(str1+str2)
            self.ui.status.repaint()

            str3='* Plotting the results ...\n'
            self.ui.status.setText(str1+str2+str3)
            self.ui.status.repaint()

        else:
            print 'Error'

        uti_plot_show()


    def thick(self,i):
        thicksheet = self.workbook.sheet_by_name('thick table')
        for n,c in enumerate(thicksheet.col(i)):
            self.ui.tableWidget.setItem(n,0,QtGui.QTableWidgetItem(str(c.value)))

    def unitstr(self,units):
        for n,u in enumerate(units):
            units[n]=str(u.value)

        return units

    def makeund(self):
        units = self.workbook.sheet_by_name('thick undulator').col(1)
        units = self.unitstr(units)
        dialog = DialogU(self,units)
        self.ShowUndParams(dialog)
        if dialog.exec_():
            self.GetUndParams(dialog)

    def makebeam(self):
        units = self.workbook.sheet_by_name('thick beam').col(1)
        units = self.unitstr(units)
        dialog = DialogB(self,units)
        self.ShowBeamParams(dialog)
        if dialog.exec_():
            self.GetBeamParams(dialog)

    def setprec(self):
        dialog = DialogP()
        self.ShowPrecision(dialog)
        if dialog.exec_():
            self.GetPrecision(dialog)

    def check(self):
        print self.arPrecF