fys1120 – Elektromagnetisme http://elektromagnetisme.no The home of FYS1120 Mon, 20 Oct 2014 11:23:54 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.3 28429679 Nanoparticles: An after-exam bonus http://elektromagnetisme.no/2011/10/14/nanoparticles-an-after-exam-bonus/ http://elektromagnetisme.no/2011/10/14/nanoparticles-an-after-exam-bonus/#respond Fri, 14 Oct 2011 08:39:40 +0000 http://mindseye.no/?p=946 Continue reading ]]> I guess you are pretty exhausted after the midterm exam this week and a lot of you have probably had other exams to deal with as well. What better time to share a game with you to put your minds at ease for a little while.

The game is called Nanoparticles and was written by me about half a year ago. It is based on Coulomb’s law with a few modifications and Newtonian physics. But instead of having you calculate anything, the game does it all for you in the background. You can download the game for Ubuntu, Linux, Windows and Nokia phones here (Mac and Android versions will be available sometime in the future):

Download Nanoparticles

Your mission is to make sure the positive particles you control does not crash into the negative ones. That would cause it to annihilate. To do this, you need to place out other positive and negative particles to keep your particle away for as long as possible.

The game is also open source, so if you’re interested in programming and C++, you can have a look at the source code too. More information about the game and how to obtain the source code is found here.

]]>
http://elektromagnetisme.no/2011/10/14/nanoparticles-an-after-exam-bonus/feed/ 0 946
The FYS1120 Weekly Assignment Post #3 http://elektromagnetisme.no/2010/10/06/the-fys1120-weekly-assignment-post-3/ http://elektromagnetisme.no/2010/10/06/the-fys1120-weekly-assignment-post-3/#comments Wed, 06 Oct 2010 05:38:57 +0000 http://mindseye.no/?p=284 Continue reading ]]> This week the problems focuses mainly on circuits and Kirchhoff’s laws.

Assignment #7

1)

a)  I_1 = \frac{11}{3} A, \ \ I_2 = -\frac{4}{3} A, \ \ I_3 = -\frac{7}{3}A.

Indicating that current nr.2 and 3 is directed in the opposite way of what was assumed.

b) P_{\varepsilon_1} = \varepsilon _1 I_1 = 44 W

P_{dissapated} = R (I_1 ^2 + I_2 ^2 + I_3 ^2) = 41.33 W.

c) Of course the second voltage supply is also doing work on the charges in the current, so this is the reason that P_{dissapated} \neq P_{\varepsilon_1} In this case it is actually doing negative work.

2)

a) At t = 0, the moment when the switch is closed, there is no charge on the capacitor and therefore no potential difference over it. This means that the current at this time will be as if there were no capacitor in the circuit, so I = \frac{\varepsilon}{R}

As time goes to infinity, the capacitor gets charged up. At some point it will reach a potential difference equal to the potential difference of the voltage supply and the current will be zero.

b) At t = 0 it is a ressistance R in one subcircuit, while there is no ressistance in the other, so the current will go trough the circuit with no ressistance. Since there is no resistance I = \frac{\varepsilon}{0} = \infty,  which means that the capacitor gets charged up infinitely fast.

When the capacitor is fully charged, no current will go towards it and therefore all current will go trough the resistance. This means that we now have I = \frac{\varepsilon}{R}

3)

a)  \tau = RC = 1 \times 10^{-6} s

b)  q(t=30s) = \varepsilon C \left[ 1 - e^{-\frac{t}{\tau}} \right] = 1.2 \times 10^{-11} C

c) I(t=30s) = \frac{dq}{dt}(30s) = 0

4)

a) Here I used Kirchhoff’s laws to get 5 equations which I put in a matrix, did rowreduction and got I_1 = 6A, \ \ I_2 = 5A, \ \ I_3 = 1 A, \ \ I_4 = 7A, \ \ I_5 = 4 A.

b) R = \frac{\varepsilon}{I} = \frac{\varepsilon}{I_1 + I_2} = \frac{\varepsilon}{I_4 + I_5} = 1.812.. (correction: Filip Sund)

]]>
http://elektromagnetisme.no/2010/10/06/the-fys1120-weekly-assignment-post-3/feed/ 4 284
Using Mayavi in Fys1120 oblig 1 http://elektromagnetisme.no/2010/10/05/using-mayavi-in-fys1120-oblig-1/ http://elektromagnetisme.no/2010/10/05/using-mayavi-in-fys1120-oblig-1/#respond Tue, 05 Oct 2010 14:41:17 +0000 http://mindseye.no/?p=268 Continue reading ]]>

3D plot for exercise 2d

There are only two days left before the first mandatory exercise in Fys1120 is due. Last time I posted about Mayavi I wrote in a comment that I would give you some hints on how to use it with the oblig. Actually, there are not so many useful applications for Mayavi in this particular oblig, since most plots are 2D and are better created using scitools.

In fact, the 3D plots in this oblig might also be plotted using scitools, but I decided to use Mayavi to test it out for this particular purpose.

In my code I stored the position vector for the particle in an array r, which is an n \times 3 array created using the NumPy function r = zeros((n,3),float). If you have stored the position in a similar array, you may use the code below to plot the position in exercise 1d, 2a and 2d. After plotting, use your mouse to find a camera angle you’re satisfied with and hit the save-button in the toolbar.

from enthought.mayavi.mlab import *
fig = figure(fgcolor=(0,0,0), bgcolor=(1,1,1)) # set the background and foreground of our figure
plot3d(r[:,0], r[:,1], r[:,2], tube_radius=None, color=(0.807, 0.10, 0.023))
axesm = axes()
axesm.title_text_property.bold = False
axesm.label_text_property.bold = False
axesm.title_text_property.italic = False
axesm.label_text_property.italic = False
axesm.title_text_property.font_family = "times"
axesm.label_text_property.font_family = "times"
outlinem = outline()

A decent tip for the LaTeX-users out there is to save your file as “.pdf”. This way you may import it to a LaTeX document without losing quality. Note that the tube_radius parameter of the plot3d function must be set to None to avoid humongous PDF files.

Below you see the output I got for the different exercises, but don’t panic if your results are different. There might be some errors in my own code for all I know.

3D plot for exercise 1d 3D plot for exercise 2a 3D plot for exercise 2d ]]>
http://elektromagnetisme.no/2010/10/05/using-mayavi-in-fys1120-oblig-1/feed/ 0 268
Using Mayavi to visualize electric fields http://elektromagnetisme.no/2010/09/25/using-mayavi-to-visualize-electric-fields/ http://elektromagnetisme.no/2010/09/25/using-mayavi-to-visualize-electric-fields/#comments Fri, 24 Sep 2010 22:53:47 +0000 http://mindseye.no/?p=141 Continue reading ]]>

Mayavi renders great field line plots.

While searching for a good Python module to visualize electric fields, I found Mayavi. Developed by Enthought, Mayavi is a very good module for visualizing a huge range of different scientific data sets. Everything from surfaces, flows and streamlines to bar charts, 3D plots and contour surfs are beautifully drawn on screen and exported to several file formats, such as PDF, PNG, EPS and more.

What I needed it for, however, was to visualize electric field lines in the course FYS1120 at the University of Oslo. We were told to use Matlab with the streamline and quiver functions, but even so, I wanted to use Python and decided to do a search and see if something similar was possible with Python. It took me some time to figure out how to use the scitools package to do streamline plots, but eventually I made it. However, these were a bit tedious to get working correctly and looked only about as good as the Matlab plots.

I continued my search and found Mayavi as an alternative. It seemed like it was a bit out of the scope of my quest, but I decided to give it a serious try in any case.

I’m glad I did. The quality of the plots in Mayavi are amazing compared to Matlab’s. Let’s take the following field line plot done in Matlab as an example:

The field line plot in Matlab.

This is done with the streamline function, and if you have been working with field lines in electromagnetism, you’ll notice that the plot doesn’t really follow the definition of field lines.

In the field line definition, the tangent of any point on the field line is the same as the electric field in that point. At the same time, the density of field lines should represent the strength of the field. That is, the electric field in a specified area is proportional to the number of field lines in the same area.

This is where this Matlab approach with the streamline function doesn’t work well. Actually, it misses completely. As you see, the density of the field lines is bigger in the edges, further away from the charge, while it should be bigger in the middle, close to the charge.

Methodically, the streamline function allows us to select from which points we want to draw field lines, and we could of course generate the streamlines in a circle around the charge instead of in a grid like now. However, the plot will still be a bit “rough” in the edges and doing this for every point requires some extra amount of logic for the programmer.

Giving Mayavi a try at the job shows us how much prettier and more correct the representation in Mayavi is:

The same field line plot in MayaVi.

Now that’s what I call a field line plot! Giving our lonely charge a few positive and negative friends to play with yields another pretty picture:

A field line plot using Python with MayaVi, showing four charges

You might notice that this plot is also not perfect in terms of the field line definition, but it is very close and a lot better than what we would have gotten from Matlab’s equivalent plot.

How to do it

Below you see the full source code for this script. I’ve included comments on each line instead of describing the script in detail here. However, I do want you to notice one important thing:

Mayavi is very good at visualizing things in 3D space, but it does not seem to be intended for 2D use. Therefore, we need to do some hacks to simulate a 2D space. It works flawlessly when you know how to do this, but it makes everything seem a bit harder than necessary. The payoff, on the other hand, is humongous, and it is one-time thing to learn how to do.

As well, it makes it easily possible to scale our plot up to a 3D-plot instead, which I will show you in the next post about Mayavi.

Enjoy!

from numpy import *
from enthought.mayavi.mlab import *

N = 100 # the bigger the number, the more calculations your script has to make, but you save MayaVi's Runge Kutta method a lot of trouble and the total plot time is lowered
a = 0. # the lowest coordinate
b = 1. # the highest coordinate
dt = b / N; # the step we need to make to get to each position on our grid

q = [1., -1., 1., -1.] # the values of our four charges
qpos = [[0.56, 0.56, 0.50], # and their positions
        [0.26, 0.76, 0.50],
        [0.66, 0.16, 0.50],
        [0.66, 0.86, 0.50]]

x,y,z = mgrid[a:b:dt, a:b:dt, 0.:1.:0.5] # here is the trick - we want a 2d plot, but MayaVi works best in 3D space. Let's just keep the z-axis to a minimum (two steps)
Ex, Ey, Ez = mgrid[a:b:dt, a:b:dt, 0.:1.:0.5] # create a few arrays to store the vector field in, using meshgrid

for i in range(N): # iterate over all rows
    for j in range(N): # and all columns
        Ex[i,j] = 0.0 # set the value of each point to 0, initially
        Ey[i,j] = 0.0
        for num in range(len(q)): # for each charge, calculate the electric field it provides
            rs = ((x[i,j] - qpos[num][0])**2 + (y[i,j] - qpos[num][1])**2) # the distance from the point to the charge, squared
            r = sqrt(rs)
            q1x = q[num] * (x[i,j] - qpos[num][0]) / (r * rs) # the x-component of the field
            q1y = q[num] * (y[i,j] - qpos[num][1]) / (r * rs) # and the y-component
            # this is $\frac{q}{r^2} \hat{\mathbf r}$ on component form
            Ex[i,j] = q1x + Ex[i,j] # now, add this to the electric field in this point, together with the contribution from the other charges
            Ey[i,j] = q1y + Ey[i,j]

fig = figure(fgcolor=(0,0,0), bgcolor=(1,1,1)) # set the background and foreground of our figure
#obj = quiver3d(x, y, z, Ex, Ey, Ez, line_width=1) # uncomment this if you want a quiver plot
streams = list() # create a list to hold all our streamlines (or flows if you speak MayaVi)

for s in range(len(q)): # for each charge, create a streamline seed
    stream = flow(x,y,z,Ex, Ey, Ez, seed_scale=0.5, seed_resolution=1, seedtype='sphere') # the seed resolution is set to a minimum initially to avoid extra calculations
    stream.stream_tracer.initial_integration_step = 0.01 # the integration step for the runge kutta method
    stream.stream_tracer.maximum_propagation = 20.0 # the maximum length each step should reach - lowered to avoid messy output
    stream.stream_tracer.integration_direction = 'both' # integrate in both directions
    stream.seed.widget.center = qpos[s] # set the stream widget to the same position as the charge
    stream.seed.widget.radius = dt * 2 # and its radius a bit bigger than the grid size
    stream.seed.widget.theta_resolution = 30 # make the resolution high enough to give a fair number of lines
    stream.seed.widget.phi_resolution = 1 # but we are looking at a plane for now, so let's not have any resolution in the z-direction
    stream.seed.widget.enabled = False # hide the widget itself
    streams.append(stream) # and eventually, add the stream to our list for convenience

xlab = xlabel("x") # set the labels
ylab = ylabel("y")
showm = show() # show everything
axesm = axes() # add some axes
axesm.axes.y_axis_visibility = False # remove the z-axis (named y for some MayaVi reason)
fig.scene.z_plus_view() # let's look at it from the top!
fig.scene.parallel_projection = True # and we don't need any projection when looking at it in 2D
print "Done."
]]>
http://elektromagnetisme.no/2010/09/25/using-mayavi-to-visualize-electric-fields/feed/ 13 141