def test_minSize(self): parameters = self.parameters.copy() shape = parameters["problemShape"] = (1024,) parameters["gridLevels"] = 24 parameters["minSize"] = 23 u_actual = np.random.random(shape).ravel() A_in = operators.poisson(shape) b = tools.flexibleMmult(A_in, u_actual) soln, info_dict = mgSolve(A_in, b, parameters) if self.verbose: print "R shapes:", [r.shape for r in info_dict['R']] print "c.v. minSize = ", parameters['minSize'] assert min(info_dict['R'][-1].shape) > parameters["minSize"]
def test_1d_noise_mg(self, parameters=None): '''Solve a 2D Poisson equation, with the solution being 2D white noise, i.e., np.random.random((NX,NY)). Useful as a sanity check. ''' if parameters is None: parameters = self.parameters N = NX = parameters['problemShape'][0] u_actual = np.random.random((NX,)).reshape((N, 1)) A_in = operators.poisson((NX,)) b = tools.flexibleMmult(A_in, u_actual) u_mg, info_dict = mgSolve(A_in, b, parameters) if self.verbose: print 'test_1d_noise_mg: final norm is ', info_dict['norm']
def test_threshStop(self): size = 36 u_actual = np.sin(np.array(range(int(size))) * 3.0 / size).T A = operators.poisson((size,)) b = tools.flexibleMmult(A, u_actual) parameters = { 'problemShape': (size,), 'gridLevels': 2, 'threshold': 8e-3, 'giveInfo': True, } u_mmg, info_dict = mgSolve(A, b, parameters) residual_norm = np.linalg.norm(tools.flexibleMmult(A, u_mmg) - b) assert parameters['threshold'] > residual_norm
def test_2d_mpl_demo(self): ''' Plot a pretty 2D surface, and the result of solving a 2D Poisson equation with that surface as the solution. ''' from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt sizemultiplier = 2 NX = 8 * sizemultiplier cycles = 3 actualtraces = 8 mgtraces = 16 NY = NX N = NX * NY (X, Y, u_actual) = mpl_test_data(delta=1 / float(sizemultiplier)) A_in = operators.poisson((NX, NY)) b = tools.flexibleMmult(A_in, u_actual.ravel()) u_mg = mgSolve(A_in, b, { 'problemShape': (NX, NY), 'gridLevels': 2, 'iterations': 1, 'verbose': False, 'cycles': 300, 'dense': True } ) fig = plt.figure() ax = Axes3D(fig) fig.suptitle('blue is actual; red is %i v-cycles; %i unknowns' % (cycles, N)) # ax = fig.add_subplot(111,projection='3d') #not in matplotlib version 0.99, only version 1.x ax.plot_wireframe(X, Y, u_actual, rstride=NX / actualtraces, cstride=NY / actualtraces, color=((0, 0, 1.0),) ) # ax.scatter(X,Y,u_mg[0],c=((1.,0,0),),s=40) ax.plot_wireframe(X, Y, u_mg.reshape((NX, NY)), rstride=NX / mgtraces, cstride=NY / mgtraces, color=((1.0, 0, 0),) ) filename = 'output/test_2d_mpl_demo-%i_vycles-%i_N.png' % (cycles, N) if self.saveFig: fig.savefig(filename) # plt.show() if self.verbose: print 'norm is', (np.linalg.norm(openmg.tools.getresidual(b, A_in, u_mg[0], N)))
def test_3d_noise_mg(self): '''Solve a 3D Poisson equation, with the solution being 2D white noise, i.e., np.random.random((NX, NY, NZ)). Useful as a sanity check. ''' NX = NY = NZ = 8 N = NX * NY * NZ shape = NX, NY, NZ u_actual = np.random.random(shape).reshape((N, 1)) A_in = operators.poisson(shape) b = tools.flexibleMmult(A_in, u_actual) u_mg, info_dict = mgSolve(A_in, b, { 'problemShape': shape, 'gridLevels': 3, 'iterations': 1, 'verbose': False, 'threshold': 4, 'giveInfo': True, } ) if self.verbose: print 'test_1d_noise_mg: final norm is ', info_dict['norm']
def test_a(self): ''' Poisson equation on a sine curve. In 3D? It wasn't originally, but the restriciton algorithm was having trouble with 1D. This might bear investigation. ''' problemscale = 12 size = problemscale ** 3 gridLevels = 4 u_zeros = np.zeros((size,)) u_actual = np.sin(np.array(range(int(size))) * 3.0 / size).T A = operators.poisson((size,)) b = tools.flexibleMmult(A, u_actual) uSmoothed = smooth(A, b, u_zeros, iterations=1) parameters = {'coarsestLevel': gridLevels - 1, 'problemShape': (problemscale, problemscale, problemscale), 'gridLevels': gridLevels, 'threshold': 8e-3, } u_mmg = mgSolve(A, b, parameters) if self.verbose: print 'norm is', (np.linalg.norm(tools.getresidual(b, A, uSmoothed.reshape((size, 1)), size))) residual_norm = np.linalg.norm(tools.flexibleMmult(A, u_mmg) - b) assert parameters['threshold'] > residual_norm