如何创建多个Python函数的迭代循环?

时间:2015-02-03 14:43:03

标签: python iterator numerical-methods

我正在尝试编写一个Python代码,它是一个具有温度相关导热系数的1-d热传导(使用FVM)的数值求解器。

求解器有三个函数我需要迭代直到收敛:

电导率函数:对温度场进行初始猜测并输出节点电导率,具体取决于节点温度的过去值。

节点系数:根据当前节点电导率计算节点系数。

节点温度:使用TDMA算法根据当前系数计算节点温度。

一旦计算出温度,就要将这些值循环到电导率函数以继续循环直到收敛。

当电导率恒定时,我可以使这项工作正常,但是当我尝试实现这种伪代码时开始出现问题。我得到最大递归深度超出错误。我的代码如下:

import numpy as np
import matplotlib.pyplot as plt
import copy

# SOLUTION ALGORITHM

# Step 1: DATA  
def data(n, hw, he, TinfW, TinfE):    
    n = 5
    hw = 10**10
    he = 10**-10
    TinfW = 100
    TinfE = 10**12

    return n, hw, he, TinfW, TinfE    

# Step 2: GRID    
def grid(x1, x2, data):
    x1 = 0 
    x2 = 0.5
    xe = (x2-x1) / data(n, hw, he, TinfW, TinfE)[0]

    return xe

# Step 3: GAMSOR     
def gamsor(k, q, TDMA):
    k0 = 100
    beta = 20
    T0 = 25   
    for i in range(data(n, hw, he, TinfW, TinfE)[0]):
        k[i] = k0 + beta#*(TDMA(data, coeff)[i]-T0)
        q[i] = 10**5

    return k, q

 #Step 4: COEFF     
def coeff(Ae, Aw, Ap, data, gamsor, grid):
    for i in range(data(n, hw, he, TinfW, TinfE)[0]):       
        Ae[i] = Aw[i] = gamsor(k,q, TDMA(data, coeff))[0][i] /     grid(x1, x2, data)
        Ap[i] = Ae[i] + Aw[i]  

# Define "b" array - including source term and boundary conditions
        b = np.zeros((data(n, hw, he, TinfW, TinfE)[0], 1))
        b.fill(gamsor(k,q, TDMA(data, coeff))[1][i])    

        b[0] = (gamsor(k,q, TDMA(data, coeff))[1][i]*(grid(x1,x2,data)/2) + 
           data(n, hw, he, TinfW, TinfE)[1]*
           data(n, hw, he, TinfW, TinfE)[3])

        b[-1] = (gamsor(k,q, TDMA(data, coeff))[1][i]*(grid(x1,x2,data)/2)+ 
            data(n, hw, he, TinfW, TinfE)[2]*
            data(n, hw, he, TinfW, TinfE)[4])

# Change values in first and last coefficients to reflect specified BC's
        Aw[0] = 0
        Ap[0] = Ae[0] + Aw[0] + data(n, hw, he, TinfW, TinfE)[1]

        Ae[-1] = 0
        Ap[-1] = Ae[0] + Aw[0] + data(n, hw, he, TinfW, TinfE)[2]

    return Ae, Aw, Ap, b

 #Step 5: SOLVE
def TDMA(data, coeff):
# Initialize "T" array - the solution array
    T = np.zeros(data(n, hw, he, TinfW, TinfE)[0]);

# Initialize recursion functions as arrays
    P = np.zeros(data(n, hw, he, TinfW, TinfE)[0]);
    Q = np.zeros(data(n, hw, he, TinfW, TinfE)[0]);

## Step 1: evaluate at node 1
    P[0] = (coeff(Ae, Aw, Ap, data, gamsor, grid)[0][0] / 
       coeff(Ae, Aw, Ap, data, gamsor, grid)[2][0])

    Q[0] = (coeff(Ae, Aw, Ap, data, gamsor, grid)[3][0] / 
       coeff(Ae, Aw, Ap, data, gamsor, grid)[2][0])

## Step 2: sweep from node 2 to node (n-1) (python
## cell 1->(n-2) ) evaluating P and Q

    for i in range(1, data(n, hw, he, TinfW, TinfE)[0]-1):
        P[i] = ((coeff(Ae, Aw, Ap, data, gamsor, grid)[0][i]) / 
           (coeff(Ae, Aw, Ap, data, gamsor, grid)[2][i] - 
           coeff(Ae, Aw, Ap, data, gamsor, grid)[1][i]*P[i-1]))

        Q[i] = ((coeff(Ae, Aw, Ap, data, gamsor, grid)[3][i] + 
           coeff(Ae, Aw, Ap, data, gamsor, grid)[1][i]*Q[i-1]) / 
           (coeff(Ae, Aw, Ap, data, gamsor, grid)[2][i] - 
           coeff(Ae, Aw, Ap, data, gamsor, grid)[1][i]*P[i-1]))

## Step 3: calculate for node n

    P[data(n, hw, he, TinfW, TinfE)[0]-1] = 0

    Q[data(n, hw, he, TinfW, TinfE)[0]-1] = ( 
(coeff(Ae, Aw, Ap, data, gamsor, grid)[3][data(n, hw, he, 
TinfW, TinfE)[0]-1] + coeff(Ae, Aw, Ap, data, gamsor, grid)
[1][data(n, hw, he, TinfW, TinfE)[0]-1]*Q[data(n, hw, he, TinfW, 
TinfE)[0]-2]) / (coeff(Ae, Aw, Ap, data, gamsor, grid)[2][data(n, 
hw, he, TinfW, TinfE)[0]-1] - coeff(Ae, Aw, Ap, data, gamsor, 
grid)[1][data(n, hw, he, TinfW, TinfE)[0]-1]*P[data(n, hw, he, 
TinfW, TinfE)[0]-2]))

    T[data(n, hw, he, TinfW, TinfE)[0]-1] = ( 
    Q[data(n, hw, he, TinfW, TinfE)[0]-1] )

## Step 4: back fill, giving the temperatures
    for i in range(data(n, hw, he, TinfW, TinfE)[0] - 2, -1, -1):
        T[i] = P[i]*T[i+1] + Q[i]

    return T

def Temps(TDMA):
    Temps = copy.copy(TDMA(data,coeff)); Temps.fill(100)
    return Temps

if __name__ == '__main__': 

# Initialize variables
    n = 0
    hw = 0
    he = 0
    TinfW = 0
    TinfE = 0

    k = np.zeros(data(n, hw, he, TinfW, TinfE)[0])
    q = np.zeros(data(n, hw, he, TinfW, TinfE)[0])
    k0 = 0
    beta = 20
    T0 = 0

    x1 = 0
    x2 = 0

    Ae = np.zeros(data(n, hw, he, TinfW, TinfE)[0])
    Aw = np.zeros(data(n, hw, he, TinfW, TinfE)[0])
    Ap = np.zeros(data(n, hw, he, TinfW, TinfE)[0])
    b = 0

    data(n, hw, he, TinfW, TinfE)
    grid(x1, x2, data)

    Temps(TDMA)
    for _ in range(1):
        gamsor(k, q)
        coeff(Ae, Aw, Ap, data, gamsor, grid)
        TDMA(data, coeff)

#print Temps(TDMA) 



    print "Nodal Temperature Distribution (C):", TDMA(data, coeff) 

1 个答案:

答案 0 :(得分:0)

如果我遇到问题,可能的解决方案是使用

之类的东西
from itertools import cycle
caller = cycle((func1, func2, func3))

然后你应该运行

caller.next()(<arguments>)

并将结果存储在某处以检查收敛性。