def test_jacobi():
    N = 100
    A, b = poisson_system_1d(N)

    x0, res = solve_jacobi(A, b, maxiter=2000)
    assert res < 0.01, "Residual is %e" % res
def test_sym_gauss_seidel():
    N = 100
    A, b = poisson_system_1d(N)

    x0, res = solve_sym_gauss_seidel(A, b, maxiter=2000)
    assert res < 0.0001, "Residual is %e" % res
def test_block_jacobi():
    N = 123
    A, b = poisson_system_1d(N)
    x0, res = solve_block_jacobi(A, b, n_partitions=7, maxiter=800)
    assert res < 0.0001, "Residual is %e" % res
                    if j != i and A_sub[i, j].nnz != 0:
                        sum_mat_vec_results += A_sub[i, j] * x_sub[j]

                x_sub[i] = spsolve(A_sub[i, i], b_sub[i] - sum_mat_vec_results)


if __name__ == "__main__":
    comm = MPI.COMM_WORLD
    my_id = comm.Get_rank()
    n_proc = comm.Get_size()

    if my_id == 0:
        n_partitions = 20

        N = 1311
        A, b = poisson_system_1d(N)

        x = np.zeros(N)

        A_sub = partition_mat_by_num(A, n_partitions)
        b_sub = partition_vec_by_num(b, n_partitions)
        x_sub = partition_vec_by_num(x, n_partitions)

        indices_ptrs = np.linspace(0, n_partitions, num=n_proc + 1, dtype=np.int)

        G = nx.Graph()
        for i in xrange(n_partitions):
            for j in xrange(n_partitions):
                if A_sub[i, j].nnz != 0 and i != j:
                    G.add_edge(i, j)