Scine::Swoose  2.1.0
This is the SCINE module Swoose.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
ReferenceDataForTests.h
Go to the documentation of this file.
1 
8 #ifndef SWOOSE_REFERENCEDATAFORTESTS_H
9 #define SWOOSE_REFERENCEDATAFORTESTS_H
10 
11 #include "../Files/tests_file_location.h"
12 #include <Eigen/Core>
13 #include <fstream>
14 #include <list>
15 #include <regex>
16 #include <vector>
17 
18 namespace Scine {
19 namespace Swoose {
20 using ForcesCollection = Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>;
21 
22 namespace ReferenceDataForTests {
23 
24 inline std::vector<double> parseReferenceEnergies() {
25  std::vector<double> energies;
26 
27  std::ifstream indata(reference_energies_file);
28  if (!indata.is_open()) {
29  throw std::runtime_error("The file containing the reference energies for the tests could not be opened.");
30  }
31 
32  std::string line;
33  while (line.empty()) {
34  std::getline(indata, line);
35  }
36 
37  while (!line.empty()) {
38  std::list<int> listForOneAtom;
39  std::regex rgx("\\s+");
40  std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
41  std::sregex_token_iterator end;
42 
43  if (*iter == "")
44  iter++;
45 
46  double value = std::stod(*iter);
47  energies.push_back(value);
48 
49  std::getline(indata, line);
50  }
51 
52  return energies;
53 }
54 
55 inline std::vector<ForcesCollection> parseReferenceForces(std::string filename, int nStruct, int nAtoms) {
56  std::vector<Swoose::ForcesCollection> forces;
57  for (int i = 0; i < nStruct; ++i) {
58  ForcesCollection f(nAtoms, 3);
59  forces.push_back(f);
60  }
61 
62  std::ifstream indata(filename);
63  if (!indata.is_open()) {
64  throw std::runtime_error("The file containing the reference forces for the tests could not be opened.");
65  }
66 
67  std::string line;
68  while (line.empty()) {
69  std::getline(indata, line);
70  }
71 
72  while (!line.empty()) {
73  std::list<int> listForOneAtom;
74  std::regex rgx("\\s+");
75  std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
76  std::sregex_token_iterator end;
77 
78  if (*iter == "")
79  iter++;
80 
81  int structureIndex = std::stoi(*iter++);
82  int atomIndex = std::stoi(*iter++);
83  int dimension = std::stoi(*iter++);
84  double value = std::stod(*iter++);
85 
86  forces.at(structureIndex)(atomIndex, dimension) = value;
87 
88  std::getline(indata, line);
89  }
90 
91  return forces;
92 }
93 
94 } // namespace ReferenceDataForTests
95 } // namespace Swoose
96 } // namespace Scine
97 
98 #endif // SWOOSE_REFERENCEDATAFORTESTS_H