Skip to content

Commit

Permalink
added model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
shimwell committed Jan 29, 2024
1 parent 5b543e2 commit 56f4dc5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 57 deletions.
31 changes: 16 additions & 15 deletions src/openmc_source_plotter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ def sample_initial_particles(self, n_samples: int = 1000, prn_seed: int = None):
self.materials.export_to_xml()
self.geometry.export_to_xml()

openmc.lib.init(output=False)
particles = openmc.lib.sample_external_source(
n_samples=n_samples, prn_seed=prn_seed
)
openmc.lib.finalize()

return particles

else: # source object

settings = openmc.Settings()
Expand All @@ -43,13 +35,13 @@ def sample_initial_particles(self, n_samples: int = 1000, prn_seed: int = None):

geometry.export_to_xml()

openmc.lib.init(output=False)
particles = openmc.lib.sample_external_source(
n_samples=n_samples, prn_seed=prn_seed
)
openmc.lib.finalize()
openmc.lib.init(output=False)
particles = openmc.lib.sample_external_source(
n_samples=n_samples, prn_seed=prn_seed
)
openmc.lib.finalize()

return particles
return particles


def plot_source_energy(
Expand Down Expand Up @@ -94,7 +86,8 @@ def plot_source_energy(
probability, bin_edges = np.histogram(e_values, bins=energy_bins, density=True)

# scaling by strength
probability = probability * self.strength
if isinstance(self, openmc.SourceBase):
probability = probability * self.strength

# Plot source energy histogram
figure.add_trace(
Expand Down Expand Up @@ -239,5 +232,13 @@ def plot_source_direction(
openmc.Model.sample_initial_particles = sample_initial_particles

openmc.SourceBase.plot_source_energy = plot_source_energy
openmc.model.Model.plot_source_energy = plot_source_energy
openmc.Model.plot_source_energy = plot_source_energy

openmc.SourceBase.plot_source_position = plot_source_position
openmc.model.Model.plot_source_position = plot_source_position
openmc.Model.plot_source_position = plot_source_position

openmc.SourceBase.plot_source_direction = plot_source_direction
openmc.model.Model.plot_source_direction = plot_source_direction
openmc.Model.plot_source_direction = plot_source_direction
88 changes: 88 additions & 0 deletions tests/test_core_with_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import openmc
import openmc_source_plotter
import numpy as np
import plotly.graph_objects as go
import pytest


@pytest.fixture
def test_model():
# initialises a new source object
my_source = openmc.Source()

# sets the location of the source to x=0 y=0 z=0
my_source.space = openmc.stats.Point((1.0, 2.0, 3.0))

# sets the direction to isotropic
my_source.angle = openmc.stats.Isotropic()

# sets the energy distribution to 100% 14MeV neutrons
my_source.energy = openmc.stats.Discrete([15e6], [1])

my_source.particle = "photon"

settings = openmc.Settings()
settings.particles = 1
settings.batches = 1
settings.source = my_source

materials = openmc.Materials()

sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
cell = openmc.Cell(region=-sph)
geometry = openmc.Geometry([cell])

model = openmc.Model(geometry, materials, settings)

return model


def test_sample_initial_particles(test_model):
particles = test_model.sample_initial_particles(n_samples=43)
for particle in particles:
assert particle.E == 15e6
assert str(particle.particle) == "photon"
assert particle.r == (1.0, 2.0, 3.0)
assert len(particles) == 43

def test_energy_plot_with_bins(test_model):
plot = test_model.plot_source_energy(
n_samples=10,
energy_bins=np.linspace(0, 20e6, 100),
)
assert isinstance(plot, go.Figure)


def test_energy_plot(test_model):
plot = test_model.plot_source_energy(n_samples=10)
assert isinstance(plot, go.Figure)
assert len(plot.data[0]["x"]) == 1


def test_position_plot(test_model):
plot = test_model.plot_source_position(n_samples=10)
assert isinstance(plot, go.Figure)


def test_direction_plot(test_model):
plot = test_model.plot_source_direction(n_samples=10)
assert isinstance(plot, go.Figure)


def test_energy_plot_with_figure(test_model):
base_figure = go.Figure()
plot = test_model.plot_source_energy(figure=base_figure, n_samples=10)
assert isinstance(plot, go.Figure)
assert len(plot.data[0]["x"]) == 1


def test_position_plot_with_figure(test_model):
base_figure = go.Figure()
plot = test_model.plot_source_position(figure=base_figure, n_samples=10)
assert isinstance(plot, go.Figure)


def test_direction_plot_with_figure(test_model):
base_figure = go.Figure()
plot = test_model.plot_source_direction(figure=base_figure, n_samples=10)
assert isinstance(plot, go.Figure)
43 changes: 1 addition & 42 deletions tests/test_core.py → tests/test_core_with_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,7 @@ def test_source():
return my_source


@pytest.fixture
def test_model():
# initialises a new source object
my_source = openmc.Source()

# sets the location of the source to x=0 y=0 z=0
my_source.space = openmc.stats.Point((1.0, 2.0, 3.0))

# sets the direction to isotropic
my_source.angle = openmc.stats.Isotropic()

# sets the energy distribution to 100% 14MeV neutrons
my_source.energy = openmc.stats.Discrete([15e6], [1])

my_source.particle = "photon"

settings = openmc.Settings()
settings.particles = 1
settings.batches = 1
settings.source = my_source

materials = openmc.Materials()

sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
cell = openmc.Cell(region=-sph)
geometry = openmc.Geometry([cell])

model = openmc.Model(geometry, materials, settings)

return model


def test_source_sample_initial_particles(test_source):
def test_sample_initial_particles(test_source):
particles = test_source.sample_initial_particles(n_samples=42)
for particle in particles:
assert particle.E == 14e6
Expand All @@ -66,15 +34,6 @@ def test_source_sample_initial_particles(test_source):
assert len(particles) == 42


def test_model_sample_initial_particles(test_model):
particles = test_model.sample_initial_particles(n_samples=43)
for particle in particles:
assert particle.E == 15e6
assert str(particle.particle) == "photon"
assert particle.r == (1.0, 2.0, 3.0)
assert len(particles) == 43


def test_energy_plot_with_bins(test_source):
plot = test_source.plot_source_energy(
n_samples=10,
Expand Down

0 comments on commit 56f4dc5

Please sign in to comment.