Skip to content

Commit 22bb669

Browse files
authored
Merge pull request #1312 from su2code/gradient_type
Another charge against pointer to pointer
2 parents 56b6080 + 0ad7a02 commit 22bb669

45 files changed

Lines changed: 406 additions & 717 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Common/include/containers/container_decorators.hpp

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@
3030

3131
#include "C2DContainer.hpp"
3232

33+
/*!
34+
* \brief Class to represent a matrix (without owning the data, this just wraps a pointer).
35+
*/
36+
template<class T>
37+
class CMatrixView {
38+
public:
39+
using Scalar = typename std::remove_const<T>::type;
40+
using Index = unsigned long;
41+
42+
private:
43+
T* m_ptr;
44+
Index m_cols;
45+
46+
public:
47+
CMatrixView(T* ptr = nullptr, Index cols = 0) : m_ptr(ptr), m_cols(cols) {}
48+
49+
template<class U> friend class CMatrixView;
50+
template<class U>
51+
CMatrixView(const CMatrixView<U>& other) : m_ptr(other.m_ptr), m_cols(other.m_cols) {}
52+
53+
explicit CMatrixView(su2matrix<Scalar>& mat) : m_ptr(mat.data()), m_cols(mat.cols()) {}
54+
55+
template<class U = T, su2enable_if<std::is_const<U>::value> = 0>
56+
explicit CMatrixView(const su2matrix<Scalar>& mat) : m_ptr(mat.data()), m_cols(mat.cols()) {}
57+
58+
const Scalar* operator[] (Index i) const noexcept { return &m_ptr[i*m_cols]; }
59+
const Scalar& operator() (Index i, Index j) const noexcept { return m_ptr[i*m_cols + j]; }
60+
61+
template<class U = T, su2enable_if<!std::is_const<U>::value> = 0>
62+
Scalar* operator[] (Index i) noexcept { return &m_ptr[i*m_cols]; }
63+
64+
template<class U = T, su2enable_if<!std::is_const<U>::value> = 0>
65+
Scalar& operator() (Index i, Index j) noexcept { return m_ptr[i*m_cols + j]; }
66+
67+
friend CMatrixView operator+ (CMatrixView mv, Index incr) { return CMatrixView(mv[incr], mv.m_cols); }
68+
};
69+
3370
/*!
3471
* \class C3DContainerDecorator
3572
* \brief Decorate a matrix type (Storage) with 3 dimensions.
@@ -44,6 +81,9 @@ class C3DContainerDecorator {
4481
static constexpr bool IsRowMajor = true;
4582
static constexpr bool IsColumnMajor = false;
4683

84+
using Matrix = CMatrixView<Scalar>;
85+
using ConstMatrix = CMatrixView<const Scalar>;
86+
4787
using CInnerIter = typename Storage::CInnerIter;
4888
template<class T, size_t N>
4989
using CInnerIterGather = typename Storage::template CInnerIterGather<simd::Array<T,N> >;
@@ -78,6 +118,18 @@ class C3DContainerDecorator {
78118
Scalar& operator() (Index i, Index j, Index k) noexcept { return m_storage(i, j*m_innerSz + k); }
79119
const Scalar& operator() (Index i, Index j, Index k) const noexcept { return m_storage(i, j*m_innerSz + k); }
80120

121+
/*!
122+
* \brief Matrix access.
123+
*/
124+
Matrix operator[] (Index i) noexcept { return Matrix(m_storage[i], m_innerSz); }
125+
ConstMatrix operator[] (Index i) const noexcept { return ConstMatrix(m_storage[i], m_innerSz); }
126+
127+
/*!
128+
* \brief Matrix access with an offset.
129+
*/
130+
Matrix operator() (Index i, Index j) noexcept { return Matrix(m_storage[i]+j*m_innerSz, m_innerSz); }
131+
ConstMatrix operator() (Index i, Index j) const noexcept { return ConstMatrix(m_storage[i]+j*m_innerSz, m_innerSz); }
132+
81133
/*!
82134
* \brief Get a scalar iterator to the inner-most dimension of the container.
83135
*/
@@ -105,42 +157,11 @@ class C3DContainerDecorator {
105157
};
106158

107159
/*!
108-
* \brief Some typedefs for the
160+
* \brief Some typedefs for 3D containers
109161
*/
110162
using C3DIntMatrix = C3DContainerDecorator<su2matrix<unsigned long> >;
111163
using C3DDoubleMatrix = C3DContainerDecorator<su2activematrix>;
112-
113-
/*!
114-
* \class CVectorOfMatrix
115-
* \brief This contrived container is used to store small matrices in a contiguous manner
116-
* but still present the "su2double**" interface to the outside world.
117-
* The "interface" part should be replaced by something more efficient, e.g. a "matrix view".
118-
*/
119-
class CVectorOfMatrix: public C3DDoubleMatrix {
120-
private:
121-
su2matrix<Scalar*> interface;
122-
123-
public:
124-
CVectorOfMatrix() = default;
125-
126-
CVectorOfMatrix(Index length, Index rows, Index cols, Scalar value = 0) noexcept {
127-
resize(length, rows, cols, value);
128-
}
129-
130-
void resize(Index length, Index rows, Index cols, Scalar value = 0) noexcept {
131-
C3DDoubleMatrix::resize(length, rows, cols, value);
132-
interface.resize(length,rows);
133-
for(Index i=0; i<length; ++i)
134-
for(Index j=0; j<rows; ++j)
135-
interface(i,j) = &(*this)(i,j,0);
136-
}
137-
138-
/*!
139-
* \brief Matrix-wise access.
140-
*/
141-
Scalar** operator[] (Index i) noexcept { return interface[i]; }
142-
const Scalar* const* operator[] (Index i) const noexcept { return interface[i]; }
143-
};
164+
using CVectorOfMatrix = C3DDoubleMatrix;
144165

145166
/*!
146167
* \class C2DDummyLastView

Common/include/geometry/dual_grid/CPoint.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ class CPoint {
809809
* \param[in] iPoint - Index of the point.
810810
* \return Grid velocity gradient at the point.
811811
*/
812-
inline const su2double* const* GetGridVel_Grad(unsigned long iPoint) const { return GridVel_Grad[iPoint]; }
812+
inline CMatrixView<const su2double> GetGridVel_Grad(unsigned long iPoint) const { return GridVel_Grad[iPoint]; }
813813

814814
/*!
815815
* \brief Set the adjoint values of the (geometric) coordinates.

0 commit comments

Comments
 (0)