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

Prevent segfault in distance to boundary calculation #2659

Merged
merged 2 commits into from
Sep 1, 2023

Conversation

paulromano
Copy link
Contributor

Description

This issue was discovered by a user. If a particle is traveling in a particular direction, leaves a cell with a complex region definition, and then goes to compute a distance to the nearest boundary and there are no boundaries in front of it (i.e., distance is infinite), right now this results in a segfault. This PR avoids that segfault with a one-line change. I've also added a quick test inspired by the user's original example that led to this.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format on any C++ source files (if applicable)
  • I have followed the style guidelines for Python source files (if applicable)
  • I have made corresponding changes to the documentation (if applicable)
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

@gridley
Copy link
Contributor

gridley commented Aug 21, 2023

Makes sense to me. So, if you have intersected on all surfaces and the distance is still infinity, this is taking the intersection surface to be any of the indices which led to infinite distance. Then it just causes the collision to limit the travel distance, right?

Now that this is allowed, maybe we should do a check around line 220 of particle.cpp that both the collision distance and intersection are not infinity. Otherwise it could cause an infinite loop (or it would go until it maxes out the particle event counter).

@paulromano
Copy link
Contributor Author

So, if you have intersected on all surfaces and the distance is still infinity, this is taking the intersection surface to be any of the indices which led to infinite distance. Then it just causes the collision to limit the travel distance, right?

Almost -- the intersection surface is actually left unset (std::numeric_limits<int32_t>::max()). The collision will indeed be limited by the travel distance and all is good.

maybe we should do a check around line 220 of particle.cpp that both the collision distance and intersection are not infinity. Otherwise it could cause an infinite loop (or it would go until it maxes out the particle event counter).

Astute observation. In fact, if you take the test I put together here and change the air material to void, the code will still segfault because now there are no collisions. I'm honestly not sure what the behavior should be in this case. Basically, the particle is flying off to infinity and there are no boundaries to stop it. Open to thoughts on what we should do for that case.

@gridley
Copy link
Contributor

gridley commented Aug 21, 2023

IMO we should fatal_error at that point, maybe a message like "particle has left the Milky Way. exiting simulation."

@paulromano
Copy link
Contributor Author

@gridley would you prefer that I handle the "particle leaving milky way" problem as part of this PR? I was going to follow-up with another PR handling that (along with a test), but I can also add it here if you'd like.

@gridley
Copy link
Contributor

gridley commented Sep 1, 2023

Sounds good to handle it in another, Paul!

@gridley gridley merged commit 1cae2f7 into openmc-dev:develop Sep 1, 2023
@paulromano paulromano deleted the geom-distance-fix branch September 5, 2023 14:44
stchaker pushed a commit to stchaker/openmc that referenced this pull request Oct 25, 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