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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/****************************************************************************
*
* McStas, neutron ray-tracing package
* Copyright 1997-2003, All rights reserved
* Risoe National Laboratory, Roskilde, Denmark
* Institut Laue Langevin, Grenoble, France
*
* Component: Pol_bender
*
* %I
* Written by: Peter Christiansen
* Date: August 2006
* Origin: RISOE
*
* Polarising bender.
*
* %D
* Based on Guide_curved written by Ross Stewart.
* Models a rectangular curved guide tube with entrance centered on the Z axis.
* The entrance lies in the X-Y plane. Draws a true depiction
* of the guide with multiple slits (but without spacers), and trajectories.
* It relies on similar physics as the Monochromator_pol.
* The reflec function and parameters are passed to this component to
* give a bigger freedom.
* The up direction is hardcoded to be along the y-axis (0, 1, 0)
*
* The guide is asummed to have half a spacer on each side:
* slit1 slit2 slit3
* |+ ++ ++ +|
* |+ ++ ++ +|
* <---------------------> xwidth
* <------> xwidth/nslit (nslit=3)
* <> d
*
* The reflection functions and parameters defaults as follows:
* Bot defaults to Top, Left defaults to Top, Right defaults to left.
* Down defaults to down and up defaults to up for all functions and
* Top(Up and Down) defaults to StdReflecFunc and {0.99,0.0219,6.07,2.0,0.003}
* which stands for {R0, Qc, alpha, m, W}.
*
* Example:
* Pol_bender(xwidth = 0.08, yheight = 0.08, length = 1.0, radius= 10.0,
* nslit=5, d=0.0, endFlat=0, drawOption=2,
* rTopUpPar={0.99, 0.0219, 6.07, 3.0, 0.003},
* rTopDownPar={0.99, 0.0219, 6.07, 1.0, 0.003})
*
* See also the example instruments Test_Pol_Bender and
* Test_Pol_Bender_Vs_Guide_Curved (under tests).
*
* %BUGS
* This component has been against tested Guide_curved and found to
* give the same intensities. Gravity option has not been tested.
*
* GRAVITY: YES (when gravity is along y-axis)
*
* %P
* INPUT PARAMETERS:
*
* xwidth: [m] Width at the guide entry
* yheight: [m] Height at the guide entry
* length: [m] length of guide along center
* radius: [m] Radius of curvature of the guide (+:curve left/-:right)
* G: [m/s^2] Gravitational constant
* nslit: [1] Number of slits
* d: [m] Width of spacers (subdividing absorbing walls)
* endFlat: [1] If endflat>0 then entrance and exit planes are parallel.
* rTopUpPar: [1] Top mirror Parameters for spin up standard reflectivity function
* rTopDownPar: [1] Top mirror Parameters for spin down standard reflectivity function
* rBotUpPar: [1] Bottom mirror Parameters for spin up standard reflectivity function
* rBotDownPar: [1] Bottom mirror Parameters for spin down standard reflectivity function
* rLeftUpPar: [1] Left mirror Parameters for spin up standard reflectivity function
* rLeftDownPar: [1] Left mirror Parameters for spin down standard reflectivity function
* rRightUpPar: [1] Right mirror Parameters for spin up standard reflectivity function
* rRightDownPar: [1] Right mirror Parameters for spin down standard reflectivity function
* rTopUpData: [1] Reflectivity file for top mirror, spin up
* rTopDownData: [1] Reflectivity file for top mirror, spin down
* rBotUpData: [1] Reflectivity file for bottom mirror, spin up
* rBotDownData: [1] Reflectivity file for bottom mirror, spin down
* rLeftUpData: [1] Reflectivity file for left mirror, spin up
* rLeftDownData: [1] Reflectivity file for left mirror, spin down
* rRightUpData: [1] Reflectivity file for right mirror, spin up
* rRightDownData: [1] Reflectivity file for right mirror, spin down
* drawOption: [1] 1: fine(all slits/90 points per arc), 2: normal (max 20/40), 3: rough (max 5/10)
* debug: [1] if debug > 0 print out some internal parameters
*
* CALCULATED PARAMETERS:
*
* localG: [m/s/s] Gravity vector in guide reference system
* normalXXX: [1] Several normal vector used for defining the geometry
* pointXXX: [1] Several points used for defining the geometry
* rXXXParPtr: [] Pointers to reflection parameters used with ref. functions.
*
* %L
*
* %E
*******************************************************************************/
DEFINE COMPONENT Pol_bender
SETTING PARAMETERS (xwidth, yheight, length, radius, G=9.8, int nslit=1, d=0.0, int debug=0, int endFlat=0,
vector rTopUpPar={0.99,0.0219,6.07,2.0,0.003},
vector rTopDownPar={0.99,0.0219,6.07,2.0,0.003},
vector rBotUpPar={0.99,0.0219,6.07,2.0,0.003},
vector rBotDownPar={0.99,0.0219,6.07,2.0,0.003},
vector rLeftUpPar={0.99,0.0219,6.07,2.0,0.003},
vector rLeftDownPar={0.99,0.0219,6.07,2.0,0.003},
vector rRightUpPar={0.99,0.0219,6.07,2.0,0.003},
vector rRightDownPar={0.99,0.0219,6.07,2.0,0.003},
string rTopUpData="", string rTopDownData="",string rBotUpData="",string rBotDownData="",
string rLeftUpData="", string rLeftDownData="",string rRightUpData="",string rRightDownData="",
int drawOption=1)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */
SHARE
%{
%include "pol-lib"
%include "ref-lib"
%}
DECLARE
%{
Coords localG;
Coords normTopBot;
Coords normIn;
Coords normOut;
Coords pointTop;
Coords pointBot;
Coords pointIn;
Coords pointOut;
t_Table rTopUpTable;
t_Table rTopDownTable;
t_Table rBotUpTable;
t_Table rBotDownTable;
t_Table rLeftUpTable;
t_Table rLeftDownTable;
t_Table rRightUpTable;
t_Table rRightDownTable;
int useTables;
%}
INITIALIZE
%{
double angle;
if(strlen(rTopUpData) && strcmp(rTopUpData,"NULL")){
useTables=1;
/*if rUpTopData is set assume we'll be usning tabled data for all reflectivities*/
if (Table_Read(&rTopUpTable, rTopUpData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rTopUpData);
exit(1);
}
if (Table_Read(&rTopDownTable, rTopDownData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rTopDownData);
exit(1);
}
if (Table_Read(&rBotUpTable, rBotUpData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rBotUpData);
exit(1);
}
if (Table_Read(&rBotDownTable, rBotDownData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rBotDownData);
exit(1);
}
if (Table_Read(&rLeftUpTable, rLeftUpData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rLeftUpData);
exit(1);
}
if (Table_Read(&rLeftDownTable, rLeftDownData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rLeftDownData);
exit(1);
}
if (Table_Read(&rRightUpTable, rRightUpData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rRightUpData);
exit(1);
}
if (Table_Read(&rRightDownTable, rRightDownData, 1) <= 0) {
fprintf(stderr,"Pol_bender: %s: can not read file %s\n",NAME_CURRENT_COMP, rRightDownData);
exit(1);
}
}
if ((xwidth<=0) || (yheight <= 0) || (length<=0) || (radius==0)) {
fprintf(stderr, "Pol_bender: %s: NULL or negative length scale!\n"
"ERROR (xwidth,yheight,length, radius). Exiting\n",
NAME_CURRENT_COMP);
exit(1);
}
if (drawOption<1 || drawOption>3) {
fprintf(stderr, "Pol_bender: %s: drawOption %ld not supported. Exiting.\n",
NAME_CURRENT_COMP, drawOption);
exit(1);
}
if (mcgravitation) {
localG = rot_apply(ROT_A_CURRENT_COMP, coords_set(0,-GRAVITY,0));
fprintf(stdout,"Pol_bender %s: Gravity is on!\n",
NAME_CURRENT_COMP);
if (localG.x!=0 || localG.z!=0)
fprintf(stderr,"WARNING: Pol_Bender: %s: "
"This component only gives correct resulta with gravitation,\n"
"when gravity is strictly along the y-axis!\n",
NAME_CURRENT_COMP);
} else
localG = coords_set(0, 0, 0);
// To be able to handle the situation properly where a component of
// the gravity is along the z-axis we also define entrance (in) and
// exit (out) planes
angle = length/radius;
normIn = coords_set(0, 0, 1);
if (endFlat)
normOut = coords_set(0, 0, 1);
else
normOut = coords_set(sin(angle), 0, cos(angle));
pointIn = coords_set(0, 0, 0);
pointOut = coords_set(radius-radius*cos(angle), 0, radius*sin(angle));
// Top and bot plane (+y dir) can be spanned by (1, 0, 0) & (0, 0, 1)
// and the top point (0, yheight/2, 0) and bot point (0, -yheight/2, 0)
// A normal vector is (0, 1, 0)
normTopBot = coords_set(0, 1, 0);
pointTop = coords_set(0, yheight/2, 0);
pointBot = coords_set(0, -yheight/2, 0);
%}
TRACE
%{
const double whalf = 0.5*xwidth; /* half width of guide */
const double hhalf = 0.5*yheight; /* half height of guide */
const double z_off = radius*sin(length/radius); /* z-comp of guide length */
const double dThreshold = 1e-10; /* distance threshold */
const double tThreshold = dThreshold/sqrt(vx*vx + vy*vy + vz*vz);
double angle_z_vout; /* angle between z-axis and v_out */
// Variables used in the case of multiple slits
const double slitWidth = xwidth/nslit; // slitwidth
const double spacerhalf = 0.5*d; /* half width of spacers */
int slitHit; // decide which slit is hit
double posInSlit; // position in slit
double t11, t12, t21, t22, theta, alpha, endtime, phi;
int i_bounce;
int nerr=0;
// Pol variables
double FN, FM, Rup, Rdown, weight;
double Rleft; /* radius of curvature of left mirror */
double Rright; /* radius of curvature of right mirror */
double absR = fabs(radius);
double sign = 1;
if(radius<0)
sign = -1;
/* Propagate neutron to guide entrance. */
PROP_Z0;
if (!inside_rectangle(x, y, xwidth, yheight))
ABSORB;
if(nslit>1) {
// check if neutron is absorbed on a spacer
posInSlit = fmod(x+whalf, slitWidth);
if(posInSlit <= spacerhalf ||
posInSlit >= slitWidth-spacerhalf)
ABSORB;
// check which slat is hit
slitHit = (int)((x+whalf)/slitWidth);
// Modify R1 and R2 according to which slat was hit
Rleft = absR + sign*whalf - sign*(slitHit+1)*slitWidth + sign*spacerhalf;
Rright = absR + sign*whalf - sign*slitHit*slitWidth - sign*spacerhalf;
if(debug>0)
printf("\nslitHit: %d/%f, Rleft: %f, Rright: %f\n",
slitHit, (x+whalf)/slitWidth, Rleft, Rright);
} else { // only 1 slit
Rleft = absR - sign*whalf;
Rright = absR + sign*whalf;
}
for(;;) {
double tLeft, tRight, tTop, tBot, tIn, tOut, tMirror;
double tUp, tSide, time, endtime;
double R, Q;
Coords vVec, xVec;
int isPolarising;
double vel_xz;
isPolarising = 0;
xVec = coords_set(x, y, z);
vVec = coords_set(vx, vy, vz);
solve_2nd_order(&tTop, NULL, 0.5*coords_sp(normTopBot,localG),
coords_sp(normTopBot, vVec),
coords_sp(normTopBot, coords_sub(xVec, pointTop)));
solve_2nd_order(&tBot, NULL, 0.5*coords_sp(normTopBot,localG),
coords_sp(normTopBot, vVec),
coords_sp(normTopBot, coords_sub(xVec, pointBot)));
solve_2nd_order(&tIn, NULL, 0.5*coords_sp(normIn,localG),
coords_sp(normIn, vVec),
coords_sp(normIn, coords_sub(xVec, pointIn)));
solve_2nd_order(&tOut, NULL, 0.5*coords_sp(normOut,localG),
coords_sp(normOut, vVec),
coords_sp(normOut, coords_sub(xVec, pointOut)));
/* Find itersection points with inside and outside guide walls */
if (!cylinder_intersect(&t11, &t12 ,x - radius, y, z, vx, vy, vz, Rleft, 2*yheight)){
/*neutron did not hit the cylinder*/
t11=t12=0;
}
if (!cylinder_intersect(&t21, &t22 ,x - radius, y, z, vx, vy, vz, Rright, 2*yheight)){
/*neutron did not hit the cylinder*/
t21=t22=0;
}
/* Choose appropriate reflection time */
tLeft = (t11 < tThreshold) ? t12 : t11;
tRight = (t21 < tThreshold) ? t22 : t21;
/* Choose appropriate reflection time */
if (tTop>tThreshold && (tTop<tBot || tBot<=tThreshold))
tUp=tTop;
else
tUp=tBot;
if (tLeft>tThreshold && (tLeft<tRight || tRight<=tThreshold))
tSide=tLeft;
else
tSide=tRight;
if (tUp>tThreshold && (tUp<tSide || tSide<=tThreshold))
time=tUp;
else
time=tSide;
if (time<=tThreshold) {
nerr++;
if (nerr < 10) {
fprintf(stdout, "tTop: %e, tBot:%e, tRight: %e, tLeft: %e\n"
"tUp: %e, tSide: %e, time: %e\n",
tTop, tBot, tRight, tLeft, tUp, tSide, time);
} else {
fprintf(stdout, "Found 10 propagation error for this neutron, terminating!\n");
break;
}
}
/* Has neutron left the guide? */
if (tOut>tThreshold && (tOut<tIn || tIn<=tThreshold))
endtime=tOut;
else
endtime=tIn;
if (time > endtime)
break;
PROP_DT(time);
SCATTER;
/* Find reflection surface */
if(time==tSide) { /* Left or right side */
if(time==tLeft)
R = sign*Rleft;
else
R = sign*Rright;
phi = atan(vx/vz); /* angle of neutron trajectory */
alpha = asin(z/R); /* angle of guide wall */
theta = fabs(phi - alpha); /* angle of reflection */
angle_z_vout = 2.0*alpha - phi;
vel_xz = sqrt(vx*vx + vz*vz); /* in plane velocity */
vz = vel_xz*cos(angle_z_vout);
vx = vel_xz*sin(angle_z_vout);
} else { /* Top or Bottom wall */
theta = fabs(atan(vy/vz));
vy = -vy;
}
/* Now compute reflectivity. */
Q = 2.0*sin(theta)*sqrt(vx*vx + vy*vy + vz*vz)*V2K;
// calculate reflection probability
if(time==tTop) {
if(useTables){
Rup=Table_Value(rTopUpTable,Q,1);
Rdown=Table_Value(rTopDownTable,Q,1);
}else{
StdReflecFunc(Q, rTopUpPar, &Rup);
StdReflecFunc(Q, rTopDownPar, &Rdown);
}
if(debug>0)
fprintf(stdout, "\tTop hit:\n");
} else if(time==tBot) {
if(useTables){
Rup=Table_Value(rBotUpTable,Q,1);
Rdown=Table_Value(rBotDownTable,Q,1);
}else{
StdReflecFunc(Q, rBotUpPar, &Rup);
StdReflecFunc(Q, rBotDownPar, &Rdown);
}
if(debug>0)
fprintf(stdout, "\tBot hit:\n");
} else if(time==tRight) {
if(useTables){
Rup=Table_Value(rRightUpTable,Q,1);
Rdown=Table_Value(rRightDownTable,Q,1);
}else{
StdReflecFunc(Q, rRightUpPar, &Rup);
StdReflecFunc(Q, rRightDownPar, &Rdown);
}
if(debug>0)
fprintf(stdout, "\tRight hit:\n");
} else if(time==tLeft) {
if(useTables){
Rup=Table_Value(rLeftUpTable,Q,1);
Rdown=Table_Value(rLeftDownTable,Q,1);
}else{
StdReflecFunc(Q, rLeftUpPar, &Rup);
StdReflecFunc(Q, rLeftDownPar, &Rdown);
}
if(debug>0)
fprintf(stdout, "\tLeft hit:\n");
}
if(Rup != Rdown) {
isPolarising = 1;
GetMonoPolFNFM(Rup, Rdown, &FN, &FM);
GetMonoPolRefProb(FN, FM, sy, &weight);
} else
weight = Rup;
if(debug>0)
printf("\tlambda: %.2f AA, Q: %.4f, Rup: %.4f, Rdown: %.4f,"
" weight: %.4f\n",
2*PI/(sqrt(vx*vx + vy*vy + vz*vz)*V2K), Q,
Rup, Rdown, weight);
// check that refWeight is meaningfull
if (weight <= 0) ABSORB;
if (weight > 1) weight =1 ;
if(isPolarising) {
SetMonoPolRefOut(FN, FM, weight, &sx, &sy, &sz);
if(sx*sx+sy*sy+sz*sz>1.000001)
fprintf(stderr,"Pol_bender: %s: Warning: polarisation |s| = %g > 1\n",
NAME_CURRENT_COMP, sx*sx+sy*sy+sz*sz); // check that polarisation is meaningfull
}
p *= weight;
if(p==0) {
ABSORB;
break;
}
}
%}
MCDISPLAY
%{
double x1, x2, z1, z2;
const int n = 90;
double *xplot=malloc(n*sizeof(double));
double *zplot=malloc(n*sizeof(double));
int ns = 0;
int j = 1;
const double lengthOfGuide = sin(length/radius)*radius;
const double slitWidth = xwidth/nslit;
double R = 0; /* radius of arc */
int nSlitsMax = nslit;
int nMax = n;
if (lengthOfGuide<=0)
exit(fprintf(stdout,"Pol_bender: %s: Negative guide length ! lengthOfGuide=%g\n",
NAME_CURRENT_COMP, lengthOfGuide));
if (drawOption==2) {
if(nSlitsMax>20)
nSlitsMax = 20;
nMax = 40;
} else if (drawOption==3) {
if(nSlitsMax>5)
nSlitsMax = 5;
nMax = 10;
}
// draw opening
rectangle("xy", 0, 0, 0, xwidth, yheight);
for(ns=0; ns < nSlitsMax+1; ns++) {
// to make sure the sides are drawn properly
if(ns==nSlitsMax && nSlitsMax<nslit)
ns=nslit;
// calculate x for this R
R = radius - 0.5*xwidth + ns*slitWidth;
for(j=0; j<nMax; j++) {
if(endFlat) {
if(ns==0) // only calculate once
zplot[j] = j*lengthOfGuide/(double)(nMax-1);
} else
zplot[j] = R*sin(length/radius * (double)j/(double)(nMax-1));
if(radius>0)
xplot[j] = radius - sqrt(R*R - zplot[j]*zplot[j]);
else
xplot[j] = radius + sqrt(R*R - zplot[j]*zplot[j]);
}
// To be able to draw end we store some of the point values
if(ns==0) { // first wall
x1 = xplot[nMax-1];
z1 = zplot[nMax-1];
} else if(ns==nslit) { //last wall
x2 = xplot[nMax-1];
z2 = zplot[nMax-1];
}
for(j=0; j<nMax-1; j++) {
line(xplot[j], 0.5*yheight, zplot[j], xplot[j+1], 0.5*yheight, zplot[j+1]);
line(xplot[j], -0.5*yheight, zplot[j], xplot[j+1], -0.5*yheight, zplot[j+1]);
}
}
// draw end gap
line(x1, 0.5*yheight, z1, x2, 0.5*yheight, z2);
line(x1, 0.5*yheight, z1, x1,-0.5*yheight, z1);
line(x2,-0.5*yheight, z2, x2, 0.5*yheight, z2);
line(x1,-0.5*yheight, z1, x2,-0.5*yheight, z2);
free(xplot);free(zplot);
%}
END
|