Programming orbits

Welcome to my series on how to build scientific models in code.

I'll be using a model of Earth and Mars orbiting the Sun to demonstrate what this means in theory and practice.

I'll start by creating a simple conceptual model of the orbits of each planet. The result will be turned into working code in subsequent posts, along with discussion of the context, considerations and motivation for this kind of work.

Along the way, we'll see how the model and code can be improved and extended to explore some basic questions about orbits.

How to know Not Much about space travel

I've been into space and science fiction most of my life. In all that time, I've mostly avoided understanding much at all of the science of actual space travel. My knowledge of fictional space travel, by comparison, is excellent.

An image of Homer Simpson in court with fake glasses on. [The Algorithm]: He's watched a lot of star trek, perhaps he'd be interested in a very brief intro to orbital mechanics?

Maybe you too don't know much about space travel. Except, you probably haven't been a student programmer at a national space agency:

Space marketer

My job was to program a looping video that showed the current location of satellites in the lobby, I suppose to impress visitors. Marketing was a surprisingly large part of space work in my country. I can't remember if it was ever used.

To be clear, I did learn a bunch of things about a space agency, and something about technology in orbit. Just not a whole lot about travelling between planets.2

So between that and The Martian, which I watched and read, my formal training in space flight is, to be generous, rudimentary.

The Martian and modelling orbits

Blogging about Silico and scientific programming seems to be the perfect incentive for learning just a tiny bit more about the topic.

An imagine of a NASA analyst briefing higher ups with a stapler in the movie The Martian. The stapler is a ship.

Since it came up a lot in The Martian, I'm going to focus on how the orbits of Earth and Mars affect travelling between the two. Not that it really matters to this exercise, but the plot of the movie was that a mission to Mars left something important behind, and everyone had to figure out how the ship might get back there to retrieve it. The thing left behind was... perishable, so they were on the clock.

Nobody is going to make of a movie of the task I'll be setting myself, although it should be illuminating. I'm going to build a simple model of the orbits of Earth and Mars, while also showing how you can build models in code, in Silico, and some of the things to think about while doing so.

Earth. Mars. Orbits: Too complex

How do we get to Mars? Through space? That's already a big simplification. First we have to get through Earth's atmosphere and gravity, through space and whatever may exist there, and finally into Mars orbit. Plus, you can assume I've just skipped over all sorts of interesting problems I have no idea about.

Making cuts

Since the purpose here is to gain a tiny window into the problem of orbits, travel and modelling - rather than land an actual mission to the satisfication of people accountable for funding such expeditions - simplifying the problem space will help a lot.

A gif of Homer Simpson cutting members of a football team in the same way we cut complex problems to simplify our model.

Time for some quick cuts:

Perfect, that's just about everything I could think of in a couple minutes. If anything else comes to mind, feel free to cut that too.

How far should these simplifications go? Given our purpose, it's not useful to think of Mars and Earth as stationary - we're interested in orbits. They'll have to move in some fascimile of an orbit (circle-like) around the Sun.

It'll also anchor the model to include some factual data: The distance of a planet from the sun, and how long it takes to go around the sun, for example. This kind of thing isn't strictly necessary, but then we can attach names like "Earth" and "Mars" to the "planets" in our model.

Guessing, errr... using judgement

There are a number of orbital details that could be important and it's a judgement call as to their inclusion. Is the 3rd dimension useful? Put another way, would its exclusion impose too heavily on our purpose? I don't know. Imagine the orbital planes as perpendicular to each other. I can see that making a difference.

A bit of searching suggests that Earth and Mars are in similar orbital planes. Accuracy isn't our objective, and so it's probably fine to ignore the 3rd dimension. It will almost certainly make the model building easier.

Other problems (after some quick glancing at Wikipedia) could include orbital irregularities (they're not perfect circles or ovoids). These will also be ignored.

Everything simplified out of the model would likely be useful given another purpose, but again, I'm trying to learn something from a very simple model of the systems we're interested in, and to learn something by building it in code.

A conceptual model

The result is a conceptual model of the following form:

In your mind it might look something like this:

A diagram of Earth and Mars moving in a circle around the Sun, representing the our simplified model of the Solar System.

I'm assuming the planets are moving around the sun in the same direction too, which is another factual detail I'm including from some wikipedia animation I glanced at for a couple seconds.

Read it thirty times

Having established this conceptual model, I had little idea how to implement it. I've seen orbits in space video games and this means it's probably within the reach of mortals and the computers they might program.

What's needed is a programmatic way to move Earth and Mars around in their orbits on a 2d grid, a way to calculate their x,y coordinates and do this in a way that can include the real world data I'm using.

Modelling physical systems in code isn't something I do, so imagine a few hours of distracted, but increasingly coherent internet research, much of which was Wikipedia rabbit holes for some of the relevant math.

It became apparent that trigonometry and angular velocity are useful mathematical tools for the problem. At the least, they're one way to approach the programming problem. I've never learned anything about angular velocity (I hope I haven't...) and haven't touched trigonometry since high school.

Working in my favour, however, is that the modern programmer is most at home understanding just enough to hopefully, probably, possibly-until-they-can-find-a-new-job, craft a solution. A lot of my life has lead up to this moment.

Here are links to concepts, and posts that pointed me in the right direction. I'll discuss how to approach the programming in the next post.