Skip to content

Conversation

@dmitry-kabanov
Copy link

@dmitry-kabanov dmitry-kabanov commented Mar 12, 2024

The CVODE solver has a function called CVodePrintAllStats that prints statistics about the integration process, such as number of right-hand-side function evaluations and number of nonlinear solves.

This PR adds a method print_stats to the ode and CVode classes, such that user could print the above statistics in case, when they specify CVode as the integrator argument at the instantiation of the ode class.

CVodePrintAllStats allows to print the statistics and two different formats (as a formatted table or as a CSV file), to a given file stream (FILE * object). Right now I have narrowed it to always printing statistics to stdout as a formatted table.

@dmitry-kabanov dmitry-kabanov force-pushed the add-print-stats-method branch from 03b60d0 to dffe1a7 Compare March 12, 2024 13:30
@aragilar
Copy link
Collaborator

@dmitry-kabanov If you're still interested in working on this, would you be able to rebase this on the latest master?

@dmitry-kabanov
Copy link
Author

dmitry-kabanov commented Jul 12, 2024

@aragilar Sure, I would be happy to do it. I have actually already rebased on the latest master, but have difficulties compiling the code via pip install -e . (from packages/scikit_sundials subdirectory).

I've created a separate issue about this #178.

@dmitry-kabanov dmitry-kabanov force-pushed the add-print-stats-method branch from dffe1a7 to be54ff6 Compare July 12, 2024 11:41
`CVODE` solver has function `CVodePrintAllStats` that prints statistics about
integrator such as number of right-hand-side function evaluations and number
of nonlinear solves.
@dmitry-kabanov dmitry-kabanov force-pushed the add-print-stats-method branch from be54ff6 to 651ef5c Compare July 19, 2024 14:48
@dmitry-kabanov
Copy link
Author

@aragilar I have rebased the PR on the latest master right now and have tested that it works with the following script:

import numpy as np
from scikits_odes import ode

def rhs(t, y, ydot):
    ydot[:] = -y

t0 = 0.0
y0 = [1.0]

s = ode("cvode", rhs, old_api=False)
sol = s.solve(np.linspace(t0, t0 + 1, 11), y0)

print(sol.values.y[:, 0])
s.print_stats()

Comment on lines +309 to +313
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}")
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" 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants