Skip to content

Commit

Permalink
#858 fix simplify bug, fix expression tree notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjrobins committed Mar 15, 2020
1 parent 40feb08 commit 3cdb6dc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
17 changes: 12 additions & 5 deletions examples/notebooks/expression_tree/expression-tree.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also calculate the expression tree representing the gradient of the equation with respect to $t$ (which is of course simply the scalar value 1),"
"We can also calculate the expression tree representing the gradient of the equation with respect to $t$,"
]
},
{
Expand All @@ -84,7 +84,7 @@
"![](expression_tree2.png)\n",
"\n",
"\n",
"...and evaluate this expression, which will again give 1."
"...and evaluate this expression,"
]
},
{
Expand All @@ -95,7 +95,7 @@
{
"data": {
"text/plain": [
"1.0"
"array([[-11.]])"
]
},
"execution_count": 4,
Expand All @@ -104,7 +104,7 @@
}
],
"source": [
"diff_wrt_equation.evaluate(1, np.array([2]))"
"diff_wrt_equation.evaluate(t=1, y=np.array([2]), y_dot=np.array([2]))"
]
},
{
Expand Down Expand Up @@ -202,6 +202,13 @@
"\n",
"After the third stage, our expression tree is now able to be evaluated by one of the solver classes. Note that we have used a single equation above to illustrate the different types of expression trees in PyBaMM, but any given models will consist of many RHS or algebraic equations, along with boundary conditions. See [here](https://github.com/pybamm-team/PyBaMM/blob/master/examples/notebooks/add-model.ipynb) for more details of PyBaMM models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -220,7 +227,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.6.7"
}
},
"nbformat": 4,
Expand Down
Binary file modified examples/notebooks/expression_tree/expression_tree2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions pybamm/expression_tree/binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def _binary_simplify(self, left, right):
if is_scalar_zero(right):
return pybamm.Scalar(1)

# zero to the power of anything is zero
if is_scalar_zero(left):
return pybamm.Scalar(0)

# anything to the power of one is itself
if is_scalar_one(right):
return left
Expand Down
2 changes: 2 additions & 0 deletions pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ def evaluate_ignoring_errors(self, t=0):
# (there is a e.g. StateVector in the tree)
if error.args[0] == "StateVector cannot evaluate input 'y=None'":
return None
elif error.args[0] == "StateVectorDot cannot evaluate input 'y_dot=None'":
return None
else:
raise error
except ValueError as e:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_expression_tree/test_operations/test_simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_symbol_simplify(self):
d = pybamm.Scalar(-1)
e = pybamm.Scalar(2)
g = pybamm.Variable("g")
gdot = pybamm.VariableDot("g'")

# negate
self.assertIsInstance((-a).simplify(), pybamm.Scalar)
Expand Down Expand Up @@ -175,6 +176,18 @@ def myfunction(x, y):
self.assertIsInstance(expr.children[1], pybamm.Negate)
self.assertIsInstance(expr.children[1].children[0], pybamm.Parameter)

expr = (e * g * b).simplify()
self.assertIsInstance(expr, pybamm.Multiplication)
self.assertIsInstance(expr.children[0], pybamm.Scalar)
self.assertEqual(expr.children[0].evaluate(), 2.0)
self.assertIsInstance(expr.children[1], pybamm.Variable)

expr = (e * gdot * b).simplify()
self.assertIsInstance(expr, pybamm.Multiplication)
self.assertIsInstance(expr.children[0], pybamm.Scalar)
self.assertEqual(expr.children[0].evaluate(), 2.0)
self.assertIsInstance(expr.children[1], pybamm.VariableDot)

expr = (e + (g - c)).simplify()
self.assertIsInstance(expr, pybamm.Addition)
self.assertIsInstance(expr.children[0], pybamm.Scalar)
Expand Down

0 comments on commit 3cdb6dc

Please sign in to comment.