How can I optimise my N-body Simulation code further?

Click For Summary
SUMMARY

The discussion focuses on optimizing a brute force O(N^2) N-body simulation code written in Python, which calculates gravitational trajectories for 10,000 to 30,000 bodies. The user has already implemented Cython for performance improvements and utilized MPI for parallel processing but seeks further optimization strategies. Key components of the code include calc2.pyx for acceleration and leapfrog integration, and Nbtest_2.py for running the simulation. The user is looking for actionable advice to enhance performance beyond current implementations.

PREREQUISITES
  • Understanding of N-body simulations and gravitational systems
  • Familiarity with Python and Cython for performance optimization
  • Knowledge of MPI (Message Passing Interface) for parallel computing
  • Experience with algorithmic complexity, specifically O(N^2) performance issues
NEXT STEPS
  • Explore spatial partitioning techniques to reduce computational complexity
  • Investigate the Barnes-Hut algorithm for efficient N-body simulations
  • Learn about GPU acceleration using CUDA for Python
  • Research optimization techniques in Cython, such as memory management and type declarations
USEFUL FOR

Researchers, developers, and students working on computational physics, particularly those focused on optimizing N-body simulations and improving performance in Python-based applications.

Chaoyi Zhang
Messages
1
Reaction score
0

Homework Statement


I wrote a brute force O(N^2) N-body code in python and I am not happy with its performance despite the fact that I have cythonised the calculation heavy parts as well as implemented MPI. I am currently out of ideas for how to optimise it further, can I get some advice please?
Edit: the code solve gravitational systems with very large N (10k - 30k) bodies by calculating the trajectories of all the bodies. By brute force, I mean direct summation, summing the force from each particle at each timestep, moving the particles then repeating the process.
Here's a link to my code: https://1drv.ms/f/s!Aks13DdO4pS0jAfWncIFbjg07HpR

calc2.pyx is the acceleration and leapfrog integration calculation written with cython

Nbtest_2.py runs the simulation

setup_calc2.py cythonises calc2.pyx

Homework Equations

The Attempt at a Solution

 
Last edited:
Physics news on Phys.org
Welcome to the PF. :smile:

Is this really homework? Seems like it would fit better in the Programming forums...

Also, I didn't click into your code, but if it isn't too large, you can use the code tags to post your code here at the PF. Like this but without the spaces inside the brackets:

[ code=Python ]
<< insert your code here >>
[ /code ]
 

Similar threads

  • · Replies 19 ·
Replies
19
Views
4K
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
7
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K