You are on page 1of 3

# # Kumar Tapesh

## # Roll No. 07AE3801

import numpy as np
from numpy import pi,sin,sinh
import matplotlib.pyplot as plt
x=0.5
y=1.0
print('Program to solve Laplace equation on a rectangular domain (using Galerkin
FEM):')
ndx = input('Enter the number of discrete points in x-direction: ')
ndy = input('Enter the number of discrete points in y-direction: ')
dx=x/ndx
dy=y/ndy
xn=np.zeros([ndx+1])
yn=np.zeros([ndy+1])
tempx=np.array(range(ndx+1))
tempy=np.array(range(ndy+1))
xn[:]=x+dx*tempx[:] #calculating x discrete cordinates
yn[:]=dy*tempy[:] #calculating y discete cordinates

## elems=list() %#creating list to construct element data structure

for i in range(2*ndx*ndy):
elems.append(np.matrix(np.zeros([3,2])))

xlen=len(xn)
ylen=len(yn)
numel=len(elems)
j=0
for i in range(0,numel,2):
elems[i][0,:]=gnodes[j]
elems[i][1,:]=gnodes[j+1]
elems[i][2,:]=gnodes[j+1+xlen]
elems[i+1][0,:]=gnodes[j+1+xlen]
elems[i+1][1,:]=gnodes[j+xlen]
elems[i+1][2,:]=gnodes[j]
j=j+1
if ((j%xlen)+1==xlen):
j=j+1
def Ae(elem):
temp=np.ones([3,3])
temp[:,:2]=elem
return 0.5*np.linalg.det(temp)
alpha=lambda elem,i:elem[(i+1)%3,0]*elem[(i+2)*3,1]-elem[(i+2)3,0]*elem[(i+1)3,1
]
beta=lambda elem,i:elem[(i+1)%3,1]-elem[(i+2)%3,1]
gama=lambda elem,i:-(elem[(i+1) %3,0]-elem[(i+2)%3,0])
nnds=len(gnodes)
K=np.matrix(np.zeros([nnds,nnds]))
f=np.matrix(np.zeros([nnds,1]))
f=np.matrix(np.zeros([nnds,1]))
Ke=np.matrix(np.zeros([3,3]))
c=np.matrix(np.array([[1,0],[0,1]]))
B=np.matrix(np.zeros([2,3]))
U=np.zeros([nnds,1])
j=0
for i in range(0,numel,2):
B[0,:]=[beta(elems[i],0),beta(elems[i],1),beta(elems[i],2)]
B[1,:]=[gama(elems[i],0),gama(elems[i],1),gama(elems[i],2)]

Ke=np.dot(np.dot(B.transpose(),c),B)
Ke=Ke/(4*Ae(elems[i]))
K[j:j+2,j:j+2]=K[j:j+2,j:j+2]+Ke[:2,:2]
K[j+1+xlen,j:j+2]=K[j+1+xlen,j:j+2]+Ke[2,:2]
K[j:j+2,j+1+xlen]=K[j:j+2,j+1+xlen]+Ke[:2,2]
K[j+1+xlen,j+1+xlen]=K[j+1+xlen,j+1+xlen]+Ke[2,2]
Ke=Ke[::-1,::-1]
K[j,j]=K[j,j]+Ke[0,0]
K[j+xlen:j+xlen+2,j+xlen:j+xlen+2]=K[j+xlen:j+xlen+2,j+xlen:j+xlen+2]+Ke[1:3
,1:3]
K[j,j+xlen:j+xlen+2]=K[j,j+xlen:j+xlen+2]+Ke[0,1:3]
K[j+xlen:j+xlen+2,j]=K[j+xlen:j+xlen+2,j]+Ke[1:3,0]
j=j+1
if ((j%xlen)+1==xlen):
j=j+1
xcal=np.zeros([1,xlen])
xcal[:]=x+dx*np.array(range(xlen))
U[:xlen]=0
U[nnds-xlen:nnds]=sin(pi*(xcal.transpose()))
U[xlen-1:nnds:xlen]=0
consize=nnds-(2*xlen+(ylen-2)) %#condensed size/reduced size
Kc=np.matrix(np.zeros([consize,consize])) %#condensed/reduced K matrix
fc=np.matrix(np.zeros([consize,1])) %#condensed force vector
Uc=np.matrix(np.zeros([consize,1])) %#condensed dispacement
Ktemp=K[xlen:-xlen,xlen:-xlen]
ftemp=f[xlen:-xlen]-K[xlen:-xlen,:xlen]*U[:xlen]
ftemp=f[xlen:-xlen]-K[xlen:-xlen,nnds-xlen:nnds]*U[nnds-xlen:nnds]
Utemp=U[xlen:-xlen]
j=0
tlen=nnds-2*xlen
for i in range(0,tlen,xlen):
ch=0
Kc[(j+ch):(j+ch)+xlen-1,j:j+xlen-1]=Ktemp[(i+h):(i+h)+xlen-1,i:i+xlen-1]
Kc[j:j+xlen-1,(j+ch):(j+ch)+xlen-1]=Ktemp[i:i+xlen-1,(i+h):(i+h)+xlen-1]
ch=ch+xlen-1
j=j+xlen-1
j=0
for i in range(0,tlen,xlen):
ch=0
h=0
while (i+h)+xlen-1<tlen:
fc[(j+ch):(j+ch)+xlen-1]=ftemp[(i+h):(i+h)+xlen-1]-Ktemp[(i+h):(i
+h)+xlen-1,(i+h)+xlen-1]*Utemp[(i+h)+xlen-1]
ch=ch+xlen-1
h=h+xlen
j=j+xlen-1
Uc=np.linalg.solve(Kc,fc)
i=xlen
for j in range(0,consize,xlen-1):
U[i:i+xlen-1]=Uc[j:j+xlen-1]
i=i+xlen
print('The solution vector U:')
print(U)
Umat=U.reshape([ylen,xlen])
Ue=np.zeros([ylen,xlen])
for i in range(xlen):
for j in range(ylen):
Ue[j][i]=(sin(pi*xn[i])*sinh(pi*yn[j]))/sinh(pi)