Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
K
kwant
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joseph Weston
kwant
Commits
21291522
Commit
21291522
authored
13 years ago
by
Christoph Groth
Browse files
Options
Downloads
Patches
Plain Diff
sparse solver: rename variables for clarity
parent
07fad1ed
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
kwant/solvers/sparse.py
+23
-23
23 additions, 23 deletions
kwant/solvers/sparse.py
with
23 additions
and
23 deletions
kwant/solvers/sparse.py
+
23
−
23
View file @
21291522
...
@@ -68,7 +68,7 @@ def factorized(A, piv_tol=1.0, sym_piv_tol=1.0):
...
@@ -68,7 +68,7 @@ def factorized(A, piv_tol=1.0, sym_piv_tol=1.0):
return
solve
return
solve
LinearSys
=
namedtuple
(
'
LinearSys
'
,
[
'
h
_sy
s
'
,
'
rhs
'
,
'
ke
e
p_vars
'
])
LinearSys
=
namedtuple
(
'
LinearSys
'
,
[
'
l
hs
'
,
'
rhs
'
,
'
kep
t
_vars
'
])
def
make_linear_sys
(
sys
,
out_leads
,
in_leads
,
energy
=
0
,
force_realspace
=
False
):
def
make_linear_sys
(
sys
,
out_leads
,
in_leads
,
energy
=
0
,
force_realspace
=
False
):
...
@@ -93,10 +93,10 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -93,10 +93,10 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
Returns
Returns
-------
-------
(h
_sy
s, rhs, ke
e
p_vars) : LinearSys
(
l
hs, rhs, kep
t
_vars) : LinearSys
`h
_sy
s` is a numpy.sparse.csc_matrix, containing the left hand side
`
l
hs` is a numpy.sparse.csc_matrix, containing the left hand side
of the system of equations, `rhs` is a list of matrices with the
of the system of equations, `rhs` is a list of matrices with the
right hand side, `ke
e
p_vars` is a list of numbers of variables in the
right hand side, `kep
t
_vars` is a list of numbers of variables in the
solution that have to be stored (typically a small part of the
solution that have to be stored (typically a small part of the
complete solution).
complete solution).
lead_info : list of objects
lead_info : list of objects
...
@@ -114,12 +114,12 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -114,12 +114,12 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
"""
"""
if
not
sys
.
lead_neighbor_seqs
:
if
not
sys
.
lead_neighbor_seqs
:
raise
ValueError
(
'
System contains no leads.
'
)
raise
ValueError
(
'
System contains no leads.
'
)
h
_sy
s
,
norb
=
sys
.
hamiltonian_submatrix
(
sparse
=
True
)[:
2
]
l
hs
,
norb
=
sys
.
hamiltonian_submatrix
(
sparse
=
True
)[:
2
]
h_sys
=
h_sy
s
.
tocsc
()
lhs
=
lh
s
.
tocsc
()
h_sys
=
h_sy
s
-
energy
*
sp
.
identity
(
h
_sy
s
.
shape
[
0
],
format
=
'
csc
'
)
lhs
=
lh
s
-
energy
*
sp
.
identity
(
l
hs
.
shape
[
0
],
format
=
'
csc
'
)
# Hermiticity check.
# Hermiticity check.
if
np
.
any
(
np
.
abs
((
h_sys
-
h_sy
s
.
T
.
conj
()).
data
)
>
1e-13
):
if
np
.
any
(
np
.
abs
((
lhs
-
lh
s
.
T
.
conj
()).
data
)
>
1e-13
):
raise
ValueError
(
'
System Hamiltonian is not Hermitian.
'
)
raise
ValueError
(
'
System Hamiltonian is not Hermitian.
'
)
offsets
=
np
.
zeros
(
norb
.
shape
[
0
]
+
1
,
int
)
offsets
=
np
.
zeros
(
norb
.
shape
[
0
]
+
1
,
int
)
...
@@ -127,7 +127,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -127,7 +127,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
# Process the leads, generate the eigenvector matrices and lambda vectors.
# Process the leads, generate the eigenvector matrices and lambda vectors.
# Then create blocks of the linear system and add them step by step.
# Then create blocks of the linear system and add them step by step.
ke
e
p_vars
=
[]
kep
t
_vars
=
[]
rhs
=
[]
rhs
=
[]
lead_info
=
[]
lead_info
=
[]
for
leadnum
,
lead_neighbors
in
enumerate
(
sys
.
lead_neighbor_seqs
):
for
leadnum
,
lead_neighbors
in
enumerate
(
sys
.
lead_neighbor_seqs
):
...
@@ -149,7 +149,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -149,7 +149,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
u
,
ulinv
,
nprop
,
svd
=
modes
u
,
ulinv
,
nprop
,
svd
=
modes
if
leadnum
in
out_leads
:
if
leadnum
in
out_leads
:
ke
e
p_vars
.
append
(
range
(
h
_sy
s
.
shape
[
0
],
h
_sy
s
.
shape
[
0
]
+
nprop
))
kep
t
_vars
.
append
(
range
(
l
hs
.
shape
[
0
],
l
hs
.
shape
[
0
]
+
nprop
))
u_out
,
ulinv_out
=
u
[:,
nprop
:],
ulinv
[:,
nprop
:]
u_out
,
ulinv_out
=
u
[:,
nprop
:],
ulinv
[:,
nprop
:]
u_in
,
ulinv_in
=
u
[:,
:
nprop
],
ulinv
[:,
:
nprop
]
u_in
,
ulinv_in
=
u
[:,
:
nprop
],
ulinv
[:,
:
nprop
]
...
@@ -161,7 +161,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -161,7 +161,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
for
i
in
lead_neighbors
)]
for
i
in
lead_neighbors
)]
coords
=
np
.
r_
[[
np
.
arange
(
neighbors
.
size
)],
[
neighbors
]]
coords
=
np
.
r_
[[
np
.
arange
(
neighbors
.
size
)],
[
neighbors
]]
tmp
=
sp
.
csc_matrix
((
np
.
ones
(
neighbors
.
size
),
coords
),
tmp
=
sp
.
csc_matrix
((
np
.
ones
(
neighbors
.
size
),
coords
),
(
neighbors
.
size
,
h
_sy
s
.
shape
[
0
]))
(
neighbors
.
size
,
l
hs
.
shape
[
0
]))
if
svd
is
not
None
:
if
svd
is
not
None
:
v_sp
=
sp
.
csc_matrix
(
svd
[
2
].
T
.
conj
())
*
tmp
v_sp
=
sp
.
csc_matrix
(
svd
[
2
].
T
.
conj
())
*
tmp
...
@@ -173,7 +173,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -173,7 +173,7 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
vdaguout_sp
=
tmp
.
T
*
sp
.
csr_matrix
(
np
.
dot
(
v
.
T
.
conj
(),
u_out
))
vdaguout_sp
=
tmp
.
T
*
sp
.
csr_matrix
(
np
.
dot
(
v
.
T
.
conj
(),
u_out
))
lead_mat
=
-
ulinv_out
lead_mat
=
-
ulinv_out
h
_sy
s
=
sp
.
bmat
([[
h
_sy
s
,
vdaguout_sp
],
[
v_sp
,
lead_mat
]])
l
hs
=
sp
.
bmat
([[
l
hs
,
vdaguout_sp
],
[
v_sp
,
lead_mat
]])
if
nprop
>
0
and
leadnum
in
in_leads
:
if
nprop
>
0
and
leadnum
in
in_leads
:
if
svd
:
if
svd
:
...
@@ -194,19 +194,19 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
...
@@ -194,19 +194,19 @@ def make_linear_sys(sys, out_leads, in_leads, energy=0, force_realspace=False):
assert
sigma
.
shape
==
2
*
indices
.
shape
assert
sigma
.
shape
==
2
*
indices
.
shape
y
,
x
=
np
.
meshgrid
(
indices
,
indices
)
y
,
x
=
np
.
meshgrid
(
indices
,
indices
)
sig_sparse
=
sp
.
coo_matrix
((
sigma
.
flat
,
[
x
.
flat
,
y
.
flat
]),
sig_sparse
=
sp
.
coo_matrix
((
sigma
.
flat
,
[
x
.
flat
,
y
.
flat
]),
h
_sy
s
.
shape
)
l
hs
.
shape
)
h_sys
=
h_sy
s
+
sig_sparse
# __iadd__ is not implemented in v0.7
lhs
=
lh
s
+
sig_sparse
# __iadd__ is not implemented in v0.7
if
leadnum
in
out_leads
:
if
leadnum
in
out_leads
:
ke
e
p_vars
.
append
(
list
(
indices
))
kep
t
_vars
.
append
(
list
(
indices
))
if
leadnum
in
in_leads
:
if
leadnum
in
in_leads
:
l
=
indices
.
shape
[
0
]
l
=
indices
.
shape
[
0
]
rhs
.
append
(
sp
.
coo_matrix
((
-
np
.
ones
(
l
),
[
indices
,
rhs
.
append
(
sp
.
coo_matrix
((
-
np
.
ones
(
l
),
[
indices
,
np
.
arange
(
l
)])))
np
.
arange
(
l
)])))
return
LinearSys
(
h
_sy
s
,
rhs
,
ke
e
p_vars
),
lead_info
return
LinearSys
(
l
hs
,
rhs
,
kep
t
_vars
),
lead_info
def
solve_linear_sys
(
a
,
b
,
ke
e
p_vars
=
None
):
def
solve_linear_sys
(
a
,
b
,
kep
t
_vars
=
None
):
"""
"""
Solve matrix system of equations a x = b with sparse input.
Solve matrix system of equations a x = b with sparse input.
...
@@ -216,7 +216,7 @@ def solve_linear_sys(a, b, keep_vars=None):
...
@@ -216,7 +216,7 @@ def solve_linear_sys(a, b, keep_vars=None):
b : a list of matrices.
b : a list of matrices.
Sizes of these matrices may be smaller than needed, the missing
Sizes of these matrices may be smaller than needed, the missing
entries at the end are padded with zeros.
entries at the end are padded with zeros.
ke
e
p_vars : list of lists of integers
kep
t
_vars : list of lists of integers
a list of numbers of variables to keep in the solution
a list of numbers of variables to keep in the solution
Returns
Returns
...
@@ -230,10 +230,10 @@ def solve_linear_sys(a, b, keep_vars=None):
...
@@ -230,10 +230,10 @@ def solve_linear_sys(a, b, keep_vars=None):
"""
"""
a
=
sp
.
csc_matrix
(
a
)
a
=
sp
.
csc_matrix
(
a
)
if
ke
e
p_vars
==
None
:
if
kep
t
_vars
==
None
:
ke
e
p_vars
=
[
range
(
a
.
shape
[
1
])]
kep
t
_vars
=
[
range
(
a
.
shape
[
1
])]
slv
=
factorized
(
a
)
slv
=
factorized
(
a
)
keeptot
=
sum
(
ke
e
p_vars
,
[])
keeptot
=
sum
(
kep
t
_vars
,
[])
sols
=
[]
sols
=
[]
vec
=
np
.
empty
(
a
.
shape
[
0
],
complex
)
vec
=
np
.
empty
(
a
.
shape
[
0
],
complex
)
for
mat
in
b
:
for
mat
in
b
:
...
@@ -308,7 +308,7 @@ def solve(sys, energy=0, out_leads=None, in_leads=None, force_realspace=False):
...
@@ -308,7 +308,7 @@ def solve(sys, energy=0, out_leads=None, in_leads=None, force_realspace=False):
raise
ValueError
(
'
No output is requested.
'
)
raise
ValueError
(
'
No output is requested.
'
)
linsys
,
lead_info
=
make_linear_sys
(
sys
,
out_leads
,
in_leads
,
energy
,
linsys
,
lead_info
=
make_linear_sys
(
sys
,
out_leads
,
in_leads
,
energy
,
force_realspace
)
force_realspace
)
out_modes
=
[
len
(
i
)
for
i
in
linsys
.
ke
e
p_vars
]
out_modes
=
[
len
(
i
)
for
i
in
linsys
.
kep
t
_vars
]
in_modes
=
[
i
.
shape
[
1
]
for
i
in
linsys
.
rhs
]
in_modes
=
[
i
.
shape
[
1
]
for
i
in
linsys
.
rhs
]
result
=
BlockResult
(
solve_linear_sys
(
*
linsys
),
lead_info
)
result
=
BlockResult
(
solve_linear_sys
(
*
linsys
),
lead_info
)
result
.
in_leads
=
in_leads
result
.
in_leads
=
in_leads
...
@@ -407,7 +407,7 @@ def ldos(fsys, energy=0):
...
@@ -407,7 +407,7 @@ def ldos(fsys, energy=0):
# TODO: fix this
# TODO: fix this
msg
=
'
ldos only works when all leads are tight binding systems.
'
msg
=
'
ldos only works when all leads are tight binding systems.
'
raise
ValueError
(
msg
)
raise
ValueError
(
msg
)
(
h
,
rhs
,
ke
e
p_vars
),
lead_info
=
\
(
h
,
rhs
,
kep
t
_vars
),
lead_info
=
\
make_linear_sys
(
fsys
,
[],
xrange
(
len
(
fsys
.
leads
)),
energy
)
make_linear_sys
(
fsys
,
[],
xrange
(
len
(
fsys
.
leads
)),
energy
)
Modes
=
physics
.
Modes
Modes
=
physics
.
Modes
num_extra_vars
=
sum
(
li
.
vecs
.
shape
[
1
]
-
li
.
nmodes
num_extra_vars
=
sum
(
li
.
vecs
.
shape
[
1
]
-
li
.
nmodes
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment