Varnish SimpleTable Component

In particular

  - Split some long lines
  - Prefer in-place constrution where possible
  - Prefer '//' comments
This commit is contained in:
Bård Skaflestad 2023-11-17 11:13:48 +01:00
parent 932d3fe85e
commit d736136acc
5 changed files with 260 additions and 205 deletions

View File

@ -28,69 +28,77 @@ namespace Opm {
class SimpleTable;
class TableContainer {
/*
The TableContainer class implements a simple map:
{tableNumber , Table}. The main functionality of the
TableContainer class is that the getTable method implements
the Eclipse behavior:
If table N is not implemented - use table N - 1.
The getTable() method will eventually throw an exception if
not even table 0 is there.
Consider the following code:
TableContainer container(10);
std::shared_ptr<TableType> table0 = std::make_shared<TableType>(...);
container.addTable( table0 , 0 )
We create a container with maximum 10 tables, and then we add
one single table at slot 0; then we have:
container.size() == 1
container.hasTable( 0 ) == true
container.hasTable( 9 ) == false
container.hasTable(10 ) == false
container.getTable( 0 ) == container[9] == table0;
container.gteTable(10 ) ==> exception
*/
/// The TableContainer class implements a simple map:
///
/// {tableNumber, Table}
///
/// The main functionality of the TableContainer class is that the
/// getTable() method implements the ECLIPSE behavior:
///
/// If table N is not implemented - use table N - 1.
///
/// The getTable() method will eventually throw an exception if not even
/// table 0 is there.
///
/// Consider the following code:
///
/// TableContainer container(10);
///
/// std::shared_ptr<TableType> table0 = std::make_shared<TableType>(...);
/// container.addTable(table0, 0)
///
/// We create a container with maximum 10 tables, and then we add one
/// single table at slot 0; then we have:
///
/// container.size() == 1
/// container.hasTable( 0 ) == true
/// container.hasTable( 9 ) == false
/// container.hasTable(10 ) == false
///
/// container.getTable( 0 ) == container[9] == table0;
/// container.gteTable(10 ) ==> exception
class TableContainer
{
public:
using TableMap = std::map<size_t, std::shared_ptr<SimpleTable>>;
TableContainer();
explicit TableContainer( size_t maxTables );
explicit TableContainer(size_t maxTables);
static TableContainer serializationTestObject();
bool empty() const;
/*
This is the number of actual tables in the container.
*/
// This is the number of actual tables in the container.
size_t size() const;
size_t max() const;
const TableMap& tables() const;
void addTable(size_t tableNumber , std::shared_ptr<SimpleTable> table);
void addTable(size_t tableNumber, std::shared_ptr<SimpleTable> table);
/*
Observe that the hasTable() method does not invoke the "If
table N is not implemented use table N - 1 behavior.
*/
size_t hasTable(size_t tableNumber) const;
// Observe that the hasTable() method does not invoke the "If table
// N is not implemented use table N - 1 behavior.
bool hasTable(size_t tableNumber) const;
const SimpleTable& getTable(size_t tableNumber) const;
const SimpleTable& operator[](size_t tableNumber) const;
const SimpleTable& operator[](size_t tableNumber) const
{
return this->getTable(tableNumber);
}
template <class TableType>
const TableType& getTable(size_t tableNumber) const {
const SimpleTable &simpleTable = getTable( tableNumber );
const TableType * table = static_cast<const TableType *>( &simpleTable );
return *table;
const TableType& getTable(size_t tableNumber) const
{
// This is, strictly speaking, a downcast so we should prefer
// dynamic_cast<>() instead. However, serializeOp() by
// construction throws away the derived TableType during object
// distribution, keeping only the SimpleTable, so dynamic_cast<>
// will throw a bad_cast exception on ranks other than the I/O
// rank (0). We therefore resort to static_cast<>() here
// instead and hope that the caller specifies the correct
// derived type...
return static_cast<const TableType&>(this->getTable(tableNumber));
}
bool operator==(const TableContainer& data) const;
@ -109,5 +117,4 @@ namespace Opm {
}
#endif
#endif // OPM_TABLE_CONTAINER_HPP

View File

@ -16,34 +16,41 @@
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <utility>
#include <iostream>
#include <opm/input/eclipse/EclipseState/Tables/SimpleTable.hpp>
#include <opm/input/eclipse/EclipseState/Tables/TableSchema.hpp>
#include <opm/input/eclipse/Deck/DeckItem.hpp>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include <fmt/format.h>
namespace Opm {
SimpleTable::SimpleTable( TableSchema schema, const std::string& tableName, const DeckItem& deckItem,
const int tableID) :
m_schema( std::move( schema ) ),
m_jfunc (false)
SimpleTable::SimpleTable(TableSchema schema,
const std::string& tableName,
const DeckItem& deckItem,
const int tableID)
: m_schema(std::move(schema))
, m_jfunc (false)
{
init(tableName, deckItem, tableID );
this->init(tableName, deckItem, tableID);
}
SimpleTable::SimpleTable( TableSchema schema ) :
m_schema( std::move( schema ) ),
m_jfunc (false)
SimpleTable::SimpleTable(TableSchema schema)
: m_schema(std::move(schema))
, m_jfunc (false)
{
addColumns();
this->addColumns();
}
SimpleTable SimpleTable::serializationTestObject()
{
SimpleTable result;
@ -54,44 +61,50 @@ namespace Opm {
return result;
}
void SimpleTable::addRow( const std::vector<double>& row, const std::string& tableName) {
if (row.size() == numColumns()) {
for (size_t colIndex = 0; colIndex < numColumns(); colIndex++) {
auto& col = getColumn( colIndex );
col.addValue( row[colIndex], tableName );
}
} else
void SimpleTable::addRow(const std::vector<double>& row,
const std::string& tableName)
{
const auto ncol = this->numColumns();
if (row.size() != ncol) {
throw std::invalid_argument("Size mismatch");
}
}
void SimpleTable::addColumns() {
for (size_t colIdx = 0; colIdx < m_schema.size(); ++colIdx) {
const auto& schemaColumn = m_schema.getColumn( colIdx );
TableColumn column(schemaColumn); // Some move trickery here ...
m_columns.insert( std::make_pair( schemaColumn.name() , column ));
for (auto colIndex = 0*ncol; colIndex < ncol; ++colIndex) {
this->getColumn(colIndex).addValue(row[colIndex], tableName);
}
}
void SimpleTable::addColumns()
{
const auto ncol = this->m_schema.size();
double SimpleTable::get(const std::string& column , size_t row) const {
const auto& col = getColumn( column );
return col[row];
for (auto colIdx = 0*ncol; colIdx < ncol; ++colIdx) {
const auto& schemaColumn = m_schema.getColumn(colIdx);
this->m_columns.insert(std::make_pair(schemaColumn.name(), TableColumn { schemaColumn }));
}
}
double SimpleTable::get(size_t column , size_t row) const {
const auto& col = getColumn( column );
return col[row];
double SimpleTable::get(const std::string& column, size_t row) const
{
return this->getColumn(column)[row];
}
void SimpleTable::init( const std::string& tableName,
const DeckItem& deckItem,
const int tableID,
double scaling_factor) {
double SimpleTable::get(size_t column, size_t row) const
{
return this->getColumn(column)[row];
}
void SimpleTable::init(const std::string& tableName,
const DeckItem& deckItem,
const int tableID,
const double scaling_factor)
{
this->addColumns();
if ( (deckItem.data_size() % numColumns()) != 0) {
const auto ncol = this->numColumns();
if ((deckItem.data_size() % ncol) != 0) {
throw std::runtime_error {
fmt::format("For table {} with ID {}: "
"Number of input table elements ({}) is "
@ -101,93 +114,117 @@ namespace Opm {
};
}
size_t rows = deckItem.data_size() / numColumns();
for (size_t colIdx = 0; colIdx < numColumns(); ++colIdx) {
auto& column = getColumn( colIdx );
for (size_t rowIdx = 0; rowIdx < rows; rowIdx++) {
size_t deckItemIdx = rowIdx*numColumns() + colIdx;
if (deckItem.defaultApplied(deckItemIdx))
column.addDefault( tableName );
const size_t rows = deckItem.data_size() / ncol;
for (size_t colIdx = 0; colIdx < ncol; ++colIdx) {
auto& column = this->getColumn(colIdx);
for (size_t rowIdx = 0; rowIdx < rows; ++rowIdx) {
const size_t deckItemIdx = rowIdx*ncol + colIdx;
if (deckItem.defaultApplied(deckItemIdx)) {
column.addDefault(tableName);
}
else if (m_jfunc) {
column.addValue( deckItem.getData<double>()[deckItemIdx], tableName );
column.addValue(deckItem.getData<double>()[deckItemIdx], tableName);
}
else if (scaling_factor > 0.0) {
column.addValue(scaling_factor * deckItem.get<double>(deckItemIdx), tableName);
}
else {
if (scaling_factor > 0.0)
column.addValue( scaling_factor * deckItem.get<double>(deckItemIdx), tableName );
else
column.addValue( deckItem.getSIDouble(deckItemIdx), tableName );
column.addValue(deckItem.getSIDouble(deckItemIdx), tableName);
}
}
if (colIdx > 0)
column.applyDefaults(getColumn( 0 ), tableName);
if (colIdx > 0) {
column.applyDefaults(this->getColumn(0), tableName);
}
}
}
size_t SimpleTable::numColumns() const {
size_t SimpleTable::numColumns() const
{
return m_schema.size();
}
size_t SimpleTable::numRows() const {
return getColumn( 0 ).size();
size_t SimpleTable::numRows() const
{
return this->getColumn(0).size();
}
const TableColumn& SimpleTable::getColumn( const std::string& name) const {
if (!this->m_jfunc)
return m_columns.get( name );
const TableColumn& SimpleTable::getColumn(const std::string& name) const
{
if (! this->m_jfunc) {
return this->m_columns.get(name);
}
if (name == "PCOW" || name == "PCOG")
if ((name == "PCOW") || (name == "PCOG")) {
assertJFuncPressure(false); // this will throw since m_jfunc=true
return m_columns.get( name );
}
return this->m_columns.get(name);
}
const TableColumn& SimpleTable::getColumn( size_t columnIndex ) const {
return m_columns.iget( columnIndex );
const TableColumn& SimpleTable::getColumn(size_t columnIndex) const
{
return this->m_columns.iget(columnIndex);
}
TableColumn& SimpleTable::getColumn(const std::string& name)
{
if (! this->m_jfunc) {
return this->m_columns.get(name);
}
TableColumn& SimpleTable::getColumn( const std::string& name) {
if (!this->m_jfunc)
return m_columns.get( name );
if (name == "PCOW" || name == "PCOG")
if ((name == "PCOW") || (name == "PCOG")) {
assertJFuncPressure(false); // this will throw since m_jfunc=true
return m_columns.get( name );
}
return this->m_columns.get(name);
}
TableColumn& SimpleTable::getColumn( size_t columnIndex ) {
return m_columns.iget( columnIndex );
TableColumn& SimpleTable::getColumn(size_t columnIndex)
{
return this->m_columns.iget(columnIndex);
}
bool SimpleTable::hasColumn(const std::string& name) const {
return m_schema.hasColumn( name );
bool SimpleTable::hasColumn(const std::string& name) const
{
return this->m_schema.hasColumn(name);
}
double SimpleTable::evaluate(const std::string& columnName, double xPos) const
{
const auto& argColumn = getColumn( 0 );
const auto& valueColumn = getColumn( columnName );
const auto index = this->getColumn(0).lookup(xPos);
const auto index = argColumn.lookup( xPos );
return valueColumn.eval( index );
return this->getColumn(columnName).eval(index);
}
void SimpleTable::assertJFuncPressure(const bool jf) const {
if (jf == m_jfunc)
void SimpleTable::assertJFuncPressure(const bool jf) const
{
if (jf == this->m_jfunc) {
return;
// if we reach here, wrong values are read from the deck! (JFUNC is used
// incorrectly.) This function writes to std err for now, but will
// after a grace period be rewritten to throw (std::invalid_argument).
if (m_jfunc)
std::cerr << "Developer warning: Pressure column is read with JFUNC in deck." << std::endl;
else
std::cerr << "Developer warning: Raw values from JFUNC column is read, but JFUNC not provided in deck." << std::endl;
}
// if we reach here, wrong values are read from the deck! (JFUNC is
// used incorrectly.) This function writes to std err for now, but
// will after a grace period be rewritten to throw
// (std::invalid_argument).
if (m_jfunc) {
std::cerr << "Developer warning: Pressure column "
"is read with JFUNC in deck.\n";
}
else {
std::cerr << "Developer warning: Raw values from JFUNC column "
"is read, but JFUNC not provided in deck.\n";
}
}
bool SimpleTable::operator==(const SimpleTable& data) const {
return this->m_schema == data.m_schema &&
this->m_columns == data.m_columns &&
this->m_jfunc == data.m_jfunc;
bool SimpleTable::operator==(const SimpleTable& data) const
{
return (this->m_schema == data.m_schema)
&& (this->m_columns == data.m_columns)
&& (this->m_jfunc == data.m_jfunc)
;
}
}

View File

@ -17,22 +17,24 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/input/eclipse/EclipseState/Tables/SimpleTable.hpp>
#include <opm/input/eclipse/EclipseState/Tables/TableContainer.hpp>
#include <opm/input/eclipse/EclipseState/Tables/SimpleTable.hpp>
#include <string>
#include <utility>
#include <stddef.h>
namespace Opm {
TableContainer::TableContainer() :
m_maxTables(0)
{
}
TableContainer::TableContainer()
: m_maxTables(0)
{}
TableContainer::TableContainer(size_t maxTables) :
m_maxTables(maxTables)
{
}
TableContainer::TableContainer(size_t maxTables)
: m_maxTables(maxTables)
{}
TableContainer TableContainer::serializationTestObject()
{
@ -44,77 +46,86 @@ namespace Opm {
return result;
}
bool TableContainer::empty() const {
bool TableContainer::empty() const
{
return m_tables.empty();
}
size_t TableContainer::size() const {
size_t TableContainer::size() const
{
return m_tables.size();
}
size_t TableContainer::max() const {
size_t TableContainer::max() const
{
return m_maxTables;
}
const TableContainer::TableMap& TableContainer::tables() const {
const TableContainer::TableMap& TableContainer::tables() const
{
return m_tables;
}
size_t TableContainer::hasTable(size_t tableNumber) const {
if (m_tables.find( tableNumber ) == m_tables.end())
return false;
else
return true;
bool TableContainer::hasTable(size_t tableNumber) const
{
return this->m_tables.find(tableNumber)
!= this->m_tables.end();
}
const SimpleTable& TableContainer::getTable(size_t tableNumber) const {
if (tableNumber >= m_maxTables)
const SimpleTable& TableContainer::getTable(size_t tableNumber) const
{
if (tableNumber >= m_maxTables) {
throw std::invalid_argument("TableContainer - invalid tableNumber");
}
if (hasTable(tableNumber)) {
auto pair = m_tables.find( tableNumber );
return *(pair->second.get());
} else {
if (tableNumber > 0)
return getTable(tableNumber -1);
else
throw std::invalid_argument("TableContainer does not have any table in the range 0..." + std::to_string( tableNumber ));
if (auto pairPos = m_tables.find(tableNumber); pairPos != m_tables.end()) {
return *pairPos->second;
}
else if (tableNumber > 0) {
return getTable(tableNumber - 1);
}
else {
throw std::invalid_argument {
"TableContainer does not have any table in "
"the range 0..." + std::to_string(tableNumber)
};
}
}
void TableContainer::addTable(size_t tableNumber, std::shared_ptr<SimpleTable> table)
{
if (tableNumber >= m_maxTables) {
throw std::invalid_argument {
"TableContainer has max: " + std::to_string(m_maxTables) +
" tables. Table number: " + std::to_string(tableNumber) + " illegal."
};
}
const SimpleTable& TableContainer::operator[](size_t tableNumber) const {
return getTable(tableNumber);
m_tables[tableNumber] = std::move(table);
}
void TableContainer::addTable(size_t tableNumber , std::shared_ptr<SimpleTable> table) {
if (tableNumber >= m_maxTables)
throw std::invalid_argument("TableContainer has max: " + std::to_string( m_maxTables ) + " tables. Table number: " + std::to_string( tableNumber ) + " illegal.");
m_tables[tableNumber] = table;
}
bool TableContainer::operator==(const TableContainer& data) const {
if (this->max() != data.max())
bool TableContainer::operator==(const TableContainer& data) const
{
if (this->max() != data.max()) {
return false;
if (this->size() != data.size())
}
if (this->size() != data.size()) {
return false;
}
for (const auto& it : m_tables) {
auto it2 = data.m_tables.find(it.first);
if (it2 == data.m_tables.end())
if (it2 == data.m_tables.end()) {
return false;
if (!(*it.second == *it2->second))
}
if (! (*it.second == *it2->second)) {
return false;
}
}
return true;
}
}
} // namespace Opm

View File

@ -355,8 +355,9 @@ std::optional<JFunc> make_jfunc(const Deck& deck) {
}
void TableManager::addTables( const std::string& tableName , size_t numTables) {
m_simpleTables.emplace(std::make_pair(tableName , TableContainer( numTables )));
void TableManager::addTables(const std::string& tableName, size_t numTables)
{
this->m_simpleTables.try_emplace(tableName, numTables);
}
@ -1555,24 +1556,26 @@ std::optional<JFunc> make_jfunc(const Deck& deck) {
void TableManager::initSimpleTableContainer(const Deck& deck,
const std::string& keywordName,
const std::string& tableName,
size_t numTables) {
if (!deck.hasKeyword(keywordName))
size_t numTables)
{
if (!deck.hasKeyword(keywordName)) {
return; // the table is not featured by the deck...
auto& container = forceGetTables(tableName , numTables);
}
if (deck.count(keywordName) > 1) {
complainAboutAmbiguousKeyword(deck, keywordName);
return;
}
auto& container = forceGetTables(tableName, numTables);
auto lastComplete = 0 * numTables;
const auto& tableKeyword = deck[keywordName].back();
for (size_t tableIdx = 0; tableIdx < tableKeyword.size(); ++tableIdx) {
const auto& dataItem = tableKeyword.getRecord( tableIdx ).getItem("DATA");
const auto& dataItem = tableKeyword.getRecord(tableIdx).getItem("DATA");
if (dataItem.data_size() > 0) {
std::shared_ptr<TableType> table = std::make_shared<TableType>( dataItem, tableIdx );
container.addTable( tableIdx , table );
container.addTable(tableIdx, std::make_shared<TableType>(dataItem, tableIdx));
lastComplete = tableIdx;
}
else if (tableIdx > static_cast<size_t>(0)) {

View File

@ -233,14 +233,11 @@ void ensurePvtApi(const OilPvt& oilPvt, const GasPvt& gasPvt, const WaterPvt& wa
struct Fixture {
Fixture()
: python(std::make_shared<Opm::Python>())
, deck(Opm::Parser().parseString(deckString1))
: deck(Opm::Parser().parseString(deckString1))
, eclState(deck)
, schedule(deck, eclState, python)
{
}
, schedule(deck, eclState, std::make_shared<Opm::Python>())
{}
std::shared_ptr<Opm::Python> python;
Opm::Deck deck;
Opm::EclipseState eclState;
Opm::Schedule schedule;