diff --git a/python/block_structure.py b/python/block_structure.py index f1501409..21f8e790 100644 --- a/python/block_structure.py +++ b/python/block_structure.py @@ -281,6 +281,46 @@ class BlockStructure(object): G = BlockGf(name_list = names, block_list = blocks) return G + def check_gf(self, G, ish=None): + """ check whether the Green's function G has the right structure + This throws an error if the structure of G is not the same + as ``gf_struct_solver``. + + Parameters + ---------- + G : BlockGf or list of BlockGf + Green's function to check + if it is a list, there should be as many entries as there + are shells, and the check is performed for all shells (unless + ish is given). + ish : int + shell index + default: 0 if G is just one Green's function is given, + check all if list of Green's functions is given + """ + + if isinstance(G, list): + assert len(G) == len(self.gf_struct_solver),\ + "list of G does not have the correct length" + if ish is None: + ishs = range(len(self.gf_struct_solver)) + else: + ishs = [ish] + for ish in ishs: + self.check_gf(G[ish], ish=ish) + return + + if ish is None: + ish = 0 + + for block in self.gf_struct_solver[ish]: + assert block in G.indices,\ + "block " + block + " not in G (shell {})".format(ish) + for block, gf in G: + assert block in self.gf_struct_solver[ish],\ + "block " + block + " not in struct (shell {})".format(ish) + assert list(gf.indices) == 2 * [map(str, self.gf_struct_solver[ish][block])],\ + "block " + block + " has wrong indices (shell {})".format(ish) def convert_gf(self,G,G_struct,ish=0,show_warnings=True,**kwargs): """ Convert BlockGf from its structure to this structure. diff --git a/test/blockstructure.py b/test/blockstructure.py index f7a68e5b..0123a141 100644 --- a/test/blockstructure.py +++ b/test/blockstructure.py @@ -4,7 +4,7 @@ from pytriqs.gf import * from pytriqs.utility.comparison_tests import assert_block_gfs_are_close from triqs_dft_tools.block_structure import BlockStructure -SK = SumkDFT('blockstructure.in.h5',use_dft_blocks=True) +SK = SumkDFT('blockstructure.in.h5', use_dft_blocks=True) original_bs = SK.block_structure @@ -13,56 +13,59 @@ pick1 = original_bs.copy() pick1.pick_gf_struct_solver([{'up_0': [1], 'up_1': [0], 'down_1': [0]}]) # check loading a block_structure from file -SK.block_structure = SK.load(['block_structure'],'mod')[0] +SK.block_structure = SK.load(['block_structure'], 'mod')[0] assert SK.block_structure == pick1, 'loading SK block structure from file failed' # check SumkDFT backward compatibility -sk_pick1 = BlockStructure(gf_struct_sumk = SK.gf_struct_sumk, - gf_struct_solver = SK.gf_struct_solver, - solver_to_sumk = SK.solver_to_sumk, - sumk_to_solver = SK.sumk_to_solver, - solver_to_sumk_block = SK.solver_to_sumk_block, - deg_shells = SK.deg_shells) +sk_pick1 = BlockStructure(gf_struct_sumk=SK.gf_struct_sumk, + gf_struct_solver=SK.gf_struct_solver, + solver_to_sumk=SK.solver_to_sumk, + sumk_to_solver=SK.sumk_to_solver, + solver_to_sumk_block=SK.solver_to_sumk_block, + deg_shells=SK.deg_shells) assert sk_pick1 == pick1, 'constructing block structure from SumkDFT properties failed' # check pick_gf_struct_sumk pick2 = original_bs.copy() -pick2.pick_gf_struct_sumk([{'up': [1, 2], 'down': [0,1]}]) +pick2.pick_gf_struct_sumk([{'up': [1, 2], 'down': [0, 1]}]) # check map_gf_struct_solver -mapping = [{ ('down_0', 0):('down', 0), - ('down_0', 1):('down', 2), - ('down_1', 0):('down', 1), - ('up_0', 0) :('down_1', 0), - ('up_0', 1) :('up_0', 0) }] +mapping = [{('down_0', 0): ('down', 0), + ('down_0', 1): ('down', 2), + ('down_1', 0): ('down', 1), + ('up_0', 0): ('down_1', 0), + ('up_0', 1): ('up_0', 0)}] map1 = original_bs.copy() map1.map_gf_struct_solver(mapping) # check create_gf -G1 = original_bs.create_gf(beta=40,n_points=3) +G1 = original_bs.create_gf(beta=40, n_points=3) i = 1 -for block,gf in G1: +for block, gf in G1: gf << SemiCircular(i) - i+=1 + i += 1 +original_bs.check_gf(G1) +original_bs.check_gf([G1]) # check approximate_as_diagonal offd = original_bs.copy() offd.approximate_as_diagonal() # check map_gf_struct_solver -G2 = map1.convert_gf(G1,original_bs,beta=40,n_points=3,show_warnings=False) +G2 = map1.convert_gf(G1, original_bs, beta=40, n_points=3, show_warnings=False) # check full_structure -full = BlockStructure.full_structure([{'up_0': [0, 1], 'up_1': [0], 'down_1': [0], 'down_0': [0, 1]}],None) +full = BlockStructure.full_structure( + [{'up_0': [0, 1], 'up_1': [0], 'down_1': [0], 'down_0': [0, 1]}], None) # check __eq__ -assert full==full, 'equality not correct (equal structures not equal)' -assert pick1==pick1, 'equality not correct (equal structures not equal)' -assert pick1!=pick2, 'equality not correct (different structures not different)' -assert original_bs!=offd, 'equality not correct (different structures not different)' +assert full == full, 'equality not correct (equal structures not equal)' +assert pick1 == pick1, 'equality not correct (equal structures not equal)' +assert pick1 != pick2, 'equality not correct (different structures not different)' +assert original_bs != offd, 'equality not correct (different structures not different)' if mpi.is_master_node(): - with HDFArchive('blockstructure.out.h5','w') as ar: + with HDFArchive('blockstructure.out.h5', 'w') as ar: ar['original_bs'] = original_bs ar['pick1'] = pick1 ar['pick2'] = pick2 @@ -75,10 +78,10 @@ if mpi.is_master_node(): # cannot use h5diff because BlockStructure testing is not implemented # there (and seems difficult to implement because it would mix triqs # and dft_tools) - with HDFArchive('blockstructure.out.h5','r') as ar,\ - HDFArchive('blockstructure.ref.h5','r') as ar2: - for k in ar2: - if isinstance(ar[k],BlockGf): - assert_block_gfs_are_close(ar[k],ar2[k],1.e-6) - else: - assert ar[k]==ar2[k], '{} not equal'.format(k) + with HDFArchive('blockstructure.out.h5', 'r') as ar,\ + HDFArchive('blockstructure.ref.h5', 'r') as ar2: + for k in ar2: + if isinstance(ar[k], BlockGf): + assert_block_gfs_are_close(ar[k], ar2[k], 1.e-6) + else: + assert ar[k] == ar2[k], '{} not equal'.format(k)