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
|
/*******************************************************************************
*
* McStas, neutron ray-tracing package
* Copyright(C) 2007 Risoe National Laboratory.
*
* %I
* Written by: Anders Komar Ravn, based on template by Mads Bertelsen and Phonon_Simple
* Date: 20.08.15
* Version: $Revision: 0.1 $
* Origin: University of Copenhagen
*
* Port of PhononSimple to Union components
*
* %D
*
* Port of the PhononSimple component from the McStas library to the Union
* components.
*
* Part of the Union components, a set of components that work together and thus
* sperates geometry and physics within McStas.
* The use of this component requires other components to be used.
*
* 1) One specifies a number of processes using process components like this one
* 2) These are gathered into material definitions using Union_make_material
* 3) Geometries are placed using Union_box / Union_cylinder, assigned a material
* 4) A Union_master component placed after all of the above
*
* Only in step 4 will any simulation happen, and per default all geometries
* defined before the master, but after the previous will be simulated here.
*
* There is a dedicated manual available for the Union_components
*
*
* Algorithm:
* Described elsewhere
*
* %P
* INPUT PARAMETERS:
* unit_cell_volume: [AA^3] Unit cell volume
* a: [AA] fcc lattice constant
* c: [meV*AA] Velocity of sound
* M: [units] Nucleus atomic mass in units
* b: [fm] Scattring length
* T: [K] Temperature
* longitudinal: [0/1] Simulate longitudinal branches
* transverse: [0/1] Simulate transverse branches
* packing_factor: [1] How dense is the material compared to optimal 0-1
* interact_fraction: [1] How large a part of the scattering events should use this process 0-1 (sum of all processes in material = 1)
* init: [string] Name of Union_init component (typically "init", default)
* DW: [1] Debye-Waller factor
* CALCULATED PARAMETERS:
* PhononSimple_storage // Important to update this output paramter
* effective_my_scattering // Variable used in initialize
*
* %L
*
* %E
******************************************************************************/
DEFINE COMPONENT PhononSimple_process
SETTING PARAMETERS(packing_factor=1,unit_cell_volume=13.8,interact_fraction=-1,a=4.95,c=10,M=207.2,b=9.4,T=290,DW=1,longitudinal=1,transverse=1, string init="init")
SHARE
%{
#ifndef Union
#error "The Union_init component must be included before this PhononSimple_process component"
#endif
// Very important to add a pointer to this struct in the union-lib.c file
struct PhononSimple_physics_storage_struct{
// Variables that needs to be transfered between any of the following places:
// The initialize in this component
// The function for calculating my
// The function for calculating scattering
double k_init;
double kk_x;
double kk_y;
double kk_z;
double ki_x;
double ki_y;
double ki_z;
// Avoid duplicates of output parameters and setting parameters in naming
double atom_mass_au;
double V_rho;
double my_scattering;
int mc_steps;
double DW_factor;
double k_f[3];
double k_final;
double aa;
double bb;
double cc;
double my_constant;
double Temp;
double solid_angle;
double parms[10];
int enable_longitudinal;
int enable_transverse;
Coords pol_vector;
int pol_mode;
};
/* Ridder's false position algorithm for finding roots. */
#define T2E (1/11.605) /* Kelvin to meV */
double nbose_union(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_union(char *s)
{
fprintf(stderr,"%s \n",s);
exit(1);
}
double zridd_union(double (*func)(double*), double x1, double x2, double *parms, double xacc)
{
int j;
double ans, fh, fl, fm, fnew, s, xh, xl, xm, xnew;
/* printf("zridd called with brackets %g %g acceptance %g \n",x1,x2,xacc);
printf("and %i parameters %g %g %g %g %g \n",Nparms,parms[0],parms[1],parms[2],parms[3], parms[4]); */
parms[0]=x1;
fl=(*func)(parms);
parms[0]=x2;
fh=(*func)(parms);
/* printf("Function values: %g %g \n",fl,fh); */
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_union("never get here in zridd");
if (fabs(xh-xl) <= xacc)
return ans;
}
fatalerror_union("zridd exceeded maximum iterations");
}
return 0.0; /* Never get here */
}
#define ROOTACC 1e-8
int findroots_union(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_union(f, brack_low+range*i/(int)steps,
brack_low+range*(i+1)/(int)steps,
(double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
/* printf("findroots found a low root: %g \n",root); */
}
}
root = zridd_union(f, brack_mid, brack_high, (double *)parms, ROOTACC);
if (root != UNUSED)
{
list[(*index)++]=root;
/* printf("findroots found a high root: %g \n",root); */
}
}
double omega_q_union(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;
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];
ah=a/2.0;
/* printf("omega_q called with parameters vf= %g, vi=%g (%g %g %g) vv=(%g, %g, %g) \n",vf,vi,vi_x,vi_y,vi_z,vv_x,vv_y,vv_z); */
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)) );
res_phonon=c/a*sqrt(12-Jq);
res_neutron = fabs(VS2E*(vi*vi-vf*vf));
/* if (fabs(res_phonon-res_neutron)<1e-3)
printf("in omega_q: q=(%g %g %g), omega_phonon=%g, omega_neutron=%g\n ",
qx,qy,qz,res_phonon,res_neutron); */
/* printf("omega_q returning %g - %g\n",res_phonon,res_neutron); */
return (res_phonon - res_neutron);
}
#undef UNUSED
#undef MAXRIDD
// Function for calculating my, the inverse penetration depth (for only this scattering process).
// The input for this function and its order may not be changed, but the names may be updated.
int PhononSimple_physics_my(double *my, double *k_initial, union data_transfer_union data_transfer, struct focus_data_struct *focus_data, _class_particle *_particle) {
double k_length = sqrt(k_initial[0]*k_initial[0]+k_initial[1]*k_initial[1]+k_initial[2]*k_initial[2]);
//printf("energy = %f \n",k_length*K2V*k_length*K2V*VS2E);
double v_i = k_length * K2V;
double a,b,c,T;
double vf_list[6]; /* List of allowed final velocities */
int nf,index;
double *parms;
double f1,f2,J_factor;
double DV = 0.001; /* Velocity change used for numerical derivative */
double omega;
double v_f;
double k_f[3],k_final;
double kappa2,kappa_x,kappa_y,kappa_z;
double solid_angle;
a=data_transfer.pointer_to_a_PhononSimple_storage_struct->aa;
b=data_transfer.pointer_to_a_PhononSimple_storage_struct->bb;
c=data_transfer.pointer_to_a_PhononSimple_storage_struct->cc;
T=data_transfer.pointer_to_a_PhononSimple_storage_struct->Temp;
Coords k_out;
focus_data->focusing_function(&k_out,&solid_angle,focus_data);
NORM(k_out.x,k_out.y,k_out.z);
data_transfer.pointer_to_a_PhononSimple_storage_struct->solid_angle = solid_angle;
// Avoid allocating the array for each neutron
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[0] = -1;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[1] = v_i;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[2] = k_out.x;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[3] = k_out.y;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[4] = k_out.z;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[5] = k_initial[0]*K2V;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[6] = k_initial[1]*K2V;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[7] = k_initial[2]*K2V;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[8] = a;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[9] = c;
parms = &data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[0];
nf = 0;
findroots_union(0, v_i, v_i+2*c*V2K/VS2E, vf_list, &nf, omega_q_union, parms);
index=(int)floor(rand01()*nf);
if (nf > 4 || index > 3) {
printf("index = %d, nf = %d \n",index, nf);
printf("energy = %f \n",k_length*K2V*k_length*K2V*VS2E);
}
v_f=vf_list[index];
//parms[0]=v_f-DV;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[0]=v_f-DV;
f1=omega_q_union(parms);
//parms[0]=v_f+DV;
data_transfer.pointer_to_a_PhononSimple_storage_struct->parms[0]=v_f+DV;
f2=omega_q_union(parms);
//J_factor = fabs(f2-f1)/(2*DV*K2V);
J_factor = fabs(f2-f1)/(2*DV);
omega=VS2E*(v_i*v_i-v_f*v_f); /* unit of energy */
k_final = v_f * V2K;
k_f[0] = k_out.x * k_final;
k_f[1] = k_out.y * k_final;
k_f[2] = k_out.z * k_final;
kappa_x = k_initial[0]-k_f[0];
kappa_y = k_initial[1]-k_f[1];
kappa_z = k_initial[2]-k_f[2];
kappa2=kappa_z*kappa_z+kappa_y*kappa_y+kappa_x*kappa_x;
double pol_weight_factor;
if (data_transfer.pointer_to_a_PhononSimple_storage_struct->enable_transverse == 1 && data_transfer.pointer_to_a_PhononSimple_storage_struct->enable_longitudinal == 1) {
pol_weight_factor = kappa2;
} else {
// polarization mode
// Find nearest bragg point. FCC: HKL all even or odd.
double h_value,k_value,l_value;
h_value = kappa_x*a*0.5/PI;
k_value = kappa_y*a*0.5/PI;
l_value = kappa_z*a*0.5/PI;
int h_guess,k_guess,l_guess;
h_guess = floor(h_value);
k_guess = floor(k_value);
l_guess = floor(l_value);
int h_check,k_check,l_check;
int nearest_bragg_h,nearest_bragg_k,nearest_bragg_l;
int check_sum;
double dist,min_dist = 99999;
for (h_check=h_guess;h_check<h_guess+2;h_check++) {
for (k_check=k_guess;k_check<k_guess+2;k_check++) {
for (l_check=l_guess;l_check<l_guess+2;l_check++) {
check_sum = h_check % 2 + k_check % 2 + l_check % 2;
if (check_sum == 0 || check_sum==3) {
dist=sqrt((h_value-h_check)*(h_value-h_check)+(k_value-k_check)*(k_value-k_check)+(l_value-l_check)*(l_value*l_check));
if (dist < min_dist) {
min_dist = dist;
nearest_bragg_h = h_check;
nearest_bragg_k = k_check;
nearest_bragg_l = l_check;
}
}
}
}
}
double k_phonon_h,k_phonon_k,k_phonon_l;
k_phonon_h = kappa_x - nearest_bragg_h;
k_phonon_k = kappa_y - nearest_bragg_k;
k_phonon_l = kappa_z - nearest_bragg_l;
NORM(k_phonon_h,k_phonon_k,k_phonon_l);
if (data_transfer.pointer_to_a_PhononSimple_storage_struct->enable_longitudinal == 1) {
pol_weight_factor = (kappa_x*k_phonon_h+kappa_y*k_phonon_k+kappa_z*k_phonon_l)*(kappa_x*k_phonon_h+kappa_y*k_phonon_k+kappa_z*k_phonon_l);
}
if (data_transfer.pointer_to_a_PhononSimple_storage_struct->enable_transverse == 1) {
pol_weight_factor = kappa_x*kappa_x+kappa_y*kappa_y+kappa_z*kappa_z - (kappa_x*k_phonon_h+kappa_y*k_phonon_k+kappa_z*k_phonon_l)*(kappa_x*k_phonon_h+kappa_y*k_phonon_k+kappa_z*k_phonon_l);
}
//Coords pol_vector = data_transfer.pointer_to_a_PhononSimple_storage_struct->pol_vector;
//pol_weight_factor = k_phonon_h*pol_vector.x + k_phonon_k*pol_vector.y + k_phonon_l*pol_vector.z;
/*
// This test shows the predicted behavior where all nearest bragg sets corresponds to Bragg peaks.
printf("--------------------------------------------\n");
printf("hkl input = [%1.3f %1.3f %1.3f] \n",h_value,k_value,l_value);
printf("Nearest bragg = [%d %d %d] \n",nearest_bragg_h,nearest_bragg_k,nearest_bragg_l);
*/
}
//*my = nf*V_rho*(k_final/k_length)*DW*(kappa2*K2V*K2V*VS2E)/fabs(omega)*nbose(omega,T)*2*VS2E*K2V*k_final/J_factor*b*b/M;
// my_constant = V_rho*DW*K2V*K2V*VS2E*2*VS2E*K2V*b*b/M // done in initialize
//*my = (double) nf*k_final/k_length*kappa2/fabs(omega)*nbose_union(omega,T)*k_final/J_factor*data_transfer.pointer_to_a_PhononSimple_storage_struct->my_constant;
*my = (double) nf*k_final/k_length*pol_weight_factor/fabs(omega)*nbose_union(omega,T)*k_final/J_factor*data_transfer.pointer_to_a_PhononSimple_storage_struct->my_constant;
//*my = 10;
data_transfer.pointer_to_a_PhononSimple_storage_struct->k_final = k_final;
data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[0] = k_f[0];
data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[1] = k_f[1];
data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[2] = k_f[2];
// Simple case, just retrive the parameter saved from initialize
return 1;
};
// Function that provides description of a basic scattering event.
// Do not change the
int PhononSimple_physics_scattering(double *k_final, double *k_initial, double *weight, union data_transfer_union data_transfer, struct focus_data_struct *focus_data, _class_particle *_particle) {
// k_final and k_initial are passed as pointers to double vector[3]
//double k_length = data_transfer.pointer_to_a_Template_physics_storage_struct->k_init;
//printf("Naar vi her til?\n");
k_final[0] = data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[0];
k_final[1] = data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[1];
k_final[2] = data_transfer.pointer_to_a_PhononSimple_storage_struct->k_f[2];
*weight *= 0.25/PI*data_transfer.pointer_to_a_PhononSimple_storage_struct->solid_angle;
// A pointer to k_final is returned, and the wavevector will be set to k_final after a scattering event
return 1; // return 1 is sucess, return 0 is failure, and the ray will be absorbed.
// failure should not happen, as this function will only be called when
// the cross section for the current k_initial is above zero.
// There is access to the data_transfer from within the scattering function
// In this case the only variable is my, but it could be read by:
// double my = data_transfer.pointer_to_a_Template_physics_storage_struct->my_scattering;
// One can assume that if the scattering function is running, the my fuction was
// executed just before and for the same k_initial.
};
// These lines help with future error correction, and tell other Union components
// that at least one process have been defined.
#ifndef PROCESS_DETECTOR
#define PROCESS_DETECTOR dummy
#endif
#ifndef PROCESS_PHONONSIMPLE_DETECTOR
#define PROCESS_PHONONSIMPLE_DETECTOR dummy
#endif
%}
DECLARE
%{
// Declare for this component, to do calculations on the input / store in the transported data
struct PhononSimple_physics_storage_struct PhononSimple_storage; // Replace template with your own name here
// Variables needed in initialize of this function.
double effective_my_scattering;
double sigma_const;
// Needed for transport to the main component, will be the same for all processes
struct global_process_element_struct global_process_element;
struct scattering_process_struct This_process;
%}
INITIALIZE
%{
// Initialize done in the component
//PhononSimple_storage.my_scattering = effective_my_scattering;
PhononSimple_storage.aa = a; // lattice side length
PhononSimple_storage.bb = b; // scattering length
PhononSimple_storage.cc = c; // speed of sound
//PhononSimple_storage.V_rho = 4.0/(a*a*a);
//PhononSimple_storage.Atom_mass_au = M;
//PhononSimple_storage.mc_steps = MCsteps;
//PhononSimple_storage.DW_factor = DW;
PhononSimple_storage.Temp = T;
/*
if (!pol_h && !pol_k && !pol_l)
PhononSimple_storage.pol_mode = 0;
else {
PhononSimple_storage.pol_mode = 1;
NORM(pol_h,pol_k,pol_l);
PhononSimple_storage.pol_vector.x = pol_h;
PhononSimple_storage.pol_vector.y = pol_k;
PhononSimple_storage.pol_vector.z = pol_l;
}
*/
if (longitudinal > 0.5)
PhononSimple_storage.enable_longitudinal = 1;
else
PhononSimple_storage.enable_longitudinal = 0;
if (transverse > 0.5)
PhononSimple_storage.enable_transverse = 1;
else
PhononSimple_storage.enable_transverse = 0;
if (PhononSimple_storage.enable_transverse == 0 && PhononSimple_storage.enable_longitudinal == 0) {
printf("PhononSimple_process named %s has no branches as both longitudinal and transverse is disabled! \n");
exit(1);
}
PhononSimple_storage.my_constant = packing_factor*4.0/(a*a*a)*DW*K2V*K2V*VS2E*2*VS2E*K2V*b*b/M;
// my_constant = V_rho*DW*K2V*K2V*VS2E*2*VS"E*K2V*b*b/M // done in initialize
// Need to specify if this process is isotropic
//This_process.non_isotropic_rot_index = -1; // Yes (powder)
This_process.non_isotropic_rot_index = 1; // No (single crystal)
// The type of the process must be saved in the global enum process
This_process.eProcess = PhononSimple;
// Packing the data into a structure that is transported to the main component
This_process.data_transfer.pointer_to_a_PhononSimple_storage_struct = &PhononSimple_storage;
This_process.probability_for_scattering_function = &PhononSimple_physics_my;
This_process.scattering_function = &PhononSimple_physics_scattering;
// This will be the same for all process's, and can thus be moved to an include.
sprintf(This_process.name,"%s",NAME_CURRENT_COMP);
This_process.process_p_interact = interact_fraction;
rot_copy(This_process.rotation_matrix,ROT_A_CURRENT_COMP);
sprintf(global_process_element.name,"%s",NAME_CURRENT_COMP);
global_process_element.component_index = INDEX_CURRENT_COMP;
global_process_element.p_scattering_process = &This_process;
if (_getcomp_index(init) < 0) {
fprintf(stderr,"PhononSimple_process:%s: Error identifying Union_init component, %s is not a known component name.\n",
NAME_CURRENT_COMP, init);
exit(-1);
}
struct pointer_to_global_process_list *global_process_list = COMP_GETPAR3(Union_init, init, global_process_list);
add_element_to_process_list(global_process_list,global_process_element);
%}
TRACE
%{
// Trace should be empty, the simulation is done in Union_master
%}
END
|