diff --git a/src/openmc_plasma_source/__init__.py b/src/openmc_plasma_source/__init__.py index ac1a8b6..8a1ddda 100644 --- a/src/openmc_plasma_source/__init__.py +++ b/src/openmc_plasma_source/__init__.py @@ -13,5 +13,5 @@ from .fuel_types import get_neutron_energy_distribution from .point_source import fusion_point_source -from .ring_source import FusionRingSource -from .tokamak_source import TokamakSource +from .ring_source import fusion_ring_source +from .tokamak_source import tokamak_source diff --git a/src/openmc_plasma_source/fuel_types.py b/src/openmc_plasma_source/fuel_types.py index 8a1e26d..45e6ac6 100644 --- a/src/openmc_plasma_source/fuel_types.py +++ b/src/openmc_plasma_source/fuel_types.py @@ -49,8 +49,10 @@ def get_neutron_energy_distribution( E_pspec = np.linspace(0, 12, num_of_vals) # E_pspec is exspected in MeV units DTmean, DTvar = nst.DTprimspecmoments(ion_temperature_kev) - print("DTmean", DTmean) + # print("DTmean", DTmean) DDmean, DDvar = nst.DDprimspecmoments(ion_temperature_kev) + # print("DDmean", DDmean) + #todo make use of these DTvar and DDvar values in muir or gaussian distribution if ["D", "T"] == sorted(set(fuel.keys())): @@ -64,8 +66,8 @@ def get_neutron_energy_distribution( dNdE_TT = strength_TT * nst.dNdE_TT(E_pspec, ion_temperature_kev) tt_source = openmc.stats.Tabular(E_pspec * 1e6, dNdE_TT) - dd_source = openmc.stats.muir(e0=2.5e6, m_rat=4, kt=ion_temperature) - dt_source = openmc.stats.muir(e0=14.06e6, m_rat=5, kt=ion_temperature) + dd_source = openmc.stats.muir(e0=DDmean*1e6, m_rat=4, kt=ion_temperature) + dt_source = openmc.stats.muir(e0=DTmean*1e6, m_rat=5, kt=ion_temperature) # todo look into combining distributions openmc.data.combine_distributions() return [tt_source, dd_source, dt_source], [ strength_TT, @@ -76,7 +78,7 @@ def get_neutron_energy_distribution( elif ["D"] == sorted(set(fuel.keys())): strength_DD = 1.0 - dd_source = openmc.stats.muir(e0=2.5e6, m_rat=4, kt=ion_temperature) + dd_source = openmc.stats.muir(e0=DDmean*1e6, m_rat=4, kt=ion_temperature) return [dd_source], [strength_DD] elif ["T"] == sorted(set(fuel.keys())): diff --git a/tests/test_fuel_types.py b/tests/test_fuel_types.py index 110ff02..17dc1c5 100644 --- a/tests/test_fuel_types.py +++ b/tests/test_fuel_types.py @@ -1,33 +1,38 @@ import pytest -from openmc_plasma_source.fuel_types import Fuel, fuel_types +from openmc_plasma_source import get_neutron_energy_distribution -@pytest.mark.parametrize("energy,mass", [(2.5e7, 5), (15, 30)]) -def test_fuel_with_correct_inputs(energy, mass): +@pytest.mark.parametrize("temperature, fuel", [ + (2e3, {'D': 1.}), + (2e3, {'T': 1.}), + (2e3, {'T': 0.5, 'D': 0.5}), + (2e3, {'T': 0.2, 'D': 0.8}), +]) +def test_fuel_with_correct_inputs(temperature, fuel): # Should accept any non-zero positive inputs to either variable - fuel = Fuel(energy, mass) - assert fuel.mean_energy == energy - assert fuel.mass_of_reactants == mass + get_neutron_energy_distribution(temperature, fuel) -@pytest.mark.parametrize( - "energy,mass", [(2.5e7, -5), (-12, 30), (1e7, 0), (0, 4), (-12, -12)] -) -def test_fuel_with_bad_inputs(energy, mass): +@pytest.mark.parametrize("temperature, fuel", [ + (2e3, {'D': 1.1}), + (2e3, {'T': 0.9}), + (2e3, {'T': -0.5, 'D': 0.5}), + (2e3, {'T': -0.2, 'D': -0.8}), +]) +def test_fuel_with_bad_inputs(temperature, fuel): # Should reject any negative numbers and zeros. with pytest.raises(ValueError): - fuel = Fuel(energy, mass) + get_neutron_energy_distribution(temperature, fuel) -@pytest.mark.parametrize("fuel_type", ["DT", "DD"]) -def test_fuel_types(fuel_type): - # Should accept 'DD' and 'DT' - assert isinstance(fuel_types[fuel_type], Fuel) - - -@pytest.mark.parametrize("fuel_type", ["dt", "dd", "Dt", "dD", "hello world", 5]) -def test_incorrect_fuel_types(fuel_type): - # Should reject everything except 'DT' and 'DD' - with pytest.raises(KeyError): - my_fuel = fuel_types[fuel_type] +@pytest.mark.parametrize("temperature, fuel", [ + (2e3, {'DD': 1.1}), + (2e3, {'DT': 0.9}), + (2e3, {'He3': -0.5, 'D': 0.5}), + (2e3, {1: -0.2, 'D': -0.8}), +]) +def test_fuel_with_incorrect_isotopese(temperature, fuel): + # Should reject anything which is not 'D' or 'T'. + with pytest.raises(ValueError): + get_neutron_energy_distribution(temperature, fuel) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 4f11a15..8027ea0 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -2,13 +2,13 @@ import numpy as np import pytest -from openmc_plasma_source import TokamakSource +from openmc_plasma_source import tokamak_source from openmc_plasma_source import plotting as ops_plt @pytest.fixture def tokamak_source(): - return TokamakSource( + return tokamak_source( elongation=1.557, ion_density_centre=1.09e20, ion_density_peaking_factor=1, @@ -128,13 +128,13 @@ def test_scatter_tokamak_source_kwargs(tokamak_source, kwargs): def test_scatter_tokamak_not_source(): - """Ensure failure when given non-TokamakSource to plot""" + """Ensure failure when given non-tokamak_source to plot""" with pytest.raises(ValueError) as excinfo: fig = plt.figure() ax = fig.gca() ops_plt.scatter_tokamak_source("hello world", ax=ax) plt.close() - assert "TokamakSource" in str(excinfo.value) + assert "tokamak_source" in str(excinfo.value) @pytest.mark.parametrize("quantity", ["coucou", "ion_density", 17]) @@ -241,13 +241,13 @@ def test_plot_tokamak_source_3D_kwargs(tokamak_source, kwargs): def test_plot_tokamak_source_3D_not_source(): - """Ensure failure when given non-TokamakSource to plot""" + """Ensure failure when given non-tokamak_source to plot""" with pytest.raises(ValueError) as excinfo: fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection="3d") ops_plt.plot_tokamak_source_3D("hello world", ax=ax) plt.close() - assert "TokamakSource" in str(excinfo.value) + assert "tokamak_source" in str(excinfo.value) @pytest.mark.parametrize("quantity", ["coucou", "ion_density", 17]) diff --git a/tests/test_point_source.py b/tests/test_point_source.py index 0c383d3..9533f66 100644 --- a/tests/test_point_source.py +++ b/tests/test_point_source.py @@ -2,11 +2,11 @@ import openmc import pytest -from openmc_plasma_source import FusionPointSource +from openmc_plasma_source import fusion_point_source def test_creation(): - my_source = FusionPointSource() + my_source = fusion_point_source() # Ensure it is of type openmc.IndependentSource assert isinstance(my_source, openmc.IndependentSource) @@ -22,7 +22,7 @@ def test_creation(): ) def test_coordinate(coordinate): # Should allow any tuple of length 3 containing numbers - my_source = FusionPointSource(coordinate=coordinate) + my_source = fusion_point_source(coordinate=coordinate) assert np.array_equal(my_source.coordinate, coordinate) @@ -33,13 +33,13 @@ def test_bad_coordinate(coordinate): # Should reject iterables of length != 3, anything non-tuple, and anything # that can't convert to float with pytest.raises(ValueError): - FusionPointSource(coordinate=coordinate) + fusion_point_source(coordinate=coordinate) @pytest.mark.parametrize("temperature", [20000, 1e4, 0.1, 25000.0]) def test_temperature(temperature): # Should accept any positive float - my_source = FusionPointSource(temperature=temperature) + my_source = fusion_point_source(temperature=temperature) assert my_source.temperature == temperature @@ -47,13 +47,13 @@ def test_temperature(temperature): def test_bad_temperature(temperature): # Should reject negative floats and anything that isn't convertible to float with pytest.raises(ValueError): - FusionPointSource(temperature=temperature) + fusion_point_source(temperature=temperature) @pytest.mark.parametrize("fuel", ["DT", "DD"]) def test_fuel(fuel): # Should accept either 'DD' or 'DT' - my_source = FusionPointSource(fuel=fuel) + my_source = fusion_point_source(fuel=fuel) assert my_source.fuel_type == fuel @@ -61,4 +61,4 @@ def test_fuel(fuel): def test_wrong_fuel(fuel): # Should reject fuel types besides those listed in fuel_types.py with pytest.raises((KeyError, TypeError)): - FusionPointSource(fuel=fuel) + fusion_point_source(fuel=fuel) diff --git a/tests/test_ring_source.py b/tests/test_ring_source.py index a8c8158..1c6d758 100644 --- a/tests/test_ring_source.py +++ b/tests/test_ring_source.py @@ -2,11 +2,11 @@ import openmc import pytest -from openmc_plasma_source import FusionRingSource +from openmc_plasma_source import fusion_ring_source def test_creation(): - my_source = FusionRingSource(radius=1.0, z_placement=1.0) + my_source = fusion_ring_source(radius=1.0, z_placement=1.0) # Ensure it is of type openmc.IndependentSource assert isinstance(my_source, openmc.IndependentSource) @@ -20,7 +20,7 @@ def test_creation(): @pytest.mark.parametrize("radius", [1, 5.6, 1e5, 7.0]) def test_radius(radius): # should allow any positive float - my_source = FusionRingSource(radius=radius) + my_source = fusion_ring_source(radius=radius) assert my_source.radius == radius @@ -28,13 +28,13 @@ def test_radius(radius): def test_bad_radius(radius): # should reject any negative float or anything not convertible to float with pytest.raises(ValueError): - my_source = FusionRingSource(radius=radius) + fusion_ring_source(radius=radius) @pytest.mark.parametrize("angles", [(1, 2), (0.0, np.pi), (np.pi, 0.0)]) def test_angles(angles): # Should allow any tuple of length 2 with contents convertible to float - my_source = FusionRingSource(radius=1.0, angles=angles) + my_source = fusion_ring_source(radius=1.0, angles=angles) assert np.array_equal(my_source.angles, angles) @@ -43,13 +43,13 @@ def test_bad_angles(angles): # Should reject iterables of length != 2, anything non tuple, and anything # that can't convert to float with pytest.raises(ValueError): - FusionRingSource(radius=1.0, angles=angles) + fusion_ring_source(radius=1.0, angles=angles) @pytest.mark.parametrize("temperature", [20000.0, 1e4, 0.1, 25000]) def test_temperature(temperature): # Should accept any positive float - my_source = FusionRingSource(radius=1.0, temperature=temperature) + my_source = fusion_ring_source(radius=1.0, temperature=temperature) assert my_source.temperature == temperature @@ -57,13 +57,13 @@ def test_temperature(temperature): def test_bad_temperature(temperature): # Should reject negative floats and anything that isn't convertible to float with pytest.raises(ValueError): - FusionRingSource(radius=1.0, temperature=temperature) + fusion_ring_source(radius=1.0, temperature=temperature) @pytest.mark.parametrize("fuel", ["DT", "DD"]) def test_fuel(fuel): # Should accept either 'DD' or 'DT' - my_source = FusionRingSource(radius=1.0, fuel=fuel) + my_source = fusion_ring_source(radius=1.0, fuel=fuel) assert my_source.fuel_type == fuel @@ -71,10 +71,10 @@ def test_fuel(fuel): def test_wrong_fuel(fuel): # Should reject fuel types besides those listed in fuel_types.py with pytest.raises((KeyError, TypeError)): - FusionRingSource(radius=1.0, fuel=fuel) + fusion_ring_source(radius=1.0, fuel=fuel) @pytest.mark.parametrize("z", ["coucou", [5, 2.0]]) def test_wrong_z_placement(z): with pytest.raises((TypeError)): - FusionRingSource(radius=1.0, z_placement=z) + fusion_ring_source(radius=1.0, z_placement=z) diff --git a/tests/test_tokamak_source.py b/tests/test_tokamak_source.py index 8e557db..28845c4 100644 --- a/tests/test_tokamak_source.py +++ b/tests/test_tokamak_source.py @@ -1,15 +1,14 @@ import numpy as np import pytest -from hypothesis import assume, given, settings +from hypothesis import given, settings from hypothesis import strategies as st -from openmc import IndependentSource - -from openmc_plasma_source import TokamakSource +import openmc +from openmc_plasma_source import tokamak_source @pytest.fixture def tokamak_args_dict(): - """Returns a dict of realistic inputs for TokamakSource""" + """Returns a dict of realistic inputs for tokamak_source""" args_dict = { "elongation": 1.557, "triangularity": 0.270, @@ -33,51 +32,49 @@ def tokamak_args_dict(): @pytest.fixture def tokamak_source_example(tokamak_args_dict): - """Returns a TokamakSource with realistic inputs""" - return TokamakSource(**tokamak_args_dict) + """Returns a tokamak_source with realistic inputs""" + return tokamak_source(**tokamak_args_dict) def test_creation(tokamak_source_example): - """Tests that the sources generated by TokamakSource are of + """Tests that the sources generated by tokamak_source are of type openmc.Source""" for source in tokamak_source_example.sources: - assert isinstance(source, IndependentSource) + assert isinstance(source, openmc.IndependentSource) @pytest.mark.parametrize( "minor_radius,major_radius", [(3.0, 10.0), (3.0, 100), (3.0, 3.00001)] ) def test_major_radius(tokamak_args_dict, minor_radius, major_radius): - """Checks that TokamakSource creation accepts valid major radius""" + """Checks that tokamak_source creation accepts valid major radius""" tokamak_args_dict["minor_radius"] = minor_radius tokamak_args_dict["major_radius"] = major_radius - tokamak_source = TokamakSource(**tokamak_args_dict) - assert tokamak_source.major_radius == major_radius + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( "minor_radius,major_radius", [(3, 3), (3, 1), (3, -5), (3, "hello world")] ) def test_bad_major_radius(tokamak_args_dict, minor_radius, major_radius): - """Checks that TokamakSource creation rejects invalid major radius""" + """Checks that tokamak_source creation rejects invalid major radius""" tokamak_args_dict["minor_radius"] = minor_radius tokamak_args_dict["major_radius"] = major_radius with pytest.raises(ValueError): - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( "major_radius,minor_radius", [(10.0, 3.0), (10.0, 9.9), (10.0, 0.1)] ) def test_minor_radius(tokamak_args_dict, major_radius, minor_radius): - """Checks that TokamakSource creation accepts valid minor radius""" + """Checks that tokamak_source creation accepts valid minor radius""" tokamak_args_dict["major_radius"] = major_radius tokamak_args_dict["minor_radius"] = minor_radius # Set shafranov factor to 0 and pedestal factor to 0.8*minor_radius for safety tokamak_args_dict["pedestal_radius"] = 0.8 * minor_radius tokamak_args_dict["shafranov_factor"] = 0.0 - tokamak_source = TokamakSource(**tokamak_args_dict) - assert tokamak_source.minor_radius == minor_radius + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( @@ -85,43 +82,41 @@ def test_minor_radius(tokamak_args_dict, major_radius, minor_radius): [(10.0, 10.0), (10.0, 20.0), (10.0, 0), (10.0, -6), (10.0, "hello world")], ) def test_bad_minor_radius(tokamak_args_dict, major_radius, minor_radius): - """Checks that TokamakSource creation rejects invalid minor radius""" + """Checks that tokamak_source creation rejects invalid minor radius""" tokamak_args_dict["major_radius"] = major_radius tokamak_args_dict["minor_radius"] = minor_radius with pytest.raises(ValueError): - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize("elongation", [1.0, 1.667, 0.5, 20, 0.001]) def test_elongation(tokamak_args_dict, elongation): - """Checks that TokamakSource creation accepts valid elongation""" + """Checks that tokamak_source creation accepts valid elongation""" tokamak_args_dict["elongation"] = elongation - tokamak_source = TokamakSource(**tokamak_args_dict) - assert tokamak_source.elongation == elongation + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize("elongation", [0, -5, "hello world"]) def test_bad_elongation(tokamak_args_dict, elongation): - """Checks that TokamakSource creation rejects invalid elongation""" + """Checks that tokamak_source creation rejects invalid elongation""" tokamak_args_dict["elongation"] = elongation with pytest.raises(ValueError): - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize("triangularity", [0.0, 0.5, 0.9, 1.0, -0.5, -0.9, -1.0]) def test_triangularity(tokamak_args_dict, triangularity): - """Checks that TokamakSource creation accepts valid triangularity""" + """Checks that tokamak_source creation accepts valid triangularity""" tokamak_args_dict["triangularity"] = triangularity - tokamak_source = TokamakSource(**tokamak_args_dict) - assert tokamak_source.triangularity == triangularity + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize("triangularity", [1.1, -1.1, 10, -10, "hello world"]) def test_bad_triangularity(tokamak_args_dict, triangularity): - """Checks that TokamakSource creation rejects invalid triangularity""" + """Checks that tokamak_source creation rejects invalid triangularity""" tokamak_args_dict["triangularity"] = triangularity with pytest.raises(ValueError): - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( @@ -137,13 +132,12 @@ def test_bad_triangularity(tokamak_args_dict, triangularity): ], ) def test_shafranov_factor(tokamak_args_dict, major_radius, minor_radius, shaf): - """Checks that TokamakSource creation accepts valid Shafranov factor""" + """Checks that tokamak_source creation accepts valid Shafranov factor""" tokamak_args_dict["major_radius"] = major_radius tokamak_args_dict["minor_radius"] = minor_radius tokamak_args_dict["pedestal_radius"] = 0.8 * minor_radius tokamak_args_dict["shafranov_factor"] = shaf - tokamak_source = TokamakSource(**tokamak_args_dict) - assert tokamak_source.shafranov_factor == shaf + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( @@ -158,13 +152,13 @@ def test_shafranov_factor(tokamak_args_dict, major_radius, minor_radius, shaf): ], ) def test_bad_shafranov_factor(tokamak_args_dict, major_radius, minor_radius, shaf): - """Checks that TokamakSource creation rejects invalid Shafranov factor""" + """Checks that tokamak_source creation rejects invalid Shafranov factor""" tokamak_args_dict["major_radius"] = major_radius tokamak_args_dict["minor_radius"] = minor_radius tokamak_args_dict["pedestal_radius"] = 0.8 * minor_radius tokamak_args_dict["shafranov_factor"] = shaf with pytest.raises((ValueError, TypeError)): - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) @pytest.mark.parametrize( @@ -174,7 +168,7 @@ def test_angles(tokamak_args_dict, angles): """Checks that custom angles can be set""" # Note: should accept negative angles and angles in reverse order tokamak_args_dict["angles"] = angles - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) assert np.array_equal(tokamak_source.angles, angles) for source in tokamak_source.sources: assert np.array_equal((source.space.phi.a, source.space.phi.b), angles) @@ -187,7 +181,7 @@ def test_bad_angles(tokamak_args_dict, angles): # Contents should convert to float tokamak_args_dict["angles"] = angles with pytest.raises(ValueError) as excinfo: - tokamak_source = TokamakSource(**tokamak_args_dict) + tokamak_source = tokamak_source(**tokamak_args_dict) def test_ion_density(tokamak_source_example): @@ -248,7 +242,7 @@ def test_bad_convert_a_alpha_to_R_Z(tokamak_source_example): @st.composite def tokamak_source_strategy(draw): - """Defines a hypothesis strategy that automatically generates a TokamakSource. + """Defines a hypothesis strategy that automatically generates a tokamak_source. Geometry attributes are varied, while plasma attributes are fixed. """ # Used to avoid generation of inappropriate float values @@ -293,7 +287,7 @@ def tokamak_source_strategy(draw): ) ) - return TokamakSource( + return tokamak_source( elongation=elongation, triangularity=triangularity, major_radius=major_radius,