Skip to content

Commit f09f1cb

Browse files
committed
Add tests and fix warnings
1 parent 669e374 commit f09f1cb

6 files changed

Lines changed: 76 additions & 11 deletions

File tree

EMpy/modesolvers/FD.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,14 @@ def build_matrix(self):
248248
return A
249249

250250
def solve(self, neigs, tol):
251-
from scipy.sparse.linalg import eigen
251+
from scipy.sparse import linalg
252252

253253
self.nmodes = neigs
254254
self.tol = tol
255255

256256
A = self.build_matrix()
257257

258-
eigvals, eigvecs = eigen.eigs( # type: ignore
258+
eigvals, eigvecs = linag.eigs( # type: ignore
259259
A, k=neigs, which="LR", tol=tol, ncv=10 * neigs, return_eigenvectors=True
260260
)
261261

@@ -2038,7 +2038,7 @@ def solve(self, neigs=4, tol=0, guess=None):
20382038
Ez = solver.modes[0].Ez
20392039
"""
20402040

2041-
from scipy.sparse.linalg import eigen
2041+
from scipy.sparse import linalg
20422042

20432043
self.nmodes = neigs
20442044
self.tol = tol
@@ -2053,7 +2053,7 @@ def solve(self, neigs=4, tol=0, guess=None):
20532053
shift = None
20542054

20552055
# Here is where the actual mode-solving takes place!
2056-
[eigvals, eigvecs] = eigen.eigs(
2056+
[eigvals, eigvecs] = linalg.eigs(
20572057
A,
20582058
k=neigs,
20592059
which="LR",

EMpy/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = "2.2.2.dev50"
1+
version = "2.2.2.dev54"

tests/devices_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from unittest import TestCase
2+
3+
import numpy
4+
5+
import EMpy.devices
6+
7+
8+
class NRRTest(TestCase):
9+
10+
def test_solve(self):
11+
N = 1000
12+
wls = numpy.linspace(1.53e-6, 1.57e-6, N)
13+
14+
Ks = [
15+
EMpy.devices.Coupler(wls, numpy.sqrt(0.08), 1.0),
16+
EMpy.devices.Coupler(wls, numpy.sqrt(0.008), 1.0),
17+
EMpy.devices.Coupler(wls, numpy.sqrt(0.006), 1.0),
18+
EMpy.devices.Coupler(wls, numpy.sqrt(0.09), 1.0),
19+
]
20+
21+
R = 5e-6
22+
l1s = [numpy.pi * R, numpy.pi * R, numpy.pi * R]
23+
l2s = [numpy.pi * R, numpy.pi * R, numpy.pi * R]
24+
25+
SWG = EMpy.devices.SWG(400, 220, 125).solve(wls)
26+
neffs = [SWG.neff, SWG.neff, SWG.neff]
27+
28+
NRR = EMpy.devices.NRR(Ks, neffs, l1s, l2s).solve()
29+
30+
self.assertEqual(NRR.THRU.shape, (N,))
31+
self.assertAlmostEqual(numpy.absolute(NRR.THRU).min(), 0.3868920546379094)

tests/materials_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# pylint: disable=no-self-use
21
from unittest import TestCase
32

43
from numpy import array
@@ -8,6 +7,7 @@
87

98

109
class RefractiveIndexTest(TestCase):
10+
1111
def test_all_nones(self):
1212
with assert_raises(ValueError):
1313
mat.RefractiveIndex()

tests/modesolvers_test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from unittest import TestCase
2+
3+
import numpy
4+
5+
import EMpy.modesolvers
6+
7+
8+
class VFDModeSolverTest(TestCase):
9+
10+
def test_solve(self):
11+
12+
def epsfunc(x_, y_):
13+
"""Return a matrix describing a 2d material.
14+
15+
:param x_: x values
16+
:param y_: y values
17+
:return: 2d-matrix
18+
"""
19+
xx, yy = numpy.meshgrid(x_, y_)
20+
return numpy.where(
21+
(numpy.abs(xx.T - 1.24e-6) <= 0.24e-6) * (numpy.abs(yy.T - 1.11e-6) <= 0.11e-6),
22+
3.4757**2,
23+
1.446**2,
24+
)
25+
26+
neigs = 2
27+
tol = 1e-8
28+
boundary = "0000"
29+
wl = 1.55e-6
30+
x = numpy.linspace(0, 2.48e-6, 125)
31+
y = numpy.linspace(0, 2.22e-6, 112)
32+
solver = EMpy.modesolvers.FD.VFDModeSolver(wl, x, y, epsfunc, boundary).solve(
33+
neigs, tol
34+
)
35+
self.assertEqual(len(solver.modes), 2)
36+
self.assertAlmostEqual(solver.modes[0].neff, 2.4179643622942937)

tests/utils_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
# pylint: disable=no-self-use
21
from unittest import TestCase
32

43
import random
54
import math
65

76
from numpy import pi
8-
from numpy.testing import assert_almost_equal
97

108
import EMpy.utils as U
119

1210

1311
class UtilsTest(TestCase):
12+
1413
def test_deg2rad(self):
15-
# TODO use hypothesis
1614
x = -2 * pi + 4 * pi * random.random()
17-
assert_almost_equal(x, U.deg2rad(U.rad2deg(x)))
15+
self.assertAlmostEqual(x, U.deg2rad(U.rad2deg(x)))
1816

1917
def test_norm(self):
2018
self.assertEqual(U.norm([1, 0, 0]), 1)
2119
self.assertEqual(U.norm([0, 1, 0]), 1)
2220
self.assertEqual(U.norm([0, 0, 1]), 1)
23-
assert_almost_equal(U.norm([1, 0, 1]), math.sqrt(2))
21+
self.assertAlmostEqual(U.norm([1, 0, 1]), math.sqrt(2))
2422

2523
def test_trapz2(self):
2624
f = [[i + j for i in range(5)] for j in range(5)]

0 commit comments

Comments
 (0)