def optical_power(opt: dict) -> float:
"""P = 2 int Re(E x H*)_z dA (W)."""
E, H = opt["E"], opt["H"]
Sz = np.real(E[0] * np.conj(H[1]) - E[1] * np.conj(H[0]))
return 2 * np.trapezoid(np.trapezoid(Sz, opt["y"], axis=1), opt["x"])
def efield_interpolator(opt: dict):
"""Linear interpolator of the complex E field at points given in meters."""
f = [RegularGridInterpolator((opt["x"], opt["y"]), opt["E"][c]) for c in range(3)]
return lambda xm, ym: np.array([g(np.c_[xm, ym]) for g in f])
def interface(a, b, normal, eps_in, eps_out, n_per_um: int = 400) -> dict:
"""Sample points along a straight interface; the normal points from 'in' to 'out'."""
a, b, normal = (np.asarray(v, float) for v in (a, b, normal))
d = b - a
L = np.hypot(*d)
n = max(int(L * n_per_um), 8)
s = (np.arange(n) + 0.5) / n
return dict(pts=a + s[:, None] * d, nrm=np.tile(normal, (n, 1)), tan=np.tile(d / L, (n, 1)),
dl=np.full(n, L / n), eps_in=eps_in, eps_out=eps_out)
def polygon_interfaces(verts: list, eps_in, eps_out, keep=lambda a, b: True) -> list:
"""Interfaces along the edges of a counterclockwise polygon (outward normals)."""
v = np.array(verts, float)
out = []
for a, b in zip(v, np.roll(v, -1, axis=0)):
if keep(a, b):
d = (b - a) / np.hypot(*(b - a))
out.append(interface(a, b, (d[1], -d[0]), eps_in, eps_out))
return out
def kottke(eps_loc: np.ndarray) -> tuple:
"""Moving-boundary quantities for a relative permittivity in the local (n, t, z) frame."""
e = eps_loc
return (EPS0 * (e[1:, 1:] - np.outer(e[1:, 0], e[0, 1:]) / e[0, 0]),
1 / (EPS0 * e[0, 0]), e[0, 1:] / e[0, 0], e[1:, 0] / e[0, 0])
def overlaps(opt, elastic, i, pe_regions, interfaces, h=0.002, delta=0.005, backward=True):
"""Q_PE, Q_RO, Q_MB (SI) and max|u| for elastic mode i."""
Efun = efield_interpolator(opt)
stokes = np.conj if backward else (lambda a: a)
Q_pe = Q_ro = Q_mb = 0j
umax = 0.0
for verts, p_lab, eps_r in pe_regions:
X, Y = area_grid(verts, h)
u, G = elastic.fields(i, X, Y)
G = G / UM
S = 0.5 * (G + G.transpose(1, 0, 2))
R = 0.5 * (G - G.transpose(1, 0, 2))
Ep = Efun(X * UM, Y * UM)
Es = stokes(Ep)
d_pe = -EPS0 * np.einsum("ia,abn,bj->ijn", eps_r, np.einsum("ijkl,kln->ijn", p_lab, S), eps_r)
d_ro = EPS0 * (np.einsum("ikn,kj->ijn", R, eps_r) - np.einsum("ik,kjn->ijn", eps_r, R))
dA = (h * UM) ** 2
Q_pe += np.einsum("in,ijn,jn->", Ep.conj(), d_pe, Es) * dA
Q_ro += np.einsum("in,ijn,jn->", Ep.conj(), d_ro, Es) * dA
umax = max(umax, np.sqrt((np.abs(u) ** 2).sum(0)).max())
for itf in interfaces:
pts, nrm, tan, dl = itf["pts"], itf["nrm"], itf["tan"], itf["dl"]
ub, _ = elastic.fields(i, pts[:, 0] - 1e-5 * nrm[:, 0], pts[:, 1] - 1e-5 * nrm[:, 1])
umax = max(umax, np.sqrt((np.abs(ub) ** 2).sum(0)).max())
E1, E2 = (Efun((pts[:, 0] - s * delta * nrm[:, 0]) * UM, (pts[:, 1] - s * delta * nrm[:, 1]) * UM) for s in (1, 2))
Eb = 2 * E1 - E2 # linear extrapolation to the interface
for k in range(len(pts)):
Qf = np.array([[nrm[k, 0], nrm[k, 1], 0], [tan[k, 0], tan[k, 1], 0], [0, 0, 1.0]])
tI, iI, cnI, ctI = kottke(Qf @ itf["eps_in"] @ Qf.T)
tO, iO, cnO, ctO = kottke(Qf @ itf["eps_out"] @ Qf.T)
Ep_l, Es_l = Qf @ Eb[:, k], Qf @ stokes(Eb[:, k])
Dp = EPS0 * (Qf @ itf["eps_in"] @ Eb[:, k])[0]
Ds = EPS0 * (Qf @ itf["eps_in"] @ stokes(Eb[:, k]))[0]
bracket = (Ep_l[1:].conj() @ (tI - tO) @ Es_l[1:] - (iI - iO) * Dp.conj() * Ds
+ Dp.conj() * ((cnI - cnO) @ Es_l[1:]) + (Ep_l[1:].conj() @ (ctI - ctO)) * Ds)
Q_mb += (ub[0, k] * nrm[k, 0] + ub[1, k] * nrm[k, 1]) * bracket * dl[k] * UM
return Q_pe, Q_ro, Q_mb, umax
def sbs_gain(opt, elastic, i, pe_regions, interfaces, mass_region=".*", **kwargs) -> dict:
"""Normalized backward SBS gain G_B/Q_m (1/(W m)) split into its contributions."""
Q_pe, Q_ro, Q_mb, umax = overlaps(opt, elastic, i, pe_regions, interfaces, **kwargs)
P = optical_power(opt)
Om = elastic.Omega[i]
m_int = elastic.mass_integral(i, mass_region) * UM**2
pref = 2 * (2 * np.pi * freq0) / (P**2 * Om**2 * m_int)
return dict(f=Om / (2 * np.pi * 1e9), m_eff=m_int / umax**2, PE=pref * abs(Q_pe) ** 2, MB=pref * abs(Q_mb) ** 2,
RO=pref * abs(Q_ro) ** 2, total=pref * abs(Q_pe + Q_mb + Q_ro) ** 2, Q=(Q_pe, Q_mb, Q_ro), P=P)