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 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
|
/*******************************************************************************
*
* McStas, the neutron ray-tracing package: Source_Optimizer.comp
* Copyright 1997-2001 Risoe National Laboratory, Roskilde, Denmark
*
* Component: Source_Optimizer
*
* %Identification
* Written by: <a href="mailto:farhi@ill.fr">Emmanuel Farhi</a>
* Date: 17 Sept 1999
* Origin: <a href="http://www.ill.fr">ILL (France)</a>
* Modified by: (v 0.06) EF, Feb 2000;
* Modified by: (v.0.07) EF, Mar 10th 2000; (smoothed, parse options). struct
* Modified by: (v.0.08) EF, Oct 12th 2000; optim divergence for v and s
* Modified by: (v.0.09) EF, Mar 13th 2001; bug on div s (SIGFPE /0 )
*
* A component that optimizes the neutron flux passing through the
* Source_Optimizer in order to have the maximum flux at the
* <b>Monitor_Optimizer</b> position.
*
* %Description
* Principle: The optimizer first (step 1) computes neutron state parameter
* limits passing in the Source_Optimizer, and then (step 2) records a Reference
* source as well as the state (at Source_Optimizer position) of neutrons
* reaching Monitor. The optimized source is defined as a fraction of the
* Reference source plus the distribution of 'good' neutrons reaching the
* Monitor. The optimization then starts (step 3), and focuses new neutrons on
* the Monitor_Optimizer. In fact it changes 'bad' neutrons into 'good' ones
* (that reach the Monitor), acting on their position, spin and divergence or
* velocity. The overall Monitor flux is kept during process. The energy and
* polarisation distributions are kept during optimization as far as possible
* during optimisation. The optimization method considers that all neutron
* parameters - (x,y), (vx,vy,vz) or (vx/v2,vy/v2,v2), (sx,sy,sz) or
* (sx/s2,sy/s2,s2) - are independent.
*
* Options: The optimized source can be computed regularly ('continuous'
* option) or only once ('not continuous'). The time spent in steps 1 and 2 can
* be reduced for a shorter optimization ('auto'). The neutrons passing during
* steps 1 and 2 can be smoothed for a better neutron weight distribution
* ('smooth' option).
*
* Source_optimizer can be placed at any position where you want to act on the
* flux, for instance just after the source.
* Monitor_Optimizer should be placed at position(s) to optimize.
* I prefer to put one just before the sample.
*
* Default parameters bins, step, and keep are 10, 10% and 10% respectively.
* The option string can be empty (""), which stands for default configuration
* that works fine in usual cases:
*
* options="continuous optimization, auto mode, smooth, SetXY+SetDivV+SetDivS"
*
* <b>Possible options are</b>
* continuous for continuous source optimization (default).
* verbose displays optimization process (debug purpose).
* auto uses the shortest possible 'step 1' and 'step 2' and sets 'step' value as required (default).
* smooth remove possible spikes generated in steps 1 and 2 (default is smooth).
* unactivate to unactivate the Optimizer.
* no or not revert next option
* bins=[value=10] set the Number of cells for sampling neutron states
* step=[value=10] Optimizer step in % of simulation.
* keep=[value=10] Percentage of initial source distribution that is kept
* file=[name] Filename where to save optimized source distributions (no file is generated if not given. Default ext. is .src)
* SetXY Keywords to indicate what may be changed during
* SetV optimisation. Default is position, divergence and spin
* SetS direction ("SetXY+SetDivV+SetdivS"). Choosing the speed
* SetDivV or spin optimization (SetV or SetS) may modify the energy
* SetDivS or polarisation distribution (norm of V and S) as the three components are then independent.
*
* Parameters bins, step and keep can also be entered as optional parameters.
*
* <b>EXAMPLE</b>: I use the following settings
*
* optim_s = Source_Optimizer(options="please be clever") (same as empty)
* (...)
* Monitor_Optimizer(xmin=-0.05, xmax=0.05, ymin=-0.05, ymax=0.05,
* optim_comp = "optim_s")
*
* A good optimization needs to record enough non optimized neutrons on Monitor
* during step 2. Typical enhancement in computation speed is by a factor 20.
* This component usually works well.
*
* <b>NOTE:</b> You must be aware that in some cases (SetV and SetS),
* the optimization might sligtly afect the energy or spin distribution of the
* source. The optimizer tries to do its best anyway.
* Also, some 'spikes' may sometime appear in monitor signals in the course of
* the optimization, coming from non-optimized neutrons with original weight.
* The 'smooth' option minimises this effect (on by default).
*
* %Parameters
* bins: [1] Number of cells for sampling neutron states.
* step: [0-100] Optimizer step in percent of simulation.
* keep: [0-100] Percentage of initial source distribution that is kept.
* options: [str] string of options. See <b>Description<b>
*
* CALCULATED PARAMETERS:
*
* DEFS: a set of constant values used in the component (struct)
* Vars: structure that contains variables used in the component (struct)
*
* %Link
* <a href="Monitor_Optimizer.html">Monitor_Optimizer</a>
*
* %End
*******************************************************************************/
/* History :
Sep 17 1999 : v0.00 first release (not effective)
Sep 26 1999 : v0.01 New_Source for continuous optimisation
Sep 27 1999 : optimizer is ok, but not very efficient
Sep 29 1999 : v0.02 re-direct 'bad' neutrons instead of ABSORB (rand generator for nothing)
Oct 06 1999 : v0.03 installed options, corrected bugs, improved efficiency
Oct 21 1999 : v0.04 optim can be choosen for xy,v,s,p
Feb 01 2000 : v0.05 absorb replaced by remove spikes smooth method
Mar 03 2000 : v0.07 change option handling, and comp geometry
Mar 10 2000 : v0.07.2 gathered all variables into 2 structures
*/
/* other options : setxy, setv, sets, setdiv to precise what parameters
* should be optimized */
/* default is 'setxy'+setdiv+sets' */
/* TODO :
* 1- can re-use previous optimisation pattern
*/
/* PB : the use of 's2' in a component makes a conflict with McStas kernel */
DEFINE COMPONENT Source_Optimizer
SETTING PARAMETERS (bins=10, step=0.1, keep=0.1, string options=0)
/* Neutron parameters: (x,y,z,vx,vy,vz,t,sx,sy,sz,p) */
SHARE
%{
/* here we define a structure of constants (some kind of DEFINES) */
struct Optim_Defines
{
char PHASE_UNACTIVATE; /* to unactivate Optimizer */
char PHASE_SET_LIMITS; /* set array limits to 0, then ask for GET_LIMITS */
char PHASE_GET_LIMITS; /* compute array limits, then ask for SET_REF */
char PHASE_SET_REF; /* set Ref and New_Source to to 0, then ask for GET_REF */
char PHASE_GET_REF; /* compute Ref (and New_Source in Monitor), then ask for SET_SOURCE */
char PHASE_SET_SOURCE; /* set Source to Ref*x%+New_Source, normalize to Ref, Passing to 0, then ask for OPTIM */
char PHASE_OPTIM; /* Optimize and get New_Source (continuous optimization), then reask SET_SOURCE when required */
char MOD_X; /* what was modified in last optim */
char MOD_Y;
char MOD_VX;
char MOD_VY;
char MOD_VZ;
char MOD_SX;
char MOD_SY;
char MOD_SZ;
char DO_XY; /* what to optimize */
char DO_V;
char DO_S;
char DO_DIVV; /* (overrides with DO_V) */
char DO_DIVS; /* (overrides with DO_S) */
/* token modifiers */
char COORD_VAR; /* normal token */
char COORD_STEP; /* next token is a min value */
char COORD_KEEP; /* next token is a max value */
char COORD_DIM; /* next token is a bin value */
char COORD_FIL; /* next token is a filename */
char TOKEN_DEL[32]; /* token separators */
}
/* here we define a structure containing all informations */
struct Optim_Variables
{
/* These are distribution arrays[bins] within limits
* flux is kept during optimisation
* NOT stored : z is the position of previous component
* t time (related to z)
*/
/* initial Reference distribution arrays (for weights) */
double *Reference_x;
double *Reference_y;
double *Reference_vx; /* will be used either as 'v' or divergence on x,y */
double *Reference_vy;
double *Reference_vz;
double *Reference_sx;
double *Reference_sy;
double *Reference_sz;
/* optimized Source distribution arrays (to reach) */
double *Source_x;
double *Source_y;
double *Source_vx; /* will be used either as 'v' or divergence on x,y */
double *Source_vy;
double *Source_vz;
double *Source_sx;
double *Source_sy;
double *Source_sz;
/* optimized New_Source distribution arrays (to reach in next step, passed to Source) */
double *New_Source_x;
double *New_Source_y;
double *New_Source_vx; /* will be used either as 'v' or divergence on x,y */
double *New_Source_vy;
double *New_Source_vz;
double *New_Source_sx;
double *New_Source_sy;
double *New_Source_sz;
/* Passing distribution arrays (should grow to reach Source) */
double *Passing_x;
double *Passing_y;
double *Passing_vx; /* will be used either as 'v' or divergence on x,y */
double *Passing_vy;
double *Passing_vz;
double *Passing_sx;
double *Passing_sy;
double *Passing_sz;
/* limits for state parameters */
/* x and y are Optimizer dimensions (input parameters) */
double x_min, x_max;
double y_min, y_max;
double vx_min, vx_max; /* will be used either as 'v' or divergence on x,y */
double vy_min, vy_max;
double vz_min, vz_max;
double sx_min, sx_max;
double sy_min, sy_max;
double sz_min, sz_max;
int good_x; /* indexes for last 'good' neutron that passed through */
int good_y;
int good_vx; /* will be used either as 'v' or divergence on x,y */
int good_vy;
int good_vz;
int good_sx;
int good_sy;
int good_sz;
int nbins;
long n_redirect; /* number of consecutive ABSORB */
int Phase; /* Optimizer function */
long Phase_Counts; /* neutron counts to achieve in each Phase */
long Phase_Counts_L; /* neutron counts to achieve in Limits Phase */
long Phase_Counts_R; /* neutron counts to achieve in Reference Phase */
char Flag_Continuous; /* 1 : continuous Source optimization */
char Flag_Verbose; /* displays optimization informations */
char Flag_Smooth; /* 1 means that first steps non optimized neutrons are smoothed */
char Flag_Auto; /* 1 is for minimum counts in 2 first steps */
char Flag_Type; /* what to act on */
long Limits_Counts; /* passing neutron counts in each Phase */
long Reference_Counts;
long Passing_Counts;
double Monitor_Counts;
double Limits_Flux; /* passing neutron flux in each Phase */
double Reference_Flux;
double Passing_Flux;
double Monitor_Flux;
double Smoothed_Weigth;
float dkeep; /* percent of kept reference source */
float keep_target; /* to be reached */
float dstep;
long Normal_Monitor_Counts; /* counts without optim */
long Total_Monitor_Counts; /* final monitor counts */
double cur_vx; /* save neutron characteristics for Monitor and ABSORDed->Redirected neutrons */
double cur_vy; /* will be used either as 'v' or divergence on x,y */
double cur_vz;
double cur_x;
double cur_y;
double cur_sx;
double cur_sy;
double cur_sz;
double cur_p;
double dvx, dvy, dvz; /* for divergence x,y + v2, or velocity */
double dsx, dsy, dsz; /* for divergence sx,sy + s2, or spin */
char file[64]; /* output file name */
double t1; /* tempo vars */
double t2;
double t3;
double u1; /* tempo vars */
double u2;
double u3;
int i1; /* tempo vars */
int i2;
int i3;
int index; /* a running Vars.index */
int index_x ; /* indexes for last neutron that passed through */
int index_y ;
int index_vx; /* will be used either as 'v' or divergence on x,y */
int index_vy;
int index_vz;
int index_sx;
int index_sy;
int index_sz;
double v2;
double S2;
char Flag_Recycle; /* record of neutron state changes : DEFS.MOD_xx */
int Monitor_Number;
}
%}
DECLARE
%{
#ifndef FLT_MAX
#define FLT_MAX 1e37
#endif
Optim_Defines DEFS;
Optim_Variables Vars;
%}
INITIALIZE
%{
unsigned char carg = 1;
char *option_copy, *token;
char Flag_New_Token = 1;
char Flag_End = 1;
char Flag_No = 0;
char Token_Mode = DEFS.COORD_VAR;
/* init OPTIM */
DEFS.PHASE_UNACTIVATE =0; /* to unactivate Optimizer */
DEFS.PHASE_SET_LIMITS =1; /* set array limits to 0, then ask for GET_LIMITS */
DEFS.PHASE_GET_LIMITS =2; /* compute array limits, then ask for SET_REF */
DEFS.PHASE_SET_REF =3; /* set Ref and New_Source to to 0, then ask for GET_REF */
DEFS.PHASE_GET_REF =4; /* compute Ref (and New_Source in Monitor), then ask for SET_SOURCE */
DEFS.PHASE_SET_SOURCE =5; /* set Source to Ref*x%+New_Source, normalize to Ref, Passing to 0, then ask for OPTIM */
DEFS.PHASE_OPTIM =6; /* Optimize and get New_Source (continuous optimization), then reask SET_SOURCE when required */
DEFS.MOD_X =1; /* what was modified in last optim */
DEFS.MOD_Y =2;
DEFS.MOD_VX =4; /* will be used either as 'v' or divergence on x,y */
DEFS.MOD_VY =8;
DEFS.MOD_VZ =16;
DEFS.MOD_SX =32;
DEFS.MOD_SY =64;
DEFS.MOD_SZ =128;
DEFS.DO_XY =1; /* what to optimize */
DEFS.DO_V =2;
DEFS.DO_S =4;
DEFS.DO_DIVV =8; /* (overrides with DO_V) */
DEFS.DO_DIVS =16; /* (overrides with DO_S) */
/* token modifiers */
DEFS.COORD_VAR =0; /* normal token */
DEFS.COORD_STEP =1; /* next token is a step value */
DEFS.COORD_KEEP =2; /* next token is a keep value */
DEFS.COORD_DIM =3; /* next token is a bin value */
DEFS.COORD_FIL =4; /* next token is a filename */
strcpy(DEFS.TOKEN_DEL, " =,;[](){}:"); /* token separators */
/* init Optim */
Vars.good_x =0; /* indexes for last 'good' neutron that passed through */
Vars.good_y =0;
Vars.good_vx =0; /* will be used either as 'v' or divergence on x,y */
Vars.good_vy =0;
Vars.good_vz =0;
Vars.good_sx =0;
Vars.good_sy =0;
Vars.good_sz =0;
Vars.nbins = (int)bins;
Vars.n_redirect =0; /* number of consecutive ABSORB */
Vars.Phase_Counts =0; /* neutron counts to achieve in each Phase */
Vars.Phase_Counts_L =0; /* neutron counts to achieve in Limits Phase */
Vars.Phase_Counts_R =0; /* neutron counts to achieve in Reference Phase */
Vars.Flag_Continuous =1; /* 1 : continuous Source optimization */
Vars.Phase = DEFS.PHASE_SET_LIMITS;
Vars.n_redirect =0;
Vars.Flag_Verbose =0; /* displays optimization informations */
Vars.Flag_Smooth =1; /* 1 means that first steps non optimized neutrons are smoothed */
Vars.Flag_Auto =1; /* 1 is for minimum counts in 2 first steps */
Vars.Flag_Type =0; /* what to act on */
Vars.Limits_Counts =0; /* passing neutron counts in each Phase */
Vars.Reference_Counts =0;
Vars.Passing_Counts =0;
Vars.Monitor_Counts =0;
Vars.Limits_Flux =0; /* passing neutron flux in each Phase */
Vars.Reference_Flux =0;
Vars.Passing_Flux =0;
Vars.Monitor_Flux =0;
Vars.Smoothed_Weigth =0;
Vars.dkeep = keep;
Vars.dstep = step;
Vars.Normal_Monitor_Counts = 0; /* counts without optim */
Vars.Total_Monitor_Counts = 0; /* final monitor counts */
Vars.Monitor_Number = 0;
/* we parse the option string just as in monitor_nD */
strcpy(Vars.file,"");
if (options != NULL)
{
option_copy = (char*)malloc(strlen(options));
if (option_copy == NULL)
{
printf("Optimizer: %s cannot allocate option_copy (%i). Fatal.\n", NAME_CURRENT_COMP, strlen(options));
exit(-1);
}
}
else
{
option_copy = (char*)malloc(128);
strcpy(option_copy, "");
}
if (strlen(options))
{
Flag_End = 0;
strcpy(option_copy, options);
}
/* general keywords */
if (strstr(option_copy,"Set")) Vars.Flag_Type = 0;
if (strstr(option_copy,"SetXY")) Vars.Flag_Type |= DEFS.DO_XY;
if (strstr(option_copy,"SetDivV")) Vars.Flag_Type |= DEFS.DO_DIVV;
else
if (strstr(option_copy,"SetV")) Vars.Flag_Type |= DEFS.DO_V;
if (strstr(option_copy,"SetDivS")) Vars.Flag_Type |= DEFS.DO_DIVS;
else
if (strstr(option_copy,"SetS")) Vars.Flag_Type |= DEFS.DO_S;
if (strstr(option_copy,"unactivate")) Vars.Phase = DEFS.PHASE_UNACTIVATE;
if (Vars.Flag_Type == 0) Vars.Flag_Type = (DEFS.DO_XY|DEFS.DO_DIVV|DEFS.DO_DIVS);
carg = 1;
while((Flag_End == 0) && (carg < 128))
{
if (Flag_New_Token) /* to get the previous token sometimes */
{
if (carg == 1) token=(char *)strtok(option_copy,DEFS.TOKEN_DEL);
else token=(char *)strtok(NULL,DEFS.TOKEN_DEL);
if (token == NULL) Flag_End=1;
}
Flag_New_Token = 1;
if ((token != NULL) && (strlen(token) != 0))
{
/* first handle option values from preceeding keyword token detected */
if (Token_Mode == DEFS.COORD_STEP)
{
Vars.dstep = atof(token);
Token_Mode = DEFS.COORD_VAR;
}
if (Token_Mode == DEFS.COORD_KEEP)
{
Vars.dkeep = atof(token);
Token_Mode = DEFS.COORD_VAR;
}
if (Token_Mode == DEFS.COORD_DIM)
{
Vars.nbins = atoi(token);
Token_Mode = DEFS.COORD_VAR;
}
if (Token_Mode == DEFS.COORD_FIL)
{
if (!Flag_No) strcpy(Vars.file,token);
else { strcpy(Vars.file,""); }
Token_Mode = DEFS.COORD_VAR;
}
/* now look for general option keywords */
if (!strcmp(token, "continuous"))
{ if (Flag_No) { Vars.Flag_Continuous = 0; Flag_No = 0; }
else Vars.Flag_Continuous = 1; }
if (!strcmp(token, "verbose"))
{ if (Flag_No) { Vars.Flag_Verbose = 0; Flag_No = 0; }
else Vars.Flag_Verbose = 1; }
if (!strcmp(token, "auto"))
{ if (Flag_No) { Vars.Flag_Auto = 0; Flag_No = 0; }
else Vars.Flag_Auto = 1; }
if (!strcmp(token, "smooth"))
{ if (Flag_No) { Vars.Flag_Smooth = 0; Flag_No = 0; }
else Vars.Flag_Smooth = 1; }
if (!strcmp(token, "bins")) Token_Mode = DEFS.COORD_DIM;
if (!strcmp(token, "step")) Token_Mode = DEFS.COORD_STEP;
if (!strcmp(token, "keep")) Token_Mode = DEFS.COORD_KEEP;
if (!strcmp(token, "file")) { Token_Mode = DEFS.COORD_FIL; if (Flag_No) strcpy(Vars.file,""); else strncpy(Vars.file,NAME_CURRENT_COMP,64); }
if (!strcmp(token, "no") || !strcmp(token, "not")) Flag_No = 1;
carg++;
} /* end if token */
} /* end while carg */
free(option_copy);
if (carg == 128) printf("Source_Optimizer: %s reached max number of tokens (%i). Skipping.\n", NAME_CURRENT_COMP, 128);
if (Vars.dstep < 0) Vars.dstep = .1; /* default values if -1 is given */
if (Vars.nbins < 0) Vars.nbins = 10;
if (Vars.dkeep < 0) Vars.dkeep = .1;
if (Vars.dstep >= 1) Vars.dstep = Vars.dstep/100; /* in case user gives % in 1-100 */
if (Vars.dstep < .01) Vars.dstep = .01; /* max 100 steps */
if (Vars.dstep > 0.5) Vars.dstep = 0.5; /* min 2 steps */
if (Vars.dkeep >= 1) Vars.dkeep = Vars.dkeep/100; /* in case user gives % in 1-100 */
if (Vars.dkeep < .1) Vars.dkeep = .1; /* at least keeps 10 % of Ref */
if (Vars.dkeep > .99) Vars.dkeep = .99;
if (Vars.nbins < 1) Vars.nbins = 1; /* this means no optimisation, but loss of time... */
if (Vars.nbins > 100) Vars.nbins = 100;
if (Vars.Flag_Auto)
{
if (Vars.nbins*10 < Vars.Phase_Counts) Vars.Phase_Counts_L = Vars.nbins*100; /* need at least 10 counts per bin for Limits */
else Vars.Phase_Counts_L = (long)(mcget_ncount() * Vars.dstep / 4);
Vars.Phase_Counts_R = (double)mcget_ncount();
Vars.Phase_Counts = (double)mcget_ncount();
Vars.keep_target = Vars.dkeep;
Vars.dkeep = 0.5;
}
else
{
Vars.Phase_Counts = (long)(mcget_ncount() * Vars.dstep);
Vars.Phase_Counts_L = (long)rint(Vars.Phase_Counts/4);
Vars.Phase_Counts_R = Vars.Phase_Counts - Vars.Phase_Counts_L; /* will make one step */
Vars.keep_target = Vars.dkeep;
}
if ((Vars.Source_x = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_y = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_vx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_vy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_vz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_sx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_sy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Source_sz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_x = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_y = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_vx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_vy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_vz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_sx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_sy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.New_Source_sz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_x = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_y = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_vx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_vy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_vz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_sx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_sy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Passing_sz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_x = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_y = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_vx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_vy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_vz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_sx = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_sy = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if ((Vars.Reference_sz = (double*)malloc(Vars.nbins * sizeof(double))) == NULL) { fprintf(stderr,"Optimizer : not enough memory\n"); exit(-1); }
if (Vars.Phase == DEFS.PHASE_UNACTIVATE)
{ if (Vars.Flag_Verbose) printf("Source_Optimizer: %s is unactivated\n", NAME_CURRENT_COMP);
Vars.Flag_Verbose = 0; }
/* end initialize */
%}
TRACE
%{
Vars.index=0; /* a running Vars.index */
Vars.index_x=0; /* indexes for last neutron that passed through */
Vars.index_y=0;
Vars.index_vx=0; /* will be used either as 'v' or divergence on x,y */
Vars.index_vy=0;
Vars.index_vz=0;
Vars.index_sx=0;
Vars.index_sy=0;
Vars.index_sz=0;
Vars.Flag_Recycle =0; /* record of neutron state changes : DEFS.MOD_xx */
if (Vars.Phase != DEFS.PHASE_UNACTIVATE)
{
PROP_Z0;
Vars.cur_vx = vx; /* save neutron characteristics for Monitor */
Vars.cur_vy = vy; /* will be used either as 'v' or divergence on x,y */
Vars.cur_vz = vz;
Vars.cur_x = x;
Vars.cur_y = y;
Vars.cur_sx = sx;
Vars.cur_sy = sy;
Vars.cur_sz = sz;
Vars.cur_p = p;
Vars.v2 = vx*vx+vy*vy+vz*vz; /* squared velocity */
Vars.S2 = sx*sx+sy*sy+sz*sz; /* squared polarisation */
if (Vars.Flag_Type & DEFS.DO_DIVV) /* use divergence */
{
Vars.dvz = Vars.v2; /* v2 */
if (Vars.dvz == 0) Vars.t1 = 1e-10;
else Vars.t1 = Vars.dvz;
Vars.dvx = vx/Vars.t1; /* vx/v2 */
Vars.dvy = vy/Vars.t1; /* vy/v2 */
}
else
{
Vars.dvx = vx;
Vars.dvy = vy;
Vars.dvz = vz;
}
if (Vars.Flag_Type & DEFS.DO_DIVS) /* use spin 'divergence' */
{
Vars.dsz = Vars.S2; /* s2 */
if (Vars.dsz == 0) Vars.t1 = 1e-10;
else Vars.t1 = Vars.dsz;
Vars.dsx = sx/Vars.t1; /* sx/s2 */
Vars.dsy = sy/Vars.t1; /* sy/s2 */
}
else
{
Vars.dsx = sx;
Vars.dsy = sy;
Vars.dsz = sz;
}
/* handle Phase sequence */
if ((Vars.Phase == DEFS.PHASE_GET_LIMITS)
&& (Vars.Limits_Counts >= Vars.Phase_Counts_L))
{
Vars.Phase = DEFS.PHASE_SET_REF;
if (Vars.Flag_Verbose)
{
printf(">> DEFS.PHASE_SET_REF (%i neutrons)\n", Vars.Limits_Counts);
if (Vars.Monitor_Number > 1) printf(" using %i Monitor_Optimizer components.\n", Vars.Monitor_Number);
}
if (Vars.Monitor_Number == 0)
{
Vars.Phase = DEFS.PHASE_UNACTIVATE;
printf("Source_Optimizer: %s is unactivated (no Monitor_Optimizer found)\n", NAME_CURRENT_COMP);
}
}
if ((Vars.Phase == DEFS.PHASE_GET_REF)
&& (Vars.Reference_Counts >= Vars.Phase_Counts_R))
{
Vars.Phase = DEFS.PHASE_SET_SOURCE;
Vars.t1 = (double)Vars.Phase_Counts/(double)Vars.Reference_Counts;
Vars.Phase_Counts = Vars.Reference_Counts+Vars.Limits_Counts;
Vars.Phase_Counts_R = Vars.Phase_Counts; /* should be Vars.Reference_Counts+Vars.Limits_Counts */
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{
Vars.Reference_x[Vars.index] *= Vars.t1;
Vars.Reference_y[Vars.index] *= Vars.t1;
Vars.Reference_vx[Vars.index] *= Vars.t1;
Vars.Reference_vy[Vars.index] *= Vars.t1;
Vars.Reference_vz[Vars.index] *= Vars.t1;
Vars.Reference_sx[Vars.index] *= Vars.t1;
Vars.Reference_sy[Vars.index] *= Vars.t1;
Vars.Reference_sz[Vars.index] *= Vars.t1;
}
Vars.Reference_Counts = (long)(Vars.t1*(double)Vars.Reference_Counts);
Vars.Reference_Flux *= Vars.t1;
if (Vars.Flag_Auto)
{
Vars.dkeep = Vars.nbins*10/Vars.Monitor_Counts;
if (Vars.dkeep < Vars.keep_target) Vars.dkeep = Vars.keep_target;
if (Vars.dkeep > .9) Vars.dkeep = 0.9;
}
if (Vars.Flag_Verbose)
{
printf(">> DEFS.PHASE_SET_SOURCE (%i neutrons) from REF\n", Vars.Reference_Counts);
printf("Counts : reference = %i, passing = %i, monitor = %.1f\n", Vars.Reference_Counts, Vars.Phase_Counts, Vars.Monitor_Counts);
printf("Flux : reference = %.2g, passing = %.2g, monitor = %.2g\n", Vars.Reference_Flux, Vars.Reference_Flux, Vars.Monitor_Flux);
}
}
if ((Vars.Phase == DEFS.PHASE_OPTIM)
&& (Vars.Passing_Counts >= Vars.Phase_Counts))
{
Vars.Phase = DEFS.PHASE_SET_SOURCE;
if (Vars.Flag_Auto)
{
Vars.dkeep = Vars.nbins*10/(double)Vars.Monitor_Counts;
if (Vars.dkeep < Vars.keep_target) Vars.dkeep = Vars.keep_target;
if (Vars.dkeep > .9) Vars.dkeep = 0.9;
}
if (Vars.Flag_Verbose)
{
printf(">> DEFS.PHASE_SET_SOURCE (%i neutrons)\n", Vars.Passing_Counts);
printf("Number of redirections : %i\n",Vars.n_redirect);
printf("Counts : reference = %i, passing = %i, monitor = %.1f\n", Vars.Reference_Counts, Vars.Passing_Counts, Vars.Monitor_Counts);
printf("Flux : reference = %.2g, passing = %.2g, monitor = %.2g\n", Vars.Reference_Flux, Vars.Passing_Flux, Vars.Monitor_Flux);
}
}
/* handle Vars.Phase functions */
if (Vars.Phase == DEFS.PHASE_SET_LIMITS) /* init : need to compute limits and flux */
{
Vars.Limits_Counts = 0;
Vars.Limits_Flux = 0;
Vars.x_min = FLT_MAX; Vars.x_max = -FLT_MAX;
Vars.y_min = FLT_MAX; Vars.y_max = -FLT_MAX;
Vars.vx_min = FLT_MAX; Vars.vx_max = -FLT_MAX;
Vars.vy_min = FLT_MAX; Vars.vy_max = -FLT_MAX;
Vars.vz_min = FLT_MAX; Vars.vz_max = -FLT_MAX;
Vars.sx_min = FLT_MAX; Vars.sx_max = -FLT_MAX;
Vars.sy_min = FLT_MAX; Vars.sy_max = -FLT_MAX;
Vars.sz_min = FLT_MAX; Vars.sz_max = -FLT_MAX;
Vars.Phase = DEFS.PHASE_GET_LIMITS;
} /* end DEFS.PHASE_SET_LIMITS */
if (Vars.Phase == DEFS.PHASE_GET_LIMITS) /* init : need to compute limits and flux */
{
Vars.Limits_Counts++;
Vars.Limits_Flux += p;
if (x < Vars.x_min) Vars.x_min = x;
if (y < Vars.y_min) Vars.y_min = y;
if (x > Vars.x_max) Vars.x_max = x;
if (y > Vars.y_max) Vars.y_max = y;
if (Vars.dvx < Vars.vx_min) Vars.vx_min = Vars.dvx;
if (Vars.dvx > Vars.vx_max) Vars.vx_max = Vars.dvx;
if (Vars.dvy < Vars.vy_min) Vars.vy_min = Vars.dvy;
if (Vars.dvy > Vars.vy_max) Vars.vy_max = Vars.dvy;
if (Vars.dvz < Vars.vz_min) Vars.vz_min = Vars.dvz;
if (Vars.dvz > Vars.vz_max) Vars.vz_max = Vars.dvz;
if (Vars.dsx < Vars.sx_min) Vars.sx_min = Vars.dsx;
if (Vars.dsx > Vars.sx_max) Vars.sx_max = Vars.dsx;
if (Vars.dsy < Vars.sy_min) Vars.sy_min = Vars.dsy;
if (Vars.dsy > Vars.sy_max) Vars.sy_max = Vars.dsy;
if (Vars.dsz < Vars.sz_min) Vars.sz_min = Vars.dsz;
if (Vars.dsz > Vars.sz_max) Vars.sz_max = Vars.dsz;
if (Vars.Flag_Smooth) { p *= Vars.dkeep; Vars.cur_p *= Vars.dkeep; Vars.Smoothed_Weigth += Vars.dkeep; }
else Vars.Smoothed_Weigth++;
} /* end if DEFS.PHASE_GET_LIMITS */
if (Vars.Phase == DEFS.PHASE_SET_REF) /* Set Ref and New_Source to 0 */
{
Vars.Reference_Counts = 0;
Vars.Reference_Flux = 0;
Vars.Monitor_Counts = 0; /* also counted as New_Source */
Vars.Monitor_Flux = 0;
for (Vars.index=0; Vars.index < Vars.nbins; Vars.index++)
{
Vars.Reference_x[Vars.index] = 0; /* initial distribution will be recorded first */
Vars.Reference_y[Vars.index] = 0;
Vars.Reference_vx[Vars.index] = 0;
Vars.Reference_vy[Vars.index] = 0;
Vars.Reference_vz[Vars.index] = 0;
Vars.Reference_sx[Vars.index] = 0;
Vars.Reference_sy[Vars.index] = 0;
Vars.Reference_sz[Vars.index] = 0;
Vars.New_Source_x[Vars.index] = 0; /* Monitor_Optimizer will compute the */
Vars.New_Source_y[Vars.index] = 0; /* optimized New_Source distribution */
Vars.New_Source_vx[Vars.index] = 0; /* that will become Source for Optim Vars.dstep */
Vars.New_Source_vy[Vars.index] = 0;
Vars.New_Source_vz[Vars.index] = 0;
Vars.New_Source_sx[Vars.index] = 0;
Vars.New_Source_sy[Vars.index] = 0;
Vars.New_Source_sz[Vars.index] = 0;
} /* end for */
Vars.Phase = DEFS.PHASE_GET_REF;
} /* end DEFS.PHASE_SET_REF */
if (Vars.Phase == DEFS.PHASE_GET_REF) /* now build the Reference in limits */
{ /* New_Source is set by Monitor_Optimizer */
Vars.Reference_Counts++;
Vars.Reference_Flux += p;
if (Vars.vx_max-Vars.vx_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvx -Vars.vx_min)/(Vars.vx_max-Vars.vx_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_vx[Vars.index]++;
if (Vars.vy_max-Vars.vy_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvy -Vars.vy_min)/(Vars.vy_max-Vars.vy_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_vy[Vars.index]++;
if (Vars.vz_max-Vars.vz_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvz -Vars.vz_min)/(Vars.vz_max-Vars.vz_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_vz[Vars.index]++;
if (Vars.x_max-Vars.x_min)
Vars.index = (int)rint(Vars.nbins * (x -Vars.x_min)/(Vars.x_max-Vars.x_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_x[Vars.index]++;
if (Vars.y_max-Vars.y_min)
Vars.index = (int)rint(Vars.nbins * (y -Vars.y_min)/(Vars.y_max-Vars.y_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_y[Vars.index]++;
if (Vars.sx_max-Vars.sx_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dsx -Vars.sx_min)/(Vars.sx_max-Vars.sx_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_sx[Vars.index]++;
if (Vars.sy_max-Vars.sy_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dsy -Vars.sy_min)/(Vars.sy_max-Vars.sy_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_sy[Vars.index]++;
if (Vars.sz_max-Vars.sz_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dsz -Vars.sz_min)/(Vars.sz_max-Vars.sz_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
Vars.Reference_sz[Vars.index]++;
if (Vars.Flag_Smooth) { p *= Vars.dkeep; Vars.cur_p *= Vars.dkeep; Vars.Smoothed_Weigth += Vars.dkeep; }
else Vars.Smoothed_Weigth++;
/*
* Each Optim phase lasts for (or at the end of this phase)
* Vars.Phase_Counts = (Vars.Reference_Counts+Vars.Limits_Counts) = mcget_ncount() * Vars.dstep
* The flux entering Source_optimizer during 1 phase should be
* Vars.Phase_Flux = (Vars.Reference_Flux +Vars.Limits_Flux)
*
* Thus the neutron flux % that was not counted is
* (Vars.Phase_Counts - Vars.Smoothed_Weigth)/Vars.Phase_Counts
* Thus remaining neutron weight should be increased by:
* p *= (mcget_ncount()-Vars.Smoothed_Weigth)/(mcget_ncount()-Vars.Phase_Counts)
* This will occur when leaving Source_Optimizer in OPTIM phase
* The achieved flux extrapolation gives an un-optimized flux at Source
* total_flux ~= (Vars.Reference_Flux+Vars.Limits_Flux)*mcget_ncount()/Vars.Phase_Counts
* while the flux at Monitor (#1) per Phase should be Vars.Monitor_Flux (at end of OPTIM)
*/
} /* end if DEFS.PHASE_GET_REF */
if (Vars.Phase == DEFS.PHASE_SET_SOURCE) /* Define optimized Source (normalized to Reference) */
{
if (Vars.Monitor_Counts)
{
Vars.t1 = (1 - Vars.dkeep) * (double)Vars.Phase_Counts/(double)Vars.Monitor_Counts;
Vars.t2 = Vars.dkeep;
/* so that total counts remains the same for each Optim phase */
}
else
{ Vars.t1 = 0; Vars.t2 = 1; }
Vars.Passing_Counts = 0;
Vars.Passing_Flux = 0;
if (Vars.Normal_Monitor_Counts == 0) Vars.Normal_Monitor_Counts = Vars.Total_Monitor_Counts; /* first un-optimized steps */
Vars.Monitor_Counts = 0; /* also counted as New_Source */
Vars.Monitor_Flux = 0;
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{ /* get Vars.dkeep % of Reference, and 1-Vars.dkeep% of New_Source normalized to Reference Counts */
if (Vars.Flag_Continuous || (Vars.n_redirect == 0))
{
Vars.Source_x[Vars.index] = Vars.t2 * Vars.Reference_x[Vars.index] + Vars.t1 * Vars.New_Source_x[Vars.index];
Vars.Source_y[Vars.index] = Vars.t2 * Vars.Reference_y[Vars.index] + Vars.t1 * Vars.New_Source_y[Vars.index];
Vars.Source_vx[Vars.index] = Vars.t2 * Vars.Reference_vx[Vars.index] + Vars.t1 * Vars.New_Source_vx[Vars.index];
Vars.Source_vy[Vars.index] = Vars.t2 * Vars.Reference_vy[Vars.index] + Vars.t1 * Vars.New_Source_vy[Vars.index];
if (Vars.Flag_Type & DEFS.DO_DIVV)
Vars.Source_vz[Vars.index] = Vars.Reference_vz[Vars.index];
else
Vars.Source_vz[Vars.index] = Vars.t2 * Vars.Reference_vz[Vars.index] + Vars.t1 * Vars.New_Source_vz[Vars.index];
Vars.Source_sx[Vars.index] = Vars.t2 * Vars.Reference_sx[Vars.index] + Vars.t1 * Vars.New_Source_sx[Vars.index];
Vars.Source_sy[Vars.index] = Vars.t2 * Vars.Reference_sy[Vars.index] + Vars.t1 * Vars.New_Source_sy[Vars.index];
if (Vars.Flag_Type & DEFS.DO_DIVS)
Vars.Source_sz[Vars.index] = Vars.Reference_sz[Vars.index];
else
Vars.Source_sz[Vars.index] = Vars.t2 * Vars.Reference_sz[Vars.index] + Vars.t1 * Vars.New_Source_sz[Vars.index];
if (Vars.New_Source_x[Vars.index] > Vars.New_Source_x[Vars.good_x]) Vars.good_x = Vars.index;
if (Vars.New_Source_y[Vars.index] > Vars.New_Source_y[Vars.good_y]) Vars.good_y = Vars.index;
if (Vars.New_Source_vx[Vars.index] > Vars.New_Source_vx[Vars.good_vx]) Vars.good_vx = Vars.index;
if (Vars.New_Source_vy[Vars.index] > Vars.New_Source_vy[Vars.good_vy]) Vars.good_vy = Vars.index;
if (Vars.New_Source_vz[Vars.index] > Vars.New_Source_vz[Vars.good_vz]) Vars.good_vz = Vars.index;
if (Vars.New_Source_sx[Vars.index] > Vars.New_Source_sx[Vars.good_sx]) Vars.good_sx = Vars.index;
if (Vars.New_Source_sy[Vars.index] > Vars.New_Source_sy[Vars.good_sy]) Vars.good_sy = Vars.index;
if (Vars.New_Source_sz[Vars.index] > Vars.New_Source_sz[Vars.good_sz]) Vars.good_sz = Vars.index;
}
Vars.Passing_x[Vars.index] = 0; /* Passing neutrons will then reach Source */
Vars.Passing_y[Vars.index] = 0; /* weights will be adapted to match Reference */
Vars.Passing_vx[Vars.index] = 0;
Vars.Passing_vy[Vars.index] = 0;
Vars.Passing_vz[Vars.index] = 0;
Vars.Passing_sx[Vars.index] = 0;
Vars.Passing_sy[Vars.index] = 0;
Vars.Passing_sz[Vars.index] = 0;
Vars.New_Source_x[Vars.index] = 0; /* Init of next Source */
Vars.New_Source_y[Vars.index] = 0;
Vars.New_Source_vx[Vars.index] = 0;
Vars.New_Source_vy[Vars.index] = 0;
Vars.New_Source_vz[Vars.index] = 0;
Vars.New_Source_sx[Vars.index] = 0;
Vars.New_Source_sy[Vars.index] = 0;
Vars.New_Source_sz[Vars.index] = 0;
} /* end for */
Vars.Phase = DEFS.PHASE_OPTIM;
} /* end DEFS.PHASE_SET_SOURCE */
if (Vars.Phase == DEFS.PHASE_OPTIM) /* Use optimized Source */
{
Vars.Flag_Recycle = 0;
Vars.index_x = Vars.good_x;
Vars.index_y = Vars.good_y;
Vars.index_vx= Vars.good_vx;
Vars.index_vy= Vars.good_vy;
Vars.index_vz= Vars.good_vz;
Vars.index_sx= Vars.good_sx;
Vars.index_sy= Vars.good_sy;
Vars.index_sz= Vars.good_sz;
/* ----------------------- VX VY VZ ----------------------- */
if (Vars.vz_max-Vars.vz_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvz -Vars.vz_min)/(Vars.vz_max-Vars.vz_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & DEFS.DO_V) && (Vars.Passing_vz[Vars.index] >= Vars.Source_vz[Vars.index]))
{ /* distribution achieved : redirect neutron near last neutron characteristic */
Vars.Flag_Recycle |= DEFS.MOD_VZ;
Vars.dvz += (Vars.index_vz-Vars.index)*(Vars.vz_max - Vars.vz_min)/Vars.nbins;
}
else
Vars.index_vz = Vars.index;
if (Vars.vx_max-Vars.vx_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvx -Vars.vx_min)/(Vars.vx_max-Vars.vx_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV)) && (Vars.Passing_vx[Vars.index] >= Vars.Source_vx[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_VX;
Vars.dvx += (Vars.index_vx-Vars.index)*(Vars.vx_max - Vars.vx_min)/Vars.nbins;
}
else
Vars.index_vx = Vars.index;
if (Vars.vy_max-Vars.vy_min)
Vars.index = (int)rint(Vars.nbins * (Vars.dvy -Vars.vy_min)/(Vars.vy_max-Vars.vy_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV)) && (Vars.Passing_vy[Vars.index] >= Vars.Source_vy[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_VY;
Vars.dvy += (Vars.index_vy-Vars.index)*(Vars.vy_max - Vars.vy_min)/Vars.nbins;
}
else
Vars.index_vy = Vars.index;
if (Vars.Flag_Type & DEFS.DO_DIVV)
{
Vars.cur_vz = Vars.v2; /* use original v^2 to keep energy */
/* now new vx,vy,vz */
Vars.cur_vx = Vars.cur_vz * Vars.dvx;
Vars.cur_vy = Vars.cur_vz * Vars.dvy;
Vars.cur_vz /= (1+Vars.dvx*Vars.dvx+Vars.dvy*Vars.dvy); /* always > 0 */
Vars.cur_vz = sqrt(Vars.cur_vz);
/* now should try the two z signs, but vz > 0 (see sz) */
Vars.Flag_Recycle |= (DEFS.MOD_VX|DEFS.MOD_VY|DEFS.MOD_VZ);
}
else
{
if (Vars.Flag_Type & DEFS.DO_V)
{
if (Vars.Flag_Recycle & (DEFS.MOD_VX|DEFS.MOD_VY|DEFS.MOD_VZ))
{ /* now try to keep E distribution */
Vars.cur_vx = Vars.dvx; /* may have been translated in vx,vy,vz 3 tests up-there */
Vars.cur_vy = Vars.dvy;
Vars.cur_vz = Vars.dvz;
Vars.t1 = Vars.v2 - Vars.cur_vz*Vars.cur_vz - Vars.cur_vy*Vars.cur_vy;
Vars.t2 = Vars.v2 - Vars.cur_vz*Vars.cur_vz - Vars.cur_vx*Vars.cur_vx;
Vars.t3 = Vars.v2 - Vars.cur_vx*Vars.cur_vx - Vars.cur_vy*Vars.cur_vy;
/* we affect the component wich is the most optimized (largest Source/Ref) */
if ((Vars.vx_max-Vars.vx_min) && (Vars.t1 > 0))
{
Vars.t1 = sqrt(Vars.t1);
if (vx < 0) Vars.t1 = -Vars.t1;
Vars.i1 = (int)rint(Vars.nbins * (Vars.t1 -Vars.vx_min)/(Vars.vx_max-Vars.vx_min));
if (Vars.i1 < 0) Vars.i1 = 0;
if (Vars.i1 >= Vars.nbins) Vars.i1 = Vars.nbins - 1;
Vars.u1 = Vars.Source_vx[Vars.i1]/(Vars.Reference_vx[Vars.i1]+1);
}
else
Vars.u1 = 0;
if ((Vars.vy_max-Vars.vy_min) && (Vars.t2 > 0))
{
Vars.t2 = sqrt(Vars.t2);
if (vy < 0) Vars.t2 = -Vars.t2;
Vars.i2 = (int)rint(Vars.nbins * (Vars.t2 -Vars.vy_min)/(Vars.vy_max-Vars.vy_min));
if (Vars.i2 < 0) Vars.i2 = 0;
if (Vars.i2 >= Vars.nbins) Vars.i2 = Vars.nbins - 1;
Vars.u2 = Vars.Source_vy[Vars.i2]/(Vars.Reference_vy[Vars.i2]+1);
}
else
Vars.u2 = 0;
if ((Vars.vz_max-Vars.vz_min) && (Vars.t3 > 0))
{
Vars.t3 = sqrt(Vars.t3);
if (vz < 0) Vars.t3 = -Vars.t3;
Vars.i3 = (int)rint(Vars.nbins * (Vars.t3 -Vars.vz_min)/(Vars.vz_max-Vars.vz_min));
if (Vars.i3 < 0) Vars.i3 = 0;
if (Vars.i3 >= Vars.nbins) Vars.i3 = Vars.nbins - 1;
Vars.u3 = Vars.Source_vz[Vars.i3]/(Vars.Reference_vz[Vars.i3]+1);
}
else
Vars.u3 = 0;
if ((Vars.u1 > Vars.u2) && (Vars.u1 > Vars.u3))
{
Vars.cur_vx = Vars.t1;
Vars.index_vx = Vars.i1;
Vars.Flag_Recycle |= DEFS.MOD_VX;
Vars.index = -1;
}
if ((Vars.u2 > Vars.u1) && (Vars.u2 > Vars.u3) )
{
Vars.cur_vy = Vars.t2;
Vars.index_vy = Vars.i2;
Vars.Flag_Recycle |= DEFS.MOD_VY;
Vars.index = -1;
}
if ((Vars.u3 > Vars.u1) && (Vars.u3 > Vars.u1))
{
Vars.cur_vz = Vars.t3;
Vars.index_vz = Vars.i3;
Vars.Flag_Recycle |= DEFS.MOD_VZ;
Vars.index = -1;
}
} /* end if Vars.Flag_Recycle */
} /* else DO_V */
} /* end if DO_DIVV else DO_V */
vx = Vars.cur_vx;
vy = Vars.cur_vy;
vz = Vars.cur_vz;
/* ----------------------- X Y ----------------------- */
if (Vars.x_max-Vars.x_min)
Vars.index = (int)rint(Vars.nbins * (x -Vars.x_min)/(Vars.x_max-Vars.x_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & DEFS.DO_XY) && (Vars.Passing_x[Vars.index] >= Vars.Source_x[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_X;
Vars.cur_x += (Vars.index_x-Vars.index)*(Vars.x_max - Vars.x_min)/Vars.nbins;
x = Vars.cur_x;
}
else
Vars.index_x = Vars.index;
if (Vars.y_max-Vars.y_min)
Vars.index = (int)rint(Vars.nbins * (y -Vars.y_min)/(Vars.y_max-Vars.y_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & DEFS.DO_XY) && (Vars.Passing_y[Vars.index] >= Vars.Source_y[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_Y;
Vars.cur_y += (Vars.index_y-Vars.index)*(Vars.y_max - Vars.y_min)/Vars.nbins;
y = Vars.cur_y;
}
else
Vars.index_y = Vars.index;
/* ----------------------- SX SY SZ ----------------------- */
if (Vars.sx_max-Vars.sx_min)
Vars.index = (int)rint(Vars.nbins * (sx -Vars.sx_min)/(Vars.sx_max-Vars.sx_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS)) && (Vars.Passing_sx[Vars.index] >= Vars.Source_sx[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_SX;
Vars.dsx += (Vars.index_sx-Vars.index)*(Vars.sx_max - Vars.sx_min)/Vars.nbins;
}
else
Vars.index_sx = Vars.index;
if (Vars.sy_max-Vars.sy_min)
Vars.index = (int)rint(Vars.nbins * (sy -Vars.sy_min)/(Vars.sy_max-Vars.sy_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS)) && (Vars.Passing_sy[Vars.index] >= Vars.Source_sy[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_SY;
Vars.dsy += (Vars.index_sy-Vars.index)*(Vars.sy_max - Vars.sy_min)/Vars.nbins;
}
else
Vars.index_sy = Vars.index;
if (Vars.sz_max-Vars.sz_min)
Vars.index = (int)rint(Vars.nbins * (sz -Vars.sz_min)/(Vars.sz_max-Vars.sz_min));
else
Vars.index = 0;
if (Vars.index < 0) Vars.index = 0;
if (Vars.index >= Vars.nbins) Vars.index = Vars.nbins - 1;
if ((Vars.Flag_Type & DEFS.DO_S) && (Vars.Passing_sz[Vars.index] >= Vars.Source_sz[Vars.index]))
{
Vars.Flag_Recycle |= DEFS.MOD_SZ;
Vars.dsz += (Vars.index_sz-Vars.index)*(Vars.sz_max - Vars.sz_min)/Vars.nbins;
}
else
Vars.index_sz = Vars.index;
if (Vars.Flag_Type & DEFS.DO_DIVS)
{
/* use original s^2 to keep polarisation */
Vars.cur_sz = Vars.S2;
/* now new sx,sy,sz */
Vars.cur_sx = Vars.cur_sz * Vars.dsx;
Vars.cur_sy = Vars.cur_sz * Vars.dsy;
Vars.cur_sz /= (1+Vars.dsx*Vars.dsx+Vars.dsy*Vars.dsy); /* always > 0 */
/* now try the two z signs for sz */
Vars.t1 = sqrt(Vars.cur_sz);
if (Vars.sy_max-Vars.sy_min)
Vars.i1 = (int)rint(Vars.nbins * (Vars.t1 -Vars.sz_min)/(Vars.sz_max-Vars.sz_min));
else
Vars.i1 = 0;
if (Vars.i1 < 0) Vars.i1 = 0;
if (Vars.i1 >= Vars.nbins) Vars.i1 = Vars.nbins - 1;
Vars.u1 = Vars.Source_sz[Vars.i1]/(Vars.Reference_sz[Vars.i1]+1);
Vars.t1 *= -1;
if (Vars.sy_max-Vars.sy_min)
Vars.i2 = (int)rint(Vars.nbins * (Vars.t1 -Vars.sz_min)/(Vars.sz_max-Vars.sz_min));
else
Vars.i2 = 0;
if (Vars.i2 < 0) Vars.i2 = 0;
if (Vars.i2 >= Vars.nbins) Vars.i2 = Vars.nbins - 1;
Vars.u2 = Vars.Source_sz[Vars.i2]/(Vars.Reference_sz[Vars.i2]+1);
if (Vars.u1 > Vars.u2)
Vars.cur_sz = -Vars.t1;
else
Vars.cur_sz = Vars.t1;
Vars.Flag_Recycle |= (DEFS.MOD_SX|DEFS.MOD_SY|DEFS.MOD_SZ);
}
else
{
if (Vars.Flag_Type & DEFS.DO_S)
{
if (Vars.Flag_Recycle & (DEFS.MOD_SX|DEFS.MOD_SY|DEFS.MOD_SZ))
{ /* now try to keep polarisation distribution */
Vars.cur_sx = Vars.dsx; /* may have been translated in vx,vy,vz 3 tests up-there */
Vars.cur_sy = Vars.dsy;
Vars.cur_sz = Vars.dsz;
Vars.t1 = Vars.S2 - Vars.cur_sz*Vars.cur_sz - Vars.cur_sy*Vars.cur_sy;
Vars.t2 = Vars.S2 - Vars.cur_sz*Vars.cur_sz - Vars.cur_sx*Vars.cur_sx;
Vars.t3 = Vars.S2 - Vars.cur_sx*Vars.cur_sx - Vars.cur_sy*Vars.cur_sy;
/* we affect the component wich is the most optimized (largest Source/Ref) */
if ((Vars.sx_max-Vars.sx_min) && (Vars.t1 > 0))
{
Vars.t1 = sqrt(Vars.t1);
if (vx < 0) Vars.t1 = -Vars.t1;
Vars.i1 = (int)rint(Vars.nbins * (Vars.t1 -Vars.sx_min)/(Vars.sx_max-Vars.sx_min));
if (Vars.i1 < 0) Vars.i1 = 0;
if (Vars.i1 >= Vars.nbins) Vars.i1 = Vars.nbins - 1;
Vars.u1 = Vars.Source_sx[Vars.i1]/(Vars.Reference_sx[Vars.i1]+1);
}
else
Vars.u1 = 0;
if ((Vars.sy_max-Vars.sy_min) && (Vars.t2 > 0))
{
Vars.t2 = sqrt(Vars.t2);
if (vy < 0) Vars.t2 = -Vars.t2;
Vars.i2 = (int)rint(Vars.nbins * (Vars.t2 -Vars.sy_min)/(Vars.sy_max-Vars.sy_min));
if (Vars.i2 < 0) Vars.i2 = 0;
if (Vars.i2 >= Vars.nbins) Vars.i2 = Vars.nbins - 1;
Vars.u2 = Vars.Source_sy[Vars.i2]/(Vars.Reference_sy[Vars.i2]+1);
}
else
Vars.u2 = 0;
if ((Vars.sz_max-Vars.sz_min) && (Vars.t3 > 0))
{
Vars.t3 = sqrt(Vars.t3);
if (vz < 0) Vars.t3 = -Vars.t3;
Vars.i3 = (int)rint(Vars.nbins * (Vars.t3 -Vars.sz_min)/(Vars.sz_max-Vars.sz_min));
if (Vars.i3 < 0) Vars.i3 = 0;
if (Vars.i3 >= Vars.nbins) Vars.i3 = Vars.nbins - 1;
Vars.u3 = Vars.Source_sz[Vars.i3]/(Vars.Reference_sz[Vars.i3]+1);
}
else
Vars.u3 = 0;
if ((Vars.u1 > Vars.u2) && (Vars.u1 > Vars.u3))
{
Vars.cur_sx = Vars.t1;
Vars.index_sx = Vars.i1;
Vars.Flag_Recycle |= DEFS.MOD_SX;
Vars.index = -1;
}
if ((Vars.u2 > Vars.u1) && (Vars.u2 > Vars.u3) )
{
Vars.cur_sy = Vars.t2;
Vars.index_sy = Vars.i2;
Vars.Flag_Recycle |= DEFS.MOD_SY;
Vars.index = -1;
}
if ((Vars.u3 > Vars.u1) && (Vars.u3 > Vars.u1))
{
Vars.cur_sz = Vars.t3;
Vars.index_sz = Vars.i3;
Vars.Flag_Recycle |= DEFS.MOD_SZ;
Vars.index = -1;
}
} /* end if Vars.Flag_Recycle */
} /* else DO_S */
} /* end if DO_DIVS else DO_S */
sx = Vars.cur_sx;
sy = Vars.cur_sy;
sz = Vars.cur_sz;
/* neutron has passed ! */
if (Vars.Source_vx[Vars.index_vx]
&& Vars.Source_vy[Vars.index_vy]
&& Vars.Source_vz[Vars.index_vz]
&& Vars.Source_x[Vars.index_x]
&& Vars.Source_y[Vars.index_y]
&& Vars.Source_sx[Vars.index_sx]
&& Vars.Source_sy[Vars.index_sy]
&& Vars.Source_sz[Vars.index_sz]
&& Vars.Reference_vx[Vars.index_vx]
&& Vars.Reference_vy[Vars.index_vy]
&& Vars.Reference_vz[Vars.index_vz]
&& Vars.Reference_x[Vars.index_x]
&& Vars.Reference_y[Vars.index_y]
&& Vars.Reference_sx[Vars.index_sx]
&& Vars.Reference_sy[Vars.index_sy]
&& Vars.Reference_sz[Vars.index_sz])
{
Vars.t1 = 1;
/*
* good neutrons have an improved distribution, so Ref/Source < 1
* unmodified (form Ref kept fraction) neutrons have Passing < Ref*Vars.dkeep.
* their weight should be about 1/keep for each variable
* at the end there will be of those :
* 2*Vars.dstep*Vars.dkeep + (1-2*Vars.dstep)*Vars.dkeep
* = Vars.dkeep % of unmodified neutrons
* the remaining part (1-Vars.dkeep neutrons) should have an
* integrated flux of (1-Vars.dkeep)
*/
if (Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV))
{
Vars.t2 = Vars.Reference_vx[Vars.index_vx]/Vars.Source_vx[Vars.index_vx];
if (Vars.t2 < 1) Vars.good_vx = Vars.index_vx;
Vars.t1 *= Vars.t2;
Vars.t2 = Vars.Reference_vy[Vars.index_vy]/Vars.Source_vy[Vars.index_vy];
if (Vars.t2 < 1) Vars.good_vy = Vars.index_vy;
Vars.t1 *= Vars.t2;
Vars.t2 = Vars.Reference_vz[Vars.index_vz]/Vars.Source_vz[Vars.index_vz];
if (Vars.t2 < 1) Vars.good_vz = Vars.index_vz;
Vars.t1 *= Vars.t2;
}
if (Vars.Flag_Type & DEFS.DO_XY)
{
Vars.t2 = Vars.Reference_x[Vars.index_x]/Vars.Source_x[Vars.index_x];
if (Vars.t2 < 1) Vars.good_x = Vars.index_x;
Vars.t1 *= Vars.t2;
Vars.t2 = Vars.Reference_y[Vars.index_y]/Vars.Source_y[Vars.index_y];
if (Vars.t2 < 1) Vars.good_y = Vars.index_y;
Vars.t1 *= Vars.t2;
}
if (Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS))
{
Vars.t2 = Vars.Reference_sx[Vars.index_sx]/Vars.Source_sx[Vars.index_sx];
if (Vars.t2 < 1) Vars.good_sx= Vars.index_sx;
Vars.t1 *= Vars.t2;
Vars.t2 = Vars.Reference_sy[Vars.index_sy]/Vars.Source_sy[Vars.index_sy];
if (Vars.t2 < 1) Vars.good_sy= Vars.index_sy;
Vars.t1 *= Vars.t2;
Vars.t2 = Vars.Reference_sz[Vars.index_sz]/Vars.Source_sz[Vars.index_sz];
if (Vars.t2 < 1) Vars.good_sz= Vars.index_sz;
Vars.t1 *= Vars.t2;
}
if (Vars.Flag_Recycle) { Vars.n_redirect++; }
/* now normalize to initial distribution */
if (fabs(Vars.Smoothed_Weigth - (double)Vars.Phase_Counts) >= 1)
Vars.t1 *= ((double)mcget_ncount()-Vars.Smoothed_Weigth)/((double)mcget_ncount()-(double)Vars.Phase_Counts);
Vars.cur_p *= Vars.t1;
p = Vars.cur_p;
SCATTER;
}
else
ABSORB; /* can't modify neutron weight -> eject */
Vars.Passing_vx[Vars.index_vx]++;
Vars.Passing_vy[Vars.index_vy]++;
Vars.Passing_vz[Vars.index_vz]++;
Vars.Passing_x[Vars.index_x]++;
Vars.Passing_y[Vars.index_y]++;
Vars.Passing_sx[Vars.index_sx]++;
Vars.Passing_sy[Vars.index_sy]++;
Vars.Passing_sz[Vars.index_sz]++;
Vars.Passing_Counts++;
Vars.Passing_Flux += p;
} /* end if DEFS.PHASE_OPTIM */
} /* end if (Vars.Phase != DEFS.PHASE_UNACTIVATE) */
/* end trace */
%}
FINALLY
%{
FILE *hfile;
if (Vars.Flag_Verbose && (Vars.Phase != DEFS.PHASE_UNACTIVATE))
{
printf("Source_Optimizer: End of optimization (%s)\n", NAME_CURRENT_COMP);
printf("Source_Optimizer: Vars.Normal_Monitor_Counts = %i, Vars.Total_Monitor_Counts = %i \n",Vars.Normal_Monitor_Counts, Vars.Total_Monitor_Counts);
if (Vars.Normal_Monitor_Counts != 0)
printf("Source_Optimizer: Optimizer speedup : %.3g \n", (double)(Vars.Total_Monitor_Counts/(Vars.Normal_Monitor_Counts*(double)mcget_ncount()/Vars.Phase_Counts)));
printf("Source_Optimizer: Number of redirections : %i\n",Vars.n_redirect);
printf("Counts : reference = %i, passing = %i, monitor = %.1f\n", Vars.Reference_Counts, Vars.Passing_Counts, Vars.Monitor_Counts);
printf("Flux : reference = %.2g, passing = %.2g, monitor = %.2g\n", Vars.Reference_Flux, Vars.Passing_Flux, Vars.Monitor_Flux);
}
if ((Vars.Phase != DEFS.PHASE_UNACTIVATE) && (strlen(Vars.file) > 0))
{
if (strchr(Vars.file,'.') == NULL) strcat(Vars.file, ".src");
hfile = fopen(Vars.file, "w");
if(!hfile)
{
fprintf(stderr, "Error: %s : could not open output file '%s'\n", NAME_CURRENT_COMP, Vars.file);
}
else
{
if (Vars.Flag_Verbose) printf("Source_Optimizer: %s write source description file %s.\n", NAME_CURRENT_COMP, Vars.file);
fprintf(hfile,"# Instrument-source: %s\n", instrument_source);
fprintf(hfile,"# type: array_2d(%i,6) \n",Vars.nbins);
fprintf(hfile,"# component: %s\n", NAME_CURRENT_COMP);
fprintf(hfile,"# title: General Optimizer distributions\n");
fprintf(hfile,"# filename: '%s'\n",Vars.file);
fprintf(hfile,"# variables: x nx y ny ");
if (Vars.Flag_Type & DEFS.DO_V) fprintf(hfile,"vx nvx vy nvy vz nvz ");
if (Vars.Flag_Type & DEFS.DO_DIVV) fprintf(hfile,"vx/v2 ndivvx vy/v2 ndivvy v2 nv2 ");
if (Vars.Flag_Type & DEFS.DO_S) fprintf(hfile,"sx nsx sy nsy sz nsz ");
if (Vars.Flag_Type & DEFS.DO_DIVS) fprintf(hfile,"sx/s2 ndivsx sy/s2 ndivsy s2 ns2 ");
fprintf(hfile,"\n");
fprintf(hfile,"# xvar: (x y ");
if (Vars.Flag_Type & DEFS.DO_V) fprintf(hfile,"vx vy vz ");
if (Vars.Flag_Type & DEFS.DO_DIVV) fprintf(hfile,"vx/v2 vy/v2 v2 ");
if (Vars.Flag_Type & DEFS.DO_S) fprintf(hfile,"sx sy sz ");
if (Vars.Flag_Type & DEFS.DO_DIVS) fprintf(hfile,"sx/s2 sy/s2 s2 ");
fprintf(hfile,")\n");
fprintf(hfile,"# yvar: (nx ny ");
if (Vars.Flag_Type & DEFS.DO_V) fprintf(hfile,"nvx nvy nvz ");
if (Vars.Flag_Type & DEFS.DO_DIVV) fprintf(hfile,"n(vx/v2) n(vy/v2) nv2 ");
if (Vars.Flag_Type & DEFS.DO_S) fprintf(hfile,"nsx nsy nsz ");
if (Vars.Flag_Type & DEFS.DO_DIVS) fprintf(hfile,"n(sx/s2) n(sy/s2) ns2 ");
fprintf(hfile,")\n");
fprintf(hfile,"# xlabel: 'Distributions'\n");
fprintf(hfile,"# ylabel: 'Counts'\n");
if (Vars.Normal_Monitor_Counts != 0)
fprintf(hfile,"# Optimizer speedup estimate: %.3g [Monitor Normal counts %i (extrapolated), Optimized %i ]\n", (double)(Vars.Total_Monitor_Counts/(Vars.Normal_Monitor_Counts*(double)mcget_ncount()/Vars.Phase_Counts)),(long)ceil(Vars.Normal_Monitor_Counts*(double)mcget_ncount()/Vars.Phase_Counts), Vars.Total_Monitor_Counts);
fprintf(hfile,"# Optimizer options: ");
if (Vars.Flag_Continuous) fprintf(hfile,"continuous "); else fprintf(hfile,"fixed ");
if (Vars.Flag_Auto) fprintf(hfile,"auto ");
if (Vars.Flag_Smooth) fprintf(hfile,"smooth ");
if (Vars.Flag_Verbose) fprintf(hfile,"verbose ");
if (Vars.Flag_Type & DEFS.DO_XY) fprintf(hfile,"SetXY ");
if (Vars.Flag_Type & DEFS.DO_DIVV) fprintf(hfile,"SetDivV ");
if (Vars.Flag_Type & DEFS.DO_V) fprintf(hfile,"SetV ");
if (Vars.Flag_Type & DEFS.DO_S) fprintf(hfile,"SetS ");
if (Vars.Flag_Type & DEFS.DO_DIVS) fprintf(hfile,"SetDivS ");
fprintf(hfile,"bins=%i, step=%.2f, keep=%.2f ", Vars.nbins, Vars.dstep, Vars.keep_target);
fprintf(hfile,"\n");
fprintf(hfile,"# Redirected neutrons: %i (%.2f %%)\n",Vars.n_redirect,(double)(100.0*Vars.n_redirect/mcget_ncount()));
fprintf(hfile,"# data: Optimized Source (%.1f Counts, Flux %.4g)\n", Vars.Monitor_Counts, Vars.Monitor_Flux);
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{
fprintf(hfile,"%10.4g ",(Vars.x_min+((Vars.index+0.5)/Vars.nbins)*(Vars.x_max - Vars.x_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_x[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.y_min+((Vars.index+0.5)/Vars.nbins)*(Vars.y_max - Vars.y_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_y[Vars.index]);
if (Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV))
{
fprintf(hfile,"%10.4g ",(Vars.vx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vx_max - Vars.vx_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_vx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vy_max - Vars.vy_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_vy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vz_max - Vars.vz_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_vz[Vars.index]);
}
if (Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS))
{
fprintf(hfile,"%10.4g ",(Vars.sx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sx_max - Vars.sx_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_sx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sy_max - Vars.sy_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_sy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sz_max - Vars.sz_min)));
fprintf(hfile,"%10.4g\t",Vars.Source_sz[Vars.index]);
}
fprintf(hfile,"\n");
}
fprintf(hfile,"# data: Reference Source (%i Counts, Flux %.4g)\n", Vars.Reference_Counts, Vars.Reference_Flux);
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{
fprintf(hfile,"%10.4g ",(Vars.x_min+((Vars.index+0.5)/Vars.nbins)*(Vars.x_max - Vars.x_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_x[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.y_min+((Vars.index+0.5)/Vars.nbins)*(Vars.y_max - Vars.y_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_y[Vars.index]);
if (Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV))
{
fprintf(hfile,"%10.4g ",(Vars.vx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vx_max - Vars.vx_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_vx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vy_max - Vars.vy_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_vy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vz_max - Vars.vz_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_vz[Vars.index]);
}
if (Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS))
{
fprintf(hfile,"%10.4g ",(Vars.sx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sx_max - Vars.sx_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_sx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sy_max - Vars.sy_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_sy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sz_max - Vars.sz_min)));
fprintf(hfile,"%10.4g\t",Vars.Reference_sz[Vars.index]);
}
fprintf(hfile,"\n");
}
fprintf(hfile,"# data: Passing (%i Counts, Flux %.4g)\n", Vars.Passing_Counts, Vars.Passing_Flux);
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{
fprintf(hfile,"%10.4g ",(Vars.x_min+((Vars.index+0.5)/Vars.nbins)*(Vars.x_max - Vars.x_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_x[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.y_min+((Vars.index+0.5)/Vars.nbins)*(Vars.y_max - Vars.y_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_y[Vars.index]);
if (Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV))
{
fprintf(hfile,"%10.4g ",(Vars.vx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vx_max - Vars.vx_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_vx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vy_max - Vars.vy_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_vy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vz_max - Vars.vz_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_vz[Vars.index]);
}
if (Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS))
{
fprintf(hfile,"%10.4g ",(Vars.sx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sx_max - Vars.sx_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_sx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sy_max - Vars.sy_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_sy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sz_max - Vars.sz_min)));
fprintf(hfile,"%10.4g\t",Vars.Passing_sz[Vars.index]);
}
fprintf(hfile,"\n");
}
fprintf(hfile,"# data: New_Source\n");
for (Vars.index = 0; Vars.index < Vars.nbins; Vars.index++)
{
fprintf(hfile,"%10.4g ",(Vars.x_min+((Vars.index+0.5)/Vars.nbins)*(Vars.x_max - Vars.x_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_x[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.y_min+((Vars.index+0.5)/Vars.nbins)*(Vars.y_max - Vars.y_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_y[Vars.index]);
if (Vars.Flag_Type & (DEFS.DO_V|DEFS.DO_DIVV))
{
fprintf(hfile,"%10.4g ",(Vars.vx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vx_max - Vars.vx_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_vx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vy_max - Vars.vy_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_vy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.vz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.vz_max - Vars.vz_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_vz[Vars.index]);
}
if (Vars.Flag_Type & (DEFS.DO_S|DEFS.DO_DIVS))
{
fprintf(hfile,"%10.4g ",(Vars.sx_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sx_max - Vars.sx_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_sx[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sy_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sy_max - Vars.sy_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_sy[Vars.index]);
fprintf(hfile,"%10.4g ",(Vars.sz_min+((Vars.index+0.5)/Vars.nbins)*(Vars.sz_max - Vars.sz_min)));
fprintf(hfile,"%10.4g\t",Vars.New_Source_sz[Vars.index]);
}
fprintf(hfile,"\n");
}
fclose(hfile);
}
Vars.Monitor_Number = 0; /* ask Monitor_Optimizer to free arrays */
}
%}
MCDISPLAY
%{
circle("xy",0,0,0,0.1);
%}
END
|