aded zentral.c
[physik/computational_physics.git] / zentral.c
diff --git a/zentral.c b/zentral.c
new file mode 100644 (file)
index 0000000..cf071cd
--- /dev/null
+++ b/zentral.c
@@ -0,0 +1,70 @@
+/*
+ * zentral.c - teilchen im zentralfeld
+ *
+ * usag: ./zentral <x_0> <y_0> <vx_0> <vy_0> <steps> <alpha>
+ *
+ */
+
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
+#include "g_plot.h"
+
+int main(int argc,char **argv) {
+ double x_p,x,y_p,y,r_3; /* ortsvektor */
+ double vx_p,vx,vy_p,vy; /* geschwindigkeitsvektor */
+ int steps,i,j;
+ double tau;
+ double alpha,f_x,f_y;
+ int fd;
+ char filename[64];
+ double *buf;
+
+ if(argc!=7) {
+  printf("usage: %s <x_0> <y_0> <vx_0> <vy_0> <steps> <alpha>\n",argv[0]);
+  return -1;
+ }
+
+ /* init + starting conditions */
+ x_p=atof(argv[1]); y_p=atof(argv[2]);
+ vx_p=atof(argv[3]); vy_p=atof(argv[4]);
+ steps=atoi(argv[5]); alpha=atof(argv[6]);
+ tau=2*M_PI/steps;
+ sprintf(filename,"zentral_%f_%f_%f_%f_%d_%f.plot",x_p,y_p,vx_p,vy_p,steps,alpha);
+ fd=gp_init(filename);
+
+ /* allocate memory for data buffer */
+ if((buf=(double *)malloc(5*steps*sizeof(double)))==NULL) {
+  puts("malloc failed!");
+  return -1;
+ }
+ buf[0]=0;
+ buf[1]=x_p; buf[2]=y_p;
+ buf[3]=vx_p; buf[4]=vy_p;
+
+ /* loop */
+ for(i=0;i<steps;i++) {
+  r_3=x_p*x_p+y_p*y_p;
+  f_x=-alpha*x_p/r_3; f_y=-alpha*y_p/r_3;
+  x=x_p+vx_p*tau; y=y_p+vy_p*tau;
+  vx=vx_p+f_x*tau; vy=vy_p+f_y*tau;
+  /* save */
+  j=5*i;
+  buf[5+j]=i*tau;
+  buf[6+j]=x; buf[7+j]=y;
+  buf[8+j]=vx; buf[9+j]=vy;
+  /* switch */
+  x_p=x; y_p=y;
+  vx_p=vx; vy_p=vy;
+ }
+
+ /* write to file */
+ gp_add_data(fd,buf,5,steps,TYPE_DOUBLE);
+ gp_close(fd);
+
+ free(buf);
+
+ return 1;
+}