Skip to content

Commit

Permalink
Improving Type Hinting (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Jan 20, 2022
1 parent db26b9c commit c6acb9d
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 77 deletions.
2 changes: 1 addition & 1 deletion armi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def configure(app: Optional[apps.App] = None, permissive=False):
flags.registerPluginFlags(pm)


def applyAsyncioWindowsWorkaround():
def applyAsyncioWindowsWorkaround() -> None:
"""
Apply Asyncio workaround for Windows and Python 3.8.
Expand Down
10 changes: 5 additions & 5 deletions armi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def setMode(cls, mode):
"""Flag indicating whether or not the FAST_PATH should be cleaned up on exit."""


def activateLocalFastPath():
def activateLocalFastPath() -> None:
"""
Specify a local temp directory to be the fast path.
Expand Down Expand Up @@ -187,7 +187,7 @@ def activateLocalFastPath():
_FAST_PATH_IS_TEMPORARY = True


def getFastPath():
def getFastPath() -> str:
"""
Callable to get the current FAST_PATH.
Expand Down Expand Up @@ -245,7 +245,7 @@ def cleanTempDirs(olderThanDays=None):
cleanAllArmiTempDirs(olderThanDays)


def cleanAllArmiTempDirs(olderThanDays: int):
def cleanAllArmiTempDirs(olderThanDays: int) -> None:
"""
Delete all ARMI-related files from other unrelated runs after `olderThanDays` days (in
case this failed on earlier runs).
Expand Down Expand Up @@ -277,7 +277,7 @@ def cleanAllArmiTempDirs(olderThanDays: int):
pass


def disconnectAllHdfDBs():
def disconnectAllHdfDBs() -> None:
"""
Forcibly disconnect all instances of HdfDB objects
Expand Down Expand Up @@ -305,7 +305,7 @@ def disconnectAllHdfDBs():
OS_SECONDS_TIMEOUT = 5 * 60


def createLogDir(mpiRank=1, logDir=None):
def createLogDir(mpiRank: int = 1, logDir: str = None) -> None:
"""A helper method to create the log directory"""
# the usual case is the user does not pass in a log dir path, so we use the global one
if logDir is None:
Expand Down
10 changes: 5 additions & 5 deletions armi/materials/b4c.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def applyInputParams(
if TD_frac is not None:
self.updateTD(TD_frac)

def updateTD(self, TD):
def updateTD(self, TD: float) -> None:
self.p.theoreticalDensityFrac = TD
self.clearCache()

Expand Down Expand Up @@ -112,7 +112,7 @@ def setNewMassFracsFromMassEnrich(self, massEnrichment):

return boron10MassGrams, boron11MassGrams, carbonMassGrams

def setDefaultMassFracs(self):
def setDefaultMassFracs(self) -> None:
r"""B4C mass fractions. Using Natural B4C. 19.9% B-10/ 80.1% B-11
Boron: 10.811 g/mol
Carbon: 12.0107 g/mol
Expand Down Expand Up @@ -143,7 +143,7 @@ def setDefaultMassFracs(self):
)

@staticmethod
def getMassEnrichmentFromNumEnrich(naturalB10NumberFraction):
def getMassEnrichmentFromNumEnrich(naturalB10NumberFraction: float) -> float:
b10AtomicMass = nuclideBases.byName["B10"].weight
b11AtomicMass = nuclideBases.byName["B11"].weight
return (
Expand All @@ -155,7 +155,7 @@ def getMassEnrichmentFromNumEnrich(naturalB10NumberFraction):
)
)

def density(self, Tk=None, Tc=None):
def density(self, Tk: float = None, Tc: float = None) -> float:
"""
mass density
"""
Expand All @@ -170,7 +170,7 @@ def density(self, Tk=None, Tc=None):
)
return density * theoreticalDensityFrac # g/cc

def linearExpansionPercent(self, Tk=None, Tc=None):
def linearExpansionPercent(self, Tk: float = None, Tc: float = None) -> float:
"""Boron carbide expansion. Very preliminary"""
Tc = getTc(Tc, Tk)
self.checkTempRange(25, 500, Tc, "linear expansion percent")
Expand Down
66 changes: 39 additions & 27 deletions armi/materials/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def duplicate(self):

return m

def linearExpansion(self, Tk=None, Tc=None):
def linearExpansion(self, Tk: float = None, Tc: float = None) -> float:
r"""
the instantaneous linear expansion coefficient (dL/L)/dT
Expand All @@ -112,7 +112,7 @@ def linearExpansion(self, Tk=None, Tc=None):
f"{self} does not have a linear expansion property defined"
)

def linearExpansionPercent(self, Tk=None, Tc=None):
def linearExpansionPercent(self, Tk: float = None, Tc: float = None) -> float:
"""
Average thermal expansion dL/L. Used for computing hot dimensions and density.
Expand All @@ -136,7 +136,7 @@ def linearExpansionPercent(self, Tk=None, Tc=None):
"""
return 0.0

def linearExpansionFactor(self, Tc, T0):
def linearExpansionFactor(self, Tc: float, T0: float) -> float:
"""
Return a dL/L factor relative to T0 instead of to the material-dependent reference
temperature. This factor dL/Lc is a ratio and will be used in dimensions through the
Expand Down Expand Up @@ -174,7 +174,9 @@ def linearExpansionFactor(self, Tc, T0):

return (dLLhot - dLLcold) / (100.0 + dLLcold)

def getThermalExpansionDensityReduction(self, prevTempInC, newTempInC):
def getThermalExpansionDensityReduction(
self, prevTempInC: float, newTempInC: float
) -> float:
"""
Return the factor required to update thermal expansion going from temperatureInC to temperatureInCNew.
"""
Expand All @@ -185,14 +187,14 @@ def setDefaultMassFracs(self):
r"""mass fractions"""
pass

def setMassFrac(self, nucName, massFrac):
def setMassFrac(self, nucName: str, massFrac: float) -> None:
self.p.massFrac[nucName] = massFrac

def applyInputParams(self):
"""Apply material-specific material input parameters."""
pass

def adjustMassEnrichment(self, massEnrichment):
def adjustMassEnrichment(self, massEnrichment: float) -> None:
"""
Adjust the enrichment of the material.
Expand All @@ -202,7 +204,7 @@ def adjustMassEnrichment(self, massEnrichment):
"""
self.adjustMassFrac(self.enrichedNuclide, massEnrichment)

def adjustMassFrac(self, nuclideName, massFraction):
def adjustMassFrac(self, nuclideName: str, massFraction: float) -> None:
"""
Change the mass fraction of the specified nuclide.
Expand Down Expand Up @@ -312,7 +314,9 @@ def adjustMassFrac(self, nuclideName, massFraction):
def volumetricExpansion(self, Tk=None, Tc=None):
pass

def getTemperatureAtDensity(self, targetDensity, temperatureGuessInC):
def getTemperatureAtDensity(
self, targetDensity: float, temperatureGuessInC: float
) -> float:
"""Get the temperature at which the perturbed density occurs."""
densFunc = (
lambda temp: self.density(Tc=temp) - targetDensity
Expand All @@ -323,14 +327,14 @@ def getTemperatureAtDensity(self, targetDensity, temperatureGuessInC):
return tAtTargetDensity

@property
def liquidPorosity(self):
def liquidPorosity(self) -> float:
return 0.0 if self.parent is None else self.parent.liquidPorosity

@property
def gasPorosity(self):
def gasPorosity(self) -> float:
return 0.0 if self.parent is None else self.parent.gasPorosity

def density(self, Tk=None, Tc=None):
def density(self, Tk: float = None, Tc: float = None) -> float:
"""
Return density that preserves mass when thermally expanded in 2D.
Expand Down Expand Up @@ -365,7 +369,7 @@ def density(self, Tk=None, Tc=None):
# rho = rho + dRho = (1 + dRho/rho) * rho
return self.p.refDens / f # g/cm^3

def densityKgM3(self, Tk=None, Tc=None):
def densityKgM3(self, Tk: float = None, Tc: float = None) -> float:
"""
Return density that preserves mass when thermally expanded in 2D in units of kg/m^3
Expand All @@ -376,7 +380,7 @@ def densityKgM3(self, Tk=None, Tc=None):
"""
return self.density(Tk, Tc) * 1000.0

def density3(self, Tk=None, Tc=None):
def density3(self, Tk: float = None, Tc: float = None) -> float:
"""
Return density that preserves mass when thermally expanded in 3D.
Expand All @@ -402,7 +406,7 @@ def density3(self, Tk=None, Tc=None):
dRhoOverRho = (1.0 - f) / f
return refD * (dRhoOverRho + 1)

def density3KgM3(self, Tk=None, Tc=None):
def density3KgM3(self, Tk: float = None, Tc: float = None) -> float:
"""Return density that preserves mass when thermally expanded in 3D in units of kg/m^3.
See Also
Expand All @@ -412,45 +416,49 @@ def density3KgM3(self, Tk=None, Tc=None):
"""
return self.density3(Tk, Tc) * 1000.0

def getCorrosionRate(self, Tk=None, Tc=None):
def getCorrosionRate(self, Tk: float = None, Tc: float = None) -> float:
r"""
given a temperature, get the corrosion rate of the material
"""
return 0.0

def getLifeMetalCorrelation(self, days, Tk):
def getLifeMetalCorrelation(self, days: float, Tk: float) -> float:
r"""
life-metal correlation calculates the wastage of the material due to fission products.
"""
return 0.0

def getReverseLifeMetalCorrelation(self, thicknessFCCIWastageMicrons, Tk):
def getReverseLifeMetalCorrelation(
self, thicknessFCCIWastageMicrons: float, Tk: float
) -> float:
r"""
Life metal correlation reverse lookup. Knowing wastage and Temperature
determine the effective time at that temperature.
"""
return 0.0

def getLifeMetalConservativeFcciCoeff(self, Tk):
def getLifeMetalConservativeFcciCoeff(self, Tk: float) -> float:
"""
Return the coefficient to be used in the LIFE-METAL correlation
"""

return 0.0

def yieldStrength(self, Tk=None, Tc=None):
def yieldStrength(self, Tk: float = None, Tc: float = None) -> float:
r"""
returns yield strength at given T in MPa
"""
return self.p.yieldStrength

def thermalConductivity(self, Tk=None, Tc=None):
def thermalConductivity(self, Tk: float = None, Tc: float = None) -> float:
r"""
thermal conductivity in given T in K
"""
return self.p.thermalConductivity

def getProperty(self, propName, Tk=None, Tc=None, **kwargs):
def getProperty(
self, propName: str, Tk: float = None, Tc: float = None, **kwargs
) -> float:
r"""gets properties in a way that caches them."""
Tk = getTk(Tc, Tk)

Expand Down Expand Up @@ -511,20 +519,20 @@ def getMassFrac(
"""
return self.p.massFrac.get(nucName, 0.0)

def clearMassFrac(self):
def clearMassFrac(self) -> None:
r"""zero out all nuclide mass fractions."""
self.p.massFrac.clear()
self.p.massFracNorm = 0.0

def removeNucMassFrac(self, nuc):
def removeNucMassFrac(self, nuc: str) -> None:
self.setMassFrac(nuc, 0)
try:
del self.p.massFrac[nuc]
except KeyError:
# the nuc isn't in the mass Frac vector
pass

def removeLumpedFissionProducts(self):
def removeLumpedFissionProducts(self) -> None:
for nuc in self.getNuclides():
if "LF" in nuc:
# this component has a lumped fission product to remove
Expand Down Expand Up @@ -565,7 +573,7 @@ def checkTempRange(self, minV, maxV, val, label=""):
label="T out of bounds for {} {}".format(self.name, label),
)

def densityTimesHeatCapacity(self, Tk=None, Tc=None):
def densityTimesHeatCapacity(self, Tk: float = None, Tc: float = None) -> float:
r"""
Return heat capacity * density at a temperature
Parameters
Expand All @@ -592,7 +600,9 @@ def getNuclides(self):
warnings.warn("Material.getNuclides is being deprecated.", DeprecationWarning)
return self.parent.getNuclides()

def getTempChangeForDensityChange(self, Tc, densityFrac, quiet=True):
def getTempChangeForDensityChange(
self, Tc: float, densityFrac: float, quiet: bool = True
) -> float:
"""Return a temperature difference for a given density perturbation."""
linearExpansion = self.linearExpansion(Tc=Tc)
linearChange = densityFrac ** (-1.0 / 3.0) - 1.0
Expand Down Expand Up @@ -637,7 +647,9 @@ def linearExpansion(self, Tk=None, Tc=None):
since it is a liquid it will fill its space."""
return 0.0

def getTempChangeForDensityChange(self, Tc, densityFrac, quiet=True):
def getTempChangeForDensityChange(
self, Tc: float, densityFrac: float, quiet: bool = True
) -> float:
"""Return a temperature difference for a given density perturbation."""
currentDensity = self.density(Tc=Tc)
perturbedDensity = currentDensity * densityFrac
Expand Down
2 changes: 1 addition & 1 deletion armi/materials/uranium.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Uranium(Material):

propertyValidTemperature = {"thermal conductivity": ((255.4, 1173.2), "K")}

def thermalConductivity(self, Tk=None, Tc=None):
def thermalConductivity(self, Tk: float = None, Tc: float = None) -> float:
"""The thermal conductivity of pure U in W-m/K."""
Tk = getTk(Tc, Tk)
(TLowerLimit, TUpperLimit) = self.propertyValidTemperature[
Expand Down
Loading

0 comments on commit c6acb9d

Please sign in to comment.