From 733d14be81ef678f02b1f10e788cec65fed53a79 Mon Sep 17 00:00:00 2001 From: shimwell Date: Thu, 12 Oct 2023 23:23:40 +0100 Subject: [PATCH 1/2] corrected z position of geometry --- examples/plot_phir_slice_point_source.py | 4 +-- examples/plot_rz_slices_point_source.py | 2 +- examples/plot_rz_slices_ring_source.py | 25 +++++++++------- src/openmc_cylindrical_mesh_plotter/core.py | 32 ++++++++------------- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/examples/plot_phir_slice_point_source.py b/examples/plot_phir_slice_point_source.py index c3f35ca..b08b701 100644 --- a/examples/plot_phir_slice_point_source.py +++ b/examples/plot_phir_slice_point_source.py @@ -3,7 +3,7 @@ # plotted using the openmc_cylindrical_plotter in the Phi R axis. import openmc -from openmc_cylindrical_mesh_plotter import plot_mesh_tally_rz_slice +from openmc_cylindrical_mesh_plotter import plot_mesh_tally_phir_slice from matplotlib.colors import LogNorm @@ -48,7 +48,7 @@ my_tally_result = statepoint.get_tally(name="my_tally") -plot = plot_mesh_tally_rz_slice( +plot = plot_mesh_tally_phir_slice( tally=my_tally_result, outline=True, geometry=my_geometry, diff --git a/examples/plot_rz_slices_point_source.py b/examples/plot_rz_slices_point_source.py index 808660f..9842a1f 100644 --- a/examples/plot_rz_slices_point_source.py +++ b/examples/plot_rz_slices_point_source.py @@ -65,6 +65,6 @@ for slice_index in range(1, len(mesh.phi_grid)): plot = plot_mesh_tally_rz_slice( - tally=my_tally_result, geometry=my_geometry, norm=LogNorm() + tally=my_tally_result, outline=True, geometry=my_geometry, norm=LogNorm() ) plot.figure.savefig(f"rz_point_source_{slice_index}.png") diff --git a/examples/plot_rz_slices_ring_source.py b/examples/plot_rz_slices_ring_source.py index 8e3b87f..9b24a6e 100644 --- a/examples/plot_rz_slices_ring_source.py +++ b/examples/plot_rz_slices_ring_source.py @@ -10,26 +10,30 @@ from matplotlib.colors import LogNorm mesh = openmc.CylindricalMesh( - phi_grid=np.linspace(0.0, 2 * pi, 3), - r_grid=np.linspace(0, 10, 50), - z_grid=np.linspace(0, 8, 50), + phi_grid=np.linspace(0.0, 2 * pi, 4), + r_grid=np.linspace(0,100, 50), + z_grid=np.linspace(-100, 50, 50), ) -tally = openmc.Tally(name="my_tally") mesh_filter = openmc.MeshFilter(mesh) -tally.filters.append(mesh_filter) -tally.scores.append("flux") + +tally = openmc.Tally(name="my_tally") +tally.filters =[mesh_filter] +tally.scores=["flux"] tallies = openmc.Tallies([tally]) -outer_surface = openmc.Sphere(r=100, boundary_type="vacuum") -cell = openmc.Cell(region=-outer_surface) +surf1=openmc.model.RectangularParallelepiped(-100,100,-100,100,-100,50, boundary_type="vacuum") +surf2=openmc.model.RectangularParallelepiped(-95,95,-95,95,-95,45) +surf3 = openmc.Sphere(r=40) +cell1 = openmc.Cell(region=-surf3) +cell2 = openmc.Cell(region=+surf2&-surf1) +cell3 = openmc.Cell(region=+surf3&-surf2) material = openmc.Material() material.add_nuclide("Fe56", 1) material.set_density("g/cm3", 0.1) my_materials = openmc.Materials([material]) -universe = openmc.Universe(cells=[cell]) -my_geometry = openmc.Geometry(universe) +my_geometry = openmc.Geometry([cell1, cell2, cell3]) my_source = openmc.Source() @@ -66,6 +70,7 @@ plot = plot_mesh_tally_rz_slice( tally=my_tally_result, geometry=my_geometry, + outline=True, norm=LogNorm(), slice_index=slice_index, ) diff --git a/src/openmc_cylindrical_mesh_plotter/core.py b/src/openmc_cylindrical_mesh_plotter/core.py index 5609386..f018a74 100644 --- a/src/openmc_cylindrical_mesh_plotter/core.py +++ b/src/openmc_cylindrical_mesh_plotter/core.py @@ -140,7 +140,15 @@ def plot_mesh_tally_rz_slice( xlabel, ylabel = f"r [{axis_units}]", f"z [{axis_units}]" axis_scaling_factor = {"km": 0.00001, "m": 0.01, "cm": 1, "mm": 10}[axis_units] - extent = [mesh.r_grid[0], mesh.r_grid[-1], mesh.z_grid[0], mesh.z_grid[-1]] + if mesh.origin[0] != 0. or mesh.origin[1] != 0.: + raise ValueError('Plotter only works for cylindrical meshes with x,y origins of 0,0') + + extent = [ + mesh.r_grid[0], + mesh.r_grid[-1], + mesh.origin[2] + mesh.z_grid[0], + mesh.origin[2] + mesh.z_grid[-1] + ] x_min, x_max, y_min, y_max = [i * axis_scaling_factor for i in extent] @@ -157,22 +165,6 @@ def plot_mesh_tally_rz_slice( if outline and geometry is not None: import matplotlib.image as mpimg - # code to make sure geometry outline is in the middle of the mesh voxel - # two of the three dimensions are just in the center of the mesh - # but the slice can move one axis off the center so this needs calculating - # x1, y1, z1 = mesh.upper_right - # x_origin, y_origin, z_origin = mesh.origin - - # # width_x = abs(x_origin + x1)/2 - # # width_y = abs(y_origin + y1)/2 - # # width_z = abs(z_origin + z1) - - # width_x, width_y, width_z = mesh.bounding_box.width - # width_x=width_x/2 - # width_y=width_y/2 - # print('width_x,width_y,width_z') - # print(width_x,width_y,width_z) - model = openmc.Model() model.geometry = geometry plot = openmc.Plot() @@ -181,9 +173,9 @@ def plot_mesh_tally_rz_slice( width_y = abs(extent[1] - extent[0]) # same width_z = abs(extent[3] - extent[2]) - x_center = abs(extent[0] + (width_x / 2)) - y_center = abs(extent[0] + (width_y / 2)) - z_center = abs(extent[2] + width_z * 0.5) + x_center = extent[0] + (width_x / 2) + y_center = extent[0] + (width_y / 2) + z_center = extent[2] + width_z * 0.5 if geometry_basis == "xz": plot.origin = (x_center, 0, z_center) From 0dd1799b8e89c0d0cb21f43605f3edd33b1bef38 Mon Sep 17 00:00:00 2001 From: shimwell Date: Thu, 12 Oct 2023 22:24:06 +0000 Subject: [PATCH 2/2] [skip ci] Apply formatting changes --- examples/plot_rz_slices_ring_source.py | 16 +++++++++------- src/openmc_cylindrical_mesh_plotter/core.py | 8 +++++--- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/plot_rz_slices_ring_source.py b/examples/plot_rz_slices_ring_source.py index 9b24a6e..f0c9aaf 100644 --- a/examples/plot_rz_slices_ring_source.py +++ b/examples/plot_rz_slices_ring_source.py @@ -11,22 +11,24 @@ mesh = openmc.CylindricalMesh( phi_grid=np.linspace(0.0, 2 * pi, 4), - r_grid=np.linspace(0,100, 50), + r_grid=np.linspace(0, 100, 50), z_grid=np.linspace(-100, 50, 50), ) mesh_filter = openmc.MeshFilter(mesh) tally = openmc.Tally(name="my_tally") -tally.filters =[mesh_filter] -tally.scores=["flux"] +tally.filters = [mesh_filter] +tally.scores = ["flux"] tallies = openmc.Tallies([tally]) -surf1=openmc.model.RectangularParallelepiped(-100,100,-100,100,-100,50, boundary_type="vacuum") -surf2=openmc.model.RectangularParallelepiped(-95,95,-95,95,-95,45) +surf1 = openmc.model.RectangularParallelepiped( + -100, 100, -100, 100, -100, 50, boundary_type="vacuum" +) +surf2 = openmc.model.RectangularParallelepiped(-95, 95, -95, 95, -95, 45) surf3 = openmc.Sphere(r=40) cell1 = openmc.Cell(region=-surf3) -cell2 = openmc.Cell(region=+surf2&-surf1) -cell3 = openmc.Cell(region=+surf3&-surf2) +cell2 = openmc.Cell(region=+surf2 & -surf1) +cell3 = openmc.Cell(region=+surf3 & -surf2) material = openmc.Material() material.add_nuclide("Fe56", 1) diff --git a/src/openmc_cylindrical_mesh_plotter/core.py b/src/openmc_cylindrical_mesh_plotter/core.py index f018a74..7d299fe 100644 --- a/src/openmc_cylindrical_mesh_plotter/core.py +++ b/src/openmc_cylindrical_mesh_plotter/core.py @@ -140,14 +140,16 @@ def plot_mesh_tally_rz_slice( xlabel, ylabel = f"r [{axis_units}]", f"z [{axis_units}]" axis_scaling_factor = {"km": 0.00001, "m": 0.01, "cm": 1, "mm": 10}[axis_units] - if mesh.origin[0] != 0. or mesh.origin[1] != 0.: - raise ValueError('Plotter only works for cylindrical meshes with x,y origins of 0,0') + if mesh.origin[0] != 0.0 or mesh.origin[1] != 0.0: + raise ValueError( + "Plotter only works for cylindrical meshes with x,y origins of 0,0" + ) extent = [ mesh.r_grid[0], mesh.r_grid[-1], mesh.origin[2] + mesh.z_grid[0], - mesh.origin[2] + mesh.z_grid[-1] + mesh.origin[2] + mesh.z_grid[-1], ] x_min, x_max, y_min, y_max = [i * axis_scaling_factor for i in extent]