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

Documentation #96

Closed
wants to merge 8 commits into from
Closed

Documentation #96

wants to merge 8 commits into from

Conversation

bjorgve
Copy link
Member

@bjorgve bjorgve commented Jan 28, 2023

I think it's time to update master with some docs and fix to killed kernels when badly defined analytic functions are projected.

#95

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Comment on lines 23 to 30
try {
auto arr = std::array<double, D>();
arr.fill(111111.111); // A number which hopefully does not divide by zero
func(arr);
} catch (py::cast_error &e) {
py::print("Error: Invalid definition of analytic function");
throw;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

what's this block supposed to do?

Copy link
Member Author

@bjorgve bjorgve Jan 28, 2023

Choose a reason for hiding this comment

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

It catches a cast_error which kills Jupyter kernels. Want a comment there? When we try to project analytic functions.
So I try to evaluate the function in a random point, and see if it works or not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment

@@ -1,7 +1,8 @@
#pragma once

#include <pybind11/functional.h>
#include <typeinfo>
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't see anything from this header being used.

@@ -1,5 +1,6 @@
myst_nb
sphinx
sphinx_rtd_theme
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is needed.

@@ -18,6 +17,18 @@ template <int D> void project(pybind11::module &m) {
.def(
"__call__",
[](PyScalingProjector<D> &P, std::function<double(const Coord<D> &r)> func) {

try {
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I see what you want to do. I don't think it's the best strategy, as you can't be sure this test won't give you a false positive. I think what is explained here is what should be done instead: https://pybind11.readthedocs.io/en/stable/advanced/exceptions.html#handling-exceptions-from-python-in-c
The try-catch block should be around auto out = P(func);, I believe.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes I did try that first, but for some reason I found it a bit challenging. I think it had to do with the return, I can have another look at it.

P_scaling = vp.ScalingProjector(mra, 2)
P_wavelet = vp.WaveletProjector(mra, 2)

with pytest.raises(Exception):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see how use f(x) = x will raise an exception here. Same in the next test.

Copy link
Member Author

Choose a reason for hiding this comment

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

This syntax does not work in vampyr (probably should in 1d) and causes the kernel to crash (in Jupyter notebooks). Correct syntax is f(x) = x[0]

Copy link
Contributor

Choose a reason for hiding this comment

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

Now I see it 🤦🏻 the signature is std::function<double(const Coord<D> &r)> so it expects one number out of it. It's not exactly great style to raise/catch Exception (because it's the base class of all exceptions in Python) but I think it's appropriate here, because one cannot know exactly what went wrong when calling the function.

Copy link
Contributor

@robertodr robertodr left a comment

Choose a reason for hiding this comment

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

For the notebooks:

@bjorgve
Copy link
Member Author

bjorgve commented Jan 31, 2023

Okei, thanks for the feedback. I'll fix it asap.

@bjorgve bjorgve closed this Apr 16, 2023
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