From 76ccbf0e44a2522b00bfc4b77732e3c0309ec6c2 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 3 Nov 2021 16:02:13 +0000 Subject: [PATCH 1/3] allowingmplt above 3.2.2 to be installed --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index ee2aa4f..144e969 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,6 @@ python_requires='>=3.6', install_requires=[ 'numpy>=1.9', - 'matplotlib >= 3.4.3' + 'matplotlib >= 3.2.2' ], ) From af1b698048c26b84027130bdfd7f8ac19f08bb4f Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Wed, 3 Nov 2021 16:06:12 +0000 Subject: [PATCH 2/3] added auto pep formatting --- .github/workflows/black.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/black.yml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml new file mode 100644 index 0000000..4eee07f --- /dev/null +++ b/.github/workflows/black.yml @@ -0,0 +1,32 @@ +name: black + +on: + push: + paths: + - '**.py' + +defaults: + run: + shell: bash + +jobs: + black: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + - name: Install black + run: | + python -m pip install --upgrade pip + pip install black + - name: Run black + run: | + black . + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "[skip ci] Apply formatting changes" From 1a09eb2e4eee5bdf4af7da4cc93e4902e3c9538e Mon Sep 17 00:00:00 2001 From: shimwell Date: Wed, 10 Nov 2021 11:30:06 +0000 Subject: [PATCH 3/3] [skip ci] Apply formatting changes --- examples/point_source_example.py | 7 +- examples/ring_source_example.py | 7 +- examples/tokamak_source_example.py | 5 +- openmc_plasma_source/plotting/__init__.py | 2 +- .../plotting/plot_tokamak_source.py | 6 +- openmc_plasma_source/point_source.py | 18 ++-- openmc_plasma_source/ring_source.py | 25 +++-- openmc_plasma_source/tokamak_source.py | 92 ++++++++++++------- setup.py | 25 +++-- tests/test_plotting.py | 2 +- tests/test_point_source.py | 4 +- tests/test_ring_source.py | 3 +- tests/test_tokamak_source.py | 12 +-- 13 files changed, 114 insertions(+), 94 deletions(-) diff --git a/examples/point_source_example.py b/examples/point_source_example.py index 8e3e313..f8db0d9 100644 --- a/examples/point_source_example.py +++ b/examples/point_source_example.py @@ -1,12 +1,7 @@ - import openmc from openmc_plasma_source import FusionPointSource -my_source = FusionPointSource( - coordinate = (0,0,0), - temperature = 20000., - fuel='DT' -) +my_source = FusionPointSource(coordinate=(0, 0, 0), temperature=20000.0, fuel="DT") # Create a single material iron = openmc.Material() diff --git a/examples/ring_source_example.py b/examples/ring_source_example.py index 753fbd7..2f233eb 100644 --- a/examples/ring_source_example.py +++ b/examples/ring_source_example.py @@ -1,11 +1,10 @@ - import openmc from openmc_plasma_source import FusionRingSource my_source = FusionRingSource( - radius = 700, - angles =(0., 6.28318530718), # 360deg source - temperature = 20000., + radius=700, + angles=(0.0, 6.28318530718), # 360deg source + temperature=20000.0, ) # Create a single material diff --git a/examples/tokamak_source_example.py b/examples/tokamak_source_example.py index 395fd34..e311fe7 100644 --- a/examples/tokamak_source_example.py +++ b/examples/tokamak_source_example.py @@ -19,9 +19,8 @@ mode="H", shafranov_factor=0.44789, triangularity=0.270, - ion_temperature_beta=6 - ) - + ion_temperature_beta=6, +) # Create a single material diff --git a/openmc_plasma_source/plotting/__init__.py b/openmc_plasma_source/plotting/__init__.py index d4efefb..9ba21fa 100644 --- a/openmc_plasma_source/plotting/__init__.py +++ b/openmc_plasma_source/plotting/__init__.py @@ -1 +1 @@ -from .plot_tokamak_source import plot_tokamak_source_3D, scatter_tokamak_source \ No newline at end of file +from .plot_tokamak_source import plot_tokamak_source_3D, scatter_tokamak_source diff --git a/openmc_plasma_source/plotting/plot_tokamak_source.py b/openmc_plasma_source/plotting/plot_tokamak_source.py index eba7f82..8e7b22c 100644 --- a/openmc_plasma_source/plotting/plot_tokamak_source.py +++ b/openmc_plasma_source/plotting/plot_tokamak_source.py @@ -33,7 +33,9 @@ def scatter_tokamak_source(source, quantity=None, **kwargs): return plt.scatter(source.RZ[0], source.RZ[1], c=colours, **kwargs) -def plot_tokamak_source_3D(source, quantity=None, angles=[0, 1/2*np.pi], colorbar="viridis", **kwargs): +def plot_tokamak_source_3D( + source, quantity=None, angles=[0, 1 / 2 * np.pi], colorbar="viridis", **kwargs +): """Creates a 3D plot of the tokamak source. See https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot for more arguments. @@ -68,7 +70,7 @@ def plot_tokamak_source_3D(source, quantity=None, angles=[0, 1/2*np.pi], colorba theta = np.linspace(*angles, 100) for i in range(source.sample_size): if values is not None: - colour = colorbar(values[i]/max(values)) + colour = colorbar(values[i] / max(values)) else: colour = None x = source.RZ[0][i] * np.sin(theta) diff --git a/openmc_plasma_source/point_source.py b/openmc_plasma_source/point_source.py index d3c2924..74d01ca 100644 --- a/openmc_plasma_source/point_source.py +++ b/openmc_plasma_source/point_source.py @@ -1,4 +1,3 @@ - from typing import Tuple import openmc @@ -10,11 +9,12 @@ class FusionPointSource(openmc.Source): after initialization if required. Default isotropic point source at the origin with a Muir energy distribution. """ + def __init__( self, coordinate: Tuple[float, float, float] = (0, 0, 0), - temperature: float = 20000., - fuel: str = 'DT' + temperature: float = 20000.0, + fuel: str = "DT", ): super().__init__() @@ -22,12 +22,14 @@ def __init__( # performed after the super init as these are Source attributes self.space = openmc.stats.Point(coordinate) self.angle = openmc.stats.Isotropic() - if fuel == 'DT': - mean_energy = 14080000. # mean energy in eV + if fuel == "DT": + mean_energy = 14080000.0 # mean energy in eV mass_of_reactants = 5 # mass of the reactants (D + T) AMU - elif fuel == 'DD': - mean_energy = 2450000. # mean energy in eV + elif fuel == "DD": + mean_energy = 2450000.0 # mean energy in eV mass_of_reactants = 4 # mass of the reactants (D + D) AMU else: raise ValueError(f'fuel must be either "DT" or "DD", not {fuel}') - self.energy = openmc.stats.Muir(e0=mean_energy , m_rat=mass_of_reactants , kt=temperature) + self.energy = openmc.stats.Muir( + e0=mean_energy, m_rat=mass_of_reactants, kt=temperature + ) diff --git a/openmc_plasma_source/ring_source.py b/openmc_plasma_source/ring_source.py index 15030df..3581cab 100644 --- a/openmc_plasma_source/ring_source.py +++ b/openmc_plasma_source/ring_source.py @@ -1,4 +1,3 @@ - import openmc import numpy as np @@ -15,13 +14,14 @@ class FusionRingSource(openmc.Source): z_placement: Location of the ring source (m). Defaults to 0. temperature: the temperature to use in the Muir distribution in eV, """ + def __init__( self, radius, - angles=(0, 2*np.pi), + angles=(0, 2 * np.pi), z_placement=0, - temperature: float = 20000., - fuel='DT' + temperature: float = 20000.0, + fuel="DT", ): super().__init__() @@ -31,18 +31,17 @@ def __init__( z_values = openmc.stats.Discrete([z_placement], [1]) angle = openmc.stats.Uniform(a=angles[0], b=angles[1]) self.space = openmc.stats.CylindricalIndependent( - r=radius, - phi=angle, - z=z_values, - origin=(0.0, 0.0, 0.0) + r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0) ) self.angle = openmc.stats.Isotropic() - if fuel == 'DT': - mean_energy = 14080000. # mean energy in eV + if fuel == "DT": + mean_energy = 14080000.0 # mean energy in eV mass_of_reactants = 5 # mass of the reactants (D + T) AMU - elif fuel == 'DD': - mean_energy = 2450000. # mean energy in eV + elif fuel == "DD": + mean_energy = 2450000.0 # mean energy in eV mass_of_reactants = 4 # mass of the reactants (D + D) AMU else: raise ValueError(f'fuel must be either "DT" or "DD", not {fuel}') - self.energy = openmc.stats.Muir(e0=mean_energy , m_rat=mass_of_reactants , kt=temperature) + self.energy = openmc.stats.Muir( + e0=mean_energy, m_rat=mass_of_reactants, kt=temperature + ) diff --git a/openmc_plasma_source/tokamak_source.py b/openmc_plasma_source/tokamak_source.py index ef4b512..84ac5c4 100644 --- a/openmc_plasma_source/tokamak_source.py +++ b/openmc_plasma_source/tokamak_source.py @@ -1,7 +1,7 @@ import numpy as np -class TokamakSource(): +class TokamakSource: """Plasma neutron source sampling. This class greatly relies on models described in [1] @@ -45,6 +45,7 @@ class TokamakSource(): sample_size (int, optional): number of neutron sources. Defaults to 1000. """ + def __init__( self, major_radius, @@ -63,8 +64,8 @@ def __init__( ion_temperature_separatrix, pedestal_radius, shafranov_factor, - angles=(0, 2*np.pi), - sample_size=1000 + angles=(0, 2 * np.pi), + sample_size=1000, ) -> None: self.major_radius = major_radius self.minor_radius = minor_radius @@ -103,8 +104,10 @@ def ion_density(self, r): float, ndarray: ion density in m-3 """ if self.mode == "L": - density = self.ion_density_centre * \ - (1 - (r/self.major_radius)**2)**self.ion_density_peaking_factor + density = ( + self.ion_density_centre + * (1 - (r / self.major_radius) ** 2) ** self.ion_density_peaking_factor + ) elif self.mode in ["H", "A"]: # TODO: find an alternative to iterating through the array if isinstance(r, np.ndarray): @@ -112,12 +115,16 @@ def ion_density(self, r): for radius in r: if 0 < radius < self.pedestal_radius: prod = self.ion_density_centre - self.ion_density_pedestal - prod *= (1 - (radius/self.pedestal_radius)**2)**self.ion_density_peaking_factor + prod *= ( + 1 - (radius / self.pedestal_radius) ** 2 + ) ** self.ion_density_peaking_factor density_loc = self.ion_density_pedestal + prod else: prod = self.ion_density_pedestal - self.ion_density_separatrix - prod *= (self.major_radius - radius)/(self.major_radius - self.pedestal_radius) + prod *= (self.major_radius - radius) / ( + self.major_radius - self.pedestal_radius + ) density_loc = self.ion_density_separatrix + prod density.append(density_loc) density = np.array(density) @@ -134,21 +141,35 @@ def ion_temperature(self, r): float, ndarray: ion temperature (keV) """ if self.mode == "L": - temperature = self.ion_temperature_centre * \ - (1 - (r/self.major_radius)**2)**self.ion_temperature_peaking_factor + temperature = ( + self.ion_temperature_centre + * (1 - (r / self.major_radius) ** 2) + ** self.ion_temperature_peaking_factor + ) elif self.mode in ["H", "A"]: # TODO: find an alternative to iterating through the array if isinstance(r, np.ndarray): temperature = [] for radius in r: if 0 < radius < self.pedestal_radius: - prod = self.ion_temperature_centre - self.ion_temperature_pedestal - prod *= (1 - (radius/self.pedestal_radius)**self.ion_temperature_beta)**self.ion_temperature_peaking_factor + prod = ( + self.ion_temperature_centre - self.ion_temperature_pedestal + ) + prod *= ( + 1 + - (radius / self.pedestal_radius) + ** self.ion_temperature_beta + ) ** self.ion_temperature_peaking_factor temperature_loc = self.ion_temperature_pedestal + prod else: - prod = self.ion_temperature_pedestal - self.ion_temperature_separatrix - prod *= (self.major_radius - radius)/(self.major_radius - self.pedestal_radius) + prod = ( + self.ion_temperature_pedestal + - self.ion_temperature_separatrix + ) + prod *= (self.major_radius - radius) / ( + self.major_radius - self.pedestal_radius + ) temperature_loc = self.ion_temperature_separatrix + prod temperature.append(temperature_loc) temperature = np.array(temperature) @@ -165,29 +186,32 @@ def convert_a_alpha_to_R_Z(self, a, alpha): Returns: ((float, ndarray), (float, ndarray)): (R, Z) coordinates """ - shafranov_shift = self.shafranov_factor*(1.0-(a/self.minor_radius)**2) - R = self.major_radius + \ - a*np.cos(alpha + (self.triangularity*np.sin(alpha))) + \ - shafranov_shift - Z = self.elongation*a*np.sin(alpha) + shafranov_shift = self.shafranov_factor * (1.0 - (a / self.minor_radius) ** 2) + R = ( + self.major_radius + + a * np.cos(alpha + (self.triangularity * np.sin(alpha))) + + shafranov_shift + ) + Z = self.elongation * a * np.sin(alpha) return (R, Z) def sample_sources(self): """Samples self.sample_size neutrons and creates attributes .densities - (ion density), .temperatures (ion temperature), .strengths - (neutron source density) and .RZ (coordinates) + (ion density), .temperatures (ion temperature), .strengths + (neutron source density) and .RZ (coordinates) """ # create a sample of (a, alpha) coordinates - a = np.random.random(self.sample_size)*self.minor_radius - alpha = np.random.random(self.sample_size)*2*np.pi + a = np.random.random(self.sample_size) * self.minor_radius + alpha = np.random.random(self.sample_size) * 2 * np.pi # compute densities, temperatures, neutron source densities and # convert coordinates self.densities = self.ion_density(a) self.temperatures = self.ion_temperature(a) self.neutron_source_density = neutron_source_density( - self.densities, self.temperatures) - self.strengths = self.neutron_source_density/sum(self.neutron_source_density) + self.densities, self.temperatures + ) + self.strengths = self.neutron_source_density / sum(self.neutron_source_density) self.RZ = self.convert_a_alpha_to_R_Z(a, alpha) def make_openmc_sources(self): @@ -218,11 +242,13 @@ def make_openmc_sources(self): # create a ring source my_source.space = openmc.stats.CylindricalIndependent( - r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0)) + r=radius, phi=angle, z=z_values, origin=(0.0, 0.0, 0.0) + ) my_source.angle = openmc.stats.Isotropic() my_source.energy = openmc.stats.Muir( - e0=14080000.0, m_rat=5.0, kt=self.temperatures[i]) + e0=14080000.0, m_rat=5.0, kt=self.temperatures[i] + ) # the strength of the source (its probability) is given by # self.strengths @@ -244,7 +270,7 @@ def neutron_source_density(ion_density, ion_temperature): Returns: float, ndarray: Neutron source density (neutron/s/m3) """ - return ion_density**2*DT_xs(ion_temperature) + return ion_density ** 2 * DT_xs(ion_temperature) def DT_xs(ion_temperature): @@ -264,12 +290,12 @@ def DT_xs(ion_temperature): 2.5773408e-3, 6.1880463e-5, 6.6024089e-2, - 8.1215505e-3 - ] - prod = ion_temperature*(c[2]+ion_temperature*(c[3]-c[4]*ion_temperature)) - prod *= 1/(1.0+ion_temperature*(c[5]+c[6]*ion_temperature)) + 8.1215505e-3, + ] + prod = ion_temperature * (c[2] + ion_temperature * (c[3] - c[4] * ion_temperature)) + prod *= 1 / (1.0 + ion_temperature * (c[5] + c[6] * ion_temperature)) U = 1 - prod - val = c[0]/(U**(5/6)*ion_temperature**(2/3)) - val *= np.exp(-c[1]*(U/ion_temperature)**(1/3)) + val = c[0] / (U ** (5 / 6) * ion_temperature ** (2 / 3)) + val *= np.exp(-c[1] * (U / ion_temperature) ** (1 / 3)) return val diff --git a/setup.py b/setup.py index 144e969..fdc7383 100644 --- a/setup.py +++ b/setup.py @@ -14,22 +14,19 @@ url="https://github.com/fusion-energy/openmc-plasma-source", packages=setuptools.find_packages(), classifiers=[ - 'Natural Language :: English', - 'Topic :: Scientific/Engineering', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'License :: OSI Approved :: MIT License', - 'Operating System :: OS Independent', + "Natural Language :: English", + "Topic :: Scientific/Engineering", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", ], tests_require=[ "pytest", ], - python_requires='>=3.6', - install_requires=[ - 'numpy>=1.9', - 'matplotlib >= 3.2.2' - ], + python_requires=">=3.6", + install_requires=["numpy>=1.9", "matplotlib >= 3.2.2"], ) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index ade20bc..b381f84 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -19,7 +19,7 @@ shafranov_factor=0.44789, triangularity=0.270, ion_temperature_beta=6, - ) +) my_plasma.sample_sources() diff --git a/tests/test_point_source.py b/tests/test_point_source.py index 700c44d..ddf2ca5 100644 --- a/tests/test_point_source.py +++ b/tests/test_point_source.py @@ -3,10 +3,12 @@ from openmc import Source import pytest + def test_creation(): my_source = FusionPointSource() assert isinstance(my_source, Source) + def test_wrong_fuel(): with pytest.raises(ValueError): - FusionPointSource(fuel='топливо') + FusionPointSource(fuel="топливо") diff --git a/tests/test_ring_source.py b/tests/test_ring_source.py index c1d4e31..eed011b 100644 --- a/tests/test_ring_source.py +++ b/tests/test_ring_source.py @@ -8,6 +8,7 @@ def test_creation(): my_source = FusionRingSource(radius=1, z_placement=1) assert isinstance(my_source, Source) + def test_wrong_fuel(): with pytest.raises(ValueError): - FusionRingSource(radius=1, fuel='топливо') + FusionRingSource(radius=1, fuel="топливо") diff --git a/tests/test_tokamak_source.py b/tests/test_tokamak_source.py index 37aab63..957cc16 100644 --- a/tests/test_tokamak_source.py +++ b/tests/test_tokamak_source.py @@ -22,15 +22,14 @@ def test_creation(): mode="H", shafranov_factor=0.44789, triangularity=0.270, - ion_temperature_beta=6 + ion_temperature_beta=6, ) for source in my_source.make_openmc_sources(): assert isinstance(source, Source) def test_strengths_are_normalised(): - """Tests that the sum of the strengths attribute is equal to - """ + """Tests that the sum of the strengths attribute is equal to""" my_source = TokamakSource( elongation=1.557, ion_density_centre=1.09e20, @@ -47,14 +46,13 @@ def test_strengths_are_normalised(): mode="H", shafranov_factor=0.44789, triangularity=0.270, - ion_temperature_beta=6 + ion_temperature_beta=6, ) assert pytest.approx(sum(my_source.strengths), 1) def test_angles(): - """Checks that custom angles can be set - """ + """Checks that custom angles can be set""" my_source = TokamakSource( elongation=1.557, ion_density_centre=1.09e20, @@ -72,5 +70,5 @@ def test_angles(): shafranov_factor=0.44789, triangularity=0.270, ion_temperature_beta=6, - angles=(0, 1) + angles=(0, 1), )