-
Notifications
You must be signed in to change notification settings - Fork 1
/
angle.h
284 lines (249 loc) · 8.88 KB
/
angle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
Copyright (C) 2010 The ESPResSo project
Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010 Max-Planck-Institute for Polymer Research, Theory Group, PO Box 3148, 55021 Mainz, Germany
This file is part of ESPResSo.
ESPResSo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ESPResSo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANGLE_H
#define ANGLE_H
/** \file angle.h
* Routines to calculate the angle energy or/and and force
* for a particle triple.
* \ref forces.c
*/
#ifdef BOND_ANGLE
#include "utils.h"
/************************************************************/
/** set parameters for the angle potential. The type of the angle potential
is chosen via config.h and cannot be changed at runtime.
*/
MDINLINE int angle_set_params(int bond_type, double bend, double phi0)
{
if(bond_type < 0)
return TCL_ERROR;
make_bond_type_exist(bond_type);
bonded_ia_params[bond_type].p.angle.bend = bend;
bonded_ia_params[bond_type].p.angle.phi0 = phi0;
#ifdef BOND_ANGLE_COSINE
bonded_ia_params[bond_type].p.angle.cos_phi0 = cos(phi0);
bonded_ia_params[bond_type].p.angle.sin_phi0 = sin(phi0);
#endif
#ifdef BOND_ANGLE_COSSQUARE
bonded_ia_params[bond_type].p.angle.cos_phi0 = cos(phi0);
#endif
bonded_ia_params[bond_type].type = BONDED_IA_ANGLE;
bonded_ia_params[bond_type].num = 2;
/* broadcast interaction parameters */
mpi_bcast_ia_params(bond_type, -1);
return TCL_OK;
}
/// parse parameters for the angle potential
MDINLINE int inter_parse_angle(Tcl_Interp *interp, int bond_type, int argc, char **argv)
{
double bend, phi0;
/* the optional parameter phi0 is due to backwards compatibility and is set to PI if not given */
if (argc != 2 && argc != 3) {
Tcl_AppendResult(interp, "angle needs 1 or 2 parameters: "
"<bend> [<phi0>]", (char *) NULL);
return (TCL_ERROR);
}
if (! ARG_IS_D(1, bend)) {
Tcl_AppendResult(interp, "angle needs a DOUBLE parameter: "
"<bend> ", (char *) NULL);
return TCL_ERROR;
}
/* special treatment of the optional parameter phi0 */
if (argc == 3) {
if (! ARG_IS_D(2, phi0)) {
Tcl_AppendResult(interp, "angle needs a DOUBLE parameter: "
"<phi0> ", (char *) NULL);
return TCL_ERROR;
}
} else {
phi0 = PI;
}
CHECK_VALUE(angle_set_params(bond_type, bend, phi0), "bond type must be nonnegative");
}
/** Computes the three body angle interaction force and adds this
force to the particle forces (see \ref #inter).
@param p_mid Pointer to second/middle particle.
@param p_left Pointer to first/left particle.
@param p_right Pointer to third/right particle.
@param iaparams bond type number of the angle interaction (see \ref #inter).
@param force1 returns force of particle 1
@param force2 returns force of particle 2
@return 0
*/
MDINLINE int calc_angle_force(Particle *p_mid, Particle *p_left, Particle *p_right,
Bonded_ia_parameters *iaparams, double force1[3], double force2[3])
{
double cosine, vec1[3], vec2[3], d1i, d2i, dist2, fac, f1=0.0, f2=0.0;
int j;
cosine=0.0;
/* vector from p_left to p_mid */
get_mi_vector(vec1, p_mid->r.p, p_left->r.p);
dist2 = sqrlen(vec1);
d1i = 1.0 / sqrt(dist2);
for(j=0;j<3;j++) vec1[j] *= d1i;
/* vector from p_mid to p_right */
get_mi_vector(vec2, p_right->r.p, p_mid->r.p);
dist2 = sqrlen(vec2);
d2i = 1.0 / sqrt(dist2);
for(j=0;j<3;j++) vec2[j] *= d2i;
/* scalar produvt of vec1 and vec2 */
cosine = scalar(vec1, vec2);
fac = iaparams->p.angle.bend;
#ifdef BOND_ANGLE_HARMONIC
{
double phi,sinphi;
if ( cosine > TINY_COS_VALUE) cosine = TINY_COS_VALUE;
if ( cosine < -TINY_COS_VALUE) cosine = -TINY_COS_VALUE;
phi = acos(-cosine);
sinphi = sin(phi);
if ( sinphi < TINY_SIN_VALUE ) sinphi = TINY_SIN_VALUE;
fac *= (phi - iaparams->p.angle.phi0)/sinphi;
}
#endif
#ifdef BOND_ANGLE_COSINE
if ( cosine > TINY_COS_VALUE ) cosine = TINY_COS_VALUE;
if ( cosine < -TINY_COS_VALUE) cosine = -TINY_COS_VALUE;
fac *= iaparams->p.angle.sin_phi0 * (cosine/sqrt(1-SQR(cosine))) + iaparams->p.angle.cos_phi0;
#endif
#ifdef BOND_ANGLE_COSSQUARE
fac *= iaparams->p.angle.cos_phi0 + cosine;
#endif
for(j=0;j<3;j++) {
f1 = fac * (cosine * vec1[j] - vec2[j]) * d1i;
f2 = fac * (cosine * vec2[j] - vec1[j]) * d2i;
force1[j] = (f1-f2);
force2[j] = -f1;
}
return 0;
}
/* The force on each particle due to a three-body bonded potential
is computed. */
MDINLINE void calc_angle_3body_forces(Particle *p_mid, Particle *p_left,
Particle *p_right, Bonded_ia_parameters *iaparams,
double force1[3], double force2[3], double force3[3]) {
int j;
double pot_dep;
double cos_phi;
double sin_phi;
double vec31[3];
double vec21[3];
double vec12[3]; // espresso convention
double vec21_sqr;
double vec31_sqr;
double vec21_magn;
double vec31_magn;
double fj[3];
double fk[3];
double fac;
get_mi_vector(vec12, p_mid->r.p, p_left->r.p);
for(j = 0; j < 3; j++)
vec21[j] = -vec12[j];
get_mi_vector(vec31, p_right->r.p, p_mid->r.p);
vec21_sqr = sqrlen(vec21);
vec21_magn = sqrt(vec21_sqr);
vec31_sqr = sqrlen(vec31);
vec31_magn = sqrt(vec31_sqr);
cos_phi = scalar(vec21, vec31) / (vec21_magn * vec31_magn);
sin_phi = sqrt(1.0 - SQR(cos_phi));
/* uncomment this block if interested in the angle
if(cos_phi < -1.0) cos_phi = -TINY_COS_VALUE;
if(cos_phi > 1.0) cos_phi = TINY_COS_VALUE;
phi = acos(cos_phi);
*/
#ifdef BOND_ANGLE_HARMONIC
{
double K, phi, phi0;
if(cos_phi < -1.0) cos_phi = -TINY_COS_VALUE;
if(cos_phi > 1.0) cos_phi = TINY_COS_VALUE;
phi = acos(cos_phi);
K = iaparams->p.angle.bend;
phi0 = iaparams->p.angle.phi0;
// potential dependent term [dU/dphi = K * (phi - phi0)]
pot_dep = K * (phi - phi0);
}
#endif
#ifdef BOND_ANGLE_COSINE
{
double K, sin_phi0, cos_phi0;
K = iaparams->p.angle.bend;
sin_phi0 = iaparams->p.angle.sin_phi0;
cos_phi0 = iaparams->p.angle.cos_phi0;
// potential dependent term [dU/dphi = K * sin(phi - phi0)]
// trig identity: sin(a - b) = sin(a)cos(b) - cos(a)sin(b)
pot_dep = K * (sin_phi * cos_phi0 - cos_phi * sin_phi0);
}
#endif
#ifdef BOND_ANGLE_COSSQUARE
fprintf(stderr, "WARNING: calc_angle_3body_forces not implemented for cossquare potential, cannot calculate stress tensor");
#endif
fac = pot_dep / sin_phi;
for(j = 0; j < 3; j++) {
fj[j] = vec31[j] / (vec21_magn * vec31_magn) - cos_phi * vec21[j] / vec21_sqr;
fk[j] = vec21[j] / (vec21_magn * vec31_magn) - cos_phi * vec31[j] / vec31_sqr;
}
// note that F1 = -(F2 + F3)
for(j = 0; j < 3; j++) {
force1[j] = force1[j] - fac * (fj[j] + fk[j]);
force2[j] = force2[j] + fac * fj[j];
force3[j] = force3[j] + fac * fk[j];
}
}
/** Computes the three body angle interaction energy (see \ref #inter, \ref #analyze).
@param p_mid Pointer to first particle.
@param p_left Pointer to second/middle particle.
@param p_right Pointer to third particle.
@param iaparams bond type number of the angle interaction (see \ref #inter).
@param _energy return energy pointer.
@return 0.
*/
MDINLINE int angle_energy(Particle *p_mid, Particle *p_left, Particle *p_right,
Bonded_ia_parameters *iaparams, double *_energy)
{
double cosine, vec1[3], vec2[3], d1i, d2i, dist2;
int j;
cosine=0.0;
/* vector from p_mid to p_left */
get_mi_vector(vec1, p_mid->r.p, p_left->r.p);
dist2 = sqrlen(vec1);
d1i = 1.0 / sqrt(dist2);
for(j=0;j<3;j++) vec1[j] *= d1i;
/* vector from p_right to p_mid */
get_mi_vector(vec2, p_right->r.p, p_mid->r.p);
dist2 = sqrlen(vec2);
d2i = 1.0 / sqrt(dist2);
for(j=0;j<3;j++) vec2[j] *= d2i;
/* scalar produvt of vec1 and vec2 */
cosine = scalar(vec1, vec2);
if ( cosine > TINY_COS_VALUE) cosine = TINY_COS_VALUE;
if ( cosine < -TINY_COS_VALUE) cosine = -TINY_COS_VALUE;
/* bond angle energy */
#ifdef BOND_ANGLE_HARMONIC
{
double phi;
phi = acos(-cosine);
*_energy = 0.5*iaparams->p.angle.bend*SQR(phi - iaparams->p.angle.phi0);
}
#endif
#ifdef BOND_ANGLE_COSINE
*_energy = iaparams->p.angle.bend*(cosine*iaparams->p.angle.cos_phi0 - sqrt(1-SQR(cosine))*iaparams->p.angle.sin_phi0+1);
#endif
#ifdef BOND_ANGLE_COSSQUARE
*_energy = 0.5*iaparams->p.angle.bend*SQR(cosine + iaparams->p.angle.cos_phi0);
#endif
return 0;
}
#endif /* BOND_ANGLE */
#endif /* ANGLE_H */