-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrain.std.c
443 lines (424 loc) · 10.3 KB
/
restrain.std.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
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
/* restrain.c
*
* collection of routines to service restrain potentials
*
* POOP (Poor-mans Object Oriented Programming) using scope rules
*
* these routines hold a data base (in terms of array indeces)
* of restraints, with the associated length and force constant
* These are updateable - unlike bonds which are "permanent"
*
* (this could be table driven but what the hell memories cheap)
*
* the routines for potential value, force and (eventually) second
* derivatives are here also
*
* force and 2nd derivative routines assume zero'd arrays for output
* this allows for parralellization if needed (on a PC?)
*
* forces are symmetric
*/
/*
* copyright 1992 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
*/
#define ANSI 1
/* misc includes - ANSI and some are just to be safe */
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#ifdef ANSI
#include <stdlib.h>
#endif
#include "ammp.h"
/* ATOM structure contains a serial number for indexing into
* arrays and the like (a Hessian)
* but otherwise is self-contained. Note the hooks for Non-restrained potentials
*/
typedef struct{
ATOM *atom1,*atom2;
float length,k;
void *next;
} RESTRAIN;
#define RLONG sizeof(RESTRAIN)
RESTRAIN *restrain_first = NULL;
RESTRAIN *restrain_last = NULL;
/* function restrain adds a restrain to the restrain list
* returns 1 if ok
* returns 0 if not
* is passed the atom serial numbers, length and constant
* allocates the new memory, initializes it and
* returns
*/
int restrain( p1,p2,bl,fk)
int p1,p2;
float bl,fk ;
{
ATOM *ap1,*ap2,*a_m_serial();
RESTRAIN *new;
char line[80];
/* get the atom pointers for the two serial numbers */
ap1 = a_m_serial( p1 );
ap2 = a_m_serial( p2 );
if( (ap1 == NULL) || (ap2 == NULL) )
{
sprintf( line,"undefined atom in restrain %d %d \0",p1,p2);
aaerror( line );
/* not a lethal error - just skip it */
return 1;
}
/* check to see if a restraint is already defined */
new = restrain_first;
if( new != NULL)
{
while(1)
{
if( new == NULL) break;
if( (new->atom1 == ap1 && new->atom2 == ap2) ||
(new->atom1 == ap2 && new->atom2 == ap1) )
{
new->length = bl; new->k = fk; return 1;
}
if( new == new->next) break;
new = new->next;
}
}
if( ( new = malloc( RLONG ) ) == NULL)
{
return 0;
}
/* initialize the pointers */
if( restrain_first == NULL) restrain_first = new;
if( restrain_last == NULL) restrain_last = new;
new -> atom1 = ap1;
new -> atom2 = ap2;
new -> length = bl;
new -> k = fk;
new -> next = new;
restrain_last -> next = new;
restrain_last = new;
return 1;
}
/* v_restrain()
* this function sums up the potentials
* for the atoms defined in the RESTRAIN data structure.
*/
/* standard returns 0 if error (any) 1 if ok
* V is the potential */
int v_restrain( V, lambda )
float *V,lambda;
{
RESTRAIN *bp;
float r,xt,yt,zt;
ATOM *a1,*a2;
bp = restrain_first;
if( bp == NULL ) return 1;
while(1)
{
if( bp == NULL) return 0;
a1 = bp->atom1; a2 = bp->atom2;
if( lambda == 0.)
{
r = (a1->x - a2->x)*(a1->x - a2->x);
r = r + (a1->y - a2->y)*(a1->y - a2->y);
r = r + (a1->z - a2->z)*(a1->z - a2->z);
} else
{
xt = (a1->x -a2->x +lambda*(a1->dx-a2->dx));
yt = (a1->y -a2->y +lambda*(a1->dy-a2->dy));
zt = (a1->z -a2->z +lambda*(a1->dz-a2->dz));
r = xt*xt+yt*yt+zt*zt;
}
r = sqrt(r);
if( a1->active || a2->active)
*V += bp->k*( r - bp->length)*(r - bp->length);
if( bp == bp->next ) return 1;
bp = bp->next;
}
}
/* f_restrain()
*
* f_restrain increments the forces in the atom structures by the force
* due to the restrain components. NOTE THE WORD increment.
* the forces should first be zero'd.
* if not then this code will be invalid. THIS IS DELIBERATE.
* on bigger (and better?) machines the different potential terms
* may be updated at random or in parrellel, if we assume that this routine
* will initialize the forces then we can't do this.
*/
int f_restrain(lambda)
float lambda;
/* returns 0 if error, 1 if OK */
{
RESTRAIN *bp;
float r,k,ux,uy,uz;
ATOM *a1,*a2;
bp = restrain_first;
if( bp == NULL ) return 1;
while(1)
{
if( bp == NULL) return 0;
k = bp->k;
a1 = bp->atom1; a2 = bp->atom2;
if( lambda == 0.)
{
ux = (a2->x - a1->x);
uy = (a2->y - a1->y);
uz = (a2->z - a1->z);
}else{
ux = (a2->x -a1->x +lambda*(a2->dx-a1->dx));
uy = (a2->y -a1->y +lambda*(a2->dy-a1->dy));
uz = (a2->z -a1->z +lambda*(a2->dz-a1->dz));
}
r = ux*ux + uy*uy + uz*uz;
/* watch for FP errors*/
if( r <= 1.e-5)
{ r = 0; ux = 1.; uy = 0.; uz = 0.; }else{
r = sqrt(r); ux = ux/r; uy = uy/r; uz = uz/r;
}
ux = 2*k*(r-bp->length)*ux;
uy = 2*k*(r-bp->length)*uy;
uz = 2*k*(r-bp->length)*uz;
if( a1->active){
a1->fx += ux;
a1->fy += uy;
a1->fz += uz;
}
if( a2->active){
a2->fx -= ux;
a2->fy -= uy;
a2->fz -= uz;
}
if( bp == bp->next ) return 1;
bp = bp->next;
}
}
/* function get_restrain( a1,restrained,10,inrestrain);
* check the RESTRAINS list for atoms restrained to a1
*/
void get_restrain( a1,restrained,mrestrain,inrestrain)
ATOM *a1, *restrained[];
int mrestrain,*inrestrain ;
{
RESTRAIN *mine;
mine = restrain_first;
*inrestrain = 0;
while(1)
{
if( (mine == NULL) )
{
return;
}
if( mine->atom1 == a1)
{
restrained[(*inrestrain)++] = mine->atom2;
}
if( mine->atom2 == a1)
{
restrained[(*inrestrain)++] = mine->atom1;
}
if( mine == mine->next) return;
mine = mine->next;
if( *inrestrain == mrestrain ) return;
}
}
/* routine dump_restrains
* this function outputs the restrain parameters
* and does it in a simple form
* restrain ser1,ser2,k,req
* the rest is just free format
*/
void dump_restrains( where )
FILE *where;
{
RESTRAIN *b;
ATOM *a1,*a2;
b = restrain_first;
if( b == NULL ) return;
while( (b->next != b) )
{
if( b->next == NULL) return;
a1 = b->atom1; a2 = b->atom2;
fprintf( where,"restrain %d %d %f %f ;\n",a1->serial,a2->serial,
b->length,b->k);
b = b->next;
}
if( b->next == NULL) return;
a1 = b->atom1; a2 = b->atom2;
fprintf( where,"restrain %d %d %f %f ;\n",a1->serial,a2->serial,
b->length,b->k);
}
/* a_restrain()
* this function sums up the potentials
* for the atoms defined in the RESTRAIN data structure.
*/
/* standard returns 0 if error (any) 1 if ok
* V is the potential */
int a_restrain( V, lambda,ilow,ihigh,op )
float *V,lambda;
int ilow,ihigh;
FILE *op;
{
RESTRAIN *bp;
float r,xt,yt,zt;
ATOM *a1,*a2;
bp = restrain_first;
if( bp == NULL ) return 1;
while(1)
{
if( bp == NULL) return 0;
a1 = bp->atom1; a2 = bp->atom2;
if(( a1->serial >= ilow && a1->serial <=ihigh)
||( a2->serial >= ilow && a2->serial <=ihigh))
{
if( lambda == 0.)
{
r = (a1->x - a2->x)*(a1->x - a2->x);
r = r + (a1->y - a2->y)*(a1->y - a2->y);
r = r + (a1->z - a2->z)*(a1->z - a2->z);
} else
{
xt = (a1->x -a2->x +lambda*(a1->dx-a2->dx));
yt = (a1->y -a2->y +lambda*(a1->dy-a2->dy));
zt = (a1->z -a2->z +lambda*(a1->dz-a2->dz));
r = xt*xt+yt*yt+zt*zt;
}
r = sqrt(r); zt= bp->k*( r - bp->length)*(r - bp->length);
*V += zt;
fprintf(op,"Restrain %d %d E %f value %f error %f\n"
,a1->serial,a2->serial,zt,r,r-bp->length);
}
if( bp == bp->next ) return 1;
bp = bp->next;
}
}
/* load the distance data into the appropriate storage for gsdg, and dgeom */
int gsdg_restrain( who)
ATOM *who;
{
RESTRAIN *bp;
ATOM *ap;
bp = restrain_first;
while(1)
{
if( bp == NULL) return;
if( bp->atom1 == who)
{ ap = bp->atom2; ap->vx = bp->length*bp->length; ap->vy = bp->k;}
if( bp->atom2 == who)
{ ap = bp->atom1; ap->vx = bp->length*bp->length; ap->vy = bp->k;}
if( bp == bp->next ) return;
bp = bp->next;
}
}
/* v_ho_restrain()
* this function sums up the potentials
* for the atoms defined in the RESTRAIN data structure.
*/
/* standard returns 0 if error (any) 1 if ok
* V is the potential */
int v_ho_restrain( V, lambda )
float *V,lambda;
{
RESTRAIN *bp;
float r,xt,yt,zt;
ATOM *a1,*a2;
float hol, get_f_variable();
float target;
hol = get_f_variable( "lambda");
if( hol < 0. ) hol = 0.;
if( hol > 1. ) hol = 1.;
bp = restrain_first;
if( bp == NULL ) return 1;
while(1)
{
if( bp == NULL) return 0;
a1 = bp->atom1; a2 = bp->atom2;
if( lambda == 0.)
{
r = (a1->x - a2->x)*(a1->x - a2->x);
r = r + (a1->y - a2->y)*(a1->y - a2->y);
r = r + (a1->z - a2->z)*(a1->z - a2->z);
} else
{
xt = (a1->x -a2->x +lambda*(a1->dx-a2->dx));
yt = (a1->y -a2->y +lambda*(a1->dy-a2->dy));
zt = (a1->z -a2->z +lambda*(a1->dz-a2->dz));
r = xt*xt+yt*yt+zt*zt;
}
r = sqrt(r);
target = hol*r + (1.-hol)*bp->length;
if( a1->active || a2->active)
*V += bp->k*( r -target)*(r - target);
if( bp == bp->next ) return 1;
bp = bp->next;
}
}
/* f_restrain()
*
* f_restrain increments the forces in the atom structures by the force
* due to the restrain components. NOTE THE WORD increment.
* the forces should first be zero'd.
* if not then this code will be invalid. THIS IS DELIBERATE.
* on bigger (and better?) machines the different potential terms
* may be updated at random or in parrellel, if we assume that this routine
* will initialize the forces then we can't do this.
*/
int f_ho_restrain(lambda)
float lambda;
/* returns 0 if error, 1 if OK */
{
RESTRAIN *bp;
float r,k,ux,uy,uz;
ATOM *a1,*a2;
float hol, get_f_variable();
float target;
hol = get_f_variable( "lambda");
if( hol < 0. ) hol = 0.;
if( hol > 1. ) hol = 1.;
bp = restrain_first;
if( bp == NULL ) return 1;
while(1)
{
if( bp == NULL) return 0;
k = bp->k;
a1 = bp->atom1; a2 = bp->atom2;
if( lambda == 0.)
{
ux = (a2->x - a1->x);
uy = (a2->y - a1->y);
uz = (a2->z - a1->z);
}else{
ux = (a2->x -a1->x +lambda*(a2->dx-a1->dx));
uy = (a2->y -a1->y +lambda*(a2->dy-a1->dy));
uz = (a2->z -a1->z +lambda*(a2->dz-a1->dz));
}
r = ux*ux + uy*uy + uz*uz;
/* watch for FP errors*/
if( r <= 1.e-5)
{ r = 0; ux = 1.; uy = 0.; uz = 0.; }else{
r = sqrt(r); ux = ux/r; uy = uy/r; uz = uz/r;
}
target = hol*r + (1.-hol)*bp->length;
ux = 2*k*(r-target)*(1.- hol)*ux;
uy = 2*k*(r-target)*(1.- hol)*uy;
uz = 2*k*(r-target)*(1.- hol)*uz;
if( a1->active){
a1->fx += ux;
a1->fy += uy;
a1->fz += uz;
}
if( a2->active){
a2->fx -= ux;
a2->fy -= uy;
a2->fz -= uz;
}
if( bp == bp->next ) return 1;
bp = bp->next;
}
}