Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions openmc/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def __init__(
# The single instance of Macroscopic data present in this material
# (only one is allowed, hence this is different than _nuclides, etc)
self._macroscopic = None

self._cross_sections = None

# If specified, a list of table names
self._sab = []
Expand Down Expand Up @@ -208,6 +210,15 @@ def __repr__(self) -> str:

return string

@property
def cross_sections(self) -> Path | None:
return self._cross_sections

@cross_sections.setter
def cross_sections(self, cross_sections):
if cross_sections is not None:
self._cross_sections = input_path(cross_sections)

@property
def name(self) -> str | None:
return self._name
Expand Down Expand Up @@ -1918,6 +1929,30 @@ def cross_sections(self) -> Path | None:
def cross_sections(self, cross_sections):
if cross_sections is not None:
self._cross_sections = input_path(cross_sections)
for mat in self:
mat.cross_sections = self.cross_sections

def _setup_cross_sections(self, material):
"""Copy cross_sections to material if exists
and copy cross_sections from material if not exists

Parameters
----------
material : openmc.Material


"""
if self.cross_sections is not None:
if material.cross_sections is not None:
if material.cross_sections != self.cross_sections:
warn("Material {material.id} cross_sections value has been overriden.")
material.cross_sections = self.cross_sections
elif material.cross_sections is not None:
self.cross_sections = material.cross_sections

def __setitem__(self, index: int, material):
self._setup_cross_sections(material)
super().__setitem__(index, material)

def append(self, material):
"""Append material to collection
Expand All @@ -1928,6 +1963,7 @@ def append(self, material):
Material to append

"""
self._setup_cross_sections(material)
super().append(material)

def insert(self, index: int, material):
Expand All @@ -1941,6 +1977,7 @@ def insert(self, index: int, material):
Material to insert

"""
self._setup_cross_sections(material)
super().insert(index, material)

def make_isotropic_in_lab(self):
Expand Down
11 changes: 11 additions & 0 deletions openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,17 @@ def plot(
x_max = (origin[x] + 0.5*width[0]) * axis_scaling_factor[axis_units]
y_min = (origin[y] - 0.5*width[1]) * axis_scaling_factor[axis_units]
y_max = (origin[y] + 0.5*width[1]) * axis_scaling_factor[axis_units]

# Determine whether any materials contains macroscopic data and if so,
# set energy mode accordingly
for mat in self.geometry.get_all_materials().values():
if mat._macroscopic is not None:
self.settings.energy_mode = 'multi-group'
break

# Convert cross_section path to absolute
if self.materials.cross_sections is not None:
self.materials.cross_sections = Path(self.materials.cross_sections).resolve()

# Get ID map from the C API
id_map = self.id_map(
Expand Down
1 change: 1 addition & 0 deletions openmc/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def plot(self, *args, **kwargs):
"""
model = openmc.Model()
model.geometry = openmc.Geometry(self)
model.materials = openmc.Materials(self.get_all_materials().values())
return model.plot(*args, **kwargs)

def get_nuclides(self):
Expand Down
46 changes: 46 additions & 0 deletions tests/unit_tests/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,46 @@
from tests.unit_tests import assert_unbounded


@pytest.fixture
def mg_lib():
groups = openmc.mgxs.EnergyGroups(group_edges=[1e-5, 20.0e6])
h2o_xsdata = openmc.XSdata('LWTR', groups)
h2o_xsdata.order = 0
h2o_xsdata.set_total([1.0])
h2o_xsdata.set_absorption([0.5])
scatter_matrix = np.array([[[0.5]]])
scatter_matrix = np.rollaxis(scatter_matrix, 0, 3)
h2o_xsdata.set_scatter_matrix(scatter_matrix)
mg_cross_sections_file = openmc.MGXSLibrary(groups)
mg_cross_sections_file.add_xsdatas([h2o_xsdata])
mg_cross_sections_file.export_to_hdf5()
return "mgxs.h5"


@pytest.fixture
def mg_model(mg_lib):
model = openmc.Model()

# Create materials for the problem
h2o_data = openmc.Macroscopic('LWTR')

water = openmc.Material(name='Water')
water.set_density('macro', 1.0)
water.add_macroscopic(h2o_data)

# Instantiate a Materials collection and export to XML
model.materials = openmc.Materials([water])
model.materials.cross_sections = mg_lib
H = 1.0
L = 100

sph00 = openmc.Sphere(r=10, boundary_type = "vacuum")
cell00 = openmc.Cell(region = -sph00 , fill = water)
univ = openmc.Universe(cells = [cell00], universe_id = 1)
model.geometry = openmc.Geometry(univ)
return model


def test_basic():
c1 = openmc.Cell()
c2 = openmc.Cell()
Expand Down Expand Up @@ -102,6 +142,12 @@ def test_plot(run_in_tmpdir, sphere_model):
# Close plots to avoid warning
import matplotlib.pyplot as plt
plt.close('all')


def test_mg_plot(mg_model):
univ = mg_model.geometry.root_universe
univ.plot(width=(200, 200), basis='yz', color_by='cell')
univ.plot(width=(200, 200), basis='yz', color_by='material')


def test_get_nuclides(uo2):
Expand Down
Loading