Speed up my RGF algorithm

조회 수: 4 (최근 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