Added boolan flag scalar = true to DeckItem.

The purpose of this flag is to keep track of whether a keyword is
supposed to have only one element, i.e. scalar, or several. The
defaultApplied method only makes sense in the case of scalar items, this
method will now throw if it is called on a non-scalar item.
This commit is contained in:
Joakim Hove
2014-04-10 22:29:18 +02:00
parent 5becf79dd1
commit f59fd25668
17 changed files with 80 additions and 14 deletions

View File

@@ -32,7 +32,7 @@ namespace Opm {
class DeckDoubleItem : public DeckItem {
public:
DeckDoubleItem(std::string name) : DeckItem(name) {}
DeckDoubleItem(std::string name, bool scalar = true) : DeckItem(name , scalar) {}
double getRawDouble(size_t index) const;
const std::vector<double>& getRawDoubleData() const;
double getSIDouble(size_t index) const;

View File

@@ -32,7 +32,7 @@ namespace Opm {
class DeckFloatItem : public DeckItem {
public:
DeckFloatItem(std::string name) : DeckItem(name) {}
DeckFloatItem(std::string name , bool scalar = true) : DeckItem(name , scalar) {}
float getRawFloat(size_t index) const;
//const std::vector<float>& getRawFloatData() const;
float getSIFloat(size_t index) const;

View File

@@ -30,7 +30,7 @@ namespace Opm {
class DeckIntItem : public DeckItem {
public:
DeckIntItem(std::string name) : DeckItem(name) {}
DeckIntItem(std::string name , bool scalar = true) : DeckItem(name , scalar) {}
int getInt(size_t index) const;
const std::vector<int>& getIntData() const;

View File

@@ -19,11 +19,14 @@
#include <opm/parser/eclipse/Deck/DeckItem.hpp>
#include <stdexcept>
namespace Opm {
DeckItem::DeckItem(const std::string& name) {
DeckItem::DeckItem(const std::string& name , bool scalar) {
m_name = name;
m_defaultApplied = false;
m_scalar = scalar;
}
const std::string& DeckItem::name() const {
@@ -31,7 +34,10 @@ namespace Opm {
}
bool DeckItem::defaultApplied() const {
return m_defaultApplied;
if (m_scalar)
return m_defaultApplied;
else
throw std::invalid_argument("Tried query deckItem: " + m_name + " if default has been applied - that only applies to scalar items");
}
}

View File

@@ -31,7 +31,7 @@ namespace Opm {
class DeckItem {
public:
DeckItem(const std::string& name);
DeckItem(const std::string& name , bool m_scalar = true);
const std::string& name() const;
bool defaultApplied() const;
@@ -106,6 +106,7 @@ namespace Opm {
bool m_defaultApplied;
private:
std::string m_name;
bool m_scalar;
};
typedef std::shared_ptr<DeckItem> DeckItemPtr;

View File

@@ -31,7 +31,7 @@ namespace Opm {
class DeckStringItem : public DeckItem {
public:
DeckStringItem(std::string name) : DeckItem(name) {
DeckStringItem(std::string name, bool scalar = true) : DeckItem(name, scalar) {
}
std::string getString(size_t index) const;
const std::vector<std::string>& getStringData() const;

View File

@@ -35,6 +35,20 @@ BOOST_AUTO_TEST_CASE(GetIntAtIndex_NoData_ExceptionThrown) {
BOOST_CHECK_THROW(deckIntItem.getInt(0), std::out_of_range);
}
BOOST_AUTO_TEST_CASE(InitializeDefaultApplied) {
DeckIntItem deckIntItem("TEST");
BOOST_REQUIRE_NO_THROW( deckIntItem.defaultApplied() );
}
BOOST_AUTO_TEST_CASE(InitializeDefaultApplied_Throws_for_nonScalar) {
DeckIntItem deckIntItem("TEST" , false);
BOOST_REQUIRE_THROW( deckIntItem.defaultApplied() , std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(PushBack_VectorPushed_ElementsCorrect) {
DeckIntItem deckIntItem("TEST");
std::deque<int> pushThese;

View File

@@ -58,3 +58,31 @@ BOOST_AUTO_TEST_CASE( ParseMissingRECORD_THrows) {
const char *data = "\n\
ENDSCALE\n\
1* 1* 2 /\n\
\n\
ENKRVD\n\
100 * 2 3 4 5 6 7 200 11 22 33 44 55 66 77 /\n\
100 1 2 3 4 5 6 7 200 11 22 33 44 55 66 77 /\n\
";
BOOST_AUTO_TEST_CASE( parse_DATAWithDefult_OK ) {
ParserPtr parser(new Parser());
DeckConstPtr deck = parser->parseString( data );
DeckKeywordConstPtr keyword = deck->getKeyword( "ENKRVD" );
DeckRecordConstPtr rec0 = keyword->getRecord(0);
DeckRecordConstPtr rec1 = keyword->getRecord(1);
DeckItemConstPtr item0 = rec0->getItem(0);
DeckItemConstPtr item1 = rec1->getItem(0);
BOOST_CHECK_EQUAL( 2U , keyword->size());
BOOST_CHECK_THROW( item0->defaultApplied() , std::invalid_argument);
}

View File

@@ -104,7 +104,7 @@ void check_parser(ParserPtr parser) {
BOOST_CHECK_EQUAL(9U , item4_1->size());
BOOST_CHECK_EQUAL(2U , record4->size());
{
Opm::PvtgTable pvtgTable(kw1, 0);

View File

@@ -128,7 +128,7 @@ namespace Opm
/// returns a DeckItem object.
/// NOTE: data are popped from the rawRecords deque!
DeckItemPtr ParserDoubleItem::scan(RawRecordPtr rawRecord) const {
DeckDoubleItemPtr deckItem(new DeckDoubleItem(name()));
DeckDoubleItemPtr deckItem(new DeckDoubleItem(name() , scalar()));
double defaultValue = m_default;
if (sizeType() == ALL) { // This can probably not be combined with a default value ....

View File

@@ -128,7 +128,7 @@ namespace Opm
/// returns a DeckItem object.
/// NOTE: data are popped from the rawRecords deque!
DeckItemPtr ParserFloatItem::scan(RawRecordPtr rawRecord) const {
DeckFloatItemPtr deckItem(new DeckFloatItem(name()));
DeckFloatItemPtr deckItem(new DeckFloatItem(name() , scalar()));
float defaultValue = m_default;
if (sizeType() == ALL) { // This can probably not be combined with a default value ....

View File

@@ -62,10 +62,11 @@ namespace Opm {
/// returns a DeckItem object.
/// NOTE: data are popped from the rawRecords deque!
DeckItemPtr ParserIntItem::scan(RawRecordPtr rawRecord) const {
DeckIntItemPtr deckItem(new DeckIntItem(name()));
DeckIntItemPtr deckItem(new DeckIntItem( name() , scalar() ));
int defaultValue = m_default;
if (sizeType() == ALL) { // This can probably not be combined with a default value ....
if (sizeType() == ALL) {
// This can probably not be combined with a default value ....
// The '*' should be interpreted as a multiplication sign
while (rawRecord->size() > 0) {
std::string token = rawRecord->pop_front();

View File

@@ -83,6 +83,13 @@ namespace Opm {
return m_sizeType;
}
bool ParserItem::scalar() const {
if (m_sizeType == SINGLE)
return true;
else
return false;
}
std::string ParserItem::getDescription() const {
return m_description;
}

View File

@@ -50,6 +50,7 @@ namespace Opm {
const std::string& name() const;
ParserItemSizeEnum sizeType() const;
std::string getDescription() const;
bool scalar() const;
void setDescription(std::string helpText);
static int defaultInt();

View File

@@ -64,7 +64,7 @@ namespace Opm {
/// NOTE: data are popped from the rawRecords deque!
DeckItemPtr ParserStringItem::scan(RawRecordPtr rawRecord) const {
DeckStringItemPtr deckItem(new DeckStringItem(name()));
DeckStringItemPtr deckItem(new DeckStringItem(name() , scalar()));
std::string defaultValue = m_default;
if (sizeType() == ALL) { // This can probably not be combined with a default value ....

View File

@@ -45,6 +45,14 @@ BOOST_AUTO_TEST_CASE(Initialize) {
BOOST_CHECK_NO_THROW(ParserFloatItem item1("ITEM1", sizeType));
}
BOOST_AUTO_TEST_CASE(ScalarCheck) {
ParserIntItem item1("ITEM1", SINGLE);
ParserIntItem item2("ITEM1", ALL);
BOOST_CHECK( item1.scalar());
BOOST_CHECK( !item2.scalar());
}
BOOST_AUTO_TEST_CASE(Initialize_DefaultSizeType) {
ParserIntItem item1(std::string("ITEM1"));
ParserStringItem item2(std::string("ITEM1"));

View File

@@ -73,7 +73,7 @@ size_t SimpleMultiRecordTable::getNumFlatItems_(Opm::DeckRecordConstPtr deckReco
int result = 0;
for (unsigned i = 0; i < deckRecord->size(); ++ i) {
Opm::DeckItemConstPtr item(deckRecord->getItem(i));
if (item->defaultApplied())
if (i == 0 && item->defaultApplied())
return result;
result += item->size();
}