callback_internal.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "callback_internal.hpp"
26 
27 namespace casadi {
28 
29 #define TRY_CALL(FCN, OBJ, ...) \
30 try { \
31  casadi_assert((OBJ)!=0, "Callback object has been deleted"); \
32  return (OBJ)->FCN(__VA_ARGS__);\
33 } catch (KeyboardInterruptException&) { \
34  throw; \
35 } catch (std::exception& ex) { \
36  casadi_error("Error calling \"" CASADI_STR(FCN) "\" for object " \
37  + name_ + ":\n" + std::string(ex.what())); \
38 }
39 
41 CallbackInternal(const std::string& name, Callback *self)
42  : FunctionInternal(name), self_(self), has_eval_buffer_(false) {
43 }
44 
46  clear_mem();
47 }
48 
50  TRY_CALL(get_n_in, self_);
51 }
52 
54  TRY_CALL(get_n_out, self_);
55 }
56 
58  TRY_CALL(get_sparsity_in, self_, i);
59 }
60 
62  TRY_CALL(get_sparsity_out, self_, i);
63 }
64 
65 std::string CallbackInternal::get_name_in(casadi_int i) {
66  TRY_CALL(get_name_in, self_, i);
67 }
68 
69 std::string CallbackInternal::get_name_out(casadi_int i) {
70  TRY_CALL(get_name_out, self_, i);
71 }
72 
74  TRY_CALL(has_eval_buffer, self_);
75 }
76 
77 bool CallbackInternal::has_jac_sparsity(casadi_int oind, casadi_int iind) const {
78  TRY_CALL(has_jac_sparsity, self_, oind, iind);
79 }
80 
81 Sparsity CallbackInternal::get_jac_sparsity(casadi_int oind, casadi_int iind,
82  bool symmetric) const {
83  TRY_CALL(get_jac_sparsity, self_, oind, iind, symmetric);
84 }
85 
86 void CallbackInternal::init(const Dict& opts) {
87  // Initialize the base classes
89 
90  // Initialize this
91  casadi_assert(self_!=nullptr, "Callback object has been deleted");
92  self_->init();
93 }
94 
96  // Finalize this
97  casadi_assert(self_!=nullptr, "Callback object has been deleted");
98  self_->finalize();
99 
100  // Finalize the base classes
102 
104 
105  if (has_eval_buffer_) {
106  sizes_arg_.resize(n_in_);
107  for (casadi_int i=0;i<n_in_;++i) {
108  sizes_arg_[i] = nnz_in(i);
109  }
110  sizes_res_.resize(n_out_);
111  for (casadi_int i=0;i<n_out_;++i) {
112  sizes_res_[i] = nnz_out(i);
113  }
114  }
115 }
116 
117 std::vector<DM> CallbackInternal::eval_dm(const std::vector<DM>& arg) const {
118  TRY_CALL(eval, self_, arg);
119 }
120 
122 int CallbackInternal::eval(const double** arg, double** res,
123  casadi_int* iw, double* w, void* mem) const {
124  setup(mem, arg, res, iw, w);
125  if (has_eval_dm()) {
126  return FunctionInternal::eval(arg, res, iw, w, mem);
127  } else {
128  TRY_CALL(eval_buffer, self_, arg, sizes_arg_, res, sizes_res_);
129  }
130 }
131 
133  TRY_CALL(uses_output, self_);
134 }
135 
136 Function CallbackInternal::factory(const std::string& name,
137  const std::vector<std::string>& s_in,
138  const std::vector<std::string>& s_out,
139  const Function::AuxOut& aux,
140  const Dict& opts) const {
141  TRY_CALL(get_factory, self_, name, s_in, s_out, aux, opts);
142 }
143 
145  TRY_CALL(has_jacobian, self_);
146 }
147 
149 get_jacobian(const std::string& name,
150  const std::vector<std::string>& inames,
151  const std::vector<std::string>& onames,
152  const Dict& opts) const {
153  TRY_CALL(get_jacobian, self_, name, inames, onames, opts);
154 }
155 
157 get_forward(casadi_int nfwd, const std::string& name,
158  const std::vector<std::string>& inames,
159  const std::vector<std::string>& onames, const Dict& opts) const {
160  TRY_CALL(get_forward, self_, nfwd, name, inames, onames, opts);
161 }
162 
163 bool CallbackInternal::has_forward(casadi_int nfwd) const {
164  TRY_CALL(has_forward, self_, nfwd);
165 }
166 
168 get_reverse(casadi_int nadj, const std::string& name,
169  const std::vector<std::string>& inames,
170  const std::vector<std::string>& onames, const Dict& opts) const {
171  TRY_CALL(get_reverse, self_, nadj, name, inames, onames, opts);
172 }
173 
174 bool CallbackInternal::has_reverse(casadi_int nadj) const {
175  TRY_CALL(has_reverse, self_, nadj);
176 }
177 
178 } // namespace casadi
std::vector< DM > eval_dm(const std::vector< DM > &arg) const override
Evaluate with DM matrices.
void finalize() override
Finalize the object creation.
bool has_jacobian() const override
Return Jacobian of all input elements with respect to all output elements.
void init(const Dict &opts) override
Initialize.
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
size_t get_n_out() override
Number of function inputs and outputs.
Callback * self_
Pointer to the public class.
bool has_forward(casadi_int nfwd) const override
Return function that calculates forward derivatives.
std::vector< casadi_int > sizes_arg_
bool has_jac_sparsity(casadi_int oind, casadi_int iind) const override
Return sparsity of Jacobian of an output respect to an input.
Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return Jacobian of all input elements with respect to all output elements.
~CallbackInternal() override
Destructor.
size_t get_n_in() override
Number of function inputs and outputs.
std::vector< casadi_int > sizes_res_
Function factory(const std::string &name, const std::vector< std::string > &s_in, const std::vector< std::string > &s_out, const Function::AuxOut &aux, const Dict &opts) const override
Customize calls to the function factory.
bool has_eval_dm() const override
Evaluate with DM matrices.
bool uses_output() const override
Do the derivative functions need nondifferentiated outputs?
Function get_reverse(casadi_int nadj, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return function that calculates adjoint derivatives.
Function get_forward(casadi_int nfwd, const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const override
Return function that calculates forward derivatives.
Sparsity get_sparsity_out(casadi_int i) override
Sparsities of function inputs and outputs.
virtual int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Evaluate numerically.
bool has_reverse(casadi_int nadj) const override
Return function that calculates adjoint derivatives.
Sparsity get_jac_sparsity(casadi_int oind, casadi_int iind, bool symmetric) const override
Return sparsity of Jacobian of an output respect to an input.
Sparsity get_sparsity_in(casadi_int i) override
Sparsities of function inputs and outputs.
CallbackInternal(const std::string &name, Callback *self)
Constructor.
User-defined Function implemented by subclassing.
Internal class for Function.
void init(const Dict &opts) override
Initialize.
void finalize() override
Finalize the object creation.
virtual int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const
Evaluate numerically.
size_t n_in_
Number of inputs and outputs.
casadi_int nnz_in() const
Number of input/output nonzeros.
casadi_int nnz_out() const
Number of input/output nonzeros.
void setup(void *mem, const double **arg, double **res, casadi_int *iw, double *w) const
Set the (persistent and temporary) work vectors.
Function object.
Definition: function.hpp:126
std::map< std::string, std::vector< std::string > > AuxOut
Definition: function.hpp:1546
void clear_mem()
Clear all memory (called from destructor)
General sparsity class.
Definition: sparsity.hpp:200
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.