Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 37 additions & 36 deletions openmc/material.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from collections import OrderedDict, defaultdict, namedtuple, Counter
from collections.abc import Iterable
from copy import deepcopy
Expand All @@ -6,7 +7,7 @@
import re
import typing # imported separately as py3.8 requires typing.Iterable
import warnings
from typing import Optional
from typing import Optional, List, Union
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
from xml.etree import ElementTree as ET

import h5py
Expand Down Expand Up @@ -134,7 +135,7 @@ def __init__(self, material_id=None, name='', temperature=None):
# If specified, a list of table names
self._sab = []

def __repr__(self):
def __repr__(self) -> str:
string = 'Material\n'
string += '{: <16}=\t{}\n'.format('\tID', self._id)
string += '{: <16}=\t{}\n'.format('\tName', self._name)
Expand Down Expand Up @@ -166,50 +167,50 @@ def __repr__(self):
return string

@property
def name(self):
def name(self) -> Optional[str]:
return self._name

@property
def temperature(self):
def temperature(self) -> Optional[float]:
return self._temperature

@property
def density(self):
def density(self) -> float:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
return self._density

@property
def density_units(self):
def density_units(self) -> str:
return self._density_units

@property
def depletable(self):
def depletable(self) -> bool:
return self._depletable

@property
def paths(self):
def paths(self) -> List[str]:
if self._paths is None:
raise ValueError('Material instance paths have not been determined. '
'Call the Geometry.determine_paths() method.')
return self._paths

@property
def num_instances(self):
def num_instances(self) -> int:
if self._num_instances is None:
raise ValueError(
'Number of material instances have not been determined. Call '
'the Geometry.determine_paths() method.')
return self._num_instances

@property
def nuclides(self):
def nuclides(self) -> List[namedtuple]:
return self._nuclides

@property
def isotropic(self):
def isotropic(self) -> List[str]:
return self._isotropic

@property
def average_molar_mass(self):
def average_molar_mass(self) -> float:
# Using the sum of specified atomic or weight amounts as a basis, sum
# the mass and moles of the material
mass = 0.
Expand All @@ -226,11 +227,11 @@ def average_molar_mass(self):
return mass / moles

@property
def volume(self):
def volume(self) -> float:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
return self._volume

@property
def ncrystal_cfg(self):
def ncrystal_cfg(self) -> str:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
return self._ncrystal_cfg

@name.setter
Expand Down Expand Up @@ -267,7 +268,7 @@ def isotropic(self, isotropic: typing.Iterable[str]):
self._isotropic = list(isotropic)

@property
def fissionable_mass(self):
def fissionable_mass(self) -> float:
if self.volume is None:
raise ValueError("Volume must be set in order to determine mass.")
density = 0.0
Expand All @@ -291,7 +292,7 @@ def decay_photon_energy(self) -> Optional[Univariate]:
return openmc.data.combine_distributions(dists, probs) if dists else None

@classmethod
def from_hdf5(cls, group: h5py.Group):
def from_hdf5(cls, group: h5py.Group) -> openmc.Material:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Create material from HDF5 group

Parameters
Expand Down Expand Up @@ -346,7 +347,7 @@ def from_hdf5(cls, group: h5py.Group):
return material

@classmethod
def from_ncrystal(cls, cfg, **kwargs):
def from_ncrystal(cls, cfg, **kwargs) -> openmc.Material:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Create material from NCrystal configuration string.

Density, temperature, and material composition, and (ultimately) thermal
Expand Down Expand Up @@ -881,7 +882,7 @@ def add_s_alpha_beta(self, name: str, fraction: float = 1.0):
def make_isotropic_in_lab(self):
self.isotropic = [x.name for x in self._nuclides]

def get_elements(self):
def get_elements(self) -> List[str]:
"""Returns all elements in the material

.. versionadded:: 0.12
Expand All @@ -895,7 +896,7 @@ def get_elements(self):

return sorted({re.split(r'(\d+)', i)[0] for i in self.get_nuclides()})

def get_nuclides(self, element: Optional[str] = None):
def get_nuclides(self, element: Optional[str] = None) -> List[str]:
"""Returns a list of all nuclides in the material, if the element
argument is specified then just nuclides of that element are returned.

Expand Down Expand Up @@ -925,7 +926,7 @@ def get_nuclides(self, element: Optional[str] = None):

return matching_nuclides

def get_nuclide_densities(self):
def get_nuclide_densities(self) -> dict:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Returns all nuclides in the material and their densities

Returns
Expand All @@ -944,7 +945,7 @@ def get_nuclide_densities(self):

return nuclides

def get_nuclide_atom_densities(self, nuclide: Optional[str] = None):
def get_nuclide_atom_densities(self, nuclide: Optional[str] = None) -> dict:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Returns one or all nuclides in the material and their atomic
densities in units of atom/b-cm

Expand Down Expand Up @@ -1030,7 +1031,7 @@ def get_nuclide_atom_densities(self, nuclide: Optional[str] = None):
return nuclides

def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False,
volume: Optional[float] = None):
volume: Optional[float] = None) -> Union[dict, float]:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Returns the activity of the material or for each nuclide in the
material in units of [Bq], [Bq/g] or [Bq/cm3].

Expand Down Expand Up @@ -1077,7 +1078,7 @@ def get_activity(self, units: str = 'Bq/cm3', by_nuclide: bool = False,
return activity if by_nuclide else sum(activity.values())

def get_decay_heat(self, units: str = 'W', by_nuclide: bool = False,
volume: Optional[float] = None):
volume: Optional[float] = None) -> Union[dict, float]:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Returns the decay heat of the material or for each nuclide in the
material in units of [W], [W/g] or [W/cm3].

Expand Down Expand Up @@ -1125,7 +1126,7 @@ def get_decay_heat(self, units: str = 'W', by_nuclide: bool = False,

return decayheat if by_nuclide else sum(decayheat.values())

def get_nuclide_atoms(self, volume: Optional[float] = None):
def get_nuclide_atoms(self, volume: Optional[float] = None) -> dict:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
"""Return number of atoms of each nuclide in the material

.. versionadded:: 0.13.1
Expand Down Expand Up @@ -1154,7 +1155,7 @@ def get_nuclide_atoms(self, volume: Optional[float] = None):
atoms[nuclide] = 1.0e24 * atom_per_bcm * volume
return atoms

def get_mass_density(self, nuclide: Optional[str] = None):
def get_mass_density(self, nuclide: Optional[str] = None) -> float:
"""Return mass density of one or all nuclides

Parameters
Expand All @@ -1176,7 +1177,7 @@ def get_mass_density(self, nuclide: Optional[str] = None):
mass_density += density_i
return mass_density

def get_mass(self, nuclide: Optional[str] = None, volume: Optional[float] = None):
def get_mass(self, nuclide: Optional[str] = None, volume: Optional[float] = None) -> float:
"""Return mass of one or all nuclides.

Note that this method requires that the :attr:`Material.volume` has
Expand Down Expand Up @@ -1206,7 +1207,7 @@ def get_mass(self, nuclide: Optional[str] = None, volume: Optional[float] = None
raise ValueError("Volume must be set in order to determine mass.")
return volume*self.get_mass_density(nuclide)

def clone(self, memo: Optional[dict] = None):
def clone(self, memo: Optional[dict] = None) -> Material:
"""Create a copy of this material with a new unique ID.

Parameters
Expand Down Expand Up @@ -1245,7 +1246,7 @@ def clone(self, memo: Optional[dict] = None):

return memo[self]

def _get_nuclide_xml(self, nuclide: str):
def _get_nuclide_xml(self, nuclide: str) -> ET.Element:
xml_element = ET.Element("nuclide")
xml_element.set("name", nuclide.name)

Expand All @@ -1256,19 +1257,19 @@ def _get_nuclide_xml(self, nuclide: str):

return xml_element

def _get_macroscopic_xml(self, macroscopic: str):
def _get_macroscopic_xml(self, macroscopic: str) -> ET.Element:
xml_element = ET.Element("macroscopic")
xml_element.set("name", macroscopic)

return xml_element

def _get_nuclides_xml(self, nuclides: typing.Iterable[str]):
def _get_nuclides_xml(self, nuclides: typing.Iterable[str]) -> List[ET.Element]:
xml_elements = []
for nuclide in nuclides:
xml_elements.append(self._get_nuclide_xml(nuclide))
return xml_elements

def to_xml_element(self):
def to_xml_element(self) -> ET.Element:
"""Return XML representation of the material

Returns
Expand Down Expand Up @@ -1338,7 +1339,7 @@ def to_xml_element(self):

@classmethod
def mix_materials(cls, materials, fracs: typing.Iterable[float],
percent_type: str = 'ao', name: Optional[str] = None):
percent_type: str = 'ao', name: Optional[str] = None) -> Material:
"""Mix materials together based on atom, weight, or volume fractions

.. versionadded:: 0.12
Expand Down Expand Up @@ -1436,7 +1437,7 @@ def mix_materials(cls, materials, fracs: typing.Iterable[float],
return new_mat

@classmethod
def from_xml_element(cls, elem: ET.Element):
def from_xml_element(cls, elem: ET.Element) -> Material:
"""Generate material from an XML element

Parameters
Expand Down Expand Up @@ -1530,7 +1531,7 @@ def __init__(self, materials=None):
self += materials

@property
def cross_sections(self):
def cross_sections(self) -> Optional[Union[str, Path]]:
Comment thread
christinacai123 marked this conversation as resolved.
Resolved
return self._cross_sections

@cross_sections.setter
Expand Down Expand Up @@ -1638,7 +1639,7 @@ def export_to_xml(self, path: PathLike = 'materials.xml'):
self._write_xml(fh)

@classmethod
def from_xml_element(cls, elem):
def from_xml_element(cls, elem) -> Material:
"""Generate materials collection from XML file

Parameters
Expand All @@ -1665,7 +1666,7 @@ def from_xml_element(cls, elem):
return materials

@classmethod
def from_xml(cls, path: PathLike = 'materials.xml'):
def from_xml(cls, path: PathLike = 'materials.xml') -> Material:
"""Generate materials collection from XML file

Parameters
Expand Down