Merge pull request #1187 from joakim-hove/field-props-has

Add has<T>() method to FieldPropsManager
This commit is contained in:
Joakim Hove 2019-11-04 13:17:06 +01:00 committed by GitHub
commit 015559b5cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 10 deletions

View File

@ -34,19 +34,57 @@ public:
FieldPropsManager(const Deck& deck, const EclipseGrid& grid);
void reset_grid(const EclipseGrid& grid);
/*
Because the FieldProps class can autocreate properties the semantics of
get() and has() is slightly non intuitve:
- The has<T>("KW") method will check if the current FieldProps container
has an installed "KW" keyword; if the container has the keyword in
question it will check if all elements have been assigned a value - only
in that case will it return true. The has<T>("KW") method will *not* try
to create a new keyword.
- The has<T>("KW") method will *not* consult the supported<T>("KW")
method; i.e. if you ask has<T>("UNKNOWN_KEYWORD") you will just get a
false.
- The get<T>("KW") method will try to create a new keyword if it does not
already have the keyword you are asking for. This implies that you can
get the following non intuitive sequence of events:
FieldPropsManager fpm(deck, grid);
fpm.has<int>("SATNUM"); => false
auto satnum = fpm.get<int>("SATNUM"); => SATNUM is autocreated
fpm.has<int>("SATNUM"); => true
- When checking whether the container has the keyword you should rephrase
the question slightly:
* Does the container have the keyword *right now* => has<T>("KW")
* Can the container provide the keyword => ptr = try_get<T>("KW")
- It is quite simple to create a deck where the keywords are only partly
initialized, all the methods in the FieldPropsManager only consider
fully initialized keywords.
*/
template <typename T>
const std::vector<T>& get(const std::string& keyword) const;
template <typename T>
const std::vector<T>* try_get(const std::string& keyword) const;
template <typename T>
bool has(const std::string& keyword) const;
template <typename T>
std::vector<T> get_global(const std::string& keyword) const;
template <typename T>
static bool supported(const std::string& keyword);
private:
std::shared_ptr<FieldProps> fp;
};

View File

@ -77,14 +77,13 @@ void assert_field_properties(const EclipseGrid& grid, const FieldPropsManager& f
"THCONR"};
for (const auto& kw : double_keywords) {
bool has = fp.try_get<double>(kw);
if (has != ep.hasDeckDoubleGridProperty(kw)) {
std::cerr << "FieldPropsManager: " << has << std::endl;
if (fp.has<double>(kw) != ep.hasDeckDoubleGridProperty(kw)) {
std::cerr << "FieldPropsManager: " << fp.has<double>(kw) << std::endl;
std::cerr << "Eclipse3dProperties: " << ep.hasDeckDoubleGridProperty(kw) << std::endl;
throw std::logic_error("Exist Error for: " + kw);
}
if (has) {
if (fp.has<double>(kw)) {
const auto& fp_data = fp.get<double>(kw);
const auto& ep_data = ep.getDoubleGridProperty(kw).compressedCopy(grid);
if (fp_data != ep_data) {
@ -102,14 +101,13 @@ void assert_field_properties(const EclipseGrid& grid, const FieldPropsManager& f
}
for (const auto& kw : int_keywords) {
bool has = fp.try_get<int>(kw);
if (has != ep.hasDeckIntGridProperty(kw)) {
std::cerr << "FieldPropsManager: " << has << std::endl;
if (fp.has<int>(kw) != ep.hasDeckIntGridProperty(kw)) {
std::cerr << "FieldPropsManager: " << fp.has<int>(kw) << std::endl;
std::cerr << "Eclipse3dProperties: " << ep.hasDeckIntGridProperty(kw) << std::endl;
throw std::logic_error("Exists error for: " + kw);
}
if (has) {
if (fp.has<int>(kw)) {
const auto& fp_data = fp.get<int>(kw);
const auto& ep_data = ep.getIntGridProperty(kw).compressedCopy(grid);
if (fp_data != ep_data) {

View File

@ -71,9 +71,21 @@ bool FieldPropsManager::supported(const std::string& keyword) {
return FieldProps::supported<T>(keyword);
}
template <typename T>
bool FieldPropsManager::has(const std::string& keyword) const {
if (!this->fp->has<T>(keyword))
return false;
const auto& data_ptr = this->fp->get<T>(keyword);
return data_ptr.valid();
}
template bool FieldPropsManager::supported<int>(const std::string&);
template bool FieldPropsManager::supported<double>(const std::string&);
template bool FieldPropsManager::has<int>(const std::string&) const;
template bool FieldPropsManager::has<double>(const std::string&) const;
template std::vector<int> FieldPropsManager::get_global(const std::string& keyword) const;
template std::vector<double> FieldPropsManager::get_global(const std::string& keyword) const;

View File

@ -68,6 +68,14 @@ GRID
PORO
1000*0.10 /
BOX
1 3 1 3 1 3 /
PERMX
27*0.6/
)";
std::vector<int> actnum(1000, 1);
for (std::size_t i=0; i< 1000; i += 2)
@ -76,12 +84,19 @@ PORO
Deck deck = Parser{}.parseString(deck_string);
FieldPropsManager fpm(deck, grid);
BOOST_CHECK(!fpm.has<double>("NO-PORO"));
BOOST_CHECK(fpm.has<double>("PORO"));
const auto& poro1 = fpm.get<double>("PORO");
BOOST_CHECK_EQUAL(poro1.size(), grid.getNumActive());
const auto& poro2 = fpm.try_get<double>("PORO");
BOOST_CHECK(poro1 == *poro2);
BOOST_CHECK(!fpm.has<double>("NO-PORO"));
// PERMX keyword is not fully initialized
BOOST_CHECK(!fpm.try_get<double>("PERMX"));
BOOST_CHECK(!fpm.has<double>("PERMX"));
}