File: AdGeneralizedBornFunctions.c

package info (click to toggle)
adun.app 0.81-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,384 kB
  • sloc: objc: 70,952; ansic: 6,668; yacc: 394; python: 75; cpp: 36; makefile: 33; xml: 15; awk: 3
file content (312 lines) | stat: -rw-r--r-- 11,171 bytes parent folder | download | duplicates (3)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 Project: AdunBase
 
 Copyright (C) 2008 Michael Johnston & Jordi Villa-Freixa
 
 Author: Michael Johnston
 
 This application 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 2 of the License, or (at your option) any later version.
 
 This application 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
 Library General Public License for more details.
 
 You should have received a copy of the GNU General Public
 License along with this library; if not, write to the Free
 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 */

#include "AdGeneralizedBornFunctions.h"

static double electrostaticConstant;


void AdSetGeneralizedBornVariables(double tau, double estConstant)
{
	electrostaticConstant = 0.5*estConstant*tau;
}

void AdGeneralizedBornEnergy(ListElement* interaction, 
					    double** coordinates,
					    double* radii, 
					    double* est_pot)
{					    
	int atomOne, atomTwo;
	double exponent, squaredSeparation, chargeProduct;
	double radiusOne, radiusTwo, numerator;
	Vector3D separation_s;
	
	atomOne = interaction->bond[0];
	atomTwo = interaction->bond[1];
	chargeProduct = interaction->params[2];
	
	//calculate seperation vector (r1 - r2)
	*(separation_s.vector + 0) = coordinates[atomOne][0] - coordinates[atomTwo][0];
	*(separation_s.vector + 1) = coordinates[atomOne][1] - coordinates[atomTwo][1];
	*(separation_s.vector + 2) = coordinates[atomOne][2] - coordinates[atomTwo][2];
	
	//calculate the interatomic distance
	Ad3DVectorLength(&separation_s);
	
	radiusOne = radii[atomOne];
	radiusTwo = radii[atomTwo];
	
	squaredSeparation = separation_s.length*separation_s.length;
	numerator =  -electrostaticConstant*chargeProduct;
	exponent = exp(-squaredSeparation/(4*radiusOne*radiusTwo));
	
	*est_pot += numerator/sqrt(squaredSeparation + radiusOne*radiusTwo*exponent);
}

/*
Returns the magnitude of the derivative of the GB energy with respect to the separation of the atoms.
*/
inline void AdGBESeparationDerivativeMagnitude(ListElement* interaction, 
				     double** coordinates,
				     double* radii, 
				     double* forceMagnitude)
{	
	int atomOne, atomTwo;
	double exponent, squaredSeparation, chargeProduct;
	double radiusOne, radiusTwo;
	double numerator, denominator, preFactor;
	Vector3D separation_s;
			     
	atomOne = interaction->bond[0];
	atomTwo = interaction->bond[1];
	chargeProduct = interaction->params[2];
	
	//calculate seperation vector (r1 - r2)
	*(separation_s.vector + 0) = coordinates[atomOne][0] - coordinates[atomTwo][0];
	*(separation_s.vector + 1) = coordinates[atomOne][1] - coordinates[atomTwo][1];
	*(separation_s.vector + 2) = coordinates[atomOne][2] - coordinates[atomTwo][2];
		
	//Use the current length given by the interaction struct
	separation_s.length = interaction->length;
		
	radiusOne = radii[atomOne];
	radiusTwo = radii[atomTwo];
	
	squaredSeparation = separation_s.length*separation_s.length;
	preFactor =  -0.5*electrostaticConstant*chargeProduct;
	
	//The value of the exponent in the derivative
	exponent = squaredSeparation/(4*radiusOne*radiusTwo);
	exponent = exp(-exponent);
	
	numerator = preFactor*exponent;
	denominator = pow((squaredSeparation + exponent), 1.5);
	
	*forceMagnitude = numerator/denominator;	
}


/*
This gives the derivate of the born energy with respect to the separation between the two atoms.
The resulting forces on the two atoms are equal in magnitude and opposite in direction.
The force vectors are accumulated into the corresponding rows of the matrix \e forces.

\param cutoff If the separation is greater than cutoff the calculation is aborted.
This parameter is an optimisation device which ensures that only
interactions within a certain distance are evaluated while keeping the calculations to a minimum.

\param flag Another optimisation device. If \e flag is zero the distance between the two
atoms is calculated as normal. Otherwise the value of the length memeber of the \e interaction
structure is used. This saves calculating length when it has been previously calculated for 
\e interaction as part of a standard coloumb/LJ calculation.

\note Due to the use of \e cutoff this function does not used
AdGBSeparationDerivativeMagnitude() to calculate the magnitude.
This is because, if a cutoff is set, the calculation can be stoped
before most of the calculations in AdGBSeparationDerivativeMagnitude() would 
be performed.
*/
void AdGBESeparationDerivative(ListElement* interaction, 
				    double** coordinates,
				    double** forces,
				    double* radii, 
				    double* est_pot)
{				    	
	int atomOne, atomTwo;
	double exponent, squaredSeparation, chargeProduct;
	double radiusOne, radiusTwo;
	double numerator, denominator, forceMagnitude, preFactor;
	Vector3D separation_s;
	
	atomOne = interaction->bond[0];
	atomTwo = interaction->bond[1];
	chargeProduct = interaction->params[2];
	
	//calculate seperation vector (r2 - r1)
	//This is the vector from atom one to atom two
	*(separation_s.vector + 0) = coordinates[atomTwo][0] - coordinates[atomOne][0];
	*(separation_s.vector + 1) = coordinates[atomTwo][1] - coordinates[atomOne][1];
	*(separation_s.vector + 2) = coordinates[atomTwo][2] - coordinates[atomOne][2];
	
	separation_s.length = interaction->length;
	
	//Calculate dG/d|r|
	//where |r| is the separation length.
	//In the following a factor of |r| is not included.
	//Since it cancels when dG/d|r| is multiplied by d|r|/dr.
	
	radiusOne = radii[atomOne];
	radiusTwo = radii[atomTwo];
	
	squaredSeparation = separation_s.length*separation_s.length;

	//The value of the exponent in the derivative
	exponent = squaredSeparation/(4*radiusOne*radiusTwo);
	preFactor =  0.5*electrostaticConstant*chargeProduct;
	exponent = exp(-exponent);
	denominator = sqrt(squaredSeparation + radiusOne*radiusTwo*exponent);
	
	numerator = preFactor*(4 - exponent);
	
	*est_pot += -2*preFactor/denominator;
	
	denominator = pow(denominator, 3);
	
	//The force is the negative of the gradient.
	//Multiply the above by minus 1 here.
	forceMagnitude = -1*numerator/denominator;

	//Finally the derivative of the separation
	//The derivative of the vector (r2 - r1) w.r.t. atomOne
	//is -1*(r2 - r1)/|r2 - r1|
	//The factor |r2 - r1| cancels with a factor in the derivative
	//of dG/d|r|. 
	//Therefore we dont calculate the unit vector.
	
	//The overall effect is as follows for (+,-)
	//The derivatives of the separation vector w.r.t each atoms position point
	//away from each other.
	//dG/d|r| is negative so this switches the vectors so they point towards each other.
	//Multiplied by the -1 for the force, becomes overall repulsion again.
	//That is for (+,-) pair the screening force opposes the attractive coulomb force.
	//The screening potential becomes lower as the forces separate.
	//The coulomb potential becomes lower (more negative) as the forces approach.
	
	*(separation_s.vector + 0) *= forceMagnitude;
	*(separation_s.vector + 1) *= forceMagnitude;
	*(separation_s.vector + 2) *= forceMagnitude;
	
	forces[atomOne][0] -= *(separation_s.vector + 0);
	forces[atomOne][1] -= *(separation_s.vector + 1);
	forces[atomOne][2] -= *(separation_s.vector + 2);
	
	forces[atomTwo][0] += *(separation_s.vector + 0);
	forces[atomTwo][1] += *(separation_s.vector + 1);
	forces[atomTwo][2] += *(separation_s.vector + 2);
}

/*
Calculates derivative of the Born energy with respect to the born radii, R_a & R_b of the atoms.
On return the 2-element array \e results contains the derivatives.
The first element is the derivate w.r.t R_a and the second the derivative w.r.t. R_b. 
*/

void AdGBEBornRadiusDerivative(ListElement* interaction, 
				     double** coordinates,
				     double* radii,
				     double *coefficient1,
				     double *coefficient2)
{				     
	
	int atomOne, atomTwo;
	double exponent, squaredSeparation, chargeProduct;
	double radiusOne, radiusTwo;
	double numerator, denominator, preFactor, f;
	Vector3D separation_s;
	
	atomOne = interaction->bond[0];
	atomTwo = interaction->bond[1];
	chargeProduct = interaction->params[2];
	
	//calculate seperation vector (r1 - r2)
	*(separation_s.vector + 0) = coordinates[atomOne][0] - coordinates[atomTwo][0];
	*(separation_s.vector + 1) = coordinates[atomOne][1] - coordinates[atomTwo][1];
	*(separation_s.vector + 2) = coordinates[atomOne][2] - coordinates[atomTwo][2];
		
	//Use the current length given by the interaction struct
	separation_s.length = interaction->length;
	
	radiusOne = radii[atomOne];
	radiusTwo = radii[atomTwo];
	
	squaredSeparation = separation_s.length*separation_s.length;
	preFactor =  electrostaticConstant*chargeProduct;
	
	//The value of the exponent in the derivative
	exponent = squaredSeparation/(4*radiusOne*radiusTwo);
	exponent = exp(-exponent);
	
	numerator = preFactor*exponent;
	denominator = pow((squaredSeparation + exponent), 1.5);
	f = numerator/denominator;
	
	//The first atom term
	*coefficient1 = f*(radiusTwo + squaredSeparation/(4*radiusOne));
	//The second atom term
	*coefficient2 = f*(radiusOne + squaredSeparation/(4*radiusTwo));
}

void AdGBEBornRadiusCoefficient(int atomOne, int atomTwo, double** coordinates, double* radii, double* charges, double* value)
{				     
	double exponent, squaredSeparation, chargeProduct;
	double radiusOne, radiusTwo;
	double numerator, denominator, preFactor, f;
	Vector3D separation_s;
	
	chargeProduct = charges[atomOne]*charges[atomTwo];
	
	//calculate seperation vector (r1 - r2)
	*(separation_s.vector + 0) = coordinates[atomOne][0] - coordinates[atomTwo][0];
	*(separation_s.vector + 1) = coordinates[atomOne][1] - coordinates[atomTwo][1];
	*(separation_s.vector + 2) = coordinates[atomOne][2] - coordinates[atomTwo][2];
	
	Ad3DVectorLengthSquared(&separation_s);
	squaredSeparation = separation_s.length;
	
	radiusOne = radii[atomOne];
	radiusTwo = radii[atomTwo];
	
	preFactor =  electrostaticConstant*chargeProduct;
	
	//The value of the exponent in the derivative
	exponent = squaredSeparation/(4*radiusOne*radiusTwo);
	exponent = exp(-exponent);
	
	numerator = preFactor*exponent;
	denominator = sqrt(squaredSeparation + exponent);
	denominator = denominator*denominator*denominator;
	f = numerator/denominator;
	
	//The first atom term
	*value = f*(radiusTwo + squaredSeparation/(4*radiusOne));
}

/*
 Returns the solvation force acting on the atom i.e. the derivative of the atoms self-energy.
 Note the derivative of the atoms born radius w.r.t. itself must have been previously calculated.
 */				     
void AdGBESelfDerivative(unsigned int atomIndex, 
					  double bornRadius, 
					  double charge, 
					  Vector3D* radiusDerivative,  
					  double** forces)
{
	double factor;
	
	factor = electrostaticConstant*charge*charge/(bornRadius*bornRadius);
	
	//Update the forces
	forces[atomIndex][0] += factor*radiusDerivative->vector[0];	
	forces[atomIndex][1] += factor*radiusDerivative->vector[1];
	forces[atomIndex][2] += factor*radiusDerivative->vector[2];
}