コード例 #1
0
ファイル: solver.py プロジェクト: aseyboldt/sunode
 def init(self,
          t0: float,
          state: Optional[np.ndarray] = None,
          recreate_rhs: bool = False) -> None:
     if state is not None:
         self.state[:] = state
     if recreate_rhs:
         self._rhs = self.problem.make_sundials_rhs()
     check(
         lib.CVodeInit(self.c_ptr, self._rhs.cffi, t0,
                       self._state_buffer.c_ptr))
コード例 #2
0
ファイル: solver.py プロジェクト: c3-robertyoung/sunode
    def __init__(self,
                 problem: Problem,
                 *,
                 compute_sens: bool = False,
                 abstol: float = 1e-10,
                 reltol: float = 1e-10,
                 sens_mode: Optional[str] = None,
                 scaling_factors: Optional[np.ndarray] = None,
                 constraints: Optional[np.ndarray] = None,
                 solver='BDF'):
        self._problem = problem
        self._user_data = problem.make_user_data()

        n_states = self._problem.n_states
        n_params = self._problem.n_params

        self._state_buffer = sunode.empty_vector(n_states)
        self._state_buffer.data[:] = 0
        self._jac = check(lib.SUNDenseMatrix(n_states, n_states))
        self._constraints = constraints

        if solver == 'BDF':
            solver_kind = lib.CV_BDF
        elif solver == 'ADAMS':
            solver_kind = lib.CV_ADAMS
        else:
            assert False
        self._ode = check(lib.CVodeCreate(solver_kind))
        rhs = problem.make_sundials_rhs()
        check(lib.CVodeInit(self._ode, rhs.cffi, 0., self._state_buffer.c_ptr))

        self._set_tolerances(abstol, reltol)
        if self._constraints is not None:
            assert constraints.shape == (n_states, )
            self._constraints_vec = sunode.from_numpy(constraints)
            check(
                lib.CVodeSetConstraints(self._ode,
                                        self._constraints_vec.c_ptr))

        self._make_linsol()

        user_data_p = ffi.cast(
            'void *', ffi.addressof(ffi.from_buffer(self._user_data.data)))
        check(lib.CVodeSetUserData(self._ode, user_data_p))

        self._compute_sens = compute_sens
        if compute_sens:
            sens_rhs = self._problem.make_sundials_sensitivity_rhs()
            self._init_sens(sens_rhs, sens_mode)
コード例 #3
0
ファイル: solver.py プロジェクト: aseyboldt/sunode
        if solver == 'BDF':
            self._solver_type = lib.CV_BDF
        elif solver == 'ADAMS':
            self._solver_type = lib.CV_ADAMS
        else:
            raise ValueError(f'Unknown solver {solver}.')

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

        self._ode = check(lib.CVodeCreate(self._solver_type))
        check(lib.CVodeInit(self._ode, rhs.cffi, 0., self._state_buffer.c_ptr))

        self._set_tolerances(abstol, reltol)
        if self._constraints is not None:
            self._constraints = np.broadcast_to(constraints,
                                                (n_states, )).copy()
            self._constraints_vec = sunode.from_numpy(self._constraints)
            check(
                lib.CVodeSetConstraints(self._ode,
                                        self._constraints_vec.c_ptr))

        self._make_linsol()

        user_data_p = ffi.cast(
            'void *', ffi.addressof(ffi.from_buffer(self._user_data.data)))
        check(lib.CVodeSetUserData(self._ode, user_data_p))