From c4fbd24be6a7c49a0568eb6ea1cfb477a7651712 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Sat, 14 Dec 2013 10:29:59 +0100 Subject: [PATCH] Added getNewDimension for get-or-create semantics. --- opm/parser/eclipse/Units/UnitSystem.cpp | 57 +++++++++++++++++++------ opm/parser/eclipse/Units/UnitSystem.hpp | 11 +++-- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/opm/parser/eclipse/Units/UnitSystem.cpp b/opm/parser/eclipse/Units/UnitSystem.cpp index 14dc11f7d..2abc5fbd2 100644 --- a/opm/parser/eclipse/Units/UnitSystem.cpp +++ b/opm/parser/eclipse/Units/UnitSystem.cpp @@ -39,42 +39,55 @@ namespace Opm { return (m_dimensions.find( dimension ) != m_dimensions.end()); } - - const Dimension& UnitSystem::getDimension(const std::string& dimension) const { - if (hasDimension( dimension )) + + std::shared_ptr UnitSystem::getNewDimension(const std::string& dimension) { + if (!hasDimension( dimension )) { + std::shared_ptr newDimension = parse( dimension ); + addDimension( newDimension ); + } + return getDimension( dimension ); + } + + + std::shared_ptr UnitSystem::getDimension(const std::string& dimension) const { + if (hasDimension( dimension )) return m_dimensions.at( dimension ); else throw std::invalid_argument("Dimension: " + dimension + " not recognized "); } - - void UnitSystem::addDimension(const std::string& dimension , double SI_factor) { - if (hasDimension(dimension)) - m_dimensions.erase( dimension ); + + void UnitSystem::addDimension(std::shared_ptr dimension) { + if (hasDimension(dimension->getName())) + m_dimensions.erase( dimension->getName() ); - m_dimensions.insert( std::make_pair(dimension , Dimension(dimension , SI_factor))); + m_dimensions.insert( std::make_pair(dimension->getName() , dimension)); } + void UnitSystem::addDimension(const std::string& dimension , double SI_factor) { + std::shared_ptr dim( new Dimension(dimension , SI_factor) ); + addDimension(dim); + } const std::string& UnitSystem::getName() const { return m_name; } - std::shared_ptr UnitSystem::parseFactor(const std::string& dimension) const { + std::shared_ptr UnitSystem::parseFactor(const std::string& dimension) const { std::vector dimensionList; boost::split(dimensionList , dimension , boost::is_any_of("*")); double SIfactor = 1.0; for (auto iter = dimensionList.begin(); iter != dimensionList.end(); ++iter) { - Dimension dim = getDimension( *iter ); - SIfactor *= dim.getSIScaling(); + std::shared_ptr dim = getDimension( *iter ); + SIfactor *= dim->getSIScaling(); } return std::shared_ptr(Dimension::newComposite( dimension , SIfactor )); } - std::shared_ptr UnitSystem::parse(const std::string& dimension) const { + std::shared_ptr UnitSystem::parse(const std::string& dimension) const { bool haveDivisor; { size_t divCount = std::count( dimension.begin() , dimension.end() , '/' ); @@ -99,6 +112,26 @@ namespace Opm { } + bool UnitSystem::equal(const UnitSystem& other) const { + bool equal = (m_dimensions.size() == other.m_dimensions.size()); + + if (equal) { + for (auto iter = m_dimensions.begin(); iter != m_dimensions.end(); ++iter) { + std::shared_ptr dim = getDimension( iter->first ); + + if (other.hasDimension( iter->first )) { + std::shared_ptr otherDim = other.getDimension( iter->first ); + if (!dim->equal(*otherDim)) + equal = false; + } else + equal = false; + + } + } + return equal; + } + + UnitSystem * UnitSystem::newMETRIC() { UnitSystem * system = new UnitSystem("Metric"); diff --git a/opm/parser/eclipse/Units/UnitSystem.hpp b/opm/parser/eclipse/Units/UnitSystem.hpp index 69abf1de4..1908d4b2d 100644 --- a/opm/parser/eclipse/Units/UnitSystem.hpp +++ b/opm/parser/eclipse/Units/UnitSystem.hpp @@ -34,18 +34,21 @@ namespace Opm { const std::string& getName() const; void addDimension(const std::string& dimension , double SI_factor); - const Dimension& getDimension(const std::string& dimension) const; + void addDimension(std::shared_ptr dimension); + std::shared_ptr getNewDimension(const std::string& dimension); + std::shared_ptr getDimension(const std::string& dimension) const; bool hasDimension(const std::string& dimension) const; + bool equal(const UnitSystem& other) const; - std::shared_ptr parse(const std::string& dimension) const; + std::shared_ptr parse(const std::string& dimension) const; static UnitSystem * newMETRIC(); static UnitSystem * newFIELD(); private: - std::shared_ptr parseFactor(const std::string& dimension) const; + std::shared_ptr parseFactor(const std::string& dimension) const; const std::string m_name; - std::map m_dimensions; + std::map > m_dimensions; }; }