def uexactfn(t,cp): th,phi,r = cart2sph(cp[:,0],cp[:,1],cp[:,2]) return np.exp(-2*t)*np.cos(phi + np.pi / 2) if __name__ == '__main__': MBlocklist = [10,20,40,80] error = [] dx = [] comm = MPI.COMM_WORLD surface = Sphere(center=np.array([0.0, 0.0, 0.0])) # exact solution rez = 30 xx, yy, zz = surface.parametric_grid(rez) th, phi, r = cart2sph(xx,yy,zz) exactu = np.cos(phi + np.pi / 2).ravel() points = np.array([xx.ravel(),yy.ravel(),zz.ravel()]).T vsize = xx.ravel().shape[0] vAssigned = vsize // comm.size + int(comm.rank < (vsize % comm.size)) vstart = comm.exscan(vAssigned) if comm.rank == 0: vstart = 0 for MBlock in MBlocklist: opt = {'M':MBlock,'m':5,'d':3} band = Band(surface,comm,opt)
u = np.cos(phi + np.pi / 2) # Let's keep a copy of the initial conditions initial_u = u.copy() # Build interpolation and differential matrix. E = build_interp_matrix(int_grid, cp, dx, p, ll, virtual_grid_shape) L = build_diff_matrix(int_grid, dx, virtual_grid_shape) M = build_linear_diagonal_splitting(L, E) # Compute eigenvalues t = time() print "Computing eigenvalues..." Evals, Evecs = splinalg.eigs(-M, k=32, which="SM") sorted_indices = np.argsort(Evals) print "...took", time() -t xp, yp, zp = s.parametric_grid(65) _, phi_plot, _ = cart2sph(xp, yp, zp) Eplot = build_interp_matrix(int_grid, np.column_stack((xp.ravel(), yp.ravel(), zp.ravel())), dx, p, ll, virtual_grid_shape) if PLOT: # Plotting code. Build a pipeline to be able to change the data later. src = mlab.pipeline.grid_source(xp, yp, zp, scalars=zp) normals = mlab.pipeline.poly_data_normals(src) surf = mlab.pipeline.surface(normals) mlab.colorbar()
virtual_grid_shape = np.abs(ur - ll) / dx + 1 # The (i,j,...) indices of the grid points, taking `ll` as origin. int_grid = np.round((grid - ll) / dx).astype(np.int) # Initial conditions th, phi, r = cart2sph(grid[:, 0], grid[:, 1], grid[:, 2]) u = np.cos(phi + np.pi / 2) # Let's keep a copy of the initial conditions initial_u = u.copy() # Build interpolation and differential matrix. E = build_interp_matrix(int_grid, cp, dx, p, ll, virtual_grid_shape) L = build_diff_matrix(int_grid, dx, virtual_grid_shape) xp, yp, zp = s.parametric_grid(65) _, phi_plot, _ = cart2sph(xp, yp, zp) Eplot = build_interp_matrix( int_grid, np.column_stack((xp.ravel(), yp.ravel(), zp.ravel())), dx, p, ll, virtual_grid_shape) if PLOT: # Plotting code. Build a pipeline to be able to change the data later. src = mlab.pipeline.grid_source(xp, yp, zp, scalars=(Eplot * u).reshape(xp.shape)) normals = mlab.pipeline.poly_data_normals(src) surf = mlab.pipeline.surface(normals) mlab.colorbar()
ymin, ymax = -1.5, 1.5 xi = np.round(horizontal_res * (grid[:, 0] - xmin) / (xmax - xmin)).astype( np.int) yi = (vertical_res - np.round(vertical_res * (grid[:, 1] - ymin) / (ymax - ymin))).astype(np.int) # Forcing functions chi = np.zeros(grid.shape[0]) # Create forcing function ix = grid[:, 2] >= surf_plane chi[ix] = image[xi[ix], yi[ix]] # Make sure forcing is a cp extension chi = E * chi # Re-threshold chi = (chi > 0.9).astype(np.int) xp, yp, zp = s.parametric_grid(256) Eplot = build_interp_matrix( int_grid, np.column_stack((xp.ravel(), yp.ravel(), zp.ravel())), dx, p, ll, virtual_grid_shape) # Plot forcing function mlab.figure(1, fgcolor=(1.0, 1.0, 1.0)) mlab.mesh(xp, yp, zp, scalars=(Eplot * (1 - chi)).reshape(xp.shape)) mlab.view(azimuth=158, elevation=25, distance=7) mlab.title('forcing function') # Parameters and functions for Gray--Scott F = 0.054 kk = 0.063 nuu = 1 / (3 / dx.min())**2 nuv = nuu / 2.
ymin, ymax = -1.5, 1.5 xi = np.round(horizontal_res * (grid[:, 0] - xmin) / (xmax - xmin)).astype(np.int) yi = (vertical_res - np.round(vertical_res * (grid[:, 1] - ymin) / (ymax - ymin))).astype(np.int) # Forcing functions chi = np.zeros(grid.shape[0]) # Create forcing function ix = grid[:, 2] >= surf_plane chi[ix] = image[xi[ix], yi[ix]] # Make sure forcing is a cp extension chi = E * chi # Re-threshold chi = (chi > 0.9).astype(np.int) xp, yp, zp = s.parametric_grid(256) Eplot = build_interp_matrix(int_grid, np.column_stack((xp.ravel(), yp.ravel(), zp.ravel())), dx, p, ll, virtual_grid_shape) # Plot forcing function mlab.figure(1,fgcolor=(1.0,1.0,1.0)) mlab.mesh(xp, yp, zp, scalars=(Eplot*(1-chi)).reshape(xp.shape)) mlab.view(azimuth=158, elevation=25, distance=7) mlab.title('forcing function') # Parameters and functions for Gray--Scott F = 0.054
def initialu(cp): th,phi,r = cart2sph(cp[:,0],cp[:,1],cp[:,2]) return np.cos(phi + np.pi / 2) if __name__ == '__main__': MBlocklist = [10,20,40,80] Tf = 0.5 error = [] dx = [] comm = MPI.COMM_WORLD surface = Sphere(center=np.array([0.0, 0.0, 0.0])) # exact solution rez = 30 xx, yy, zz = surface.parametric_grid(rez) th, phi, r = cart2sph(xx,yy,zz) exactu = np.cos(phi + np.pi / 2).ravel() points = np.array([xx.ravel(),yy.ravel(),zz.ravel()]).T vsize = xx.ravel().shape[0] vAssigned = vsize // comm.size + int(comm.rank < (vsize % comm.size)) vstart = comm.exscan(vAssigned) if comm.rank == 0: vstart = 0 for MBlock in MBlocklist: opt = {'M':MBlock,'m':5,'d':3}