This blog post briefly describes the project "Print equations" with PyBaMM during the first three weeks of GSoC'21.
Work Done
- Added a
BaseParameters
class - Modified all parameters classes to inherit from the base class
- Added SymPy in dependencies
- Added
to_equation
in some classes - Printed SPM model equations
Base Parameters class
I added a BaseParameters
class to overload the __setattr__
method in the
parameters class to record what the variable was called (as suggested by Valentin Sulzer in this
comment) and modified all parameters classes to inherit from the base parameters class.
SymPy to render SPM equations
- Added SymPy in PyBaMM's dependencies
- Added
to_equation
method in- Binary Operators
- Scalars
- Arrays
- FunctionParameter
- Symbol
- UnaryOperator (base class)
- Gradient
- Divergence
- Variable
I added to_equation
in all the classes required to print Single Particle Model (SPM) equations and doing this (will add this function later to the model)
model = pybamm.lithium_ion.SPM()
output = []
for var, eqn in model.rhs.items():
lhs = sympy.Derivative(var.print_name, 't')
rhs = sympy.nsimplify(eqn.to_equation())
final = sympy.Eq(lhs, rhs, evaluate=False)
output.append(final)
resulted in this
Merged Pull Requests
- Add BaseParameters and SymPy (#1495)
- Add
to_equation
in Binary and Scalar (#1502) - Add
to_equation
for SPM (#1517)
Challenges I ran into
The most challenging part of printing the equations is finding the appropriate SymPy object in to_equation
to return to. It sometimes took me days to find the equivalent function in SymPy which returns something similar to what the methods in PyBaMM returns and debugging the entire expression trees by putting import pdb; pdb.set_trace()
in various places to see what went wrong.
Later I found that jupyter notebook has a %pdb
command which came in very handy. Just say %pdb
on any cell and subsequently, the pdb debugger will run on all exceptions, no matter how deep in the call stack.
To Do in the coming week
Now in the coming week, I will try to print the voltage expressions of the SPM
That’s all for this post. Thanks!