Week 4, 5 and 6 - GSoC 2021

Week 4, 5 and 6 - GSoC 2021

This blog post briefly describes the work done on the project during the 4th, 5th and 6th week of GSoC 2021

Work Done

  1. Added to_equation in
    • Function
    • Arcsinh
    • Parameter
    • BoundaryValue
    • Broadcasts
    • Concatenation
    • Log
    • Sinh
    • Integral
    • Independent Variable
  2. Automated print_name using regex
  3. Added sympy_overrides to override sympy methods

Added to_equation

I added to_equation in all the classes required to print the voltage expressions for Single Particle Model (SPM), Single Particle Model with electrolyte (SPMe) and Doyle-Fuller-Newman Model (DFN).

Boundary Value and Concatenation required additional customization to meet the needs. Boundary Value had various if-else conditions and was required to add the superscript of surf whereas, in Concatenation, all the nodes should be cases in a curly bracket. This was achieved by converting the objects into latex first, adding the required values in latex and then converting them back to Symbol objects which looked like this

spme-2.PNG

and after attaching a print name to the variables and just parsing the concatenation nodes using depth-first search which has a print name attached looked like this

Capture.PNG

Automated print name

Overrides print names for each variable would be a lot of work, so the print_name was automated to some extent using regex. These were some conditions automated using regex

  • “_init”, “_ref”, “_typ”, “_max”, “_0” should always become superscripts, comma-separated
  • everything else after a _ should be subscripts, comma-separated
  • “_dim” or “dimensional” should become a hat
  • “_xav” or “_av” should become a bar
  • greek letters: automatically recognize these (from the finite list of greek alphabet letters) and add a backslash before

Added sympy_overrides to override sympy methods

Some derivatives in the LHS were supposed to be partial derivative but were printing as d/dt as _print_Derivative of sympy currently decides based on requires_partial if it's going to use the rounded d symbol or not. The requires_partial function checks how many free symbols the expression is using. So I overrode the _print_Derivative of sympy with the help of this StackOverflow answer.

Merged Pull Requests

Challenges I ran into

The most challenging part of printing the equations is finding the appropriate SymPy object in to_equation to return to and tackling the custom cases of Boundary Value and Concatenation.

TODO in the coming weeks

Now in the coming weeks, I will try to refine the equations and add a function in BaseModel to print all the equations.

That’s all for this post. Thanks and stay tuned!