1

I am trying to plot streamlines and velocity potential for basic potential flows (uniform, source/sink, vortex, etc.)

I am just starting out with python and so I'm a little confused. I am following this guide..

I can plot the streamlines for the flow around a cylinder using this function

def cylinder_stream_function(U=1, R=1):
  r = sympy.sqrt(x**2 + y**2)
  theta = sympy.atan2(y, x)
  return U * (r - R**2 / r) * sympy.sin(theta)

and it works. But when I change the return statement to

return U * r * sympy.cos(theta)

for uniform flow I get the following error

Traceback (most recent call last):
   File "test.py", line 42, in
<module>
    plot_streamlines(ax, u, v)
   File "test.py", line 32, in plot_streamlines
    ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')
   File "/usr/local/lib/python3.6/site-packages/matplotlib/__init__.py",
line 1710, in inner
    return func(ax, *args, **kwargs)
   File "/usr/local/lib/python3.6/site-packages/matplotlib/axes/_axes.py",
line 4688, in streamplot
    integration_direction=integration_direction)
   File "/usr/local/lib/python3.6/site-packages/matplotlib/streamplot.py",
line 136, in streamplot
    if (u.shape != grid.shape) or (v.shape != grid.shape):
   AttributeError: 'int' object has no attribute 'shape'

I checked the type for the return object and it is <class 'sympy.core.mul.Mul'> with the first return statement and <class 'sympy.core.symbol.Symbol'> with the second one. Maybe this is related to why it doesn't work but I'm not sure how?

I plot the streamlines with the following

import numpy as np
import matplotlib.pyplot as plt
import sympy
from sympy.abc import x, y 

def uniform_flow_stream_function(U=1):
  r = sympy.sqrt(x**2 + y**2)
  theta = sympy.atan2(y, x)
  return U * r * sympy.sin(theta)

def velocity_field(psi):
  u = sympy.lambdify((x, y), psi.diff(y), 'numpy')
  v = sympy.lambdify((x, y), -psi.diff(x), 'numpy')
  return u, v

def plot_streamlines(ax, u, v, xlim=(-4, 4), ylim=(-4, 4)):
    x0, x1 = xlim
    y0, y1 = ylim
    # create a grid of values
    Y, X =  np.ogrid[y0:y1:100j, x0:x1:100j]
    ax.streamplot(X, Y, u(X, Y), v(X, Y), color='cornflowerblue')

psi = uniform_flow_stream_function()
u, v = velocity_field(psi)
fig, ax = plt.subplots(figsize=(5, 5))

plot_streamlines(ax, u, v)
plt.show()

Can someone please help me understand why this doesn't work and how I can get it to work? Thank you!

4

1 回答 1

0

这不起作用的原因是因为阶级差异。你的函数 U * r * sympy.cos(theta)=y。这意味着您返回的函数只有 y。因此,您的 -psi.diff(x)=0 并且您得到 v 的整数。

用一维数据绘制流线是不可能的。因此,为了在 2D 中绘制流线,您的uniform_flow_stream_function.

于 2017-10-19T04:44:46.983 回答