1

I have a function that initialises an array:

def array(dim):
    return np.zeros((dim,dim), float)

and I also have a boundary condition function that specifies the BC for a particular system(since it could change):

def boundcond(v, K):
    """
    v is some value
    K is an array
    """
    for k in range(10, 31, 1):
        K[k, 18] = v     #these are boundary conditions that I want for this
        K[k, 22] = -v    #particular scenario
return K

for which both I'll want to feed into an iterative algorithm of mine:

def iteration(K, v, BC): 
    """
    K is an initialised array fed in
    v is again a scalar value from boundcond(v, K)
    BC is what I'd want a boundary condition FUNCTION to put in
    """
    for i in range(1, dim-1):
        for j in range(1, dim-1):
            K[i, j] = (1/4)*(K[i+1, j]+K[i-1, j]+K[i, j+1]+K[i, j-1])
            boundcond(v, K)  # I want every intermediate K[i, j] during the iteration
                             # to run through boundcond(v, K), such that some K[i, j] gets
                             # changed to the BC values
return K # so that in the end it returns K

However my problem comes when I try to call my iteration function with inputs in it, since by running it for say a 50x50 array, v has some scalar value of say 10 and putting my boundary condition function in.

iteration(array(40), 10, boundcond(10, K))

Here if I run the line above, the initial K is defined as array(40) and my intermediate K within boundcond(10, K) is not defined until my initial K gets iterated within the i,j for loops. If it take it out then it means I can't put my boundcond(10, K) within my iteration(K, v, BC).

Does anyone have a way round this?

I'm doing it this way because I have other scenarios with different BC that I'll want to feed into this iteration(K, v, BC). For example I can have another BC called boundcond2().

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
user3613025
  • 373
  • 3
  • 10
  • you can simply pass the function name: `iteration(array(40), 10, boundcond)` and inside the function call `BC(v,K)` – Patrick Artner Mar 03 '19 at 15:42
  • thanks, I didn't know that I can call a function as a parameter without its own parameters. If you want to post your comment as the official answer I'll mark it as solved. – user3613025 Mar 03 '19 at 16:35
  • You can also accept the duplicate if that solved your problem. :o) thanks for asking though. – Patrick Artner Mar 03 '19 at 16:39

0 Answers0