ppap4lmp  0.7.2
pro_time_correlation_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 Vec<std::pair<ElPtr,ElPtr>> &pairs)
19 {
20  Vec<ShPtr<GenDict>> gens;
21 
22  for (const auto &pair : pairs)
23  {
24  gens.push_back(ShPtr<GenDict>(
25  new GenDict({{"Mols", pair.first}, {"Atoms", pair.second}})));
26  }
27 
28  register_generators(gens);
29 }
30 
31 /* ------------------------------------------------------------------ */
32 
34  std::function<void(const RowVector3d &)> callback,
35  const ElPtr &el_mols,
36  const ElPtr &el_atoms)
37 {
38  // NOTE: `id` property is check_required_keys to ensure data is sorted.
39  el_mols->check_required_keys({"id", "atom-ids"});
40 
41  auto &mols = el_mols->get_data();
42 
43  el_atoms->check_required_keys({"id", "xu", "yu", "zu"});
44 
45  auto &atoms = el_atoms->get_data();
46 
47  auto id2index_atom = ut::map_to_index(atoms, "id");
48 
49  for (const auto &mol : mols)
50  {
51  if (mol.value("type", 1) != target_moltype) continue;
52 
53  auto &atom_ids = mol["atom-ids"];
54 
55  auto &atom1 = atoms[id2index_atom[atom_ids.at(index1_in_mol)]];
56  auto &atom2 = atoms[id2index_atom[atom_ids.at(index2_in_mol)]];
57 
58  RowVector3d vector_tmp;
59  vector_tmp << atom2["xu"].get<double>() - atom1["xu"].get<double>(),
60  atom2["yu"].get<double>() - atom1["yu"].get<double>(),
61  atom2["zu"].get<double>() - atom1["zu"].get<double>();
62 
63  callback(vector_tmp);
64  }
65 }
66 
67 /* ------------------------------------------------------------------ */
68 
70  const int index)
71 {
72  Vec<double> coefficients_tmp;
73 
75  [this, &coefficients_tmp](const RowVector3d &vec)
76  {
77  auto index = coefficients_tmp.size();
78  coefficients_tmp.push_back(
79  initial_vectors[index].dot(vec)
80  /* normalize */ * initial_reciprocal_norm2[index]);
81  },
82  generators[index]->get_element("Mols"),
83  generators[index]->get_element("Atoms"));
84 
85  if (coefficients_tmp.size() != initial_vectors.size())
86  {
88  "Number of molecules and molecular types must be unchanged");
89  }
90 
92  coefficients_tmp.data(), coefficients_tmp.size());
93 }
94 
95 /* ------------------------------------------------------------------ */
96 
98 {
100 
102  [this](const RowVector3d &vec)
103  {
104  initial_vectors.push_back(vec);
105  initial_reciprocal_norm2.push_back(1.0/vec.dot(vec));
106  },
107  generators.front()->get_element("Mols"),
108  generators.front()->get_element("Atoms"));
109 
111 }
112 
113 /* ------------------------------------------------------------------ */
114 
116 {
118 
119  for (int i = 0; i != n_generators; ++i)
120  {
122  }
123 
125 
126  initial_vectors.clear();
127  initial_vectors.shrink_to_fit();
128  initial_reciprocal_norm2.clear();
129  initial_reciprocal_norm2.shrink_to_fit();
130  coefficients_traj.clear();
131  coefficients_traj.shrink_to_fit();
132 }
133 
134 /* ------------------------------------------------------------------ */
135 
137  int index1_in_mol_,
138  int index2_in_mol_)
139 {
140  index1_in_mol = index1_in_mol_;
141  index2_in_mol = index2_in_mol_;
142 }
143 
144 /* ------------------------------------------------------------------ */
145 
147  int target_moltype_)
148 {
149  target_moltype = target_moltype_;
150 }
151 /* ------------------------------------------------------------------ */
152 
154 {
155  return coefficients_array;
156 }
157 
158 /* ------------------------------------------------------------------ */
159 
161 {
163 }
Eigen::RowVector3d RowVector3d
RowVector3d is an alias for a 3-elements row vector of Eigen float numbers.
Definition: eigen.h:110
Vec< ShPtr< Generator > > generators
Definition: processor.h:37
void set_indices(int index1_in_mol_, int index2_in_mol_)
Specify two atoms in a molecule by zero-based index. Time correlation function of a vector between th...
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
void compute_temporary_vectors(std::function< void(const RowVector3d &)> callback, const ElPtr &el_mols, const ElPtr &el_atoms)
Compute intramolecular vectors and conduct customized manipulation using them.
virtual void use_generator_at(const int i)
Call Generator::generate_data of i th Generator object in generators.
Definition: processor.cpp:58
virtual void prepare() override
Set initial vectors and resize coefficients_traj.
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
const ArrayXXd & get_coefficients_array()
Get a two-dimensional array containing computed time correlation coefficients of vectors connecting t...
const ArrayXd & get_time_correlation()
Get an averaged time correlation function of atom-to-atom vector; each pair of atoms is in the same m...
This file has a definition of ProTimeCorrelationInMolecule class, which is a subclass of Processor cl...
virtual void finish() override
Fill coefficients_array with values of coefficients_traj, and calculate time_correlation_function by ...
void set_moltype(int target_moltype_)
Specify a molecular type. Only molecules whose type property is the specified type are analyzed...
int n_generators
Definition: processor.h:31
ProTimeCorrelationInMolecule(const Vec< std::pair< ElPtr, ElPtr >> &pairs)
Constructor of ProTimeCorrelationInMolecule class.
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 run_impl(const int index) override
Implementation of analysis using an element of generators.
GenDict is a dictionary (or map) of Generator objects.
Definition: gen_dict.h:20
Namespace for utility functions.
Definition: join.h:14
Eigen::Array< double, Eigen::Dynamic, 1 > ArrayXd
ArrayXd is an alias for a column array of float numbers.
Definition: eigen.h:42
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