Skip to content

Commit 7c81a4e

Browse files
Merge pull request #25 from AsymmetryChou/btd_acc
Refactor and test vectorized BTD
2 parents 7f6e946 + 7727b83 commit 7c81a4e

8 files changed

Lines changed: 848 additions & 739 deletions

File tree

dpnegf/negf/lead_property.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def _estimate_worker_memory(lead_L, lead_R, kpoint=None, temp_allocation_factor=
487487
Estimated memory in bytes per worker.
488488
"""
489489
# Base overhead for Python process + libraries (interpreter, numpy, scipy, torch, etc.)
490-
base_overhead = 300 * 1024 * 1024 # 300 MB
490+
base_overhead = 100 * 1024 * 1024 # 100 MB
491491

492492
matrix_bytes = 0
493493

@@ -522,7 +522,7 @@ def _estimate_worker_memory(lead_L, lead_R, kpoint=None, temp_allocation_factor=
522522
return total_estimate
523523

524524

525-
def _get_safe_n_jobs(lead_L, lead_R, requested_n_jobs=-1, max_memory_fraction=0.7, min_workers=1, kpoint=None, n_cpus=None):
525+
def _get_safe_n_jobs(lead_L, lead_R, requested_n_jobs=-1, max_memory_fraction=0.9, min_workers=1, kpoint=None, n_cpus=None):
526526
"""
527527
Calculate safe number of parallel workers based on available system memory.
528528
@@ -533,7 +533,7 @@ def _get_safe_n_jobs(lead_L, lead_R, requested_n_jobs=-1, max_memory_fraction=0.
533533
requested_n_jobs : int
534534
User-requested n_jobs. -1 means auto-detect.
535535
max_memory_fraction : float
536-
Maximum fraction of available memory to use. Default 0.7.
536+
Maximum fraction of available memory to use. Default 0.9.
537537
min_workers : int
538538
Minimum number of workers to use. Default 1.
539539
kpoint : array-like, optional

dpnegf/negf/negf_hamiltonian_init.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ def __init__(self,
141141
atom_norbs.append(int(self.model.idp.atom_norb[model.idp.chemical_symbol_to_type[atom.symbol]]))
142142
self.atom_norbs = atom_norbs
143143

144-
def initialize(self, kpoints, block_tridiagnal=False,useBloch=False,bloch_factor=None,\
144+
def initialize(self, kpoints, block_tridiagnal=False, plot_blocks=False, \
145+
useBloch=False,bloch_factor=None,\
145146
use_saved_HS=False, saved_HS_path=None):
146147
'''This function initializes the structure and Hamiltonian for a system with optional block tridiagonal
147148
and Bloch factor parameters.
@@ -152,6 +153,8 @@ def initialize(self, kpoints, block_tridiagnal=False,useBloch=False,bloch_factor
152153
Kpoints in the Brillouin zone
153154
block_tridiagnal, optional
154155
a boolean flag that determines whether the Hamiltonian matrix should be stored in a block-tridiagonal form.
156+
plot_blocks, optional
157+
a boolean flag that determines whether to visualize the block structure using `show_blocks`.
155158
useBloch, optional
156159
a boolean flag that determines whether Bloch boundary conditions should be used in the lead self energy calculations.
157160
bloch_factor
@@ -199,7 +202,7 @@ def initialize(self, kpoints, block_tridiagnal=False,useBloch=False,bloch_factor
199202
log.info(msg="=="*40)
200203
else:
201204
self.saved_HS_path = self.results_path
202-
self.Hamiltonian_initialized(kpoints,useBloch,bloch_factor,block_tridiagnal,\
205+
self.Hamiltonian_initialized(kpoints,useBloch,bloch_factor,block_tridiagnal,plot_blocks,\
203206
lead_atom_range,structure_leads,structure_leads_fold)
204207
log.info(msg="--"*40)
205208
log.info(msg=f"The Hamiltonian has been initialized by model.")
@@ -211,7 +214,8 @@ def initialize(self, kpoints, block_tridiagnal=False,useBloch=False,bloch_factor
211214

212215

213216
def Hamiltonian_initialized(self,kpoints:List[List[float]],useBloch:bool,bloch_factor:List[int],\
214-
block_tridiagnal:bool,lead_atom_range:dict,structure_leads:Atoms,structure_leads_fold:Atoms):
217+
block_tridiagnal:bool, plot_blocks:bool,\
218+
lead_atom_range:dict,structure_leads:Atoms,structure_leads_fold:Atoms):
215219
'''This function initializes the Hamiltonian for a device with leads, handling various calculations
216220
and checks along the way.
217221
@@ -236,6 +240,8 @@ def Hamiltonian_initialized(self,kpoints:List[List[float]],useBloch:bool,bloch_f
236240
should be stored in a block-tridiagonal form. If `block_tridiagnal` is set to `True`, the Hamiltonian
237241
matrix will be stored in a block-tridiagonal form. Otherwise, the Hamiltonian matrix will be stored in
238242
a full matrix format.
243+
plot_blocks : bool
244+
The `plot_blocks` parameter is a boolean flag that determines whether to visualize the block structure
239245
lead_atom_range : dict
240246
The `lead_atom_range` parameter indicates the range of leads. The key of the dictionary is the
241247
lead name, and the value is a list containing the start and end indices of the lead atoms.
@@ -432,7 +438,8 @@ def Hamiltonian_initialized(self,kpoints:List[List[float]],useBloch:bool,bloch_f
432438
leftmost_size = coupling_width['lead_L']
433439
rightmost_size = coupling_width['lead_R']
434440
hd, hu, hl, sd, su, sl, subblocks = self.get_block_tridiagonal(HD*self.h_factor,SD.cdouble(),self.structase,\
435-
leftmost_size,rightmost_size)
441+
leftmost_size,rightmost_size,
442+
plot_blocks=plot_blocks)
436443
HS_device.update({"hd":hd, "hu":hu, "hl":hl, "sd":sd, "su":su, "sl":sl, \
437444
"subblocks":subblocks, "block_tridiagonal":True})
438445

@@ -637,7 +644,7 @@ def get_lead_structure(self,kk,natom,useBloch=False,bloch_factor=None):
637644

638645
return stru_lead, stru_lead_fold, bloch_sorted_indice, bloch_R_list
639646

640-
def get_block_tridiagonal(self,HK,SK,structase:ase.Atoms,leftmost_size:int,rightmost_size:int):
647+
def get_block_tridiagonal(self,HK,SK,structase:ase.Atoms,leftmost_size:int,rightmost_size:int,plot_blocks=False):
641648
"""
642649
Block-tridiagonalizes the Hamiltonian (HK) and overlap (SK) matrices for a given atomic structure.
643650
This method splits the input matrices into block tridiagonal form based on the atomic structure along the z-axis.
@@ -671,6 +678,8 @@ def get_block_tridiagonal(self,HK,SK,structase:ase.Atoms,leftmost_size:int,right
671678
List of lower-diagonal blocks of SK for each k-point.
672679
subblocks : list
673680
List of subblock sizes (number of orbitals in each block).
681+
plot_blocks : bool
682+
Whether to visualize the block structure using `show_blocks`.
674683
Notes
675684
-----
676685
- Uses optimized or fallback block splitting depending on the structure.
@@ -726,7 +735,9 @@ def get_block_tridiagonal(self,HK,SK,structase:ase.Atoms,leftmost_size:int,right
726735
log.info(msg=" occupation of subblocks: {} %".format(num_total/(HK[0].shape[0]**2)*100))
727736

728737
subblocks = subblocks[1:]
729-
show_blocks(subblocks,HK[0],self.results_path)
738+
739+
if plot_blocks:
740+
show_blocks(subblocks,HK[0],self.results_path)
730741

731742
return hd, hu, hl, sd, su, sl, subblocks
732743

0 commit comments

Comments
 (0)