17

i hope the title itself was quite clear , i am solving 2D lid-driven cavity(square domain) problem using fractional step method , finite difference formulation (Navier-Stokes primitive variable form) , i have got u and v components of velocity over the entire domain , without manually calculating streamlines , is there a command or plotting tool which does the job for me?

i hope this question is relevant enough to programming , as i need a tool for plotting streamlines without explicitly calculating them.

I have solved the same problem in stream-vorticity NS form , i just had to take contour plot of stream function to get the streamlines.

I hope that tool or plotter is a python library, and morevover installable in fedora (i can compromise and use mint)without much fuss!!

i would be grateful if someone points out the library and relevant command (would save a lot of time)

imranal
  • 656
  • 12
  • 35
fedvasu
  • 1,232
  • 3
  • 18
  • 38

3 Answers3

22

Have a look at Tom Flannaghan's streamplot function. The relevant thread on the user's list is here, and there's also another similar code snippet by Ray Speth that does things slightly differently.

If you have problems with speed, it might be more efficient to use some of scipy's integration functionality instead of the pure-numpy integration functions used in both of these examples. I haven't tried it, though, and these deliberately avoid a dependency on scipy. (scipy is a rather heavy dependency compared to numpy)

From it's example plot:

import matplotlib.pyplot as plt
import numpy as np
from streamplot import streamplot

x = np.linspace(-3,3,100)
y = np.linspace(-3,3,100)
u = -1-x**2+y[:,np.newaxis]
v = 1+x-y[:,np.newaxis]**2
speed = np.sqrt(u*u + v*v)

plt.figure()
plt.subplot(121)
streamplot(x, y, u, v, density=1, INTEGRATOR='RK4', color='b')
plt.subplot(122)
streamplot(x, y, u, v, density=(1,1), INTEGRATOR='RK4', color=u,
           linewidth=5*speed/speed.max())
plt.show()

enter image description here

Another option is to use VTK. It's accelerated 3D plotting, so making a 2D plot will require setting the camera properly (which isn't too hard), and you won't be able to get vector output.

Mayavi, tvtk, and mlab provide pythonic wrappers for VTK. It has lots of functionality along these lines.

The easiest way to use VTK to plot streamlines from numpy arrays is to use mayavi.mlab.flow. I'll skip an example for the moment, but if you want to explore using VTK to do this, I can add one.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 1
    This could be updated to point to [`matplotlib.pyplot.streamplot`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.streamplot) which is an improved version, its history [here](https://github.com/johnyf/matplotlib/commits/master/lib/matplotlib/streamplot.py?page=2) – 0 _ Dec 07 '13 at 19:01
4

In version 1.2 of Matplotlib, there is now a streamplot function.

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
3

Have a look at matplotlib's quiver: http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html

ev-br
  • 24,968
  • 9
  • 65
  • 78
  • this almost does it!!, but i need more scalar iso contours than vector graphs , +1 though. – fedvasu Nov 28 '11 at 14:45
  • If you need scalar contour plot, then google for `matplotlib contour`. – ev-br Nov 28 '11 at 18:14
  • i know, scalar contour plot of matplotlib just takes one variable and understandably so , i was looking if there is a function which takes components of vector , calculates and plots streamlines in given domain, contour is **not** that function/procedure. – fedvasu Nov 28 '11 at 20:34
  • 1
    (after looking up the definition of streamlines) ok, I see now. Basically what you're after is a set of lines to which the `quiver` arrows would be tangents, is this right? In that case what I'd do, I'd look into the source of `quiver` as it'll be a good approximation to what you need. – ev-br Nov 28 '11 at 22:16