26 #include "collocation.hpp"
27 #include "casadi/core/polynomial.hpp"
28 #include "casadi/core/casadi_misc.hpp"
33 int CASADI_INTEGRATOR_COLLOCATION_EXPORT
36 plugin->name =
"collocation";
38 plugin->version = CASADI_VERSION;
50 double t0,
const std::vector<double>& tout)
59 {{
"interpolation_order",
61 "Order of the interpolating polynomials"}},
62 {
"collocation_scheme",
64 "Collocation scheme: radau|legendre"}}
74 for (
auto&& op : opts) {
75 if (op.first==
"interpolation_order") {
77 }
else if (op.first==
"collocation_scheme") {
87 MX ret = vertcat(x0, z0);
88 return repmat(ret,
deg_);
100 tau_root.insert(tau_root.begin(), 0);
103 std::vector<std::vector<double> >
C(
deg_ + 1, std::vector<double>(
deg_ + 1, 0));
106 std::vector<double>
D(
deg_ + 1, 0);
109 std::vector<double> B(
deg_ + 1, 0);
112 for (casadi_int j = 0; j <
deg_ + 1; ++j) {
116 for (casadi_int r = 0; r <
deg_+1; ++r) {
118 p *=
Polynomial(-tau_root[r], 1) / (tau_root[j] - tau_root[r]);
125 D[j] = j==
deg_ ? 1 : 0;
133 for (casadi_int r = 0; r <
deg_ + 1; ++r) {
134 C[j][r] = dp(tau_root[r]);
151 std::vector<casadi_int> v_offset(1, 0);
152 for (casadi_int d = 0; d <
deg_; ++d) {
153 v_offset.push_back(v_offset.back() +
nx1_);
154 v_offset.push_back(v_offset.back() +
nz1_);
156 std::vector<MX> vv = vertsplit(v, v_offset);
157 std::vector<MX>::const_iterator vv_it = vv.begin();
160 std::vector<MX> x(
deg_ + 1), z(
deg_ + 1);
161 for (casadi_int d = 1; d <=
deg_; ++d) {
165 casadi_assert_dev(vv_it == vv.end());
168 std::vector<MX> tt(
deg_ + 1);
169 for (casadi_int d = 0; d <=
deg_; ++d) {
170 tt[d] = t0 + h * tau_root[d];
183 for (casadi_int j = 1; j <
deg_ + 1; ++j) {
187 f_arg[
DYN_T] = tt[j];
192 std::vector<MX> f_res = f(f_arg);
195 MX xp_j =
C[0][j] * x0;
196 for (casadi_int r = 1; r <
deg_ + 1; ++r) {
197 xp_j +=
C[r][j] * x[r];
201 eq.push_back(vec(h * f_res[
DYN_ODE] - xp_j));
204 eq.push_back(vec(f_res[
DYN_ALG]));
225 Function F(
"implicit_step", F_in, F_out,
226 {
"t",
"h",
"x0",
"v0",
"p",
"u"}, {
"xf",
"vf",
"qf"});
239 for (casadi_int d = 0; d <
deg_; ++d) {
Collocation(const std::string &name, const Function &dae, double t0, const std::vector< double > &tout)
Constructor.
void setup_step() override
Setup step functions.
MX algebraic_state_output(const MX &Z) const override
void reset(IntegratorMemory *mem, bool first_call) const override
Reset the forward solver at the start or after an event.
void init(const Dict &opts) override
Initialize stage.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
static Integrator * creator(const std::string &name, const Function &dae, double t0, const std::vector< double > &tout)
Create a new integrator.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
std::string collocation_scheme_
static const std::string meta_doc
A documentation string.
~Collocation() override
Destructor.
MX algebraic_state_init(const MX &x0, const MX &z0) const override
static const Options options_
Options.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void reset(IntegratorMemory *mem, bool first_call) const override
Reset the forward solver at the start or after an event.
const Sparsity & sparsity_in(casadi_int ind) const
Get sparsity of a given input.
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
static MX zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
void init(const Dict &opts) override
Initialize stage.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
static const Options options_
Options.
void set_function(const Function &fcn, const std::string &fname, bool jit=false)
std::vector< std::string > get_function() const override
Get list of dependency functions.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
Helper class for differentiating and integrating polynomials.
Polynomial anti_derivative() const
Create a new polynomial for the anti-derivative (primitive function)
Polynomial derivative() const
Create a new polynomial for the derivative.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
Class representing a Slice.
void CASADI_INTEGRATOR_COLLOCATION_EXPORT casadi_load_integrator_collocation()
@ STEP_NUM_IN
Number of arguments.
@ STEP_V0
Dependent variables.
@ STEP_XF
State vector at next time.
@ STEP_QF
Quadrature state contribution.
@ STEP_VF
Dependent variables at next time.
@ STEP_NUM_OUT
Number of arguments.
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
int CASADI_INTEGRATOR_COLLOCATION_EXPORT casadi_register_integrator_collocation(Integrator::Plugin *plugin)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::vector< double > collocation_points(casadi_int order, const std::string &scheme)
Obtain collocation points of specific order and scheme.
Options metadata for a class.