Advertisement
Guest User

perc_threshold.c

a guest
Jul 28th, 2014
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5.  
  6. //MAKE SURE HEIGHT IS A MULTIPLE OF BINHEIGHT!
  7. #define BINHEIGHT 2
  8. #define HEIGHT 200
  9. #define NUM_BINS HEIGHT/BINHEIGHT
  10.  
  11. typedef enum{READ_NUMBER,READ_PARTICLE,SKIP_ONE,ERROR,END_OF_FRAME} file_state;
  12.  
  13. //STATUS VARIABLES
  14. int N; //Number of particles
  15. int frame = 0; //How many frames have been read?
  16. int current_particle;
  17. double *x=NULL;
  18. double *z=NULL;
  19. double *patch_x[3]={NULL};
  20. double *patch_z[3]={NULL};
  21. unsigned short int *patches;
  22. unsigned int *bin;
  23. long double bin_bonds[NUM_BINS]={0.0};
  24. unsigned long int bin_patches[NUM_BINS]={0};
  25. const double sigma = 1.0;
  26. const double delta = 0.11965683746373795115;
  27. //----------------
  28.  
  29. file_state read_number(const char *);
  30. file_state read_particle(const char *);
  31. unsigned int binof(double);
  32. unsigned int bondsof(void);
  33. double patch_distance(int p1, int patch1, int p2, int patch2);
  34. double distance(int p1, int p2);
  35. bool hasbond(int p1, int p2);
  36.  
  37. int main(int argc, char **argv){
  38.     if(argc != 2){
  39.         printf("usage: pbond <moviefile>\n");
  40.         return 1;
  41.     }
  42.  
  43.     FILE *datei=fopen(argv[1],"r");
  44.     if(datei == NULL){
  45.         printf("file %s does not exist.\n",argv[1]);
  46.         return 1;
  47.     }
  48.     int maxlength = 100;
  49.     char zeile[maxlength];
  50.     file_state fs = READ_NUMBER;
  51.     while(fgets(zeile,maxlength,datei) != NULL){
  52.         switch(fs){
  53.             case END_OF_FRAME:
  54.                 printf("Frame %d done.\n",++frame);
  55.             case READ_NUMBER:
  56.                 fs = read_number(zeile);
  57.                 break;
  58.             case SKIP_ONE:
  59.                 fs = READ_PARTICLE;
  60.                 break;
  61.             case READ_PARTICLE:
  62.                 fs = read_particle(zeile);
  63.                 break;
  64.             default:
  65.                 printf("error: file not valid!\n");
  66.                 return 1;
  67.         }
  68.     }
  69.     fclose(datei);
  70.     if(fs == END_OF_FRAME){
  71.         printf("All frames (%d) done.\n",++frame);
  72.         int i;
  73.         for(i=0;i<NUM_BINS;++i){
  74.             if(bin_patches[i]>0){
  75.                 bin_bonds[i]/=bin_patches[i];
  76.             }else{
  77.                 printf("%lu patches in bin %d\n",bin_patches[i],i);
  78.             }
  79.         }
  80.         printf("writing..."); fflush(stdout);
  81.         FILE *output=fopen("pbonds.dat","w");
  82.         for(i=0;i<NUM_BINS;++i){
  83.             fprintf(output,"%d\t%lf\t%Lf\n",i,(double)((i+0.5)*BINHEIGHT),bin_bonds[i]);
  84.         }
  85.         fflush(output);
  86.         fclose(output);
  87.         printf("done.\n");
  88.         free(x);
  89.         free(z);
  90.         free(patches);
  91.         free(bin);
  92.         for(i=0;i<3;++i){
  93.             free(patch_x[i]);
  94.             free(patch_z[i]);
  95.         }
  96.         return 0;
  97.     }else{
  98.         return 1;
  99.     }
  100. }
  101.  
  102. file_state read_number(const char *zeile){
  103.     int vars=0;
  104.     if((vars=sscanf(zeile,"%d",&N))!=1){
  105.         printf("Read %d vars instead of 1 (N).\n%s\n",vars,zeile);
  106.         return ERROR;
  107.     }
  108.     int i;
  109.     free(x);
  110.     free(z);
  111.     free(patches);
  112.     free(bin);
  113.     for(i=0;i<3;++i){
  114.         free(patch_x[i]);
  115.         free(patch_z[i]);
  116.     }
  117.     x = (double *)calloc(N,sizeof(double));
  118.     z = (double *)calloc(N,sizeof(double));
  119.     bin = (unsigned int *)calloc(N,sizeof(unsigned int));
  120.     for(i=0;i<3;++i){
  121.         patch_x[i]=(double *)calloc(N,sizeof(double));
  122.         patch_z[i]=(double *)calloc(N,sizeof(double));
  123.     }
  124.     patches = (unsigned short int*)calloc(N,sizeof(unsigned short int));
  125.     current_particle = 0;
  126.     return SKIP_ONE;
  127. }
  128.  
  129. file_state read_particle(const char *zeile){
  130.     char species_indicator;
  131.     double a;
  132.     int vars=0;
  133.     if((vars=sscanf(zeile,"%[CN]\t%lf\t%lf\t%lf",&species_indicator,&(x[current_particle]),&(z[current_particle]),&a)) != 4){
  134.         printf("Read %d vars instead of 4 (species,x,z,a).\n%s\n",vars,zeile);
  135.         return ERROR;
  136.     }
  137.     bin[current_particle]=binof(z[current_particle]);
  138.     switch(species_indicator){
  139.         case 'N':
  140.             patches[current_particle]=2;
  141.             patch_x[0][current_particle]=x[current_particle]+sigma/2.0*cos(a);
  142.             patch_z[0][current_particle]=z[current_particle]+sigma/2.0*sin(a);
  143.             patch_x[1][current_particle]=x[current_particle]+sigma/2.0*cos(a+M_PI);
  144.             patch_z[1][current_particle]=z[current_particle]+sigma/2.0*sin(a+M_PI);
  145.             break;
  146.         case 'C':
  147.             patches[current_particle]=3;
  148.             patch_x[0][current_particle]=x[current_particle]+sigma/2.0*cos(a);
  149.             patch_z[0][current_particle]=z[current_particle]+sigma/2.0*sin(a);
  150.             patch_x[1][current_particle]=x[current_particle]+sigma/2.0*cos(a+2.0/3.0*M_PI);
  151.             patch_z[1][current_particle]=z[current_particle]+sigma/2.0*sin(a+2.0/3.0*M_PI);
  152.             patch_x[2][current_particle]=x[current_particle]+sigma/2.0*cos(a+4.0/3.0*M_PI);
  153.             patch_z[2][current_particle]=z[current_particle]+sigma/2.0*sin(a+4.0/3.0*M_PI);
  154.             break;
  155.         default:
  156.             printf("Error. Unknown species.\n");
  157.             return ERROR;
  158.     }
  159.     bin_patches[bin[current_particle]]+=patches[current_particle];
  160.     bin_bonds[bin[current_particle]]+=bondsof();
  161.     if(++current_particle==N) return END_OF_FRAME;
  162.     else return READ_PARTICLE;
  163. }
  164.  
  165. unsigned int binof(double z){
  166.     if(z>HEIGHT){
  167.         printf("Error. Invalid point.\n");
  168.         exit(1);
  169.     }
  170.     return (unsigned int)(floor(z/BINHEIGHT));
  171. }
  172.  
  173. unsigned int bondsof(void){
  174.     unsigned int bonds=0;
  175.     int j=0;
  176.     for(j=0;j<current_particle;++j){
  177.         if(hasbond(current_particle,j)){
  178.             bin_bonds[bin[j]]+=1;
  179.             ++bonds;
  180.         }
  181.     }
  182.     return bonds;
  183. }
  184.  
  185. bool hasbond(int p1, int p2){
  186.     if(distance(p1,p2)>sigma+delta) return false;
  187.     int i=0,j=0;
  188.     for(i=0;i<patches[p1];++i){
  189.         for(j=0;j<patches[p2];++j){
  190.             if(patch_distance(p1,i,p2,j)<=delta) return true;
  191.         }
  192.     }
  193.     return false;
  194. }
  195.  
  196. double patch_distance(int p1, int patch1, int p2, int patch2){
  197.     return sqrt(pow(patch_x[patch1][p1]-patch_x[patch2][p2],2)+pow(patch_z[patch1][p1]-patch_z[patch2][p2],2));
  198. }
  199.  
  200. double distance(int p1, int p2){
  201.     return sqrt(pow(x[p1]-x[p2],2)+pow(z[p1]-z[p2],2));
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement