使用Mayavi中的TraitsUI创建GUI,磁场模拟

时间:2015-12-07 02:00:58

标签: python enthought mayavi traitsui

我目前正在开展一个项目,我必须模拟圆形线圈产生的磁场。我编写了一段代码并将其放入一个完美的类中,但是当我尝试使用TaitsUI创建GUI时,它就失败了。我不知道应该如何将这一个连接到另一个。

class Wireloop:
    x,y,z = np.mgrid[-10:10:150j, -10:10:150j, -10:10:150j]

    ### Transform to Cylindrial Polar
    r = np.sqrt(x**2 + y**2) # ρ in polar
    x_trans = x/r            # cos(θ)
    y_trans = y/r            # sin(θ)
    """
    """
    def __init__(self, R, I):
        self.R = R #radious 
        self.I = I #DC current
        #Constants  
        self.mew0 = 1 #μ0 constant, it to be able to change

    # Elliptic Integral E    
    def E(self): 
        """
        """
        r,z,R = self.r,self.z,self.R
        return special.ellipe((4*R*r)/((R+r)**2 + z**2))


    # Elliptic Integral K
    def K(self): 
        """
        """
        r,z,R = self.r,self.z,self.R
        return special.ellipk((4*R*r)/((R+r) ** 2 + z**2))

    #Translational Magnetic Field    
    def Br(self):
        """
        """
        E,K = self.E(), self.K()
        r,z,R,I = self.r,self.z,self.R,self.I
        mew0 = self.mew0
        return (mew0 * I/(2*np.pi * r))*(z/(np.sqrt((R+r)**2 + z**2)))*(-K + E*((R**2 + r**2 + z**2)/((R-r)**2 + z**2)))


    #Axial Magnetic Field
    def Bz(self):
        """
        """
        E,K = self.E(), self.K()
        r,z,R,I = self.r,self.z,self.R,self.I
        mew0 = self.mew0
        return (mew0 * I)/ (2*np.pi*np.sqrt((R + r) ** 2 + z ** 2)) * (K + E * (R ** 2 - r ** 2 - z ** 2)/((R - r) ** 2 + z ** 2))

    #Generate Bx, By
    def Bx(self):
        x_trans = self.x_trans
        return x_trans * self.Br()

    def By(self):
        return self.y_trans * self.Br()

a = Wireloop(1,5)
Bx, By, Bz = a.Bx(),a.By(),a.Bz()
fig = mlab.figure(1, size=(500, 500), bgcolor=(1, 1, 1), fgcolor=(0, 0, 0))

field = mlab.pipeline.vector_field(Bx, By, Bz)
magnitude = mlab.pipeline.extract_vector_norm(field)
contours = mlab.pipeline.iso_surface(magnitude,
                                    contours=[ 0.01, 3.0, 4.0],
                                    transparent=True,
                                    opacity=0.5,
                                    colormap='Greys',
                                    vmin=0, vmax=1)

field_lines = mlab.pipeline.streamline(magnitude, seedtype='line',
                                        integration_direction='both',
                                        colormap='jet',opacity=0.6,
                                        vmin=0, vmax=0.7)


field_lines.seed.widget.resolution = 20
field_lines.stream_tracer.maximum_propagation = 100
field_lines.seed.widget.point1 = [69, 75.5, 75.5]#placing seed inside the loop
field_lines.seed.widget.point2 = [82, 75.5, 75.5]
#field_lines.seed.widget.enabled = False

mlab.show()


import scipy
from traits.api import HasTraits, Range, Instance, on_trait_change, Array,    Tuple, Str
from traitsui.api import View, Item, HSplit, Group
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor 


class Visualisation(HasTraits):
    Radius = Range(1.0, 5.0, 1.0, desc='the radius of the wire loop',enter_set=True,
          auto_set=False)
    i = Range(-5.0, 5.0, 1.0, desc='the current passing throught the wire',enter_set=True,
          auto_set=False)
    # The mayavi(mlab) scene.
    scene = Instance(MlabSceneModel, args=())

    def __init__(self):
        HasTraits.__init__(self)
        Bx = Wireloop(self.Radius, self.i).Bx(self)
        By = Wireloop(self.Radius, self.i).By(self)
        Bz = Wireloop(self.Radius, self.i).Bz(self)
        self.plot = self.scene.mlab.pipeline.vector_field(Bx, By, Bz,colormap='Spectral')


    @on_trait_change('Radius,i')
    def update_plot(self):
        Bx = Wireloop(self.Radius, self.i).Bx(self)
        By = Wireloop(self.Radius, self.i).By(self)
        Bz = Wireloop(self.Radius, self.i).Bz(self)
        self.plot.mlab_source.set(Bx=Bx, By=By, Bz=Bz)


    view = View(HSplit(
                    Group(
                        Item('scene', editor=SceneEditor(scene_class=MayaviScene),
                             height=500, width=500, show_label=False)),
                    Group(
                        Item('Radious'),
                        Item('i'))))


visualisation = Visualisation()
visualisation.configure_traits()    

Visualasation类尝试重新创建代码的第一部分,但添加了一些滑块。不幸的是,我甚至无法在Visualasation中绘制磁场

0 个答案:

没有答案