Code generation utilities
These are lower-level functions that ModelingToolkit leverages to generate code for building numerical problems.
ModelingToolkit.generate_rhs
— Functiongenerate_rhs(
sys::System;
implicit_dae,
scalar,
expression,
wrap_gfw,
eval_expression,
eval_module,
override_discrete,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the RHS function for the equations
of a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
implicit_dae
: Whether the generated function should be in the implicit form. Applicable only for ODEs/DAEs or discrete systems. Instead off(u, p, t)
(f(du, u, p, t)
for the in-place form) the function isf(du, u, p, t)
(respectivelyf(resid, du, u, p, t)
).override_discrete
: Whether to assume the system is discrete regardless ofis_discrete_system(sys)
.scalar
: Whether to generate a single-out-of-place function that returns a scalar for the only equation in the system.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_diffusion_function
— Functiongenerate_diffusion_function(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Any
Generate the diffusion function for the noise equations of a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_jacobian
— Functiongenerate_jacobian(
sys::System;
simplify,
sparse,
eval_expression,
eval_module,
expression,
wrap_gfw,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the jacobian function for the equations of a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_jacobian
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_tgrad
— Functiongenerate_tgrad(
sys::System;
simplify,
eval_expression,
eval_module,
expression,
wrap_gfw,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the tgrad function for the equations of a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
: Forwarded tocalculate_tgrad
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_W
— Functiongenerate_W(
sys::System;
simplify,
sparse,
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the W = γ * M + J
function for the equations of a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_jacobian
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_dae_jacobian
— Functiongenerate_dae_jacobian(
sys::System;
simplify,
sparse,
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the DAE jacobian γ * J′ + J
function for the equations of a System
. J′
is the jacobian of the equations with respect to the du
vector, and J
is the standard jacobian.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_jacobian
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_history
— Functiongenerate_history(
sys::System,
u0;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the history function for a System
, given a symbolic representation of the u0
vector prior to the initial time.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_boundary_conditions
— Functiongenerate_boundary_conditions(
sys::System,
u0,
u0_idxs,
t0;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the boundary condition function for a System
given the state vector u0
, the indexes of u0
to consider as hard constraints u0_idxs
and the initial time t0
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_cost
— Functiongenerate_cost(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Any
Generate the cost function for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_cost_gradient
— Functiongenerate_cost_gradient(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
simplify,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the gradient of the cost function with respect to unknowns for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
: Forwarded tocalculate_cost_gradient
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_cost_hessian
— Functiongenerate_cost_hessian(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
simplify,
sparse,
return_sparsity,
kwargs...
) -> Union{Tuple{Expr, Expr}, Tuple{Tuple{Expr, Expr}, Union{Nothing, SparseArrays.SparseMatrixCSC{Float64, Int64}, Matrix{Float64}}}}
Generate the hessian of the cost function for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_cost_hessian
.return_sparsity
: Whether to also return the sparsity pattern of the hessian as the second return value.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_cons
— Functiongenerate_cons(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the constraint function for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_constraint_jacobian
— Functiongenerate_constraint_jacobian(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
return_sparsity,
simplify,
sparse,
kwargs...
) -> Union{Expr, Tuple{Union{Expr, Tuple{Expr, Expr}}, Any}}
Generate the jacobian of the constraint function for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_constraint_jacobian
.return_sparsity
: Whether to also return the sparsity pattern of the jacobian as the second return value.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_constraint_hessian
— Functiongenerate_constraint_hessian(
sys::System;
expression,
wrap_gfw,
eval_expression,
eval_module,
return_sparsity,
simplify,
sparse,
kwargs...
) -> Union{Expr, Tuple{Union{Expr, Tuple{Expr, Expr}}, Any}}
Generate the hessian of the constraint function for a System
.
Keyword Arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_constraint_hessian
.return_sparsity
: Whether to also return the sparsity pattern of the hessian as the second return value.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_control_jacobian
— Functiongenerate_control_jacobian(
sys::ModelingToolkit.AbstractSystem;
expression,
wrap_gfw,
eval_expression,
eval_module,
simplify,
sparse,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Generate the jacobian function of the equations of sys
with respect to the inputs.
Keyword arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
simplify
,sparse
: Forwarded tocalculate_constraint_hessian
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.build_explicit_observed_function
— Functionbuild_explicit_observed_function(sys, ts; kwargs...) -> Function(s)
Generates a function that computes the observed value(s) ts
in the system sys
, while making the assumption that there are no cycles in the equations.
Arguments
sys
: The system for which to generate the functionts
: The symbolic observed values whose value should be computed
Keywords
return_inplace = false
: If true and the observed value is a vector, then return both the in place and out of place methods.expression = false
: Generates a JuliaExpr
computing the observed value if
expression` is trueeval_expression = false
: If true andexpression = false
, evaluates the returned function in the moduleeval_module
output_type = Array
the type of the array generated by a out-of-place vector-valued functionparam_only = false
if true, only allow the generated function to access system parametersinputs = nothing
additinoal symbolic variables that should be provided to the generated functioncheckbounds = true
checks bounds if true when destructuring parametersop = Operator
sets the recursion terminator for the walk done byvars
to identify the variables that appear ints
. See the documentation forvars
for more detail.throw = true
if true, throw an error when generating a function forts
that reference variables that do not exist.mkarray
: only used if the output is an array (that is,!isscalar(ts)
andts
is not a tuple, in which case the result will always be a tuple). Called asmkarray(ts, output_type)
wherets
are the expressions to put in the array andoutput_type
is the argument of the same name passed to buildexplicitobserved_function.cse = true
: Whether to use Common Subexpression Elimination (CSE) to generate a more efficient function.wrap_delays = is_dde(sys)
: Whether to add an argument for the history function and use it to calculate all delayed variables.
Returns
The return value will be either:
- a single function
f_oop
if the input is a scalar or if the input is a Vector butreturn_inplace
is false - the out of place and in-place functions
(f_ip, f_oop)
ifreturn_inplace
is true and the input is aVector
The function(s) f_oop
(and potentially f_ip
) will be:
RuntimeGeneratedFunction
s by default,- A Julia
Expr
ifexpression
is true, - A directly evaluated Julia function in the module
eval_module
ifeval_expression
is true andexpression
is false.
The signatures will be of the form g(...)
with arguments:
output
for in-place functionsunknowns
ifparam_only
isfalse
inputs
ifinputs
is an array of symbolic inputs that should be available ints
p...
unconditionally; note that in the case ofMTKParameters
more than one parameters argument may be present, so it must be splattedt
if the system is time-dependent; for example systems of nonlinear equations will not havet
For example, a function g(op, unknowns, p..., inputs, t)
will be the in-place function generated if return_inplace
is true, ts
is a vector, an array of inputs inputs
is given, and param_only
is false for a time-dependent system.
ModelingToolkit.generate_control_function
— Functiongenerate_control_function(sys::ModelingToolkit.AbstractSystem, input_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}, dist_ap_name::Union{Symbol, Vector{Symbol}, AnalysisPoint, Vector{AnalysisPoint}}; system_modifier = identity, kwargs)
When called with analysis points as input arguments, we assume that all analysis points corresponds to connections that should be opened (broken). The use case for this is to get rid of input signal blocks, such as Step
or Sine
, since these are useful for simulation but are not needed when using the plant model in a controller or state estimator.
(f_oop, f_ip), x_sym, p_sym, io_sys = generate_control_function(
sys::System,
inputs = unbound_inputs(sys),
disturbance_inputs = disturbances(sys);
implicit_dae = false,
simplify = false,
)
For a system sys
with inputs (as determined by unbound_inputs
or user specified), generate functions with additional input argument u
The returned functions are the out-of-place (f_oop
) and in-place (f_ip
) forms:
f_oop : (x,u,p,t) -> rhs
f_ip : (xout,x,u,p,t) -> nothing
The return values also include the chosen state-realization (the remaining unknowns) x_sym
and parameters, in the order they appear as arguments to f
.
If disturbance_inputs
is an array of variables, the generated dynamics function will preserve any state and dynamics associated with disturbance inputs, but the disturbance inputs themselves will (by default) not be included as inputs to the generated function. The use case for this is to generate dynamics for state observers that estimate the influence of unmeasured disturbances, and thus require unknown variables for the disturbance model, but without disturbance inputs since the disturbances are not available for measurement. To add an input argument corresponding to the disturbance inputs, either include the disturbance inputs among the control inputs, or set disturbance_argument=true
, in which case an additional input argument w
is added to the generated function (x,u,p,t,w)->rhs
.
Example
using ModelingToolkit: generate_control_function, varmap_to_vars, defaults
f, x_sym, ps = generate_control_function(sys, expression=Val{false}, simplify=false)
p = varmap_to_vars(defaults(sys), ps)
x = varmap_to_vars(defaults(sys), x_sym)
t = 0
f[1](x, inputs, p, t)
ModelingToolkit.generate_update_A
— Functiongenerate_update_A(
sys::System,
A::AbstractMatrix;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Given a system sys
and the A
from calculate_A_b
generate the function that updates A
given the parameter object.
Keyword arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
ModelingToolkit.generate_update_b
— Functiongenerate_update_b(
sys::System,
b::AbstractVector;
expression,
wrap_gfw,
eval_expression,
eval_module,
kwargs...
) -> Union{Expr, Tuple{Expr, Expr}}
Given a system sys
and the b
from calculate_A_b
generate the function that updates b
given the parameter object.
Keyword arguments
expression
:Val{true}
if this should return anExpr
(or tuple ofExpr
s) of the generated code.Val{false}
otherwise.wrap_gfw
:Val{true}
if the returned functions should be wrapped in a callable struct to make them callable using the expected syntax. The callable struct itself is internal API. Ifexpression == Val{true}
, the returned expression will construct the callable struct. If this function returns a tuple of functions/expressions, both will be identical ifwrap_gfw == Val{true}
.eval_expression
: Whether to compile any functions viaeval
orRuntimeGeneratedFunctions
.eval_module
: Ifeval_expression == true
, the module toeval
into. Otherwise, the module in which to generate theRuntimeGeneratedFunction
.
All other keyword arguments are forwarded to build_function_wrapper
.
For functions such as jacobian calculation which require symbolic computation, there are calculate_*
equivalents to obtain the symbolic result without building a function.
ModelingToolkit.calculate_tgrad
— Functioncalculate_tgrad(sys::System; simplify) -> Any
Calculate the gradient of the equations of sys
with respect to the independent variable. simplify
is forwarded to Symbolics.expand_derivatives
.
ModelingToolkit.calculate_jacobian
— Functioncalculate_jacobian(
sys::System;
sparse,
simplify,
dvs
) -> Any
Calculate the jacobian of the equations of sys
.
Keyword arguments
simplify
,sparse
: Forwarded toSymbolics.jacobian
.dvs
: The variables with respect to which the jacobian should be computed.
Symbolics.jacobian_sparsity
— Functionjacobian_sparsity(
sys::System
) -> SparseArrays.SparseMatrixCSC{Bool, Int64}
Return the sparsity pattern of the jacobian of sys
as a matrix.
ModelingToolkit.jacobian_dae_sparsity
— Functionjacobian_dae_sparsity(
sys::System
) -> SparseArrays.SparseMatrixCSC{Int64, Int64}
Return the sparsity pattern of the DAE jacobian of sys
as a matrix.
See also: generate_dae_jacobian
.
ModelingToolkit.calculate_hessian
— Functioncalculate_hessian(sys::System; simplify, sparse) -> Any
Return an array of symbolic hessians corresponding to the equations of the system.
Keyword Arguments
sparse
: Controls whether the symbolic hessians are sparse matricessimplify
: Forwarded toSymbolics.hessian
Symbolics.hessian_sparsity
— Functionhessian_sparsity(sys::System) -> Any
Return the sparsity pattern of the hessian of the equations of sys
.
ModelingToolkit.calculate_massmatrix
— Functioncalculate_massmatrix(sys::System; simplify) -> Any
Calculate the mass matrix of sys
. simplify
controls whether Symbolics.simplify
is applied to the symbolic mass matrix. Returns a Diagonal
or LinearAlgebra.I
wherever possible.
ModelingToolkit.W_sparsity
— FunctionW_sparsity(sys::System) -> Any
Return the sparsity pattern of the W
matrix of sys
.
See also: generate_W
.
ModelingToolkit.calculate_W_prototype
— Functioncalculate_W_prototype(W_sparsity; u0, sparse) -> Any
Return the matrix to use as the jacobian prototype given the W-sparsity matrix of the system. This is not the same as the jacobian sparsity pattern.
Keyword arguments
u0
: Theu0
vector for the problem.sparse
: The prototype isnothing
for non-sparse matrices.
ModelingToolkit.calculate_cost_gradient
— Functioncalculate_cost_gradient(sys::System; simplify) -> Any
Calculate the gradient of the consolidated cost of sys
with respect to the unknowns. simplify
is forwarded to Symbolics.gradient
.
ModelingToolkit.calculate_cost_hessian
— Functioncalculate_cost_hessian(
sys::System;
sparse,
simplify
) -> Union{SparseArrays.SparseMatrixCSC{Float64, Int64}, Matrix{Num}}
Calculate the hessian of the consolidated cost of sys
with respect to the unknowns. simplify
is forwarded to Symbolics.hessian
. sparse
controls whether a sparse matrix is returned.
ModelingToolkit.cost_hessian_sparsity
— Functioncost_hessian_sparsity(
sys::System
) -> Union{SparseArrays.SparseMatrixCSC{Float64, Int64}, Matrix{Float64}}
Return the sparsity pattern for the hessian of the cost function of sys
.
ModelingToolkit.calculate_constraint_jacobian
— Functioncalculate_constraint_jacobian(
sys::System;
simplify,
sparse,
return_sparsity
) -> Any
Return the jacobian of the constraints of sys
with respect to unknowns.
Keyword arguments
simplify
,sparse
: Forwarded toSymbolics.jacobian
.return_sparsity
: Whether to also return the sparsity pattern of the jacobian.
ModelingToolkit.calculate_constraint_hessian
— Functioncalculate_constraint_hessian(
sys::System;
simplify,
sparse,
return_sparsity
) -> Any
Return the hessian of the constraints of sys
with respect to unknowns.
Keyword arguments
simplify
,sparse
: Forwarded toSymbolics.hessian
.return_sparsity
: Whether to also return the sparsity pattern of the hessian.
ModelingToolkit.calculate_control_jacobian
— Functioncalculate_control_jacobian(
sys::ModelingToolkit.AbstractSystem;
sparse,
simplify
) -> Any
Calculate the jacobian of the equations of sys
with respect to the inputs.
Keyword arguments
simplify
,sparse
: Forwarded toSymbolics.jacobian
.
ModelingToolkit.calculate_A_b
— Functioncalculate_A_b(
sys::System;
sparse
) -> Tuple{Union{SparseArrays.SparseMatrixCSC{Any, Int64}, Matrix{Any}}, Any}
Return matrix A
and vector b
such that the system sys
can be represented as A * x = b
where x
is unknowns(sys)
. Errors if the system is not affine.
Keyword arguments
sparse
: return a sparseA
.
All code generation eventually calls build_function_wrapper
.