rootfinder_impl.hpp
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 
26 #ifndef CASADI_ROOTFINDER_IMPL_HPP
27 #define CASADI_ROOTFINDER_IMPL_HPP
28 
29 #include "rootfinder.hpp"
30 #include "oracle_function.hpp"
31 #include "plugin_interface.hpp"
32 
34 namespace casadi {
35 
39  struct CASADI_EXPORT RootfinderMemory : public OracleMemory {
40  // Inputs
41  const double** iarg;
42 
43  // Outputs
44  double** ires;
45 
46  // Success?
47  bool success;
48 
49  // Return status
50  UnifiedReturnStatus unified_return_status;
51  };
52 
54  class CASADI_EXPORT
55  Rootfinder : public OracleFunction, public PluginInterface<Rootfinder> {
56  public:
62  Rootfinder(const std::string& name, const Function& oracle);
63 
65  ~Rootfinder() override = 0;
66 
68 
71  size_t get_n_in() override { return oracle_.n_in();}
72  size_t get_n_out() override { return oracle_.n_out();}
74 
76 
79  Sparsity get_sparsity_in(casadi_int i) override { return oracle_.sparsity_in(i);}
80  Sparsity get_sparsity_out(casadi_int i) override { return oracle_.sparsity_out(i);}
82 
84 
87  std::string get_name_in(casadi_int i) override { return oracle_.name_in(i);}
88  std::string get_name_out(casadi_int i) override { return oracle_.name_out(i);}
90 
92 
95  static const Options options_;
96  const Options& get_options() const override { return options_;}
98 
100  void init(const Dict& opts) override;
101 
105  int init_mem(void* mem) const override;
106 
110  void set_work(void* mem, const double**& arg, double**& res,
111  casadi_int*& iw, double*& w) const override;
112 
113  // Evaluate numerically
114  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const override;
115 
116  // Solve the NLP
117  virtual int solve(void* mem) const = 0;
118 
120  Dict get_stats(void* mem) const override;
121 
125  int sp_forward(const bvec_t** arg, bvec_t** res,
126  casadi_int* iw, bvec_t* w, void* mem) const override;
127 
131  int sp_reverse(bvec_t** arg, bvec_t** res, casadi_int* iw, bvec_t* w, void* mem) const override;
132 
135  bool has_spfwd() const override { return true;}
136  bool has_sprev() const override { return true;}
138 
142  bool uses_output() const override {return true;}
143 
145 
148  bool has_forward(casadi_int nfwd) const override { return true;}
149  Function get_forward(casadi_int nfwd, const std::string& name,
150  const std::vector<std::string>& inames,
151  const std::vector<std::string>& onames,
152  const Dict& opts) const override;
154 
156 
159  bool has_reverse(casadi_int nadj) const override { return true;}
160  Function get_reverse(casadi_int nadj, const std::string& name,
161  const std::vector<std::string>& inames,
162  const std::vector<std::string>& onames,
163  const Dict& opts) const override;
165 
169  virtual void ad_forward(const std::vector<MX>& arg, const std::vector<MX>& res,
170  const std::vector<std::vector<MX> >& fseed,
171  std::vector<std::vector<MX> >& fsens,
172  bool always_inline, bool never_inline) const;
173 
177  virtual void ad_reverse(const std::vector<MX>& arg, const std::vector<MX>& res,
178  const std::vector<std::vector<MX> >& aseed,
179  std::vector<std::vector<MX> >& asens,
180  bool always_inline, bool never_inline) const;
181 
183  casadi_int n_;
184 
186  Linsol linsol_;
187  Sparsity sp_jac_;
188 
190  std::vector<casadi_int> u_c_;
191 
193  casadi_int iin_, iout_;
194 
195  // Creator function for internal class
196  typedef Rootfinder* (*Creator)(const std::string& name, const Function& oracle);
197 
198  // No static functions exposed
199  struct Exposed{ };
200 
202  static std::map<std::string, Plugin> solvers_;
203 
205  static std::string shortname() { return "rootfinder";}
206 
208  static const std::string infix_;
209 
211  template<typename XType>
212  static Function create_oracle(const std::map<std::string, XType>& d,
213  const Dict& opts);
214 
218  void serialize_body(SerializingStream &s) const override;
222  void serialize_type(SerializingStream &s) const override;
223 
227  static ProtoFunction* deserialize(DeserializingStream& s);
228 
232  std::string serialize_base_function() const override { return "Rootfinder"; }
233 
234  protected:
238  explicit Rootfinder(DeserializingStream& s);
239  };
240 
241 
242 
243 } // namespace casadi
245 
246 #endif // CASADI_ROOTFINDER_IMPL_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
UnifiedReturnStatus