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
来源:https://stackoverflow.com/questions/34125317/using-traitsui-in-mayavi-to-create-a-gui-magnetic-field-simulation