Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wtbarnes committed Aug 23, 2024
1 parent e122b75 commit 6e522c5
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 120 deletions.
9 changes: 3 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,17 @@ classifiers = [
"Typing :: Typed",
]
dynamic = ["version"]
dependencies = []
dependencies = [
"astropy",
]

[project.optional-dependencies]
test = [
"astropy",
"pytest >=6",
"pytest-cov >=3",
"hissw",
"h5py",
]
dev = [
"pytest >=6",
"pytest-cov >=3",
]
docs = [
"sphinx>=7.0",
"sphinx_copybutton",
Expand Down
8 changes: 4 additions & 4 deletions src/ebtelplusplus/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ def plot_comparison(r_cpp, r_idl):
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8,9),layout='constrained')
ax = fig.add_subplot(311)
ax.plot(r_cpp['time'], r_cpp['electron_temperature'], label='ebtel++')
ax.plot(r_cpp.time, r_cpp.electron_temperature, label='ebtel++')
ax.plot(r_idl['time'], r_idl['temperature'], label='IDL')
ax.set_xlabel('$t$ [s]')
ax.set_ylabel('$T$ [K]')
ax.legend()
ax = fig.add_subplot(312,sharex=ax)
ax.plot(r_cpp['time'], r_cpp['density'])
ax.plot(r_cpp.time, r_cpp.density)
ax.plot(r_idl['time'], r_idl['density'])
ax.set_xlabel('$t$ [s]')
ax.set_ylabel('$n$ [cm$^{-3}$]')
ax = fig.add_subplot(313)
ax.plot(r_cpp['time'], (r_cpp['electron_temperature']-r_idl['temperature'])/r_idl['temperature'],
ax.plot(r_cpp.time, (r_cpp.electron_temperature-r_idl['temperature'])/r_idl['temperature'],
label='$T$')
ax.plot(r_cpp['time'], (r_cpp['density']-r_idl['density'])/r_idl['density'],
ax.plot(r_cpp.time, (r_cpp.density-r_idl['density'])/r_idl['density'],
label='$n$')
ax.axhline(y=0, color='k', ls='--')
ax.axhline(y=0.1, color='k', ls=':')
Expand Down
7 changes: 4 additions & 3 deletions src/ebtelplusplus/tests/test_compare_hydrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import astropy.units as u
from scipy.interpolate import interp1d

import ebtelplusplus
from .helpers import read_hydrad_test_data
from .util import run_ebtel


@pytest.fixture
Expand Down Expand Up @@ -70,12 +70,13 @@ def test_compare_hydrad_single_event_peak_values(base_config, tau, heating_type)
'decay_end': tau,
}}
]
r_ebtel = run_ebtel(config)
r_ebtel = ebtelplusplus.run(config)
r_hydrad = read_hydrad_test_data('hydrad_results.h5', tau, heating_type)
# Require all quantities at the peak to be <20% in accordance with the comparisons
# done in Barnes et al. (2016a)
for name in ['electron_temperature', 'ion_temperature', 'density']:
f_interp = interp1d(r_ebtel['time'].to_value('s'), r_ebtel[name].to_value(r_hydrad[name].unit),
f_interp = interp1d(r_ebtel.time.to_value('s'),
getattr(r_ebtel, name).to_value(r_hydrad[name].unit),
fill_value='extrapolate')
x_ebtel_interp = u.Quantity(f_interp(r_hydrad['time'].to_value('s')), r_hydrad[name].unit)
i_peak = r_hydrad[name].argmax()
Expand Down
27 changes: 14 additions & 13 deletions src/ebtelplusplus/tests/test_compare_idl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import astropy.units as u

import ebtelplusplus
from .helpers import read_idl_test_data, plot_comparison
from .util import run_ebtel

# Tolerated error between IDL and C++ results
RTOL = 0.01
Expand Down Expand Up @@ -57,16 +57,17 @@ def base_config():
@pytest.mark.xfail
def test_compare_idl_single_event(base_config, ebtel_idl_path, plot_idl_comparisons):
config = copy.deepcopy(base_config)
r_cpp = run_ebtel(config)
r_cpp = ebtelplusplus.run(config)
r_idl = read_idl_test_data('idl_single_event.txt', ebtel_idl_path, config)
if plot_idl_comparisons:
plot_comparison(r_cpp, r_idl)
assert u.allclose(r_cpp['electron_temperature'], r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp['ion_temperature'], r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp['density'], r_idl['density'], rtol=RTOL)
assert u.allclose(r_cpp['electron_pressure']+r_cpp['ion_pressure'], r_idl['pressure'],
assert u.allclose(r_cpp.electron_temperature, r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp.ion_temperature, r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp.density, r_idl['density'], rtol=RTOL)
assert u.allclose(r_cpp.electron_pressure+r_cpp.ion_pressure,
r_idl['pressure'],
rtol=RTOL)
assert u.allclose(r_cpp['velocity'], r_idl['velocity'], rtol=RTOL)
assert u.allclose(r_cpp.velocity, r_idl['velocity'], rtol=RTOL)


@pytest.mark.parametrize(('A_c', 'A_0', 'A_tr'), [
Expand All @@ -79,12 +80,12 @@ def test_compare_idl_area_expansion(A_c, A_0, A_tr, base_config, ebtel_idl_path,
config['loop_length_ratio_tr_total'] = 0.15
config['area_ratio_tr_corona'] = A_tr/A_c
config['area_ratio_0_corona'] = A_0/A_c
r_cpp = run_ebtel(config)
r_cpp = ebtelplusplus.run(config)
r_idl = read_idl_test_data(f'idl_area_expansion_{A_c=}_{A_0=}_{A_tr=}.txt', ebtel_idl_path, config)
if plot_idl_comparisons:
plot_comparison(r_cpp, r_idl)
assert u.allclose(r_cpp['electron_temperature'], r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp['ion_temperature'], r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp['density'], r_idl['density'], rtol=RTOL)
assert u.allclose(r_cpp['electron_pressure']+r_cpp['ion_pressure'], r_idl['pressure'], rtol=RTOL)
assert u.allclose(r_cpp['velocity'], r_idl['velocity'], rtol=RTOL)
assert u.allclose(r_cpp.electron_temperature, r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp.ion_temperature, r_idl['temperature'], rtol=RTOL)
assert u.allclose(r_cpp.density, r_idl['density'], rtol=RTOL)
assert u.allclose(r_cpp.electrong_pressure+r_cpp.ion_pressure, r_idl['pressure'], rtol=RTOL)
assert u.allclose(r_cpp.velocity, r_idl['velocity'], rtol=RTOL)
56 changes: 0 additions & 56 deletions src/ebtelplusplus/tests/test_new_return_types.py

This file was deleted.

6 changes: 3 additions & 3 deletions src/ebtelplusplus/tests/test_rad_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from .util import run_ebtel
import ebtelplusplus


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -47,7 +47,7 @@ def base_config():
def test_rad_loss_options(base_config, radiation):
# Just a smoke test to make sure the new radiative loss options work
base_config['radiation'] = radiation
results = run_ebtel(base_config)
results = ebtelplusplus.run(base_config)
quantities = [
'electron_temperature',
'ion_temperature',
Expand All @@ -57,4 +57,4 @@ def test_rad_loss_options(base_config, radiation):
'velocity'
]
for q in quantities:
assert q in results
assert getattr(results, q) is not None
8 changes: 4 additions & 4 deletions src/ebtelplusplus/tests/test_single_fluid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import astropy.units as u
import pytest

from .util import run_ebtel
import ebtelplusplus


@pytest.fixture
Expand Down Expand Up @@ -57,6 +57,6 @@ def test_single_fluid_gentle(base_config, rise_start, rise_end, decay_start, dec
'magnitude': magnitude,
}}
]
results = run_ebtel(base_config)
assert u.allclose(results['electron_temperature'], results['ion_temperature'], rtol=1e-10)
assert u.allclose(results['electron_pressure'], results['ion_pressure'], rtol=1e-10)
results = ebtelplusplus.run(base_config)
assert u.allclose(results.electron_temperature, results.ion_temperature, rtol=1e-10)
assert u.allclose(results.electron_pressure, results.ion_pressure, rtol=1e-10)
66 changes: 35 additions & 31 deletions src/ebtelplusplus/tests/test_solver.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
"""
Compare results of adaptive and static solvers
"""
import copy
from collections import OrderedDict

import copy
import pytest
import numpy as np
import pytest

from .util import run_ebtel
#from util import EbtelPlusPlusError
import ebtelplusplus


@pytest.fixture(scope='module')
Expand Down Expand Up @@ -55,14 +54,14 @@ def base_config():
def adaptive_results(base_config):
config = copy.deepcopy(base_config)
config['use_adaptive_solver'] = True
return run_ebtel(config)
return ebtelplusplus.run(config)


@pytest.fixture(scope='module')
def static_results(base_config):
config = copy.deepcopy(base_config)
config['use_adaptive_solver'] = False
return run_ebtel(config)
return ebtelplusplus.run(config)


@pytest.mark.parametrize(('name', 'atol'), [
Expand All @@ -74,34 +73,39 @@ def static_results(base_config):
('velocity', 1e4),
])
def test_quantities_equal_adaptive_static(adaptive_results, static_results, name, atol):
t_static = static_results['time'].to_value('s')
t_adaptive = adaptive_results['time'].to_value('s')
adapt_interp = np.interp(t_static, t_adaptive, adaptive_results[name].value)
t_static = static_results.time.to_value('s')
t_adaptive = adaptive_results.time.to_value('s')
adapt_interp = np.interp(t_static, t_adaptive, getattr(adaptive_results, name).value)
# NOTE: Skip the first 5 steps b/c there is always one anomalous point that gives
# an error > 10%; due to static case not rising fast enough
assert np.allclose(adapt_interp[5:], static_results[name][5:].to_value(adaptive_results[name].unit),
rtol=1e-2, atol=atol)
assert np.allclose(adapt_interp[5:],
getattr(static_results, name)[5:].to_value(getattr(adaptive_results,name).unit),
rtol=1e-2,
atol=atol)


#@pytest.mark.parametrize('value', [-1e-5, 0, 1e-15])
#def test_insufficient_heating(base_config, value):
# config = copy.deepcopy(base_config)
# config['use_adaptive_solver'] = False
# config['heating']['background'] = value
# with pytest.raises(EbtelPlusPlusError):
# run_ebtel(config)
@pytest.mark.parametrize('value', [-1e-5, 0, 1e-15])
def test_insufficient_heating(base_config, value):
config = copy.deepcopy(base_config)
config['use_adaptive_solver'] = False
config['heating']['background'] = value
with pytest.raises(RuntimeError):
ebtelplusplus.run(config)


#@pytest.mark.parametrize('use_adaptive_solver', [True, False])
#def test_NaNs_in_solver(base_config, use_adaptive_solver):
# config = copy.deepcopy(base_config)
# config['use_adaptive_solver'] = use_adaptive_solver
# config['heating']['events'] = [
# {'event': {'rise_start': 0.0, 'rise_end': 100.0, 'decay_start': 100.0,
# 'decay_end': 200.0, 'magnitude': -10.0}}
# ]
# with pytest.raises(EbtelPlusPlusError):
# run_ebtel(config)
@pytest.mark.parametrize('use_adaptive_solver', [True, False])
def test_NaNs_in_solver(base_config, use_adaptive_solver):
config = copy.deepcopy(base_config)
config['use_adaptive_solver'] = use_adaptive_solver
config['heating']['events'] = [
{'event': {'rise_start': 0.0,
'rise_end': 100.0,
'decay_start': 100.0,
'decay_end': 200.0,
'magnitude': -10.0}}
]
with pytest.raises(RuntimeError):
ebtelplusplus.run(config)


@pytest.mark.parametrize(('A_c', 'A_0', 'A_tr'), [
Expand All @@ -115,7 +119,7 @@ def test_area_expansion(A_c, A_0, A_tr, base_config):
config['loop_length_ratio_tr_total'] = 0.15
config['area_ratio_tr_corona'] = A_tr/A_c
config['area_ratio_0_corona'] = A_0/A_c
results = run_ebtel(config)
results = ebtelplusplus.run(config)
vars = [
'electron_temperature',
'ion_temperature',
Expand All @@ -126,5 +130,5 @@ def test_area_expansion(A_c, A_0, A_tr, base_config):
'time',
]
for v in vars:
assert v in results
assert not np.any(np.isnan(results[v]))
assert getattr(results, v) is not None
assert not np.any(np.isnan(getattr(results, v)))

0 comments on commit 6e522c5

Please sign in to comment.