Beispiel #1
0
def harm(obj, y, z, ro, np):
    arHarm = [complex(0, 0)] * np
    d_tet = 2 * pi / np
    tet = 0
    for i in range(np):
        cosTet = cos(tet)
        sinTet = sin(tet)
        re = rad.FldInt(obj, 'inf', 'ibz',
                        [-1, y + ro * cosTet, z + ro * sinTet],
                        [1, y + ro * cosTet, z + ro * sinTet])
        im = rad.FldInt(obj, 'inf', 'iby',
                        [-1, y + ro * cosTet, z + ro * sinTet],
                        [1, y + ro * cosTet, z + ro * sinTet])
        arHarm[i] = complex(re, im)
        tet += d_tet
    return [np, ro, arHarm]
Beispiel #2
0
def CalcField(g):

    #Vertical Magnetic Field vs Longitudinal Position
    yMin = 0.; yMax = 300.; ny = 301
    yStep = (yMax - yMin)/(ny - 1)
    xc = 0.; zc = 0.
    #y = yMin
    #Points = []
    #for i in range(ny):
    #    Points.append([xc,y,zc])
    #    y += yStep
    #BzVsY = rad.Fld(g, 'bz', Points)
    #More compact method to do the above: 
    BzVsY = rad.Fld(g, 'bz', [[xc,yMin+iy*yStep,zc] for iy in range(ny)])

    #Vertical Magnetic Field Integral (along Longitudinal Position) vs Horizontal Position
    xMin = 0.; xMax = 400.; nx = 201
    xStep = (xMax - xMin)/(nx - 1)
    zc = 0.
    #x = xMin
    #IBzVsX = []
    #for i in range(ny):
    #    IBzVsX.append(rad.FldInt(g, 'inf', 'ibz', [x,-300.,zc], [x,300.,zc]))
    #    x += xStep
    #More compact method to do the above: 
    IBzVsX = [rad.FldInt(g, 'inf', 'ibz', [xMin+ix*xStep,-300.,zc], [xMin+ix*xStep,300.,zc]) for ix in range(nx)]
    
    return BzVsY, [yMin, yMax, ny], IBzVsX, [xMin, xMax, nx]
Beispiel #3
0
    def build_model(self):
        """Build a Radia or Opera model with the current result set."""
        length = self.length_spinbox.value()
        if self.build_button.text() == 'Radia':
            rad.UtiDelAll()
            item = self.listview.selectedItems()[0]
            # build magnet geometry
            magnet = rad.ObjCnt([rad.ObjThckPgn(0, length, pg[2:].reshape((4, 2)).tolist(), "z", list(pg[:2]) + [0, ])
                                 for pg in self.state['results'][tuple(item.text().split(', '))]])
            rad.MatApl(magnet, rad.MatStd('NdFeB', next(c for c in self.controls if c.switch == 'Br').control.value()))

            # plot geometry in 3d
            ax = self.plot3d.axes
            ax.cla()
            ax.set_axis_off()
            polygons = rad.ObjDrwVTK(magnet)['polygons']
            vertices = np.array(polygons['vertices']).reshape((-1, 3))  # [x, y, z, x, y, z] -> [[x, y, z], [x, y, z]]
            [set_lim(vertices.min(), vertices.max()) for set_lim in (ax.set_xlim3d, ax.set_ylim3d, ax.set_zlim3d)]
            vertices = np.split(vertices, np.cumsum(polygons['lengths'])[:-1])  # split to find each face
            ax.add_collection3d(Poly3DCollection(vertices, linewidths=0.1, edgecolors='black',
                                                 facecolors=self.get_colour(), alpha=0.2))

            # add arrows
            magnetisation = np.array(rad.ObjM(magnet)).reshape((-1, 6)).T  # reshape to [x, y, z, mx, my, mz]
            for end in (-1, 1):  # one at each end of the block, not in the middle
                magnetisation[2] = end * length / 2
                ax.quiver(*magnetisation, color='black', lw=1, pivot='middle')

            self.tab_control.setCurrentIndex(2)  # switch to '3d' tab

            # solve the model
            try:
                rad.Solve(magnet, 0.00001, 10000)  # precision and number of iterations
            except RuntimeError:
                self.statusBar().showMessage('Radia solve error')

            # get results
            dx = 0.1
            multipoles = [mpole_names.index(c.label) for c in self.controls if c.label.endswith('pole') and c.get_arg()]
            i = multipoles[-1]
            xs = np.linspace(-dx, dx, 4)
            fit_field = np.polyfit(xs / 1000, [rad.Fld(magnet, 'by', [x, 0, 0]) for x in xs], i)
            fit_int = np.polyfit(xs / 1000,
                                 [rad.FldInt(magnet, 'inf', 'iby', [x, 0, -1], [x, 0, 1]) * 0.001 for x in xs], i)
            text = ''
            for j, (l, c, ic, u, iu) in enumerate(
                    zip(mpole_names, fit_field[::-1], fit_int[::-1], units[1:], units[:-1])):
                if j in multipoles:
                    f = factorial(j)  # 1 for dip, quad; 2 for sext; 6 for oct
                    text += f'{l} field = {c * f:.3g} {u}, integral = {ic * f:.3g} {iu}, length = {ic / c:.3g} m\n'
            ax.text2D(1, 1, text, transform=ax.transAxes, va='top', ha='right', fontdict={'size': 8})
            self.plot3d.canvas.draw()
Beispiel #4
0
def undulator_1st_int(obj, per, nper, prec=1e-5, maxIter=10000):
    """
    compute undulator K value
    arguments:
      obj = undulator object
      per = undulator period / m
      nper = undulator number of periods
      prec = precision goal for this computation
      maxIter = maximum allowed iterations
    return:
      the 1st field (Bz) integral at y = per*(nper+1)
    """
#     res = rad.Solve(obj, prec, maxIter)
    Bz_int1st = rad.FldInt(obj,'fin','ibz',[0,-1e5,0],[0,per*(nper+1),0])

    return Bz_int1st
Beispiel #5
0
def field_integral(g_id, f_type, p1, p2):
    return radia.FldInt(g_id, 'inf', f_type, p1, p2)
Beispiel #6
0
 def int_by_dz(self, x):
     """Return the integral along z of the vertical B-field."""
     if self.solve_state < SolveState.SOLVED:
         self.solve()
     return rad.FldInt(self.radia_object, 'inf', 'iby', [x, 0, -1], [x, 0, 1])
Beispiel #7
0
    print()
    nr5 = ny

    t0 = time()
    rad.UtiDelAll()
    ironmat = rad.MatSatIsoFrm([2000, 2], [0.1, 2], [0.1, 2])
    g = Geom()
    size = rad.ObjDegFre(g)

    t1 = time()
    Nmax = 10000
    res = rad.Solve(g, 0.00001, Nmax)
    t2 = time()

    Bz = rad.Fld(g, 'Bz', [0, 1, 0]) * 1000
    Iz = rad.FldInt(g, 'inf', 'ibz', [-1, 1, 0], [1, 1, 0])
    Iz1 = rad.FldInt(g, 'inf', 'ibz', [-1, 10, 0], [1, 10, 0]) / 10

    print('M_Max H_Max N_Iter = ', round(res[1], 4), 'T', round(res[2], 4),
          'T', round(res[3]))
    if (res[3] == Nmax): print('Unstable or Incomplete Relaxation')
    print('Built & Solved in ', round(t1 - t0, 3), '&', round(t2 - t1, 3),
          ' seconds')
    print('Interaction Matrix : ', size, 'X', size, 'or',
          round(size * size * 4 / 1000000, 3), 'MB')
    print('Gradient = ', round(Bz, 4), 'T/m')
    print('Int. Quad. @ 1 mm = ', round(Iz, 5), 'T')
    print('Delta Int. Quad. @ 10 mm = ', round(100 * (Iz1 / Iz - 1), 2), '%')

    #Display the Geometry
    rad.ObjDrwOpenGL(g)
    rmax = 30
    np = 40
    rstep = 2 * rmax / (np - 1)
    BzVsXY = rad.Fld(t, 'bz', [[-rmax + ix * rstep, -rmax + iy * rstep, z]
                               for iy in range(np) for ix in range(np)])

    uti_plot2d1d(BzVsXY, [-rmax, rmax, np], [-rmax, rmax, np],
                 x=0,
                 y=0,
                 labels=('X', 'Y',
                         'Bz in Magnet Gap at Z = ' + repr(z) + ' mm'),
                 units=['mm', 'mm', 'T'])

    z = 3
    IBzVsY = [
        rad.FldInt(t, 'inf', 'ibz', [-1, -rmax + iy * rstep, z],
                   [1, -rmax + iy * rstep, z]) for iy in range(np)
    ]
    print('Field calculation after solving (post-processing) done in',
          round(t1 - t0, 2), 'seconds')

    uti_plot1d(IBzVsY, [-rmax, rmax, np], [
        'Y', 'Vertical Field Integral',
        'Vertical Field Integral along X at Z = ' + repr(z) + ' mm'
    ], ['mm', 'T'])

    print('')
    print('Close all magnetic field graphs to continue this example.')
    uti_plot_show()  #show all graphs (and block further execution, if any)

    #Creating the Model and Solving with Rectangular Segmentation in the Corners
    t = Geom(0)