Exemplo n.º 1
0
def test_sparse_view():
    order = []

    def make_fin(name):
        def fin():
            order.append(name)

        return fin

    mat = matrix.empty_matrix((10, 8),
                              "sparse",
                              sparsity=np.random.randn(10, 8) > 0,
                              format="csr")
    weakref.finalize(mat, make_fin("vector"))
    weakref.finalize(mat.c_ptr, make_fin("c_ptr"))

    view1 = mat.data
    weakref.finalize(view1, make_fin("view1"))

    view2 = view1[::2]
    weakref.finalize(view2, make_fin("view2"))

    view3 = mat.data
    weakref.finalize(view3, make_fin("view3"))

    del mat
    assert order == []
    del view1
    assert order == []
    del view2
    assert order == ["view2", "view1"]
    del view3
    assert order == ["view2", "view1", "view3", "vector", "c_ptr"]
Exemplo n.º 2
0
def test_matrix_view():
    order = []

    def make_fin(name):
        def fin():
            order.append(name)

        return fin

    mat = matrix.empty_matrix((10, 8))
    weakref.finalize(mat, make_fin("vector"))
    weakref.finalize(mat.c_ptr, make_fin("c_ptr"))

    view1 = mat.data
    weakref.finalize(view1, make_fin("view1"))

    view2 = view1[0]
    weakref.finalize(view2, make_fin("view2"))

    view3 = mat.data
    weakref.finalize(view3, make_fin("view3"))

    del mat
    assert order == []
    del view1
    assert order == ["view1"]
    del view2
    assert order == ["view1", "view2"]
    del view3
    assert order == ["view1", "view2", "view3", "vector", "c_ptr"]
Exemplo n.º 3
0
def test_sparse_realloc():
    mat = matrix.empty_matrix(
        (10, 8), "sparse", sparsity=np.random.randn(10, 8) > 0, format="csr"
    )
    data = mat.data
    with raises(RuntimeError):
        mat.realloc()

    del data
    mat.realloc()
    dat = mat.scipy

    with raises(RuntimeError):
        mat.realloc()
    assert mat.shape == (10, 8)
Exemplo n.º 4
0
    def __init__(self,
                 problem: Problem,
                 *,
                 solver: str = 'BDF',
                 jac_kind: str = "dense"):
        super().__init__()

        self.mark_changed()

        self.problem = problem
        self.user_data = problem.make_user_data()

        self._state_buffer = sunode.empty_vector(self.n_states)
        self._state_buffer.data[:] = 0.

        self.borrow(self._state_buffer)

        if jac_kind == 'dense':
            self._jac = matrix.empty_matrix((self.n_states, self.n_states))
        elif jac_kind == 'sparse':
            self._jac = problem.make_rhs_sparse_jac_template()
        else:
            raise ValueError(f'Unknown jac_kind {jac_kind}.')

        self.borrow(self._jac)

        if solver == 'BDF':
            self.c_ptr = check_ptr(lib.CVodeCreate(lib.CV_BDF))
        elif solver == 'ADAMS':
            self.c_ptr = check_ptr(lib.CVodeCreate(lib.CV_ADAMS))
        else:
            raise ValueError(f'Unknown solver {solver}.')

        self._rhs = self.problem.make_sundials_rhs()

        def finalize(c_ptr: CPointer,
                     release_borrowed: Callable[[], None]) -> None:
            if c_ptr == ffi.NULL:
                logger.warn("Trying to free Solver, but it is NULL.")
            else:
                logger.debug("Freeing Solver")
                lib.CVodeFree(c_ptr)
            release_borrowed()

        weakref.finalize(self, finalize, self.c_ptr,
                         self.release_borrowed_func())