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
|
/*******************************************************************************
*
* McStas, neutron ray-tracing package
* Copyright(C) 2000 Risoe National Laboratory.
*
* %I
* Written by: Kim Lefmann
* Date: 04.02.04
* Origin: Risoe
* Modified by: MB, 15.01.24 (removed extra K2V factor in weight, reduced scattered intensity significantly)
*
* A sample for phonon scattering based on cross section expressions from Squires, Ch.3.
* Possibility for adding an (unphysical) bandgap.
*
* %D
* Single-cylinder shape.
* Absorption included.
* No multiple scattering.
* No incoherent scattering emitted.
* No attenuation from coherent scattering. No Bragg scattering.
* fcc crystal n.n. interactions only
* One phonon branch only -> phonon polarization not accounted for.
* Bravais lattice only. (i.e. just one atom per unit cell)
*
* Algorithm:
* 0. Always perform the scattering if possible (otherwise ABSORB)
* 1. Choose direction within a focusing solid angle
* 2. Calculate the zeros of (E_i-E_f-hbar omega(kappa)) as a function of k_f
* 3. Choose one value of k_f (always at least one is possible!)
* 4. Perform the correct weight transformation
*
* %P
* INPUT PARAMETERS:
* radius: [m] Outer radius of sample in (x,z) plane
* yheight: [m] Height of sample in y direction
* sigma_abs: [barns] Absorption cross section at 2200 m/s per atom
* sigma_inc: [barns] Incoherent scattering cross section per atom
* a: [AA] fcc Lattice constant
* b: [fm] Scattering length
* M: [a.u.] Atomic mass
* c: [meV/AA^(-1)] Velocity of sound
* DW: [1] Debye-Waller factor
* T: [K] Temperature
* focus_r: [m] Radius of sphere containing target.
* focus_xw: [m] horiz. dimension of a rectangular area
* focus_yh: [m] vert. dimension of a rectangular area
* focus_aw: [deg] horiz. angular dimension of a rectangular area
* focus_ah: [deg] vert. angular dimension of a rectangular area
* target_x: [m] position of target to focus at . Transverse coordinate
* target_y: [m] position of target to focus at. Vertical coordinate
* target_z: [m] position of target to focus at. Straight ahead.
* target_index: [1] relative index of component to focus at, e.g. next is +1
* gap: [meV] Bandgap energy (unphysical)
*
* CALCULATED PARAMETERS:
* V_rho: [AA^-3] Atomic density
* V_my_s: [m^-1] Attenuation factor due to incoherent scattering
* V_my_a_v: [m^-1] Attenuation factor due to absorbtion
*
* %L
* The test/example instrument <a href="../examples/Test_Phonon.instr">Test_Phonon.instr</a>.
*
* %E
******************************************************************************/
DEFINE COMPONENT Phonon_simple
SETTING PARAMETERS (radius,yheight,sigma_abs,sigma_inc,a,b,M,c,DW,T,
target_x=0, target_y=0, target_z=0, int target_index=0,focus_r=0,focus_xw=0,focus_yh=0,focus_aw=0,focus_ah=0, gap=0)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */
SHARE
%{
#ifndef PHONON_SIMPLE
#define PHONON_SIMPLE $Revision$
#define T2E (1/11.605) /* Kelvin to meV */
#pragma acc routine
double nbose(double omega, double T) /* Other name ?? */
{
double nb;
nb= (omega>0) ? 1+1/(exp(omega/(T*T2E))-1) : 1/(exp(-omega/(T*T2E))-1);
return nb;
}
#undef T2E
/* Routine types from Numerical Recipies book */
#define UNUSED (-1.11e30)
#define MAXRIDD 60
void fatalerror_cpu(char *s)
{
fprintf(stderr,"%s \n",s);
exit(1);
}
#pragma acc routine
void fatalerror(char *s)
{
#ifndef OPENACC
fatalerror_cpu(s);
#endif
}
#pragma acc routine
double omega_q(double* parms)
{
/* dispersion in units of meV */
double vi, vf, vv_x, vv_y, vv_z, vi_x, vi_y, vi_z;
double q, qx, qy, qz, Jq, res_phonon, res_neutron;
double ah, a, c;
double gap;
vf=parms[0];
vi=parms[1];
vv_x=parms[2];
vv_y=parms[3];
vv_z=parms[4];
vi_x=parms[5];
vi_y=parms[6];
vi_z=parms[7];
a =parms[8];
c =parms[9];
gap =parms[10];
ah=a/2.0;
qx=V2K*(vi_x-vf*vv_x);
qy=V2K*(vi_y-vf*vv_y);
qz=V2K*(vi_z-vf*vv_z);
q=sqrt(qx*qx+qy*qy+qz*qz);
Jq=2*(cos(ah*(qx+qy))+cos(ah*(qx-qy))+cos(ah*(qx+qz))+cos(ah*(qx-qz))
+cos(ah*(qy+qz))+cos(ah*(qy-qz)) );
if (gap>0) {
res_phonon=sqrt(gap*gap+(12-Jq)*(c*c)/(a*a));
} else {
res_phonon=c/a*sqrt(12-Jq);
}
res_neutron = fabs(VS2E*(vi*vi-vf*vf));
return (res_phonon - res_neutron);
}
double zridd(double (*func)(double*), double x1, double x2, double *parms, double xacc)
{
int j;
double ans, fh, fl, fm, fnew, s, xh, xl, xm, xnew;
parms[0]=x1;
fl=(*func)(parms);
parms[0]=x2;
fh=(*func)(parms);
if (fl*fh >= 0)
{
if (fl==0) return x1;
if (fh==0) return x2;
return UNUSED;
}
else
{
xl=x1;
xh=x2;
ans=UNUSED;
for (j=1; j<MAXRIDD; j++)
{
xm=0.5*(xl+xh);
parms[0]=xm;
fm=(*func)(parms);
s=sqrt(fm*fm-fl*fh);
if (s == 0.0)
return ans;
xnew=xm+(xm-xl)*((fl >= fh ? 1.0 : -1.0)*fm/s);
if (fabs(xnew-ans) <= xacc)
return ans;
ans=xnew;
parms[0]=ans;
fnew=(*func)(parms);
if (fnew == 0.0) return ans;
if (fabs(fm)*SIGN(fnew) != fm)
{
xl=xm;
fl=fm;
xh=ans;
fh=fnew;
}
else
if (fabs(fl)*SIGN(fnew) != fl)
{
xh=ans;
fh=fnew;
}
else
if(fabs(fh)*SIGN(fnew) != fh)
{
xl=ans;
fl=fnew;
}
else
fatalerror("never get here in zridd");
if (fabs(xh-xl) <= xacc)
return ans;
}
fatalerror("zridd exceeded maximum iterations");
}
return 0.0; /* Never get here */
}
#pragma acc routine
double zridd_gpu(double x1, double x2, double *parms, double xacc)
{
int j;
double ans, fh, fl, fm, fnew, s, xh, xl, xm, xnew;
parms[0]=x1;
fl=omega_q(parms);
parms[0]=x2;
fh=omega_q(parms);
if (fl*fh >= 0)
{
if (fl==0) return x1;
if (fh==0) return x2;
return UNUSED;
}
else
{
xl=x1;
xh=x2;
ans=UNUSED;
for (j=1; j<MAXRIDD; j++)
{
xm=0.5*(xl+xh);
parms[0]=xm;
fm=omega_q(parms);
s=sqrt(fm*fm-fl*fh);
if (s == 0.0)
return ans;
xnew=xm+(xm-xl)*((fl >= fh ? 1.0 : -1.0)*fm/s);
if (fabs(xnew-ans) <= xacc)
return ans;
ans=xnew;
parms[0]=ans;
fnew=omega_q(parms);
if (fnew == 0.0) return ans;
if (fabs(fm)*SIGN(fnew) != fm)
{
xl=xm;
fl=fm;
xh=ans;
fh=fnew;
}
else
if (fabs(fl)*SIGN(fnew) != fl)
{
xh=ans;
fh=fnew;
}
else
if(fabs(fh)*SIGN(fnew) != fh)
{
xl=ans;
fl=fnew;
}
else
fatalerror("never get here in zridd");
if (fabs(xh-xl) <= xacc)
return ans;
}
fatalerror("zridd exceeded maximum iterations");
}
return 0.0; /* Never get here */
}
#define ROOTACC 1e-8
int findroots(double brack_low, double brack_mid, double brack_high, double *list, int* index, double (*f)(double*), double *parms)
{
double root,range=brack_mid-brack_low;
int i, steps=100;
for (i=0; i<steps; i++)
{
root = zridd(f, brack_low+range*i/(int)steps,
brack_low+range*(i+1)/(int)steps,
(double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
}
}
root = zridd(f, brack_mid, brack_high, (double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
}
}
#pragma acc routine
int findroots_gpu(double brack_low, double brack_mid, double brack_high, double *list, int* index, double *parms)
{
double root,range=brack_mid-brack_low;
int i, steps=100;
for (i=0; i<steps; i++)
{
root = zridd_gpu(brack_low+range*i/(int)steps,
brack_low+range*(i+1)/(int)steps,
(double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
}
}
root = zridd_gpu(brack_mid, brack_high, (double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
}
}
#undef UNUSED
#undef MAXRIDD
#endif
%}
DECLARE
%{
double V_rho;
double V_my_s;
double V_my_a_v;
double DV;
%}
INITIALIZE
%{
V_rho = 4/(a*a*a);
V_my_s = (V_rho * 100 * sigma_inc);
V_my_a_v = (V_rho * 100 * sigma_abs * 2200);
DV = 0.001; /* Velocity change used for numerical derivative */
/* now compute target coords if a component index is supplied */
if (!target_index && !target_x && !target_y && !target_z) target_index=1;
if (target_index){
Coords ToTarget;
ToTarget = coords_sub(POS_A_COMP_INDEX(INDEX_CURRENT_COMP+target_index),POS_A_CURRENT_COMP);
ToTarget = rot_apply(ROT_A_CURRENT_COMP, ToTarget);
coords_get(ToTarget, &target_x, &target_y, &target_z);
}
if (!(target_x || target_y || target_z)) {
printf("Phonon_simple: %s: The target is not defined. Using direct beam (Z-axis).\n",
NAME_CURRENT_COMP);
target_z=1;
}
%}
TRACE
%{
double t0, t1; /* Entry/exit time for cylinder */
double v_i, v_f; /* Neutron velocities: initial, final */
double vx_i, vy_i, vz_i; /* Neutron initial velocity vector */
double dt0, dt; /* Flight times through sample */
double l_full; /* Flight path length for non-scattered neutron */
double l_i, l_o; /* Flight path lenght in/out for scattered neutron */
double my_a_i; /* Initial attenuation factor */
double my_a_f; /* Final attenuation factor */
double solid_angle; /* Solid angle of target as seen from scattering point */
double aim_x=0, aim_y=0, aim_z=1; /* Position of target relative to scattering point */
double kappa_x, kappa_y, kappa_z; /* Scattering vector */
double kappa2; /* Square of the scattering vector */
double bose_factor; /* Calculated value of the Bose factor */
double omega; /* energy transfer */
int nf, index; /* Number of allowed final velocities */
double vf_list[2]; /* List of allowed final velocities */
double J_factor; /* Jacobian from delta fnc.s in cross section */
double f1, f2; /* probed values of omega_q minus omega */
double p1,p2,p3,p4,p5; /* temporary multipliers */
double parms[11];
if(cylinder_intersect(&t0, &t1, x, y, z, vx, vy, vz, radius, yheight))
{
if(t0 < 0)
ABSORB; /* Neutron came from the sample or begins inside */
/* Neutron enters at t=t0. */
dt0 = t1-t0; /* Time in sample */
v_i = sqrt(vx*vx + vy*vy + vz*vz);
l_full = v_i * dt0; /* Length of path through sample if not scattered */
dt = rand01()*dt0; /* Time of scattering (relative to t0) */
l_i = v_i*dt; /* Penetration in sample at scattering */
vx_i=vx;
vy_i=vy;
vz_i=vz;
PROP_DT(dt+t0); /* Point of scattering */
aim_x = target_x-x; /* Vector pointing at target (e.g. analyzer) */
aim_y = target_y-y;
aim_z = target_z-z;
if(focus_aw && focus_ah) {
randvec_target_rect_angular(&vx, &vy, &vz, &solid_angle,
aim_x, aim_y, aim_z, focus_aw, focus_ah, ROT_A_CURRENT_COMP);
} else if(focus_xw && focus_yh) {
randvec_target_rect(&vx, &vy, &vz, &solid_angle,
aim_x, aim_y, aim_z, focus_xw, focus_yh, ROT_A_CURRENT_COMP);
} else {
randvec_target_sphere(&vx,&vy,&vz,&solid_angle,aim_x,aim_y,aim_z, focus_r);
}
NORM(vx, vy, vz);
nf=0;
parms[0]=-1;
parms[1]=v_i;
parms[2]=vx;
parms[3]=vy;
parms[4]=vz;
parms[5]=vx_i;
parms[6]=vy_i;
parms[7]=vz_i;
parms[8]=a;
parms[9]=c;
parms[10]=gap;
#ifndef OPENACC
findroots(0, v_i, v_i+2*c*V2K/VS2E, vf_list, &nf, omega_q, parms);
#else
findroots_gpu(0, v_i, v_i+2*c*V2K/VS2E, vf_list, &nf, parms);
#endif
index=(int)floor(rand01()*nf);
if (index<2) {
v_f=vf_list[index];
parms[0]=v_f-DV;
f1=omega_q(parms);
parms[0]=v_f+DV;
f2=omega_q(parms);
J_factor = fabs(f2-f1)/(2*DV);
omega=VS2E*(v_i*v_i-v_f*v_f);
vx *= v_f;
vy *= v_f;
vz *= v_f;
kappa_x=V2K*(vx_i-vx);
kappa_y=V2K*(vy_i-vy);
kappa_z=V2K*(vz_i-vz);
kappa2=kappa_z*kappa_z+kappa_y*kappa_y+kappa_x*kappa_x;
if(!cylinder_intersect(&t0, &t1, x, y, z, vx, vy, vz, radius, yheight))
{
/* ??? did not hit cylinder */
printf("FATAL ERROR: Did not hit cylinder from inside.\n");
exit(1);
}
dt = t1;
l_o = v_f*dt;
my_a_i = V_my_a_v/v_i;
my_a_f = V_my_a_v/v_f;
bose_factor=nbose(omega,T);
p1 = exp(-(V_my_s*(l_i+l_o)+my_a_i*l_i+my_a_f*l_o)); /* Absorption factor */
p2 = nf*solid_angle*l_full*V_rho/(4*PI); /* Focusing factors; assume random choice of n_f possibilities */
p3 = (v_f/v_i)*DW*(kappa2*K2V*K2V*VS2E)/fabs(omega)*bose_factor; /* Cross section factor 1 */
p4 = 2*VS2E*v_f/J_factor; /* Jacobian of delta functions in cross section */
p5 = b*b/M; /* Cross section factor 2 */
p *= p1*p2*p3*p4*p5;
} else {
ABSORB; // findroots returned junk
}
} /* else transmit: Neutron did not hit the sample */
%}
MCDISPLAY
%{
circle("xz", 0, yheight/2.0, 0, radius);
circle("xz", 0, -yheight/2.0, 0, radius);
line(-radius, -yheight/2.0, 0, -radius, +yheight/2.0, 0);
line(+radius, -yheight/2.0, 0, +radius, +yheight/2.0, 0);
line(0, -yheight/2.0, -radius, 0, +yheight/2.0, -radius);
line(0, -yheight/2.0, +radius, 0, +yheight/2.0, +radius);
%}
END
|