我想在极坐标图上绘制围绕旋转圆柱体的流动的速度方程。(方程式来自 Andersen 的“空气动力学基础”。)您可以在 for 循环语句中看到这两个方程式。
我不能大声喊叫设法将计算出的数据表示到极坐标图上。我已经尝试了我的每一个想法,但一无所获。我确实检查了数据,这似乎是正确的,因为它表现得应该如何。
这是我上次尝试的代码:
import numpy as np
import matplotlib.pyplot as plt
RadiusColumn = 1.0
VelocityInfinity = 10.0
RPM_Columns = 0.0#
ColumnOmega = (2*np.pi*RPM_Columns)/(60)#rad/s
VortexStrength = 2*np.pi*RadiusColumn**2 * ColumnOmega#rad m^2/s
NumberRadii = 6
NumberThetas = 19
theta = np.linspace(0,2*np.pi,NumberThetas)
radius = np.linspace(RadiusColumn, 10 * RadiusColumn, NumberRadii)
f = plt.figure()
ax = f.add_subplot(111, polar=True)
for r in xrange(len(radius)):
for t in xrange(len(theta)):
VelocityRadius = (1.0 - (RadiusColumn**2/radius[r]**2)) * VelocityInfinity * np.cos(theta[t])
VelocityTheta = - (1.0 + (RadiusColumn**2/radius[r]**2))* VelocityInfinity * np.sin(theta[t]) - (VortexStrength/(2*np.pi*radius[r]))
TotalVelocity = np.linalg.norm((VelocityRadius, VelocityTheta))
ax.quiver(theta[t], radius[r], theta[t] + VelocityTheta/TotalVelocity, radius[r] + VelocityRadius/TotalVelocity)
plt.show()
如您所见,我现在已将 RPM 设置为 0。这意味着流量应该从左向右流动,并且在水平轴上对称。(流动应该在两侧以相同的方式围绕圆柱体流动。)然而,结果看起来更像这样:
这完全是胡说八道。似乎有一个漩涡,即使没有设置!更奇怪的是,当我只显示从 0 到 pi/2 的数据时,流程会发生变化!
正如您从代码中看到的那样,我尝试使用单位向量,但显然这不是要走的路。我将不胜感激任何有用的输入。
谢谢!
基本问题是.quiver
极坐标Axes
对象的方法仍然需要笛卡尔坐标中的矢量分量,因此您需要自己将 theta 和径向分量转换为 x 和 y:
for r in range(len(radius)):
for t in range(len(theta)):
VelocityRadius = (1.0 - (RadiusColumn**2/radius[r]**2)) * VelocityInfinity * np.cos(theta[t])
VelocityTheta = - (1.0 + (RadiusColumn**2/radius[r]**2))* VelocityInfinity * np.sin(theta[t]) - (VortexStrength/(2*np.pi*radius[r]))
TotalVelocity = np.linalg.norm((VelocityRadius, VelocityTheta))
ax.quiver(theta[t], radius[r],
VelocityRadius/TotalVelocity*np.cos(theta[t])
- VelocityTheta/TotalVelocity*np.sin(theta[t]),
VelocityRadius/TotalVelocity*np.sin(theta[t])
+ VelocityTheta/TotalVelocity*np.cos(theta[t]))
plt.show()
However, you can improve your code a lot by making use of vectorization: you don't need to loop over each point to obtain what you need. So the equivalent of your code, but even clearer:
def pol2cart(th,v_th,v_r):
"""convert polar velocity components to Cartesian, return v_x,v_y"""
return v_r*np.cos(th) - v_th*np.sin(th), v_r*np.sin(th) + v_th*np.cos(th)
theta = np.linspace(0, 2*np.pi, NumberThetas, endpoint=False)
radius = np.linspace(RadiusColumn, 10 * RadiusColumn, NumberRadii)[:,None]
f = plt.figure()
ax = f.add_subplot(111, polar=True)
VelocityRadius = (1.0 - (RadiusColumn**2/radius**2)) * VelocityInfinity * np.cos(theta)
VelocityTheta = - (1.0 + (RadiusColumn**2/radius**2))* VelocityInfinity * np.sin(theta) - (VortexStrength/(2*np.pi*radius))
TotalVelocity = np.linalg.norm([VelocityRadius, VelocityTheta],axis=0)
VelocityX,VelocityY = pol2cart(theta, VelocityTheta, VelocityRadius)
ax.quiver(theta, radius, VelocityX/TotalVelocity, VelocityY/TotalVelocity)
plt.show()
Few notable changes:
endpoint=False
to theta
: since your function is periodic in 2*pi
, you don't need to plot the endpoints twice. Note that this means that currently you have more visible arrows; if you want the original behaviour I suggest that you decrease NumberThetas
by one.[:,None]
to radius
: this will make it a 2d array, so later operations in the definition of the velocities will give you 2d arrays: different columns correspond to different angles, different rows correspond to different radii. quiver
is compatible with array-valued input, so a single call to quiver
will do your work.np.linalg.norm
on essentially a 3d array, but this works as expected if we specify an axis to work over.pol2cart
auxiliary function to do the conversion from polar to Cartesian components; this is not necessary but it seems clearer to me this way.Final remark: I suggest choosing shorter variable names, and ones that don't have CamelCase. That would probably make your coding faster too.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句