Skip to content

Commit

Permalink
New plugin hook for before reactor construction (#1945)
Browse files Browse the repository at this point in the history
  • Loading branch information
opotowsky authored Oct 15, 2024
1 parent 0c4b93d commit 01b0ade
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
15 changes: 15 additions & 0 deletions armi/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ def onProcessCoreLoading(core, cs, dbLoad) -> None:
constructing a Core from Blueprints, or after loading it from a database.
"""

@staticmethod
@HOOKSPEC
def beforeReactorConstruction(cs) -> None:
"""
Function to call before the reactor is constructed.
.. impl:: Plugins can inject code before reactor initialization.
:id: I_ARMI_PLUGIN_BEFORE_REACTOR_HOOK
:implements: R_ARMI_PLUGIN_BEFORE_REACTOR_HOOK
This method allows for plugin developers to implement code after settings
are loaded but before the reactor is constructed. This hook is called
in :py:func:`armi.reactor.reactors.factory`.
"""

@staticmethod
@HOOKSPEC
def defineFlags() -> Dict[str, Union[int, flags.auto]]:
Expand Down
14 changes: 14 additions & 0 deletions armi/reactor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

from typing import Dict, Callable, Union, TYPE_CHECKING

from armi import materials
from armi import plugins

if TYPE_CHECKING:
Expand All @@ -62,6 +63,19 @@
class ReactorPlugin(plugins.ArmiPlugin):
"""Plugin exposing built-in reactor components, blocks, assemblies, etc."""

@staticmethod
@plugins.HOOKIMPL
def beforeReactorConstruction(cs) -> None:
"""Just before reactor construction, update the material "registry" with user settings,
if it is set. Often it is set by the application.
"""
from armi.settings.fwSettings.globalSettings import (
CONF_MATERIAL_NAMESPACE_ORDER,
)

if cs[CONF_MATERIAL_NAMESPACE_ORDER]:
materials.setMaterialNamespaceOrder(cs[CONF_MATERIAL_NAMESPACE_ORDER])

@staticmethod
@plugins.HOOKIMPL
def defineBlockTypes():
Expand Down
9 changes: 3 additions & 6 deletions armi/reactor/reactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from typing import Optional
import copy

from armi import materials
from armi import getPluginManagerOrFail
from armi import runLog
from armi.reactor import composites
from armi.reactor import reactorParameters
Expand All @@ -29,7 +29,6 @@
from armi.reactor.systemLayoutInput import SystemLayoutInput
from armi.settings.fwSettings.globalSettings import (
CONF_GEOM_FILE,
CONF_MATERIAL_NAMESPACE_ORDER,
CONF_SORT_REACTOR,
)
from armi.utils import directoryChangers
Expand Down Expand Up @@ -180,10 +179,8 @@ def factory(cs, bp, geom: Optional[SystemLayoutInput] = None) -> Reactor:
from armi.reactor import blueprints

runLog.header("=========== Constructing Reactor and Verifying Inputs ===========")
# just before reactor construction, update the material "registry" with user settings,
# if it is set. Often it is set by the application.
if cs[CONF_MATERIAL_NAMESPACE_ORDER]:
materials.setMaterialNamespaceOrder(cs[CONF_MATERIAL_NAMESPACE_ORDER])
getPluginManagerOrFail().hook.beforeReactorConstruction(cs=cs)

r = Reactor(cs.caseTitle, bp)

if cs[CONF_GEOM_FILE]:
Expand Down
23 changes: 23 additions & 0 deletions armi/tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ def getAxialExpansionChanger() -> type[SillyAxialExpansionChanger]:
return SillyAxialExpansionChanger


class BeforeReactorPlugin(plugins.ArmiPlugin):
"""Trivial plugin that implements the before reactor construction hook."""

@staticmethod
@plugins.HOOKIMPL
def beforeReactorConstruction(cs) -> None:
cs.beforeReactorConstructionFlag = True


class TestPluginRegistration(unittest.TestCase):
def setUp(self):
"""
Expand Down Expand Up @@ -119,6 +128,20 @@ def test_axialExpansionHook(self):
# that plugin's axial expander
self.assertIs(second, SillyAxialExpansionChanger)

def test_beforeReactorConstructionHook(self):
"""Test that plugin hook successfully injects code before reactor initialization.
.. test:: Capture code in the beforeReactorConstruction hook from reactor construction being carried out.
:id: T_ARMI_PLUGIN_BEFORE_REACTOR_HOOK
:tests: R_ARMI_PLUGIN_BEFORE_REACTOR_HOOK
"""
pm = getPluginManagerOrFail()
pm.register(BeforeReactorPlugin)
o = loadTestReactor(
TEST_ROOT, inputFileName="smallestTestReactor/armiRunSmallest.yaml"
)[0]
self.assertTrue(o.cs.beforeReactorConstructionFlag)


class TestPluginBasics(unittest.TestCase):
def test_defineParameters(self):
Expand Down
6 changes: 4 additions & 2 deletions doc/release/0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ New Features
#. Adding ``--skip-inspection`` flag to ``CompareCases`` CLI. (`PR#1842 <https://github.com/terrapower/armi/pull/1842>`_)
#. Allow merging a component with zero area into another component (`PR#1858 <https://github.com/terrapower/armi/pull/1858>`_)
#. Use ``Block.getNumPins()`` in ``HexBlock._rotatePins()``. (`PR#1859 <https://github.com/terrapower/armi/pull/1859>`_)
#. Provide utilities for determining location of a rotated object in a hexagonal lattice (``getIndexOfRotatedCell``). (`PR#1846 <https://github.com/terrapower/armi/1846`)
#. Provide utilities for determining location of a rotated object in a hexagonal lattice (``getIndexOfRotatedCell``). (`PR#1846 <https://github.com/terrapower/armi/1846>`_)
#. Allow merging a component with zero area into another component. (`PR#1858 <https://github.com/terrapower/armi/pull/1858>`_)
#. Provide ``Parameter.hasCategory`` for quickly checking if a parameter is defined with a given category. (`PR#1899 <https://github.com/terrapower/armi/pull/1899>`_)
#. Provide ``ParameterCollection.where`` for efficient iteration over parameters who's definition matches a given condition. (`PR#1899 <https://github.com/terrapower/armi/pull/1899>`_)
#. Plugins can provide the ``getAxialExpansionChanger`` hook to customize axial expansion. (`PR#1870 <https://github.com/terrapower/armi/pull/1870`_)
#. Plugins can provide the ``getAxialExpansionChanger`` hook to customize axial expansion. (`PR#1870 <https://github.com/terrapower/armi/pull/1870>`_)
#. New plugin hook ``beforeReactorConstruction`` added to enable plugins to process case settings before reactor init. (`PR#1945 <https://github.com/terrapower/armi/pull/1945>`_)
#. Provide ``Block.getInputHeight`` for determining the height of a block from blueprints. (`PR#1927 <https://github.com/terrapower/armi/pull/1927`_)
#. TBD

Expand All @@ -35,7 +37,7 @@ API Changes
#. Allowing for unknown Flags when opening a DB. (`PR#1844 <https://github.com/terrapower/armi/pull/1835>`_)
#. Removing ``Assembly.doubleResolution()``. (`PR#1951 <https://github.com/terrapower/armi/pull/1951>`_)
#. Removing ``assemblyLists.py`` and the ``AssemblyList`` class. (`PR#1891 <https://github.com/terrapower/armi/pull/1891>`_)
#. Removing ``Assembly.rotatePins`` and ``Block.rotatePins``. Prefer ``Assembly.rotate`` and ``Block.rotate``. (`PR#1846 <https://github.com/terrapower/armi/1846`_)
#. Removing ``Assembly.rotatePins`` and ``Block.rotatePins``. Prefer ``Assembly.rotate`` and ``Block.rotate``. (`PR#1846 <https://github.com/terrapower/armi/1846>`_)
#. Transposing ``pinMgFluxes`` parameters so that leading dimension is pin index (`PR#1937 <https://github.com/terrapower/armi/pull/1937>`)
#. Removing ``globalFluxInterface.DoseResultsMapper`` class (`PR#1952 <https://github.com/terrapower/armi/pull/1952>`)
#. TBD
Expand Down

0 comments on commit 01b0ade

Please sign in to comment.