Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Bug fixes

- Parameters can now be imported from any given path in `Windows` ([#1900](https://github.com/pybamm-team/PyBaMM/pull/1900))
- Fixed initial conditions for the EC SEI model ([#1895](https://github.com/pybamm-team/PyBaMM/pull/1895))

# [v21.12](https://github.com/pybamm-team/PyBaMM/tree/v21.11) - 2021-12-29
Expand Down
5 changes: 5 additions & 0 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,13 @@ def find_parameter(path):
"""Look for parameter file in the different locations
in PARAMETER_PATH
"""
# Check for absolute path
if os.path.isfile(path) and os.path.isabs(path):
pybamm.logger.verbose(f"Using absolute path: '{path}'")
return path
for location in pybamm.PARAMETER_PATH:
trial_path = os.path.join(location, path)
if os.path.isfile(trial_path):
pybamm.logger.verbose(f"Using path: '{location}' + '{path}'")
return trial_path
raise FileNotFoundError("Could not find parameter {}".format(path))
19 changes: 19 additions & 0 deletions pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,38 @@ def load_function(filename):
# Assign path to _ and filename to tail
_, tail = os.path.split(filename)

# Store the current working directory
orig_dir = os.getcwd()

# Strip absolute path to pybamm/input/example.py
if "pybamm" in filename:
root_path = filename[filename.rfind("pybamm") :]
# If the function is in the current working directory
elif os.getcwd() in filename:
root_path = filename.replace(os.getcwd(), "")
# getcwd() returns "C:\\" when in the root drive and "C:\\a\\b\\c" otherwise
if root_path[0] == "\\" or root_path[0] == "/":
root_path = root_path[1:]
# If the function is not in the current working directory and the path provided is
# absolute
elif os.path.isabs(filename) and not os.getcwd() in filename: # pragma: no cover
# Change directory to import the function
dir_path = os.path.split(filename)[0]
os.chdir(dir_path)
root_path = filename.replace(os.getcwd(), "")
root_path = root_path[1:]
else:
root_path = filename

path = root_path.replace("/", ".")
path = path.replace("\\", ".")
pybamm.logger.debug(
f"Importing function '{tail}' from file '{filename}' via path '{path}'"
)
module_object = importlib.import_module(path)

# Revert back current working directory if it was changed
os.chdir(orig_dir)
return getattr(module_object, tail)


Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_parameters/test_parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def test_init(self):
)
self.assertEqual(param["Positive electrode porosity"], 0.3)

# from file, absolute path
param = pybamm.ParameterValues(
os.path.join(
pybamm.root_dir(),
"pybamm",
"input",
"parameters",
"lithium_ion/positive_electrodes/lico2_Marquis2019/parameters.csv",
)
)
self.assertEqual(param["Positive electrode porosity"], 0.3)

# values vs chemistry
with self.assertRaisesRegex(
ValueError, "values and chemistry cannot both be None"
Expand Down