# from enthought.mayavi import mlab from cp.surfaces.coordinate_transform import cart2sph 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:
"""Solves the heat equation on a true sphere.""" import numpy as np try: from mayavi import mlab except ImportError: from enthought.mayavi import mlab from cp.surfaces import Sphere from cp.build_matrices import build_interp_matrix, build_diff_matrix from cp.surfaces.coordinate_transform import cart2sph PLOT = False s = Sphere() p = 3 diff_stencil_arm = 1 dim = 3 # As a byproduct of finding the banded grid, we already have its # closest points, so we don't really have to call s.closest_point() cp, distance, grid, dx = s.grid(num_blocks_per_dim=41, levels=1, p=p, diff_stencil_arm=diff_stencil_arm) cp2, distance2, _, _ = s.closest_point(grid) assert np.allclose(cp, cp2) assert np.allclose(distance, distance2) # Corners of the virtual grid, superset of `grid` ll = np.array(dim * [grid.min()]) - 3 * dx
from time import time import numpy as np import scipy.sparse.linalg as splinalg try: from mayavi import mlab except ImportError: from enthought.mayavi import mlab from cp.surfaces import Sphere from cp.build_matrices import build_interp_matrix, build_diff_matrix, build_linear_diagonal_splitting from cp.surfaces.coordinate_transform import cart2sph PLOT = True s = Sphere() p = 3 diff_stencil_arm = 1 dim = 3 # As a byproduct of finding the banded grid, we already have its # closest points, so we don't really have to call s.closest_point() cp, distance, grid, dx = s.grid(num_blocks_per_dim=41, levels=1, p=p, diff_stencil_arm=diff_stencil_arm) cp2, distance2, _, _ = s.closest_point(grid) assert np.allclose(cp, cp2) assert np.allclose(distance, distance2)
# grid 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() # Let's build the matrices. TODO: I think it would be nicer to use # `grid` instead of `int_grid`. It is a simple change. E = build_interp_matrix(int_grid, cp, dx, p, ll, virtual_grid_shape) # TODO: being able to select different laplacian operators. Currently # it uses the second order laplacian for 2D and 3D. We could probably # use stencils.py, and give the stencil as a parameter L = build_diff_matrix(int_grid, dx, virtual_grid_shape) # Points in the surface of the sphere, used por plotting xp, yp, zp = Sphere().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()
def setUp(self): self.surface = Sphere()
"""Controlled patterns using the Grey-Scott reaction-diffusion equations and a forcing term.""" import numpy as np from scipy.sparse import eye import matplotlib.pyplot as plt try: from mayavi import mlab except ImportError: from enthought.mayavi import mlab from cp.surfaces import Sphere from cp.build_matrices import build_interp_matrix, build_diff_matrix PLOT = True s = Sphere() p = 3 diff_stencil_arm = 1 dim = 3 # As a byproduct of finding the banded grid, we already have its # closest points, so we don't really have to call s.closest_point() cp, distance, grid, dx = s.grid(num_blocks_per_dim=41, levels=2, p=p, diff_stencil_arm=diff_stencil_arm) # Corners of the virtual grid, superset of `grid` # TODO: Hide this inside grid ll = np.array(dim * [grid.min()]) - 3 * dx ur = np.array(dim * [grid.max()]) + 3 * dx
from cp.surfaces.coordinate_transform import cart2sph 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: