C Language

I learned to program C in 1999 when I worked for Richard Crandall.
Here is the wp page for the language.

Here is the beginning of headtail.c

#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit() */
#include <math.h> /* math lib */

#define PI 3.1415926535897932
/*#define ALPHA 2000.*/
#define ALPHA 200.
#define WB (18.19*860000.)
#define WB2 (WB*WB)
#define WBINV 1/WB
#define TS 2*PI/(0.044*860000)

#define N 0.96*6.4E11
#define R0 2.818E-15
#define W0 580000.
#define C 3.E8
#define GAMMA 14.5/0.000511
#define CIR 2200.
#define MATHEMATICA_OUTPUT 0

#define W N*R0*W0*C*C/(2*GAMMA*CIR)
/*#define W 0.*/

#define UPSILON W*TS/(4*WB) /* should be 1.92 */

#define DT 5.E-10
/*#define TF .03*/

#define CW cos(WB*DT/2)
#define SW sin(WB*DT/2)

#define TF 8E-3

#define OUTPUTNUM 2500/*0000*/

double iteratefront(double x0,double v0,double *x,double *v);
void iterateback(double x0,double v0,double x1,double *x,double *v);
void move_unperturbed(double x0,double v0,double *x,double *v);

int main(int argc, char **argv)
{
double x1,x2,v1,v2,a1,a2,xb; /* x1's in the front, x2's in the back */
double t = 0,ts = 0,temp;
int out_inc=0;
/*
printf("UPSILON = %f\n",UPSILON);
exit(0);
*/
x1 = atof(argv[1]);
v1 = atof(argv[2]);
x2 = atof(argv[3]);
v2 = atof(argv[4]);
if(MATHEMATICA_OUTPUT)
printf("{%f,\n",(x1+x2)/2);
else
printf("%f\n",(x1+x2)/2);
while(t<TF)
{
if(out_inc == OUTPUTNUM)
{
if(MATHEMATICA_OUTPUT)
printf("%f,\n",(x1+x2)/2);
else
printf("%f\n",(x1+x2)/2);
out_inc = 0;
}
else
out_inc++;

xb= iteratefront(x1,v1,&x1,&v1);
iterateback(x2,v2,xb,&x2,&v2);

/*
move_unperturbed(x1,v1,&x1,&v1);
move_unperturbed(x2,v2,&x2,&v2);
*/
/*
printf("x=%f,v=%f\n",x1,v1);
*/
t+=DT;
ts+=DT;
if(ts>=TS/2){
temp = x2;
x2 = x1;
x1 = temp;
temp = v2;
v2 = v1;
v1 = temp;
ts = 0;
};
};
if(MATHEMATICA_OUTPUT)
printf("%f}\n",(x1+x2)/2);
else
printf("%f\n",(x1+x2)/2);
/*printf("%f}",x1);*/
return(0);
}

double iteratefront(double x0,double v0,double *x,double *v)
{
double xa,va,xb,vb,xn,vn;

move_unperturbed(x0,v0,&xa,&va);
xb = xa;
vb = va - DT*ALPHA*va; /* + DT*W*x1 */
/* printf("v = %f dv = %f\n",vb, DT*ALPHA*xa);*/
move_unperturbed(xb,vb,&xn,&vn);
*x = xn;
*v = vn;
return(xb);
}

void iterateback(double x0,double v0,double x1,double *x,double *v)
{
double xa,va,xb,vb,xn,vn;

move_unperturbed(x0,v0,&xa,&va);
xb = xa;
vb = va - DT*ALPHA*va + DT*W*x1;
move_unperturbed(xb,vb,&xn,&vn);

*x = xn;
*v = vn;
return;
}

void move_unperturbed(double x0,double v0,double *x,double *v)
{
*x = CW*x0 + SW*v0*WBINV;
*v = -SW*x0*WB + CW*v0;
return;
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License