📜 ⬆️ ⬇️

Life on 3D particles

Hello! I decided to share my readers with small experiments with particle systems in three-dimensional space. Based on a publication on Habré about experiments with particles in 2D space.



Let's start with a link to an article that pushed me to action. But there is another reason, more recently, I’ve been turning more and more into my Ubuntu experiments, I’m happy with the free installation of the OS and the list of advantages. There are downsides, I have a struggle with drivers when installing a non-standard OS like two video cards and several monitors.


He took C ++ as a basis, added support for CUDA as a computing platform, there are many particles and the central processor obviously cannot cope with such load in real time and the Ogre3D graphics engine to them in the company. Brewed porridge, I will spice up the story Gif animation and quotes from an article on the 2D version of the simulation.


“At first I followed the footsteps of the game“ life ”: each particle has a“ overpopulation ”counter, which is equal to the sum of inverse squares of distances to other particles. If this counter is less than a certain limit, that is, there are few neighbors, then the particle attracts to other particles, if there are many neighbors, it repels. If the particles intersect, then they repel, in any case, so as not to pass through each other.


Randomly scatter particles across the field and see what happens. "



Advanced video option
Now a little about the logic of what is happening in the program. An array of particles is created with specific parameters. Some of the parameters are responsible for the physical properties: radius, mass, velocity, etc., and some for creating bonds between particles, such as: the type of particles, the number of bonds of the particle with other particles, and so on. On average, I used in the simulation from 700 to 3000 particles, since I wanted to count in real time, the greater value led to the deceleration of the picture as a result of the increase in the amount of calculations. Then all this stuff is transferred to the memory of the video card, where the GPU already in the strong parallelization mode processes three main subroutines: simulation of the motion of particles, processing of collisions of particles (collisions) and formation-destruction of bonds between particles.


"We are changing the rules of the game. We will no longer consider our neighbors. Let the particles be simply attracted or repelled depending on their types. If all the particles are of the same type, then there are only 2 options: they either all repel or all attract."


I tried several variants of attraction-repulsion forces, linear dependences on the distances between particles, inversely proportional to the distance and so on. stopped on inverse quadratic dependence, but with a limitation on the radius of action, something like 45 radii of a particle.



"Let's not change the rules much. Instead, we will add a new feature. Now the particles will form connections at a short distance. If the particles are connected, they are constantly attracted to each other. This attraction does not weaken with the distance. But, if the distance is above a certain threshold, then the connection breaks. "


Here I changed the rule a little, introduced the bond formation distance, it is the distance of the bond destruction and the particle rest distance in the bond, that is, the particle constantly tries to occupy a position near the rest position, therefore at discrete time the particle oscillates around the zero position. This is visible on all videos. It is possible to apply more complex laws in the formation of a bond like elasticity (elasticity), but for the time being I have simplified it, but we still have “primary soup” and not solid matter.



Advanced video option


or as a picture



Then began the process of trial and error. First, programming on the GPU requires additional care in the part of the so-called "races on read-modify-write of shared data", which means that there may be problems when several threads simultaneously try to change a variable. There are instabilities like these:



Then it was necessary to limit the region of space in which the experiment is taking place, the first thing that came to mind was a cube. But thanks to the bahamas of the first versions of the program, the particles boldly sprawled out of it, forming something like the space stations from the fiction of the 70s.



extended video version


As they say, if not a cube, then let it be a ball:



Here the logic is that after the departure from the center, the force begins to act on the particle inverse to its motion, which even looks like gravity.


__device__ static int Links[3][3] = {{1,0,1},{1,0,0},{1,1,0}}; __device__ static int LinksField[3][3] = {{0,0,0},{0,0,1},{0,0,0}}; __device__ static int LinkTypeSize[3] = {3,8,2}; __device__ static int LinkTypePP[3][3] = {{0,1,1},{8,1,6},{0,1,1}}; 

He brought a piece of code as it is, these are matrices of interaction of three types of particles with each other.
-the first line is the principle of formation of bonds: 1 there is an opportunity to establish a connection with another particle, 0, respectively, no.
- The second line is the law of attraction of repulsion of particles. In fact, in this embodiment, all repel each other, except for the attraction of the second type of particles to the third. You can of course mirror the matrix, but in this case, so.
- the third line is the number of common connections of particles by type.
- the fourth is the number of links of a particle with each type of particles. The first type, for example, cannot form a link with itself.


We get the particles in a strong Brown movement:



extended video version


It was necessary to introduce energy loss during collisions.



extended video version


Everything has become too static, reducing the forces of interaction of particles.



extended video version


If you watch the video you can see how the construction of the frame. Then it remains static anyway.


Change the rules in terms of coeff matrices.



extended video version
We see the formation of the similarity of organic molecules. There are even benzene rings.



Another video that demonstrates what will happen if in the dynamics to change the length of the bonds between particles, from the second minute the video is particularly pronounced.



But we must try to repeat the 2D version of the author, whose quotes are in the body of the article. There is a variant of this "life":



or such



What can I add? First source programs . Secondly, there are several articles on Habré describing cellular automata or similar systems (in theory, a cellular automaton is equivalent to a Turing machine ) with a heading that includes the word "life", so I decided to use it too, sort of like a tradition. Although it is rather simply a kaledoscope, simple rules give rise to complex behavior, like particles and a system of mirrors in a children's toy.


And the video added, in fact, a fascinating picture, then the molecules seem to be, then the neural network, but life is not there yet. As an option, the development of genetics must be added, but it is not clear what may be a fitness function for these "creatures."



I hope someone will like it and it will step out of 3D further, as the author stepped out of 2D. The main thing for myself repeated the lessons on CUDA and Ogre3D under Ubuntu.



And there will be more ideas to write, maybe we can think of something interesting :)


')

Source: https://habr.com/ru/post/454430/


All Articles