Showing posts with label Euler integration. Show all posts
Showing posts with label Euler integration. Show all posts

Wednesday, 15 August 2012

Fizzbox's Second Sim

Last time I promised I'd get started on an ideal and non-ideal gas movement simulator. Well I didn't. I got distracted working on my new website (not online yet...) which is looking super cool now. Keep an eye out for news on that! I've been using Bootstrap, a beautiful web design tool made by the creators of Twitter (see shameless link to my own twitter page). It's pretty much done thanks to Bootstrap's simplicity but it's lacking in content. I think I'll fork out for a domain name once I have another two sims to show off.

With that in mind I've been working on a very simple simulation of a mass-spring system. Writing a second sim was a million times easier than the first - not much more work than deleting all the code that had anything to do with gravity and sticking some springy stuff in there instead. This is just first draft, and its not interactive at all at the moment. There are two mass-spring systems with slightly different initial conditions. The idea will eventually be that you can choose the setup yourself and see the effects of the changes you've made by comparing the two systems. Here it is in all its simplish simpleness:

http://pastehtml.com/view/c8cbm9i4f.html.

As this sim is much simpler I thought that before it gets more complicated should share the source code. All of the interesting physics happens in the function called 'step'. If you're an A-level standard physicists or above should be able to see what that function is doing using a bit of your integration knowledge. BoxA and boxB are the two masses. I'm not going to annotate this line by line but I'll happily answer any questions posted as comments if I'm able. Here it is:


Monday, 6 August 2012

Teething Problems

The first draft of Fizzbox gravity is done - here it is in all its glitchy badness! Click in the box to make a planet.


There are loads of annoying problems to sort out, some I understand, some I don't.
I promise you won't need to understand much physics to enjoy most of my sims, but some boring geeks like me might want to know what's gone wrong. Non geeks could skip to 'Good Things to Come'.

Ping Effect: Make two planets very close together. You can do that by clicking twice in the same spot relatively quickly. The planets will accelerate towards each other before shooting off, sometimes too fast to see. This is to do with the Integration technique, Euler Integration. A thousand times a second this program calculates the force of gravity due to each planet on every other planet. When two planets become very close, the force of gravity between them is very strong causing huge acceleration. In a thousandth of a second the virtual planet will shoot past its partner to a point were gravity is weaker, and fly off. 

In our Universe, this obviously doesn't happen, for two main reasons. Unlike those above, real planets aren't point sources of gravity - they have a size. Unless two point sources come together, there's no problem. Secondly, there's no time delay between 'simulation steps' in the real world. You could think about the Universe doing an infinite number of calculation sets every second, rather than a measly thousand. Alternatively you could just accept that the Universe doesn't bother to do integration, it just happily physicses along physicsly.

I've put in two fudgey fixes to this problem:
  • Gravity 'switches off' when planets come within a certain distance of each other. You might be able to see this if you watch a collision carefully. This is a horrible fix and I hope I can get rid of it asap, possibly by allowing planets to bounce off each other. I wonder if I could just spread the mass through  the disk of the planet instead - more like the real case?
  • Planets start with random initial speed. You can see this by clicking once - the planet will move even though there are no other planets to interact with. That makes two planets less likely to cross paths. This isn't bad physics, since the Universe didn't just start out as a bunch of stationary planets wanting to interact.
There are more problems which I don't get. I'm not convinced that all the planets 'notice' each other. Some just don't seem to interact. Fix to come soon (hopefully!).

Good Things to Come: After a few hours of bug fixing there are a few cool things I can do to make this way more interesting. Watch out for the next draft in a day or two:

  • Choose the mass of a planet. I think this would be done by the user holding the mouse button down as the planet grows.
  • Set initial velocity. This should let you set up interesting orbits.
  • Set up our solar system. I'm hoping to set up a sim with real data to model our system. That way you'll be able to edit it and see how things would go if something were different.
  • Planets that aren't just red dots! Expect to see the sim looking a bit less boring next time.


About Me

My photo
Manchester, United Kingdom
Second year physics student at the University of Manchester, interested in simulation and education.