Example usage for pyJHTDB.


In [1]:
%matplotlib inline
import numpy as np
import pyJHTDB

I'm going to create a 2D grid of points, and then get the values of the velocity at those points. Note that the points must be in single precision if they are to be used for any getData call.


In [2]:
t = np.linspace(0, 1, 64)
x = np.zeros((t.shape[0], t.shape[0], 3), np.float32)
x[:, :, 0] = t[np.newaxis, :]
x[:, :, 1] = t[:, np.newaxis]
x[:, :, 2] = .0

Since the dataset I'm gonna use is the isotropic turbulence dataset, it doesn't really matter what value I choose for the z coordinates, if it's fixed. Not for this simple example anyway.

The next step is to generate a libJHTDB object, connect it to the database, and use it to read the data that we need. I'm using 4th order Lagrange interpolation (that's the sinterp = 4 line), and for now we need to specify the actual number corresponding to that (you can find all the different codes in turblib.h from the C package).


In [3]:
lJHTDB = pyJHTDB.libJHTDB()
lJHTDB.initialize()

#Add token
auth_token  = "edu.jhu.pha.turbulence.testing-201311"  #Replace with your own token here
lJHTDB.add_token(auth_token)

import pyJHTDB.dbinfo
T = pyJHTDB.dbinfo.isotropic1024coarse['time'][-1]
time = np.random.random()*T
u = lJHTDB.getData(
               time,
               x,
               sinterp = 4,
               getFunction='getVelocity')
ubox = lJHTDB.getBoxFilter(
               time,
               x,
               field = 'velocity',
               filter_width = 5*(2*np.pi / 1024))
lJHTDB.finalize()

Now we have the velocity stored in u, and we're gonna compute the energy and make a nice plot of it.


In [4]:
e = np.sum(u**2, axis = 2)
ebox = np.sum(ubox**2, axis = 2)

import matplotlib.pyplot as plt
fig = plt.figure(figsize = (10, 5))
a = fig.add_subplot(121)
a.set_axis_off()
a.imshow(e,
         extent = [t[0], t[-1] - t[0], t[0], t[-1] - t[0]],
         interpolation = 'none')
a = fig.add_subplot(122)
a.imshow(ebox,
         extent = [t[0], t[-1] - t[0], t[0], t[-1] - t[0]],
         interpolation = 'none')


Out[4]:
<matplotlib.image.AxesImage at 0xb16102320>

Next, get some trajectories.


In [5]:
lJHTDB.initialize()

x, t = lJHTDB.getPosition(
    starttime = 0.1,
    endtime = 0.2,
    dt = 0.001,
    point_coords = 2*np.pi * np.random.random((20, 3)),
    steps_to_keep = 50)

lJHTDB.finalize()


starting integration loop, dataset is  isotropic1024coarse
at time step 1 out of 50
got next position for time step 1
at time step 2 out of 50
got next position for time step 2
at time step 3 out of 50
got next position for time step 3
at time step 4 out of 50
got next position for time step 4
at time step 5 out of 50
got next position for time step 5
at time step 6 out of 50
got next position for time step 6
at time step 7 out of 50
got next position for time step 7
at time step 8 out of 50
got next position for time step 8
at time step 9 out of 50
got next position for time step 9
at time step 10 out of 50
got next position for time step 10
at time step 11 out of 50
got next position for time step 11
at time step 12 out of 50
got next position for time step 12
at time step 13 out of 50
got next position for time step 13
at time step 14 out of 50
got next position for time step 14
at time step 15 out of 50
got next position for time step 15
at time step 16 out of 50
got next position for time step 16
at time step 17 out of 50
got next position for time step 17
at time step 18 out of 50
got next position for time step 18
at time step 19 out of 50
got next position for time step 19
at time step 20 out of 50
got next position for time step 20
at time step 21 out of 50
got next position for time step 21
at time step 22 out of 50
got next position for time step 22
at time step 23 out of 50
got next position for time step 23
at time step 24 out of 50
got next position for time step 24
at time step 25 out of 50
got next position for time step 25
at time step 26 out of 50
got next position for time step 26
at time step 27 out of 50
got next position for time step 27
at time step 28 out of 50
got next position for time step 28
at time step 29 out of 50
got next position for time step 29
at time step 30 out of 50
got next position for time step 30
at time step 31 out of 50
got next position for time step 31
at time step 32 out of 50
got next position for time step 32
at time step 33 out of 50
got next position for time step 33
at time step 34 out of 50
got next position for time step 34
at time step 35 out of 50
got next position for time step 35
at time step 36 out of 50
got next position for time step 36
at time step 37 out of 50
got next position for time step 37
at time step 38 out of 50
got next position for time step 38
at time step 39 out of 50
got next position for time step 39
at time step 40 out of 50
got next position for time step 40
at time step 41 out of 50
got next position for time step 41
at time step 42 out of 50
got next position for time step 42
at time step 43 out of 50
got next position for time step 43
at time step 44 out of 50
got next position for time step 44
at time step 45 out of 50
got next position for time step 45
at time step 46 out of 50
got next position for time step 46
at time step 47 out of 50
got next position for time step 47
at time step 48 out of 50
got next position for time step 48
at time step 49 out of 50
got next position for time step 49
at time step 50 out of 50
got next position for time step 50

Now, plot trajectories. Not spectactular because they're not that long, but this is the way a simple plot would work for long trajectories as well.


In [6]:
fig = plt.figure(figsize = (10, 5))
a = fig.add_subplot(111)
a.plot(x[:, 0], x[:, 1])


Out[6]:
[<matplotlib.lines.Line2D at 0xb161aca90>,
 <matplotlib.lines.Line2D at 0xb1621b9e8>,
 <matplotlib.lines.Line2D at 0xb1621bb38>]

In [ ]: