Galaxy simulation with the velocity Verlet algorithm

In summary, the conversation discusses a programming project involving simulating the trajectories of solar systems around a black hole using C++ classes. The algorithm for the simulation involves evaluating initial acceleration and then using an auxiliary variable to calculate changes in position and acceleration. The conversation also mentions changes made to the units used in the simulation. A gif is provided to demonstrate the behavior of the system.
  • #1
Adec
3
2
Homework Statement
Simulate a galaxy in C++ using the velocity Verlet algorithm as the integration method
Relevant Equations
Newton's laws of motion, velocity Verlet
To simulate the trayectories of solar systems around a black hole (i.e. a galaxy) I have 3 classes in C++: cSystem, cBlackHole and cGalaxy. cSystem assigns initial values of position, velocity, etc to a solar system. cBlackHole does the same but just for the black hole. And cGalaxy mixes cBlackHole with an array of cSystem, it's where the algorithm works (as a method/class function)

I've implemented the algorithm as I was told:
(0) Give initial random positions and velocities
(1) Evaluate the initial acceleration $$\mathbf{a}_i=-G \sum_{j \neq i} \frac{m_j (\mathbf{r}_i-\mathbf{r}_j)}{|\mathbf{r}_i-\mathbf{r}_j|^3}$$
(2) Evaluate the change in position, and use an auxilary variable "w":
$$\mathbf{r}_i(t+h)=\mathbf{r}_i(t)+h\mathbf{v}_i(t)+\frac{h^2}{2}\mathbf{a}_i(t)$$
$$\mathbf{w}_i=\mathbf{v}_i(t)+\frac{h}{2}\mathbf{a}_i(t)$$
(3) Evaluate the change in acceleration using the new positions (see formula in (1))
(4) Evaluate the change in velocity
$$\mathbf{v}_i(t+h)=\mathbf{w}_i+\frac{h}{2}\mathbf{a}_i(t+h)$$
(5) t=t+h, go back to (2).

However, even when I use only 1 solar system and give it its orbital velocity, it ends up flying away, or collapsing into the black hole. So, my inclination is that the algorithm is the thing that is failing (but it could be other thing).

Here's the algorithm in my program (comments are in Spanish, ignore them if you don't understand it):
Algorithm:
//Método de aplicación del algoritmo de verlet a la galaxia completa
void cGalaxia::verlet(cSistema (&Solar)[numsist], cAgujero Negro,
 double (&ax)[numsist+1], double (&ay)[numsist+1], double h)
{
    //Declaración de variables
    double wx[numsist+1],wy[numsist+1],xaux,yaux,denom, xauxBH, yauxBH, denomBH;
    int i,j;

    //Posiciones, y velocidades auxiliares
    for(i=0; i<numsist; i++)
    {
        //Cambio de r(t) a r(t+h)
        Solar[i].setPosX(Solar[i].getPosX()+(h*Solar[i].getVelX())+((h*h/2.)*ax[i]));
        Solar[i].setPosY(Solar[i].getPosY()+(h*Solar[i].getVelY())+((h*h/2.)*ay[i]));
        //Velocidades auxiliares
        wx[i]=Solar[i].getVelX()+((h/2.)*ax[i]);
        wy[i]=Solar[i].getVelY()+((h/2.)*ay[i]);
    }
    //Para el agujero negro
    Negro.setPosXBH(Negro.getPosXBH()+(h*Negro.getVelXBH())+((h*h/2.)*ax[numsist]));
    Negro.setPosYBH(Negro.getPosYBH()+(h*Negro.getVelYBH())+((h*h/2.)*ay[numsist]));
    wx[numsist]=Negro.getVelXBH()+((h/2.)*ax[numsist]);
    wy[numsist]=Negro.getVelYBH()+((h/2.)*ay[numsist]);
    

    //Aceleraciones
    for(i=0; i<numsist;i++) //Evalúo cada sistema
    {
        ax[i]=0;ay[i]=0; //Inicialización aceleraciones
        for(j=0;j<numsist;j++) //Evalúo la interacción con los demás sistemas
        if (j!=i && (abs(Solar[i].getPosX()-Solar[j].getPosX())<5)
        && (abs(Solar[i].getPosY()-Solar[j].getPosY())<5)) //Cuadrado de 5x5 para no hacer
                                                           //calculos inútiles, solo influencia cercana
        {
            //Variables auxiliares
            xaux=Solar[i].getPosX()-Solar[j].getPosX();
            yaux=Solar[i].getPosY()-Solar[j].getPosY();
            denom=pow(((xaux*xaux)+(yaux*yaux)),1.5);

            //Aceleraciones
            ax[i]-=G*Solar[j].masa()*xaux/denom;
            ay[i]-=G*Solar[j].masa()*yaux/denom;
        }   
        //Añado la contribución del agujero negro a cada sistema i
        xaux=Solar[i].getPosX()-Negro.getPosXBH();
        yaux=Solar[i].getPosY()-Negro.getPosYBH();
        denom=pow(((xaux*xaux)+(yaux*yaux)),1.5);
        ax[i]-=G*Negro.masaBH()*xaux/denom;
        ay[i]-=G*Negro.masaBH()*yaux/denom;
        //Calculo la aceleración del agujero negro
        xauxBH=Negro.getPosXBH()-Solar[i].getPosX();
        yauxBH=Negro.getPosYBH()-Solar[i].getPosY();
        denomBH=pow(((xauxBH*xauxBH)+(yauxBH*yauxBH)),1.5);
        ax[numsist]-=G*Solar[i].masa()*xauxBH/denomBH;
        ay[numsist]-=G*Solar[i].masa()*yauxBH/denomBH;
    }

    //Velocidades
    for(i=0;i<numsist;i++)
    {
        Solar[i].setVelX(wx[i]+((h/2.)*ax[i]));
        Solar[i].setVelY(wy[i]+((h/2.)*ay[i]));
    }
    //Para el BH
    Negro.setVelXBH(wx[numsist]+((h/2.)*ax[numsist]));
    Negro.setVelYBH(wy[numsist]+((h/2.)*ay[numsist]));
    
    return;
}

I've also changed the units of some variables, for the sake of productivity. I've used positions between 0 and 100 kilolightyears (the typical width of a galaxy), time is in mega years, and mass is in solar masses; for that I've also used $$G=6.351695379\times 10^{-10} \ \frac{kly^3}{M_{\odot} My^2}$$

You can see the full program by clicking here.
Here's a gif showing the behaviour of the system (made with gnuplot): gif
 
Physics news on Phys.org
  • #2
Adec said:
However, even when I use only 1 solar system and give it its orbital velocity, it ends up flying away, or collapsing into the black hole. So, my inclination is that the algorithm is the thing that is failing (but it could be other thing).
Since the initial positions and velocities are random, this does not surprise me. The conditions have to be just right to have closed orbits.
 
  • #3
DrClaude said:
Since the initial positions and velocities are random, this does not surprise me. The conditions have to be just right to have closed orbits.
I'm supposed to give them random velocities as well. But, just for the sake of checking, I'm giving them the orbital speed (not random at all), and I do not get a circular orbit. Therefore, there has to be a problem.
If you read the code, line 87, there's the formula
$$v=\sqrt{\frac{GM}{r}}$$
Which is the speed that I'm using for every system, given its (random) position r.
That is why I said
Adec said:
However, even when I use only 1 solar system and give it its orbital velocity, it ends up flying away, or collapsing into the black hole.
 
  • #4
I've already solved the problem. In line 351 I called "aceleracionx" twice instead of using the y component, now I get the circular orbits perfectly! :D
 
  • Like
Likes DrClaude

1. How does the velocity Verlet algorithm simulate galaxies?

The velocity Verlet algorithm is a numerical method used to simulate the motion of particles in a system. In the context of galaxy simulation, the algorithm calculates the positions and velocities of each galaxy over time based on their initial conditions and the forces acting upon them, such as gravity. By iteratively updating the positions and velocities of the galaxies, the algorithm can simulate the movement and interactions of the galaxies in a realistic manner.

2. What makes the velocity Verlet algorithm a suitable choice for galaxy simulation?

The velocity Verlet algorithm is a second-order integrator, meaning it is more accurate than simpler first-order methods. This is important for galaxy simulation as it allows for more precise calculations of the complex forces and interactions between galaxies. Additionally, the algorithm is symplectic, which means it conserves energy over time, making it a more stable choice for long-term simulations.

3. Can the velocity Verlet algorithm be used to simulate different types of galaxies?

Yes, the velocity Verlet algorithm can be used to simulate a variety of galaxy types, including spiral, elliptical, and irregular galaxies. The algorithm is versatile and can be adapted to different initial conditions and force calculations, making it suitable for simulating a wide range of galactic systems.

4. How does the velocity Verlet algorithm handle collisions between galaxies?

When galaxies collide in a simulation, the velocity Verlet algorithm calculates the forces between them and adjusts their positions and velocities accordingly. However, the algorithm does not account for the physical effects of the collision, such as gas dynamics or stellar evolution. These effects would need to be added separately to create a more realistic simulation of galaxy collisions.

5. Are there any limitations to using the velocity Verlet algorithm for galaxy simulation?

Like any numerical method, the velocity Verlet algorithm has its limitations. It is most accurate when the time step used for calculations is small, but this can also make the simulation time-consuming. Additionally, the algorithm may struggle with simulating highly chaotic or unstable systems, such as galaxies with extremely close encounters or mergers. In these cases, alternative methods may be more suitable for accurate simulations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Electromagnetism
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
26
Views
3K
  • Special and General Relativity
Replies
13
Views
1K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
Replies
20
Views
678
Back
Top