--
[physik/morpheus.git] / main.c
diff --git a/main.c b/main.c
index 4ff223b..2f1cff5 100644 (file)
--- a/main.c
+++ b/main.c
  *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
  */
 
+#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>
 
 /* important defines */
 #include "defines.h"
@@ -26,7 +28,9 @@
 #include "display.h"
 
 /* global variables */
-u32 sum_z_cells,sum_c_dist;
+u32 *rand_buf,*rand_current;
+char random_file[MAX_CHARS_RANDOM_FILE];
+u32 gr;
 int random_fd; /* /dev/urandom file descriptor */
 
 int usage()
@@ -46,8 +50,13 @@ int usage()
  printf("-r <value> \t pressure range from amorphous SiCx (default %d)\n",DEFAULT_A_P_RANGE);
  printf("-f <value> \t faktor for pressure from amorphous SiCx (default %f)\n",DEFAULT_A_P_FAKTOR);
  printf("-p <value> \t p0 for probability of cell getting amorph (default %f)\n",DEFAULT_A_P_P0);
- printf("-C <value> \t C start concentration (default %d)\n",DEFAULT_C_START_CONC);
- printf("-S <value> \t slope of linear C distribution (default %d)\n",DEFAULT_C_SLOPE);
+ printf("-C <value> \t C disribution start concentration (default %d)\n",DEFAULT_C_DIST_START_CONC);
+ printf("-S <value> \t slope of linear C distribution (default %d)\n",DEFAULT_C_DIST_SLOPE);
+ printf("-b <value> \t initial C concentration (default %d)\n",DEFAULT_C_C0);
+ puts("-R <file> \t read random data from file (default not used)");
+ puts("-D <file> \t dump cell info into <file> (default not used)");
+ puts("-L <file> \t load cell info and display it (default no)");
+ puts("-n \t do not wait for user interaction (default no)");
  return -23;
 }
 
@@ -63,50 +72,169 @@ int make_cryst(cell *cell)
  return 23;
 }
 
-int distrib_c_conc(cell *cell_p,u32 c_c0,u32 c_slope,int add_c,u32 x_max,u32 y_max,u32 z_max)
+int load_from_file(char *file,display *display)
 {
- /* cryst. c to distribute */
- int i,j,k;
- double cryst_c;
- cryst_c=0;
- for(i=0;i<x_max*y_max*z_max;i++) if(!(cell_p+i)->status&AMORPH) cryst_c+=(cell_p+i)->conc;
+ int load_file_fd;
+
+ if((load_file_fd=open(file,O_RDONLY))<0)
+ {
+  puts("cannot open load file");
+  return -23;
+ }
+ if(read(load_file_fd,&(display->max_x),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed reading max x");
+  return-23;
+ }
+ if(read(load_file_fd,&(display->max_y),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed reading max y");
+  return -23;
+ }
+ if(read(load_file_fd,&(display->max_z),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed reading max z");
+  return -23;
+ }
+ if(read(load_file_fd,display->cell_p,display->max_x*display->max_y*display->max_z*sizeof(cell))<display->max_x*display->max_y*display->max_z*sizeof(cell))
+ {
+  puts("failed reading cell info from file");
+  return -23;
+ }
+ close(load_file_fd);
+ puts("loaded file");
+ return 23;
+}
+
+int save_to_file(char *file,display *display)
+{
+ int save_file_fd;
+
+ if((save_file_fd=open(file,O_CREAT|O_WRONLY|O_TRUNC))<0)
+ {
+  puts("cannot open save file");
+  return -23;
+ }
+ if(write(save_file_fd,&(display->max_x),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed saving max x");
+  return -23;
+ }
+ if(write(save_file_fd,&(display->max_y),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed saving max y");
+  return -23;
+ }
+ if(write(save_file_fd,&(display->max_z),sizeof(u32))<sizeof(u32))
+ {
+  puts("failed saving max z");
+  return -23;
+ }
+ if(write(save_file_fd,display->cell_p,display->max_x*display->max_y*display->max_z*sizeof(cell))!=display->max_x*display->max_y*display->max_z*sizeof(cell))
+ {
+  puts("saving file failed");
+  return -23;
+ }
+ close(save_file_fd);
+ puts("saved file");
+ return 23;
+}
+
+int distrib_c_conc(cell *cell_p,int c_c0,int c_slope,u32 c_conc,u32 x_max,u32 y_max,u32 z_max)
+{
+ int i,j;
+ u32 area,c_area,total,count;
+ u32 x,y,z,sum_c_z;
+
+ total=0;
+ area=x_max*y_max;
+ sum_c_z=c_c0*z_max+c_slope*gr;
+
  for(i=0;i<z_max;i++)
  {
-  for(j=0;j<x_max*y_max;j++)
+  count=0;
+  c_area=(c_conc*(c_c0+(i+1)*c_slope))/sum_c_z;
+  for(j=0;j<area;j++)
   {
-   if(!(cell_p+j+i*x_max*y_max)->status&AMORPH) 
+   if(!(cell_p+j+i*area)->status&AMORPH) count++; 
+  }
+  for(j=0;j<area;j++)
+  {
+   if(!(cell_p+j+i*area)->status&AMORPH)
+   { 
+    (cell_p+j+i*area)->conc=c_area/count;
+    total+=c_area/count;
+   }
+  }
+  j=0;
+  while(j<c_area%count)
+  {
+   x=rand_get(x_max);
+   y=rand_get(y_max);
+   if(!(cell_p+x+y*x_max+i*area)->status&AMORPH)
+   {
+    (cell_p+x+y*x_max+i*area)->conc+=1;
+    total++;
+    j++;
+   }
+  }
+ }
+ i=0;
+ while(i<c_conc-total)
+ {
+  x=rand_get(x_max);
+  y=rand_get(y_max);
+  z=rand_get_lgp(c_slope,c_c0,z_max);
+  if(!(cell_p+x+y*x_max+z*area)->status&AMORPH)
+  {
+   (cell_p+x+y*x_max+z*area)->conc+=1;
+   i++;
+  }
+ }
  return 23;
 }
 
 /* look at cell ... */
-int process_cell(cell *cell_p,u32 x,u32 y,u32 z,u32 x_max,u32 y_max,u32 z_max,int range,double faktor,double p0)
+int process_cell(cell *cell_p,u32 x,u32 y,u32 z,u32 x_max,u32 y_max,u32 z_max,int range,double faktor,double p0,u32 *c_conc)
 {
  cell *this_cell;
  int i,j,k;
  double pressure;
+
  this_cell=cell_p+x+y*x_max+z*x_max*y_max;
  pressure=p0*URAND_2BYTE_MAX;
+
  for(i=-range;i<=range;i++)
  {
   for(j=-range;j<=range;j++)
   {
-   for(k=-range;k<=range;k++)
-   {
-    if(!(i==0 && j==0 && k==0))
+   // z relaxation, no pressure in z direction
+   // for(k=-range;k<=range;k++)
+   // {
+    if(!(i==0 && j==0)) //  && k==0))
     {
-     if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+((z+k+z_max)%z_max)*x_max*y_max)->status&AMORPH) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j+k*k);
+     // if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+((z+k+z_max)%z_max)*x_max*y_max)->status&AMORPH) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j+k*k);
+     if((cell_p+((x+x_max+i)%x_max)+((y+j+y_max)%y_max)*x_max+z*x_max*y_max)->status&AMORPH) pressure+=faktor*URAND_2BYTE_MAX/(i*i+j*j);
     }
-   }
+   // }
   }
  }
+ pressure*=this_cell->conc;
  if(this_cell->status&AMORPH)
  {
   /* wrong probability! just test by now ...*/
-  if(rand_get(URAND_2BYTE_MAX)>pressure) make_cryst(this_cell);
+  if(rand_get(URAND_2BYTE_MAX)>pressure)
+  {
+    make_cryst(this_cell);
+    *c_conc=*c_conc+1+this_cell->conc;
+  } else *c_conc+=1;
  } else
  {
-  if(rand_get(URAND_2BYTE_MAX)<=pressure) make_amorph(this_cell);
+  if(rand_get(URAND_2BYTE_MAX)<=pressure)
+  {
+   make_amorph(this_cell);
+   *c_conc=*c_conc+1-this_cell->conc;
+  } else *c_conc+=1;
  }
  return 23;
 }
@@ -115,17 +243,21 @@ int main(int argc,char **argv)
 {
  u32 x_cell,y_cell,z_cell; /* amount of segments */
  u32 x,y,z; /* cells */
- int i,gr; /* for counting */
+ int i; /* for counting */
  int slope_nel,start_nel; /* nuclear energy loss: slope, constant */
  int a_p_range; /* p. range of amorphous sic */
  double a_p_faktor,a_p_p0; /* p0 and faktor of amorphous sic */
- u32 c_c0,c_slope; /* c start concentration and linear slope of c distribution */
+ int c_dist_c0,c_dist_slope; /* c start concentration and linear slope of c distribution */
+ u32 c_conc;
  int steps; /* # steps */
  cell *cell_p;
  struct __display display;
  u32 display_x,display_y,display_z; /* intercept point of diplayed areas */
  u32 display_refresh_rate; /* refresh rate for display */
  int quit=0; /* continue/quit status */
+ char save_file[MAX_CHARS_SAVE_FILE];
+ char load_file[MAX_CHARS_LOAD_FILE];
+ unsigned char no_wait;
 
  printfd("debug: sizeof my u32 variable: %d\n",sizeof(u32));
  printfd("debug: sizeof my cell struct: %d\n",sizeof(cell));
@@ -139,13 +271,18 @@ int main(int argc,char **argv)
  a_p_range=DEFAULT_A_P_RANGE;
  a_p_faktor=DEFAULT_A_P_FAKTOR;
  a_p_p0=DEFAULT_A_P_P0;
- c_c0=DEFAULT_C_START_CONC;
- c_slope=DEFAULT_C_SLOPE;
+ c_dist_c0=DEFAULT_C_DIST_START_CONC;
+ c_dist_slope=DEFAULT_C_DIST_SLOPE;
+ c_conc=DEFAULT_C_C0;
  steps=DEFAULT_STEPS;
  display_x=x_cell/2;
  display_y=y_cell/2;
  display_z=z_cell/2;
  display_refresh_rate=DEFAULT_DISPLAY_REF_RATE;
+ strcpy(random_file,"");
+ strcpy(save_file,"");
+ strcpy(load_file,"");
+ no_wait=0;
  
  /* parse command args */
  for(i=1;i<argc;i++)
@@ -197,11 +334,26 @@ int main(int argc,char **argv)
     case 'p':
      a_p_p0=atof(argv[++i]);
      break;
+    case 'b':
+     c_conc=atoi(argv[++i]);
+     break;
     case 'C':
-     c_c0=atoi(argv[++i]);
+     c_dist_c0=atoi(argv[++i]);
      break;
     case 'S':
-     c_slope=atoi(argv[++i]);
+     c_dist_slope=atoi(argv[++i]);
+     break;
+    case 'R':
+     strcpy(random_file,argv[++i]);
+     break;
+    case 'D':
+     strcpy(save_file,argv[++i]);
+     break;
+    case 'L':
+     strcpy(load_file,argv[++i]);
+     break;
+    case 'n':
+     no_wait=1;
      break;
     default:
      usage();
@@ -211,59 +363,99 @@ int main(int argc,char **argv)
  }
 
  /* open random fd */
- if((random_fd=open("/dev/urandom",O_RDONLY))<0)
+ if(!strcmp(random_file,""))
  {
-  puts("cannot open /dev/urandom\n");
+  if((random_fd=open("/dev/urandom",O_RDONLY))<0)
+  {
+   puts("cannot open /dev/urandom");
+   return -23;
+  }
+ } else
+ {
+  if((random_fd=open(random_file,O_RDONLY))<0)
+  {
+   puts("cannot open random file");
+   return -23;
+  }
+ }
+ /* allocate random number buffer */
+ printf("malloc will free %d bytes now ...\n",RAND_BUF_SIZE);
+ if((rand_buf=(u32 *)malloc(RAND_BUF_SIZE))==NULL)
+ {
+  puts("failed allocating memory for random numbers");
   return -23;
  }
+ rand_current=rand_buf+RAND_BUF_SIZE;
 
- /* calculate sum_z_cells one time! */
+ /* calculate gr one time! */
  gr=0;
  for(i=1;i<=z_cell;i++) gr+=i;
- sum_z_cells=z_cell*start_nel+slope_nel*gr;
- sum_c_dist=z_cell*c_c0+c_slope*gr;
- printfd("debug: sum_z_cells -> %u\ndebug: sum_c_dist -> %u\n",sum_z_cells,sum_c_dist);
-
- /* testing ... */
+ printfd("debug: gr = %d\n",gr);
 
  /* allocate cells */
  printf("malloc will free %d bytes now ...\n",x_cell*y_cell*z_cell*sizeof(cell));
  if((cell_p=(cell *)malloc(x_cell*y_cell*z_cell*sizeof(cell)))==NULL)
  {
-  puts("failed allocating memory for cells\n");
+  puts("failed allocating memory for cells");
   return -23;
  }
  memset(cell_p,0,x_cell*y_cell*z_cell*sizeof(cell));
 
+ /* load from file */
+ if(strcmp(load_file,""))
+ {
+  display.cell_p=cell_p;
+  load_from_file(load_file,&display);
+  x_cell=display.max_x;
+  y_cell=display.max_y;
+  z_cell=display.max_z;
+ }
+
  /* init display */
+ printfd("debug: init display now ...\n");
  display_init(x_cell,y_cell,z_cell,&display,cell_p,&argc,argv);
 
  /* main routine */
- for(i=0;i<steps;i++)
+ /* did we load from file? */
+ if(!strcmp(load_file,""))
+ {
+
+ printfd("debug: starting main routine ...\n");
+ for(display.step=0;display.step<steps;display.step++)
  {
   x=rand_get(x_cell);
   y=rand_get(y_cell);
-  z=rand_get_lgp(slope_nel,start_nel);
+  z=rand_get_lgp(slope_nel,start_nel,z_cell);
 
-  /* todo */
-  distrib_c_conc(cell_p,c_c0,c_slope,i,x_cell,y_cell,z_cell);
+  /* distribute c concentration */
+  distrib_c_conc(cell_p,c_dist_c0,c_dist_slope,c_conc,x_cell,y_cell,z_cell);
 
-  // process_cell(cell_p+x+y*x_cell+z*x_cell*y_cell);
-  process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0);
+  process_cell(cell_p,x,y,z,x_cell,y_cell,z_cell,a_p_range,a_p_faktor,a_p_p0,&c_conc);
 
   /* display stuff */
-  if((i%display_refresh_rate)==0)
+  if((display.step%display_refresh_rate)==0)
    display_draw(&display,display_x,display_y,display_z);
  }
+
+ }
+
+ if(strcmp(save_file,"")) save_to_file(save_file,&display);
+
+ printfd("debug: main routine finished!\n");
   
  /* display again and listen for events */
  display_draw(&display,display_x,display_y,display_z);
- display_event_init(&display);
 
while(!quit)
if(!no_wait)
  {
-  display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
-  display_draw(&display,display_x,display_y,display_z);
+  display_event_init(&display);
+
+  while(!quit)
+  {
+   display_scan_event(&display,&display_x,&display_y,&display_z,&quit);
+   display_draw(&display,display_x,display_y,display_z);
+  }
  }
 
  display_release(&display);