linsol_internal.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_LINSOL_INTERNAL_HPP
27 #define CASADI_LINSOL_INTERNAL_HPP
28 
29 #include "linsol.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 
34 
35 namespace casadi {
36 
37  struct CASADI_EXPORT LinsolMemory : public ProtoFunctionMemory {
38  // Current state of factorization
39  bool is_sfact, is_nfact;
40 
41  // Constructor
42  LinsolMemory() : is_sfact(false), is_nfact(false) {}
43  };
44 
48  class CASADI_EXPORT LinsolInternal
49  : public ProtoFunction, public PluginInterface<LinsolInternal> {
50  public:
52  LinsolInternal(const std::string& name, const Sparsity& sp);
53 
55  ~LinsolInternal() override;
56 
60  void disp(std::ostream& stream, bool more) const override;
61 
65  virtual void disp_more(std::ostream& stream) const {}
66 
68  void init(const Dict& opts) override;
69 
73  void* alloc_mem() const override { return new LinsolMemory();}
74 
78  int init_mem(void* mem) const override;
79 
83  void free_mem(void *mem) const override { delete static_cast<LinsolMemory*>(mem);}
84 
86  virtual void linsol_eval_sx(const SXElem** arg, SXElem** res,
87  casadi_int* iw, SXElem* w, void* mem,
88  bool tr, casadi_int nrhs) const;
89 
90 #if 0
91  // (Re)factorize the system
92  casadi_int factorize(void* mem, const double* A) const;
93 
94  // Needs symbolic factorization
95  virtual bool needs_sfact(void* mem, const double* A) const;
96 
97  // Needs numeric factorization
98  virtual bool needs_nfact(void* mem, const double* A) const;
99 #endif
100 
101  // Symbolic factorization
102  virtual int sfact(void* mem, const double* A) const { return 0;}
103 
105  virtual int nfact(void* mem, const double* A) const;
106 
107  // Solve numerically
108  virtual int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const;
109 
111  virtual casadi_int neig(void* mem, const double* A) const;
112 
114  virtual casadi_int rank(void* mem, const double* A) const;
115 
117  virtual void generate(CodeGenerator& g, const std::string& A, const std::string& x,
118  casadi_int nrhs, bool tr) const;
119 
120  // Creator function for internal class
121  typedef LinsolInternal* (*Creator)(const std::string& name, const Sparsity& sp);
122 
123  // No static functions exposed
124  struct Exposed{ };
125 
127  static std::map<std::string, Plugin> solvers_;
128 
130  static const std::string infix_;
131 
132  // Get name of the plugin
133  const char* plugin_name() const override = 0;
134 
136  casadi_int nrow() const { return sp_.size1();}
137  casadi_int ncol() const { return sp_.size2();}
138  const casadi_int* colind() const { return sp_.colind();}
139  const casadi_int* row() const { return sp_.row();}
140  casadi_int nnz() const { return sp_.nnz();}
141 
145  void serialize_type(SerializingStream &s) const override;
149  void serialize_body(SerializingStream &s) const override;
150 
154  static ProtoFunction* deserialize(DeserializingStream& s);
155 
156  // Sparsity pattern of the linear system
157  Sparsity sp_;
158 
159  protected:
163  explicit LinsolInternal(DeserializingStream& s);
164  };
165 
166 } // namespace casadi
168 
169 #endif // CASADI_LINSOL_INTERNAL_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.