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

Enforce non-negative percents for material.add_nuclide to prevent unintended ao/wo flipping #3075

Merged
merged 3 commits into from
Jul 8, 2024

Conversation

johvincau
Copy link
Contributor

Description

In the Python API, when adding nuclides to a material, there appears to be an unintentional second way to flip between at% and wt% definitions as was discovered in #2669.

Behind the scenes and on the C++ side of things, OpenMC differentiates between at% and wt% inputs with sign, with negatively signed values indicating the latter. Thus, in the Python API, when the add_nuclide method is called with 'wo', the input percentage later has its sign flipped when running OpenMC.

However, what this means is that material.add_nuclide(nuc, -(percent), 'wo') is interpreted as an at% input, which can lead to the unintentional mixing of 'ao' and 'wo' for a material if by chance a stray negative sign were entered. This throws an error at runtime that fails to indicate that all add_nuclide percent values should be positive for composition type consistency.

Quick Fix

Simply requiring a non-negative value when calling add_nuclide makes the fix much more apparent by returning a negative ValueError when the given percent is negative.

While there's a chance some users manually utilize the negative percent to toggle between ao and wo, it's clear this is undocumented and likely unintentional behavior, and it's more transparent to just require 'ao' or 'wo' for controlling percent composition type.

Checklist

  • I have performed a self-review of my own code
  • I have run clang-format (version 15) 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)

Copy link
Contributor

@paulromano paulromano left a comment

Choose a reason for hiding this comment

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

Thanks for addressing this! Can you do the same for the add_element method?

@johvincau
Copy link
Contributor Author

Added! Let me know if there's anything else

@paulromano paulromano linked an issue Jul 8, 2024 that may be closed by this pull request
@paulromano paulromano merged commit e74dc50 into openmc-dev:develop Jul 8, 2024
16 checks passed
church89 pushed a commit to openmsr/openmc that referenced this pull request Jul 18, 2024
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.

Erroneous error message
2 participants