d6ce369bb580022d8d979f51ab5e85dccc1c4cbb
[physik/posic.git] / visual / visual.c
1 /*
2  * visual.c - visualization functions
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
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 #include <string.h>
15
16 #include "visual.h"
17 #include "../moldyn.h"
18 #include "../math/math.h"
19
20 /* the pse, only needed here */
21 static char *pse[]={
22         "*",
23         "H",
24         "He",
25         "Li",
26         "Be",
27         "B",
28         "C",
29         "N",
30         "O",
31         "F",
32         "Ne",
33         "Na",
34         "Mg",
35         "Al",
36         "Si",
37         "P",
38         "S",
39         "Cl",
40         "Ar",
41 };
42
43 int visual_init(t_visual *v,char *filebase) {
44
45         char file[128+8];
46
47         strncpy(v->fb,filebase,128);
48         memset(file,0,128+8);
49         sprintf(file,"%s/visualize.scr",v->fb);
50
51         v->fd=open(file,O_WRONLY|O_CREAT|O_EXCL,S_IRUSR|S_IWUSR);
52         if(v->fd<0) {
53                 perror("open visual fd");
54                 return -1;
55         }
56         dprintf(v->fd,"set write on\n");
57
58         return 0;
59 }
60
61 int visual_tini(t_visual *v) {
62
63         if(v->fd) {
64                 dprintf(v->fd,"quit\n");
65                 close(v->fd);
66         }
67
68         return 0;
69 }
70
71 int visual_atoms(t_visual *v,double time,t_atom *atom,int n) {
72
73         int i,fd;
74         char file[128+64];
75         t_3dvec dim;
76
77         dim.x=v->dim.x;
78         dim.y=v->dim.y;
79         dim.z=v->dim.z;
80
81         sprintf(file,"%s/visualize_%07.f.xyz",v->fb,time);
82         fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
83         if(fd<0) {
84                 perror("open visual save file fd");
85                 return -1;
86         }
87
88         /* script file update */
89         dprintf(v->fd,"load xyz %s\n",file);
90         dprintf(v->fd,"spacefill\n");
91         //dprintf(v->fd,"spacefill 100\n");
92         dprintf(v->fd,"rotate x 100\n");
93         dprintf(v->fd,"rotate y 10\n");
94         dprintf(v->fd,"set ambient 20\n");
95         dprintf(v->fd,"set specular on\n");
96         dprintf(v->fd,"set boundbox on\n");
97         //dprintf(v->fd,"label\n");
98         sprintf(file,"%s/visualize_%07.f.ppm",v->fb,time);
99         dprintf(v->fd,"write ppm %s\n",file);
100         dprintf(v->fd,"zap\n");
101
102         /* write the actual data file */
103         dprintf(fd,"%d\n",(dim.x==0)?n:n+8);
104         dprintf(fd,"atoms at time %07.f\n",time);
105         for(i=0;i<n;i++)
106                 dprintf(fd,"%s %f %f %f\n",pse[atom[i].element],
107                                            atom[i].r.x,
108                                            atom[i].r.y,
109                                            atom[i].r.z);
110         if(dim.x) {
111                 dprintf(fd,"%s %f %f %f\n",pse[0],dim.x/2,dim.y/2,dim.z/2);
112                 dprintf(fd,"%s %f %f %f\n",pse[0],-dim.x/2,dim.y/2,dim.z/2);
113                 dprintf(fd,"%s %f %f %f\n",pse[0],dim.x/2,-dim.y/2,dim.z/2);
114                 dprintf(fd,"%s %f %f %f\n",pse[0],dim.x/2,dim.y/2,-dim.z/2);
115                 dprintf(fd,"%s %f %f %f\n",pse[0],-dim.x/2,-dim.y/2,dim.z/2);
116                 dprintf(fd,"%s %f %f %f\n",pse[0],dim.x/2,-dim.y/2,-dim.z/2);
117                 dprintf(fd,"%s %f %f %f\n",pse[0],-dim.x/2,dim.y/2,-dim.z/2);
118                 dprintf(fd,"%s %f %f %f\n",pse[0],-dim.x/2,-dim.y/2,-dim.z/2);
119         }
120         close(fd);
121
122         return 0;
123 }
124