% Constants
hbar          = 1.0545718e-34;     % Reduced Planck constant (Joule second)
m_e           = 9.10938356e-31;    % Mass of electron (kg)
wavelength    = 5e-9;              % de Broglie wavelength (meters)
k0            = 2 * pi / wavelength; % Wave number

% Grid parameters
Lx            = 1e-6;              % Simulation domain size (meters)
Nx            = 1024;              % Number of grid points
dx            = Lx / Nx;           % Grid spacing
x             = linspace(-Lx / 2, Lx / 2, Nx); % Spatial grid

% Time parameters
dt            = 3e-15;             % Time step (seconds)
Nt            = 1001;              % Number of time steps

% Wave packet parameters
sigma_x       = 60e-9;             % Width of the Gaussian packet (meters)
x0            = -2e-7;             % Initial position of the packet (meters)
kx0           = k0;                % Initial wave vector

% Barrier parameters
barrier_width  = 4e-8;             % Width of the square barrier (meters)
barrier_height = 0.8 * hbar^2 * k0^2 / (2 * m_e); % Height of the barrier (Joules)

% Initial Gaussian wave packet
psi0          = exp(-(x - x0).^2 / (2 * sigma_x^2)) .* exp(1i * kx0 * x);
psi0          = psi0 / sqrt(sum(abs(psi0).^2) * dx); % Normalization

% Potential energy (Square barrier in the center)
V             = zeros(size(x));
V(abs(x) < barrier_width / 2) = barrier_height;

% Fourier space components
kx            = (2 * pi / Lx) * (-Nx/2:Nx/2-1); % Frequency domain representation
kx            = fftshift(kx);                   % Shift zero frequency component to center
K2            = kx.^2;                          % Squared wave numbers

% Split-step Fourier method
psi           = psi0; % Initialize wave function
transmission_prob = 0; % Initialize transmission probability

figure(1); % Create a figure for visualization
clf
set(gcf,'Position',[100 100 950 750])
set(gca,'FontSize',16,'Box','On','Linewidth',2);
for t = 1:Nt
    % (a) 1/2 Evolution for the potential energy in real space
    psi = psi .* exp(-1i * V * dt / (2 * hbar));
    
    % (b) Forward transform to Fourier space
    psi_k = fft(psi);
    
    % (c) Full evolution for the kinetic energy in Fourier space
    psi_k = psi_k .* exp(-1i * hbar * K2 * dt / (2 * m_e));
    
    % (d) Inverse Fourier transform back to real space
    psi = ifft(psi_k);
    
    % (e) 1/2 Evolution for the potential energy in real space
    psi = psi .* exp(-1i * V * dt / (2 * hbar));
    
    % Calculate transmission probability
    Rho = abs(psi).^2; % Probability density
    transmission_prob = sum(Rho(x > barrier_width / 2)) * dx;
    
    % Visualization of wave packet evolution
    if mod(t, 50) == 0
        clf; % Clear figure before drawing
        plot(x * 1e9, Rho / max(Rho), 'r', 'LineWidth', 2); % Plot normalized density
        hold on;
        plot(x * 1e9, V / max(V), 'k', 'LineWidth', 2); % Plot barrier potential
        title(sprintf('Time step: %d\nTransmission Probability: %.3f', t, transmission_prob),'FontSize',16);
        xlabel('x (nm)', 'FontSize', 16);
        ylabel('Probability Density', 'FontSize', 16);
        grid on
        drawnow; % Update the figure dynamically
    end
end

% Final wave packet distribution
figure(2);
clf
set(gcf,'Position',[100 100 950 750])
set(gca,'FontSize',16,'Box','On','Linewidth',2);
plot(x * 1e9, abs(psi).^2, 'b', 'LineWidth', 2);
xlabel('x (nm)','FontSize',16);
ylabel('Probability Density','FontSize',16);
title('Final Wave Packet Distribution','FontSize',16);
grid on
%%