Skip to content

Commit

Permalink
adapted tests for new api
Browse files Browse the repository at this point in the history
  • Loading branch information
shimwell committed Mar 6, 2024
1 parent 6dfd64c commit a70b018
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 91 deletions.
4 changes: 2 additions & 2 deletions src/openmc_plasma_source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/openmc_plasma_source/fuel_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())):

Expand All @@ -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,
Expand All @@ -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())):
Expand Down
49 changes: 27 additions & 22 deletions tests/test_fuel_types.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 6 additions & 6 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down
16 changes: 8 additions & 8 deletions tests/test_point_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)


Expand All @@ -33,32 +33,32 @@ 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


@pytest.mark.parametrize("temperature", [-20000.0, "hello world", [10000]])
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


@pytest.mark.parametrize("fuel", ["топливо", 5])
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)
22 changes: 11 additions & 11 deletions tests/test_ring_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,21 +20,21 @@ 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


@pytest.mark.parametrize("radius", [-1.0, "hello world", [1e5]])
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)


Expand All @@ -43,38 +43,38 @@ 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


@pytest.mark.parametrize("temperature", [-20000.0, "hello world", [10000]])
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


@pytest.mark.parametrize("fuel", ["топливо", 5])
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)
Loading

0 comments on commit a70b018

Please sign in to comment.