30 Days of Python: Day 27 Traveling Electrons

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 27: Traveling Electrons

For today’s project, I simulated an electron moving in electric and magnet fields. I used the vpython package which is a great tool for physical simulations because it is simple to do the vector calculations in and to pair those with visible objects in a screen. Unlike the orrery project where I used a clockwork model of the system, for this project I simulated the net force on the particle. The Lorenz Force describes the force of an electric and magnetic field on a particle charge. Here’s what it looks like in code:

while True:
    F = particle.charge*(E_field.mag*E_field.axis + particle.vel.cross(M_field.mag*M_field.axis))
    accel = F/particle.mass
    particle.vel = particle.vel + accel*DT
    particle.pos = particle.pos + particle.vel*DT

Because the magnetic force on the particle is perpendicular to both the particles velocity and the magnetic field, it creates a centripetal force on the particle sending it in a circle:

Only Magnetic Field Circle

Only Magnetic Field – Circle

If the particle has a small portion of the velocity in the same direction as the magnetic field, the result is a helix in the direction of the magnetic field:

Only Magnetic Field Helix

Only Magnetic Field – Helix

If an electric field is added on top of that the helix expands in the direction of the electric field:

Electric and Magnetic Field - Expanding Helix

Electric and Magnetic Field – Expanding Helix

If the electric field points to a side then it causes drift:

Electric and Magnetic Field - Drifting Helix

Electric and Magnetic Field – Drifting Helix

I initially had a bug in my code that made all of the shapes wrong and not match my intuition. I finally spotted it and the simulation started behaving correctly. That’s a good lesson learned to really work out what a few solutions should look like before moving on to the more complex ones. I’d like to spend more time with this one and see if I can add in varying electromagnetic fields to see what I can get the electron to do!

For those that are interested, here’s my science simulations repository: https://github.com/robb07/science_sims

Advertisements

30 Days of Python: Day 10: An Orrery

I’m making a small project every day in python for the next 30 days (minus some vacation days). I’m hoping to learn many new packages and  make a wide variety of projects, including games, computer tools, machine learning, and maybe some science. It should be a good variety and I think it will be a lot of fun.

Day 10: An Orrery

An orrery is a device for demonstrating the motion of the planets in the solar system. Typically it was made with gears but mine is with python code. Specifically, I used the Vpython package which is a great tool for visualizing physics because it uses the numpy package for the numerical backend and allows you to manipulate the graphics with ordinary math coordinates instead of computer screen coordinates. This means you can write a physics equation and get the resulting motion out very easily. The inspiration for this project and the other science simulations that I’m planning is Rhett Allain’s blog over at Wired. I love the simulations that he puts together to answer scientific questions.

Scale

It’s impossible to show everything in the the solar system within a single scale since it is so big. Instead, I use two scales: astronomical units for distance from the sun and earth radii for radii of the planet. I can then balance the ability to see the planets and the ability to see their orbits. The Sun’s radius is in astronomical units so it won’t dominate the visual (it’s 100 times bigger than Earth). Here’s a snapshot of the 6 classical planets travelling around the sun:


The 6 Classical Planets around the Sun

The 6 Classical Planets around the Sun

 

I can turn on and off planets with a control list to get more or less detail. Here’s the view that includes Pluto:

Heliocentric with Nine Planets

Heliocentric with Nine Planets

 

Epicycles

Because this is computer code, it’s very easy to change the frame of reference. Instead of looking at the solar system with the sun in the center, you can see how the planets move “around” the earth (or any other planet). Looking at the shape of the orbits from other reference frames gives you a sense of what the Greek model of epicycles would have been like. Epicycles are a method of describing planetary motion using nested rotating shells within shells that create the complex motions observed from earth. Each planet gets several epicycles to account for getting closer and further from Earth at various times. It’s a very complex model to account for all of the motion. Plus, they are fun to watch:

Writing out an explicit equation for these complex motions would be nearly impossible, but since everything is relative in the code it’s quite easy:

  • Calculate Earth’s position (fixed)
  • Calculate Sun’s position (circular to Earth)
  • Calculate third body (circular to the Sun)

No need to solve for Jupiter relative to Earth or anything like that. Here’s what an geoentric model and Jovian (Jupiter) centric model look like:

 

Earth Centric with the Classical Planets

Geocentric with the Classical Planets

Jupiter Centric model with the Classical Planets

Jovian Centric model with the Classical Planets

I love the way that the inner planets dive towards the sun as they “orbit” around Jupiter. Each one of the loops would be explained away as part of an epicycle around the larger cycle that went around the Earth. My model is not an exact representation of the solar system though so you would need fewer (probably two) epicycles to build a truly earth centric model. To truly account for all the variation the Greeks saw, they had to include a lot more epicycles than just the two that show up here. Their system actually provided accurate calculations for a long time; it took a thousand years for the planetary positions to be noticeably off the predictions.

Limitations

Science is about figuring out how reality works (or at least how it tends to work). To that end we build models through sets of equations, tables of properties, or some simulation code. In the end, we hopefully get a better model than we had before, but it it may never be perfect. Some limitations in my model:

  • It models motion not force (this is a clock work universe)
  • It assumes circular motion (see how off Pluto is?, this is Copernicus style not Kepler style)
  • It assumes a flat solar system (Pluto would be off the plane)

Improvements

There are a few things that I think I could add:

  • Add rotation to show days
  • More moons!
  • Maybe a comet or two
  • How about some asteroids?

For those that are interested, here’s my new science simulations repository: https://github.com/robb07/science_sims