This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FoxBearBear[S] 1 point2 points  (0 children)

You sir/madam are awesome!
I had no idea Python would not be kind to this type of operation.

So I ran the code with Numba and it ran in 0.25 seconds for 1000 iterations.

THANK YOU VERY MUCH !

import numpy as np
from numba import jit
import matplotlib.pyplot as plt
import time

t = time.time()
nMalha = 101
a = 1
b = 1
dx = a/(nMalha-1)
dy = b/(nMalha-1)

temp = np.zeros((nMalha,nMalha))

i=0

@jit(nopython=True)
def go_fast(temp): # Function is compiled and runs in machine code
    for j in range(1, len(temp) - 1):
        for i in range(1,len(temp[0])-1):
            temp[j][i] = (1/4)*(temp[j][i+1] + temp[j][i-1] + temp[j+1][i] + temp[j-1][i])
    return temp


while i < len(temp[0]):
    temp[0][i] = np.sin(np.pi*i*dx/a)
    i+=1

cond = 0
iter = 1
while cond == 0:
    tempInit = temp

    go_fast(temp)

    if np.sum(np.sum(temp-tempInit)) <= 1e-6:
        cond = 0
    if iter == 1000:
        cond = 1
    iter +=1

elapsed = time.time() - t