Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add print_stats method to ode and implement for CVODE integrator #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ cdef extern from "cvode/cvode.h":
int CVodeGetNumNonlinSolvConvFails(void *cvode_mem, long int *nncfails)
int CVodeGetNonlinSolvStats(void *cvode_mem, long int *nniters,
long int *nncfails)
int CVodePrintAllStats(void* cvode_mem, FILE* outfile, SUNOutputFormat fmt)
char *CVodeGetReturnFlagName(long int flag)
void CVodeFree(void **cvode_mem)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ cdef extern from "sundials/sundials_types.h":
ctypedef float sunrealtype
ctypedef unsigned int sunbooleantype
ctypedef long sunindextype

cdef enum SUNOutputFormat:
SUN_OUTPUTFORMAT_TABLE,
SUN_OUTPUTFORMAT_CSV

cdef extern from "sundials/sundials_context.h":
struct _SUNContext:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ include "sundials_config.pxi"
import numpy as np
cimport numpy as np

from libc cimport stdio

from . import (
CVODESolveFailed, CVODESolveFoundRoot, CVODESolveReachedTSTOP,
_get_num_args,
)

from .c_sundials cimport (
sunrealtype, N_Vector, SUNContext_Create, SUNContext_Free,
sunrealtype, N_Vector, SUNContext_Create, SUNContext_Free, SUN_OUTPUTFORMAT_TABLE
)
from .c_nvector_serial cimport *
from .c_sunmatrix cimport *
Expand Down Expand Up @@ -2048,6 +2050,9 @@ cdef class CVODE:
'NumRhsEvalsJtimesFD': nfevalsLS})

return info

def print_stats(self):
CVodePrintAllStats(self._cv_mem, stdio.stdout, SUN_OUTPUTFORMAT_TABLE)

def __dealloc__(self):
if self._cv_mem is not NULL: CVodeFree(&self._cv_mem)
Expand Down
6 changes: 6 additions & 0 deletions packages/scikits-odes/src/scikits_odes/ode.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ def get_info(self):
else:
return {}

def print_stats(self):
if hasattr(self._integrator, "print_stats"):
self._integrator.print_stats()
else:
print(f"Method `print_stats` is not implemented for integrator {self._integrator}")
Comment on lines +309 to +313
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably throw an exception or use logging.error, rather than printing.

Copy link
Author

@dmitry-kabanov dmitry-kabanov Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIR, I have modeled this method based on the method directly above it:

    def get_info(self):
        ...
        if hasattr(self._integrator, 'get_info'):
            return self._integrator.get_info()
        else:
            return {}

Will this work?

    def print_stats(self):
        if hasattr(self._integrator, "print_stats"):
            self._integrator.print_stats()
        else:
            raise AttributeError(
                f"Method `print_stats` is not implemented for integrator "
                f"'{self._integrator}'")

claude.ai says that "[this] approach is idiomatic and well-suited to the adapter pattern you're implementing" 😀


#------------------------------------------------------------------------------
# ODE integrators
#------------------------------------------------------------------------------
Expand Down
Loading