Skip to content

Commit

Permalink
Merge pull request #4 from fusion-energy/adding_materials
Browse files Browse the repository at this point in the history
adding same mat as original cell
  • Loading branch information
shimwell authored Sep 27, 2023
2 parents 4621a56 + b108ff7 commit 434cce2
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 135 deletions.
25 changes: 9 additions & 16 deletions examples/segment_rectange_with_zplane_xplane_yplane.py
Original file line number Diff line number Diff line change
@@ -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")
21 changes: 10 additions & 11 deletions examples/segment_spheres_with_zplane_and_zcylinder.py
Original file line number Diff line number Diff line change
@@ -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")
37 changes: 20 additions & 17 deletions examples/segment_text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# This example was just used for the readme image.
# It would need vacuum surfaces and voids for transport.

Expand All @@ -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
Expand All @@ -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")
2 changes: 1 addition & 1 deletion src/openmc_cell_segmenter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .core import *
from .core import *
123 changes: 62 additions & 61 deletions src/openmc_cell_segmenter/core.py
Original file line number Diff line number Diff line change
@@ -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]

Expand All @@ -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
Loading

0 comments on commit 434cce2

Please sign in to comment.