 Welcome back, in the last segment we discussed the second order method for the cosmological problem and also an algorithm based on it. So before the program let us make a few remarks, so as always in order to write the program we should first try and enumerate the important entities. Now in this program, this program is all about positions, velocities, accelerations and these are not entirely simple quantities, so they are actually three dimensional vectors and since they are important entities we should use a class for them and luckily we did develop such a class which was our V3 class which we developed sometime earlier. So that is exactly what we will use, so instead of thinking of it as an array of three elements or something like that we will use our V3 class and if you remember our V3 class was developed so that we can add two vectors together by writing plus or if you could multiply a vector by a scalar just by writing star. So that will come in very handy and that will make our code very easy to understand and everywhere we would not have to write down looping over the three components. We can just say u plus v if we want to add vectors u and vectors vector v. Then there were the other, the major entities from a different point of view I guess are the stars. Well the stars possess the position, velocities and acceleration but the stars are important entities. So clearly we should have a struct. Now we could create a struct for the entire galaxy as well, so that struct would just contain the array of stars but here we have chosen not to do this, we will discuss this a little bit later when you have seen the entire program but what we have said so far is that the entities sort of the physical entities are the stars and inside the stars we will have v3 entities which will represent the positions, velocities and accelerations of the stars. What are the important actions that the program needs to take? Well first of all of course the program has to read in data then it has to calculate the force due to stars on each other. So this will involve all stars then it has to update the position. So what do we need in order to update the position? Well we should change the position of each star individually and for that we need the velocity of that star. So this is a bit of a local operation if we know the velocities of the stars and similarly we have to update the velocities so this is also a local operation because if we know the accelerations then the new velocity of a star it depends on its old velocity and its acceleration but these will be important actions that we are going to perform and we want to show all this on our canvas. So we should actually move things around as well. So that is another important action. So this suggests that suggests what functions we should have because we said at some point that if something is an important action then we should make it into a function or a member function. So we will almost do that some of these if something happens to be really tiny then maybe we will just write it directly rather than making it into a function but yes in general if something is important we should sort of make it have a name in our program and the way to do that is by making it a function or a member function. And then of course there is the main program we have to say what happens in the main program. Well the main program sets everything up and it also loops over the time steps. So in that sense the main program is kind of a controller, kind of a choreographer if you will. So it sort of sends messages to everyone saying now we do the next step of the iteration now that it is over do the next step and next step and so on. So that is what our program is going to be like basically it is going to follow this outline. Alright, so I am now going to show you the program I am not going to so yeah so I am going to show you the program and I will begin with the main program I will talk about the function which reads the data then the function that calculates the forces I will show you the star class the class V3 and while doing this I would like you to observe that really the whole thing corresponds to the algorithm that we have discussed. Alright, so let me get out of this. So here is our main program so we begin by creating the canvas on which everything is going to be shown and then we read in the duration of the simulation the step size and the number of stars and then we have a function which will read in the star related information so this is the function. So it does not do a whole lot of things it will read in the values the mass x y and z the positions and the velocities. So these are the initial things that it is going to read and we are going to have a star array so this is it is going to be read into the star array and we are going to initialize each element of that star array using these values. So these three values we are going to convert into a position vector by calling this constructor if you remember this is the constructor for V3 so this is giving the position vector this is giving the velocity vector and this is the mass. So it is going to initialize each star so let us quickly take a look at that because there is something that happens in addition over there. So here is that initialization so m, v3, r1, v3, v1 so these are the positions and velocities so they are going to be put inside the corresponding members so v and r are the velocity and position of this star so these are the data members and mass is also there so m is going to go into mass but we want everything to be seen on the screen as well. So this is not a constructor this is kind of a reset like thing so it is the initialization thing so in this initialization we will actually make this circle appear on our screen by resetting it and where should it go well it should go at position x and y where x and y are the positions of the star so the way we have done it is that we have used we are pretending that our stars are moving in sort of this pixel space. So after all this is only for a demonstration and so we are not doing any kind of a change of coordinates over here so this is all pixel space so pixel coordinate x coordinate we get pixel y coordinate we get and over there we are going to show the star so that star is going to appear as a circle and we are going to make that circle be red and filled and we will have a pen for it which will go down and it will go down because we want the orbit to be seen as the circle as the star moves. So yeah so this is initializing the data part as well as initializing what happens on the screen. So we read the data that is over here and next we are going to calculate the forces so how do we calculate the forces well for that we need the information of all stars and that is we pass it the array stars and we also need to calculate the result in some array called forces so that is what is going to happen over here. So in the array forces we will calculate the forces so how does that happen so it is as you might expect so we are going to go through every pair of stars and we are going to calculate the mutual force so how is the mutual force calculated well we have this function this member function on a star object so what this is saying is that tell me the force due to star due to star i on star j so and tell it to me as a vector or rather tell me the force on star i due to star j and that force we are going to add to the total force on i and we know that by symmetry or by Newton's third law exactly the equal force will be exerted on star j and so we are going to subtract from it and notice that these are vectors remember that these are vectors and this is also a vector and the result of this calculation is also a vector so this calculation will produce vectors we are we have overloaded the plus and minus operators we will see that and you already saw that in an earlier lecture so that we can just directly add the incremental contribution due to star j on star i and on star star i due to star j so this is the incremental contribution and exactly where does Newton's law of gravitation come in well let me show you so this is where it comes in so this r is the distance but distance expressed as a the distance expressed as a vector so this is r j so the other minus r i if you look at our formula and that is the vector distance and that is so our formula says it is the vector distance times the gravitational constant which we are taking as 1 here and times the mass times the 2 masses divided by the power of the distance to the cube because we already have a distance term over here in the vector in the vector part so this is again exactly like what we had in the slides so we have calculated the net force so going back to the main program so once we have the net force why did we calculate the net force well if you remember we need to have the velocities at time step delta by 2 so if you remember when we started things off we wanted the positions at time step 0 and velocities at time step delta by 2 so we got that by a first order update so what was that first order update well we are going to take the original velocity of each star and to that we are going to add acceleration times delta by 2 but here we have forces so in this member function will convert the forces into accelerations and add them so let us see that so there is this velocity step that I am going to show you so v step says that you had an original velocity to it you are going to add the force divided by mass which is the acceleration and whatever time displacement you want okay how much how much ever you want to advance so in this particular call we are advancing everything by delta by 2 because we started off with all the data at 0 and we wanted the velocities to have been calculated for delta by 2 so this gives us at the end of this will have velocities at t equal to 0.5 delta and positions will be at t equal to 0 okay because that is what we started off and we have not modified the positions then there is the main loop okay so how long does the loop run well exactly as before it goes from 0 to t upon delta okay and increment of delta so here we are going to calculate okay we are going to update the position okay so this is this update is what we are calling an r step and this time we are going to advance the position by the full delta okay so therefore the argument that we give to this is delta so let us see this r step so we are passing the duration and that duration is going to be multiplied by v well what is v so v is just the velocity of this star okay so remember the velocities are kept along with the stars so see this as a part of the star the velocities are kept there itself okay so in the v step this velocity that is there with the star we are taking and we are getting the acceleration and that is got by f divided by mass and then we are multiplying it by the duration okay in this case delta was passed so this is just saying a times delta okay v equal to v plus a times delta that is what this is so we have come to this point we have updated the the the positions the the the positions and we have updated the velocities and yeah so before we update the velocities we need to of course calculate the forces so it is exactly the same force calculation function okay and we updated the velocities and then this iteration has to happen as many times as needed so if you want to advance time by t it has to happen t by delta times okay so that is it so at the end of it we are just putting a get click over here just so that the at the end the display does not vanish right away so the vector class is pretty much like what we had before except that we have added a minus because we had we had to subtract two vectors so therefore we have added this okay and oh we have so ignore this line this line is not really needed okay this is somewhat complicated this is this is described in the book and this is a way to allow the class to be printed but don't worry about it I should I should really remove this line okay alright so that is the vector class and so now you can see that we have the vector class we have the star class and we have these two functions and we have the main program okay so let us compile it and we are supposed to type in all the data about the stars so let me show you two pieces of data that I have so one piece is the model of a sun and the earth so let me type that out first okay so what has happened over here is that I am asking time to go up to 30,000 okay and my step size is 10 so this is the mass these three things are the initial position and these three things are the velocities okay so one is 400 one has one has mass 400 times the other so really you can think of this as the sun and this as the earth if you like okay okay so let me run this and I want to take this as my input so I will just redirect redirect this file as standard input and let us see what happens so that is the sun and this is the earth which is rotating around it okay and you will see that it traces pretty much the same orbit it does not really diverge so despite numerical errors in numerical calculations and things like that this is pretty good okay so yeah I could have put in more planets in fact in the demo that I had showed that on the very first day of class you have this exact this exact program was used and over there I had many planets orbiting the sun okay so you can go back and look at that demo as well but here today I want to show you something else which is quite interesting so let me show you the data first okay so here here I have 30,000 steps again with 10 being the step size and there being three bodies okay so the coordinate they are all of mass hundreds so they are basically all equal and these are the coordinates and velocities now the positions and the velocities have been calculated very cleverly okay so this is what some astronomers did this is this has been taken from the internet okay so these positions and velocities have been taken from the internet and these were calculated by some astronomers and the interesting thing about these positions and velocities are that you will see that the attractive force is such that the bodies will describe a very strange looking orbit okay so let us see that so I am going to give it this three body data okay so this is what this is what the stars do as they are moving under gravitational force isn't it strange that they should that first of all three bodies should trace such an orbit they are sort of following each other but you can see that they are actually that they may be doing this because of gravitation okay because just think about say this at this point this thing which is in the middle is being dragged by the two others and therefore they are staying staying in this okay so this is a curiosity but it goes to show that our code is actually correct this is behavior that should be expected for these initial velocities and positions and masses okay so what did we discuss in the last segment well we discussed a program based on the second order method and in the next we are going to conclude but we will take a break before that