PLaSK library
Loading...
Searching...
No Matches
reflection3d.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3# Copyright (c) 2022 Lodz University of Technology
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, version 3.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14import unittest
15
16from numpy import *
17
18from plask import *
19from plask import material, geometry, mesh
20from optical.modal import Fourier3D, PML
21
22
23@material.simple()
25 nr = 2.
26
27
29
30 def setUp(self):
31 config.axes = 'xyz'
32 background = geometry.Cuboid(1.0, 1.0, 0.2, None)
33 obj = geometry.Cuboid(0.5, 0.5, 0.2, Mat())
35 align.append(background, back=0., left=0., bottom=0.)
36 align.append(obj, back=0., left=0., bottom=0.)
37 geom = geometry.Cartesian3D(align, back='periodic', front='periodic', left='periodic', right='periodic')
38 self.solver = Fourier3D()
39 self.solver.geometry = geom
40 self.solver.set_interface(0.5)
41 self.solver.wavelength = 1000.
42 self.solver.smooth = 0.
43 self.solver.size = 11, 11 # number of material coefficients in each direction 4*11+1 = 45
44 self.solver.refine = 16, 16
45 self.solver.rule = 'direct'
46
47 # 2.5 = ( 1**2 + 2**2 ) / 2
48 # 1.6 = 2 / ( 1**(-2) + 2**(-2) )
49
50 def testTran(self):
51 msh_tran = mesh.Rectangular3D(mesh.Ordered([0.25]), mesh.Regular(0, 1, 46), mesh.Ordered([0.1]))
52 prof_tran = self.solver.outEpsilon(msh_tran, 'nearest').array
53 self.assertAlmostEqual(prof_tran[0,0,0,0,0], 2.5, 5)
54 self.assertAlmostEqual(prof_tran[0,0,0,1,1], 1.6, 5)
55 self.assertAlmostEqual(prof_tran[0,0,0,2,2], 2.5, 5)
56 self.assertAlmostEqual(prof_tran[0,1,0,0,0], 4., 5)
57 self.assertAlmostEqual(prof_tran[0,1,0,1,1], 4., 5)
58 self.assertAlmostEqual(prof_tran[0,1,0,2,2], 4., 5)
59 self.assertAlmostEqual(prof_tran[0,-2,0,0,0], 1., 5)
60 self.assertAlmostEqual(prof_tran[0,-2,0,1,1], 1., 5)
61 self.assertAlmostEqual(prof_tran[0,-2,0,2,2], 1., 5)
62 self.assertAlmostEqual(prof_tran[0,22,0,0,0], 4., 5)
63 self.assertAlmostEqual(prof_tran[0,22,0,1,1], 4., 5)
64 self.assertAlmostEqual(prof_tran[0,22,0,2,2], 4., 5)
65 self.assertAlmostEqual(prof_tran[0,23,0,0,0], 1., 5)
66 self.assertAlmostEqual(prof_tran[0,23,0,1,1], 1., 5)
67 self.assertAlmostEqual(prof_tran[0,23,0,2,2], 1., 5)
68 print("{} {} {}".format(*prof_tran[0, [0,1,-2], 0, :3].real))
69 # figure()
70 # plot(msh_tran.axis1, prof_tran[0,:,0,0].real, '.')
71 # plot(msh_tran.axis1, prof_tran[0,:,0,1].real, '.')
72 # plot(msh_tran.axis1, prof_tran[0,:,0,2].real, '.')
73 # xlabel(u'$x$ (µm)')
74 # ylabel(u'$n_r$')
75
76 def testLong(self):
77 msh_long = mesh.Rectangular3D(mesh.Regular(0, 1, 46), mesh.Ordered([0.25]), mesh.Ordered([0.1]))
78 prof_long = self.solver.outEpsilon(msh_long, 'nearest').array
79 self.assertAlmostEqual(prof_long[0,0,0,0,0], 1.6, 5)
80 self.assertAlmostEqual(prof_long[0,0,0,1,1], 2.5, 5)
81 self.assertAlmostEqual(prof_long[0,0,0,2,2], 2.5, 5)
82 self.assertAlmostEqual(prof_long[1,0,0,0,0], 4., 5)
83 self.assertAlmostEqual(prof_long[1,0,0,1,1], 4., 5)
84 self.assertAlmostEqual(prof_long[1,0,0,2,2], 4., 5)
85 self.assertAlmostEqual(prof_long[-2,0,0,0,0], 1., 5)
86 self.assertAlmostEqual(prof_long[-2,0,0,1,1], 1., 5)
87 self.assertAlmostEqual(prof_long[-2,0,0,2,2], 1., 5)
88 self.assertAlmostEqual(prof_long[22,0,0,0,0], 4., 5)
89 self.assertAlmostEqual(prof_long[22,0,0,1,1], 4., 5)
90 self.assertAlmostEqual(prof_long[22,0,0,2,2], 4., 5)
91 self.assertAlmostEqual(prof_long[23,0,0,0,0], 1., 5)
92 self.assertAlmostEqual(prof_long[23,0,0,1,1], 1., 5)
93 self.assertAlmostEqual(prof_long[23,0,0,2,2], 1., 5)
94 print("{} {} {}".format(*prof_long[[0,1,-2], 0, 0, :3].real))
95 # figure()
96 # plot(msh_long.axis0, prof_long[:,0,0,0].real, '.')
97 # plot(msh_long.axis0, prof_long[:,0,0,1].real, '.')
98 # plot(msh_long.axis0, prof_long[:,0,0,2].real, '.')
99 # xlabel(u'$z$ (µm)')
100 # ylabel(u'$n_r$')
101
102
103 def testWrappers(self):
104 self.solver.size = 2, 3
105 self.solver.refine = 5, 6
106
107 self.assertEqual(str(self.solver.size), "(2, 3)")
108
109 self.assertEqual(self.solver.size.x, 2)
110 self.assertEqual(self.solver.size[1], 3)
111
112 self.solver.size[0] = 4
113 self.assertEqual(self.solver.size.x, 4)
114
115 self.solver.wavelength = 1000.
116 self.solver.outEpsilon(mesh.Rectangular3D([0.], [0.25], [0.1]))
117 self.assertEqual(self.solver.initialized, True)
118 self.solver.size = 5
119 self.assertEqual(self.solver.initialized, False)
120 self.assertEqual(str(self.solver.size), "(5, 5)")
121
122 self.solver.outEpsilon(mesh.Rectangular3D([0.], [0.25], [0.1]))
123 self.assertEqual(self.solver.initialized, True)
124 self.solver.refine[1] = 7
125 self.assertEqual(self.solver.refine.y, 7)
126 self.assertEqual(self.solver.initialized, False)
127
128 print (self.solver.pmls)
129 self.solver.pmls = PML(1-2j, 1.0, 0.5)
130 self.assertEqual(self.solver.pmls[0].size, 1.)
131
132 self.assertEqual(self.solver.initialized, False)
133 self.solver.outEpsilon(mesh.Rectangular3D([0.], [0.25], [0.1]))
134 self.assertEqual(self.solver.initialized, True)
135 self.solver.pmls[1].size = 2.
136 self.assertEqual(self.solver.pmls.y.size, 2.)
137 self.assertEqual(self.solver.initialized, False)
138
139
140if __name__ == '__main__':