def test_1d(self): stencil = array([1, 2, 3]) result = stencil_grid(stencil, (3, )).todense() expected = matrix([[2, 3, 0], [1, 2, 3], [0, 1, 2]]) assert_equal(result, expected) stencil = array([1, 2, 3, 4, 5]) result = stencil_grid(stencil, (5, )).todense() expected = matrix([[3, 4, 5, 0, 0], [2, 3, 4, 5, 0], [1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 0, 1, 2, 3]]) assert_equal(result, expected)
def test_poisson3d_7pt(self): stencil = array([[[0, 0, 0], [0, -1, 0], [0, 0, 0]], [[0, -1, 0], [-1, 6, -1], [0, -1, 0]], [[0, 0, 0], [0, -1, 0], [0, 0, 0]]]) cases = [] cases.append(((1, 1, 1), matrix([[6]]))) cases.append(((2, 1, 1), matrix([[6, -1], [-1, 6]]))) cases.append(((2, 2, 1), matrix([[6, -1, -1, 0], [-1, 6, 0, -1], [-1, 0, 6, -1], [0, -1, -1, 6]]))) cases.append(((2, 2, 2), matrix([[6, -1, -1, 0, -1, 0, 0, 0], [-1, 6, 0, -1, 0, -1, 0, 0], [-1, 0, 6, -1, 0, 0, -1, 0], [0, -1, -1, 6, 0, 0, 0, -1], [-1, 0, 0, 0, 6, -1, -1, 0], [0, -1, 0, 0, -1, 6, 0, -1], [0, 0, -1, 0, -1, 0, 6, -1], [0, 0, 0, -1, 0, -1, -1, 6]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).todense() assert_equal(result, expected)
def test_poisson3d_7pt(self): stencil = np.array([[[0, 0, 0], [0, -1, 0], [0, 0, 0]], [[0, -1, 0], [-1, 6, -1], [0, -1, 0]], [[0, 0, 0], [0, -1, 0], [0, 0, 0]]]) cases = [] cases.append(((1, 1, 1), np.array([[6]]))) cases.append(((2, 1, 1), np.array([[6, -1], [-1, 6]]))) cases.append(((2, 2, 1), np.array([[6, -1, -1, 0], [-1, 6, 0, -1], [-1, 0, 6, -1], [0, -1, -1, 6]]))) cases.append(((2, 2, 2), np.array([[6, -1, -1, 0, -1, 0, 0, 0], [-1, 6, 0, -1, 0, -1, 0, 0], [-1, 0, 6, -1, 0, 0, -1, 0], [0, -1, -1, 6, 0, 0, 0, -1], [-1, 0, 0, 0, 6, -1, -1, 0], [0, -1, 0, 0, -1, 6, 0, -1], [0, 0, -1, 0, -1, 0, 6, -1], [0, 0, 0, -1, 0, -1, -1, 6]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).toarray() assert_equal(result, expected)
def test_1d(self): stencil = array([1, 2, 3]) result = stencil_grid(stencil, (3,)).todense() expected = matrix([[2, 3, 0], [1, 2, 3], [0, 1, 2]]) assert_equal(result, expected) stencil = array([1, 2, 3, 4, 5]) result = stencil_grid(stencil, (5,)).todense() expected = matrix([[3, 4, 5, 0, 0], [2, 3, 4, 5, 0], [1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 0, 1, 2, 3]]) assert_equal(result, expected)
def test_2d(self): stencil = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) result = stencil_grid(stencil, (2, 3)).todense() expected = matrix([[5, 6, 0, 8, 9, 0], [4, 5, 6, 7, 8, 9], [0, 4, 5, 0, 7, 8], [2, 3, 0, 5, 6, 0], [1, 2, 3, 4, 5, 6], [0, 1, 2, 0, 4, 5]]) assert_equal(result, expected)
def test_poisson1d(self): stencil = array([-1, 2, -1]) cases = [] cases.append(((1, ), matrix([[2]]))) cases.append(((2, ), matrix([[2, -1], [-1, 2]]))) cases.append(((4, ), matrix([[2, -1, 0, 0], [-1, 2, -1, 0], [0, -1, 2, -1], [0, 0, -1, 2]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).todense() assert_equal(result, expected)
def test_poisson1d(self): stencil = array([-1, 2, -1]) cases = [] cases.append(((1,), matrix([[2]]))) cases.append(((2,), matrix([[2, -1], [-1, 2]]))) cases.append(((4,), matrix([[2, -1, 0, 0], [-1, 2, -1, 0], [0, -1, 2, -1], [0, 0, -1, 2]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).todense() assert_equal(result, expected)
def test_poisson2d_9pt(self): stencil = array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]) cases = [] cases.append(((1, 1), matrix([[8]]))) cases.append(((2, 1), matrix([[8, -1], [-1, 8]]))) cases.append(((1, 2), matrix([[8, -1], [-1, 8]]))) cases.append(((1, 3), matrix([[8, -1, 0], [-1, 8, -1], [0, -1, 8]]))) cases.append(((2, 2), matrix([[8, -1, -1, -1], [-1, 8, -1, -1], [-1, -1, 8, -1], [-1, -1, -1, 8]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).todense() assert_equal(result, expected)
def test_poisson2d_5pt(self): stencil = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) cases = [] cases.append(((1, 1), np.array([[4]]))) cases.append(((2, 1), np.array([[4, -1], [-1, 4]]))) cases.append(((1, 2), np.array([[4, -1], [-1, 4]]))) cases.append(((1, 3), np.array([[4, -1, 0], [-1, 4, -1], [0, -1, 4]]))) cases.append(((2, 2), np.array([[4, -1, -1, 0], [-1, 4, 0, -1], [-1, 0, 4, -1], [0, -1, -1, 4]]))) for grid, expected in cases: result = stencil_grid(stencil, grid).toarray() assert_equal(result, expected)
from copy import deepcopy # ------------------------------------------------------------------------- # # ------------------------------------------------------------------------- # SOC = 'evol' SOC_drop = 4.0 SA = 1 # Use SA type coarsening or CR pow_G = 1 epsilon = 0.1 theta = 3.0 * math.pi / 16.0 N = 40 n = N * N grid_dims = [N, N] stencil = diffusion_stencil_2d(epsilon, theta) A = stencil_grid(stencil, grid_dims, format='csr') [d, d, A] = symmetric_rescaling(A) A = csr_matrix(A) B = np.kron(np.ones((A.shape[0] / 1, 1), dtype=A.dtype), np.eye(1)) tol = 1e-12 # Drop tolerance for singular values if SA: if SOC == 'evol': C = evolution_strength_of_connection(A, B, epsilon=SOC_drop, k=2) else: SOC = 'symm' C = symmetric_strength_of_connection(A, theta=SOC_drop) AggOp, Cpts = standard_aggregation(C) else: splitting = CR(A, method='habituated')