Skip to content

Commit

Permalink
Starting the armi.testing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Dec 6, 2024
1 parent 68268c1 commit b5f97da
Show file tree
Hide file tree
Showing 21 changed files with 149 additions and 117 deletions.
2 changes: 1 addition & 1 deletion armi/bookkeeping/db/tests/test_database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from armi.reactor.excoreStructure import ExcoreCollection, ExcoreStructure
from armi.reactor.reactors import Core, Reactor
from armi.reactor.spentFuelPool import SpentFuelPool
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.settings.fwSettings.globalSettings import CONF_SORT_REACTOR
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT, mockRunLogs
from armi.utils import getPreviousTimeNode, safeCopy
from armi.utils.directoryChangers import TemporaryDirectoryChanger
Expand Down
2 changes: 1 addition & 1 deletion armi/bookkeeping/db/tests/test_databaseInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from armi.physics.neutronics.settings import CONF_LOADING_FILE
from armi.reactor import grids
from armi.reactor.flags import Flags
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT
from armi.utils import directoryChangers

Expand Down
2 changes: 1 addition & 1 deletion armi/bookkeeping/report/tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
writeCycleSummary,
writeWelcomeHeaders,
)
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor
from armi.tests import mockRunLogs
from armi.utils.directoryChangers import TemporaryDirectoryChanger

Expand Down
2 changes: 1 addition & 1 deletion armi/cli/tests/test_runEntryPoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from armi.cli.run import RunEntryPoint
from armi.cli.runSuite import RunSuiteCommand
from armi.physics.neutronics.diffIsotxs import CompareIsotxsLibraries
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import mockRunLogs, TEST_ROOT, ARMI_RUN_PATH
from armi.utils.directoryChangers import TemporaryDirectoryChanger
from armi.utils.dynamicImporter import getEntireFamilyTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
CONF_XS_BLOCK_REPRESENTATION,
CONF_DISABLE_BLOCK_TYPE_EXCLUSION_IN_XS_GENERATION,
)
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor
from armi.tests import TEST_ROOT


Expand Down
2 changes: 1 addition & 1 deletion armi/physics/neutronics/tests/test_crossSectionTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
)
from armi.physics.neutronics.latticePhysics import ORDER
from armi.reactor.tests.test_blocks import loadTestBlock
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.settings import Settings
from armi.testing import loadTestReactor
from armi.tests import ISOAA_PATH


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from armi.physics.neutronics.macroXSGenerationInterface import (
MacroXSGenerationInterface,
)
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.settings import Settings
from armi.testing import loadTestReactor
from armi.tests import ISOAA_PATH


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import os
import unittest

from armi.tests import TEST_ROOT
from armi.physics.neutronics.settings import CONF_EPS_FSPOINT
from armi.reactor.converters.parameterSweeps.generalParameterSweepConverters import (
NeutronicConvergenceModifier,
ParameterSweepConverter,
SettingsModifier,
)
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.physics.neutronics.settings import CONF_EPS_FSPOINT
from armi.testing import loadTestReactor
from armi.tests import TEST_ROOT


THIS_DIR = os.path.dirname(__file__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
areAxiallyLinked,
)
from armi.reactor.flags import Flags
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT
from armi.utils import units

Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/converters/tests/test_blockConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from armi.reactor.converters import blockConverters
from armi.reactor.flags import Flags
from armi.reactor.tests.test_blocks import loadTestBlock
from armi.reactor.tests.test_reactors import TEST_ROOT, loadTestReactor
from armi.testing import TEST_ROOT, loadTestReactor
from armi.utils import hexagon
from armi.utils.directoryChangers import TemporaryDirectoryChanger

Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/converters/tests/test_geometryConverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from armi.reactor.converters import geometryConverters
from armi.reactor.converters import uniformMesh
from armi.reactor.flags import Flags
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT, mockRunLogs
from armi.utils import directoryChangers

Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/converters/tests/test_meshConverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import unittest

from armi.reactor.converters import meshConverters, geometryConverters
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor
from armi.tests import TEST_ROOT


Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/converters/tests/test_uniformMesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from armi.reactor.flags import Flags
from armi.reactor.tests import test_assemblies
from armi.reactor.tests import test_blocks
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT, ISOAA_PATH


Expand Down
7 changes: 3 additions & 4 deletions armi/reactor/tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
from armi.reactor.components import basicShapes, complexShapes
from armi.reactor.flags import Flags
from armi.reactor.tests.test_assemblies import makeTestAssembly
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.reactor.tests.test_reactors import TEST_ROOT
from armi.tests import ISOAA_PATH
from armi.testing import loadTestReactor
from armi.tests import ISOAA_PATH, TEST_ROOT
from armi.utils import hexagon, units, densityTools
from armi.utils.directoryChangers import TemporaryDirectoryChanger
from armi.utils.units import MOLES_PER_CC_TO_ATOMS_PER_BARN_CM
from armi.utils.units import ASCII_LETTER_A, ASCII_LETTER_Z, ASCII_LETTER_a
from armi.utils.units import MOLES_PER_CC_TO_ATOMS_PER_BARN_CM


NUM_PINS_IN_TEST_BLOCK = 217
Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
UnshapedVolumetricComponent,
materials,
)
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor


class TestComponentFactory(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/tests/test_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from armi.reactor.composites import getReactionRateDict
from armi.reactor.flags import Flags, TypeSpec
from armi.reactor.tests.test_blocks import loadTestBlock
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor
from armi.tests import ISOAA_PATH
from armi.tests import mockRunLogs

Expand Down
2 changes: 1 addition & 1 deletion armi/reactor/tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from armi.reactor import parameters
from armi.reactor.reactorParameters import makeParametersReadOnly
from armi.reactor.tests.test_reactors import loadTestReactor
from armi.testing import loadTestReactor
from armi.tests import TEST_ROOT
from armi.utils.directoryChangers import TemporaryDirectoryChanger

Expand Down
98 changes: 5 additions & 93 deletions armi/reactor/tests/test_reactors.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
from armi.reactor.spentFuelPool import SpentFuelPool
from armi.settings.fwSettings.globalSettings import CONF_ASSEM_FLAGS_SKIP_AXIAL_EXP
from armi.settings.fwSettings.globalSettings import CONF_SORT_REACTOR
from armi.tests import ARMI_RUN_PATH, mockRunLogs, TEST_ROOT
from armi.testing import loadTestReactor, reduceTestReactorRings # noqa: F401
from armi.tests import mockRunLogs, TEST_ROOT
from armi.utils import directoryChangers

THIS_DIR = os.path.dirname(__file__)
TEST_REACTOR = None # pickled string of test reactor (for fast caching)
_THIS_DIR = os.path.dirname(__file__)


def buildOperatorOfEmptyHexBlocks(customSettings=None):
Expand Down Expand Up @@ -130,95 +130,6 @@ def buildOperatorOfEmptyCartesianBlocks(customSettings=None):
return o


"""
NOTE: If this reactor had 3 rings instead of 9, most unit tests that use it
go 4 times faster (based on testing). The problem is it would breat a LOT
of downstream tests that import this method. Probably still worth it though.
"""


def loadTestReactor(
inputFilePath=TEST_ROOT,
customSettings=None,
inputFileName="armiRun.yaml",
):
"""
Loads a test reactor. Can be used in other test modules.
Parameters
----------
inputFilePath : str, default=TEST_ROOT
Path to the directory of the input file.
customSettings : dict with str keys and values of any type, default=None
For each key in customSettings, the cs which is loaded from the
armiRun.yaml will be overwritten to the value given in customSettings
for that key.
inputFileName : str, default="armiRun.yaml"
Name of the input file to run.
Returns
-------
o : Operator
r : Reactor
"""
global TEST_REACTOR
fName = os.path.join(inputFilePath, inputFileName)
customSettings = customSettings or {}
isPickeledReactor = fName == ARMI_RUN_PATH and customSettings == {}

if isPickeledReactor and TEST_REACTOR:
# return test reactor only if no custom settings are needed.
o, r, assemNum = cPickle.loads(TEST_REACTOR)
o.reattach(r, o.cs)
return o, r

cs = settings.Settings(fName=fName)

# Overwrite settings if desired
if customSettings:
cs = cs.modified(newSettings=customSettings)

if "verbosity" not in customSettings:
runLog.setVerbosity("error")

newSettings = {}
cs = cs.modified(newSettings=newSettings)

o = operators.factory(cs)
r = reactors.loadFromCs(cs)

o.initializeInterfaces(r)

o.r.core.regenAssemblyLists()

if isPickeledReactor:
# cache it for fast load for other future tests
# protocol=2 allows for classes with __slots__ but not __getstate__ to be pickled
TEST_REACTOR = cPickle.dumps((o, o.r, o.r.p.maxAssemNum), protocol=2)

return o, o.r


def reduceTestReactorRings(r, cs, maxNumRings):
"""Helper method for the test reactor above.
The goal is to reduce the size of the reactor for tests that don't neeed
such a large reactor, and would run much faster with a smaller one.
"""
maxRings = r.core.getNumRings()
if maxNumRings > maxRings:
runLog.info("The test reactor has a maximum of {} rings.".format(maxRings))
return
elif maxNumRings <= 1:
raise ValueError("The test reactor must have multiple rings.")

# reducing the size of the test reactor, by removing the outer rings
for ring in range(maxRings, maxNumRings, -1):
r.core.removeAssembliesInRing(ring, cs)


class ReactorTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
Expand Down Expand Up @@ -867,7 +778,8 @@ class MockLib:
for b in self.r.core.getBlocks():
b.p.mgFlux = range(5)
b.p.adjMgFlux = range(5)
with directoryChangers.TemporaryDirectoryChanger(root=THIS_DIR):

with directoryChangers.TemporaryDirectoryChanger(root=_THIS_DIR):
self.r.core.saveAllFlux()

def test_getFluxVector(self):
Expand Down
Loading

0 comments on commit b5f97da

Please sign in to comment.