ppap4lmp  0.7.2
sta_dump_atoms.cpp
Go to the documentation of this file.
1 
9 #include <fstream>
10 #include <tuple>
11 
12 #include "sta_dump_atoms.h"
13 #include "../utils/runtime_error.h"
14 #include "../utils/split.h"
15 
16 namespace ut = utils;
17 
18 /* ------------------------------------------------------------------ */
19 
21  Json &data,
22  JsonToVoidFunc check_required_keys,
23  JsonToBoolFunc check_optional_keys)
24 {
25  std::ifstream ifs(filepath);
26  Str line;
27  bool timestep_matches = false;
28  int n_atoms = 0;
29 
30  if (!ifs.is_open())
31  {
32  ut::runtime_error("No such a file '" + filepath + "'");
33  }
34 
35  while (std::getline(ifs, line))
36  {
37  if (line.find("ITEM: TIMESTEP") == 0)
38  {
39  std::getline(ifs, line);
40 
41  if (std::stoi(line) == timestep)
42  {
43  timestep_matches = true;
44  }
45  }
46  else if (line.find("ITEM: NUMBER OF ATOMS") == 0)
47  {
48  std::getline(ifs, line);
49  n_atoms = std::stoi(line);
50 
51  if (timestep_matches)
52  {
53  data = Json::array();
54  data.get_ref<Json::array_t&>().resize(n_atoms);
55  }
56  }
57  else if (line.find("ITEM: ATOMS") == 0)
58  {
59  if (timestep_matches)
60  {
61  auto keys = ut::split(line);
62  keys.erase(keys.begin(), keys.begin()+2);
63 
64  std::getline(ifs, line);
65 
66  Vec<bool> is_int_vector;
67 
68  for (const auto &s : ut::split(line))
69  {
70  // NOTE: If string containes '.', it is converted to float.
71  is_int_vector.push_back(s.find(".") == Str::npos);
72  }
73 
74  // read & set data from the 1st line
75 
76  auto &d = data.front();
77 
78  auto strs = ut::split(line);
79 
80  for (int i = 0; i != is_int_vector.size(); ++i)
81  {
82  if (is_int_vector[i])
83  {
84  d[keys[i]] = std::stoi(strs[i]);
85  }
86  else
87  {
88  d[keys[i]] = std::stod(strs[i]);
89  }
90  }
91 
92  // read & set data from the remaining lines
93 
94  // each tuple contains info of key, int/float, delimiter
95  Vec<std::tuple<Str, bool, char>> tuples(is_int_vector.size());
96  for (int i = 0; i < tuples.size(); ++i)
97  {
98  tuples[i] = std::make_tuple(
99  keys[i], is_int_vector[i], i+1 == tuples.size() ? '\n' : ' ');
100  }
101 
102  for (auto it = data.begin()+1; it != data.end(); ++it)
103  {
104  for (auto jt = tuples.cbegin(); jt != tuples.cend(); ++jt)
105  {
106  Str str;
107 
108  while (str.empty())
109  {
110  std::getline(ifs, str, std::get<2>(*jt));
111  }
112 
113  if (std::get<1>(*jt))
114  {
115  (*it)[std::get<0>(*jt)] = std::stoi(str);
116  }
117  else
118  {
119  (*it)[std::get<0>(*jt)] = std::stod(str);
120  }
121  }
122  }
123 
124  break;
125  }
126  else
127  {
128  for (int i = 0; i != n_atoms; ++i)
129  {
130  std::getline(ifs, line);
131  }
132  }
133  }
134  }
135 }
Str filepath
Definition: sta_dump.h:33
std::function< bool(const Json &)> JsonToBoolFunc
An alias for a function accepts a Json object and returns a bool.
Definition: updater.h:20
Vec< Str > split(const Str &str, char delim=' ')
Mimicking Python&#39;s split.
Definition: split.cpp:16
std::string Str
Str is an alias for string.
Definition: std.h:21
nlohmann::json Json
Json is an alias for nlohmann::json.
Definition: json.h:22
std::vector< T > Vec
Vec is an alias for vector (same as list in Python).
Definition: std.h:27
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
virtual void compute_impl(Json &data, JsonToVoidFunc check_required_keys, JsonToBoolFunc check_optional_keys) override
This method overrides Updater::compute_impl.
This file has a definition of StaDumpAtoms class, which is a subclass of Starter class.
Namespace for utility functions.
Definition: join.h:14
std::function< void(const Json &)> JsonToVoidFunc
An alias for a function accepts a Json object.
Definition: updater.h:18
int timestep
Definition: sta_dump.h:29