-
Notifications
You must be signed in to change notification settings - Fork 159
Add Python interfaces based on nanobind #796
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
base: develop
Are you sure you want to change the base?
Conversation
…eature/python-interfaces
Co-authored-by: David Gardner <[email protected]>
|
|
||
| This chapter covers details developers need to know about the SUNDIALS Python interfaces, distributed as the Python package sundials4py. | ||
|
|
||
| We use `nanobind <https://github.com/wjakob/nanobind>__` for the Python bindings. nanobind is a sleeker, faster ``pybind11``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is nanobind a fork/version of pybind11? Is it just a completely separate package? As written, it sounds like the developers of pybind11 just cleaned things up and changed the name.
| We use `nanobind <https://github.com/wjakob/nanobind>__` for the Python bindings. nanobind is a sleeker, faster ``pybind11``. | |
| We use `nanobind <https://github.com/wjakob/nanobind>__` for the Python bindings. nanobind is a sleeker, faster replacement for ``pybind11``. |
drreynolds
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've finished going through doc. I have a good amount of bindings/sundials4py to finish...
drreynolds
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! I have a few more questions/comments, but I believe I've finished a first pass over my "assignments"
| using namespace sundials::experimental; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is duplicated below
| using namespace sundials::experimental; |
| // m.def("MRIStepInnerStepper_GetForcingData", | ||
| // [](MRIStepInnerStepper stepper) -> std::tuple<int, sunrealtype, sunrealtype, std::vector<N_Vector>, int> { | ||
|
|
||
| // sunrealtype tshift = 0.0; | ||
| // sunrealtype tscale = 0.0; | ||
| // N_Vector* forcing_1d = nullptr; | ||
| // int nforcing = 0; | ||
|
|
||
| // int status = MRIStepInnerStepper_GetForcingData(stepper, &tshift, &tscale, &forcing_1d, &nforcing); | ||
|
|
||
| // std::vector<N_Vector> forcing(nforcing); | ||
| // // TODO(CJB): for some reason this causes a segfault unless you clone | ||
| // // for (int i = 0; i < nforcing; i++) { | ||
| // // // forcing[i] = N_VClone(forcing_1d[i]); | ||
| // // forcing[i] = forcing_1d[i]; | ||
| // // } | ||
|
|
||
| // return std::make_tuple(status, tshift, tscale, forcing, nforcing); | ||
| // }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this commented-out code be removed or fixed?
| assert status == ARK_SUCCESS | ||
|
|
||
| # PCG linear solver with no preconditioning, with up to N iterations | ||
| LS = SUNLinSol_PCG(y, 0, N, sunctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update the C example to use the enum as well:
| LS = SUNLinSol_PCG(y, 0, N, sunctx) | |
| LS = SUNLinSol_PCG(y, SUN_PREC_NONE, N, sunctx) |
| status = ark.ARKodeSetMaxNumSteps(arkode.get(), int((tf - t0) / dt) + 1) | ||
| assert status == ark.ARK_SUCCESS | ||
|
|
||
| # # Enable checkpointing during the forward run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # # Enable checkpointing during the forward run | |
| # Enable checkpointing during the forward run |
| # transition, both u and v continue to evolve somewhat | ||
| # rapidly for another 1.4 time units, and finish off smoothly. | ||
| # | ||
| # This program solves the problem with the DIRK method, using a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # This program solves the problem with the DIRK method, using a | |
| # This program solves the problem with the BDF method, using a |
| # Reshape to 2D for vectorized operations | ||
| u2d = np.zeros((self.NX + 2, self.NY + 2), dtype=sunrealtype) | ||
| # Fill interior points | ||
| for i in range(1, self.NX + 1): | ||
| for j in range(1, self.NY + 1): | ||
| u2d[i, j] = u[idx(i, j)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on your comment, it looks like you were attempting to use np.reshape but couldn't get it to work, so you ended up copying u into a new 2D array, u2d. If so, it's too bad that this didn't work, since numpy's multi-dimensional arrays are incredibly attractive for structured grid calculations, but I recommend that you modify the comment:
| # Reshape to 2D for vectorized operations | |
| u2d = np.zeros((self.NX + 2, self.NY + 2), dtype=sunrealtype) | |
| # Fill interior points | |
| for i in range(1, self.NX + 1): | |
| for j in range(1, self.NY + 1): | |
| u2d[i, j] = u[idx(i, j)] | |
| # Copy state to 2D for vectorized operations | |
| u2d = np.zeros((self.NX + 2, self.NY + 2), dtype=sunrealtype) | |
| # Fill interior points | |
| for i in range(1, self.NX + 1): | |
| for j in range(1, self.NY + 1): | |
| u2d[i, j] = u[idx(i, j)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment was chatgpt and I missed updating it (chatgpt did a great job at the initial porting of C examples to Python). I did not try using np.reshape.
Co-authored-by: Daniel R. Reynolds <[email protected]>
Steven-Roberts
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review of docs. I now realize that wasn't assigned to me, but I needed to read it anyway to install and understand sundials4py.
doc/shared/Python/Usage.rst
Outdated
|
|
||
| SUNDIALS packages and several modules/classes require user-supplied callback functions to define problem-specific behavior, | ||
| such as the right-hand side of an ODE or a nonlinear system function. In sundials4py, you can provide these as standard Python functions or lambdas. | ||
| Somethings to note: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Somethings to note: | |
| Here are some things to note: |
This PR adds sundials4py, which is a Python interface to SUNDIALS.
The generator code can be found here: https://github.com/sundials-codes/sundials4py-generate.
Rendered docs: https://sundials--796.org.readthedocs.build/en/796/.
Review Requests
@drreynolds:
doc/bindings/sundials4py/arkodebindings/sundials4py/idasbindings/sundials4py/kinsolbindings/sundials4py/sundomeigestbindings/sundials4py/sunlinsolbindings/sundials4py/sunmatrixbindings/sundials4py/examples@gardner48:
Everything but can skip:
_generated.hppfiles (Steven and I will review these)bindings/sundials4py/examples(Steven and Dan are both reviewing these)bindings/sundials4py/arkode(Steven and Dan both reviewing these)bindings/sundials4py/idas(Steven and Dan both reviewing these)@Steven-Roberts
_generated.hppfiles inbindings/sundials4pybindings/sundials4py/arkodebindings/sundials4py/idasbindings/sundials4py/includebindings/sundials4py/sundialsbindings/sundials4py/nvectorbindings/sundials4py/sunadaptcontrollerbindings/sundials4py/sunadjointcheckpointschemebindings/sundials4py/sunnonlinsolbindings/sundials4py/sunmemorybindings/sundials4py/examples