Skip to content

Commit

Permalink
Merge branch 'pybamm-team:develop' into jax_cli
Browse files Browse the repository at this point in the history
  • Loading branch information
priyanshuone6 authored Jan 18, 2022
2 parents 7699da5 + 393ff5d commit dc608c5
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 13 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ jobs:
if: matrix.os == 'ubuntu-latest'
run: tox -e pybamm-requires

- name: Run unit tests for GNU/Linux with Python 3.8 and 3.9
if: matrix.os == 'ubuntu-latest' && matrix.python-version != 3.7
- name: Run unit tests for GNU/Linux with Python 3.7 and 3.8
if: matrix.os == 'ubuntu-latest' && matrix.python-version != 3.9
run: python -m tox -e unit

- name: Run unit tests for GNU/Linux with Python 3.7 and generate coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.7
- name: Run unit tests for GNU/Linux with Python 3.9 and generate coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.9
run: tox -e coverage

- name: Upload coverage report
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.7
if: matrix.os == 'ubuntu-latest' && matrix.python-version == 3.9
uses: codecov/codecov-action@v2.1.0

- name: Run integration tests for GNU/Linux
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

## 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))
- Fixed issue in extraction of sensitivites ([#1894](https://github.com/pybamm-team/PyBaMM/pull/1894))

# [v21.12](https://github.com/pybamm-team/PyBaMM/tree/v21.11) - 2021-12-29

Expand All @@ -17,7 +19,6 @@
- Added cylindrical geometry and finite volume method ([#1824](https://github.com/pybamm-team/PyBaMM/pull/1824))

## Bug fixes

- `PyBaMM` is now importable in `Linux` systems where `jax` is already installed ([#1874](https://github.com/pybamm-team/PyBaMM/pull/1874))
- Simulations with drive cycles now support `initial_soc` ([#1842](https://github.com/pybamm-team/PyBaMM/pull/1842))
- Fixed bug in expression tree simplification ([#1831](https://github.com/pybamm-team/PyBaMM/pull/1831))
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))
7 changes: 6 additions & 1 deletion pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,12 @@ def _set_up_ext_and_inputs(
inputs[name] = casadi.MX.sym(name, input_param._expected_size)

external_variables = external_variables or {}
ext_and_inputs = {**external_variables, **inputs}

ordered_inputs_names = list(inputs.keys())
ordered_inputs_names.sort()
ordered_inputs = {name: inputs[name] for name in ordered_inputs_names}

ext_and_inputs = {**external_variables, **ordered_inputs}
return ext_and_inputs


Expand Down
13 changes: 9 additions & 4 deletions pybamm/solvers/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,14 @@ def initialise_2D(self):
self.second_dimension = "x"
self.R_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0] in [
"negative particle size",
"positive particle size",
] and self.auxiliary_domains["secondary"] == ["current collector"]:
elif (
self.domain[0]
in [
"negative particle size",
"positive particle size",
]
and self.auxiliary_domains["secondary"] == ["current collector"]
):
self.first_dimension = "R"
self.second_dimension = "z"
self.R_sol = first_dim_pts
Expand Down Expand Up @@ -563,6 +567,7 @@ def initialise_sensitivity_explicit_forward(self):
name: casadi.MX.sym(name, value.shape[0])
for name, value in self.all_inputs[0].items()
}

p_casadi_stacked = casadi.vertcat(*[p for p in p_casadi.values()])

# Convert variable to casadi format for differentiating
Expand Down
19 changes: 19 additions & 0 deletions pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,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
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def test_special_functions(self):
for np_fun in [
np.sqrt,
np.tanh,
np.cosh,
np.sinh,
np.exp,
np.log,
Expand All @@ -133,6 +132,21 @@ def test_special_functions(self):
pybamm.Function(np_fun, c).to_casadi(), casadi.MX(np_fun(3)), evalf=True
)

# A workaround to fix the tests running on GitHub Actions -
# casadi.evalf(
# pybamm.Function(np_fun, c).to_casadi()
# ) - casadi.evalf(casadi.MX(np_fun(3)))
# is not zero, but a small number of the order 10^-15 when np_func is np.cosh
for np_fun in [
np.cosh
]:
self.assert_casadi_almost_equal(
pybamm.Function(np_fun, c).to_casadi(),
casadi.MX(np_fun(3)),
decimal=14,
evalf=True,
)

# test functions with assert_casadi_almost_equal
for np_fun in [special.erf]:
self.assert_casadi_almost_equal(
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
3 changes: 2 additions & 1 deletion tests/unit/test_solvers/test_casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,10 @@ def test_solve_sensitivity_scalar_var_scalar_input(self):
solution = solver.solve(
model,
t_eval,
inputs={"p": 0.1, "q": 2, "r": -1, "s": 0.5},
inputs={"r": -1, "s": 0.5, "q": 2, "p": 0.1},
calculate_sensitivities=True,
)

np.testing.assert_allclose(solution.y[0], -1 + 0.2 * solution.t)
np.testing.assert_allclose(
solution.sensitivities["p"],
Expand Down

0 comments on commit dc608c5

Please sign in to comment.