Example #1
0
print("number of elements in x direction is %d " % nx)
print("number of elements in y direction is %d " % ny)
(bdnodes, left_nodes, right_nodes, bottom_nodes, top_nodes,
 free_nodes) = cg.getboundary_dof(nnx - 1, nny - 1)
n_bdnodes = bdnodes.shape[0]
x = np.linspace(0, Lx, nnx)
y = np.linspace(0, Ly, nny)
xp, yp = np.meshgrid(x, y)
xp = np.reshape(xp, nnx * nny)
yp = np.reshape(yp, nnx * nny)
## coeff of the general elliptic operator, constant here
stiff_coeff = np.ones((nny - 1, nnx - 1))

## assemble global matrix and force
print("Assembling FEM matrix... ")
local_massmatrix = cg.local_mass(hx, hy)  ## element stiff matrix
local_stiffmatrix = cg.local_stiff(hx, hy)  ## element mass matrix
Astiff = cg.assembleweightmatrix(stiff_coeff, local_stiffmatrix)
Massforce = cg.assembleweightmatrix(np.ones((nny - 1, nnx - 1)),
                                    local_massmatrix)
f = 4 * (-yp * yp + yp) * np.sin(np.pi * xp)
F = Massforce.dot(f)  ## F is mass matrix times the force value

print("Setting Dirichlet boundary condition...")

Astiff_free = Astiff[free_nodes][:, free_nodes]
F_free = F[free_nodes]

print("Solving linear system... ")
u = np.zeros(n_nodes)
u[free_nodes] = spla.spsolve(Astiff_free, F_free)
Example #2
0
stiff_coeff[ ny+1:ny*2,nx+1:(Nx-2)*nx]=high_value
stiff_coeff[ ny*3:ny*4-2,nx+1:(Nx-2)*nx]=high_value
stiff_coeff[ ny*5:ny*6-2,nx+5:(Nx-3)*nx]=high_value
stiff_coeff[ ny*7:ny*8-2,nx+5:(Nx-3)*nx]=high_value
'''
stiff_coeff=np.ones((nny-1,nnx-1))
#stiff_coeff=cg.loadmatrix('k1.txt')
#stiff_coeff=np.reshape(stiff_coeff,(100,100)).transpose()
#print stiff_coeff
#cg.plot_matrix(stiff_coeff,Lx,Ly)
#cg.assembleweightstiff(hx,hy,stiff_coeff)
#ss=np.linspace(1, 5,5)

## assemble global matrix and force
print " Assembling fine scale matrix... "
local_massmatrix=cg.local_mass(hx,hy)
local_stiffmatrix=cg.local_stiff(hx,hy)
Astiff=cg.assembleweightmatrix(stiff_coeff,local_stiffmatrix)
#Massweight=cg.assembleweightmatrix(stiff_coeff,local_massmatrix)
Massforce=cg.assembleweightmatrix(np.ones((nny-1,nnx-1)),local_massmatrix)
f=np.ones((n_nodes,1));
F=Massforce.dot(f);#print F

#print "Setting Dirichlet boundary condition..."
#F[bdnodes]=0;#print F
#Astiff=sparse.lil_matrix(Astiff)
#Astiff[bdnodes,:] = sparse.lil_matrix((n_bdnodes, n_nodes))
#for i in range(0, n_bdnodes):
#	Astiff[bdnodes[i],bdnodes[i]]=1

Astiff_free=Astiff[free_nodes][:,free_nodes];