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

Python source class refactor #2524

Merged
merged 42 commits into from
Jun 21, 2023
Merged

Conversation

pshriwise
Copy link
Contributor

The Source class currently holds attributes that allow it to specify sources that contain independent distributions to be sampled, file sources, and custom/compiled source libraries to be called to generate particles.
On the C++ side, we have three classes that inherit from a common Source class: IndependentSource, FileSource, and CustomSource.

These changes refactor the Python API's source classes so that there are corresponding classes to those in the C++ interface. Source has been renamed to IndependentSource and a new base class, Source has been added to the module. Three classes inherit from Source : IndpendentSource, FileSource, and CompiledSource. (I renamed CustomSource to CompiledSource in both the C++ and Python code since I think that name is more clear).

This name change from Source to IndependentSource is going to cause a lot of updates to user scripts/code. With that being, such a hassle I'd understand if we don't decide to keep that change just for the sake of parity between the C++ and Python classes. cue revival of a discussion about PyBind/automated wrapping here

In terms of file specs, sources can now be identified by a type attribute on the XML element, but interestingly the logic in C++ for how these sources are created still holds, so old settings.xml files should still work with the OpenMC executable if we don't change that code. This is not true for XML files, however. Older XML files can no longer be read into Python objects unless we use similar logic to that of the C++ code, which would make the type attribute just a placeholder for information. I'd be interested in others' thoughts here.

Copy link
Contributor

@gonuke gonuke 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 this clear improvement @pshriwise. A few comments on readability and the test for compiled sources.

tests/regression_tests/adj_cell_rotation/inputs_true.dat Outdated Show resolved Hide resolved
openmc/source.py Outdated
Attributes
----------
type : str
Indicator of source type. One of ('source', 'mesh', 'file', 'compiled')
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems redundant and potentially confusion to declare a Source as type 'source'. Perhaps choose something other than 'source' for the default type. Maybe 'analytic', 'computed', ???

Copy link
Contributor Author

@pshriwise pshriwise May 16, 2023

Choose a reason for hiding this comment

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

Fair point. I think matching the class name here makes sense ("independent"). If we feel that this name isn't sufficiently descriptive then perhaps we should change both.

I also need to remove "mesh" from the list here. These changes were initially added on top of my branch adding a mesh source class..

openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
tests/regression_tests/source_dlopen/inputs_true.dat Outdated Show resolved Hide resolved
@@ -18,6 +18,6 @@
<particles>1000</particles>
<batches>10</batches>
<inactive>0</inactive>
<source library="build/libsource.so" parameters="1e3" strength="1.0"/>
<source strength="1.0" type="source"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

This source should still have a 'library' member, if I understand the changes to source.py correctly.

Copy link
Contributor

@gonuke gonuke left a comment

Choose a reason for hiding this comment

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

There's a lingering inconsistency in the string to be used for the type of the independent source. Wonder if that should be declared once at a high scope and reused?

@@ -449,24 +449,29 @@ attributes/sub-elements:

*Default*: 1.0

:type:
Indicator of source type. One of ``independent_source``, ``file``, or ``compiled``.
Copy link
Contributor

Choose a reason for hiding this comment

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

The files below only have type='independent' - either this documentation or those files should change...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The other classes use the unique component of the class name as their type, so we should do the same here. Updating that docstring now.

openmc/source.py Show resolved Hide resolved
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 this PR @pshriwise! In addition to the line comments, please search through the documentation (*.rst) for the needed name changes (openmc.Source as well as anything related to custom sources).

docs/source/io_formats/settings.rst Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
tests/regression_tests/source_dlopen/test.py Outdated Show resolved Hide resolved
tests/unit_tests/test_source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
tests/regression_tests/source_dlopen/test.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
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.

Two more small changes and then this is good to go!

openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated
Comment on lines 536 to 543
@property
def file(self):
return self._file

@file.setter
def file(self, filename: str):
cv.check_type('source file', filename, str)
self._file = filename
Copy link
Contributor

Choose a reason for hiding this comment

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

The argument in the constructor is filename but the property is file. Since this is a new class, let's pick one and be consistent. In fact, it might be better to call it path and make sure it accepts pathlib.Path objects.

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.

A few more little fixes needed:

openmc/settings.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
openmc/source.py Outdated Show resolved Hide resolved
@paulromano paulromano merged commit eda39ad into openmc-dev:develop Jun 21, 2023
@paulromano
Copy link
Contributor

Thanks again @pshriwise!

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.

6 participants