optimized defaults, added sputter condition
[physik/nlsop.git] / nlsop_client.c
1 /*
2  * client nlsop code
3  *
4  * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
5  *
6  * this program tries helping to understand the amorphous depuration
7  * and recrystallization of SiCx while ion implantation at temperatures
8  * below 400 degree celsius.
9  * hopefully the program will simulate the stabilization of the
10  * selforganizing lamella structure in the observed behaviour.
11  *
12  * refs: 
13  *  - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
15  *
16  * Copyright (C) 2004 Frank Zirkelbach
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33
34 #define _GNU_SOURCE
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42
43 #include <signal.h>
44
45 #include "nlsop.h"
46 #include "dfbapi.h"
47 #include "random.h"
48
49 #include "network.h"
50 #include "event.h"
51
52 #include "nlsop_general.h"
53
54 #define MAKE_AMORPH(N) *(N)|=AMORPH
55 #define MAKE_CRYST(N) *(N)&=~AMORPH
56
57 /* globals */
58
59 char p_file[MAX_CHARS];
60 char n_e_file[MAX_CHARS];
61 char r_file[MAX_CHARS];
62 char s_file[MAX_CHARS];
63 int start_fd;
64 t_net *gnet;
65 d3_lattice *gd3_l;
66 info *gmy_info;
67 int *gi;
68 unsigned char dc;
69 unsigned char shut_down;
70 double c_ratio;
71
72 int get_data_and_calc(t_event *event,void *allineed);
73 int nop(t_event *event,void *allineed);
74
75 int usage(char *prog)
76 {
77  puts("usage:");
78  printf("%s -i ip -p port -r/P/n/s random/profile/neloss/start file\n",prog);
79  return 1;
80 }
81
82 /*
83  * nlsop internal functions
84  */
85
86 int sputter(d3_lattice *d3_l)
87 {
88  int i,size;
89  int offh,offl;
90
91  size=d3_l->max_x*d3_l->max_y;
92  offl=0;
93  offh=size;
94
95  for(i=0;i<d3_l->max_z-1;i++)
96  {
97   memcpy(d3_l->status+offl,d3_l->status+offh,size);
98   memcpy(d3_l->extra+offl,d3_l->extra+offh,size*sizeof(int));
99   offl=offh;
100   offh+=size;
101  }
102  memset(d3_l->status+offl,0,size);
103  memset(d3_l->extra+offl,0,size);
104
105  return 1;
106 }
107
108 int get_c_ratio(double *c_ratio,char *pfile,info *my_info,d3_lattice *d3_l)
109 {
110  double all,a,b,d;
111  int i,k;
112  int p_fd;
113  unsigned char buf[32];
114  char *p;
115  unsigned char c;
116
117  if((p_fd=open(pfile,O_RDONLY))<0)
118  {
119   puts("cannot open profile file");
120   return -1;
121  }
122  k=1;
123  d=0;
124  all=0;
125  while(k)
126  {
127   for(i=0;i<32;i++)
128   {
129    k=read(p_fd,&c,1);
130    buf[i]=c;
131    if(c=='\n') break;
132   }
133   if(k)
134   {
135    p=strtok(buf," ");
136    a=atof(p)/10; /* nm */
137    p=strtok(NULL," ");
138    b=atof(p);
139    if(a<=d3_l->max_z*CELL_LENGTH) d+=b;
140    all+=b;
141   }
142  }
143  *c_ratio=d/all;
144  close(p_fd);
145
146  return 1;
147 }
148
149 int process_cell(d3_lattice *d3_l,u32 x,u32 y,u32 z,info *my_info,u32 nel_z)
150 {
151  unsigned char *thiz;
152  int *conc;
153  int i,j;
154  int off;
155  double p,q;
156
157  thiz=d3_l->status+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
158  conc=d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y;
159  //p=my_info->b*nel_z; // energieuebertrag prop zu nukl. bk
160  p=my_info->b*URAND_MAX; // konstanter energieuebertrag
161  for(i=-(my_info->range);i<=my_info->range;i++)
162  {
163   for(j=-(my_info->range);j<=my_info->range;j++)
164   {
165    if(!(i==0 && j==0))
166    {
167     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;
168     if(*(d3_l->status+off)&AMORPH) p+=my_info->s*(*(d3_l->extra+off))*URAND_MAX/(i*i+j*j);
169    } 
170   }
171  }
172  p+=*conc*my_info->c*URAND_MAX;
173  if(p>=URAND_MAX) MAKE_AMORPH(thiz);
174  else {
175   if(!(*thiz&AMORPH)) {
176    if(get_rand(URAND_MAX)<=p) MAKE_AMORPH(thiz);
177   }
178   else {
179    /* assume 1-p probability */
180    /* also look for neighbours ! */
181    q=(URAND_MAX-p)>0?URAND_MAX-p:0;
182    j=0;
183    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;
184    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;
185    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;
186    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;
187    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;
188    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;
189  
190    p+=((q/6)*j);
191    if(p<=URAND_MAX) {
192     if(get_rand(URAND_MAX)>p) MAKE_CRYST(thiz);
193    }
194   }
195  }
196  
197  return 1;
198 }
199
200 int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
201 {
202  u32 x,y,z;
203  int i,j,k,c;
204  int offset,off;
205  int carry;
206
207  /* put one c ion somewhere in the lattice */
208  if(c_ratio*step<=my_info->cc) {
209    x=get_rand(d3_l->max_x);
210    y=get_rand(d3_l->max_y);
211    z=get_rand_reject(d3_l->max_z,rj_m,rj_g);
212    *(d3_l->extra+x+y*d3_l->max_x+z*d3_l->max_x*d3_l->max_y)+=1;
213    (my_info->cc)++;
214  }
215
216  if(step%my_info->diff_rate==0)
217  {
218
219  for(i=0;i<d3_l->max_x;i++)
220  {
221   for(j=0;j<d3_l->max_y;j++)
222   {
223    for(k=0;k<d3_l->max_z;k++)
224    {
225     offset=i+j*d3_l->max_x+k*d3_l->max_x*d3_l->max_y;
226     /* case amorph: amorph <- cryst diffusion */
227     if(*(d3_l->status+offset)&AMORPH)
228     {
229      for(c=-1;c<=1;c++)
230      {
231       if(c!=0)
232       {
233        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;
234        carry=0;
235        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
236        if(carry!=0)
237        {
238         *(d3_l->extra+offset)+=carry;
239         *(d3_l->extra+off)-=carry;
240        }
241       }
242      }
243      for(c=-1;c<=1;c++)
244      {
245       if(c!=0)
246       {
247        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;
248        carry=0;
249        if(!(*(d3_l->status+off)&AMORPH)) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
250        if(carry!=0)
251        {
252         *(d3_l->extra+offset)+=carry; 
253         *(d3_l->extra+off)-=carry; 
254        }
255       }
256      }
257      /* diff in z direction */
258      if(k!=0)
259      {
260       off=i+j*d3_l->max_x+(k-1)*d3_l->max_x*d3_l->max_y;
261       carry=0;
262       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
263       if(carry!=0)
264       {
265        *(d3_l->extra+off)-=carry;
266        *(d3_l->extra+offset)+=carry;
267       }
268      }
269      if(k!=d3_l->max_z-1)
270      {
271       off=i+j*d3_l->max_x+(k+1)*d3_l->max_x*d3_l->max_y;
272       carry=0;
273       if(!*(d3_l->status+off)&AMORPH) carry=(int)(my_info->dr_ac*(*(d3_l->extra+off)));
274       if(carry!=0)
275       {
276        *(d3_l->extra+off)-=carry;
277        *(d3_l->extra+offset)+=carry;
278       }
279      }
280     }
281    } /* for z */
282   } /* for y */
283  } /* for x */
284
285  } /* if step modulo diff_rate == 0 */
286
287  return 1;
288 }
289
290 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
291  double a,b;
292  int i,j,k;
293  int fd;
294  char buf[32],*p;
295  unsigned char *flag;
296  u32 max;
297
298  max=0;
299  if((fd=open(file,O_RDONLY))<0)
300  {
301   puts("cannot open file to calculate rejection graph");
302   return -1;
303  }
304  if((flag=(unsigned char *)malloc(d3_l->max_z))==NULL)
305  {
306   puts("cannot malloc flag memory for rejection graph");
307   return -1;
308  }
309  memset(flag,0,d3_l->max_z);
310  memset(graph,0,d3_l->max_z*sizeof(u32));
311  /* get fixpoints */
312  k=1;
313  while(k)
314  {
315   for(i=0;i<32;i++)
316   {
317    k=read(fd,&buf[i],1);
318    if((buf[i]=='\n')||(k==0)) break;
319   }
320   if(k)
321   {
322    p=strtok(buf," ");
323    a=atof(p)/10; /* nm */
324    p=strtok(NULL," ");
325    b=atof(p);
326    if(a>d3_l->max_z*CELL_LENGTH) k=0;
327    else 
328    {
329     graph[(int)(a/CELL_LENGTH)]=(int)(URAND_MAX/100*b);
330     flag[(int)(a/CELL_LENGTH)]=1;
331    }
332   }
333  }
334  /* do (linear) interpolation here! */
335  i=0;
336  a=0;
337  while(i<d3_l->max_z)
338  {
339   /* graph[0] is 0! */
340   j=i;
341   i++;
342   while(flag[i]==0&&i<d3_l->max_z) i++;
343   for(k=j+1;k<i;k++) graph[k]=(int)((k-j)*((int)graph[i]-(int)graph[j])/(i-j))+graph[j];
344   if(graph[i]>max) max=graph[i];
345  }
346
347  free(flag);
348
349 #ifdef DEBUG_INTERPOL_PROFILE
350  printf("debug: %s (interpolated profile)\n",file);
351  for(i=0;i<d3_l->max_z;i++) printf("%d %d\n",i,graph[i]);
352 #endif
353
354  return max;
355 }
356
357 void send_data(int signum) {
358
359   int c;
360   unsigned char ack=DATA_OK;
361
362   c=gd3_l->max_x*gd3_l->max_y*gd3_l->max_z;
363
364   network_send_chan(gnet,0,&dc,1);
365   network_send_chan(gnet,0,(unsigned char *)gd3_l,sizeof(d3_lattice));
366   network_send_chan(gnet,0,(unsigned char *)gmy_info,sizeof(info));
367   network_send_chan(gnet,0,gd3_l->status,c*sizeof(unsigned char));
368   network_send_chan(gnet,0,(unsigned char *)gd3_l->extra,c*sizeof(int));
369   network_send_chan(gnet,0,(unsigned char *)gi,sizeof(int));
370   network_send_chan(gnet,0,&ack,sizeof(unsigned char));
371
372   if(dc==DC_QUIT) shut_down=1;
373 }
374
375
376 /*
377  * main program
378  */
379
380 int main(int argc,char **argv)
381 {
382
383   char server_ip[16];
384   int port;
385   t_net net;
386   t_event event;
387   unsigned char data[256];
388   int i;
389
390   gnet=&net;
391
392   /* default values */
393   strcpy(server_ip,"137.250.82.105");
394   strcpy(p_file,IMP_PROFILE);
395   strcpy(n_e_file,NEL_PROFILE);
396   strcpy(r_file,"");
397   strcpy(s_file,"");
398   start_fd=0;
399   port=1025;
400
401   /* parse/check argv */
402   for(i=1;i<argc;i++) {
403     if(argv[i][0]=='-') {
404       switch(argv[i][1]) {
405         case 'h':
406           usage(argv[0]);
407           return -1;
408         case 'i':
409           strncpy(server_ip,argv[++i],16);
410           break;
411         case 'r':
412           strcpy(r_file,argv[++i]);
413           break;
414         case 'P':
415           strcpy(p_file,argv[++i]);
416           break;
417         case 'n':
418           strcpy(n_e_file,argv[++i]);
419           break;
420         case 'p':
421           port=atoi(argv[++i]);
422           break;
423         case 's':
424           strcpy(s_file,argv[++i]);
425           break;
426         default:
427           usage(argv[0]);
428           return -1;
429       }
430     }
431   }
432   if(!strcmp(server_ip,"")) {
433     usage(argv[0]);
434     return -1;
435   }
436
437   /* try a file to start from */
438   if(strcmp(s_file,"")) {
439     start_fd=open(s_file,O_RDONLY);
440     if(start_fd>0) printf("using %s as a starting file ...\n",s_file);
441     else printf("errors opening %s!\n",s_file);
442   }
443
444   /* event init */
445   event_init(&event,1);
446   event_set_timeout(&event,0,0);
447
448   /* connect to server */
449   network_init(&net,1);
450   network_set_connection_info(&net,0,server_ip,port);
451   if(network_connect(&net,0)==N_E_CONNECT) {
452     printf("unable to connect to server, aborting ...\n");
453     return -1;
454   }
455   network_select(&net,0);
456
457   /* tell server: i am a client, i may work for you */
458   data[0]=NLSOP_CLIENT;
459   network_send(net.connection[0].fd,data,1);
460
461   /* wait for job */
462   event_math(net.connection[0].fd,&event,READ,ADD);
463   printf("idle, waiting for jobs ...\n");
464   event_start(&event,NULL,get_data_and_calc,nop);
465
466   network_shutdown(&net);
467   if(start_fd>0) close(start_fd);
468
469   return 1;
470 }
471
472 int nop(t_event *event,void *allineed) {
473
474   printf("\ni did a nop :)\n");
475
476   return 1;
477 }
478
479 int get_data_and_calc(t_event *event,void *allineed) {
480
481   d3_lattice d3_l;
482   info my_info;
483   u32 *c_profile;
484   u32 *n_e_loss;
485   u32 ne_max,ip_max;
486   u32 *nel_z;
487   u32 x_c,y_c,z_c;
488   int i,j;
489   int c_step;
490   unsigned char do_sputter;
491   unsigned char data;
492
493   t_net *net;
494
495   c_step=0;
496   ne_max=0;
497   ip_max=0;
498   do_sputter=1;
499   c_ratio=0;
500
501   net=gnet;
502   gd3_l=&d3_l;
503   gmy_info=&my_info;
504   gi=&i;
505   dc=0;
506   shut_down=0;
507
508   printf("got a new job ...\n");
509   
510   /* get info (+data) */
511   network_receive(net->connection[0].fd,&data,sizeof(unsigned char));
512   if(data==NLSOP_NJOB || data==NLSOP_CJOB) {
513     network_receive(net->connection[0].fd,(unsigned char *)&d3_l,
514                     sizeof(d3_lattice));
515     network_receive(net->connection[0].fd,(unsigned char *)&my_info,
516                     sizeof(info));
517     c_step=0;
518     j=d3_l.max_x*d3_l.max_y*d3_l.max_z;
519     d3_l.status=(unsigned char *)malloc(j*sizeof(unsigned char));
520     if(d3_l.status==NULL) {
521       printf("status alloc failed\n");
522       return -1;
523     }
524     d3_l.extra=(int *)malloc(j*sizeof(int));
525     if(d3_l.extra==NULL) {
526       printf("extra malloc failed\n");
527       return -1;
528     }
529     if(data==NLSOP_CJOB) {
530       data=DATA_OK;
531       network_receive(net->connection[0].fd,d3_l.status,
532                       j*sizeof(unsigned char));
533       network_send(net->connection[0].fd,&data,sizeof(unsigned char));
534       network_receive(net->connection[0].fd,(unsigned char *)d3_l.extra,
535                       j*sizeof(int));
536       network_send(net->connection[0].fd,&data,sizeof(unsigned char));
537       network_receive(net->connection[0].fd,(unsigned char *)&c_step,
538                       sizeof(int));
539       network_send(net->connection[0].fd,&data,sizeof(unsigned char));
540     }
541     if(c_step==0) {
542       printf("important: clear status/conc data!\n");
543       memset(d3_l.status,0,j*sizeof(unsigned char));
544       memset(d3_l.extra,0,j*sizeof(int));
545     }
546     /* check for file to start from ... */
547     if(start_fd>0) {
548       printf("starting from a save file!\n");
549       unsigned char *nullbuf;
550       nullbuf=(unsigned char *)malloc(sizeof(d3_lattice));
551       if(read(start_fd,nullbuf,sizeof(d3_lattice))!=sizeof(d3_lattice)) {
552         printf("read failed (start file d3l)\n");
553         return -1;
554       }
555       free(nullbuf);
556       nullbuf=(unsigned char *)malloc(sizeof(info));
557       if(read(start_fd,nullbuf,sizeof(info))!=sizeof(info)) {
558         printf("read failed (start file info)\n");
559         return -1;
560       }
561       free(nullbuf);
562       if(read(start_fd,d3_l.status,j*sizeof(unsigned char))!=j*sizeof(unsigned char)) {
563         printf("read failed (start file status)\n");
564         return -1;
565       }
566       if(read(start_fd,d3_l.extra,j*sizeof(int))!=j*sizeof(int)) {
567         printf("read failed (start file extra)\n");
568         return -1;
569       }
570     }
571   }
572   else {
573     printf("unknown instruction, restarting ...\n");
574     return -1;
575   }
576
577   printf("starting simulation with following parameters:\n");
578   printf("b = %f | c = %f | s = %f\n",my_info.b,my_info.c,my_info.s);
579   printf("diff every %d steps | diff rate = %f\n",my_info.diff_rate,
580                                                   my_info.dr_ac);
581   printf("current step: %d | total steps: %d\n",c_step,my_info.steps);
582   printf("...\n");
583
584   /* care for signals */
585   dc=DC_QUIT;
586   signal(SIGTERM,send_data);
587
588   /* rand init */
589   if(!strcmp(r_file,"")) rand_init(NULL);
590   else rand_init(r_file);
591
592   /* compute graphs for random number rejection method */
593   if((c_profile=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
594     puts("failed allocating memory for carbon profile graph");
595     return -1;
596   }
597   if((n_e_loss=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int)))==NULL) {
598     puts("failed allocating memory for nuclear energy loss graph");
599     return -1;
600   }
601   ip_max=get_reject_graph(&my_info,&d3_l,p_file,c_profile);
602   ne_max=get_reject_graph(&my_info,&d3_l,n_e_file,n_e_loss);
603
604   /* array nel_z[] keeping nuclear energy loss values scaled to URAND_MAX */
605   nel_z=(u32 *)malloc(d3_l.max_z*sizeof(unsigned int));
606   if(nel_z==NULL) {
607     printf("failed allocating nel_z array mem\n");
608     return -1;
609   }
610   for(i=0;i<d3_l.max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
611
612   /* this should be obsolete - z is high enough - we check now! */
613   if(c_profile[d3_l.max_z-1]!=0) {
614     printf("max_z (%d) too small - sputtering not possible\n",d3_l.max_z);
615     do_sputter=0;
616   }
617
618   /* sputtering really possible ?*/
619   if(n_e_loss[d3_l.max_z-1]!=0)
620     printf("warning: max_z (%d) too small, there may be amorphous volumes\n",
621            d3_l.max_z);
622
623   /* maybe we need a ratio! */
624   if(!do_sputter) {
625     get_c_ratio(&c_ratio,p_file,&my_info,&d3_l);
626     printf("calculated c ratio: %f\n",c_ratio);
627   }
628
629   /*
630    * start of simulation
631    */
632
633   i=(c_step?c_step:0);
634   while(i<my_info.steps) {
635     for(j=0;j<my_info.cpi;j++) {
636       x_c=get_rand(d3_l.max_x);
637       y_c=get_rand(d3_l.max_y);
638       z_c=get_rand_reject(d3_l.max_z,ne_max,n_e_loss);
639       //z_c=get_rand(d3_l.max_z);
640       process_cell(&d3_l,x_c,y_c,z_c,&my_info,nel_z[z_c]);
641     }
642     distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
643     i++;
644     if(i%my_info.save_rate==0) {
645       dc=DC_OK;
646       send_data(0);
647       dc=DC_QUIT;
648     }
649     if((do_sputter)&(i%my_info.s_rate==0)) sputter(&d3_l);
650     if(shut_down) {
651       free(d3_l.status);
652       free(d3_l.extra);
653       free(c_profile);
654       free(n_e_loss);
655       free(nel_z);
656       event_stop(event); 
657     }
658   }
659
660   /* finished */
661   dc=DC_END;
662   send_data(0);
663   dc=DC_QUIT;
664
665   /* shutdown/free/close everything now ... */
666   free(d3_l.status);
667   free(d3_l.extra);
668   free(c_profile);
669   free(n_e_loss);
670   free(nel_z);
671
672   return 1;
673 }