Skip to content

Commit

Permalink
Merge pull request #352 from GitPaean/adding_cartesian_to_compressed
Browse files Browse the repository at this point in the history
adding mapping cartesianToCompressed
  • Loading branch information
bska authored Dec 19, 2018
2 parents 2ec5099 + d6c5290 commit 06296f8
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ list (APPEND MAIN_SOURCE_FILES
opm/grid/UnstructuredGrid.c
opm/grid/grid_equal.cpp
opm/grid/utility/compressedToCartesian.cpp
opm/grid/utility/cartesianToCompressed.cpp
opm/grid/utility/StopWatch.cpp
opm/grid/utility/WachspressCoord.cpp
)
Expand Down Expand Up @@ -100,6 +101,7 @@ list (APPEND TEST_SOURCE_FILES
tests/test_repairzcorn.cpp
tests/test_sparsetable.cpp
tests/test_quadratures.cpp
tests/test_compressed_cartesian_mapping.cpp
)

if(HAVE_ECL_INPUT)
Expand Down Expand Up @@ -204,6 +206,7 @@ list (APPEND PUBLIC_HEADER_FILES
opm/grid/transmissibility/TransTpfa_impl.hpp
opm/grid/utility/CompressedPropertyAccess.hpp
opm/grid/utility/compressedToCartesian.hpp
opm/grid/utility/cartesianToCompressed.hpp
opm/grid/utility/extractPvtTableIndex.hpp
opm/grid/utility/RegionMapping.hpp
opm/grid/utility/SparseTable.hpp
Expand Down
53 changes: 53 additions & 0 deletions opm/grid/utility/cartesianToCompressed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2018 SINTEF Digital, Mathematics & Cybernetics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/


#include <opm/grid/utility/cartesianToCompressed.hpp>

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});
}
}
return retval;
}


} // namespace Opm
41 changes: 41 additions & 0 deletions opm/grid/utility/cartesianToCompressed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2018 SINTEF Digital, Mathematics & Cybernetics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OPM_CARTESIANTOCOMPRESSED_HEADER_INCLUDED
#define OPM_CARTESIANTOCOMPRESSED_HEADER_INCLUDED

#include <unordered_map>

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);

} // namespace Opm

#endif // OPM_CARTESIANTOCOMPRESSED_HEADER_INCLUDED
71 changes: 71 additions & 0 deletions tests/test_compressed_cartesian_mapping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2018 SINTEF ICT, Applied Mathematics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#define BOOST_TEST_MODULE mappingTest

#include <boost/test/unit_test.hpp>

#include <opm/grid/utility/compressedToCartesian.hpp>
#include <opm/grid/utility/cartesianToCompressed.hpp>

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();

const std::unordered_map<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);

const int non_existing_index = 1829;
BOOST_CHECK_THROW(cartesian_to_compressed.at(non_existing_index), std::out_of_range);

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(nullmapping)
{
const int num_cells = 30;

const std::unordered_map<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);
}

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

for (int i = 0; i < num_cells; ++i) {
BOOST_CHECK_EQUAL(compressed_to_cartesian[i], i);
}
}

0 comments on commit 06296f8

Please sign in to comment.