Skip to content

Commit

Permalink
fixed data access bug and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
shimwell committed Jul 17, 2022
1 parent 5fd67f1 commit 6930adf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
65 changes: 43 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@

A Python package for plotting the locations, directions or energy distributions of OpenMC source particles
A Python package for plotting the locations, directions or energy distributions of OpenMC sources.

# Installation

```bash
pip install openmc_source_plotter
```

temporary fix
For position and direction plotting with fixed source sources it is currently
necessary to use openmc version 0.11 and also to point the ```openmc_exec```
path to the openmc executable.

This can be installed with:
```bash
conda install -c conda-forge openmc=0.11
```


# Features

The package provides functions to:

- create the initial_source.h5 for a given openmc.source
- extract the locations, directions and energy of particles
- visualise an openmc.source with respect to:
- visualise an ```openmc.Source``` with respect to:
- direction
- energy
- location

# Example plots

Plot of energy distribution of the source
## Plot of energy distribution of the source

```python
import openmc_source_plotter as osp
Expand All @@ -47,16 +35,15 @@ my_source.energy = openmc.stats.Muir(e0=14080000.0, m_rat=5.0, kt=20000.0)
# plots the particle energy distribution
plot = osp.plot_source_energy(
source=my_source,
number_of_particles=2000,
energy_bins=np.linspace(0, 20e6, 100),
n_samples=2000,
)

plot.show()
```
![openmc particle source energy plot](https://user-images.githubusercontent.com/8583900/143615694-a3578115-f8a2-4971-bf26-458177b4f113.png)


Plot of energy distribution of two sources
## Plot of energy distribution of two sources

```python
import openmc_source_plotter as osp
Expand All @@ -77,15 +64,16 @@ my_dd_source.energy = openmc.stats.Muir(e0=2080000.0, m_rat=2.0, kt=20000.0)
# plots the particle energy distribution
plot = osp.plot_source_energy(
source=[my_dt_source, my_dd_source],
number_of_particles=10000,
energy_bins=np.linspace(0, 20e6, 100),
n_samples=10000,
)

plot.show()
```

![openmc particle source energy plot](https://user-images.githubusercontent.com/8583900/151376414-fb1555eb-61d1-4c82-bc4d-a05f62819c5d.png)

## Plot direction of particles

```python
import openmc_source_plotter as osp
import openmc
Expand All @@ -99,14 +87,47 @@ my_source.angle = openmc.stats.Isotropic()
# plots the particle energy distribution
plot = osp.plot_source_direction(
source=my_source,
number_of_particles=100,
openmc_exec="/home/jshim/miniconda3/envs/openmc_0_11_0/bin/openmc",
n_samples=100,
)

plot.show()
```
![openmc particle source direction plot](https://user-images.githubusercontent.com/8583900/143615706-3b3a8467-0233-42d6-a66c-d536c80a01d8.png)


## Plot position of particles

```python

import openmc_source_plotter as osp
import openmc

# initialises a new source object
my_source = openmc.Source()

# the distribution of radius is just a single value
radius = openmc.stats.Discrete([10], [1])

# the distribution of source z values is just a single value
z_values = openmc.stats.Discrete([0], [1])

# the distribution of source azimuthal angles values is a uniform distribution between 0 and 2 Pi
angle = openmc.stats.Uniform(a=0.0, b=2 * 3.14159265359)

# this makes the ring source using the three distributions and a radius
my_source.space = openmc.stats.CylindricalIndependent(
r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0)
)

# plots the particle energy distribution
plot = osp.plot_source_position(source=my_source)

plot.show()

```

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

# Usage

See the [examples folder](https://github.com/fusion-energy/openmc_source_plotter/tree/main/examples) for example usage scripts.
11 changes: 7 additions & 4 deletions openmc_source_plotter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ def sample_initial_particles(source: openmc.source, n_samples=1000, prn_seed=Non
settings = openmc.Settings()
settings.particles = 1
settings.batches = 1
settings.source = source
settings.export_to_xml()

materials = openmc.Materials()
materials.export_to_xml()

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

geometry.export_to_xml()
# model.geometry = openmc.Geometry([cell])

# model = openmc.Model()
# # model = openmc.Model()
# model.geometry = geometry
# model.materials = geometry

openmc.lib.init()
particles = openmc.lib.sample_external_source(
Expand Down Expand Up @@ -58,7 +60,7 @@ def plot_source_energy(

for single_source in source:

e_values = single_source.energy.sample(n_samples=n_samples)
e_values = single_source.energy.sample(n_samples=n_samples, prn_seed=prn_seed)

# Calculate pdf for source energies
probability, bin_edges = np.histogram(e_values, bins="auto", density=True)
Expand Down Expand Up @@ -104,6 +106,7 @@ def plot_source_position(
for single_source in source:

data = sample_initial_particles(single_source, n_samples, prn_seed)
print([p.r for p in data])

text = ["Energy = " + str(particle.E) + " eV" for particle in data]

Expand Down

0 comments on commit 6930adf

Please sign in to comment.