Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b341616
commit 6ca684d
Showing
2 changed files
with
69 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# | ||
# Tests for the Scalar class | ||
# | ||
import pybamm | ||
|
||
import unittest | ||
import numpy as np | ||
|
||
|
||
class TestDDT(unittest.TestCase): | ||
def test_time_derivative(self): | ||
a = pybamm.Scalar(5).diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.Scalar) | ||
self.assertEqual(a.value, 0) | ||
|
||
a = pybamm.t.diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.Scalar) | ||
self.assertEqual(a.value, 1) | ||
|
||
a = (pybamm.t**2).diff(pybamm.t) | ||
self.assertEqual(a.id, (2 * pybamm.t ** 1 * 1).id) | ||
self.assertEqual(a.simplify().id, (2 * pybamm.t).id) | ||
self.assertEqual(a.evaluate(t=1), 2) | ||
|
||
a =(2 + pybamm.t**2).diff(pybamm.t) | ||
self.assertEqual(a.simplify().id, (2*pybamm.t).id) | ||
self.assertEqual(a.evaluate(t=1), 2) | ||
|
||
def test_time_derivative_of_variable(self): | ||
|
||
a = (pybamm.Variable('a')).diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.VariableDot) | ||
self.assertEqual(a.name, "a'") | ||
|
||
p = pybamm.Parameter('p') | ||
a = (1 + p*pybamm.Variable('a')).diff(pybamm.t).simplify() | ||
self.assertIsInstance(a, pybamm.Multiplication) | ||
self.assertEqual(a.children[0].name, 'p') | ||
self.assertEqual(a.children[1].name, "a'") | ||
|
||
with self.assertRaises(pybamm.ModelError): | ||
a = (pybamm.Variable('a')).diff(pybamm.t).diff(pybamm.t) | ||
|
||
with self.assertRaises(pybamm.ModelError): | ||
a = pybamm.ExternalVariable("a", 1).diff(pybamm.t) | ||
|
||
def test_time_derivative_of_state_vector(self): | ||
|
||
sv = pybamm.StateVector(slice(0, 10)) | ||
y_dot = np.linspace(0, 2, 19) | ||
|
||
a = sv.diff(pybamm.t) | ||
self.assertIsInstance(a, pybamm.StateVectorDot) | ||
self.assertEqual(a.name[-1], "'") | ||
np.testing.assert_array_equal( | ||
a.evaluate(y_dot=y_dot), np.linspace(0, 1, 10)[:, np.newaxis] | ||
) | ||
|
||
with self.assertRaises(pybamm.ModelError): | ||
a = (sv).diff(pybamm.t).diff(pybamm.t) | ||
|
||
if __name__ == "__main__": | ||
print("Add -v for more debug output") | ||
import sys | ||
|
||
if "-v" in sys.argv: | ||
debug = True | ||
pybamm.settings.debug_mode = True | ||
unittest.main() |