Skip to content

Commit 03b60d0

Browse files
Add print_stats method to ode and implement for CVODE integrator
`CVODE` solver has function `CVodePrintAllStats` that prints statistics about integrator such as number of right-hand-side function evaluations and number of nonlinear solves.
1 parent 57466a9 commit 03b60d0

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

scikits/odes/ode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ def get_info(self):
455455
else:
456456
return {}
457457

458+
def print_stats(self):
459+
if hasattr(self._integrator, "print_stats"):
460+
self._integrator.print_stats()
461+
else:
462+
print(f"Method `print_stats` is not implemented for integrator {self._integrator}")
463+
458464
#------------------------------------------------------------------------------
459465
# ODE integrators
460466
#------------------------------------------------------------------------------

scikits/odes/sundials/c_cvode.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ cdef extern from "cvode/cvode.h":
120120
int CVodeGetNumNonlinSolvConvFails(void *cvode_mem, long int *nncfails)
121121
int CVodeGetNonlinSolvStats(void *cvode_mem, long int *nniters,
122122
long int *nncfails)
123+
int CVodePrintAllStats(void* cvode_mem, FILE* outfile, SUNOutputFormat fmt)
123124
char *CVodeGetReturnFlagName(long int flag)
124125
void CVodeFree(void **cvode_mem)
125126

scikits/odes/sundials/c_sundials.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ cdef extern from "sundials/sundials_types.h":
44
ctypedef float sunrealtype
55
ctypedef unsigned int sunbooleantype
66
ctypedef long sunindextype
7+
8+
cdef enum SUNOutputFormat:
9+
SUN_OUTPUTFORMAT_TABLE,
10+
SUN_OUTPUTFORMAT_CSV
711

812
cdef extern from "sundials/sundials_context.h":
913
struct _SUNContext:

scikits/odes/sundials/cvode.pyx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ include "sundials_config.pxi"
1010
import numpy as np
1111
cimport numpy as np
1212

13+
from libc cimport stdio
14+
1315
from . import (
1416
CVODESolveFailed, CVODESolveFoundRoot, CVODESolveReachedTSTOP,
1517
_get_num_args,
1618
)
1719

1820
from .c_sundials cimport (
19-
sunrealtype, N_Vector, SUNContext_Create, SUNContext_Free,
21+
sunrealtype, N_Vector, SUNContext_Create, SUNContext_Free, SUN_OUTPUTFORMAT_TABLE
2022
)
2123
from .c_nvector_serial cimport *
2224
from .c_sunmatrix cimport *
@@ -1147,7 +1149,7 @@ cdef class CVODE:
11471149
if np.isscalar(opts_atol):
11481150
flag = CVodeSStolerances(cv_mem, <sunrealtype> opts_rtol,
11491151
<sunrealtype> opts_atol)
1150-
else:
1152+
else:
11511153
np_atol = np.asarray(opts_atol, dtype=DTYPE)
11521154
if len(np_atol) != self.N:
11531155
raise ValueError("Array length inconsistency: 'atol' "
@@ -2046,6 +2048,9 @@ cdef class CVODE:
20462048
'NumRhsEvalsJtimesFD': nfevalsLS})
20472049

20482050
return info
2051+
2052+
def print_stats(self):
2053+
CVodePrintAllStats(self._cv_mem, stdio.stdout, SUN_OUTPUTFORMAT_TABLE)
20492054

20502055
def __dealloc__(self):
20512056
if self._cv_mem is not NULL: CVodeFree(&self._cv_mem)

0 commit comments

Comments
 (0)