Skip to content

Commit

Permalink
Use resolvers in lammps/vasp/sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
pmrv committed Aug 15, 2024
1 parent 576cc2f commit 7965f3d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 49 deletions.
4 changes: 1 addition & 3 deletions pyiron_atomistics/atomistics/job/potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,12 @@ def _get_potential_default_df(
except ResourceNotFound:
raise ValueError("Was not able to locate the potential files.") from None


def find_potential_file_base(path, resource_path_lst, rel_path):
try:
return ResourceResolver(
resource_path_lst,
rel_path,
name=path
).first()
).first(path)
except ResourceNotFound:
raise ValueError(
"Either the filename or the functional has to be defined.",
Expand Down
29 changes: 14 additions & 15 deletions pyiron_atomistics/lammps/potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pyiron_base import GenericParameters, state

from pyiron_atomistics.atomistics.job.potentials import (
PotentialAbstract,
PotentialAbstract
)
from pyiron_atomistics.atomistics.structure.atoms import Atoms
from pyiron_snippets.resources import ResourceResolver, ResourceNotFound
Expand All @@ -26,7 +26,6 @@
__status__ = "production"
__date__ = "Sep 1, 2017"


class LammpsPotential(GenericParameters):
"""
This module helps write commands which help in the control of parameters related to the potential used in LAMMPS
Expand Down Expand Up @@ -66,6 +65,16 @@ def remove_structure_block(self):

@property
def files(self):
env = os.environ
resolver = ResourceResolver(
state.settings.resource_paths,
"lammps", "potentials",
).chain(
ResourceResolver(
[env[var] for var in ("CONDA_PREFIX", "CONDA_DIR") if var in env],
"share", "iprpy",
)
)
if len(self._df["Filename"].values[0]) > 0 and self._df["Filename"].values[
0
] != [""]:
Expand All @@ -78,20 +87,10 @@ def files(self):
if not os.path.isabs(files)
]
for path in relative_file_paths:
resolver = ResourceResolver(
state.settings.resource_paths,
"lammps", "potentials",
).chain(
# support iprpy-data package; data paths in the iprpy are of a different form than in
# pyiron resources, so we cannot add it as an additional path to the resolver above.
# Instead make a new resolver and chain it after the first one.
ResourceResolver(
[env[var] for var in ("CONDA_PREFIX", "CONDA_DIR") if var in env],
"share", "iprpy",
),
)
try:
absolute_file_paths.append(resolver.first(path))
absolute_file_paths.append(
resolver.first(path)
)
except ResourceNotFound:
raise ValueError("Was not able to locate the potentials.") from None
return absolute_file_paths
Expand Down
27 changes: 11 additions & 16 deletions pyiron_atomistics/sphinx/potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

import pandas
from pyiron_base import state

from pyiron_atomistics.vasp.potential import (
VaspPotentialAbstract,
find_potential_file_base,
)
from pyiron_atomistics.vasp.potential import VaspPotentialAbstract
from pyiron_snippets.resources import ResourceResolver, ResourceNotFound

__author__ = "Osamu Waseda"
__copyright__ = (
Expand Down Expand Up @@ -80,14 +77,12 @@ def add_new_element(self, parent_element, new_element):

def find_potential_file(path):
env = os.environ
resource_path_lst = state.settings.resource_paths
for conda_var in ["CONDA_PREFIX", "CONDA_DIR"]:
if conda_var in env.keys(): # support sphinx-data package
path_to_add = os.path.join(env[conda_var], "share", "sphinxdft")
if path_to_add not in resource_path_lst:
resource_path_lst += [path_to_add]
return find_potential_file_base(
path=path,
resource_path_lst=resource_path_lst,
rel_path=os.path.join("sphinx", "potentials"),
)
return ResourceResolver(
state.settings.resource_paths,
"sphinx", "potentials"
).chain(
ResourceResolver(
[env[var] for var in ("CONDA_PREFIX", "CONDA_DIR") if var in env],
"share", "sphinxdft",
)
).first(path)
24 changes: 9 additions & 15 deletions pyiron_atomistics/vasp/potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pandas
from pyiron_base import GenericParameters, state
from pyiron_snippets.deprecate import deprecate
from pyiron_snippets.resources import ResourceResolver, ResourceNotFound

from pyiron_atomistics.atomistics.job.potentials import (
PotentialAbstract,
Expand Down Expand Up @@ -120,24 +121,14 @@ def list_potential_names(self):
else:
return []

@staticmethod
def _return_potential_file(file_name):
for resource_path in state.settings.resource_paths:
resource_path_potcar = os.path.join(
resource_path, "vasp", "potentials", file_name
)
if os.path.exists(resource_path_potcar):
return resource_path_potcar
return None

def __dir__(self):
return [val.replace("-", "_") for val in self.list_potential_names()]

def __getitem__(self, item):
item_replace = item.replace("_gga_pbe", "-gga-pbe").replace("_lda", "-lda")
if item_replace in self.list_potential_names():
df = self.list()
return self._return_potential_file(
return return_potential_file(
file_name=list(df[df["Name"] == item_replace]["Filename"])[0][0]
)
selected_atoms = self._selected_atoms + [item]
Expand Down Expand Up @@ -267,10 +258,13 @@ def __repr__(self):


def find_potential_file(path):
return ResourceResolver(
state.settings.resource_paths,
"vasp", "potentials",
).first(path)
try:
return ResourceResolver(
state.settings.resource_paths,
"vasp", "potentials",
).first(path)
except ResourceNotFound:
return None


@deprecate(
Expand Down

0 comments on commit 7965f3d

Please sign in to comment.