Example #1
0
    def test_standard_aggregation(self):
        for A in self.cases:
            S = symmetric_strength_of_connection(A)

            (expected, expected_Cpts) = reference_standard_aggregation(S)
            (result, Cpts) = standard_aggregation(S)

            assert_equal((result - expected).nnz, 0)
            assert_equal(Cpts.shape[0], expected_Cpts.shape[0])
            assert_equal(np.setdiff1d(Cpts, expected_Cpts).shape[0], 0)

        # S is diagonal - no dofs aggregated
        S = spdiags([[1, 1, 1, 1]], [0], 4, 4, format='csr')
        (result, Cpts) = standard_aggregation(S)
        expected = np.array([[0], [0], [0], [0]])
        assert_equal(result.toarray(), expected)
        assert_equal(Cpts.shape[0], 0)
Example #2
0
    def test_standard_aggregation(self):
        for A in self.cases:
            S = symmetric_strength_of_connection(A)

            (expected, expected_Cpts) = reference_standard_aggregation(S)
            (result, Cpts) = standard_aggregation(S)

            assert_equal((result - expected).nnz, 0)
            assert_equal(Cpts.shape[0], expected_Cpts.shape[0])
            assert_equal(np.setdiff1d(Cpts, expected_Cpts).shape[0], 0)

        # S is diagonal - no dofs aggregated
        S = spdiags([[1, 1, 1, 1]], [0], 4, 4, format='csr')
        (result, Cpts) = standard_aggregation(S)
        expected = np.array([[0], [0], [0], [0]])
        assert_equal(result.toarray(), expected)
        assert_equal(Cpts.shape[0], 0)
def get_aggregate(A, strength, aggregate, diagonal_dominance, B, **kwargs):
    def unpack_arg(v):
        if isinstance(v, tuple):
            return v[0], v[1]
        else:
            return v, {}

    # Compute the strength-of-connection matrix C, where larger
    # C[i,j] denote stronger couplings between i and j.
    fn, kwargs = unpack_arg(strength)
    if fn == 'symmetric':
        C = symmetric_strength_of_connection(A, **kwargs)
    elif fn == 'classical':
        C = classical_strength_of_connection(A, **kwargs)
    elif fn == 'distance':
        C = distance_strength_of_connection(A, **kwargs)
    elif (fn == 'ode') or (fn == 'evolution'):
        if 'B' in kwargs:
            C = evolution_strength_of_connection(A, **kwargs)
        else:
            C = evolution_strength_of_connection(A, B, **kwargs)
    elif fn == 'energy_based':
        C = energy_based_strength_of_connection(A, **kwargs)
    elif fn == 'predefined':
        C = kwargs['C'].tocsr()
    elif fn == 'algebraic_distance':
        C = algebraic_distance(A, **kwargs)
    elif fn == 'affinity':
        C = affinity_distance(A, **kwargs)
    elif fn is None:
        C = A.tocsr()
    else:
        raise ValueError('unrecognized strength of connection method: %s' %
                         str(fn))

    # Avoid coarsening diagonally dominant rows
    flag, kwargs = unpack_arg(diagonal_dominance)
    if flag:
        C = eliminate_diag_dom_nodes(A, C, **kwargs)

    # Compute the aggregation matrix AggOp (i.e., the nodal coarsening of A).
    # AggOp is a boolean matrix, where the sparsity pattern for the k-th column
    # denotes the fine-grid nodes agglomerated into k-th coarse-grid node.
    fn, kwargs = unpack_arg(aggregate)
    if fn == 'standard':
        AggOp = standard_aggregation(C, **kwargs)[0]
    elif fn == 'naive':
        AggOp = naive_aggregation(C, **kwargs)[0]
    elif fn == 'lloyd':
        AggOp = lloyd_aggregation(C, **kwargs)[0]
    elif fn == 'pairwise':
        AggOp = pairwise_aggregation(A, B, **kwargs)[0]
    elif fn == 'predefined':
        AggOp = kwargs['AggOp'].tocsr()
    else:
        raise ValueError('unrecognized aggregation method %s' % str(fn))

    return AggOp
Example #4
0
    def test_standard_aggregation(self):
        for A in self.cases:
            S = symmetric_strength_of_connection(A)

            (expected, expected_Cpts) = reference_standard_aggregation(S)
            (result, Cpts) = standard_aggregation(S)

            assert_equal((result - expected).nnz, 0)
            assert_equal(Cpts.shape[0], expected_Cpts.shape[0])
            assert_equal(np.setdiff1d(Cpts, expected_Cpts).shape[0], 0)
Example #5
0
    def test_standard_aggregation(self):
        for A in self.cases:
            S = symmetric_strength_of_connection(A)

            (expected, expected_Cpts) = reference_standard_aggregation(S)
            (result, Cpts) = standard_aggregation(S)

            assert_equal((result - expected).nnz, 0)
            assert_equal(Cpts.shape[0], expected_Cpts.shape[0])
            assert_equal(np.setdiff1d(Cpts, expected_Cpts).shape[0], 0)
Example #6
0
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')
    Cpts = [i for i in range(0, n) if splitting[i] == 1]

Fpts = [i for i in range(0, n) if i not in Cpts]
num_Fpts = len(Fpts)
num_Cpts = len(Cpts)
num_bad_guys = 1
cf_ratio = float(num_Cpts) / num_Fpts

# Permutation matrix to sort rows
permute = identity(n, format='csr')
permute.indices = np.concatenate((Fpts, Cpts))
permute = permute.T