--- /dev/null
+/*
+ * atom_match.c - match atoms and process
+ *
+ * author: frank.zirkelbach@physik.uni-augsburg.de
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+
+#include "moldyn.h"
+
+#define PSE_NAME
+#define PSE_COL
+#include "pse.h"
+#undef PSE_NAME
+#undef PSE_COL
+
+#define ME "[atom match]"
+
+#define LAND 0
+#define LOR 1
+
+#define SELECT 1
+#define UNSELECT 0
+
+#define S_COL_NO_OVERRIDE 0
+#define S_COL_NONE 1
+#define S_COL_BLUE 2
+#define S_COL_RED 3
+#define S_COL_BLACK 4
+
+/* rule types */
+#define RT_ELEMENT 0
+
+typedef int t_element;
+
+typedef struct s_rule {
+ u8 type;
+ u8 logic_op;
+ void *params;
+} t_rule;
+
+typedef struct s_am {
+ int count;
+ t_rule rule[32];
+ char infile[128];
+} t_am;
+
+int parse_rule(t_am *am,char *line) {
+
+ int wcnt;
+ char *wptr;
+ char word[16][64];
+
+ t_element *element;
+
+ wcnt=0;
+ while(1) {
+ if(wcnt)
+ wptr=strtok(NULL," ");
+ else
+ wptr=strtok(line," ");
+ if(wptr==NULL)
+ break;
+ strncpy(word[wcnt],wptr,64);
+ wcnt+=1;
+ }
+
+ switch(word[0][0]) {
+ case 'e':
+ am->rule[am->count].params=malloc(sizeof(t_element));
+ element=am->rule[am->count].params;
+ if(element==NULL) {
+ printf("%s malloc (element).\n",ME);
+ return -1;
+ }
+ *element=atoi(word[1]);
+ break;
+ /*
+ case '':
+ break;
+ case '':
+ break;
+ */
+ default:
+ return -1;
+ }
+
+ return 0;
+}
+
+int parse_argv(int argc,char **argv,t_am *am) {
+
+ int i;
+ int ret;
+
+ memset(am,0,sizeof(t_am));
+ ret=0;
+
+ for(i=1;i<argc;i++) {
+ if(argv[i][0]=='-') {
+ switch(argv[i][1]) {
+ case 'i':
+ // infile
+ strncpy(am->infile,argv[++i],128);
+ break;
+ case 'a':
+ // and rule
+ am->rule[am->count].logic_op=LAND;
+ ret=parse_rule(am,argv[++i]);
+ am->count+=1;
+ break;
+ case 'o':
+ // or rule
+ am->rule[am->count].logic_op=LOR;
+ ret=parse_rule(am,argv[++i]);
+ am->count+=1;
+ break;
+ case 'p':
+ // how to process data
+ break;
+ default:
+ printf("%s unknown switch: %s\n",
+ ME,argv[i]);
+ return -1;
+
+ }
+ }
+ else {
+ printf("%s unknown argument: %s\n",ME,argv[i]);
+ return -1;
+ }
+ }
+
+ return ret;
+}
+
+int main(int argc,char **argv) {
+
+ t_am am;
+ t_moldyn moldyn;
+ u8 *sel_atom;
+ u8 *sel_color;
+ int i,j,acnt;
+ t_atom *atom;
+
+ t_element *e;
+
+ memset(&moldyn,0,sizeof(t_moldyn));
+
+ if(parse_argv(argc,argv,&am)<0) {
+ printf("%s aborted (bad args).\n",ME);
+ return -1;
+ }
+
+ if(moldyn_read_save_file(&moldyn,am.infile)<0) {
+ printf("%s aborted (bad infile).\n",ME);
+ return -1;
+ }
+
+ acnt=moldyn.count;
+ atom=moldyn.atom;
+
+ link_cell_init(&moldyn,VERBOSE);
+
+ /* alloc select status and color memory */
+ sel_atom=malloc(acnt*sizeof(u8));
+ if(sel_atom==NULL) {
+ printf("%s aborted (malloc failed).\n",ME);
+ return -1;
+ }
+ sel_color=malloc(acnt*sizeof(u8));
+ if(sel_color==NULL) {
+ printf("%s aborted (malloc failed).\n",ME);
+ return -1;
+ }
+
+ /* apply rules */
+ for(i=0;i<am.count;i++) {
+
+ /* initialize status and color in first run */
+ if(i==0) {
+ if(am.rule[0].logic_op==LAND)
+ memset(sel_atom,SELECT,acnt*sizeof(u8));
+ else
+ memset(sel_atom,UNSELECT,acnt*sizeof(u8));
+ }
+
+ /* rules */
+ switch(am.rule[i].type) {
+ case RT_ELEMENT:
+ e=am.rule[0].params;
+ if(am.rule[i].type==LAND)
+ for(j=0;j<acnt;j++)
+ if(atom[j].element==*e)
+ sel_atom[j]&=SELECT;
+ else
+ sel_atom[j]&=UNSELECT;
+ else
+ for(j=0;j<acnt;j++)
+ if(atom[j].element==*e)
+ sel_atom[j]|=SELECT;
+ break;
+ default:
+ printf("%s unknown rule %c -> skipped.\n",
+ ME,am.rule[i].type);
+ break;
+ }
+ }
+
+ /* process data */
+ for(i=0;i<acnt;i++)
+ if(sel_atom[i]==SELECT)
+ printf("%s %f %f %f %s %f\n",
+ pse_name[atom[i].element],
+ atom[i].r.x,atom[i].r.y,atom[i].r.z,
+ pse_col[atom[i].element],
+ atom[i].ekin);
+
+ /* exit and cleanup */
+ free(sel_atom);
+ free(sel_color);
+ link_cell_shutdown(&moldyn);
+ moldyn_free_save_file(&moldyn);
+
+ return 0;
+}