idas_interface.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_IDAS_INTERFACE_HPP
27 #define CASADI_IDAS_INTERFACE_HPP
28 
29 #include <casadi/interfaces/sundials/casadi_integrator_idas_export.h>
30 #include "sundials_interface.hpp"
31 #include <idas/idas.h> /* prototypes for CVODE fcts. and consts. */
32 #include <idas/idas_dense.h>
33 #include <idas/idas_band.h>
34 #include <idas/idas_spgmr.h>
35 #include <idas/idas_spbcgs.h>
36 #include <idas/idas_sptfqmr.h>
37 #include <idas/idas_impl.h> /* Needed for the provided linear solver */
38 #include <ctime>
39 
50 
51 namespace casadi {
52 
53 // Forward declaration
54 class IdasInterface;
55 
56 // IdasMemory
57 struct IdasMemory : public SundialsMemory {
59  const IdasInterface& self;
60 
62  void* mem;
63 
65  int whichB;
66 
68  double cj_last;
69 
71  IdasMemory(const IdasInterface& s);
72 
74  ~IdasMemory();
75 };
76 
85 class IdasInterface : public SundialsInterface {
86  public:
87 
89  IdasInterface(const std::string& name, const Function& dae,
90  double t0, const std::vector<double>& tout);
91 
93  static Integrator* creator(const std::string& name, const Function& dae,
94  double t0, const std::vector<double>& tout) {
95  return new IdasInterface(name, dae, t0, tout);
96  }
97 
99  ~IdasInterface() override;
100 
101  // Get name of the plugin
102  const char* plugin_name() const override { return "idas";}
103 
104  // Get name of the class
105  std::string class_name() const override { return "IdasInterface";}
106 
108 
109  static const Options options_;
110  const Options& get_options() const override { return options_;}
112 
114  void init(const Dict& opts) override;
115 
117  void* alloc_mem() const override { return new IdasMemory(*this);}
118 
120  int init_mem(void* mem) const override;
121 
123  void free_mem(void *mem) const override { delete static_cast<IdasMemory*>(mem);}
124 
126  void reset(IntegratorMemory* mem,
127  const double* u, const double* x, const double* z, const double* p) const override;
128 
130  void advance(IntegratorMemory* mem,
131  const double* u, double* x, double* z, double* q) const override;
132 
134  void resetB(IntegratorMemory* mem) const override;
135 
137  void z_impulseB(IdasMemory* m, const double* rz) const;
138 
140  int solve_transposed(IdasMemory* m, double t, const double* xz, const double* rxz,
141  const double* rhs, double* sol) const;
142 
144  void impulseB(IntegratorMemory* mem,
145  const double* rx, const double* rz, const double* rp) const override;
146 
148  void retreat(IntegratorMemory* mem, const double* u,
149  double* rx, double* rq, double* uq) const override;
150 
152  static IdasMemory* to_mem(void *mem) {
153  IdasMemory* m = static_cast<IdasMemory*>(mem);
154  casadi_assert_dev(m);
155  return m;
156  }
157 
159  static const std::string meta_doc;
160 
161  protected:
162 
163  // Sundials callback functions
164  static int resF(double t, N_Vector xz, N_Vector xzdot, N_Vector rr, void *user_data);
165  static int resB(double t, N_Vector xz, N_Vector xzdot, N_Vector rxz, N_Vector rxzdot,
166  N_Vector rr, void *user_data);
167  static void ehfun(int error_code, const char *module, const char *function, char *msg,
168  void *eh_data);
169  static int jtimesF(double t, N_Vector xz, N_Vector xzdot, N_Vector rr, N_Vector v,
170  N_Vector Jv, double cj, void *user_data, N_Vector tmp1, N_Vector tmp2);
171  static int jtimesB(double t, N_Vector xz, N_Vector xzdot, N_Vector xzB, N_Vector xzdotB,
172  N_Vector resvalB, N_Vector vB, N_Vector JvB, double cjB,
173  void *user_data, N_Vector tmp1B, N_Vector tmp2B);
174  static int rhsQF(double t, N_Vector xz, N_Vector xzdot, N_Vector qdot, void *user_data);
175  static int rhsQB(double t, N_Vector xz, N_Vector xzdot, N_Vector rxz,
176  N_Vector rxzdot, N_Vector ruqdot, void *user_data);
177  static int psolveF(double t, N_Vector xz, N_Vector xzdot, N_Vector rr, N_Vector rvec,
178  N_Vector zvec, double cj, double delta, void *user_data, N_Vector tmp);
179  static int psetupF(double t, N_Vector xz, N_Vector xzdot, N_Vector rr, double cj,
180  void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
181  static int psolveB(double t, N_Vector xz, N_Vector xzdot, N_Vector rxz, N_Vector rxzdot,
182  N_Vector resvalB, N_Vector rvecB, N_Vector zvecB, double cjB,
183  double deltaB, void *user_dataB, N_Vector tmpB);
184  static int psetupB(double t, N_Vector xz, N_Vector xzdot, N_Vector rxz, N_Vector rxzdot,
185  N_Vector resvalB, double cjB, void *user_dataB,
186  N_Vector tmp1B, N_Vector tmp2B, N_Vector tmp3B);
187  static int lsetupF(IDAMem IDA_mem, N_Vector xz, N_Vector xzdot, N_Vector resp,
188  N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3);
189  static int lsolveF(IDAMem IDA_mem, N_Vector b, N_Vector weight, N_Vector ycur,
190  N_Vector xzdotcur, N_Vector rescur);
191  static int lsetupB(IDAMem IDA_mem, N_Vector xz, N_Vector xzdot, N_Vector resp,
192  N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3);
193  static int lsolveB(IDAMem IDA_mem, N_Vector b, N_Vector weight, N_Vector ycur,
194  N_Vector xzdotcur, N_Vector rescur);
195  public:
196 
197  // Throw error
198  static void idas_error(const char* module, int flag);
199 
200  // Options
201  bool cj_scaling_;
202  bool calc_ic_;
203  bool calc_icB_;
204  bool suppress_algebraic_;
205  std::vector<double> abstolv_;
206  double first_time_;
207 
208  // Constraints on decision variables
209  std::vector<casadi_int> y_c_;
210 
211  // Initial values for \p xdot
212  std::vector<double> init_xdot_;
213 
215  void serialize_body(SerializingStream &s) const override;
216 
218  static ProtoFunction* deserialize(DeserializingStream& s) { return new IdasInterface(s); }
219 
220  protected:
222  explicit IdasInterface(DeserializingStream& s);
223 };
224 
225 } // namespace casadi
226 
228 #endif // CASADI_IDAS_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.