Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5.  
  6. void spheremaker(float r, float* c, int dim, float* mat);
  7. void centerofmass(int dim, float* mat, float* cm);
  8. void xyzprinter(float* matrix, int dim, FILE* file);
  9.  
  10.  
  11. int main(){
  12.  
  13. int i;
  14. FILE* fp;
  15. float* matrix;
  16. float* com;
  17.  
  18. int np=10000;
  19. float c[]={0.0,0.0,0.0};
  20. float rc=10.0;
  21.  
  22. /*INITIALIZING*/
  23. fp=fopen("data.dat","w+");
  24. matrix=(float*)malloc(np*3*sizeof(float));
  25. com=(float*)malloc(3*sizeof(float));
  26.  
  27. /*CORE*/
  28. spheremaker(rc,c,np,matrix);
  29. centerofmass(np,matrix,com);
  30. xyzprinter(matrix,np,fp);
  31. xyzprinter(com,1,fp);
  32.  
  33. /*OUTITIALIZING*/
  34. fclose(fp);
  35. free((void*)matrix);
  36. free((void*)com);
  37. exit(0);
  38. }
  39.  
  40.  
  41.  
  42.  
  43. void spheremaker(float r, float* c, int dim, float* matrix){
  44. float x,y,z,norm;
  45. int i=0;
  46.  
  47. while(i<dim)
  48. {
  49.  
  50. x=2.0*rand()/RAND_MAX-1;
  51. y=2.0*rand()/RAND_MAX-1;
  52. z=2.0*rand()/RAND_MAX-1;
  53.  
  54. norm=pow(x,2)+pow (y,2)+pow(z,2);
  55. norm=sqrt(norm);
  56. if(norm<1){
  57. matrix[0+3*i]=r*x+c[0];
  58. matrix[1+3*i]=r*y+c[1];
  59. matrix[2+3*i]=r*z+c[2];
  60. i=i+1;
  61. }
  62. }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. void centerofmass(int dim, float* matrix, float* com){
  69. int i,j;
  70.  
  71. for(j=0;j<3;j=j+1){
  72. com[j]=0;
  73. for(i=0;i<dim;i=i+1){
  74. com[j]=com[j]+matrix[j+3*i];
  75. }
  76. com[j]=com[j]/dim;
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83. void xyzprinter(float* matrix, int dim, FILE* file){
  84. int i;
  85.  
  86. for(i=0;i<dim;i=i+1){
  87. fprintf(file, "%f %f %f \n", matrix[0+3*i],matrix[1+3*i],matrix[2+3*i]);
  88. }
  89.  
  90. fprintf(file,"\n");
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement