Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cartesianToCompressed changed to unordered multimap #687

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.emplace(global_cell[i], i);
}
} else {
for (int i = 0; i < num_cells; ++i) {
retval.emplace(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
96 changes: 84 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,96 @@
#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());


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);
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 +128,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