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

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.

@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.

`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
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