2

I am currently working on a project that I have to simulate the magnetic field produced by a circular wire loop. I have written a piece of code and put it into a class, that works perfectly, but when I try to create a GUI using TaitsUI, it just fails. I have no idea how should I connect the one to the other.

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()    

The Visualasation class tries to recreate what the first bit of code does but with the addition of some sliders. Unfortunately, I can't even plot the magnetic field within the Visualasation

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
gnikit
  • 1,031
  • 15
  • 25
  • This is a lot of code to debug. Can you reduce the problem to a minimal example that fails in the way you are struggling with? Also, if you post any tracebacks, that would be helpful. I copied your code into a file and tried to run it, and there were a lot of errors related to syntax, missing imports, etc. It's hard to know where to start. – Tim D Dec 10 '15 at 03:25
  • Incidentally, after fixing the initial import errors, I found a Mayavi scene open behind the other windows, so the rendering piece appears to be working. If you can elaborate on the error, more than "it just fails" then we might be able to help out. – Tim D Dec 10 '15 at 14:47
  • This is bad piece of work from my end, the GUI is poorly constracted, actually it is not even finished. TraitsUI ended up being too difficult to learn in a week, hence I used Tkinter to achieve my goal for this project. Which was to plot a series of 'mayavi.mlab.pipeline' functions into a scene and alter the scene's parameters through the GUI – gnikit Dec 18 '15 at 17:54

0 Answers0