Skip to content

Commit

Permalink
add TBtrans Interface tbtrans_init.py and its test files (#50)
Browse files Browse the repository at this point in the history
* add tbtrans_init and related files for Shell command

* rename load_dptb_model as load_model

* add or modify some docstring

* add test files for tbtrans_init.py

* modify test_tbtrans_init.py

* add some comments

* change test_hBN_struct to orthogonal unit cell

* remove unnecessary sisl import

* add warning for input structure file in vasp format.

* add docstring to class

* fix some small problems

* rewrite orbitals_get function

* hamil_get to hamil_get_write

* add runtime error for .vasp and change R_vec assert

* add comments to sort([2,1,0])

* add TODO for double leads and  notes on Z-direction trans.

* move set_nsc to hamiltonian_get

* add notes on Hamil_sisl.set_nsc

* add import properties in __init__

* remove unnecessary print

* rewrite load_model in for

* fix load_model docstring problem

* delete load_model and merge its function to hamil_get_write

* delete load_model

* fix  bugs in chemical_symbol determination

* check _orbital_name_get
  • Loading branch information
AsymmetryChou authored Dec 11, 2023
1 parent 2569acd commit 287663d
Show file tree
Hide file tree
Showing 8 changed files with 1,070 additions and 1 deletion.
9 changes: 9 additions & 0 deletions dptb/entrypoints/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from dptb.postprocess.bandstructure.ifermi_api import ifermiapi, ifermi_installed, pymatgen_installed
from dptb.postprocess.write_skparam import WriteNNSKParam
from dptb.postprocess.NEGF import NEGF
from dptb.postprocess.tbtrans_init import TBTransInputSet,sisl_installed

__all__ = ["run"]

Expand Down Expand Up @@ -215,6 +216,14 @@ def run(
negf.compute()
log.info(msg='NEGF calculation successfully completed.')

if task == 'tbtrans_negf':
if not(sisl_installed):
log.error(msg="sisl is required to perform tbtrans calculation !")
raise RuntimeError

tbtrans_init = TBTransInputSet(apiHrk, run_opt, task_options)
tbtrans_init.hamil_get_write(write_nc=True)
log.info(msg='TBtrans input files are successfully generated.')

if output:
with open(os.path.join(output, "run_config.json"), "w") as fp:
Expand Down
664 changes: 664 additions & 0 deletions dptb/postprocess/tbtrans_init.py

Large diffs are not rendered by default.

Binary file added dptb/tests/data/test_negf/lead_R.nc
Binary file not shown.
122 changes: 122 additions & 0 deletions dptb/tests/data/test_tbtrans/best_nnsk_b3.600_c3.600_w0.300.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"onsite": {
"N-N-2s-2s-0": [
0.02462027035653591,
0.007205560803413391
],
"N-N-2s-2p-0": [
0.008309782482683659,
-0.007032226305454969
],
"N-N-2p-2p-0": [
0.012606431730091572,
0.010783562436699867
],
"N-N-2p-2p-1": [
0.0068643586710095406,
-0.011892829090356827
],
"N-B-2s-2s-0": [
0.041020166128873825,
-0.007834071293473244
],
"N-B-2s-2p-0": [
26.214815139770508,
-28.22139549255371
],
"N-B-2p-2p-0": [
0.2541739046573639,
0.3701082468032837
],
"N-B-2p-2p-1": [
-0.052932459861040115,
0.03325718641281128
],
"B-N-2s-2s-0": [
-0.10863762348890305,
-0.10621777176856995
],
"B-N-2s-2p-0": [
24.486974716186523,
26.85447883605957
],
"B-N-2p-2p-0": [
0.13345032930374146,
-0.3127709925174713
],
"B-N-2p-2p-1": [
-0.03844165802001953,
0.00011800970969488844
],
"B-B-2s-2s-0": [
-0.007172069512307644,
0.007495054975152016
],
"B-B-2s-2p-0": [
0.004442985635250807,
0.0030813836492598057
],
"B-B-2p-2p-0": [
-0.0013882589992135763,
-6.591381679754704e-05
],
"B-B-2p-2p-1": [
-0.009361814707517624,
-0.017272837460041046
]
},
"hopping": {
"N-N-2s-2s-0": [
0.05967297405004501,
-0.21457917988300323
],
"N-N-2s-2p-0": [
0.042178086936473846,
0.5767796635627747
],
"N-N-2p-2p-0": [
0.1008036881685257,
0.5027011632919312
],
"N-N-2p-2p-1": [
-0.005753065925091505,
-1.0040007829666138
],
"N-B-2s-2s-0": [
0.06605493277311325,
2.485130786895752
],
"N-B-2s-2p-0": [
-0.19711704552173615,
-1.884203314781189
],
"N-B-2p-2s-0": [
-0.06678403168916702,
-2.8326284885406494
],
"N-B-2p-2p-0": [
-0.2129121571779251,
3.601222276687622
],
"N-B-2p-2p-1": [
0.034046269953250885,
-4.79115104675293
],
"B-B-2s-2s-0": [
-0.061647042632102966,
-0.4071168601512909
],
"B-B-2s-2p-0": [
0.048091448843479156,
0.8385105729103088
],
"B-B-2p-2p-0": [
-0.11344866454601288,
0.5754562020301819
],
"B-B-2p-2p-1": [
0.007624409161508083,
-0.7790966033935547
]
}
}
90 changes: 90 additions & 0 deletions dptb/tests/data/test_tbtrans/negf_tbtrans.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"common_options": {
"onsitemode": "strain",
"onsite_cutoff": 1.6,
"bond_cutoff": 3.6,
"env_cutoff": 3.5,
"atomtype": [
"N",
"B"
],
"proj_atom_neles": {
"N": 5,
"B": 3
},
"proj_atom_anglr_m": {
"N": [
"2s",
"2p"
],
"B": [
"2s",
"2p"
]
}
},
"model_options": {
"sknetwork": {
"sk_hop_nhidden": 1,
"sk_onsite_nhidden": 1
},
"skfunction": {
"sk_cutoff": 3.6,
"sk_decay_w": 0.3
}
},
"structure":"./test_hBN_zigzag_struct.xyz",
"task_options":
{
"task": "tbtrans_negf",
"scf": true,
"block_tridiagonal": false,
"ele_T": 500,
"unit": "Hartree",
"scf_options":{
"mode": "PDIIS",
"mixing_period": 3,
"step_size": 0.05,
"n_history": 6,
"abs_err": 1e-6,
"rel_err": 1e-4,
"max_iter": 100
},
"stru_options":{
"pbc":[false, true, false],
"kmesh":[1,1,1],
"device":{
"id":"8-12",
"sort": true
},
"lead_L":{
"id":"0-8",
"voltage":0.0
},
"lead_R":{
"id":"12-20",
"voltage":0.0
}
},
"poisson_options": {
"solver": "fmm",
"err": 1e-5
},
"sgf_solver": "Sancho-Rubio",
"espacing": 0.1,
"emin": -2,
"emax": 2,
"e_fermi": -9.874357223510742,
"density_options":{
"method": "Ozaki"
},
"eta_lead":1e-5,
"eta_device":0.0,
"out_dos": true,
"out_tc": true,
"out_ldos": true,
"out_current_nscf": true,
"out_density": true,
"out_lcurrent": true
}
}
22 changes: 22 additions & 0 deletions dptb/tests/data/test_tbtrans/test_hBN_zigzag_struct.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
20
Lattice="30.0 0.0 0.0 0.0 4.337055133 0.0 0.0 0.0 12.519999742500001" Properties=species:S:1:pos:R:3 pbc="T T T"
N 15.00000000 3.97563387 0.62599999
B 15.00000000 2.52994883 0.62599999
N 15.00000000 1.80710631 1.87799996
B 15.00000000 0.36142126 1.87799996
N 15.00000000 3.97563387 3.12999994
B 15.00000000 2.52994883 3.12999994
N 15.00000000 1.80710631 4.38199991
B 15.00000000 0.36142126 4.38199991
N 15.00000000 3.97563387 5.63399988
B 15.00000000 2.52994883 5.63399988
N 15.00000000 1.80710631 6.88599986
B 15.00000000 0.36142126 6.88599986
N 15.00000000 3.97563387 8.13799983
B 15.00000000 2.52994883 8.13799983
N 15.00000000 1.80710631 9.38999981
B 15.00000000 0.36142126 9.38999981
N 15.00000000 3.97563387 10.64199978
B 15.00000000 2.52994883 10.64199978
N 15.00000000 1.80710631 11.89399976
B 15.00000000 0.36142126 11.89399976
106 changes: 106 additions & 0 deletions dptb/tests/test_tbtrans_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import pytest
from dptb.nnops.apihost import NNSKHost
from dptb.plugins.init_nnsk import InitSKModel
from dptb.nnops.NN2HRK import NN2HRK
from dptb.postprocess.tbtrans_init import TBTransInputSet
from dptb.utils.tools import j_loader
import numpy as np

@pytest.fixture(scope='session', autouse=True)
def root_directory(request):
return str(request.config.rootdir)


def test_tbtrans_init(root_directory):

# check whether sisl is installed: if not, skip this test
try:
import sisl
except:
pytest.skip('sisl is not installed which is necessary for TBtrans Input Generation. Therefore, skipping test_tbtrans_init.')

model_ckpt = f'{root_directory}/dptb/tests/data/test_tbtrans/best_nnsk_b3.600_c3.600_w0.300.json'
config = f'{root_directory}/dptb/tests/data/test_tbtrans/negf_tbtrans.json'
apihost = NNSKHost(checkpoint=model_ckpt, config=config)
apihost.register_plugin(InitSKModel())
apihost.build()
apiHrk = NN2HRK(apihost=apihost, mode='nnsk')

run_opt = {
"run_sk": True,
"init_model":model_ckpt,
"results_path":f'{root_directory}/dptb/tests/data/test_tbtrans/',
"structure":f'{root_directory}/dptb/tests/data/test_tbtrans/test_hBN_zigzag_struct.xyz',
"log_path": '/data/DeepTB/dptb_Zjj/DeePTB/dptb/tests/data/test_tbtrans/output',
"log_level": 5,
"use_correction":False
}

jdata = j_loader(config)
jdata = jdata['task_options']
tbtrans_hBN = TBTransInputSet(apiHrk=apiHrk,run_opt=run_opt, jdata=jdata)

# check _orbitals_name_get
element_orbital_name = tbtrans_hBN._orbitals_name_get(['2s', '2p'])
assert element_orbital_name == ['2s', '2py', '2pz', '2px']
element_orbital_name = tbtrans_hBN._orbitals_name_get(['3s', '3p', 'd*'])
assert element_orbital_name == ['3s', '3py', '3pz', '3px', 'dxy*', 'dyz*', 'dz2*', 'dxz*', 'dx2-y2*']



tbtrans_hBN.hamil_get_write(write_nc=False)
assert (tbtrans_hBN.allbonds_all[0].detach().numpy()-np.array([5, 0, 5, 0, 0, 0, 0])).max()<1e-5
assert (tbtrans_hBN.allbonds_all[50].detach().numpy()-np.array([ 5, 2, 5, 18, 0, 0, -1])).max()<1e-5
assert (tbtrans_hBN.hamil_block_all[0].detach().numpy()- np.array([[-0.73303634, 0. , 0. , 0. ],
[ 0. , 0.04233637, 0. , 0. ],
[ 0. , 0. , 0.04233636, 0. ],
[ 0. , 0. , 0. , -0.27156556]])).max()<1e-5
assert (tbtrans_hBN.hamil_block_all[50].detach().numpy()-np.array([[-0.03164609, -0. , 0.02028139, -0. ],
[ 0. , 0.00330366, 0. , 0. ],
[-0.02028139, 0. , -0.05393751, 0. ],
[ 0. , 0. , 0. , 0.00330366]])).max()<1e-5
assert (tbtrans_hBN.allbonds_lead_L[0].detach().numpy()-np.array([5, 0, 5, 0, 0, 0, 0])).max()<1e-5
assert (tbtrans_hBN.allbonds_lead_L[50].detach().numpy()-np.array([5, 4, 7, 7, 0, 0, 0])).max()<1e-5
assert (tbtrans_hBN.hamil_block_lead_L[0].detach().numpy()-np.array([[-0.73303634, 0. , 0. , 0. ],
[ 0. , 0.04233637, 0. , 0. ],
[ 0. , 0. , 0.04233636, 0. ],
[ 0. , 0. , 0. , -0.27156556]])).max()<1e-5
assert (tbtrans_hBN.hamil_block_lead_L[50].detach().numpy()-np.array([[ 0.1145315 , -0.06116847, 0.10594689, 0. ],
[ 0.15539739, -0.04634972, 0.22751862, 0. ],
[-0.26915616, 0.22751862, -0.30906558, 0. ],
[-0. , 0. , 0. , 0.08500822]])).max()<1e-5


# tbtrans_hBN.hamil_write()
# check the hamiltonian through Hk at Gamma and M
H_lead = tbtrans_hBN.H_lead_L
G_eigs = sisl.BandStructure(H_lead, [[0., 0.,0.]], 1, ["G"])
M_eigs = sisl.BandStructure(H_lead, [[0, 0.5 ,0 ]], 1, ["M"])
Ef = -9.874358177185059
G_eigs = G_eigs.apply.array.eigh() -Ef
M_eigs = M_eigs.apply.array.eigh() -Ef

G_eigs_right = np.array([[-19.95362763, -17.10774579, -17.10774579, -16.9118761 ,
-11.20609829, -10.01050689, -10.01050689, -8.11443067,
-8.11443067, -7.60442045, -6.6550603 , -3.79211664,
-3.79211663, -3.22863532, -3.22863532, -3.17535758,
3.18703263, 3.24031037, 3.24031037, 6.45306332,
7.70583557, 7.91107113, 10.91058699, 10.91058699,
23.64785516, 23.64785516, 28.30755414, 28.30755428,
28.65719263, 30.78452851, 33.25399887, 33.25399887]])

M_eigs_right = np.array([[-18.69653568, -18.69653568, -16.91187582, -16.91187582,
-11.20609828, -11.20609828, -7.44726991, -7.44726991,
-6.65506047, -6.65506047, -5.79252308, -5.79252308,
-5.2193769 , -5.2193769 , -3.17535758, -3.17535758,
3.18703263, 3.18703263, 5.84906816, 5.84906816,
7.74726616, 7.74726616, 7.91107121, 7.91107121,
28.12912079, 28.12912079, 28.65719227, 28.65719227,
29.54182975, 29.54182975, 30.78452877, 30.78452877]])

assert (G_eigs[0]-G_eigs_right[0]).max()<1e-5
assert (M_eigs[0]-M_eigs_right[0]).max()<1e-5




Loading

0 comments on commit 287663d

Please sign in to comment.