I want to define or access the eight variables J1L, J2L, J3L, J4L and J1R, J2R, J3R, J4R outside of loop. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.