def star_mesh(): figure() # fake the corners: pfix = [[0.25, 0.25], [-0.25, 0.25], [-0.25, -0.25], [0.25, -0.25]] pts, tri = distmesh2d(star, huniform, 0.1, bbox, pfix) plotmesh(pts, tri) show()
def ex_disc_refine(h0): print " meshing ..." p1, t1 = distmesh2d(fd_disc, huniform, h0, bbox, []) pts, mytri = fixmesh(p1, t1) edges, tedges = edgelist(pts, mytri) print " original mesh has %d nodes and %d edges" % (np.shape(pts)[0], np.shape(edges)[0]) fig1 = plt.figure() plt.subplot(1, 2, 1) plotmesh(pts, mytri) plt.title('original mesh') rp, rt, e, ind = bdyrefine(pts, mytri, fd_disc, h0) redges, tmp = edgelist(rp, rt) print " refined mesh has %d nodes and %d edges" % (np.shape(rp)[0], np.shape(redges)[0]) plt.subplot(1, 2, 2) plotmesh(rp, rt) plt.title('final refined mesh') uh, inside = poisson(f_ex, fd_disc, h0, rp, rt, announce=True) print " plotting ..." fig4 = plt.figure() ax = fig4.gca(projection='3d') ax.plot_trisurf(rp[:, 0], rp[:, 1], uh, cmap=cm.jet, linewidth=0.2) uexact = 1.0 - rp[:, 0]**2.0 - rp[:, 1]**2.0 # exact: u(x,y) = 1 - x^2 - y^2 err = max(abs(uh - uexact)) print "max error = %f" % err
def star_mesh(): figure() # fake the corners: pfix = [[0.25, 0.25], [-0.25, 0.25], [-0.25, -0.25], [0.25, -0.25]] pts, tri = distmesh2d(star, huniform, 0.1, bbox, pfix) plotmesh(pts, tri) show()
def ex_ell(h0): pfix = [[1, 1], [1, -1], [0, -1], [0, 0], [-1, 0], [-1, 1]] plotmethod = 2 print " meshing ..." fig1 = plt.figure() p1, t1 = distmesh2d(fd_ell, huniform, h0, bbox, pfix) pts, mytri = fixmesh(p1, t1) plotmesh(pts, mytri) uh, inside = poisson(f_ex, fd_ell, h0, pts, mytri, announce=True) print " plotting ..." fig2 = plt.figure() if plotmethod == 1: plotmesh(pts, tri, uh) elif plotmethod == 2: ax = fig2.gca(projection='3d') ax.scatter(pts[:, 0], pts[:, 1], uh, c=uh, cmap=cm.jet) else: # seems unreliable TRI = tri.Triangulation(pts[:, 0], pts[:, 1], triangles=mytri) ax.plot_trisurf(pts[:, 0], pts[:, 1], uh, TRI.triangles, cmap=cm.jet, linewidth=0.2)
def ell(): """L-shaped domain from 'Finite Elements and Fast Iterative Solvers' by Elman, Silvester, and Wathen.""" pfix = [[1,1], [1, -1], [0, -1], [0, 0], [-1, 0], [-1, 1]] def d(pts): return ddiff(drectangle(pts, -1, 1, -1, 1), drectangle(pts, -2, 0, -2, 0)) figure() pts, tri = distmesh2d(d, huniform, 0.25, bbox, pfix) plotmesh(pts, tri) show()
def ex_disc(h0): print " meshing ..." p1, t1 = distmesh2d(fd_disc, huniform, h0, bbox, []) pts, mytri = fixmesh(p1,t1) fig1 = plt.figure() plotmesh(pts, mytri) uh, inside = poisson(f_ex,fd_disc,h0,pts,mytri,announce=True) print " plotting ..." fig2 = plt.figure() ax = fig2.gca(projection='3d') ax.plot_trisurf(pts[:,0], pts[:,1], uh, cmap=cm.jet, linewidth=0.2) uexact = 1.0 - pts[:,0]**2.0 - pts[:,1]**2.0 # exact: u(x,y) = 1 - x^2 - y^2 err = max(abs(uh-uexact)) print "max error = %f" % err
def ell(): """L-shaped domain from 'Finite Elements and Fast Iterative Solvers' by Elman, Silvester, and Wathen.""" pfix = [[1, 1], [1, -1], [0, -1], [0, 0], [-1, 0], [-1, 1]] def d(pts): return ddiff(drectangle(pts, -1, 1, -1, 1), drectangle(pts, -2, 0, -2, 0)) figure() pts, tri = distmesh2d(d, huniform, 0.25, bbox, pfix) plotmesh(pts, tri) show()
def ex_disc(h0): print " meshing ..." p1, t1 = distmesh2d(fd_disc, huniform, h0, bbox, []) pts, mytri = fixmesh(p1, t1) fig1 = plt.figure() plotmesh(pts, mytri) uh, inside = poisson(f_ex, fd_disc, h0, pts, mytri, announce=True) print " plotting ..." fig2 = plt.figure() ax = fig2.gca(projection='3d') ax.plot_trisurf(pts[:, 0], pts[:, 1], uh, cmap=cm.jet, linewidth=0.2) uexact = 1.0 - pts[:, 0]**2.0 - pts[:, 1]**2.0 # exact: u(x,y) = 1 - x^2 - y^2 err = max(abs(uh - uexact)) print "max error = %f" % err
def obscircle(h0,psifcn): print " meshing and fixing mesh ..." p1, t1 = distmesh2d(fd_disc, huniform, h0, bbox, []) p1a, t1a = fixmesh(p1,t1) print " ... original mesh has %d nodes" % np.shape(p1a)[0] print " refining mesh once ..." p2, t2, e, ind = bdyrefine(p1a,t1a,fd_disc,h0) print " ... first refined mesh has %d nodes" % np.shape(p2)[0] print " refining mesh again ..." pts, mytri, e, ind = bdyrefine(p2,t2,fd_disc,h0) print " ... second refined mesh has %d nodes" % np.shape(pts)[0] print " solving ..." tol = 1.0e-6 uh, ii, ierr = obstacle(psifcn, f_ex, tol, fd_disc, h0, pts, mytri, \ announce=True) print " obstacle: %d iterations total to reach iteration tolerance %.2e" \ % (len(ierr), tol) psi = np.maximum(psifcn(pts),-1.0*np.ones(np.shape(pts)[0])) fig1 = plt.figure() plotmesh(pts, mytri) coin = (uh==psi) # coincidence set plt.plot(pts[coin,0],pts[coin,1],'ro',markersize=8.0) fig2 = plt.figure() ax = fig2.gca(projection='3d') #ax.plot_trisurf(pts[:,0], pts[:,1], psi, cmap=cm.Blues, linewidth=0.1) ax.plot_trisurf(pts[:,0], pts[:,1], uh, cmap=cm.jet, linewidth=0.2) ax.set_xlim3d(-1.0,1.0) ax.set_ylim3d(-1.0,1.0) ax.set_zlim3d(-0.25,1.25) fig4 = plt.figure() plt.semilogy(np.array(range(len(ierr)))+1.0,ierr,'o-') plt.xlabel('j = iteration') plt.ylabel('max diff successive iterations') plt.grid(True) if psifcn == psi_sphere: print print 'for sphere obstacle, exact solution is known:' print ' (away from the obstacle: u(r) = b log(r) )' apart = (uh > psi) print ' max norm error = %.3e' % max(abs(uh[apart] - u_exact_sphere(pts[apart])))
def ex_ell(h0): pfix = [[1,1], [1, -1], [0, -1], [0, 0], [-1, 0], [-1, 1]] plotmethod = 2 print " meshing ..." fig1 = plt.figure() p1, t1 = distmesh2d(fd_ell, huniform, h0, bbox, pfix) pts, mytri = fixmesh(p1,t1) plotmesh(pts, mytri) uh, inside = poisson(f_ex,fd_ell,h0,pts,mytri,announce=True) print " plotting ..." fig2 = plt.figure() if plotmethod == 1: plotmesh(pts, tri, uh) elif plotmethod == 2: ax = fig2.gca(projection='3d') ax.scatter(pts[:,0], pts[:,1], uh, c=uh, cmap=cm.jet) else: # seems unreliable TRI = tri.Triangulation(pts[:,0], pts[:,1], triangles=mytri) ax.plot_trisurf(pts[:,0], pts[:,1], uh, TRI.triangles, cmap=cm.jet, linewidth=0.2)
def ex_disc_refine(h0): print " meshing ..." p1, t1 = distmesh2d(fd_disc, huniform, h0, bbox, []) pts, mytri = fixmesh(p1,t1) edges, tedges = edgelist(pts,mytri) print " original mesh has %d nodes and %d edges" % (np.shape(pts)[0],np.shape(edges)[0]) fig1 = plt.figure() plt.subplot(1,2,1) plotmesh(pts, mytri) plt.title('original mesh') rp, rt, e, ind = bdyrefine(pts,mytri,fd_disc,h0) redges, tmp = edgelist(rp,rt) print " refined mesh has %d nodes and %d edges" % (np.shape(rp)[0],np.shape(redges)[0]) plt.subplot(1,2,2) plotmesh(rp, rt) plt.title('final refined mesh') uh, inside = poisson(f_ex,fd_disc,h0,rp,rt,announce=True) print " plotting ..." fig4 = plt.figure() ax = fig4.gca(projection='3d') ax.plot_trisurf(rp[:,0], rp[:,1], uh, cmap=cm.jet, linewidth=0.2) uexact = 1.0 - rp[:,0]**2.0 - rp[:,1]**2.0 # exact: u(x,y) = 1 - x^2 - y^2 err = max(abs(uh-uexact)) print "max error = %f" % err
def example_3_online(): figure() pts, tri = distmesh2d(example3_online, example3_online_h, 0.02, bbox, square) plotmesh(pts, tri) show()
def example_3b(): figure() pts, tri = distmesh2d(example3, example3_h, 0.035, bbox, square) plotmesh(pts, tri) show()
def example_3a(): figure() pts, tri = distmesh2d(example3, huniform, 0.15, bbox, square) plotmesh(pts, tri, example3(pts)) show()
def example_2(): figure() pts, tri = distmesh2d(example2, huniform, 0.1, bbox, []) plotmesh(pts, tri) show()
def example_1b(): figure() pts, tri = distmesh2d(example1, huniform, 0.2, bbox, []) plotmesh(pts, tri) show()
def example_1b(): figure() pts, tri = distmesh2d(example1, huniform, 0.2, bbox, []) plotmesh(pts, tri) show()
def circle_nonuniform(): figure() # fake the corners: pts, tri = distmesh2d(example1, circle_h, 0.1, bbox, []) plotmesh(pts, tri) show()
def example_2(): figure() pts, tri = distmesh2d(example2, huniform, 0.1, bbox, []) plotmesh(pts, tri) show()
def annulus(): figure() pts, tri = distmesh2d(example2, annulus_h, 0.04, bbox, square) plotmesh(pts, tri) show()
def example_3a(): figure() pts, tri = distmesh2d(example3, huniform, 0.15, bbox, square) plotmesh(pts, tri, example3(pts)) show()
def circle_nonuniform(): figure() # fake the corners: pts, tri = distmesh2d(example1, circle_h, 0.1, bbox, []) plotmesh(pts, tri) show()
def example_3b(): figure() pts, tri = distmesh2d(example3, example3_h, 0.035, bbox, square) plotmesh(pts, tri) show()
def example_3_online(): figure() pts, tri = distmesh2d(example3_online, example3_online_h, 0.02, bbox, square) plotmesh(pts, tri) show()
def annulus(): figure() pts, tri = distmesh2d(example2, annulus_h, 0.04, bbox, square) plotmesh(pts, tri) show()