From c4984da6045241cf7bbb879f330c76f9fa88450a Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:11:54 +0200 Subject: [PATCH 1/7] added test to reproduce the error --- tests/test_tokamak_source.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/test_tokamak_source.py b/tests/test_tokamak_source.py index 9f6f962..89ec3fc 100644 --- a/tests/test_tokamak_source.py +++ b/tests/test_tokamak_source.py @@ -2,6 +2,8 @@ from openmc import Source +import pytest + def test_creation(): my_source = TokamakSource( @@ -22,7 +24,29 @@ def test_creation(): triangularity=0.270, ion_temperature_beta=6 ) - my_source.sample_sources() - my_source.make_openmc_sources() 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 + """ + my_source = TokamakSource( + elongation=1.557, + ion_density_centre=1.09e20, + ion_density_peaking_factor=1, + ion_density_pedestal=1.09e20, + ion_density_separatrix=3e19, + ion_temperature_centre=45.9, + ion_temperature_peaking_factor=8.06, + ion_temperature_pedestal=6.09, + ion_temperature_separatrix=0.1, + major_radius=9.06, + minor_radius=2.92258, + pedestal_radius=0.8 * 2.92258, + mode="H", + shafranov_factor=0.44789, + triangularity=0.270, + ion_temperature_beta=6 + ) + assert pytest.approx(sum(my_source.strengths), 1) From f2150215f536640fa8d93b23d5376f324ebc0bad Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:12:00 +0200 Subject: [PATCH 2/7] normalised strengths --- openmc_plasma_source/tokamak_source.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openmc_plasma_source/tokamak_source.py b/openmc_plasma_source/tokamak_source.py index 9c91b87..11f4fcc 100644 --- a/openmc_plasma_source/tokamak_source.py +++ b/openmc_plasma_source/tokamak_source.py @@ -180,7 +180,9 @@ def sample_sources(self): # convert coordinates self.densities = self.ion_density(a) self.temperatures = self.ion_temperature(a) - self.strengths = strength(self.densities, self.temperatures) + self.neutron_source_density = 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, angles=(0., 2*np.pi)): @@ -226,7 +228,7 @@ def make_openmc_sources(self, angles=(0., 2*np.pi)): return sources -def strength(ion_density, ion_temperature): +def neutron_source_density(ion_density, ion_temperature): """Computes the neutron source density given ion density and ion temperature. @@ -266,5 +268,3 @@ def DT_xs(ion_temperature): val = c[0]/(U**(5/6)*ion_temperature**(2/3)) val *= np.exp(-c[1]*(U/ion_temperature)**(1/3)) return val - - From aa12994cf84e6ddc7b1a63afee7f566e7580b19f Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:13:19 +0200 Subject: [PATCH 3/7] adapted plotting --- openmc_plasma_source/plotting/plot_tokamak_source.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/openmc_plasma_source/plotting/plot_tokamak_source.py b/openmc_plasma_source/plotting/plot_tokamak_source.py index d289828..eba7f82 100644 --- a/openmc_plasma_source/plotting/plot_tokamak_source.py +++ b/openmc_plasma_source/plotting/plot_tokamak_source.py @@ -19,7 +19,8 @@ def scatter_tokamak_source(source, quantity=None, **kwargs): quantity_to_attribute = { "ion_temperature": source.temperatures, - "neutron_source_density": source.strengths + "neutron_source_density": source.neutron_source_density, + "strength": source.strengths, } if quantity in quantity_to_attribute: colours = quantity_to_attribute[quantity] @@ -52,7 +53,8 @@ def plot_tokamak_source_3D(source, quantity=None, angles=[0, 1/2*np.pi], colorba quantity_to_attribute = { "ion_temperature": source.temperatures, - "neutron_source_density": source.strengths + "neutron_source_density": source.neutron_source_density, + "strength": source.strengths, } if quantity in quantity_to_attribute: values = quantity_to_attribute[quantity] From 881097bb34ab71f803dbd7ae764af2531d2f527c Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:47:13 +0200 Subject: [PATCH 4/7] new test --- tests/test_tokamak_source.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_tokamak_source.py b/tests/test_tokamak_source.py index 89ec3fc..37aab63 100644 --- a/tests/test_tokamak_source.py +++ b/tests/test_tokamak_source.py @@ -50,3 +50,27 @@ def test_strengths_are_normalised(): ion_temperature_beta=6 ) assert pytest.approx(sum(my_source.strengths), 1) + + +def test_angles(): + """Checks that custom angles can be set + """ + my_source = TokamakSource( + elongation=1.557, + ion_density_centre=1.09e20, + ion_density_peaking_factor=1, + ion_density_pedestal=1.09e20, + ion_density_separatrix=3e19, + ion_temperature_centre=45.9, + ion_temperature_peaking_factor=8.06, + ion_temperature_pedestal=6.09, + ion_temperature_separatrix=0.1, + major_radius=9.06, + minor_radius=2.92258, + pedestal_radius=0.8 * 2.92258, + mode="H", + shafranov_factor=0.44789, + triangularity=0.270, + ion_temperature_beta=6, + angles=(0, 1) + ) From f35a0990bfd22e4dea42e35bcb3cc6c1ef555332 Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:47:42 +0200 Subject: [PATCH 5/7] added angles to the classes --- openmc_plasma_source/ring_source.py | 9 ++++----- openmc_plasma_source/tokamak_source.py | 8 ++++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/openmc_plasma_source/ring_source.py b/openmc_plasma_source/ring_source.py index 7770482..216d193 100644 --- a/openmc_plasma_source/ring_source.py +++ b/openmc_plasma_source/ring_source.py @@ -1,5 +1,6 @@ import openmc +import numpy as np class FusionRingSource(openmc.Source): @@ -10,16 +11,14 @@ class FusionRingSource(openmc.Source): Args: radius: the inner radius of the ring source - start_angle: the start angle of the ring in radians, - stop_angle: the end angle of the ring in radians, + angles: the start and stop angles of the ring in radians, 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, - start_angle: float =0., - stop_angle: float = 6.28318530718, + angles=(0, 2*np.pi), z_placement=0, temperature: float = 20000., fuel='DT' @@ -30,7 +29,7 @@ def __init__( # performed after the super init as these are Source attributes radius = openmc.stats.Discrete([radius], [1]) z_values = openmc.stats.Discrete([z_placement], [1]) - angle = openmc.stats.Uniform(a=start_angle, b=stop_angle) + angle = openmc.stats.Uniform(a=angles[0], b=angles[1]) self.space = openmc.stats.CylindricalIndependent( r=radius, phi=angle, diff --git a/openmc_plasma_source/tokamak_source.py b/openmc_plasma_source/tokamak_source.py index 11f4fcc..7f9c4fd 100644 --- a/openmc_plasma_source/tokamak_source.py +++ b/openmc_plasma_source/tokamak_source.py @@ -40,6 +40,7 @@ class TokamakSource(): shafranov_factor (float): Shafranov factor (referred in [1] as esh) also known as outward radial displacement of magnetic surfaces (m) + angles: the start and stop angles of the ring in radians, sample_size (int, optional): number of neutron sources. Defaults to 1000. """ @@ -61,6 +62,7 @@ def __init__( ion_temperature_separatrix, pedestal_radius, shafranov_factor, + angles=(0, 2*np.pi), sample_size=1000 ) -> None: self.major_radius = major_radius @@ -84,6 +86,8 @@ def __init__( self.sample_size = sample_size + self.angles = angles + self.sample_sources() self.sources = self.make_openmc_sources() @@ -185,7 +189,7 @@ def sample_sources(self): 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, angles=(0., 2*np.pi)): + def make_openmc_sources(self): """Creates a list of OpenMC Sources() objects. The created sources are ring sources based on the .RZ coordinates between two angles. The energy of the sources are Muir energy spectra with ion temperatures @@ -209,7 +213,7 @@ def make_openmc_sources(self, angles=(0., 2*np.pi)): # extract the RZ values accordingly radius = openmc.stats.Discrete([self.RZ[0][i]], [1]) z_values = openmc.stats.Discrete([self.RZ[1][i]], [1]) - angle = openmc.stats.Uniform(a=angles[0], b=angles[1]) + angle = openmc.stats.Uniform(a=self.angles[0], b=self.angles[1]) # create a ring source my_source.space = openmc.stats.CylindricalIndependent( From 0ae6960f106f05d9979ff98ffa4b02a0dde9e29c Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:50:20 +0200 Subject: [PATCH 6/7] added type to docstrings --- openmc_plasma_source/ring_source.py | 2 +- openmc_plasma_source/tokamak_source.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/openmc_plasma_source/ring_source.py b/openmc_plasma_source/ring_source.py index 216d193..15030df 100644 --- a/openmc_plasma_source/ring_source.py +++ b/openmc_plasma_source/ring_source.py @@ -11,7 +11,7 @@ class FusionRingSource(openmc.Source): Args: radius: the inner radius of the ring source - angles: the start and stop angles of the ring in radians, + angles (iterable of floats): the start and stop angles of the ring in radians, z_placement: Location of the ring source (m). Defaults to 0. temperature: the temperature to use in the Muir distribution in eV, """ diff --git a/openmc_plasma_source/tokamak_source.py b/openmc_plasma_source/tokamak_source.py index 7f9c4fd..ef4b512 100644 --- a/openmc_plasma_source/tokamak_source.py +++ b/openmc_plasma_source/tokamak_source.py @@ -40,7 +40,8 @@ class TokamakSource(): shafranov_factor (float): Shafranov factor (referred in [1] as esh) also known as outward radial displacement of magnetic surfaces (m) - angles: the start and stop angles of the ring in radians, + angles (iterable of floats): the start and stop angles of the ring in + radians sample_size (int, optional): number of neutron sources. Defaults to 1000. """ From e699d09d1dca49d22fd4313b09e11f1671e0ad9c Mon Sep 17 00:00:00 2001 From: remdelaportemathurin Date: Wed, 29 Sep 2021 15:51:37 +0200 Subject: [PATCH 7/7] fixed example --- examples/ring_source_example.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/ring_source_example.py b/examples/ring_source_example.py index 76c82fb..753fbd7 100644 --- a/examples/ring_source_example.py +++ b/examples/ring_source_example.py @@ -4,8 +4,7 @@ my_source = FusionRingSource( radius = 700, - start_angle =0., - stop_angle = 6.28318530718, + angles =(0., 6.28318530718), # 360deg source temperature = 20000., )