cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

Include functionality for rearranging algebraic loops to turn certain DAE's into ODE's #1050

Open FinbarArgus opened 1 year ago

FinbarArgus commented 1 year ago

Sometimes a system of equations will have algebraic loops that causes them to be seen as DAE's by the compiler. The process of doing the algebraic manipulation, to turn the system of DAE's into a system of ODE's would be very useful to have as a part of libcellml.

The attached file (change ext to cellml) is seen as a DAE when the line below "DAE Below" is uncommented and an ODE when instead the rearranged equation under "ODE below" is uncommented.

Checking_V3.txt

hsorby commented 1 year ago

Thanks for this, we are not yet able to deal with basic algebraic equations so this one is a little down the road. We will endeavour to address this issue when we get to DAEs proper.

gbernardino commented 1 year ago

I have been struggling with this for quite some time already, and finally I use a hacky solution. Current cellml does not seem very adapted to work with algebraic equations, at least opencor (where I write the cellml) constraints what you put in the LHS of an equation. I wrote a python script that parses the model to sympy, and then I can use their symbolic solver. It is not ideal, but works while waiting. After solving the equation I generate C-code, but it should be relatively easy to regenerate a cellml file, since sympy allows exortation to vanilla mathml. Just keep in mind that in the process of solving the equations, they are modified and might use more variables, so the module structure can be broken. I tried, and it works in your example. I can share the code if you want. Some comments:

  1. It is not guaranteed to find solutions in all cases, but works well in linear cases which are the ones I use the most (basically bifurcations in 0D models). Also, keep in mind the sympy solve used to give problems with piecewise defined functions.
  2. It is slow for a high number of variables.
  3. I only tested in a reduced set of cellml (basically the one I need for my models), and I do not deal with imports.