|
| 1 | +# Test for vectorized optics module |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +# Setting the path for XLuminA modules: |
| 6 | +current_path = os.path.abspath(os.path.join('..')) |
| 7 | +module_path = os.path.join(current_path) |
| 8 | + |
| 9 | +if module_path not in sys.path: |
| 10 | + sys.path.append(module_path) |
| 11 | + |
| 12 | +import unittest |
| 13 | +import jax.numpy as jnp |
| 14 | +import numpy as np |
| 15 | +from xlumina.vectorized_optics import PolarizedLightSource, VectorizedLight |
| 16 | + |
| 17 | +class TestVectorizedOptics(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + self.wavelength = 633e-3 #nm |
| 20 | + self.resolution = 1024 |
| 21 | + self.x = np.linspace(-1500, 1500, self.resolution) |
| 22 | + self.y = np.linspace(-1500, 1500, self.resolution) |
| 23 | + self.k = 2 * jnp.pi / self.wavelength |
| 24 | + |
| 25 | + def test_vectorized_light(self): |
| 26 | + light = VectorizedLight(self.x, self.y, self.wavelength) |
| 27 | + self.assertEqual(light.wavelength, self.wavelength) |
| 28 | + self.assertEqual(light.k, self.k) |
| 29 | + self.assertEqual(light.Ex.shape, (self.resolution, self.resolution)) |
| 30 | + self.assertEqual(light.Ey.shape, (self.resolution, self.resolution)) |
| 31 | + self.assertEqual(light.Ez.shape, (self.resolution, self.resolution)) |
| 32 | + |
| 33 | + def test_polarized_light_source_horizontal(self): |
| 34 | + source = PolarizedLightSource(self.x, self.y, self.wavelength) |
| 35 | + source.gaussian_beam(w0=(1200, 1200), jones_vector=(1, 0)) |
| 36 | + # TEST POLARIZATION |
| 37 | + self.assertGreater(jnp.sum(jnp.abs(source.Ex)**2), 0) |
| 38 | + self.assertEqual(jnp.sum(jnp.abs(source.Ey)**2), 0) |
| 39 | + |
| 40 | + def test_polarized_light_source_vertical(self): |
| 41 | + source = PolarizedLightSource(self.x, self.y, self.wavelength) |
| 42 | + source.gaussian_beam(w0=(1200, 1200), jones_vector=(0, 1)) |
| 43 | + # TEST POLARIZATION |
| 44 | + self.assertGreater(jnp.sum(jnp.abs(source.Ey)**2), 0) |
| 45 | + self.assertEqual(jnp.sum(jnp.abs(source.Ex)**2), 0) |
| 46 | + |
| 47 | + def test_polarized_light_source_diagonal(self): |
| 48 | + source = PolarizedLightSource(self.x, self.y, self.wavelength) |
| 49 | + source.gaussian_beam(w0=(1200, 1200), jones_vector=(1, 1)) |
| 50 | + # TEST POLARIZATION |
| 51 | + self.assertGreater(jnp.sum(jnp.abs(source.Ex)**2), 0) |
| 52 | + self.assertGreater(jnp.sum(jnp.abs(source.Ey)**2), 0) |
| 53 | + |
| 54 | + def test_vrs_propagation(self): |
| 55 | + light = PolarizedLightSource(self.x, self.y, self.wavelength) |
| 56 | + light.gaussian_beam(w0=(1200, 1200), jones_vector=(1, 1)) |
| 57 | + propagated, _ = light.VRS_propagation(z=1000) |
| 58 | + self.assertEqual(propagated.Ex.shape, (self.resolution, self.resolution)) |
| 59 | + self.assertEqual(propagated.Ey.shape, (self.resolution, self.resolution)) |
| 60 | + self.assertEqual(propagated.Ez.shape, (self.resolution, self.resolution)) |
| 61 | + # Check same SoP |
| 62 | + self.assertGreater(jnp.sum(jnp.abs(light.Ex)**2), 0) |
| 63 | + self.assertGreater(jnp.sum(jnp.abs(light.Ey)**2), 0) |
| 64 | + |
| 65 | + def test_vczt(self): |
| 66 | + light = PolarizedLightSource(self.x, self.y, self.wavelength) |
| 67 | + light.gaussian_beam(w0=(1200, 1200), jones_vector=(1, 1)) |
| 68 | + propagated = light.VCZT(1000, self.x, self.y) |
| 69 | + self.assertEqual(propagated.Ex.shape, (self.resolution, self.resolution)) |
| 70 | + self.assertEqual(propagated.Ey.shape, (self.resolution, self.resolution)) |
| 71 | + self.assertEqual(propagated.Ez.shape, (self.resolution, self.resolution)) |
| 72 | + # Check same SoP |
| 73 | + self.assertGreater(jnp.sum(jnp.abs(light.Ex)**2), 0) |
| 74 | + self.assertGreater(jnp.sum(jnp.abs(light.Ey)**2), 0) |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments