-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpole.c
234 lines (214 loc) · 4.54 KB
/
pole.c
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
/* AMMP pole correction */
/* add a factored pole term to the system
// form of
// pole atom_id constant n
//
// for (constant *constant)/r^n
//
// only applied when the n values match.
copyright (C) 2006 Robert W Harrison
* This notice may not be removed
* This program may be copied for scientific use
* It may not be sold for profit without explicit
* permission of the author(s) who retain any
* commercial rights including the right to modify
* this notice
*/
#include "ammp.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
ATOM *atom;
float k;
int n;
void *next;
} POLE;
#define poleLONG sizeof(POLE)
static POLE *pole_first = NULL;
static POLE *pole_last = NULL;
int pole( p1, konst, n)
int p1,n;
float konst;
{
ATOM *ap1, *a_m_serial(int);
POLE *new;
ap1 = a_m_serial( p1);
if( ap1 == (ATOM*)NULL) return 1;
new = pole_first;
if( new != (POLE*)NULL)
{
while( 1)
{
if( new->next == NULL) break;
if( new->atom == ap1 && n == new->n)
{
new->k = konst;
return 1;
}
if( new->next == new) break;
new = new->next;
}
}/* new is not defined */
if( (new = malloc( poleLONG) ) == NULL)
{
aaerror("cannot allocate memory in pole\n");
return 0;
}
if( pole_first == NULL) pole_first = new;
if( pole_last == NULL) pole_last = new;
new->atom = ap1;
new->k = konst;
new->n = n;
new->next = new;
pole_last -> next = new;
pole_last = new;
return 1;
}/* end of pole */
/* this gets used a bunch so we'll wrap it
* remember static means that it only has scope in this file */
/* probably better to use pow( double, double) for this */
static float my_pow( r,n)
float r;
int n;
{
float accum = 1.;
float mult ;
int i;
i = n;
mult = r;
while( i > 0)
{
if( (i & 1) != 0) /* low order bit */
accum *= mult;
mult *= mult;
i /= 2;
}
return accum;
}
int v_pole( V, lambda)
float *V;
float lambda;
{
POLE *apole;
POLE *bpole;
ATOM *ap,*bp;
if( pole_first == NULL ) return 0 ;
apole = pole_first;
/* check for trivial calculations here */
bpole = apole->next;
if( apole == bpole) return 0;
if( bpole == NULL ) return 0;
/* loop over every pair of poles */
while( 1==1)
{
int i;
float dx,dy,dz,dr;
ap = apole->atom;
bpole = apole->next;
while( 1==1)
{
bp = bpole->atom;
if( bpole->n != apole->n) goto SKIP_THIS_ATOM;
for( i=0; i< ap->dontuse; i++)
{
if( bp == ap->excluded[i]) goto SKIP_THIS_ATOM;
}
dx = ap->x - bp->x + lambda*(ap->dx - bp->dx);
dy = ap->y - bp->y + lambda*(ap->dy - bp->dy);
dz = ap->z - bp->z + lambda*(ap->dz - bp->dz);
dr = dx*dx + dy*dy + dz*dz;
if( dr > 1.)
{
dr = sqrt(dr);
dr = my_pow( dr, apole->n);
} else { dr = 1.;}
*V += apole->k*bpole->k/dr;
SKIP_THIS_ATOM: ;
if( bpole == bpole->next) break;
if( bpole->next == NULL) break;
bpole = bpole->next;
}/* inner loop */
if( apole == apole->next) return 0;
if( apole->next == NULL ) return 0;
apole = apole->next;
}
}/* end of v_pole */
int f_pole( lambda)
float lambda;
{
POLE *apole;
POLE *bpole;
ATOM *ap,*bp;
float accum;
accum = 0.;
if( pole_first == NULL ) return 0.;
apole = pole_first;
/* check for trivial calculations here */
bpole = apole->next;
if( apole == bpole) return 0;
if( bpole == NULL ) return 0;
/* loop over every pair of poles */
while( 1==1)
{
int i;
float konst;
float dx,dy,dz,dr;
ap = apole->atom;
bpole = apole->next;
while( 1==1)
{
bp = bpole->atom;
if( bpole->n != apole->n) goto SKIP_THIS_ATOM;
for( i=0; i< ap->dontuse; i++)
{
if( bp == ap->excluded[i]) goto SKIP_THIS_ATOM;
}
dx = ap->x - bp->x + lambda*(ap->dx - bp->dx);
dy = ap->y - bp->y + lambda*(ap->dy - bp->dy);
dz = ap->z - bp->z + lambda*(ap->dz - bp->dz);
dr = dx*dx + dy*dy + dz*dz;
if( dr > 1.)
{
dr = sqrt(dr);
dr = my_pow( dr, apole->n+2);
} else { dr = 1.;}
konst = apole->n*apole->k*bpole->k/dr;
if( ap->active)
{
ap->fx += dx*konst;
ap->fy += dy*konst;
ap->fz += dz*konst;
}
if( bp->active)
{
bp->fx -= dx*konst;
bp->fy -= dy*konst;
bp->fz -= dz*konst;
}
SKIP_THIS_ATOM: ;
if( bpole == bpole->next) break;
if( bpole->next == NULL ) break;
bpole = bpole->next;
}/* inner loop */
if( apole == apole->next) return 0;
if( apole->next == NULL ) return 0;
apole = apole->next;
}
}/* end of f_pole */
int dump_pole( op)
FILE *op;
{
POLE *apole;
ATOM *ap;
if( pole_first == NULL ) return 0 ;
apole = pole_first;
while( 1==1)
{
ap = apole->atom;
fprintf(op,"pole %d %f %d;\n", ap->serial, apole->k, apole->n);
if( apole->next == NULL) return 0;
if( apole == apole->next) return 0;
apole = apole->next;
}
}