Skip to content

Commit

Permalink
cartesianToCompressed changed to unordered multimap
Browse files Browse the repository at this point in the history
  • Loading branch information
aritorto committed Nov 7, 2023
1 parent a165a07 commit 94769bd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 45 deletions.
46 changes: 23 additions & 23 deletions opm/grid/utility/cartesianToCompressed.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2018 SINTEF Digital, Mathematics & Cybernetics.
Copyright 2023 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
Expand All @@ -23,31 +24,30 @@
namespace Opm
{

// Construct explicit mapping from local cartesian to active/compressed
// indices, either based on global_cell or as { 0, 1, 2, ....} if null.
// \param[in] num_cartesian_cells The number of cartesian cells.
// \param[in] global_cell Either null, or an array of size num_cells.
// \return A unordered_map containing the mapping from local cartesian
// to active/compressed,
// or the map { {0, 0}, {1, 1}, ... , {num_cells - 1, num_cells - 1} }
// if global_cell was null.
std::unordered_map<int, int> cartesianToCompressed(const int num_cells,
const int* global_cell)
{
std::unordered_map<int, int> retval;
retval.max_load_factor(0.9);
retval.reserve(num_cells);
if (global_cell) {
for (int i = 0; i < num_cells; ++i) {
retval.insert({global_cell[i], i});
}
} else {
for (int i = 0; i < num_cells; ++i) {
retval.insert({i, i});
}
// Construct explicit mapping from local cartesian to active/compressed
// indices, either based on global_cell or as { 0, 1, 2, ....} if null.
// \param[in] num_cartesian_cells The number of cartesian cells.
// \param[in] global_cell Either null, or an array of size num_cells.
// \return A unordered_map containing the mapping from local cartesian
// to active/compressed (Attention! When LGRs, one key takes multiple values),
// or the map { {0, 0}, {1, 1}, ... , {num_cells - 1, num_cells - 1} }
// if global_cell was null.
std::unordered_multimap<int, int> cartesianToCompressed(const int num_cells,
const int* global_cell)
{
std::unordered_multimap<int, int> retval;
retval.reserve(num_cells);
if (global_cell) {
for (int i = 0; i < num_cells; ++i) {
retval.insert(std::pair<int,int>(global_cell[i], i));
}
} else {
for (int i = 0; i < num_cells; ++i) {
retval.insert(std::pair<int,int>(i, i));
}
return retval;
}
return retval;
}


} // namespace Opm
22 changes: 12 additions & 10 deletions opm/grid/utility/cartesianToCompressed.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2018 SINTEF Digital, Mathematics & Cybernetics.
Copyright 2023 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
Expand All @@ -21,20 +22,21 @@
#define OPM_CARTESIANTOCOMPRESSED_HEADER_INCLUDED

#include <unordered_map>
#include <utility>

namespace Opm
{

// Construct explicit mapping from local cartesian to active/compressed
// indices, either based on global_cell or as { 0, 1, 2, ....} if null.
// \param[in] num_cartesian_cells The number of cartesian cells.
// \param[in] global_cell Either null, or an array of size num_cells.
// \return A unordered_map containing the mapping from local cartesian
// to active/compressed,
// or the map { {0, 0}, {1, 1}, ... , {num_cells - 1, num_cells - 1} }
// if global_cell was null.
std::unordered_map<int, int> cartesianToCompressed(const int num_cells,
const int* global_cell);
// Construct explicit mapping from local cartesian to active/compressed
// indices, either based on global_cell or as { 0, 1, 2, ....} if null.
// \param[in] num_cartesian_cells The number of cartesian cells.
// \param[in] global_cell Either null, or an array of size num_cells.
// \return A unordered_multimap containing the mapping from local cartesian
// to active/compressed (Attention! When LGRs, one key takes multiple values),
// or the map { {0, 0}, {1, 1}, ... , {num_cells - 1, num_cells - 1} }
// if global_cell was null.
std::unordered_multimap<int, int> cartesianToCompressed(const int num_cells,
const int* global_cell);

} // namespace Opm

Expand Down
107 changes: 95 additions & 12 deletions tests/test_compressed_cartesian_mapping.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2018 SINTEF ICT, Applied Mathematics.
Copyright 2023 Equinor ASA.
This file is part of the Open Porous Media project (OPM).
Expand All @@ -26,25 +27,107 @@
#include <opm/grid/utility/compressedToCartesian.hpp>
#include <opm/grid/utility/cartesianToCompressed.hpp>

#include <unordered_map>
#include <iterator>

BOOST_AUTO_TEST_CASE(mapping)
{
const std::vector<int> global_cell{0, 1, 2, 3, 5, 6, 8, 9};
const int num_cells = global_cell.size(); // 8

const std::unordered_multimap<int, int> cartesian_to_compressed =
Opm::cartesianToCompressed(num_cells, global_cell.data());

for (const auto pair : cartesian_to_compressed)
{
std::cout << pair.first << " " << pair.second << '\n';
}


BOOST_CHECK_EQUAL(cartesian_to_compressed.count(0), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(1), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(2), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(3), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(5), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(6), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(8), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(9), 1);

BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(0), 0);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(1), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(2), 2);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(3), 3);

auto it5 = cartesian_to_compressed.equal_range(5);
BOOST_CHECK_EQUAL(it5.first -> second, 4);
auto it6 = cartesian_to_compressed.equal_range(6);
BOOST_CHECK_EQUAL(it6.first -> second, 5);
auto it8 = cartesian_to_compressed.equal_range(8);
BOOST_CHECK_EQUAL(it8.first -> second, 6);
auto it9 = cartesian_to_compressed.equal_range(9);
BOOST_CHECK_EQUAL(it9.first -> second, 7);

const int non_existing_index = 1829;
auto it = cartesian_to_compressed.equal_range(non_existing_index);
BOOST_CHECK(it.first == it.second);

const std::vector<int> compressed_to_cartesian = Opm::compressedToCartesian(num_cells, global_cell.data());

BOOST_CHECK_EQUAL_COLLECTIONS(compressed_to_cartesian.begin(), compressed_to_cartesian.end(),
global_cell.begin(), global_cell.end());
}

BOOST_AUTO_TEST_CASE(multiMapping)
{
const std::vector<int> global_cell{0, 1, 2, 3,3, 3,3, 3,3, 5, 6,6,6,6, 6,6,6,6, 6,6,6,6, 8, 9}; // 24
const int num_cells = global_cell.size();

const std::unordered_map<int, int> cartesian_to_compressed =
const std::unordered_multimap<int, int> cartesian_to_compressed =
Opm::cartesianToCompressed(num_cells, global_cell.data());

BOOST_CHECK_EQUAL(cartesian_to_compressed.at(0), 0);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(1), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(2), 2);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(3), 3);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(5), 4);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(6), 5);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(8), 6);
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(9), 7);
for (const auto pair : cartesian_to_compressed)
{
std::cout << pair.first << " " << pair.second << '\n';
}

BOOST_CHECK_EQUAL(cartesian_to_compressed.count(0), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(1), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(2), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(3), 6);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(5), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(6), 12);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(8), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.count(9), 1);


BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(0), 0);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(1), 1);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(2), 2);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(3), 3);

auto it3 = cartesian_to_compressed.equal_range(3);
for (auto it = it3.first; it != it3.second; ++it)
{
BOOST_CHECK((it->second > 2) && (it->second < 9)); // it's unordered, so we cannot expect a specific position.
}

auto it5 = cartesian_to_compressed.equal_range(5);
BOOST_CHECK_EQUAL(it5.first -> second, 9);

auto it6 = cartesian_to_compressed.equal_range(6);
for (auto it = it6.first; it != it6.second; ++it)
{
BOOST_CHECK((it->second > 9) && (it->second < 22)); // it's unordered, so we cannot expect a specific position.
}

auto it8 = cartesian_to_compressed.equal_range(8);
BOOST_CHECK_EQUAL(it8.first -> second, 22);
auto it9 = cartesian_to_compressed.equal_range(9);
BOOST_CHECK_EQUAL(it9.first -> second, 23);

const int non_existing_index = 1829;
BOOST_CHECK_THROW(cartesian_to_compressed.at(non_existing_index), std::out_of_range);
auto it = cartesian_to_compressed.equal_range(non_existing_index);
BOOST_CHECK(it.first == it.second);

const std::vector<int> compressed_to_cartesian = Opm::compressedToCartesian(num_cells, global_cell.data());

Expand All @@ -56,11 +139,11 @@ BOOST_AUTO_TEST_CASE(nullmapping)
{
const int num_cells = 30;

const std::unordered_map<int, int> cartesian_to_compressed =
const std::unordered_multimap<int, int> cartesian_to_compressed =
Opm::cartesianToCompressed(num_cells, nullptr);

for (int i = 0; i < num_cells; ++i) {
BOOST_CHECK_EQUAL(cartesian_to_compressed.at(i), i);
BOOST_CHECK_EQUAL(cartesian_to_compressed.bucket(i), i);
}

const std::vector<int> compressed_to_cartesian = Opm::compressedToCartesian(num_cells, nullptr);
Expand Down

0 comments on commit 94769bd

Please sign in to comment.