示例#1
0
def test_norm():
    assert U.norm([1, 0, 0]) == 1
    assert U.norm([0, 1, 0]) == 1
    assert U.norm([0, 0, 1]) == 1
    assert_almost_equal(U.norm([1, 0, 1]), math.sqrt(2))
示例#2
0
        def find_roots(wl, epsilon, alpha, beta):
            """Find roots of characteristic equation.

            Given a wavelength, a 3x3 tensor epsilon and the tangential components
            of the wavevector k = (alpha,beta,gamma_i), returns the 4 possible
            gamma_i, i = 1,2,3,4 that satisfy the boundary conditions.
            """

            omega = 2.0 * S.pi * c / wl
            K = omega ** 2 * mu0 * epsilon

            k0 = 2.0 * S.pi / wl
            K /= k0 ** 2
            alpha /= k0
            beta /= k0

            alpha2 = alpha ** 2
            alpha3 = alpha ** 3
            alpha4 = alpha ** 4
            beta2 = beta ** 2
            beta3 = beta ** 3
            beta4 = beta ** 4

            coeff = [
                K[2, 2],
                alpha * (K[0, 2] + K[2, 0]) + beta * (K[1, 2] + K[2, 1]),
                alpha2 * (K[0, 0] + K[2, 2])
                + alpha * beta * (K[1, 0] + K[0, 1])
                + beta2 * (K[1, 1] + K[2, 2])
                + (K[0, 2] * K[2, 0] + K[1, 2] * K[2, 1] - K[0, 0] * K[2, 2] - K[1, 1] * K[2, 2]),
                alpha3 * (K[0, 2] + K[2, 0])
                + beta3 * (K[1, 2] + K[2, 1])
                + alpha2 * beta * (K[1, 2] + K[2, 1])
                + alpha * beta2 * (K[0, 2] + K[2, 0])
                + alpha * (K[0, 1] * K[1, 2] + K[1, 0] * K[2, 1] - K[0, 2] * K[1, 1] - K[2, 0] * K[1, 1])
                + beta * (K[0, 1] * K[2, 0] + K[1, 0] * K[0, 2] - K[0, 0] * K[1, 2] - K[0, 0] * K[2, 1]),
                alpha4 * (K[0, 0])
                + beta4 * (K[1, 1])
                + alpha3 * beta * (K[0, 1] + K[1, 0])
                + alpha * beta3 * (K[0, 1] + K[1, 0])
                + alpha2 * beta2 * (K[0, 0] + K[1, 1])
                + alpha2 * (K[0, 1] * K[1, 0] + K[0, 2] * K[2, 0] - K[0, 0] * K[2, 2] - K[0, 0] * K[1, 1])
                + beta2 * (K[0, 1] * K[1, 0] + K[1, 2] * K[2, 1] - K[0, 0] * K[1, 1] - K[1, 1] * K[2, 2])
                + alpha * beta * (K[0, 2] * K[2, 1] + K[2, 0] * K[1, 2] - K[0, 1] * K[2, 2] - K[1, 0] * K[2, 2])
                + K[0, 0] * K[1, 1] * K[2, 2]
                - K[0, 0] * K[1, 2] * K[2, 1]
                - K[1, 0] * K[0, 1] * K[2, 2]
                + K[1, 0] * K[0, 2] * K[2, 1]
                + K[2, 0] * K[0, 1] * K[1, 2]
                - K[2, 0] * K[0, 2] * K[1, 1],
            ]

            gamma = S.roots(coeff)
            tmp = S.sort_complex(gamma)
            gamma = tmp[[3, 0, 2, 1]]  # convention

            k = k0 * S.array([alpha * S.ones(gamma.shape), beta * S.ones(gamma.shape), gamma]).T
            v = S.zeros((4, 3), dtype=complex)

            for i, g in enumerate(gamma):

                H = K + [
                    [-beta2 - g ** 2, alpha * beta, alpha * g],
                    [alpha * beta, -alpha2 - g ** 2, beta * g],
                    [alpha * g, beta * g, -alpha2 - beta2],
                ]
                v[i, :] = [
                    (K[1, 1] - alpha2 - g ** 2) * (K[2, 2] - alpha2 - beta2) - (K[1, 2] + beta * g) ** 2,
                    (K[1, 2] + beta * g) * (K[2, 0] + alpha * g)
                    - (K[0, 1] + alpha * beta) * (K[2, 2] - alpha2 - beta2),
                    (K[0, 1] + alpha * beta) * (K[1, 2] + beta * g)
                    - (K[0, 2] + alpha * g) * (K[1, 1] - alpha2 - g ** 2),
                ]

            p3 = v[0, :]
            p3 /= norm(p3)
            p4 = v[1, :]
            p4 /= norm(p4)
            p1 = S.cross(p3, k[0, :])
            p1 /= norm(p1)
            p2 = S.cross(p4, k[1, :])
            p2 /= norm(p2)

            p = S.array([p1, p2, p3, p4])
            q = wl / (2.0 * S.pi * mu0 * c) * S.cross(k, p)

            return k, p, q
示例#3
0
        def find_roots(wl, epsilon, alpha, beta):
            """Find roots of characteristic equation.

            Given a wavelength, a 3x3 tensor epsilon and the tangential components
            of the wavevector k = (alpha,beta,gamma_i), returns the 4 possible
            gamma_i, i = 1,2,3,4 that satisfy the boundary conditions.
            """

            omega = 2. * S.pi * c / wl
            K = omega**2 * mu0 * epsilon

            k0 = 2. * S.pi / wl
            K /= k0**2
            alpha /= k0
            beta /= k0

            alpha2 = alpha**2
            alpha3 = alpha**3
            alpha4 = alpha**4
            beta2 = beta**2
            beta3 = beta**3
            beta4 = beta**4

            coeff = [
                K[2,
                  2], alpha * (K[0, 2] + K[2, 0]) + beta * (K[1, 2] + K[2, 1]),
                alpha2 * (K[0, 0] + K[2, 2]) + alpha * beta *
                (K[1, 0] + K[0, 1]) + beta2 * (K[1, 1] + K[2, 2]) +
                (K[0, 2] * K[2, 0] + K[1, 2] * K[2, 1] - K[0, 0] * K[2, 2] -
                 K[1, 1] * K[2, 2]), alpha3 * (K[0, 2] + K[2, 0]) + beta3 *
                (K[1, 2] + K[2, 1]) + alpha2 * beta * (K[1, 2] + K[2, 1]) +
                alpha * beta2 * (K[0, 2] + K[2, 0]) + alpha *
                (K[0, 1] * K[1, 2] + K[1, 0] * K[2, 1] - K[0, 2] * K[1, 1] -
                 K[2, 0] * K[1, 1]) + beta *
                (K[0, 1] * K[2, 0] + K[1, 0] * K[0, 2] - K[0, 0] * K[1, 2] -
                 K[0, 0] * K[2, 1]), alpha4 * (K[0, 0]) + beta4 * (K[1, 1]) +
                alpha3 * beta * (K[0, 1] + K[1, 0]) + alpha * beta3 *
                (K[0, 1] + K[1, 0]) + alpha2 * beta2 * (K[0, 0] + K[1, 1]) +
                alpha2 * (K[0, 1] * K[1, 0] + K[0, 2] * K[2, 0] -
                          K[0, 0] * K[2, 2] - K[0, 0] * K[1, 1]) + beta2 *
                (K[0, 1] * K[1, 0] + K[1, 2] * K[2, 1] - K[0, 0] * K[1, 1] -
                 K[1, 1] * K[2, 2]) + alpha * beta *
                (K[0, 2] * K[2, 1] + K[2, 0] * K[1, 2] - K[0, 1] * K[2, 2] -
                 K[1, 0] * K[2, 2]) + K[0, 0] * K[1, 1] * K[2, 2] -
                K[0, 0] * K[1, 2] * K[2, 1] - K[1, 0] * K[0, 1] * K[2, 2] +
                K[1, 0] * K[0, 2] * K[2, 1] + K[2, 0] * K[0, 1] * K[1, 2] -
                K[2, 0] * K[0, 2] * K[1, 1]
            ]

            gamma = S.roots(coeff)
            tmp = S.sort_complex(gamma)
            gamma = tmp[[3, 0, 2, 1]]  # convention

            k = k0 * \
                S.array([alpha *
                         S.ones(gamma.shape), beta *
                         S.ones(gamma.shape), gamma]).T
            v = S.zeros((4, 3), dtype=complex)

            for i, g in enumerate(gamma):

                H = K + [[-beta2 - g**2, alpha * beta, alpha * g],
                         [alpha * beta, -alpha2 - g**2, beta * g],
                         [alpha * g, beta * g, -alpha2 - beta2]]
                v[i, :] = [
                    (K[1, 1] - alpha2 - g**2) *
                    (K[2, 2] - alpha2 - beta2) - (K[1, 2] + beta * g)**2,
                    (K[1, 2] + beta * g) * (K[2, 0] + alpha * g) -
                    (K[0, 1] + alpha * beta) * (K[2, 2] - alpha2 - beta2),
                    (K[0, 1] + alpha * beta) * (K[1, 2] + beta * g) -
                    (K[0, 2] + alpha * g) * (K[1, 1] - alpha2 - g**2)
                ]

            p3 = v[0, :]
            p3 /= norm(p3)
            p4 = v[1, :]
            p4 /= norm(p4)
            p1 = S.cross(p3, k[0, :])
            p1 /= norm(p1)
            p2 = S.cross(p4, k[1, :])
            p2 /= norm(p2)

            p = S.array([p1, p2, p3, p4])
            q = wl / (2. * S.pi * mu0 * c) * S.cross(k, p)

            return k, p, q
示例#4
0
 def test_norm(self):
     self.assertEqual(U.norm([1, 0, 0]), 1)
     self.assertEqual(U.norm([0, 1, 0]), 1)
     self.assertEqual(U.norm([0, 0, 1]), 1)
     assert_almost_equal(U.norm([1, 0, 1]), math.sqrt(2))