Skip to content

Commit

Permalink
Switching form a Setting deepcopy to a copy (terrapower#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science authored Nov 12, 2021
1 parent a10a396 commit 47d3cc7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
26 changes: 13 additions & 13 deletions armi/bookkeeping/db/tests/test_database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def makeHistory(self):
self.db.writeToDB(self.r)

def makeShuffleHistory(self):
"""
Walk the reactor through a few time steps with some shuffling.
"""
"""Walk the reactor through a few time steps with some shuffling."""
# Serial numbers *are not stable* (i.e., they can be different between test runs
# due to parallelism and test run order). However, they are the simplest way to
# check correctness of location-based history tracking. So we stash the serial
Expand All @@ -76,7 +74,8 @@ def makeShuffleHistory(self):
self.centralTopBlockSerialNums = []

grid = self.r.core.spatialGrid
for cycle in range(3):

for cycle in range(2):
a1 = self.r.core.childrenByLocator[grid[cycle, 0, 0]]
a2 = self.r.core.childrenByLocator[grid[0, 0, 0]]
olda1Loc = a1.spatialLocator
Expand All @@ -86,20 +85,21 @@ def makeShuffleHistory(self):
self.centralAssemSerialNums.append(c.p.serialNum)
self.centralTopBlockSerialNums.append(c[-1].p.serialNum)

for node in range(3):
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 the right data went to the right new groups/cycles
self.r.p.cycleLength = cycle

self.db.writeToDB(self.r)

# add some more data that isnt written to the database to test the
# DatabaseInterface API
self.r.p.cycle = 3
self.r.p.cycle = 2
self.r.p.timeNode = 0
self.r.p.cycleLength = cycle
self.r.core[0].p.chargeTime = 3
self.r.core[0].p.chargeTime = 2

# add some fake missing parameter data to test allowMissing
self.db.h5db["c00n00/Reactor/missingParam"] = "i don't exist"
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_load(self) -> None:
del self.db.h5db["c00n00/Reactor/missingParam"]
_r = self.db.load(0, 0, allowMissing=False)

def test_history(self) -> None:
def test_history(self):
self.makeShuffleHistory()

grid = self.r.core.spatialGrid
Expand All @@ -219,15 +219,15 @@ def test_history(self) -> None:
testAssem, params=["chargeTime", "serialNum"]
)
expectedSn = {
(c, n): self.centralAssemSerialNums[c] for c in range(3) for n in range(3)
(c, n): self.centralAssemSerialNums[c] for c in range(2) for n in range(2)
}
self.assertEqual(expectedSn, hist["serialNum"])

# test block
hists = self.db.getHistoriesByLocation(
[testBlock], params=["serialNum"], timeSteps=[(0, 0), (1, 0), (2, 0)]
[testBlock], params=["serialNum"], timeSteps=[(0, 0), (1, 0)]
)
expectedSn = {(c, 0): self.centralTopBlockSerialNums[c] for c in range(3)}
expectedSn = {(c, 0): self.centralTopBlockSerialNums[c] for c in range(2)}
self.assertEqual(expectedSn, hists[testBlock]["serialNum"])

# cant mix blocks and assems, since they are different distance from core
Expand All @@ -238,8 +238,8 @@ def test_history(self) -> None:
hist = self.dbi.getHistory(
self.r.core[0], params=["chargeTime", "serialNum"], byLocation=True
)
self.assertIn((3, 0), hist["chargeTime"].keys())
self.assertEqual(hist["chargeTime"][(3, 0)], 3)
self.assertIn((2, 0), hist["chargeTime"].keys())
self.assertEqual(hist["chargeTime"][(2, 0)], 2)

def test_replaceNones(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion armi/settings/caseSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def getSetting(self, key, default=None):
NOTE: This is used very rarely, try to organize your code to only need a Setting value.
"""
if key in self.__settings:
return deepcopy(self.__settings[key])
return copy(self.__settings[key])
elif default is not None:
return default
else:
Expand Down
19 changes: 19 additions & 0 deletions armi/settings/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ def test_modified(self):
cs3 = cs2.modified(newSettings={"numberofGenericParams": 7})
cs4 = cs3.modified(newSettings={"somethingElse": 123})

def test_copySetting(self):
"""Ensure that when we copy a Setting() object, the result is sound.
NOTE: In particuar, self.schema and self._customSchema on a Setting object are
removed by Setting.__getstate__, and that has been a problem in the past.
"""
# get a baseline: show how the Setting object looks to start
s1 = setting.Setting("testCopy", 765)
self.assertEquals(s1.name, "testCopy")
self.assertEquals(s1._value, 765)
self.assertTrue(hasattr(s1, "schema"))
self.assertTrue(hasattr(s1, "_customSchema"))

# show that copy(Setting) is working correctly
s2 = copy.copy(s1)
self.assertEquals(s2._value, 765)
self.assertEquals(s2.name, "testCopy")
self.assertTrue(hasattr(s2, "schema"))
self.assertTrue(hasattr(s2, "_customSchema"))


class TestSettingsConversion(unittest.TestCase):
"""Make sure we can convert from old XML type settings to new Yaml settings."""
Expand Down

0 comments on commit 47d3cc7

Please sign in to comment.