Skip to content

Commit

Permalink
update unit test for skint types and struct.proj_atom_symbols (#47)
Browse files Browse the repository at this point in the history
* 🧪 test(sktypes): update test_skintTypes.py

 change the test case to N 2s 2p. and B 3s 3p orbitals case

* 🧪 test((structure)): update test_structure

add the test for get the atom type from struct.proj_atom_symbols and from the bond list. make sure the proj_atom_symbols follows the same order of atomic index in bond list.
  • Loading branch information
QG-phy authored Nov 5, 2023
1 parent 53aac37 commit 33ffb65
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 87 deletions.
3 changes: 3 additions & 0 deletions dptb/hamiltonian/hamil_eig_sk_crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def get_hs_hopping(self, bonds_hoppings = None):
direction_vec = bonds_hoppings[ib,8:11].float()
iatype = self.__struct__.proj_atom_symbols[int(ibond[1])]
jatype = self.__struct__.proj_atom_symbols[int(ibond[3])]
# Risk!, the index pf the order of atoms in proj_structure must be the same as the index in the proj_atom_symbols.
# actually, ibond[0] and ibond[2] are the atom index which can be used to get the atom symbol.
# the unit test must be set to keep this feature not be changed by other developers!

sub_hamil_block = th.zeros([self.__struct__.proj_atomtype_norbs[iatype], self.__struct__.proj_atomtype_norbs[jatype]], dtype=self.dtype, device=self.device)
if not self.use_orthogonal_basis:
Expand Down
7 changes: 4 additions & 3 deletions dptb/nnsktb/skintTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,13 @@ def all_skint_types(bond_index_map):
if iskint_type_ex in reducted_skint_types:
all_skint_types_dict[iskint_type] = iskint_type_ex
else:
assert iskint_type not in reducted_skint_types, f'{iskint_type} is already in reducted_skint_types.'
reducted_skint_types.append(iskint_type)
all_skint_types_dict[iskint_type] = iskint_type

elif iaid > jaid:
# note: here we set the order of reduced iaid > jaid.
# for iaid < jaid, we use the one with iaid > jaid and exchange iorb and jorb.
# for different atom type, exchange the pair (ia, iorb) with (ja, jorb), they are the same.
for isk in bond_index_map[ibm].keys():
iorb, jorb = isk.split('-')
Expand Down Expand Up @@ -101,8 +104,6 @@ def all_skint_types(bond_index_map):
iskint_type = f'{ia}-{ja}-{iorb}-{jorb}-{iisk}'
sk_bond_ind_dict[ibm][ bond_index_map[ibm][isk][iisk] ] = all_skint_types_dict[iskint_type]



return all_skint_types_dict, reducted_skint_types, sk_bond_ind_dict


Expand Down
209 changes: 126 additions & 83 deletions dptb/tests/test_skintTypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,55 @@
import torch
# add test for all_onsite_intgrl_types
def test_onsite_intgrl_types():
proj_atom_anglr_m = {'B':['2s'],'N':['2s','2p']}
proj_atom_anglr_m = {'B':['2s','2p'],'N':['3s','3p']}
indm = Index_Mapings(proj_atom_anglr_m)
onsite_strain_index_map, onsite_strain_num, onsite_index_map, onsite_num = \
indm.Onsite_Ind_Mapings(onsitemode='strain',atomtype=['N','B'])

all_onsite_int_types_dict, reducted_onsite_int_types, sk_onsite_ind_dict = all_onsite_intgrl_types(onsite_strain_index_map)

all_onsite_int_types_dict_check = {'N-N-2s-2s-0': 'N-N-2s-2s-0',
'N-N-2s-2p-0': 'N-N-2s-2p-0',
'N-N-2p-2s-0': 'N-N-2s-2p-0',
'N-N-2p-2p-0': 'N-N-2p-2p-0',
'N-N-2p-2p-1': 'N-N-2p-2p-1',
'N-B-2s-2s-0': 'N-B-2s-2s-0',
'N-B-2s-2p-0': 'N-B-2s-2p-0',
'N-B-2p-2s-0': 'N-B-2s-2p-0',
'N-B-2p-2p-0': 'N-B-2p-2p-0',
'N-B-2p-2p-1': 'N-B-2p-2p-1',
'B-N-2s-2s-0': 'B-N-2s-2s-0',
'B-B-2s-2s-0': 'B-B-2s-2s-0'}

reducted_onsite_int_types_check = ['N-N-2s-2s-0',
'N-N-2s-2p-0',
'N-N-2p-2p-0',
'N-N-2p-2p-1',
'N-B-2s-2s-0',
'N-B-2s-2p-0',
'N-B-2p-2p-0',
'N-B-2p-2p-1',
'B-N-2s-2s-0',
'B-B-2s-2s-0']

sk_onsite_ind_dict_check = {'N-N': ['N-N-2s-2s-0', 'N-N-2s-2p-0', 'N-N-2p-2p-0', 'N-N-2p-2p-1'],
'N-B': ['N-B-2s-2s-0', 'N-B-2s-2p-0', 'N-B-2p-2p-0', 'N-B-2p-2p-1'],
'B-N': ['B-N-2s-2s-0'],
'B-B': ['B-B-2s-2s-0']}
all_onsite_int_types_dict_check = {'N-N-3s-3s-0': 'N-N-3s-3s-0',
'N-N-3s-3p-0': 'N-N-3s-3p-0',
'N-N-3p-3s-0': 'N-N-3s-3p-0',
'N-N-3p-3p-0': 'N-N-3p-3p-0',
'N-N-3p-3p-1': 'N-N-3p-3p-1',
'N-B-3s-3s-0': 'N-B-3s-3s-0',
'N-B-3s-3p-0': 'N-B-3s-3p-0',
'N-B-3p-3s-0': 'N-B-3s-3p-0',
'N-B-3p-3p-0': 'N-B-3p-3p-0',
'N-B-3p-3p-1': 'N-B-3p-3p-1',
'B-N-2s-2s-0': 'B-N-2s-2s-0',
'B-N-2s-2p-0': 'B-N-2s-2p-0',
'B-N-2p-2s-0': 'B-N-2s-2p-0',
'B-N-2p-2p-0': 'B-N-2p-2p-0',
'B-N-2p-2p-1': 'B-N-2p-2p-1',
'B-B-2s-2s-0': 'B-B-2s-2s-0',
'B-B-2s-2p-0': 'B-B-2s-2p-0',
'B-B-2p-2s-0': 'B-B-2s-2p-0',
'B-B-2p-2p-0': 'B-B-2p-2p-0',
'B-B-2p-2p-1': 'B-B-2p-2p-1'}

reducted_onsite_int_types_check = ['N-N-3s-3s-0',
'N-N-3s-3p-0',
'N-N-3p-3p-0',
'N-N-3p-3p-1',
'N-B-3s-3s-0',
'N-B-3s-3p-0',
'N-B-3p-3p-0',
'N-B-3p-3p-1',
'B-N-2s-2s-0',
'B-N-2s-2p-0',
'B-N-2p-2p-0',
'B-N-2p-2p-1',
'B-B-2s-2s-0',
'B-B-2s-2p-0',
'B-B-2p-2p-0',
'B-B-2p-2p-1']

sk_onsite_ind_dict_check = {'N-N': ['N-N-3s-3s-0', 'N-N-3s-3p-0', 'N-N-3p-3p-0', 'N-N-3p-3p-1'],
'N-B': ['N-B-3s-3s-0', 'N-B-3s-3p-0', 'N-B-3p-3p-0', 'N-B-3p-3p-1'],
'B-N': ['B-N-2s-2s-0', 'B-N-2s-2p-0', 'B-N-2p-2p-0', 'B-N-2p-2p-1'],
'B-B': ['B-B-2s-2s-0', 'B-B-2s-2p-0', 'B-B-2p-2p-0', 'B-B-2p-2p-1']}

assert isinstance(all_onsite_int_types_dict, dict)
assert isinstance(reducted_onsite_int_types, list)
Expand Down Expand Up @@ -69,14 +83,17 @@ def test_onsite_intgrl_types():


def test_onsite_ene_types():
proj_atom_anglr_m = {'B':['2s'],'N':['2s','2p']}
proj_atom_anglr_m = {'B':['2s','2p'],'N':['3s','3p']}
indm = Index_Mapings(proj_atom_anglr_m)
onsite_strain_index_map, onsite_strain_num, onsite_index_map, onsite_num = \
indm.Onsite_Ind_Mapings(onsitemode='strain',atomtype=['N','B'])
all_onsiteE_types_dict, reduced_onsiteE_types, onsiteE_ind_dict = all_onsite_ene_types(onsite_index_map)
all_onsiteE_types_dict_check = {'N-2s-0': 'N-2s-0', 'N-2p-0': 'N-2p-0', 'B-2s-0': 'B-2s-0'}
reduced_onsiteE_types_check = ['N-2s-0', 'N-2p-0', 'B-2s-0']
onsiteE_ind_dict_check = {'N': ['N-2s-0', 'N-2p-0'], 'B': ['B-2s-0']}
all_onsiteE_types_dict_check = {'N-3s-0': 'N-3s-0',
'N-3p-0': 'N-3p-0',
'B-2s-0': 'B-2s-0',
'B-2p-0': 'B-2p-0'}
reduced_onsiteE_types_check = ['N-3s-0', 'N-3p-0', 'B-2s-0', 'B-2p-0']
onsiteE_ind_dict_check = {'N': ['N-3s-0', 'N-3p-0'], 'B': ['B-2s-0', 'B-2p-0']}

assert isinstance(all_onsiteE_types_dict, dict)
assert isinstance(reduced_onsiteE_types, list)
Expand All @@ -89,7 +106,7 @@ def test_onsite_ene_types():
def test_skintTypes():
envtype = ['N','B']
bondtype = ['N','B']
proj_atom_anglr_m={'N':['2s','2p'],'B':['2p']}
proj_atom_anglr_m={'N':['2s','2p'],'B':['3s','3p']}
indmap = Index_Mapings(proj_atom_anglr_m=proj_atom_anglr_m)
bond_map, bond_num = indmap.Bond_Ind_Mapings()
all_skint_types_dict, reducted_skint_types, sk_bond_ind_dict = all_skint_types(bond_map)
Expand All @@ -100,29 +117,40 @@ def test_skintTypes():
'N-N-2p-2s-0': 'N-N-2s-2p-0',
'N-N-2p-2p-0': 'N-N-2p-2p-0',
'N-N-2p-2p-1': 'N-N-2p-2p-1',
'N-B-2s-2p-0': 'N-B-2s-2p-0',
'N-B-2p-2p-0': 'N-B-2p-2p-0',
'N-B-2p-2p-1': 'N-B-2p-2p-1',
'B-N-2p-2s-0': 'N-B-2s-2p-0',
'B-N-2p-2p-0': 'N-B-2p-2p-0',
'B-N-2p-2p-1': 'N-B-2p-2p-1',
'B-B-2p-2p-0': 'B-B-2p-2p-0',
'B-B-2p-2p-1': 'B-B-2p-2p-1'}
'N-B-2s-3s-0': 'N-B-2s-3s-0',
'N-B-2s-3p-0': 'N-B-2s-3p-0',
'N-B-2p-3s-0': 'N-B-2p-3s-0',
'N-B-2p-3p-0': 'N-B-2p-3p-0',
'N-B-2p-3p-1': 'N-B-2p-3p-1',
'B-N-3s-2s-0': 'N-B-2s-3s-0',
'B-N-3s-2p-0': 'N-B-2p-3s-0',
'B-N-3p-2s-0': 'N-B-2s-3p-0',
'B-N-3p-2p-0': 'N-B-2p-3p-0',
'B-N-3p-2p-1': 'N-B-2p-3p-1',
'B-B-3s-3s-0': 'B-B-3s-3s-0',
'B-B-3s-3p-0': 'B-B-3s-3p-0',
'B-B-3p-3s-0': 'B-B-3s-3p-0',
'B-B-3p-3p-0': 'B-B-3p-3p-0',
'B-B-3p-3p-1': 'B-B-3p-3p-1'}

reducted_skint_types_check = ['N-N-2s-2s-0',
'N-N-2s-2p-0',
'N-N-2p-2p-0',
'N-N-2p-2p-1',
'N-B-2s-2p-0',
'N-B-2p-2p-0',
'N-B-2p-2p-1',
'B-B-2p-2p-0',
'B-B-2p-2p-1']
'N-B-2s-3s-0',
'N-B-2s-3p-0',
'N-B-2p-3s-0',
'N-B-2p-3p-0',
'N-B-2p-3p-1',
'B-B-3s-3s-0',
'B-B-3s-3p-0',
'B-B-3p-3p-0',
'B-B-3p-3p-1']

sk_bond_ind_check = {'N-N': ['N-N-2s-2s-0', 'N-N-2s-2p-0', 'N-N-2p-2p-0', 'N-N-2p-2p-1'],
'N-B': ['N-B-2s-2p-0', 'N-B-2p-2p-0', 'N-B-2p-2p-1'],
'B-N': ['N-B-2s-2p-0', 'N-B-2p-2p-0', 'N-B-2p-2p-1'],
'B-B': ['B-B-2p-2p-0', 'B-B-2p-2p-1']}
'N-B': ['N-B-2s-3s-0', 'N-B-2s-3p-0', 'N-B-2p-3s-0', 'N-B-2p-3p-0', 'N-B-2p-3p-1'],
'B-N': ['N-B-2s-3s-0', 'N-B-2s-3p-0', 'N-B-2p-3s-0', 'N-B-2p-3p-0', 'N-B-2p-3p-1'],
'B-B': ['B-B-3s-3s-0', 'B-B-3s-3p-0', 'B-B-3p-3p-0', 'B-B-3p-3p-1']}

assert isinstance(all_skint_types_dict, dict)
assert isinstance(reducted_skint_types, list)
Expand Down Expand Up @@ -151,53 +179,68 @@ def test_skintTypes():
def test_onsiteint_types():
envtype = ['N','B']
bondtype = ['N','B']
proj_atom_anglr_m = {'B':['2s'],'N':['2s','2p']}
proj_atom_anglr_m = {'B':['2s','2p'],'N':['3s','3p']}
indmap = Index_Mapings(proj_atom_anglr_m=proj_atom_anglr_m)
onsite_intgrl_index_map,_ = indmap._OnsiteStrain_Ind_Mapings(atomtypes=['N','B'])
all_onsiteint_types_dict, reducted_onsiteint_types, sk_onsite_ind_dict = all_onsite_intgrl_types(onsite_intgrl_index_map)

all_onsiteint_types_dict_check= {'N-N-2s-2s-0': 'N-N-2s-2s-0',
'N-N-2s-2p-0': 'N-N-2s-2p-0',
'N-N-2p-2s-0': 'N-N-2s-2p-0',
'N-N-2p-2p-0': 'N-N-2p-2p-0',
'N-N-2p-2p-1': 'N-N-2p-2p-1',
'N-B-2s-2s-0': 'N-B-2s-2s-0',
'N-B-2s-2p-0': 'N-B-2s-2p-0',
'N-B-2p-2s-0': 'N-B-2s-2p-0',
'N-B-2p-2p-0': 'N-B-2p-2p-0',
'N-B-2p-2p-1': 'N-B-2p-2p-1',
'B-N-2s-2s-0': 'B-N-2s-2s-0',
'B-B-2s-2s-0': 'B-B-2s-2s-0'}

reducted_onsiteint_types_check = ['N-N-2s-2s-0',
'N-N-2s-2p-0',
'N-N-2p-2p-0',
'N-N-2p-2p-1',
'N-B-2s-2s-0',
'N-B-2s-2p-0',
'N-B-2p-2p-0',
'N-B-2p-2p-1',
'B-N-2s-2s-0',
'B-B-2s-2s-0']

sk_onsite_ind_dict_check = {'N-N': ['N-N-2s-2s-0', 'N-N-2s-2p-0', 'N-N-2p-2p-0', 'N-N-2p-2p-1'],
'N-B': ['N-B-2s-2s-0', 'N-B-2s-2p-0', 'N-B-2p-2p-0', 'N-B-2p-2p-1'],
'B-N': ['B-N-2s-2s-0'],
'B-B': ['B-B-2s-2s-0']}
all_onsite_int_types_dict_check = {'N-N-3s-3s-0': 'N-N-3s-3s-0',
'N-N-3s-3p-0': 'N-N-3s-3p-0',
'N-N-3p-3s-0': 'N-N-3s-3p-0',
'N-N-3p-3p-0': 'N-N-3p-3p-0',
'N-N-3p-3p-1': 'N-N-3p-3p-1',
'N-B-3s-3s-0': 'N-B-3s-3s-0',
'N-B-3s-3p-0': 'N-B-3s-3p-0',
'N-B-3p-3s-0': 'N-B-3s-3p-0',
'N-B-3p-3p-0': 'N-B-3p-3p-0',
'N-B-3p-3p-1': 'N-B-3p-3p-1',
'B-N-2s-2s-0': 'B-N-2s-2s-0',
'B-N-2s-2p-0': 'B-N-2s-2p-0',
'B-N-2p-2s-0': 'B-N-2s-2p-0',
'B-N-2p-2p-0': 'B-N-2p-2p-0',
'B-N-2p-2p-1': 'B-N-2p-2p-1',
'B-B-2s-2s-0': 'B-B-2s-2s-0',
'B-B-2s-2p-0': 'B-B-2s-2p-0',
'B-B-2p-2s-0': 'B-B-2s-2p-0',
'B-B-2p-2p-0': 'B-B-2p-2p-0',
'B-B-2p-2p-1': 'B-B-2p-2p-1'}

reducted_onsite_int_types_check = ['N-N-3s-3s-0',
'N-N-3s-3p-0',
'N-N-3p-3p-0',
'N-N-3p-3p-1',
'N-B-3s-3s-0',
'N-B-3s-3p-0',
'N-B-3p-3p-0',
'N-B-3p-3p-1',
'B-N-2s-2s-0',
'B-N-2s-2p-0',
'B-N-2p-2p-0',
'B-N-2p-2p-1',
'B-B-2s-2s-0',
'B-B-2s-2p-0',
'B-B-2p-2p-0',
'B-B-2p-2p-1']

sk_onsite_ind_dict_check = {'N-N': ['N-N-3s-3s-0', 'N-N-3s-3p-0', 'N-N-3p-3p-0', 'N-N-3p-3p-1'],
'N-B': ['N-B-3s-3s-0', 'N-B-3s-3p-0', 'N-B-3p-3p-0', 'N-B-3p-3p-1'],
'B-N': ['B-N-2s-2s-0', 'B-N-2s-2p-0', 'B-N-2p-2p-0', 'B-N-2p-2p-1'],
'B-B': ['B-B-2s-2s-0', 'B-B-2s-2p-0', 'B-B-2p-2p-0', 'B-B-2p-2p-1']}


assert isinstance(all_onsiteint_types_dict, dict)
assert isinstance(reducted_onsiteint_types, list)
assert isinstance(sk_onsite_ind_dict, dict)

assert all_onsiteint_types_dict == all_onsiteint_types_dict_check
assert reducted_onsiteint_types == reducted_onsiteint_types_check
assert all_onsiteint_types_dict == all_onsite_int_types_dict_check
assert reducted_onsiteint_types == reducted_onsite_int_types_check
assert sk_onsite_ind_dict == sk_onsite_ind_dict_check


uniq_sktype = set(all_onsiteint_types_dict.values())
assert len(uniq_sktype) == len(reducted_onsiteint_types_check)
assert len(uniq_sktype) == len(reducted_onsite_int_types_check)
for ia in uniq_sktype:
assert ia in reducted_onsiteint_types_check
assert ia in reducted_onsite_int_types_check

assert list(sk_onsite_ind_dict.keys()) == (['N-N', 'N-B', 'B-N', 'B-B'])
assert onsite_intgrl_index_map.keys() == sk_onsite_ind_dict.keys()
Expand Down
10 changes: 9 additions & 1 deletion dptb/tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
from dptb.structure.structure import BaseStruct
from dptb.utils.tools import get_uniq_symbol
from dptb.utils.constants import anglrMId, atomic_num_dict
from dptb.utils.constants import anglrMId, atomic_num_dict, atomic_num_dict_r

@pytest.fixture(scope='session', autouse=True)
def root_directory(request):
Expand Down Expand Up @@ -100,6 +100,14 @@ def test_BaseStruct(root_directory):
assert struct.onsite_index_map == onsite_index_map
assert struct.onsite_num == onsite_num

atom_symb1_inbond = [atomic_num_dict_r[int(i)] for i in bonds[:,0]]
proj_atom_sym1_inbond =[struct.proj_atom_symbols[int(i)] for i in bonds[:,1]]

assert (atom_symb1_inbond == proj_atom_sym1_inbond)

atom_symb2_inbond = [atomic_num_dict_r[int(i)] for i in bonds[:,2]]
proj_atom_sym2_inbond =[struct.proj_atom_symbols[int(i)] for i in bonds[:,3]]
assert (atom_symb2_inbond == proj_atom_sym2_inbond)

struct.update_struct(atom=filename,format='vasp',onsitemode='split')
assert struct.onsite_index_map == {'N': {'s': [0], 'p': [1, 2, 3]}, 'B': {'s': [0], 'p': [1, 2, 3]}}
Expand Down

0 comments on commit 33ffb65

Please sign in to comment.