From 9b1efd78104b3201dfe7bd97a61596745c5ae2ab Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Thu, 4 Feb 2016 18:31:02 +0100 Subject: [PATCH] Revert: Removed unused SimulatorState comparison. This reverts commit 5ce1d84ac1390362e1d9a9536eb5350dc3469ccb. --- opm/core/simulator/SimulatorState.cpp | 34 +++++++++++++++++++++++++++ opm/core/simulator/SimulatorState.hpp | 20 ++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/opm/core/simulator/SimulatorState.cpp b/opm/core/simulator/SimulatorState.cpp index 68faf8af..21f73eaa 100644 --- a/opm/core/simulator/SimulatorState.cpp +++ b/opm/core/simulator/SimulatorState.cpp @@ -6,6 +6,40 @@ using namespace Opm; +bool +SimulatorState::equals (const SimulatorState& other, + double epsilon) const { + bool equal = (num_phases_ == other.num_phases_); + + // if we use &=, then all the tests will be run regardless + equal = equal && vectorApproxEqual( pressure() , other.pressure() , epsilon); + equal = equal && vectorApproxEqual( temperature() , other.temperature() , epsilon); + equal = equal && vectorApproxEqual( facepressure() , other.facepressure() , epsilon); + equal = equal && vectorApproxEqual( faceflux() , other.faceflux() , epsilon); + equal = equal && vectorApproxEqual( saturation() , other.saturation() , epsilon); + + return equal; +} + +bool +SimulatorState::vectorApproxEqual(const std::vector& v1, + const std::vector& v2, + double epsilon) { + if (v1.size() != v2.size()) { + return false; + } + + for (size_t i = 0; i < v1.size(); i++) { + const double diff = std::abs(v1[i] - v2[i]); + const double scale = std::abs(v1[i]) + std::abs(v2[i]); + if (diff > epsilon * scale) { + return false; + } + } + + return true; +} + void SimulatorState::init(int number_of_cells, int number_of_faces, int num_phases) diff --git a/opm/core/simulator/SimulatorState.hpp b/opm/core/simulator/SimulatorState.hpp index 3487d711..4ef06d95 100644 --- a/opm/core/simulator/SimulatorState.hpp +++ b/opm/core/simulator/SimulatorState.hpp @@ -61,6 +61,13 @@ namespace Opm const std::vector& faceflux () const { return faceData_[ faceFluxId_ ]; } const std::vector& saturation () const { return cellData_[ saturationId_ ]; } + /** + * Compare this state with another, to see if they are different + * only within a small margin. + */ + virtual bool equals(const SimulatorState& other, + double epsilon = 1e-8) const; + std::vector< std::vector >& cellData() { return cellData_; } const std::vector< std::vector >& cellData() const { return cellData_; } @@ -87,6 +94,19 @@ namespace Opm /// \brief names for the face data std::vector< std::string > faceDataNames_; + protected: + /** + * Check if two vectors are equal within a margin. + * + * @param epsilon Relative difference that is tolerated for the + * vectors to still be considered equal. + * + * @return True if every element is within the margin, false if + * there is at least one that is not. + */ + static bool vectorApproxEqual(const std::vector& v1, + const std::vector& v2, + double epsilon); }; } // namespace Opm