diff --git a/examples/segment_rectange_with_zplane_xplane_yplane.py b/examples/segment_rectange_with_zplane_xplane_yplane.py index 57813a2..e6460ef 100644 --- a/examples/segment_rectange_with_zplane_xplane_yplane.py +++ b/examples/segment_rectange_with_zplane_xplane_yplane.py @@ -1,35 +1,28 @@ - import openmc from openmc_cell_segmenter import segment # example surfaces surface_1 = openmc.model.RectangularParallelepiped( - xmin=20, - xmax=200, - ymin=40, - ymax=400, - zmin=60, - zmax=600, - boundary_type='vacuum' + xmin=20, xmax=200, ymin=40, ymax=400, zmin=60, zmax=600, boundary_type="vacuum" ) # above (+) inner_sphere_surface and below (-) outer_sphere_surface region_1 = -surface_1 -# region_1 = +inner_sphere_surface & -outer_sphere_surface +# region_1 = +inner_sphere_surface & -outer_sphere_surface # example cell cell = openmc.Cell(region=region_1) -cells = segment(cell, [10,10, 10], ['zplane', 'xplane', 'yplane']) +cells = segment(cell, [10, 10, 10], ["zplane", "xplane", "yplane"]) # cells = segment_cell_with_surfaces(cell, [10], ['zcylinder']) geometry = openmc.Geometry(cells) -plot = geometry.plot(basis='xz',outline=True) -plot.figure.savefig('seg_rectangle_with_zplane_xplane_yplane_xz.png') +plot = geometry.plot(basis="xz", outline=True) +plot.figure.savefig("seg_rectangle_with_zplane_xplane_yplane_xz.png") -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('seg_rectangle_with_zplane_xplane_yplane_xy.png') +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("seg_rectangle_with_zplane_xplane_yplane_xy.png") -plot = geometry.plot(basis='yz',outline=True) -plot.figure.savefig('seg_rectangle_with_zplane_xplane_yplane_yz.png') +plot = geometry.plot(basis="yz", outline=True) +plot.figure.savefig("seg_rectangle_with_zplane_xplane_yplane_yz.png") diff --git a/examples/segment_spheres_with_zplane_and_zcylinder.py b/examples/segment_spheres_with_zplane_and_zcylinder.py index dc24b74..6a1d6bb 100644 --- a/examples/segment_spheres_with_zplane_and_zcylinder.py +++ b/examples/segment_spheres_with_zplane_and_zcylinder.py @@ -1,29 +1,28 @@ - import openmc from openmc_cell_segmenter import segment # example surfaces -inner_sphere_surface = openmc.Sphere(r=500, boundary_type='vacuum') -inner_sphere_surface2 = openmc.Sphere(r=150, z0=550, boundary_type='vacuum') +inner_sphere_surface = openmc.Sphere(r=500, boundary_type="vacuum") +inner_sphere_surface2 = openmc.Sphere(r=150, z0=550, boundary_type="vacuum") # outer_sphere_surface = openmc.Sphere(r=600) # above (+) inner_sphere_surface and below (-) outer_sphere_surface blanket_region = -inner_sphere_surface | -inner_sphere_surface2 -# blanket_region = +inner_sphere_surface & -outer_sphere_surface +# blanket_region = +inner_sphere_surface & -outer_sphere_surface # example cell cell = openmc.Cell(region=blanket_region) -cells = segment(cell, [10,10], ['zplane', 'zcylinder']) +cells = segment(cell, [10, 10], ["zplane", "zcylinder"]) # cells = segment_cell_with_surfaces(cell, [10], ['zcylinder']) geometry = openmc.Geometry(cells) -plot = geometry.plot(basis='xz',outline=True) -plot.figure.savefig('seg_spheres_with_zplane_and_zcylinder_xz.png') +plot = geometry.plot(basis="xz", outline=True) +plot.figure.savefig("seg_spheres_with_zplane_and_zcylinder_xz.png") -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('seg_spheres_with_zplane_and_zcylinder_xy.png') +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("seg_spheres_with_zplane_and_zcylinder_xy.png") -plot = geometry.plot(basis='yz',outline=True) -plot.figure.savefig('seg_spheres_with_zplane_and_zcylinder_yz.png') +plot = geometry.plot(basis="yz", outline=True) +plot.figure.savefig("seg_spheres_with_zplane_and_zcylinder_yz.png") diff --git a/examples/segment_text.py b/examples/segment_text.py index c187950..0d12381 100644 --- a/examples/segment_text.py +++ b/examples/segment_text.py @@ -1,4 +1,3 @@ - # This example was just used for the readme image. # It would need vacuum surfaces and voids for transport. @@ -22,9 +21,13 @@ surf_z_front = openmc.ZPlane(z0=1) lower_region = +surf_s_left & -surf_s_right & +surf_s_bottom & -surf_s_upper_bottom & +surf_z_back & -surf_z_front -lower_upper_region = +surf_s_mid_right & -surf_s_right & +surf_s_upper_bottom & -surf_s_lower_midy & +surf_z_back & -surf_z_front +lower_upper_region = ( + +surf_s_mid_right & -surf_s_right & +surf_s_upper_bottom & -surf_s_lower_midy & +surf_z_back & -surf_z_front +) mid_region = +surf_s_left & -surf_s_right & +surf_s_lower_midy & -surf_s_upper_midy & +surf_z_back & -surf_z_front -upper_lower_region = -surf_s_mid_left & +surf_s_left & +surf_s_upper_midy & -surf_s_lower_top & +surf_z_back & -surf_z_front +upper_lower_region = ( + -surf_s_mid_left & +surf_s_left & +surf_s_upper_midy & -surf_s_lower_top & +surf_z_back & -surf_z_front +) upper_region = +surf_s_left & -surf_s_right & +surf_s_lower_top & -surf_s_top & +surf_z_back & -surf_z_front all_regions = lower_region | mid_region @@ -36,33 +39,33 @@ geometry = openmc.Geometry([cell]) -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('letter_s_csg_xy.png', bbox_inches="tight") +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("letter_s_csg_xy.png", bbox_inches="tight") -segmented_cells = segment(cell, [10,10, 10], ['zplane', 'xplane', 'yplane']) +segmented_cells = segment(cell, [10, 10, 10], ["zplane", "xplane", "yplane"]) geometry = openmc.Geometry(segmented_cells) -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('letter_s_csg_xyz_xy.png', bbox_inches="tight") +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("letter_s_csg_xyz_xy.png", bbox_inches="tight") -segmented_cells = segment(cell, [10], ['yplane']) +segmented_cells = segment(cell, [10], ["yplane"]) geometry = openmc.Geometry(segmented_cells) -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('letter_s_csg_y_xy.png', bbox_inches="tight") +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("letter_s_csg_y_xy.png", bbox_inches="tight") -segmented_cells = segment(cell, [10], ['xplane']) +segmented_cells = segment(cell, [10], ["xplane"]) geometry = openmc.Geometry(segmented_cells) -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('letter_s_csg_x_xy.png', bbox_inches="tight") +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("letter_s_csg_x_xy.png", bbox_inches="tight") -segmented_cells = segment(cell, [10], ['zcylinder']) +segmented_cells = segment(cell, [10], ["zcylinder"]) geometry = openmc.Geometry(segmented_cells) -plot = geometry.plot(basis='xy',outline=True) -plot.figure.savefig('letter_s_csg_zcy_xy.png', bbox_inches="tight") +plot = geometry.plot(basis="xy", outline=True) +plot.figure.savefig("letter_s_csg_zcy_xy.png", bbox_inches="tight") diff --git a/src/openmc_cell_segmenter/__init__.py b/src/openmc_cell_segmenter/__init__.py index a8ce586..bb67a43 100644 --- a/src/openmc_cell_segmenter/__init__.py +++ b/src/openmc_cell_segmenter/__init__.py @@ -1 +1 @@ -from .core import * \ No newline at end of file +from .core import * diff --git a/src/openmc_cell_segmenter/core.py b/src/openmc_cell_segmenter/core.py index 2dc6839..64a0bad 100644 --- a/src/openmc_cell_segmenter/core.py +++ b/src/openmc_cell_segmenter/core.py @@ -1,43 +1,54 @@ import numpy as np import openmc -import matplotlib.pyplot as plt from typing import Sequence, List + def segment( cell: openmc.Cell, number_of_segments: Sequence[int], surface_types: Sequence[str] - ) -> List[openmc.Cell]: +) -> List[openmc.Cell]: + """Segments the cell into a number of smaller cells. + + Parameters + ---------- + cell : openmc.Cell + The cell to segment + number_of_segments : Sequence[int] + The number of segments to divide the input cell into. For example to + segment into 10 cells then set to [10]. To segment into 5 cells with + the first surface_type and 4 cells with the second surface type then + set to [5, 4]. + surface_types : Sequence[str] + The surface types to use when segmenting the cells. For example to + segment using XPlanes then set to ['XPlane']. To segment into cells + with the two different surfaces. + + Returns + ------- + List[openmc.Cell] + A list of cells + """ groups_of_surfaces = [] for i, (num_segs, surface_type) in enumerate(zip(number_of_segments, surface_types)): - surfaces = get_surfaces_for_segmentation( - cell, - num_segs, - surface_type=surface_type - ) + surfaces = get_surfaces_for_segmentation(cell, num_segs, surface_type=surface_type) groups_of_surfaces.append(surfaces) for i, surfaces in enumerate(groups_of_surfaces): if i == 0: - segment_of_cells = segment_cell_with_surfaces( - cell, - surfaces - ) + segment_of_cells = segment_cell_with_surfaces(cell, surfaces) else: - segment_of_cells = segment_cells_with_surfaces( - segment_of_cells, - surfaces - ) + segment_of_cells = segment_cells_with_surfaces(segment_of_cells, surfaces) return segment_of_cells def get_surfaces_for_segmentation(cell, number_of_segments, surface_type): - number_of_surfaces = number_of_segments-1 - # if bounding_box==None: + number_of_surfaces = number_of_segments - 1 + # if bounding_box==None: todo allow bounding boxes to be manually entered min_x = cell.bounding_box[0][0] max_x = cell.bounding_box[1][0] @@ -47,80 +58,70 @@ def get_surfaces_for_segmentation(cell, number_of_segments, surface_type): min_z = cell.bounding_box[0][2] max_z = cell.bounding_box[1][2] - x_width_of_segment = abs(min_x-max_x)/number_of_surfaces - y_width_of_segment = abs(min_y-max_y)/number_of_surfaces - z_width_of_segment = abs(min_z-max_z)/number_of_surfaces + x_width_of_segment = abs(min_x - max_x) / number_of_surfaces + y_width_of_segment = abs(min_y - max_y) / number_of_surfaces + z_width_of_segment = abs(min_z - max_z) / number_of_surfaces + + if surface_type.lower() == "zcylinder": - if surface_type == 'zcylinder': - start = min(min_x, min_y) end = max(max_x, max_y) - width_of_segment = (abs(end-start)/2)/(number_of_surfaces-0.5) + width_of_segment = (abs(end - start) / 2) / (number_of_surfaces - 0.5) # inner surface is double of the width # 0-----50----------100----------200 - radiuses = np.linspace(width_of_segment/2, end, number_of_surfaces, endpoint=False) + radiuses = np.linspace(width_of_segment / 2, end, number_of_surfaces, endpoint=False) - elif surface_type == 'zplane': - radiuses = np.linspace(min_z+z_width_of_segment, max_z, number_of_surfaces, endpoint=False) - elif surface_type == 'xplane': - radiuses = np.linspace(min_x+x_width_of_segment, max_x, number_of_surfaces, endpoint=False) - elif surface_type == 'yplane': - radiuses = np.linspace(min_y+y_width_of_segment, max_y, number_of_surfaces, endpoint=False) + elif surface_type.lower() == "zplane": + radiuses = np.linspace(min_z + z_width_of_segment, max_z, number_of_surfaces, endpoint=False) + elif surface_type.lower() == "xplane": + radiuses = np.linspace(min_x + x_width_of_segment, max_x, number_of_surfaces, endpoint=False) + elif surface_type.lower() == "yplane": + radiuses = np.linspace(min_y + y_width_of_segment, max_y, number_of_surfaces, endpoint=False) - print('radiuses',radiuses) surfaces = [] for radius in radiuses: - if surface_type == 'zcylinder': + if surface_type.lower() == "zcylinder": surfaces.append(openmc.ZCylinder(r=radius)) - if surface_type == 'zplane': + if surface_type.lower() == "zplane": surfaces.append(openmc.ZPlane(z0=radius)) - if surface_type == 'xplane': + if surface_type.lower() == "xplane": surfaces.append(openmc.XPlane(x0=radius)) - if surface_type == 'yplane': + if surface_type.lower() == "yplane": surfaces.append(openmc.YPlane(y0=radius)) - + return surfaces -def segment_cells_with_surfaces( - cells, - surfaces - ): +def segment_cells_with_surfaces(cells, surfaces): all_segmented_cells = [] for cell in cells: - segmented_cells = segment_cell_with_surfaces( - cell, - surfaces - ) - all_segmented_cells= all_segmented_cells+segmented_cells + segmented_cells = segment_cell_with_surfaces(cell, surfaces) + all_segmented_cells = all_segmented_cells + segmented_cells return all_segmented_cells -def segment_cell_with_surfaces( - cell, - surfaces - ): +def segment_cell_with_surfaces(cell, surfaces): new_regions = [] for i, surface in enumerate(surfaces): - if i == 0: - new_region = cell.region & -surface - else: - new_region = cell.region & +surfaces[i-1] & -surfaces[i] - new_regions.append(new_region) + if i == 0: + new_region = cell.region & -surface + else: + new_region = cell.region & +surfaces[i - 1] & -surfaces[i] + new_regions.append(new_region) - if i == len(surfaces)-1: - new_region = cell.region & +surface - new_regions.append(new_region) + if i == len(surfaces) - 1: + new_region = cell.region & +surface + new_regions.append(new_region) - cells = [] + new_cells = [] for region in new_regions: - cell = openmc.Cell(region=region) - cells.append(cell) - return cells + new_cell = openmc.Cell(region=region, fill=cell.fill) + new_cells.append(new_cell) + return new_cells diff --git a/tests/test_segment.py b/tests/test_segment.py index 1892a4e..4ade18a 100644 --- a/tests/test_segment.py +++ b/tests/test_segment.py @@ -1,46 +1,42 @@ - import openmc from openmc_cell_segmenter import segment -# example surfaces -surface_1 = openmc.model.RectangularParallelepiped( - xmin=20, - xmax=200, - ymin=40, - ymax=400, - zmin=60, - zmax=600, - boundary_type='vacuum' -) -# above (+) inner_sphere_surface and below (-) outer_sphere_surface -region_1 = -surface_1 -# region_1 = +inner_sphere_surface & -outer_sphere_surface +def test_correct_number_of_segments(): -# example cell -cell = openmc.Cell(region=region_1) + # example surfaces + surface_1 = openmc.model.RectangularParallelepiped( + xmin=20, xmax=200, ymin=40, ymax=400, zmin=60, zmax=600, boundary_type="vacuum" + ) -def test_correct_number_of_segments(): - cells = segment(cell, [3], ['yplane']) + # above (+) inner_sphere_surface and below (-) outer_sphere_surface + region_1 = -surface_1 + # region_1 = +inner_sphere_surface & -outer_sphere_surface + + # example cell + cell = openmc.Cell(region=region_1) + + cells = segment(cell, [3], ["yplane"]) assert len(cells) == 3 - cells = segment(cell, [3], ['xplane']) + cells = segment(cell, [3], ["xplane"]) assert len(cells) == 3 - cells = segment(cell, [3], ['zplane']) + cells = segment(cell, [3], ["zplane"]) assert len(cells) == 3 - cells = segment(cell, [3], ['zcylinder']) + cells = segment(cell, [3], ["zcylinder"]) assert len(cells) == 3 - cells = segment(cell, [3,7], ['zplane', 'xplane']) - assert len(cells) == 3*7 + cells = segment(cell, [3, 7], ["zplane", "xplane"]) + assert len(cells) == 3 * 7 - cells = segment(cell, [3,7, 9], ['zplane', 'xplane', 'yplane']) - assert len(cells) == 3*7*9 + cells = segment(cell, [3, 7, 9], ["zplane", "xplane", "yplane"]) + assert len(cells) == 3 * 7 * 9 + + cells = segment(cell, [3, 7, 9], ["zcylinder", "xplane", "yplane"]) + assert len(cells) == 3 * 7 * 9 - cells = segment(cell, [3,7, 9], ['zcylinder', 'xplane', 'yplane']) - assert len(cells) == 3*7*9 def test_in_transport(): @@ -49,9 +45,24 @@ def test_in_transport(): material.set_density("g/cm3", 0.1) my_materials = openmc.Materials([material]) - cells = segment(cell, [3,4], ['zcylinder', 'zplane']) + # example surfaces + surface_1 = openmc.model.RectangularParallelepiped( + xmin=20, xmax=200, ymin=40, ymax=400, zmin=60, zmax=600, boundary_type="vacuum" + ) + + # above (+) inner_sphere_surface and below (-) outer_sphere_surface + region_1 = -surface_1 + # region_1 = +inner_sphere_surface & -outer_sphere_surface + + # example cell + cell = openmc.Cell(region=region_1, fill=material) + + cells = segment(cell, [3, 4], ["zcylinder", "zplane"]) my_geometry = openmc.Geometry(cells) + for new_cell in cells: + assert new_cell.fill == material + my_source = openmc.IndependentSource() my_source.space = openmc.stats.Point(cell.bounding_box.center) @@ -67,4 +78,4 @@ def test_in_transport(): my_settings.source = my_source model = openmc.model.Model(my_geometry, my_materials, my_settings) - model.run() \ No newline at end of file + model.run()