简体   繁体   English

在循环外定义和访问变量

[英]Defining and accessing the variables outside of loop

I want to define or access the eight variables J1L, J2L, J3L, J4L and J1R, J2R, J3R, J4R outside of loop. 我想在循环外定义或访问八个变量J1L,J2L,J3L,J4L和J1R,J2R,J3R,J4R。 To make it clear, here in the code, I am defining these eight quantities inside the loop, is there a way to define them outside? 为了清楚起见,在代码中,我在循环中定义了这八个量,是否可以在外部定义它们? Or, as they are already defined here, how can I use them outside the loop. 或者,正如这里已经定义的那样,如何在循环之外使用它们。 Here is the relevant part of the code: 这是代码的相关部分:

import numpy as np

def sweep():
    num_cells = 10
    phi = np.ones(num_cells)
    psi = np.zeros((num_cells + 1, 4))
    cur = np.ones(num_cells)
    mu = np.array([-0.8611363115, -0.3399810435, 0.3399810435, 0.8611363115])
    w = np.array([0.3478548451, 0.6521451549, 0.6521451549, 0.3478548451])
    tolerance = 1e-6
    max_it = 100
    err_phi = 1
    it = 0
    A = np.zeros((num_cells, 4))
    B = np.zeros((num_cells, 4))
    S = np.ones((num_cells, 4)) * 0.5
    Q = np.copy(S)
    L = 10.0
    sigma_s = 1.0
    sigma_t = 2.0
    dx = L / num_cells

for i in range(0, num_cells):
    for n in range(0, 4):
        smu = np.sign(mu[n])
        denom = 2.0 * mu[n] + smu * sigma_t * dx
        A[i, n] = (2.0 * mu[n] - smu * sigma_t * dx) / denom
        B[i, n] = smu * 2.0 * dx / denom

while err_phi > tolerance and it <= max_it:
    phi_old = np.zeros(num_cells)
    for i in range(num_cells):
        phi_old[i] = phi[i]

    for i in range(num_cells):
        Q[i, :] = S[i, :] + 0.5 * phi[i] * sigma_s

    #Boundary Condition
    for i in range(0, num_cells):
    psi[i, :] = 0.0

    # Right to left
    for i in range(num_cells - 1, -1, -1):
    psi[i, 0:2] = A[i, 0:2] * psi[i + 1, 0:2] + B[i, 0:2] * Q[i, 0:2]
        J1L = psi[9, 0:2] * mu[0:2]        
        J2L = psi[6, 0:2] * mu[0:2] 
        J3L = psi[3, 0:2] * mu[0:2]
        J4L = psi[0, 0:2] * mu[0:2]
        print J1L, J2L, J3L, J4L

    # Left to right
    for i in range(0, num_cells):
        psi[i + 1, 2:4] = A[i, 2:4] * psi[i, 2:4] + B[i, 2:4] * Q[i, 2:4]
        J1R = psi[0, 0:2] * mu[0:2]        
        J2R = psi[3, 0:2] * mu[0:2] 
        J3R = psi[6, 0:2] * mu[0:2]
        J4R = psi[9, 0:2] * mu[0:2]
        print J1L, J2L, J3L, J4L

    # Update phi
    for i in range(num_cells):
        tot = 0.0
        for n in range(0, 4):
            tot = tot + w[n] * 0.5 * (psi[i, n] + psi[i + 1, n])
        phi[i] = tot

    err_phi = np.amax(np.absolute(((phi - phi_old) / phi_old)))
    it = it + 1
    if it <= max_it:
        print(("converged in", it, "iterations"))

return phi

You could define them at the top of your method by setting each variable equal to zero. 您可以通过将每个变量设置为零来在方法的顶部定义它们。 The values will change in your loops and then at the end of the loop, the values will remain what they were at the end. 这些值将在循环中更改,然后在循环结束时,这些值将保持它们在结束时的状态。

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

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