2017-10-19 3 views
0

Ich versuche, rationalisiert und Geschwindigkeitspotential für Grundpotentialströmungen zu plotten (Uniform, Quelle/Senke, Wirbel, etc.)Plotten rationalisiert mit Python

ich nur mit Python aus bin ab und so bin ich ein wenig verwirrt. Ich folge this guide ..

ich diese Funktion, um die Stromlinien für die Strömung um einen Zylinder mit plotten können

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) 

und es funktioniert. Aber wenn ich die Rückkehr Anweisung

return U * r * sympy.cos(theta) 

für gleichmäßige Strömung ändern bekomme ich folgende Fehler

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' 

ich den Typen für die Rückkehr Objekt geprüft und es ist <class 'sympy.core.mul.Mul'> mit der ersten Rückrechnung und <class 'sympy.core.symbol.Symbol'> mit der zweite. Vielleicht hängt das damit zusammen, warum es nicht funktioniert, aber ich weiß nicht wie?

ich plotten die Stromlinien mit den folgenden

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() 

Kann jemand bitte helfen Sie mir zu verstehen, warum dies nicht funktioniert und wie ich bekommen kann es zu arbeiten? Vielen Dank!

+0

Für ein nächstes Mal: ​​bitte tracebacks mit einem Code-Block formatieren: das macht es viel besser zu lesen. – Evert

Antwort

0

Der Grund, dass dies nicht funktioniert, ist der Klassenunterschied. Ihre Funktion U * r * sympy.cos (Theta) = y. Das bedeutet, dass Sie nur eine Funktion von y zurückgeben. Daher ist Ihre -psi.diff (x) = 0 und Sie erhalten eine ganze Zahl für v.

Es ist unmöglich, Stromlinien mit 1D Daten zu plotten. Um also Stromlinien in 2D zu plotten, müssen Sie sowohl x als auch y in Ihrer uniform_flow_stream_function haben.