]> www.hackdaworld.org Git - physik/posic.git/blob - atom_match.c
just to have this stored somewhere ...
[physik/posic.git] / atom_match.c
1 /*
2  * atom_match.c - match atoms and process
3  *
4  * author: frank.zirkelbach@physik.uni-augsburg.de
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10
11 #include "moldyn.h"
12
13 #define PSE_NAME
14 #define PSE_COL
15 #include "pse.h"
16 #undef PSE_NAME
17 #undef PSE_COL
18
19 #define ME      "[atom match]"
20
21 #define LAND    0
22 #define LOR     1
23
24 #define SELECT          1
25 #define UNSELECT        0
26
27 #define S_COL_NO_OVERRIDE       0
28 #define S_COL_NONE              1
29 #define S_COL_BLUE              2
30 #define S_COL_RED               3
31 #define S_COL_BLACK             4
32
33 /* rule types */
34 #define RT_ELEMENT              0
35
36 typedef int t_element;
37
38 typedef struct s_rule {
39         u8 type;
40         u8 logic_op;
41         void *params;
42 } t_rule;
43
44 typedef struct s_am {
45         int count;
46         t_rule rule[32];
47         char infile[128];
48 } t_am;
49
50 int parse_rule(t_am *am,char *line) {
51
52         int wcnt;
53         char *wptr;
54         char word[16][64];
55
56         t_element *element;
57
58         wcnt=0;
59         while(1) {
60                 if(wcnt)
61                         wptr=strtok(NULL," ");
62                 else
63                         wptr=strtok(line," ");
64                 if(wptr==NULL)
65                         break;
66                 strncpy(word[wcnt],wptr,64);
67                 wcnt+=1;
68         }
69
70         switch(word[0][0]) {
71                 case 'e':
72                         am->rule[am->count].params=malloc(sizeof(t_element));
73                         element=am->rule[am->count].params;
74                         if(element==NULL) {
75                                 printf("%s malloc (element).\n",ME);
76                                 return -1;
77                         }
78                         *element=atoi(word[1]);
79                         break;
80                 /*
81                 case '':
82                         break;
83                 case '':
84                         break;
85                 */
86                 default:
87                         return -1;
88         }
89
90         return 0;
91 }
92
93 int parse_argv(int argc,char **argv,t_am *am) {
94
95         int i;
96         int ret;
97
98         memset(am,0,sizeof(t_am));
99         ret=0;
100
101         for(i=1;i<argc;i++) {
102                 if(argv[i][0]=='-') {
103                         switch(argv[i][1]) {
104                                 case 'i':
105                                         // infile
106                                         strncpy(am->infile,argv[++i],128);
107                                         break;
108                                 case 'a':
109                                         // and rule
110                                         am->rule[am->count].logic_op=LAND;
111                                         ret=parse_rule(am,argv[++i]);
112                                         am->count+=1;
113                                         break;
114                                 case 'o':
115                                         // or rule
116                                         am->rule[am->count].logic_op=LOR;
117                                         ret=parse_rule(am,argv[++i]);
118                                         am->count+=1;
119                                         break;
120                                 case 'p':
121                                         // how to process data
122                                         break;
123                                 default:
124                                         printf("%s unknown switch: %s\n",
125                                                ME,argv[i]);
126                                         return -1;
127                         
128                         }
129                 }
130                 else {
131                         printf("%s unknown argument: %s\n",ME,argv[i]);
132                         return -1;
133                 }
134         }
135
136         return ret;
137 }
138
139 int main(int argc,char **argv) {
140
141         t_am am;
142         t_moldyn moldyn;
143         u8 *sel_atom;
144         u8 *sel_color;
145         int i,j,acnt;
146         t_atom *atom;
147
148         t_element *e;
149
150         memset(&moldyn,0,sizeof(t_moldyn));
151
152         if(parse_argv(argc,argv,&am)<0) {
153                 printf("%s aborted (bad args).\n",ME);
154                 return -1;
155         }
156
157         if(moldyn_read_save_file(&moldyn,am.infile)<0) {
158                 printf("%s aborted (bad infile).\n",ME);
159                 return -1;
160         }
161
162         acnt=moldyn.count;
163         atom=moldyn.atom;
164
165         link_cell_init(&moldyn,VERBOSE);
166
167         /* alloc select status and color memory */
168         sel_atom=malloc(acnt*sizeof(u8));
169         if(sel_atom==NULL) {
170                 printf("%s aborted (malloc failed).\n",ME);
171                 return -1;
172         }
173         sel_color=malloc(acnt*sizeof(u8));
174         if(sel_color==NULL) {
175                 printf("%s aborted (malloc failed).\n",ME);
176                 return -1;
177         }
178
179         /* apply rules */
180         for(i=0;i<am.count;i++) {
181
182                 /* initialize status and color in first run */
183                 if(i==0) {
184                         if(am.rule[0].logic_op==LAND)
185                                 memset(sel_atom,SELECT,acnt*sizeof(u8));
186                         else
187                                 memset(sel_atom,UNSELECT,acnt*sizeof(u8));
188                 }
189
190                 /* rules */
191                 switch(am.rule[i].type) {
192                         case RT_ELEMENT:
193                                 e=am.rule[0].params;
194                                 if(am.rule[i].type==LAND)
195                                         for(j=0;j<acnt;j++)
196                                                 if(atom[j].element==*e)
197                                                         sel_atom[j]&=SELECT;
198                                                 else
199                                                         sel_atom[j]&=UNSELECT;
200                                 else
201                                         for(j=0;j<acnt;j++)
202                                                 if(atom[j].element==*e)
203                                                         sel_atom[j]|=SELECT;
204                                 break;
205                         default:
206                                 printf("%s unknown rule %c -> skipped.\n",
207                                        ME,am.rule[i].type);
208                                 break;
209                 }
210         }
211
212         /* process data */
213         for(i=0;i<acnt;i++)
214                 if(sel_atom[i]==SELECT)
215                         printf("%s %f %f %f %s %f\n",
216                                pse_name[atom[i].element],
217                                atom[i].r.x,atom[i].r.y,atom[i].r.z,
218                                pse_col[atom[i].element],
219                                atom[i].ekin);
220
221         /* exit and cleanup */
222         free(sel_atom);
223         free(sel_color);
224         link_cell_shutdown(&moldyn);
225         moldyn_free_save_file(&moldyn);
226         
227         return 0;
228 }