ppap4lmp  0.7.2
sta_beads.cpp
Go to the documentation of this file.
1 
9 #include "sta_beads.h"
10 #include "../utils/runtime_error.h"
11 
12 namespace ut = utils;
13 
14 /* ------------------------------------------------------------------ */
15 
17  const ElPtr &el_mols,
18  const Vec<Json> &scheme)
19 {
20  ext_generator = el_mols;
21  mol_type_to_abst_beads[1] = scheme;
22 }
23 
24 /* ------------------------------------------------------------------ */
25 
27  const ElPtr &el_mols,
28  const Map<int,Vec<Json>> &schemes)
29 {
30  ext_generator = el_mols;
31  mol_type_to_abst_beads = schemes;
32 }
33 
34 /* ------------------------------------------------------------------ */
35 
37 {
38  int counter = 0;
39  int type_counter = 0;
40  int weights_counter = 0;
41 
42  for (const auto &item : mol_type_to_abst_beads)
43  {
44  for (const auto &abst_bead : item.second)
45  {
46  const auto it_end = abst_bead.cend();
47  const auto it_index = abst_bead.find("indices-in-mol");
48 
49  if (it_index == it_end)
50  {
52  "Mapping to beads must be specified by 'indices-in-mol'");
53  }
54 
55  if (abst_bead.find("type") != it_end)
56  {
57  type_counter++;
58  }
59 
60  auto it_weights = abst_bead.find("weights");
61 
62  if (it_weights != it_end)
63  {
64  if ((*it_index).size() != (*it_weights).size())
65  {
67  "The numbers of elements in 'indices-in-mol' and 'weights' "
68  "are inconsistent");
69  }
70 
71  weights_counter++;
72  }
73 
74  counter++;
75  }
76  }
77 
78  std::pair<bool,bool> have_type_andor_weights = std::make_pair(false, false);
79 
80  if (type_counter == counter)
81  {
82  // all the beads have 'type' attribute
83  have_type_andor_weights.first = true;
84  }
85  else if (type_counter != 0)
86  {
87  ut::runtime_error("The number of 'type' is invalid");
88  }
89 
90  if (weights_counter == counter)
91  {
92  // all the beads have 'weights' attribute
93  have_type_andor_weights.second = true;
94  }
95  else if (weights_counter != 0)
96  {
97  ut::runtime_error("The number of 'weights' is invalid");
98  }
99 
100  return have_type_andor_weights;
101 }
102 
103 /* ------------------------------------------------------------------ */
104 
106  Json &data,
107  JsonToVoidFunc check_required_keys,
108  JsonToBoolFunc check_optional_keys)
109 {
110  auto have_type_andor_weights = check_mol_type_to_abst_beads();
111 
112  auto el_mols = ext_generator->get_element();
113 
114  el_mols->check_required_keys("atom-ids");
115 
116  auto &mols = el_mols->get_data();
117 
118  int bead_id = 0;
119 
120  for (const auto &mol : mols)
121  {
122  auto &atom_ids = mol["atom-ids"];
123  auto &abst_beads = mol_type_to_abst_beads[mol.value("type", 1)];
124 
125  for (const auto &abst_bead : abst_beads)
126  {
127  data.push_back({{"id", ++bead_id}, {"mol", mol["id"]}});
128 
129  auto &back = data.back();
130 
131  auto &ids_ref = back["atom-ids"];
132 
133  for (const int &index : abst_bead["indices-in-mol"])
134  {
135  ids_ref.push_back(atom_ids.at(index));
136  }
137 
138  if (have_type_andor_weights.first)
139  {
140  back["type"] = abst_bead["type"];
141  }
142 
143  if (have_type_andor_weights.second)
144  {
145  back["atom-weights"] = abst_bead["weights"];
146  }
147  }
148  }
149 }
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
StaBeads(const ElPtr &el_mols, const Vec< Json > &scheme)
Constructor of StaBeads class for molecules all of whose type property are 1, or molecules all of whi...
Definition: sta_beads.cpp:16
std::function< bool(const Json &)> JsonToBoolFunc
An alias for a function accepts a Json object and returns a bool.
Definition: updater.h:20
nlohmann::json Json
Json is an alias for nlohmann::json.
Definition: json.h:22
This file has a definition of StaBeads class, which is a subclass of Starter class.
ShPtr< Generator > ext_generator
Definition: updater.h:64
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.
Definition: sta_beads.cpp:105
std::pair< bool, bool > check_mol_type_to_abst_beads()
Definition: sta_beads.cpp:36
Map< int, Vec< Json > > mol_type_to_abst_beads
Definition: sta_beads.h:41
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
std::unordered_map< T, U > Map
Map is an alias for unordered map (same as dict in Python).
Definition: std.h:38