我试图在2D中模拟一个聚合物,其中一个聚合物是由多个单体组成的。模拟的方法是通过随机化两个单体之间的夹角。这显然是一个很大的简化:每个单体都是一条线,末端是一个球体。条件是球体不能重叠。我遇到的问题是,如果球体确实重叠,continue
命令将完成当前迭代,但不会创建单体,这意味着如果发生1个重叠,则将比预期少1个单体。需要发生的是,如果它重叠,它将尝试再次构建单体,直到它不与任何其他单体重叠。我尝试了一个while
循环,但我在循环中构建了每个单体,所以我无法弄清楚。重叠检查(check_monomer_distance
)需要检查已生成的每个单体。(我也需要在3D中进行此模拟,但我关注的是2DATM,这就是为什么有一个未使用的变量dim
)
import numpy as np
import random
dim = 2
monomer_length = 10
edge_diameter = 1 #radius of spheres
polymer_length = 100 # a single polymer is made out of many monomers of identical length
d_angle = 10
d_theta_range = np.arange(0.0, 2 * np.pi, d_angle)
last_coordinates= np.zeros(int(dim))
list_coo = [np.zeros(int(dim))] #every element in this list is the coordinates of a monomer, which means the polymer at its entirety
for n_m in range(0, polymer_length): # n_m == Number of monomer
random_d = random.choice(d_theta_range) # The angle between one monomer to the next
step = monomer_length * np.array((np.cos(random_d), np.sin(random_d)))
next_coordinates = last_coordinates + step
if not check_monomer_distance(list_coo, next_coordinates, dim): # check to see if monomers overlap
continue
last_coordinates = next_coordinates
list_coo.append(next_coordinates)
def check_monomer_distance(list_coo, next_coordinates, dim):
for i in range(len(list_coo)-1): # The minus 1 is because it will never overlap with the first previous monomer
vector = next_coordinates - list_coo[i] # vector from the proposed monomer to every other generated
vector_length = np.sqrt(vector[0]**2 + vector[1]**2)
if vector_length < edge_diameter:
return False # The return values need to be like this I can't change them: if the spheres overlap return False
return True
我需要在最后运行一些分析,这就是为什么聚合物是一个列表(list_coo
),我感兴趣的是分析,而不是聚合物本身。角度的随机化需要像这样,我知道这有点麻烦。
发布于 2021-01-19 01:02:26
非常接近:您用文字准确地描述了逻辑,但没有在代码中实现它。这是一个典型的“直到有效”循环,在许多语言中被视为“虽然无效”。
for n_m in range(0, polymer_length): # n_m == Number of monomer
valid = False
while not valid # Repeat this loop until it generates a non-overlapping monomer.
random_d = random.choice(d_theta_range) # The angle between one monomer to the next
step = monomer_length * np.array((np.cos(random_d), np.sin(random_d)))
next_coordinates = last_coordinates + step
valid = check_monomer_distance(list_coo, next_coordinates, dim):
last_coordinates = next_coordinates
list_coo.append(next_coordinates)
发布于 2021-01-19 10:33:12
通过利用while结构和continue/break语句,可以避免使用额外的变量
在满足条件时使用break
停止:
for n_m in range(0, polymer_length): # n_m == Number of monomer
while True: # repeat while condition is not met, break when it is
random_d = random.choice(d_theta_range) # The angle between one monomer to the next
step = monomer_length * np.array((np.cos(random_d), np.sin(random_d)))
next_coordinates = last_coordinates + step
if check_monomer_distance(list_coo, next_coordinates, dim): # check to see if monomers overlap
break
last_coordinates = next_coordinates
list_coo.append(next_coordinates)
或者在不满足条件时使用continue
进行循环:
for n_m in range(0, polymer_length): # n_m == Number of monomer
while True: # continue loop until condition met, break when done
random_d = random.choice(d_theta_range) # The angle between one monomer to the next
step = monomer_length * np.array((np.cos(random_d), np.sin(random_d)))
next_coordinates = last_coordinates + step
if not check_monomer_distance(list_coo, next_coordinates, dim): # check to see if monomers overlap
continue
last_coordinates = next_coordinates
list_coo.append(next_coordinates)
break
https://stackoverflow.com/questions/65778853
复制相似问题