For my modelling and simulation class project, I want to simulate a solar system. I'm starting with just a star (sun) and a planet (earth), but I'm running into a few problems already. I've spent some time now just reviewing and learning about different formulas and way to simulate how the planet's orbits will be affected by the star and surrounding objects. I want to use velocity verlet and eventually look into the n-body problem. I'm having numerous issues with my velocity verlet function. Sometimes it acts as if it's making earth orbit normally and then it " warp drives" earth off into some random location. I've also noticed I never get a "negative" acceleration, so my x and y coord. are always increasing, so I don't see how the earth is suppose to wrap back around the sun. Any help is greatly appreciated. The AGK::Prints I have just so I can see how the different variables are changing.
double velocityVerlet(float positionCalc, double position2,
float &velocity, double massCalc, double mass2)
//positionCalc is the position being updated, position 2 is position of
// other object, same with mass
{
float force = forceFunc(positionCalc, position2, massCalc, mass2);
agk::PrintC("Force is: ");
agk::Print(force);
float acceleration = accelerationFunc(force,massCalc);
agk::PrintC("Accel is: ");
agk::Print(acceleration);`;
double newAccel = 0;
positionCalc = positionCalc + velocity*dt +
(.5*acceleration)*pow(dt,2); //calculates new position
agk::PrintC("New Position is: ");
agk::Print(positionCalc);
force = forceFunc(positionCalc,position2,massCalc,mass2);
newAccel = accelerationFunc(force, massCalc);
velocity = velocity + .5*(acceleration + newAccel)*dt; //new velocity
agk::PrintC("Velocity is: ");
agk::Print(velocity);
return positionCalc;
}
There are some problems with the physics and some problems with the code.
First, the problem with the physics. Assuming that we're not modelling an alternative universe where the laws of physics are different, Newton's law of universal gravitation says that F=G*m1*m2/(r*r). However, force is a vector, not a scalar, so it has both magnitude and direction.
What the code calculates in
forceFuncX
is actually the magnitude rather than merely the component of the force parallel to the X axis.forceFuncY
has the same flaw.Next is the calculation for acceleration. Physics says that this is a=F/m. Mass is a scalar but both acceleration and force are vectors. So to calculate a_x, we could either use F_x/m or we could calculate F*cos(a)/m. Because cos(a) (with a being the angle from one
CelesitalObject
to another in 2D space) = dx/r, we can make this a_x = F*dx/(m*r) which is almost but not quite what you've got in your calculation (it's missing r in the divisor).An alternative approach is to use
std::complex
but I'll not show that approach with the assumption that you may wish to extend this model to three dimensions.That brings us to problems with the code. First, since you're using C++ and writing a simulation of a physical system of discrete objects, it makes sense that you've defined a
CelestialObject
class. What makes less sense is that your functions are called by picking out individual pieces of those objects and then calling C-style functions. We can improve the code by using those objects better. To start, since you haven't posted one, here's aCelestialObject
class based on the interface I inferred from your code:Next, a few helper functions:
Finally the actual verlet:
And finally some simple test code.
The test code uses a time step of 0.1s which is far too short for your solar system but fine for this quick test which is to see if we get a reasonable result for a known system. In this case, I've chosen a two-body system consisting of the planet Earth and an anvil. This code simulates dropping an anvil from the top of a 370m building which we can easily calculate will hit the ground in about 8.7s if we neglect air resistance. To keep the coordinates simple, I chose to place the origin (0,0) at the surface of the earth, and to consider the top of the building to be at (370,0). When the code is compiled and run, it produces the following:
As you can see, this seems to work, but there are problems. The first problem is that since the objects should only move along the X axis, all Y components should be 0. They are not because this code is not very well-designed from a numerical analysis point of view. Doing additions and subtractions of floating point numbers when one number is large and another is small is one problem. Another is the use of the
atan2f
function which returns only afloat
and then using that result incos()
andsin()
. Trigonometric functions are actually best avoided if possible.Finally, this program currently only works with two objects. Adding a third would be painful with this kind of scheme, so a better design would be to process a
std::vector<CelestialObject>
by first calculating the net force on each object by considering the positions and masses of all of the others. I'll leave that to you, but this should at least give you a start in the right direction.The facts that your integrator accepts scalars and that your question is about 2-dimensional system makes me think that you are calling the integrator twice, once for each component. This simply won't work since your system will be taking unrealistic moves through the phase space. The integrator works with vector quantities:
Here X(t) is a column-vector that consists of the coordinates of all particles - this is the configuration subspace of the system's phase space. V(t) is a column-vector of the speeds of all particles, technically representing the momentum subspace. The same applies to A(t). Those have to updated simultaneously, not separately.
The whole velocity Verlet procedure translates as follows in code for force fields that do not depend on the speed (e.g. classical gravity):
Note that all positions are updated first before the next round of force evaluation. Also it is only necessary to evaluate the forces once per iteration since the positions do not change during the second update to the velocities. In the example code above
Vector
is a class that implements an n-dimensional vector and holdsn
components (e.g. 2 in your 2d-case). It also overloads the+
and+=
operators to implement vector (component-wise) addition, as well as*
and/
to implement multiplication / division by a scalar. This is just to illustrate the case and can be replaced by inner loops over the components of each position/velocity vector.The correct choice of time step is very important. Too small time step will slow down the simulation significantly. Too large time step will result in impossible physics, e.g. jumping planets.