ma27_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_MA27_INTERFACE_HPP
27 #define CASADI_MA27_INTERFACE_HPP
28 
29 #include "casadi/core/linsol_internal.hpp"
30 #include <casadi/interfaces/hsl/casadi_linsol_ma27_export.h>
31 
32 extern "C" {
33  void ma27id_(int* ICNTL, double* CNTL);
34  void ma27ad_(int *N, int *NZ, const int *IRN, const int* ICN,
35  int *IW, int* LIW, int* IKEEP, int *IW1,
36  int* NSTEPS, int* IFLAG, int* ICNTL,
37  double* CNTL, int *INFO, double* OPS);
38  void ma27bd_(int *N, int *NZ, const int *IRN, const int* ICN,
39  double* A, int* LA, int* IW, int* LIW,
40  int* IKEEP, int* NSTEPS, int* MAXFRT,
41  int* IW1, int* ICNTL, double* CNTL,
42  int* INFO);
43  void ma27cd_(int *N, double* A, int* LA, int* IW,
44  int* LIW, double* W, int* MAXFRT,
45  double* RHS, int* IW1, int* NSTEPS,
46  int* ICNTL, double* CNTL);
47 }
48 
62 namespace casadi {
63  struct CASADI_LINSOL_MA27_EXPORT Ma27Memory : public LinsolMemory {
64  // Constructor
65  Ma27Memory();
66 
67  // Destructor
68  ~Ma27Memory();
69 
70  /* Work vector for MA27AD */
71  std::vector<int> iw1;
72 
73  /* Number of nonzeros in the current linear system. */
74  int nnz;
75 
76  /* matrix/factor for MA27 (A in MA27) */
77  std::vector<double> nz;
78 
79  /* Row entries of matrix (IRN in MA27) */
80  std::vector<int> irn;
81 
82  /* Column entries of matrix (JCN in MA27) */
83  std::vector<int> jcn;
84 
85  /* integer control values (ICNRL in MA27) */
86  int icntl[30];
87 
88  /* real control values (CNRL in MA27) */
89  double cntl[5];
90 
91  /* integer work space (IW in MA27) */
92  std::vector<int> iw;
93 
94  /* Real work space (W in MA27CD) */
95  std::vector<double> w;
96 
97  /* IKEEP in MA27 */
98  std::vector<int> ikeep;
99 
100  /* NSTEPS in MA27 */
101  int nsteps;
102 
103  /* MAXFRT in MA27 */
104  int maxfrt;
105 
106  /* number of negative eigenvalues */
107  int neig;
108 
109  // Rank of matrix
110  int rank;
111  };
112 
117  class CASADI_LINSOL_MA27_EXPORT Ma27Interface : public LinsolInternal {
118  public:
119 
120  // Create a linear solver given a sparsity pattern and a number of right hand sides
121  Ma27Interface(const std::string& name, const Sparsity& sp);
122 
124  static LinsolInternal* creator(const std::string& name, const Sparsity& sp) {
125  return new Ma27Interface(name, sp);
126  }
127 
128  // Destructor
129  ~Ma27Interface() override;
130 
131  // Initialize the solver
132  void init(const Dict& opts) override;
133 
135  void* alloc_mem() const override { return new Ma27Memory();}
136 
138  int init_mem(void* mem) const override;
139 
141  void free_mem(void *mem) const override { delete static_cast<Ma27Memory*>(mem);}
142 
143  // Factorize the linear system
144  int nfact(void* mem, const double* A) const override;
145 
147  casadi_int neig(void* mem, const double* A) const override;
148 
150  casadi_int rank(void* mem, const double* A) const override;
151 
152  // Solve the linear system
153  int solve(void* mem, const double* A, double* x, casadi_int nrhs, bool tr) const override;
154 
156  static const std::string meta_doc;
157 
158  // Get name of the plugin
159  const char* plugin_name() const override { return "ma27";}
160 
161  // Get name of the class
162  std::string class_name() const override { return "Ma27Interface";}
163 
165  static ProtoFunction* deserialize(DeserializingStream& s) { return new Ma27Interface(s); }
166 
167  protected:
169  explicit Ma27Interface(DeserializingStream& s) : LinsolInternal(s) {}
170  };
171 
172 } // namespace casadi
173 
175 
176 #endif // CASADI_MA27_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.