I want to calculate the kinetic energy of the dynamical wavefunction colliding with a well using
$$E = \int \mathrm{d}x \, \frac12 \left| \frac{\mathrm{d}\psi(x,t)}{\mathrm{d}x} \right|^2$$
which should be constant throughout the dynamics and the value comes close to $(NK^2)/2$.
For this particular case, $N=40$ (number of particles) and $K=0.1$ (speed). Here is my code for dynamics:
L = 200;
mu = -0.2222222222222222222222222211689169810351288660408099720909 -
1.2557735735320599617305095419979235954708106033*10^-31 I;
ϕ[x_, t_] := -3 mu Exp[-I mu t]/(1 +
Sqrt[1 + 9 mu/2] Cosh[Sqrt[-2 mu x^2]]);
particleno = NIntegrate[ϕ[x, 0]*Conjugate[ϕ[x, 0]], {x, -L, L}];(*N*)
W[x_] := If[0.5 > x > -0.5, -0.3, 0];(*potential*)
k0 = 0.1;(*speed or K*)
x0 = 65;(*initial position*)
eq = I D[u[x, t], t] == -1/2 D[u[x, t], x, x] +
Abs[u[x, t]]^2 u[x, t] - Abs[u[x, t]] u[x, t] + W[x] u[x, t];
ic = u[x, 0] ==
Exp[I k0 x] ϕ[x + x0, 0]; bc = {u[L, t] == ic[[2]] /. {x -> L},
u[-L, t] == ic[[2]] /. {x -> -L}};
ψ = NDSolveValue[{eq, bc, ic}, u, {x, -L, L}, {t, 0, 2000},
Method -> {"MethodOfLines",
"SpatialDiscretization" -> {"TensorProductGrid",
"MinPoints" -> 400, "MaxPoints" -> 2001,
"DifferenceOrder" -> 4}}, MaxSteps -> 10^6];
(*Plotting the dynamics*)
DensityPlot[Abs[ψ[x, t]]^2, {t, 0, 2000}, {x, -L, L},
AspectRatio -> 1/2, Frame -> True, FrameTicks -> Automatic,
PlotPoints -> 200, ImageSize -> 500,
ColorFunction -> "AvocadoColors",
FrameLabel -> {{Style["x", FontFamily -> "Times New Roman",
FontSlant -> "Italic", FontWeight -> Bold, FontSize -> 30],
None}, {Style["t", FontFamily -> "Times New Roman",
FontSlant -> "Italic", FontWeight -> Bold, FontSize -> 30],
None}}]
Here is my attempt to calculate and plot the energy throughout the dynamics
(*calculating ∫|(dψ(x,t))/dx|^2\[DifferentialD]x *)
data = Flatten[Table[{x, t, ψ[x, t]}, {x, -L, L}, {t, 0, 2000}], 1];
if = Interpolation[data]
dψdx = Derivative[1, 0][if]
finalkin =
Table[{t, NIntegrate[0.5 Abs[dψdx[x, t]]^2, {x, -L, L}]}, {t, 0,
2000, 500}]
(*plotting energy*)
ListLinePlot[finalkin, AspectRatio -> 1/2, Frame -> True,
FrameTicks -> Automatic, ImageSize -> 500,
PlotStyle -> {Orange, Thickness[0.005]},
LabelStyle -> {24, Bold, Large, Black},
FrameLabel -> {{Style["E", FontFamily -> "Times New Roman",
FontSlant -> "Italic", FontWeight -> Bold, FontSize -> 30],
None}, {Style["t", FontFamily -> "Times New Roman",
FontSlant -> "Italic", FontWeight -> Bold, FontSize -> 30],
None}}]