Skip to content

Commit

Permalink
More fixes for Cython3
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Jan 12, 2024
1 parent 5b8324b commit e1891c8
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 87 deletions.
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def fortran_compiler_flags(self):
logging.debug('Platform {}'.format(self.platform))

if args[0].sundials_home:
self.incdirs = os.path.join(self.sundialsdir,'include')
self.libdirs = os.path.join(self.sundialsdir,'lib')
self.incdirs = os.path.join(self.sundialsdir, 'include')
self.libdirs = os.path.join(self.sundialsdir, 'lib')
elif 'win' in self.platform:
self.incdirs = ''
self.libdirs = ''
Expand Down Expand Up @@ -420,7 +420,7 @@ def check_SUNDIALS(self):
break
if os.path.exists(os.path.join(self.libdirs,'sundials_nvecserial.lib')) and not os.path.exists(os.path.join(self.libdirs,'libsundials_nvecserial.a')):
sundials_with_msvc = True
except Exception as e:
except Exception:
if os.path.exists(os.path.join(os.path.join(self.incdirs,'arkode'), 'arkode.h')): #This was added in 2.6
sundials_version = (2,6,0)
logging.debug('SUNDIALS 2.6 found.')
Expand Down
4 changes: 2 additions & 2 deletions src/algebraic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import numpy as N
cimport numpy as N
from timeit import default_timer as timer

from exception import Algebraic_Exception, AssimuloException
from problem import Algebraic_Problem
from assimulo.exception import Algebraic_Exception, AssimuloException
from assimulo.problem import Algebraic_Problem

include "constants.pxi" #Includes the constants (textual include)

Expand Down
2 changes: 1 addition & 1 deletion src/explicit_ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import numpy as N
cimport numpy as N
from timeit import default_timer as timer

from assimulo.ode cimport ODE
from assimulo.ode cimport ODE
from assimulo.explicit_ode cimport Explicit_ODE, f_event_locator

from assimulo.problem import Explicit_Problem, Delay_Explicit_Problem, SingPerturbed_Problem, cExplicit_Problem
Expand Down
4 changes: 1 addition & 3 deletions src/implicit_ode.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


from ode cimport ODE
import numpy as N
cimport numpy as N

from assimulo.ode cimport ODE

cdef class Implicit_ODE(ODE):
cpdef _simulate(self, double t0, double tfinal,N.ndarray output_list,int COMPLETE_STEP, int INTERPOLATE_OUTPUT,int TIME_EVENT)

12 changes: 5 additions & 7 deletions src/implicit_ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@

# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION

from ode cimport ODE
from problem import Implicit_Problem, cImplicit_Problem, Overdetermined_Problem
from problem import cExplicit_Problem

import itertools
import sys
import warnings
import numpy as N
cimport numpy as N

from exception import TerminateSimulation, TimeLimitExceeded
from timeit import default_timer as timer
import warnings

from assimulo.ode cimport ODE
from assimulo.problem import Implicit_Problem, cExplicit_Problem, cImplicit_Problem, Overdetermined_Problem
from assimulo.exception import TerminateSimulation, TimeLimitExceeded

realtype = float

Expand Down
16 changes: 8 additions & 8 deletions src/lib/sundials_callbacks.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import cython
# Module functions
#=================

cdef N_Vector N_VNewEmpty_Euclidean(long int n):
cdef N_Vector N_VNewEmpty_Euclidean(long int n) noexcept:
IF SUNDIALS_VERSION >= (6,0,0):
cdef SUNDIALS.SUNContext ctx = NULL
cdef void * comm = NULL
Expand All @@ -32,7 +32,7 @@ cdef N_Vector N_VNewEmpty_Euclidean(long int n):
v.ops.nvwrmsnorm = v.ops.nvwl2norm #Overwrite the WRMS norm to the 2-Norm
return v

cdef inline N_Vector arr2nv(x):
cdef inline N_Vector arr2nv(x) noexcept:
x=N.array(x)
cdef long int n = len(x)
cdef N.ndarray[realtype, ndim=1,mode='c'] ndx=x
Expand All @@ -47,7 +47,7 @@ cdef inline N_Vector arr2nv(x):
memcpy((<N_VectorContent_Serial>v.content).data, data_ptr, n*sizeof(realtype))
return v

cdef inline N_Vector arr2nv_euclidean(x):
cdef inline N_Vector arr2nv_euclidean(x) noexcept:
x=N.array(x)
cdef long int n = len(x)
cdef N.ndarray[realtype, ndim=1,mode='c'] ndx=x
Expand All @@ -56,33 +56,33 @@ cdef inline N_Vector arr2nv_euclidean(x):
memcpy((<N_VectorContent_Serial>v.content).data, data_ptr, n*sizeof(realtype))
return v

cdef inline void arr2nv_inplace(x, N_Vector out):
cdef inline void arr2nv_inplace(x, N_Vector out) noexcept:
x=N.array(x)
cdef long int n = len(x)
cdef N.ndarray[realtype, ndim=1,mode='c'] ndx=x
cdef void* data_ptr=PyArray_DATA(ndx)
memcpy((<N_VectorContent_Serial>out.content).data, data_ptr, n*sizeof(realtype))

cdef inline N.ndarray nv2arr(N_Vector v):
cdef inline N.ndarray nv2arr(N_Vector v) noexcept:
cdef long int n = (<N_VectorContent_Serial>v.content).length
cdef realtype* v_data = (<N_VectorContent_Serial>v.content).data
cdef N.ndarray[realtype, ndim=1, mode='c'] x=N.empty(n)
memcpy(PyArray_DATA(x), v_data, n*sizeof(realtype))
return x

cdef inline void nv2arr_inplace(N_Vector v, N.ndarray o):
cdef inline void nv2arr_inplace(N_Vector v, N.ndarray o) noexcept:
cdef long int n = (<N_VectorContent_Serial>v.content).length
cdef realtype* v_data = (<N_VectorContent_Serial>v.content).data
memcpy(PyArray_DATA(o), v_data, n*sizeof(realtype))

cdef inline void nv2mat_inplace(int Ns, N_Vector *v, N.ndarray o):
cdef inline void nv2mat_inplace(int Ns, N_Vector *v, N.ndarray o) noexcept:
cdef long int i,j, Nf
for i in range(Ns):
Nf = (<N_VectorContent_Serial>v[i].content).length
for j in range(Nf):
o[j,i] = (<N_VectorContent_Serial>v[i].content).data[j]

cdef inline realtype2arr(realtype *data, int n):
cdef inline realtype2arr(realtype *data, int n) noexcept:
"""Create new numpy array from realtype*"""
cdef N.ndarray[realtype, ndim=1, mode='c'] x=N.empty(n)
memcpy(PyArray_DATA(x), data, n*sizeof(realtype))
Expand Down
44 changes: 22 additions & 22 deletions src/lib/sundials_callbacks_ida_cvode.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import cython
import traceback
from assimulo.exception import AssimuloRecoverableError

cdef int cv_rhs(realtype t, N_Vector yv, N_Vector yvdot, void* problem_data):
cdef int cv_rhs(realtype t, N_Vector yv, N_Vector yvdot, void* problem_data) noexcept:
"""
This method is used to connect the Assimulo.Problem.f to the Sundials
right-hand-side function.
Expand Down Expand Up @@ -57,7 +57,7 @@ cdef int cv_rhs(realtype t, N_Vector yv, N_Vector yvdot, void* problem_data):

cdef int cv_sens_rhs_all(int Ns, realtype t, N_Vector yv, N_Vector yvdot,
N_Vector *yvS, N_Vector *yvSdot, void *problem_data,
N_Vector tmp1, N_Vector tmp2):
N_Vector tmp1, N_Vector tmp2) noexcept:

cdef ProblemData pData = <ProblemData>problem_data
cdef N.ndarray y = pData.work_y
Expand Down Expand Up @@ -92,7 +92,7 @@ IF SUNDIALS_VERSION >= (3,0,0):
@cython.boundscheck(False)
@cython.wraparound(False)
cdef int cv_jac_sparse(realtype t, N_Vector yv, N_Vector fy, SUNMatrix Jac,
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Sparse Jacobian function.
Expand Down Expand Up @@ -146,7 +146,7 @@ ELSE:
@cython.boundscheck(False)
@cython.wraparound(False)
cdef int cv_jac_sparse(realtype t, N_Vector yv, N_Vector fy, SlsMat Jacobian,
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Sparse Jacobian function.
Expand Down Expand Up @@ -213,7 +213,7 @@ ELSE:

IF SUNDIALS_VERSION >= (3,0,0):
cdef int cv_jac(realtype t, N_Vector yv, N_Vector fy, SUNMatrix Jac,
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Jacobian function.
Expand Down Expand Up @@ -264,7 +264,7 @@ IF SUNDIALS_VERSION >= (3,0,0):
return CVDLS_SUCCESS
ELSE:
cdef int cv_jac(long int Neq, realtype t, N_Vector yv, N_Vector fy, DlsMat Jacobian,
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Jacobian function.
Expand Down Expand Up @@ -315,7 +315,7 @@ ELSE:


cdef int cv_jacv(N_Vector vv, N_Vector Jv, realtype t, N_Vector yv, N_Vector fyv,
void *problem_data, N_Vector tmp):
void *problem_data, N_Vector tmp) noexcept:
"""
This method is used to connect the Assimulo.Problem.jacv to the Sundials
Jacobian times vector function.
Expand Down Expand Up @@ -365,7 +365,7 @@ cdef int cv_jacv(N_Vector vv, N_Vector Jv, realtype t, N_Vector yv, N_Vector fyv
IF SUNDIALS_VERSION >= (3,0,0):
cdef int cv_prec_setup(realtype t, N_Vector yy, N_Vector fyy,
bint jok, bint *jcurPtr,
realtype gamma, void *problem_data):
realtype gamma, void *problem_data) noexcept:
"""
For information see CVODES documentation 4.6.9
"""
Expand All @@ -387,7 +387,7 @@ IF SUNDIALS_VERSION >= (3,0,0):
cdef int cv_prec_solve(realtype t, N_Vector yy, N_Vector fyy,
N_Vector rr, N_Vector z,
realtype gamma, realtype delta,
int lr, void *problem_data):
int lr, void *problem_data) noexcept:
"""
For information see CVODES documentation 4.6.8
"""
Expand All @@ -412,7 +412,7 @@ ELSE:
bint jok, bint *jcurPtr,
realtype gamma, void *problem_data,
N_Vector tmp1, N_Vector tmp2,
N_Vector tmp3):
N_Vector tmp3) noexcept:
"""
For information see CVODES documentation 4.6.9
"""
Expand All @@ -434,7 +434,7 @@ ELSE:
cdef int cv_prec_solve(realtype t, N_Vector yy, N_Vector fyy,
N_Vector rr, N_Vector z,
realtype gamma, realtype delta,
int lr, void *problem_data, N_Vector tmp):
int lr, void *problem_data, N_Vector tmp) noexcept:
"""
For information see CVODES documentation 4.6.8
"""
Expand All @@ -461,9 +461,9 @@ cdef int cv_prec(realtype t, N Vector yv, N Vector fyv,
void *problem_data, N Vector tmp):
cdef ProblemData pData = <ProblemData>problem_data
cdef N.ndarray y = nv2arr(yv)
cdef N.ndarray y = nv2arr(yv)
cdef N.ndarray fy = nv2arr(fyv)
cdef N.ndarray r = nv2arr(rv)
cdef N.ndarray r = nv2arr(rv)
cdef int i
cdef realtype* zptr=(<N_VectorContent_Serial>z.content).data
Expand All @@ -480,7 +480,7 @@ cdef int cv_prec(realtype t, N Vector yv, N Vector fyv,
return SPGMR_PSOLVE_FAIL_UNREC
"""

cdef int cv_root(realtype t, N_Vector yv, realtype *gout, void* problem_data):
cdef int cv_root(realtype t, N_Vector yv, realtype *gout, void* problem_data) noexcept:
"""
This method is used to connect the Assimulo.Problem.state_events to the Sundials
Root-finding function.
Expand All @@ -503,9 +503,9 @@ cdef int cv_root(realtype t, N_Vector yv, realtype *gout, void* problem_data):

return CV_SUCCESS
except Exception:
return CV_RTFUNC_FAIL # Unrecoverable Error
return CV_RTFUNC_FAIL # Unrecoverable Error

cdef int ida_res(realtype t, N_Vector yv, N_Vector yvdot, N_Vector residual, void* problem_data):
cdef int ida_res(realtype t, N_Vector yv, N_Vector yvdot, N_Vector residual, void* problem_data) noexcept:
"""
This method is used to connect the Assimulo.Problem.f to the Sundials
residual function.
Expand Down Expand Up @@ -559,7 +559,7 @@ cdef int ida_res(realtype t, N_Vector yv, N_Vector yvdot, N_Vector residual, voi

IF SUNDIALS_VERSION >= (3,0,0):
cdef int ida_jac(realtype t, realtype c, N_Vector yv, N_Vector yvdot, N_Vector residual, SUNMatrix Jac,
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void *problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Jacobian function.
Expand Down Expand Up @@ -612,7 +612,7 @@ IF SUNDIALS_VERSION >= (3,0,0):
return IDADLS_JACFUNC_UNRECVR
ELSE:
cdef int ida_jac(long int Neq, realtype t, realtype c, N_Vector yv, N_Vector yvdot, N_Vector residual, DlsMat Jacobian,
void* problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3):
void* problem_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3) noexcept:
"""
This method is used to connect the Assimulo.Problem.jac to the Sundials
Jacobian function.
Expand Down Expand Up @@ -664,7 +664,7 @@ ELSE:
return IDADLS_JACFUNC_UNRECVR


cdef int ida_root(realtype t, N_Vector yv, N_Vector yvdot, realtype *gout, void* problem_data):
cdef int ida_root(realtype t, N_Vector yv, N_Vector yvdot, realtype *gout, void* problem_data) noexcept:
"""
This method is used to connect the Assimulo.Problem.state_events to the Sundials
root function.
Expand Down Expand Up @@ -693,7 +693,7 @@ cdef int ida_root(realtype t, N_Vector yv, N_Vector yvdot, realtype *gout, void
return IDA_RTFUNC_FAIL # Unrecoverable Error

cdef int ida_jacv(realtype t, N_Vector yy, N_Vector yp, N_Vector rr, N_Vector vv, N_Vector Jv, realtype cj,
void *problem_data, N_Vector tmp1, N_Vector tmp2):
void *problem_data, N_Vector tmp1, N_Vector tmp2) noexcept:
"""
This method is used to connect the Assimulo.Problem.jacv to the Sundials
Jacobian times vector function.
Expand Down Expand Up @@ -744,7 +744,7 @@ cdef int ida_jacv(realtype t, N_Vector yy, N_Vector yp, N_Vector rr, N_Vector vv
# Error handling callback functions
# =================================

cdef void cv_err(int error_code, const char *module, const char *function, char *msg, void *problem_data):
cdef void cv_err(int error_code, const char *module, const char *function, char *msg, void *problem_data) noexcept:
"""
This method overrides the default handling of error messages.
"""
Expand All @@ -757,7 +757,7 @@ cdef void cv_err(int error_code, const char *module, const char *function, char
if error_code < 0: #Error
print('[CVode Error]', msg)

cdef void ida_err(int error_code, const char *module, const char *function, char *msg, void *problem_data):
cdef void ida_err(int error_code, const char *module, const char *function, char *msg, void *problem_data) noexcept:
"""
This method overrides the default handling of error messages.
"""
Expand Down
Loading

0 comments on commit e1891c8

Please sign in to comment.