在 Python 中使用用户输入调用/选择变量(浮点值)

发布于 2024-09-03 07:03:13 字数 2879 浏览 4 评论 0原文

我一直在从事一个计算物理项目(绘制化学反应物相对于彼此的相关速率以显示振荡行为)并取得了相当大的成功。然而,我的一个模拟涉及两种以上的活性振荡剂(实际上是五种),这显然不适合任何单一的视觉图...

因此,我的方案是让用户选择他们想要在 x 上绘制的两种反应物分别为 - 轴和 y 轴。我尝试(愚蠢地)将字符串输入值转换为相应的变量名称,但我想我需要一种完全不同的方法(如果存在)?

如果它有助于澄清任何问题,这是我的代码的一部分:

def coupledBrusselator(A, B, t_trial,display_x,display_y):
    t = 0
    t_step = .01
    X = 0       
    Y = 0
    E = 0
    U = 0
    V = 0
    dX = (A) - (B+1)*(X) + (X**2)*(Y)  
    dY = (B)*(X) - (X**2)*(Y)
    dE = -(E)*(U) - (X)  
    dU = (U**2)*(V) -(E+1)*(U) - (B)*(X)
    dV = (E)*(U) - (U**2)*(V)
    array_t = [0]
    array_X = [0]
    array_Y = [0]
    array_U = [0]
    array_V = [0]       

    while t <= t_trial:             
        X_1 = X + (dX)*(t_step/2)   
        Y_1 = Y + (dY)*(t_step/2)
        E_1 = E + (dE)*(t_step/2)
        U_1 = U + (dU)*(t_step/2)
        V_1 = V + (dV)*(t_step/2) 
        dX_1 = (A) - (B+1)*(X_1) + (X_1**2)*(Y_1)  
        dY_1 = (B)*(X_1) - (X_1**2)*(Y_1)
        dE_1 = -(E_1)*(U_1) - (X_1)  
        dU_1 = (U_1**2)*(V_1) -(E_1+1)*(U_1) - (B)*(X_1)
        dV_1 = (E_1)*(U_1) - (U_1**2)*(V_1)
        X_2 = X + (dX_1)*(t_step/2)
        Y_2 = Y + (dY_1)*(t_step/2)
        E_2 = E + (dE_1)*(t_step/2)
        U_2 = U + (dU_1)*(t_step/2)
        V_2 = V + (dV_1)*(t_step/2) 
        dX_2 = (A) - (B+1)*(X_2) + (X_2**2)*(Y_2)
        dY_2 = (B)*(X_2) - (X_2**2)*(Y_2)
        dE_2 = -(E_2)*(U_2) - (X_2)  
        dU_2 = (U_2**2)*(V_2) -(E_2+1)*(U_2) - (B)*(X_2)
        dV_2 = (E_2)*(U_2) - (U_2**2)*(V_2)   
        X_3 = X + (dX_2)*(t_step)
        Y_3 = Y + (dY_2)*(t_step)
        E_3 = E + (dE_2)*(t_step)
        U_3 = U + (dU_2)*(t_step)
        V_3 = V + (dV_2)*(t_step) 
        dX_3 = (A) - (B+1)*(X_3) + (X_3**2)*(Y_3)
        dY_3 = (B)*(X_3) - (X_3**2)*(Y_3)
        dE_3 = -(E_3)*(U_3) - (X_3)  
        dU_3 = (U_3**2)*(V_3) -(E_3+1)*(U_3) - (B)*(X_3)
        dV_3 = (E_3)*(U_3) - (U_3**2)*(V_3) 
        X = X + ((dX + 2*dX_1 + 2*dX_2 + dX_3)/6) * t_step 
        Y = Y + ((dX + 2*dY_1 + 2*dY_2 + dY_3)/6) * t_step
        E = E + ((dE + 2*dE_1 + 2*dE_2 + dE_3)/6) * t_step          
        U = U + ((dU + 2*dU_1 + 2*dY_2 + dE_3)/6) * t_step
        V = V + ((dV + 2*dV_1 + 2*dV_2 + dE_3)/6) * t_step
        dX = (A) - (B+1)*(X) + (X**2)*(Y)  
        dY = (B)*(X) - (X**2)*(Y)
        t_step = .01 / (1 + dX**2 + dY**2) ** .5
        t = t + t_step
        array_X.append(X) 
        array_Y.append(Y)
        array_E.append(E)
        array_U.append(U)
        array_V.append(V)
        array_t.append(t)   

之前的位置

display_x = raw_input("Choose catalyst you wish to analyze in the phase/field diagrams (X, Y, E, U, or V) ") 
display_y = raw_input("Choose one other catalyst from list you wish to include in phase/field diagrams ")

coupledBrusselator(A, B, t_trial, display_x, display_y) 

谢谢!

I've been working on a computational physics project (plotting related rates of chemical reactants with respect to eachother to show oscillatory behavior) with a fair amount of success. However, one of my simulations involves more than two active oscillating agents (five, in fact) which would obviously be unsuitable for any single visual plot...

My scheme was hence to have the user select which two reactants they wanted plotted on the x-axis and y-axis respectively. I tried (foolishly) to convert string input values into the respective variable names, but I guess I need a radically different approach if any exist?

If it helps clarify any, here is part of my code:

def coupledBrusselator(A, B, t_trial,display_x,display_y):
    t = 0
    t_step = .01
    X = 0       
    Y = 0
    E = 0
    U = 0
    V = 0
    dX = (A) - (B+1)*(X) + (X**2)*(Y)  
    dY = (B)*(X) - (X**2)*(Y)
    dE = -(E)*(U) - (X)  
    dU = (U**2)*(V) -(E+1)*(U) - (B)*(X)
    dV = (E)*(U) - (U**2)*(V)
    array_t = [0]
    array_X = [0]
    array_Y = [0]
    array_U = [0]
    array_V = [0]       

    while t <= t_trial:             
        X_1 = X + (dX)*(t_step/2)   
        Y_1 = Y + (dY)*(t_step/2)
        E_1 = E + (dE)*(t_step/2)
        U_1 = U + (dU)*(t_step/2)
        V_1 = V + (dV)*(t_step/2) 
        dX_1 = (A) - (B+1)*(X_1) + (X_1**2)*(Y_1)  
        dY_1 = (B)*(X_1) - (X_1**2)*(Y_1)
        dE_1 = -(E_1)*(U_1) - (X_1)  
        dU_1 = (U_1**2)*(V_1) -(E_1+1)*(U_1) - (B)*(X_1)
        dV_1 = (E_1)*(U_1) - (U_1**2)*(V_1)
        X_2 = X + (dX_1)*(t_step/2)
        Y_2 = Y + (dY_1)*(t_step/2)
        E_2 = E + (dE_1)*(t_step/2)
        U_2 = U + (dU_1)*(t_step/2)
        V_2 = V + (dV_1)*(t_step/2) 
        dX_2 = (A) - (B+1)*(X_2) + (X_2**2)*(Y_2)
        dY_2 = (B)*(X_2) - (X_2**2)*(Y_2)
        dE_2 = -(E_2)*(U_2) - (X_2)  
        dU_2 = (U_2**2)*(V_2) -(E_2+1)*(U_2) - (B)*(X_2)
        dV_2 = (E_2)*(U_2) - (U_2**2)*(V_2)   
        X_3 = X + (dX_2)*(t_step)
        Y_3 = Y + (dY_2)*(t_step)
        E_3 = E + (dE_2)*(t_step)
        U_3 = U + (dU_2)*(t_step)
        V_3 = V + (dV_2)*(t_step) 
        dX_3 = (A) - (B+1)*(X_3) + (X_3**2)*(Y_3)
        dY_3 = (B)*(X_3) - (X_3**2)*(Y_3)
        dE_3 = -(E_3)*(U_3) - (X_3)  
        dU_3 = (U_3**2)*(V_3) -(E_3+1)*(U_3) - (B)*(X_3)
        dV_3 = (E_3)*(U_3) - (U_3**2)*(V_3) 
        X = X + ((dX + 2*dX_1 + 2*dX_2 + dX_3)/6) * t_step 
        Y = Y + ((dX + 2*dY_1 + 2*dY_2 + dY_3)/6) * t_step
        E = E + ((dE + 2*dE_1 + 2*dE_2 + dE_3)/6) * t_step          
        U = U + ((dU + 2*dU_1 + 2*dY_2 + dE_3)/6) * t_step
        V = V + ((dV + 2*dV_1 + 2*dV_2 + dE_3)/6) * t_step
        dX = (A) - (B+1)*(X) + (X**2)*(Y)  
        dY = (B)*(X) - (X**2)*(Y)
        t_step = .01 / (1 + dX**2 + dY**2) ** .5
        t = t + t_step
        array_X.append(X) 
        array_Y.append(Y)
        array_E.append(E)
        array_U.append(U)
        array_V.append(V)
        array_t.append(t)   

where previously

display_x = raw_input("Choose catalyst you wish to analyze in the phase/field diagrams (X, Y, E, U, or V) ") 
display_y = raw_input("Choose one other catalyst from list you wish to include in phase/field diagrams ")

coupledBrusselator(A, B, t_trial, display_x, display_y) 

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-09-10 07:03:13

计算出不同的数组后,您可以将它们添加到将名称映射到数组的 dict 中。然后可以使用它来查找 display_xdisplay_y 的正确数组:

named_arrays = {
  "X": array_X,
  "Y": array_Y,
  "E": array_E,
  ...
}

return (named_arrays[display_x], named_arrays[display_y])

Once you have calculated the different arrays, you could add them to a dict that maps names to arrays. This can then be used to look up the correct arrays for display_x and display_y:

named_arrays = {
  "X": array_X,
  "Y": array_Y,
  "E": array_E,
  ...
}

return (named_arrays[display_x], named_arrays[display_y])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文