Speed up my RGF algorithm

6 次查看(过去 30 天)
jo
jo 2024-4-3
编辑: jo 2024-4-3
Hi everyone,
I have this function called solve_transport_negf which allows me to solve a 1D transport model with Green's formalism.
I am trying to speed up this routine that takes approximately 15 seconds on my device, to less than 1 second. Do you think it's possible?
I am attaching the whole code below with a profiler as well.
The most critical steps are the vector operations inside the two for loops for calculating g and GR. A smart move would be to use parfoor but each iteration is based on the value of a previous one. I tried to vectorize as much as possible but the final result still takes too long.
It is doable, but I need some help to see how. Thank you in advance
function [E, TEL, TER, gEL, gER] = solve_transport_negf(varargin)
tollim = 1e-6;
e = 1.6022e-19;
mat = varargin{1}{1}{1};
Nx = mat.Nx;
dE = mat.dE;
V = mat.V;
x = mat.x;
dx = x(2) - x(1);
H = calc_discrete_hamiltonian(Nx, mat, V);
Emin = min(V);
Emax = max(V) + 0.3;
E = Emin - 5 * dE:dE:Emax;
NE = length(E);
TEL = zeros(1, NE);
TER = zeros(1, NE);
gEL = zeros(NE, Nx);
gER = zeros(NE, Nx);
TL = -H(1, 2);
TR = -H(Nx - 1, Nx);
for IE = 1:NE
DL = E(IE) - H(1, 1);
kL = 1 / dx * acos(-DL / (2 * TL));
Sigma11R = -TL * exp(1i * kL * dx);
Gamma11 = 1i * (Sigma11R - conj(Sigma11R));
DR = E(IE) - H(Nx, Nx);
kR = 1 / dx * acos(-DR / (2 * TR));
SigmaNNR = -TR * exp(1i * kR * dx);
GammaNN = 1i * (SigmaNNR - conj(SigmaNNR));
SigmaR = sparse(Nx, Nx);
SigmaR(1, 1) = Sigma11R;
SigmaR(Nx, Nx) = SigmaNNR;
g = zeros(1, Nx);
GR = zeros(Nx, Nx);
g(Nx) = 1 ./ (E(IE) - H(Nx, Nx) - SigmaR(Nx, Nx));
for I = Nx - 1:-1:2
g(I) = 1 ./ (E(IE) - H(I, I) - H(I, I + 1) * g(I + 1) * H(I + 1, I));
end
GR(1, 1) = 1 / (E(IE) - H(1, 1) - SigmaR(1, 1) - H(1, 2) * g(2) * H(2, 1));
for I = 2:Nx
GR(I, 1) = g(I) * H(I, I - 1) * GR(I - 1, 1);
GR(I, I) = g(I) + g(I) * (-H(I, I - 1)) * GR(I - 1, I - 1) * (-H(I - 1, I)) * g(I);
end
gEL(IE, :) = real(1e9 / (2 * pi * dx) * diag(GR(:, 1) .* Gamma11 * ctranspose(GR(:, 1))));
gER(IE, :) = real(-1e9 / (2 * pi * dx) * 2 * imag(diag(GR)));
TEL(IE) = GR(Nx, 1) * Gamma11 * conj(GR(Nx, 1)) * GammaNN;
TER(IE) = GR(1, Nx) * GammaNN * conj(GR(1, Nx)) * Gamma11;
end
gER = gER - gEL;
end
  4 个评论
Mike Croucher
Mike Croucher 2024-4-3
@Giovanni you are unlikley to get any help unless you provide everything required for us to run your code on our machines. This is because it is extremely difficult to make code go faster if we cannot run it.
jo
jo 2024-4-3
编辑:jo 2024-4-3
I see, please find attached the whole code needed to run the program. In main.m you can switch solver from WF to NEGF. The function that has to be modified is solve_transport_negf

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Frequently-used Algorithms 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by