Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
tensor.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_DATASTRUCTURE_TENSOR_HPP_
24#define VULPES_DATASTRUCTURE_TENSOR_HPP_
25
26#include <type_traits>
27#include <vector>
28
29namespace vulpes {
30namespace datastructure {
31
54template <
55 bool Triangular,
56 bool Tensor,
57 typename DataType,
58 typename IndexType,
59 typename StorageType = std::vector<DataType>>
60class TriMatrix {
61 public:
75 template <
76 typename T = void,
77 typename = std::enable_if_t<
78 !Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
79 T>>
80 explicit TriMatrix(IndexType tri_dim)
81 : m_tri_dim(tri_dim),
82 m_tensor_size(1ULL),
84 Triangular ? (tri_dim + 1) * tri_dim / 2 : tri_dim * tri_dim),
86 m_data(m_tri_size * m_tensor_size, DataType(0)) {}
87
96 explicit TriMatrix(const StorageType& data, IndexType tri_dim)
97 : m_tri_dim(tri_dim),
98 m_tensor_size(1ULL),
100 Triangular ? (tri_dim + 1) * tri_dim / 2 : tri_dim * tri_dim),
102 m_data(data) {}
103
116 template <
117 typename T = void,
118 typename = std::enable_if_t<
119 Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
120 T>>
121 explicit TriMatrix(IndexType tri_dim, IndexType tensor_size)
122 : m_tri_dim(tri_dim),
123 m_tensor_size(tensor_size),
125 Triangular ? (tri_dim + 1) * tri_dim / 2 : tri_dim * tri_dim),
127 m_data(m_tri_size * tensor_size, DataType(0)) {}
128
143 template <typename T = void, typename = std::enable_if_t<!Tensor, T>>
144 const DataType& operator()(IndexType i, IndexType j) const {
145 return m_data[ij(i, j)];
146 }
147
162 template <typename T = void, typename = std::enable_if_t<Tensor, T>>
163 const DataType& operator()(IndexType i, IndexType j, IndexType k) const {
164 return m_data[ijk(i, j, k)];
165 }
166
180 template <
181 typename T = void,
182 typename = std::enable_if_t<
183 !Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
184 T>>
185 DataType& operator()(IndexType i, IndexType j) {
186 return m_data[ij(i, j)];
187 }
188
203 template <typename T = void, typename = std::enable_if_t<Tensor, T>>
204 DataType& operator()(IndexType i, IndexType j, IndexType k) {
205 return m_data[ijk(i, j, k)];
206 }
207
212 IndexType dim() const { return m_tri_dim; }
216 IndexType tdim() const { return m_tensor_size; }
220 IndexType numel() const { return m_numel; }
221
225 auto begin() { return &m_data[0]; }
229 auto end() { return &m_data[0] + numel(); }
233 auto begin() const { return &m_data[0]; }
237 auto end() const { return &m_data[0] + numel(); }
238
239 protected:
241 IndexType m_tri_dim;
243 IndexType m_tensor_size;
245 IndexType m_tri_size;
247 IndexType m_numel;
249 StorageType m_data;
250
261 IndexType ij(IndexType i, IndexType j) const {
262 if constexpr (Triangular) {
263 return (i + j) * (i + j + 1) / 2 + j;
264 } else {
265 return i * m_tri_dim + j;
266 }
267 }
268
278 IndexType ijk(IndexType i, IndexType j, IndexType k) const {
279 return ij(i, j) + k * m_tri_size;
280 }
281};
282
301template <
302 bool Triangular,
303 bool Tensor,
304 typename DataType,
305 typename IndexType,
306 typename StorageType = std::vector<DataType>>
308 public:
316 template <
317 typename T = void,
318 typename = std::enable_if_t<
319 !Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
320 T>>
321 explicit TetraTensor(IndexType tri_dim)
322 : m_tri_dim(tri_dim),
323 m_tensor_size(1ULL),
325 Triangular ? (tri_dim + 2) * (tri_dim + 1) * tri_dim / 6
326 : tri_dim * tri_dim * tri_dim),
329
338 explicit TetraTensor(const StorageType& data, IndexType tri_dim)
339 : m_tri_dim(tri_dim),
340 m_tensor_size(1ULL),
342 Triangular ? (tri_dim + 2) * (tri_dim + 1) * tri_dim / 6
343 : tri_dim * tri_dim * tri_dim),
345 m_data(data) {}
346
355 template <
356 typename T = void,
357 typename = std::enable_if_t<
358 Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
359 T>>
360 explicit TetraTensor(IndexType tri_dim, IndexType tensor_size)
361 : m_tri_dim(tri_dim),
362 m_tensor_size(tensor_size),
364 Triangular ? (tri_dim + 2) * (tri_dim + 1) * tri_dim / 6
365 : tri_dim * tri_dim * tri_dim),
368
377 template <typename T = void, typename = std::enable_if_t<!Tensor, T>>
378 const DataType& operator()(IndexType i, IndexType j, IndexType k) const {
379 return m_data[ijk(i, j, k)];
380 }
381
392 template <typename T = void, typename = std::enable_if_t<Tensor, T>>
393 const DataType& operator()(
394 IndexType i, IndexType j, IndexType k, IndexType l) const {
395 return m_data[ijkl(i, j, k, l)];
396 }
397
406 template <
407 typename T = void,
408 typename = std::enable_if_t<
409 !Tensor && std::is_same_v<StorageType, std::vector<DataType>>,
410 T>>
411 DataType& operator()(IndexType i, IndexType j, IndexType k) {
412 return m_data[ijk(i, j, k)];
413 }
414
424 template <typename T = void, typename = std::enable_if_t<Tensor, T>>
425 DataType& operator()(IndexType i, IndexType j, IndexType k, IndexType l) {
426 return m_data[ijkl(i, j, k, l)];
427 }
428
433 IndexType dim() const { return m_tri_dim; }
437 IndexType tdim() const { return m_tensor_size; }
441 IndexType numel() const { return m_numel; }
442
446 auto begin() { return &m_data[0]; }
450 auto end() { return &m_data[0] + numel(); }
454 auto begin() const { return &m_data[0]; }
458 auto end() const { return &m_data[0] + numel(); }
459
460 protected:
462 IndexType m_tri_dim;
464 IndexType m_tensor_size;
466 IndexType m_tri_size;
468 IndexType m_numel;
470 StorageType m_data;
471
481 IndexType ijk(IndexType i, IndexType j, IndexType k) const {
482 if constexpr (Triangular) {
483 const IndexType j_k = j + k;
484 const IndexType i_j_k = i + j_k;
485 return i_j_k * (i_j_k + 1) * (i_j_k + 2) / 6 + j_k * (j_k + 1) / 2 + k;
486 } else {
487 return (k * m_tri_dim + i) * m_tri_dim + j;
488 }
489 }
490
501 IndexType ijkl(IndexType i, IndexType j, IndexType k, IndexType l) const {
502 return ijk(i, j, k) + l * m_tri_size;
503 }
504};
505
506template <std::size_t Dim, bool Simplex, typename DataType, typename IndexType>
507using SimplexDataView = std::conditional_t<
508 Dim == 2,
510 std::conditional_t<
511 Dim == 3,
513 const DataType*>>;
514
515} // namespace datastructure
516} // namespace vulpes
517
518#endif // VULPES_DATASTRUCTURE_TENSOR_HPP_
A class that represents a tetrahedral matrix or tensor, with optional support for higher-dimensional ...
Definition tensor.hpp:307
IndexType ijkl(IndexType i, IndexType j, IndexType k, IndexType l) const
Helper function to compute the flattened index for a 4D tensor matrix.
Definition tensor.hpp:501
IndexType dim() const
Dimension n of the matrix (or one slice of the tensor).
Definition tensor.hpp:433
const DataType & operator()(IndexType i, IndexType j, IndexType k) const
Accessor for non-tensor tetrahedral or cubic matrix (const version).
Definition tensor.hpp:378
TetraTensor(IndexType tri_dim)
Constructor for a non-tensor tetrahedral or cubic matrix.
Definition tensor.hpp:321
IndexType tdim() const
Dimension along the tensor dimension.
Definition tensor.hpp:437
auto begin() const
Const begin iterator.
Definition tensor.hpp:454
std::vector< FloatType > m_data
Definition tensor.hpp:470
TetraTensor(IndexType tri_dim, IndexType tensor_size)
Constructor for a tensor-like tetrahedral or cubic matrix.
Definition tensor.hpp:360
DataType & operator()(IndexType i, IndexType j, IndexType k)
Mutator for non-tensor tetrahedral or cubic matrix.
Definition tensor.hpp:411
auto end()
End iterator.
Definition tensor.hpp:450
const DataType & operator()(IndexType i, IndexType j, IndexType k, IndexType l) const
Accessor for tensor-like tetrahedral or cubic matrix (const version).
Definition tensor.hpp:393
DataType & operator()(IndexType i, IndexType j, IndexType k, IndexType l)
Mutator for tensor-like tetrahedral or cubic matrix.
Definition tensor.hpp:425
auto begin()
Begin iterator.
Definition tensor.hpp:446
IndexType ijk(IndexType i, IndexType j, IndexType k) const
Helper function to compute the flattened index for a 3D tetrahedral or cubic matrix.
Definition tensor.hpp:481
auto end() const
Const end iterator.
Definition tensor.hpp:458
TetraTensor(const StorageType &data, IndexType tri_dim)
Copy-constructor (almost).
Definition tensor.hpp:338
IndexType numel() const
Total number of elements.
Definition tensor.hpp:441
A class that represents a triangular or square matrix, with optional support for tensor-like multi-di...
Definition tensor.hpp:60
IndexType dim() const
Dimension n of the matrix (or one slice of the tensor).
Definition tensor.hpp:212
auto begin()
Begin iterator.
Definition tensor.hpp:225
DataType & operator()(IndexType i, IndexType j, IndexType k)
Mutator for tensor matrices.
Definition tensor.hpp:204
IndexType tdim() const
Dimension along the tensor dimension.
Definition tensor.hpp:216
DataType & operator()(IndexType i, IndexType j)
Mutator for non-tensor matrices.
Definition tensor.hpp:185
TriMatrix(IndexType tri_dim)
Constructor for a non-tensor matrix.
Definition tensor.hpp:80
TriMatrix(const StorageType &data, IndexType tri_dim)
Copy-constructor (almost).
Definition tensor.hpp:96
auto begin() const
Const begin iterator.
Definition tensor.hpp:233
IndexType numel() const
Total number of elements.
Definition tensor.hpp:220
IndexType ij(IndexType i, IndexType j) const
Helper function to compute the flattened index for a 2D matrix.
Definition tensor.hpp:261
const DataType & operator()(IndexType i, IndexType j) const
Accessor for non-tensor matrices (const version).
Definition tensor.hpp:144
std::vector< FloatType > m_data
Definition tensor.hpp:249
auto end()
End iterator.
Definition tensor.hpp:229
auto end() const
Const end iterator.
Definition tensor.hpp:237
IndexType ijk(IndexType i, IndexType j, IndexType k) const
Helper function to compute the flattened index for a 3D tensor matrix.
Definition tensor.hpp:278
TriMatrix(IndexType tri_dim, IndexType tensor_size)
Constructor for a tensor-like matrix.
Definition tensor.hpp:121
const DataType & operator()(IndexType i, IndexType j, IndexType k) const
Accessor for tensor matrices (const version).
Definition tensor.hpp:163
Definition crs_map.hpp:36
std::conditional_t< Dim==2, TriMatrix< Simplex, false, DataType, IndexType, const DataType * >, std::conditional_t< Dim==3, TetraTensor< Simplex, false, DataType, IndexType, const DataType * >, const DataType * > > SimplexDataView
Definition tensor.hpp:507
@ Tensor
Definition fe_space.hpp:48
Definition cell_manager.hpp:38