Beispiel #1
0
    def test_coefficient_vector(self):
        mesh = (np.arange(3), np.arange(10, 12))
        G = Grid.Grid(mesh, initializer=lambda *x: 0.0)
        # print G
        F = FD.FiniteDifferenceEngineADI(G,
                                         coefficients={
                                             (0, 1): lambda t, *x: 1
                                         })

        op = self.F.operators[(0, 1)]
        op.D.data[op.D.data != 0] = op.D.data[op.D.data != 0]**0
        op.D = todia(op.D)
        # diamat = op.D
        # densemat = diamat.todense()

        # f0 = lambda t, *x: x[0]
        # f1 = lambda t, *x: x[1]
        f = lambda t, *x: x

        vec = F.coefficient_vector(f, 0, 0)
        ref = np.tile(mesh[0], G.shape[1]), np.repeat(mesh[1], G.shape[0])
        npt.assert_array_equal(ref, vec, "Failed when focused on dim: %i", 0)

        vec = F.coefficient_vector(f, 0, 1)
        ref = np.repeat(mesh[0], G.shape[1]), np.tile(mesh[1], G.shape[0])
        npt.assert_array_equal(ref, vec, "Failed when focused on dim: %i", 1)
Beispiel #2
0
def apWithRes(A, U, R, blocks=1):
    A = todia(A)
    d = foldMatFor(A, blocks)
    diagonalize = d.dot
    undiagonalize = d.todense().I.dot
    ret = undiagonalize(diagonalize(A).dot(U)) + R
    return d, ret
Beispiel #3
0
 def test_migrate_01(self):
     B = self.F.operators[(0,1)]
     B.D = utils.todia(B.D.tocoo().todia())
     ref = B.copy()
     B = BOG.BandedOperator(B, "test 01")
     B = B.immigrate("test 01")
     npt.assert_array_equal(ref.D.todense(), B.D.todense())
     assert ref == B
Beispiel #4
0
def solveWithRes(A, U, R, blocks=1):
    A = todia(A)
    d = foldMatFor(A, blocks)
    diagonalize = compose(todia, d.dot)
    # undiagonalize = compose(lambda x: x.A[0], d.todense().I.dot)
    diaA = diagonalize(A)
    ret = diaA.todense().I.dot(d.dot(U-R)).A[0]
    return ret
Beispiel #5
0
 def test_copy_csr(self):
     ref = self.F.operators[(0,1)]
     tst = BOG.BandedOperator(ref).copy().immigrate()
     ref.D = utils.todia(ref.D.tocoo().todia())
     # fp(ref.D)
     # print
     # fp(tst.D)
     # print
     # fp(tst.D - ref.D)
     npt.assert_array_equal(ref.D.todense(), tst.D.todense())
     npt.assert_equal(tst, ref)
    def setUp(self):
        loops = 200
        blocks = 2
        decimals = 12

        self.loops = loops
        self.blocks = blocks
        self.decimals = decimals

        mat = np.matrix("""
                        -1.5 0.5 2 0 0;
                        -1   2  -1 0 0;
                        0   -1   2 -1 0;
                        0   0    -1 2 -1;
                        0   0   -1.5 0.5 2""").A
        diamat = todia(scipy.sparse.dia_matrix(mat))

        x = foldMatFor(diamat, 1).todense().A
        topx = x.copy()
        bottomx = x.copy()
        topx[-1,-2] = 0
        bottomx[0,1] = 0
        x, topx, bottomx = map(todia, [x, topx, bottomx])


        topblockx = scipy.sparse.block_diag([topx]*blocks)
        bottomblockx = scipy.sparse.block_diag([bottomx]*blocks)
        blockx = todia(scipy.sparse.block_diag([x]*blocks))
        self.blockxI = blockx.todense().I.A
        self.topblockxI = topblockx.todense().I.A
        self.bottomblockxI = bottomblockx.todense().I.A

        npt.assert_array_equal(todia(topblockx.dot(bottomblockx)).data, blockx.data)

        blockdiamat = todia(scipy.sparse.block_diag([diamat]*blocks))

        blocktridiamat = todia(blockx.dot(blockdiamat))
        topblocktridiamat = todia(topblockx.dot(blockdiamat))
        bottomblocktridiamat = todia(bottomblockx.dot(blockdiamat))

        self.blockdiamat = blockdiamat

        self.blockx = blockx
        self.topblockx = topblockx
        self.bottomblockx = bottomblockx

        self.blocktridiamat = blocktridiamat
        self.topblocktridiamat = topblocktridiamat
        self.bottomblocktridiamat = bottomblocktridiamat

        self.vec = np.random.random(mat.shape[1]*blocks)

        self.B = FD.BandedOperator((blockdiamat.data, (2, 1, 0, -1, -2)), inplace=False)
        self.B.blocks = blocks
    def test_coefficient_vector(self):
        mesh = (np.arange(3), np.arange(10, 12))
        G = Grid.Grid(mesh, initializer=lambda *x: 0.0)
        # print G
        F = FD.FiniteDifferenceEngineADI(G, coefficients={(0, 1): lambda t, *x: 1})

        op = self.F.operators[(0, 1)]
        op.D.data[op.D.data != 0] = op.D.data[op.D.data != 0] ** 0
        op.D = todia(op.D)
        # diamat = op.D
        # densemat = diamat.todense()

        # f0 = lambda t, *x: x[0]
        # f1 = lambda t, *x: x[1]
        f = lambda t, *x: x

        vec = F.coefficient_vector(f, 0, 0)
        ref = np.tile(mesh[0], G.shape[1]), np.repeat(mesh[1], G.shape[0])
        npt.assert_array_equal(ref, vec, "Failed when focused on dim: %i", 0)

        vec = F.coefficient_vector(f, 0, 1)
        ref = np.repeat(mesh[0], G.shape[1]), np.tile(mesh[1], G.shape[0])
        npt.assert_array_equal(ref, vec, "Failed when focused on dim: %i", 1)
Beispiel #8
0
    def test_combine_dimensional_operators_1(self):
        if self.F.operators[1].solve_banded_offsets[1] != 2:
            print self.skipTest(
                "Using first order boundary approximation. Top is tridiag.")
        oldL2 = self.L2_.copy()
        oldL2 = todia(oldL2.todense())
        # high, low = 2, -2
        # m = tuple(oldL2.offsets).index(0)
        # oldL2.data = oldL2.data[m-high:m-low+1]
        # oldL2.offsets = oldL2.offsets[m-high:m-low+1]

        oldR2 = self.R2_.flatten()

        L2 = self.F.operators[1]

        # print "old"
        # fp(oldL2.data)
        # print
        # print "new"
        # fp(L2.D.data)
        # print "old"
        # fp(oldL2.todense())
        # print
        # print "new"
        # fp(L2.D.todense())
        # print
        # print "diff"
        # fp(oldL2.todense() - L2.D.todense())
        npt.assert_allclose(L2.D.data, oldL2.data)
        # print "old"
        # print oldR2
        # print
        # print "new"
        # print L2.R
        # print "diff"
        # fp(L2.R - oldR2)
        npt.assert_allclose(L2.R, oldR2)
    def test_combine_dimensional_operators_1(self):
        if self.F.operators[1].solve_banded_offsets[1] != 2:
            print self.skipTest("Using first order boundary approximation. Top is tridiag.")
        oldL2 = self.L2_.copy()
        oldL2 = todia(oldL2.todense())
        # high, low = 2, -2
        # m = tuple(oldL2.offsets).index(0)
        # oldL2.data = oldL2.data[m-high:m-low+1]
        # oldL2.offsets = oldL2.offsets[m-high:m-low+1]

        oldR2 = self.R2_.flatten()

        L2 = self.F.operators[1]

        # print "old"
        # fp(oldL2.data)
        # print
        # print "new"
        # fp(L2.D.data)
        # print "old"
        # fp(oldL2.todense())
        # print
        # print "new"
        # fp(L2.D.todense())
        # print
        # print "diff"
        # fp(oldL2.todense() - L2.D.todense())
        npt.assert_allclose(L2.D.data, oldL2.data)
        # print "old"
        # print oldR2
        # print
        # print "new"
        # print L2.R
        # print "diff"
        # fp(L2.R - oldR2)
        npt.assert_allclose(L2.R, oldR2)
Beispiel #10
0
    def test_create_mixed_derivative(self):
        B = self.F.simple_operators[(0,1)]
        # BG = BOG.mixed_for_vector(self.F.grid.mesh[0],
                                  # self.F.grid.mesh[1]).immigrate()
        B.D = scipy.sparse.coo_matrix(B.D)
        refcoo = B.D.copy()

        n0 = self.F.grid.mesh[0].size
        n1 = self.F.grid.mesh[1].size

        compute_deltas = lambda v: np.hstack((np.nan, np.diff(v)))

        d0 = compute_deltas(self.F.grid.mesh[0])
        d1 = compute_deltas(self.F.grid.mesh[1])

        dlen = (n1-2) * (n0-2)

        sup =   d1[1:n1-1]             /   (d1[2:n1]*(d1[1:n1-1]+d1[2:n1]))
        mid = (-d1[1:n1-1] + d1[2:n1]) /             (d1[1:n1-1]*d1[2:n1])
        sub =               -d1[2:n1]  / (d1[1:n1-1]*(d1[1:n1-1]+d1[2:n1]))

        supsup = np.tile(sup, n0-2)
        supmid = np.tile(mid, n0-2)
        supsub = np.tile(sub, n0-2)

        midsup = supsup.copy()
        midmid = supmid.copy()
        midsub = supsub.copy()

        subsup = supsup.copy()
        submid = supmid.copy()
        subsub = supsub.copy()

        dsup =   d0[1:n0-1]             / (d0[2:n0]*(d0[1:n0-1]+d0[2:n0]))
        dmid = (-d0[1:n0-1] + d0[2:n0]) /           (d0[1:n0-1]*d0[2:n0])
        dsub =               -d0[2:n0]  / (d0[1:n0-1]*(d0[1:n0-1]+d0[2:n0]))

        dsup = np.repeat(dsup, n1-2)
        dmid = np.repeat(dmid, n1-2)
        dsub = np.repeat(dsub, n1-2)

        supsup *= dsup
        supmid *= dsup
        supsub *= dsup

        midsup *= dmid
        midmid *= dmid
        midsub *= dmid

        subsup *= dsub
        submid *= dsub
        subsub *= dsub

        tstdata = np.vstack(reversed([supsup,supmid,supsub,midsup,midmid,midsub,subsup,submid,subsub])).ravel()
        B.D = utils.todia(scipy.sparse.dia_matrix(B.D))
        BOG.scipy_to_cublas(B)
        refdata = B.D.data[B.D.data.nonzero()].reshape((9, -1))[::-1].ravel()
        BOG.cublas_to_scipy(B)
        npt.assert_array_equal(tstdata, refdata)

        row = np.tile(np.arange(1, n1-1), n0-2)
        row += np.repeat(np.arange(1, n0-1), n1-2) * n1
        row = np.tile(row, 9)

        col = row.copy()
        offsets = np.array([-n1-1, -n1, -n1+1, -1, 0, 1, n1-1, n1, n1+1])
        col += np.repeat(offsets, dlen)

        tstcoo = scipy.sparse.coo_matrix((tstdata, (row, col)), shape=2*[n0*n1])

        # fp(tstcoo - B.D ,'e')
        npt.assert_equal(tstcoo.todense(), refcoo.todense())