def discSol(h): (p,t,e,z,be)=mesh.generate_shifted_quad(h,1,True) width=mesh.max_mesh_width() nDN=FE.notDiricNodes(p,t,be) Stiff=FE.stiffness(p,t).tocsr() Load=FE.load(p,t,3,f) N=sp.lil_matrix((nDN.size,p[0:,0].size)) for j in range(nDN.size): # Initialisierung von Reduktionsmatrizen, aehnlich der T Matrizen. N[j,nDN[j]]=1 # Dies sind quasi NxN Einheitsmatrizen bei denen die Zeilen entfernt # sind, deren Indizes mit denen der Boundary Nodes korrelieren. rStiff=N.dot(Stiff).dot(N.transpose()) # Durch Multiplikation der N Matrizen von Links und Rechts werden die rLoad=N.dot(Load) run=spla.spsolve(rStiff,rLoad) un=N.transpose().dot(run) # if h==0.1: # FE.plot(p,t,un) # plt.title('Diskrete Loesung') # plt.show() return (np.dot(un, Load),width)
def main(h0, n): p, t, be = mesh.grid_square(2, h0) # shift mesh to origin# h = mesh.max_mesh_width(p, t) IN = fem.interiorNodes(p, t, be) S = fem.stiffness(p, t) M = fem.mass(p, t) L = fem.load(p, t, n, lambda x, y: 1) S = S.tocsr() M = M.tocsr() L = L.tocsr() # neumannnodes N = np.zeros(len(p)) j = 0 for i in range(0, len(p)): if p[i, 0] == 0 or p[i, 0] == 2: if 2 > p[i, 1] and p[i, 1] >= 1: N[j] = i j = j + 1 if p[i, 1] == 2: N[j] = i j = j + 1 j = j - 1 # number of neumannnodes print(N) Sr = sp.csr_matrix((len(IN) + j, len(IN) + j)) Mr = sp.csr_matrix((len(IN) + j, len(IN) + j)) Lr = sp.csr_matrix((len(IN) + j, 1)) A = sp.csr_matrix((len(IN) + j, len(p))) for i in range(0, len(IN)): A[i, IN[i]] = 1 for i in range(0, j): A[i + len(IN), N[i]] = 1 Sr = A * S * A.transpose() Mr = A * M * A.transpose() Lr = A * L un = spla.spsolve(Sr + Mr, Lr) u1 = A.transpose() * un fem.plot(p, t, u1)
def main(msh,n): p,t,be=mesh.read_gmsh(msh) h=mesh.max_mesh_width(p,t) IN=fem.interiorNodes(p,t,be) S=fem.stiffness(p,t) M=fem.mass(p,t) L=fem.load(p,t,n,lambda x,y:1) S=S.tocsr() M=M.tocsr() L=L.tocsr() #neumannnodes N=np.zeros(len(p)) j=0 for i in range (0,len(p)): if (p[i,0]==-1 or p[i,0]==1): if 1>p[i,1] and p[i,1]>=0: N[j]=i j=j+1 if p[i,1]==1: N[j]=i j=j+1 j=j-1 #number of neumannnodes Sr=sp.csr_matrix((len(IN)+j,len(IN)+j)) #adjusting solution to boundary condition Mr=sp.csr_matrix((len(IN)+j,len(IN)+j)) Lr=sp.csr_matrix((len(IN)+j,1)) A=sp.csr_matrix((len(IN)+j,len(p))) for i in range(0,len(IN)): A[i,IN[i]]=1 for i in range(0,j): A[i+len(IN),N[i]]=1 Sr=A*S*A.transpose() Mr=A*M*A.transpose() Lr=A*L un=spla.spsolve(Sr+Mr,Lr) u1=A.transpose()*un fem.plot(p,t,u1)
Nspacings = 6; # number of mesh widths to test (in powers of sqrt(2)) -> CHANGE BACK TO 10!!!! h0 = 0.3; # maximum value of mesh width h = [None]*Nspacings; # vector for mesh widths (set values) mmw = np.zeros(Nspacings); # vector for mesh widths (true values) discret_err_lin = [None]*Nspacings; # vector for discretization errors for linear FEM discret_err_quad = [None]*Nspacings; # vector for discretization errors for quadratic FEM for j in range(0,Nspacings): # Maximal mesh width h0 of the grid: h[j] = h0*2.0**(-j/2.0); # Mesh generation: [p,t]=msh.square(q0,h[j]) # determine actual max. mesh width mmw[j] = msh.max_mesh_width(p,t); # LINEAR FINITE ELEMENTS # Compute the discretized system: A_lin = FEM.stiffness(p, t); # stiffness-matrix M_lin = FEM.mass(p, t); # mass-matrix F_lin = FEM.load(p, t, 3, f); # load-vector # Solve the discretized system (A + M)u = F: A_lin = A_lin.tocsr(); # conversion from lil to csr format M_lin = M_lin.tocsr(); u_lin = spsolve(A_lin + M_lin, F_lin); eIndex = msh.edgeIndex(p,t) N = p.shape[0];
# unstructured mesh generated by gmsh, all of them are meshes for a square of side length a=1, and # the value of maximal width will be varied in gmsh in 6 different values: # Lets put in an array yu the number of nodes for each h0 # and xu for the actual mesh width calculated with max_mesh_width xu = [] yu = [] # 1.- h0=np.sqrt(2)/5=0.28284271247461901... file = 'square_mesh1.msh' mesh = msh.read_gmsh(file) p = mesh[0] t = mesh[1] xu.append(msh.max_mesh_width(p, t)) yu.append(len(p)) # 2.- h0=np.sqrt(2)/14=0.10101525445522108... file = 'square_mesh2.msh' mesh = msh.read_gmsh(file) p = mesh[0] t = mesh[1] xu.append(msh.max_mesh_width(p, t)) yu.append(len(p)) # 3.- h0=np.sqrt(2)/25= 0.056568542494923803... file = 'square_mesh3.msh' mesh = msh.read_gmsh(file) p = mesh[0]
# unstructured mesh generated by gmsh, all of them are meshes for a square of side length a=1, and # the value of maximal width will be varied in gmsh in 6 different values: # Lets put in an array yu the number of nodes for each h0 # and xu for the actual mesh width calculated with max_mesh_width xu=[] yu=[] # 1.- h0=np.sqrt(2)/5=0.28284271247461901... file='square_mesh1.msh' mesh=msh.read_gmsh(file) p=mesh[0] t=mesh[1] xu.append(msh.max_mesh_width(p,t)) yu.append(len(p)) # 2.- h0=np.sqrt(2)/14=0.10101525445522108... file='square_mesh2.msh' mesh=msh.read_gmsh(file) p=mesh[0] t=mesh[1] xu.append(msh.max_mesh_width(p,t)) yu.append(len(p)) # 3.- h0=np.sqrt(2)/25= 0.056568542494923803... file='square_mesh3.msh' mesh=msh.read_gmsh(file) p=mesh[0]
import meshes as msh import FEM h0 = 0.5 qo = 1 u = lambda x,y: cos(2*pi*x)*cos(2*pi*y) f = lambda x,y: (8*pi*pi+1)*u(x,y) lu = (8*pi*pi+1)/4.0 n=10 error = np.zeros((n)) h = np.zeros((n)) for i in range(n): h[i]=h0*pow(2,-i/2.0) [p,t]=msh.square(1,h[i]) h[i]=msh.max_mesh_width(p,t) A=FEM.stiffness(p,t).tocsr() M=FEM.mass(p,t).tocsr() F=FEM.load(p,t,qo,f) Un=spsolve(A+M,F) error[i] = np.sqrt(lu-np.dot(Un,F)) if i>0: print "rate", (np.log(error[i-1])-np.log(error[i]))/(np.log(h[i-1])-np.log(h[i])) plt.loglog(h,error) plt.grid(True) plt.show()