[go: up one dir, main page]

Menu

[r377]: / gr_scat / src / scatter.h  Maximize  Restore  History

Download this file

357 lines (284 with data), 10.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <stdio.h>
#include <stdlib.h>
//#include "e:\files2\sci_lib\graph_to.h"
#include "/home/mills/sci_lib/random.h"
/*#include <math.h>*/
#define CIRCLENUMBER 3
#define PI 3.141592654
/*This include file contains the object definitions for a simulation of the three-disc scattering
system*/
class disc {
float rad;
float cx, cy;
public:
void init(float radius, float centerx, float centery);
void intercept (float begx, float begy, float dirx, float diry,
float *t1, float *t2);
int int_near (float begx, float begy, float dirx, float diry, float *t);
int int_far (float begx, float begy, float dirx, float diry, float *t);
void bounce (float interceptx, float intercepty, float dirx, float diry,
float *reflectxhat, float *reflectyhat);
void plot();
};
/************************************************************************/
class Ndisc {
/*Contains several disc objects which compose the scattering system*/
disc alldiscs[CIRCLENUMBER]; /*pointer to array of disc objects*/
disc boundary; /*defines the outer border at which particle
exits the system*/
float (*noise_func)(long *idum);
long idum; /*seed value for randum number generator*/
int Ndisc::intercept(float x, float y, float dirx, float diry, float *t);
public:
void init(int noise_type);
/*initializes object with 3 discs in the usual pattern*/
/*noise_type: 0=gaussian, 1=white*/
void init2 (int noise_type);
/*initializes object same as experimental apparatus*/
void plot_discs();
/*plots the scattering system*/
#if NOISY
void scatter (float *startx1, float *starty1, float *theta, float *tdelay,
long *bounces, float var);
#else
void scatter (float *startx1, float *starty1, float *theta, float *tdelay,
long *bounces);
#endif
/*simulates a point particle moving through the scattering system*/
};
/************************************************************************/
//graph con; /*Defines graphics conversions*/
/************************************************************************/
void disc::init(float radius, float centerx, float centery) {
rad=radius;
cx=centerx;
cy=centery;
}
/*______________________________________________________________________*/
void disc::intercept(float begx, float begy, float dirx, float diry,
float *t1, float *t2)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, find the intercept points between that line and the
circle, and return in the form of the parameters of the line, t1 & t2.
Returns -1 for each parameter if there are no intercepts*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float Atimes2; /* " */
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
Atimes2=2*(dirx*dirx+diry*diry);
discriminant=-pow(dirx*Y-diry*X,2)+pow(dirx*rad,2)+pow(diry*rad,2);
if (discriminant >= 0) {
*t1=(-2*B+2*sqrt(discriminant))/Atimes2;
*t2=(-2*B-2*sqrt(discriminant))/Atimes2;
} else {
*t1=-1;
*t2=-1;
}
}
/*______________________________________________________________________*/
int disc::int_near(float begx, float begy, float dirx, float diry, float *t)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, returns the closest intercept in the form of the
parameter of the line, t.
Returns -1 for each parameter if there are no intercepts*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float Atimes2; /* " */
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
Atimes2=2*(dirx*dirx+diry*diry);
discriminant=-pow(dirx*Y-diry*X,2)+pow(dirx*rad,2)+pow(diry*rad,2);
if (discriminant >= 0) {
*t=(-2*B-2*sqrt(discriminant))/Atimes2;
return 1;
} else {
*t=-1;
return 0;
}
}
/*______________________________________________________________________*/
int disc::int_far(float begx, float begy, float dirx, float diry, float *t)
/*Given the origin of a line, its direction vector, the center of a circle,
its radius vector, returns the farthest intercept in the form of the
parameter of the line, t.
/*Returns 0, *t=-1 if there are no intercepts 1 if there are*/
{
float X; /*intermediate value in calculation*/
float Y; /* " */
float B; /* " */
float Atimes2; /* " */
float discriminant; /*Square root of the discriminant in quadratic*/
X=begx-cx;
Y=begy-cy;
B=dirx*X+diry*Y;
Atimes2=2*(dirx*dirx+diry*diry);
discriminant=-pow(dirx*Y-diry*X,2)+pow(dirx*rad,2)+pow(diry*rad,2);
if (discriminant >= 0) {
*t=(-2*B+2*sqrt(discriminant))/Atimes2;
return 1;
} else {
*t=-1;
return 0;
}
}
/*______________________________________________________________________*/
void disc::bounce(float interceptx, float intercepty, float dirx, float diry,
float *reflectxhat, float *reflectyhat) {
float normx, normy; /*x,y coords of normal to radius vector*/
float reflectlength; /*projection of direction vector on normal*/
normx=intercepty-cy;
normy=cx-interceptx;
reflectlength=(normx*dirx+normy*diry)/(normx*normx+normy*normy);
*reflectxhat=-dirx+2*reflectlength*normx;
*reflectyhat=-diry+2*reflectlength*normy;
}
void disc::plot() {
// con.disc(cx,cy,rad);
}
/**************************************************************************/
void Ndisc::init (int noise_type) {
if (noise_type==1) {
noise_func=&white;
} else {
noise_func=&gasdev;
}
idum=-784521258;
alldiscs[0].init(1,-0.7216878,1.25);
alldiscs[1].init(1,1.4433756,0);
alldiscs[2].init(1,-0.7216878,-1.25);
boundary.init(5,0,0);
}
/*______________________________________________________________________*/
void Ndisc::init2 (int noise_type) {
if (noise_type==1) {
noise_func=&white;
} else {
noise_func=&gasdev;
}
idum=-784521258;
alldiscs[0].init(2.5,-1.847520861,3.2);
alldiscs[1].init(2.5,3.69041723,0);
alldiscs[2].init(2.5,-1.847520861,-3.2);
boundary.init(7,0,0);
}
/*______________________________________________________________________*/
void Ndisc::plot_discs() {
int i;
/* con.window(-3,-2.5,3,2.5);*/
// con.window(-4,-3,4,3);
/* con.window(-9,-7,9,7);*/
/* con.pos(-3,0);
// con.linerel(3,0);
// con.pos(0,-2.5);
// con.linerel(0,2.5);*/
for (i=0;i<=CIRCLENUMBER-1;i++) {
alldiscs[i].plot();
}
}
/*________________________________________________________________________*/
int Ndisc::intercept(float x, float y, float dirx, float diry, float *t) {
/*Calculates the distance to the nearest point where the particle intercepts
a disc. Returns this value in *t. Returns the number of the circle. If
this is greater than CIRCLENUMBER then the particle will exit the system.*/
float ttest; /*test value for distance to intercept*/
float tdum; /*value for intercept which is thrown away*/
int whichcircle; /*the disc which the particle intercepts*/
int a; /*counts through the discs*/
int exit_system; /*particle exits the system*/
exit_system=1;
for (a=0;a<=CIRCLENUMBER-1;a++) {
/* alldiscs[a].intercept (x,y,dirx,diry,&tdum,&ttest);*/
if (alldiscs[a].int_near (x,y,dirx,diry,&ttest)&&(ttest>0.0)) {
if (exit_system) {
*t=ttest;
exit_system=0;
whichcircle=a;
} else if ((*t>ttest)&&(ttest>0.0)) {
*t=ttest;
whichcircle=a;
}
}
}
if (exit_system) {
/* boundary.intercept(x,y,dirx,diry,t,&tdum);*/
if (!boundary.int_far (x,y,dirx,diry,t)) {
fprintf(stderr, "Improper initial conditions");
exit(1);
}
whichcircle=CIRCLENUMBER;
}
return whichcircle;
}
/*______________________________________________________________________*/
void Ndisc::scatter (float *startx, float *starty, float *theta, float *tdelay,
long *bounces
#if NOISY
, float stddev)
#else
)
#endif
/*Given starting coords and angle, find time for particle to exit system
final exit pint is returned in startx1, starty1 while angle at which the
particle exits the system is returned in theta.*/
{
float tmin; /*final value for distance to intercept*/
float interceptx; /*coordinate of interception with disc*/
float intercepty;
float dirxhat; /*momentum (normalized)*/
float diryhat;
float deltatheta; /*random change in reflection angle for noise*/
float cosdel; /*cos of deltatheta*/
float sindel; /*sin " " */
float theta1; /*exit angle*/
int whichcircle;
int a;
float reflectxhat, reflectyhat; /*momentum after reflection*/
dirxhat=cos(*theta);
diryhat=sin(*theta);
*bounces=0;
*tdelay=0;
whichcircle=0;
// con.pos(*startx,*starty);
while (whichcircle!=CIRCLENUMBER) {
whichcircle=intercept(*startx,*starty,dirxhat,diryhat,&tmin);
if (whichcircle==CIRCLENUMBER) {
*startx=(*startx+dirxhat*tmin);
*starty=(*starty+diryhat*tmin);
(*tdelay)+=tmin;
// con.linerel(*startx, *starty);
} else {
(*tdelay)+=tmin;
(*bounces)++;
/* printf ("%5i\n",*bounces);*/
interceptx=(*startx+dirxhat*tmin);
intercepty=(*starty+diryhat*tmin);
alldiscs[whichcircle].bounce(interceptx,intercepty,dirxhat,diryhat,
&reflectxhat,&reflectyhat);
#if NOISY
deltatheta=stddev*(*noise_func)(&idum);
sindel=sin(deltatheta);
cosdel=cos(deltatheta);
dirxhat=reflectxhat*cosdel-reflectyhat*sindel;
diryhat=reflectyhat*sindel+reflectyhat*cosdel;
#else
dirxhat=reflectxhat;
diryhat=reflectyhat;
#endif
*startx=interceptx;
*starty=intercepty;
// con.linerel(*startx,*starty);
}
}
theta1=atan(diryhat/dirxhat);
if (dirxhat<0.0) {*theta=theta1-PI*fabs(theta1)/theta1;}
else {*theta=theta1;}
}