Physarum Simulations

Physarum Polycephalum

Physarum Polycephalum aka slime mold is made up of a vast number of individual single cell organisms. These organisms have no brains or intelligence, but complex behaviors emerge when many of them are put together. Depending on their environment they move like what seems to be a much more complex entity.

Here are some great videos about slime molds with some awesome time lapse footage.

Once you have watched those you should hopefully have a better appreciation for the simple slime mold and the rest of this post will make more sense.

Here is one final video showing time lapse footage of various Physarum

Simulating Slime Molds

I have been interested in trying to simulate slime molds fror years now and my interest was once again peaked from seeing Sage Jenson‘s Physarum page here describing his simulations.

Sage was inspired by the paper Characteristics of Pattern Formation and Evolution in Approximations of Physarum Transport Networks.

He gives this simple diagram explaining the steps.

The basic explanation is a bunch of particles move over an area turning towards spots with higher concentrations of a pheromone trail. They also leave a trail as they move. These basic steps create interesting patterns and structures.

My method

Physarum Simulation

Following the principals from Sage and the paper, this is how my take on simulating Physarum works.

Physarum Simulation

1. Create a 2D array that tracks the pheromone trail intensity at every pixel location. Initially all spots are set to 0 intensity. I tried setting various shapes and perlin noise clouds to start, but the moving particles quickly erase any starting shapes and create their own paths so I just start with an empty space. Sage’s examples show interesting patterns and structures when starting with circles or other shapes, so I need to do some more work on start patterns.

Physarum Simulation

2. Create a list of particles with properties heading (direction/angle the particle is moving), x,y (positions), sense angle (how wide the particle looks to the left and right) and sense distance (how far in front the particle looks), turn angle (how quick the particle turns towards the sensed areas). I set the number of particles to match the image width multiplied by the image height. That seems to nicely adjust the particle count when changing image sizes.

Physarum Simulation

3. Main loop

Physarum Simulation

a) Display. For display I scale the minimum and maximum trail values to between 0 and 255 for a gray scale intensity (or to be used as an index into a color palette, but simple gray scale seems to look the best).

Physarum Simulation

b) Each particle looks at the 3 locations in front of it based on the sense angle and distance. You then work out which of the left, front and right spots have the highest concentration of the pheromone trail.

Physarum Simulation

c) Turn the particle towards the highest pheromone intensity. ie if the left spot is highest then subtract turn angle from the particle heading. If the front is highest do not make any change to the particle heading. If the right is highest add turn angle to the particle heading. You can also reverse this process so the particles turn away from the highest pheromone levels.

Physarum Simulation

d) Move the particle forwards by a specified move amount.

Physarum Simulation

e) Eat/absorb. I added a setting so that particles can absorb a bit of the pheromone trail at this point.

Physarum Simulation

f) Deposit an amount of pheromone onto the trail to increase it.

Physarum Simulation

g) Blur the trail array. This simulates the pheromones diffusing over the surface. I use this quick blur with an option for a blur radius between 1 and 5.

Physarum Simulation

h) Evaporate the trail by a small amount. This slowly decays the amount of pheromone.

Physarum Simulation

Repeat the main loop as long as necessary.

Physarum Simulation

Results

See my Physarum Simulations gallery for more images.

Here is a movie with some example results showing the simulations running. For the display the pheromone trail intensities are mapped to a gray scale palette (brighter = higher intensities).

Multiple Species Physarum Simulations

Physarum Simulation

My next idea was to have multiple Physarum types in the same area. For these cases I used 3 sets of Physarum (3 groups of particles with their own unique settings) as shown in the following settings dialog.

Physarum Simulation

Each of the pheromone trail intensities are then converted to RGB color components.

Physarum Simulation

This works but the results are just 3 separate simulations that do not interact. The idea is to have each of the particle types attract to their pheromones, but move away from the other 2 types of pheromones.

Physarum Simulation

The main change is in the pheromone detection and turn code. For the single Physarum simulation the particles look left, forward and right and then turn and move based on the location with the highest pheromone concentration. For 3 particle types they take into account their pheromone concentrations but subtract the pheromone concentrations of the other 2 types. For example if the 3 trail/pheromone arrays are called rtrail, gtrail and btrail, then the red particles pheromones are calculated by using rtrail[x,y]-gtrail[x,y]-btrail[x,y]. The highest concentration of left, forward and right is then turned and moved towards.

Physarum Simulation

More example images can be seen in my Physarum Simulations Gallery.

Here is a sample movie showing some of the multiple species results.

Physarum Image Processing

This was inspired after seeing the following video from Magic Jesus.

A bunch of Physarum particles start on the surface of an image. The particle colors are based on the image color they start on.

After this let them wander around the image area following Physarum simulation rules with a slight change. In this case rather than turning left or right based on a pheromone trail intensity, they turn towards the pixel that is closest in color to themselves.

This is my result after running Physarum simulations on three colorful paintings. The first and third are from Leonid Afremov and the second by Kandinsky (same painting as in Magic Jesus’ example movie).

These would look great on a large wall in a modern art gallery. Playing slowly enough so you could just notice the changing colors (like clouds moving slow enough you don’t notice they change until you look away and back again). The exhibits with those dark rooms you enter and read the little white plaque with a blurb on what it is all about. “The slow interplay of colors represents the human condition and the struggles of how humans still cannot find a peaceful equilibrium of coexistence with themselves and the planet.”

Availability

Both single and multiple species Physarum Simulations and Physarum Pixel Flow are now included with the latest version of Visions of Chaos.

Jason.

Style Transfer GANs (Generative Adversarial Networks)

Style Transfer Generative Adversarial Networks take two images and apply the style from one image to the other image. Here are some sample results from here.

Style Transfer GAN examples

For a more technical explanation of how these work, you can refer to the following papers;

Image Style Transfer Using Convolutional Neural Networks
Artistic style transfer for videos
Preserving Color in Neural Artistic Style Transfer

Ever since first seeing this technique I wanted to add it as an image processing option within Visions of Chaos.

If you only want to play around with style transfer or only have a few photos you want to experiment with, then I recommend you use an online service like DeepArt because this can be a tedious process to setup and use on your own PC.

Installing Style Transfer Prerequisites

Follow these steps to get Python, Python Libraries, CUDA and CuDNN installed.

Style Transfer in Visions of Chaos

Generate any image, then select Image->Image Processing->Style Transfer.

Visions of Chaos Style Transfer GAN

The first time you select Style Transfer it will download the 500 MB neural network model that is used for all the style transfer magic.

Start with smaller image sizes to get an idea of how long the process will take on your system before going for larger sized images.

You can also select any external image file to apply the style transfer to. So dig out those cat photos and have fun. Note that if you get tired of the limited style images that come with Visions of Chaos, you can put any image you like under the Style Transfer folder (by default this will be C:\Users\\AppData\Roaming\Visions of Chaos\Examples\TensorFlow\Style Transfer\) and use those. Grab an image of your favorite artist’s works and experiment.

For these next examples I used the following photo of Miss Marple.

Miss Marple

And applied some various transfer style images.

MC Escher Plane Filling II

Miss Marple Style Transfer GAN

A Mandelbrot fractal

Miss Marple Style Transfer GAN

Another Mandelbrot fractal

Miss Marple Style Transfer GAN

HR Giger Biomechanical Landscape

Miss Marple Style Transfer GAN

Kandinsky Composition VII

Miss Marple Style Transfer GAN

Mondrian

Miss Marple Style Transfer GAN

Monet

Miss Marple Style Transfer GAN

Picasso Les Femmes d’Alger

Miss Marple Style Transfer GAN

Picasso Seated Nude

Miss Marple Style Transfer GAN

Hokusai The Great Wave off Kanagawa

Miss Marple Style Transfer GAN

Munch The Scream

Miss Marple Style Transfer GAN

Turner The Wreck of a Transport Ship

Miss Marple Style Transfer GAN

van Gogh Starry Night

Miss Marple Style Transfer GAN

How It Works

Behind the scenes the Style Transfer uses Cameron Smith’s excellent Python script from here. After trying various Style Transfer related scripts that one gives the sharpest and most interesting results. See that link if you want to run these sort of style transfers yourself from the command line outside Visions of Chaos.

Troubleshooting

If you get a failed style transfer and an error message, here are a few things to try;
1. Smaller image size. Depending on the RAM in your PC and GPU you may have maxed out.
2. Wait 30 seconds and try again. This seems to help sometimes.
3. Reboot. If all else fails. Seems to always fix a stubborn error for me. The Cuda and/or cuDNN seem to be the main culprit. They get hung or locked or something and only a reboot will get them working again.

Jason.

Automatic Color Palette Creation

Fractint MAP format palette files

Going back 30 years, Fractint was a fractal generation program for DOS based systems. For its time it was the fractal program of choice for enthusiasts.

Fractint used a simple text format for its color palettes. These *.MAP files were text files with each color’s RGB values separated by spaces each on a new line. So, for example if you wanted the first color in your palette to be blue the first line would be “0 0 255”.

When I first started creating Visions of Chaos I adopted the format. The most common map files had 256 colors (you could have palettes with other color counts but I only use 256 color palettes).

The rest of this post covers the palette creation methods that have been included with Visions of Chaos. Although I use these methods specifically to create 256 color MAP files the principles could be applied to any number of colors for different sized palettes.

If you are just looking for a Fractint color palette collection, scroll down to the end of this post and grab the archive provided.

Smoothly blending colors

Visions of Chaos Color Palette Editor

This is probably the first and most obvious method to use. Take a small number of base colors (I allow up to 16) and blend them into a palette.

How you get the colors to blend can be;

1. User selects them from the standard color picker dialog.
2. User can use eye dropper functionality to pick them out of a photo.
3. Set them at random.
4. Use the color wheel. Allows selection of complmentary colors, tetrads, and other color theory based colors.

Visions of Chaos Color Palette Editor

5. Extract colors from an image. See this previous blog post explaining how that works.

Visions of Chaos Color Palette Editor

Once you have the colors there are numerous ways you can blend them;

1. Smooth blend. Smoothly interpolate the colors.

Visions of Chaos Color Palette Editor

2. Fade out blend. Fade each of the colors to black.

Visions of Chaos Color Palette Editor

3. Fade in blend. Fade each of the colors from black.

Visions of Chaos Color Palette Editor

4. Neon blend. Fade from black to the colors then back to black.

Visions of Chaos Color Palette Editor

5. Stripe blend. Alternate each color for the duration of the palette.

Visions of Chaos Color Palette Editor

Using curves to create palettes

The idea here is to use various mathematical functions to generate curves for the RGB components of the palette. The following is a list of the various methods I use so far.

Sine. Each RGB color component is its own sine wave. Randomize the wave amplitude, frequency and period.

Visions of Chaos Color Palette Editor

Multiple Sine. Add multiple sine waves together for each RGB component and then scale down to between 0 and 255.

Visions of Chaos Color Palette Editor

IQ. Idea from Inigo Quilez.

Visions of Chaos Color Palette Editor

Perlin. Use repeating noise loops as in this coding train video. Map the resulting noise values to each RGB channel. Using a looping noise function is best because it means the palette wraps around smoothly and using it for fractal zooms does not show a sharp break when the palette ends and restarts. I have only implemented this method over the last few days (at the time of writing this post), but so far it gives some really unique color palettes.

Visions of Chaos Color Palette Editor

Here are some examples palettes created using Perlin noise. Click to see the full sized image.

Visions of Chaos Color Palette Editor

Simplex. Same as Perlin, but uses Simplex noise.

Visions of Chaos Color Palette Editor

Simplex + Perlin. Create each RGB value by adding Simplex noise to Perlin noise.

Visions of Chaos Color Palette Editor

Here are some examples of Simplex and Simplex + Perlin palettes. Click for full size.

Visions of Chaos Color Palette Editor

Multiple Perlin – Add/subtract multiple Perlin Noise curves into RGB amounts.

Visions of Chaos Color Palette Editor

Random Walk. Random curve for each RGB component between index 0 and 127. Reverse for the rest of the palette. Each step the RGB is changed by +random(5)-2 to randomly go up and/or down.

Visions of Chaos Color Palette Editor

Terrain Fault. Take 2 random points between 0 and 255. Between the points randomly raise or lower by a small amount. Repeat this a number of times.

Visions of Chaos Color Palette Editor

HSL to RGB. Random HSL curves converted to RGB.

Visions of Chaos Color Palette Editor

RGB. Random curves for each RGB component. Use various easing functions to tween curve control points.

Visions of Chaos Color Palette Editor

YUV to RGB. Random YUV curves converted to RGB.

Visions of Chaos Color Palette Editor

Combine palettes. Take 2 previously created palettes and combine their RGB components by addition, subtraction or multiplication.

Visions of Chaos Color Palette Editor

Multiple RGB. Combine multiple RGB curves.

Visions of Chaos Color Palette Editor

Multiple YUV to RGB. Combine multiple YUV to RGB curves.

Visions of Chaos Color Palette Editor

Modify an existing palette

Once you have palette files, you can also use various techniques to modify them;

1. Increase or decrease the individual RGB channel amounts
2. Brightness
3. Contrast
4. Increase or decrease the individual YUV channel amounts
5. Wrap. Take the existing palette, halve it, then add the flipped half to itself. This is useful when you want a non repeating palette to wrap around.

Visions of Chaos Color Palette Editor

Visions of Chaos Color Palette Editor

6. Double. If you have a palette that is too smooth/sparse for the current fractal image, doubling can add more lines/gradients to the palette.

Visions of Chaos Color Palette Editor

Visions of Chaos Color Palette Editor

7. Blur. Just like a blur function in image processing. Averages out the palette values with neighbor colors.
8. Sharpen. Just like a sharpen function in image processing.
9. Shift RGB. R->G,G->B,B->R.

Visions of Chaos Color Palette Editor

Visions of Chaos Color Palette Editor

Visions of Chaos Color Palette Editor

10. Invert. R=255-R, G=255-G, B=255-B.
11. Reverse. Flip the order of the palette colors.
12. Histogram equalize palette. Like the auto-levels in Photoshop. My method tends to make the results slightly too bright. Needs fixing when I get a chance.

Visions of Chaos Color Palette Editor

Visions of Chaos Color Palette Editor

13. Matrix multiplication. Take a 3×3 matrix and multiply the 1×3 RGB components by the matrix to get new RGB amounts.

Visions of Chaos Color Palette Editor

Any other ideas?

If you know of any other ways to generate palettes, or have an idea for ways to create new unique color palettes, let me know.

Availability

The color palette editor shown in this post is included with Visions of Chaos.

Just give me the palettes!

If you are using another program that uses Fractint palette files you can download the 3371 color palettes I include with Visions of Chaos here. Some created by me, others found on various Internet sites over the years, some converted from gradient packs. No copyright on them so do with them as you wish.

If you do have any other sets of MAP palettes you would like to share, send me an email. You can never have enough colors when creating fractal images.

Jason.

Vorticity Confinement for Eulerian Fluid Simulations

Eulerian MAC Fluid Simulation with Vorticity Confinement

Eulerian fluid simulations simulate the flow of fluids by tracking fluid velocity and density over a set of individual (discreet) evenly spaced grid locations. One downside to this approach is that the finer details in the fluid can be smoothed out, so you lose those little swirls and vortices.

Eulerian MAC Fluid Simulation with Vorticity Confinement

A simple fix for this is to add Vorticity Confinement. If you read the Wikipedia page on Vorticity Confinement you may be no wiser on what it is or how to add it into your fluid simulations.

Eulerian MAC Fluid Simulation with Vorticity Confinement

My explanation of vorticity confinement is that it looks for curls (vortices) in the fluid and adds in velocity to help boost the swirling motion of the fluid. Adding vorticity confinement can also give more turbulent looking fluid simulations which tend to be more aesthetically pleasing in simulations (unless you are a member of team laminar flow).

Eulerian MAC Fluid Simulation with Vorticity Confinement

The code for implementing vorticity confinement is relatively simple. For 2D I used the snippet provided by Iam0x539 in this video.


function Curl(x,y:integer):double;
begin
     Curl:=xvelocity[x,y+1]-xvelocity[x,y-1] + yvelocity[x-1,y]-yvelocity[x+1, y];
end;

procedure VorticityConfinement(vorticity:double);
var dx,dy,len:double;
    x,y:integer;
begin
     for y:=2 to _h-3 do
     begin
          for x:=2 to _w-3 do
          begin
               dx:=abs(curl(x + 0, y - 1)) - abs(curl(x + 0, y + 1));
               dy:=abs(curl(x + 1, y + 0)) - abs(curl(x - 1, y + 0));
               len:=sqrt(sqr(dx)+sqr(dy))+1e-5;
               dx:=vorticity/len*dx;
               dy:=vorticity/len*dy;
               xvelocity[x,y]:=xvelocity[x,y]+timestep*curl(x,y)*dx);
               yvelocity[x,y]:=yvelocity[x,y]+timestep*curl(x,y)*dy);
          end;
     end;
end;

Eulerian MAC Fluid Simulation with Vorticity Confinement

The VorticityConfinement procedure is called once per simulation step. It looks for local curl at each fluid grid point and then increases the local x and y velocities using the curl. This is what helps preserve the little vortices and helps reduce the smoothing out of the fluid.

Eulerian MAC Fluid Simulation with Vorticity Confinement

To demonstrate how vorticity confinement changes a fluid simulation, the images within this post and the following movie add vorticity confinement to my previous Eulerian MAC Fluid Simulations code.

Eulerian MAC Fluid Simulations with Vorticity Confinement is now included in the latest version of Visions of Chaos.

Jason.

Eulerian Marker-and-Cell Fluids

Eulerian MAC Fluid Simulation

Benedikt Bitterli has a set of YouTube videos that have been an inspiration for years.

Eulerian MAC Fluid Simulation

He generously shares the source code to a series of programs on his Incremental Fluids GitHub that cover implementing a 2D fluid simulation. His code is based on Robert Bridson’s book, “Fluid Simulation for Computer Graphics”. I have seen that book mentioned all over the place and almost bought a copy, but reviews say it is focused more on the math (not so helpful to me) and not on the code (which I can follow much easier than math formulas).

Eulerian MAC Fluid Simulation

So far, I have converted Benedikt’s first and second programs for inclusion in Visions of Chaos. Calculations at 4K resolution were originally taking up to 10 minutes per frame, but with some multi-threading and code optimizations I got it down to around 10 seconds per 4K resolution frame on a relatively modern i7 CPU.

Eulerian MAC Fluid Simulation

The results so far are really nice. The resulting flows show very high detailed vortices and fluid behavior.

Eulerian MAC Fluid Simulation

Here is a sample 4K resolution movie showing these fluids in motion.

Eulerian Marker-and-Cell Fluid Simulations are now available in the latest version of Visions of Chaos.

Jason.

Jos Stam’s Fluid Simulations in 3D

I am a huge fan of anything related to fluid simulations. This interest was once again sparked when Daniel Shiffman covered fluid simulation in his latest Coding Train video.

As always, I recommend Coding Train to any developer. Dan has a unique way of making his programming topics both interesting and entertaining.

The code converted during the Coding Train video (Mike Ash’s Fluid Simulation For Dummies) is based on Jos Stam’s Stable Fluids code.

Jos Stam

Jos Stam

Jos Stam wrote his seminal paper Real-Time Fluid Dynamics for Games back in March 2003. I have lost count of the times I seen the paper cited or linked to. It has had a huge influence on fluid simulation.

The original source code from the paper is provided here and here.

Going back 11 years, one of my first YouTube videos was this super low resolution example of 2D fluid simulation using Stam’s methods.

3D Fluid

My main objective when revisiting Stam’s stable fluids was to get a 3D version going.

A quick Google search led me to Blain Maguire’s implementation which I was able to translate into a working 3D fluid simulation.

By default Stam’s fluid method generates a fairly smoothed fluid/smoke flow. To make things more interesting you want to add a bit of turbulence. The key term here is “vorticity confinement”. Vorticity confinemement was first described in the paper Visual Simulation of Smoke by Fedkiw et al.

I found some 3D source code for adding vorticity confinement to Stam’s stable fluids here. See the fluid.cpp file inside the fire32.tar.gz archive.

At this stage I had 3D fluid working. Now comes the fun part, how to display the fluid.

Displaying 3D Fluid

Once you get the code working, the main issue becomes how to display the fluid.

Stam’s fluid uses what is known as an Eulerian approach to fluid simulation. Rather than track individual fluid particles (as in SPH simulations) the fluid is simulated by tracking velocity and density at fixed grid based locations in 3D space. This means that as the simulation runs you have a 3D grid of fluid properties that need to be displayed.

The method I use is to hide the cells that contain a fluid velocity lower than a threshold value. 0.01 seems to work OK for my tests. Then render the cells as spheres or cubes. Color shaded based on fluid velocities.

3D Jos Stam Stable Fluid

For a fake volume rendering like approach you can render the fluid using translucent billboard quads. If you run a pre-render pass over the array and strip any cell that is surrounded by other cells it results in only having the “shell” of the fluids rendering. Rendering these shells with the billboard particles gives a nice result.

3D Jos Stam Stable Fluid

Movie results

Availability

3D Jos Stam Stable Fluids are now included in Visions of Chaos.

Jason.

Mitchell Gravity Set Fractals

Gravity Set Fractal

Origins

Back in the 1980’s Fred Mitchell designed a method to visualize Newtonian gravity he called the Mitchell Gravity Set or MGS. See here for his page explaining MGS.

This process of plotting dynamic systems is usually referred to as basins of attraction. See this paper and this paper for other versions of plotting gravitational basins of attraction. Similar methods are used when plotting Root-Finding Fractals and the basins of a Magnetic Pendulum.

Gravity Set Fractal

The 2D images in this post are from when I first experimented with MGS back in 2005.

Gravity Set Fractal

The basics of MGS is that you have a number of gravity objects (stars or gravity wells) in 2D space. Each pixel of a 2D image becomes the starting coordinate for a particle in that 2D space. The particle is attracted to the stars through Newtonian gravity and moves. You follow the path the particle takes for a number of steps and color the pixel depending on how long it took to be flung out of the simulation area.

Gravity Set Fractal

I ended up rewriting/translating my old code into GLSL for speed. Click here to see the shader source code that generates these 2D Gravity Set images.

Gravity Set Fractal

By playing with the various star positions and masses you can get a variety of different images.

The following image shows the current Gravity Set Options dialog in Visions of Chaos showing the various settings that can be tweaked.

Gravity Set Fractal

Revisiting and Extending Into The 3rd Dimension

I was recently contacted by Fred again and he mentioned he was working on getting a 3D version of MGS going. Getting the 2D code going in 3D is simple enough. Just a few extra lines for adding the Z dimension, so I was inspired to have a go at seeing what the 3D versions could look like. The settings dialog is extended as follows.

Gravity Set 3D Settings Dialog

Similar to the other 3D grid based displays like 3D Cellular Automata I have worked with, displaying a 3D grid of values can be tricky.

The following examples use a 500x500x500 resolution space.

The most obvious is a 3D Grid of little cubes. Each smaller cube is a starting point for the gravity calculations. Color the cubes depending on which gravity well they come closest to during their journey.

3D Gravity Set Fractal

That gives you a general idea of what the shapes of the 3D Gravity Set will be, but the interior is completely hidden.

You can slice an eighth of the cubes away….

3D Gravity Set Fractal

or even slice a half…

3D Gravity Set Fractal

Another option is to only display the cubes for only half of the gravity wells….

3D Gravity Set Fractal

or even for only a single well.

3D Gravity Set Fractal

3D Gravity Set Fractal

Then came the idea for “volumetric rendering”. I already had code for rendering transparent billboard quads so I used that. I am only rendering the edges of each gravity well area. If you render all points the image is just a white blob mess.

3D Gravity Set Fractal

3D Gravity Set Fractal

3D Gravity Set Fractal

Another method. Export the visible cubes as a OBJ point cloud. Import the OBJ into MeshLab. Use MeshLab to generate normals for the points and then marching cubes to generate a mesh from the point cloud. Export the mesh as a PLY format file. Import the PLY into Blender and render it. These are rough results. I am sure someone who knows more about MeshLab and Blender could clean up the mesh and make a much better looking render.

3D Gravity Set Fractal

3D Gravity Set Fractal

Here is a sample movie of rotating gravity sets.

Magnetic Pendulum Alternative to MGS

I also tried using Magnetic Pendulum formulas in 3D to do similar plots. The formula is a 3D extension of the 2D code I used here. Otherwise the rest of the code is the same as for the 3D MGS above.

3D Magnetic Pendulum

3D Magnetic Pendulum

3D Magnetic Pendulum

3D Magnetic Pendulum

3D Magnetic Pendulum

Availability

Both 2D and 3D Gravity Set Fractals are now included in the latest version of Visions of Chaos.

Jason.