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

Allow strictly finding material components #663

Open
wants to merge 20 commits into
base: alpha-test-dev
Choose a base branch
from

Conversation

MicahGale
Copy link
Collaborator

@MicahGale MicahGale commented Feb 13, 2025

Pull Request Checklist for MontePy

Description

This is a feature implementation for contains, get_containing, and find. The topline is that there is a new strict argument that switches from implicit to explicit matching. So with strict=True "H" only matches 1000 and not 1001.80c nor 1000.80c. The other main thing was adding some feedback from @gonuke.

This feedback splint contains (and get_containing) to be: contains_all and contains_any. This should be clearer to users, and be more useful. The main use case of contains_any might be:

fissile = list(problem.material.get_containing_any("U-235", "U-233", "Pu-239",...)

Fixes #642


General Checklist

  • I have performed a self-review of my own code.
  • The code follows the standards outlined in the development documentation.
  • I have formatted my code with black version 25.
  • I have added tests that prove my fix is effective or that my feature works (if applicable).

Documentation Checklist

  • I have documented all added classes and methods.
  • For infrastructure updates, I have updated the developer's guide.
  • For significant new features, I have added a section to the getting started guide.

First-Time Contributor Checklist

  • If this is your first contribution, add yourself to pyproject.toml if you wish to do so.

Additional Notes for Reviewers

Ensure that:

  • The submitted code is consistent with the merge checklist outlined here.
  • The PR covers all relevant aspects according to the development guidelines.
  • 100% coverage of the patch is achieved, or justification for a variance is given.

📚 Documentation preview 📚: https://montepy--663.org.readthedocs.build/en/663/

@MicahGale MicahGale self-assigned this Feb 13, 2025
@MicahGale MicahGale added alpha testing Issues that came up during alpha testing feature request An issue that improves the user interface. labels Feb 13, 2025
@MicahGale MicahGale added this to the release-1.0.0 milestone Feb 13, 2025
@MicahGale
Copy link
Collaborator Author

MicahGale commented Feb 13, 2025

TODO: find

resolved

@MicahGale MicahGale marked this pull request as ready for review February 26, 2025 16:59
@MicahGale MicahGale requested a review from tjlaboss February 27, 2025 21:49
@MicahGale MicahGale enabled auto-merge February 27, 2025 21:50
Copy link
Collaborator

@tjlaboss tjlaboss left a comment

Choose a reason for hiding this comment

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

Without running the code: the logic makes sense and the tests make sense.

I will give this function a test drive and confirm that everything works as expected before approving. In the meanwhile,

The type hinting for `nuclides is incorrect and one of the docstrings needs to be fixed. There are a few optional things to consider.

Checks if this material contains any of the given nuclide.

A boolean "or" is used for this comparison.
That is this material must contain all nuclides at or above the given threshold
Copy link
Collaborator

Choose a reason for hiding this comment

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

That this material must contain any nuclides at or above the given threshold!

Comment on lines +724 to +734
if mat.contains_all("U"):
pass

# try to find a uranium
.. note::

For details on how to use the ``strict`` argument see the examples in: :func:`find`.

.. note::

If a nuclide is in a material multiple times, and cumulatively exceeds the threshold,
but for each instance it appears it is below the threshold this method will return False.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Clarify behavior:

  • If some nuclide appears multiple times, they are not summed when evaluating the threshold.
  • But if an element is requested, the all isotopes of that element are summed [unless strict]

Is this the intended behavior?

Is there any reason not to sum a nuclide that appears more than once? After all, we're just returning a bool, not a match.

def _contains_arb(
self,
*nuclides: Union[Nuclide, Nucleus, Element, str, int],
bool_func: co.abc.Callable[co.abc.Iterable[bool]] = all,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think bool_func should have a default.

Comment on lines 857 to 859
nuclides_search = {}
nuclei_search = {}
element_search = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we only ever use values(), these don't need to be dicts. We can actually get away with just a set.

The dicts make it easier to maintain and debug though, and the performance penalty is very minor.


def get_containing_all(
self,
*nuclides: Union[
Copy link
Collaborator

Choose a reason for hiding this comment

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

*args are necessarily a tuple.

self,
nuclide: Union[Nuclide, Nucleus, Element, str, int],
*args: Union[Nuclide, Nucleus, Element, str, int],
*nuclides: Union[Nuclide, Nucleus, Element, str, int],
Copy link
Collaborator

Choose a reason for hiding this comment

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

*args are necessarily a tuple:

Tuple[Union[Nuclide, Nucleus, Element, str, int], ...]

This type hint is long and frequently reused. Store it as a variable and reuse it.

self,
nuclide: Union[
*nuclides: Union[
Copy link
Collaborator

Choose a reason for hiding this comment

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

*args are necessarily a tuple


def _contains_arb(
self,
*nuclides: Union[
Copy link
Collaborator

Choose a reason for hiding this comment

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

*args are necessarily a tuple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
alpha testing Issues that came up during alpha testing feature request An issue that improves the user interface.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants