Debian Codegraph @

avogadro/calc/gradients.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CALC_GRADIENTS_H
7 #define AVOGADRO_CALC_GRADIENTS_H
8
9 #include <avogadro/core/vector.h>
10 #include <avogadro/core/angletools.h>
11
12 #include <algorithm>
13 #include <cmath>
14
15 namespace Avogadro::Calc {
16
17 /**
18 * Calculate the components of the gradient for the distance a-b.
19 * @return true if the length is well-defined.
20 */
21 inline bool distanceGradient(const Vector3& a, const Vector3& b, Real& distance,
22 Vector3& aGrad, Vector3& bGrad)
23 {
24 bGrad = aGrad = Vector3::Zero();
25
26 const Vector3 ab = a - b;
27 distance = ab.norm();
28 if (distance < 1e-3)
29 return false;
30
31 const Vector3 direction = ab / distance;
32 aGrad = direction;
33 bGrad = -direction;
34 return true;
35 }
36
37 /**
38 * Calculate the components of the gradient for the angle a-b-c using atan2.
39 * @return the angle between a-b-c in radians or 0.0 if ill-defined
40 */
41 inline Real angleGradient(const Vector3& a, const Vector3& b, const Vector3& c,
42 Vector3& aGrad, Vector3& bGrad, Vector3& cGrad)
43 {
44 cGrad = bGrad = aGrad = Vector3::Zero();
45
46 const Vector3 ab = a - b;
47 const Vector3 cb = c - b;
48 const Real rab = ab.norm();
49 const Real rcb = cb.norm();
50
51 if (rab < 1.e-3 || rcb < 1.e-3)
52 return 0.0;
53
54 const Real dot = ab.dot(cb);
55 const Vector3 ab_cross_cb = ab.cross(cb);
56 const Real crossNorm = ab_cross_cb.norm();
57 if (!std::isfinite(crossNorm) || crossNorm < 1.e-6)
58 return 0.0;
59
60 Real angle = atan2(crossNorm, dot);
61 if (angle < -PI)
62 angle += 2 * PI;
63 else if (angle > PI)
64 angle -= 2 * PI;
65
66 // Use the cross product to get the gradients
67 const Vector3 n = ab_cross_cb / crossNorm;
68
69 // Gradients of the cross products
70 Vector3 grad_cross_a = (cb.cross(n));
71 Vector3 grad_cross_c = (n.cross(ab));
72 Vector3 grad_cross_b = -(grad_cross_a + grad_cross_c);
73
74 // Gradients of the dot product
75 Vector3 grad_dot_a = cb;
76 Vector3 grad_dot_c = ab;
77 Vector3 grad_dot_b = -(cb + ab);
78
79 // Final gradient using atan2 derivative: d/dx(atan2(y,x)) = (x*dy/dx -
80 // y*dx/dx)/(x^2 + y^2)
81 const Real denom = crossNorm * crossNorm + dot * dot;
82 aGrad = (grad_cross_a * dot - crossNorm * grad_dot_a) / denom;
83 bGrad = (grad_cross_b * dot - crossNorm * grad_dot_b) / denom;
84 cGrad = (grad_cross_c * dot - crossNorm * grad_dot_c) / denom;
85
86 return angle;
87 }
88
89 /**
90 * Calculate the components of the gradient for the dihedral i-j-k-;
91 * @return the torsion angle around i-j-k-l in radians
92 */
93 inline Real dihedralGradient(const Vector3& i, const Vector3& j,
94 const Vector3& k, const Vector3& l, Vector3& iGrad,
95 Vector3& jGrad, Vector3& kGrad, Vector3& lGrad)
96 {
97 lGrad = kGrad = jGrad = iGrad = Vector3::Zero();
98
99 // get the bond vectors
100 Vector3 ij = j - i;
101 Vector3 jk = k - j;
102 Vector3 kl = l - k;
103
104 Real rij = ij.norm();
105 Real rjk = jk.norm();
106 Real rkl = kl.norm();
107
108 Real phi = calculateDihedral(i, j, k, l) * DEG_TO_RAD;
109
110 // check if the bond vectors are near zero
111 if (rij < 1e-3 || rjk < 1e-3 || rkl < 1e-3)
112 return phi; // skip this torsion
113
114 Real sinPhi = sin(phi);
115 Real cosPhi = cos(phi);
116
117 // Using the BallView / Open Babel formula
118 // https://doi.org/10.22028/D291-25896 (Appendix A)
119 // Thanks to Andreas Moll
120 // for the derivation of the gradients
121
122 // get the unit vectors
123 Vector3 n1 = ij / rij;
124 Vector3 n2 = jk / rjk;
125 Vector3 n3 = kl / rkl;
126
127 // get the angles between ijk and jkl
128 Vector3 n1_cross_n2 = n1.cross(n2);
129 Vector3 n2_cross_n3 = n2.cross(n3);
130
131 // check for near-zero cross products
132 if (n1_cross_n2.norm() < 1e-6 || n2_cross_n3.norm() < 1e-6)
133 return phi; // skip this torsion
134
135 Real sinAngleIJK = n1_cross_n2.norm();
136 Real sinAngleJKL = n2_cross_n3.norm();
137 Real cosAngleIJK = n1.dot(n2);
138 Real cosAngleJKL = n2.dot(n3);
139
140 // get the gradient components
141 iGrad = -n1_cross_n2 / (rij * sinAngleIJK * sinAngleIJK);
142 lGrad = n2_cross_n3 / (rkl * sinAngleJKL * sinAngleJKL);
143
144 // grad_j and grad_k are a bit more complicated
145 // clamp the cosines to -1 to 1
146 cosAngleIJK = std::clamp(cosAngleIJK, -1.0, 1.0);
147 cosAngleJKL = std::clamp(cosAngleJKL, -1.0, 1.0);
148
149 Real fraction1 = (rij / rjk) * (-cosAngleIJK);
150 Real fraction2 = (rkl / rjk) * (-cosAngleJKL);
151 jGrad = iGrad * (fraction1 - 1) - lGrad * (fraction2);
152 kGrad = -(iGrad + lGrad + jGrad);
153
154 return phi;
155 }
156
157 inline Real outOfPlaneGradient(const Vector3& point, const Vector3& b,
158 const Vector3& c, const Vector3& d,
159 Vector3& aGrad, Vector3& bGrad, Vector3& cGrad,
160 Vector3& dGrad)
161 {
162 dGrad = cGrad = bGrad = aGrad = Vector3::Zero();
163
164 // Bond vectors from central atom to surrounding atoms
165 Vector3 u1 = b - point;
166 Vector3 u2 = c - point;
167 Vector3 u3 = d - point;
168
169 const Real r1 = u1.norm();
170 const Real r2 = u2.norm();
171 const Real r3 = u3.norm();
172 if (r1 < 1e-3 || r2 < 1e-3 || r3 < 1e-3)
173 return 0.0;
174
175 // Normalize to unit vectors
176 u1 /= r1;
177 u2 /= r2;
178 u3 /= r3;
179
180 // Use outOfPlaneAngle (Wilson angle) for the angle value
181 const Real chi = outOfPlaneAngle(point, b, c, d) * DEG_TO_RAD;
182 const Real sinChi = std::sin(chi);
183 const Real cosChi = std::cos(chi);
184 if (std::abs(cosChi) < 1e-6)
185 return chi;
186
187 // Cross product and derived quantities needed for gradient formulas
188 const Vector3 n = u1.cross(u2);
189 const Real sinTheta = n.norm();
190 if (sinTheta < 1e-6)
191 return chi;
192
193 const Real cosTheta = u1.dot(u2);
194 const Vector3 n_hat = n / sinTheta;
195 const Real sinTheta2 = sinTheta * sinTheta;
196
197 // Gradient for atom d (out-of-plane atom)
198 dGrad = (n_hat - u3 * sinChi) / (r3 * cosChi);
199
200 // Gradient for atom b (in-plane)
201 const Vector3 u2xu3 = u2.cross(u3);
202 bGrad = (u2xu3 * sinTheta - u1 * sinChi + u2 * sinChi * cosTheta) /
203 (r1 * cosChi * sinTheta2);
204
205 // Gradient for atom c (in-plane)
206 const Vector3 u3xu1 = u3.cross(u1);
207 cGrad = (u3xu1 * sinTheta - u2 * sinChi + u1 * sinChi * cosTheta) /
208 (r2 * cosChi * sinTheta2);
209
210 // Central atom gradient from translation invariance
211 aGrad = -(bGrad + cGrad + dGrad);
212
213 return chi;
214 }
215
216 } // namespace Avogadro::Calc
217
218 #endif // AVOGADRO_CALC_GRADIENTS_H