简体   繁体   English

有没有办法根据列表的长度动态更改变量

[英]Is there a way to dynamically change variables depending on length of list

May seem like a silly question is there any way to create variables depending on the number of objects in a list.可能看起来像一个愚蠢的问题是有什么方法可以根据列表中的对象数量创建变量。 The context to this is I am trying to create an n body simulation.这方面的背景是我正在尝试创建一个 n 身体模拟。 One in which the user can manually chose the planets they want in the solar system and then run the script with only those planets.一种用户可以在太阳系中手动选择他们想要的行星,然后仅使用这些行星运行脚本的一种方式。 So prior to them running the chosing the planets and running the script I will not know how many variables to create.所以在他们运行选择行星和运行脚本之前,我不知道要创建多少变量。 This problem of how many variables to create is show by: The mass is created by a class objects: class Objects():这个要创建多少变量的问题通过以下方式显示: 质量由 class 对象创建: class 对象():

position = np.full((1, 3), 0)
velocity = np.full((1, 3), 0)
acceleration = np.full((1, 3), 0)
name = ""
mass = np.full((1, 1), 0)

planets_init = np.full((1, 3), 0)

def __init__(self, Name, Mass, initPosition, initVelocity, initAcceleration):

    au = 149597870.700e3
    v_factor = 1731460

    self.name = Name
    self.mass = Mass

The function is solved by using scipy.integrate.solve_ivp by: function 通过使用 scipy.integrate.solve_ivp 解决:

three_body_sol = sci.integrate.solve_ivp(fun=Objects.ThreeBodyEquations,t_span=domain,y0=init_params,args=(G,planets_mass,N), max_step=max_step)

Where the function is:其中 function 是:

def ThreeBodyEquations(t,w,G,mass,N):
    # N being the number of objects in the system so in this case 2
    m1, m2 = mass

    #Unpack all the variables from the array "w"
    r1=w[:3]
    r2=w[3:6]
    v1=w[6:9]
    v2=w[9:12]
  
    # Harry's attempt
    G = G
    planets_pos = np.vstack((r1, r2))
    planets_mass = mass # np.vstack((m1, m2))

    # positions r = [x,y,z] for all particles
    x = planets_pos[:,0:1]
    y = planets_pos[:,1:2]
    z = planets_pos[:,2:3]


    # matrix that stores all pairwise particle separations: r_j - r_i
    dx = x.T - x
    dy = y.T - y
    dz = z.T - z

    # matrix that stores 1/r^3 for all particle pairwise particle separations
    inv_r3 = (dx**2 + dy**2 + dz**2)

    inv_r3[inv_r3>0] = inv_r3[inv_r3>0]**(-1.5)

    ax = G * (dx * inv_r3) @ planets_mass
    ay = G * (dy * inv_r3) @ planets_mass
    az = G * (dz * inv_r3) @ planets_mass

    # planets_acceleration = np.sqrt(ax**2 + ay**2 + az**2)
    planets_acceleration = np.vstack((ax,ay,az))
    planets_acceleration = planets_acceleration.flatten()

    dv1bydt=planets_acceleration[0::N]
    dv2bydt=planets_acceleration[1::N]
    # print(planets_acceleration)
    dr1bydt=v1
    dr2bydt=v2

    #Package the derivatives into one final size-18 array
    r12_derivs=np.concatenate((dr1bydt,dr2bydt))
    r_derivs = r12derivs    
    v12_derivs=np.concatenate((dv1bydt,dv2bydt))
    v_derivs= v12_derivs
    derivs=np.concatenate((r_derivs,v_derivs))
    return derivs

My main question centres around this function.我的主要问题围绕着这个 function。 When the user defines what planets they want to use I have no idea what the number of planets will be.当用户定义他们想要使用的行星时,我不知道行星的数量是多少。 I know the range which is might be but that's all.我知道可能的范围,但仅此而已。 Is there a feasible way to do this or is this a lost cause?有没有可行的方法来做到这一点,或者这是一个失败的原因?

I hope this clarifys the question with some additional code.我希望这可以通过一些额外的代码来澄清这个问题。 Sorry about the previous lack of code its, I didn't realise it was valuable at first and didn't want to burden with too much code.抱歉之前缺少代码,一开始我没有意识到它很有价值,也不想负担太多代码。

I would create a function that calculates the mass first, such as我将创建一个首先计算质量的 function,例如

      mass = 0
      for planet in planet_list:
            mass = mass+ planet.mass
      return mass

total_mass = calculate_combined_mass([earth, Mars, jupiter] )

three_body_equations(t,w,G,total_mass,N)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM