Skip to content

Commit

Permalink
Add more unit tests (pynucastro#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
yut23 authored Nov 10, 2023
1 parent 57efb55 commit e54ed8f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
23 changes: 23 additions & 0 deletions pynucastro/networks/tests/test_python_custom_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,29 @@ def N14_p__O15__generic(rate_eval, tf):

assert r_custom.function_string_py() == func

def test_evaluate_ydots(self, pynet):
rho = 1.e4
T = 4e7
comp = pyna.Composition(pynet.unique_nuclei)
comp.set_equal()

ydots = pynet.evaluate_ydots(rho, T, comp)

ydots_ref = {
pyna.Nucleus("p"): -4.89131088e-06,
pyna.Nucleus("he4"): 4.84655620e-06,
pyna.Nucleus("c12"): 4.83556908e-06,
pyna.Nucleus("c13"): 9.87368448e-06,
pyna.Nucleus("n13"): -9.89635377e-06,
pyna.Nucleus("n14"): 7.79536222e-05,
pyna.Nucleus("n15"): 3.72390497e-05,
pyna.Nucleus("o14"): -7.79200769e-05,
pyna.Nucleus("o15"): -4.20854947e-05,
}

for n, value in ydots_ref.items():
assert ydots[n] == approx(value)

def test_import(self, pynet):
pynet.write_network("custom.py")
custom = importlib.import_module("custom")
Expand Down
38 changes: 38 additions & 0 deletions pynucastro/rates/tests/test_library.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# unit tests for rates

import pytest

import pynucastro as pyna


Expand Down Expand Up @@ -49,8 +51,44 @@ def test_get_num_rates(self):
assert self.library.get_num_rates() == 8

def test_get_rate(self):
# get by rate id
assert self.library.get_rate("c12 + p --> n13 <ls09_reaclib__>") == pyna.load_rate("c12-pg-n13-ls09")

# get by fname
assert self.library.get_rate("p_N14__O15") == pyna.load_rate("n14-pg-o15-im05")

# get by fname, lowercase
assert self.library.get_rate("p_n15__he4_c12") == pyna.load_rate("n15-pa-c12-nacr")

def test_get_rate_failure(self):
# missing rate id
with pytest.raises(LookupError):
self.library.get_rate("N15 + p --> O16 <li10_reaclib__>")

# missing fname
with pytest.raises(LookupError):
self.library.get_rate("F18__He4_N14")

# invalid rate
with pytest.raises(LookupError):
self.library.get_rate("this is not a rate")

def test_get_rate_by_nuclei(self):
assert self.library.get_rate_by_nuclei(
[pyna.Nucleus("p"), pyna.Nucleus("c13")], [pyna.Nucleus("n14")]
) == pyna.load_rate("c13-pg-n14-nacr")

assert self.library.get_rate_by_nuclei(
[pyna.Nucleus("p")], [pyna.Nucleus("n14")]
) is None

dup_rates = [pyna.load_rate("f17--o17-wc12"), pyna.load_rate("f17--o17-toki")]
dup_lib = self.library + pyna.Library(rates=dup_rates)

assert dup_lib.get_rate_by_nuclei(
[pyna.Nucleus("f17")], [pyna.Nucleus("o17")]
) == dup_rates

def test_diff(self):
diff_lib = self.library - self.smaller_lib
assert sorted(diff_lib.get_rates()) == sorted(self.removed_rates)
Expand Down
1 change: 0 additions & 1 deletion pynucastro/reduction/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Routines for nuclear reaction network reduction."""

from .drgep import drgep
from .reduction_utils import FailedMPIImport, mpi_importer, mpi_numpy_decomp
from .sensitivity_analysis import binary_search_trim, sens_analysis
3 changes: 2 additions & 1 deletion pynucastro/reduction/reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from pynucastro import Composition, Nucleus
from pynucastro.constants import constants
from pynucastro.reduction import drgep, mpi_importer, sens_analysis
from pynucastro.reduction import mpi_importer, sens_analysis
from pynucastro.reduction.drgep import drgep
from pynucastro.reduction.generate_data import dataset
from pynucastro.reduction.load_network import load_network

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Test the various helper functions in the reduction module."""

import pytest
from numpy.testing import assert_allclose
from pytest import approx

import pynucastro as pyna
from pynucastro.reduction.drgep import (calc_interaction_matrix,
calc_interaction_matrix_numpy,
get_adj_nuc)
from pynucastro.reduction import drgep, reduction


class TestHelpers:
class TestDrgepHelpers:
@pytest.fixture(scope="class")
def net(self, reaclib_library):
rate_names = ["c12(p,g)n13",
Expand Down Expand Up @@ -59,16 +60,16 @@ def test_interaction_matrix(self, net, comp):
]

rvals = net.evaluate_rates(rho=1e4, T=1e8, composition=comp)
r_AB = calc_interaction_matrix(net, rvals)
r_AB = drgep.calc_interaction_matrix(net, rvals)
assert_allclose(r_AB, expected, rtol=1e-10, atol=1e-100)

def test_interaction_matrix_numpy(self, net, comp):
rvals = net.evaluate_rates(rho=1e5, T=1e8, composition=comp)
rvals_arr = [rvals[r] for r in net.rates]
expected = calc_interaction_matrix(net, rvals)
expected = drgep.calc_interaction_matrix(net, rvals)

net.clear_arrays()
r_AB = calc_interaction_matrix_numpy(net, rvals_arr)
r_AB = drgep.calc_interaction_matrix_numpy(net, rvals_arr)

assert_allclose(r_AB, expected, rtol=1e-10, atol=1e-100)

Expand All @@ -94,5 +95,52 @@ def test_adj_nuc(self, net):
o15: {o15, n14, p, n15},
}

adj_nuc = get_adj_nuc(net)
adj_nuc = drgep.get_adj_nuc(net)
assert adj_nuc == expected


class TestReductionHelpers:
@pytest.fixture(scope="class")
def net(self, reaclib_library):
mylib = reaclib_library.linking_nuclei(["p", "he4", "c12", "n13", "o16"])
return pyna.RateCollection(libraries=[mylib])

@pytest.fixture(scope="class")
def thermo_state(self, net):
rho = 1e5
T = 1e8
comp = pyna.Composition(net.unique_nuclei)
comp.set_equal()
return (rho, T, comp)

@pytest.fixture(scope="class")
def net_info(self, net, thermo_state):
rho, T, comp = thermo_state
return reduction.get_net_info(net, comp, rho, T)

def test_enuc_dot(self, net_info, net, thermo_state):
enuc_dot = net.evaluate_energy_generation(*thermo_state)
assert reduction.enuc_dot(net_info) == approx(enuc_dot)

def test_ye_dot(self, net_info, net, thermo_state):
comp = thermo_state[2]
y_e = comp.eval_ye()

Y_dot = net.evaluate_ydots(*thermo_state)
nuc = net.unique_nuclei
Y = comp.get_molar()
ye_dot = y_e * (
sum(Y_dot[n] * n.Z for n in nuc) / sum(Y[n] * n.Z for n in nuc) -
sum(Y_dot[n] * n.A for n in nuc) / sum(comp.X[n] for n in nuc)
)

assert reduction.ye_dot(net_info) == approx(ye_dot, rel=1e-6, abs=0)

def test_abar_dot(self, net_info, net, thermo_state):
comp = thermo_state[2]
abar = comp.eval_abar()

ydots = net.evaluate_ydots(*thermo_state)
abar_dot = -abar**2 * sum(Y for Y in ydots.values())

assert reduction.abar_dot(net_info) == approx(abar_dot)

0 comments on commit e54ed8f

Please sign in to comment.