2013-05-27 14:27:22 +02:00
|
|
|
/*
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
#include <algorithm>
|
2013-08-01 12:50:42 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
2013-06-04 14:32:30 +02:00
|
|
|
#include <opm/parser/eclipse/Deck/Deck.hpp>
|
2016-01-24 21:49:39 +01:00
|
|
|
#include <opm/parser/eclipse/Deck/DeckKeyword.hpp>
|
2016-02-09 12:16:44 +01:00
|
|
|
#include <opm/parser/eclipse/Deck/Section.hpp>
|
2016-01-15 08:42:57 +01:00
|
|
|
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
|
2013-05-27 14:27:22 +02:00
|
|
|
|
|
|
|
|
namespace Opm {
|
|
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
bool DeckView::hasKeyword( const DeckKeyword& keyword ) const {
|
|
|
|
|
auto key = this->keywordMap.find( keyword.name() );
|
2013-05-27 14:27:22 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
if( key == this->keywordMap.end() ) return false;
|
2015-04-13 17:26:26 +02:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
for( auto index : key->second )
|
|
|
|
|
if( &this->getKeyword( index ) == &keyword ) return true;
|
2015-04-13 17:26:26 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
return false;
|
2013-05-30 10:11:12 +02:00
|
|
|
}
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
bool DeckView::hasKeyword( const std::string& keyword ) const {
|
|
|
|
|
return this->keywordMap.find( keyword ) != this->keywordMap.end();
|
|
|
|
|
}
|
2015-04-13 17:05:29 +02:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
const DeckKeyword& DeckView::getKeyword( const std::string& keyword, size_t index ) const {
|
2016-02-09 12:16:44 +01:00
|
|
|
if( !this->hasKeyword( keyword ) )
|
|
|
|
|
throw std::invalid_argument("Keyword " + keyword + " not in deck.");
|
2015-04-13 17:05:29 +02:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
return this->getKeyword( this->offsets( keyword ).at( index ) );
|
2015-04-13 17:26:26 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
const DeckKeyword& DeckView::getKeyword( const std::string& keyword ) const {
|
2016-02-09 12:16:44 +01:00
|
|
|
if( !this->hasKeyword( keyword ) )
|
|
|
|
|
throw std::invalid_argument("Keyword " + keyword + " not in deck.");
|
2015-04-13 17:05:29 +02:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
return this->getKeyword( this->offsets( keyword ).back() );
|
2013-05-27 14:27:22 +02:00
|
|
|
}
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
const DeckKeyword& DeckView::getKeyword( size_t index ) const {
|
2016-02-09 12:16:44 +01:00
|
|
|
if( index >= this->size() )
|
|
|
|
|
throw std::out_of_range("Keyword index " + std::to_string( index ) + " is out of range.");
|
2015-04-13 17:26:26 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
return *( this->begin() + index );
|
2013-06-04 14:32:30 +02:00
|
|
|
}
|
2013-08-11 12:36:16 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
size_t DeckView::count( const std::string& keyword ) const {
|
|
|
|
|
if( !this->hasKeyword( keyword ) ) return 0;
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
return this->offsets( keyword ).size();
|
2016-02-09 12:16:44 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
const std::vector< const DeckKeyword* > DeckView::getKeywordList( const std::string& keyword ) const {
|
2016-03-14 16:02:27 +01:00
|
|
|
if( !hasKeyword( keyword ) ) return {};
|
2016-02-09 12:16:44 +01:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
const auto& indices = this->offsets( keyword );
|
2016-02-09 12:16:44 +01:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
std::vector< const DeckKeyword* > ret;
|
|
|
|
|
ret.reserve( indices.size() );
|
|
|
|
|
|
|
|
|
|
for( size_t i : indices )
|
|
|
|
|
ret.push_back( &this->getKeyword( i ) );
|
|
|
|
|
|
|
|
|
|
return ret;
|
2013-08-11 12:36:16 +02:00
|
|
|
}
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
size_t DeckView::size() const {
|
|
|
|
|
return std::distance( this->begin(), this->end() );
|
2013-08-20 15:51:19 +02:00
|
|
|
}
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
DeckView::const_iterator DeckView::begin() const {
|
|
|
|
|
return this->first;
|
2013-08-01 12:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
DeckView::const_iterator DeckView::end() const {
|
|
|
|
|
return this->last;
|
2013-08-01 12:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
void DeckView::add( const DeckKeyword* kw, const_iterator f, const_iterator l ) {
|
|
|
|
|
this->keywordMap[ kw->name() ].push_back( std::distance( f, l ) - 1 );
|
|
|
|
|
this->first = f;
|
|
|
|
|
this->last = l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const std::vector< size_t > empty_indices = {};
|
|
|
|
|
const std::vector< size_t >& DeckView::offsets( const std::string& keyword ) const {
|
|
|
|
|
if( !hasKeyword( keyword ) ) return empty_indices;
|
|
|
|
|
|
|
|
|
|
return this->keywordMap.find( keyword )->second;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 10:25:35 +01:00
|
|
|
DeckView::DeckView( const_iterator first_arg, const_iterator last_arg ) :
|
|
|
|
|
first( first_arg ), last( last_arg )
|
2016-02-09 12:16:44 +01:00
|
|
|
{
|
2016-02-09 12:25:01 +01:00
|
|
|
size_t index = 0;
|
|
|
|
|
for( const auto& kw : *this )
|
|
|
|
|
this->keywordMap[ kw.name() ].push_back( index++ );
|
2015-04-13 17:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
DeckView::DeckView( std::pair< const_iterator, const_iterator > limits ) :
|
|
|
|
|
DeckView( limits.first, limits.second )
|
|
|
|
|
{}
|
|
|
|
|
|
2016-03-14 16:02:27 +01:00
|
|
|
Deck::Deck() : Deck( std::vector< DeckKeyword >() ) {}
|
|
|
|
|
Deck::Deck( std::vector< DeckKeyword >&& x ) : DeckView( x.begin(), x.end() ),
|
|
|
|
|
keywordList( std::move( x ) ) {}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
void Deck::addKeyword( DeckKeyword&& keyword ) {
|
|
|
|
|
this->keywordList.push_back( std::move( keyword ) );
|
2015-04-13 17:05:29 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
auto first = this->keywordList.begin();
|
|
|
|
|
auto last = this->keywordList.end();
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
this->add( &this->keywordList.back(), first, last );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Deck::addKeyword( const DeckKeyword& keyword ) {
|
|
|
|
|
DeckKeyword kw = keyword;
|
|
|
|
|
this->addKeyword( std::move( kw ) );
|
2013-08-13 14:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-14 10:23:56 +01:00
|
|
|
void Deck::initUnitSystem() {
|
2016-02-09 12:25:01 +01:00
|
|
|
this->defaultUnits = std::unique_ptr< UnitSystem >( UnitSystem::newMETRIC() );
|
2013-12-14 10:23:56 +01:00
|
|
|
if (hasKeyword("FIELD"))
|
2016-02-09 12:25:01 +01:00
|
|
|
this->activeUnits = std::unique_ptr< UnitSystem >( UnitSystem::newFIELD() );
|
2013-12-14 10:23:56 +01:00
|
|
|
else
|
2016-02-09 12:25:01 +01:00
|
|
|
this->activeUnits = std::unique_ptr< UnitSystem >( UnitSystem::newMETRIC() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DeckKeyword& Deck::getKeyword( size_t index ) {
|
|
|
|
|
return this->keywordList.at( index );
|
2013-12-14 10:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
UnitSystem& Deck::getDefaultUnitSystem() const {
|
2016-03-14 16:02:27 +01:00
|
|
|
if( !this->defaultUnits ) const_cast< Deck* >( this )->initUnitSystem();
|
2016-02-09 12:25:01 +01:00
|
|
|
return *this->defaultUnits;
|
2013-12-14 10:23:56 +01:00
|
|
|
}
|
2014-12-08 16:34:28 +01:00
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
UnitSystem& Deck::getActiveUnitSystem() const {
|
2016-03-14 16:02:27 +01:00
|
|
|
if( !this->activeUnits ) const_cast< Deck* >( this )->initUnitSystem();
|
2016-02-09 12:25:01 +01:00
|
|
|
return *this->activeUnits;
|
2013-12-14 10:23:56 +01:00
|
|
|
}
|
2013-06-04 14:32:30 +02:00
|
|
|
|
2013-05-27 14:27:22 +02:00
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
}
|