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)
def no_homogen(h0,f,n,g): # First lets generate the mesh for the unite circle mesh_gen(h0) mesh=msh.read_gmsh('circle.msh') # Matrix of nodes p=mesh[0] # Matrix of triangles index t=mesh[1] # Matrix of boundary edges be=mesh[2] # Now lets compute the stiffness matrix A=fem.stiffness(p,t) # Lets compute the load vector Load=fem.load(p,t,n,f) # Lets compute the load neumann vector Loadneumann=fem.loadNeumann(p,be,n,g) #Finally the Loadvector will be the sum of the one with the source f # and the one with f Load=Load+Loadneumann # Lets compute the vector m that analogous to the load vector # with f(x.y)=1 m=fem.load(p,t,n,lambda x,y:1) # Lets create the matrix and vectors of the modified bigger system to # solve with lagrangian multipliers of size (size(A)+(1,1)) size=A.shape[0] B=sparse.lil_matrix(np.zeros([size+1,size+1])) B[0:size,0:size]=A B[0:size,size]=m B[size,0:size]=m.transpose() # We add a zero at the end of f Load=np.concatenate((Load,np.array([[0]]))) # Now lets get the solution of the linear system using spsolve function U=spla.spsolve(B,Load) u=U[0:size] l=U[size] return [p,t,u,l]
def no_homogen(h0, f, n, g): # First lets generate the mesh for the unite circle mesh_gen(h0) mesh = msh.read_gmsh('circle.msh') # Matrix of nodes p = mesh[0] # Matrix of triangles index t = mesh[1] # Matrix of boundary edges be = mesh[2] # Now lets compute the stiffness matrix A = fem.stiffness(p, t) # Lets compute the load vector Load = fem.load(p, t, n, f) # Lets compute the load neumann vector Loadneumann = fem.loadNeumann(p, be, n, g) #Finally the Loadvector will be the sum of the one with the source f # and the one with f Load = Load + Loadneumann # Lets compute the vector m that analogous to the load vector # with f(x.y)=1 m = fem.load(p, t, n, lambda x, y: 1) # Lets create the matrix and vectors of the modified bigger system to # solve with lagrangian multipliers of size (size(A)+(1,1)) size = A.shape[0] B = sparse.lil_matrix(np.zeros([size + 1, size + 1])) B[0:size, 0:size] = A B[0:size, size] = m B[size, 0:size] = m.transpose() # We add a zero at the end of f Load = np.concatenate((Load, np.array([[0]]))) # Now lets get the solution of the linear system using spsolve function U = spla.spsolve(B, Load) u = U[0:size] l = U[size] return [p, t, u, l]
### ### ### Script that Produces the plots ### ### in 3f) and 3h) ### ### ### ###-----------------------------------------------------------### # First call the module meshes.py import meshes as msh import numpy as np import matplotlib.pyplot as plt # Get the p and t arrays for the file of unstructured mesh of the square # generated by gmsh file = 'square_mesh.msh' mesh1 = msh.read_gmsh(file) p1 = mesh1[0] t1 = mesh1[1] #Name of the file of the output plot file = 'unstructured.png' title = 'Unstructured mesh' msh.show(p1, t1, file, title) # Get the p and t arrays for the regular mesh of the square of side 1 # For structured meshes the value of h0 is not arbitrary, as it has to fit # a integer number of 1-dimensional elements in each border, the closest # h0 to 0.01 that fullfill that is h0=np.sqrt(2)/14=0.10101.... a = 1 h0 = np.sqrt(2) / 14 mesh2 = msh.grid_square(a, h0) p2 = mesh2[0]
def main(): lu = 1.548888; # value of l(sol) f = lambda x, y: (1.0); Nspacings = 9; # 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) dof = np.zeros(Nspacings); # vector for mesh widths (true values) dof_reg = np.zeros(Nspacings); # vector for mesh widths (true values) discret_err_bgm = [None]*Nspacings; # vector for discretization errors for linear FEM discret_err_reg = [None]*Nspacings; # vector for discretization errors for quadratic FEM # regular mesh for j in range(Nspacings): h = h0*2.0**(-j/2.0); [p, t, be, bd]=msh.create_msh("regular", h) # LINEAR FINITE ELEMENTS # Compute the discretized system: A_lin = FEM.stiffness(p, t); # stiffness-matrix F_lin = FEM.load(p, t, 3, f); # load-vector # degrees of freedom dof_reg[j] = p.shape[0] # solve system with Dirichtlet boundary conditions u_n = FEM.solve_d0(p, t, bd, A_lin, F_lin) # LINEAR FINITE ELEMENTS lun_lin = np.dot(u_n,F_lin); # value of l(u_n) en_lin = lu-sqrt(abs(lun_lin)); # energy norm of error vector discret_err_reg[j] = en_lin; print print "energy error with the regular mesh" print discret_err_reg # background mesh for j in range(Nspacings): #[p, t, be, bd]=msh.create_msh_bgm("bgmesh"+str(j)) [p, t, be, bd]=msh.read_gmsh("bgmesh"+str(j)+".msh") # LINEAR FINITE ELEMENTS # Compute the discretized system: A_lin = FEM.stiffness(p, t); # stiffness-matrix F_lin = FEM.load(p, t, 3, f); # load-vector # degrees of freedom dof[j] = p.shape[0] # solve system with Dirichtlet boundary conditions u_n = FEM.solve_d0(p, t, bd, A_lin, F_lin) # LINEAR FINITE ELEMENTS lun_lin = np.dot(u_n,F_lin); # value of l(u_n) en_lin = lu-sqrt(abs(lun_lin)); # energy norm of error vector discret_err_bgm[j] = en_lin; print print "energy error with the background mesh" print discret_err_bgm # ploting energy error distribution plt.loglog(dof_reg, discret_err_reg,':*', label='energy error - regular mesh') plt.loglog(dof, discret_err_bgm,':*', label='energy error - bgm') plt.title('energy error depending on degrees of freedom') plt.legend(loc=1) plt.show() # Visualization of the solution FEM.plot(p, t, u_n); # approximated solution plt.show()
### ### ### Script that Produces the plots ### ### in 3f) and 3h) ### ### ### ###-----------------------------------------------------------### # First call the module meshes.py import meshes as msh import numpy as np import matplotlib.pyplot as plt # Get the p and t arrays for the file of unstructured mesh of the square # generated by gmsh file='square_mesh.msh' mesh1=msh.read_gmsh(file) p1=mesh1[0] t1=mesh1[1] #Name of the file of the output plot file='unstructured.png' title='Unstructured mesh' msh.show(p1,t1,file,title) # Get the p and t arrays for the regular mesh of the square of side 1 # For structured meshes the value of h0 is not arbitrary, as it has to fit # a integer number of 1-dimensional elements in each border, the closest # h0 to 0.01 that fullfill that is h0=np.sqrt(2)/14=0.10101.... a=1 h0=np.sqrt(2)/14 mesh2=msh.grid_square(a,h0) p2=mesh2[0]
import meshes as mesh import FEM as fem import scipy.sparse as sp import numpy as np import scipy.sparse.linalg as spla print('comparing mesh sizes (4a)') p,t,be=mesh.read_gmsh('bgmesh0.msh') print(len(p)) p,t,be=mesh.read_gmsh('bgmesh1.msh') print(len(p)) 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