ppap4lmp  0.7.2
add_special_bonds.cpp
Go to the documentation of this file.
1 
9 #include "add_special_bonds.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 &el_mols,
19  const Vec<Vec<int>> &scheme)
20 {
21  ext_generator = el_mols;
23 }
24 
25 /* ------------------------------------------------------------------ */
26 
28  const ElPtr &el_mols,
29  const Map<int,Vec<Vec<int>>> &type_to_scheme)
30 {
31  ext_generator = el_mols;
32  mol_type_to_sbonds_list_in_mol = type_to_scheme;
33 }
34 
35 /* ------------------------------------------------------------------ */
36 
38  Json &data,
39  JsonToVoidFunc check_required_keys,
40  JsonToBoolFunc check_optional_keys)
41 {
42  if (check_optional_keys("special-bonds"))
43  {
44  ut::runtime_error("Key 'special-bonds' already exists");
45  }
46 
47  auto el_mols = ext_generator->get_element();
48 
49  el_mols->check_required_keys("atom-ids");
50 
51  auto &mols = el_mols->get_data();
52 
53  auto id2index = ut::map_to_index(data, "id");
54 
55  for (const auto &mol : mols)
56  {
57  auto &atom_ids = mol["atom-ids"];
58  auto sbonds_list_in_mol
59  = mol_type_to_sbonds_list_in_mol[mol.value("type", 1)];
60 
61  auto n_atoms_in_mol = atom_ids.size();
62 
63  if (sbonds_list_in_mol.size() != n_atoms_in_mol)
64  {
66  "The numbers of atoms in a molecule are inconsistent");
67  }
68 
69  for (int i = 0; i != n_atoms_in_mol; ++i)
70  {
71  auto absolute_sbonds = Json::array();
72 
73  for (const int &index: sbonds_list_in_mol[i])
74  {
75  // convert relative index in a molecule to absolute atom id
76  absolute_sbonds.push_back(atom_ids.at(index));
77  }
78 
79  data[id2index[atom_ids[i]]]["special-bonds"].swap(absolute_sbonds);
80  }
81  }
82 }
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
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 AddSpecialBonds class, which is a subclass of Adder class...
Map< int, Vec< Vec< int > > > mol_type_to_sbonds_list_in_mol
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
AddSpecialBonds(const ElPtr &el_mols, const Vec< Vec< int >> &scheme)
Constructor of AddSpecialBonds class for molecules all of whose type property are 1...
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.
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
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...