ppap4lmp  0.7.2
pro_distance_in_molecule.cpp
Go to the documentation of this file.
1 
10 #include "../utils/map_to_index.h"
11 #include "../utils/runtime_error.h"
12 
13 namespace ut = utils;
14 
15 /* ------------------------------------------------------------------ */
16 
18  const ElPtr &mols,
19  const ElPtr &atoms)
20 {
22  new GenDict({{"Mols", mols}, {"Atoms", atoms}})));
23 }
24 
25 /* ------------------------------------------------------------------ */
26 
28  const Vec<std::pair<ElPtr,ElPtr>> &pairs)
29 {
30  Vec<ShPtr<GenDict>> gens;
31 
32  for (const auto &pair : pairs)
33  {
34  gens.push_back(ShPtr<GenDict>(
35  new GenDict({{"Mols", pair.first}, {"Atoms", pair.second}})));
36  }
37 
38  register_generators(gens);
39 }
40 
41 /* ------------------------------------------------------------------ */
42 
44  const int index)
45 {
46  auto el_mols = generators[index]->get_element("Mols");
47 
48  // NOTE: `id` property is check_required_keys to ensure data is sorted.
49  el_mols->check_required_keys({"id", "atom-ids"});
50 
51  auto &mols = el_mols->get_data();
52 
53  auto el_atoms = generators[index]->get_element("Atoms");
54 
55  el_atoms->check_required_keys({"xu", "yu", "zu", "id"});
56 
57  auto &atoms = el_atoms->get_data();
58 
59  auto id2index_atom = ut::map_to_index(atoms, "id");
60 
61  Vec<double> distance2_tmp;
62 
63  for (const auto &mol : mols)
64  {
65  if (mol.value("type", 1) != target_moltype) continue;
66 
67  auto &atom_ids = mol["atom-ids"];
68 
69  auto &atom1 = atoms[id2index_atom[atom_ids.at(index1_in_mol)]];
70  auto &atom2 = atoms[id2index_atom[atom_ids.at(index2_in_mol)]];
71 
72  auto dx = atom2["xu"].get<double>() - atom1["xu"].get<double>();
73  auto dy = atom2["yu"].get<double>() - atom1["yu"].get<double>();
74  auto dz = atom2["zu"].get<double>() - atom1["zu"].get<double>();
75 
76  distance2_tmp.push_back(dx*dx+dy*dy+dz*dz);
77  }
78 
80  distance2_tmp.data(), distance2_tmp.size());
81 }
82 
83 /* ------------------------------------------------------------------ */
84 
86 {
88 }
89 
90 /* ------------------------------------------------------------------ */
91 
93 {
94  const auto n_mols = distance2_traj.front().size();
95 
96  for (const auto &d2 : distance2_traj)
97  {
98  if (n_mols != d2.size())
99  {
100  ut::runtime_error("Data sizes must be the same");
101  }
102  }
103 
105 
106  for (int i = 0; i != n_generators; ++i)
107  {
108  distance2_array.row(i) = distance2_traj[i];
109  }
110 
111  distance2_traj.clear();
112  distance2_traj.shrink_to_fit();
113 }
114 
115 /* ------------------------------------------------------------------ */
116 
118  int index1_in_mol_,
119  int index2_in_mol_)
120 {
121  index1_in_mol = index1_in_mol_;
122  index2_in_mol = index2_in_mol_;
123 }
124 
125 /* ------------------------------------------------------------------ */
126 
128  int target_moltype_)
129 {
130  target_moltype = target_moltype_;
131 }
132 
133 /* ------------------------------------------------------------------ */
134 
136 {
137  return distance2_array.sqrt();
138 }
139 
140 /* ------------------------------------------------------------------ */
141 
143 {
144  return distance2_array;
145 }
ArrayXXd get_distance_array()
Get computed distances between two atoms in each molecule as a two-dimensional array: each row corres...
Vec< ShPtr< Generator > > generators
Definition: processor.h:37
const ArrayXXd & get_distance2_array()
Get computed squared distances between two atoms in each molecule as a two-dimensional array: each ro...
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > ArrayXXd
ArrayXXd is an alias for a two-dimensional array of float numbers.
Definition: eigen.h:32
int n_generators
Definition: processor.h:31
virtual void finish() override
Fill distance2_array with values of distance2_traj.
std::vector< T > Vec
Vec is an alias for vector (same as list in Python).
Definition: std.h:27
Vec< RowArrayXd > distance2_traj
void register_generator(const ShPtr< GEN > &gen)
Definition: processor.cpp:16
ProDistanceInMolecule(const ElPtr &mols, const ElPtr &atoms)
Constructor of ProDistanceInMolecule class for a snapshot of simulation.
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
This file has a definition of ProDistanceInMolecule class, which is a subclass of Processor class...
GenDict is a dictionary (or map) of Generator objects.
Definition: gen_dict.h:20
virtual void run_impl(const int index) override
Implementation of analysis using an element of generators.
Namespace for utility functions.
Definition: join.h:14
void set_moltype(int target_moltype_)
Specify a molecular type. Only molecules whose type property is the specified type are analyzed...
void set_indices(int index1_in_mol_, int index2_in_mol_)
Specify two atoms in a molecule by zero-based index. Distance between the two atoms is computed for e...
std::shared_ptr< T > ShPtr
ShPtr is an alias for shared pointer.
Definition: std.h:16
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...
void register_generators(const Vec< ShPtr< GEN >> &gens)
Definition: processor.cpp:36
virtual void prepare() override
Resize distance2_traj.