added computational physic directory, added g_plot api + first newton.c prog
authorhackbard <hackbard>
Thu, 23 Oct 2003 13:49:15 +0000 (13:49 +0000)
committerhackbard <hackbard>
Thu, 23 Oct 2003 13:49:15 +0000 (13:49 +0000)
.cvsignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
g_plot.c [new file with mode: 0644]
g_plot.h [new file with mode: 0644]
newton.c [new file with mode: 0644]

diff --git a/.cvsignore b/.cvsignore
new file mode 100644 (file)
index 0000000..7c417c0
--- /dev/null
@@ -0,0 +1,3 @@
+*.o
+newton
+*.plot
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..c65ab86
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,18 @@
+# computational physics Makefile, rules for all example programs
+
+INCLUDEDIR = /usr/include
+CFLAGS = -O3 -Wall
+LIBS = -L/usr/lib -lm
+
+API = g_plot.o
+OBJS = newton
+
+all: $(OBJS)
+
+newton: $(API)
+       $(CC) $(CFLAGS) -o $@ $(API) $(LIBS) newton.c
+
+clean:
+       rm $(API) $(OBJS)
+
+remake: clean all
diff --git a/g_plot.c b/g_plot.c
new file mode 100644 (file)
index 0000000..d6ee6f8
--- /dev/null
+++ b/g_plot.c
@@ -0,0 +1,41 @@
+/*
+ * g_plot.c - api for creating files viewable by gnuplot
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "g_plot.h"
+
+int gp_init(char *f_name) {
+ int fd;
+ if((fd=open(f_name,O_WRONLY|O_CREAT))<0)
+  printf("failed to open file: %s\n",f_name);
+ return fd;
+}
+
+int gp_add_data(int fd,void *data,int x_c,int y_c,int type) {
+ int x,y;
+ /* we need pointers */
+ for(y=0;y<y_c;y++) {
+  for(x=0;x<x_c;x++) {
+   if(type==TYPE_INT)
+    dprintf(fd,"%d ",*(((int *)data)+y*x_c+x));
+   if(type==TYPE_DOUBLE)
+    dprintf(fd,"%f ",*(((double *)data)+y*x_c+x));
+  }
+  dprintf(fd,"\n");
+ }
+ return 1;
+}
+
+int gp_close(int fd) {
+ return(close(fd));
+}
diff --git a/g_plot.h b/g_plot.h
new file mode 100644 (file)
index 0000000..2c84f00
--- /dev/null
+++ b/g_plot.h
@@ -0,0 +1,17 @@
+/*
+ * g_plot.h - defines, prototypes
+ *
+ * author: hackbard@hackdaworld.dyndns.org
+ *
+ */
+
+#define MAX_FILE_OPENS 32
+#define MAX_CHAR_FNAME 64
+
+#define TYPE_INT 0
+#define TYPE_DOUBLE 1
+
+int gp_init(char *f_name);
+int gp_add_data(int fd,void *data,int x_c,int y_c,int type);
+int gp_close(int fd);
+
diff --git a/newton.c b/newton.c
new file mode 100644 (file)
index 0000000..dbb2d34
--- /dev/null
+++ b/newton.c
@@ -0,0 +1,61 @@
+/*
+ * newton'sche bewegungsgleichung
+ *
+ *
+ * usage: ./newton <steps> <alpha> <x_p> <v_p>
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
+#include "g_plot.h"
+
+int main(int argc,char **argv) {
+ double x,x_p,v,v_p;
+ double alpha,force,tau;
+ int i,j,steps;
+ int fd; /* data file */
+ double *buf;
+ char filename[32];
+
+ if(argc!=5) {
+  printf("usage: %s <steps> <alpha> <x_0> <v_0>\n",argv[0]);
+  return -1;
+ }
+
+ /* init + starting conditions */
+ steps=atoi(argv[1]);
+ alpha=atof(argv[2]);
+ x_p=atof(argv[3]);
+ v_p=atof(argv[4]);
+ tau=2*M_PI/steps;
+ sprintf(filename,"newton_%d_%f_%f_%f.plot",steps,alpha,x_p,v_p);
+ fd=gp_init(filename);
+
+ /* allocate memory for data buffer */
+ if((buf=(double *)malloc(3*steps*sizeof(double)))==NULL) {
+  puts("malloc failed!");
+  return -1;
+ }
+ buf[0]=0; buf[1]=x_p; buf[2]=v_p;
+
+ /* loop */
+ for(i=0;i<steps-1;i++) {
+  force=-x_p-alpha*v_p;
+  x=x_p+v_p*tau;
+  v=v_p+force*tau; /* masse = 1 */
+  /* save */
+  j=3*i;
+  buf[3+j]=i*tau; buf[4+j]=x; buf[5+j]=v;
+  /* switch */
+  x_p=x; v_p=v;
+ }
+
+ /* write to file */
+ gp_add_data(fd,buf,3,steps,TYPE_DOUBLE);
+ gp_close(fd);
+
+ return 1;
+}