Rayleigh–Ritz method for FEM code - python

I´m trying to generate a code to model a finite elements analysis based on the following equation, using the Rayleigh Ritz Method.
-d/dx(adu/dx)+ c u - q
with U(0)=1 and adu/dx=0
i´ve got this far in the code but when it tries to integrate a TypeError: unsupported operand type(s) for /: 'int' and 'function' appears.
L = 1.0
n = 4
dx = L/n
dofpe = 2 # Grados de libertad por elemento, por ahora solo programado 2
Uo=1 #condicion esccencial
Qo=0 #condicion natural
coords = np.linspace(0,L,n+1)
elementos = np.zeros((n,dofpe),dtype=int)
for i in range(n):
elementos[i,0] = int(i)
elementos[i,1] = int(i+1)
def basis(xbar,i,he):
"""Returns the ith basis funciton at x
xbar - local element coordinate
i - basis function index
he - width of each element
"""
if i == 0: return 1 - xbar/he
if i == 1: return xbar/he
return -1 # Este seria un codigo de error, toca mejorar
def dbasis(xbar,i,he):
"""Returns the ith basis funciton at x
xbar - local element coordinate
i - basis funciton index
he - width of each element
"""
if i == 0: return -1/he
if i == 1: return 1/he
return -1 # Este seria un codigo de error, toca mejorar
def a():
return 1
def c():
return 1
def q():
return 1
xvec = np.linspace(0,1,10)
basisv = np.vectorize(basis)
plt.plot(xvec,basisv(xvec,0,1))
dbasisv = np.vectorize(dbasis)
plt.plot(xvec,dbasisv(xvec,0,1))
K = np.zeros((n+1,n+1))
for e in range(n): # loop over elements
dx = coords[elementos[e,1]] - coords[elementos[e,0]]
for i in range(dofpe): # loop over basis i
for j in range(dofpe): # loop over basis j
def fun(x,i,j,dx,a,c):
return a*dbasis(x,i,dx)*dbasis(x,j,dx)+ c*basis(x,i,dx)*basis(x,j,dx)
kij=scipy.integrate.quad(fun,coords[elementos[e,0]],coords[elementos[e,1]],args=(i,j,a,c,dx))
K[elementos[e,i],elementos[e,j]] +=kij
F = np.zeros((n+1))
for e in range(n):
dx = coords[elementos[e,1]] - coords[elementos[e,0]]
for i in range(dofpe):
def l(x,i,dx,q):
return q*basis(x,i,dx)
fij=scipy.integrate.quad(l,coords[elementos[e,0]],coords[elementos[e,1]],args=(i,dx,q))
F[elementos[e,i]] += fij
I don´t know why, does any one knows how to fix it?

Related

Parallelization of multiple numpy.linalg.multi_dot calls

I'm trying to parallelize this function:
def update_G_left(self):
num = np.linalg.multi_dot([self.association_matrix,
self.G_right, self.S.transpose()])
den = np.linalg.multi_dot([self.G_left, self.S, self.G_right.transpose(),
self.G_right, self.S.transpose()])
for am in self.dep_own_left_other_left:
num += np.linalg.multi_dot([am.association_matrix,
am.G_right, am.S.transpose()])
den += np.linalg.multi_dot([self.G_left, am.S, am.G_right.transpose(),
am.G_right, am.S.transpose()])
for am in self.dep_own_left_other_right:
num += np.linalg.multi_dot([am.association_matrix.transpose(),
am.G_left, am.S])
den += np.linalg.multi_dot([self.G_left, am.S.transpose(),
am.G_left.transpose(), am.G_left, am.S])
div = np.divide(num, den + sys.float_info.min)
return np.multiply(self.G_left, div)
The function is working well, but when I use pools:
def update_G_left(self):
num = np.linalg.multi_dot([self.association_matrix, self.G_right, self.S.transpose()])
den = np.linalg.multi_dot([self.G_left, self.S, self.G_right.transpose(), self.G_right, self.S.transpose()])
with multiprocessing.Pool(6) as pool:
for x in pool.map(np.linalg.multi_dot,
[[am.association_matrix, am.G_right, am.S.transpose()]
for am in self.dep_own_right_other_left]):
num += x
for x in pool.map(np.linalg.multi_dot,
[[self.G_left, am.S,
am.G_right.transpose(), am.G_right, am.S.transpose()]
for am in self.dep_own_right_other_left]):
den += x
for x in pool.map(np.linalg.multi_dot,
[[am.association_matrix.transpose(), am.G_left, am.S]
for am in self.dep_own_right_other_right]):
num += x
for x in pool.map(np.linalg.multi_dot,
[[self.G_left, am.S.transpose(),
am.G_left.transpose(), am.G_left, am.S]
for am in self.dep_own_right_other_right]):
den += x
div = np.divide(num, den + sys.float_info.min)
return np.multiply(self.G_left, div)
Now it is no more working.
I get the error ValueError: operands could not be broadcast together with shapes (10,6) (7,6) (10,6) at the second num += x .
I'm wondering why because i don't see any differences in the code.
There are other patterns to parallelize my function?

TypeError: 'int' object is not subscriptable ARRAY

Im trying to create a function called "exists " that checks if a given number from user already exists in the array.I keep getting the errror "TypeError: 'int' object is not subscriptable"
I have no idea how to continue on solving it.Here's my code:
from numpy import*
t=array([int]*10)
#Takes the length of array from user,should be between 5 AND 10
def taille():
n = int(input("Donner le taille de tableau: "))
while n<5 or n >10:
n = int(input("Donner une taille 5<=n<=10: "))
return(n)
#CHECKS IF GIVEN NUMBER EXISTS IN ARRAY
def exist(t,n):
Found = False
for i in range(n):
if t[i] in t[i+1:]:
Found = True
return(Found)
#Fills the array from user input
def remplir(t,n):
t[0]=int(input("donner t[0]: "))
for i in range(1,n):
t[i] = int(input("Donner une element: "))
while exist(t[i]) == True:
t[i] = int(input("Donner un element distinct!: "))
#Sorts array
def shell(t,n):
p =0
while p<n:
p = p*3+1
while p!=1:
for i in range(t):
key = t[i]
j = i-1
while j>=0 and t[j]>key:
t[j+1] = t[j]
j-=1
t[j+1] = key
#Prints it
def affichage(t,n):
for i in range(n):
print(t[i])
n = taille()
remplir(t,n)
shell(t,n)
affichage(t,n)

How to remove a variable in a list a loop as already gone through

My goal here is to create a loop where I get a list ordered by beginning in Inicial and then the item closest to Inicial and then the one closest to the previous one and so on.
The casa_mais_proxima function gives me the closest item in a list to a given item.
I keep getting a list.remove(x): x not in list in grupo.remove(resultado[casa]) and don't know how to change this so that I can remove the items on the list the loop has already gone through.
Inicial = (3,2)
Casas = [(0,1),(1,0),(1,2),(2,3)]
def percurso(Inicial,Casas):
grupo = [Inicial]
grupo.extend(Casas)
resultado = [Inicial]
casas = Casas
for casa in range(len(Casas)):
grupo.remove(resultado[casa])
proxima = casa_mais_proxima(resultado[casa],grupo)
resultado.append(proxima)
return(resultado)
print(percurso(Inicial,Casas))
I think there may be an issue with the other function but i can't spot it
Casas = [(0,1),(1,0),(1,2),(2,3)]
def casa_mais_proxima(P,Casas):
menor_distancia = 0
resultado = []
for elemento in Casas:
if menor_distancia == 0:
menor_distancia = distancia_casas(P,elemento)
if menor_distancia == distancia_casas(P,elemento):
resultado.append(elemento)
if menor_distancia > distancia_casas(P,elemento):
resultado = [elemento]
else:
continue
if len(resultado) == 1:
return resultado
else:
resultado_a = []
for elemento in resultado:
if len(resultado_a) == 0:
resultado_a.append(elemento)
if elemento[0] < resultado_a[0][0]:
resultado_a = [elemento]
if elemento[0] == resultado_a[0][0]:
resultado_a.append(elemento)
else:
continue
if len(resultado_a) == 1:
return resultado_a
else:
resultado_b = []
for elemento in resultado_a:
if len(resultado_b) == 0:
resultado_b.append(elemento)
if elemento[1] < resultado_b[1][1]:
resultado_b = list(elemento)
if elemento[1] == resultado_b[1][1]:
resultado_b.append(elemento)
else:
continue
return resultado_b
print (casa_mais_proxima((1,1),[(0,2),(1,3),(2,1)]))

Python - list index out of range - genetic algorithm

I'm having problems with my code and I know this problem is simple but I just can't figure it out how to solve it, I'll really appreciate if someone could tell me what I'm doing wrong:
import random
from math import *
def create_population(dim,pn):
t = log(factorial(dim**2),2)
b = int(t+1)
d = ""
indarray = []
bits_array=[]
#print("bits usados: ",b)
for x in range(pn):
for y in range(b) :
if random.randint(0,400000) %2:
d = "1"+d
else:
d="0"+d
num=int(d,2)%factorial(dim**2)
bits_array.append(d)
indarray.append(num)
#print("\n index #",len(indarray),": ",num)
d=""
return indarray,dim,bits_array,b
def i2ms(index,b):
squares=[]
a=init_a(b)
i=0
t=b
b = (b**2)-1
for i in range(len(index)):
s=""
cont = 1
while(index[i]>0):
c = factorial(b)
ind =(index[i]/c)
s = s+str(a[int(ind)])+" "
del a[(int(ind))]
index[i] = index[i]%c
b-=1
cont +=1
for i in range(len(a)):
s = s+str(a[i])+" "
squares.append(s)
a = init_a(t)
b = t
b = (b**2)-1
s=""
return squares
def init_a(b):
a=[]
for i in range(b**2):
a.append(i+1)
return a
def score(squares):
scores=[]
print("\n")
for i in range(len(squares)):
r = squares[i]
r = r.split(' ')
n = int(sqrt(len(r)))
nd = r
goal = n * (n * n + 1) / 2;
nd.reverse()
m = [[nd.pop() for i in range(n)] for j in range(n)]
#print ("Cubo #",i+1,": \n")
#for i in range(n):
#print(m[i],'\n')
min_sum,max_sum= 0,0
minn = 1
maxx = n * n
for i in range (n):
min_sum += minn
minn += 1
max_sum += maxx
maxx += 1
min_b,max_b = abs(goal - min_sum), abs(goal - max_sum)
if min_sum < max_sum:
final_b = max_sum
else:
final_b = min_sum
total_cases = 2 * n + 2
bias = total_cases * final_b
fitness = bias
#print ("Max score: ",fitness)
for i in range(n):
s =0
for j in range(n):
s +=int(m[i][j])
fitness -= abs(goal-s)
for j in range(n):
s=0
for i in range(n):
s += int(m[i][j])
fitness -= abs(goal-s)
s = 0
if n%2 == 1:
for i in range(n):
s+= int(m[i][i])
fitness -= abs(goal-s)
m.reverse()
s = 0
for i in range(n):
s+= int(m[i][i])
fitness -= abs(goal-s)
#print("Actual score: ",fitness,"\n")
scores.append(int(fitness))
#print("goal",goal)
return scores,bias
def breed(popul,score,breed_size,b):#popul= la poblacion , score : sus notas ind, breed_size, tamaño de poblacion que esco
#escogeremos, b numero de bites;
#Calculamos las medidas de la poblacion a "mergear"
print(popul)
print(score)
maxx = max(score)
#Acomodamos los cubos(en binario) con su respectivo score
breed_pop=[]
new_pop=[]
for y in range(breed_size):
for z in score:
if score[z] == maxx:
breed_pop.append(popul[z])
del score[z]
del popul[z]
maxx= max(score)
print(breed_pop)
if breed_pop>breed_size:
breed_pop.pop()
print(breed_pop)
##sorted(pop_dict.values())
if __name__ == '__main__':
#Dar Dimensiones y determinar la poblacion inicial
print("dimensiones?")
n = input()
print("poblacion?")
pn = input()
print("breed size?")
p= int(input())
##g = input()
#Pasar los datos de dim y pob por el metodo de create_population, devuelve una lista con los index del cubo y su dimensiones
ind,b,bits_a,bitsn= create_population(int(n),int(pn))
#Convertimos cada uno de esos indices a un cubo magico con i2ms, devuelve un array de cubos magicos
squares = i2ms(ind,b)
'''print("\n")
for i in range(len(squares)):
print("Cubo #",i+1,": " , squares[i])
#Pasamos cada cubo por score, nos dara el puntaje de cada cubo, devuelve una lista con los scores y el puntaje maximo
'''
scores,perfect = score(squares)
breed(bits_a,scores,p,bitsn)
'''for y in range(len(scores)):
print(scores[y],"/",perfect)
'''
I'm using dimension = 3, population =10, and breed_size=4 but I keep getting:
if score[z] ==max
IndexError: list index out of range
Edit:
Traceback(most recent call last):
File "squaresolver.py",line 156 in
breed(bits:a,scores,p,bitsn)
File "squaresolver.py", line 125, in breed
if score[z] == maxx:
IndexError: list index out of range
You don't need "score[z]" when you do "for z in score", z is not an index, is a value of the score list.
You can just do
if z == maxx
As you delete things in a list, you end up with index problems using range(len). If you have a list and then delete an item in it, you end up with a list whose length is now one less. This leads to IndexErrors as you try to access up to the original len(list).
Perhaps think of copying the original and working with that.

Too many values to unpack with python

I have a little problem with Python.
I'm try to write an application for DCM standard who some slice and draw the final model.
This is my code:
from lar import *
from scipy import *
import scipy
import numpy as np
from time import time
from pngstack2array3d import pngstack2array3d
colors = 2
theColors = []
DEBUG = False
MAX_CHAINS = colors
# It is VERY important that the below parameter values
# correspond exactly to each other !!
# ------------------------------------------------------------
MAX_CHUNKS = 75
imageHeight, imageWidth = 250,250 # Dx, Dy
# configuration parameters
# ------------------------------------------------------------
beginImageStack = 430
endImage = beginImageStack
nx = ny = 50
imageDx = imageDy = 50
count = 0
# ------------------------------------------------------------
# Utility toolbox
# ------------------------------------------------------------
def ind(x,y): return x + (nx+1) * (y + (ny+1) )
def invertIndex(nx,ny):
nx,ny = nx+1,ny+1
def invertIndex0(offset):
a0, b0 = offset / nx, offset % nx
a1, b1 = a0 / ny, a0 % ny
return b0,b1
return invertIndex0
def invertPiece(nx,ny):
def invertIndex0(offset):
a0, b0 = offset / nx, offset % nx
a1, b1 = a0 / ny, a0 % ny
return b0,b1
return invertIndex0
# ------------------------------------------------------------
# computation of d-chain generators (d-cells)
# ------------------------------------------------------------
# cubic cell complex
# ------------------------------------------------------------
def the3Dcell(coords):
x,y= coords
return [ind(x,y),ind(x+1,y),ind(x,y+1),ind(x+1,y+1)]
# construction of vertex coordinates (nx * ny )
# ------------------------------------------------------------
V = [[x,y] for y in range(ny+1) for x in range(nx+1) ]
if __name__=="__main__" and DEBUG == True:
print "\nV =", V
# construction of CV relation (nx * ny)
# ------------------------------------------------------------
CV = [the3Dcell([x,y]) for y in range(ny) for x in range(nx)]
if __name__=="__main__" and DEBUG == True:
print "\nCV =", CV
#hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,CV[:500]+CV[-500:])))
#box = SKELETON(1)(BOX([1,2,3])(hpc))
#VIEW(STRUCT([box,hpc]))
# construction of FV relation (nx * ny )
# ------------------------------------------------------------
FV = []
v2coords = invertIndex(nx,ny)
for h in range(len(V)):
x,y= v2coords(h)
if (x < nx) and (y < ny): FV.append([h,ind(x+1,y),ind(x,y+1),ind(x+1,y+1)])
if __name__=="__main__" and DEBUG == True:
print "\nFV =",FV
#hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,FV[:500]+FV[-500:])))
#box = SKELETON(1)(BOX([1,2,3])(hpc))
#VIEW(STRUCT([box,hpc]))
# construction of EV relation (nx * ny )
# ------------------------------------------------------------
EV = []
v2coords = invertIndex(nx,ny)
for h in range(len(V)):
x,y = v2coords(h)
if x < nx: EV.append([h,ind(x+1,y)])
if y < ny: EV.append([h,ind(x,y+1)])
if __name__=="__main__" and DEBUG == True:
print "\nEV =",EV
#hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,EV[:500]+EV[-500:])))
#box = SKELETON(1)(BOX([1,2,3])(hpc))
#VIEW(STRUCT([box,hpc]))
# ------------------------------------------------------------
# computation of boundary operators (∂3 and ∂2s)
# ------------------------------------------------------------
"""
# computation of the 2D boundary complex of the image space
# ------------------------------------------------------------
Fx0V, Ex0V = [],[] # x == 0
Fx1V, Ex1V = [],[] # x == nx-1
Fy0V, Ey0V = [],[] # y == 0
Fy1V, Ey1V = [],[] # y == ny-1
v2coords = invertIndex(nx,ny)
for h in range(len(V)):
x,y = v2coords(h)
if (y == 0):
if x < nx: Ey0V.append([h,ind(x+1,y)])
if (x < nx):
Fy0V.append([h,ind(x+1,y),ind(x,y)])
elif (y == ny):
if x < nx: Ey1V.append([h,ind(x+1,y)])
if (x < nx):
Fy1V.append([h,ind(x+1,y),ind(x,y)])
if (x == 0):
if y < ny: Ex0V.append([h,ind(x,y+1)])
if (y < ny):
Fx0V.append([h,ind(x,y+1),ind(x,y)])
elif (x == nx):
if y < ny: Ex1V.append([h,ind(x,y+1)])
if (y < ny):
Fx1V.append([h,ind(x,y+1),ind(x,y)])
FbV = Fy0V+Fy1V+Fx0V+Fx1V
EbV = Ey0V+Ey1V+Ex0V+Ex1V
"""
"""
if __name__=="__main__" and DEBUG == True:
hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,FbV)))
VIEW(hpc)
hpc = EXPLODE(1.2,1.2,1.2)(MKPOLS((V,EbV)))
VIEW(hpc)
"""
# computation of the ∂2 operator on the boundary space
# ------------------------------------------------------------
print "start partial_2_b computation"
#partial_2_b = larBoundary(EbV,FbV)
print "end partial_2_b computation"
# computation of ∂3 operator on the image space
# ------------------------------------------------------------
print "start partial_3 computation"
partial_3 = larBoundary(FV,CV)
print "end partial_3 computation"
# ------------------------------------------------------------
# input from volume image (test: 250 x 250 x 250)
# ------------------------------------------------------------
out = []
Nx,Ny = imageHeight/imageDx, imageWidth/imageDx
segFaces = set(["Fy0V","Fy1V","Fx0V","Fx1V"])
for inputIteration in range(imageWidth/imageDx):
startImage = endImage
endImage = startImage + imageDy
xEnd, yEnd = 0,0
theImage,colors,theColors = pngstack2array3d('SLICES2/', startImage, endImage, colors)
print "\ntheColors =",theColors
theColors = theColors.reshape(1,2)
background = max(theColors[0])
foreground = min(theColors[0])
print "\n(background,foreground) =",(background,foreground)
if __name__=="__main__" and DEBUG == True:
print "\nstartImage, endImage =", (startImage, endImage)
for i in range(imageHeight/imageDx):
for j in range(imageWidth/imageDy):
xStart, yStart = i * imageDx, j * imageDy
xEnd, yEnd = xStart+imageDx, yStart+imageDy
image = theImage[:, xStart:xEnd, yStart:yEnd]
nx,ny = image.shape
if __name__=="__main__" and DEBUG == True:
print "\n\tsubimage count =",count
print "\txStart, yStart =", (xStart, yStart)
print "\txEnd, yEnd =", (xEnd, yEnd)
print "\timage.shape",image.shape
# ------------------------------------------------------------
# image elaboration (chunck: 50 x 50)
# ------------------------------------------------------------
"""
# Computation of (local) boundary to be removed by pieces
# ------------------------------------------------------------
if pieceCoords[0] == 0: boundaryPlanes += ["Fx0V"]
elif pieceCoords[0] == Nx-1: boundaryPlanes += ["Fx1V"]
if pieceCoords[1] == 0: boundaryPlanes += ["Fy0V"]
elif pieceCoords[1] == Ny-1: boundaryPlanes += ["Fy1V"]
"""
#if __name__=="__main__" and DEBUG == True:
#planesToRemove = list(segFaces.difference(boundaryPlanes))
#FVtoRemove = CAT(map(eval,planesToRemove))
count += 1
# compute a quotient complex of chains with constant field
# ------------------------------------------------------------
chains2D = [[] for k in range(colors)]
def addr(x,y): return x + (nx) * (y + (ny))
for x in range(nx):
for y in range(ny):
if (image[x,y] == background):
chains2D[1].append(addr(x,y))
else:
chains2D[0].append(addr(x,y))
#if __name__=="__main__" and DEBUG == True:
#print "\nchains3D =\n", chains3D
# compute the boundary complex of the quotient cell
# ------------------------------------------------------------
objectBoundaryChain = larBoundaryChain(partial_3,chains2D[1])
b2cells = csrChainToCellList(objectBoundaryChain)
sup_cell_boundary = MKPOLS((V,[FV[f] for f in b2cells]))
# remove the (local) boundary (shared with the piece boundary) from the quotient cell
# ------------------------------------------------------------
"""
cellIntersection = matrixProduct(csrCreate([FV[f] for f in b2cells]),csrCreate(FVtoRemove).T)
#print "\ncellIntersection =", cellIntersection
cooCellInt = cellIntersection.tocoo()
b2cells = [cooCellInt.row[k] for k,val in enumerate(cooCellInt.data) if val >= 4]
"""
# ------------------------------------------------------------
# visualize the generated model
# ------------------------------------------------------------
print "xStart, yStart =", xStart, yStart
if __name__=="__main__":
sup_cell_boundary = MKPOLS((V,[FV[f] for f in b2cells]))
if sup_cell_boundary != []:
out += [T([1,2])([xStart,yStart]) (STRUCT(sup_cell_boundary))]
if count == MAX_CHUNKS:
VIEW(STRUCT(out))
# ------------------------------------------------------------
# interrupt the cycle of image elaboration
# ------------------------------------------------------------
if count == MAX_CHUNKS: break
if count == MAX_CHUNKS: break
if count == MAX_CHUNKS: break
And this is the error take from the terminal :
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-2e498c6090a0> in <module>()
213
214 image = theImage[:, xStart:xEnd, yStart:yEnd]
--> 215 nx,ny = image.shape
216
217 if __name__=="__main__" and DEBUG == True:
ValueError: too many values to unpack
Someone can help me to solve this issue????
Based on the line:
image = theImage[:, xStart:xEnd, yStart:yEnd]
image is a 3d array, not a 2d array (it appears to be multiple slices of an image), with the 2nd and 3rd dimensions representing x and y respectively. Thus, if you want to get its dimensions you'll need to unpack it into three dimensions, something like:
nslice, nx, ny = image.shape

Categories