Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Getting Started with C++ (for N-Body Simulations)

  1. Oct 11, 2016 #1
    Hello all. I don't really know what to exactly ask from any of you other than how I should get started with C++ programming to hopefully create N-body simulations down the road. How to exactly start, i'm not entirely sure.

    For a little about what I know, I know python extensively. I use it almost everyday for data analysis in my research and I can create programs that coincide with numerical computations. But that is just about it.

    I have some knowledge on how C++ works, but not concretely (ie parallel programming??). Python can be simple in the case of just downloading and importing packages like numpy, scipy, or astropy to do data analysis, but for C++, i have no idea where to start on this (my apologies for reiterating this). In particular, I'm interested in devising N-body simulations and everything implemented with it (numerical solving and visualization). I have Hockney's "Computational Simulations using Particles" that goes over the theory, but all I have left is to just apply it.

    How should I go about achieving my goals? What should I look into? What should I download.
  2. jcsd
  3. Oct 11, 2016 #2


    Staff: Mentor

    My first question is why have you chosen C/C++ as the language of choice?

    There are many good references on N-body simulations that use Java and Python. Are you interested in "speed" of execution or is it because of some class requirement?

    Assuming that language is not really the issue then I would suggest looking at Open Source Physics. Its a collection of java classes for writing complex simulations such as N-body simulations. It comes with several ODE solvers, and many 2D and 3D graphical plotting classes. THe authors have also written a book covering a lot of simulation material including and going beyond N-body.


    and the book


    If instead you want ot use Python then the Anaconda version has numpy, and scipy included:


    for directions on how to install and use it.

    Alternatively you could consider using julia (see the above link) which is a MATLAB like clone as the programming language of choice


    Lastly, you could look as Processing.org, a java language environment with extensive graphics libraries, examples and tutorials to get you up and running with Java. Some examples could be adapted to the N-body problem if they don't simulate it already.

    Processing makes the power of OpenGL available to you via the java language so give it a try.

  4. Oct 12, 2016 #3


    Staff: Mentor

    To add to my previous post, Processing has a contributed library called PixelFlow which has some fantastic particle system examples which you should definitely checkout.

    His github page has several videos to check out:


    The source code is here:


    to run any examples:
    - download processing,
    - install it and
    - from the Sektch>>Import Libraries...>>Add Library option,
    - select this library to install it.
    - then File>>Examples>>PixelFlow select an example.

    One example, I've run recently is Softbody2D_ParticleSystems which interacts with the mouse and sloshes a bunch of variously sized particles around.
  5. Oct 15, 2016 #4
    You need to first make a trivial version where particle are a point class with
    class particle
    Vector3 speed
    Vector3 position
    double mass

    then with two loop on the particle vector you can compute gravitationnal force and then integrate acceleration to move the particle.
    For the visualisation OpenGl is well defined. After this you must use an Octree to accelerate the computation time. It's more complex but faster.
    Octree can be parallelised on multiple CPU by using OpenMP or on multiple computer by an handcoded parralel computing system.
    It need time, this is the only secret.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Getting Started with C++ (for N-Body Simulations)