File: RITA-II.instr

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (1872 lines) | stat: -rw-r--r-- 75,167 bytes parent folder | download
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
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright 1997-2011, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: RITA-II @ PSI with hkl calculator
*
* %Identification
* Written by: <a href="mailto:udby@fys.ku.dk">Linda Udby</a>  and <a href="mailto:pkwi@fysik.dtu.dk">Peter Willendrup</a>
* Date: 2012
* Origin: <a href="http://www.risoe.dk">Ris&oslash; (Denmark)</a>
* %INSTRUMENT_SITE: PSI
*
* RITA type triple-axis spectrometer (TAS)
*
* %Description
* This instrument is model of RITA-II at the RNR13 guide at SINQ, PSI as of 2009. The energy and q-resolution as been verified against measured data in 2- and 3-axis modes. See Udby et al., Nuclear Instruments and Methods 634 (2011) s138-.<br>
* The model is based on the following components in downstream order
* <ul>
* <li> The SINQ source in 2009 - tested up to Ei=15 meV versus measurements. The spectrum of genereated rays is controlled by the BPL and BPH parameters.</li>
* <li> The RNR13 guide with average m-value based on color-codes on guide pieces and gaps as measured/from technical drawings.</li>
* <li> The Druckal monochromator: 5 slabs of PG(002) with vertically focusing option. The horizontal rotation angle (A1) and  scattering angle (A2) can either be set by user input or calculated automatically from EI/KI. </li>
* <li> Optional filter after the monochromator, before the sample. </li>
* <li> Variable linear collimator after the monochromator, before the sample. </li>
* <li> Optional perspex attenuation. </li>
* <li> Inbound Diaphagm slit. </li>
* <li> Sample: Incoherent scatterer, powder or single crystal from reflection list. </li>
* <li> Outbound Diaphragm slit. </li>
* <li> Optional filter after the sample, with radial collimator.</li>
* <li> 9 - blade analyser: Base-rotation (AA5) and individual blade angles (C1-C9) can either be set by user or calculated automatically from EF/KF. </li>
* <li> Coarse collimator: Removes cross-talk by allowing only passage of scattered neutrons from specified blade in each channel.</li>
* <li> Detector: PSD detector with 100% efficiency and specified point-spread function from measurements.</li>
* <li> Windows: Monitors the neutrons in each electronically specified window [XWinMin,XWinMax;YWinMin,YWinMax].</li>
* </ul>
* EXAMPLES
* <ul>
* <li>Generating a virtual source </li>
* mcrun RITA-II.instr -n3e6  BPL=0.97 BPH=1.03 EI=5 EF=5 COLL_MS=40 SAMPLE=1 OUTFILTER=0 REP=10 VIRTUALOUT=1 -d  RITA-II_Vsource40 <br>
* <li>Vanadium sample, no filters, seeing 1st-3rd order neutrons </li>
* mcrun RITA-II.instr -n3e6 BPL=0.30 BPH=1.03 EI=5 EF=5 SAMPLE=1 INFILTER=0 OUTFILTER=0  REP=10 -d RITA-II_Vanadium <br>
* <li>Powder sample (default sapphire), simulation 1) through entire instrument or  2)from virtual source 3) in 2-axis mode, with particular slit settings scaling the source yield to 2008 values </li>
* 1) mcrun RITA-II.instr -n3e6 EF=5 EN=0 SAMPLE=2 COLL_MS=40 SAMPLEFILE="Al2O3_sapphire.lau" BARNS=0 SOURCEFILE="RITA-II_Vsource40/Vin_default.dat" REP=10 -d RITA-II_40_powder<br>
* 2) mcrun RITA-II.instr EI=5 EF=5 SAMPLE=2 SAMPLEFILE="Al2O3_sapphire.lau" BARNS=0 SOURCEFILE="RITA-II_Vsource40/Vin_default.dat" VIRTUALIN=1 REP=3 -d RITA-II_VINsource40_powder <br>
* 3) mcrun RITA-II.instr -n 1e7 ITAR=0.71 COLL_MS=19.6 BPL=0.97 BPH=1.03 EI=5 EF=5 QH=0 QK=0 QL=0 QM=1 A3=0.001 A4=-71 AA5=-90 A6=1e-3 MST=25 MSB=25 MSL=10 MSR=10 SAMPLE=2 SAMPLEFILE="Al2O3_sapphire.lau" SST=25 SSB=25 SSL=10 SSR=10 OUTFILTER=0 OUTFILTERFILE=Be.trm COARSE=1 REP=5 LC=6 RC=4 REP=1 BARNS=0 ANAFORCE=1 -d RITA-II_Al2O3_2axis<br>
* <li>Single crystal sample. Define first lattice vector (a) along -x (to the right) , second lattice vector (b) along z (forward), third lattice vector (c) along y (up). </li>
*  mcrun RITA-II.instr -n1e6 BPL=0.97 BPH=1.03 EI=5 EF=5 SAMPLE=3 REP=10 QH=2 QM=0 AS=4.95 BS=4.95 CS=4.95 AAX=-4.95 BBZ=4.95 CCY=4.95 AH=0 AK=1 AL=0  BH=1 BK=0 BL=0 SAMPLEFILE="Pb.laz" -d RITA-II_SXPb200 <br>
* <li> Phonon sample, inelastic scattering, no Bragg peaks in sample </li>
* mcrun RITA-II.instr -n1e6 L0=3.418 BPL=0.97 BPH=1.03 EI=7 EF=5 SAMPLE=4 REP=10 QH=2 QK=0.16 QM=0 AS=4.95 BS=4.95 CS=4.95 AAX=-4.95 BBZ=4.95 CCY=4.95 AH=0 AK=1 AL=0  BH=1 BK=0 BL=0 SAMPLEFILE="Pb.laz" -d RITA-II_SXPb200_2meV <br>
* </ul>
* %Parameters
* ITAR: [mA]               Relative neutron yield from the spallation target. Value relative to 2009
* EI: [meV]                Incoming neutron energy. Also used to set range of energy monitors
* EF: [meV]                Outgoing neutron energy. Also used to set range of energy monitors
* QH: [rlu]                Measurement QH position in crystal
* QK: [rlu]                Measurement QK position in crystal
* QL: [rlu]                Measurement QL position in crystal
* EN: [meV]                Energy transferred in crystal
* QM: [Angs-1]             Wavevector transferred in sample, use QM=0 if (QH,QK,QL) is specified
* A1: [deg]                Monohromator rotation angle
* A2: [deg]                Monohromator take-off angle
* A3: [deg]                Sample rotation angle
* A4: [deg]                Sample take-off angle
* AA5: [deg]               Analyzer rack rotation angle
* A6: [deg]                Analyzer take-off angle
* C1: [deg]                Analyzer blade 1 position
* C2: [deg]                Analyzer blade 2 position
* C3: [deg]                Analyzer blade 3 position
* C4: [deg]                Analyzer blade 4 position
* C5: [deg]                Analyzer blade 5 position
* C6: [deg]                Analyzer blade 6 position
* C7: [deg]                Analyzer blade 7 position
* C8: [deg]                Analyzer blade 8 position
* C9: [deg]                Analyzer blade 9 position
* COLL_MS: [deg]           Primary collimator max divergence
* MONO_N: [1]              Order of reflection used on mono
* MONOFORCE: [1]           If set, monochromator is flat
* L0: [Angs]               Centre of generated wavelength distribution from source
* BPL:  Band Pass Low factor, multiplied on source wavelength L0 to allow neutrons with wavelengths with lambda {BPL*L0,BPH*L0} to be traced from the source.
* BPH:  Band Pass High factor, multiplied on source wavelength L0 to allow neutrons with wavelengths with lambda {BPL*L0,BPH*L0} to be traced from the source.
* MST: [m]                 Monochromator TOP slit
* MSB: [m]                 Monochromator BOTTOM slit
* MSR: [m]                 Monochromator RIGHT slit
* MSL: [m]                 Monochromator LEFT slit
* SST: [m]                 Sample TOP slit
* SSB: [m]                 Sample BOTTOM slit
* SSR: [m]                 Sample RIGHT slit
* SSL: [m]                 Sample LEFT slit
* SM: [1]
* SS: [1]                  Scattering configuration signs. 'W' is SM=1,SS=-1,SA=1
* SA: [1]
* OUTFILTER: [1]           Flag to indicate if the outbound filter is in or out
* OUTFILTERFILE: [string]  An input file of wavevector vs transmission to use with outgoing filter
* INFILTER: [1]            Flag to indicate if mono-block filter is in or out
* INFILTERFILE: [string]   An input file of wavevector vs transmission to use with incoming filter
* COARSE: [1]              Flag to indicate if Detector collimator is in or out
* REP: [1]                 Repetition factor of virtual_input or Monochromator/Sample/Analyzer SPLITs
* ANAFORCE: [1]            If set the analyzer is forced flat
* MONO_MOS_H: [min]        Monochromator, horizontal mosaicity
* MONO_MOS_V: [min]        Monochromator, vertical mosaicity
* LC: [deg]                Detector-collimator angle of the leftmost blade
* RC: [deg]                Detector-collimator angle of the rightmost blade
* PERSPEX: [1]             Flag to indicate if perspex attenuator is in or out
* PTHICK: [m]              Thickness of perspex attenuator
* VIRTUALOUT: [1]          If set, flag indicates that a Virtual source is created after the monochromator collimator
* VIRTUALIN: [1]           If set, flag to indicates that Virtual source is used after the monochromator collimator
* SOURCEFILE: [string]     Name of file to be used/genereated as virtual input/output
* verbose: [1]             print TAS configuration. 0 to be quiet
* SAMPLE: [1]              1 is incoherent scatterer, 2 is powder, 3 is single crystal.
* SAMPLEFILE: [string]     Name of samplefile (with reflectionlist etc)
* SAMPLESIZE: [m]          Length, height and width of single crystal sample, or radius and height of phonon sample
* MOS: []                 Isotropic 'mosaicity' of single crystal
* DD_D: []                spead of lattice parameter
* AS: [Angs]               Sample lattice parameter A
* BS: [Angs]               Sample lattice parameter B
* CS: [Angs]               Sample lattice parameter C
* AA: [deg]                Angle between lattice vectors B,C
* BB: [deg]                Angle between lattice vectors C,A
* CC: [deg]                Angle between lattice vectors A,B
* AH: [rlu]                First reciprocal lattice vector in scattering plane, X
* AK: [rlu]                First reciprocal lattice vector in scattering plane, Y
* AL: [rlu]                First reciprocal lattice vector in scattering plane, Z
* BH: [rlu]                Second reciprocal lattice vector in scattering plane, X
* BK: [rlu]                Second reciprocal lattice vector in scattering plane, Y
* BL: [rlu]                Second reciprocal lattice vector in scattering plane, Z
* AAX: []
* AAY: []                 Orientation vector of unit cell, single_crystal
* AAZ: []
* BBX: []
* BBY: []                 Orientation vector of unit cell, single_crystal
* BBZ: []
* CCX: []
* CCY: []                 Orientation vector of unit cell, single_crystal
* CCZ: []
* TILT: [deg]              Tilt angle out of plane of the sample scattering vector out of plane (rotation around z of the A3 arm)
* BARNS: [1]               If set the flag indicates that reflection list structure factors are in units of barns, otherwise fm^2
* BPL: []
* BPH: []
* MOS: []
* DD_D: []
* AAX: []
* AAY: []
* AAZ: []
* BBX: []
* BBY: []
* BBZ: []
* CCX: []
* CCY: []
* CCZ: []
*
* %Link
* Rescal for Matlab at http://www.ill.fr/tas/matlab
* %Link
* Restrax at http://omega.ujf.cas.cz/restrax/
* %End
*******************************************************************************/
DEFINE INSTRUMENT RITA_II( ITAR=1.0,L0=4.045,BPL=0.97,BPH=1.03, MONO_N=1,MONOFORCE=0,MONO_MOS_H=37,
  MONO_MOS_V=37, EI=0,EF=0,EN=0, SM=1,SS=-1,SA=1, QH=0, QK=0, QL=0, QM=1.8051,
  AS=4.95, BS=4.95, CS=4.95, AA=90, BB=90, CC=90, AH=0, AK=1, AL=0, BH=1, BK=0, BL=0,
  INFILTER=0, string INFILTERFILE="Be.trm", COLL_MS=80, MST=40, MSB=40, MSL=40, MSR=40,
  PTHICK=1e-3,PERSPEX=0, SAMPLE=1, string SAMPLEFILE="default",MOS=100,DD_D=1e-3,
  SAMPLESIZE=0.01,BARNS=1,
  AAX=-4.95, AAY=0, AAZ=0, BBX=0, BBY=0, BBZ=4.95, CCX=0, CCY=4.95, CCZ=0,
  A1=0,A2=0,A3=0,A4=0,A6=0,TILT=0,  SST=100, SSB=100, SSL=100, SSR=100,
  OUTFILTER=1, string OUTFILTERFILE="Be.trm", ANAFORCE=0,
  AA5=0,C1=0,C2=0,C3=0,C4=0,C5=0,C6=0,C7=0,C8=0,C9=0, COARSE=1,LC=4, RC=3, REP=1,
  VIRTUALOUT=0, VIRTUALIN=0, string SOURCEFILE="Vin_default.dat",verbose=1 )

DECLARE
%{
/* The following is from RITA2front, Kim Lefmann / Linda Udby */


/* Static values... */
double l0,lmin,lmax;
double emini,emaxi;
double eminf,emaxf;
double nefchan;
double neichan;
double lI,KI,KF;

/* Internal variable for SPLIT repetitions */
int SPLITREP;
int SPLITMREP;
int SPLITAREP;
#pragma acc declare create (SPLITREP,SPLITMREP,SPLITAREP)
/* Guide element parameters*/
double angleGuideCurved;
double R = 0.88;
double R0 = 0.995;
double Qc = 0.0217;
double W = 0;
double  M= 2.15;
double ALPHA;

/* Monochromator material parameters */
double mono_q = 1.87325;
double mono_r0 = 0.8;
double DM;      /*d-spacing monochromator*/
double mono_mosaic_h;
double mono_mosaic_v;
/* Monochromator curvature parmeters */
//double u,v;
double dms;  /* Target vector for focusing */
double tx,tz;    /* Target vector for focusing */
double sintm,sinta;
double rv; /* Mono focusing parameter */
/* Monochromator geometrical parameters */
/* Size of monochromator blades are hard-coded in the component*/
double mono_d = 0.026;  /* Distance between mono blades. From drawings */
double dmc; /* Distance monochromator to front of collimator. Was 0.32 from Stine */
double rmh = 0.58; /* Radius of monohousing. Measured 2008/11/05 */
double lc = 0.198; /*Length of monocollimator. Measured 2008/11/05*/

/* RITA Monitor efficiency 1.7e-5 */
double EFF = 1.7e-5;


/* Sample parameters */
double d_sample_slit = 0.35; /* Measured 2008/11/05, was 25 cm from Stine */
double d_sample_filter = 0.51; /*To centre of filter. Measured 2008/11/05, was to filter front 25 cm from Stine */
double dsa = 1.195;  /* distance sample-analyzer (m). Was 1.256 from Stine */
double dsb ; /*distance sample to blade, depends on analyser rack postion*/
double dmv = 1.00; /* distance monochromator to virtual out */
//double dvs = 0.67; /* distance virtual in to sample. dmv+dvs=1.67=rmh+1.09. Ma15 setting */
double dvs = 0.54; /* distance virtual in to sample. dmv+dvs=1.54 */
double is_incoh;/* Random number pr. neutron event for incoherent V scattering */
/* Filenames for the sample comps: */
char *PowderFile;
char *SingleXFile;

/* Analyser material parameters*/
double ana_mosaic_h;
double ana_mosaic_v;
double ana_q = 1.87325;
double ana_r0 = 0.8;
double DA;    /* d-spacing analyser*/
/*Analyser geometrical parameters */
double ana_d = 0.025;  /* Width of analyser blades. From drawings */
double ana_h = 0.15;   /* Height of analyser blades. From drawings */
double dad = 0.338;  /* distance analyzer to detector front (m) */
double dadw = 0.3595; //= 0.3595;  /* distance analyzer to detector wire (m) */
double wan = 0.024;  /* width of analyzer blades , by ruler (m) */
double RR; //  = 0.3008; /* ratio of dadw and dsa*/
//double DA4; /*angle between blades*/
double A5; /* scattering angle for flat analyser*/
/* Declarations for 'Coarse Collimator' at the PSD detector surface */
int EntrySlit;
int ExitSlit;
double BladeThickness = 0.007;// detector coll after 2006, from drawings
double WindowSize = 0.025;
double BladeLength = 0.179;// detector coll after 2006, from drawings
double BladeHeight = 0.272;// detector coll after 2006, from drawings
//double BladeThickness = 0.002;// detector coll before 2006
//double BladeLength = 0.180;// detector coll before 2006
//double BladeHeight = 0.250;// detector coll before 2006
double FirstWindowSizeL;
double FirstWindowSizeR;
double deltaL;
int coarse;

/* Detector parameters */
//double det_width = 0.2735;/* was 0.3 from Stine*/
double det_width = 0.275;
double det_height =0.5;
double PSF = 0.0074/2.35;// FWHM=0.0074m, measured by C. Bahl NIMB 246, 452.
/* Electronic Window positions in the PSD */
int XwinMin[9];
int YwinMin[9];
int XwinMax[9];
int YwinMax[9];
#pragma acc declare create (XwinMin,YwinMin,XwinMax,YwinMax)




/* BEGIN declaring stuff for the HKL calculator adapted from templateTAS */
  struct sample_struct {
    double as, bs, cs; // Lattice parameters
    double aa, bb, cc; // Lattice angles
    double ax, ay, az; // First scattering plane vector
    double bx, by, bz; // Second scattering plane vector
  } sample;

  struct machine_hkl_struct {
    double dm, da;     // Mono and ana d-spacings
    double sm, ss, sa; // Mono, sample, ana angle signs
    double ki, kf, ei, ef; // Initial and Final wavevectors and energies
    double qh, qk, ql, en; // Momentum transfer and energy transfer in sample
  } machine_hkl;

  struct machine_real_struct {
    double a1,a2,a3,a4,a5,a6;
    double da4,aa5;
    double c1,c2,c3,c4,c5,c6,c7,c8,c9;
    double rmh, rmv, rah, rav;
    double qm, qs, qt[3];
    char   message[256];
  } machine_real;

  struct machine_real_struct qhkl2angles(
    struct sample_struct      sample,
    struct machine_hkl_struct machine_hkl,
    struct machine_real_struct machine_real) {

      /* code from TASMAD/t_rlp.F:SETRLP */
      double qhkl[3];
      double alpha[3];
      double a[3];
      double aspv[3][2];
      double cosa[3], sina[3];
      double cosb[3], sinb[3];
      double b[3], c[3], s[4][4];
      double vv[3][3], bb[3][3];
      double arg, cc;
      int i,j,k,l,m,n;
      char liquid_case=1;
      /* transfered parameters to local arrays */
      qhkl[0]   = machine_hkl.qh; /* HKL target */
      qhkl[1]   = machine_hkl.qk;
      qhkl[2]   = machine_hkl.ql;
      alpha[0]  = sample.aa; /* cell angles */
      alpha[1]  = sample.bb;
      alpha[2]  = sample.cc;
      a[0]      = sample.as; /* cell parameters */
      a[1]      = sample.bs;
      a[2]      = sample.cs;
      aspv[0][0]= sample.ax; /* cell axis A */
      aspv[1][0]= sample.ay;
      aspv[2][0]= sample.az;
      aspv[0][1]= sample.bx; /* cell axis B */
      aspv[1][1]= sample.by;
      aspv[2][1]= sample.bz;

      /* default return values */
      strcpy(machine_real.message, "");
      machine_real.a3 = machine_real.a4 = 0;
      machine_real.a1 = machine_real.a5 = 0;

      /* if using HKL positioning in crystal (QM = 0) */
      if (machine_real.qm <= 0) {
        liquid_case = 0;
        /* compute reciprocal cell */
        for (i=0; i< 3; i++)
          if (a[i] <=0) sprintf(machine_real.message, "Lattice parameters a[%i]=%g", i, a[i]);
          else {
            a[i]    /= 2*PI;
            alpha[i]*= DEG2RAD;
            cosa[i]  = cos(alpha[i]);
            sina[i]  = sin(alpha[i]);
          }
        cc = cosa[0]*cosa[0]+cosa[1]*cosa[1]+cosa[2]*cosa[2]; /* nprm */
        cc = 1 + 2*cosa[0]*cosa[1]*cosa[2] - cc;
        if (cc <= 0) sprintf(machine_real.message, "Lattice angles (AA,BB,CC) cc=%g", cc);
        else cc = sqrt(cc);

        if (strlen(machine_real.message)) return machine_real;

        /* compute bb */
        j=1; k=2;
        for (i=0; i<3; i++) {
          b[i] = sina[i]/(a[i]*cc);
          cosb[i] = (cosa[j]*cosa[k] - cosa[i])/(sina[j]*sina[k]);
          sinb[i] = sqrt(1 - cosb[i]*cosb[i]);
          j=k; k=i;
        }

        bb[0][0] = b[0];
        bb[1][0] = 0;
        bb[2][0] = 0;
        bb[0][1] = b[1]*cosb[2];
        bb[1][1] = b[1]*sinb[2];
        bb[2][1] = 0;
        bb[0][2] = b[2]*cosb[1];
        bb[1][2] =-b[2]*sinb[1]*cosa[0];
        bb[2][2] = 1/a[2];

        /* compute vv */
        for (k=0; k< 3; k++)
          for (i=0; i< 3; i++) vv[k][i] = 0;

        for (k=0; k< 2; k++)
          for (i=0; i< 3; i++)
            for (j=0; j< 3; j++)
              vv[k][i] += bb[i][j]*aspv[j][k];

        for (m=2; m>=1; m--)
          for (n=0; n<3; n++) {
            i = (int)fmod(m+1,3); j= (int)fmod(m+2,3);
            k = (int)fmod(n+1,3); l= (int)fmod(n+2,3);
            vv[m][n]=vv[i][k]*vv[j][l]-vv[i][l]*vv[j][k];
          }

        for (i=0; i< 3; i++) { /* compute norm(vv) */
          c[i]=0;
          for (j=0; j< 3; j++)
            c[i] += vv[i][j]*vv[i][j];
          if (c[i]>0) c[i] =  sqrt(c[i]);
          else {
            sprintf(machine_real.message, "Vectors A and B, c[%i]=%g", i, c[i]);
            return machine_real;
          }
        }

        for (i=0; i< 3; i++) /* normalize vv */
          for (j=0; j< 3; j++)
            vv[j][i] /= c[j];

        for (i=0; i< 3; i++) /* compute S */
          for (j=0; j< 3; j++) {
            s[i][j] = 0;
            for (k=0; k< 3; k++)
              s[i][j] += vv[i][k]*bb[k][j];
          }
        s[3][3]=1;
        for (i=0;  i< 3;  i++)  s[3][i]=s[i][3]=0;

        /* compute q modulus and transverse component */
        machine_real.qs = 0;
        for (i=0; i< 3; i++) {
          machine_real.qt[i] = 0;
          for (j=0; j< 3; j++) machine_real.qt[i] += qhkl[j]*s[i][j];
          machine_real.qs += machine_real.qt[i]*machine_real.qt[i];
        }
        if (machine_real.qs > 0) machine_real.qm = sqrt(machine_real.qs);
        else sprintf(machine_real.message, "Q modulus too small QM^2=%g", machine_real.qs);
      } else {
        machine_real.qs = machine_real.qm*machine_real.qm;
      }
      /* end if  qm <= 0 ********************************************* */

      /* positioning of monochromator and analyser */
      arg = PI/machine_hkl.dm/machine_hkl.ki;
      if (fabs(arg > 1))
        sprintf(machine_real.message, "Monochromator can not reach this KI. arg=%g", arg);
      else {
        if (machine_hkl.dm <= 0 || machine_hkl.ki <= 0)
          strcpy(machine_real.message, "Monochromator DM=0 or KI=0.");
        else
          machine_real.a1 = asin(arg)*RAD2DEG;
        machine_real.a1 *= machine_hkl.sm;
      }
      machine_real.a2=2*machine_real.a1;

      arg = PI/machine_hkl.da/machine_hkl.kf;
      if (fabs(arg > 1))
        sprintf(machine_real.message, "Analyzer can not reach this KF. arg=%g",arg);
      else {
        if (machine_hkl.da <= 0 || machine_hkl.kf <= 0)
          strcpy(machine_real.message, "Analyzer DA=0 or KF=0.");
        else
          machine_real.a5 = asin(arg)*RAD2DEG;
        machine_real.a5 *= machine_hkl.sa;
      }
      machine_real.a6=2*machine_real.a5;
      if (strlen(machine_real.message)) return machine_real;


      /* code from TASMAD/t_conv.F:SAM_CASE */
      arg = (machine_hkl.ki*machine_hkl.ki + machine_hkl.kf*machine_hkl.kf - machine_real.qs)
          / (2*machine_hkl.ki*machine_hkl.kf);
      if (fabs(arg) < 1)
        machine_real.a4 = RAD2DEG*acos(arg);
      else
        sprintf(machine_real.message, "Q modulus too big. Can not close triangle. arg=%g", arg);
      machine_real.a4 *= machine_hkl.ss;

      if (!liquid_case) { /* compute a3 in crystals */
        machine_real.a3 =
            -atan2(machine_real.qt[1],machine_real.qt[0])
            -acos( (machine_hkl.kf*machine_hkl.kf-machine_real.qs-machine_hkl.ki*machine_hkl.ki)
                  /(-2*machine_real.qm*machine_hkl.ki) );
        machine_real.a3 *= RAD2DEG*(machine_real.a4 > 0 ? 1 : -1 );
  //machine_real.a3 = machine_real.a3 -90;// Add by PW & LU


      }

/* Analyser angles */
/* Angle of the analyser rack aa5 */
RR = dadw/dsa;
machine_real.aa5 = -RAD2DEG*(acos((sin(DEG2RAD*machine_real.a6)-(cos(DEG2RAD*machine_real.a6)+RR)*sqrt(RR*RR+2*RR*cos(DEG2RAD*machine_real.a6)))/(1+RR*RR+2*RR*cos(DEG2RAD*machine_real.a6)))); // From Bahl et al. , NIMB 226 (2004)
/*printf("(RITA Analyzer Rack Angle: AA5=%.4g[deg])\n", machine_real.aa5);*/
/*A4 angle between blades */
dsb = sqrt(dsa*dsa + ana_d*ana_d - 2*dsa*ana_d*cos(DEG2RAD*machine_real.aa5));
 machine_real.da4 = RAD2DEG*asin(ana_d*sin(DEG2RAD*abs(machine_real.aa5))/dsb);
//machine_real.da4 = RAD2DEG*atan(ana_d*cos(DEG2RAD*machine_real.aa5)/dsa);
printf("(RITA Analyzer Angle between blades: DA4=%.4g[deg])\n", machine_real.da4);
/*Blade angle rotation */
 machine_real.c1= -machine_real.aa5+machine_real.a5-(1-5)*machine_real.da4;
 machine_real.c2= -machine_real.aa5+machine_real.a5-(2-5)*machine_real.da4;
 machine_real.c3= -machine_real.aa5+machine_real.a5-(3-5)*machine_real.da4;
 machine_real.c4= -machine_real.aa5+machine_real.a5-(4-5)*machine_real.da4;
 machine_real.c5= -machine_real.aa5+machine_real.a5-(5-5)*machine_real.da4;
 machine_real.c6= -machine_real.aa5+machine_real.a5-(6-5)*machine_real.da4;
 machine_real.c7= -machine_real.aa5+machine_real.a5-(7-5)*machine_real.da4;
 machine_real.c8= -machine_real.aa5+machine_real.a5-(8-5)*machine_real.da4;
 machine_real.c9= -machine_real.aa5+machine_real.a5-(9-5)*machine_real.da4;

    return machine_real;
  }
/* END declaring stuff for the HKL calculator adapted from templateTAS */



%}
/* end of DECLARE */

USERVARS
%{
  /* Order of scattering from the monochromator */
  double Mono_order;
  int AnaBlade;
  int BinX;
  int BinY;
%}


INITIALIZE %{

/* Source parameters */
//l0=9.045/sqrt(EI);
l0=L0;// fix 20100222 by LU since we want source independent of mono
lmin=BPL*l0;/* MONO_N is the order of the reflection */
lmax=BPH*l0;
printf("Source wavelength interval=%.4g - %.4g[Ang] \n",lmin, lmax);
/* Calculate min and max energy for source to use with energy monitors before sample */
emini=(9.045*9.045)/(lmax*lmax);
emaxi=(9.045*9.045)/(lmin*lmin);
//neichan=floor((emaxi-emini)/0.01);
printf("Source energy interval=%.4g - %.4g[meV] \n",emini, emaxi);
/* Calculate min and max energy for the detectors after the sample */
eminf=EF*0.95;
emaxf=EF*1.05;
//nefchan=floor((emaxf-eminf)/0.005);
printf("Detector energy interval=%.4g - %.4g[meV] \n",eminf, emaxf);

/* Only SPLIT if we are not running with VIRTUAL parms */
if (VIRTUALIN || VIRTUALOUT) {
  SPLITREP = 1;
  if (!VIRTUALOUT){
    SPLITMREP = 1;
  }else{
    SPLITMREP = REP;
  }
  if (!VIRTUALIN){
    SPLITAREP = 1;
  }else{
    SPLITAREP = REP;
  }
 } else {
  SPLITREP = REP;
  SPLITMREP = REP;
  SPLITAREP = REP;
}


/* Guide parameters */
angleGuideCurved=20.0/2408.0;
/* calculate mirror reflectivity slope */
 ALPHA=0;//(R0-R)/(Qc*(M-1));
printf("* GUIDE MIRROR ALPHA=%g [AA]\n", ALPHA);

/* Monochromator parameters */
double Vi, Vf;
double tmp=0;
char Qmode = 0;
DM = 2*PI/mono_q;
DA = 2*PI/ana_q;
mono_mosaic_h = MONO_MOS_H; /* MON_MOSAIC; */
mono_mosaic_v = MONO_MOS_V; /* MON_MOSAIC; */
dms = dmv+dvs;/* Distance between mono and sample*/
dmc = rmh-lc; /*distance monochromator to front of monocollimator*/
/* fix 08/02/2009 by LU */
/* Forcing monochromator to set value */
 if (MONOFORCE) {/* Flat mono */
   rv=0;
   printf("* MONOCHROMATOR IS VERTICALLY FOCUSING: RV=%g \n",rv);
 }
 else {rv=dms; /* curved mono */
 printf("* MONOCHROMATOR IS FLAT: RV=%g \n", rv);
 }


/* BEGIN HKL calculator adapted from templateTAS */

machine_real.a1 = A1;
machine_real.a2 = A2;
machine_real.a3 = A3;
machine_real.a4 = A4;
machine_real.a5 = A5;
machine_real.a6 = A6;

machine_real.c1 = -C1;
machine_real.c2 = -C2;
machine_real.c3 = -C3;
machine_real.c4 = -C4;
machine_real.c5 = -C5;
machine_real.c6 = -C6;
machine_real.c7 = -C7;
machine_real.c8 = -C8;
machine_real.c9 = -C9;

/* energy conservation */
 if (EI && EF) {
   EN = EI - EF;
   fprintf(stderr,"%s WARNING: EN is now set to %g since you provided both EI=%g (KI) and EF=%g (KF)\n", NAME_INSTRUMENT, EN, EI, EF);
 } else if (EI && !EF){
   EF = EI - EN;
   fprintf(stderr,"%s WARNING: EF is now set to %g since you provided both EI=%g (KI) and EN=%g\n", NAME_INSTRUMENT, EF, EI, EN);
   fprintf(stderr,"%s POSSIBLE ERROR: On RITA, EF is normally SET, EI calculated - are you sure about this?\n", NAME_INSTRUMENT);
 } else if (EF && !EI) {
   EI = EF + EN;
   fprintf(stderr,"%s WARNING: EI is now set to %g since you provided both EF=%g (KF) and EN=%g\n", NAME_INSTRUMENT, EI, EF, EN);
 } else {
   fprintf(stderr,"%s WARNING: Neither EI, EF nor EN defined: Energies are set from user angle input:\n", NAME_INSTRUMENT);
   l0 = 2 * DM * sin(DEG2RAD*A2/2)/MONO_N;
   EI = 9.045/l0;
   EI = EI*EI;
   fprintf(stderr,"%s: WARNING: EI has been adjusted to %g[meV]  (A2 = %g[deg])\n", NAME_INSTRUMENT, EI, A2);
   l0 = 2 * DA * sin(DEG2RAD*A6/2)/MONO_N;
   EF= 9.045/l0;
   EF = EF*EF;
   fprintf(stderr,"%s: WARNING: EF has been adjusted to %g[meV]  (A6 = %g[deg])\n", NAME_INSTRUMENT, EF, A2);
 }
 /* determine remaining neutron energies */
 Vi = SE2V*sqrt(EI);
 KI = V2K*Vi;
 Vf = SE2V*sqrt(EF);
 KF = V2K*Vf;

/* transfered sample parameters */
sample.aa = AA;
sample.bb = BB;
sample.cc = CC;
sample.as = AS;
sample.bs = BS;
sample.cs = CS;
sample.ax = AH;
sample.ay = AK;
sample.az = AL;
sample.bx = BH;
sample.by = BK;
sample.bz = BL;

/* transfered target parameters */
machine_hkl.ki = KI;
machine_hkl.kf = KF;
machine_hkl.ei = EI;
machine_hkl.ef = EF;
machine_hkl.qh = QH;
machine_hkl.qk = QK;
machine_hkl.ql = QL;
machine_hkl.en = EN;
machine_real.qm = QM;

if (QM || QH || QK || QL) {
  Qmode=1;
  fprintf(stderr,"%s: Running in HKL mode\n", NAME_INSTRUMENT);
} else {
  fprintf(stderr,"%s: Running in angle mode\n", NAME_INSTRUMENT);
}

if (verbose && Qmode) {
  printf("%s: Detailed TAS configuration\n", NAME_INSTRUMENT);
  printf("* Incoming beam: EI=%.4g [meV] KI=%.4g [Angs-1] Vi=%g [m/s]\n", EI, KI, Vi);
  printf("* Outgoing beam: EF=%.4g [meV] KF=%.4g [Angs-1] Vf=%g [m/s]\n", EF, KF, Vf);
}

/* transfered machine parameters */
/* For W configuartion of TAS: */
machine_hkl.sm = SM;
machine_hkl.ss = SS;
machine_hkl.sa = SA;
/* These two are actually constants, see top of INITIALIZE */
machine_hkl.dm = DM;
machine_hkl.da = DA;

if (Qmode) {
  machine_real = qhkl2angles(sample, machine_hkl, machine_real);
  if (strlen(machine_real.message)) {
    exit(fprintf(stderr, "%s: ERROR: %s [qhkl2angles]\n", NAME_INSTRUMENT, machine_real.message));
  }
}



if (A1 && (machine_real.a1 != A1)) {
   printf("Warning, resetting A1 from calculated %g to user provided %g\n",machine_real.a1,A1);
   machine_real.a1 = A1;
}
if (A2 && (machine_real.a2 != A2)) {
   printf("Warning, resetting A2 from calculated %g to user provided %g\n",machine_real.a2,A2);
   machine_real.a2 = A2;
}
if (A3 && (machine_real.a3 != A3)) {
   printf("Warning, resetting A3 from calculated %g to user provided %g\n",machine_real.a3,A3);
   machine_real.a3 = A3;
}
if (A4 && (machine_real.a4 != A4)) {
   printf("Warning, resetting A4 from calculated %g to user provided %g\n",machine_real.a4,A4);
   machine_real.a4 = A4;
}
if (A5 && (machine_real.a5 != A5)) {
   printf("Warning, resetting A5 from calculated %g to user provided %g\n",machine_real.a5,A5);
   machine_real.a5 = A5;
}
if (A6 && (machine_real.a6 != A6)) {
   printf("Warning, resetting A6 from calculated %g to user provided %g\n",machine_real.a6,A6);
   machine_real.a6 = A6;
}


if (AA5 && (machine_real.aa5 != AA5) && !ANAFORCE) {
   printf("Warning, resetting AA5 from calculated %g to user provided %g\n",machine_real.aa5,AA5);
   machine_real.aa5 = AA5;
   /*A4 angle between blades */
machine_real.da4 = RAD2DEG*atan(ana_d*cos(DEG2RAD*machine_real.aa5)/dsa);
/*Blade angle rotation */
 machine_real.c1= -machine_real.aa5+machine_real.a5-(1-5)*machine_real.da4;
 machine_real.c2= -machine_real.aa5+machine_real.a5-(2-5)*machine_real.da4;
 machine_real.c3= -machine_real.aa5+machine_real.a5-(3-5)*machine_real.da4;
 machine_real.c4= -machine_real.aa5+machine_real.a5-(4-5)*machine_real.da4;
 machine_real.c5= -machine_real.aa5+machine_real.a5-(5-5)*machine_real.da4;
 machine_real.c6= -machine_real.aa5+machine_real.a5-(6-5)*machine_real.da4;
 machine_real.c7= -machine_real.aa5+machine_real.a5-(7-5)*machine_real.da4;
 machine_real.c8= -machine_real.aa5+machine_real.a5-(8-5)*machine_real.da4;
 machine_real.c9= -machine_real.aa5+machine_real.a5-(9-5)*machine_real.da4;
 printf("Warning, recalculating C's to %g %g %g %g %g %g %g %g %g \n",machine_real.c1,machine_real.c2,machine_real.c3,machine_real.c4,machine_real.c5,machine_real.c6,machine_real.c7,machine_real.c8,machine_real.c9);
}


if (C1 && (machine_real.c1 != -C1)) {
   printf("Warning, resetting C1 from calculated %g to user provided %g\n",machine_real.c1,-C1);
   machine_real.c1 = -C1;
}
if (C2 && (machine_real.c2 != -C2)) {
   printf("Warning, resetting C2 from calculated %g to user provided %g\n",machine_real.c2,-C2);
   machine_real.c2 = -C2;
}
if (C3 && (machine_real.c3 != -C3)) {
   printf("Warning, resetting C3 from calculated %g to user provided %g\n",machine_real.c3,-C3);
   machine_real.c3 = -C3;
}
if (C4 && (machine_real.c4 != -C4)) {
   printf("Warning, resetting C4 from calculated %g to user provided %g\n",machine_real.c4,-C4);
   machine_real.c4 = -C4;
}
if (C5 && (machine_real.c5 != -C5)) {
   printf("Warning, resetting C5 from calculated %g to user provided %g\n",machine_real.c5,-C5);
   machine_real.c5 = -C5;
}
if (C6 && (machine_real.c6 != -C6)) {
   printf("Warning, resetting C6 from calculated %g to user provided %g\n",machine_real.c6,-C6);
   machine_real.c6 = -C6;
}
if (C7 && (machine_real.c7 != -C7)) {
   printf("Warning, resetting C7 from calculated %g to user provided %g\n",machine_real.c7,-C7);
   machine_real.c7 = -C7;
}
if (C8 && (machine_real.c8 != -C8)) {
   printf("Warning, resetting C8 from calculated %g to user provided %g\n",machine_real.c8,-C8);
   machine_real.c8 = -C8;
}
if (C9 && (machine_real.c9 != -C9)) {
   printf("Warning, resetting C9 from calculated %g to user provided %g\n",machine_real.c9,-C9);
   machine_real.c9 = -C9;
}

/* Forcing ana to set value */
 if (ANAFORCE) {/* Flat ana mode */
   if (AA5 && machine_real.a5 != AA5){
     machine_real.aa5=AA5;
     printf("Warning, resetting AA5 from calculated %g to user provided %g\n",machine_real.a5,AA5);
   } else{
     machine_real.aa5=machine_real.a5;
   }
   machine_real.c1=0;
   machine_real.c2=0;
   machine_real.c3=0;
   machine_real.c4=0;
   machine_real.c5=0;
   machine_real.c6=0;
   machine_real.c7=0;
   machine_real.c8=0;
   machine_real.c9=0;
   C1 = machine_real.c1;
   C2 = machine_real.c2;
   C3 = machine_real.c3;
   C4 = machine_real.c4;
   C5 = machine_real.c5;
   C6 = machine_real.c6;
   C7 = machine_real.c7;
   C8 = machine_real.c8;
   C9 = machine_real.c9;
   printf("* ANALYSER IN FLAT MODE \n");
 }
 else {/* else imaging mode ana */
   printf("* ANALYSER IN IMAGING MODE \n");
 }


if (verbose) {
  printf("* Transfered:     EN=%g [meV] QM=%g [Angs-1]\n", EN, machine_real.qm);
  printf("Angles: A1=%.4g A2=%.4g A3=%.4g A4=%.4g A5=%.4g A6=%.4g [deg]\n",
    machine_real.a1, machine_real.a2,
    machine_real.a3, machine_real.a4,
    machine_real.a5, machine_real.a6);
    printf("(RITA Analyzer Angles: AA5=%.4g C1=%.4g C2=%.4g C3=%.4g C4=%.4g C5=%.4g C6=%.4g C7=%.4g C8=%.4g C9=%.4g [deg])\n",
   machine_real.aa5,
   machine_real.c1,machine_real.c2,machine_real.c3,
   machine_real.c4,machine_real.c5,machine_real.c6,
   machine_real.c7,machine_real.c8,machine_real.c9);
}

/* END HKL calculator adapted from templateTAS */



/* Sample paramaters */
if (SAMPLE == 2){
  /* Powder sample, relevant WHEN at sample position */
  if (!strcmp(SAMPLEFILE,"default")) {
    PowderFile="Al2O3_sapphire.lau";
  } else {
    PowderFile=SAMPLEFILE;
  }
  SingleXFile="";
} else if (SAMPLE == 3) {
  /* Single crystal sample, relevant WHEN at sample position */
  if (!strcmp(SAMPLEFILE,"default")) {
    SingleXFile="Pb.laz";
  } else {
    SingleXFile=SAMPLEFILE;
  }
  PowderFile="";
} else if (SAMPLE==4){
  /* Phonon sample */
 printf("Phonon sample used - no elastic contribution. \n");
}else {
  /* Purely incoherent scatterer, relevant WHEN at sample position */
  SingleXFile="";
  PowderFile="";
}


/* Coarse collimator */
if (COARSE) {coarse = 1;}
else {coarse = 0;}

/* Opening slits of coarse collimator depends on analyzer settings: */
deltaL = 2 * WindowSize * cos(DEG2RAD*machine_real.a6 + fabs(DEG2RAD*machine_real.a5));
FirstWindowSizeL = WindowSize * (dad-BladeLength-deltaL+BladeLength*sin(DEG2RAD*machine_real.a6 + fabs(DEG2RAD*machine_real.a5)))/(dad-deltaL);
FirstWindowSizeR = WindowSize * (dad-BladeLength+deltaL+BladeLength*sin(DEG2RAD*machine_real.a6 + fabs(DEG2RAD*machine_real.a5)))/(dad+deltaL);

/* Window positions */
XwinMin[0] = 0;
XwinMin[1] = 12;
XwinMin[2] = 24;
XwinMin[3] = 36;
XwinMin[4] = 48;
XwinMin[5] = 60;
XwinMin[6] = 72;
XwinMin[7] = 84;
XwinMin[8] = 95;
XwinMin[9] = 106;

XwinMax[0] = 0;
XwinMax[1] = 21;
XwinMax[2] = 33;
XwinMax[3] = 45;
XwinMax[4] = 57;
XwinMax[5] = 69;
XwinMax[6] = 81;
XwinMax[7] = 93;
XwinMax[8] = 104;
XwinMax[9] = 115;

YwinMin[0] = 0;
YwinMin[1] = 39;
YwinMin[2] = 39;
YwinMin[3] = 39;
YwinMin[4] = 39;
YwinMin[5] = 39;
YwinMin[6] = 39;
YwinMin[7] = 39;
YwinMin[8] = 39;
YwinMin[9] = 39;

YwinMax[0] = 0;
YwinMax[1] = 91;
YwinMax[2] = 91;
YwinMax[3] = 91;
YwinMax[4] = 91;
YwinMax[5] = 91;
YwinMax[6] = 91;
YwinMax[7] = 91;
YwinMax[8] = 91;
YwinMax[9] = 91;


%}

/* end of INITIALIZE */

TRACE

/* Source description */
COMPONENT armSource = Progress_bar()
  AT (0,0,0) ABSOLUTE


COMPONENT source = Source_gen4 (
    h = 0.135, w = 0.08, xw = 0.03, yh = 0.12,
    dist = 1.465, Lmin=lmin, Lmax=lmax, /*Lmin=0.1 LMax=10*/
    T1=301.287, I1=ITAR*(1.27e13/4/PI),
    T2=105.655,I2=ITAR*(3.818e12/4/PI),
    T3=25.379,I3=ITAR*(2.331e12/4/PI),
    HEtailA=ITAR*8.306e11/4/PI, HEtailL0=-0.398)
  WHEN (!VIRTUALIN)  AT (0,0,0) RELATIVE armSource  ROTATED (0,0,0) RELATIVE armSource


COMPONENT slitGuideBegin = Slit(
  xmin = -0.015, xmax = 0.015,
  ymin = -0.06, ymax = 0.06)
  WHEN (!VIRTUALIN)  AT (0,0,1.464999) RELATIVE armSource


COMPONENT lmon_guide_start = L_monitor(
    nL = 100, filename = "lmon_guide_start.dat", xmin = -0.02, xmax = 0.02,
    ymin = -0.075, ymax = 0.075, Lmin = lmin, Lmax = lmax, restore_neutron = 1)
WHEN (!VIRTUALIN)  AT (0, 0, 1.4649992 ) RELATIVE armSource

COMPONENT guideStraight = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 4.628,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 1.465) RELATIVE armSource

 /* 0.035m gap*/

COMPONENT guideCurved1 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 4.663) RELATIVE guideStraight
  ROTATED (0,angleGuideCurved,0) RELATIVE guideStraight

COMPONENT guideCurved2 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved1
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved1

COMPONENT guideCurved3 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved2
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved2

COMPONENT guideCurved4 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved3
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved3

COMPONENT guideCurved5 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved4
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved4

COMPONENT guideCurved6 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved5
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved5

COMPONENT guideCurved7 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved6
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved6

COMPONENT guideCurved8 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved7
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved7

COMPONENT guideCurved9 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN) AT (0, 0, 0.5) RELATIVE guideCurved8
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved8

COMPONENT guideCurved10 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved9
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved9

COMPONENT guideCurved11 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved10
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved10

COMPONENT guideCurved12 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved11
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved11

COMPONENT guideCurved13 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved12
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved12

COMPONENT guideCurved14 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN) AT (0, 0, 0.5) RELATIVE guideCurved13
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved13

COMPONENT guideCurved15 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved14
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved14

COMPONENT guideCurved16 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved15
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved15

COMPONENT guideCurved17 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved16
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved16

COMPONENT guideCurved18 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved17
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved17

COMPONENT guideCurved19 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved18
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved18

COMPONENT guideCurved20 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved19
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved19

COMPONENT guideCurved21 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved20
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved20

COMPONENT guideCurved22 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved21
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved21

COMPONENT guideCurved23 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved22
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved22

COMPONENT guideCurved24 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved23
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved23

COMPONENT guideCurved25 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved24
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved24

COMPONENT guideCurved26 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved25
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved25

COMPONENT guideCurved27 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN) AT (0, 0, 0.5) RELATIVE guideCurved26
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved26

COMPONENT guideCurved28 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved27
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved27

COMPONENT guideCurved29 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved28
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved28

COMPONENT guideCurved30 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved29
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved29

COMPONENT guideCurved31 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved30
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved30

COMPONENT guideCurved32 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved31
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved31

COMPONENT guideCurved33 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved32
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved32

COMPONENT guideCurved34 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved33
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved33

COMPONENT guideCurved35 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved34
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved34

COMPONENT guideCurved36 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved35
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved35

COMPONENT guideCurved37 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved36
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved36

COMPONENT guideCurved38 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved37
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved37

COMPONENT guideCurved39 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved38
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved38

COMPONENT guideCurved40 = Guide(
        w1 = 0.03, h1 = 0.12, w2 = 0.03, h2 = 0.12, l = 0.499995,
         R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)  AT (0, 0, 0.5) RELATIVE guideCurved39
  ROTATED (0,angleGuideCurved,0) RELATIVE guideCurved39


/* bunker wall, m=2, 3.0 m */
 COMPONENT bunker = Guide(w1= 0.03, h1=0.12, w2=0.03, h2=0.12,
        l=3.45, R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)    AT (0,0,0.5) RELATIVE guideCurved40

/*15cm space for main shutter*/

/* guide segment 3, m=2, 1.3 m */
 COMPONENT guide3 = Guide(w1=0.03, h1=0.12, w2=0.03, h2=0.12,
        l=5.2, R0 = R0, Qc = Qc, alpha = ALPHA, m = M, W = W)
WHEN (!VIRTUALIN)    AT (0,0,3.6) RELATIVE bunker


COMPONENT slitGuideEnd = Slit(xmin=-0.016,xmax=0.016, ymin=-0.061,ymax=0.061)
WHEN (!VIRTUALIN)  AT (0,0,5.2001) RELATIVE guide3

COMPONENT psd_guide_end = PSD_monitor(
  xwidth=0.04, yheight=0.15,
  nx=64, ny=64, filename="psd_guide_end.dat", restore_neutron = 1)
WHEN (!VIRTUALIN)  AT (0, 0, 0.04) RELATIVE slitGuideEnd

COMPONENT emon_guide_end = E_monitor(
 nE = 100,filename = "emon_guide_end.dat",xwidth=0.04, yheight=0.15,  Emin = emini, Emax = emaxi, restore_neutron = 1)
  AT (0, 0, 0.05) RELATIVE slitGuideEnd

COMPONENT lmon_guide_end = L_monitor(
  nL = 100, filename = "lmon_guide_end.dat", xwidth=0.04, yheight=0.15,Lmin = lmin, Lmax = lmax, restore_neutron = 1)
WHEN (!VIRTUALIN)  AT (0, 0, 0.06) RELATIVE slitGuideEnd

COMPONENT divmon_guide_end = Divergence_monitor(
    nh = 128, nv = 128, filename = "divmon_guide_end.dat",
    restore_neutron = 1, xwidth = 0.04, yheight = 0.15,
    maxdiv_h = 2, maxdiv_v = 2, restore_neutron = 1)
WHEN (!VIRTUALIN)  AT (0, 0, 0.07) RELATIVE slitGuideEnd


COMPONENT focus_mono = Arm()
WHEN (!VIRTUALIN)  AT (0, 0, 0.15) RELATIVE slitGuideEnd
  ROTATED (0, machine_real.a1, 0) RELATIVE slitGuideEnd
EXTEND %{
  Mono_order=-1;
%}

SPLIT SPLITMREP COMPONENT monochromator_curved = Monochromator_curved(
    reflect = "HOPG.rfl", zwidth = 0.15, yheight = 0.025,
    gap = 0.001, NH = 1, NV = 5, mosaich = mono_mosaic_h,
    mosaicv = mono_mosaic_v, r0 = 1, Q = 1.8734, RV = rv,
    RH = 0)
WHEN (!VIRTUALIN)  AT (0, 0, 0) RELATIVE focus_mono
EXTEND %{
  if(SCATTERED) Mono_order=order;
%}

COMPONENT a2 = Arm()
WHEN (!VIRTUALIN)  AT (0,0,0) RELATIVE focus_mono
  ROTATED (0, machine_real.a2, 0) RELATIVE slitGuideEnd


COMPONENT slitShutter = Slit(
  xmin = -0.02, xmax = 0.02,
  ymin = -0.075, ymax = 0.075)
WHEN (!VIRTUALIN)  AT (0, 0, 0.2) RELATIVE a2
  ROTATED (0,0,0) RELATIVE a2

COMPONENT MSCollimator = Collimator_linear(
     xmin = -0.02, xmax = 0.02, ymin = -0.075, ymax = 0.075, length = 0.20 ,
     divergence = COLL_MS )
WHEN (!VIRTUALIN) AT (0, 0, dmc) RELATIVE a2


COMPONENT infilter = Filter_gen(
xmin = -0.02, xmax = 0.02, ymin = -0.075, ymax = 0.075, options="wavevector multiply", filename=INFILTERFILE)
WHEN (INFILTER>0 && !VIRTUALIN) AT (0, 0,0.201) RELATIVE MSCollimator


COMPONENT psd_virt = PSD_monitor(
    nx=128, ny=128, filename="psd_virt.dat", xwidth = 0.10, yheight = 0.10 )
WHEN (!VIRTUALIN)  AT (0, 0, dmv-0.01) RELATIVE a2


COMPONENT lmon_virt = L_monitor(
    nL = 100, filename = "lmon_virt.dat", xwidth=0.06, yheight=0.10,//the size of the beam
    Lmin = lmin, Lmax = lmax, restore_neutron=1)
WHEN (!VIRTUALIN)  AT (0, 0, dmv) RELATIVE a2 // Needs to be here in order to propagate virtual_out to dmv

COMPONENT aa2 = Arm()
WHEN (!VIRTUALIN)  AT (0,0,dmv) RELATIVE a2


/* If the virtual output is generated the secondary spectrometer IS NOT simulated */
/*  COMPONENT virtualout = Virtual_output(filename=SOURCEFILE, bufsize=0)*/
/*WHEN (!VIRTUALIN && VIRTUALOUT)  AT (0, 0, 0) RELATIVE aa2*/
/**/
/**/
/*COMPONENT virtualsource = Virtual_input(filename=SOURCEFILE, verbose=1, repeat_count=REP)*/
/*WHEN (VIRTUALIN && !VIRTUALOUT) AT (0,0,0) RELATIVE aa2*/


COMPONENT OrderMon = Monitor_nD(
      xwidth = 0.04, yheight = 0.08,
      user1="Mono_order", username1="Intensity of multiples",
      options="user1 bins=5 limits=[0.5 5.5], user1 bins=5 limits=[0.5 5.5]", filename="OrderMonNtest.dat", restore_neutron=1)
 WHEN (Mono_order && !VIRTUALOUT)    AT (0, 0, 0.170-4e-3) RELATIVE aa2

COMPONENT kMoni = PSD_monitor_psf_eff(eff=EFF, k0=1.553, xwidth = 0.04, yheight = 0.08, restore_neutron=1,filename="kMoni.dat")
  WHEN(!VIRTUALOUT)     AT (0, 0, 0.170-3e-3) RELATIVE aa2

COMPONENT kMoni1st = PSD_monitor_psf_eff(eff=EFF, k0=1.553, xwidth = 0.04, yheight = 0.08, restore_neutron=1,filename="kMoni1st.dat")
WHEN (Mono_order==1 && !VIRTUALOUT) AT (0, 0, 0.170-2e-3) RELATIVE aa2

COMPONENT kMoni2nd = PSD_monitor_psf_eff(eff=EFF, k0=1.553, xwidth = 0.04, yheight = 0.08, restore_neutron=1,filename="kMoni2nd.dat")
WHEN (Mono_order==2 && !VIRTUALOUT) AT (0, 0, 0.170-1e-3) RELATIVE aa2


COMPONENT kMoni3rd = PSD_monitor_psf_eff(eff=EFF,  k0=1.553, xwidth = 0.04, yheight = 0.08, restore_neutron=1,filename="kMoni3rd.dat")
WHEN (Mono_order==3 && !VIRTUALOUT) AT (0, 0, 0.170) RELATIVE aa2


COMPONENT slitMonochromator = Slit(
  xmin = -MSL/1000.0, xmax = MSR/1000.0,
  ymin = -MSB/1000.0, ymax = MST/1000.0)
WHEN(!VIRTUALOUT) AT (0, 0, 0.285) RELATIVE aa2

COMPONENT Perspex = Incoherent(Vc=1,sigma_abs=0.019,sigma_inc=4.7, xwidth = 0.1, yheight = 0.1, zdepth = PTHICK, p_interact=1e-2)
 WHEN (PERSPEX>0 && !VIRTUALOUT) AT (0, 0, 0.3) RELATIVE aa2

COMPONENT psd_samplepos_1cm2 = PSD_monitor( xwidth = 0.01, yheight = 0.01, restore_neutron=1,filename="psd_samplepos_1cm2.dat")
WHEN(!VIRTUALOUT)    AT (0, 0, dvs) RELATIVE aa2

COMPONENT emon_samplepos_1cm2 = E_monitor(
  nE=100, filename="emon_samplepos_1cm2.dat",xwidth = 0.01, yheight = 0.01, Emin=emini, Emax=emaxi, restore_neutron=1)
WHEN(!VIRTUALOUT)   AT (0, 0, dvs) RELATIVE aa2

COMPONENT divmon_samplepos_1cm2 = Divergence_monitor(
    nh = 128, nv = 128, filename = "divmon_samplepos_1cm2.dat",
    xwidth = 0.01, yheight = 0.01, maxdiv_h = 3, maxdiv_v = 3, restore_neutron=1)
WHEN(!VIRTUALOUT)  AT (0, 0, dvs) RELATIVE aa2

COMPONENT psd_samplepos_large = PSD_monitor( xwidth = 0.06, yheight = 0.06, restore_neutron=1,filename="psd_samplepos_large.dat")
WHEN(!VIRTUALOUT)    AT (0, 0, dvs) RELATIVE aa2

SPLIT SPLITREP COMPONENT a3 = Arm()
WHEN(!VIRTUALOUT) AT (0,0,dvs) RELATIVE aa2
 ROTATED (0, machine_real.a3, 0) RELATIVE aa2

COMPONENT aa3 = Arm()
WHEN(!VIRTUALOUT) AT (0,0,0) RELATIVE a3
 ROTATED (0, 0, TILT) RELATIVE a3


COMPONENT incohSample = Incoherent( // The size is the standard  Vanadium sample at RITA
/*      V0=1,sig_a=0,sig_i=4.70,radius_i=0, radius_o=0.0025, h=0.068,target_index=10, // perspex sample  */
    Vc=13.827,sigma_abs=5.08,sigma_inc=5.08,thickness=0.0201/2-0.0149/2, radius=0.0201/2, yheight=0.0187,target_index=10, // vanadium sample
    focus_xw=0.16,focus_yh=0.16) // focus on ana_slit1.
  WHEN (SAMPLE==1 && !VIRTUALOUT)
  AT (0, 0, 0) RELATIVE aa3

COMPONENT powderSample = PowderN( // The size is the standard sapphire sample at RITA
    reflections=PowderFile, d_phi = 12 , radius = 0.0068 , yheight = 0.015, pack = 1 , Vc = 254.52, sigma_abs = 0.4625 ,
    sigma_inc = 0.0188, p_inc = 0, barns=BARNS)
  WHEN (SAMPLE==2 && !VIRTUALOUT)
  AT (0, 0, 0) RELATIVE aa3

COMPONENT crystalSample = Single_crystal(//order=1, p_transmit=0,
     reflections = SingleXFile, mosaic=MOS, xwidth = SAMPLESIZE, yheight = SAMPLESIZE,zdepth = SAMPLESIZE, delta_d_d=DD_D,
     ax =AAX , ay = AAY, az =AAZ , bx =BBX , by =BBY, bz =BBZ , cx =CCX , cy =CCY , cz =CCZ, barns=BARNS)
   WHEN (SAMPLE==3 && !VIRTUALOUT)
  AT (0, 0, 0) RELATIVE aa3
EXTEND %{
  if(!SCATTERED) ABSORB;
  %}


COMPONENT phononSample = Phonon_simple(
    radius = SAMPLESIZE/2, yheight = SAMPLESIZE, focus_xw =0.16 ,
    focus_yh = 0.16, sigma_abs = 0.171, sigma_inc = 0.003,
    a = 4.95, b = 4.95, c = 10, M=207.2, DW = 1, T = 300,
    target_index = 7)// Focus on the ana_slit1
WHEN (SAMPLE==4 && !VIRTUALOUT)  AT (0, 0, 0) RELATIVE aa3

COMPONENT psd_4pi = PSD_monitor_4PI(
    nx = 1000, ny = 1000, filename = "psd_4pi",
    restore_neutron = 1, radius = 0.1)
 WHEN (!VIRTUALOUT) AT (0, 0, dvs) RELATIVE aa2

COMPONENT a4 = Arm()
 WHEN (!VIRTUALOUT) AT (0, 0, 0) RELATIVE a3
  ROTATED (0, machine_real.a4, 0) RELATIVE aa2


COMPONENT slitSample = Slit(
  xmin = -SSL/1000.0, xmax = SSR/1000.0,
  ymin = -SSB/1000.0, ymax = SST/1000.0)
  WHEN (!VIRTUALOUT) AT (0, 0, d_sample_slit) RELATIVE a4


COMPONENT filter_coll=Exact_radial_coll(
  radius=0.4525, nslit=9, d=0.000125,verbose=1,h_in=0.2,h_out=0.2,length=0.0988,theta_min=-10.26/2,theta_max=10.26/2
)
WHEN (OUTFILTER>0 && !VIRTUALOUT)
AT (0, 0, d_sample_filter-0.4525-0.0988/2) RELATIVE a4

COMPONENT filter = Filter_gen(
    xmin = -0.1, xmax = 0.1, ymin = -0.1, ymax = 0.1, options="wavevector multiply", filename=OUTFILTERFILE)
  WHEN (OUTFILTER>0 && !VIRTUALOUT)
  AT (0, 0,0.4525+0.0988+0.0001) RELATIVE filter_coll



COMPONENT ana_slit1 = Slit(
  xmin = -0.158/2, xmax = 0.158/2,
  ymin = -0.08/2, ymax = 0.08/2)
WHEN (!VIRTUALOUT)  AT (0, 0, d_sample_filter+0.13) RELATIVE a4
  ROTATED (0,0,0) RELATIVE a4

COMPONENT ana_slit2 = Slit(
  xmin = -0.158/2, xmax = 0.158/2,
  ymin = -0.103/2, ymax = 0.103/2)
WHEN (!VIRTUALOUT)  AT (0, 0, d_sample_filter+0.33) RELATIVE a4
  ROTATED (0,0,0) RELATIVE a4


COMPONENT emon_before_ana = E_monitor(
  xmin = -0.060, xmax = 0.060, ymin = -0.085, ymax = 0.085,
  Emin=eminf, Emax=emaxi, nE=100, filename="emon_before_ana.dat", restore_neutron=1)
WHEN (!VIRTUALOUT)  AT (0, 0, dsa-5*ana_d) RELATIVE a4

COMPONENT psd_before_ana = PSD_monitor(
    nx = 128, ny = 128, filename = "psd_before_ana.dat",
    //xmin = -0.1, xmax = 0.1, ymin = -0.1, ymax = 0.1)
    xmin = -0.060, xmax = 0.060, ymin = -0.085, ymax = 0.085)
WHEN (!VIRTUALOUT)  AT (0, 0, dsa-5*ana_d+0.001) RELATIVE a4

COMPONENT divmon_before_ana = Divergence_monitor(
    nh = 128, nv = 128, filename = "divmon_before_ana",
    xwidth = 0.02, yheight = 0.17, maxdiv_h = 1, maxdiv_v = 1, restore_neutron = 1)
WHEN (!VIRTUALOUT)  AT (0, 0, dsa-5*ana_d+0.002) RELATIVE a4

SPLIT SPLITAREP COMPONENT focus_ana = Arm()
WHEN (!VIRTUALOUT)  AT (0, 0, dsa) RELATIVE a4
ROTATED (0, machine_real.aa5, 0) RELATIVE a4
EXTEND %{
  AnaBlade=0;
%}

COMPONENT an1l= Monochromator_flat(// The mosaic values for this blade have not been checked!
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=40, mosaicv=40,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -4*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c1, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 1;
%}

COMPONENT an1u= Monochromator_flat( // The mosaic values for this blade have not been checked!
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=40, mosaicv=40,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -4*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c1, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 1;
%}


COMPONENT an2l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=38.7, mosaicv=38.7,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -3*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c2, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 2;
%}

COMPONENT an2u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=43.0, mosaicv=43.0,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -3*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c2, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 2;
%}

COMPONENT an3l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=31.1, mosaicv=31.1,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -2*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c3, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 3;
%}

COMPONENT an3u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=35.5, mosaicv=35.5,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -2*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c3, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 3;
%}


COMPONENT an4l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=27.2, mosaicv=27.2,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, -1*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c4, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 4;
%}

COMPONENT an4u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=30.4, mosaicv=30.4,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT) AT (0, 0, -1*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c4, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 4;
%}

COMPONENT an5l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=36.6, mosaicv=36.6,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 0) RELATIVE focus_ana
  ROTATED (0, machine_real.c5, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 5;
%}

COMPONENT an5u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=35.9, mosaicv=35.9,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 0) RELATIVE focus_ana
  ROTATED (0, machine_real.c5, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 5;
%}

COMPONENT an6l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=31.5, mosaicv=31.5,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c6, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 6;
%}

COMPONENT an6u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=36.1, mosaicv=36.1,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c6, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 6;
%}


COMPONENT an7l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=33.1, mosaicv=33.1,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 2*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c7, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 7;
%}

COMPONENT an7u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=37.2, mosaicv=37.2,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 2*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c7, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 7;
%}

COMPONENT an8l= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=46.8, mosaicv=46.8,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 3*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c8, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 8;
%}

COMPONENT an8u= Monochromator_flat(
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=51.3, mosaicv=51.3,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 3*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c8, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 8;
%}

COMPONENT an9l= Monochromator_flat(// The mosaic values for this blade have not been checked!
  zmin=-wan/2.0, zmax=wan/2.0, ymin=-ana_h/2.0, ymax=0,
  mosaich=40, mosaicv=40,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 4*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c9, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 9;
%}

COMPONENT an9u= Monochromator_flat( // The mosaic values for this blade have not been checked!
  zmin=-wan/2.0, zmax=wan/2.0, ymin=0, ymax=ana_h/2.0,
  mosaich=40, mosaicv=40,
  r0=ana_r0, Q=ana_q)
WHEN (!VIRTUALOUT)  AT (0, 0, 4*ana_d) RELATIVE focus_ana
  ROTATED (0, machine_real.c9, 0) RELATIVE focus_ana
//GROUP ANA
EXTEND %{
  if(SCATTERED) AnaBlade = 9;
%}


COMPONENT a6 = Arm()
WHEN (!VIRTUALOUT)  AT (0,0,0) RELATIVE focus_ana
  ROTATED (0, machine_real.a6, 0) RELATIVE a4

COMPONENT emon_before_coarse = E_monitor(
    nE=100, filename = "emon_before_coarse.dat", Emin=eminf, Emax=emaxf,
    xmin = -0.01, xmax = 0.01, ymin = -0.15, ymax = 0.15, restore_neutron=1)
    //xmin = -0.060, xmax = 0.060, ymin = -0.085, ymax = 0.085, restore_neutron=1)
WHEN (!VIRTUALOUT)  AT (0, 0, dad-BladeLength-0.02) RELATIVE a6

COMPONENT psd_before_coarse = PSD_monitor(
    nx = 128, ny = 128, filename = "psd_before_coarse.dat",
    xmin = -0.15, xmax = 0.15, ymin = -0.15, ymax = 0.15, restore_neutron=1)
    //xmin = -0.060, xmax = 0.060, ymin = -0.085, ymax = 0.085, restore_neutron=1)
WHEN (!VIRTUALOUT)  AT (0, 0, dad-BladeLength-0.01) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6


COMPONENT ArmR1 = Arm()
WHEN (!VIRTUALOUT) AT (-WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,-RC/9,0) RELATIVE a6

COMPONENT BladeR1 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0 && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmR1
EXTEND %{
  if (SCATTERED) printf("Absorption in R1\n");
%}

COMPONENT ArmR2 = Arm()
 AT (-3*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,-3*RC/9,0) RELATIVE a6

COMPONENT BladeR2 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0 && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmR2

COMPONENT ArmR3 = Arm()
WHEN (!VIRTUALOUT) AT (-5*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,-5*RC/9,0) RELATIVE a6

COMPONENT BladeR3 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
     WHEN (COARSE>0) AT (0,0,0) RELATIVE ArmR3

COMPONENT ArmR4 = Arm()
WHEN (!VIRTUALOUT) AT (-7*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,-7*RC/9,0) RELATIVE a6

COMPONENT BladeR4 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
     WHEN (COARSE>0) AT (0,0,0) RELATIVE ArmR4

COMPONENT ArmR5 = Arm()
WHEN (!VIRTUALOUT) AT (-9*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,-9*RC/9,0) RELATIVE a6

COMPONENT BladeR5 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
     WHEN (COARSE>0) AT (0,0,0) RELATIVE ArmR5



COMPONENT ArmL1 = Arm()
WHEN (!VIRTUALOUT) AT (WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,LC/9,0) RELATIVE a6

COMPONENT BladeL1 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0 && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmL1


COMPONENT ArmL2 = Arm()
 AT (3*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,3*LC/9,0) RELATIVE a6

COMPONENT BladeL2 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0  && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmL2

COMPONENT ArmL3 = Arm()
 AT (5*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,5*LC/9,0) RELATIVE a6

COMPONENT BladeL3 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0  && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmL3

COMPONENT ArmL4 = Arm()
WHEN (!VIRTUALOUT) AT (7*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,7*LC/9,0) RELATIVE a6

COMPONENT BladeL4 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0 && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmL4

COMPONENT ArmL5 = Arm()
WHEN (!VIRTUALOUT) AT (9*WindowSize/2,0,dad-0.005) RELATIVE a6
ROTATED (0,9*LC/9,0) RELATIVE a6
EXTEND %{
  BinX = 0; BinY=0;
%}

COMPONENT BladeL5 = Absorber(xmin=-BladeThickness/2, xmax=BladeThickness/2,
           ymin=-BladeHeight/2, ymax=BladeHeight/2,
           zmin=-BladeLength,zmax=0)
WHEN (COARSE>0 && !VIRTUALOUT) AT (0,0,0) RELATIVE ArmL5

  COMPONENT psd_detector = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
   ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_detector.dat",restore_neutron=0) // restore_neutron needs to be 0 to propagate the netrons in use for EXTEND section
 WHEN (!VIRTUALOUT) AT (0, 0, dad+0.0215) RELATIVE a6
 ROTATED (0,180,0) RELATIVE a6
EXTEND %{
  BinX = floor((x - xmin)*nx/(xmax - xmin)); BinY = floor((y - ymin)*ny/(ymax - ymin));
%}


 COMPONENT emon_detector = E_monitor(
     nE=100, filename="detector.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (!VIRTUALOUT)    AT (0, 0, dad+0.0215) RELATIVE a6
ROTATED (0,180,0) RELATIVE a6

  COMPONENT psd_window1 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
    xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
    ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window1.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[1] && BinX <= XwinMax[1] && BinY >= YwinMin[1] && BinY <= YwinMax[1] && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6

  COMPONENT emon_window1 = E_monitor(
       nE = 100, filename="window1.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[1] && BinX <= XwinMax[1] && BinY >= YwinMin[1] && BinY <= YwinMax[1]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

  COMPONENT psd_window2 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
    xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
    ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window2.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[2] && BinX <= XwinMax[2] && BinY >= YwinMin[2] && BinY <= YwinMax[2] && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


  COMPONENT emon_window2 = E_monitor(
       nE = 100, filename="window2.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[2] && BinX <= XwinMax[2] && BinY >= YwinMin[2] && BinY <= YwinMax[2]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

  COMPONENT psd_window3 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
    xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
    ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window3.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[3] && BinX <= XwinMax[3] && BinY >= YwinMin[3] && BinY <= YwinMax[3]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


  COMPONENT emon_window3 = E_monitor(
       nE = 100, filename="window3.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[3] && BinX <= XwinMax[3] && BinY >= YwinMin[3] && BinY <= YwinMax[3]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215+0.0008) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

 COMPONENT psd_window4 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
    ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window4.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[4] && BinX <= XwinMax[4] && BinY >= YwinMin[4] && BinY <= YwinMax[4] &&  !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


  COMPONENT emon_window4 = E_monitor(
     nE = 100, filename="window4.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[4] && BinX <= XwinMax[4] && BinY >= YwinMin[4] && BinY <= YwinMax[4]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

  COMPONENT psd_window5 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
    ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window5.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[5] && BinX <= XwinMax[5] && BinY >= YwinMin[5] && BinY <= YwinMax[5] &&  !VIRTUALOUT)    AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


  COMPONENT emon_window5 = E_monitor(
       nE = 100, filename="window5.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[5] && BinX <= XwinMax[5] && BinY >= YwinMin[5] && BinY <= YwinMax[5]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6


   COMPONENT psd_window6 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
     xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
     ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window6.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[6] && BinX <= XwinMax[6] && BinY >= YwinMin[6] && BinY <= YwinMax[6]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


  COMPONENT emon_window6 = E_monitor(
      nE = 100, filename="window6.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[6] && BinX <= XwinMax[6] && BinY >= YwinMin[6] && BinY <= YwinMax[6]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

 COMPONENT psd_window7 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
   ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window7.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[7] && BinX <= XwinMax[7] && BinY >= YwinMin[7] && BinY <= YwinMax[7]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


 COMPONENT emon_window7 = E_monitor(
       nE = 100, filename="window7.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[7] && BinX <= XwinMax[7] && BinY >= YwinMin[7] && BinY <= YwinMax[7]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

 COMPONENT psd_window8 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
   ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window8.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[8] && BinX <= XwinMax[8] && BinY >= YwinMin[8] && BinY <= YwinMax[8]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


 COMPONENT emon_window8 = E_monitor(
       nE = 100, filename="window8.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[8] && BinX <= XwinMax[8] && BinY >= YwinMin[8] && BinY <= YwinMax[8]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6

 COMPONENT psd_window9 = PSD_monitor_psf_eff(eff=0.8, k0=1.553,
   xmin = -det_width/2.0, xmax = det_width/2.0, ymin = -det_height/2.0,
   ymax = det_height/2.0, nx = 128, ny = 128, psf = PSF, filename = "psd_window9.dat",restore_neutron=1)
 WHEN (BinX >= XwinMin[9] && BinX <= XwinMax[9] && BinY >= YwinMin[9] && BinY <= YwinMax[9]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
   ROTATED (0,180,0) RELATIVE a6


 COMPONENT emon_window9 = E_monitor(
       nE = 128, filename="window9.dat", xwidth =det_width , yheight=det_height, Emin=eminf, Emax=emaxf,restore_neutron=1)
 WHEN (BinX >= XwinMin[9] && BinX <= XwinMax[9] && BinY >= YwinMin[9] && BinY <= YwinMax[9]  && !VIRTUALOUT)  AT (0, 0, dad+0.0215) RELATIVE a6
  ROTATED (0,180,0) RELATIVE a6



END