Documentation 6b54b78464
Loading...
Searching...
No Matches
CPyWrapperMatrixView.hpp
Go to the documentation of this file.
1/*!
2 * \file CPyWrapperMatrixView.hpp
3 * \brief Simple matrix views to use with the python wrapper.
4 * \author P. Gomes
5 * \version 8.5.0 "Harrier"
6 *
7 * SU2 Project Website: https://su2code.github.io
8 *
9 * The SU2 Project is maintained by the SU2 Foundation
10 * (http://su2foundation.org)
11 *
12 * Copyright 2012-2026, SU2 Contributors (cf. AUTHORS.md)
13 *
14 * SU2 is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * SU2 is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with SU2. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28#pragma once
29
30#include <string>
31#include <utility>
32#include <vector>
33
34#include "../geometry/dual_grid/CVertex.hpp"
35#include "../parallelization/mpi_structure.hpp"
36#include "C2DContainer.hpp"
37
38/*!
39 * \brief Python wrapper matrix interface
40 * The accessors in this macro provide a passive double interface to matrix of
41 * su2double. This can be extended to allow access to derivative information.
42 * Classes that use this macro encapsulate the access to the raw data (su2double)
43 * via the functions "Access(row, col) -> su2double&" (const and non-const versions).
44 * We use a macro because it is diffult to use modern C++ idioms (e.g. CRTP) with
45 * SWIG. In addition to "Access" classes must have member variables "rows_", "cols_",
46 * and "read_only_".
47 */
48#define PY_WRAPPER_MATRIX_INTERFACE \
49 /*! \brief Returns the shape of the matrix. */ \
50 std::pair<unsigned long, unsigned long> Shape() const { return std::make_pair(rows_, cols_); } \
51 \
52 /*! \brief Returns whether the data is read-only [true] or if it can be modified [false]. */ \
53 bool IsReadOnly() const { return read_only_; } \
54 \
55 /*! \brief Gets the value for a (row, column) pair. */ \
56 passivedouble operator()(unsigned long row, unsigned long col) const { return Get(row, col); } \
57 \
58 /*! \brief Gets the values for a row of the matrix. */ \
59 std::vector<passivedouble> operator()(unsigned long row) const { return Get(row); } \
60 \
61 /*! \brief Gets the value for a (row, column) pair. */ \
62 passivedouble Get(unsigned long row, unsigned long col) const { return SU2_TYPE::GetValue(Access(row, col)); } \
63 \
64 /*! \brief Gets the values for a row of the matrix. */ \
65 std::vector<passivedouble> Get(unsigned long row) const { \
66 std::vector<passivedouble> vals(cols_); \
67 for (unsigned long j = 0; j < cols_; ++j) vals[j] = Get(row, j); \
68 return vals; \
69 } \
70 /*! \brief Sets the value for a (row, column) pair. This clears derivative information. */ \
71 void Set(unsigned long row, unsigned long col, passivedouble val) { Access(row, col) = val; } \
72 \
73 /*! \brief Sets the values for a row of the matrix. */ \
74 void Set(unsigned long row, std::vector<passivedouble> vals) { \
75 unsigned long j = 0; \
76 for (const auto& val : vals) Set(row, j++, val); \
77 }
78
79/*!
80 * \class CPyWrapperMatrixView
81 * \ingroup PySU2
82 * \brief This class wraps su2activematrix for the python wrapper matrix interface.
83 * It is generaly used to wrap access to solver variables defined for the entire volume.
84 */
86 protected:
87 static_assert(su2activematrix::IsRowMajor);
88 su2double* data_ = nullptr;
89 unsigned long rows_ = 0, cols_ = 0;
90 std::string name_;
91 bool read_only_ = false;
92
93 /*--- Define the functions required by the interface macro. ---*/
94 inline const su2double& Access(unsigned long row, unsigned long col) const {
95 if (row > rows_ || col > cols_) SU2_MPI::Error(name_ + " out of bounds", "CPyWrapperMatrixView");
96 return data_[row * cols_ + col];
97 }
98 inline su2double& Access(unsigned long row, unsigned long col) {
99 if (read_only_) SU2_MPI::Error(name_ + " is read-only", "CPyWrapperMatrixView");
100 const auto& const_me = *this;
101 return const_cast<su2double&>(const_me.Access(row, col));
102 }
103
104 public:
106
107 /*!
108 * \brief Construct the view of the matrix.
109 * \note "name" should be set to the variable name being returned to give better information to users.
110 * \note "read_only" can be set to true to prevent the data from being modified.
111 */
112 CPyWrapperMatrixView(su2activematrix& mat, const std::string& name, bool read_only)
113 : data_(mat.data()), rows_(mat.rows()), cols_(mat.cols()), name_(name), read_only_(read_only) {}
114
115 /*--- Use the macro to generate the interface. ---*/
117};
118
119/*!
120 * \class CPyWrapperMarkerMatrixView
121 * \ingroup PySU2
122 * \brief This class wraps su2activematrix for the python wrapper matrix interface restricting it
123 * to the vertices of a given marker.
124 */
126 private:
127 static_assert(su2activematrix::IsRowMajor);
128 su2double* data_ = nullptr;
129 const CVertex* const* vertices_ = nullptr;
130 unsigned long rows_ = 0, cols_ = 0;
131 std::string name_;
132 bool read_only_ = false;
133
134 /*--- Define the functions required by the interface macro. ---*/
135 inline const su2double& Access(unsigned long row, unsigned long col) const {
136 if (row > rows_ || col > cols_) SU2_MPI::Error(name_ + " out of bounds", "CPyWrapperMarkerMatrixView");
137 return data_[vertices_[row]->GetNode() * cols_ + col];
138 }
139 inline su2double& Access(unsigned long row, unsigned long col) {
140 if (read_only_) SU2_MPI::Error(name_ + " is read-only", "CPyWrapperMarkerMatrixView");
141 const auto& const_me = *this;
142 return const_cast<su2double&>(const_me.Access(row, col));
143 }
144
145 public:
147
148 /*!
149 * \brief Construct the view of the matrix.
150 * \note "name" should be set to the variable name being returned to give better information to users.
151 * \note "read_only" can be set to true to prevent the data from being modified.
152 */
153 CPyWrapperMarkerMatrixView(su2activematrix& mat, const CVertex* const* vertices, unsigned long n_vertices,
154 const std::string& name, bool read_only)
155 : data_(mat.data()),
156 vertices_(vertices),
157 rows_(n_vertices),
158 cols_(mat.cols()),
159 name_(name),
160 read_only_(read_only) {
161 if (mat.rows() < n_vertices) {
162 SU2_MPI::Error(name + " has fewer rows than the number of vertices in the marker.", "CPyWrapperMarkerMatrixView");
163 }
164 }
165
166 /*--- Use the macro to generate the interface. ---*/
168};
169
170/*!
171 * \class CPyWrapper3DMatrixView
172 * \ingroup PySU2
173 * \brief This class wraps C3DDoubleMatrix for the python wrapper matrix interface.
174 * It is generaly used to wrap access to solver gradients defined for the entire volume.
175 */
177 protected:
178 static_assert(su2activematrix::IsRowMajor);
179 su2double* data_ = nullptr;
180 unsigned long rows_ = 0, cols_ = 0, dims_ = 0;
181 std::string name_;
182 bool read_only_ = false;
183
184 /*--- Define the functions required by the interface macro. ---*/
185 inline const su2double& Access(unsigned long row, unsigned long col, unsigned long dim) const {
186 if (row > rows_ || col > cols_ || dim > dims_) SU2_MPI::Error(name_ + " out of bounds", "CPyWrapper3DMatrixView");
187 return data_[row * (cols_ * dims_) + col * dims_ + dim];
188 }
189 inline su2double& Access(unsigned long row, unsigned long col, unsigned long dim) {
190 if (read_only_) SU2_MPI::Error(name_ + " is read-only", "CPyWrapper3DMatrixView");
191 const auto& const_me = *this;
192 return const_cast<su2double&>(const_me.Access(row, col, dim));
193 }
194
195 public:
197
198 /*!
199 * \brief Construct the view of the matrix.
200 * \note "name" should be set to the variable name being returned to give better information to users.
201 * \note "read_only" can be set to true to prevent the data from being modified.
202 */
203 CPyWrapper3DMatrixView(C3DDoubleMatrix& mat, const std::string& name, bool read_only)
204 : data_(mat.data()),
205 rows_(mat.length()),
206 cols_(mat.rows()),
207 dims_(mat.cols()),
208 name_(name),
209 read_only_(read_only) {}
210
211 /*! \brief Returns the shape of the matrix. */
212 std::vector<unsigned long> Shape() const { return {rows_, cols_, dims_}; }
213
214 /*! \brief Returns whether the data is read-only [true] or if it can be modified [false]. */
215 bool IsReadOnly() const { return read_only_; }
216
217 /*! \brief Gets the value for a (row, column, dimension) triplet. */
218 passivedouble operator()(unsigned long row, unsigned long col, unsigned long dim) const { return Get(row, col, dim); }
219
220 /*! \brief Gets the values for a row and column of the matrix. */
221 std::vector<passivedouble> operator()(unsigned long row, unsigned long col) const { return Get(row, col); }
222
223 /*! \brief Gets the value for a (row, column, dimension) triplet. */
224 passivedouble Get(unsigned long row, unsigned long col, unsigned long dim) const {
225 return SU2_TYPE::GetValue(Access(row, col, dim));
226 }
227
228 /*! \brief Gets the values for a row and column of the matrix. */
229 std::vector<passivedouble> Get(unsigned long row, unsigned long col) const {
230 std::vector<passivedouble> vals(dims_);
231 for (unsigned long j = 0; j < dims_; ++j) vals[j] = Get(row, col, j);
232 return vals;
233 }
234 /*! \brief Sets the value for a (row, column, dimension) triplet. This clears derivative information. */
235 void Set(unsigned long row, unsigned long col, unsigned long dim, passivedouble val) { Access(row, col, dim) = val; }
236
237 /*! \brief Sets the values for a row and column of the matrix. */
238 void Set(unsigned long row, unsigned long col, std::vector<passivedouble> vals) {
239 unsigned long j = 0;
240 for (const auto& val : vals) Set(row, col, j++, val);
241 }
242};
A templated vector/matrix object.
#define PY_WRAPPER_MATRIX_INTERFACE
Python wrapper matrix interface The accessors in this macro provide a passive double interface to mat...
Definition CPyWrapperMatrixView.hpp:48
A templated matrix/vector-like object.
Definition C2DContainer.hpp:374
static constexpr bool IsRowMajor
Definition C2DContainer.hpp:391
Index_t rows() const noexcept
Definition C2DContainer.hpp:222
static void Error(const std::string &ErrorMsg, const std::string &FunctionName)
Definition mpi_structure.cpp:131
This class wraps C3DDoubleMatrix for the python wrapper matrix interface. It is generaly used to wrap...
Definition CPyWrapperMatrixView.hpp:176
unsigned long cols_
Definition CPyWrapperMatrixView.hpp:180
std::string name_
Definition CPyWrapperMatrixView.hpp:181
CPyWrapper3DMatrixView(C3DDoubleMatrix &mat, const std::string &name, bool read_only)
Construct the view of the matrix.
Definition CPyWrapperMatrixView.hpp:203
unsigned long dims_
Definition CPyWrapperMatrixView.hpp:180
unsigned long rows_
Definition CPyWrapperMatrixView.hpp:180
std::vector< passivedouble > operator()(unsigned long row, unsigned long col) const
Gets the values for a row and column of the matrix.
Definition CPyWrapperMatrixView.hpp:221
void Set(unsigned long row, unsigned long col, unsigned long dim, passivedouble val)
Sets the value for a (row, column, dimension) triplet. This clears derivative information.
Definition CPyWrapperMatrixView.hpp:235
su2double * data_
Definition CPyWrapperMatrixView.hpp:179
passivedouble operator()(unsigned long row, unsigned long col, unsigned long dim) const
Gets the value for a (row, column, dimension) triplet.
Definition CPyWrapperMatrixView.hpp:218
bool read_only_
Definition CPyWrapperMatrixView.hpp:182
void Set(unsigned long row, unsigned long col, std::vector< passivedouble > vals)
Sets the values for a row and column of the matrix.
Definition CPyWrapperMatrixView.hpp:238
std::vector< unsigned long > Shape() const
Returns the shape of the matrix.
Definition CPyWrapperMatrixView.hpp:212
passivedouble Get(unsigned long row, unsigned long col, unsigned long dim) const
Gets the value for a (row, column, dimension) triplet.
Definition CPyWrapperMatrixView.hpp:224
bool IsReadOnly() const
Returns whether the data is read-only [true] or if it can be modified [false].
Definition CPyWrapperMatrixView.hpp:215
const su2double & Access(unsigned long row, unsigned long col, unsigned long dim) const
Definition CPyWrapperMatrixView.hpp:185
CPyWrapper3DMatrixView()=default
std::vector< passivedouble > Get(unsigned long row, unsigned long col) const
Gets the values for a row and column of the matrix.
Definition CPyWrapperMatrixView.hpp:229
su2double & Access(unsigned long row, unsigned long col, unsigned long dim)
Definition CPyWrapperMatrixView.hpp:189
This class wraps su2activematrix for the python wrapper matrix interface restricting it to the vertic...
Definition CPyWrapperMatrixView.hpp:125
CPyWrapperMarkerMatrixView(su2activematrix &mat, const CVertex *const *vertices, unsigned long n_vertices, const std::string &name, bool read_only)
Construct the view of the matrix.
Definition CPyWrapperMatrixView.hpp:153
CPyWrapperMarkerMatrixView()=default
This class wraps su2activematrix for the python wrapper matrix interface. It is generaly used to wrap...
Definition CPyWrapperMatrixView.hpp:85
su2double * data_
Definition CPyWrapperMatrixView.hpp:88
CPyWrapperMatrixView(su2activematrix &mat, const std::string &name, bool read_only)
Construct the view of the matrix.
Definition CPyWrapperMatrixView.hpp:112
CPyWrapperMatrixView()=default
unsigned long rows_
Definition CPyWrapperMatrixView.hpp:89
bool read_only_
Definition CPyWrapperMatrixView.hpp:91
std::string name_
Definition CPyWrapperMatrixView.hpp:90
const su2double & Access(unsigned long row, unsigned long col) const
Definition CPyWrapperMatrixView.hpp:94
su2double & Access(unsigned long row, unsigned long col)
Definition CPyWrapperMatrixView.hpp:98
unsigned long cols_
Definition CPyWrapperMatrixView.hpp:89
Class for vertex definition (equivalent to edges, but for the boundaries).
Definition CVertex.hpp:38
unsigned long GetNode() const
Get the node of the vertex.
Definition CVertex.hpp:70
double passivedouble
Definition code_config.hpp:129
passivedouble su2double
Definition code_config.hpp:181
passivedouble GetValue(const su2double &data)
Get the (primitive) value of the datatype (needs to be specialized for active types).
Definition datatype_structure.hpp:113