From 001c32a2e0b85c327e36c5bbb62b283c4ba11f10 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Tue, 9 Jul 2019 20:08:33 -0700 Subject: [PATCH] Partially fix arbitrary mode phase --- fdfd_tools/waveguide.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fdfd_tools/waveguide.py b/fdfd_tools/waveguide.py index 1566a74..89e0d9c 100644 --- a/fdfd_tools/waveguide.py +++ b/fdfd_tools/waveguide.py @@ -112,9 +112,16 @@ def normalized_fields(v: numpy.ndarray, P = 0.5 * numpy.real(S.sum()) assert P > 0, 'Found a mode propagating in the wrong direction! P={}'.format(P) + energy = epsilon * e.conj() * e + norm_amplitude = 1 / numpy.sqrt(P) - norm_angle = -numpy.angle(e[e.size//2]) - norm_factor = norm_amplitude * numpy.exp(1j * norm_angle) + norm_angle = -numpy.angle(e[energy.argmax()]) # Will randomly add a negative sign when mode is symmetric + + # Try to break symmetry to assign a consistent sign [experimental] + E_weighted = unvec(e * energy * numpy.exp(1j * norm_angle), shape) + sign = numpy.sign(E_weighted[:, :max(shape[0]//2, 1), :max(shape[1]//2, 1)].real.sum()) + + norm_factor = sign * norm_amplitude * numpy.exp(1j * norm_angle) e *= norm_factor h *= norm_factor