Skip to content

Commit

Permalink
Parsing of PES options as a dictionary for FFDirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ceriottm committed Nov 11, 2024
1 parent 59130b9 commit 90551a9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
3 changes: 2 additions & 1 deletion drivers/py/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from ipi.pes import *
from ipi.utils.io.inputs import read_args_kwargs

description = """
Minimal example of a Python driver connecting to i-PI and exchanging energy, forces, etc.
Expand Down Expand Up @@ -222,7 +223,7 @@ def run_driver(

args = parser.parse_args()

driver_args, driver_kwargs = parse_args_kwargs(args.param)
driver_args, driver_kwargs = read_args_kwargs(args.param)

if args.mode in __drivers__:
d_f = __drivers__[args.mode](
Expand Down
4 changes: 2 additions & 2 deletions ipi/engine/forcefields.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ def __init__(
super().__init__(latency, offset, name, pars, dopbc, active, threaded)

self.pes = pes
# TODO sanitize the handling of pars
self.driver = __drivers__[self.pes]("", verbosity.high)
print("PARS ", pars)
self.driver = __drivers__[self.pes](verbose=verbosity.high, **pars)

def evaluate(self, request):
request["result"] = list(self.driver(request["cell"][0], request["pos"]))
Expand Down
35 changes: 0 additions & 35 deletions ipi/pes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,4 @@
f"PES module `{module_name}` does not define __DRIVER_CLASS__ and __DRIVER_NAME__"
)


def _parse_value(s):
"""Attempt to parse a string to int or float; fallback to string."""
s = s.strip()
for cast in (int, float):
try:
return cast(s)
except ValueError:
continue
return s


def parse_args_kwargs(input_str):
"""
Parses a string into positional arguments and keyword arguments.
Args:
input_str (str): The input string containing comma-separated values and key-value pairs.
Returns:
tuple: A tuple containing a list of positional arguments and a dictionary of keyword arguments.
"""
args = []
kwargs = {}
tokens = input_str.split(",")
for token in tokens:
token = token.strip()
if "=" in token:
key, value = token.split("=", 1)
kwargs[key.strip()] = _parse_value(value)
elif len(token) > 0:
args.append(_parse_value(token))
return args, kwargs


__all__.append("__drivers__")
34 changes: 34 additions & 0 deletions ipi/utils/io/inputs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,37 @@


__all__ = ["io_xml"]


def read_value(s):
"""Attempt to parse a string to int or float; fallback to string."""
s = s.strip()
for cast in (int, float):
try:
return cast(s)
except ValueError:
continue
return s


def read_args_kwargs(input_str):
"""
Parses a string into positional arguments and keyword arguments.
Args:
input_str (str): The input string containing comma-separated values and key-value pairs.
Returns:
tuple: A tuple containing a list of positional arguments and a dictionary of keyword arguments.
"""
args = []
kwargs = {}
tokens = input_str.split(",")
for token in tokens:
token = token.strip()
if "=" in token:
key, value = token.split("=", 1)
kwargs[key.strip()] = _parse_value(value)
elif len(token) > 0:
args.append(_parse_value(token))
return args, kwargs
3 changes: 2 additions & 1 deletion ipi/utils/io/inputs/io_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import numpy as np
from ipi import ipi_global_settings
from . import read_value

__all__ = [
"xml_node",
Expand Down Expand Up @@ -448,7 +449,7 @@ def mystrip(data):
rtuple = list(map(mystrip, s.split(key_split)))
if not len(rtuple) == 2:
raise ValueError("Format for a key:value format is wrong for item " + s)
rdict[rtuple[0]] = rtuple[1]
rdict[rtuple[0]] = read_value(rtuple[1])

return rdict

Expand Down

0 comments on commit 90551a9

Please sign in to comment.