<p>In my n-body simulation I would like to color the particles in way which corresponds to the magnitude of the net force acting on them. </p>

<p>This is so that I can confirm visually that the particles closer to the center of a galaxy have higher accelerations (so for example particles near the rim of a galaxy are lighter colored, and the core is red).</p>

<p>Let P be a particle. If F is the magntitude of the net force acting on P, the idea is to calculate s=F/F_max where F_max is the maximum possible force between two particles (which exists since my simulation uses gravitational softening). Since s is a number between 0 and 1, I can associate to P an HSV color by fixing saturation and value, and setting hue=s.</p>

<p>The issue: because the gravitational force scales as 1/r^2 the mapping of F to s is not linear. The result is that, when their are few particles (N&lt;50), the color of a particle seem to change only if it becomes locked in a binary (in a bound system of two particles which are very close to eachother). The binaries are easy to identify because they blink (default color when they are at maximal distance, another color when they are at minimal distance).</p>

<p>For N=10,000 the result is pretty at least, but I'm not sure how realistic it is.</p>

<p><a href="https://i.sstatic.net/rsQtH.jpg" rel="noreferrer"><img src="https://i.sstatic.net/rsQtH.jpg" alt="enter image description here"></a></p>

<p>So how should I map the force to a color in a realistic way? My goal would be to get something more like this:</p>

<p><a href="https://i.sstatic.net/tqwVR.jpg" rel="noreferrer"><img src="https://i.sstatic.net/tqwVR.jpg" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-09-23T16:18:26.253Z
Can you explain a few things? What's your color scale? Are you using the full hue range? (For example 0° = red, 60° = yellow, 120° = green, 180° = cyan, 360° = red?) Also, what makes you think what you're seeing isn't correct or on the right track? Do you know how many particles were used in the "goal" picture? Given what you said about the colors for small `N`, it seems feasible that by using a larger `N` you might get coloring more like the goal. But I'm just speculating.
user1118321
CC BY-SA 3.0
2017-09-23T19:12:45.643Z
I am using HSV colors where each parameter is between 0 and 1. I fix S and V to be 1 and H corresponds to the magnitude of the force. Then I convert HSV colors to RGB and multiply each parameter by 255 because pygame display only accepts RGB colors in this format.
math_lover
CC BY-SA 3.0
2017-09-23T19:15:19.577Z
What I mean is are you using the full Hue range from 0° to 360°? If so, you might want to limit it to either 0-180° or 0-270°. Otherwise, things with very large force look the same as things with very little force, since the hues wrap around at 360°.
user1118321
CC BY-SA 3.0
2017-09-23T19:15:21.003Z
@user1118321: you might be right that the colors in my sim look weird because I don't have the same conditions as in the "goal" sim. We are both using 10,000 particles, but my initial conditions are randomized positions and velocities, which will never produce a galaxy like the one shown above. We need to give the system some initial angular momentum to get a rotating galaxy. So I will see what happens by using proper initial conditions. I was just wondering if there was anything else I should be considering w.r.t the color scaling, or anything I'm doing that is obviously wrong.
math_lover
CC BY-SA 3.0
2017-09-24T17:53:39.803Z
@user1118321: as I said, I use HSV colors where each parameter is from 0-1 instead of 0-360. If I set S and V to 1, and let H vary between 0 and 1, there is not this problem of wrapping around. H=0 gives red while H=1 gives green.
math_lover
CC BY-SA 3.0
2017-09-24T18:24:25.960Z
OK cool. I'm used to positive Hue going from red through yellow, to green, but I saw you had cyan, blue, and magenta in your image, so I was confused. But I think I get it now.
user1118321
CC BY-SA 3.0
2017-09-27T10:12:09.300Z
What do you mean by "properly" or "realistic"? It's not at all realistic to colour things according to the force acting on them. There are many choices of colour map and the answer depends on what your goal is for using colour.
Dan Hulme
CC BY-SA 3.0
2017-09-27T10:54:45.613Z
@DanHulme: By realistic I mean a realistic representation of the forces acting on the bodies. I want to be able to see some sort of gradation where the force gradually increases as we move closer to the center of the galaxy, like the second image shown.
math_lover
<color>
How to properly color particles in N body sim?
math_lover
# How to properly color particles in N body sim?

(Asked by: username_0 on 2017-09-23)

In my n-body simulation I would like to color the particles in way which corresponds to the magnitude of the net force acting on them.

This is so that I can confirm visually that the particles closer to the center of a galaxy have higher accelerations (so for example particles near the rim of a galaxy are lighter colored, and the core is red).

Let P be a particle. If F is the magntitude of the net force acting on P, the idea is to calculate s=F/F\_max where F\_max is the maximum possible force between two particles (which exists since my simulation uses gravitational softening). Since s is a number between 0 and 1, I can associate to P an HSV color by fixing saturation and value, and setting hue=s.

The issue: because the gravitational force scales as 1/r^2 the mapping of F to s is not linear. The result is that, when their are few particles (N<50), the color of a particle seem to change only if it becomes locked in a binary (in a bound system of two particles which are very close to eachother). The binaries are easy to identify because they blink (default color when they are at maximal distance, another color when they are at minimal distance).

For N=10,000 the result is pretty at least, but I'm not sure how realistic it is.

[![enter image description here](https://i.sstatic.net/rsQtH.jpg)](https://i.sstatic.net/rsQtH.jpg)

So how should I map the force to a color in a realistic way? My goal would be to get something more like this:

[![enter image description here](https://i.sstatic.net/tqwVR.jpg)](https://i.sstatic.net/tqwVR.jpg)

**Tags:** color

> **Comments:**
> **username_1**: Can you explain a few things? What's your color scale? Are you using the full hue range? (For example 0° = red, 60° = yellow, 120° = green, 180° = cyan, 360° = red?) Also, what makes you think what you're seeing isn't correct or on the right track? Do you know how many particles were used in the "goal" picture? Given what you said about the colors for small `N`, it seems feasible that by using a larger `N` you might get coloring more like the goal. But I'm just speculating.  
> **username_0**: I am using HSV colors where each parameter is between 0 and 1. I fix S and V to be 1 and H corresponds to the magnitude of the force. Then I convert HSV colors to RGB and multiply each parameter by 255 because pygame display only accepts RGB colors in this format.  
> **username_1**: What I mean is are you using the full Hue range from 0° to 360°? If so, you might want to limit it to either 0-180° or 0-270°. Otherwise, things with very large force look the same as things with very little force, since the hues wrap around at 360°.  
> **username_0**: @username_1: you might be right that the colors in my sim look weird because I don't have the same conditions as in the "goal" sim. We are both using 10,000 particles, but my initial conditions are randomized positions and velocities, which will never produce a galaxy like the one shown above. We need to give the system some initial angular momentum to get a rotating galaxy. So I will see what happens by using proper initial conditions. I was just wondering if there was anything else I should be considering w.r.t the color scaling, or anything I'm doing that is obviously wrong.  
> **username_0**: @username_1: as I said, I use HSV colors where each parameter is from 0-1 instead of 0-360. If I set S and V to 1, and let H vary between 0 and 1, there is not this problem of wrapping around. H=0 gives red while H=1 gives green.  
> **username_1**: OK cool. I'm used to positive Hue going from red through yellow, to green, but I saw you had cyan, blue, and magenta in your image, so I was confused. But I think I get it now.  
> **username_2**: What do you mean by "properly" or "realistic"? It's not at all realistic to colour things according to the force acting on them. There are many choices of colour map and the answer depends on what your goal is for using colour.  
> **username_0**: @username_2: By realistic I mean a realistic representation of the forces acting on the bodies. I want to be able to see some sort of gradation where the force gradually increases as we move closer to the center of the galaxy, like the second image shown.  

---

## 0 answers

