Skip to content
Snippets Groups Projects
Commit e7da47a7 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add a few missing assertions

Note that some of them were already set by Kokkos (in debug mode), but
it seems better to only rely on `pugs` definitions
parent 96e80be0
No related branches found
No related tags found
1 merge request!43Feature/glace boundary conditions
...@@ -28,14 +28,16 @@ class SparseMatrixDescriptor ...@@ -28,14 +28,16 @@ class SparseMatrixDescriptor
return m_id_value_map.size(); return m_id_value_map.size();
} }
const DataType& operator[](const IndexType& j) const const DataType&
operator[](const IndexType& j) const
{ {
auto i_value = m_id_value_map.find(j); auto i_value = m_id_value_map.find(j);
Assert(i_value != m_id_value_map.end()); Assert(i_value != m_id_value_map.end());
return i_value->second; return i_value->second;
} }
DataType& operator[](const IndexType& j) DataType&
operator[](const IndexType& j)
{ {
auto i_value = m_id_value_map.find(j); auto i_value = m_id_value_map.find(j);
if (i_value != m_id_value_map.end()) { if (i_value != m_id_value_map.end()) {
...@@ -51,7 +53,7 @@ class SparseMatrixDescriptor ...@@ -51,7 +53,7 @@ class SparseMatrixDescriptor
operator<<(std::ostream& os, const SparseRowDescriptor& row) operator<<(std::ostream& os, const SparseRowDescriptor& row)
{ {
for (auto [j, value] : row.m_id_value_map) { for (auto [j, value] : row.m_id_value_map) {
os << ' ' << j << ':' << value; os << ' ' << static_cast<size_t>(j) << ':' << value;
} }
return os; return os;
} }
...@@ -73,24 +75,30 @@ class SparseMatrixDescriptor ...@@ -73,24 +75,30 @@ class SparseMatrixDescriptor
SparseRowDescriptor& SparseRowDescriptor&
row(const IndexType i) row(const IndexType i)
{ {
Assert(i < m_row_array.size());
return m_row_array[i]; return m_row_array[i];
} }
const SparseRowDescriptor& const SparseRowDescriptor&
row(const IndexType i) const row(const IndexType i) const
{ {
Assert(i < m_row_array.size());
return m_row_array[i]; return m_row_array[i];
} }
DataType& DataType&
operator()(const IndexType& i, const IndexType& j) operator()(const IndexType& i, const IndexType& j)
{ {
Assert(i < m_row_array.size());
Assert(j < m_row_array.size());
return m_row_array[i][j]; return m_row_array[i][j];
} }
const DataType& const DataType&
operator()(const IndexType& i, const IndexType& j) const operator()(const IndexType& i, const IndexType& j) const
{ {
Assert(i < m_row_array.size());
Assert(j < m_row_array.size());
const auto& r = m_row_array[i]; // split to ensure const-ness of call const auto& r = m_row_array[i]; // split to ensure const-ness of call
return r[j]; return r[j];
} }
...@@ -99,7 +107,7 @@ class SparseMatrixDescriptor ...@@ -99,7 +107,7 @@ class SparseMatrixDescriptor
operator<<(std::ostream& os, const SparseMatrixDescriptor& M) operator<<(std::ostream& os, const SparseMatrixDescriptor& M)
{ {
for (IndexType i = 0; i < M.m_row_array.size(); ++i) { for (IndexType i = 0; i < M.m_row_array.size(); ++i) {
os << i << " |" << M.m_row_array[i] << '\n'; os << static_cast<size_t>(i) << " |" << M.m_row_array[i] << '\n';
} }
return os; return os;
} }
......
...@@ -29,8 +29,10 @@ class ItemToItemMatrix ...@@ -29,8 +29,10 @@ class ItemToItemMatrix
} }
PUGS_INLINE PUGS_INLINE
TargetItemId operator[](size_t j) const TargetItemId
operator[](size_t j) const
{ {
Assert(j < m_row.length);
return m_row(j); return m_row(j);
} }
...@@ -73,15 +75,19 @@ class ItemToItemMatrix ...@@ -73,15 +75,19 @@ class ItemToItemMatrix
} }
PUGS_INLINE PUGS_INLINE
auto operator[](const SourceItemId& source_id) const auto
operator[](const SourceItemId& source_id) const
{ {
Assert(source_id < m_connectivity_matrix.numRows());
using RowType = decltype(m_connectivity_matrix.rowConst(source_id)); using RowType = decltype(m_connectivity_matrix.rowConst(source_id));
return SubItemList<RowType>(m_connectivity_matrix.rowConst(source_id)); return SubItemList<RowType>(m_connectivity_matrix.rowConst(source_id));
} }
template <typename IndexType> template <typename IndexType>
PUGS_INLINE const auto& operator[](const IndexType& source_id) const PUGS_INLINE const auto&
operator[](const IndexType& source_id) const
{ {
Assert(source_id < m_connectivity_matrix.numRows());
static_assert(std::is_same_v<IndexType, SourceItemId>, "ItemToItemMatrix must be indexed using correct ItemId"); static_assert(std::is_same_v<IndexType, SourceItemId>, "ItemToItemMatrix must be indexed using correct ItemId");
using RowType = decltype(m_connectivity_matrix.rowConst(source_id)); using RowType = decltype(m_connectivity_matrix.rowConst(source_id));
return SubItemList<RowType>(m_connectivity_matrix.rowConst(source_id)); return SubItemList<RowType>(m_connectivity_matrix.rowConst(source_id));
......
...@@ -168,4 +168,30 @@ TEST_CASE("SparseMatrixDescriptor", "[algebra]") ...@@ -168,4 +168,30 @@ TEST_CASE("SparseMatrixDescriptor", "[algebra]")
REQUIRE(value_array[6] == 1); REQUIRE(value_array[6] == 1);
REQUIRE(value_array[7] == -2); REQUIRE(value_array[7] == -2);
} }
SECTION("output")
{
SparseMatrixDescriptor<int, uint8_t> S{5};
S(0, 2) = 3;
S(1, 2) = 11;
S(1, 1) = 1;
S(0, 2) += 2;
S(3, 3) = 5;
S(3, 1) = -3;
S(4, 1) = 1;
S(2, 2) = 4;
S(4, 4) = -2;
std::ostringstream output;
output << '\n' << S;
std::string expected_output = R"(
0 | 2:5
1 | 1:1 2:11
2 | 2:4
3 | 1:-3 3:5
4 | 1:1 4:-2
)";
REQUIRE(output.str() == expected_output);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment