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

New API to allow In-place and Static Return functions #13

Merged
merged 31 commits into from
Nov 30, 2021
Merged

Conversation

bjack205
Copy link
Member

@bjack205 bjack205 commented Nov 18, 2021

Complete overhaul of existing code. Now provides an abstract FunctionBase that provides basic functionality for defining custom functions and their derivatives.

New Function Base

Example of the new API:

evaluate(fun, x, u, p)     // with parameters p
evaluate!(fun, y, x, u, p) // with output to y
jacobian!(sig, diff, fun, J, y, x, u, p) // evaluate the Jacobian for function signature `sig` using diff method `diff`

Function signatures are either StaticReturn or Inplace, and diff methods are either ForwardAD or FiniteDifference or UserDefined.

By default, these Jacobian methods must be defined by the user, but this PR does provide a macro to define them automatically for both auto and finite differencing:

@autodiff struct MyFunction <: AbstractFunction end

which defines these methods and modifies the struct (adding caches) to provide allocation-free differentiation methods.

New API for Discrete Models

Instead of defining both continuous and discrete methods for the same model, all models are either continuous or discrete. A continuous model can be converted to a discrete one using DiscretizedModel and providing an integrator, such as Euler or RK4. Rather than being abstract or singleton types as they were before, these types now allocate storage needed to evaluate these methods and their Jacobians allocation-free.

The continuous dynamics API now looks like

dynamics(model, x, u, t)             // must be defined by user
dynamics!(model, xdot, x, u, t)   // must be defined by user
dynamics(model, z)        // pass in knot point (provided automatically)
dynamics(model, xdot)   // pass in knot point (provided automatically)
jacobian!(sig, diff, model, J, xdot, z)         /// provided automatically (via @autodiff)
jacobian!(model, J, xdot, x, u, t) // the signature a user can define to provide a `UserDefined` method

The discrete dynamics API:

discrete_dynamics(model, x, u, t, h)         // must be defined by user
discrete_dynamics!(model, xn, x, u, t, h)  // must be defined by user
discrete_dynamics(model, z)         // provided automatically
discrete_dynamics!(model, xn, z)  // provided automatically
jacobian!(sig, diff, model, J, xn, z)  // provided automatically (via @autodiff)
jacobian!(model, xn, x, u, t, h)  // can be defined by user to provide a `UserDefined` method

All discrete models also provide the follow methods to evaluate the dynamics error:

dynamics_error(model, z2, z1)
dynamics_error!(model, y2, y1, z2, z1)
dynamics_error_jacobian!(sig, diff, model, J2, J1, y2, y1, z2, z1)

which is f(x,u) - xn for explicit integrators (defined automatically) or f(xn,un,x,u) - xn for implicit integrators.

Other Changes

  • Linear models have been removed (may be added back in the future).
  • Knot points have been modified to avoid static arrays if the underlying type is a normal vector
  • state_diff_size has been replaced by errstate_size
  • Order of arguments for state_diff_jacobian and ∇²differential! have swapped the order of the first 2 arguments
  • Integrators need to implement both in-place and static return methods

@bjack205 bjack205 requested a review from kevin-tracy November 18, 2021 19:04
@bjack205 bjack205 self-assigned this Nov 18, 2021
@bjack205 bjack205 merged commit e0e8fe3 into master Nov 30, 2021
@bjack205 bjack205 deleted the function_base branch November 30, 2021 12:05
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

Successfully merging this pull request may close these issues.

1 participant