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.
|
|
|
|
|
|
|
|
|
|
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>
|
2017-08-30 16:25:06 +02:00
|
|
|
#include <opm/parser/eclipse/Deck/DeckOutput.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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 07:38:40 +02:00
|
|
|
DeckView::DeckView( const_iterator first_arg, const_iterator last_arg)
|
2016-02-09 12:16:44 +01:00
|
|
|
{
|
2019-09-17 07:38:40 +02:00
|
|
|
this->init(first_arg, last_arg);
|
2015-04-13 17:05:29 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-17 07:38:40 +02:00
|
|
|
void DeckView::init( const_iterator first_arg, const_iterator last_arg ) {
|
2017-09-07 09:15:13 +02:00
|
|
|
this->first = first_arg;
|
|
|
|
|
this->last = last_arg;
|
|
|
|
|
|
|
|
|
|
this->keywordMap.clear();
|
|
|
|
|
|
|
|
|
|
size_t index = 0;
|
|
|
|
|
for( const auto& kw : *this )
|
|
|
|
|
this->keywordMap[ kw.name() ].push_back( index++ );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:16:44 +01:00
|
|
|
DeckView::DeckView( std::pair< const_iterator, const_iterator > limits ) :
|
|
|
|
|
DeckView( limits.first, limits.second )
|
|
|
|
|
{}
|
|
|
|
|
|
2019-09-17 07:38:40 +02:00
|
|
|
Deck::Deck() :
|
|
|
|
|
Deck( std::vector<DeckKeyword>() )
|
|
|
|
|
{}
|
|
|
|
|
|
2016-04-20 10:25:35 +02:00
|
|
|
|
2019-09-17 07:38:40 +02:00
|
|
|
/*
|
|
|
|
|
This constructor should be ssen as a technical implemtation detail of the
|
|
|
|
|
default constructor, and not something which should be invoked directly.
|
|
|
|
|
The point is that the derived class DeckView contains iterators to the
|
|
|
|
|
keywordList member in the base class - this represents some ordering
|
|
|
|
|
challenges in the construction phase.
|
|
|
|
|
*/
|
|
|
|
|
Deck::Deck( std::vector<DeckKeyword>&& x) :
|
|
|
|
|
DeckView(x.begin(), x.end()),
|
|
|
|
|
keywordList(std::move(x)),
|
2016-10-13 13:37:56 +02:00
|
|
|
defaultUnits( UnitSystem::newMETRIC() ),
|
2019-09-17 07:38:40 +02:00
|
|
|
activeUnits( UnitSystem::newMETRIC() )
|
2016-10-13 13:37:56 +02:00
|
|
|
{
|
|
|
|
|
}
|
2016-03-14 16:02:27 +01:00
|
|
|
|
2017-09-07 09:15:13 +02:00
|
|
|
Deck::Deck( const Deck& d ) :
|
2019-09-17 07:38:40 +02:00
|
|
|
DeckView(d.begin(), d.end()),
|
2017-09-07 09:15:13 +02:00
|
|
|
keywordList( d.keywordList ),
|
|
|
|
|
defaultUnits( d.defaultUnits ),
|
|
|
|
|
activeUnits( d.activeUnits ),
|
2018-07-19 08:20:04 +02:00
|
|
|
m_dataFile( d.m_dataFile ),
|
2019-09-17 07:38:40 +02:00
|
|
|
input_path( d.input_path )
|
|
|
|
|
{
|
|
|
|
|
this->init(this->keywordList.begin(), this->keywordList.end());
|
2017-09-07 09:15:13 +02:00
|
|
|
}
|
|
|
|
|
|
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-07-13 09:49:54 +02:00
|
|
|
auto fst = this->keywordList.begin();
|
|
|
|
|
auto lst = this->keywordList.end();
|
2016-02-09 12:16:44 +01:00
|
|
|
|
2016-07-13 09:49:54 +02:00
|
|
|
this->add( &this->keywordList.back(), fst, lst );
|
2016-02-09 12:25:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Deck::addKeyword( const DeckKeyword& keyword ) {
|
|
|
|
|
DeckKeyword kw = keyword;
|
|
|
|
|
this->addKeyword( std::move( kw ) );
|
2013-08-13 14:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 12:25:01 +01:00
|
|
|
|
|
|
|
|
DeckKeyword& Deck::getKeyword( size_t index ) {
|
|
|
|
|
return this->keywordList.at( index );
|
2013-12-14 10:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-03 00:51:27 +02:00
|
|
|
UnitSystem& Deck::getDefaultUnitSystem() {
|
2016-10-13 13:37:56 +02:00
|
|
|
return this->defaultUnits;
|
2013-12-14 10:23:56 +01:00
|
|
|
}
|
2013-06-04 14:32:30 +02:00
|
|
|
|
2016-04-03 00:51:27 +02:00
|
|
|
const UnitSystem& Deck::getDefaultUnitSystem() const {
|
2016-10-13 13:37:56 +02:00
|
|
|
return this->defaultUnits;
|
2016-04-03 00:51:27 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 13:37:56 +02:00
|
|
|
UnitSystem& Deck::getActiveUnitSystem() {
|
|
|
|
|
return this->activeUnits;
|
2016-04-03 00:51:27 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-17 08:00:43 +02:00
|
|
|
void Deck::selectActiveUnitSystem(UnitSystem::UnitType unit_type) {
|
|
|
|
|
this->activeUnits = UnitSystem(unit_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-10-13 13:37:56 +02:00
|
|
|
const UnitSystem& Deck::getActiveUnitSystem() const {
|
|
|
|
|
return this->activeUnits;
|
2016-04-03 00:51:27 +02:00
|
|
|
}
|
2013-05-27 14:27:22 +02:00
|
|
|
|
2018-07-19 08:20:04 +02:00
|
|
|
const std::string& Deck::getDataFile() const {
|
2016-04-20 10:25:35 +02:00
|
|
|
return m_dataFile;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-19 08:20:04 +02:00
|
|
|
const std::string& Deck::getInputPath() const {
|
|
|
|
|
return this->input_path;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 10:45:27 +02:00
|
|
|
std::string Deck::makeDeckPath(const std::string& path) const {
|
|
|
|
|
if (path.size() > 0 && path[0] == '/')
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
|
|
if (this->input_path.size() == 0)
|
|
|
|
|
return path;
|
|
|
|
|
else
|
|
|
|
|
return this->input_path + "/" + path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-20 10:25:35 +02:00
|
|
|
void Deck::setDataFile(const std::string& dataFile) {
|
2018-07-19 08:20:04 +02:00
|
|
|
this->m_dataFile = dataFile;
|
|
|
|
|
|
|
|
|
|
auto slash_pos = dataFile.find_last_of("/\\");
|
|
|
|
|
if (slash_pos == std::string::npos)
|
|
|
|
|
this->input_path = "";
|
|
|
|
|
else
|
|
|
|
|
this->input_path = dataFile.substr(0, slash_pos);
|
2016-04-20 10:25:35 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-05 19:18:53 +02:00
|
|
|
Deck::iterator Deck::begin() {
|
|
|
|
|
return this->keywordList.begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Deck::iterator Deck::end() {
|
|
|
|
|
return this->keywordList.end();
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 16:25:06 +02:00
|
|
|
|
|
|
|
|
void Deck::write( DeckOutput& output ) const {
|
|
|
|
|
size_t kw_index = 1;
|
|
|
|
|
for (const auto& keyword: *this) {
|
|
|
|
|
keyword.write( output );
|
|
|
|
|
kw_index++;
|
|
|
|
|
if (kw_index < size())
|
|
|
|
|
output.write_string( output.keyword_sep );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Deck& deck) {
|
2018-08-07 08:52:12 +02:00
|
|
|
DeckOutput out( os, 10 );
|
2017-08-30 16:25:06 +02:00
|
|
|
deck.write( out );
|
|
|
|
|
return os;
|
|
|
|
|
}
|
2016-02-09 12:16:44 +01:00
|
|
|
}
|