-
-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added pchip Interpolator #4871
Merged
+120
−14
Merged
Added pchip Interpolator #4871
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5085c1d
added pchip interpolator
Rishab87 d8082f9
added changelog and few minor changes
Rishab87 469becf
fixing tests
Rishab87 0a5e52d
Merge branch 'develop' into pchip
MarcBerliner 80ff387
added extrapolation and tests
Rishab87 0c41863
Merge branch 'pchip' of https://github.com/Rishab87/PyBaMM into pchip
Rishab87 e4185d2
Update CHANGELOG.md
Rishab87 2978a09
Merge branch 'develop' into pchip
MarcBerliner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
import pybamm | ||
import pytest | ||
import numpy as np | ||
import casadi | ||
from scipy.interpolate import PchipInterpolator | ||
|
||
|
||
class TestExperiment: | ||
|
@@ -281,3 +283,75 @@ def test_voltage_without_directions(self): | |
|
||
voltage = solution["Terminal voltage [V]"].entries | ||
assert np.allclose(voltage, 2.5, atol=1e-3) | ||
|
||
def test_pchip_interpolation_experiment(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add a few more tests:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added these tests |
||
x = np.linspace(0, 1, 11) | ||
y_values = x**3 | ||
|
||
y = pybamm.StateVector(slice(0, 1)) | ||
interp = pybamm.Interpolant(x, y_values, y, interpolator="pchip") | ||
|
||
test_points = np.linspace(0, 1, 21) | ||
casadi_y = casadi.MX.sym("y", len(test_points), 1) | ||
interp_casadi = interp.to_casadi(y=casadi_y) | ||
f = casadi.Function("f", [casadi_y], [interp_casadi]) | ||
|
||
casadi_results = f(test_points.reshape((-1, 1))) | ||
expected = interp.evaluate(y=test_points) | ||
np.testing.assert_allclose(casadi_results, expected, rtol=1e-7, atol=1e-6) | ||
|
||
def test_pchip_interpolation_uniform_grid(self): | ||
x = np.linspace(0, 1, 11) | ||
y_values = np.sin(x) | ||
|
||
state = pybamm.StateVector(slice(0, 1)) | ||
interp = pybamm.Interpolant(x, y_values, state, interpolator="pchip") | ||
|
||
test_points = np.linspace(0, 1, 21) | ||
expected = PchipInterpolator(x, y_values)(test_points) | ||
|
||
casadi_y = casadi.MX.sym("y", 1) | ||
interp_casadi = interp.to_casadi(y=casadi_y) | ||
f = casadi.Function("f", [casadi_y], [interp_casadi]) | ||
result = np.array(f(test_points)).flatten() | ||
|
||
np.testing.assert_allclose(result, expected, rtol=1e-7, atol=1e-6) | ||
|
||
def test_pchip_interpolation_nonuniform_grid(self): | ||
x = np.array([0, 0.05, 0.2, 0.4, 0.65, 1.0]) | ||
y_values = np.exp(-x) | ||
state = pybamm.StateVector(slice(0, 1)) | ||
interp = pybamm.Interpolant(x, y_values, state, interpolator="pchip") | ||
|
||
test_points = np.linspace(0, 1, 21) | ||
expected = PchipInterpolator(x, y_values)(test_points) | ||
|
||
casadi_y = casadi.MX.sym("y", 1) | ||
interp_casadi = interp.to_casadi(y=casadi_y) | ||
f = casadi.Function("f", [casadi_y], [interp_casadi]) | ||
result = np.array(f(test_points)).flatten() | ||
|
||
np.testing.assert_allclose(result, expected, rtol=1e-7, atol=1e-6) | ||
|
||
def test_pchip_non_increasing_x(self): | ||
x = np.array([0, 0.5, 0.5, 1.0]) | ||
y_values = np.linspace(0, 1, 4) | ||
state = pybamm.StateVector(slice(0, 1)) | ||
with pytest.raises(ValueError, match="strictly increasing sequence"): | ||
_ = pybamm.Interpolant(x, y_values, state, interpolator="pchip") | ||
|
||
def test_pchip_extrapolation(self): | ||
x = np.linspace(0, 1, 11) | ||
y_values = np.log1p(x) # a smooth function on [0,1] | ||
state = pybamm.StateVector(slice(0, 1)) | ||
interp = pybamm.Interpolant(x, y_values, state, interpolator="pchip") | ||
|
||
test_points = np.array([-0.1, 1.1]) | ||
expected = PchipInterpolator(x, y_values)(test_points) | ||
|
||
casadi_y = casadi.MX.sym("y", 1) | ||
interp_casadi = interp.to_casadi(y=casadi_y) | ||
f = casadi.Function("f", [casadi_y], [interp_casadi]) | ||
result = np.array(f(test_points)).flatten() | ||
|
||
np.testing.assert_allclose(result, expected, rtol=1e-7, atol=1e-6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we access this array directly from the properties of the pchip class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes i think so as protected attribute