From: hackbard Date: Thu, 23 Oct 2003 13:49:15 +0000 (+0000) Subject: added computational physic directory, added g_plot api + first newton.c prog X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=bb6da8568bd94316d57e211df75f357e5250457d;p=physik%2Fcomputational_physics.git added computational physic directory, added g_plot api + first newton.c prog --- bb6da8568bd94316d57e211df75f357e5250457d diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 0000000..7c417c0 --- /dev/null +++ b/.cvsignore @@ -0,0 +1,3 @@ +*.o +newton +*.plot diff --git a/Makefile b/Makefile new file mode 100644 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 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 +#include +#include +#include +#include + +#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 + * + */ + +#include +#include +#include +#include +#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 \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