ppap4lmp  0.7.2
add_molecular_orientation.cpp
Go to the documentation of this file.
1 
9 #include <Eigen/Eigenvalues>
10 
12 
13 /* ------------------------------------------------------------------ */
14 
16  Json &data,
17  JsonToVoidFunc check_required_keys,
18  JsonToBoolFunc check_optional_keys)
19 {
20  check_required_keys({"I_xx", "I_yy", "I_zz", "I_xy", "I_xz", "I_yz"});
21 
22  for (auto &d : data)
23  {
24  // compute principal values and vectors
25 
26  Matrix3d inertia_moment;
27  inertia_moment << d["I_xx"], d["I_xy"], d["I_xz"],
28  d["I_xy"], d["I_yy"], d["I_yz"],
29  d["I_xz"], d["I_yz"], d["I_zz"];
30 
31  Eigen::EigenSolver<Matrix3d> solver(inertia_moment);
32 
33  // NOTE: Eigenvalues are real since inertia moment is symmetric.
34  ArrayXd evals = solver.eigenvalues().real();
35  ArrayXXd evecs = solver.eigenvectors().real().transpose();
36 
37  d["I_values"] = {evals(0), evals(1), evals(2)};
38 
39  auto evecs_as_json = Json::array();
40 
41  for (int i = 0; i != 3; ++i)
42  {
43  auto evec = evecs.row(i);
44  evecs_as_json.push_back({evec(0), evec(1), evec(2)});
45  }
46 
47  d["I_vectors"].swap(evecs_as_json);
48 
49  // compute molecular orientation
50 
51  int index;
52  evals.minCoeff(&index);
53 
54  RowArrayXd orientation = evecs.row(index).square().unaryExpr(
55  [](double cos2)
56  {
57  return 0.5 * (3.0*cos2 - 1.0);
58  });
59 
60  d["S_x"] = orientation(0);
61  d["S_y"] = orientation(1);
62  d["S_z"] = orientation(2);
63  }
64 }
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
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
Eigen::Array< double, 1, Eigen::Dynamic > RowArrayXd
RowArrayXd is an alias for a row array of float numbers.
Definition: eigen.h:52
virtual void compute_impl(Json &data, JsonToVoidFunc check_required_keys, JsonToBoolFunc check_optional_keys) override
This method overrides Updater::compute_impl.
Eigen::Array< double, Eigen::Dynamic, 1 > ArrayXd
ArrayXd is an alias for a column array of float numbers.
Definition: eigen.h:42
This file has a definition of AddMolecularOrientation class, which is a subclass of Adder class...
std::function< void(const Json &)> JsonToVoidFunc
An alias for a function accepts a Json object.
Definition: updater.h:18
Eigen::Matrix3d Matrix3d
Matrix3d is an alias for a 3x3 matrix of float numbers.
Definition: eigen.h:90