reimplemented the ratio amount
[physik/nlsop.git] / nlsop_client.c
index 403bfe0..770ecb9 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <signal.h>
+
 #include "nlsop.h"
 #include "dfbapi.h"
 #include "random.h"
 
-#include "../api/network.h"
-#include "../api/event.h"
-#include "../api/list.h"
+#include "network.h"
+#include "event.h"
+
+#include "nlsop_general.h"
 
 #define MAKE_AMORPH(N) *(N)|=AMORPH
 #define MAKE_CRYST(N) *(N)&=~AMORPH
 
+/* globals */
+
+char p_file[MAX_CHARS];
+char n_e_file[MAX_CHARS];
+char r_file[MAX_CHARS];
+char s_file[MAX_CHARS];
+int start_fd;
+t_net *gnet;
+d3_lattice *gd3_l;
+info *gmy_info;
+int *gi;
+unsigned char dc;
+unsigned char shut_down;
+double c_ratio;
+
+int get_data_and_calc(t_event *event,void *allineed);
+int nop(t_event *event,void *allineed);
+
 int usage(char *prog)
 {
  puts("usage:");
- printf("%s <server ip>\n",prog);
+ printf("%s -i ip -p port -r/P/n/s random/profile/neloss/start file\n",prog);
  return 1;
 }
 
@@ -84,6 +105,47 @@ int sputter(d3_lattice *d3_l)
  return 1;
 }
 
+int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
+{
+ double all,a,b,d;
+ int i,k;
+ int p_fd;
+ unsigned char buf[32];
+ char *p;
+ unsigned char c;
+
+ if((p_fd=open(pfile,O_RDONLY))<0)
+ {
+  puts("cannot open profile file");
+  return -1;
+ }
+ k=1;
+ d=0;
+ all=0;
+ while(k)
+ {
+  for(i=0;i<32;i++)
+  {
+   k=read(p_fd,&c,1);
+   buf[i]=c;
+   if(c=='\n') break;
+  }
+  if(k)
+  {
+   p=strtok(buf," ");
+   a=atof(p)/10; /* nm */
+   p=strtok(NULL," ");
+   b=atof(p);
+   if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
+   all+=b;
+  }
+ }
+ *c_ratio=d/all;
+ close(p_fd);
+
+ return 1;
+}
+
 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
 {
  unsigned char *thiz;
@@ -94,7 +156,8 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
 
  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
- p=my_info->b*nel_z;
+ //p=my_info->b*nel_z; // energieuebertrag prop zu nukl. bk
+ p=my_info->b*URAND_MAX; // konstanter energieuebertrag
  for(i=-(my_info->range);i<=my_info->range;i++)
  {
   for(j=-(my_info->range);j<=my_info->range;j++)
@@ -107,24 +170,28 @@ int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
   }
  }
  p+=*conc*my_info->c*URAND_MAX;
- if(!(*thiz&AMORPH))
- {
-  if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
- } else
- {
-  /* assume 1-p probability */
-  /* also look for neighbours ! */
-  q=(URAND_MAX-p)>0?URAND_MAX-p:0;
-  j=0;
-  j+=(*(d3_l->status+((x+d3_l->max_x+1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-  j+=(*(d3_l->status+((x+d3_l->max_x-1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-  j+=(*(d3_l->status+x+((y+1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-  j+=(*(d3_l->status+x+((y-1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-  j+=(*(d3_l->status+x+y*d3_l->max_x+((z+1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-  j+=(*(d3_l->status+x+y*d3_l->max_x+((z-1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
-
-  p+=((q/6)*j);
-  if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
+ if(p>=URAND_MAX) MAKE_AMORPH(thiz);
+ else {
+  if(!(*thiz&AMORPH)) {
+   if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
+  }
+  else {
+   /* assume 1-p probability */
+   /* also look for neighbours ! */
+   q=(URAND_MAX-p)>0?URAND_MAX-p:0;
+   j=0;
+   j+=(*(d3_l->status+((x+d3_l->max_x+1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   j+=(*(d3_l->status+((x+d3_l->max_x-1)%d3_l->max_x)+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   j+=(*(d3_l->status+x+((y+1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   j+=(*(d3_l->status+x+((y-1+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   j+=(*(d3_l->status+x+y*d3_l->max_x+((z+1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   j+=(*(d3_l->status+x+y*d3_l->max_x+((z-1+d3_l->max_z)%d3_l->max_z)*d3_l->max_x*d3_l->max_y)&AMORPH)?1:0;
+   p+=((q/6)*j);
+   if(p<=URAND_MAX) {
+    if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
+   }
+  }
  }
  
  return 1;
@@ -138,11 +205,13 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
  int carry;
 
  /* put one c ion somewhere in the lattice */
- x=get_rand(d3_l->max_x);
- y=get_rand(d3_l->max_y);
- z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
- *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
- (my_info->cc)++;
+ if(c_ratio*step<my_info->cc) {
+   x=get_rand(d3_l->max_x);
+   y=get_rand(d3_l->max_y);
+   z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
+   *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
+   (my_info->cc)++;
+ }
 
  if(step%my_info->diff_rate==0)
  {
@@ -185,31 +254,29 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
        }
       }
      }
-     if(my_info->z_diff)
+     /* diff in z direction */
+     if(k!=0)
      {
-      if(k!=0)
+      off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
+      carry=0;
+      if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
+      if(carry!=0)
       {
-       off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
-       carry=0;
-       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
-       if(carry!=0)
-       {
-        *(d3_l->extra+off)-=carry;
-        *(d3_l->extra+offset)+=carry;
-       }
+       *(d3_l->extra+off)-=carry;
+       *(d3_l->extra+offset)+=carry;
       }
-      if(k!=d3_l->max_z-1)
+     }
+     if(k!=d3_l->max_z-1)
+     {
+      off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
+      carry=0;
+      if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
+      if(carry!=0)
       {
-       off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
-       carry=0;
-       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
-       if(carry!=0)
-       {
-        *(d3_l->extra+off)-=carry;
-        *(d3_l->extra+offset)+=carry;
-       }
+       *(d3_l->extra+off)-=carry;
+       *(d3_l->extra+offset)+=carry;
       }
-     }  
+     }
     }
    } /* for z */
   } /* for y */
@@ -220,119 +287,6 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
  return 1;
 }
 
-/* save to file --> send to server :)  --> T O D O <-- */
-int send_data( ) {
-
-  return 1;
-}
-
-int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
-{
- int sf_fd,c;
-
- if((sf_fd=open(sf,O_WRONLY|O_CREAT))<0)
- {
-  puts("cannot open save file");
-  return -1;
- }
- if(write(sf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
- {
-  puts("failed saving d3 lattice struct");
-  return -1;
- }
- if(write(sf_fd,my_inf,sizeof(info))<sizeof(info))
- {
-  puts("failed saving info struct");
-  return-1;
- }
- c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
- if(write(sf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
- {
-  puts("failed saving status of d3 lattice sites");
-  return -1;
- }
- if(write(sf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
- {
-  puts("failed saving sites concentration");
-  return -1;
- }
- close(sf_fd);
-
- return 1;
-}
-
-/* load from file --> receive from server :) --> T O D O <-- */
-int receive_data(  ) {
-
-  return 1;
-}
-
-int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
-{
-
- int lf_fd,c,pos,end,data,data_len,strip;
-
- if((lf_fd=open(lf,O_RDONLY))<0)
- {
-  puts("cannot open load file");
-  return -1;
- }
- if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
- {
-  puts("failed reading d3 lattice struct");
-  return -1;
- }
- pos=lseek(lf_fd,0,SEEK_CUR);
- printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
- data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
- data_len=data*(sizeof(int)+sizeof(unsigned char));
- printf("there are %d volumes so we need %d of bytes\n",data,data_len);
- end=lseek(lf_fd,0,SEEK_END);
- c=end-pos-data_len;
- printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
- strip=sizeof(info)-c;
- printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
- lseek(lf_fd,pos,SEEK_SET);
- c=sizeof(info);
- if(strip>0) c-=strip;
- if(c<0)
- {
-  puts("info smaller then strip size");
-  return -1;
- }
- if(read(lf_fd,my_inf,c)<c)
- {
-  puts("failed reading info struct");
-  return-1;
- }
- if(strip>0) memset(my_inf+c,0,strip);
- if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
- c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
- if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
- {
-  puts("cannot allocate status buffer");
-  return -1;
- }
- if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
- {
-  puts("cannot allocate concentration buffer");
-  return -1;
- }
- if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
- {
-  puts("failed reading status of d3 lattice sites");
-  return -1;
- }
- if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
- {
-  puts("failed reading sites concentration");
-  return -1;
- }
- close(lf_fd);
-
- return 1;
-}
-
 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
  double a,b;
  int i,j,k;
@@ -400,6 +354,23 @@ u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
  return max;
 }
 
+void send_data(int signum) {
+
+  int c;
+  unsigned char ack=DATA_OK;
+
+  c=gd3_l->max_x*gd3_l->max_y*gd3_l->max_z;
+
+  network_send_chan(gnet,0,&dc,1);
+  network_send_chan(gnet,0,(unsigned char *)gd3_l,sizeof(d3_lattice));
+  network_send_chan(gnet,0,(unsigned char *)gmy_info,sizeof(info));
+  network_send_chan(gnet,0,gd3_l->status,c*sizeof(unsigned char));
+  network_send_chan(gnet,0,(unsigned char *)gd3_l->extra,c*sizeof(int));
+  network_send_chan(gnet,0,(unsigned char *)gi,sizeof(int));
+  network_send_chan(gnet,0,&ack,sizeof(unsigned char));
+
+  if(dc==DC_QUIT) shut_down=1;
+}
 
 
 /*
@@ -409,38 +380,22 @@ u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
 int main(int argc,char **argv)
 {
 
-  u32 x,y,z,x_c,y_c,z_c;
-  int i,j;
-  int resave;
-  int c_step;
   char server_ip[16];
   int port;
-  char p_file[MAX_CHARS];
-  char n_e_file[MAX_CHARS];
-  char r_file[MAX_CHARS];
-  d3_lattice d3_l;
-  info my_info;
-  u32 *c_profile;
-  u32 *n_e_loss;
-  u32 ne_max,ip_max;
-  u32 *nel_z;
-  unsigned char err_dc,dc;
-#define DC_QUIT (1<<0)
-#define DC_OK (1<<1)
-#define DC_END (1<<2)
   t_net net;
+  t_event event;
+  unsigned char data[256];
+  int i;
+
+  gnet=&net;
 
   /* default values */
-  resave=RESAVE;
-  c_step=0;
-  strcpy(server_ip,"");
+  strcpy(server_ip,"137.250.82.105");
   strcpy(p_file,IMP_PROFILE);
   strcpy(n_e_file,NEL_PROFILE);
   strcpy(r_file,"");
-  ne_max=0;
-  ip_max=0;
-  err_dc=0;
-  dc=0;
+  strcpy(s_file,"");
+  start_fd=0;
   port=1025;
 
   /* parse/check argv */
@@ -465,6 +420,9 @@ int main(int argc,char **argv)
         case 'p':
           port=atoi(argv[++i]);
           break;
+        case 's':
+          strcpy(s_file,argv[++i]);
+          break;
         default:
           usage(argv[0]);
           return -1;
@@ -476,18 +434,156 @@ int main(int argc,char **argv)
     return -1;
   }
 
-  /* connect to server and notify about free resource */
+  /* try a file to start from */
+  if(strcmp(s_file,"")) {
+    start_fd=open(s_file,O_RDONLY);
+    if(start_fd>0) printf("using %s as a starting file ...\n",s_file);
+    else printf("errors opening %s!\n",s_file);
+  }
+
+  /* event init */
+  event_init(&event,1);
+  event_set_timeout(&event,0,0);
+
+  /* connect to server */
   network_init(&net,1);
   network_set_connection_info(&net,0,server_ip,port);
-  network_connect(&net,0);
+  if(network_connect(&net,0)==N_E_CONNECT) {
+    printf("unable to connect to server, aborting ...\n");
+    return -1;
+  }
+  network_select(&net,0);
+
+  /* tell server: i am a client, i may work for you */
+  data[0]=NLSOP_CLIENT;
+  network_send(net.connection[0].fd,data,1);
 
   /* wait for job */
+  event_math(net.connection[0].fd,&event,READ,ADD);
+  printf("idle, waiting for jobs ...\n");
+  event_start(&event,NULL,get_data_and_calc,nop);
 
+  network_shutdown(&net);
+  if(start_fd>0) close(start_fd);
+
+  return 1;
+}
+
+int nop(t_event *event,void *allineed) {
+
+  printf("\ni did a nop :)\n");
+
+  return 1;
+}
+
+int get_data_and_calc(t_event *event,void *allineed) {
+
+  d3_lattice d3_l;
+  info my_info;
+  u32 *c_profile;
+  u32 *n_e_loss;
+  u32 ne_max,ip_max;
+  u32 *nel_z;
+  u32 x_c,y_c,z_c;
+  int i,j;
+  int c_step;
+  unsigned char do_sputter;
+  unsigned char data;
+
+  t_net *net;
+
+  c_step=0;
+  ne_max=0;
+  ip_max=0;
+  do_sputter=1;
+  c_ratio=0;
+
+  net=gnet;
+  gd3_l=&d3_l;
+  gmy_info=&my_info;
+  gi=&i;
+  dc=0;
+  shut_down=0;
+
+  printf("got a new job ...\n");
+  
   /* get info (+data) */
+  network_receive(net->connection[0].fd,&data,sizeof(unsigned char));
+  if(data==NLSOP_NJOB || data==NLSOP_CJOB) {
+    network_receive(net->connection[0].fd,(unsigned char *)&d3_l,
+                    sizeof(d3_lattice));
+    network_receive(net->connection[0].fd,(unsigned char *)&my_info,
+                    sizeof(info));
+    c_step=0;
+    j=d3_l.max_x*d3_l.max_y*d3_l.max_z;
+    d3_l.status=(unsigned char *)malloc(j*sizeof(unsigned char));
+    if(d3_l.status==NULL) {
+      printf("status alloc failed\n");
+      return -1;
+    }
+    d3_l.extra=(int *)malloc(j*sizeof(int));
+    if(d3_l.extra==NULL) {
+      printf("extra malloc failed\n");
+      return -1;
+    }
+    if(data==NLSOP_CJOB) {
+      data=DATA_OK;
+      network_receive(net->connection[0].fd,d3_l.status,
+                      j*sizeof(unsigned char));
+      network_send(net->connection[0].fd,&data,sizeof(unsigned char));
+      network_receive(net->connection[0].fd,(unsigned char *)d3_l.extra,
+                      j*sizeof(int));
+      network_send(net->connection[0].fd,&data,sizeof(unsigned char));
+      network_receive(net->connection[0].fd,(unsigned char *)&c_step,
+                      sizeof(int));
+      network_send(net->connection[0].fd,&data,sizeof(unsigned char));
+    }
+    if(c_step==0) {
+      printf("important: clear status/conc data!\n");
+      memset(d3_l.status,0,j*sizeof(unsigned char));
+      memset(d3_l.extra,0,j*sizeof(int));
+    }
+    /* check for file to start from ... */
+    if(start_fd>0) {
+      printf("starting from a save file!\n");
+      unsigned char *nullbuf;
+      nullbuf=(unsigned char *)malloc(sizeof(d3_lattice));
+      if(read(start_fd,nullbuf,sizeof(d3_lattice))!=sizeof(d3_lattice)) {
+        printf("read failed (start file d3l)\n");
+        return -1;
+      }
+      free(nullbuf);
+      nullbuf=(unsigned char *)malloc(sizeof(info));
+      if(read(start_fd,nullbuf,sizeof(info))!=sizeof(info)) {
+        printf("read failed (start file info)\n");
+        return -1;
+      }
+      free(nullbuf);
+      if(read(start_fd,d3_l.status,j*sizeof(unsigned char))!=j*sizeof(unsigned char)) {
+        printf("read failed (start file status)\n");
+        return -1;
+      }
+      if(read(start_fd,d3_l.extra,j*sizeof(int))!=j*sizeof(int)) {
+        printf("read failed (start file extra)\n");
+        return -1;
+      }
+    }
+  }
+  else {
+    printf("unknown instruction, restarting ...\n");
+    return -1;
+  }
+
+  printf("starting simulation with following parameters:\n");
+  printf("b = %f | c = %f | s = %f\n",my_info.b,my_info.c,my_info.s);
+  printf("diff every %d steps | diff rate = %f\n",my_info.diff_rate,
+                                                  my_info.dr_ac);
+  printf("current step: %d | total steps: %d\n",c_step,my_info.steps);
+  printf("...\n");
 
   /* care for signals */
-  err_dc=DC_QUIT;
-  signal(SIGTERM,send_data(&d3_l,&my_info,i,&err_dc));
+  dc=DC_QUIT;
+  signal(SIGTERM,send_data);
 
   /* rand init */
   if(!strcmp(r_file,"")) rand_init(NULL);
@@ -511,13 +607,12 @@ int main(int argc,char **argv)
     printf("failed allocating nel_z array mem\n");
     return -1;
   }
-  for(i=0;i<d3_l-max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
-
+  for(i=0;i<d3_l.max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
 
   /* this should be obsolete - z is high enough - we check now! */
   if(c_profile[d3_l.max_z-1]!=0) {
     printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
-    return -1;
+    do_sputter=0;
   }
 
   /* sputtering really possible ?*/
@@ -525,6 +620,11 @@ int main(int argc,char **argv)
     printf("warning: max_z (%d) too small, there may be amorphous volumes\n",
            d3_l.max_z);
 
+  /* maybe we need a ratio! */
+  if(!do_sputter) {
+    get_c_ratio(&c_ratio,p_file,&my_info,&d3_l);
+  }
+
   /*
    * start of simulation
    */
@@ -534,24 +634,39 @@ int main(int argc,char **argv)
     for(j=0;j<my_info.cpi;j++) {
       x_c=get_rand(d3_l.max_x);
       y_c=get_rand(d3_l.max_y);
-      // z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
-      z_c=get_rand(d3_l.max_z);
+      z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
+      //z_c=get_rand(d3_l.max_z);
       process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z[z_c]);
     }
     distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
-    if(i%resave==0 && i!=0) {
+    i++;
+    if(i%my_info.save_rate==0) {
       dc=DC_OK;
-      send_data(&d3_l,&my_info,i,&dc);
+      send_data(0);
+      dc=DC_QUIT;
+    }
+    if((do_sputter)&(i%my_info.s_rate==0)) sputter(&d3_l);
+    if(shut_down) {
+      free(d3_l.status);
+      free(d3_l.extra);
+      free(c_profile);
+      free(n_e_loss);
+      free(nel_z);
+      event_stop(event); 
     }
-    i++;
-    if(i%my_info.s_rate==0) sputter(&d3_l);
   }
 
   /* finished */
   dc=DC_END;
-  send_data(&d3_l,&my_info,i,&dc);
+  send_data(0);
+  dc=DC_QUIT;
 
   /* shutdown/free/close everything now ... */
+  free(d3_l.status);
+  free(d3_l.extra);
+  free(c_profile);
+  free(n_e_loss);
+  free(nel_z);
 
   return 1;
 }