Skip to content

Commit

Permalink
Allow for loading from databases with extra parameters (#372)
Browse files Browse the repository at this point in the history
This allows loading from old databases when parameters have since been removed from the application. In such cases, a warning will be issued.
  • Loading branch information
youngmit committed Aug 17, 2021
1 parent 4e100dd commit 0a85899
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
30 changes: 24 additions & 6 deletions armi/bookkeeping/db/database3.py
Expand Up @@ -358,6 +358,7 @@ def loadState(
statePointName=timeStepName,
cs=self.cs,
bp=self.r.blueprints,
allowMissing=True,
)
break
else:
Expand Down Expand Up @@ -989,7 +990,9 @@ def syncToSharedFolder(self):
self.h5db.flush()
shutil.copy(self._fullPath, self._fileName)

def load(self, cycle, node, cs=None, bp=None, statePointName=None):
def load(
self, cycle, node, cs=None, bp=None, statePointName=None, allowMissing=False
):
"""Load a new reactor from (cycle, node).
Case settings, blueprints, and geom can be provided by the client, or read from
Expand All @@ -1008,6 +1011,11 @@ def load(self, cycle, node, cs=None, bp=None, statePointName=None):
if not provided one is read from the database
bp : armi.reactor.Blueprints (Optional)
if not provided one is read from the database
statePointName : str
Optional arbitrary statepoint name (e.g., "special" for "c00n00-special/")
allowMissing : bool
Whether to emit a warning, rather than crash if reading a database
with undefined parameters. Default False.
Returns
-------
Expand All @@ -1028,7 +1036,7 @@ def load(self, cycle, node, cs=None, bp=None, statePointName=None):

# populate data onto initialized components
for compType, compTypeList in groupedComps.items():
self._readParams(h5group, compType, compTypeList)
self._readParams(h5group, compType, compTypeList, allowMissing=allowMissing)

# assign params from blueprints
if bp is not None:
Expand Down Expand Up @@ -1246,7 +1254,7 @@ def _addHomogenizedNumberDensityParams(blocks, h5group):
h5group.create_dataset(nucName, data=numDens, compression="gzip")

@staticmethod
def _readParams(h5group, compTypeName, comps):
def _readParams(h5group, compTypeName, comps, allowMissing=False):
g = h5group[compTypeName]

renames = armi.getApp().getParamRenames()
Expand All @@ -1264,11 +1272,21 @@ def _readParams(h5group, compTypeName, comps):
pDef = pDefs[paramName]
except KeyError:
if re.match(r"^n[A-Z][a-z]?\d*", paramName):
# This is a temporary viz param made by _addHomogenizedNumberDensities
# ignore it safely
# This is a temporary viz param (number density) made by
# _addHomogenizedNumberDensityParams ignore it safely
continue
else:
raise
# If a parameter exists in the database but not in the application
# reading it, we can technically keep going. Since this may lead to
# potential correctness issues, raise a warning
if allowMissing:
runLog.warning(
"Found `{}` parameter `{}` in the database, which is not defined. "
"Ignoring it.".format(compTypeName, paramName)
)
continue
else:
raise

data = dataSet[:]
attrs = _resolveAttrs(dataSet.attrs, h5group)
Expand Down
13 changes: 13 additions & 0 deletions armi/bookkeeping/db/tests/test_database3.py
Expand Up @@ -101,6 +101,9 @@ def makeShuffleHistory(self):
self.r.p.cycleLength = cycle
self.r.core[0].p.chargeTime = 3

# add some fake missing parameter data to test allowMissing
self.db.h5db["c00n00/Reactor/missingParam"] = "i don't exist"

def _compareArrays(self, ref, src):
"""
Compare two numpy arrays.
Expand Down Expand Up @@ -194,6 +197,16 @@ def test_computeParents(self):
database3.Layout.computeAncestors(serialNums, numChildren, 3), expected_3
)

def test_load(self) -> None:
self.makeShuffleHistory()
with self.assertRaises(KeyError):
_r = self.db.load(0, 0)

_r = self.db.load(0, 0, allowMissing=True)

del self.db.h5db["c00n00/Reactor/missingParam"]
_r = self.db.load(0, 0, allowMissing=False)

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

Expand Down

0 comments on commit 0a85899

Please sign in to comment.