-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.std.c
572 lines (564 loc) · 15.6 KB
/
math.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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* math.c
* perform basic mathematical operations on data
* return values to memory locations
* uses floating point contagion
* defined ops
* add a b a <= a+b
* sub a b a <= a - b
* mul a b a <= a*b
* div a b a <= a/b
* fix a a <= (int) a (drops the fraction for atomdata)
* sqrt a a <= sqrt a (becomes a float !!!)
* nop a just echo the value
* mov a b a <= b
* randf a put a random variable 0-<1. in a
* max a b a <= max( a b )
* min a b b <= min( a b )
* serial a res atom move the serial number of the atom
* into a (res, atom are like 100 ca)
* index a i move the serial number of the i'th atom into a
*
* je a b label: jump to label if equal
* jl a b label: jump to label if a < b
* jg a b label: jump to label if a > b
* jes a string label: jump to label: if a->label == string
* jnes a string label: jump to label: if a->label != string
* label: is a label within the current script file
* which may be within the current loop
* the j<elg> commands will rewind the input file
* and search for label:
*
*
* data types
* imeadiate e.g. a number like 3.14159
* variable e.g. a name like pi
* atomdata serial.<x,y,z,fx,fy,fz,dx,dy,dz,vx,vy,vz,q,a,b,m,chi,jaa>
* serial may be a variable
* valid format atoms with non-extant serial numbers will be
* ignored and the operation will silently not happen
* this allows sums over discontinous atom ranges
*
*
* routines defined in this module
*
* math() does the work
* getatomdata() returns a float * or null for an atomdata
* validatom() returns nonzero when atomdata format is valid
*
*/
/* header from variable.c
*
* variable storage and retreival routines for AMMP
*
* using scope rules for structuring
*
*
* variables are stored in linked list form
*
* get_f_variable( char *name, float *fvalue )
* get_i_variable( char *name, int *ivalue )
* returns variable who matches name (all lower case )
* set_f_variable( char *name, float fvalue )
* set_i_variable( char *name, int ivalue )
* sets variable who matches name (all lower case )
* match_variable( char *name ) returns pointer to name if there NULL if not
* dump_variable(FILE *output )
* dumps variables to file
*/
/*
* copyright 1992 1993 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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#ifdef ANSI
#include <stdlib.h>
#endif
#include "ammp.h"
#define NUM_SIG 6
#define NUM_TOT 7
#define INTEGER 0
#define FLOAT 1
typedef struct{
int type;
char name[NUM_TOT];
union{ float f; int i;} value;
void *next;
} VARIABLE;
/* next define is from ammp.c */
#define TOKENLENGTH 80
#define ATOMDATA 0
#define IMEADIATE 1
#define VARDATA 2
int math( tokens,fvalue,ivalue,ip,op,echo)
#ifdef ESV
#define tokens (*tokens)
#endif
char tokens[][TOKENLENGTH];
float fvalue[];
int ivalue[];
FILE *ip,*op;
int echo ;
{
int adata,bdata;
int atype, btype;
float *foutpointer,*fp,fa,fb,randf();
int *ioutpointer,ia,ib;
VARIABLE *vp,*vos,*match_variable();
float *getatomdata(); /* return the address of the atomdata if valid */
int validatom(); /* return 0 iff not a valid atomdata format */
int set_f_variable(), set_i_variable();
int a_number(),tisint();
ATOM *ap,*a_next(),*a_m_serial();
int i,j;
float a_l2_f(),a_max_f();
/* set some default variables */
set_i_variable( "numatm",a_number());
set_f_variable( "l2f",a_l2_f());
set_f_variable( "lmaxf",a_max_f());
/*
* set_f_variable( char *name, float fvalue )
* set_i_variable( char *name, int ivalue )
*/
/* type the variables */
NEW_AVAR:
if( (vp = match_variable(&tokens[1][0])) != NULL)
{
adata = VARDATA;
atype = vp->type;
vos = vp;
if( atype == FLOAT){ fa = vp->value.f;}
foutpointer = &vp->value.f;
if( atype == INTEGER){ia = vp->value.i; }
ioutpointer = &vp->value.i;
goto AFOUND;
}
if( (foutpointer = getatomdata(&tokens[1][0])) != NULL)
{
adata = ATOMDATA;
atype = FLOAT;
fa = *foutpointer;
goto AFOUND;
}
if( validatom(&tokens[1][0]) != 0 ) return( 1);
foutpointer = NULL; /* shouldn't change it but this is safe */
adata = IMEADIATE;
fa = fvalue[1];
ia = ivalue[1];
atype = FLOAT;
if( tisint( &tokens[1][0]) == 1) atype = INTEGER;
/*
if( tisint( &tokens[1][0]) == 1)printf(" INTEGER ");
printf(">%s<\n",&tokens[1][0]);
*/
AFOUND:
if( (vp = match_variable(&tokens[2][0])) != NULL)
{
/* bdata = VARDATA;
*/
btype = vp->type;
if( btype == FLOAT) fb = vp->value.f;
if( btype == INTEGER) ib = vp->value.i;
goto BFOUND;
}
if( (fp = getatomdata(&tokens[2][0])) != NULL)
{
/* bdata = ATOMDATA;
*/
btype = FLOAT;
fb = *fp;
goto BFOUND;
}
if( validatom(&tokens[2][0]) != 0 ) return( 1);
/* bdata = IMEADIATE;
*/
fb = fvalue[2];
ib = ivalue[2];
btype = FLOAT;
if( tisint( &tokens[2][0]) == 1) btype = INTEGER;
/*
if( tisint( &tokens[2][0]) == 1)printf(" INTEGER ");
printf(">%s<\n",&tokens[2][0]);
*/
BFOUND:
/* make the converted data */
if( atype == FLOAT) ia = (int)fa;
if( atype == INTEGER) fa = (float)ia;
if( btype == FLOAT) ib = (int)fb;
if( btype == INTEGER) fb = (float)ib;
/* is it a defined command ? */
if( strcmp( &tokens[0][0], "add" ) == 0 )
{
if( atype == FLOAT) fa = fa + fb;
if( atype == INTEGER) ia = ia + ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "sub" ) == 0 )
{
if( atype == FLOAT) fa = fa - fb;
if( atype == INTEGER) ia = ia - ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "mul" ) == 0 )
{
if( atype == FLOAT) fa = fa * fb;
if( atype == INTEGER) ia = ia * ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "div" ) == 0 )
{
if( atype == FLOAT) fa = fa / fb;
if( atype == INTEGER) ia = ia / ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "fix" ) == 0 )
{
if( adata == ATOMDATA ) {
ia = (int)fa;
*foutpointer = (float)ia;
if( echo ) fprintf(op,"%d \n",ia);
return(1);
}
if( atype == FLOAT) {atype = INTEGER; ia = (int)fa;}
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "sqrt" ) == 0 )
{
if( adata == ATOMDATA ) {
if( fa > 0.)
*foutpointer = sqrt(fa);
else
*foutpointer = -sqrt(-fa);
if( echo ) fprintf(op,"%f \n",*foutpointer);
return(1);
}
atype = FLOAT;
if( fa > 0) fa = sqrt(fa);
else fa = -sqrt(-fa);
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "nop" ) == 0 )
{
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "mov" ) == 0 )
{
ia = ib;
fa = fb;
atype = btype;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "max" ) == 0 )
{
if( atype == FLOAT && fa < fb) fa = fb;
if( atype == INTEGER && ia < ib) ia = ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "min" ) == 0 )
{
if( atype == FLOAT && fa > fb) fa = fb;
if( atype == INTEGER && ia > ib) ia = ib;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "randf" ) == 0 )
{
atype = FLOAT;
fa = randf( -1 );
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "serial" ) == 0 )
{
ia = 100*ib-1;
j = ia + 100;
i = -1;
while( (ap = a_next(i)) != NULL )
{
i = 1;
if( ap->serial > ia && ap->serial < j)
if( math_match_atom(&tokens[3][0],ap) != 0 ){
atype = INTEGER;
ia = ap->serial;
goto GOOD_OP;
}
}
ia = -1; /* never a serial number */
atype = INTEGER;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "index" ) == 0 )
{
ap = a_next(-1);
for( i=0; i< ib; i++)
ap = a_next(i);
ia = ap->serial;
atype = INTEGER;
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "jes" ) == 0 )
{
if( tokens[3][0] == '\0')
{ aaerror("label: required for a jump \n"); goto GOOD_OP;}
ap = a_m_serial( ia ); if( ap == NULL) return -1;
if( strcmp( &ap->name[0], &tokens[2][0]) == 0)
{
rewind(ip);
math_findlabel( ip,&tokens[3][0]);
}
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "jnes" ) == 0 )
{
if( tokens[3][0] == '\0')
{ aaerror("label: required for a jump \n"); goto GOOD_OP;}
ap = a_m_serial( ia ); if( ap == NULL) return -1;
if( strcmp( &ap->name[0], &tokens[2][0]) != 0)
{
rewind(ip);
math_findlabel( ip,&tokens[3][0]);
}
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "je" ) == 0 )
{
if( tokens[3][0] == '\0')
{ aaerror("label: required for a jump \n"); goto GOOD_OP;}
if( (atype == INTEGER && ia == ib ) ||
(atype == FLOAT && fa == fb ) )
{
rewind(ip);
math_findlabel( ip,&tokens[3][0]);
}
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "jg" ) == 0 )
{
if( tokens[3][0] == '\0')
{ aaerror("label: required for a jump \n"); goto GOOD_OP;}
if( (atype == INTEGER && ia > ib ) ||
(atype == FLOAT && fa > fb ) )
{
rewind(ip);
math_findlabel( ip,&tokens[3][0]);
}
goto GOOD_OP;
}
if( strcmp( &tokens[0][0], "jl" ) == 0 )
{
if( tokens[3][0] == '\0')
{ aaerror("label: required for a jump \n"); goto GOOD_OP;}
if( (atype == INTEGER && ia < ib ) ||
(atype == FLOAT && fa < fb ) )
{
rewind(ip);
math_findlabel( ip,&tokens[3][0]);
}
goto GOOD_OP;
}
return(-1);
/* if found we jump here */
GOOD_OP:
if( tisvariable(&tokens[1][0] ) && tokens[1][0] != '\0' && adata == IMEADIATE )
{set_i_variable(&tokens[1][0],0); adata = VARDATA;
vos = match_variable(&tokens[1][0]);
foutpointer = &vos->value.f;
ioutpointer = &vos->value.i;}
if( adata != IMEADIATE )
{
if( adata == VARDATA) vos->type = atype;
if( adata == ATOMDATA ){
if( atype == INTEGER){atype = FLOAT;}
}
if( atype == FLOAT && foutpointer != NULL) *foutpointer = fa;
if( atype == INTEGER && ioutpointer != NULL) *ioutpointer = ia;
}
if( echo && atype == INTEGER ) fprintf(op,"%d \n",ia);
if( echo && atype == FLOAT ) fprintf(op,"%f \n",fa);
return( 1);
#ifdef ESV
#undef tokens
#endif
}
/* int validatom()
* given a string return 0 if it is not of the form
* stuff.<x y z fx fy fz dx dy dz vx vy vz q a b m chi jaa>
* these being the valid atomdata parameters
* returns 1 to 16 for x y z fx fy fz dx dy dz vx vy vz q a b m chi jaa
* so we don't have to lex again
*/
int validatom( who )
char *who;
{
char *cp,*pp,*cp1,*cp2,*cp3;
int i;
cp = who;
i = 0;
while ( *cp != '\0')
{
if( *cp == '.') {i++; pp = cp;}
cp++;
}
if( i != 1) return (0 ); /* there can only be one '.' */
cp = pp ; cp++;
/* now we check the tailing characters */
cp1 = cp; cp1++;
cp2 = cp1; cp2++;
cp3 = cp2; cp3++;
if( *cp1 == '\0')
{
if( *cp == 'x') return (1);
if( *cp == 'y') return (2);
if( *cp == 'z') return (3);
if( *cp == 'q') return (13);
if( *cp == 'a') return (14);
if( *cp == 'b') return (15);
if( *cp == 'm') return (16);
return (0);
}
if( *cp2 == '\0')
{
if( *cp == 'f') {
if( *cp1 == 'x') return (4);
if( *cp1 == 'y') return (5);
if( *cp1 == 'z') return (6);
}
if( *cp == 'd' ){
if( *cp1 == 'x') return (7);
if( *cp1 == 'y') return (8);
if( *cp1 == 'z') return (9);
}
if( *cp == 'v' ){
if( *cp1 == 'x') return (10);
if( *cp1 == 'y') return (11);
if( *cp1 == 'z') return (12);
}
}
if( *cp3 == '\0')
{
if( *cp == 'c' && *cp1 == 'h' && *cp2 == 'i') return 17;
if( *cp == 'j' && *cp1 == 'a' && *cp2 == 'a') return 18;
}
return (0);
}
/* float *getatomdata() returns a float * or null for an atomdata
*/
float *getatomdata( who )
char *who ;
{
int validatom();
int i,j;
char aser[TOKENLENGTH],*cp;
ATOM *ap,*a_m_serial();
VARIABLE *vp,*match_variable();
i = validatom( who );
if( i == 0 ) return ( NULL ); /* if not the right format it aint one */
cp = who; j = 0;
while( *cp != '.')
{ aser[j++] = *cp; cp++; }
aser[j] = '\0';
if( (vp = match_variable(aser)) == NULL)
{ j = atoi(aser); } else {
if( vp ->type == INTEGER) j = vp->value.i;
if( vp ->type == FLOAT) j = (int)vp->value.f;
}
ap = a_m_serial(j);
if( ap == NULL) return( NULL );
if( i == 1 ) return ( &ap->x );
if( i == 2 ) return ( &ap->y );
if( i == 3 ) return ( &ap->z );
if( i == 4 ) return ( &ap->fx );
if( i == 5 ) return ( &ap->fy );
if( i == 6 ) return ( &ap->fz );
if( i == 7 ) return ( &ap->dx );
if( i == 8 ) return ( &ap->dy );
if( i == 9 ) return ( &ap->dz );
if( i == 10 ) return ( &ap->vx );
if( i == 11 ) return ( &ap->vy );
if( i == 12 ) return ( &ap->vz );
if( i == 13 ) return ( &ap->q );
if( i == 14 ) return ( &ap->a );
if( i == 15 ) return ( &ap->b );
if( i == 16 ) return ( &ap->mass );
if( i == 17 ) return ( &ap->chi );
if( i == 18 ) return ( &ap->jaa );
return (NULL );
}
/* math_match_atom(char* atomname, ATOM * ap)
*
* find if the atomname is part of the atom .name field
* as in atomname = "ca" and ap->name = arg.ca
*
*/
int math_match_atom( who,ap)
char *who;
ATOM *ap;
{
char *cp;
cp = & ap->name[0];
while( *cp != '.' )
{
if( *cp == '\0') return 0;
cp++;
}
cp++;
/* printf(" >%s< >%s<\n",cp,who);
*/
if( strcmp( who,cp ) == 0 )
{ return 1; }
return 0;
}
/*
* math_findlabel(FILE * ip, char *label);
* search a file for label:
* if label doesn't end in : add it
*
*/
math_findlabel( fp,label)
FILE *fp;
char *label;
{
char *cp,*lp;
char llabel[TOKENLENGTH];
char myline[TOKENLENGTH]; /* since label is no longer than TOKENLENGTH we can skip such lines */
int inmyline;
int i;
char ac;
cp = label;
lp = &llabel[0];
while( *cp != '\0')
{
*lp = *cp ; lp++; cp ++;
}
cp = lp; cp--;
if( *cp != ':'){ *lp = ':'; lp++;}
*lp = '\0';
/* now one char at a time scan ip for a blank */
inmyline = 0;
lp = &llabel[0];
while( (i= fgetc( fp )) != EOF )
{
ac = (char)i;
if( !isspace((int) ac) )
{
if( ac == ';')
{
myline[inmyline] = '\0';
/* printf(">%s<\n",&myline[0]);
*/
if( strcmp( lp,&myline[0]) == 0 ) return ;
inmyline = 0;
}else{
if( inmyline > TOKENLENGTH) inmyline = 0;
myline[inmyline++] = ac;
}
}
}
}