lu = (1 + 8*(pi**2))/4; # value of l(sol) f = lambda x, y: (1.0 + 8.0*(pi**2))*sol(x,y); 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);
# If theta<1 the number of iterations has to be as large as n=5000 for final # time T=1! kappa=1 h0 = 0.15 qo = 3 theta = 0.0 T = 1 n = 1000 time = np.linspace(0.0,T,num=n+1) dt = T/float(n) u = lambda x,y: kappa*sin(pi*x)*sin(pi*y) f = lambda x,y: (2*pi*pi)*u(x,y) ft = 1.0-np.exp(-10*time) [p,t]=msh.square(1,h0) A=kappa*FEM.stiffness(p,t).tocsr() M=FEM.mass(p,t).tocsr() F=FEM.load(p,t,qo,f) IN = FEM.interiorNodes(p,t) T0 = sparse.lil_matrix((len(p),len(IN))) for j in range(len(IN)): T0[IN[j],j] = 1 T0t = T0.transpose() T0 = T0.tocsr() T0t = T0t.tocsr() A = T0t.dot(A.dot(T0)) M = T0t.dot(M.dot(T0)) F = T0t.dot(F)
h0 = 0.1 qo = 3 theta = 0.0 T = 1 n = 1000 t = np.linspace(0.0,T,num=n+1) dt = T/float(n) u = lambda x,y: cos(2*pi*x)*cos(2*pi*y) f = lambda x,y: (8*pi*pi+1)*u(x,y) ft = 1.0-np.exp(-10*t) # scaling of time derivative dt /= 10 # FE mesh [coord,trian] = msh.square(1,h0) # analytical static limit solution uASL=np.zeros((coord.shape[0])) for i in range(0,coord.shape[0]): uASL[i]=u(coord[i,0],coord[i,1]) # l(u) lofu = (8*pi*pi+1)/4.0 # FE matrices and load vector A = FEM.stiffness(coord,trian) M = FEM.mass(coord,trian) F = FEM.load(coord,trian,qo,f) # time-stepping methods
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()
# import numpy as np import wir108_series4_FEM as lfem from scipy.sparse.linalg import spsolve from scipy.sparse import csr_matrix import meshes as meshes import FEM as FEM # # constructing and defining raw data # print("[constructing and defining raw data]") h0 = 0.1 n = 5 [p, t] = meshes.square(1,h0) def f(x,y): return np.cos(np.pi*x)*np.cos(np.pi*y)*(1+2*np.power(np.pi,2)) # # calculating the system to solve # print("[Calculating the system Bu = l]") cStiff = lfem.stiffness(p,t) cMass = lfem.mass(p,t) B = cMass+cStiff l = lfem.load(p, t, n, f) # # Solving the system # print("[Solving the system Bu = l]")