src/examples/turbulence.py

Python interface

Basilisk also includes a high-level interface for the Python programming language.

This example documents how to use this interface to setup the decaying two-dimensional turbulence simulation.

Basilisk python module

The first step is to create a python module importing the necessary functions from Basilisk. For this example we just need to create a file called stream.c with the following lines

#include grid/multigrid.h
#include navier-stokes/stream.h

i.e. we will use the streamfunction–vorticity Navier–Stokes solver with a multigrid discretisation.

To create the corresponding python module, assuming that a Makefile has been setup properly, we can just do

make stream.py

Python program

The next step is to create the python program itself, let’s call it turbulence.py.

We first import matplotib, numpy and our Basilisk module.

import matplotlib.pyplot as plt
import numpy as np
import stream as bas

We setup a numpy regular grid which will hold the values interpolated from the Basilisk field.

N = 256
x = np.linspace(0, 1, N)
y = np.linspace(0, 1, N)
X,Y = np.meshgrid(x,y)

We define a function which sets initial conditions for the vorticity ω: a white noise.

def init(i,t):
    bas.omega.f = bas.noise

We define a function which uses matplotlib to generate a graph of the ω field.

def graph(i,t):
    print "t=",t
    Z = bas.omega.f(X,Y)
    plt.imshow(Z)
    plt.draw()
    plt.show(block=False)

We setup Basilisk’s resolution to match that of our numpy grid.

bas.init_grid(N)

We register the initial condition (at t=0) and graph function (at t = 0,10,20,…,1000).

bas.event(init, t = 0.)
bas.event(graph, t = range(0,1000,10))

We run the simulation.

bas.run()

This is just so that matplotlib does not close the window.

plt.show()