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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
/*******************************************************************************
*
* McStas, neutron ray-tracing package
* Copyright (C) 1997-2006, All rights reserved
* Risoe National Laboratory, Roskilde, Denmark
* Institut Laue Langevin, Grenoble, France
*
* Component: Virtual_mcnp_input.comp
*
* %I
* Written by: <a href="mailto:hennanec@ensimag.fr">Chama Hennane</a> and E. Farhi
* Date: June 28th, 2006
* Origin: <a href="http://www.ill.fr/">ILL</a>
* Modified by: EF, July 25th 2006: bug fixes.
*
* This component uses a filename of recorded neutrons from the reactor monte carlo
* code MCNP as a source of particles.
*
* %D
* This component generates neutron events from a filename created using the
* MCNP Monte Carlo code for nuclear reactors. It is used to
* calculate flux exiting from hot or cold neutron sources.
* Neutron position and velocity is set from the filename. The neutron time is
* left at zero.
*
* Note that axes orientation may be different between MCNP and McStas.
* The component has the ability to center and orient the neutron beam to the Z-axis.
* It also may change the coordinate system from the MCNP frame to the McStas one.
* The verbose mode is highly recommended as it displays lots of useful informations.
* To obtain absolute intensity, set 'intensity' and 'nps' parameters.
* The source total intensity is 1.054e18 for LLB/Saclay (14 MW) and 4.28e18 for
* ILL/Grenoble (58 MW).
*
* Format of MCNP events are :
*
* position_X position_Y position_Z dir_X dir_Y dir_Z Energy Weight Time
*
* energy is in Mega eV, time in shakes (1e-8 s),
* positions are in cm and the direction vector is normalized to 1.
*
* %BUGS
* We recommend NOT to use parallel execution (MPI) with this component. If you
* need to, set parameter 'smooth=1'.
*
* EXAMPLE:
* To generate PTRAC files using MCNP/MCNPX, add at the end of your input file:
* f1:n 2001 // tally
* (...)
* ptrac filename = asc
* max = -1000000 // number of neutrons to generate and stop
* write = all
* event = sur
* filter = 2001,jsu // surface tally id
* To create a 'source' from a MCNP simulation event file for the ILL:
* COMPONENT source = Virtual_mcnp_input(
* filename = "H10p", intensity=4.28e18, nps=11328982,
* verbose = 1, autocenter="translate rotate rescale")
*
* %P
* INPUT PARAMETERS
* filename: [str] Name of the MCNP PTRAC neutron input file. Empty string "" unactivates component
* repeat_count: [1] Number of times the source must be generated. 0 unactivates the component
* verbose: [0|1] Displays additional informations if set to 1
* intensity: [n/s] Intensity multiplication factor
* nps: [1] Number of total events shot by MCNP to generate the PTRAC as indicated at the end of the MCNP 'o' file as 'source particle weight for summary table normalization' or alternatively 'nps = '.
* autocenter: [str] String which may contain following keywords. "translate" or "center" to center the beam area AT (0,0,0) "rotate" or "orient" to center the beam velocity along Z "change axes" to change coordinate system definition "rescale" to adapt intensity to abs. units. with factor intensity/nps. Other words are ignored.
* surface_id: [1] Index of the emitting MCNP surface to use. -1 for all.
* MCNP_ANALYSE: [1] Number of neutron events to read for file pre-analysis. Use 0 to analyze them all.
* smooth: [0/1] Smooth sparsed event files for file repetitions.
*
* OUTPUT PARAMETERS
* nl: [long] nb of lines in header
* mean_x: [m] source center coordinates
* mean_y: [m] source center coordinates
* mean_z: [m] source center coordinates
* angle2z: [rad] rotation angle required to orient beam along Z axis
*
* %L
* <a href="http://mcnp-green.lanl.gov/index.html">MCNP</a>
* %L
* MCNP -- A General Monte Carlo N-Particle Transport Code, Version 5, Volume II: User's Guide, p177
*
* %E
*
*******************************************************************************/
DEFINE COMPONENT Virtual_mcnp_input
SETTING PARAMETERS (string filename=0, string autocenter=0, repeat_count=1, verbose=0, intensity=0,MCNP_ANALYSE=10000,
int surface_id=-1,
nps=0,smooth=1)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */
SHARE
%{
#ifndef MCNP_INPUT_DEFS
#define MCNP_INPUT_DEFS
/* number of neutron events to read for file pre-analysis */
#include <sys/stat.h>
/* mcnp_get_particle: read a neutron event from the MCNP PTRAC file */
int mcnp_get_particle(FILE *hfile,float * tableau,int *old_surface,int *surface_id,int *informed_event,int *informed_surface)
{
char *c;
int exit_flag=0;
float arg1=-1;
float arg2=-1;
float arg3=-1;
float arg4=-1;
float arg5=-1;
float arg6=-1;
float arg7=-1;
float arg8=-1;
float arg9=-1;
int count=-1;
int new_surface=-1;
char str[1024];
int serr=0;
if (!tableau) exit(fprintf(stderr, "MCNP_input: Error in mem alloc (mcnp_get_particle).\n"));
c=fgets(str,1024,hfile);
while (c!=NULL) {
count=sscanf(str,"%e %e %e %e %e %e %e %e %e",&arg1,&arg2,&arg3,&arg4,&arg5,&arg6,&arg7,&arg8,&arg9);
if (count==2) { /* test number of numerical values read from line */
if(arg2!=3000 && arg2!=9000){
printf("Warning: Unknown event %g in current file\n",arg2);
} else if (informed_event)
*informed_event = arg2;
} else if (count==7 || count==6) {
new_surface=arg3;
if (new_surface!=*surface_id && *surface_id!=-1) serr=1;
else
*informed_surface=new_surface;
if (*old_surface!=-1 && !*informed_surface && new_surface!=*old_surface) {
printf("Warning: Different surface IDs in current file: previous=%i current=%i\n", *old_surface, new_surface);
}
*old_surface =new_surface;
} else if (count==9 && (!informed_surface || *informed_surface)
&& (!informed_event || (informed_event && (*informed_event==3000 || *informed_event==9000)))) {
if(serr==0){
tableau[0]=arg1;
tableau[1]=arg2;
tableau[2]=arg3;
tableau[3]=arg4;
tableau[4]=arg5;
tableau[5]=arg6;
tableau[6]=arg7;
tableau[7]=arg8;
tableau[8]=arg9;
return (exit_flag);
}
serr=0;
} else {
if (informed_event && *informed_event) {
exit_flag=1;
printf("MCNP_input: Warning: skip line %s\n", str);
return (exit_flag);
}
}
c=fgets(str,1024,hfile);
}
exit_flag=1;
return (exit_flag);
}
/* mcnp_create_neutron:
function that creates an event
and assigns neutron parameters.
Warning: mcnp Coord system (x y z) = (-x z y)
*/
int mcnp_create_neutron(FILE *hfile, double *x,double *y,double *z,
double *vx, double *vy, double *vz,
double *t,
double *sx, double *sy, double *sz,
double *p,int *old_surface,int *surface_id,int *informed_event,
int *informed_surface)
{
double Mev2Joule=1.602e-13;
double speed;
int exit_flag=0, ifield=0;
float field[9];
if(mcnp_get_particle(hfile,field,old_surface,surface_id,informed_event,informed_surface)) { exit_flag=1; }
else {
speed=sqrt(2.* field[6]*Mev2Joule/MNEUTRON);
*x=field[0]/100.0;
*y=field[1]/100.0;
*z=field[2]/100.0;
*vx=field[3]*speed;
*vy=field[4]*speed;
*vz=field[5]*speed;
*p=field[7];
*sx=1.;*sy=*sz=0;
*t=field[8]*1e-8;
// now change axis system
double tx, ty, tz;
tx=*x; ty=*y; tz=*z; *x=-ty; *y=-tz; *z=tx;
tx=*vx; ty=*vy; tz=*vz; *vx=-ty; *vy=-tz; *vz=tx;
}
return (exit_flag);
}
/* mcnp_get_header:
function that gets/allocate header lines until the MCNP start of data
*/
char *mcnp_get_header(FILE *hfile)
{
int i;
char *head=NULL;
char buffer[1024];
char *c=NULL;
c=fgets(buffer,1024,hfile);
head=malloc(2*1024+1);
if (!head) exit(fprintf(stderr, "MCNP_input: Error in mem alloc (mcnp_get_header).\n"));
strcpy(head,"");
c=fgets(buffer,1024,hfile);
strcat(head, buffer);
c=fgets(buffer,1024,hfile);
strcat(head, buffer);
/* skip 6 lines */
for (i=0; i<=6; i++) c=fgets(buffer,1024,hfile);
return head;
}
#endif
%}
DECLARE
%{
int rep; /* Neutron repeat count */
int repeat_cnt; /* Repeat count, MPI taken into account */
FILE *hfile; /* Neutron input file handle */
char do_rotate, do_translate;
double mean_x, mean_y, mean_z;
double bx,by,bz;
double angle2z;
long begin_neutrons;
int old_surface;
int surface;
int informed_event;
int informed_surface;
double n_count_extrapolated;
double min_x, min_y, min_z;
double max_x, max_y, max_z;
double min_vx, min_vy, min_vz;
double max_vx, max_vy, max_vz;
double mean_dx, mean_dy, mean_dz;
%}
INITIALIZE
%{
rep=1;
do_rotate=0;
do_translate=0;
mean_x=0;
mean_y=0;
mean_z=0;
angle2z=0;
begin_neutrons=0;
old_surface=-1;
informed_event=0;
informed_surface=0;
n_count_extrapolated=0;
min_x=FLT_MAX;
min_y=FLT_MAX;
min_z=FLT_MAX;
max_x=-FLT_MAX;
max_y=-FLT_MAX;
max_z=-FLT_MAX;
min_vx=FLT_MAX;
min_vy=FLT_MAX;
min_vz=FLT_MAX;
max_vx=-FLT_MAX;
max_vy=-FLT_MAX;
max_vz=-FLT_MAX;
mean_dx=0;
mean_dy=0;
mean_dz=0;
char exit_flag=0; /* set to 1 if end of simulation */
double mean_vx=0, mean_vy=0, mean_vz=0, mean_v=0;
double n_neutrons=0;
double n_neutrons_p=0;
long filesize =0;
char *head=NULL; /*MCNP header*/
struct stat stfile;
surface=surface_id;
/* Open neutron input file. */
/* (If empty file given, present warning in case of verbose, perform nothing) */
if (!filename || !strcmp(filename,"") || !strcmp(filename,"0") || !strcmp(filename,"NULL"))
exit(printf("MCNP_input: %s: Empty file given, doing nothing!. Stop.\n", NAME_CURRENT_COMP));
if (filename && strlen(filename) && strcmp(filename, "NULL") && strcmp(filename, "0")) {
stat(filename,&stfile);
filesize = stfile.st_size;
hfile = fopen(filename, "r");
}
if(!hfile)
{
exit(printf("MCNP_input: %s: Error: Cannot open input file %s.\n", NAME_CURRENT_COMP, filename));
} else if (verbose)
printf("MCNP_input: %s: opening MCNP/PTRAC file '%s'\n", NAME_CURRENT_COMP, filename);
head = mcnp_get_header(hfile); /* and reset to filename start */
begin_neutrons = ftell(hfile);
if (verbose) fprintf(stdout, "%s\n", head);
if (head) free(head); head=NULL;
if (verbose) {
printf("Analysing MCNP file %s (", filename);
if (MCNP_ANALYSE) printf("%g events)\n", MCNP_ANALYSE);
else printf("all events)\n");
}
/* analyse MCNP file: count neutrons, get beam center and mean speed
* do that for the first 1e4 neutrons, and extrapolates to file size */
while (!exit_flag && (n_neutrons <= MCNP_ANALYSE || !MCNP_ANALYSE)) {
double x,y,z,vx,vy,vz,t,p,sx,sy,sz;
if (mcnp_create_neutron(
hfile,&x,&y,&z,&vx,&vy,&vz,&t,&sx,&sy,&sz,&p,
&old_surface,&surface,&informed_event,&informed_surface)) {
/*END OF FILE*/
fprintf(stderr, "MCNP_input: %s: Cannot get neutron %g : Error or End of File reached .\n",NAME_CURRENT_COMP,n_neutrons+1);
exit_flag = 1;
} else {
double v;
v = sqrt(vx*vx+vy*vy+vz*vz);
mean_x += p*x; mean_y += p*y; mean_z += p*z;
mean_vx += p*vx; mean_vy += p*vy; mean_vz += p*vz; mean_v += p*v;
angle2z += p*vz/v;
if (v) {
mean_dx += p*fabs(vx/v); mean_dy += p*fabs(vy/v); mean_dz += p*fabs(vz/v); }
if (x < min_x) min_x = x;
if (y < min_y) min_y = y;
if (z < min_z) min_z = z;
if (vx < min_vx) min_vx = vx;
if (vy < min_vy) min_vy = vy;
if (vz < min_vz) min_vz = z;
if (x > max_x) max_x = x;
if (y > max_y) max_y = y;
if (z > max_z) max_z = z;
if (vx > max_vx) max_vx = vx;
if (vy > max_vy) max_vy = vy;
if (vz > max_vz) max_vz = z;
n_neutrons++;
n_neutrons_p += p;
}
} /* end while */
if (n_neutrons) {
long end_analyse=0;
double cx,cy,cz;
end_analyse = ftell(hfile);
mean_x /= n_neutrons_p;
mean_y /= n_neutrons_p;
mean_z /= n_neutrons_p;
mean_vx /= n_neutrons_p;
mean_vy /= n_neutrons_p;
mean_vz /= n_neutrons_p;
mean_dx /= n_neutrons_p;
mean_dy /= n_neutrons_p;
mean_dz /= n_neutrons_p;
mean_v /= n_neutrons_p;
angle2z /= n_neutrons_p;
n_count_extrapolated = n_neutrons*(filesize-begin_neutrons)/(end_analyse - begin_neutrons);
if (verbose) {
double mean_k, mean_w=0, mean_L=0;
mean_k = V2K*mean_v;
if (mean_k) mean_L = 2*PI/mean_k;
mean_w = VS2E*mean_v*mean_v;
printf("MCNP file %s\nContains %s%g neutrons from surface %i with event %i\n",
filename, MCNP_ANALYSE ? "about " : "",
n_count_extrapolated, informed_surface, informed_event);
if (n_count_extrapolated > mcget_ncount())
printf(" (will use only %.3g %% of file)\n", 100.0*mcget_ncount()/n_count_extrapolated);
else
printf(" (limiting simulation to %g neutrons)\n", n_count_extrapolated*repeat_count);
printf(" Source size (full width in [m]): ");
printf(" dX=%g dY=%g dZ=%g\n", max_x-min_x, max_y-min_y, max_z-min_z);
printf(" Source center (in [m]): ");
printf(" X0=%g Y0=%g Z0=%g\n", mean_x, mean_y, mean_z);
printf(" Beam divergence (full width in [deg]):");
printf(" dVx=%g dVy=%g dVz=%g\n",
atan(mean_dx)*RAD2DEG,
atan(mean_dy)*RAD2DEG,
atan(mean_dz)*RAD2DEG);
printf(" Beam speed (in [m/s]): ");
printf(" Vx=%g Vy=%g Vz=%g\n", mean_vx, mean_vy, mean_vz);
printf(" Beam mean energy:\n");
printf(" speed=%g [m/s] energy=%g [meV]\n wavelength=%g [Angs] wavevector=%g [Angs-1]\n", mean_v, mean_w, mean_L, mean_k);
}
if (autocenter) {
if (strstr(autocenter, "rotate") || strstr(autocenter, "orient")) {
do_rotate = 1;
}
if (strstr(autocenter, "translate") || strstr(autocenter, "center"))
do_translate = 1;
if (strstr(autocenter, "rescale") && intensity)
printf("* Automatic Normalisation factor Intensity/nps = %g [n/s]\n",
intensity/nps);
}
/* compute the rotation matrix to make mean_v along the Z-axis */
/* first normalize mean velocity (will be Z axis): c=v/|v| */
cx = mean_vx/mean_v; cy = mean_vy/mean_v; cz = mean_vz/mean_v;
/* compute angle to rotate in order to come back to Z axis */
angle2z = acos(angle2z); /* in RAD */
/* get rotation axis b=c x [0 0 1] */
if (angle2z) {
vec_prod(bx,by,bz, cx,cy,cz, 0,0,1);
} else { /* already well oriented: nothing to do */
do_rotate = 0;
}
if (verbose && (do_rotate || do_translate || smooth)) {
printf("* Beam will be ");
if (do_translate) printf("translated (in position) ");
if (do_rotate) printf("rotated (%.3g [deg] to Z-axis) ", angle2z*RAD2DEG);
if (smooth) printf("smoothed ");
printf("\n");
}
} else {
exit(printf("MCNP_input: %s: Error: file %s neutrons does not contain any neutron. \n",NAME_CURRENT_COMP, filename));
}
/* reposition at start of filename (data start) */
if (fseek(hfile, begin_neutrons,SEEK_SET)) {
fprintf(stderr, "MCNP_input: %s: Error: Can not reset MCNP file (fseek error at analyse). \n",NAME_CURRENT_COMP);
exit_flag = 1;
}
#if defined (USE_MPI)
if (!smooth && mpi_node_count > 1) {
if (verbose)
printf("MCNP_input: %s: smoothing (smooth=1) is recommended when running MPI execution.\n", NAME_CURRENT_COMP);
}
#endif
double min_dv=fabs(max_vx-min_vx);
if (min_dv > fabs(max_vy-min_vy)) min_dv = fabs(max_vy-min_vy);
if (min_dv > fabs(max_vz-min_vz)) min_dv = fabs(max_vz-min_vz);
min_vx = min_dv;
repeat_cnt = repeat_count;
#if defined (USE_MPI)
repeat_cnt = ceil(1.0*repeat_cnt/mpi_node_count);
#endif
informed_event=informed_surface=0; /* reset ids so that new search in TRACE starts from scratch */
%}
TRACE
%{
char exit_flag=0; /* set to 1 if end of simulation */
int result_read=1;
while(!exit_flag) {
if (mcnp_create_neutron(hfile,&x,&y,&z,&vx,&vy,&vz,&t,&sx,&sy,&sz,&p,&old_surface,&surface,&informed_event,&informed_surface)) {
result_read=EOF;
exit_flag = 1;
} else {
if (do_translate) { /* translate the beam to origin */
x -= mean_x; y -= mean_y; z -= mean_z;
}
if (do_rotate) { /* rotate the beam so that its main axis is along Z */
double nvx, nvy, nvz;
rotate(nvx,nvy,nvz, vx,vy,vz, angle2z, bx,by,bz);
vx = nvx; vy=nvy; vz=nvz;
rotate(nvx,nvy,nvz, x, y, z, angle2z, bx,by,bz);
x = nvx; y=nvy; z=nvz;
}
if (intensity && nps) p *= intensity/nps/repeat_cnt;
SCATTER;
}
#if defined (USE_MPI)
if (smooth && n_count_extrapolated)
#else
if (smooth && rep > 1 && n_count_extrapolated)
#endif
{
/* apply smmothing */
x += randnorm()*(max_x-min_x)/n_count_extrapolated/2;
y += randnorm()*(max_y-min_y)/n_count_extrapolated/2;
z += randnorm()*(max_z-min_z)/n_count_extrapolated/2;
vx += randnorm()*min_vx/n_count_extrapolated/2;
vy += randnorm()*min_vx/n_count_extrapolated/2;
vx += randnorm()*min_vx/n_count_extrapolated/2;
}
if (result_read==EOF) { /* normal end of file */
rep++;
if (rep <= repeat_cnt) {
/* reposition at start of file (data start) */
int ret = fseek(hfile, begin_neutrons,SEEK_SET);
if (ret) {
fprintf(stderr, "MCNP_input: %s: Error: Can not repeat MCNP file (fseek error at repeat %d). \n",NAME_CURRENT_COMP, rep);
exit_flag = 1;
} else {
if (verbose) printf("MCNP_input: %s: Start Neutron Number %ld (iteration %d)\n",NAME_CURRENT_COMP,begin_neutrons, rep);
exit_flag=0;
}
}
else exit_flag=1;
}
if (exit_flag) {
if (verbose) printf(" Finishing simulation\n");
mcset_ncount(mcget_run_num()); ABSORB;
}
SCATTER;
break;
#ifdef USE_MPI
/* We always repeat by the number of nodes in an MPI run */
p /= mpi_node_count;
#endif
} /* end while */
%}
FINALLY
%{
if (filename && strlen(filename) && strcmp(filename,"0") && strcmp(filename,"NULL")) {
if (hfile) fclose(hfile);
if (verbose) {
printf("MCNP_input: %s: file %s\n", NAME_CURRENT_COMP, filename);
printf(" %g neutrons generated\n", (double)mcget_ncount());
}
}
%}
MCDISPLAY
%{
/* a box and a line in the main beam direction */
double nx=mean_x, ny=mean_y, nz=mean_z;
double dx=mean_dx,dy=mean_dy,dz=mean_dz;
if (do_translate) nx=ny=nz=0;
if (do_rotate) { dx=dy=0; dz=.1; }
box(nx,ny,nz, max_x-min_x, max_y-min_y, max_z-min_z, 0, 0, 1, 0);
line( nx,ny,nz, dx,dy,dz);
%}
END
|