mirror of
https://github.com/vale981/master-thesis
synced 2025-03-12 22:26:38 -04:00
14 KiB
14 KiB
Configuration and Setup
This will be tangled into the config file that can be used with the HOPS cli.
from hops.core.hierarchy_parameters import HIParams, HiP, IntP, SysP, ResultType
from hops.core.hierarchyLib import HI
from hops.util.bcf_fits import get_ohm_g_w
from hops.util.truncation_schemes import TruncationScheme_Power_multi
import hops.util.bcf
import numpy as np
import hops.util.matrixLib as ml
from stocproc import StocProc_FFT
wc = 2
s = 1
# The BCF fit
bcf_terms = 5
g, w = get_ohm_g_w(bcf_terms, s, wc)
integration = IntP(t_max=30, t_steps=int(20 // 0.01))
system = SysP(
H_sys=0.5 * np.array([[-1, 0], [0, 1]]),
L=0.5 * np.array([[0, 1], [1, 0]]),
psi0=np.array([0, 1]),
g=g,
w=w,
bcf_scale=0.5,
T=1.5,
)
params = HIParams(
SysP=system,
IntP=integration,
HiP=HiP(
nonlinear=True,
normalized_by_hand=True,
result_type=ResultType.ZEROTH_AND_FIRST_ORDER,
truncation_scheme=TruncationScheme_Power_multi.from_g_w(
g=.5 * g, w=w, p=1, q=0.5, kfac=1.7
),
save_therm_rng_seed=True,
),
Eta=StocProc_FFT(
spectral_density=hops.util.bcf.OhmicSD_zeroTemp(
s,
1,
wc,
),
alpha=hops.util.bcf.OhmicBCF_zeroTemp(
s,
1,
wc,
),
t_max=integration.t_max,
intgr_tol=1e-5,
intpl_tol=1e-5,
negative_frequencies=False,
),
EtaTherm=StocProc_FFT(
spectral_density=hops.util.bcf.Ohmic_StochasticPotentialDensity(
s, 1, wc, beta=1 / system.__non_key__["T"]
),
alpha=hops.util.bcf.Ohmic_StochasticPotentialCorrelations(
s, 1, wc, beta=1 / system.__non_key__["T"]
),
t_max=integration.t_max,
intgr_tol=1e-5,
intpl_tol=1e-5,
negative_frequencies=False,
),
)
Using the Data
Jupyter Setup
import numpy as np
import matplotlib.pyplot as plt
import utilities as ut
import figsaver as fs
Let's export some infos about the model to TeX.
fs.tex_value(system.bcf_scale, prec=1, save="bcf_scale", prefix="η="), fs.tex_value(
wc, prec=0, save="cutoff_freq", prefix="ω_c="
), fs.tex_value(system.__non_key__["T"], prec=1, save="temp", prefix="T=")
\(η=0.5\) | \(ω_c=2\) | \(T=1.5\) |
Load the Data
from hopsflow import hopsflow, util
from hops.core.hierarchyData import HIMetaData
Now we read the trajectory data.
class result:
hd = HIMetaData("data", ".").get_HIData(params, read_only=True)
N = hd.samples
τ = hd.get_time()
ψ_1 = hd.aux_states
ψ = hd.stoc_traj
seeds = hd.rng_seed
result.N
fs.tex_value(result.N, prefix="N=", save="samples")
\(N=10000\)
Calculate System Energy
Simple sanity check.
_, e_sys, σ_e_sys = util.operator_expectation_ensemble(
iter(result.ψ),
system.H_sys,
result.N,
params.HiP.nonlinear,
save="./results/new_energy1.npy",
)
with fs.hiro_style():
plt.gcf().set_size_inches(fs.get_figsize(239, 1, .8))
plt.errorbar(result.τ[::100], e_sys.real[::100], yerr=σ_e_sys.real[::100], ecolor="yellow")
plt.ylabel(r"$\langle H_S\rangle$")
plt.xlabel(r"$τ$")
fs.export_fig("system_energy")
100% 9999/9999 [00:09<00:00, 1009.10it/s]
Calculate the Heat Flow
Now let's calculate the heatflow. In this simple case it is engouh to know the first hierarchy states.
First we set up some parameter objects for the alogrithm.
hf_system = hopsflow.SystemParams(
system.L, system.g, system.w, system.bcf_scale, params.HiP.nonlinear
)
η = params.Eta
ξ = params.EtaTherm
ξ.calc_deriv = True
ξ.set_scale(params.SysP.bcf_scale)
hf_therm = hopsflow.ThermalParams(ξ=ξ, τ=result.τ, num_deriv=False)
Now we can apply our tooling to one trajectory for testing.
hf_sample_run = hopsflow.HOPSRun(result.ψ[0], result.ψ_1[0], hf_system)
hf_sample_run_therm = hopsflow.ThermalRunParams(hf_therm, result.seeds[0])
first_flow = hopsflow.flow_trajectory_coupling(hf_sample_run, hf_system)
first_flow_therm = hopsflow.flow_trajectory_therm(hf_sample_run, hf_sample_run_therm)
plt.plot(result.τ, first_flow)
plt.plot(result.τ, first_flow_therm)
<matplotlib.lines.Line2D | at | 0x7f8956fadfd0> |
And now for all trajectories.
full_flow = hopsflow.heat_flow_ensemble(
iter(result.ψ),
iter(result.ψ_1),
hf_system,
result.N,
(iter(result.seeds), hf_therm),
every=result.N // 4,
save="results/flow_more.npy",
n_proc=4
)
with fs.hiro_style():
fig, ax = fs.plot_convergence(result.τ, full_flow, transform=lambda y: -y)
fig.set_size_inches(fs.get_figsize(239, 1, .8))
ax.legend()
ax.set_xlabel("$τ$")
ax.set_ylabel("$-J$")
#fs.export_fig("flow", fig)
1% 90/9999 [00:21<39:31, 4.18it/s]Process ForkPoolWorker-173: Process ForkPoolWorker-172:
[0;31m---------------------------------------------------------------------------[0m [0;31mIndexError[0m Traceback (most recent call last) [0;32m/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/multiprocessing/pool.py[0m in [0;36mnext[0;34m(self, timeout)[0m [1;32m 852[0m [0;32mtry[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;32m--> 853[0;31m [0mitem[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_items[0m[0;34m.[0m[0mpopleft[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 854[0m [0;32mexcept[0m [0mIndexError[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;31mIndexError[0m: pop from an empty deque During handling of the above exception, another exception occurred: [0;31mKeyboardInterrupt[0m Traceback (most recent call last) [0;32m/tmp/ipykernel_94001/1430602402.py[0m in [0;36m<module>[0;34m[0m [0;32m----> 1[0;31m full_flow = hopsflow.heat_flow_ensemble( [0m[1;32m 2[0m [0miter[0m[0;34m([0m[0mresult[0m[0;34m.[0m[0mψ[0m[0;34m)[0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m [1;32m 3[0m [0miter[0m[0;34m([0m[0mresult[0m[0;34m.[0m[0mψ_1[0m[0;34m)[0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m [1;32m 4[0m [0mhf_system[0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m [1;32m 5[0m [0mresult[0m[0;34m.[0m[0mN[0m[0;34m,[0m[0;34m[0m[0;34m[0m[0m [0;32m/nix/store/c9msmd5k6clygvhbasl6871wb2ldg58c-python3-3.9.9-env/lib/python3.9/site-packages/hopsflow/hopsflow.py[0m in [0;36mheat_flow_ensemble[0;34m(ψ_0s, ψ_1s, params, N, therm_args, only_therm, **kwargs)[0m [1;32m 357[0m [0;32mraise[0m [0mValueError[0m[0;34m([0m[0;34m"Can't calculate only thermal part if therm_args are None."[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 358[0m [0;34m[0m[0m [0;32m--> 359[0;31m return util.ensemble_mean( [0m[1;32m 360[0m [0miter[0m[0;34m([0m[0mzip[0m[0;34m([0m[0mψ_0s[0m[0;34m,[0m [0mψ_1s[0m[0;34m,[0m [0mtherm_args[0m[0;34m[[0m[0;36m0[0m[0;34m][0m[0;34m)[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 361[0m [0;32mif[0m [0mtherm_args[0m[0;34m[0m[0;34m[0m[0m [0;32m/nix/store/c9msmd5k6clygvhbasl6871wb2ldg58c-python3-3.9.9-env/lib/python3.9/site-packages/hopsflow/util.py[0m in [0;36mensemble_mean[0;34m(arg_iter, function, N, const_args, const_kwargs, n_proc, every, save)[0m [1;32m 221[0m ) [1;32m 222[0m [0;34m[0m[0m [0;32m--> 223[0;31m [0;32mfor[0m [0mres[0m [0;32min[0m [0mtqdm[0m[0;34m([0m[0mresult_iter[0m[0;34m,[0m [0mtotal[0m[0;34m=[0m[0;34m([0m[0mN[0m [0;34m-[0m [0;36m1[0m[0;34m)[0m [0;32mif[0m [0mN[0m [0;32melse[0m [0;32mNone[0m[0;34m)[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 224[0m [0maggregate[0m[0;34m.[0m[0mupdate[0m[0;34m([0m[0mres[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [1;32m 225[0m [0;34m[0m[0m [0;32m/nix/store/c9msmd5k6clygvhbasl6871wb2ldg58c-python3-3.9.9-env/lib/python3.9/site-packages/tqdm/std.py[0m in [0;36m__iter__[0;34m(self)[0m [1;32m 1178[0m [0;34m[0m[0m [1;32m 1179[0m [0;32mtry[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;32m-> 1180[0;31m [0;32mfor[0m [0mobj[0m [0;32min[0m [0miterable[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 1181[0m [0;32myield[0m [0mobj[0m[0;34m[0m[0;34m[0m[0m [1;32m 1182[0m [0;31m# Update and possibly print the progressbar.[0m[0;34m[0m[0;34m[0m[0m [0;32m/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/multiprocessing/pool.py[0m in [0;36m<genexpr>[0;34m(.0)[0m [1;32m 446[0m [0mresult[0m[0;34m.[0m[0m_set_length[0m[0;34m[0m[0;34m[0m[0m [1;32m 447[0m )) [0;32m--> 448[0;31m [0;32mreturn[0m [0;34m([0m[0mitem[0m [0;32mfor[0m [0mchunk[0m [0;32min[0m [0mresult[0m [0;32mfor[0m [0mitem[0m [0;32min[0m [0mchunk[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 449[0m [0;34m[0m[0m [1;32m 450[0m def apply_async(self, func, args=(), kwds={}, callback=None, [0;32m/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/multiprocessing/pool.py[0m in [0;36mnext[0;34m(self, timeout)[0m [1;32m 856[0m [0mself[0m[0;34m.[0m[0m_pool[0m [0;34m=[0m [0;32mNone[0m[0;34m[0m[0;34m[0m[0m [1;32m 857[0m [0;32mraise[0m [0mStopIteration[0m [0;32mfrom[0m [0;32mNone[0m[0;34m[0m[0;34m[0m[0m [0;32m--> 858[0;31m [0mself[0m[0;34m.[0m[0m_cond[0m[0;34m.[0m[0mwait[0m[0;34m([0m[0mtimeout[0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 859[0m [0;32mtry[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [1;32m 860[0m [0mitem[0m [0;34m=[0m [0mself[0m[0;34m.[0m[0m_items[0m[0;34m.[0m[0mpopleft[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0;32m/nix/store/dn4fwp0yx6nsa85cr20cwvdmg64xwmcy-python3-3.9.9/lib/python3.9/threading.py[0m in [0;36mwait[0;34m(self, timeout)[0m [1;32m 310[0m [0;32mtry[0m[0;34m:[0m [0;31m# restore state no matter what (e.g., KeyboardInterrupt)[0m[0;34m[0m[0;34m[0m[0m [1;32m 311[0m [0;32mif[0m [0mtimeout[0m [0;32mis[0m [0;32mNone[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;32m--> 312[0;31m [0mwaiter[0m[0;34m.[0m[0macquire[0m[0;34m([0m[0;34m)[0m[0;34m[0m[0;34m[0m[0m [0m[1;32m 313[0m [0mgotit[0m [0;34m=[0m [0;32mTrue[0m[0;34m[0m[0;34m[0m[0m [1;32m 314[0m [0;32melse[0m[0;34m:[0m[0;34m[0m[0;34m[0m[0m [0;31mKeyboardInterrupt[0m:
We can integrate the energy change in the bath:
import scipy.integrate
e_bath = np.array([0] + [
scipy.integrate.simpson(-full_flow[-1][1][:i], result.τ[:i])
for i in range(1, len(result.τ))
])
plt.plot(result.τ, e_bath)
σ_e_bath = np.sqrt(np.array([0] + [
scipy.integrate.simpson(full_flow[-1][2][:i]**2, result.τ[:i])
for i in range(1, len(result.τ))
])).real
plt.errorbar(result.τ, e_bath, yerr=σ_e_bath, ecolor="yellow")
<ErrorbarContainer object of 3 artists>
Calculate the Interaction Energy
First we calculate it from energy conservation.
e_int = (1/2 - e_sys - e_bath).real
σ_e_int = np.sqrt(σ_e_sys ** 2 + σ_e_bath ** 2).real
plt.errorbar(result.τ, e_int, yerr=σ_e_int, ecolor="yellow")
<ErrorbarContainer object of 3 artists>
And then from first principles:
_, e_int_ex, σ_e_int_ex = hopsflow.interaction_energy_ensemble(
result.ψ,
result.ψ_1,
hf_system,
result.N,
(result.seeds, hf_therm),
save="results/interaction_energy_new.npy",
)
100% 499/499 [02:09<00:00, 3.86it/s]
And both together:
with fs.hiro_style():
plt.errorbar(result.τ, e_int, yerr=σ_e_int, label="from energy conservation", ecolor="yellow")
plt.errorbar(result.τ, e_int_ex, yerr=σ_e_int_ex, label="direct", ecolor="pink")
plt.gcf().set_size_inches(fs.get_figsize(239, 1, .8))
plt.legend()
plt.ylabel(r"$\langle H_I\rangle$")
plt.xlabel(r"$τ$")
#fs.export_fig("interaction")
Seems to work :P.
plt.plot(result.τ, 1/2 - np.array(e_bath) - e_int_ex )
plt.plot(result.τ, e_sys)
/nix/store/maj240ris2x1a0r1wz26m0m785bc0xn9-python3-3.9.9-env/lib/python3.9/site-packages/matplotlib/cbook/__init__.py:1298: ComplexWarning: Casting complex values to real discards the imaginary part return np.asarray(x, float)
<matplotlib.lines.Line2D | at | 0x7f15e986fbb0> |
Close the Data File
We need to release the hold on the file.
result.hd.close()