Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalized integrator class for use with FMUs #2823

Closed
jaeandersson opened this issue Aug 22, 2021 · 5 comments
Closed

Generalized integrator class for use with FMUs #2823

jaeandersson opened this issue Aug 22, 2021 · 5 comments
Assignees
Milestone

Comments

@jaeandersson
Copy link
Member

jaeandersson commented Aug 22, 2021

The binary FMU interface (#2779) allows FMUs to be evaluated pointwise. This is would in principle allow the FMU to be integrated using the existing Integrator class, but this would potentially be very limited and inefficient, due to:

  • Excessive initializations of the FMU, which is expensive
  • No event handling
  • No easy way to incorporate piecewise constant control inputs
  • Derivative calculation might be very expensive
  • Need to keep track of which states are differential and which are algebraic

To get around this, it makes sense to add a dedicated Simulator plugin class which can be constructed from an FMU function instance:

dae = DaeBuilder(...)
...
f = dae.fmu_fun(...)
time_grid = ...
simulator = casadi.simulator('simulator', f, time_grid)

To start with, the simulator class would only be defined for fmu_functions, but it could easily be extended to symbolic representations of the dynamic system. It would also not be differentiable, at least not at first. It might be possible and desirable to combine this class with the integrator class, but given the disruption it would result in, it's better to keep the classes separate at first, but with maximum amount of communality. The dynamic system argument should correspond to a state-space representation with quadratures: (t, x, z, p, u) -> (xdot, alg, y, qdot). The Simulator class will have the same signature as Integrator but add piecewise controls ("u") and outputs ("y").

To be able to simulate an FMU, a CVodes plugin makes sense to begin with.

Update:
This issue has taken some unexpected turns. The latest conclusion is that maintaining both Integrator and Simulator is not realistic. Instead, Integrator has now been extended with elements from the Simulator class, in particular control inputs. The output support has been left out since they can be calculated after the integration with few drawbacks. What remains of this issue is to port forward sensitivity approach from Simulator to Integrator, i.e. add "nfwd" as an option, instead of deducing it from the number of columns in the oracle_ inputs/outputs.

@jaeandersson jaeandersson self-assigned this Aug 22, 2021
jaeandersson added a commit that referenced this issue Aug 24, 2021
New class (Simulator) will exist alongside old class (Integrator) until it's mature enough to replace it
jaeandersson added a commit that referenced this issue Aug 24, 2021
jaeandersson added a commit that referenced this issue Aug 24, 2021
@jaeandersson jaeandersson added this to the Version 3.6 milestone Mar 2, 2022
@jaeandersson
Copy link
Member Author

Closing this issue. Additional features handled in dedicated issues.

jaeandersson added a commit that referenced this issue May 6, 2022
Now includes option to redirect validation data to file
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
New class (Simulator) will exist alongside old class (Integrator) until it's mature enough to replace it
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
jcotem pushed a commit to jcotem/casadi that referenced this issue Sep 12, 2022
…ction

Now includes option to redirect validation data to file
@jaeandersson
Copy link
Member Author

Reopened to better align syntax with integrator, and to add quadrature support

jaeandersson added a commit that referenced this issue Mar 22, 2023
Enabled the new implementation
jaeandersson added a commit that referenced this issue Mar 22, 2023
jaeandersson added a commit that referenced this issue Mar 22, 2023
The oracle is now the non-augmented DAE and the option nfwd indicates how many sensitivity right-hand-sides
jaeandersson added a commit that referenced this issue Mar 22, 2023
Now created on-demand inside differentiation functions and cached
jaeandersson added a commit that referenced this issue Mar 22, 2023
jaeandersson added a commit that referenced this issue Mar 22, 2023
Jacobians inherited even if derivative_of_ has sensitivities
jaeandersson added a commit that referenced this issue Mar 29, 2023
All key functionalities of the Simulator class are now part of the generalized Integrator class: Control support,
analytic derivatives (forward/adjoint) in combination with multiple stopping times for the integrator,
structure exploiting derivatives.
Some lingering features had not been merged as of this writing: Sensitivitiy calculations using Sundials native
forward sensitivity approach (covered by #3045) and problem-specific scalar factors (covered by #3046).
Both those features are important for large-scale applications, but they can easily be ported to the Integrator class
later.
jaeandersson added a commit that referenced this issue Mar 29, 2023
@jaeandersson
Copy link
Member Author

Work completed. This issue is a major change internally, but with limited changes for the end-user. Relevant changes for the end-user include:

  • Deprecation of the t0, tf, output_t0 and grid options. They are now replaced by additional, optional, constructor arguments.
  • An additional nfwd argument to obtain forward sensitivity equations as additional right-hand-sides in the outputs.
  • Breaking changes to the serialization API for the integrator class specifically.

@weberscode
Copy link

Hei All,

Is it possible to upload a simple optimal control example from fmu model? That would definitely be very helpful for my research.

Kind regards Simon Weber

@jaeandersson
Copy link
Member Author

jaeandersson commented Apr 15, 2023

Hei All,

Is it possible to upload a simple optimal control example from fmu model? That would definitely be very helpful for my research.

Kind regards Simon Weber

Sorry, things got a bit hectic before the release and the FMU demo I had never got uploaded. You can find the latest version here:
https://github.com/casadi/casadi/blob/develop/docs/notebooks/fmu_demo.ipynb

The demo is pretty rough, and I just discovered that one of part of it got accidentally broken before the 3.6.0 release. I'll try to include a fixed version in 3.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants