1 | #include "testsphere.h" |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <math.h> |
---|
5 | #include <time.h> |
---|
6 | #include <memory.h> |
---|
7 | #include "modelCalculations.h" |
---|
8 | |
---|
9 | |
---|
10 | /// 1D scattering function |
---|
11 | double testsphere_analytical_1D(TestSphereParameters *pars, double q) { |
---|
12 | /*** |
---|
13 | * Things to keep here: |
---|
14 | * - volume calc |
---|
15 | * - point generation |
---|
16 | * |
---|
17 | */ |
---|
18 | |
---|
19 | // Check if Rho array is available |
---|
20 | int volume_points; |
---|
21 | int r_points; |
---|
22 | int ptsGenerated; |
---|
23 | |
---|
24 | double r_step; |
---|
25 | double vol; |
---|
26 | double bin_width; |
---|
27 | double twopiq; |
---|
28 | double tmp; |
---|
29 | |
---|
30 | // These should be parameters |
---|
31 | vol = 4.0*acos(-1.0)/3.0*pars->radius*pars->radius*pars->radius; |
---|
32 | |
---|
33 | |
---|
34 | r_points = pars->calcPars.r_points; |
---|
35 | volume_points = pars->calcPars.volume_points; |
---|
36 | |
---|
37 | if(pars->calcPars.isPointMemAllocated==0) { |
---|
38 | |
---|
39 | // Call modelCalc function here to init_volume |
---|
40 | |
---|
41 | twopiq = (2*acos(-1.0)*pars->qmax); |
---|
42 | tmp = twopiq*twopiq*twopiq*vol; |
---|
43 | volume_points = (int) floor(tmp); |
---|
44 | //r_points = (int) floor(10.0*pow(tmp,0.3333)); |
---|
45 | r_points = (int) floor(pow(tmp,0.3333)); |
---|
46 | printf("v, r = %d, %d\n",volume_points, r_points); |
---|
47 | |
---|
48 | // TEST |
---|
49 | volume_points = 1000; |
---|
50 | r_points = 1000; |
---|
51 | |
---|
52 | pars->calcPars.volume_points = volume_points; |
---|
53 | pars->calcPars.r_points = r_points; |
---|
54 | |
---|
55 | // Memory allocation |
---|
56 | pars->calcPars.points = (SpacePoint*)malloc(volume_points*sizeof(SpacePoint)); |
---|
57 | if(pars->calcPars.points==NULL) { |
---|
58 | printf("Problem allocating memory for 1D volume points\n"); |
---|
59 | return -1.0; |
---|
60 | } |
---|
61 | pars->calcPars.isPointMemAllocated=1; |
---|
62 | } |
---|
63 | |
---|
64 | if(pars->calcPars.isRhoAvailable==0) { |
---|
65 | // Generate random points accross the volume |
---|
66 | ptsGenerated = testsphere_generatePoints(pars->calcPars.points, volume_points, pars->radius); |
---|
67 | |
---|
68 | // Consistency check |
---|
69 | if(ptsGenerated <= 0) { |
---|
70 | // Set error code here |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | // Allocate memory |
---|
75 | |
---|
76 | pars->calcPars.rho = (double*) malloc(r_points*sizeof(double)); |
---|
77 | if(pars->calcPars.rho==NULL){ |
---|
78 | printf("Problem allocating memory for 1D correlation points\n"); |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | // Lump all this in modelCalc function |
---|
84 | |
---|
85 | bin_width = 2.0*pars->radius/r_points; |
---|
86 | |
---|
87 | if(modelcalculations_calculatePairCorrelation_1D(pars->calcPars.points, volume_points, pars->calcPars.rho, r_points, bin_width)<=0){ |
---|
88 | printf("Error occured!\n"); |
---|
89 | return 0; |
---|
90 | }; |
---|
91 | pars->calcPars.isRhoAvailable=1; |
---|
92 | } |
---|
93 | |
---|
94 | // Calculate I(q,phi) and return that value |
---|
95 | r_step = 2.0*pars->radius/((double)(r_points)); |
---|
96 | |
---|
97 | return modelcalculations_calculateIq_1D(pars->calcPars.rho, r_points, r_step, q) * vol; |
---|
98 | |
---|
99 | } |
---|
100 | /// 1D scattering function |
---|
101 | double testsphere_analytical_2D_3Darray(TestSphereParameters *pars, double q, double phi) { |
---|
102 | // Check if Rho array is available |
---|
103 | int volume_points; |
---|
104 | int r_points; |
---|
105 | int ptsGenerated; |
---|
106 | double bin_width; |
---|
107 | double r_step; |
---|
108 | double vol; |
---|
109 | |
---|
110 | // These should be parameters |
---|
111 | volume_points = 1000; |
---|
112 | r_points = 10; |
---|
113 | |
---|
114 | if(pars->calcPars.isPointMemAllocated_2D==0) { |
---|
115 | pars->calcPars.points_2D = (SpacePoint*)malloc(volume_points*sizeof(SpacePoint)); |
---|
116 | if(pars->calcPars.points_2D==NULL) { |
---|
117 | printf("Problem allocating memory for 2D volume points\n"); |
---|
118 | return -1.0; |
---|
119 | } |
---|
120 | pars->calcPars.isPointMemAllocated_2D=1; |
---|
121 | } |
---|
122 | |
---|
123 | //r_step = 2.0*pars->radius/((double)(r_points)); |
---|
124 | // Allow negative values.... |
---|
125 | r_step = 4.0*pars->radius/((double)(r_points)); |
---|
126 | |
---|
127 | if(pars->calcPars.isRhoAvailable_2D==0) { |
---|
128 | // Initialize random number generator |
---|
129 | int seed; |
---|
130 | seed = 10000; |
---|
131 | srand(seed); |
---|
132 | |
---|
133 | // Generate random points accross the volume |
---|
134 | ptsGenerated = testsphere_generatePoints(pars->calcPars.points_2D, volume_points, pars->radius); |
---|
135 | |
---|
136 | // Calculate correlation function |
---|
137 | pars->calcPars.rho_2D = (float*) malloc(r_points*r_points*r_points*sizeof(float)); |
---|
138 | if(pars->calcPars.rho_2D==NULL){ |
---|
139 | printf("Problem allocating memory for 2D correlations points\n"); |
---|
140 | return -1.0; |
---|
141 | } |
---|
142 | if(modelcalculations_calculatePairCorrelation_2D_3Darray(pars->calcPars.points_2D, volume_points, pars->calcPars.rho_2D, r_points, r_step)==NULL){ |
---|
143 | return 0; |
---|
144 | }; |
---|
145 | pars->calcPars.isRhoAvailable_2D=1; |
---|
146 | } |
---|
147 | // Calculate I(q,phi) and return that value |
---|
148 | vol = 4.0*acos(-1.0)/3.0*pars->radius*pars->radius*pars->radius; |
---|
149 | |
---|
150 | //printf("in ana_2D %f %f\n",q, phi); |
---|
151 | return modelcalculations_calculateIq_2D_3Darray(pars->calcPars.rho_2D, r_points, r_step, q, phi)*vol; |
---|
152 | |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | /// 1D scattering function |
---|
157 | double testsphere_analytical_2D(TestSphereParameters *pars, double q, double phi) { |
---|
158 | // Check if Rho array is available |
---|
159 | int volume_points; |
---|
160 | int r_points; |
---|
161 | int ptsGenerated; |
---|
162 | double bin_width; |
---|
163 | double r_step; |
---|
164 | double vol; |
---|
165 | |
---|
166 | // These should be parameters |
---|
167 | //r_points = pars->calcPars.r_points; |
---|
168 | //volume_points = pars->calcPars.volume_points; |
---|
169 | volume_points = 5000; |
---|
170 | r_points = 100; |
---|
171 | |
---|
172 | if(pars->calcPars.isPointMemAllocated_2D==0) { |
---|
173 | //volume_points = 2000; |
---|
174 | //r_points = 100; |
---|
175 | //pars->calcPars.volume_points = volume_points; |
---|
176 | //pars->calcPars.r_points = r_points; |
---|
177 | pars->calcPars.points_2D = (SpacePoint*)malloc(volume_points*sizeof(SpacePoint)); |
---|
178 | if(pars->calcPars.points_2D==NULL) { |
---|
179 | printf("Problem allocating memory for 2D volume points\n"); |
---|
180 | return -1.0; |
---|
181 | } |
---|
182 | pars->calcPars.isPointMemAllocated_2D=1; |
---|
183 | } |
---|
184 | |
---|
185 | r_step = 2.0*pars->radius/((double)(r_points)); |
---|
186 | |
---|
187 | if(pars->calcPars.isRhoAvailable_2D==0) { |
---|
188 | // Initialize random number generator |
---|
189 | int seed; |
---|
190 | seed = 10000; |
---|
191 | srand(seed); |
---|
192 | |
---|
193 | // Generate random points accross the volume |
---|
194 | ptsGenerated = testsphere_generatePoints(pars->calcPars.points_2D, volume_points, pars->radius); |
---|
195 | |
---|
196 | // Calculate correlation function |
---|
197 | pars->calcPars.rho_2D = (float*) malloc(r_points*r_points*sizeof(float)); |
---|
198 | if(pars->calcPars.rho_2D==NULL){ |
---|
199 | printf("Problem allocating memory for 2D correlations points\n"); |
---|
200 | return -1.0; |
---|
201 | } |
---|
202 | if(modelcalculations_calculatePairCorrelation_2D(pars->calcPars.points_2D, volume_points, pars->calcPars.rho_2D, r_points, r_step)==NULL){ |
---|
203 | //if(modelcalculations_calculatePairCorrelation_2D_vector(pars->calcPars.points_2D, volume_points, pars->calcPars.rho_2D, r_points, r_step,0.0,0.0,0.0)==NULL){ |
---|
204 | return 0; |
---|
205 | }; |
---|
206 | pars->calcPars.isRhoAvailable_2D=1; |
---|
207 | } |
---|
208 | // Calculate I(q,phi) and return that value |
---|
209 | vol = 4.0*acos(-1.0)/3.0*pars->radius*pars->radius*pars->radius; |
---|
210 | |
---|
211 | //printf("in ana_2D %f %f\n",q, phi); |
---|
212 | return modelcalculations_calculateIq_2D(pars->calcPars.rho_2D, r_points, r_step, q, phi)*vol; |
---|
213 | |
---|
214 | } |
---|
215 | |
---|
216 | /// 1D scattering function |
---|
217 | double testsphere_numerical_1D(TestSphereParameters *pars, int *array, double q) { return 0;} |
---|
218 | |
---|
219 | /// 2D scattering function |
---|
220 | double testsphere_numerical_2D(TestSphereParameters *pars, int *array, double q, double phi) {return 0;} |
---|
221 | |
---|
222 | /** |
---|
223 | * Generate points randomly accross the volume |
---|
224 | * @param points [SpacePoint*] Array of 3D points to be filled |
---|
225 | * @param n [int] Number of points to generat |
---|
226 | * @param radius [double] Radius of the sphere |
---|
227 | * @return Number of points generated |
---|
228 | */ |
---|
229 | int testsphere_generatePoints(SpacePoint * points, int n, double radius) { |
---|
230 | int i; |
---|
231 | int testcounter; |
---|
232 | double x, y, z; |
---|
233 | |
---|
234 | // Create points |
---|
235 | // To have a uniform density, you want to generate |
---|
236 | // random points in a box and keep only those that are |
---|
237 | // within the volume. |
---|
238 | |
---|
239 | // Initialize random number generator |
---|
240 | int seed; |
---|
241 | seed = 10000; |
---|
242 | srand(seed); |
---|
243 | |
---|
244 | testcounter = 0; |
---|
245 | |
---|
246 | memset(points,0,n*sizeof(SpacePoint)); |
---|
247 | for(i=0;i<n;i++) { |
---|
248 | // Generate in a box centered around zero |
---|
249 | x = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
250 | y = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
251 | z = (2.0*((double)rand())/((double)(RAND_MAX)+(double)(1))-1.0) * radius; |
---|
252 | |
---|
253 | // reject those that are not within the volume |
---|
254 | if( sqrt(x*x+y*y+z*z) <= radius ) { |
---|
255 | points[i].x = x; |
---|
256 | points[i].y = y; |
---|
257 | points[i].z = z; |
---|
258 | testcounter++; |
---|
259 | } else { |
---|
260 | i--; |
---|
261 | } |
---|
262 | } |
---|
263 | //printf("test counter = %d\n", testcounter); |
---|
264 | |
---|
265 | // Consistency check |
---|
266 | if(testcounter != n) { |
---|
267 | return -1; |
---|
268 | } |
---|
269 | |
---|
270 | return testcounter; |
---|
271 | } |
---|