style and type fixes (per mypy and flake8)
This commit is contained in:
parent
0e04f5ca77
commit
d13a3796a9
25 changed files with 242 additions and 217 deletions
|
|
@ -1,18 +1,18 @@
|
|||
from typing import List, Tuple
|
||||
import numpy
|
||||
import pytest
|
||||
"""
|
||||
|
||||
Test fixtures
|
||||
|
||||
"""
|
||||
import numpy # type: ignore
|
||||
import pytest # type: ignore
|
||||
|
||||
from .utils import PRNG
|
||||
|
||||
#####################################
|
||||
# Test fixtures
|
||||
#####################################
|
||||
|
||||
@pytest.fixture(scope='module',
|
||||
params=[(5, 5, 1),
|
||||
(5, 1, 5),
|
||||
(5, 5, 5),
|
||||
#(7, 7, 7),
|
||||
# (7, 7, 7),
|
||||
])
|
||||
def shape(request):
|
||||
yield (3, *request.param)
|
||||
|
|
@ -41,7 +41,7 @@ def epsilon(request, shape, epsilon_bg, epsilon_fg):
|
|||
|
||||
epsilon = numpy.full(shape, epsilon_bg, dtype=float)
|
||||
if request.param == 'center':
|
||||
epsilon[:, shape[1]//2, shape[2]//2, shape[3]//2] = epsilon_fg
|
||||
epsilon[:, shape[1] // 2, shape[2] // 2, shape[3] // 2] = epsilon_fg
|
||||
elif request.param == '000':
|
||||
epsilon[:, 0, 0, 0] = epsilon_fg
|
||||
elif request.param == 'random':
|
||||
|
|
@ -52,7 +52,7 @@ def epsilon(request, shape, epsilon_bg, epsilon_fg):
|
|||
yield epsilon
|
||||
|
||||
|
||||
@pytest.fixture(scope='module', params=[1.0])#, 1.5])
|
||||
@pytest.fixture(scope='module', params=[1.0]) # 1.5
|
||||
def j_mag(request):
|
||||
yield request.param
|
||||
|
||||
|
|
@ -70,7 +70,7 @@ def dxes(request, shape, dx):
|
|||
dxes = [[numpy.full(s, dx) for s in shape[1:]] for _ in range(2)]
|
||||
for eh in (0, 1):
|
||||
for ax in (0, 1, 2):
|
||||
dxes[eh][ax][dxes[eh][ax].size // 2] *= 1.1
|
||||
dxes[eh][ax][dxes[eh][ax].size // 2] *= 1.1
|
||||
elif request.param == 'random':
|
||||
dxe = [PRNG.uniform(low=1.0 * dx, high=1.1 * dx, size=s) for s in shape[1:]]
|
||||
dxh = [(d + numpy.roll(d, -1)) / 2 for d in dxe]
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
# pylint: disable=redefined-outer-name
|
||||
from typing import List, Tuple
|
||||
import dataclasses
|
||||
import pytest
|
||||
import numpy
|
||||
import pytest # type: ignore
|
||||
import numpy # type: ignore
|
||||
#from numpy.testing import assert_allclose, assert_array_equal
|
||||
|
||||
from .. import fdfd
|
||||
from ..fdmath import vec, unvec
|
||||
from .utils import assert_close, assert_fields_close
|
||||
from .utils import assert_close # , assert_fields_close
|
||||
|
||||
|
||||
def test_residual(sim):
|
||||
|
|
@ -53,7 +52,7 @@ def test_poynting_planes(sim):
|
|||
#####################################
|
||||
# Also see conftest.py
|
||||
|
||||
@pytest.fixture(params=[1/1500])
|
||||
@pytest.fixture(params=[1 / 1500])
|
||||
def omega(request):
|
||||
yield request.param
|
||||
|
||||
|
|
@ -74,11 +73,11 @@ def pmc(request):
|
|||
# yield (3, *request.param)
|
||||
|
||||
|
||||
@pytest.fixture(params=['diag']) #'center'
|
||||
@pytest.fixture(params=['diag']) # 'center'
|
||||
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
|
||||
center_mask[:, shape[1] // 2, shape[2] // 2, shape[3] // 2] = True
|
||||
|
||||
if request.param == 'center':
|
||||
j[center_mask] = j_mag
|
||||
|
|
@ -102,6 +101,9 @@ class FDResult:
|
|||
|
||||
@pytest.fixture()
|
||||
def sim(request, shape, epsilon, dxes, j_distribution, omega, pec, pmc):
|
||||
"""
|
||||
Build simulation from parts
|
||||
"""
|
||||
# is3d = (numpy.array(shape) == 1).sum() == 0
|
||||
# if is3d:
|
||||
# pytest.skip('Skipping dt != 0.3 because test is 3D (for speed)')
|
||||
|
|
|
|||
|
|
@ -1,20 +1,15 @@
|
|||
#####################################
|
||||
# pylint: disable=redefined-outer-name
|
||||
from typing import List, Tuple
|
||||
import dataclasses
|
||||
import pytest
|
||||
import numpy
|
||||
from numpy.testing import assert_allclose, assert_array_equal
|
||||
import pytest # type: ignore
|
||||
import numpy # type: ignore
|
||||
from numpy.testing import assert_allclose # type: ignore
|
||||
|
||||
from .. import fdfd
|
||||
from ..fdmath import vec, unvec
|
||||
from .utils import assert_close, assert_fields_close
|
||||
#from .utils import assert_close, assert_fields_close
|
||||
from .test_fdfd import FDResult
|
||||
|
||||
|
||||
def test_pml(sim, src_polarity):
|
||||
dim = numpy.where(numpy.array(sim.shape[1:]) > 1)[0][0] # Propagation axis
|
||||
|
||||
e_sqr = numpy.squeeze((sim.e.conj() * sim.e).sum(axis=0))
|
||||
|
||||
# from matplotlib import pyplot
|
||||
|
|
@ -43,10 +38,10 @@ def test_pml(sim, src_polarity):
|
|||
|
||||
|
||||
# Test fixtures
|
||||
#####################################
|
||||
# ####################################
|
||||
# Also see conftest.py
|
||||
|
||||
@pytest.fixture(params=[1/1500])
|
||||
@pytest.fixture(params=[1 / 1500])
|
||||
def omega(request):
|
||||
yield request.param
|
||||
|
||||
|
|
@ -61,7 +56,6 @@ def pmc(request):
|
|||
yield request.param
|
||||
|
||||
|
||||
|
||||
@pytest.fixture(params=[(30, 1, 1),
|
||||
(1, 30, 1),
|
||||
(1, 1, 30)])
|
||||
|
|
@ -82,16 +76,15 @@ def j_distribution(request, shape, epsilon, dxes, omega, src_polarity):
|
|||
other_dims = [0, 1, 2]
|
||||
other_dims.remove(dim)
|
||||
|
||||
dx_prop = (dxes[0][dim][shape[dim + 1] // 2] +
|
||||
dxes[1][dim][shape[dim + 1] // 2]) / 2 #TODO is this right for nonuniform dxes?
|
||||
dx_prop = (dxes[0][dim][shape[dim + 1] // 2]
|
||||
+ dxes[1][dim][shape[dim + 1] // 2]) / 2 # TODO is this right for nonuniform dxes?
|
||||
|
||||
# Mask only contains components orthogonal to propagation direction
|
||||
center_mask = numpy.zeros(shape, dtype=bool)
|
||||
center_mask[other_dims, shape[1]//2, shape[2]//2, shape[3]//2] = True
|
||||
center_mask[other_dims, shape[1] // 2, shape[2] // 2, shape[3] // 2] = True
|
||||
if (epsilon[center_mask] != epsilon[center_mask][0]).any():
|
||||
center_mask[other_dims[1]] = False # If epsilon is not isotropic, pick only one dimension
|
||||
|
||||
|
||||
wavenumber = omega * numpy.sqrt(epsilon[center_mask].mean())
|
||||
wavenumber_corrected = 2 / dx_prop * numpy.arcsin(wavenumber * dx_prop / 2)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
# pylint: disable=redefined-outer-name, no-member
|
||||
from typing import List, Tuple
|
||||
import dataclasses
|
||||
import pytest
|
||||
import numpy
|
||||
from numpy.testing import assert_allclose, assert_array_equal
|
||||
import pytest # type: ignore
|
||||
import numpy # type: ignore
|
||||
#from numpy.testing import assert_allclose, assert_array_equal # type: ignore
|
||||
|
||||
from .. import fdtd
|
||||
from .utils import assert_close, assert_fields_close, PRNG
|
||||
|
|
@ -29,7 +28,7 @@ def test_initial_energy(sim):
|
|||
e0 = sim.es[0]
|
||||
h0 = sim.hs[0]
|
||||
h1 = sim.hs[1]
|
||||
mask = (j0 != 0)
|
||||
|
||||
dV = numpy.prod(numpy.meshgrid(*sim.dxes[0], indexing='ij'), axis=0)
|
||||
u0 = (j0 * j0.conj() / sim.epsilon * dV).sum(axis=0)
|
||||
args = {'dxes': sim.dxes,
|
||||
|
|
@ -53,10 +52,10 @@ def test_energy_conservation(sim):
|
|||
'epsilon': sim.epsilon}
|
||||
|
||||
for ii in range(1, 8):
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii-1], h1=sim.hs[ii], e2=sim.es[ii], **args) # pylint: disable=bad-whitespace
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args) # pylint: disable=bad-whitespace
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii-1], dxes=sim.dxes)
|
||||
delta_j_B = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii], dxes=sim.dxes) # pylint: disable=bad-whitespace
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii - 1], h1=sim.hs[ii], e2=sim.es[ii], **args)
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args)
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii - 1], dxes=sim.dxes)
|
||||
delta_j_B = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii], dxes=sim.dxes)
|
||||
|
||||
u += delta_j_A.sum()
|
||||
assert_close(u_hstep.sum(), u)
|
||||
|
|
@ -70,8 +69,8 @@ def test_poynting_divergence(sim):
|
|||
|
||||
u_eprev = None
|
||||
for ii in range(1, 8):
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii-1], h1=sim.hs[ii], e2=sim.es[ii], **args) # pylint: disable=bad-whitespace
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args) # pylint: disable=bad-whitespace
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii - 1], h1=sim.hs[ii], e2=sim.es[ii], **args)
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args)
|
||||
delta_j_B = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii], dxes=sim.dxes)
|
||||
|
||||
du_half_h2e = u_estep - u_hstep - delta_j_B
|
||||
|
|
@ -83,10 +82,10 @@ def test_poynting_divergence(sim):
|
|||
continue
|
||||
|
||||
# previous half-step
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii-1], dxes=sim.dxes)
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii - 1], dxes=sim.dxes)
|
||||
|
||||
du_half_e2h = u_hstep - u_eprev - delta_j_A
|
||||
div_s_e2h = sim.dt * fdtd.poynting_divergence(e=sim.es[ii-1], h=sim.hs[ii], dxes=sim.dxes)
|
||||
div_s_e2h = sim.dt * fdtd.poynting_divergence(e=sim.es[ii - 1], h=sim.hs[ii], dxes=sim.dxes)
|
||||
assert_fields_close(du_half_e2h, -div_s_e2h)
|
||||
u_eprev = u_estep
|
||||
|
||||
|
|
@ -105,8 +104,8 @@ def test_poynting_planes(sim):
|
|||
|
||||
u_eprev = None
|
||||
for ii in range(1, 8):
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii-1], h1=sim.hs[ii], e2=sim.es[ii], **args) # pylint: disable=bad-whitespace
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args) # pylint: disable=bad-whitespace
|
||||
u_hstep = fdtd.energy_hstep(e0=sim.es[ii - 1], h1=sim.hs[ii], e2=sim.es[ii], **args)
|
||||
u_estep = fdtd.energy_estep(h0=sim.hs[ii], e1=sim.es[ii], h2=sim.hs[ii + 1], **args)
|
||||
delta_j_B = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii], dxes=sim.dxes)
|
||||
du_half_h2e = u_estep - u_hstep - delta_j_B
|
||||
|
||||
|
|
@ -121,7 +120,7 @@ def test_poynting_planes(sim):
|
|||
u_eprev = u_estep
|
||||
continue
|
||||
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii-1], dxes=sim.dxes)
|
||||
delta_j_A = fdtd.delta_energy_j(j0=sim.js[ii], e1=sim.es[ii - 1], dxes=sim.dxes)
|
||||
du_half_e2h = u_hstep - u_eprev - delta_j_A
|
||||
|
||||
s_e2h = -fdtd.poynting(e=sim.es[ii - 1], h=sim.hs[ii], dxes=sim.dxes) * sim.dt
|
||||
|
|
@ -158,7 +157,7 @@ class TDResult:
|
|||
js: List[numpy.ndarray] = dataclasses.field(default_factory=list)
|
||||
|
||||
|
||||
@pytest.fixture(params=[(0, 4, 8),]) #(0,)])
|
||||
@pytest.fixture(params=[(0, 4, 8)]) # (0,)
|
||||
def j_steps(request):
|
||||
yield request.param
|
||||
|
||||
|
|
@ -167,7 +166,7 @@ def j_steps(request):
|
|||
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
|
||||
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':
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import numpy
|
||||
import numpy # type: ignore
|
||||
|
||||
PRNG = numpy.random.RandomState(12345)
|
||||
|
||||
def assert_fields_close(x, y, *args, **kwargs):
|
||||
numpy.testing.assert_allclose(x, y, verbose=False,
|
||||
err_msg='Fields did not match:\n{}\n{}'.format(numpy.rollaxis(x, -1),
|
||||
numpy.rollaxis(y, -1)), *args, **kwargs)
|
||||
numpy.testing.assert_allclose(
|
||||
x, y, verbose=False,
|
||||
err_msg='Fields did not match:\n{}\n{}'.format(numpy.rollaxis(x, -1),
|
||||
numpy.rollaxis(y, -1)), *args, **kwargs)
|
||||
|
||||
def assert_close(x, y, *args, **kwargs):
|
||||
numpy.testing.assert_allclose(x, y, *args, **kwargs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue