@@ -4535,58 +4535,62 @@ class VaspParseError(ParseError):
4535
4535
4536
4536
4537
4537
def get_band_structure_from_vasp_multiple_branches (
4538
- dir_name : str ,
4538
+ dir_name : PathLike ,
4539
4539
efermi : float | None = None ,
4540
4540
projections : bool = False ,
4541
4541
) -> BandStructureSymmLine | BandStructure | None :
4542
4542
"""Get band structure info from a VASP directory.
4543
4543
4544
- It takes into account that a run can be divided in several branches named
4545
- "branch_x". If the run has not been divided in branches the method will
4546
- turn to parsing vasprun.xml directly.
4544
+ It takes into account that a run can be divided in several branches,
4545
+ each inside a directory named "branch_x". If the run has not been
4546
+ divided in branches the function will turn to parse vasprun.xml
4547
+ directly from the selected directory.
4547
4548
4548
4549
Args:
4549
- dir_name: Directory containing all bandstructure runs.
4550
- efermi: Efermi for bandstructure.
4551
- projections: True if you want to get the data on site projections if
4552
- any. Note that this is sometimes very large
4550
+ dir_name (PathLike): Parent directory containing all bandstructure runs.
4551
+ efermi (float): Fermi level for bandstructure.
4552
+ projections (bool) : True if you want to get the data on site
4553
+ projections if any. Note that this is sometimes very large
4553
4554
4554
4555
Returns:
4555
- A BandStructure Object.
4556
- None is there's a parsing error .
4556
+ A BandStructure/BandStructureSymmLine Object.
4557
+ None if no vasprun.xml found in given directory and branch directory .
4557
4558
"""
4558
- # TODO: Add better error handling!!!
4559
- if os .path .isfile (f"{ dir_name } /branch_0" ):
4560
- # Get all branch dir names
4559
+ if os .path .isdir (f"{ dir_name } /branch_0" ):
4560
+ # Get and sort all branch directories
4561
4561
branch_dir_names = [os .path .abspath (d ) for d in glob (f"{ dir_name } /branch_*" ) if os .path .isdir (d )]
4562
-
4563
- # Sort by the directory name (e.g, branch_10)
4564
4562
sorted_branch_dir_names = sorted (branch_dir_names , key = lambda x : int (x .split ("_" )[- 1 ]))
4565
4563
4566
- # Populate branches with Bandstructure instances
4567
- branches = []
4568
- for dname in sorted_branch_dir_names :
4569
- xml_file = f"{ dname } /vasprun.xml"
4570
- if os .path .isfile (xml_file ):
4571
- run = Vasprun (xml_file , parse_projected_eigen = projections )
4572
- branches .append (run .get_band_structure (efermi = efermi ))
4573
- else :
4574
- # TODO: It might be better to throw an exception
4575
- warnings .warn (
4576
- f"Skipping { dname } . Unable to find { xml_file } " ,
4577
- stacklevel = 2 ,
4578
- )
4564
+ # Collect BandStructure from all branches
4565
+ bs_branches : list [BandStructure | BandStructureSymmLine ] = []
4566
+ for directory in sorted_branch_dir_names :
4567
+ vasprun_file = f"{ directory } /vasprun.xml"
4568
+ if not os .path .isfile (vasprun_file ):
4569
+ raise FileNotFoundError (f"cannot find vasprun.xml in { directory = } " )
4579
4570
4580
- return get_reconstructed_band_structure (branches , efermi )
4571
+ run = Vasprun (vasprun_file , parse_projected_eigen = projections )
4572
+ bs_branches .append (run .get_band_structure (efermi = efermi ))
4581
4573
4582
- xml_file = f"{ dir_name } /vasprun.xml"
4583
- # Better handling of Errors
4584
- if os .path .isfile (xml_file ):
4585
- return Vasprun (xml_file , parse_projected_eigen = projections ).get_band_structure (
4574
+ return get_reconstructed_band_structure (bs_branches , efermi )
4575
+
4576
+ # Read vasprun.xml directly if no branch head (branch_0) is found
4577
+ # TODO: remove this branch and raise error directly after 2026-06-01
4578
+ vasprun_file = f"{ dir_name } /vasprun.xml"
4579
+ if os .path .isfile (vasprun_file ):
4580
+ warnings .warn (
4581
+ (
4582
+ f"no branch dir found, reading directly from { dir_name = } \n "
4583
+ "this fallback branch would be removed after 2026-06-01\n "
4584
+ "please check your data dir or use Vasprun.get_band_structure directly"
4585
+ ),
4586
+ DeprecationWarning ,
4587
+ stacklevel = 2 ,
4588
+ )
4589
+ return Vasprun (vasprun_file , parse_projected_eigen = projections ).get_band_structure (
4586
4590
kpoints_filename = None , efermi = efermi
4587
4591
)
4588
4592
4589
- return None
4593
+ raise FileNotFoundError ( f"failed to find any vasprun.xml in selected { dir_name = } " )
4590
4594
4591
4595
4592
4596
class Xdatcar :
0 commit comments