Enforce non-negative percents for material.add_nuclide to prevent unintended ao/wo flipping #3075
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 alladd_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 negativeValueError
when the givenpercent
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