4 #include "gromacs/math/vectypes.h"
11 void center_frame(std::vector< RVec > &cf_frame, std::vector< int > cf_atoms) {
12 if (cf_atoms.empty()) {
14 for (int i = 0; i < cf_frame.size(); i++) {
15 cf_atoms.push_back(i);
22 for (int i = 0; i < cf_atoms.size(); i++) {
23 cf_temp += cf_frame[cf_atoms[i]];
25 cf_temp[0] /= cf_atoms.size();
26 cf_temp[1] /= cf_atoms.size();
27 cf_temp[2] /= cf_atoms.size();
28 for (int i = 0; i < cf_atoms.size(); i++) {
29 cf_frame[cf_atoms[i]] -= cf_temp;
33 void implement_fit(std::vector< RVec > &if_frame, RVec if_step, double if_A, double if_B, double if_C) {
35 for (int i = 0; i < if_frame.size(); i++) {
36 if_frame[i] += if_step;
38 if_frame[i][0] = cos(if_C) * cos(if_B) * temp[0] + (-sin(if_C)* cos(if_A) + sin(if_B) * cos(if_C) * sin(if_A)) * temp[1] + (sin(if_C) * sin(if_A) + sin(if_B) * cos(if_C) * cos(if_A)) * temp[2];
39 if_frame[i][1] = sin(if_C) * cos(if_B) * temp[0] + (cos(if_C) * cos(if_A) + sin(if_C) * sin(if_B) * sin(if_A)) * temp[1] + (-cos(if_C) * sin(if_A) + sin(if_C) * sin(if_B) * cos(if_A)) * temp[2];
40 if_frame[i][2] = -sin(if_B) * temp[0] + cos(if_B) * sin(if_A) * temp[1] + cos(if_B) * cos(if_A) * temp[2];
44 double F(double aix, double aiy, double aiz, double bix, double biy, double biz, double cA, double sA, double cB, double sB, double cC, double sC) {
45 return gmx::square(aix + biy*(cA*sC - cC*sA*sB) - biz*(sA*sC + cA*cC*sB) - cB*cC*bix) +
46 gmx::square(aiy - biy*(cA*cC + sA*sB*sC) + biz*(cC*sA - cA*sB*sC) - cB*sC*bix) +
47 gmx::square(aiz + sB*bix - cA*cB*biz - cB*sA*biy) ;
50 double Fx(double temp1, double temp2, double temp3, double cB, double sB, double cC, double sC) {
56 double Fy(double temp1, double temp2, double temp3, double cA, double sA, double cB, double sB, double cC, double sC) {
57 return 2*(cA*sC - cC*sA*sB)*temp1 -
58 2*(cA*cC + sA*sB*sC)*temp2 -
62 double Fz(double temp1, double temp2, double temp3, double cA, double sA, double cB, double sB, double cC, double sC) {
63 return 2*(cC*sA - cA*sB*sC)*temp2 -
64 2*(sA*sC + cA*cC*sB)*temp1 -
68 double FA(double temp1, double temp2, double temp3, double biy_pl_y, double biz_pl_z, double cA, double sA, double cB, double sB, double cC, double sC) {
69 return 2*(biy_pl_y*(cC*sA - cA*sB*sC) + biz_pl_z*(cA*cC + sA*sB*sC))*temp2 -
70 2*(cA*cB*biy_pl_y - cB*sA*biz_pl_z)*temp3 -
71 2*(biy_pl_y*(sA*sC + cA*cC*sB) + biz_pl_z*(cA*sC - cC*sA*sB))*temp1 ;
74 double FB(double temp1, double temp2, double temp3, double bix_pl_x, double biy_pl_y, double biz_pl_z, double cA, double sA, double cB, double sB, double cC, double sC) {
75 return 2*(cB*bix_pl_x + sA*sB*biy_pl_y + cA*sB*biz_pl_z)*temp3 -
76 2*(cA*cB*cC*biz_pl_z - cC*sB*bix_pl_x + cB*cC*sA*biy_pl_y)*temp1 -
77 2*(cA*cB*sC*biz_pl_z - sB*sC*bix_pl_x + cB*sA*sC*biy_pl_y)*temp2 ;
80 double FC(double temp1, double temp2, double bix_pl_x, double biy_pl_y, double biz_pl_z, double cA, double sA, double cB, double sB, double cC, double sC) {
81 return 2*(biy_pl_y*(cA*cC + sA*sB*sC) - biz_pl_z*(cC*sA - cA*sB*sC) + cB*sC*bix_pl_x)*temp1 -
82 2*(biz_pl_z*(sA*sC + cA*cC*sB) - biy_pl_y*(cA*sC - cC*sA*sB) + cB*cC*bix_pl_x)*temp2 ;
85 double dist(std::vector< RVec > alfa, std::vector< RVec > beta, std::vector< std::pair< int, int > > cros) {
87 for (int i = 0; i < cros.size(); i++) {
88 distance += F(alfa[cros[i].first][0], alfa[cros[i].first][1], alfa[cros[i].first][2], beta[cros[i].second][0], beta[cros[i].second][1], beta[cros[i].second][2], 1, 0, 1, 0, 1, 0);
93 double dist_old_format(rvec *alfa, rvec *beta, std::vector< std::pair< int, int > > cros) {
95 for (int i = 0; i < cros.size(); i++) {
96 distance += F(alfa[cros[i].first][0], alfa[cros[i].first][1], alfa[cros[i].first][2], beta[cros[i].second][0], beta[cros[i].second][1], beta[cros[i].second][2], 1, 0, 1, 0, 1, 0);
101 void new_fit(std::vector< RVec > alfa, std::vector< RVec > &beta, std::vector< std::pair< int, int > > cros, double prec) {
104 double x1 = 0, y1 = 0, z1 = 0, A1 = 0, B1 = 0, C1 = 0;
105 double x2 = 0, y2 = 0, z2 = 0, A2 = 0, B2 = 0, C2 = 0;
106 double cA, sA, cB, sB, cC, sC, temp1, temp2, temp3;
107 long double H1 = 0.0001, H2 = 0.0001, F1 = 0, F2 = 0, H1_temp, H2_temp, H1_const = 1, H2_const = M_PI/180;
108 F1 = dist(alfa, beta, cros) / cros.size();
112 for (int i = 0; i < cros.size(); i++) {
113 bx = beta[cros[i].second][0] + x1;
114 by = beta[cros[i].second][1] + y1;
115 bz = beta[cros[i].second][2] + z1;
122 temp1 = alfa[cros[i].first][0] + by*(cA*sC - cC*sA*sB) - bz*(sA*sC + cA*cC*sB) - cB*cC*bx;
123 temp2 = alfa[cros[i].first][1] - by*(cA*cC + sA*sB*sC) + bz*(cC*sA - cA*sB*sC) - cB*sC*bx;
124 temp3 = alfa[cros[i].first][2] + sB*bx - cA*cB*bz - cB*sA*by;
125 x2 += Fx(temp1, temp2, temp3, cB, sB, cC, sC);
126 y2 += Fy(temp1, temp2, temp3, cA, sA, cB, sB, cC, sC);
127 z2 += Fz(temp1, temp2, temp3, cA, sA, cB, sB, cC, sC);
128 A2 += FA(temp1, temp2, temp3, by, bz, cA, sA, cB, sB, cC, sC);
129 B2 += FB(temp1, temp2, temp3, bx, by, bz, cA, sA, cB, sB, cC, sC);
130 C2 += FC(temp1, temp2, bx, by, bz, cA, sA, cB, sB, cC, sC);
132 H1_temp = std::max(std::fabs(H1*x2), std::max(std::fabs(H1*y2), std::fabs(H1*z2)));
133 if (H1_temp > H1_const) {
140 H2_temp = std::max(std::fabs(H2*A2), std::max(std::fabs(H2*B2), std::fabs(H2*C2)));
141 if (H2_temp > H2_const) {
148 for (int i = 0; i < cros.size(); i++) {
155 F2 += F(alfa[cros[i].first][0], alfa[cros[i].first][1], alfa[cros[i].first][2], beta[cros[i].second][0] + x2, beta[cros[i].second][1] + y2, beta[cros[i].second][2] + z2, cA, sA, cB, sB, cC, sC);
171 if (std::fabs(F1 - F2) <= prec) {
180 implement_fit(beta, step, A1, B1, C1);