SPH
Common.cpp
Go to the documentation of this file.
1 #include "Common.h"
2 #include "quantities/Iterate.h"
3 
5 
6 Outcome areFilesIdentical(const Path& path1, const Path& path2) {
7  if (!FileSystem::pathExists(path1) || !FileSystem::pathExists(path2)) {
8  return makeFailed("One or both files do not exist");
9  }
10  if (FileSystem::fileSize(path1) != FileSystem::fileSize(path2)) {
11  return makeFailed("Files have different sizes");
12  }
13  std::ifstream ifs1(path1.native());
14  std::ifstream ifs2(path2.native());
15  StaticArray<char, 1024> buffer1, buffer2;
16 
17  while (ifs1 && ifs2) {
18  ifs1.read(&buffer1[0], buffer1.size());
19  ifs2.read(&buffer2[0], buffer2.size());
20 
21  if (buffer1 != buffer2) {
22  return makeFailed("Difference found at position ", ifs1.tellg());
23  }
24  }
25 
26  return SUCCESS;
27 }
28 
29 Outcome areFilesApproxEqual(const Path& path1, const Path& path2) {
30  BinaryInput input;
31  Storage storage1, storage2;
32  Statistics stats1, stats2;
33  Outcome o1 = input.load(path1, storage1, stats1);
34  Outcome o2 = input.load(path2, storage2, stats2);
35  if (!o1 || !o2) {
36  return o1 && o2;
37  }
38  if (storage1.getParticleCnt() != storage2.getParticleCnt()) {
39  return makeFailed("Different particle counts");
40  }
41  if (storage1.getMaterialCnt() != storage2.getMaterialCnt()) {
42  return makeFailed("Different material counts");
43  }
44  if (storage1.getQuantityCnt() != storage2.getQuantityCnt()) {
45  return makeFailed("Different quantity counts");
46  }
47 
48  bool buffersEqual = true;
49  iteratePair<VisitorEnum::ALL_BUFFERS>(storage1, storage2, [&buffersEqual](auto& b1, auto& b2) {
50  if (b1.size() != b2.size()) {
51  buffersEqual = false;
52  } else {
53  for (Size i = 0; i < b1.size(); ++i) {
54  buffersEqual &= almostEqual(b1[i], b2[i], EPS);
55  }
56  }
57  });
58 
60  if (!buffersEqual) {
61  return makeFailed("Different quantity values");
62  } else {
63  return SUCCESS;
64  }
65 }
66 
67 void measureRun(const Path& file, Function<void()> run) {
68  Timer timer;
69  run();
70  const int64_t duration = timer.elapsed(TimerUnit::SECOND);
72  logger.write("\"", __DATE__, "\" ", duration);
73 }
74 
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
NAMESPACE_SPH_BEGIN Outcome areFilesIdentical(const Path &path1, const Path &path2)
Checks whether the two files are identical (to the bit).
Definition: Common.cpp:6
void measureRun(const Path &file, Function< void()> run)
Definition: Common.cpp:67
Outcome areFilesApproxEqual(const Path &path1, const Path &path2)
Checks if two .ssf files are almost equal (may have eps-differences in quantities).
Definition: Common.cpp:29
Functions for iterating over individual quantities in Storage.
#define NAMESPACE_SPH_END
Definition: Object.h:12
const SuccessTag SUCCESS
Global constant for successful outcome.
Definition: Outcome.h:141
INLINE Outcome makeFailed(TArgs &&... args)
Constructs failed object with error message.
Definition: Outcome.h:157
Input for the binary file, generated by BinaryOutput.
Definition: Output.h:352
virtual Outcome load(const Path &path, Storage &storage, Statistics &stats) override
Loads data from the file into the storage.
Definition: Output.cpp:718
File output logger.
Definition: Logger.h:160
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
Definition: Logger.h:37
Object representing a path on a filesystem.
Definition: Path.h:17
std::string native() const
Returns the native version of the path.
Definition: Path.cpp:71
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
INLINE TCounter size() const
Returns the current size of the array (number of constructed elements).
Definition: StaticArray.h:147
Object holding various statistics about current run.
Definition: Statistics.h:22
Container storing all quantities used within the simulations.
Definition: Storage.h:230
Size getMaterialCnt() const
Return the number of materials in the storage.
Definition: Storage.cpp:437
Size getParticleCnt() const
Returns the number of particles.
Definition: Storage.cpp:445
Size getQuantityCnt() const
Returns the number of stored quantities.
Definition: Storage.cpp:441
Basic time-measuring tool. Starts automatically when constructed.
Definition: Timer.h:27
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
Definition: Timer.cpp:55
bool pathExists(const Path &path)
Checks if a file or directory exists (or more precisely, if a file or directory is accessible).
Definition: FileSystem.cpp:21
Size fileSize(const Path &path)
Returns the size of a file.
Definition: FileSystem.cpp:29