1
1
# cython: embedsignature=True
2
2
from cpython.exc cimport PyErr_CheckSignals
3
3
from collections import namedtuple
4
- from enum import IntEnum
4
+ try :
5
+ from enum import IntEnum
6
+ except ImportError :
7
+ from enum34 import IntEnum
5
8
import inspect
6
9
from warnings import warn
7
10
@@ -127,7 +130,8 @@ cdef class IDA_RhsFunction:
127
130
recoverable failure, negative for unrecoverable failure (as per IDA
128
131
documentation).
129
132
"""
130
- cpdef int evaluate(self , DTYPE_t t,
133
+ cpdef int evaluate(self ,
134
+ DTYPE_t t,
131
135
np.ndarray[DTYPE_t, ndim= 1 ] y,
132
136
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
133
137
np.ndarray[DTYPE_t, ndim= 1 ] result,
@@ -139,16 +143,14 @@ cdef class IDA_WrapRhsFunction(IDA_RhsFunction):
139
143
"""
140
144
set some residual equations as a ResFunction executable class
141
145
"""
142
- self .with_userdata = 0
143
- nrarg = _get_num_args(resfn)
144
- if nrarg > 5 :
145
- # hopefully a class method
146
- self .with_userdata = 1
147
- elif nrarg == 5 and inspect.isfunction(resfn):
146
+ if _get_num_args(resfn) == 5 :
148
147
self .with_userdata = 1
148
+ else :
149
+ self .with_userdata = 0
149
150
self ._resfn = resfn
150
151
151
- cpdef int evaluate(self , DTYPE_t t,
152
+ cpdef int evaluate(self ,
153
+ DTYPE_t t,
152
154
np.ndarray[DTYPE_t, ndim= 1 ] y,
153
155
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
154
156
np.ndarray[DTYPE_t, ndim= 1 ] result,
@@ -197,7 +199,8 @@ cdef class IDA_RootFunction:
197
199
Note that evaluate must return a integer, 0 for success, non-zero for error
198
200
(as per IDA documentation).
199
201
"""
200
- cpdef int evaluate(self , DTYPE_t t,
202
+ cpdef int evaluate(self ,
203
+ DTYPE_t t,
201
204
np.ndarray[DTYPE_t, ndim= 1 ] y,
202
205
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
203
206
np.ndarray[DTYPE_t, ndim= 1 ] g,
@@ -209,16 +212,14 @@ cdef class IDA_WrapRootFunction(IDA_RootFunction):
209
212
"""
210
213
set root-ing condition(equations) as a RootFunction executable class
211
214
"""
212
- self .with_userdata = 0
213
- nrarg = _get_num_args(rootfn)
214
- if nrarg > 5 :
215
- # hopefully a class method, self gives 5 arg!
216
- self .with_userdata = 1
217
- elif nrarg == 5 and inspect.isfunction(rootfn):
215
+ if _get_num_args(rootfn) == 5 :
218
216
self .with_userdata = 1
217
+ else :
218
+ self .with_userdata = 0
219
219
self ._rootfn = rootfn
220
220
221
- cpdef int evaluate(self , DTYPE_t t,
221
+ cpdef int evaluate(self ,
222
+ DTYPE_t t,
222
223
np.ndarray[DTYPE_t, ndim= 1 ] y,
223
224
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
224
225
np.ndarray[DTYPE_t, ndim= 1 ] g,
@@ -268,12 +269,14 @@ cdef class IDA_JacRhsFunction:
268
269
recoverable failure, negative for unrecoverable failure (as per IDA
269
270
documentation).
270
271
"""
271
- cpdef int evaluate(self , DTYPE_t t,
272
+ cpdef int evaluate(self ,
273
+ DTYPE_t t,
272
274
np.ndarray[DTYPE_t, ndim= 1 ] y,
273
275
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
274
276
np.ndarray[DTYPE_t, ndim= 1 ] residual,
275
277
DTYPE_t cj,
276
- np.ndarray J) except ? - 1 :
278
+ np.ndarray J,
279
+ object userdata = None ) except ? - 1 :
277
280
"""
278
281
Returns the Jacobi matrix of the residual function, as
279
282
d(res)/d y + cj d(res)/d ydot
@@ -291,24 +294,29 @@ cdef class IDA_WrapJacRhsFunction(IDA_JacRhsFunction):
291
294
"""
292
295
Set some jacobian equations as a JacResFunction executable class.
293
296
"""
297
+ if _get_num_args(jacfn) == 7 :
298
+ self .with_userdata = 1
299
+ else :
300
+ self .with_userdata = 0
294
301
self ._jacfn = jacfn
295
302
296
- cpdef int evaluate(self , DTYPE_t t,
303
+ cpdef int evaluate(self ,
304
+ DTYPE_t t,
297
305
np.ndarray[DTYPE_t, ndim= 1 ] y,
298
306
np.ndarray[DTYPE_t, ndim= 1 ] ydot,
299
307
np.ndarray[DTYPE_t, ndim= 1 ] residual,
300
308
DTYPE_t cj,
301
- np.ndarray J) except ? - 1 :
309
+ np.ndarray J,
310
+ object userdata = None ) except ? - 1 :
302
311
"""
303
312
Returns the Jacobi matrix (for dense the full matrix, for band only
304
313
bands. Result has to be stored in the variable J, which is preallocated
305
314
to the corresponding size.
306
315
"""
307
- # # if self.with_userdata == 1:
308
- # # self._jacfn(t, y, ydot, cj, J, userdata)
309
- # # else:
310
- # # self._jacfn(t, y, ydot, cj, J)
311
- user_flag = self ._jacfn(t, y, ydot, residual, cj, J)
316
+ if self .with_userdata == 1 :
317
+ user_flag = self ._jacfn(t, y, ydot, residual, cj, J, userdata)
318
+ else :
319
+ user_flag = self ._jacfn(t, y, ydot, residual, cj, J)
312
320
if user_flag is None :
313
321
user_flag = 0
314
322
return user_flag
@@ -336,7 +344,7 @@ cdef int _jacdense(realtype tt, realtype cj,
336
344
nv_s2ndarray(yy, yy_tmp)
337
345
nv_s2ndarray(yp, yp_tmp)
338
346
nv_s2ndarray(rr, residual_tmp)
339
- user_flag = aux_data.jac.evaluate(tt, yy_tmp, yp_tmp, residual_tmp, cj, jac_tmp)
347
+ user_flag = aux_data.jac.evaluate(tt, yy_tmp, yp_tmp, residual_tmp, cj, jac_tmp, aux_data.user_data )
340
348
341
349
if parallel_implementation:
342
350
raise NotImplemented
@@ -355,7 +363,8 @@ cdef class IDA_PrecSetupFunction:
355
363
recoverable failure, negative for unrecoverable failure (as per CVODE
356
364
documentation).
357
365
"""
358
- cpdef int evaluate(self , DTYPE_t t,
366
+ cpdef int evaluate(self ,
367
+ DTYPE_t t,
359
368
np.ndarray[DTYPE_t, ndim= 1 ] y,
360
369
np.ndarray[DTYPE_t, ndim= 1 ] yp,
361
370
np.ndarray[DTYPE_t, ndim= 1 ] rr,
@@ -377,16 +386,14 @@ cdef class IDA_WrapPrecSetupFunction(IDA_PrecSetupFunction):
377
386
set a precondititioning setup method as a IDA_PrecSetupFunction
378
387
executable class
379
388
"""
380
- self .with_userdata = 0
381
- nrarg = _get_num_args(prec_setupfn)
382
- if nrarg > 5 :
383
- # hopefully a class method, self gives 6 arg!
384
- self .with_userdata = 1
385
- elif nrarg == 5 and inspect.isfunction(prec_setupfn):
389
+ if _get_num_args(prec_setupfn) == 6 :
386
390
self .with_userdata = 1
391
+ else :
392
+ self .with_userdata = 0
387
393
self ._prec_setupfn = prec_setupfn
388
394
389
- cpdef int evaluate(self , DTYPE_t t,
395
+ cpdef int evaluate(self ,
396
+ DTYPE_t t,
390
397
np.ndarray[DTYPE_t, ndim= 1 ] y,
391
398
np.ndarray[DTYPE_t, ndim= 1 ] yp,
392
399
np.ndarray[DTYPE_t, ndim= 1 ] rr,
@@ -433,7 +440,8 @@ cdef class IDA_PrecSolveFunction:
433
440
recoverable failure, negative for unrecoverable failure (as per CVODE
434
441
documentation).
435
442
"""
436
- cpdef int evaluate(self , DTYPE_t t,
443
+ cpdef int evaluate(self ,
444
+ DTYPE_t t,
437
445
np.ndarray[DTYPE_t, ndim= 1 ] y,
438
446
np.ndarray[DTYPE_t, ndim= 1 ] yp,
439
447
np.ndarray[DTYPE_t, ndim= 1 ] r,
@@ -460,16 +468,14 @@ cdef class IDA_WrapPrecSolveFunction(IDA_PrecSolveFunction):
460
468
set a precondititioning solve method as a IDA_PrecSolveFunction
461
469
executable class
462
470
"""
463
- self .with_userdata = 0
464
- nrarg = _get_num_args(prec_solvefn)
465
- if nrarg > 9 :
466
- # hopefully a class method, self gives 10 arg!
467
- self .with_userdata = 1
468
- elif nrarg == 9 and inspect.isfunction(prec_solvefn):
471
+ if _get_num_args(prec_solvefn) == 9 :
469
472
self .with_userdata = 1
473
+ else :
474
+ self .with_userdata = 0
470
475
self ._prec_solvefn = prec_solvefn
471
476
472
- cpdef int evaluate(self , DTYPE_t t,
477
+ cpdef int evaluate(self ,
478
+ DTYPE_t t,
473
479
np.ndarray[DTYPE_t, ndim= 1 ] y,
474
480
np.ndarray[DTYPE_t, ndim= 1 ] yp,
475
481
np.ndarray[DTYPE_t, ndim= 1 ] r,
@@ -567,13 +573,10 @@ cdef class IDA_WrapJacTimesVecFunction(IDA_JacTimesVecFunction):
567
573
set a jacobian-times-vector method as a IDA_JacTimesVecFunction
568
574
executable class
569
575
"""
570
- self .with_userdata = 0
571
- nrarg = _get_num_args(jac_times_vecfn)
572
- if nrarg > 8 :
573
- # hopefully a class method, self gives 9 arg!
574
- self .with_userdata = 1
575
- elif nrarg == 8 and inspect.isfunction(jac_times_vecfn):
576
+ if _get_num_args(jac_times_vecfn) == 8 :
576
577
self .with_userdata = 1
578
+ else :
579
+ self .with_userdata = 0
577
580
self ._jac_times_vecfn = jac_times_vecfn
578
581
579
582
cpdef int evaluate(self ,
@@ -655,13 +658,10 @@ cdef class IDA_WrapJacTimesSetupFunction(IDA_JacTimesSetupFunction):
655
658
set a jacobian-times-vector method setup as a IDA_JacTimesSetupFunction
656
659
executable class
657
660
"""
658
- self .with_userdata = 0
659
- nrarg = _get_num_args(jac_times_setupfn)
660
- if nrarg > 6 :
661
- # hopefully a class method, self gives 7 arg!
662
- self .with_userdata = 1
663
- elif nrarg == 6 and inspect.isfunction(jac_times_setupfn):
661
+ if _get_num_args(jac_times_setupfn) == 6 :
664
662
self .with_userdata = 1
663
+ else :
664
+ self .with_userdata = 0
665
665
self ._jac_times_setupfn = jac_times_setupfn
666
666
667
667
cpdef int evaluate(self ,
@@ -734,10 +734,10 @@ cdef class IDA_WrapErrHandler(IDA_ErrHandler):
734
734
"""
735
735
set some (c/p)ython function as the error handler
736
736
"""
737
- nrarg = _get_num_args(err_handler)
738
- self .with_userdata = (nrarg > 5 ) or (
739
- nrarg == 5 and inspect.isfunction(err_handler)
740
- )
737
+ if _get_num_args(err_handler) == 5 :
738
+ self .with_userdata = 1
739
+ else :
740
+ self .with_userdata = 0
741
741
self ._err_handler = err_handler
742
742
743
743
cpdef evaluate(self ,
0 commit comments