brocksam / pycollo

General-purpose optimal control, trajectory optimisation and parameter optimisation using direct collocation
https://brocksam.github.io/pycollo/
MIT License
8 stars 3 forks source link

Support `sympy.physics.mechanics.dynamicsymbols` as well as `sympy.Symbol` in SymPy backend #64

Open brocksam opened 1 year ago

brocksam commented 1 year ago

The SymPy backend should natively support sympy.physics.mechanics.dynamicsymbols as well as sympy.Symbol objects (as was the case prior to https://github.com/brocksam/pycollo/commit/24eb461975834279ecad098a58b9cb71b4a314a3). It is common for users to use sympy.physics.mechanics to derive equations of motion (EoMs) that are then used to construct their optimal control problem. These EoMs will be built from both sympy.physics.mechanics.dynamicsymbols and sympy.Symbol objects. It should be possible to directly set sympy.physics.mechanics.dynamicsymbols as state and control variables in the OCP definition when the SymPy backend is being used to save users doing a replacement to sympy.Symbol objects, which can be costly for large and complex models.

Note: the workaround until this issue is resolved is for users to manually substitute sympy.Symbol objects in place of sympy.physics.mechanics.dynamicsymbols before constructing their OCP via pycollo.OptimalControlProblem.

brocksam commented 1 year ago

PR #67 has implemented the basics of this in a way that'll work with the CasADi backend. See the PR description for details about how it can be used. Particularly if you wish to access the state variables from Phase.state_variables as attributes using the symbol's name, the (t) of the me.dynamicsymbol is trimmed off. I.e. using me.dynamicsymbols("x") with name x(t) can be accessed by phase.state_variables.x.

brocksam commented 1 year ago

Currently it's not possible to use Derivative(x(t), t) as state or control variables without error.