File: AdCoulombAndLennardJonesA.c

package info (click to toggle)
adun.app 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,824 kB
  • ctags: 713
  • sloc: objc: 49,683; ansic: 4,680; sh: 523; python: 79; makefile: 67; cpp: 33
file content (558 lines) | stat: -rwxr-xr-x 17,841 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
   Project: Adun

   Copyright (C) 2005 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 <Base/AdForceFieldFunctions.h>

bool __NonbondedEnergyDebug__ = false;
bool __NonbondedForceDebug__ = false;
bool __ShiftedNonbondedEnergyDebug__ = false;
bool __ShiftedNonbondedForceDebug__ = false;
bool __GRFNonbondedEnergyDebug__ = false;
bool __GRFNonbondedForceDebug__ = false;

inline void AdNonbondedEnergyLog(char* cutType, char* ljType, int a1, int a2, double ljA,
		double ljB, double charge, double sep, double estPot, double ljPot, bool flag)
{
	if(flag)
		fprintf(stderr, "%-8s%-3s%-6d%-6d%-12.5lf%-12.5lf%-12.5lf%-12.5lf%-12.lf%-12.5lf\n",
			cutType, ljType, a1, a2, ljA, ljB, charge, sep, estPot, ljPot);
}

inline void AdNonbondedForceLog(char* cutType, char* ljType, int a1, int a2, double ljA,
		double ljB, double charge, double sep, double estPot, double ljPot, double force, bool flag)
{
	if(flag)
		fprintf(stderr, "%-8s%-3s%-6d%-6d%-12.5lf%-12.5lf%-12.5lf%-12.5lf%-12.lf%-12.5lf%-12.5lf\n",
			cutType, ljType, a1, a2, ljA, ljB, charge, sep, estPot, ljPot, force);	
}

inline void AdCoulombAndLennardJonesAEnergy(ListElement* interaction, 
		double** coordinates, 
		double EPSILON_RP, 
		double cutoff,
		double* vdw_pot, 
		double* est_pot)
{
	int atom_one, atom_two;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];

	//calculate seperation vector (r1 - r2)
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	//calculate the interatomic distance
	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cutoff)
		return;
	
	//add this length to the linked list element the bond belongs to
	interaction->length = seperation_s.length;

	//get reciprocal of seperation
	length_rec = 1/seperation_s.length;
	
	vdw_hold = pow(length_rec, 6);
	est_hold = (EPSILON_RP*chargeProduct*length_rec);
	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;

	*est_pot += est_hold;
	*vdw_pot += lennardJonesA - lennardJonesB;	

#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedEnergyLog("Normal", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
		chargeProduct, separation_s.length, est_hold, (lennardJonesA - lennardJonesB),
		__NonbondedEnergyDebug__); 
#endif	
}

inline void AdCoulombAndLennardJonesAForce(ListElement* interaction, 
		double** coordinates, 
		double** forces,
		double EPSILON_RP, 
		double cutoff,
		double* vdw_pot, 
		double* est_pot)
{
	int atom_one, atom_two;
	double force_mag;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	//calculate seperation vector (r1 - r2)
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	//calculate the interatomic distance
	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cutoff)
		return;
	
	//add this length to the linked list wlement the bond belongs to
	interaction->length = seperation_s.length;

	//get reciprocal of seperation
	length_rec = 1/seperation_s.length;
	
	//calculate vdw holder
	vdw_hold = pow(length_rec, 6);

	//while this is being calculate 
	//calculate some non dependant variables

	//est holder
	est_hold = EPSILON_RP*chargeProduct*length_rec;

	//vdw holder may be finished now so calculate vdw consts
	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;
	
	*est_pot += est_hold;
	*vdw_pot += lennardJonesA - lennardJonesB;	

	force_mag = est_hold*length_rec;
	
	//add the vdw force to the est force
	force_mag +=  6*length_rec*(2*lennardJonesA - lennardJonesB);
	force_mag *= length_rec;

	//calculate the force on atom one along the vector (r1 - r2)
	//the force on atom two is the opposite of this force
	*(seperation_s.vector + 0) *= force_mag;
	*(seperation_s.vector + 1) *= force_mag;
	*(seperation_s.vector + 2) *= force_mag;
	
	forces[atom_one][0] += *(seperation_s.vector + 0);
	forces[atom_one][1] += *(seperation_s.vector + 1);
	forces[atom_one][2] += *(seperation_s.vector + 2);

	forces[atom_two][0] -= *(seperation_s.vector + 0);
	forces[atom_two][1] -= *(seperation_s.vector + 1);
	forces[atom_two][2] -= *(seperation_s.vector + 2);
	
#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedForceLog("Normal", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
			     chargeProduct, separation_s.length, est_hold,
			    (lennardJonesA - lennardJonesB), forceMag,
			    __NonbondedForceDebug__); 
#endif
}

inline void AdCoulombAndLennardJonesAForceTest(ListElement* interaction, 
		Vector3D* seperation_s, 
		double** forces,
		double EPSILON_RP, 
		double* vdw_pot, 
		double* est_pot)
{
	int atom_one, atom_two;
	double force_mag;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	/*//calculate the interatomic distance
	Ad3DVectorLength(&seperation_s);*/
	
	//add this length to the linked list wlement the bond belongs to
	interaction->length = seperation_s->length;

	//get reciprocal of seperation
	length_rec = 1/seperation_s->length;
	
	//calculate vdw holder
	vdw_hold = pow(length_rec, 6);

	//while this is being calculate 
	//calculate some non dependant variables

	//est holder
	est_hold = EPSILON_RP*chargeProduct*length_rec;

	//vdw holder may be finished now so calculate vdw consts
	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;
	
	*est_pot += est_hold;
	*vdw_pot += lennardJonesA - lennardJonesB;	

	force_mag = est_hold*length_rec;
	
	//add the vdw force to the est force
	force_mag +=  6*length_rec*(2*lennardJonesA - lennardJonesB);
	force_mag *= length_rec;

	//calculate the force on atom one along the vector (r1 - r2)
	//the force on atom two is the opposite of this force
	*(seperation_s->vector + 0) *= force_mag;
	*(seperation_s->vector + 1) *= force_mag;
	*(seperation_s->vector + 2) *= force_mag;
	
	forces[atom_one][0] += *(seperation_s->vector + 0);
	forces[atom_one][1] += *(seperation_s->vector + 1);
	forces[atom_one][2] += *(seperation_s->vector + 2);

	forces[atom_two][0] -= *(seperation_s->vector + 0);
	forces[atom_two][1] -= *(seperation_s->vector + 1);
	forces[atom_two][2] -= *(seperation_s->vector + 2);
}

inline void AdShiftedCoulombAndLennardJonesAForce(ListElement* interaction, 
		double** coordinates, 
		double** forces,
		double EPSILON_RP, 
		double cut,
		double r_cutoff2,
		double* vdw_pot, 
		double* est_pot)
{
	int atom_one, atom_two;
	double force_mag;
	double length_rec, vdw_hold, est_hold;
	double shift_fac;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];

	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cut)
		return;
	interaction->length = seperation_s.length;

	length_rec = 1/seperation_s.length;
	vdw_hold = pow(length_rec, 6);
	est_hold = EPSILON_RP*chargeProduct*length_rec;
	
	//est shift factor
	shift_fac = (cut - seperation_s.length)*(cut - seperation_s.length)*r_cutoff2;

	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;

	//the shifted est force is EST_HOLD*(length_rec - r_cutoff2*length)
	force_mag = (length_rec - r_cutoff2*seperation_s.length);

	*est_pot += (est_hold*shift_fac);
	*vdw_pot += lennardJonesA - lennardJonesB;	
	
	//apply the shift to the est force
	force_mag *= est_hold;

	//add the vdw force
	force_mag +=  6*length_rec*(2*lennardJonesA - lennardJonesB);
	force_mag *= length_rec;

	*(seperation_s.vector + 0) *= force_mag;
	*(seperation_s.vector + 1) *= force_mag;
	*(seperation_s.vector + 2) *= force_mag;
	
	forces[atom_one][0] += *(seperation_s.vector + 0);
	forces[atom_one][1] += *(seperation_s.vector + 1);
	forces[atom_one][2] += *(seperation_s.vector + 2);

	forces[atom_two][0] -= *(seperation_s.vector + 0);
	forces[atom_two][1] -= *(seperation_s.vector + 1);
	forces[atom_two][2] -= *(seperation_s.vector + 2);
	
#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedForceLog("Shifted", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
			    chargeProduct, separation_s.length, est_hold,
			    (lennardJonesA - lennardJonesB), forceMag,
			    __ShiftedNonbondedForceDebug__); 
#endif
}

inline void AdGRFCoulombAndLennardJonesAForce(ListElement* interaction, 
		double** coordinates, 
		double** forces,
		double EPSILON_RP, 
		double cutoff,
		double b0, 
		double b1,
		double* vdw_pot, 
		double* est_pot) 
{
	int atom_one, atom_two;
	double force_mag;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];

	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cutoff)
		return;
	interaction->length = seperation_s.length;
	length_rec = 1/seperation_s.length;
	
	vdw_hold = pow(length_rec, 6);
	est_hold = EPSILON_RP*chargeProduct*length_rec;

	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;
	
	//GRF
	*est_pot += est_hold + EPSILON_RP*chargeProduct*b0;
	*vdw_pot += lennardJonesA - lennardJonesB;	
	force_mag = est_hold*length_rec + EPSILON_RP*chargeProduct*b1*seperation_s.length;

	//add the vdw force to the est force
	force_mag +=  6*length_rec*(2*lennardJonesA - lennardJonesB);
	//Since we have to divide the separation vector by this
	force_mag *=length_rec;

	*(seperation_s.vector + 0) *= force_mag;
	*(seperation_s.vector + 1) *= force_mag;
	*(seperation_s.vector + 2) *= force_mag;
	
	forces[atom_one][0] += *(seperation_s.vector + 0);
	forces[atom_one][1] += *(seperation_s.vector + 1);
	forces[atom_one][2] += *(seperation_s.vector + 2);

	forces[atom_two][0] -= *(seperation_s.vector + 0);
	forces[atom_two][1] -= *(seperation_s.vector + 1);
	forces[atom_two][2] -= *(seperation_s.vector + 2);
	
#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedForceLog("GRF", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
			    chargeProduct, separation_s.length, est_hold,
			    (lennardJonesA - lennardJonesB), forceMag,
			    __GRFNonbondedForceDebug__); 
#endif
}

inline void AdShiftedCoulombAndLennardJonesAEnergy(ListElement* interaction, 
		double** coordinates, 
		double EPSILON_RP, 
		double cut,
		double r_cutoff2,
		double* vdw_pot, 
		double* est_pot)
{		
	int atom_one, atom_two;
	double length_rec, vdw_hold, est_hold;
	double shift_fac;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cut)
		return;
	interaction->length = seperation_s.length;
			
	length_rec = 1/seperation_s.length;
	
	vdw_hold = pow(length_rec, 6);
	est_hold = (EPSILON_RP*chargeProduct*length_rec);

	//est shift factor
	shift_fac = (cut - seperation_s.length)*(cut - seperation_s.length)*r_cutoff2;
	
	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;
	
	//modify the potential by the shift function
	*est_pot += (est_hold*shift_fac);
	*vdw_pot += lennardJonesA - lennardJonesB;	
	
#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedEnergyLog("Shifted", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
			     chargeProduct, separation_s.length, est_hold, (lennardJonesA - lennardJonesB),
			     __ShiftedNonbondedEnergyDebug__); 
#endif
}

inline void AdGRFCoulombAndLennardJonesAEnergy(ListElement* interaction, 
		double** coordinates, 
		double EPSILON_RP, 
		double cutoff,
		double b0, 
		double b1,
		double* vdw_pot, 
		double* est_pot) 
{
	int atom_one, atom_two;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	Ad3DVectorLength(&seperation_s);
	if(seperation_s.length > cutoff)
		return;
	interaction->length = seperation_s.length;
	length_rec = 1/seperation_s.length;
	vdw_hold = pow(length_rec, 6);
	est_hold = EPSILON_RP*chargeProduct*(length_rec + b0);
	lennardJonesA *= vdw_hold*vdw_hold;
	lennardJonesB *= vdw_hold;

	*est_pot += est_hold;
	*vdw_pot += lennardJonesA - lennardJonesB;
		
#ifdef BASE_NONBONDED_DEBUG
	AdNonbondedEnergyLog("GRF", "A", atomOne, atomTwo, lennardJonesA, lennardJonesB, 
			     chargeProduct, separation_s.length, est_hold, (lennardJonesA - lennardJonesB),
			     __GRFNonbondedEnergyDebug__); 
#endif
}

/*
inline void AdCoulombAndLennardJonesAForce(ListElement* interaction, 
		double** coordinates, 
		double** forces,
		double EPSILON_RP, 
		double* vdw_pot, 
		double* est_pot)
{
	int atom_one, atom_two;
	double force_mag;
	double length_rec, vdw_hold, est_hold;
	double lennardJonesA, lennardJonesB, chargeProduct;
	Vector3D seperation_s;
	
	atom_one = interaction->bond[0];
	atom_two = interaction->bond[1];
	lennardJonesA = interaction->params[0];
	lennardJonesB = interaction->params[1];
	chargeProduct = interaction->params[2];
			
	//calculate seperation vector (r1 - r2)
	*(seperation_s.vector + 0) = coordinates[atom_one][0] - coordinates[atom_two][0];
	*(seperation_s.vector + 1) = coordinates[atom_one][1] - coordinates[atom_two][1];
	*(seperation_s.vector + 2) = coordinates[atom_one][2] - coordinates[atom_two][2];
	
	//calculate the interatomic distance
	Ad3DVectorLength(&seperation_s);


	
	
	//add this length to the linked list wlement the bond belongs to
	interaction->length = seperation_s.length;

	//get reciprocal of seperation
	length_rec = 1/seperation_s.length;
	
	//calculate vdw holder
	e = _mm_mul(d,d);
	f = _mm_mul(d,d);
	g = __mm_mul(d,d);
	e = __mm_mul(f,e);
	g = __mm_mul(g,e)
	
	//while this is being calculate 
	//calculate some non dependant variables

	//est holder
	est_hold = _mm_mul(EPSILON_RP, chargeProduct);
	est_hold = __mm_mul(est_hold, length_rec);

	//vdw holder may be finished now so calculate vdw consts
	lennardJonesA *= __mm_mul(lennardJonesA, vdw_hold)
	lennardJonesB *= __mm_mul(lennardJonesB, vdw_hold)
	lennardJonesA *= __mm_mul(lennardJonesA, vdw_hold)
	
	*est_pot += est_hold;
	*vdw_pot += lennardJonesA - lennardJonesB;	

	force_mag = __mm_mul(est_hold, length_rec);
	
	//add the vdw force to the est force
	force_mag +=  6*length_rec*(2*lennardJonesA - lennardJonesB);
	force_mag *= length_rec;

	//calculate the force on atom one along the vector (r1 - r2)
	//the force on atom two is the opposite of this force
	*(seperation_s.vector + 0) *= force_mag;
	*(seperation_s.vector + 1) *= force_mag;
	*(seperation_s.vector + 2) *= force_mag;
	
	forces[atom_one][0] += *(seperation_s.vector + 0);
	forces[atom_one][1] += *(seperation_s.vector + 1);
	forces[atom_one][2] += *(seperation_s.vector + 2);

	forces[atom_two][0] -= *(seperation_s.vector + 0);
	forces[atom_two][1] -= *(seperation_s.vector + 1);
	forces[atom_two][2] -= *(seperation_s.vector + 2);

}
*/