Skip to content

Commit

Permalink
Merge e9f18f2 into 07cc416
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Dec 16, 2022
2 parents 07cc416 + e9f18f2 commit 78a8a43
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 102 deletions.
38 changes: 23 additions & 15 deletions armi/bookkeeping/db/tests/test_database3.py
Expand Up @@ -24,7 +24,7 @@
from armi.bookkeeping.db import database3
from armi.bookkeeping.db.databaseInterface import DatabaseInterface
from armi.reactor import parameters
from armi.reactor.tests import test_reactors
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.tests import TEST_ROOT
from armi.utils import getPreviousTimeNode
from armi.utils.directoryChangers import TemporaryDirectoryChanger
Expand All @@ -36,9 +36,10 @@ class TestDatabase3(unittest.TestCase):
def setUp(self):
self.td = TemporaryDirectoryChanger()
self.td.__enter__()
self.o, self.r = test_reactors.loadTestReactor(
self.o, self.r = loadTestReactor(
TEST_ROOT, customSettings={"reloadDBName": "reloadingDB.h5"}
)
reduceTestReactorRings(self.r, self.o.cs, maxNumRings=3)

self.dbi = DatabaseInterface(self.r, self.o.cs)
self.dbi.initDB(fName=self._testMethodName + ".h5")
Expand Down Expand Up @@ -95,7 +96,7 @@ def test_getH5File(self):

def makeHistory(self):
"""Walk the reactor through a few time steps and write them to the db."""
for cycle, node in ((cycle, node) for cycle in range(3) for node in range(3)):
for cycle, node in ((cycle, node) for cycle in range(2) for node in range(2)):
self.r.p.cycle = cycle
self.r.p.timeNode = node
# something that splitDatabase won't change, so that we can make sure that
Expand Down Expand Up @@ -180,10 +181,11 @@ def test_prepRestartRun(self):
created here for this test.
"""
# first successfully call to prepRestartRun
o, r = test_reactors.loadTestReactor(
o, r = loadTestReactor(
TEST_ROOT, customSettings={"reloadDBName": "reloadingDB.h5"}
)
cs = o.cs
reduceTestReactorRings(r, cs, maxNumRings=3)

ratedPower = cs["power"]
startCycle = cs["startCycle"]
Expand All @@ -195,9 +197,11 @@ def test_prepRestartRun(self):
]
cycleP, nodeP = getPreviousTimeNode(startCycle, startNode, cs)
cyclesSetting[cycleP]["power fractions"][nodeP] = 0.5
numCycles = 2
numNodes = 2
cs = cs.modified(
newSettings={
"nCycles": 3,
"nCycles": numCycles,
"cycles": cyclesSetting,
"reloadDBName": "something_fake.h5",
}
Expand All @@ -209,7 +213,9 @@ def test_prepRestartRun(self):
db = dbi.database

# populate the db with some things
for cycle, node in ((cycle, node) for cycle in range(3) for node in range(2)):
for cycle, node in (
(cycle, node) for cycle in range(numCycles) for node in range(numNodes)
):
r.p.cycle = cycle
r.p.timeNode = node
r.p.cycleLength = sum(cyclesSetting[cycle]["step days"])
Expand Down Expand Up @@ -245,7 +251,9 @@ def test_prepRestartRun(self):
db = dbi.database

# populate the db with something
for cycle, node in ((cycle, node) for cycle in range(3) for node in range(2)):
for cycle, node in (
(cycle, node) for cycle in range(numCycles) for node in range(numNodes)
):
r.p.cycle = cycle
r.p.timeNode = node
r.p.cycleLength = 2000
Expand Down Expand Up @@ -368,12 +376,12 @@ def test_load_updateGlobalAssemNum(self):
# assemblies in blueprints/core.
r = self.db.load(0, 0, allowMissing=True, updateGlobalAssemNum=True)
expected = len(self.r.core) + len(self.r.blueprints.assemblies.values())
self.assertEqual(assemblies._assemNum, expected)
self.assertEqual(15, expected)

# repeat the test above to show that subsequent db loads (with updateGlobalAssemNum=True)
# do not continue to increase the global assem num.
self.db.load(0, 0, allowMissing=True, updateGlobalAssemNum=True)
self.assertEqual(assemblies._assemNum, expected)
self.assertEqual(15, expected)

def test_history(self):
self.makeShuffleHistory()
Expand Down Expand Up @@ -484,23 +492,23 @@ def test_mergeHistory(self):
self.assertTrue(numpy.array_equal(attrs["fakeBigData"], numpy.eye(6400)))

keys = sorted(db2.keys())
self.assertEqual(len(keys), 8)
self.assertEqual(keys[:3], ["/c00n00", "/c00n01", "/c00n02"])
self.assertEqual(len(keys), 4)
self.assertEqual(keys[:3], ["/c00n00", "/c00n01", "/c01n00"])

def test_splitDatabase(self):
self.makeHistory()

self.db.splitDatabase(
[(c, n) for c in (1, 2) for n in range(3)], "-all-iterations"
[(c, n) for c in (0, 1) for n in range(2)], "-all-iterations"
)

# Closing to copy back from fast path
self.db.close()

with h5py.File("test_splitDatabase.h5", "r") as newDb:
self.assertEqual(newDb["c00n00/Reactor/cycle"][()], 0)
self.assertEqual(newDb["c00n00/Reactor/cycleLength"][()], 1)
self.assertNotIn("c02n00", newDb)
self.assertEqual(newDb["c00n00/Reactor/cycleLength"][()][0], 0)
self.assertNotIn("c03n00", newDb)
self.assertEqual(newDb.attrs["databaseVersion"], database3.DB_VERSION)

# validate that the min set of meta data keys exists
Expand Down Expand Up @@ -530,7 +538,7 @@ def test_splitDatabase(self):
with self.assertRaises(ValueError):
self.db.h5db = None
self.db.splitDatabase(
[(c, n) for c in (1, 2) for n in range(3)], "-all-iterations"
[(c, n) for c in (0, 1) for n in range(2)], "-all-iterations"
)

def test_grabLocalCommitHash(self):
Expand Down
80 changes: 36 additions & 44 deletions armi/bookkeeping/db/tests/test_databaseInterface.py
Expand Up @@ -11,9 +11,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r""" Tests of the Database Interface
"""
# pylint: disable=missing-function-docstring,missing-class-docstring,abstract-method,protected-access
r""" Tests of the Database Interface"""
# pylint: disable=missing-function-docstring,missing-class-docstring,protected-access,invalid-name,no-method-argument,import-outside-toplevel
import os
import types
import unittest
Expand All @@ -31,7 +30,7 @@
from armi.cases import case
from armi.reactor import grids
from armi.reactor.flags import Flags
from armi.reactor.tests import test_reactors
from armi.reactor.tests.test_reactors import loadTestReactor, reduceTestReactorRings
from armi.settings.fwSettings.databaseSettings import CONF_FORCE_DB_PARAMS
from armi.tests import TEST_ROOT
from armi.utils import directoryChangers
Expand All @@ -52,10 +51,8 @@ def getSimpleDBOperator(cs):
newSettings["db"] = True
newSettings["runType"] = "Standard"
newSettings["geomFile"] = "geom1Assem.xml"
newSettings["nCycles"] = 2
newSettings[CONF_FORCE_DB_PARAMS] = [
"baseBu",
]
newSettings["nCycles"] = 1
newSettings[CONF_FORCE_DB_PARAMS] = ["baseBu"]
cs = cs.modified(newSettings=newSettings)
genDBCase = case.Case(cs)
settings.setMasterCs(cs)
Expand Down Expand Up @@ -89,7 +86,7 @@ class TestDatabaseInterface(unittest.TestCase):
def setUp(self):
self.td = directoryChangers.TemporaryDirectoryChanger()
self.td.__enter__()
self.o, self.r = test_reactors.loadTestReactor(TEST_ROOT)
self.o, self.r = loadTestReactor(TEST_ROOT)

self.dbi = DatabaseInterface(self.r, self.o.cs)
self.dbi.initDB(fName=self._testMethodName + ".h5")
Expand All @@ -103,7 +100,7 @@ def tearDown(self):

def test_interactEveryNodeReturn(self):
"""test that the DB is NOT written to if cs["numCoupledIterations"] != 0"""
self.o.cs["numCoupledIterations"] = 2
self.o.cs["numCoupledIterations"] = 1
self.dbi.interactEveryNode(0, 0)
self.assertFalse(self.dbi.database.hasTimeStep(0, 0))

Expand Down Expand Up @@ -150,7 +147,7 @@ def goodMethod(cycle, node): # pylint: disable=unused-argument
with self.o:
self.o.operate()

self.assertEqual(1, self.r.p.cycle)
self.assertEqual(0, self.r.p.cycle)
self.assertEqual(2, self.r.p.timeNode)

with h5py.File(self.o.cs.caseTitle + ".h5", "r") as h5:
Expand All @@ -162,15 +159,14 @@ def goodMethod(cycle, node): # pylint: disable=unused-argument
self.assertIn("startTime", h5.attrs)
self.assertIn("machines", h5.attrs)
self.assertIn("caseTitle", h5.attrs)

self.assertIn("geomFile", h5["inputs"])
self.assertIn("settings", h5["inputs"])
self.assertIn("blueprints", h5["inputs"])
self.assertIn("baseBu", h5["c01n02/HexBlock"])
self.assertIn("baseBu", h5["c00n02/HexBlock"])

def test_metaDataEndFail(self):
def failMethod(cycle, node): # pylint: disable=unused-argument
if cycle == 1 and node == 1:
if cycle == 0 and node == 1:
raise Exception("forcing failure")

self.o.interfaces.append(MockInterface(self.o.r, self.o.cs, failMethod))
Expand All @@ -179,7 +175,7 @@ def failMethod(cycle, node): # pylint: disable=unused-argument
with self.o:
self.o.operate()

self.assertEqual(1, self.r.p.cycle)
self.assertEqual(0, self.r.p.cycle)
self.assertEqual(1, self.r.p.timeNode)

with h5py.File(self.o.cs.caseTitle + ".h5", "r") as h5:
Expand All @@ -203,7 +199,7 @@ def setFluxAwesome(cycle, node): # pylint: disable=unused-argument
self.called = False

def getFluxAwesome(cycle, node): # pylint: disable=unused-argument
if cycle != 1 or node != 2:
if cycle != 0 or node != 2:
return

blocks = self.r.core.getBlocks()
Expand Down Expand Up @@ -261,9 +257,10 @@ def setUpClass(cls):
# than the original input file. This allows settings to be
# changed in memory like this and survive for testing.
newSettings = {"verbosity": "extra"}
newSettings["nCycles"] = 3
newSettings["burnSteps"] = 3
o, _r = test_reactors.loadTestReactor(customSettings=newSettings)
newSettings["nCycles"] = 2
newSettings["burnSteps"] = 2
o, r = loadTestReactor(customSettings=newSettings)
reduceTestReactorRings(r, o.cs, 3)

settings.setMasterCs(o.cs)

Expand Down Expand Up @@ -295,29 +292,34 @@ def tearDownClass(cls):
del cls.r
cls.r = None

def _fullCoreSizeChecker(self, r):
"""TODO"""
self.assertEqual(r.core.numRings, 3)
self.assertEqual(r.p.cycle, 0)
self.assertEqual(len(r.core.assembliesByName), 19)
self.assertEqual(len(r.core.circularRingList), 0)
self.assertEqual(len(r.core.blocksByName), 95)

def test_growToFullCore(self):
with Database3(self.dbName, "r") as db:
r = db.load(0, 0, allowMissing=True)

r.core.growToFullCore(None)

self.assertEqual(r.core.numRings, 9)
# test partial core values
self.assertEqual(r.core.numRings, 3)
self.assertEqual(r.p.cycle, 0)
self.assertEqual(len(r.core.assembliesByName), 217)
self.assertEqual(len(r.core.assembliesByName), 7)
self.assertEqual(len(r.core.circularRingList), 0)
self.assertEqual(len(r.core.blocksByName), 1085)
self.assertEqual(len(r.core.blocksByName), 35)

r.core.growToFullCore(None)
self._fullCoreSizeChecker(r)

def test_growToFullCoreWithCS(self):
with Database3(self.dbName, "r") as db:
r = db.load(0, 0, allowMissing=True)

r.core.growToFullCore(self.cs)

self.assertEqual(r.core.numRings, 9)
self.assertEqual(r.p.cycle, 0)
self.assertEqual(len(r.core.assembliesByName), 217)
self.assertEqual(len(r.core.circularRingList), 0)
self.assertEqual(len(r.core.blocksByName), 1085)
self._fullCoreSizeChecker(r)

def test_growToFullCoreFromFactory(self):
from armi.bookkeeping.db import databaseFactory
Expand All @@ -327,12 +329,7 @@ def test_growToFullCoreFromFactory(self):
r = db.load(0, 0, allowMissing=True)

r.core.growToFullCore(None)

self.assertEqual(r.core.numRings, 9)
self.assertEqual(r.p.cycle, 0)
self.assertEqual(len(r.core.assembliesByName), 217)
self.assertEqual(len(r.core.circularRingList), 0)
self.assertEqual(len(r.core.blocksByName), 1085)
self._fullCoreSizeChecker(r)

def test_growToFullCoreFromFactoryWithCS(self):
from armi.bookkeeping.db import databaseFactory
Expand All @@ -342,12 +339,7 @@ def test_growToFullCoreFromFactoryWithCS(self):
r = db.load(0, 0, allowMissing=True)

r.core.growToFullCore(self.cs)

self.assertEqual(r.core.numRings, 9)
self.assertEqual(r.p.cycle, 0)
self.assertEqual(len(r.core.assembliesByName), 217)
self.assertEqual(len(r.core.circularRingList), 0)
self.assertEqual(len(r.core.blocksByName), 1085)
self._fullCoreSizeChecker(r)

def test_readWritten(self):
with Database3(self.dbName, "r") as db:
Expand Down Expand Up @@ -403,7 +395,7 @@ def test_readWithoutInputs(self):

def test_variousTypesWork(self):
with Database3(self.dbName, "r") as db:
r2 = db.load(1, 3)
r2 = db.load(1, 1)

b1 = self.r.core.getFirstBlock(Flags.FUEL)
b2 = r2.core.getFirstBlock(Flags.FUEL)
Expand Down Expand Up @@ -490,7 +482,7 @@ def test_standardRestart(self):
newSettings = {}
newSettings["loadStyle"] = "fromDB"
newSettings["reloadDBName"] = loadDB
newSettings["startCycle"] = 1
newSettings["startCycle"] = 0
newSettings["startNode"] = 1
cs = cs.modified(newSettings=newSettings)
o = self._getOperatorThatChangesVariables(cs)
Expand Down
6 changes: 3 additions & 3 deletions armi/bookkeeping/tests/test_historyTracker.py
Expand Up @@ -74,6 +74,7 @@ def setUp(self):
cs = settings.Settings(f"{CASE_TITLE}.yaml")
newSettings = {}
newSettings["db"] = True
newSettings["nCycles"] = 2
newSettings["detailAssemLocationsBOL"] = ["001-001"]
newSettings["loadStyle"] = "fromDB"
newSettings["reloadDBName"] = pathlib.Path(f"{CASE_TITLE}.h5").absolute()
Expand Down Expand Up @@ -119,9 +120,8 @@ def test_calcMGFluence(self):

hti = o.getInterface("history")

timesInYears = [
duration or 1.0 for duration in hti.getTimeSteps()
] # duration is None in this DB
# duration is None in this DB
timesInYears = [duration or 1.0 for duration in hti.getTimeSteps()]
timeStepsToRead = [
utils.getCycleNodeFromCumulativeNode(i, self.o.cs)
for i in range(len(timesInYears))
Expand Down

0 comments on commit 78a8a43

Please sign in to comment.