ppap4lmp  0.7.2
add_child_positions.cpp
Go to the documentation of this file.
1 
9 #include "add_child_positions.h"
10 #include "../utils/map_to_index.h"
11 #include "../utils/runtime_error.h"
12 
13 namespace ut = utils;
14 
15 /* ------------------------------------------------------------------ */
16 
18  const ElPtr &elem,
19  const Str &child_name_)
20 {
21  ext_generator = elem;
22  child_name = child_name_;
23 }
24 
25 /* ------------------------------------------------------------------ */
26 
28  Json &data,
29  JsonToVoidFunc check_required_keys,
30  JsonToBoolFunc check_optional_keys)
31 {
32  auto key_for_child_ids = child_name + "-ids";
33 
34  check_required_keys({key_for_child_ids, "xu", "yu", "zu"});
35 
36  auto el_children = ext_generator->get_element();
37 
38  el_children->check_required_keys({"id", "xu", "yu", "zu"});
39 
40  auto &children = el_children->get_data();
41 
42  auto id2index_child = ut::map_to_index(children, "id");
43 
44  auto key_x = child_name + "-xs";
45  auto key_y = child_name + "-ys";
46  auto key_z = child_name + "-zs";
47 
48  for (auto &d : data)
49  {
50  double x = d["xu"];
51  double y = d["yu"];
52  double z = d["zu"];
53 
54  auto tmp_x = Json::array();
55  auto tmp_y = Json::array();
56  auto tmp_z = Json::array();
57 
58  for (const int &child_id : d[key_for_child_ids])
59  {
60  auto &child = children[id2index_child[child_id]];
61 
62  tmp_x.push_back(child["xu"].get<double>() - x);
63  tmp_y.push_back(child["yu"].get<double>() - y);
64  tmp_z.push_back(child["zu"].get<double>() - z);
65  }
66 
67  d[key_x].swap(tmp_x);
68  d[key_y].swap(tmp_y);
69  d[key_z].swap(tmp_z);
70  }
71 }
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
virtual void compute_impl(Json &data, JsonToVoidFunc check_required_keys, JsonToBoolFunc check_optional_keys) override
This method overrides Updater::compute_impl.
std::function< bool(const Json &)> JsonToBoolFunc
An alias for a function accepts a Json object and returns a bool.
Definition: updater.h:20
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
This file has a definition of AddChildPositions class, which is a subclass of Adder class...
ShPtr< Generator > ext_generator
Definition: updater.h:64
Namespace for utility functions.
Definition: join.h:14
AddChildPositions(const ElPtr &elem, const Str &child_name_)
Constructor of AddChildPositions class.
std::function< void(const Json &)> JsonToVoidFunc
An alias for a function accepts a Json object.
Definition: updater.h:18
Map< Json, int > map_to_index(const Json &data, const Str &key)
Create a Map object from values of selected property in an array Json object to corresponding index i...