Loading...
Searching...
No Matches
SatfuncConsistencyChecks.hpp
/*
Copyright 2024 Equinor AS
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.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_SATFUNC_CONSISTENCY_CHECK_MODULE_HPP
#define OPM_SATFUNC_CONSISTENCY_CHECK_MODULE_HPP
#include <opm/simulators/utils/ParallelCommunication.hpp>
#include <cstddef>
#include <functional>
#include <memory>
#include <random>
#include <string>
#include <string_view>
#include <vector>
namespace Opm {
template <typename Scalar>
struct EclEpsScalingPointsInfo;
} // namespace Opm
namespace Opm {
template <typename Scalar>
class SatfuncConsistencyChecks
{
public:
class Check
{
public:
virtual ~Check() = default;
virtual void test(const EclEpsScalingPointsInfo<Scalar>& endPoints) = 0;
virtual bool isViolated() const = 0;
virtual bool isCritical() const = 0;
virtual std::size_t numExportedCheckValues() const = 0;
virtual void exportCheckValues(Scalar* exportedCheckValues) const = 0;
virtual std::string description() const = 0;
virtual std::string condition() const = 0;
virtual void columnNames(std::string* headers) const = 0;
};
enum class ViolationLevel : std::size_t {
Standard,
Critical,
NumLevels,
};
using ReportRecordOutput = std::function<void(std::string_view)>;
using PointIDFormatCallback = std::function<std::string(std::size_t)>;
explicit SatfuncConsistencyChecks(std::string_view pointName,
const std::size_t numSamplePoints);
~SatfuncConsistencyChecks() = default;
SatfuncConsistencyChecks(const SatfuncConsistencyChecks& rhs) = delete;
SatfuncConsistencyChecks(SatfuncConsistencyChecks&& rhs);
SatfuncConsistencyChecks&
operator=(const SatfuncConsistencyChecks& rhs) = delete;
SatfuncConsistencyChecks& operator=(SatfuncConsistencyChecks&& rhs);
SatfuncConsistencyChecks&
setPointIDFormatCallback(const PointIDFormatCallback& formatPointID)
{
this->formatPointID_ = formatPointID;
return *this;
}
void resetCheckSet();
void addCheck(std::unique_ptr<Check> check);
void finaliseCheckSet();
void checkEndpoints(const std::size_t pointID,
const EclEpsScalingPointsInfo<Scalar>& endPoints);
void collectFailures(int root, const Parallel::Communication& comm);
bool anyFailedStandardChecks() const;
bool anyFailedCriticalChecks() const;
void reportFailures(const ViolationLevel level,
const ReportRecordOutput& emitReportRecord) const;
private:
using RandomBitGenerator = std::minstd_rand;
struct ViolationSample
{
std::vector<std::size_t> count{};
std::vector<std::size_t> pointID{};
std::vector<Scalar> checkValues{};
void clear();
};
using ViolationCollection = std::array
<ViolationSample, static_cast<std::size_t>(ViolationLevel::NumLevels)>;
std::string pointName_{};
std::size_t numSamplePoints_;
PointIDFormatCallback formatPointID_{};
std::vector<typename std::vector<Scalar>::size_type> startCheckValues_{};
ViolationCollection violations_{};
std::vector<std::unique_ptr<Check>> battery_{};
std::unique_ptr<RandomBitGenerator> urbg_{};
void collectFailures(int root,
const Parallel::Communication& comm,
ViolationSample& violation);
void buildStructure(ViolationSample& violation);
template <typename PopulateCheckValues>
void processViolation(ViolationSample& violation,
const std::size_t checkIx,
const std::size_t pointID,
PopulateCheckValues&& populateCheckValues);
void processViolation(const ViolationLevel level,
const std::size_t checkIx,
const std::size_t pointID);
void incorporateRankViolations(const std::size_t* count,
const std::size_t* pointID,
const Scalar* checkValues,
ViolationSample& violation);
std::size_t getSampleIndex(const std::size_t sampleSize);
void ensureRandomBitGeneratorIsInitialised();
std::vector<std::size_t>::size_type
violationPointIDStart(const std::size_t checkIx) const;
typename std::vector<Scalar>::size_type
violationValueStart(const std::size_t checkIx,
const std::size_t sampleIx) const;
void writeReportHeader(const Check* currentCheck,
const std::size_t violationCount,
const ReportRecordOutput& emitReportRecord) const;
void writeTabulatedReportSample(const std::size_t nValueChar,
const Check* currentCheck,
const ViolationSample& violation,
const std::size_t checkIx,
const ReportRecordOutput& emitReportRecord) const;
std::pair<std::vector<std::string>, std::string::size_type>
formatPointIDs(const ViolationSample& violation,
const std::size_t checkIx) const;
std::vector<std::string>
collectColumnHeaders(const Check* currentCheck) const;
std::vector<std::size_t>
sortedPointIndices(const ViolationSample& violation,
const std::size_t checkIx) const;
std::size_t numPoints(const ViolationSample& violation,
const std::size_t checkIx) const;
std::size_t numPoints(const std::size_t violationCount) const;
bool anyFailedChecks(const ViolationLevel level) const;
auto index(const ViolationLevel level) const
{
return static_cast<typename ViolationCollection::size_type>(level);
}
template <typename Body>
void checkLoop(Body&& body);
template <typename Body>
void checkLoop(Body&& body) const;
};
} // namespace Opm
#endif // OPM_SATFUNC_CONSISTENCY_CHECK_MODULE_HPP
Opm::SatfuncConsistencyChecks::Check
Call-back interface for an individual check.
Definition SatfuncConsistencyChecks.hpp:52
Opm::SatfuncConsistencyChecks::Check::description
virtual std::string description() const =0
Descriptive textual summary of this check.
Opm::SatfuncConsistencyChecks::Check::~Check
virtual ~Check()=default
Virtual destructor since class has virtual functions.
Opm::SatfuncConsistencyChecks::Check::test
virtual void test(const EclEpsScalingPointsInfo< Scalar > &endPoints)=0
Run specific check against a set of saturation function end-points.
Opm::SatfuncConsistencyChecks::Check::isCritical
virtual bool isCritical() const =0
Whether or not this check is critical to the simulator's ability to run the case.
Opm::SatfuncConsistencyChecks::Check::exportCheckValues
virtual void exportCheckValues(Scalar *exportedCheckValues) const =0
Get a linearised copy of the Scalar values involved in the check.
Opm::SatfuncConsistencyChecks::Check::condition
virtual std::string condition() const =0
Textual representation of the consistency condition.
Opm::SatfuncConsistencyChecks::Check::isViolated
virtual bool isViolated() const =0
Whether or not last set of end-points violated this particular check.
Opm::SatfuncConsistencyChecks::Check::numExportedCheckValues
virtual std::size_t numExportedCheckValues() const =0
Number of Scalar values involved in the check.
Opm::SatfuncConsistencyChecks::Check::columnNames
virtual void columnNames(std::string *headers) const =0
Retrieve names of the exported check values.
Opm::SatfuncConsistencyChecks::anyFailedCriticalChecks
bool anyFailedCriticalChecks() const
Whether or not any checks failed at the Critical level.
Definition SatfuncConsistencyChecks.cpp:171
Opm::SatfuncConsistencyChecks::ViolationLevel
ViolationLevel
Severity level for consistency condition violation.
Definition SatfuncConsistencyChecks.hpp:107
Opm::SatfuncConsistencyChecks::ViolationLevel::NumLevels
@ NumLevels
Implementation helper. Must be last enumerator.
Definition SatfuncConsistencyChecks.hpp:117
Opm::SatfuncConsistencyChecks::ViolationLevel::Critical
@ Critical
Consistency condition violated and we're not able to continue the run.
Definition SatfuncConsistencyChecks.hpp:114
Opm::SatfuncConsistencyChecks::ViolationLevel::Standard
@ Standard
Consistency condition violated, but we're able to continue the run.
Definition SatfuncConsistencyChecks.hpp:110
Opm::SatfuncConsistencyChecks::operator=
SatfuncConsistencyChecks & operator=(const SatfuncConsistencyChecks &rhs)=delete
Deleted assignment operator.
Opm::SatfuncConsistencyChecks::resetCheckSet
void resetCheckSet()
Clear current set of end-point checks.
Definition SatfuncConsistencyChecks.cpp:86
Opm::SatfuncConsistencyChecks::checkEndpoints
void checkEndpoints(const std::size_t pointID, const EclEpsScalingPointsInfo< Scalar > &endPoints)
Run current set of checks against a specific set of end-points.
Definition SatfuncConsistencyChecks.cpp:122
Opm::SatfuncConsistencyChecks::SatfuncConsistencyChecks
SatfuncConsistencyChecks(std::string_view pointName, const std::size_t numSamplePoints)
Constructor.
Definition SatfuncConsistencyChecks.cpp:51
Opm::SatfuncConsistencyChecks::addCheck
void addCheck(std::unique_ptr< Check > check)
Add specific check to in-progress check set.
Definition SatfuncConsistencyChecks.cpp:100
Opm::SatfuncConsistencyChecks::SatfuncConsistencyChecks
SatfuncConsistencyChecks(const SatfuncConsistencyChecks &rhs)=delete
Deleted copy constructor.
Opm::SatfuncConsistencyChecks::~SatfuncConsistencyChecks
~SatfuncConsistencyChecks()=default
Destructor.
Opm::SatfuncConsistencyChecks::anyFailedStandardChecks
bool anyFailedStandardChecks() const
Whether or not any checks failed at the Standard level.
Definition SatfuncConsistencyChecks.cpp:165
Opm::SatfuncConsistencyChecks::PointIDFormatCallback
std::function< std::string(std::size_t)> PointIDFormatCallback
Call-back function type for formatting a numeric end-point ID.
Definition SatfuncConsistencyChecks.hpp:125
Opm::SatfuncConsistencyChecks::setPointIDFormatCallback
SatfuncConsistencyChecks & setPointIDFormatCallback(const PointIDFormatCallback &formatPointID)
Replace formatting function for end-point IDs.
Definition SatfuncConsistencyChecks.hpp:174
Opm::SatfuncConsistencyChecks::collectFailures
void collectFailures(int root, const Parallel::Communication &comm)
Collect consistency violations from all ranks in MPI communicator.
Definition SatfuncConsistencyChecks.cpp:150
Opm::SatfuncConsistencyChecks::finaliseCheckSet
void finaliseCheckSet()
Commit current set of checks and build requisite internal support structures.
Definition SatfuncConsistencyChecks.cpp:109
Opm::SatfuncConsistencyChecks::ReportRecordOutput
std::function< void(std::string_view)> ReportRecordOutput
Call-back function type for outputting a single record of a consistency condition violation report.
Definition SatfuncConsistencyChecks.hpp:122
Opm::SatfuncConsistencyChecks::reportFailures
void reportFailures(const ViolationLevel level, const ReportRecordOutput &emitReportRecord) const
Generate textual summary output of all failed consistency checks at specific level.
Definition SatfuncConsistencyChecks.cpp:178
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43
Opm::EclEpsScalingPointsInfo
Definition SatfuncCheckPointInterface.hpp:28