<p>I suppose it depends on your definition of &quot;simple&quot;, but I would say &quot;No&quot;.</p>
<h3>Kepler simulations</h3>
<p>A solar system is simple because you have one dominating mass with orbiting masses that can, at least to first order, be modeled as mass-less. If you neglect the planets' masses, you can in fact calculate analytically their positions at any time, using Kepler's law. However, as soon as you have three or more masses, you need to calculate positions numerically.</p>
<h3><em>N</em>-body simulations</h3>
<p>The simplest model of the structure formation of the Universe are <a href="https://en.wikipedia.org/wiki/N-body_simulation" rel="noreferrer"><em>N</em>-body codes</a> which calculate the gravitational forces between a large number of particles that each represent a certain amount of dark matter (say, <span class="math-container">$10^4\,M_\odot$</span> for high-resolution simulations, or <span class="math-container">$10^8\,M_\odot$</span> for low-resolution simulations, where <span class="math-container">$1M_\odot$</span> is the mass of the Sun). At each point in time you can then advance each particle in space according to the sum of the gravitational forces from all other particles. The reason this is moderately simple is that dark matter is <em>collisionless</em>, so you only have gravity to worry about.</p>
<p>Modern-day <em>N</em>-body simulations have billions or even trillions of particles. With, say, <span class="math-container">$N=10^{12}$</span> this means that, for each time step, you in principle need to calculate <span class="math-container">$10^{24}$</span> forces. However, various numerical tricks exist that result in the number of calculations scaling not with <span class="math-container">$N^2$</span>, but with <span class="math-container">$N\log N$</span>, reducing the computational cost tremendously. For instance, for <span class="math-container">$N=10^{12}$</span> you now &quot;only&quot; need <span class="math-container">$\sim10^{13}$</span> calculations.</p>
<p>One such method (<a href="https://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation" rel="noreferrer">Barnes-Hut</a>) is to only calculate forces between nearby particles individually, while treating more distant particles collectively. Another method (<a href="https://en.wikipedia.org/wiki/Particle_mesh" rel="noreferrer">particle-mesh</a>; PM) discretizes space into cubic cells and solves forces in Fourier space where equations are simpler. The Bolshoi simulation that PM 2Ring mentions uses a variety of this method, except that the grid adapts to the density such that a cell in a high-density region is split into eight cells, recursively, and at each time step, the grid adapts to the new density field.</p>
<p>I wrote such a (non-adaptive) PM code once, as a master student; it took me a week, and I knew much of the physics in advance. So that's why I don't think I'd say it's downright &quot;simple&quot;, but doable, yes. Here's the result (I'm sorry for the bad resolution; I had to reduce the file):</p>
<p><a href="https://i.sstatic.net/dJFRY.gif" rel="noreferrer"><img src="https://i.sstatic.net/dJFRY.gif" alt="n-body" /></a></p>
<h3>Hydro-simulations</h3>
<p>With <em>N</em>-body simulations you can simulate the cosmic web rather accurately on large scales, including galactic haloes. But the galaxies themselves are made of gas which requires hydrodynamics, complicating the simulations quite a lot. Several approaches exist to this: Arguably the easiest to understand is analogous to the <em>N</em>-body code, but in addition to dark matter particles, you now also have star particles (which are also collisionless), and gas particles. The latter are what complicates the calculations, because gas is not collisionless but has pressure, viscosity, etc. (and even magnetic fields, if you're really crazy). To make the fluid more continuous, the particles are &quot;smoothed&quot; in space, which is why this method is called <a href="https://en.wikipedia.org/wiki/Smoothed-particle_hydrodynamics" rel="noreferrer">smoothed particles hydrodynamics</a>.</p>
<p>Another approach discretized space in cells, calculating the diffusion of gas across boundaries. Usually the cells are refined in dense regions to achieve higher resolution without spending computer power on refining less dense regions, similar to the Bolshoi simulation, but with gas. This method is called <a href="https://en.wikipedia.org/wiki/Adaptive_mesh_refinement" rel="noreferrer">adaptive mesh refinement</a>.</p>
<p>The different techniques each have the pros and cons. The &quot;moving mesh&quot; method combines the SPH and AMR techniques: Space is divided into irregular polygons that move around like particles, all the time change shape and size, while gas diffuses through boundaries. The <a href="https://en.wikipedia.org/wiki/Illustris_project" rel="noreferrer">Illustris simulation</a> is an example of such a simulation.</p>
<p>With hydrodynamics, you can catch all the intricacies of heating, cooling, stellar population evolution, feedback from star formation and active galactic nuclei, metal enrichment, dust formation, galactic morphology, etc.</p>
<p>But such simulations are most definitely not &quot;simple&quot;, requiring years of effort to develop from large teams of (astro-)physicists and computer scientists.</p>

CC BY-SA 4.0
2023-10-13T11:27:45.577Z
Do I understand correctly that the simulation of N-bodies is based on a random arrangement of particles-points in a certain limited volume and in a given number, and then the gravitational interaction between them is turned on and their equations of motion are solved numerically?
ayr
CC BY-SA 4.0
2023-10-13T11:59:37.533Z
@dtn Yes, exactly. A box of some finite size, but with periodic boundaries so that a particle exiting one face enters the opposite face. A very small cosmological volume is perhaps 100 million lightyears on one side, while a very large one is a few billion lightyears across. For a given number of particles (limited by your computer power), the advantage of a small box is the high resolution, while the advantage of the large box is the high number of galaxies, i.e. better statistics. The initial arrangement is not completely random, however, but follows a power spectrum obtained from the CMB.
pela
CC BY-SA 4.0
2023-10-13T16:53:11.283Z
It turns out that the algorithm has two main blocks: preparing the points/grid and calculating their “positions”. If we take into account all the factors: the spectrum of the CMB radiation, a sufficient number of points, hydrodynamic effects and consider the simulated filaments in the vicinity - will we see formed clusters of galaxies there? This would be an emergent effect of the model, but it probably doesn't assume that. Because this would require so many points that even a supercomputer could not handle it.
ayr
CC BY-SA 4.0
2023-10-13T18:37:36.977Z
Thank you for the detailed and informative answer! I had hope there is some easier way for a representation of the current state. I think those methods mentioned might be too precise and with this too compute intense or too complicated for implementation. I should have specify 'simple' better. However they might enhance or replace a more simple version in later update.
J. Doe
CC BY-SA 4.0
2023-10-13T21:17:13.283Z
Am I imagining things, or was the starting position of that simulation generated by [RANDU](https://en.wikipedia.org/wiki/RANDU)?
Mark
CC BY-SA 4.0
2023-10-14T21:40:04.923Z
@dtn Do you mean, will we see clusters forming where the initial overdensities are located? If so, then: there will be a correlation, but exactly how the density field evolves from the initial configuration can only be predicted a short while into the future. Once the density surpasses a certain amount (2×mean, if I remember correctly), things go non-linear and unpredictable. Or do mean, can these method predict the formation of galaxy clusters in general? Then certainly yes! You can find plenty of of really cool movies, e.g. from the [Illustris simulation](https://www.illustris-project.org).
pela
CC BY-SA 4.0
2023-10-14T21:42:49.443Z
@J.Doe Yes, the largest simulations are definitely not simple. In addition to taking a long time to develop, they take extremely long time to run, easily millions of hours, although distributed on thousands of computers. The simple one I show that I made myself only takes 10 minutes to run though.
pela
CC BY-SA 4.0
2023-10-14T21:47:19.833Z
@Mark The initial setup of a cosmological simulation does indeed use a random number generator, which in principle function like RANDU, starting from a seed and using the modulo for the next random number. They are much more sophisticated than RANDU though, although formally they don't produce _random_ numbers, but _pseudo_-random numbers, meaning that the numbers follow an order that is in principle predictable. The only true random number generator that I know of is [this one](https://xkcd.com/221/).
pela
CC BY-SA 4.0
2023-10-15T05:41:10.683Z
@pela What this meant was that we selected some area in the large-scale structure modeled and started zooming out. Will we see formed galaxies (not necessarily like in reality, but at least something similar)?
ayr
CC BY-SA 4.0
2023-10-15T10:34:39.127Z
@dtn I'm still not entirely sure I understand, but: The large-scale structure modeled is full of galaxies, so select any point in the box, and zoom out, you will eventually include a galaxy. Depending on your resolution, this galaxy may be more or less realistic: Lo-res sims will mostly just give you a collection of particles that may however still be realistic in terms of mass distribution (i.e. the number of large collection vs. small collections). High-res sims give you beautiful galaxies with realistic stellar populations, metallicity gradients, molecular clouds, spiral arms, and what not.
pela
CC BY-SA 4.0
2023-10-15T12:14:58.337Z
@pela You understood me correctly, thank you very much for your answer! This topic has interested me and I would like to learn the basics of these simulations. I found 2 articles on Wikipedia: https://en.wikipedia.org/wiki/N-body_simulation and https://en.wikipedia.org/wiki/N-body_problem and an article about GADGET-1 https://wwwmpa.mpa-garching.mpg.de/gadget/gadget1-paper.pdf Is this enough to get started?
ayr
CC BY-SA 4.0
2023-10-15T15:12:34.770Z
@dtn The Wikipedia articles give a relatively simple introduction of the basic concepts. GADGET used to be the state-of-the-art hydrocode, although arguably it has been surpassed by moving-mesh codes such as Illustris. The paper you link to is _the_ paper describing GADGET, but unless you're a physicist/astronomer, it might not be the most accessible literature. It mostly describes the physics, not the coding. I would rather recommend a simple tutorial on how to write your own hydro-code, and it might be easiest if you find one that uses a computer language you already know (cont. below).
pela
CC BY-SA 4.0
2023-10-15T15:14:12.903Z
These links might be helpful: [here](https://philip-mocz.medium.com/create-your-own-smoothed-particle-hydrodynamics-simulation-with-python-76e1cec505f1), [here](https://towardsdatascience.com/computational-fluid-dynamics-using-python-modeling-laminar-flow-272dad1ebec), [here](https://pysph.readthedocs.io/en/latest/overview.html), [here](https://www.reddit.com/r/physicsgifs/comments/15m59l8/oc_create_your_own_physics_simulations_with/), [here](https://astrobites.org/2011/06/11/running-your-first-sph-simulation/), and [here](https://www.usm.uni-muenchen.de/~ildar/hydro_course/ws2223/index.html).
pela
CC BY-SA 4.0
2023-10-17T03:33:40.670Z
@pela, I'm referring to the apparent large-scale structure in the starting positions.  RANDU was infamous for producing such a structure when used to generate three-dimensional coordinates.
Mark
CC BY-SA 4.0
2023-10-17T07:00:02.353Z
@Mark Ah sorry, whoosh 
pela
CC BY-SA 4.0
2023-10-13T10:25:15.683Z
2023-10-13T10:25:15.683Z
pela
<p>Here's a relatively simple way to make fake cosmic web patterns. Unfortunately, it takes a lot of points to make diagrams that resemble the one from Wikipedia, and with relatively small numbers of points we only have hints of the filamentary structure.</p>
<p>As mentioned by Pela and Anders, it takes a <em>lot</em> of computation to simulate gravity on huge numbers of points. My program doesn't attempt to do that. ;) It just produces a randomly distributed set of points that are vaguely similar to the cosmic web.</p>
<p>My method is based on random &quot;blue noise&quot; created by Poisson Disc sampling, which generates points that are evenly distributed, without the clumping and large voids that generally result when plain (white noise) random numbers are used to place points. Poisson Disc sampling is often used in CGI for a variety of tasks, eg placing trees, blades of grass, hair, animal fur, etc. Blue noise is also used in dithering / anti-aliasing. Incidentally, the cones in our retinas follow a blue noise distribution.</p>
<p>Here's a typical example.</p>
<p><a href="https://i.sstatic.net/lXrG1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lXrG1.png" alt="2d blue noise" /></a></p>
<p>I noticed that when you project 3D blue noise down to the plane it retains a lot of its openness, but some clustering &amp; filament formation occurs, resulting in the kind of image we want for a cosmic web pattern.</p>
<p><a href="https://i.sstatic.net/PfPAQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/PfPAQ.png" alt="3d blue noise projected to the plane" /></a></p>
<p>So if we generate 4D blue noise and project it down to 3D space, we <em>should</em> get something resembling the cosmic web...</p>
<p><a href="https://i.sstatic.net/BLyQt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BLyQt.png" alt="4d blue noise, projected" /></a></p>
<p>Those 3 diagrams were generated using this Sage / Python <a href="https://sagecell.sagemath.org/?z=eJyVVMGK2zAQvfsrBvVge%2Du6sbNblgVBKaHtpSW0hx6WxSiOnKgrS6ok002%5FviPJ3oSFHmqIMoxm3rw3MzYhBD5xxS3zHIwWyjuYnFAH2GrhnFawEe4RdnLioNDDM8BHKGg3Fazxpy1cb8AZ1vM63v2wwnuuYHeC7RdovwWsdtWu62ZVN%2D8yQkiWidFo60FNozkBc6BMNlg9guuFOdXOM%2D%5FqX2MPc9xMJTBZUkfmjdReil1tTsEKMEb6LHuPGlBO77M9H6Ar%5FJOnn%5F0oP%2DinIv8vqf%2DQmZcVClV7MdLvXPLea1vcF20FpN2QsoJijeYazYcK3BzQ%2DZPhNLdsL3RegWQ7LmmOFnJkk%5FS0jaCDZKF39COTjlehP10iSq9XqyqR7pz4w2lzE8D5nn7VCiN7hnz2KMzR9iYg9VpqS8mrht%5Fe8huChZh9XHDZ5HU3mRCfXOVdHJ1BTUCjNHgNDYhhYQQco%2DJFDAw6Joehg9TMFw28veAKV1cQXQGuLBOyxZuCpDxkk4wyS9s0RCkgcBFwkEFR4hOeeEMhpAdzTuHqIBRH%5F8VqFCEm1lzwafrDRl7052xWcMSpWGeO3HJK3GQHHC%2DZG5uqRfI%2DSE0l60FI2cU9KMqF%5FdykM%2DmUgef9XQV3b5qHiyagt3ZHZnAOJDWMJKA9l3ORpHEbIPScUs0TjScyDDtwXodzI%2DPsKIX2ooP%2DJEOr8rAC3Y71jwerJ7XPQ3zwpeHmvWTOiT4%5Fq5C%2Djsn15HgRrfL5cosi9O%2DCPWFLl7VyBpe9w%5FdLaNqk0AB9puKPfHym8rK%2DFIejz18WGCwb%2DVIh5tN44ttg9U%2DsJ7SiOX4SjvpgmTmigDL7CyyPhBQ=&amp;lang=sage" rel="nofollow noreferrer">script</a>. The script allows you to zoom in and rotate the 3D views. The <code>num_points</code> parameter is an upper bound on the actual number of points generated. It's used to calculate the Poisson disc radius: the minimum radial distance permitted between points. The <code>candidates</code> parameter determines how many random points are generated at each placement step. A higher candidate number leads to slightly higher density and a more accurate blue noise distribution, but takes longer to generate. Useful values range from around 15 to 35.</p>
<hr />
<p>In the actual cosmic web, clusters and filaments occur in regions of higher mass density. So we can enhance the diagram by modifying the size and color of points based on the density in their neighbourhood. A simple way to estimate that density is to count the neighbours within a certain distance of each point. I found that using half the Poisson disc radius gives good results.</p>
<p><a href="https://i.sstatic.net/lg7qM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/lg7qM.png" alt="Cosmic web, multicolor" /></a></p>
<p>Here's the <a href="https://sagecell.sagemath.org/?z=eJyFVV1v2zYUfdevuNCASUpVObaXYgjKYliNbcDQItgK7CEIBFqmLC6UyJAUGvfX715SspU8dH6QKfLec7%5FOodI0hZ10RvETcEgb7XrZwFexT8Fw74UdQA6w2YG2sN0lgL%5FfxSAs9wKMloN3MDo5HOFOS%2Df0QGCPsFejgAF3BHlvg%5FdPO3CGN6IKIB%2D5akZFKL4T0GiFFj%2DCk98E6BYEb7oZvrW6n4zGwdOpxN1ByGO316N1Ee8fKzHbAfYnuPsEm78opc31Zlutr6v1uyTFMpMkQLXj0HitlQPZG209KDvWDQYUybQxjL3BbmAQE11cI82pcp57Vz31zew4lUwVv7DDxkmuZqs%5Fd1%2DsOGP33BulvZL7ypxoRXGM8pjdL%2DdEciw83xRFchAtxJHkw0H2bFOC5Qc5OoZV3ZQwNBz3D9hGx%2DjdCXFgn%5FUgitvQFTEc5SCALVMN4IQGb2BdnAHj30vIxbqE7mSEdaYTVrDUjbbFWaZTSHoUIaLB2bApbtVKpeow9Ly4vy3h9u36IVg9Hjz2BA1jc3L0iu6XseJhtKqeRmFP9Z4jFr3ncbuE6%2DoGrqb0o3cTgt9f9dzkSgzlAq6Iga3wI1Ia45XBOkn8s0efLMv%2DRwWR5Tx5z6GzomVp571xt6vVYqLaHlfIkr0SqyOmi2mvArPjE7Oq0RP7N6AKOt%2DrH%2Db9t%2Df99ENyAYT5%5FP2Kf1jqsEowYaIMzhK12PjAlDrHYtgfCPyrfqZ1UWLRgTh%5FCyWQ9Ta%5Fz7clpNtdipPPkU7pBpcPNMZoUHscM8uoqzorQfG9UCzLItLY11GUbP3z9XUZFVqTaNm7BfmwtRcObW%5FIdS7kkghyvpp3XY7ZYAV8VJ5lOAXX84y2uH1kX%2DwoCIKPXtejIVT2G1du5ngkAM6wVZr7fA2rRaJwdQVhCx9vQiuKYiKqJR2k0TudZVBgU%2DlUtqEckHgJoEapqhiNfuGEAfkH4k8%2DZ1KxF5qlx3dVthTZazWFJBG4ch032Ng0lpXOMTsZr06GF8tz3swyin7TYUmExv%2DLV4M9RxcawVH4ml7zeRaTkoIDiekjrfJgIlcTIop5%2D1BAi4El0dLy4SjmcOFaiWojZgSQRkiVX9iCsnVP1i8Avw82pX2HUG7sI1Buypgli7neN8RiomKIiq8Rk8wI9ps0%2DTyhYjHmcBMyBpvFfP1J0eWUEf%5Fw4mkejxa%5FPYeM7GkPBNIPsgaJ6mSTnR2pocG5Gp3Iw6o4H97hDPXXnD%5FjyAN%5FS7z6DYqhxo%2Dp1GwdTQn6kgp%2D9%5FpzKq%5FjK7zcfPY6QGt5L%2DYIwZ%2DFJ8rV6n8xntQDy%5FBb1Omj5QYbnRXJf5Hsll4=&amp;lang=sage" rel="nofollow noreferrer">script</a>. It can use any standard matplotlib color map, but it's designed to use color maps that run from dark to light. The scripts run on the SageMath server, but the interactive 3D rendering happens in your browser, via three.js. If you try to create diagrams with too many points and you don't have enough RAM, your browser may complain or crash.</p>
<p>The scripts are written in plain Python, using Numpy, SciPy, and matplotlib. They only use Sage functions for the actual plotting. SciPy uses a fairly old Poisson Disc algorithm. There are modern algorithms which are faster, and which give a more accurate blue noise spectrum, eg the sample elimination method of Cem Yuksel.</p>

CC BY-SA 4.0
2023-10-16T14:50:17.483Z
Very nice! I wonder if you could get an even more filamentary structure by using "violet noise"?
pela
CC BY-SA 4.0
2023-10-16T17:32:17.740Z
Thanks, @pela! I don't know a cheap way to make 4D violet noise (and I'd like to avoid doing Fourier transforms on large 4d arrays), but I'm happy to test it if you know an algorithm I can use. FWIW, I also tried projecting 5D blue noise. I guess it looks slightly better, but it takes longer to generate. (I guess that's mostly because there are lots of neighbouring cells in a 5D grid).
PM 2Ring
CC BY-SA 4.0
2023-10-16T19:46:08.070Z
Unfortunately no, this is far from my expertise. I would not have guessed that it would be more difficult than blue noise. I had thought that one could simply tilt the input spectrum to be even steeper than the blue noise, but I suppose not…
pela
CC BY-SA 4.0
2023-10-27T07:21:34.160Z
thanks for the alternative answer and the detailed explanation ! (sorry for delay, just noted it today, didn't expect more answers). The mass distribution looks precise enough and it also needs no converging. But If I understood the Poisson Disc sampling Algorithm correctly we need to sample the whole area first to get the same result.
J. Doe
CC BY-SA 4.0
2023-10-27T07:22:06.833Z
If I'm not mistaken Poisson Disc sampling could also be done by permuting all possible points and pick the first points, mark them as valid if they have no other (earlier picked) inside their radius. Repeat until reaching 'num_points'. If an array with random uniform values is given (like in my case) an approximation of the Poisson Disc sampling could be searching for smaller values inside the radius (those would have picked and marked already earlier). For flattening this need to be done for each depth. ...
J. Doe
CC BY-SA 4.0
2023-10-27T07:22:14.307Z
...This has some error: The nearby smaller value might be invalid by having an even smaller value nearby. (could be checked for multiple instance). Have to check if it is still good enough. With a pseudo random function which generates random values for each point an endless picture could be generated which is always equal, no matter were we started from.
J. Doe
CC BY-SA 4.0
2023-10-27T17:02:42.723Z
@J.Doe SciPy uses the Bridson algorithm to do Poisson Disc sampling, which (kind of) generates the points one by one. On each loop iteration it generates a bunch of candidates, and tests if any of them are valid. An alternative is Robert Ulichney's Void & Cluster algorithm, but that's more suited for making continuous fields. OTOH, because it uses FFT it creates tileable patterns. By layering tiles of different sizes you can make infinite regions that aren't obviously tiled.
PM 2Ring
CC BY-SA 4.0
2023-10-27T17:03:21.710Z
@J.Doe https://blog.demofox.org/2019/06/25/generating-blue-noise-textures-with-void-and-cluster/ has some info on both types of blue noise. And I have a Python demo of both algorithms here: https://gist.github.com/PM2Ring/04375fca6727eb2df125491c390b147c
PM 2Ring
CC BY-SA 4.0
2023-10-27T23:36:34.017Z
@PM2Ring thanks again. Wow, even a lot of code. Thank you for all that. It looks like you also had some fun with that. Above I meant I would like to get the same result starting from different regions. E.g. the Bridson algorithm generates point by point. Let's say we start from point $p_0$ and generate the next points until we are reaching $p_j$ and continue up to our final point $p_k$. Now I would also like to start at point $p_j$ and get the same points out of it starting from there. ...
J. Doe
CC BY-SA 4.0
2023-10-27T23:38:08.293Z
..However this is solvable but upon trying around with it I noticed the size of the voids is related to density of the points. It would be nice if the voids can be larger and the points more dense in other areas. This might be possible with layers of different densities. And network hubs could be more present. This is probably hard to represent without any kind of simulation. I will try around some more and do some more tinkering. Ty all again.
J. Doe
CC BY-SA 4.0
2023-10-28T08:20:05.880Z
@J.Doe I should mention that I originally got that blue noise code from Github. See https://gist.github.com/PM2Ring/04375fca6727eb2df125491c390b147c?permalink_comment_id=4741782#gistcomment-4741782
PM 2Ring
CC BY-SA 4.0
2023-10-28T08:22:05.430Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/149391/discussion-between-pm-2ring-and-j-doe).
PM 2Ring
CC BY-SA 4.0
2023-10-16T14:07:59.363Z
2023-10-16T14:07:59.363Z
PM 2Ring
<p>In order to make a cosmic web pattern one can run a physics-based simulation approximating the flow of mass, as in Pela's answer. However, it is also possible to make &quot;phenomenological models&quot; that imitate the pattern without the underlying physics.</p>
<p>One common approach in physics is to look at the Fourier transform of phenomena, and if it has a simple functional form generate a new random pattern of that form and apply the inverse transform. This for example works well in replicating the texture of ocean waves under many conditions. However, for the cosmic web this approach does not work great. The reason is that <a href="https://www.astro.rug.nl/%7Eweygaert/tim1publication/cosmicweb2017/cosmicweb2017.lecture4.cosmicweb_identification.pdf" rel="noreferrer">the two-point correlation function (which dominates the power spectrum of the Fourier transform) is fairly insensitive to the web-like structure</a>. The interesting patterns are <a href="https://arxiv.org/abs/0708.1441" rel="noreferrer">due to phase correlations in the cosmic matter distribution</a>, that are harder to turn into something that can be sampled and inverse-transformed.</p>
<p><a href="https://arxiv.org/abs/1801.09070" rel="noreferrer">Another approach is generative adversarial networks</a>. Train a neural network to distinguish pictures of the cosmic web from other pictures, and another one to generate pictures that fools the first. This, once trained, can efficiently <a href="https://arxiv.org/abs/2005.03050" rel="noreferrer">make even 3D webs</a>.</p>
<p>One can also use aggregation processes to move randomly distributed points together, mimicking gravitation without doing a proper N-body simulation. <a href="https://arxiv.org/abs/2204.01256" rel="noreferrer">One amusing approach is inspired by <em>Physarum</em> slime molds</a>.</p>
<p>However, if only general visual similarity is required one can also use <a href="https://catlikecoding.com/unity/tutorials/pseudorandom-noise/voronoi-noise/" rel="noreferrer">Voronoi noise</a>, where random points are used to generate a density function that can then be perturbed by <a href="https://en.wikipedia.org/wiki/Perlin_noise" rel="noreferrer">Perlin noise</a> and sampled to quickly generate a fake cosmic web.</p>

CC BY-SA 4.0
2023-10-13T15:29:22.710Z
Good answer [!](https://astronomy.stackexchange.com/a/55016/5264)
pela
CC BY-SA 4.0
2023-10-13T19:31:35.613Z
Thank you for the answer mentioning multiple interesting methods! It's funny how interdisciplinary this is, from slime-mold growth over hydrodynamics to Fourier transformation. My current favorite due to is simplicity is the [Voronoi noise](https://catlikecoding.com/unity/tutorials/pseudorandom-noise/voronoi-noise/) solution. However a little more accurate to the real structure would be nice to have. Maybe I could use it as initialization for other methods. [The slime mold](https://arxiv.org/abs/2204.01256) solution looks very good but might be a little to compute intensive for target use case
J. Doe
CC BY-SA 4.0
2023-10-13T19:33:57.607Z
(I will have a closer look on the mentioned methods. Unless there is no better fitting answer I will mark this as answer in next days. I hope pela is ok with this. Sadly there can only be one winner. His post was very informative too but the question was about a  simple method. Should have define it better, sorry for that. Thank you for all answers and commends. )
J. Doe
CC BY-SA 4.0
2023-10-15T01:30:45.740Z
tested around with Voronoi noise. It can look pretty good. However some local structure is missing for some variation in lightening and object sizes. Just adding some noise is not sufficient enough. Are there any simple ideas to make it look more realistic?
Most mentioned methods are about the mass distribution and not the lightening. Happy to hear about some easy way doing this. However I don't think any exists. I will mark this as answer (at least for now). Thank you all for helping out. Learned many new fancy things to try around with.
J. Doe
CC BY-SA 4.0
2023-10-13T13:48:15.920Z
2023-10-13T13:48:15.920Z
Anders Sandberg
<p>To visualize the structure of a solar system we have some simple mathematical model: <br>Planets orbiting around a Star in circular motion. <br>
If we want to be more precise the circle becomes a ellipse, the center of motion shifted slightly to the planet and so on.</p>
<p><strong>Q:</strong> Is there also a (simple) mathematical model to visualize the cellular/web structure of the universe?<br>
It doesn't need to be very precise. It could be as simple as the model for solar system using circular motion. 2D is fine too.</p>
<p>Here some picture what I'm looking for:<br />
(It doesn't need to be that precise, just look similar too this)
<img src="https://upload.wikimedia.org/wikipedia/commons/9/9d/Structure_of_the_Universe.jpg" >
(picture from wikipedia, could not find how they made it)</p>
<p>In best case the mathematical model uses a 2D/3D field (array) of initialization values for generation.</p>
<p>Edit: It won't need to represent every step from big bang to now. The current state is sufficient.</p>

CC BY-SA 4.0
2023-10-13T03:52:39.007Z
There's some info at https://en.wikipedia.org/wiki/Observable_universe#Walls,_filaments,_nodes,_and_voids & https://en.wikipedia.org/wiki/Bolshoi_Cosmological_Simulation They don't give any details about the algorithms, but they mention an algorithm inspired by slime moulds.
PM 2Ring
CC BY-SA 4.0
2023-10-13T06:51:34.037Z
This looks promising: [Monte Carlo Physarum Machine: Characteristics of Pattern Formation in Continuous Stochastic Transport Networks](https://arxiv.org/abs/2204.01256) "MCPM is a probabilistic generalization of Jones's 2010 agent-based model for simulating the growth of Physarum polycephalum slime mold. We compare MCPM to Jones's work on theoretical grounds, and describe a task-specific variant designed for reconstructing the large-scale distribution of gas and dark matter in the Universe known as the Cosmic web."
PM 2Ring
CC BY-SA 4.0
2023-10-13T19:03:34.157Z
Blender : [Is there any way to create this material? (black cold lava rock)](https://blender.stackexchange.com/questions/228416/is-there-any-way-to-create-this-material-black-cold-lava-rock) "This kind of rock is called Scoria (as opposed to **pumice**, which is usually lighter in color)."
Mazura
CC BY-SA 4.0
2023-10-16T20:43:06.357Z
Can you say what you mean by a 'simple' mathematical model which could simulate anything about the structure of the universe?
Robbie Goodwin
CC BY-SA 4.0
2023-10-27T08:10:57.260Z
@RobbieGoodwin can't give an exact definition of 'simple' but here some nice to have properties: 1)  It should not have a simulation of all particles in it 2) and with this is also locally computable, the more local the better (but always generates the same global picture) 3) Given a 2D/3D field (array) of uniform distributed random values (my use case) the best case would be if the model can tell the state of a certain pixel/point just by reading it's related random value (most likely won't work out, so we also allow some neighborhood evaluation) ..
J. Doe
CC BY-SA 4.0
2023-10-27T08:11:04.517Z
...4) easy to code (let's say less than 1000 lines in most programming languages), not containing things like an Integral of and endless series of irrational numbers 5) good enough that most people can guess what it is by looking on it (mass distribution is a good starting point but total goal is also introducing lightening and colors), sry for delay, just noticed it now
J. Doe
<universe><galactic-dynamics><visualization><universe-sandbox>
Are there some (simple) mathematical models which can simulate the cellular/web structure of the universe?
J. Doe
# Are there some (simple) mathematical models which can simulate the cellular/web structure of the universe?

(Asked by: username_0 on 2023-10-13)

To visualize the structure of a solar system we have some simple mathematical model:   
Planets orbiting around a Star in circular motion.   
If we want to be more precise the circle becomes a ellipse, the center of motion shifted slightly to the planet and so on.

**Q:** Is there also a (simple) mathematical model to visualize the cellular/web structure of the universe?  
It doesn't need to be very precise. It could be as simple as the model for solar system using circular motion. 2D is fine too.

Here some picture what I'm looking for:

In best case the mathematical model uses a 2D/3D field (array) of initialization values for generation.

Edit: It won't need to represent every step from big bang to now. The current state is sufficient.

**Tags:** universe, galactic-dynamics, visualization, universe-sandbox
> **Comments:**
> **username_1**: There's some info at https://en.wikipedia.org/wiki/Observable_universe#Walls,_filaments,_nodes,_and_voids & https://en.wikipedia.org/wiki/Bolshoi_Cosmological_Simulation They don't give any details about the algorithms, but they mention an algorithm inspired by slime moulds.  
> **username_1**: This looks promising: [Monte Carlo Physarum Machine: Characteristics of Pattern Formation in Continuous Stochastic Transport Networks](https://arxiv.org/abs/2204.01256) "MCPM is a probabilistic generalization of Jones's 2010 agent-based model for simulating the growth of Physarum polycephalum slime mold. We compare MCPM to Jones's work on theoretical grounds, and describe a task-specific variant designed for reconstructing the large-scale distribution of gas and dark matter in the Universe known as the Cosmic web."  
> **username_2**: Blender : [Is there any way to create this material? (black cold lava rock)](https://blender.stackexchange.com/questions/228416/is-there-any-way-to-create-this-material-black-cold-lava-rock) "This kind of rock is called Scoria (as opposed to **pumice**, which is usually lighter in color)."  
> **username_3**: Can you say what you mean by a 'simple' mathematical model which could simulate anything about the structure of the universe?  
> **username_0**: @RobbieGoodwin can't give an exact definition of 'simple' but here some nice to have properties: 1)  It should not have a simulation of all particles in it 2) and with this is also locally computable, the more local the better (but always generates the same global picture) 3) Given a 2D/3D field (array) of uniform distributed random values (my use case) the best case would be if the model can tell the state of a certain pixel/point just by reading it's related random value (most likely won't work out, so we also allow some neighborhood evaluation) ..  
> **username_0**: ...4) easy to code (let's say less than 1000 lines in most programming languages), not containing things like an Integral of and endless series of irrational numbers 5) good enough that most people can guess what it is by looking on it (mass distribution is a good starting point but total goal is also introducing lightening and colors), sry for delay, just noticed it now  

---

## 3 answers

### Answer by username_7 on 2023-10-13. Score: 10 | Accepted answer

In order to make a cosmic web pattern one can run a physics-based simulation approximating the flow of mass, as in Pela's answer. However, it is also possible to make "phenomenological models" that imitate the pattern without the underlying physics.

One common approach in physics is to look at the Fourier transform of phenomena, and if it has a simple functional form generate a new random pattern of that form and apply the inverse transform. This for example works well in replicating the texture of ocean waves under many conditions. However, for the cosmic web this approach does not work great. The reason is that [the two-point correlation function (which dominates the power spectrum of the Fourier transform) is fairly insensitive to the web-like structure](https://www.astro.rug.nl/%7Eweygaert/tim1publication/cosmicweb2017/cosmicweb2017.lecture4.cosmicweb_identification.pdf). The interesting patterns are [due to phase correlations in the cosmic matter distribution](https://arxiv.org/abs/0708.1441), that are harder to turn into something that can be sampled and inverse-transformed.

[Another approach is generative adversarial networks](https://arxiv.org/abs/1801.09070). Train a neural network to distinguish pictures of the cosmic web from other pictures, and another one to generate pictures that fools the first. This, once trained, can efficiently [make even 3D webs](https://arxiv.org/abs/2005.03050).

One can also use aggregation processes to move randomly distributed points together, mimicking gravitation without doing a proper N-body simulation. [One amusing approach is inspired by *Physarum* slime molds](https://arxiv.org/abs/2204.01256).

However, if only general visual similarity is required one can also use [Voronoi noise](https://catlikecoding.com/unity/tutorials/pseudorandom-noise/voronoi-noise/), where random points are used to generate a density function that can then be perturbed by [Perlin noise](https://en.wikipedia.org/wiki/Perlin_noise) and sampled to quickly generate a fake cosmic web.

> **Comments:**
> **username_4**: Good answer [!](https://astronomy.stackexchange.com/a/55016/5264)  
> **username_0**: Thank you for the answer mentioning multiple interesting methods! It's funny how interdisciplinary this is, from slime-mold growth over hydrodynamics to Fourier transformation. My current favorite due to is simplicity is the [Voronoi noise](https://catlikecoding.com/unity/tutorials/pseudorandom-noise/voronoi-noise/) solution. However a little more accurate to the real structure would be nice to have. Maybe I could use it as initialization for other methods. [The slime mold](https://arxiv.org/abs/2204.01256) solution looks very good but might be a little to compute intensive for target use case  
> **username_0**: (I will have a closer look on the mentioned methods. Unless there is no better fitting answer I will mark this as answer in next days. I hope pela is ok with this. Sadly there can only be one winner. His post was very informative too but the question was about a  simple method. Should have define it better, sorry for that. Thank you for all answers and commends. )  
> **username_0**: tested around with Voronoi noise. It can look pretty good. However some local structure is missing for some variation in lightening and object sizes. Just adding some noise is not sufficient enough. Are there any simple ideas to make it look more realistic? Most mentioned methods are about the mass distribution and not the lightening. Happy to hear about some easy way doing this. However I don't think any exists. I will mark this as answer (at least for now). Thank you all for helping out. Learned many new fancy things to try around with.  

---

### Answer by username_4 on 2023-10-13. Score: 15

I suppose it depends on your definition of "simple", but I would say "No".

### Kepler simulations

A solar system is simple because you have one dominating mass with orbiting masses that can, at least to first order, be modeled as mass-less. If you neglect the planets' masses, you can in fact calculate analytically their positions at any time, using Kepler's law. However, as soon as you have three or more masses, you need to calculate positions numerically.

### *N*-body simulations

The simplest model of the structure formation of the Universe are [*N*-body codes](https://en.wikipedia.org/wiki/N-body_simulation) which calculate the gravitational forces between a large number of particles that each represent a certain amount of dark matter (say, $10^4\,M\_\odot$ for high-resolution simulations, or $10^8\,M\_\odot$ for low-resolution simulations, where $1M\_\odot$ is the mass of the Sun). At each point in time you can then advance each particle in space according to the sum of the gravitational forces from all other particles. The reason this is moderately simple is that dark matter is *collisionless*, so you only have gravity to worry about.

Modern-day *N*-body simulations have billions or even trillions of particles. With, say, $N=10^{12}$ this means that, for each time step, you in principle need to calculate $10^{24}$ forces. However, various numerical tricks exist that result in the number of calculations scaling not with $N^2$, but with $N\log N$, reducing the computational cost tremendously. For instance, for $N=10^{12}$ you now "only" need $\sim10^{13}$ calculations.

One such method ([Barnes-Hut](https://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation)) is to only calculate forces between nearby particles individually, while treating more distant particles collectively. Another method ([particle-mesh](https://en.wikipedia.org/wiki/Particle_mesh); PM) discretizes space into cubic cells and solves forces in Fourier space where equations are simpler. The Bolshoi simulation that PM 2Ring mentions uses a variety of this method, except that the grid adapts to the density such that a cell in a high-density region is split into eight cells, recursively, and at each time step, the grid adapts to the new density field.

I wrote such a (non-adaptive) PM code once, as a master student; it took me a week, and I knew much of the physics in advance. So that's why I don't think I'd say it's downright "simple", but doable, yes. Here's the result (I'm sorry for the bad resolution; I had to reduce the file):

[![n-body](https://i.sstatic.net/dJFRY.gif)](https://i.sstatic.net/dJFRY.gif)

### Hydro-simulations

With *N*-body simulations you can simulate the cosmic web rather accurately on large scales, including galactic haloes. But the galaxies themselves are made of gas which requires hydrodynamics, complicating the simulations quite a lot. Several approaches exist to this: Arguably the easiest to understand is analogous to the *N*-body code, but in addition to dark matter particles, you now also have star particles (which are also collisionless), and gas particles. The latter are what complicates the calculations, because gas is not collisionless but has pressure, viscosity, etc. (and even magnetic fields, if you're really crazy). To make the fluid more continuous, the particles are "smoothed" in space, which is why this method is called [smoothed particles hydrodynamics](https://en.wikipedia.org/wiki/Smoothed-particle_hydrodynamics).

Another approach discretized space in cells, calculating the diffusion of gas across boundaries. Usually the cells are refined in dense regions to achieve higher resolution without spending computer power on refining less dense regions, similar to the Bolshoi simulation, but with gas. This method is called [adaptive mesh refinement](https://en.wikipedia.org/wiki/Adaptive_mesh_refinement).

The different techniques each have the pros and cons. The "moving mesh" method combines the SPH and AMR techniques: Space is divided into irregular polygons that move around like particles, all the time change shape and size, while gas diffuses through boundaries. The [Illustris simulation](https://en.wikipedia.org/wiki/Illustris_project) is an example of such a simulation.

With hydrodynamics, you can catch all the intricacies of heating, cooling, stellar population evolution, feedback from star formation and active galactic nuclei, metal enrichment, dust formation, galactic morphology, etc.

But such simulations are most definitely not "simple", requiring years of effort to develop from large teams of (astro-)physicists and computer scientists.

> **Comments:**
> **username_5**: Do I understand correctly that the simulation of N-bodies is based on a random arrangement of particles-points in a certain limited volume and in a given number, and then the gravitational interaction between them is turned on and their equations of motion are solved numerically?  
> **username_4**: @dtn Yes, exactly. A box of some finite size, but with periodic boundaries so that a particle exiting one face enters the opposite face. A very small cosmological volume is perhaps 100 million lightyears on one side, while a very large one is a few billion lightyears across. For a given number of particles (limited by your computer power), the advantage of a small box is the high resolution, while the advantage of the large box is the high number of galaxies, i.e. better statistics. The initial arrangement is not completely random, however, but follows a power spectrum obtained from the CMB.  
> **username_5**: It turns out that the algorithm has two main blocks: preparing the points/grid and calculating their “positions”. If we take into account all the factors: the spectrum of the CMB radiation, a sufficient number of points, hydrodynamic effects and consider the simulated filaments in the vicinity - will we see formed clusters of galaxies there? This would be an emergent effect of the model, but it probably doesn't assume that. Because this would require so many points that even a supercomputer could not handle it.  
> **username_0**: Thank you for the detailed and informative answer! I had hope there is some easier way for a representation of the current state. I think those methods mentioned might be too precise and with this too compute intense or too complicated for implementation. I should have specify 'simple' better. However they might enhance or replace a more simple version in later update.  
> **username_6**: Am I imagining things, or was the starting position of that simulation generated by [RANDU](https://en.wikipedia.org/wiki/RANDU)?  
> **username_4**: @dtn Do you mean, will we see clusters forming where the initial overdensities are located? If so, then: there will be a correlation, but exactly how the density field evolves from the initial configuration can only be predicted a short while into the future. Once the density surpasses a certain amount (2×mean, if I remember correctly), things go non-linear and unpredictable. Or do mean, can these method predict the formation of galaxy clusters in general? Then certainly yes! You can find plenty of of really cool movies, e.g. from the [Illustris simulation](https://www.illustris-project.org).  
> **username_4**: @J.Doe Yes, the largest simulations are definitely not simple. In addition to taking a long time to develop, they take extremely long time to run, easily millions of hours, although distributed on thousands of computers. The simple one I show that I made myself only takes 10 minutes to run though.  
> **username_4**: @Mark The initial setup of a cosmological simulation does indeed use a random number generator, which in principle function like RANDU, starting from a seed and using the modulo for the next random number. They are much more sophisticated than RANDU though, although formally they don't produce _random_ numbers, but _pseudo_-random numbers, meaning that the numbers follow an order that is in principle predictable. The only true random number generator that I know of is [this one](https://xkcd.com/221/).  
> **username_5**: @pela What this meant was that we selected some area in the large-scale structure modeled and started zooming out. Will we see formed galaxies (not necessarily like in reality, but at least something similar)?  
> **username_4**: @dtn I'm still not entirely sure I understand, but: The large-scale structure modeled is full of galaxies, so select any point in the box, and zoom out, you will eventually include a galaxy. Depending on your resolution, this galaxy may be more or less realistic: Lo-res sims will mostly just give you a collection of particles that may however still be realistic in terms of mass distribution (i.e. the number of large collection vs. small collections). High-res sims give you beautiful galaxies with realistic stellar populations, metallicity gradients, molecular clouds, spiral arms, and what not.  
> **username_5**: @pela You understood me correctly, thank you very much for your answer! This topic has interested me and I would like to learn the basics of these simulations. I found 2 articles on Wikipedia: https://en.wikipedia.org/wiki/N-body_simulation and https://en.wikipedia.org/wiki/N-body_problem and an article about GADGET-1 https://wwwmpa.mpa-garching.mpg.de/gadget/gadget1-paper.pdf Is this enough to get started?  
> **username_4**: @dtn The Wikipedia articles give a relatively simple introduction of the basic concepts. GADGET used to be the state-of-the-art hydrocode, although arguably it has been surpassed by moving-mesh codes such as Illustris. The paper you link to is _the_ paper describing GADGET, but unless you're a physicist/astronomer, it might not be the most accessible literature. It mostly describes the physics, not the coding. I would rather recommend a simple tutorial on how to write your own hydro-code, and it might be easiest if you find one that uses a computer language you already know (cont. below).  
> **username_4**: These links might be helpful: [here](https://philip-mocz.medium.com/create-your-own-smoothed-particle-hydrodynamics-simulation-with-python-76e1cec505f1), [here](https://towardsdatascience.com/computational-fluid-dynamics-using-python-modeling-laminar-flow-272dad1ebec), [here](https://pysph.readthedocs.io/en/latest/overview.html), [here](https://www.reddit.com/r/physicsgifs/comments/15m59l8/oc_create_your_own_physics_simulations_with/), [here](https://astrobites.org/2011/06/11/running-your-first-sph-simulation/), and [here](https://www.usm.uni-muenchen.de/~ildar/hydro_course/ws2223/index.html).  
> **username_6**: @pela, I'm referring to the apparent large-scale structure in the starting positions.  RANDU was infamous for producing such a structure when used to generate three-dimensional coordinates.  
> **username_4**: @Mark Ah sorry, whoosh   

---

### Answer by username_1 on 2023-10-16. Score: 3

Here's a relatively simple way to make fake cosmic web patterns. Unfortunately, it takes a lot of points to make diagrams that resemble the one from Wikipedia, and with relatively small numbers of points we only have hints of the filamentary structure.

As mentioned by Pela and Anders, it takes a *lot* of computation to simulate gravity on huge numbers of points. My program doesn't attempt to do that. ;) It just produces a randomly distributed set of points that are vaguely similar to the cosmic web.

My method is based on random "blue noise" created by Poisson Disc sampling, which generates points that are evenly distributed, without the clumping and large voids that generally result when plain (white noise) random numbers are used to place points. Poisson Disc sampling is often used in CGI for a variety of tasks, eg placing trees, blades of grass, hair, animal fur, etc. Blue noise is also used in dithering / anti-aliasing. Incidentally, the cones in our retinas follow a blue noise distribution.

Here's a typical example.

[![2d blue noise](https://i.sstatic.net/lXrG1.png)](https://i.sstatic.net/lXrG1.png)

I noticed that when you project 3D blue noise down to the plane it retains a lot of its openness, but some clustering & filament formation occurs, resulting in the kind of image we want for a cosmic web pattern.

[![3d blue noise projected to the plane](https://i.sstatic.net/PfPAQ.png)](https://i.sstatic.net/PfPAQ.png)

So if we generate 4D blue noise and project it down to 3D space, we *should* get something resembling the cosmic web...

[![4d blue noise, projected](https://i.sstatic.net/BLyQt.png)](https://i.sstatic.net/BLyQt.png)

Those 3 diagrams were generated using this Sage / Python [script](https://sagecell.sagemath.org/?z=eJyVVMGK2zAQvfsrBvVge%2Du6sbNblgVBKaHtpSW0hx6WxSiOnKgrS6ok002%5FviPJ3oSFHmqIMoxm3rw3MzYhBD5xxS3zHIwWyjuYnFAH2GrhnFawEe4RdnLioNDDM8BHKGg3Fazxpy1cb8AZ1vM63v2wwnuuYHeC7RdovwWsdtWu62ZVN%2D8yQkiWidFo60FNozkBc6BMNlg9guuFOdXOM%2D%5FqX2MPc9xMJTBZUkfmjdReil1tTsEKMEb6LHuPGlBO77M9H6Ar%5FJOnn%5F0oP%2DinIv8vqf%2DQmZcVClV7MdLvXPLea1vcF20FpN2QsoJijeYazYcK3BzQ%2DZPhNLdsL3RegWQ7LmmOFnJkk%5FS0jaCDZKF39COTjlehP10iSq9XqyqR7pz4w2lzE8D5nn7VCiN7hnz2KMzR9iYg9VpqS8mrht%5Fe8huChZh9XHDZ5HU3mRCfXOVdHJ1BTUCjNHgNDYhhYQQco%2DJFDAw6Joehg9TMFw28veAKV1cQXQGuLBOyxZuCpDxkk4wyS9s0RCkgcBFwkEFR4hOeeEMhpAdzTuHqIBRH%5F8VqFCEm1lzwafrDRl7052xWcMSpWGeO3HJK3GQHHC%2DZG5uqRfI%2DSE0l60FI2cU9KMqF%5FdykM%2DmUgef9XQV3b5qHiyagt3ZHZnAOJDWMJKA9l3ORpHEbIPScUs0TjScyDDtwXodzI%2DPsKIX2ooP%2DJEOr8rAC3Y71jwerJ7XPQ3zwpeHmvWTOiT4%5Fq5C%2Djsn15HgRrfL5cosi9O%2DCPWFLl7VyBpe9w%5FdLaNqk0AB9puKPfHym8rK%2DFIejz18WGCwb%2DVIh5tN44ttg9U%2DsJ7SiOX4SjvpgmTmigDL7CyyPhBQ=&lang=sage). The script allows you to zoom in and rotate the 3D views. The `num_points` parameter is an upper bound on the actual number of points generated. It's used to calculate the Poisson disc radius: the minimum radial distance permitted between points. The `candidates` parameter determines how many random points are generated at each placement step. A higher candidate number leads to slightly higher density and a more accurate blue noise distribution, but takes longer to generate. Useful values range from around 15 to 35.

---

In the actual cosmic web, clusters and filaments occur in regions of higher mass density. So we can enhance the diagram by modifying the size and color of points based on the density in their neighbourhood. A simple way to estimate that density is to count the neighbours within a certain distance of each point. I found that using half the Poisson disc radius gives good results.

[![Cosmic web, multicolor](https://i.sstatic.net/lg7qM.png)](https://i.sstatic.net/lg7qM.png)

Here's the [script](https://sagecell.sagemath.org/?z=eJyFVV1v2zYUfdevuNCASUpVObaXYgjKYliNbcDQItgK7CEIBFqmLC6UyJAUGvfX715SspU8dH6QKfLec7%5FOodI0hZ10RvETcEgb7XrZwFexT8Fw74UdQA6w2YG2sN0lgL%5FfxSAs9wKMloN3MDo5HOFOS%2Df0QGCPsFejgAF3BHlvg%5FdPO3CGN6IKIB%2D5akZFKL4T0GiFFj%2DCk98E6BYEb7oZvrW6n4zGwdOpxN1ByGO316N1Ee8fKzHbAfYnuPsEm78opc31Zlutr6v1uyTFMpMkQLXj0HitlQPZG209KDvWDQYUybQxjL3BbmAQE11cI82pcp57Vz31zew4lUwVv7DDxkmuZqs%5Fd1%2DsOGP33BulvZL7ypxoRXGM8pjdL%2DdEciw83xRFchAtxJHkw0H2bFOC5Qc5OoZV3ZQwNBz3D9hGx%2DjdCXFgn%5FUgitvQFTEc5SCALVMN4IQGb2BdnAHj30vIxbqE7mSEdaYTVrDUjbbFWaZTSHoUIaLB2bApbtVKpeow9Ly4vy3h9u36IVg9Hjz2BA1jc3L0iu6XseJhtKqeRmFP9Z4jFr3ncbuE6%2DoGrqb0o3cTgt9f9dzkSgzlAq6Iga3wI1Ia45XBOkn8s0efLMv%2DRwWR5Tx5z6GzomVp571xt6vVYqLaHlfIkr0SqyOmi2mvArPjE7Oq0RP7N6AKOt%2DrH%2Db9t%2Df99ENyAYT5%5FP2Kf1jqsEowYaIMzhK12PjAlDrHYtgfCPyrfqZ1UWLRgTh%5FCyWQ9Ta%5Fz7clpNtdipPPkU7pBpcPNMZoUHscM8uoqzorQfG9UCzLItLY11GUbP3z9XUZFVqTaNm7BfmwtRcObW%5FIdS7kkghyvpp3XY7ZYAV8VJ5lOAXX84y2uH1kX%2DwoCIKPXtejIVT2G1du5ngkAM6wVZr7fA2rRaJwdQVhCx9vQiuKYiKqJR2k0TudZVBgU%2DlUtqEckHgJoEapqhiNfuGEAfkH4k8%2DZ1KxF5qlx3dVthTZazWFJBG4ch032Ng0lpXOMTsZr06GF8tz3swyin7TYUmExv%2DLV4M9RxcawVH4ml7zeRaTkoIDiekjrfJgIlcTIop5%2D1BAi4El0dLy4SjmcOFaiWojZgSQRkiVX9iCsnVP1i8Avw82pX2HUG7sI1Buypgli7neN8RiomKIiq8Rk8wI9ps0%2DTyhYjHmcBMyBpvFfP1J0eWUEf%5Fw4mkejxa%5FPYeM7GkPBNIPsgaJ6mSTnR2pocG5Gp3Iw6o4H97hDPXXnD%5FjyAN%5FS7z6DYqhxo%2Dp1GwdTQn6kgp%2D9%5FpzKq%5FjK7zcfPY6QGt5L%2DYIwZ%2DFJ8rV6n8xntQDy%5FBb1Omj5QYbnRXJf5Hsll4=&lang=sage). It can use any standard matplotlib color map, but it's designed to use color maps that run from dark to light. The scripts run on the SageMath server, but the interactive 3D rendering happens in your browser, via three.js. If you try to create diagrams with too many points and you don't have enough RAM, your browser may complain or crash.

The scripts are written in plain Python, using Numpy, SciPy, and matplotlib. They only use Sage functions for the actual plotting. SciPy uses a fairly old Poisson Disc algorithm. There are modern algorithms which are faster, and which give a more accurate blue noise spectrum, eg the sample elimination method of Cem Yuksel.

> **Comments:**
> **username_4**: Very nice! I wonder if you could get an even more filamentary structure by using "violet noise"?  
> **username_1**: Thanks, @pela! I don't know a cheap way to make 4D violet noise (and I'd like to avoid doing Fourier transforms on large 4d arrays), but I'm happy to test it if you know an algorithm I can use. FWIW, I also tried projecting 5D blue noise. I guess it looks slightly better, but it takes longer to generate. (I guess that's mostly because there are lots of neighbouring cells in a 5D grid).  
> **username_4**: Unfortunately no, this is far from my expertise. I would not have guessed that it would be more difficult than blue noise. I had thought that one could simply tilt the input spectrum to be even steeper than the blue noise, but I suppose not…  
> **username_0**: thanks for the alternative answer and the detailed explanation ! (sorry for delay, just noted it today, didn't expect more answers). The mass distribution looks precise enough and it also needs no converging. But If I understood the Poisson Disc sampling Algorithm correctly we need to sample the whole area first to get the same result.  
> **username_0**: If I'm not mistaken Poisson Disc sampling could also be done by permuting all possible points and pick the first points, mark them as valid if they have no other (earlier picked) inside their radius. Repeat until reaching 'num_points'. If an array with random uniform values is given (like in my case) an approximation of the Poisson Disc sampling could be searching for smaller values inside the radius (those would have picked and marked already earlier). For flattening this need to be done for each depth. ...  
> **username_0**: ...This has some error: The nearby smaller value might be invalid by having an even smaller value nearby. (could be checked for multiple instance). Have to check if it is still good enough. With a pseudo random function which generates random values for each point an endless picture could be generated which is always equal, no matter were we started from.  
> **username_1**: @J.Doe SciPy uses the Bridson algorithm to do Poisson Disc sampling, which (kind of) generates the points one by one. On each loop iteration it generates a bunch of candidates, and tests if any of them are valid. An alternative is Robert Ulichney's Void & Cluster algorithm, but that's more suited for making continuous fields. OTOH, because it uses FFT it creates tileable patterns. By layering tiles of different sizes you can make infinite regions that aren't obviously tiled.  
> **username_1**: @J.Doe https://blog.demofox.org/2019/06/25/generating-blue-noise-textures-with-void-and-cluster/ has some info on both types of blue noise. And I have a Python demo of both algorithms here: https://gist.github.com/PM2Ring/04375fca6727eb2df125491c390b147c  
> **username_0**: @PM2Ring thanks again. Wow, even a lot of code. Thank you for all that. It looks like you also had some fun with that. Above I meant I would like to get the same result starting from different regions. E.g. the Bridson algorithm generates point by point. Let's say we start from point $p_0$ and generate the next points until we are reaching $p_j$ and continue up to our final point $p_k$. Now I would also like to start at point $p_j$ and get the same points out of it starting from there. ...  
> **username_0**: ..However this is solvable but upon trying around with it I noticed the size of the voids is related to density of the points. It would be nice if the voids can be larger and the points more dense in other areas. This might be possible with layers of different densities. And network hubs could be more present. This is probably hard to represent without any kind of simulation. I will try around some more and do some more tinkering. Ty all again.  
> **username_1**: @J.Doe I should mention that I originally got that blue noise code from Github. See https://gist.github.com/PM2Ring/04375fca6727eb2df125491c390b147c?permalink_comment_id=4741782#gistcomment-4741782  
> **username_1**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/149391/discussion-between-pm-2ring-and-j-doe).  

---

