【发布时间】:2021-07-07 19:17:53
【问题描述】:
Python
在我的设置中,我有一个带有关联网格点的网格。我称这个变量为 r_grid。然后我生成了粒子、源点等。我想做的是将粒子沉积到网格的特定部分的四个周围节点上,粒子根据其与节点的距离发现自己所在的网格。请注意,这些源点的生成使得它们中的大多数最终位于中间(高斯分布)。到目前为止,我已经编写了代码来生成网格、源点以及指定构成整个网格一部分的四个网格点的起点。我的想法是我可以遍历每个网格部分,确定源点是否在其中,然后从那里做一些数学运算,将粒子的电荷分布为 r 的函数,它与四个周围的每一个的距离节点。我将在这里发布我的代码:
import numpy as np
import matplotlib.pyplot as plt
#creating grid
index = 3
x = np.linspace(-1, 1, index)
y = np.linspace(-1, 1, index)
x_1, y_1 = np.meshgrid(x, y)
r_grid = np.vstack((x_1.flatten(), y_1.flatten())).transpose()
r_grid.shape
print(r_grid)
# generating source points
# starting with a small number to start with
S = 1
r_s = np.random.randn(S, 2)/3.0
r_s.shape
# plots the four different nodes for the purposes of visualization
for i in range(len(r_grid)):
j = 0
if (i + 1) == (j + index):
j = j + 1
i = i + 1
else:
plt.plot(x_1, y_1, 'bo')
plt.plot(r_s[:, 0], r_s[:, 1], 'ro', linestyle='None', marker='.', ms=4)
plt.plot(r_grid[i, 0], r_grid[i, 1], 'ro')
plt.plot(r_grid[i + 1, 0], r_grid[i + 1, 1], 'bo')
plt.plot(r_grid[i + index, 0], r_grid[i + index, 1], 'go')
plt.plot(r_grid[i + (index + 1), 0], r_grid[i + (index + 1), 1], 'mo')
plt.show()
这是我目前所拥有的!我知道 r_grid 中的点对应于什么,以便以后可以在我按顺序遍历每个部分以确定粒子是否在所述部分和索引 i 中时使用该信息循环。如果我能确定一个点是否在指定的框中,我认为这会给我一个好的开始。我在这里碰壁了,所以我想我会把它贴在这里!那么,问题来了:如何有效地遍历由四个节点表示的每个正方形,然后确定一个粒子(已随机生成)是否在该框中?谢谢!
【问题讨论】: