1 | #include "sphere_fast.h" |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <math.h> |
---|
5 | #include <time.h> |
---|
6 | #include <memory.h> |
---|
7 | |
---|
8 | |
---|
9 | /// 1D scattering function |
---|
10 | double sphere_fast_analytical_1D(SimSphereFParameters *pars, double q) { |
---|
11 | int j, npts, volume_points; |
---|
12 | double sum, phi, vol; |
---|
13 | |
---|
14 | return simcylinder_fast_analytical_2D(pars, q, 0.0); |
---|
15 | |
---|
16 | //return simcylinder_fast_analytical_1D_average(pars, q); |
---|
17 | |
---|
18 | sum = 0; |
---|
19 | npts = 21; |
---|
20 | volume_points = (int)floor(pars->npoints); |
---|
21 | |
---|
22 | for(j=0; j<npts; j++) { |
---|
23 | phi = acos(-1.0)/npts * j; |
---|
24 | //sum += simcylinder_simple_analytical_2D(pars, q, phi, volume_points); |
---|
25 | sum += simcylinder_fast_analytical_2D(pars, q, phi); |
---|
26 | } |
---|
27 | |
---|
28 | // Calculate I(q,phi) and return that value |
---|
29 | vol = 4.0/3.0*acos(-1.0)*pars->radius*pars->radius*pars->radius; |
---|
30 | sum = 1.0e8/volume_points/volume_points*vol*sum*acos(-1.0)/2; |
---|
31 | return sum/npts; |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | /// 1D scattering function |
---|
36 | double sphere_fast_analytical_2D(SimSphereFParameters *pars, double q, double phi) { |
---|
37 | // Check if Rho array is available |
---|
38 | int volume_points; |
---|
39 | int r_points; |
---|
40 | int ptsGenerated; |
---|
41 | double bin_width; |
---|
42 | double r_step; |
---|
43 | double vol; |
---|
44 | double retval; |
---|
45 | |
---|
46 | int i,j; |
---|
47 | int ix_bin, iy_bin; |
---|
48 | SpacePoint p1, p2; |
---|
49 | |
---|
50 | clock_t start; |
---|
51 | clock_t finish; |
---|
52 | double cos_term; |
---|
53 | double sin_term; |
---|
54 | double qx, qy, qz; |
---|
55 | double phase; |
---|
56 | double cyl_x, cyl_y, cyl_z; |
---|
57 | double q_x, q_y, q_z; |
---|
58 | double cos_val, alpha; |
---|
59 | |
---|
60 | |
---|
61 | volume_points = (int)floor(pars->npoints); |
---|
62 | cos_term = 0.0; |
---|
63 | sin_term = 0.0; |
---|
64 | |
---|
65 | qx = q*cos(phi); |
---|
66 | qy = q*sin(phi); |
---|
67 | qz = 0.0; |
---|
68 | |
---|
69 | // Generate random points accross the volume |
---|
70 | if(pars->calcPars.isPointMemAllocated_2D==0) { |
---|
71 | pars->calcPars.points_2D = (SpacePoint*)malloc(volume_points*sizeof(SpacePoint)); |
---|
72 | if(pars->calcPars.points_2D==NULL) { |
---|
73 | printf("Problem allocating memory for 2D volume points\n"); |
---|
74 | return -1.0; |
---|
75 | } |
---|
76 | |
---|
77 | ptsGenerated = sphere_fast_generatePoints(pars->calcPars.points_2D, |
---|
78 | volume_points, pars->radius, (int)floor(pars->seed)); |
---|
79 | |
---|
80 | pars->calcPars.isPointMemAllocated_2D=1; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | for(i=0;i<volume_points-1;i++) { |
---|
85 | |
---|
86 | //p1 = fast_rotate(pars->calcPars.points_2D[i], pars->theta, pars->phi, 0.0); |
---|
87 | p1 = pars->calcPars.points_2D[i]; |
---|
88 | phase = qx*p1.x + qy*p1.y; |
---|
89 | cos_term += cos(phase); |
---|
90 | sin_term += sin(phase); |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | // Calculate I(q,phi) and return that value |
---|
95 | vol = 4.0/3.0*acos(-1.0)*pars->radius*pars->radius*pars->radius; |
---|
96 | |
---|
97 | return 1.0e8/volume_points/volume_points*vol*(cos_term*cos_term + sin_term*sin_term); |
---|
98 | //return 1.0e8/volume_points/volume_points*vol*(cos_term*cos_term); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * Generate points randomly accross the volume |
---|
107 | * @param points [SpacePoint*] Array of 3D points to be filled |
---|
108 | * @param n [int] Number of points to generat |
---|
109 | * @param radius [double] Radius of the sphere |
---|
110 | * @return Number of points generated |
---|
111 | */ |
---|
112 | int sphere_fast_generatePoints(SpacePoint * points, int n, double radius, int seed) { |
---|
113 | int i; |
---|
114 | int testcounter; |
---|
115 | double x, y, z; |
---|
116 | |
---|
117 | // Create points |
---|
118 | // To have a uniform density, you want to generate |
---|
119 | // random points in a box and keep only those that are |
---|
120 | // within the volume. |
---|
121 | |
---|
122 | // Initialize random number generator |
---|
123 | //int seed; |
---|
124 | time_t now; |
---|
125 | |
---|
126 | time(&now); |
---|
127 | //seed = 10000; |
---|
128 | |
---|
129 | //seed = (int)floor(fmod(now,10000)); |
---|
130 | //seed = 10009; |
---|
131 | srand(seed); |
---|
132 | //printf("Seed = %i\n", seed); |
---|
133 | |
---|
134 | testcounter = 0; |
---|
135 | |
---|
136 | memset(points,0,n*sizeof(SpacePoint)); |
---|
137 | for(i=0;i<n;i++) { |
---|
138 | // Generate in a box centered around zero |
---|
139 | x = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
140 | y = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
141 | z = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
142 | |
---|
143 | // reject those that are not within the volume |
---|
144 | if( sqrt(x*x+y*y+z*z) < radius ) { |
---|
145 | points[i].x = x; |
---|
146 | points[i].y = y; |
---|
147 | points[i].z = z; |
---|
148 | testcounter++; |
---|
149 | } else { |
---|
150 | i--; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | // Consistency check |
---|
155 | if(testcounter != n) { |
---|
156 | return -1; |
---|
157 | } |
---|
158 | |
---|
159 | return testcounter; |
---|
160 | } |
---|