File: mode_coeff_phase.py

package info (click to toggle)
meep 1.29.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,676 kB
  • sloc: cpp: 32,541; python: 31,061; javascript: 9,819; lisp: 1,225; makefile: 519; sh: 249; ansic: 131
file content (249 lines) | stat: -rw-r--r-- 6,889 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""
Verifies that the magnitude and phase of the reflection coefficient of a
total internal reflected (TIR) mode of a flat interface of two lossless
materials given an incident planewave at oblique incidence computed using
the mode-decomposition feature matches the Fresnel equations.

ref: https://meep.readthedocs.io/en/latest/Python_Tutorials/Mode_Decomposition/#phase-of-a-total-internal-reflected-mode
"""

import cmath
from enum import Enum
import math

import meep as mp
import numpy as np

Polarization = Enum("Polarization", "S P")

# refractive indices of materials 1 and 2
n1 = 1.5
n2 = 1.0


def refl_coeff_meep(pol: Polarization, theta: float, L: float) -> complex:
    """Returns the complex reflection coefficient of a TIR mode computed
       using mode decomposition.

    Args:
        pol: polarization of the incident planewave (S or P).
        theta: angle of the incident planewave (radians).
        L: position of the mode monitor relative to the flat interface.
    """
    if theta < math.asin(n2 / n1):
        raise ValueError(
            f"incident angle of {math.degrees(theta):.2f}° is "
            f"not a total internal reflected mode."
        )

    resolution = 50.0

    # cell size is arbitrary
    sx = 7.0
    sy = 3.0
    dpml = 2.0
    cell_size = mp.Vector3(sx + 2 * dpml, sy, 0)
    pml_layers = [mp.PML(dpml, direction=mp.X)]

    fcen = 1.0  # source/monitor frequency
    df = 0.1 * fcen

    # k (in source medium) with correct length
    # plane of incidence is xy
    k = mp.Vector3(n1 * fcen, 0, 0).rotate(mp.Vector3(0, 0, 1), theta)

    # planewave amplitude function (for line source)
    def pw_amp(k, x0):
        def _pw_amp(x):
            return cmath.exp(1j * 2 * math.pi * k.dot(x + x0))

        return _pw_amp

    src_pt = mp.Vector3(-0.5 * sx, 0, 0)

    if pol.name == "S":
        src_cmpt = mp.Ez
        eig_parity = mp.ODD_Z
    elif pol.name == "P":
        src_cmpt = mp.Hz
        eig_parity = mp.EVEN_Z
    else:
        raise ValueError("pol must be S or P, only.")

    sources = [
        mp.Source(
            mp.GaussianSource(fcen, fwidth=df),
            component=src_cmpt,
            center=src_pt,
            size=mp.Vector3(0, cell_size.y, 0),
            amp_func=pw_amp(k, src_pt),
        ),
    ]

    sim = mp.Simulation(
        resolution=resolution,
        cell_size=cell_size,
        default_material=mp.Medium(index=n1),
        boundary_layers=pml_layers,
        k_point=k,
        sources=sources,
    )

    # DFT monitor for incident fields
    mode_mon = sim.add_mode_monitor(
        fcen,
        0,
        1,
        mp.FluxRegion(
            center=mp.Vector3(-L, 0, 0),
            size=mp.Vector3(0, cell_size.y, 0),
        ),
    )

    sim.run(
        until_after_sources=mp.stop_when_fields_decayed(
            50,
            src_cmpt,
            mp.Vector3(-L, 0, 0),
            1e-6,
        ),
    )

    res = sim.get_eigenmode_coefficients(
        mode_mon,
        bands=[1],
        eig_parity=eig_parity,
        kpoint_func=lambda *not_used: k,
        direction=mp.NO_DIRECTION,
    )

    input_mode_coeff = res.alpha[0, 0, 0]
    input_flux_data = sim.get_flux_data(mode_mon)

    sim.reset_meep()

    geometry = [
        mp.Block(
            material=mp.Medium(index=n1),
            center=mp.Vector3(-0.25 * (sx + 2 * dpml), 0, 0),
            size=mp.Vector3(0.5 * (sx + 2 * dpml), mp.inf, mp.inf),
        ),
        mp.Block(
            material=mp.Medium(index=n2),
            center=mp.Vector3(0.25 * (sx + 2 * dpml), 0, 0),
            size=mp.Vector3(0.5 * (sx + 2 * dpml), mp.inf, mp.inf),
        ),
    ]

    sim = mp.Simulation(
        resolution=resolution,
        cell_size=cell_size,
        boundary_layers=pml_layers,
        k_point=k,
        sources=sources,
        geometry=geometry,
    )

    # DFT monitor for reflected fields
    mode_mon = sim.add_mode_monitor(
        fcen,
        0,
        1,
        mp.FluxRegion(
            center=mp.Vector3(-L, 0, 0),
            size=mp.Vector3(0, cell_size.y, 0),
        ),
    )

    sim.load_minus_flux_data(mode_mon, input_flux_data)

    sim.run(
        until_after_sources=mp.stop_when_fields_decayed(
            50,
            src_cmpt,
            mp.Vector3(-L, 0, 0),
            1e-6,
        ),
    )

    res = sim.get_eigenmode_coefficients(
        mode_mon,
        bands=[1],
        eig_parity=eig_parity,
        kpoint_func=lambda *not_used: k,
        direction=mp.NO_DIRECTION,
    )

    # mode coefficient of reflected planewave
    refl_mode_coeff = res.alpha[0, 0, 1]

    # reflection coefficient
    refl_coeff = refl_mode_coeff / input_mode_coeff

    # apply phase correction factor
    refl_coeff /= cmath.exp(1j * k.x * 2 * math.pi * 2 * L)

    return refl_coeff


def refl_coeff_Fresnel(pol: Polarization, theta: float) -> complex:
    """Returns the complex reflection coefficient of a TIR mode computed
       using the Fresnel equations.

    Args:
        pol: polarization of the incident planewave (S or P).
        theta: angle of the incident planewave (radians).
    """
    if pol.name == "S":
        refl_coeff = (
            math.cos(theta) - ((n2 / n1) ** 2 - math.sin(theta) ** 2) ** 0.5
        ) / (math.cos(theta) + ((n2 / n1) ** 2 - math.sin(theta) ** 2) ** 0.5)
    else:
        refl_coeff = (
            -((n2 / n1) ** 2) * math.cos(theta)
            + ((n2 / n1) ** 2 - math.sin(theta) ** 2) ** 0.5
        ) / (
            (n2 / n1) ** 2 * math.cos(theta)
            + ((n2 / n1) ** 2 - math.sin(theta) ** 2) ** 0.5
        )

    return refl_coeff


if __name__ == "__main__":
    # angle of incident planewave (degrees)
    thetas = [54.3, 48.5]

    # position of mode monitor relative to flat interface
    Ls = [0.4, 1.2]

    # polarization of incident planewave
    pols = [Polarization.S, Polarization.P]

    for pol, theta, L in zip(pols, thetas, Ls):
        theta_rad = np.radians(theta)
        R_meep = refl_coeff_meep(pol, theta_rad, L)
        R_fres = refl_coeff_Fresnel(pol, theta_rad)

        complex_to_str = lambda cnum: f"{cnum.real:.5f}{cnum.imag:+.5f}j"
        print(
            f"refl-coeff:, {pol.name}, {theta}, "
            f"{complex_to_str(R_meep)} (Meep), "
            f"{complex_to_str(R_fres)} (Fresnel)"
        )

        mag_meep = abs(R_meep)
        mag_fres = abs(R_fres)
        err_mag = abs(mag_meep - mag_fres) / mag_fres
        print(
            f"magnitude:, {mag_meep:.5f} (Meep), {mag_fres:.5f} (Fresnel), "
            f"{err_mag:.5f} (error)"
        )

        phase_meep = cmath.phase(R_meep)
        phase_fres = cmath.phase(R_fres)
        err_phase = abs(phase_meep - phase_fres) / abs(phase_fres)
        print(
            f"phase:, {phase_meep:.5f} (Meep), {phase_fres:.5f} (Fresnel), "
            f"{err_phase:.5f} (error)"
        )