All Classes Functions Variables
EclFilesComparator.hpp
1 /*
2  Copyright 2016 Statoil ASA.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ECLFILESCOMPARATOR_HPP
21 #define ECLFILESCOMPARATOR_HPP
22 
23 #include <vector>
24 #include <string>
25 
26 struct ecl_file_struct;
27 typedef struct ecl_file_struct ecl_file_type;
28 
29 struct ecl_grid_struct;
30 typedef struct ecl_grid_struct ecl_grid_type;
31 struct ecl_kw_struct;
32 typedef struct ecl_kw_struct ecl_kw_type;
33 
34 
39 struct Deviation {
40  double abs = -1;
41  double rel = -1;
42 };
43 
44 
53  private:
54  int file_type;
55  double absTolerance = 0;
56  double relTolerance = 0;
57  protected:
58  ecl_file_type* ecl_file1 = nullptr;
59  ecl_grid_type* ecl_grid1 = nullptr;
60  ecl_file_type* ecl_file2 = nullptr;
61  ecl_grid_type* ecl_grid2 = nullptr;
62  std::vector<std::string> keywords1, keywords2;
63  bool throwOnError = true;
64  mutable size_t num_errors = 0;
65 
69  void keywordValidForComparing(const std::string& keyword) const;
76  unsigned int getEclKeywordData(ecl_kw_type*& ecl_kw1, ecl_kw_type*& ecl_kw2, const std::string& keyword, int occurrence1, int occurrence2) const;
84  template <typename T>
85  void printValuesForCell(const std::string& keyword, int occurrence1, int occurrence2, size_t cell, const T& value1, const T& value2) const;
86 
87  public:
95  ECLFilesComparator(int file_type, const std::string& basename1, const std::string& basename2, double absTolerance, double relTolerance);
98 
100  void throwOnErrors(bool dothrow) { throwOnError = dothrow; }
101 
103  size_t getNoErrors() const { return num_errors; }
104 
106  int getFileType() const {return file_type;}
108  double getAbsTolerance() const {return absTolerance;}
110  double getRelTolerance() const {return relTolerance;}
111 
113  void printKeywords() const;
115  void printKeywordsDifference() const;
116 
119  static Deviation calculateDeviations(double val1, double val2);
122  static double median(std::vector<double> vec);
125  static double average(const std::vector<double>& vec);
126 };
127 
128 
129 
139  private:
140  // These vectors store absolute and relative deviations, respecively. Note that they are whiped clean for every new keyword comparison.
141  std::vector<double> absDeviation, relDeviation;
142  // Keywords which should not contain negative values, i.e. uses allowNegativeValues = false in deviationsForCell():
143  const std::vector<std::string> keywordDisallowNegatives = {"SGAS", "SWAT", "PRESSURE"};
144 
145  // Only compare last occurrence
146  bool onlyLastOccurrence = false;
147 
148  // Prints results stored in absDeviation and relDeviation.
149  void printResultsForKeyword(const std::string& keyword) const;
150 
151  // Function which compares data at specific occurrences and for a specific keyword type. The functions takes two occurrence inputs to also be able to
152  // compare keywords which are shifted relative to each other in the two files. This is for instance handy when running flow with restart from different timesteps,
153  // and comparing the last timestep from the two runs.
154  void boolComparisonForOccurrence(const std::string& keyword, int occurrence1, int occurrence2) const;
155  void charComparisonForOccurrence(const std::string& keyword, int occurrence1, int occurrence2) const;
156  void intComparisonForOccurrence(const std::string& keyword, int occurrence1, int occurrence2) const;
157  void doubleComparisonForOccurrence(const std::string& keyword, int occurrence1, int occurrence2);
158  // deviationsForCell throws an exception if both the absolute deviation AND the relative deviation
159  // are larger than absTolerance and relTolerance, respectively. In addition,
160  // if allowNegativeValues is passed as false, an exception will be thrown when the absolute value
161  // of a negative value exceeds absTolerance. If no exceptions are thrown, the absolute and relative deviations are added to absDeviation and relDeviation.
162  void deviationsForCell(double val1, double val2, const std::string& keyword, int occurrence1, int occurrence2, size_t cell, bool allowNegativeValues = true);
163  public:
171  RegressionTest(int file_type, const std::string& basename1, const std::string& basename2, double absTolerance, double relTolerance):
172  ECLFilesComparator(file_type, basename1, basename2, absTolerance, relTolerance) {}
173 
175  void setOnlyLastOccurrence(bool onlyLastOccurrenceArg) {this->onlyLastOccurrence = onlyLastOccurrenceArg;}
176 
178  // gridCompare() checks if both the number of active and global cells in the two cases are the same. If they are, all cells are looped over to calculate the cell volume deviation for the two cases. If the both the relative and absolute deviation exceeds the tolerances, an exception is thrown.
179  void gridCompare() const;
181  // This function checks if the number of keywords of the two cases are equal, and if it is, resultsForKeyword() is called for every keyword. If not, an exception is thrown.
182  void results();
186  void resultsForKeyword(const std::string& keyword);
187 };
188 
189 
190 
191 
200  private:
201  std::vector<double> cellVolumes;
202  std::vector<double> initialCellValues;
203 
204  // These are the only keywords which are compared, since SWAT should be "1 - SOIL - SGAS", this keyword is omitted.
205  const std::vector<std::string> keywordWhitelist = {"SGAS", "SWAT", "PRESSURE"};
206 
207  void setCellVolumes();
208  void initialOccurrenceCompare(const std::string& keyword);
209  void occurrenceCompare(const std::string& keyword, int occurrence) const;
210  public:
217  IntegrationTest(const std::string& basename1, const std::string& basename2, double absTolerance, double relTolerance);
218 
221  bool elementInWhitelist(const std::string& keyword) const;
224  void equalNumKeywords() const;
225 
228  void results();
231 
242  void resultsForKeyword(const std::string& keyword);
243 };
244 
245 #endif
~ECLFilesComparator()
Closing the ECLIPSE files.
Definition: EclFilesComparator.cpp:173
RegressionTest(int file_type, const std::string &basename1, const std::string &basename2, double absTolerance, double relTolerance)
Sets up the regression test.
Definition: EclFilesComparator.hpp:171
void printValuesForCell(const std::string &keyword, int occurrence1, int occurrence2, size_t cell, const T &value1, const T &value2) const
Prints values for a given keyword, occurrence and cell.
Definition: EclFilesComparator.cpp:97
void equalNumKeywords() const
Checks if the number of keywords equal in the two input cases.
Definition: EclFilesComparator.cpp:637
void printKeywords() const
Print all keywords and their respective Eclipse type for the two input cases.
Definition: EclFilesComparator.cpp:182
void results()
Calculates deviations for all keywords.
Definition: EclFilesComparator.cpp:422
IntegrationTest(const std::string &basename1, const std::string &basename2, double absTolerance, double relTolerance)
Sets up the integration test.
Definition: EclFilesComparator.cpp:621
void resultsForKeyword(const std::string &keyword)
Finds deviations for a specific keyword.
Definition: EclFilesComparator.cpp:654
void gridCompare() const
Compares grid properties of the two cases.
Definition: EclFilesComparator.cpp:382
static Deviation calculateDeviations(double val1, double val2)
Calculate deviations for two values.
Definition: EclFilesComparator.cpp:221
double getRelTolerance() const
Returns the relative tolerance stored as a private member variable in the class.
Definition: EclFilesComparator.hpp:110
double rel
Relative deviation.
Definition: EclFilesComparator.hpp:41
void results()
Finds deviations for all supported keywords.
Definition: EclFilesComparator.cpp:647
Deviation struct.
Definition: EclFilesComparator.hpp:39
double abs
Absolute deviation.
Definition: EclFilesComparator.hpp:40
ECLFilesComparator(int file_type, const std::string &basename1, const std::string &basename2, double absTolerance, double relTolerance)
Open ECLIPSE files and set tolerances and keywords.
Definition: EclFilesComparator.cpp:114
void setOnlyLastOccurrence(bool onlyLastOccurrenceArg)
Option to only compare last occurrence.
Definition: EclFilesComparator.hpp:175
void resultsForKeyword(const std::string &keyword)
Calculates deviations for a specific keyword.
Definition: EclFilesComparator.cpp:443
A class for executing a integration test for two ECLIPSE files.
Definition: EclFilesComparator.hpp:199
static double average(const std::vector< double > &vec)
Calculate average of a vector.
Definition: EclFilesComparator.cpp:254
void keywordValidForComparing(const std::string &keyword) const
Checks if the keyword exists in both cases.
Definition: EclFilesComparator.cpp:66
void throwOnErrors(bool dothrow)
Set whether to throw on errors or not.
Definition: EclFilesComparator.hpp:100
unsigned int getEclKeywordData(ecl_kw_type *&ecl_kw1, ecl_kw_type *&ecl_kw2, const std::string &keyword, int occurrence1, int occurrence2) const
Stores keyword data for a given occurrence.
Definition: EclFilesComparator.cpp:78
static double median(std::vector< double > vec)
Calculate median of a vector.
Definition: EclFilesComparator.cpp:236
bool elementInWhitelist(const std::string &keyword) const
Checks if a keyword is supported for comparison.
Definition: EclFilesComparator.cpp:630
size_t getNoErrors() const
Returns the number of errors encountered in the performed comparisons.
Definition: EclFilesComparator.hpp:103
bool throwOnError
Throw on first error.
Definition: EclFilesComparator.hpp:63
int getFileType() const
Returns the ECLIPSE filetype of this.
Definition: EclFilesComparator.hpp:106
double getAbsTolerance() const
Returns the absolute tolerance stored as a private member variable in the class.
Definition: EclFilesComparator.hpp:108
void printKeywordsDifference() const
Print common and uncommon keywords for the two input cases.
Definition: EclFilesComparator.cpp:195
A class for comparing ECLIPSE files.
Definition: EclFilesComparator.hpp:52
A class for executing a regression test for two ECLIPSE files.
Definition: EclFilesComparator.hpp:138