Move j_steps and j_distribution out of conftest

- also move PRNG back into utils
This commit is contained in:
Jan Petykiewicz 2019-11-04 20:27:22 -08:00
parent 60961db2d3
commit 9253200eaf
4 changed files with 36 additions and 25 deletions

View File

@ -2,9 +2,7 @@ from typing import List, Tuple
import numpy import numpy
import pytest import pytest
from .utils import PRNG
PRNG = numpy.random.RandomState(12345)
##################################### #####################################
# Test fixtures # Test fixtures
@ -59,18 +57,6 @@ def j_mag(request):
yield request.param yield request.param
@pytest.fixture(scope='module', params=['center', 'random'])
def j_distribution(request, shape, j_mag):
j = numpy.zeros(shape)
if request.param == 'center':
j[:, shape[1]//2, shape[2]//2, shape[3]//2] = j_mag
elif request.param == '000':
j[:, 0, 0, 0] = j_mag
elif request.param == 'random':
j[:] = PRNG.uniform(low=-j_mag, high=j_mag, size=shape)
yield j
@pytest.fixture(scope='module', params=[1.0, 1.5]) @pytest.fixture(scope='module', params=[1.0, 1.5])
def dx(request): def dx(request):
yield request.param yield request.param
@ -82,11 +68,3 @@ def dxes(request, shape, dx):
dxes = [[numpy.full(s, dx) for s in shape[1:]] for _ in range(2)] dxes = [[numpy.full(s, dx) for s in shape[1:]] for _ in range(2)]
yield dxes yield dxes
@pytest.fixture(scope='module',
params=[(0, 4, 8),
#(0,),
]
)
def j_steps(request):
yield request.param

View File

@ -5,7 +5,7 @@ import numpy
from numpy.testing import assert_allclose, assert_array_equal from numpy.testing import assert_allclose, assert_array_equal
from .. import fdfd, vec, unvec from .. import fdfd, vec, unvec
from .utils import assert_close, assert_fields_close from .utils import assert_close, assert_fields_close, PRNG
def test_poynting_planes(sim): def test_poynting_planes(sim):
@ -53,6 +53,20 @@ def pmc(request):
yield request.param yield request.param
@pytest.fixture(params=['center', 'diag'])
def j_distribution(request, shape, j_mag):
j = numpy.zeros(shape, dtype=complex)
center_mask = numpy.zeros(shape, dtype=bool)
center_mask[:, shape[1]//2, shape[2]//2, shape[3]//2] = True
if request.param == 'center':
j[center_mask] = j_mag
elif request.param == 'diag':
j[numpy.roll(center_mask, [1, 1, 1], axis=(1, 2, 3))] = j_mag
j[numpy.roll(center_mask, [-1, -1, -1], axis=(1, 2, 3))] = -1j * j_mag
yield j
@dataclasses.dataclass() @dataclasses.dataclass()
class SimResult: class SimResult:
shape: Tuple[int] shape: Tuple[int]
@ -64,6 +78,7 @@ class SimResult:
pmc: numpy.ndarray pmc: numpy.ndarray
pec: numpy.ndarray pec: numpy.ndarray
@pytest.fixture() @pytest.fixture()
def sim(request, shape, epsilon, dxes, j_distribution, omega, pec, pmc): def sim(request, shape, epsilon, dxes, j_distribution, omega, pec, pmc):
# is3d = (numpy.array(shape) == 1).sum() == 0 # is3d = (numpy.array(shape) == 1).sum() == 0

View File

@ -5,7 +5,7 @@ import numpy
from numpy.testing import assert_allclose, assert_array_equal from numpy.testing import assert_allclose, assert_array_equal
from .. import fdtd from .. import fdtd
from .utils import assert_close, assert_fields_close from .utils import assert_close, assert_fields_close, PRNG
def test_initial_fields(sim): def test_initial_fields(sim):
@ -157,6 +157,23 @@ class SimResult:
js: List[numpy.ndarray] = dataclasses.field(default_factory=list) js: List[numpy.ndarray] = dataclasses.field(default_factory=list)
@pytest.fixture(params=[(0, 4, 8),]) #(0,)])
def j_steps(request):
yield request.param
@pytest.fixture(params=['center', 'random'])
def j_distribution(request, shape, j_mag):
j = numpy.zeros(shape)
if request.param == 'center':
j[:, shape[1]//2, shape[2]//2, shape[3]//2] = j_mag
elif request.param == '000':
j[:, 0, 0, 0] = j_mag
elif request.param == 'random':
j[:] = PRNG.uniform(low=-j_mag, high=j_mag, size=shape)
yield j
@pytest.fixture() @pytest.fixture()
def sim(request, shape, epsilon, dxes, dt, j_distribution, j_steps): def sim(request, shape, epsilon, dxes, dt, j_distribution, j_steps):
is3d = (numpy.array(shape) == 1).sum() == 0 is3d = (numpy.array(shape) == 1).sum() == 0

View File

@ -1,5 +1,6 @@
import numpy import numpy
PRNG = numpy.random.RandomState(12345)
def assert_fields_close(x, y, *args, **kwargs): def assert_fields_close(x, y, *args, **kwargs):
numpy.testing.assert_allclose(x, y, verbose=False, numpy.testing.assert_allclose(x, y, verbose=False,