Skip to content

Commit 5b543e2

Browse files
authored
Merge pull request #32 from fusion-energy/adding_sample_source_to_model_class
adds sample_initial_particles to the openmc.Model method
2 parents 82656ef + dd83e70 commit 5b543e2

3 files changed

Lines changed: 142 additions & 40 deletions

File tree

README.md

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ pip install openmc_source_plotter
1212

1313
# Features
1414

15-
The package simply extends the default ```openmc.Source``` to provides additional functions that:
15+
The package simply extends the default ```openmc.IndependentSourceBase``` to provides additional functions that:
1616

1717
- extract the positions, directions and energy of particles
18-
- visualise an ```osp.SourceWithPlotting``` with respect to:
18+
- visualise an ```openmc.IndependentSourceBase``` with respect to:
1919
- direction
2020
- energy
2121
- position
2222

23+
Or just sample the with ```openmc.Model.sample_initial_particles```
24+
2325
# Example plots
2426

2527
Below are some basic examples, for more examples see the [examples folder](https://github.com/fusion-energy/openmc_source_plotter/tree/main/examples) for example usage scripts.
@@ -29,13 +31,13 @@ Below are some basic examples, for more examples see the [examples folder](https
2931

3032
```python
3133
import openmc
32-
import openmc_source_plotter # extends openmc.Source with plotting functions
34+
import openmc_source_plotter # extends openmc.IndependentSource with plotting functions
3335

3436
# initialises a new source object
35-
my_source = openmc.Source()
37+
my_source = openmc.IndependentSource()
3638

37-
# sets the energy distribution to a Muir distribution neutrons for DT fusion neutrons
38-
my_source.energy = openmc.stats.Muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
39+
# sets the energy distribution to a muir distribution neutrons for DT fusion neutrons
40+
my_source.energy = openmc.stats.muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
3941

4042
# plots the particle energy distribution
4143
plot = my_source.plot_source_energy(n_samples=2000)
@@ -49,18 +51,18 @@ plot.show()
4951

5052
```python
5153
import openmc
52-
import openmc_source_plotter # extends openmc.Source with plotting functions
54+
import openmc_source_plotter # extends openmc.IndependentSource with plotting functions
5355

5456
# initialises a new source object
55-
my_dt_source = openmc.Source()
57+
my_dt_source = openmc.IndependentSource()
5658

57-
# sets the energy distribution to a Muir distribution DT neutrons
58-
my_dt_source.energy = openmc.stats.Muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
59+
# sets the energy distribution to a muir distribution DT neutrons
60+
my_dt_source.energy = openmc.stats.muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
5961

6062
# initialises a new source object
61-
my_dd_source = openmc.Source()
62-
# sets the energy distribution to a Muir distribution DD neutrons
63-
my_dd_source.energy = openmc.stats.Muir(e0=2080000.0, m_rat=2.0, kt=20000.0)
63+
my_dd_source = openmc.IndependentSource()
64+
# sets the energy distribution to a muir distribution DD neutrons
65+
my_dd_source.energy = openmc.stats.muir(e0=2080000.0, m_rat=2.0, kt=20000.0)
6466

6567
# plots the particle energy distribution by building on the first plot
6668
figure1 = my_dd_source.plot_source_energy(n_samples=10000)
@@ -75,10 +77,10 @@ figure2.show()
7577

7678
```python
7779
import openmc
78-
import openmc_source_plotter # extends openmc.Source with plotting functions
80+
import openmc_source_plotter # extends openmc.IndependentSource with plotting functions
7981

8082
# initializes a new source object
81-
my_source = openmc.Source()
83+
my_source = openmc.IndependentSource()
8284

8385
# sets the direction to isotropic
8486
my_source.angle = openmc.stats.Isotropic()
@@ -92,7 +94,7 @@ plot.show()
9294
![openmc particle source direction plot](https://user-images.githubusercontent.com/8583900/143615706-3b3a8467-0233-42d6-a66c-d536c80a01d8.png)
9395

9496

95-
## Plot gamma spectrum of particles
97+
<!-- ## Plot gamma spectrum of particles
9698
9799
```python
98100
import openmc
@@ -114,16 +116,16 @@ plt.xscale("log") # modify axis from default settings
114116
plt.savefig("gamma_spec.png")
115117
```
116118
117-
![openmc gamma spectrum](https://user-images.githubusercontent.com/8583900/228280129-b8160e18-9ca9-4b20-a4e1-d2948908daf6.png)
119+
![openmc gamma spectrum](https://user-images.githubusercontent.com/8583900/228280129-b8160e18-9ca9-4b20-a4e1-d2948908daf6.png) -->
118120

119121
## Plot position of particles
120122

121123
```python
122124
import openmc
123-
import openmc_source_plotter # extends openmc.Source with plotting functions
125+
import openmc_source_plotter # extends openmc.IndependentSource with plotting functions
124126

125127
# initialises a new source object
126-
my_source = openmc.Source()
128+
my_source = openmc.IndependentSource()
127129

128130
# the distribution of radius is just a single value
129131
radius = openmc.stats.Discrete([10], [1])
@@ -149,6 +151,39 @@ plot.show()
149151

150152
![openmc particle source position plot](https://user-images.githubusercontent.com/8583900/179424915-bee56a87-6214-46ef-8625-92b8f4cbd1b3.png)
151153

154+
155+
## Extract particle objects
156+
157+
A list of ```openmc.Particle``` objects can be obtained using ```model.sample_initial_particles()``` or ```openmc.SourceBase.sample_initial_particles()```
158+
159+
```python
160+
import openmc
161+
import openmc_source_plotter # extents openmc.Model with sample_initial_particles method
162+
163+
settings = openmc.Settings()
164+
settings.particles = 1
165+
settings.batches = 1
166+
my_source = openmc.IndependentSource()
167+
my_source.energy = openmc.stats.muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
168+
settings.source = my_source
169+
materials = openmc.Materials()
170+
sph = openmc.Sphere(r=100, boundary_type="vacuum")
171+
cell = openmc.Cell(region=-sph)
172+
geometry = openmc.Geometry([cell])
173+
174+
model = openmc.Model(geometry, materials, settings)
175+
176+
particles = model.sample_initial_particles(n_samples=10)
177+
178+
print(particles)
179+
>>>[<SourceParticle: neutron at E=1.440285e+07 eV>, <SourceParticle: neutron at E=1.397691e+07 eV>, <SourceParticle: neutron at E=1.393681e+07 eV>, <SourceParticle: neutron at E=1.470896e+07 eV>, <SourceParticle: neutron at E=1.460563e+07 eV>, <SourceParticle: neutron at E=1.420684e+07 eV>, <SourceParticle: neutron at E=1.413932e+07 eV>, <SourceParticle: neutron at E=1.412428e+07 eV>, <SourceParticle: neutron at E=1.464779e+07 eV>, <SourceParticle: neutron at E=1.391648e+07 eV>]
180+
181+
print(particles[0].E)
182+
>>>1.440285e+07
183+
```
184+
185+
## Related packages
186+
152187
Tokamak sources can also be plotted using the [openmc-plasma-source](https://github.com/fusion-energy/openmc-plasma-source) package
153188

154189
![openmc_source_plotter_openmc-plasma-source_tokamak](https://user-images.githubusercontent.com/8583900/187487894-ba0bd025-46f2-4c7d-8b15-3d260aed47a0.png)

src/openmc_source_plotter/core.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,45 @@
1111

1212

1313
def sample_initial_particles(self, n_samples: int = 1000, prn_seed: int = None):
14-
settings = openmc.Settings()
15-
settings.particles = 1
16-
settings.batches = 1
17-
settings.source = self
18-
settings.export_to_xml()
1914

20-
materials = openmc.Materials()
21-
materials.export_to_xml()
15+
if isinstance(self, openmc.Model):
2216

23-
sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
24-
cell = openmc.Cell(region=-sph)
25-
geometry = openmc.Geometry([cell])
17+
self.settings.export_to_xml()
18+
self.materials.export_to_xml()
19+
self.geometry.export_to_xml()
2620

27-
geometry.export_to_xml()
21+
openmc.lib.init(output=False)
22+
particles = openmc.lib.sample_external_source(
23+
n_samples=n_samples, prn_seed=prn_seed
24+
)
25+
openmc.lib.finalize()
2826

29-
openmc.lib.init(output=False)
30-
particles = openmc.lib.sample_external_source(
31-
n_samples=n_samples, prn_seed=prn_seed
32-
)
33-
openmc.lib.finalize()
27+
return particles
28+
29+
else: # source object
30+
31+
settings = openmc.Settings()
32+
settings.particles = 1
33+
settings.batches = 1
34+
settings.source = self
35+
settings.export_to_xml()
36+
37+
materials = openmc.Materials()
38+
materials.export_to_xml()
3439

35-
return particles
40+
sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
41+
cell = openmc.Cell(region=-sph)
42+
geometry = openmc.Geometry([cell])
43+
44+
geometry.export_to_xml()
45+
46+
openmc.lib.init(output=False)
47+
particles = openmc.lib.sample_external_source(
48+
n_samples=n_samples, prn_seed=prn_seed
49+
)
50+
openmc.lib.finalize()
51+
52+
return particles
3653

3754

3855
def plot_source_energy(
@@ -218,6 +235,9 @@ def plot_source_direction(
218235
plot_source_position(), plot_source_direction(), sample_initial_particles()
219236
"""
220237
openmc.SourceBase.sample_initial_particles = sample_initial_particles
238+
openmc.model.Model.sample_initial_particles = sample_initial_particles
239+
openmc.Model.sample_initial_particles = sample_initial_particles
240+
221241
openmc.SourceBase.plot_source_energy = plot_source_energy
222242
openmc.SourceBase.plot_source_position = plot_source_position
223243
openmc.SourceBase.plot_source_direction = plot_source_direction

tests/test_core.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,68 @@ def test_source():
1111
my_source = openmc.Source()
1212

1313
# sets the location of the source to x=0 y=0 z=0
14-
my_source.space = openmc.stats.Point((0, 0, 0))
14+
my_source.space = openmc.stats.Point((4.0, 5.0, 6.0))
1515

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

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

22+
my_source.particle = "neutron"
23+
2224
my_source = my_source
2325
return my_source
2426

2527

26-
def test_sample_initial_particles(test_source):
27-
data = test_source.sample_initial_particles(n_samples=42)
28-
assert len(data) == 42
28+
@pytest.fixture
29+
def test_model():
30+
# initialises a new source object
31+
my_source = openmc.Source()
32+
33+
# sets the location of the source to x=0 y=0 z=0
34+
my_source.space = openmc.stats.Point((1.0, 2.0, 3.0))
35+
36+
# sets the direction to isotropic
37+
my_source.angle = openmc.stats.Isotropic()
38+
39+
# sets the energy distribution to 100% 14MeV neutrons
40+
my_source.energy = openmc.stats.Discrete([15e6], [1])
41+
42+
my_source.particle = "photon"
43+
44+
settings = openmc.Settings()
45+
settings.particles = 1
46+
settings.batches = 1
47+
settings.source = my_source
48+
49+
materials = openmc.Materials()
50+
51+
sph = openmc.Sphere(r=9999999999, boundary_type="vacuum")
52+
cell = openmc.Cell(region=-sph)
53+
geometry = openmc.Geometry([cell])
54+
55+
model = openmc.Model(geometry, materials, settings)
56+
57+
return model
58+
59+
60+
def test_source_sample_initial_particles(test_source):
61+
particles = test_source.sample_initial_particles(n_samples=42)
62+
for particle in particles:
63+
assert particle.E == 14e6
64+
assert str(particle.particle) == "neutron"
65+
assert particle.r == (4.0, 5.0, 6.0)
66+
assert len(particles) == 42
67+
68+
69+
def test_model_sample_initial_particles(test_model):
70+
particles = test_model.sample_initial_particles(n_samples=43)
71+
for particle in particles:
72+
assert particle.E == 15e6
73+
assert str(particle.particle) == "photon"
74+
assert particle.r == (1.0, 2.0, 3.0)
75+
assert len(particles) == 43
2976

3077

3178
def test_energy_plot_with_bins(test_source):

0 commit comments

Comments
 (0)