Exemple #1
0
    def validate_momentum(self):
        """Ensure the momentum value is valid"""
        if self.bunch_source_manager.is_momentum_required():
            try:
                momentum = float(self.ui.momentumLineEdit.text())
            except ValueError:
                try:
                    momentum = convertUnitsStringToNumber(
                        self.ui.momentumLineEdit.text(), 'MeV')
                except ValueError:
                    self.show_warning_box('Unable to parse momentum')
                    self.ui.momentumLineEdit.setFocus()
                    return None
        else:
            bunchTab = self.bunch_source_manager.get_tab_widget()
            bunchTab.generateBunch() # make sure any new settings get read

            try:
                momentum = convertUnitsNumber(
                    bunchTab.myBunch.getDesignMomentumEV(), 'eV', 'MeV')
            except ValueError:
                self.show_warning_box(
                    'Invalid momentum value on Bunch Tab')
                return None
        return momentum
Exemple #2
0
    def plotZX(self):
        # instance of the plot utility class
        myPlotUtils = radtrack.plot.RbPlotUtils.RbPlotUtils()

        zArr = np.zeros(self.numZ)
        xArr = np.zeros(self.numX)

        for iLoop in range(self.numZ):
            zArr[iLoop] = self.minZ + iLoop * (self.maxZ - self.minZ) / (self.numZ - 1)
        #            print('zArr[', iLoop, '] = ', zArr[iLoop])

        for jLoop in range(self.numX):
            xArr[jLoop] = self.minX + jLoop * (self.maxX - self.minX) / (self.numX - 1)
        #            print('xArr[', jLoop, '] = ', xArr[jLoop])

        # specify y position for plot
        yValue = 0.0

        # Calculate Ex at the 2D array of x,y values
        zxEData = np.zeros((self.numX, self.numZ))
        for iLoop in range(self.numZ):
            for jLoop in range(self.numX):
                zxEData[jLoop, iLoop] = np.real(self.myPulse.evalEnvelopeEx(xArr[jLoop], yValue, zArr[iLoop]))
        #                print('zxEData[', iLoop, jLoop, '] = ', zxEData[iLoop, jLoop]

        # generate the xy plot
        canvas = self.ui.zxPlot.canvas
        canvas.ax.clear()

        levels = myPlotUtils.generateContourLevels(zxEData)
        canvas.ax.contourf(
            zArr * convertUnitsNumber(1, "m", self.unitsZ),
            xArr * convertUnitsNumber(1, "m", self.unitsXY),
            zxEData,
            levels,
            extent="none",
            aspect="equal",
        )

        canvas.ax.axis(
            [
                self.minZ * convertUnitsNumber(1, "m", self.unitsZ),
                self.maxZ * convertUnitsNumber(1, "m", self.unitsZ),
                self.minX * convertUnitsNumber(1, "m", self.unitsXY),
                self.maxX * convertUnitsNumber(1, "m", self.unitsXY),
            ]
        )
        canvas.ax.xaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.yaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.set_xlabel("z [" + self.unitsZ + "]")
        canvas.ax.set_ylabel("x [" + self.unitsXY + "]")
        if self.plotTitles == True:
            canvas.ax.set_title(
                "ZX slice, at  y={0:4.2f} [{1}]".format(yValue * convertUnitsNumber(1, "m", self.unitsXY), self.unitsXY)
            )
        canvas.fig.tight_layout()
        canvas.fig.set_facecolor("w")
        canvas.draw()
def populateTreeItem(addToBeamClickText, item, element):
    item.setText(0, element.name)
    item.setText(1, element.displayLine())
    item.setText(2, displayWithUnitsNumber(roundSigFig(element.getLength(), 4), 'm'))
    item.setText(3, displayWithUnitsNumber(roundSigFig(element.getDisplacement(), 4), 'm'))
    item.setText(4, str(roundSigFig(convertUnitsNumber(element.getAngle(), 'rad', 'deg'), 4))+' deg')
    item.setText(5, str(element.getNumberOfElements()) if element.isBeamline() else '')
    item.setText(6, addToBeamClickText)
    item.setForeground(6, rt_qt.QtCore.Qt.blue)
    font = item.font(6)
    font.setBold(True)
    font.setUnderline(True)
    item.setFont(6, font)
def test_unit_conversion():
    # Simple test
    a = '12 in'
    ac = convertUnitsString(a, 'km')
    b = '1 ft'
    bc = convertUnitsString(b, 'km')
    assert ac == bc

    # Compound test
    a = '60 mi/hr'
    ac = roundSigFig(convertUnitsStringToNumber(a, 'm/s'), 10)
    b = '88 ft/sec'
    bc = roundSigFig(convertUnitsStringToNumber(b, 'm/s'), 10)
    assert ac == bc

    # Invalid test
    a = '4 score'
    with pytest.raises(ValueError):
        convertUnitsString(a, 'years')

    # Higher dimension test
    a = 16.7 # km^2, 
    ac = convertUnitsNumberToString(a, 'km^2', 'mi^2')
    b = '6.44790604766 mi^2'
    bc = convertUnitsString(b, 'mi^2')
    assert ac == bc

    # Angle test
    a = 3 # radians
    ac = roundSigFig(convertUnitsNumber(a, 'rad', 'deg'), 10)
    b = 3*180/pi # 3 rad in degrees
    bc = roundSigFig(b, 10)
    assert ac == bc

    # Compound units
    a = "9.8 m/s^2"
    ac = roundSigFig(convertUnitsStringToNumber(a, "ft/ms^2"), 6)
    b = 3.21522e-5 # ft / (ms^2)
    assert ac == b

    # Inverse units
    a = "10 1/s"
    ac = convertUnitsString(a, 'Hz')
    b = "10.0 Hz"
    assert ac == b

    a = "1 1/ns"
    ac = convertUnitsString(a, 'GHz')
    b = "1.0 GHz"
    assert ac == b
Exemple #5
0
    def mirrorWithHole(self):
        # toggle the corresponding flag
        self.externalFields = True

        # get input from text boxes
        self.lambda0 = convertUnitsStringToNumber(self.ui.wavelength.text(), "m")
        self.w0 = convertUnitsStringToNumber(self.ui.waistSize.text(), "m")
        self.w0_z = convertUnitsStringToNumber(self.ui.waistPosition.text(), "m")
        self.zR = math.pi * self.w0 ** 2 / self.lambda0  # horiz. Rayleigh range [m]

        # load up the x,y locations of the mesh
        self.xMin = -4.0 * self.w0
        self.xMax = 4.0 * self.w0

        self.yMin = self.xMin
        self.yMax = self.xMax

        self.numPts = 64
        self.nCells = self.numPts ** 2

        self.xArr = np.zeros(self.numPts)
        for iLoop in range(self.numPts):
            self.xArr[iLoop] = self.xMin + iLoop * (self.xMax - self.xMin) / (self.numPts - 1)

        self.yArr = np.zeros(self.numPts)
        for jLoop in range(self.numPts):
            self.yArr[jLoop] = self.yMin + jLoop * (self.yMax - self.yMin) / (self.numPts - 1)

        self.xGrid = np.zeros((self.numPts, self.numPts))
        self.yGrid = np.zeros((self.numPts, self.numPts))

        for iLoop in range(self.numPts):
            for jLoop in range(self.numPts):
                self.xGrid[iLoop, jLoop] = self.xMin + iLoop * (self.xMax - self.xMin) / (self.numPts - 1)
                self.yGrid[iLoop, jLoop] = self.yMin + jLoop * (self.yMax - self.yMin) / (self.numPts - 1)

        # Create transverse field profile (#3 elliptical Gaussian donut)
        self.ExGridExternal = np.zeros((self.numPts, self.numPts))
        self.wx3 = 2.0 * self.w0
        self.rad1 = 1.0 * self.w0
        self.rad2 = 2.0 * self.w0
        for iLoop in range(self.numPts):
            for jLoop in range(self.numPts):
                xArg = self.xArr[iLoop]
                yArg = self.yArr[jLoop]
                rArg = math.sqrt(xArg ** 2 + yArg ** 2)
                rFactor = 1.0
                if rArg <= self.rad2:
                    rFactor = 0.5 + 0.5 * math.cos(math.pi * ((rArg - self.rad1) / (self.rad2 - self.rad1) - 1.0))
                if rArg <= self.rad1:
                    rFactor = 0.0
                self.ExGridExternal[iLoop, jLoop] = (
                    rFactor * math.exp(-(xArg / self.wx3) ** 2) * math.exp(-(yArg / self.wx3) ** 2)
                )

        # generate the xy plot
        canvas = self.ui.xyPlotExtFields.canvas
        canvas.ax.clear()

        myPlotUtils = radtrack.plot.RbPlotUtils.RbPlotUtils()
        levels = myPlotUtils.generateContourLevels(self.ExGridExternal)
        canvas.ax.contourf(
            self.xGrid * convertUnitsNumber(1, "m", self.unitsXY),
            self.yGrid * convertUnitsNumber(1, "m", self.unitsXY),
            self.ExGridExternal,
            levels,
            extent="none",
            aspect="equal",
        )
        canvas.ax.xaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.yaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.set_xlabel("x [" + self.unitsXY + "]")
        canvas.ax.set_ylabel("y [" + self.unitsXY + "]")
        canvas.ax.axis(
            [
                self.xMin * convertUnitsNumber(1, "m", self.unitsXY),
                self.xMax * convertUnitsNumber(1, "m", self.unitsXY),
                self.yMin * convertUnitsNumber(1, "m", self.unitsXY),
                self.yMax * convertUnitsNumber(1, "m", self.unitsXY),
            ]
        )
        if self.plotTitles == True:
            canvas.ax.set_title("slice: quadratic square; at z={0:4.2f} [{1}]".format(0.0, self.unitsZ))

        canvas.fig.tight_layout()
        canvas.fig.set_facecolor("w")
        canvas.draw()
Exemple #6
0
    def plotXY(self):
        # instance of the plot utility class
        myPlotUtils = radtrack.plot.RbPlotUtils.RbPlotUtils()

        # Specify the desired grid size
        self.xyNumH = 64
        self.xyNumV = 64
        self.xyNumCells = self.xyNumH * self.xyNumV

        # specify the min's and max's
        self.xyMinH = -4.0 * self.w0
        self.xyMaxH = 4.0 * self.w0

        self.xyMinV = -4.0 * self.w0
        self.xyMaxV = 4.0 * self.w0

        xArr = np.zeros(self.xyNumH)
        yArr = np.zeros(self.xyNumV)
        xTmp = np.zeros(self.xyNumV)

        for iLoop in range(self.xyNumH):
            xArr[iLoop] = self.xyMinH + iLoop * (self.xyMaxH - self.xyMinH) / (self.xyNumH - 1)

        for jLoop in range(self.xyNumV):
            yArr[jLoop] = self.xyMinV + jLoop * (self.xyMaxV - self.xyMinV) / (self.xyNumV - 1)

        # Calculate Ex at the 2D array of x,y values
        xyEData = np.zeros((self.xyNumV, self.xyNumH))  # interchange of V/H is weirdly necessary!?
        for iLoop in range(self.xyNumH):
            for jLoop in range(self.xyNumV):
                xTmp[jLoop] = xArr[iLoop]
            xyEData[0 : self.xyNumV, iLoop] = np.real(self.myPulse.evalEnvelopeEx(xTmp, yArr, self.w0_z))

        # generate the xy plot
        canvas = self.ui.xyPlot.canvas
        canvas.ax.clear()

        levels = myPlotUtils.generateContourLevels(xyEData)
        canvas.ax.contourf(
            xArr * convertUnitsNumber(1, "m", self.unitsXY),
            yArr * convertUnitsNumber(1, "m", self.unitsXY),
            xyEData,
            levels,
            extent="none",
            aspect="equal",
        )

        # For some reason, I can't get the color bar to appear...
        #        contours = canvas.ax.contourf(xArr, yArr, xyEData, levels, extent='none', aspect='equal')
        #        plt.gcf().colorbar(contours, format='%3.2e')

        canvas.ax.axis(
            [
                self.xyMinH * convertUnitsNumber(1, "m", self.unitsXY),
                self.xyMaxH * convertUnitsNumber(1, "m", self.unitsXY),
                self.xyMinV * convertUnitsNumber(1, "m", self.unitsXY),
                self.xyMaxV * convertUnitsNumber(1, "m", self.unitsXY),
            ]
        )
        canvas.ax.xaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.yaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.set_xlabel("x [" + self.unitsXY + "]")
        canvas.ax.set_ylabel("y [" + self.unitsXY + "]")
        if self.plotTitles == True:
            canvas.ax.set_title(
                "XY slice, at  z={0:4.2f} [{1}]".format(
                    self.w0_z * convertUnitsNumber(1, "m", self.unitsZ), self.unitsZ
                )
            )
        canvas.fig.tight_layout()
        canvas.fig.set_facecolor("w")
        canvas.draw()
Exemple #7
0
    def plotZY(self):
        # instance of the plot utility class
        myPlotUtils = radtrack.plot.RbPlotUtils.RbPlotUtils()

        freq0 = self.c / self.lambda0
        yLoc = 0.0

        # Specify the desired grid size
        self.zyNumH = 64
        self.zyNumV = 64
        self.zyNumCells = self.zyNumH * self.zyNumV

        # specify the min's and max's
        self.zyMinH = -4.0 * self.lambda0
        self.zyMaxH = 4 * self.lambda0

        self.zyMinV = -4.0 * self.w0
        self.zyMaxV = 4.0 * self.w0

        zArr = np.zeros(self.zyNumH)
        yArr = np.zeros(self.zyNumV)

        for iLoop in range(self.zyNumH):
            zArr[iLoop] = self.zyMinH + iLoop * (self.zyMaxH - self.zyMinH) / (self.zyNumH - 1)
        #            print('zArr[', iLoop, '] = ', zArr[iLoop])

        for jLoop in range(self.zyNumV):
            yArr[jLoop] = self.zyMinV + jLoop * (self.zyMaxV - self.zyMinV) / (self.zyNumV - 1)
        #            print('xArr[', jLoop, '] = ', xArr[jLoop])

        # Choose values of x,t for plot
        xValue = 0.0
        tValue = 0.0

        # Calculate Ex at the 2D array of x,y values
        zyEData = np.zeros((self.zyNumV, self.zyNumH))
        for iLoop in range(self.zyNumH):
            for jLoop in range(self.zyNumV):
                zyEData[jLoop, iLoop] = self.myPulse.evaluateEx(xValue, yArr[jLoop], zArr[iLoop], tValue)
        #                print('zyEData[', iLoop, jLoop, '] = ', zyEData[iLoop, jLoop])

        # generate the xy plot
        canvas = self.ui.zyPlot.canvas
        canvas.ax.clear()

        levels = myPlotUtils.generateContourLevels(zyEData)
        canvas.ax.contourf(
            zArr * convertUnitsNumber(1, "m", self.unitsZ),
            yArr * convertUnitsNumber(1, "m", self.unitsXY),
            zyEData,
            levels,
            extent="none",
            aspect="equal",
        )
        # plt.colorbar(cs1, format='%3.2e')
        # plt.gcf().colorbar(contours, format='%3.2e')

        canvas.ax.axis(
            [
                self.zyMinH * convertUnitsNumber(1, "m", self.unitsZ),
                self.zyMaxH * convertUnitsNumber(1, "m", self.unitsZ),
                self.zyMinV * convertUnitsNumber(1, "m", self.unitsXY),
                self.zyMaxV * convertUnitsNumber(1, "m", self.unitsXY),
            ]
        )
        canvas.ax.xaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.yaxis.set_major_locator(plt.MaxNLocator(self.numTicks))
        canvas.ax.set_xlabel("z [" + self.unitsZ + "]")
        canvas.ax.set_ylabel("y [" + self.unitsXY + "]")
        if self.plotTitles == True:
            canvas.ax.set_title(
                "ZY slice, at  x={0:4.2f} [{1}]".format(xValue * convertUnitsNumber(1, "m", self.unitsXY), self.unitsXY)
            )
        canvas.fig.tight_layout()
        canvas.fig.set_facecolor("w")
        canvas.draw()