Skip to content

Commit

Permalink
Starting the armi.testing modules (#2028)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science authored Dec 29, 2024
1 parent bc1ec01 commit 66551d5
Show file tree
Hide file tree
Showing 23 changed files with 147 additions and 120 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 @@ -29,7 +29,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 ARMI_RUN_PATH, TEST_ROOT, mockRunLogs
from armi.utils.directoryChangers import TemporaryDirectoryChanger
from armi.utils.dynamicImporter import getEntireFamilyTree
Expand Down
7 changes: 1 addition & 6 deletions armi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ def setMode(cls, mode):


try:
# Check for MPI. The mpi4py module uses cPickle to serialize python objects in preparation for
# network transmission. Sometimes, when cPickle fails, it gives very cryptic error messages that
# do not help much. If you uncomment th following line, you can trick mpi4py into using the
# pure-python pickle module in place of cPickle and now you will generally get much more
# meaningful and useful error messages Then comment it back out because it's slow.
# import sys, pickle; sys.modules['cPickle'] = pickle
# Check for MPI
from mpi4py import MPI

MPI_COMM = MPI.COMM_WORLD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
CONF_DISABLE_BLOCK_TYPE_EXCLUSION_IN_XS_GENERATION,
CONF_XS_BLOCK_REPRESENTATION,
)
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 @@ -23,8 +23,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 @@ -22,7 +22,7 @@
ParameterSweepConverter,
SettingsModifier,
)
from armi.reactor.tests.test_reactors import loadTestReactor
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 @@ -23,7 +23,7 @@
from armi.reactor import blocks, geometry, grids
from armi.reactor.converters import geometryConverters, 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 geometryConverters, meshConverters
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 @@ -23,8 +23,8 @@
from armi.reactor.converters import uniformMesh
from armi.reactor.flags import Flags
from armi.reactor.tests import test_assemblies, test_blocks
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.settings.fwSettings.globalSettings import CONF_UNIFORM_MESH_MINIMUM_SIZE
from armi.testing import loadTestReactor, reduceTestReactorRings
from armi.tests import ISOAA_PATH, TEST_ROOT


Expand Down
4 changes: 2 additions & 2 deletions armi/reactor/tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
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 TEST_ROOT, loadTestReactor
from armi.tests import ISOAA_PATH
from armi.testing import loadTestReactor
from armi.tests import ISOAA_PATH, TEST_ROOT
from armi.utils import densityTools, hexagon, units
from armi.utils.directoryChangers import TemporaryDirectoryChanger
from armi.utils.units import (
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 @@ -48,7 +48,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 @@ -28,7 +28,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, 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 @@ -36,11 +36,11 @@
CONF_ASSEM_FLAGS_SKIP_AXIAL_EXP,
CONF_SORT_REACTOR,
)
from armi.tests import ARMI_RUN_PATH, TEST_ROOT, mockRunLogs
from armi.testing import loadTestReactor, reduceTestReactorRings # noqa: F401
from armi.tests import TEST_ROOT, mockRunLogs
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 @@ -125,95 +125,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 = pickle.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 = pickle.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 @@ -862,7 +773,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 66551d5

Please sign in to comment.