Skip to content

Commit

Permalink
use cfg in io module
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Nov 28, 2024
1 parent 47af3bf commit e7c49b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sparc/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ def __init__(
if label is None:
label = "SPARC" if restart is None else None

# Use psp dir from user input or env
self.sparc_bundle = SparcBundle(
directory=Path(self.directory),
mode="w",
atoms=self.atoms,
label=label, # The order is tricky here. Use label not self.label
psp_dir=psp_dir,
validator=self.validator,
cfg=self.cfg,
)

# Try restarting from an old calculation and set results
Expand Down
14 changes: 11 additions & 3 deletions sparc/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy as np
from ase.atoms import Atoms
from ase.calculators.singlepoint import SinglePointDFTCalculator
from ase.config import cfg as _cfg

# various io formatters
from .api import SparcAPI
Expand All @@ -21,10 +22,10 @@
from .sparc_parsers.out import _read_out
from .sparc_parsers.pseudopotential import copy_psp_file, parse_psp8_header
from .sparc_parsers.static import _add_cell_info, _read_static
from .utils import deprecated, locate_api, string2index
from .utils import deprecated, locate_api, sanitize_path, string2index

# from .sparc_parsers.ion import read_ion, write_ion
defaultAPI = locate_api()
defaultAPI = locate_api(cfg=_cfg)


class SparcBundle:
Expand Down Expand Up @@ -85,6 +86,7 @@ def __init__(
label=None,
psp_dir=None,
validator=defaultAPI,
cfg=_cfg,
):
"""
Initializes a SparcBundle for accessing SPARC calculation data.
Expand Down Expand Up @@ -114,6 +116,7 @@ def __init__(
self.init_inputs = {}
self.psp_data = {}
self.raw_results = {}
self.cfg = cfg
self.psp_dir = self.__find_psp_dir(psp_dir)
# Sorting should be consistent across the whole bundle!
self.sorting = None
Expand Down Expand Up @@ -182,9 +185,14 @@ def __find_psp_dir(self, psp_dir=None):
return Path(psp_dir)
else:
for var in self.psp_env:
env_psp_dir = os.environ.get(var, None)
env_psp_dir = self.cfg.get(var, None)
if env_psp_dir:
return Path(env_psp_dir)
# Use pp_path field in cfg
parser = self.cfg.parser["sparc"] if "sparc" in self.cfg.parser else {}
psp_dir_ini = parser.get("pp_path", None)
if psp_dir_ini:
return sanitize_path(psp_dir_ini)
# At this point, we try to use the psp files bundled with sparc
if is_psp_download_complete(default_psp_dir):
return default_psp_dir
Expand Down

0 comments on commit e7c49b9

Please sign in to comment.