added nlsop_client code
authorhackbard <hackbard>
Thu, 28 Oct 2004 18:53:36 +0000 (18:53 +0000)
committerhackbard <hackbard>
Thu, 28 Oct 2004 18:53:36 +0000 (18:53 +0000)
nlsop_client.c [new file with mode: 0644]

diff --git a/nlsop_client.c b/nlsop_client.c
new file mode 100644 (file)
index 0000000..403bfe0
--- /dev/null
@@ -0,0 +1,557 @@
+/*
+ * client nlsop code
+ *
+ * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
+ *
+ * this program tries helping to understand the amorphous depuration
+ * and recrystallization of SiCx while ion implantation at temperatures
+ * below 400 degree celsius.
+ * hopefully the program will simulate the stabilization of the
+ * selforganizing lamella structure in the observed behaviour.
+ *
+ * refs: 
+ *  - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
+ *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
+ *
+ * Copyright (C) 2004 Frank Zirkelbach
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "nlsop.h"
+#include "dfbapi.h"
+#include "random.h"
+
+#include "../api/network.h"
+#include "../api/event.h"
+#include "../api/list.h"
+
+#define MAKE_AMORPH(N) *(N)|=AMORPH
+#define MAKE_CRYST(N) *(N)&=~AMORPH
+
+int usage(char *prog)
+{
+ puts("usage:");
+ printf("%s <server ip>\n",prog);
+ return 1;
+}
+
+/*
+ * nlsop internal functions
+ */
+
+int sputter(d3_lattice *d3_l)
+{
+ int i,size;
+ int offh,offl;
+
+ size=d3_l->max_x*d3_l->max_y;
+ offl=0;
+ offh=size;
+
+ for(i=0;i<d3_l->max_z-1;i++)
+ {
+  memcpy(d3_l->status+offl,d3_l->status+offh,size);
+  memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
+  offl=offh;
+  offh+=size;
+ }
+ memset(d3_l->status+offl,0,size);
+ memset(d3_l->extra+offl,0,size);
+
+ return 1;
+}
+
+int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
+{
+ unsigned char *thiz;
+ int *conc;
+ int i,j;
+ int off;
+ double p,q;
+
+ 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;
+ for(i=-(my_info->range);i<=my_info->range;i++)
+ {
+  for(j=-(my_info->range);j<=my_info->range;j++)
+  {
+   if(!(i==0 && j==0))
+   {
+    off=((x+d3_l->max_x+i)%d3_l->max_x)+((y+d3_l->max_y+j)%d3_l->max_x)*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
+    if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
+   } 
+  }
+ }
+ 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);
+ }
+ return 1;
+}
+
+int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
+{
+ u32 x,y,z;
+ int i,j,k,c;
+ int offset,off;
+ 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(step%my_info->diff_rate==0)
+ {
+
+ for(i=0;i<d3_l->max_x;i++)
+ {
+  for(j=0;j<d3_l->max_y;j++)
+  {
+   for(k=0;k<d3_l->max_z;k++)
+   {
+    offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
+    /* case amorph: amorph <- cryst diffusion */
+    if(*(d3_l->status+offset)&AMORPH)
+    {
+     for(c=-1;c<=1;c++)
+     {
+      if(c!=0)
+      {
+       off=((i+d3_l->max_x+c)%d3_l->max_x)+j*d3_l->max_x+k*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+offset)+=carry;
+        *(d3_l->extra+off)-=carry;
+       }
+      }
+     }
+     for(c=-1;c<=1;c++)
+     {
+      if(c!=0)
+      {
+       off=i+((j+c+d3_l->max_y)%d3_l->max_y)*d3_l->max_x+k*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+offset)+=carry; 
+        *(d3_l->extra+off)-=carry; 
+       }
+      }
+     }
+     if(my_info->z_diff)
+     {
+      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)
+       {
+        *(d3_l->extra+off)-=carry;
+        *(d3_l->extra+offset)+=carry;
+       }
+      }
+      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)
+       {
+        *(d3_l->extra+off)-=carry;
+        *(d3_l->extra+offset)+=carry;
+       }
+      }
+     }  
+    }
+   } /* for z */
+  } /* for y */
+ } /* for x */
+
+ } /* if step modulo diff_rate == 0 */
+
+ 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;
+ int fd;
+ char buf[32],*p;
+ unsigned char *flag;
+ u32 max;
+
+ max=0;
+ if((fd=open(file,O_RDONLY))<0)
+ {
+  puts("cannot open file to calculate rejection graph");
+  return -1;
+ }
+ if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
+ {
+  puts("cannot malloc flag memory for rejection graph");
+  return -1;
+ }
+ memset(flag,0,d3_l->max_z);
+ memset(graph,0,d3_l->max_z*sizeof(u32));
+ /* get fixpoints */
+ k=1;
+ while(k)
+ {
+  for(i=0;i<32;i++)
+  {
+   k=read(fd,&buf[i],1);
+   if((buf[i]=='\n')||(k==0)) 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) k=0;
+   else 
+   {
+    graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
+    flag[(int)(a/CELL_LENGTH)]=1;
+   }
+  }
+ }
+ /* do (linear) interpolation here! */
+ i=0;
+ a=0;
+ while(i<d3_l->max_z)
+ {
+  /* graph[0] is 0! */
+  j=i;
+  i++;
+  while(flag[i]==0&&i<d3_l->max_z) i++;
+  for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
+  if(graph[i]>max) max=graph[i];
+ }
+
+ free(flag);
+
+#ifdef DEBUG_INTERPOL_PROFILE
+ printf("debug: %s (interpolated profile)\n",file);
+ for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
+#endif
+
+ return max;
+}
+
+
+
+/*
+ * main program
+ */
+
+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;
+
+  /* default values */
+  resave=RESAVE;
+  c_step=0;
+  strcpy(server_ip,"");
+  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;
+  port=1025;
+
+  /* parse/check argv */
+  for(i=1;i<argc;i++) {
+    if(argv[i][0]=='-') {
+      switch(argv[i][1]) {
+        case 'h':
+          usage(argv[0]);
+          return -1;
+        case 'i':
+          strncpy(server_ip,argv[++i],16);
+          break;
+        case 'r':
+          strcpy(r_file,argv[++i]);
+          break;
+        case 'P':
+          strcpy(p_file,argv[++i]);
+          break;
+        case 'n':
+          strcpy(n_e_file,argv[++i]);
+          break;
+        case 'p':
+          port=atoi(argv[++i]);
+          break;
+        default:
+          usage(argv[0]);
+          return -1;
+      }
+    }
+  }
+  if(!strcmp(server_ip,"")) {
+    usage(argv[0]);
+    return -1;
+  }
+
+  /* connect to server and notify about free resource */
+  network_init(&net,1);
+  network_set_connection_info(&net,0,server_ip,port);
+  network_connect(&net,0);
+
+  /* wait for job */
+
+  /* get info (+data) */
+
+  /* care for signals */
+  err_dc=DC_QUIT;
+  signal(SIGTERM,send_data(&d3_l,&my_info,i,&err_dc));
+
+  /* rand init */
+  if(!strcmp(r_file,"")) rand_init(NULL);
+  else rand_init(r_file);
+
+  /* compute graphs for random number rejection method */
+  if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
+    puts("failed allocating memory for carbon profile graph");
+    return -1;
+  }
+  if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
+    puts("failed allocating memory for nuclear energy loss graph");
+    return -1;
+  }
+  ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
+  ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
+
+  /* array nel_z[] keeping nuclear energy loss values scaled to URAND_MAX */
+  nel_z=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int));
+  if(nel_z==NULL) {
+    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);
+
+
+  /* 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;
+  }
+
+  /* sputtering really possible ?*/
+  if(n_e_loss[d3_l.max_z-1]!=0)
+    printf("warning: max_z (%d) too small, there may be amorphous volumes\n",
+           d3_l.max_z);
+
+  /*
+   * start of simulation
+   */
+
+  i=(c_step?c_step:0);
+  while(i<my_info.steps) {
+    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);
+      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) {
+      dc=DC_OK;
+      send_data(&d3_l,&my_info,i,&dc);
+    }
+    i++;
+    if(i%my_info.s_rate==0) sputter(&d3_l);
+  }
+
+  /* finished */
+  dc=DC_END;
+  send_data(&d3_l,&my_info,i,&dc);
+
+  /* shutdown/free/close everything now ... */
+
+  return 1;
+}