callback.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 
30  }
31 
32  Callback::Callback(const Callback& obj) : Function() { // NOLINT
33  casadi_error("Callback objects cannot be copied");
34  }
35 
36  void Callback::construct(const std::string& name, const Dict& opts) {
37  if (!is_null()) {
38  casadi_error("Cannot create '" + name + "': Internal class already created");
39  }
40  own(new CallbackInternal(name, this));
41  (*this)->construct(opts);
42  }
43 
45  try {
46  // Make sure that this object isn't used after its deletion
47  if (!is_null()) get<CallbackInternal>()->self_ = nullptr;
48  } catch (...) {
49  casadi_warning("Problem in Callback destructor");
50  }
51  }
52 
53  int Callback::eval_buffer(const double **arg, const std::vector<casadi_int>& sizes_arg,
54  double **res, const std::vector<casadi_int>& sizes_res) const {
55  casadi_error("eval_buffer not overloaded.");
56  }
58  return false;
59  }
60  std::vector<DM> Callback::eval(const std::vector<DM>& arg) const {
61  return (*this)->FunctionInternal::eval_dm(arg);
62  }
63 
64  casadi_int Callback::get_n_in() {
65  return (*this)->FunctionInternal::get_n_in();
66  }
67 
68  casadi_int Callback::get_n_out() {
69  return (*this)->FunctionInternal::get_n_out();
70  }
71 
73  return (*this)->FunctionInternal::get_sparsity_in(i);
74  }
75 
77  return (*this)->FunctionInternal::get_sparsity_out(i);
78  }
79 
80  std::string Callback::get_name_in(casadi_int i) {
81  return (*this)->FunctionInternal::get_name_in(i);
82  }
83 
84  std::string Callback::get_name_out(casadi_int i) {
85  return (*this)->FunctionInternal::get_name_out(i);
86  }
87 
88  bool Callback::uses_output() const {
89  return (*this)->FunctionInternal::uses_output();
90  }
91 
92  bool Callback::has_jacobian() const {
93  return (*this)->FunctionInternal::has_jacobian();
94  }
95 
97  get_jacobian(const std::string& name,
98  const std::vector<std::string>& inames,
99  const std::vector<std::string>& onames,
100  const Dict& opts) const {
101  return (*this)->FunctionInternal::get_jacobian(name, inames, onames, opts);
102  }
103 
105  get_forward(casadi_int nfwd, const std::string& name,
106  const std::vector<std::string>& inames,
107  const std::vector<std::string>& onames,
108  const Dict& opts) const {
109  return (*this)->FunctionInternal::get_forward(nfwd, name, inames, onames, opts);
110  }
111 
112  bool Callback::has_forward(casadi_int nfwd) const {
113  return (*this)->FunctionInternal::has_forward(nfwd);
114  }
115 
117  get_reverse(casadi_int nadj, const std::string& name,
118  const std::vector<std::string>& inames,
119  const std::vector<std::string>& onames,
120  const Dict& opts) const {
121  return (*this)->FunctionInternal::get_reverse(nadj, name, inames, onames, opts);
122  }
123 
124  bool Callback::has_reverse(casadi_int nadj) const {
125  return (*this)->FunctionInternal::has_reverse(nadj);
126  }
127 
128 } // namespace casadi
Callback function functionality.
virtual 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
Return function that calculates forward derivatives.
Definition: callback.cpp:105
virtual Sparsity get_sparsity_in(casadi_int i)
Get the sparsity of an input.
Definition: callback.cpp:72
void construct(const std::string &name, const Dict &opts=Dict())
Construct internal object.
Definition: callback.cpp:36
virtual std::string get_name_out(casadi_int i)
Get the name of an output.
Definition: callback.cpp:84
virtual bool has_eval_buffer() const
Does the Callback class support a copy-free low level interface ?
Definition: callback.cpp:57
virtual 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
Return function that calculates adjoint derivatives.
Definition: callback.cpp:117
virtual std::string get_name_in(casadi_int i)
Get the name of an input.
Definition: callback.cpp:80
Callback()
Default constructor.
Definition: callback.cpp:29
virtual bool has_jacobian() const
Return Jacobian of all input elements with respect to all output elements.
Definition: callback.cpp:92
virtual ~Callback()
Destructor.
Definition: callback.cpp:44
virtual bool has_reverse(casadi_int nadj) const
Return function that calculates adjoint derivatives.
Definition: callback.cpp:124
virtual casadi_int get_n_out()
Get the number of outputs.
Definition: callback.cpp:68
virtual bool has_forward(casadi_int nfwd) const
Return function that calculates forward derivatives.
Definition: callback.cpp:112
virtual int eval_buffer(const double **arg, const std::vector< casadi_int > &sizes_arg, double **res, const std::vector< casadi_int > &sizes_res) const
A copy-free low level interface.
Definition: callback.cpp:53
virtual std::vector< DM > eval(const std::vector< DM > &arg) const
Evaluate numerically, using temporary matrices and work vectors.
Definition: callback.cpp:60
virtual bool uses_output() const
Do the derivative functions need nondifferentiated outputs?
Definition: callback.cpp:88
virtual Sparsity get_sparsity_out(casadi_int i)
Get the sparsity of an output.
Definition: callback.cpp:76
virtual casadi_int get_n_in()
Get the number of inputs.
Definition: callback.cpp:64
virtual Function get_jacobian(const std::string &name, const std::vector< std::string > &inames, const std::vector< std::string > &onames, const Dict &opts) const
Return Jacobian of all input elements with respect to all output elements.
Definition: callback.cpp:97
Function object.
Definition: function.hpp:60
const std::string & name() const
Name of the function.
Definition: function.cpp:1307
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.