added runge kutta example
[physik/computational_physics.git] / g_plot.c
1 /*
2  * g_plot.c - api for creating files viewable by gnuplot
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14
15 #include "g_plot.h"
16
17 int gp_init(char *f_name) {
18  int fd;
19  if((fd=open(f_name,O_WRONLY|O_CREAT))<0)
20   printf("failed to open file: %s\n",f_name);
21  return fd;
22 }
23
24 int gp_add_data(int fd,void *data,int x_c,int y_c,int type) {
25  int x,y;
26  /* we need pointers */
27  for(y=0;y<y_c;y++) {
28   for(x=0;x<x_c;x++) {
29    if(type==TYPE_INT)
30     dprintf(fd,"%d ",*(((int *)data)+y*x_c+x));
31    if(type==TYPE_DOUBLE)
32     dprintf(fd,"%f ",*(((double *)data)+y*x_c+x));
33   }
34   dprintf(fd,"\n");
35  }
36  return 1;
37 }
38
39 int gp_close(int fd) {
40  return(close(fd));
41 }