 The title is per Python a Dastra and please welcome Juan Luis Kano Well as the previous speaker said, there's a Python library for everything in life even for rocket science Well, let me introduce myself first. My name is Juan Luis Kano I'm an almost aerospace engineering Engineer studying in Madrid and right now working in finance as a Python developer for BBBA Almost the self-taught programmer because in university they used to A little bit of mad lab and well, we run all our algorithms in Excel So it was not a great background to start with and I'm passionate Individual about open source open hardware open science and its relevance in the world that we live now I'm also the chair of the Python Spain non-profit and organizing many events like the Python Spain conference the Python Madrid meetup And I remind you that the Python Spain call for purposes is still open in case you didn't I Didn't make it clear yesterday in my quick lightning talk and well space Fascinating, well, I found something very amusing about space and is that Almost nobody knows how it works and what's going on up there and yet It's like the only field adults are willing to accept their ignorance and ask all kind of questions And I say adults here because children have this amazing superpower of asking almost everything and this infinite curiosity that all of us start losing with time Well, so wait a minute Before explaining what is exactly this astrodynamics thing and give you any Wikipedia the exact definitions Let's start with a little video You might recognize here Clark Kent from the Super Mario Jones movie and he's watching live through his alien eyes and these ridiculous haircut and wondering what if I use my super strength to put this baseball in orbit and So it goes Bye-bye then the dogs quickly runs to catch the ball but realizes the situation and Turn his back to Clark like seriously and then will the butt hits some random guy in New Zealand or something So what's happening here like well? Superman is super super strong. So if he's launching the baseball very very quick then he's going to Reach a very long distance and as the earth is round Well, there are no flat earthers in this room, right? Because I'm going to disappoint you a lot. You can you might leave now Okay, as the earth is round then the ground is starting to curve under your feet So the ball is hitting some point To the other side of the world eventually it's going to reach like New Zealand the other the other side And if you launch the baseball even quicker then at some point The ground is curving so fast that you never touch it and this is what we call orbital velocity or orbital motion and Freefall because you don't actually need any propulsion or any means to Like increase your velocity and you are just falling all the time. This example is not mine It was devised by Newton in his masterpiece of the 17th century Principia Mathematica And he's one of the earliest examples of a thought experiment But obviously he didn't use Superman for the analogy the title of the trap of the Of the pieces in Latin and we will talk more about Latin at the end of the keep this in mind Well, so with this in mind, what is extra dynamics exactly what is a branch of celestial mechanics that studies the motion of human-made objects through space and there is a very essential a couple of essential differences between studying the motion of the planets and the motion of human-made objects because the Satellites rockets and stuff are so small that we have to take into account all the perturbations that might act to them and Also, they have propulsion means so they can act on their own trajectory and correct The velocity and this complicates everything. Well, and this is where the introduction Stops, I'm going to put a little bit of math, but I'm going to try to keep it very simple I don't need everybody in the audience to understand everything But I just want you to keep in mind the ideas that are behind this kind of problems I'm going to talk about the basic problems that we saw in astrodynamics and later on I'm going to say How do I solve them in Python? Well, the first one is the two-body problem, which is just one body orbiting around another one Okay, in the limiting case We are considering that these masses have no radius. Okay, so it's there are just like geometric points in space and As we are usually considering like the motion of a spacecraft around the planet or a moon or something Then we can assume that the second body is very very small and doesn't Have any effect on the orbit of the first one and that is the equation that controls everything And the second one is the Kepler problem, which is like the initial value problem of the thing that I said before I have Some state and some moment in time have a position and a velocity and after some time I want to know where my satellite my spacecraft, whatever is going to be this is called also propagation And these are the equations that for the or for the elliptical case that govern everything and I want to put this here because that equation over there the first one if you remember if you remember your Your secondary school mathematics you cannot solve that equation for e For capital e and that is the for some people they say that this equation is so difficult to solve that It motivated 200 years of mathematicians to develop many different and innovative techniques to solve it And we made huge progress in mathematics. Thanks to the instructor of this equation And the last one is the Lambert problem, which is a little bit different, but it's still based on the same thing I have one position and I want to reach another position in a given time So I want to know what is exactly the trajectory they have to develop In the early days like when we are designing a trajectory around the solar system because I have some mission As I will say after this We can assume that all the planets are like points and only consider the gravity of the Sun So to solve all these kind of problems, I created polyastro, which is an astrodynamics library written in Python. It is released under a permissive license and it have physical units handling It solves all the problems that I said before Includes some basic 2d plotting as we will say after this And it would be impossible without the work of many many people. I'm going to talk about a couple of the dependencies The first one in case you don't know it is astro pi, which is a like a basic Astronomy library written in Python. It's a joint effort of many many developers around the world And it's meant to have like the very building blocks of any astronomy project that you might have For instance, it has physical units, which is like static typing for engineers because If you mix meters with miles or something like that, then very bad things start to happen It has also handling of dates and times. If you think that handling some time zones is a pain Then you better don't enter the astronomical times. It's a real mess And it also changes the compression between reference systems So I can express one position with respect to the sun with respect to the problem, etc The second one is JPL if m which is a library by run Brandon rots, which is one of my favorite Python developers And the thing is that the NASA and the jet propulsion laboratory They provide some planetary positions and velocity in very broad range that goes to go between Hundreds or thousands of years and they provide them in a binary format Which is called SPK and with this library I can take that data and know exactly where a planet is going to be in the year 3000 Okay, so what happens with the very basic algorithms because this involves like integration integrating differential equations and stuff like that And when I started working on this, I said, okay Let's see what have other people done on this before me and I found a lot of Fortran mad lab Jiva algorithms that were Were okay because they worked and they had a very good performance But the code was a bit poorly written. There were no tests whatsoever They were very difficult to distribute because they were works on my computer state released to the internet in a zip file and Wrapping that those algorithms in really important in C++ or whatever from Python is possible, but it might be a challenge So I ended up with a thing that only was known to work on my computer And then some years after that I discovered Namba, which is a project by continuum analytics There is also free and it's meant to accelerate the code the numerical Python code that uses a lot number crunching numerical computations non-pire race and It supports an a subset of the language and compiles to LLVM, which is the compiler toolset that is Is getting very famous now and also it supports GPUs So I tried to rewrite all the algorithms that were included in thousands of lights of Fortran Only in Python and see well, let's see how it goes And these are the results of a paper that I presented to the European Space Agency some months ago And if you can see here the top line was the previous version compiled with the Intel Fortran compiler Which in theory is one of the best ones there is the like the reference for all the performance measures With G Fortran, it was a bit slower like I lost 30% of the performance and then you can see what the bottom line is the Python code, which is like Two orders of magnitude is lower than Fortran, which is the expected result and then you have here this Python plus Namba result that is Visibly slower than Fortran, but it's still More or less within the same order of magnitude So I said well, I'm going to throw to the to the trash bin thousands of lines of Fortran a lot of pain and In return I'm going to lose a 70% of Performance that in any case I can optimize later or wait for the technology to build up So this is more or less what I did with these Fortran code Yeah, I was very happy to throw all these away because now the people that know Python Which are much much more than the people that know Fortran can easily contribute to my library I can understand the code ten months later after writing it and Distribution is much easier because I don't need to force everybody in Windows to have a Fortran compiler And in the case who knows what's that? anyway So to give a practical example of this as this talk was presented in the hot topics called for papers I wanted to bring something really really hot Which is the The arrival of the Juno mission To Jupiter the other day if I can press this link. Thank you The Juno spacecraft was a mission that NASA launched in 2011 okay as you can see here and he arrived to Jupiter two weeks ago so it's been a quite a long trip and the trajectory was Pretty involved as you can see there you you have the or the orbit of the earth in August 2011 And the first thing is launching this Juno spacecraft in a very wide orbit that even crosses the orbit of Mars and You always know fuel in all these arcs here And what it's going to do next is going to perform a maneuver over here in the point that is most Far away from the Sun to correct the trajectory and try to encounter the earth in a different point so exactly at that point without losing any fuel it's Using the gravity of the earth to change the trajectory and go to the orbit of Jupiter Well the video goes blah blah blah and when we arrive to the end I Remember I remind you that it was launched in 2011 and in July 2016 these arrived to Jupiter And this is like cosmic billiards because there was The planning of this trajectory involves a lot of man hours and you have to take into account the positions of all the planets and for me it's so beautiful and So what we can do now is reproduce exactly this orbit with polyastro So if I go this to this wonderful, right python notebook Let's do this a bit quickly Okay, so here I'm importing a lot of modules from polyastro which include like the vision of the of the Planets of the solar system the sun some objects to provide an API and here for instance What I'm doing is downloading these files from NASA that I told you before to compute all the positions of the planets I already have them in my computer And here are some data that I got from the internet like the date of launch the velocity of the initial maneuver The date of the flyby of the earth and the date of arrival. So The first thing that I'm going to do is to recover the position and the velocity of the earth in the date of the launch And I can have here a couple of vectors and as you can see this is handling Physical units using astro pi. So if I use these high-level functions that I'm providing with polyastro There is no risk of mixing physical units if I provide a vectoring kilometers and another one in meters Then everything is going to be In order and if I provide some incorrect unit that is going to warn me So I create some state which is going to hold some variables that we need later And I do the same thing computing the position and the velocity of the earth the day of the flyby Okay So then I'm going to use this maneuver objects to say okay now I'm on the earth the day of the launch and I'm going to do the first impulse to get into the first orbit So if I apply the maneuver and I see the period of the orbit this means that the time that it takes To one complete orbit to complete then we see that is Above two years. So the period of the orbit of the earth is obviously one year So now I'm spending two if I plot this thing Then I have the position the orbit of the earth and the first orbit of the spacecraft if I go on doing this propagating and Computing some more velocities and data that I need Then I have not only the position of the earth and the first orbit, but also The point where I'm correcting the orbit to encounter the earth one year later If I go on using the these functions that you can check I'm going to upload all the materials and Plot this as you can see the the API is pretty simple then I have this complete This complete plot of all the segments of the orbit you can see here the orbit of the earth the first segment Then the correction. This is the point of the flyby and then this is the last arc until I go to Jupiter I wanted to stop here because there are some limitations in the API of polyester because for instance I'm plotting all the segments that I don't That I don't travel through for instance like this one So there is a little bit of noise in this plot and also the three dimensional API is not existing yet So I welcome any poor requests So going back to my presentation Well, the conclusions of this is that Python or only works as a language, but it can be fast enough Using some tricks from some purposes and we can optimize it later and improve the readability and everything ecosystem of libraries that we have for some in these kind of problems is Amazing and people is pouring a lot of work into this and It powers a lot of different projects There are several things missing in polyester as I told you before and the good thing is that open development Developing everything on github pouring do good documentation writing tutorials is key for Encouraging collaboration and making this as easy to develop to develop as possible Before finishing my talk, I wanted to explain the title because the it's a Latin catchphrase That used as a motto of the Royal Air Force, which was per ardua ad astra through struggle to space This open source thing. It's many times a struggle. Maybe you have felt it in the past Especially pushing it to businesses and companies So I wanted to touch the duck per Python ad astra to reflect that fact and Also, I wanted to put again the picture of the International Space Station Which is a collaboration between the United States Russia China Europe and many other countries which for me means that even through political differences and historical differences We can collaborate to build great things. So, thank you very much. Keep dreaming. Don't lose your curiosity and thank you Thank you for a very very nice talk Do you have any question? Yes This was awesome. Thank you And I want to say I have a question I read once that when they we went to the moon, they were using it that if you looked into the source code They were using six decimal places for pie Which was funny because usually we try to use like a lot and no, you don't need that many and you can put people there So could we use this thing to send people to the moon? No, and I'm going to explain why well the first is that Contrary to popular belief, you don't need that many decimal places for pie And if you use like 10 then you can approximate the circle of the universe to the size of the human hair or something like that It's ridiculous. The thing is that with polyastro. I'm taking into account only this problem that is like only assuming that my body is very small and also with the Lumber problem that is calculated directly from point up from point to point B And you see I'm only taking into account one body of gravitational attraction And when you are going from the earth to the moon, you cannot do that because the moon is very big it's very close to the earth and in all The trajectory you have to take into account both bodies. So for now we cannot use it to go to the moon But we can go to Mars The moon is very boring there's nothing there But well, do we have other questions? Yes You recreated Juno's trajectory, which is great. Sorry, can you can you repeat again? You recreated you know, you know, try About you know trajectory. Yes, I don't I don't think I have a picture here Yes Here I was wondering How much how much time did it take to recreate it? Sorry, I didn't understand How much time how how long did it take to recreate it to recreate it? Yes to do this. I did it in real time, right? Right now Like I just did it I didn't have any anything well I calculated the the notebook an hour before but I restarted it So I'm computing everything on the fly like all the algorithms like going from point A to point B They are extremely fast now and the complete the history like in real life is taking like five years or something or six years Are there questions? Yes My question is if from point A to point B is super fast What would be a challenge on computing or on whatever for this library? Yeah, the thing is that well, I didn't say it I think but for many practical problems You have to compute these solutions thousands of times for instance when you want to optimize an orbit and say okay I'm doing to try this billiards thing for instance There was a contest some months ago and there were solutions like I don't know go one fly by on the earth Then Venus then Mars and then Jupiter or there are many combinations as well You can imagine that there are many many combinations and you can you have to compute these solutions thousands of times So even if this is very fast to do it once Then if you start adding up and competing this a lot of times, then it's critical to have a good performance other questions I Have a question myself because I didn't exactly know what you were presenting but I Tested polyester because I'm doing Calculation with all real dynamics, but with satellites low earth satellites. Okay. Are you going to add the J2 term? someday no, no and I tell you why because this is This is going to be optimized for Interplanetary trajectories so for low earth orbits You have to take many things into account like the thing that the earth is not a sphere, but it's like something like a pier Very strange and also the pressure of the Sun because the Sun Pushes you when you are in orbit and you can actually feel the light like displacing you so I don't think I'm going to add those but we have a parallel project with which Hopefully we will try to Be more suitable for near earth objects. So do you know Any Python library that can be used for lower orbit? Well, you have for example the library from Brandon rules. Yes. Yes, you can at least you can compute the SGP for propagation model Which takes into account the orbital drag and stuff like that So it's pretty accurate for most things like for calculating when some piece of space debris is going to hit us in our heads Other questions, okay, if this is not the case, please