From 9253200eaf4591126675253f60510a3e09432f4e Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Mon, 4 Nov 2019 20:27:22 -0800 Subject: [PATCH] Move j_steps and j_distribution out of conftest - also move PRNG back into utils --- meanas/test/conftest.py | 24 +----------------------- meanas/test/test_fdfd.py | 17 ++++++++++++++++- meanas/test/test_fdtd.py | 19 ++++++++++++++++++- meanas/test/utils.py | 1 + 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/meanas/test/conftest.py b/meanas/test/conftest.py index c2adeb9..d25103b 100644 --- a/meanas/test/conftest.py +++ b/meanas/test/conftest.py @@ -2,9 +2,7 @@ from typing import List, Tuple import numpy import pytest - -PRNG = numpy.random.RandomState(12345) - +from .utils import PRNG ##################################### # Test fixtures @@ -59,18 +57,6 @@ def j_mag(request): 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]) def dx(request): 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)] yield dxes - -@pytest.fixture(scope='module', - params=[(0, 4, 8), - #(0,), - ] - ) -def j_steps(request): - yield request.param diff --git a/meanas/test/test_fdfd.py b/meanas/test/test_fdfd.py index a00c5e9..8325d63 100644 --- a/meanas/test/test_fdfd.py +++ b/meanas/test/test_fdfd.py @@ -5,7 +5,7 @@ import numpy from numpy.testing import assert_allclose, assert_array_equal 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): @@ -53,6 +53,20 @@ def pmc(request): 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() class SimResult: shape: Tuple[int] @@ -64,6 +78,7 @@ class SimResult: pmc: numpy.ndarray pec: numpy.ndarray + @pytest.fixture() def sim(request, shape, epsilon, dxes, j_distribution, omega, pec, pmc): # is3d = (numpy.array(shape) == 1).sum() == 0 diff --git a/meanas/test/test_fdtd.py b/meanas/test/test_fdtd.py index 2fdcbea..4861f25 100644 --- a/meanas/test/test_fdtd.py +++ b/meanas/test/test_fdtd.py @@ -5,7 +5,7 @@ import numpy from numpy.testing import assert_allclose, assert_array_equal 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): @@ -157,6 +157,23 @@ class SimResult: 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() def sim(request, shape, epsilon, dxes, dt, j_distribution, j_steps): is3d = (numpy.array(shape) == 1).sum() == 0 diff --git a/meanas/test/utils.py b/meanas/test/utils.py index 53c25e6..ac657a1 100644 --- a/meanas/test/utils.py +++ b/meanas/test/utils.py @@ -1,5 +1,6 @@ import numpy +PRNG = numpy.random.RandomState(12345) def assert_fields_close(x, y, *args, **kwargs): numpy.testing.assert_allclose(x, y, verbose=False,