21 KiB
- System Parameters
- Definition of the RHS
- Scipy Hack
- IDESolver
- Interpolation
- Calculate the Convolutions
- Analytical Solution
import scipy
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
import utilities
System Parameters
Ω = 1.5
A = np.array([[0, Ω], [-Ω, 0]])
η = 2
ω_c = 1
t_max = 16
def α(t):
return η / np.pi * (ω_c / (1 + 1j * ω_c * t)) ** 2
def α_0_dot(t):
return 2 * η / (1j * np.pi) * (ω_c / (1 + 1j * ω_c * t)) ** 3
def K(τ):
return np.array([[0, 0], [α(τ).imag, 0]])
def L(t, s):
return np.exp(-A * t) @ K(t - s) @ np.exp(A * s)
Definition of the RHS
def H_dot(t, _):
return -integrate.trapezoid(
[(L(t, ts[s]) @ np.array(H_to_date[s])).flatten() for s in range(len(ts))],
ts,
axis=0,
)
def K_dot(t, curr):
return (A @ curr.reshape(2, 2)).flatten() - integrate.simpson(
[(K(t - ts[s]) @ np.array(H_to_date[s])).flatten() for s in range(len(ts))]
+ [(K(0) @ curr.reshape(2, 2)).flatten()],
ts + [t],
axis=0,
)
Scipy Hack
Hacking together an integro-differential solver.
ts = [0]
H_to_date = [np.eye(2)]
r = integrate.ode(K_dot).set_integrator('vode')
r.set_initial_value(np.eye(2, dtype="float").flatten(), 0)
t1 = t_max
dt = .001
while r.successful() and r.t < t1:
sol = r.integrate(r.t + dt)
H_to_date.append(sol.reshape(2,2))
ts.append(r.t + dt)
Dividing out the unitary dynamics.
#plt.plot(ts, (np.array([scipy.linalg.expm(-A * np.array(t)) for t in ts]) @ np.array(H_to_date))[:,1,0])
proper = np.array(H_to_date)
\(\langle q\rangle\)
plt.plot(ts, proper @ np.array([1, 0]))
<matplotlib.lines.Line2D | at | 0x7f3384cfda60> | <matplotlib.lines.Line2D | at | 0x7f3384cfd9d0> |
This looks like a slightly dampened HO.
IDESolver
With a proper solver it works like a charm. See the docs.
from idesolver import IDESolver
steps = 1000
t = np.linspace(0, t_max, steps)
solver = IDESolver(
x = t,
y_0 = np.array([1, 0, 0, 1]),
c = lambda x, y: (A @ y.reshape(2, 2)).flatten(),
d = lambda x: 1,
k = lambda x, s: -K(x-s)[1, 0],
f = lambda y: np.array([0, 0, y[0], y[1]]),
lower_bound = lambda x: 0,
upper_bound = lambda x: x,
)
sol = solver.solve()
/nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 0 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 1 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 2 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 3 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 4 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 5 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 6 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 7 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 8 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 9 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 10 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 11 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 12 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 13 warnings.warn( /nix/store/j2851gf1ssx8b9qz14b8n44f1cq5dhi9-python3-3.9.4-env/lib/python3.9/site-packages/idesolver/idesolver.py:292: IDEConvergenceWarning: Error increased on iteration 14 warnings.warn(
Reshape \(G\) into a time series of matrices.
G = np.einsum("ijk->kij", sol.reshape((2,2,steps)))
And plot the time development of the mean values of \(p\) and \(p\).
plt.plot(t, G @ np.array([1, 0]))
<matplotlib.lines.Line2D | at | 0x7f4794650670> | <matplotlib.lines.Line2D | at | 0x7f4794650520> |
Looks OK.
plt.plot(
t,
(np.array([scipy.linalg.expm(-A * np.array(τ)) for τ in t]) @ G).reshape(steps, 4),
)
<matplotlib.lines.Line2D | at | 0x7fef5c714370> | <matplotlib.lines.Line2D | at | 0x7fef5c7143a0> | <matplotlib.lines.Line2D | at | 0x7fef5c7144c0> | <matplotlib.lines.Line2D | at | 0x7fef5c7145e0> |
We see that the derivative at the begining is really 0 if we divide out the unitary dynamics.
The scipy integration arrives at pretty much the same picture :).
plt.plot(
ts,
(np.array([scipy.linalg.expm(-A * np.array(t)) for t in ts]) @ np.array(H_to_date)).reshape(len(ts), 4),
label="scipy"
)
plt.plot(
t,
(np.array([scipy.linalg.expm(-A * np.array(τ)) for τ in t]) @ G).reshape(steps, 4),
linestyle="--",
label="idesolve"
)
plt.legend()
plt.xlabel('t')
plt.ylabel('$e^{-At}G_{ij}$')
Text(0, 0.5, '$e^{-At}G_{ij}$')
The initial slip is removed in idesolve due to the iteration. You have to look at the off diagonal curves to see that.
plt.plot(t, np.linalg.det(G))
<matplotlib.lines.Line2D | at | 0x7fef5c614c40> |
We see that the "volume" bleeds out of the system.
Interpolation
import fcSpline
class FCSWrap:
def __init__(self, t_min, t_max, G):
self._G = G
self._t_min = t_min
self._t_max = t_max
self._splines = np.array(
list(map(lambda g: fcSpline.FCS(self._t_min, self._t_max, g), G))
)
def __call__(self, t):
t = np.asarray(t).astype('float64')
res = np.array(list(map(lambda g: g(t), self._splines)))
if t.size == 1:
return res.reshape((2,2))
return res.reshape((2,2,t.size)).swapaxes(0,2).swapaxes(1,2)
def __getitem__(self, key):
return self._splines.reshape(2,2)[key]
G_inter = FCSWrap(t.min(), t.max(), sol)
Calculate the Convolutions
We use quadpy for complex integration.
import quadpy
We will need \(G_{12}\) often.
G12 = G_inter[0, 1]
Calculate \(G_{12}\ast \dot{\alpha_0}\).
G12_star_α_0_dot = np.array(
[
quadpy.quad(lambda τ: G12(τ) * α_0_dot(t_now - τ), 0, t_now)[0]
for t_now in t
]
)
G12_star_α_0_dot = fcSpline.FCS(t.min(), t.max(), G12_star_α_0_dot)
Calculate \(\tau_r\alpha\ast G_{12}\).
τ_r_alpha_star_G12 = np.array(
[
[
quadpy.quad(lambda τ: G_inter[0, 1](t_now - τ) * α_0_dot(τ - r), 0, t_now)[
0
]
for r in t
]
for t_now in t
]
)
τ_r_alpha_star_G12_real = scipy.interpolate.interp2d(t, t, τ_r_alpha_star_G12.real)
τ_r_alpha_star_G12_imag = scipy.interpolate.interp2d(t, t, τ_r_alpha_star_G12.imag)
def τ_r_alpha_star_G12_inter(t, r):
return τ_r_alpha_star_G12_real(t, r) + 1j * τ_r_alpha_star_G12_imag(t, r)
plt.plot(t, G12_star_α_0_dot(t).imag)
plt.plot(t, G12_star_α_0_dot(t).real)
<matplotlib.lines.Line2D | at | 0x7fef55daa2b0> |
plt.imshow(τ_r_alpha_star_G12.real)
<matplotlib.image.AxesImage at 0x7fef5d1550d0>
plt.imshow(τ_r_alpha_star_G12.imag)
<matplotlib.image.AxesImage at 0x7fef812c1970>
And the final integral.
flow_half = np.array(
[
quadpy.quad(
lambda r: G12_star_α_0_dot(t_now - r) * τ_r_alpha_star_G12_inter(t_now, r)[0],
0,
t_now,
)[0]
for t_now in t
]
)
flow_half = -1/2 * flow_half.imag
plt.plot(t, flow_half)
<matplotlib.lines.Line2D | at | 0x7fef56207490> |
And now the \(G\) part of the flow.
n = 1
flow_G_half = np.array(
[
quadpy.quad(
lambda s: (
(
G_inter[0, 0](t_now) * G_inter[0, 1](s)
- G_inter[0, 0](s) * G_inter[0, 1](t_now)
)
,* α_0_dot(t_now - s).real
+ (2 * n + 1)
,* (
G_inter[0, 0](t_now) * G_inter[0, 0](s)
+ G_inter[0, 1](t_now) * G_inter[0, 1](s)
)
,* α_0_dot(t_now - s).imag
),
0,
t_now,
)[0]
for t_now in t
]
)
flow_G_half = -1 / 2 * flow_G_half
plt.plot(t, flow_G_half)
<matplotlib.lines.Line2D | at | 0x7fef567f73a0> |
flow = flow_G_half + flow_half
plt.plot(t, flow)
<matplotlib.lines.Line2D | at | 0x7fef565810a0> |
#+end_src
scipy.integrate.trapz(flow, t)
1.039473736501838
There remains the zero point energy.
def integrand(r, s, l, t_now):
return -1 / 2 * (G12(t_now - l) * G12(s - r) * α(l - r) * α_0_dot(t_now - s)).imag
flow_half_alt = np.array(
[
scipy.integrate.nquad(
integrand, [lambda s, _, __: [0, s], [0, t_now], [0, t_now]], args=[t_now]
)
for t_now in np.linspace(0, t.max(), 100)
]
)
flow_half_alt_int = fcSpline.FCS(0, t.max(), flow_half_alt[:, 0])
plt.plot(t, flow_half_alt_int(t))
plt.plot(t, flow_half)
<matplotlib.lines.Line2D | at | 0x7fef561557c0> |
flow_alt = flow_G_half + flow_half_alt_int(t)
plt.plot(t, flow_alt)
<matplotlib.lines.Line2D | at | 0x7fef5657b4c0> |
scipy.integrate.trapz(flow_alt, t)
1.1423457634247889
Analytical Solution
Exponential Fit
First we need an exponential fit for our BCF.
W, G_raw = utilities.fit_α(α, 3, 80, 10_000)
τ = np.linspace(0, t_max, 1000)
Looks quite good.
fig, ax = utilities.plot_complex(τ, α(τ), label="exact")
utilities.plot_complex(
τ, utilities.α_apprx(τ, G_raw, W), ax=ax, label="fit", linestyle="--"
)
hline | <AxesSubplot:> |
Calculate the Magic Numbers
We begin with the $\varphi_n$ and $G_n$ from the original G
.
φ = -np.angle(G_raw)
φ
array([-1.18710245, 0.64368323, 2.11154332])
G = np.abs(G_raw)
G
array([0.51238635, 0.62180167, 0.10107935])
Now we calculate the real and imag parts of the W
parameters and
call them $\gamma_n$ and $\delta_n$.
γ, δ = W.real, W.imag
Now the \(s_n, c_n\).
s, c = np.sin(φ), np.cos(φ)
Now we calculate the roots of $f_n(z)=-G_n ((z+\gamma_n) s_n + \delta_n c_n)$. Normally we should be vary of one of the \(\deltas\) being zero, but this is not the case.
roots_f = -(γ + δ * c/s)
roots_f
array([-1.19725058, -2.0384181 , -0.23027627])
Now the \(z_k\) the roots of \(\delta_k^2 + (\gamma_k + z)^2\). We don't include the conjugates.
roots_z = -W
roots_z
array([-2.29230292-2.71252483j, -1.07996581-0.719112j , -0.28445344-0.09022829j])
Construct the Polynomials
from numpy.polynomial import Polynomial
We later need \(f_0(z) = \prod_k (z-z_k) (z-z_k^{\ast})\).
f_0 = utilities.poly_real(Polynomial.fromroots(np.concatenate((roots_z, roots_z.conj()))))
f_0
\(x \mapsto \text{1.8908489286231014} + \text{15.192611898242761}\,x + \text{43.27633825204506}\,x^{2} + \text{49.327191029134}\,x^{3} + \text{28.124395600960767}\,x^{4} + \text{7.313444336316629}\,x^{5} + \text{1.0}\,x^{6}\)
Another polynomial is simply \(p_1 = (z^2 + \Omega^2)\prod_k (z-z_k) (z-z_k^{\ast})\) and we can construct it from its roots.
p_1 = Polynomial([Ω**2, 0, 1]) * f_0
p_1
\(x \mapsto \text{4.254410089401978} + \text{34.18337677104621}\,x + \text{99.26260999572447}\,x^{2} + \text{126.17879171379425}\,x^{3} + \text{106.55622835420678}\,x^{4} + \text{65.78244078584642}\,x^{5} + \text{30.374395600960767}\,x^{6} + \text{7.313444336316629}\,x^{7} + \text{1.0}\,x^{8}\)
The next ones are given through \(q_n=\Omega f_n(z) \prod_{k\neq n}(z-z_k) (z-z_k^{\ast})\)
q = [
-G_c
,* Ω * s_c
,* utilities.poly_real(Polynomial.fromroots(
np.concatenate(
(
[root_f],
utilities.except_element(roots_z, i),
utilities.except_element(roots_z, i).conj(),
)
)
))
for root_f, G_c, γ_c, δ_c, s_c, c_c, i in zip(roots_f, G, γ, δ, s, c, range(len(c)))
]
With this we construct our master polynomial \(p = p_1 + \sum_n q_n\).
p = p_1 + sum(q)
p
\(x \mapsto \text{2.4651931085584264} + \text{22.18343348787121}\,x + \text{75.66089297865284}\,x^{2} + \text{112.84892451644214}\,x^{3} + \text{104.42196182727498}\,x^{4} + \text{65.80539139821734}\,x^{5} + \text{30.374395600960767}\,x^{6} + \text{7.313444336316629}\,x^{7} + \text{1.0}\,x^{8}\)
And find its roots.
master_roots = p.roots()
master_roots
array([-2.28139877-2.68284887j, -2.28139877+2.68284887j, -0.93979297-0.62025112j, -0.93979297+0.62025112j, -0.24204514-0.10390896j, -0.24204514+0.10390896j, -0.19348529-1.49063362j, -0.19348529+1.49063362j])
Let's see if they're all unique. This should make things easier.
np.unique(master_roots).size == master_roots.size
True
Very nice!
Calculate the Residuals
These are the prefactors for the diagonal.
R_l = f_0(master_roots) / p.deriv()(master_roots)
R_l
array([ 0.00251589-0.00080785j, 0.00251589+0.00080785j, 0.06323421+0.02800137j, 0.06323421-0.02800137j, 0.02732669-0.00211665j, 0.02732669+0.00211665j, -0.09307679+0.36145133j, -0.09307679-0.36145133j])
And these are for the most compliciated element.
R_l_21 = (Ω + α_tilde(master_roots))* f_0(master_roots) / p.deriv()(master_roots)
R_l_21
array([-0.00325014-0.02160514j, -0.00325014+0.02160514j, 0.00074814-0.05845205j, 0.00074814+0.05845205j, -0.00094159-0.00084894j, -0.00094159+0.00084894j, 0.00344359+0.56219924j, 0.00344359-0.56219924j])
And the laplace transform of \(\alpha\).
def α_tilde(z):
return (
-G[None, :]
,* ((z[:, None] + γ[None, :]) * s[None, :] + δ[None, :] * c[None, :])
/ (δ[None, :] ** 2 + (γ[None, :] + z[:, None]) ** 2)
).sum(axis=1)
Now we can calculate \(G\).
def G_12_ex(t):
t = np.asarray(t)
return Ω * (R_l[None, :] * np.exp(t[:, None] * master_roots[None, :])).real.sum(
axis=1
)
def G_11_ex(t):
t = np.asarray(t)
return (
R_l[None, :]
,* master_roots[None, :]
,* np.exp(t[:, None] * master_roots[None, :])
).real.sum(axis=1)
def G_21_ex(t):
t = np.asarray(t)
return -(R_l_21[None, :] * np.exp(t[:, None] * master_roots[None, :])).real.sum(
axis=1
)
def G_21_ex_alt(t):
t = np.asarray(t)
return (
R_l[None, :]
,* master_roots[None, :] ** 2
,* np.exp(t[:, None] * master_roots[None, :])
).real.sum(axis=1) / Ω
def G_ex(t):
t = np.asarray(t)
if t.size == 1:
t = np.array([t])
diag = G_11_ex(t)
return (
np.array([[diag, G_12_ex(t)], [G_21_ex(t), diag]]).swapaxes(0, 2).swapaxes(1, 2)
)
def G_ex_new(t):
t = np.asarray(t)
if t.size == 1:
t = np.array([t])
g_12 = R_l[None, :] * np.exp(t[:, None] * master_roots[None, :])
diag = master_roots[None, :] * g_12
g_21 = master_roots[None, :] * diag / Ω
return (
np.array([[diag, g_12 * Ω], [g_21, diag]])
.real.sum(axis=3)
.swapaxes(0, 2)
.swapaxes(1, 2)
)
#plt.plot(τ, G_ex(τ).reshape(len(τ), 4))
plt.plot(τ, G_ex_new(τ).reshape(len(τ), 4))
plt.plot(ts, proper.reshape(len(ts), 4), linestyle="--")
<matplotlib.lines.Line2D | at | 0x7f337f09cfa0> | <matplotlib.lines.Line2D | at | 0x7f337f1cf9a0> | <matplotlib.lines.Line2D | at | 0x7f337f09fc40> | <matplotlib.lines.Line2D | at | 0x7f337f7a2ca0> |
plt.plot(τ, G_21_ex_alt(τ) - G_21_ex(τ))
<matplotlib.lines.Line2D | at | 0x7f337e630ac0> |