Skip to content
Snippets Groups Projects
Commit dbd273cc authored by Christoph Groth's avatar Christoph Groth
Browse files

sparse solver: include zero-shaped matrices in the RHS

parent c1954981
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,15 @@ import scipy.sparse as sp ...@@ -8,6 +8,15 @@ import scipy.sparse as sp
import scipy.sparse.linalg.dsolve.umfpack as umfpack import scipy.sparse.linalg.dsolve.umfpack as umfpack
from kwant import physics, system from kwant import physics, system
# Currently, scipy.sparse does not support matrices with one dimension being
# zero: http://projects.scipy.org/scipy/ticket/1602 We use numpy dense matrices
# as a replacement.
# TODO: Once this issue is fixed, code for the special cases can be removed
# from make_linear_sys, solve_linear_sys and possibly other places marked by
# the line "See comment about zero-shaped sparse matrices at the top."
# This patches a memory leak in scipy: # This patches a memory leak in scipy:
# http://projects.scipy.org/scipy/ticket/1597 # http://projects.scipy.org/scipy/ticket/1597
# #
...@@ -146,6 +155,8 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False): ...@@ -146,6 +155,8 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
modes = physics.modes(h, v) modes = physics.modes(h, v)
lead_info.append(modes) lead_info.append(modes)
if not np.any(v): if not np.any(v):
# See comment about zero-shaped sparse matrices at the top.
rhs.append(np.zeros((lhs.shape[1], 0)))
continue continue
u, ulinv, nprop, svd = modes u, ulinv, nprop, svd = modes
...@@ -176,17 +187,19 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False): ...@@ -176,17 +187,19 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
lhs = sp.bmat([[lhs, vdaguout_sp], [v_sp, lead_mat]]) lhs = sp.bmat([[lhs, vdaguout_sp], [v_sp, lead_mat]])
if nprop > 0 and leadnum in in_leads: if leadnum not in in_leads:
continue
if nprop > 0:
if svd: if svd:
vdaguin_sp = tmp.T * sp.csr_matrix(-np.dot(svd[2] * svd[1], vdaguin_sp = tmp.T * sp.csr_matrix(
u_in)) -np.dot(svd[2] * svd[1], u_in))
lead_mat_in = ulinv_in
else: else:
vdaguin_sp = tmp.T * sp.csr_matrix(-np.dot(v.T.conj(), vdaguin_sp = tmp.T * sp.csr_matrix(
u_in)) -np.dot(v.T.conj(), u_in))
lead_mat_in = ulinv_in rhs.append(sp.bmat([[vdaguin_sp], [ulinv_in]]))
else:
rhs.append(sp.bmat([[vdaguin_sp], [lead_mat_in]])) # See comment about zero-shaped sparse matrices at the top.
rhs.append(np.zeros((lhs.shape[1], 0)))
else: else:
sigma = lead.self_energy(energy) sigma = lead.self_energy(energy)
lead_info.append(sigma) lead_info.append(sigma)
...@@ -238,9 +251,9 @@ def solve_linear_sys(a, b, kept_vars=None): ...@@ -238,9 +251,9 @@ def solve_linear_sys(a, b, kept_vars=None):
sols = [] sols = []
vec = np.empty(a.shape[0], complex) vec = np.empty(a.shape[0], complex)
for mat in b: for mat in b:
if mat.shape[1] == 0: if mat.shape[1] != 0:
continue # See comment about zero-shaped sparse matrices at the top.
mat = sp.csr_matrix(mat) mat = sp.csr_matrix(mat)
for j in xrange(mat.shape[1]): for j in xrange(mat.shape[1]):
vec[: mat.shape[0]] = mat[:, j].todense().flatten() vec[: mat.shape[0]] = mat[:, j].todense().flatten()
vec[mat.shape[0] :] = 0 vec[mat.shape[0] :] = 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment