ppap4lmp  0.7.2
sta_dump_box.cpp
Go to the documentation of this file.
1 
9 #include <fstream>
10 
11 #include "sta_dump_box.h"
12 #include "../utils/runtime_error.h"
13 #include "../utils/split.h"
14 
15 namespace ut = utils;
16 
17 /* ------------------------------------------------------------------ */
18 
20  Json &data,
21  JsonToVoidFunc check_required_keys,
22  JsonToBoolFunc check_optional_keys)
23 {
24  std::ifstream ifs(filepath);
25  Str line;
26  bool timestep_matches = false;
27  int n_atoms = 0;
28 
29  if (!ifs.is_open())
30  {
31  ut::runtime_error("No such a file '" + filepath + "'");
32  }
33 
34  while (std::getline(ifs, line))
35  {
36  if (line.find("ITEM: TIMESTEP") == 0)
37  {
38  std::getline(ifs, line);
39 
40  if (std::stoi(line) == timestep)
41  {
42  timestep_matches = true;
43  }
44  }
45  else if (
46  line.find("ITEM: NUMBER OF ATOMS") == 0 && !timestep_matches)
47  {
48  std::getline(ifs, line);
49  n_atoms = std::stoi(line);
50  }
51  else if (line.find("ITEM: BOX BOUNDS") == 0 && timestep_matches)
52  {
53  auto strs = ut::split(line);
54 
55  for (int i = 0; i != 3; ++i)
56  {
57  auto key = "pbc_" + Str("xyz").substr(i, 1);
58 
59  data[key] = strs[3+i] == "pp";
60  }
61 
62  for (const Str &dim : {"x", "y", "z"})
63  {
64  std::getline(ifs, line);
65  auto strs = ut::split(line);
66 
67  data["lo_"+dim] = std::stod(strs[0]);
68  data["hi_"+dim] = std::stod(strs[1]);
69  }
70 
71  break;
72  }
73  else if (line.find("ITEM: ATOMS") == 0)
74  {
75  for (int i = 0; i != n_atoms; ++i)
76  {
77  std::getline(ifs, line);
78  }
79  }
80  }
81 }
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
virtual void compute_impl(Json &data, JsonToVoidFunc check_required_keys, JsonToBoolFunc check_optional_keys) override
This method overrides Updater::compute_impl.
nlohmann::json Json
Json is an alias for nlohmann::json.
Definition: json.h:22
This file has a definition of StaDumpBox class, which is a subclass of Starter class.
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
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