From e820eadfebbabd2ac431233b2c58926a7a107931 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Mon, 26 May 2014 07:50:25 +0200 Subject: [PATCH] Added Box() class to book keep the indices in a box. --- opm/parser/eclipse/CMakeLists.txt | 4 +- opm/parser/eclipse/EclipseState/Grid/Box.cpp | 103 ++++++++++++++++++ opm/parser/eclipse/EclipseState/Grid/Box.hpp | 49 +++++++++ .../EclipseState/Grid/tests/BoxTests.cpp | 60 ++++++++++ .../EclipseState/Grid/tests/CMakeLists.txt | 5 + 5 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 opm/parser/eclipse/EclipseState/Grid/Box.cpp create mode 100644 opm/parser/eclipse/EclipseState/Grid/Box.hpp create mode 100644 opm/parser/eclipse/EclipseState/Grid/tests/BoxTests.cpp diff --git a/opm/parser/eclipse/CMakeLists.txt b/opm/parser/eclipse/CMakeLists.txt index 735c2a3b8..c37397f81 100644 --- a/opm/parser/eclipse/CMakeLists.txt +++ b/opm/parser/eclipse/CMakeLists.txt @@ -73,8 +73,9 @@ EclipseState/Schedule/CompletionSet.cpp EclipseState/Schedule/ScheduleEnums.cpp EclipseState/Schedule/GroupTreeNode.cpp EclipseState/Schedule/GroupTree.cpp -EclipseState/Grid/GridProperty.cpp # +EclipseState/Grid/GridProperty.cpp +EclipseState/Grid/Box.cpp EclipseState/Grid/EclipseGrid.cpp) set (utility_source @@ -132,6 +133,7 @@ EclipseState/Schedule/OrderedMap.hpp EclipseState/Grid/EclipseGrid.hpp EclipseState/Grid/GridProperty.hpp EclipseState/Grid/GridProperties.hpp +EclipseState/Grid/Box.hpp # Utility/WconinjeWrapper.hpp Utility/CompdatWrapper.hpp diff --git a/opm/parser/eclipse/EclipseState/Grid/Box.cpp b/opm/parser/eclipse/EclipseState/Grid/Box.cpp new file mode 100644 index 000000000..ec5c89a8e --- /dev/null +++ b/opm/parser/eclipse/EclipseState/Grid/Box.cpp @@ -0,0 +1,103 @@ +/* + Copyright 2014 Statoil ASA. + + 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 . +*/ + +#include + +#include + +namespace Opm { + + Box::Box(int nx , int ny , int nz) { + if (nx <= 0) + throw std::invalid_argument("The input nx value is invalid"); + + if (ny <= 0) + throw std::invalid_argument("The input ny value is invalid"); + + if (nz <= 0) + throw std::invalid_argument("The input nz value is invalid"); + + m_dims[0] = (size_t) nx; + m_dims[1] = (size_t) ny; + m_dims[2] = (size_t) nz; + + m_offset = {0,0,0}; + + m_stride[0] = 1; + m_stride[1] = m_dims[0]; + m_stride[2] = m_dims[0] * m_dims[1]; + + m_isGlobal = true; + } + + + size_t Box::size() const { + return m_dims[0] * m_dims[1] * m_dims[2]; + } + + + bool Box::isGlobal() const { + return m_isGlobal; + } + + + size_t Box::getDim(size_t idim) const { + if (idim >= 3) + throw std::invalid_argument("The input dimension value is invalid"); + + return m_dims[idim]; + } + + + const std::vector& Box::getIndexList() { + assertIndexList(); + return m_indexList; + } + + + void Box::assertIndexList() { + if (m_indexList.size() > 0) + return; + + { + m_indexList.resize( size() ); + + size_t ii,ij,ik; + size_t l = 0; + + for (ik=0; ik < m_dims[2]; ik++) { + size_t k = ik + m_offset[2]; + for (ij=0; ij < m_dims[1]; ij++) { + size_t j = ij + m_offset[1]; + for (ii=0; ii < m_dims[0]; ii++) { + size_t i = ii + m_offset[0]; + size_t g = i * m_stride[0] + j*m_stride[1] + k*m_stride[2]; + + m_indexList[l] = g; + l++; + } + } + } + } + } + + + +} + diff --git a/opm/parser/eclipse/EclipseState/Grid/Box.hpp b/opm/parser/eclipse/EclipseState/Grid/Box.hpp new file mode 100644 index 000000000..61de7317d --- /dev/null +++ b/opm/parser/eclipse/EclipseState/Grid/Box.hpp @@ -0,0 +1,49 @@ +/* + Copyright 2014 Statoil ASA. + + 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 . +*/ + + +#ifndef BOX_HPP_ +#define BOX_HPP_ + +#include + + +namespace Opm { + + class Box { + public: + Box(int nx , int ny , int nz); + size_t size() const; + bool isGlobal() const; + size_t getDim(size_t idim) const; + const std::vector& getIndexList(); + + private: + void assertIndexList(); + + size_t m_dims[3]; + size_t m_offset[3]; + size_t m_stride[3]; + bool m_isGlobal; + std::vector m_indexList; + }; +} + + +#endif diff --git a/opm/parser/eclipse/EclipseState/Grid/tests/BoxTests.cpp b/opm/parser/eclipse/EclipseState/Grid/tests/BoxTests.cpp new file mode 100644 index 000000000..f352e7845 --- /dev/null +++ b/opm/parser/eclipse/EclipseState/Grid/tests/BoxTests.cpp @@ -0,0 +1,60 @@ +/* + Copyright 2013 Statoil ASA. + + 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 . + */ + +#include +#include +#include + +#define BOOST_TEST_MODULE EclipseGridTests +#include +#include + +#include + +BOOST_AUTO_TEST_CASE(CreateBox) { + BOOST_CHECK_THROW( new Opm::Box(-1,0,0) , std::invalid_argument); + BOOST_CHECK_THROW( new Opm::Box(10,0,10) , std::invalid_argument); + BOOST_CHECK_THROW( new Opm::Box(10,10,-1) , std::invalid_argument); + + Opm::Box box(4,3,2); + BOOST_CHECK_EQUAL( 24U , box.size() ); + BOOST_CHECK( box.isGlobal() ); + BOOST_CHECK_EQUAL( 4 , box.getDim(0) ); + BOOST_CHECK_EQUAL( 3 , box.getDim(1) ); + BOOST_CHECK_EQUAL( 2 , box.getDim(2) ); + + BOOST_CHECK_THROW( box.getDim(5) , std::invalid_argument); + + + { + size_t i,j,k; + const std::vector& indexList = box.getIndexList(); + + for (k=0; k < box.getDim(2); k++) { + for (j=0; j < box.getDim(1); j++) { + for (i=0; i < box.getDim(0); i++) { + size_t g = i + j*box.getDim(0) + k*box.getDim(0)*box.getDim(1); + BOOST_CHECK_EQUAL( indexList[g] , g); + + } + } + } + } + +} diff --git a/opm/parser/eclipse/EclipseState/Grid/tests/CMakeLists.txt b/opm/parser/eclipse/EclipseState/Grid/tests/CMakeLists.txt index e91952e43..6151d5386 100644 --- a/opm/parser/eclipse/EclipseState/Grid/tests/CMakeLists.txt +++ b/opm/parser/eclipse/EclipseState/Grid/tests/CMakeLists.txt @@ -12,3 +12,8 @@ add_executable(runGridPropertiesTests GridPropertiesTests.cpp) target_link_libraries(runGridPropertiesTests Parser ${Boost_LIBRARIES}) add_test(NAME runGridPropertiesTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${TEST_MEMCHECK_TOOL} ${EXECUTABLE_OUTPUT_PATH}/runGridPropertiesTests ) + +add_executable(runBoxTests BoxTests.cpp) +target_link_libraries(runBoxTests Parser ${Boost_LIBRARIES}) +add_test(NAME runBoxTests WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND ${TEST_MEMCHECK_TOOL} ${EXECUTABLE_OUTPUT_PATH}/runBoxTests ) +