From bb6da8568bd94316d57e211df75f357e5250457d Mon Sep 17 00:00:00 2001 From: hackbard Date: Thu, 23 Oct 2003 13:49:15 +0000 Subject: [PATCH] added computational physic directory, added g_plot api + first newton.c prog --- .cvsignore | 3 +++ Makefile | 18 ++++++++++++++++ g_plot.c | 41 ++++++++++++++++++++++++++++++++++++ g_plot.h | 17 +++++++++++++++ newton.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 .cvsignore create mode 100644 Makefile create mode 100644 g_plot.c create mode 100644 g_plot.h create mode 100644 newton.c 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