OGS
TestDefinition.cpp
Go to the documentation of this file.
1 
12 #include "TestDefinition.h"
13 
14 #include <cmath>
15 #include <cstdlib>
16 #include <regex>
17 #include <vector>
18 
19 #include "BaseLib/ConfigTree.h"
20 #include "BaseLib/Error.h"
21 #include "BaseLib/FileTools.h"
22 #include "filesystem.h"
23 #ifdef USE_PETSC
24 #include <petsc.h>
25 
26 #include "MeshLib/IO/VtkIO/VtuInterface.h" // For petsc file name conversion.
27 #endif
28 
29 namespace
30 {
32 bool isConvertibleToDouble(std::string const& s)
33 {
34  std::size_t pos = 0;
35  double value;
36  try
37  {
38  value = std::stod(s, &pos);
39  }
40  catch (...)
41  {
42  OGS_FATAL("The given string '{:s}' is not convertible to double.", s);
43  }
44  if (pos != s.size())
45  {
46  OGS_FATAL(
47  "Only {:d} characters were used for double conversion of string "
48  "'{:s}'",
49  pos, s);
50  }
51 
52  if (std::isnan(value))
53  {
54  OGS_FATAL("The given string '{:s}' results in a NaN value.", s);
55  }
56  return true;
57 }
58 
60 std::string safeString(std::string const& s)
61 {
62  return "\"" + s + "\"";
63 }
64 
67 std::string findVtkdiff()
68 {
69  // Try to read the VTKDIFF_EXE environment variable.
70  if (const char* vtkdiff_exe_environment_variable =
71  std::getenv("VTKDIFF_EXE"))
72  {
73  std::string const vtkdiff_exe{vtkdiff_exe_environment_variable};
74  DBUG("VTKDIFF_EXE set to {:s}.", vtkdiff_exe);
75 
76  //
77  // Sanity checks.
78  //
79  { // The base name is 'vtkdiff'
80  auto const& base_name =
82  if (base_name != "vtkdiff")
83  {
84  OGS_FATAL(
85  "The VTKDIFF_EXE environment variable does not point to "
86  "'vtkdiff'. VTKDIFF_EXE='{:s}'",
87  vtkdiff_exe);
88  }
89  }
90  { // vtkdiff must exist.
91  if (!BaseLib::IsFileExisting(vtkdiff_exe))
92  {
93  OGS_FATAL(
94  "The VTKDIFF_EXE points to a non-existing file. "
95  "VTKDIFF_EXE='{:s}'",
96  vtkdiff_exe);
97  }
98  }
99 
100  //
101  // Test the actual call.
102  //
103  int const return_value =
104  // TODO (naumov) replace system call with output consuming call
105  // (fork + execl seems to be more safe), and extract the vtkdiff
106  // call to common function. Also properly escape all strings in
107  // command lines.
108  // Reference for POSIX and Windows:
109  // https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=87152177
110  // Take care when using fork, which might copy resources.
111  std::system((vtkdiff_exe + " --version").c_str());
112  if (return_value == 0)
113  {
114  return vtkdiff_exe;
115  }
116  WARN(
117  "Calling {:s} from the VTKDIFF_EXE environment variable didn't "
118  "work as expected. Return value was {:d}.",
119  vtkdiff_exe, return_value);
120  }
121 
122  std::string const vtkdiff_exe{"vtkdiff"};
123  std::vector<std::string> const paths = {"", "bin"};
124  auto const path =
125  find_if(begin(paths), end(paths),
126  [&vtkdiff_exe](std::string const& path)
127  {
128  int const return_value =
129  // TODO (naumov) replace system call with output
130  // consuming call as in an above todo comment.
131  std::system((BaseLib::joinPaths(path, vtkdiff_exe) +
132  " --version")
133  .c_str());
134  return return_value == 0;
135  });
136  if (path == end(paths))
137  {
138  OGS_FATAL("vtkdiff not found.");
139  }
140  return BaseLib::joinPaths(*path, vtkdiff_exe);
141 }
142 
143 } // namespace
144 
145 namespace ApplicationsLib
146 {
148  std::string const& reference_path,
149  std::string const& output_directory)
150 {
151  if (reference_path.empty())
152  {
153  OGS_FATAL(
154  "Reference path containing expected result files can not be "
155  "empty.");
156  }
157 
158  std::string const vtkdiff = findVtkdiff();
159 
160  // Construct command lines for each entry.
162  auto const& vtkdiff_configs = config_tree.getConfigSubtreeList("vtkdiff");
163  _command_lines.reserve(vtkdiff_configs.size());
164  for (auto const& vtkdiff_config : vtkdiff_configs)
165  {
166  std::string const& field_name =
168  vtkdiff_config.getConfigParameter<std::string>("field");
169  DBUG("vtkdiff will compare field '{:s}'.", field_name);
170 
171  std::vector<std::string> filenames;
172  if (auto const regex_string =
174  vtkdiff_config.getConfigParameterOptional<std::string>("regex"))
175  {
176  // TODO: insert rank into regex for mpi case
177  DBUG("vtkdiff regex is '{}'.", *regex_string);
178  auto const regex = std::regex(*regex_string);
179  for (auto const& p :
180  fs::directory_iterator(fs::path(reference_path)))
181  {
182  auto const filename = p.path().filename().string();
183  if (std::regex_match(filename, regex))
184  {
185  DBUG(" -> matched '{}'", filename);
186  filenames.push_back(filename);
187  }
188  }
189  }
190  else
191  {
192 #ifdef USE_PETSC
193  int rank;
194  MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
195  int mpi_size;
196  MPI_Comm_size(PETSC_COMM_WORLD, &mpi_size);
197  std::string const& filename =
200  vtkdiff_config.getConfigParameter<std::string>("file")) +
201  "_" + std::to_string(rank) + ".vtu";
202 #else
203  std::string const& filename =
205  vtkdiff_config.getConfigParameter<std::string>("file");
206 #endif // OGS_USE_PETSC
207  filenames.push_back(filename);
208  }
209 
210  if (empty(filenames))
211  {
212  OGS_FATAL(
213  "No files from test definitions were added for tests but {} "
214  "{:s} specified.",
215  size(vtkdiff_configs),
216  (size(vtkdiff_configs) == 1 ? "test was" : "tests were"));
217  }
218 
219  auto const absolute_tolerance =
221  vtkdiff_config.getConfigParameter<std::string>("absolute_tolerance",
222  "");
223  if (!absolute_tolerance.empty() &&
224  !isConvertibleToDouble(absolute_tolerance))
225  {
226  OGS_FATAL(
227  "The absolute tolerance value '{:s}' is not convertible to "
228  "double.",
229  absolute_tolerance);
230  }
231  std::string const absolute_tolerance_parameter =
232  "--abs " + absolute_tolerance;
233  auto const relative_tolerance =
235  vtkdiff_config.getConfigParameter<std::string>("relative_tolerance",
236  "");
237  if (!relative_tolerance.empty() &&
238  !isConvertibleToDouble(relative_tolerance))
239  {
240  OGS_FATAL(
241  "The relative tolerance value '{:s}' is not convertible to "
242  "double.",
243  relative_tolerance);
244  }
245  std::string const relative_tolerance_parameter =
246  "--rel " + relative_tolerance;
247 
248  for (auto const& filename : filenames)
249  {
250  std::string const& output_filename =
251  BaseLib::joinPaths(output_directory, filename);
252  _output_files.push_back(output_filename);
253  std::string const& reference_filename =
254  BaseLib::joinPaths(reference_path, filename);
255 
256  //
257  // Construct command line.
258  //
259  std::string command_line =
260  vtkdiff + " -a " + safeString(field_name) + " -b " +
261  safeString(field_name) + " " + safeString(reference_filename) +
262  " " + safeString(output_filename) + " " +
263  absolute_tolerance_parameter + " " +
264  relative_tolerance_parameter;
265  INFO("Will run '{:s}'", command_line);
266  _command_lines.emplace_back(std::move(command_line));
267  }
268  }
269 }
270 
272 {
273  std::vector<int> return_values;
274  transform(begin(_command_lines), end(_command_lines),
275  back_inserter(return_values),
276  [](std::string const& command_line)
277  {
278  int const return_value = std::system(command_line.c_str());
279  if (return_value != 0)
280  {
281  WARN("Return value {:d} was returned by '{:s}'.",
282  return_value, command_line);
283  }
284  return return_value;
285  });
286  return !return_values.empty() &&
287  all_of(begin(return_values), end(return_values),
288  [](int const& return_value) { return return_value == 0; });
289 }
290 
291 std::vector<std::string> const& TestDefinition::getOutputFiles() const
292 {
293  return _output_files;
294 }
295 
296 std::size_t TestDefinition::numberOfTests() const
297 {
298  return size(_command_lines);
299 }
300 } // namespace ApplicationsLib
#define OGS_FATAL(...)
Definition: Error.h:26
Filename manipulation routines.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void DBUG(char const *fmt, Args const &... args)
Definition: Logging.h:27
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Implementation of the VtuInterface class.
std::vector< std::string > _output_files
std::vector< std::string > _command_lines
TestDefinition(BaseLib::ConfigTree const &config_tree, std::string const &reference_path, std::string const &output_directory)
std::vector< std::string > const & getOutputFiles() const
Range< SubtreeIterator > getConfigSubtreeList(std::string const &root) const
Definition: ConfigTree.cpp:169
bool IsFileExisting(const std::string &strFilename)
Returns true if given file exists.
Definition: FileTools.cpp:43
std::string extractBaseNameWithoutExtension(std::string const &pathname)
Definition: FileTools.cpp:180
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:212
std::string getVtuFileNameForPetscOutputWithoutExtension(std::string const &file_name)
bool isConvertibleToDouble(std::string const &s)
Test if the given string is convertible to a valid double value, not a NaN.
std::string safeString(std::string const &s)
Wraps a string into double ticks.