Skip to content

Commit fb3ae38

Browse files
authored
Merge pull request #4 from DanShort12/serialise_plasma_source
Serialise plasma source
2 parents 789d575 + 0e34dc6 commit fb3ae38

23 files changed

Lines changed: 817 additions & 1427 deletions

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
exclude = __init__.py

.github/workflows/python.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ jobs:
2020
uses: actions/setup-python@v2
2121
with:
2222
python-version: ${{ matrix.python-version }}
23+
- name: Install OpenMC
24+
run: |
25+
sudo apt-get install -y g++ cmake libhdf5-dev
26+
git clone --recurse-submodules https://github.com/openmc-dev/openmc.git
27+
cd openmc
28+
git checkout
29+
mkdir build && cd build
30+
cmake ..
31+
make
32+
sudo make install
2333
- name: Install plasma source
2434
run: |
2535
pip install -r requirements-develop.txt

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ dist/
3939
# Python
4040
__pycache__/
4141
*.py[cod]
42+
.venv
4243

44+
# Outputs
45+
*.xml
46+
*.h5
47+
48+
# VS Code
49+
.vscode

README.md

Lines changed: 90 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,106 @@ The plasma source is based on a paper by [C. Fausser et al](https://www.scienced
88

99
## Installation
1010

11+
### Installing from PyPI
12+
1113
```pip install parametric_plasma_source```
1214

15+
### Installing from source
16+
17+
Installation of the parametric plasma source from source requires cmake to build the underlying C++ code. This can be obtained from
18+
your OS's package manager by e.g. `sudo apt-get install cmake` or from cmake source.
19+
20+
If you intend to develop the code then it is recommended to work in a virtual environment.
21+
22+
The requirements for developing the code can be installed by running:
23+
24+
```pip install -r requirements-develop.txt```
25+
26+
The package can be built and installed in editable mode by:
27+
28+
```pip install -e .```
29+
1330
## Usage
1431

15-
The parametric plasma source can be imported an used in Python 3 in the following manner.
32+
The parametric plasma source can be sampled either directly in Python 3 or sampled in an OpenMC simulation.
33+
34+
For a better understanding of the varibles take a look at the [C. Fausser et al](https://www.sciencedirect.com/science/article/pii/S0920379612000853) paper.
35+
36+
### Sampling in Python
37+
38+
The parametric plasma source can be imported an used in Python 3 in the following manner:
1639

1740
```[python]
18-
from parametric_plasma_source import Plasma
19-
my_plasma = Plasma(major_radius=6,
20-
minor_radius=1.5,
21-
elongation = 2.0
22-
triangularity = 0.55)
23-
my_plasma.export_plasma_source('custom_openmc_plasma_source.so')
41+
from parametric_plasma_source import PlasmaSource
42+
from random import random
43+
44+
plasma_params = {
45+
"elongation": 1.557,
46+
"ion_density_origin": 1.09e20,
47+
"ion_density_peaking_factor": 1,
48+
"ion_density_pedestal": 1.09e20,
49+
"ion_density_separatrix": 3e19,
50+
"ion_temperature_origin": 45.9,
51+
"ion_temperature_peaking_factor": 8.06,
52+
"ion_temperature_pedestal": 6.09,
53+
"ion_temperature_separatrix": 0.1,
54+
"major_radius": 9.06,
55+
"minor_radius": 2.92258,
56+
"pedestal_radius": 0.8 * 2.92258,
57+
"plasma_id": 1,
58+
"shafranov_shift": 0.44789,
59+
"triangularity": 0.270,
60+
"ion_temperature_beta": 6,
61+
}
62+
63+
my_plasma = PlasmaSource(**plasma_params)
64+
sample = my_plasma.sample([random(), random(), random(), random(), random(), random(), random(), random()])
65+
particle_x, particle_y, particle_z = sample[0], sample[1], sample[2]
66+
particle_x_dir, particle_y_dir, particle_z_dir = sample[3], sample[4], sample[5]
67+
particle_energy_mev = sample[6]
2468
```
2569

26-
In the above example the major_radius, minor_radius, elongation and triangularity while the other varibles are kept as the default values.
70+
### Sampling in OpenMC
71+
72+
The parametric plasma source also contains a plugin library for OpenMC to allow the source to be sampled in an OpenMC simulation.
2773

28-
There are a number of additional arguments that can be passed to the Plasma class on construction. Units are in SI (e.g. meters not cm)
74+
When using the OpenMC sampling the inputs must be provided in meters where applicable (the sampling will convert to cm).
2975

3076
```[python]
31-
ion_density_pedistal = 1.09e+20
32-
ion_density_seperatrix = 3e+19
33-
ion_density_origin = 1.09e+20
34-
ion_temperature_pedistal = 6.09
35-
ion_temperature_seperatrix = 0.1
36-
ion_temperature_origin = 45.9
37-
pedistal_radius = 0.8
38-
ion_density_peaking_factor = 1
39-
ion_temperature_peaking_factor = 8.06
40-
minor_radius = 1.56
41-
major_radius = 2.5
42-
elongation = 2.0
43-
triangularity = 0.55
44-
shafranov_shift = 0.0
45-
number_of_bins = 100
46-
plasma_type = 1
77+
from parametric_plasma_source import PlasmaSource, SOURCE_SAMPLING_PATH
78+
import openmc
79+
80+
plasma_params = {
81+
"elongation": 1.557,
82+
"ion_density_origin": 1.09e20,
83+
"ion_density_peaking_factor": 1,
84+
"ion_density_pedestal": 1.09e20,
85+
"ion_density_separatrix": 3e19,
86+
"ion_temperature_origin": 45.9,
87+
"ion_temperature_peaking_factor": 8.06,
88+
"ion_temperature_pedestal": 6.09,
89+
"ion_temperature_separatrix": 0.1,
90+
"major_radius": 9.06,
91+
"minor_radius": 2.92258,
92+
"pedestal_radius": 0.8 * 2.92258,
93+
"plasma_id": 1,
94+
"shafranov_shift": 0.44789,
95+
"triangularity": 0.270,
96+
"ion_temperature_beta": 6,
97+
}
98+
99+
my_plasma = PlasmaSource(**plasma_params)
100+
settings = openmc.Settings()
101+
settings.run_mode = "fixed source"
102+
settings.batches = 10
103+
settings.particles = 1000
104+
source = openmc.Source()
105+
source.library = SOURCE_SAMPLING_PATH
106+
source.parameters = str(my_plasma)
107+
settings.source = source
108+
settings.export_to_xml()
47109
```
48110

49-
For a better understanding of the varibles take a look at the [C. Fausser et al](https://www.sciencedirect.com/science/article/pii/S0920379612000853) paper.
111+
## Running Tests
112+
113+
The tests are run by executing `pytest tests` from within your virtual environment.

data/example_source.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major_radius=4.5, minor_radius=1.5, elongation=2, triangularity=0.55, shafranov_shift=0, pedestal_radius=1.2, ion_density_pedestal=1.09e+20, ion_density_separatrix=3e+19, ion_density_origin=1.09e+20, ion_density_alpha=1, ion_temperature_pedestal=6.09, ion_temperature_separatrix=0.1, ion_temperature_origin=45.9, ion_temperature_alpha=8.06, ion_temperature_beta=6, plasma_type=plasma, plasma_id=1, number_of_bins=100, minimum_toroidal_angle=0, maximum_toroidal_angle=360

examples/build_and_run_model.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
OpenMC Plasma Source example.
3+
4+
An example of how to generate a settings file containing the parameterised
5+
plasma source sampling library and how to generate the parameterisation in the
6+
OpenMC settings.xml file.
7+
"""
8+
9+
from parametric_plasma_source import PlasmaSource, SOURCE_SAMPLING_PATH
10+
import openmc
11+
12+
plasma_params = {
13+
"elongation": 1.557,
14+
"ion_density_origin": 1.09e20,
15+
"ion_density_peaking_factor": 1,
16+
"ion_density_pedestal": 1.09e20,
17+
"ion_density_separatrix": 3e19,
18+
"ion_temperature_origin": 45.9,
19+
"ion_temperature_peaking_factor": 8.06,
20+
"ion_temperature_pedestal": 6.09,
21+
"ion_temperature_separatrix": 0.1,
22+
"major_radius": 9.06,
23+
"minor_radius": 2.92258,
24+
"pedestal_radius": 0.8 * 2.92258,
25+
"plasma_id": 1,
26+
"shafranov_shift": 0.44789,
27+
"triangularity": 0.270,
28+
"ion_temperature_beta": 6,
29+
}
30+
31+
plasma = PlasmaSource(**plasma_params)
32+
33+
# Create a single material
34+
iron = openmc.Material()
35+
iron.set_density("g/cm3", 5.0)
36+
iron.add_element("Fe", 1.0)
37+
mats = openmc.Materials([iron])
38+
39+
# Create a 5 cm x 5 cm box filled with iron
40+
cells = []
41+
inner_box1 = openmc.ZCylinder(r=600.0)
42+
inner_box2 = openmc.ZCylinder(r=1400.0)
43+
outer_box = openmc.model.rectangular_prism(4000.0, 4000.0, boundary_type="vacuum")
44+
cells += [openmc.Cell(fill=iron, region=-inner_box1)]
45+
cells += [openmc.Cell(fill=None, region=+inner_box1 & -inner_box2)]
46+
cells += [openmc.Cell(fill=iron, region=+inner_box2 & outer_box)]
47+
geometry = openmc.Geometry(cells)
48+
49+
# Tell OpenMC we're going to use our custom source
50+
settings = openmc.Settings()
51+
settings.run_mode = "fixed source"
52+
settings.batches = 10
53+
settings.particles = 1000
54+
source = openmc.Source()
55+
source.library = SOURCE_SAMPLING_PATH
56+
source.parameters = str(plasma)
57+
settings.source = source
58+
59+
# Finally, define a mesh tally so that we can see the resulting flux
60+
mesh = openmc.RegularMesh()
61+
mesh.lower_left = (-2000.0, -2000.0)
62+
mesh.upper_right = (2000.0, 2000.0)
63+
mesh.dimension = (50, 50)
64+
65+
tally = openmc.Tally()
66+
tally.filters = [openmc.MeshFilter(mesh)]
67+
tally.scores = ["flux"]
68+
tallies = openmc.Tallies([tally])
69+
70+
model = openmc.model.Model(
71+
materials=mats, geometry=geometry, settings=settings, tallies=tallies
72+
)
73+
74+
model.run()

examples/plot_flux.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import matplotlib as mpl
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
import openmc
5+
6+
mpl.use("TkAgg")
7+
8+
# Get the flux from the statepoint
9+
with openmc.StatePoint("statepoint.10.h5") as sp:
10+
flux = np.log(sp.get_tally(scores=["flux"]).mean)
11+
flux.shape = (50, 50)
12+
13+
# Plot the flux
14+
fig, ax = plt.subplots()
15+
ax.imshow(flux, origin="lower", extent=(-2000.0, 2000.0, -2000.0, 2000.0))
16+
ax.set_xlabel("x [cm]")
17+
ax.set_ylabel("y [cm]")
18+
plt.show()

parametric_plasma_source/Makefile

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import os
2+
3+
PLASMA_SOURCE_PATH = os.path.dirname(__file__)
4+
SOURCE_SAMPLING_PATH = os.sep.join([PLASMA_SOURCE_PATH, "source_sampling.so"])
5+
6+
try:
7+
from .plasma_source import *
8+
except ImportError:
9+
print("The plasma_source module could not be found. Please compile before using.")

parametric_plasma_source/build_python.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)