Skip to content

Commit

Permalink
Modify block_structure for new gf_struct convention.
Browse files Browse the repository at this point in the history
  • Loading branch information
hschnait committed Mar 8, 2022
1 parent a833881 commit 0a95ffd
Showing 1 changed file with 37 additions and 34 deletions.
71 changes: 37 additions & 34 deletions python/triqs_dft_tools/block_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class BlockStructure(object):
Parameters
----------
gf_struct_sumk : list of list of tuple
gf_struct_sumk[ish][idx] = (block_name,list of indices in block)
gf_struct_sumk[ish][idx] = (block_name, dimension of block)
for correlated shell ish; idx is just a counter in the list
gf_struct_solver : list of dict
gf_struct_solver[ish][block] = list of indices in that block
gf_struct_solver[ish][block] = dimension of that block
for *inequivalent* correlated shell ish
solver_to_sumk : list of dict
Expand Down Expand Up @@ -197,7 +197,7 @@ def gf_struct_sumk_dict(self):
"""
if self.gf_struct_sumk is None:
return None
return [{block: indices for block, indices in gfs}
return [{block: block_dim for block, block_dim in gfs}
for gfs in self.gf_struct_sumk]

@property
Expand Down Expand Up @@ -249,7 +249,7 @@ def effective_transformation_sumk(self):
raise Exception('gf_struct_solver not set.')

if trans is None:
trans = [{block: np.eye(len(indices)) for block, indices in gfs}
trans = [{block: np.eye(block_dim) for block, block_dim in gfs}
for gfs in self.gf_struct_sumk]

assert isinstance(trans, list),\
Expand All @@ -261,12 +261,12 @@ def effective_transformation_sumk(self):
for icrsh in list(range(len(trans))):
ish = self.corr_to_inequiv[icrsh]
if trans[icrsh] is None:
trans[icrsh] = {block: np.eye(len(indices))
for block, indices in self.gf_struct_sumk[icrsh]}
trans[icrsh] = {block: np.eye(block_dim)
for block, block_dim in self.gf_struct_sumk[icrsh]}

if not isinstance(trans[icrsh], dict):
trans[icrsh] = {block: copy.deepcopy(trans[icrsh])
for block, indices in self.gf_struct_sumk[icrsh]}
for block, block_dim in self.gf_struct_sumk[icrsh]}

assert list(trans[icrsh].keys()) == list(self.gf_struct_sumk_dict[icrsh].keys()),\
"wrong block names used in transformation (icrsh = {})".format(icrsh)
Expand All @@ -275,14 +275,18 @@ def effective_transformation_sumk(self):
assert trans[icrsh][block].shape[0] == trans[icrsh][block].shape[1],\
"Transformation has to be quadratic; throwing away orbitals can be achieved on the level of the mapping. (icrsh = {}, block = {})".format(icrsh, block)

assert trans[icrsh][block].shape[0] == len(self.gf_struct_sumk_dict[icrsh][block]),\
assert trans[icrsh][block].shape[0] == self.gf_struct_sumk_dict[icrsh][block],\
"Transformation block shape does not match gf_struct_sumk. (icrsh = {}, block = {})".format(icrsh, block)

# zero out all the lines of the transformation that are
# not included in gf_struct_solver
for iorb, norb in enumerate(self.gf_struct_sumk_dict[icrsh][block]):
if self.sumk_to_solver[ish][(block, norb)][0] is None:
#TODO CHECK THIS!
for iorb in range(self.gf_struct_sumk_dict[icrsh][block]):
if self.sumk_to_solver[ish][(block, iorb)][0] is None:
trans[icrsh][block][iorb, :] = 0.0
# for iorb, norb in enumerate(self.gf_struct_sumk_dict[icrsh][block]):
# if self.sumk_to_solver[ish][(block, norb)][0] is None:
# trans[icrsh][block][iorb, :] = 0.0
return trans

@property
Expand Down Expand Up @@ -321,10 +325,10 @@ def effective_transformation_solver(self):
for block in self.gf_struct_solver[ish]:
block_sumk = self.solver_to_sumk_block[ish][block]
T = eff_trans_sumk[icrsh][block_sumk]
ets[ish][block] = np.zeros((len(self.gf_struct_solver[ish][block]),
ets[ish][block] = np.zeros((self.gf_struct_solver[ish][block],
len(T)),
dtype=T.dtype)
for i in self.gf_struct_solver[ish][block]:
for i in range(self.gf_struct_solver[ish][block]):
i_sumk = self.solver_to_sumk[ish][block, i]
assert i_sumk[0] == block_sumk,\
"Wrong block in solver_to_sumk"
Expand All @@ -343,7 +347,7 @@ def full_structure(cls,gf_struct,corr_to_inequiv):
Parameters
----------
gf_struct : list of dict
gf_struct[ish][block] = list of indices in that block
gf_struct[ish][block] = block dimension of that block
for (inequivalent) correlated shell ish
corr_to_inequiv : list
Expand All @@ -365,7 +369,7 @@ def full_structure(cls,gf_struct,corr_to_inequiv):
gss = []
for block in gf_struct[ish]:
so2sublock[block]=block
for ind in gf_struct[ish][block]:
for ind in range(gf_struct[ish][block]):
so2su[(block,ind)]=(block,ind)
gss.append((block,gf_struct[ish][block]))
solver_to_sumk.append(so2su)
Expand Down Expand Up @@ -409,16 +413,14 @@ def pick_gf_struct_solver(self, new_gf_struct):
[{'up':[0],'down':[1]}]
Note that the indices will be renamed to be a 0-based
sequence of integers, i.e. the new structure will actually
be [{'up':[0],'down':[0]}].
Note that as of version 3.1.x, the new structure itself will
actually be stored as [{'up':1,'down':1}].
For dropped indices, sumk_to_solver will map to (None,None).
Parameters
----------
new_gf_struct : list of dict
formatted the same as gf_struct_solver:
new_gf_struct[ish][block]=list of indices in that block.
"""
Expand Down Expand Up @@ -454,7 +456,8 @@ def pick_gf_struct_solver(self, new_gf_struct):

# reindexing gf_struct so that it starts with 0
for k in gf_struct:
gf_struct[k]=list(range(len(gf_struct[k])))
# gf_struct[k]=list(range(len(gf_struct[k])))
gf_struct[k]=len(gf_struct[k])
self.gf_struct_solver[ish]=gf_struct

def adapt_deg_shells(self, gf_struct, ish=0):
Expand All @@ -468,7 +471,7 @@ def adapt_deg_shells(self, gf_struct, ish=0):
if not key in gf_struct:
del degsh[key]
continue
if gf_struct[key] != self.gf_struct_solver[ish][key]:
if len(gf_struct[key]) != self.gf_struct_solver[ish][key]:
v, C = degsh[key]
degsh[key][0] = \
v[gf_struct[key], :][:, gf_struct[key]]
Expand Down Expand Up @@ -509,7 +512,6 @@ def pick_gf_struct_sumk(self, new_gf_struct):
Parameters
----------
new_gf_struct : list of dict
formatted the same as gf_struct_solver:
new_gf_struct[ish][block]=list of indices in that block.
Expand All @@ -529,16 +531,16 @@ def pick_gf_struct_sumk(self, new_gf_struct):
# one non-zero entry

for icrsh in range(len(new_gf_struct)):
for block, indices in self.gf_struct_sumk[icrsh]:
for block, block_dim in self.gf_struct_sumk[icrsh]:
if not block in new_gf_struct[icrsh]:
#del new_gf_struct_transformed[block] # this MUST be wrong, as new_gf_struct_transformed needs to have a integer index for icrsh... # error when index is not kept at all
continue
T = eff_trans_sumk[icrsh][block]
for index in indices:
for index in range(block_dim):
if not index in new_gf_struct[icrsh][block]:
T[:, index] = 0.0
new_indices = []
for index in indices:
for index in range(block_dim):
if np.any(np.abs(T[index, :]) > 1.e-15):
new_indices.append(index)
new_gf_struct_transformed[icrsh][block] = new_indices
Expand All @@ -558,6 +560,7 @@ def pick_gf_struct_sumk(self, new_gf_struct):
gfs[icrsh][ind_sol[0]].append(ind_sol[1])
self.pick_gf_struct_solver(gfs)

# TODO!
def map_gf_struct_solver(self, mapping):
r""" Map the Green function structure from one struct to another.
Expand Down Expand Up @@ -660,8 +663,8 @@ def create_matrix(self, ish=0, space='solver', dtype=np.complex_):
which space the structure should correspond to
"""

def gf_function(indices):
return np.zeros((len(indices), len(indices)), dtype=dtype)
def gf_function(target_shape):
return np.zeros(target_shape, dtype=dtype)

def block_function(name_list, block_list):
d = dict()
Expand All @@ -683,7 +686,7 @@ def _create_gf_or_matrix(self, ish=0, gf_function=GfImFreq, block_function=Block
names = list(gf_struct[ish].keys())
blocks = []
for n in names:
G = gf_function(indices=gf_struct[ish][n], **kwargs)
G = gf_function(target_shape=(gf_struct[ish][n],gf_struct[ish][n]), **kwargs)
blocks.append(G)
G = block_function(name_list=names, block_list=blocks)
return G
Expand Down Expand Up @@ -766,7 +769,7 @@ def _check_gf_or_matrix(self, G, ish=None, space='solver'):
for block, gf in G:
assert block in gf_struct[ish],\
"block " + block + " not in struct (shell {})".format(ish)
assert list(gf.indices) == 2 * [list(map(str, gf_struct[ish][block]))],\
assert (gf.target_shape) == (gf_struct[ish][block], gf_struct[ish][block]),\
"block " + block + \
" has wrong indices (shell {})".format(ish)
else:
Expand All @@ -776,7 +779,7 @@ def _check_gf_or_matrix(self, G, ish=None, space='solver'):
for block, gf in list(G.items()):
assert block in gf_struct[ish],\
"block " + block + " not in struct (shell {})".format(ish)
assert list(range(len(gf))) == 2 * [list(map(str, gf_struct[ish][block]))],\
assert len(gf) == gf_struct[ish][block],\
"block " + block + \
" has wrong indices (shell {})".format(ish)

Expand Down Expand Up @@ -936,8 +939,8 @@ def _convert_gf_or_matrix(self, G, G_struct=None, ish_from=0, ish_to=None, show_
block_mapping_from = G_struct.sumk_to_solver_block[ish_from]
elif space_from == 'sumk':
gf_struct_from = G_struct.gf_struct_sumk_dict[ish_from]
eff_trans_from = {block: np.eye(len(indices))
for block, indices in G_struct.gf_struct_sumk[ish_from]}
eff_trans_from = {block: np.eye(block_dim)
for block, block_dim in G_struct.gf_struct_sumk[ish_from]}
block_mapping_from = {b: [b] for b in gf_struct_from}
else:
raise Exception(
Expand All @@ -949,8 +952,8 @@ def _convert_gf_or_matrix(self, G, G_struct=None, ish_from=0, ish_to=None, show_
block_mapping_to = self.solver_to_sumk_block[ish_to]
elif space_to == 'sumk':
gf_struct_to = self.gf_struct_sumk_dict[ish_to]
eff_trans_to = {block: np.eye(len(indices))
for block, indices in self.gf_struct_sumk_list[ish_to]}
eff_trans_to = {block: np.eye(block_dim)
for block, block_dim in self.gf_struct_sumk_list[ish_to]}
block_mapping_to = {b: b for b in gf_struct_to}
else:
raise Exception(
Expand Down Expand Up @@ -1046,7 +1049,7 @@ def approximate_as_diagonal(self):
self.solver_to_sumk_block.append({})
for frm,to in list(self.sumk_to_solver[ish].items()):
if to[0] is not None:
self.gf_struct_solver[ish][frm[0]+'_'+str(frm[1])]=[0]
self.gf_struct_solver[ish][frm[0]+'_'+str(frm[1])]=1
self.sumk_to_solver[ish][frm]=(frm[0]+'_'+str(frm[1]),0)
self.solver_to_sumk[ish][(frm[0]+'_'+str(frm[1]),0)]=frm
self.solver_to_sumk_block[ish][frm[0]+'_'+str(frm[1])]=frm[0]
Expand Down

0 comments on commit 0a95ffd

Please sign in to comment.