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
|
# -*- coding: utf-8 -*-
"""
Apertures
---------
Module :mod:`apertures` defines rectangular and round apertures and a set of
coplanar rectangular apertures. Rectangular apertures may have one or more
defining edges. For example, a simple obstacle, like a beam stop block would
have one edge, a block of front-end slits would have two edges at 90 degrees to
each other, and a collimator would have all four edges.
The classes have useful methods for getting divergence from the aperture size,
for setting divergence (calculating the aperture size given the divergence) and
for touching the beam with the aperture, i.e. calculating the minimum aperture
size that lets the whole beam through.
.. autoclass:: xrt.backends.raycing.apertures.RectangularAperture()
:members: __init__, get_divergence, set_divergence, propagate, touch_beam,
prepare_wave
.. autoclass:: xrt.backends.raycing.apertures.RoundAperture()
:members: __init__, get_divergence, propagate
.. autoclass:: xrt.backends.raycing.apertures.RoundBeamStop()
.. autoclass:: xrt.backends.raycing.apertures.DoubleSlit()
:members: __init__
.. autoclass:: xrt.backends.raycing.apertures.PolygonalAperture()
:members: __init__
.. autoclass:: xrt.backends.raycing.apertures.GridAperture()
:members: __init__
.. autoclass:: xrt.backends.raycing.apertures.SiemensStar()
:members: __init__
"""
import numpy as np
import inspect
from matplotlib.path import Path as mplPath
import copy
from .. import raycing
from . import sources as rs
from .physconsts import CHBAR
__author__ = "Konstantin Klementiev, Roman Chernikov"
__date__ = "1 Nov 2019"
__all__ = ('RectangularAperture', 'RoundAperture', 'RoundBeamStop',
'DoubleSlit', 'PolygonalAperture', 'GridAperture', 'SiemensStar')
allArguments = ('bl', 'name', 'center', 'kind', 'opening', 'x', 'z',
'alarmLevel', 'r', 'shadeFraction',
'dx', 'dz', 'px', 'pz', 'nx', 'nz',
'nSpokes' 'rx', 'rz', 'phi0', 'vortex', 'vortexNradial')
class RectangularAperture(object):
"""Implements an aperture or an obstacle as a combination of straight
edges."""
def __init__(self, bl=None, name='', center=[0, 0, 0],
kind=['left', 'right', 'bottom', 'top'],
opening=[-10, 10, -10, 10], x='auto', z='auto',
alarmLevel=None):
"""
*bl*: instance of :class:`~xrt.backends.raycing.BeamLine`
Container for beamline elements. Optical elements are added to its
`slits` list.
*name*: str
User-specified name, can be used for diagnostics output.
*center*: 3-sequence of floats
3D point in global system.
*kind*: sequence
Any combination of 'top', 'bottom', 'left', 'right'.
*opening*: sequence
Distances (with sign according to the local coordinate system) from
the blade edges to the aperture center with the length
corresponding to *kind*.
*x, z*: 3-tuples or 'auto'.
Normalized 3D vectors in the global system which determine the
local x and z axes lying in the aperture plane. If *x* is 'auto',
it is horizontal and normal to the beam line. If *z* is 'auto', it
is vertical. Both *x* and *z* can also be set as instance
attributes.
*alarmLevel*: float or None.
Allowed fraction of number of rays absorbed at the aperture
relative to the number of incident rays. If exceeded, an alarm
output is printed in the console.
"""
self.bl = bl
if bl is not None:
if self not in bl.slits:
bl.slits.append(self)
self.ordinalNum = len(bl.slits)
self.lostNum = -self.ordinalNum - 1000
raycing.set_name(self, name)
# if name not in [None, 'None', '']:
# self.name = name
# elif not hasattr(self, 'name'):
# self.name = '{0}{1}'.format(self.__class__.__name__,
# self.ordinalNum)
if bl is not None:
if self.bl.flowSource != 'Qook':
bl.oesDict[self.name] = [self, 1]
self.center = center
# if any([xc == 'auto' for xc in self.center]):
# self._center = copy.copy(self.center)
self._x = x
self._z = z
self._set_orientation()
if isinstance(kind, str):
self.kind = (kind,)
self.opening = [opening, ]
else:
self.kind = kind
self.opening = opening
self.alarmLevel = alarmLevel
# For plotting footprint images with the envelope aperture:
self.surface = name,
self.limOptX = [-500, 500]
self.limOptY = [-500, 500]
self.limPhysX = self.limOptX
self.limPhysY = self.limOptY
if opening is not None:
self.set_optical_limits()
self.shape = 'rect'
self.spotLimits = []
@property
def x(self):
return self._x
@x.setter
def x(self, x):
self._x = copy.copy(x)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def z(self):
return self._z
@z.setter
def z(self, z):
self._z = copy.copy(z)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def center(self):
return self._center if self._centerVal is None else self._centerVal
@center.setter
def center(self, center):
if any([x == 'auto' for x in center]):
self._center = copy.copy(center)
self._centerVal = None
self._centerInit = copy.copy(center)
else:
self._centerVal = center
def _set_orientation(self):
"""Determines the local x, y and z as vectors in the global system."""
if isinstance(self._x, raycing.basestring):
self._x = None
if isinstance(self._z, raycing.basestring):
self._z = None
self.xyz = raycing.xyz_from_xz(self, self._x, self._z)
def set_orientation(self, x=None, z=None):
"""Compatibility method. All calculations moved to setters."""
self._x = x
self._z = z
self._set_orientation()
def set_optical_limits(self):
"""For plotting footprint images with the envelope aperture."""
for akind, d in zip(self.kind, self.opening):
td = float(d) # otherwise is of type 'numpy.float64' and is
# raycing.is_sequence(d) returns True which is not expected.
if akind.startswith('l'):
self.limOptX[0] = td
elif akind.startswith('r'):
self.limOptX[1] = td
elif akind.startswith('b'):
self.limOptY[0] = td
elif akind.startswith('t'):
self.limOptY[1] = td
def get_divergence(self, source):
"""Gets divergences given the blade openings."""
sourceToAperture = ((self.center[0]-source.center[0])**2 +
(self.center[1]-source.center[1])**2 +
(self.center[2]-source.center[2])**2)**0.5
divergence = []
for d in self.opening:
divergence.append(d / sourceToAperture)
return divergence
def set_divergence(self, source, divergence):
"""Gets the blade openings given divergences.
*divergence* is a sequence corresponding to *kind*"""
sourceToAperture = ((self.center[0]-source.center[0])**2 +
(self.center[1]-source.center[1])**2 +
(self.center[2]-source.center[2])**2)**0.5
d = []
for div in divergence:
if div > 0:
sgn = 1
else:
sgn = -1
d.append(div*sourceToAperture + sgn*raycing.accuracyInPosition)
self.opening = d
def propagate(self, beam=None, needNewGlobal=False):
"""Assigns the "lost" value to *beam.state* array for the rays
intercepted by the aperture. The "lost" value is
``-self.ordinalNum - 1000.``
.. Returned values: beamLocal
"""
if self.bl is not None:
self.bl.auto_align(self, beam)
good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
path = -lo.y[good] / lo.b[good]
lo.x[good] += lo.a[good] * path
lo.z[good] += lo.c[good] * path
lo.path[good] += path
badIndices = np.zeros(len(beam.x), dtype=bool)
for akind, d in zip(self.kind, self.opening):
if akind.startswith('l'):
badIndices[good] = badIndices[good] | (lo.x[good] < d)
elif akind.startswith('r'):
badIndices[good] = badIndices[good] | (lo.x[good] > d)
elif akind.startswith('b'):
badIndices[good] = badIndices[good] | (lo.z[good] < d)
elif akind.startswith('t'):
badIndices[good] = badIndices[good] | (lo.z[good] > d)
beam.state[badIndices] = self.lostNum
lo.state[:] = beam.state
lo.y[good] = 0.
if hasattr(lo, 'Es'):
propPhase = np.exp(1e7j * (lo.E[good]/CHBAR) * path)
lo.Es[good] *= propPhase
lo.Ep[good] *= propPhase
goodN = lo.state > 0
try:
if self.spotLimits:
self.spotLimits[0] = min(self.spotLimits[0], lo.x[goodN].min())
self.spotLimits[1] = max(self.spotLimits[1], lo.x[goodN].max())
self.spotLimits[2] = min(self.spotLimits[2], lo.z[goodN].min())
self.spotLimits[3] = max(self.spotLimits[3], lo.z[goodN].max())
else:
self.spotLimits = [lo.x[goodN].min(), lo.x[goodN].max(),
lo.z[goodN].min(), lo.z[goodN].max()]
except ValueError:
self.spotLimits = []
if self.alarmLevel is not None:
raycing.check_alarm(self, good, beam)
if needNewGlobal:
glo = rs.Beam(copyFrom=lo)
raycing.virgin_local_to_global(self.bl, glo, self.center, good)
raycing.append_to_flow(self.propagate, [glo, lo],
inspect.currentframe())
return glo, lo
else:
raycing.append_to_flow(self.propagate, [lo],
inspect.currentframe())
return lo
def touch_beam(self, beam):
"""Adjusts the aperture (i.e. sets self.opening) so that it touches the
*beam*."""
good = (beam.state == 1) | (beam.state == 2)
# good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
lo.y[good] /= lo.b[good]
if ('left' in self.kind) or ('right' in self.kind):
lo.x[good] -= lo.a[good] * lo.y[good]
if ('top' in self.kind) or ('bottom' in self.kind):
lo.z[good] -= lo.c[good] * lo.y[good]
locOpening = []
if good.sum() > 0:
for akind, d in zip(self.kind, self.opening):
if akind.startswith('l'):
locOpening.append(lo.x[good].min())
elif akind.startswith('r'):
locOpening.append(lo.x[good].max())
elif akind.startswith('t'):
locOpening.append(lo.z[good].max())
elif akind.startswith('b'):
locOpening.append(lo.z[good].min())
else:
continue
self.opening = locOpening
self.set_optical_limits()
def local_to_global(self, glo, returnBeam=False, **kwargs):
if returnBeam:
retGlo = rs.Beam(copyFrom=glo)
raycing.virgin_local_to_global(self.bl, retGlo,
self.center, **kwargs)
return retGlo
else:
raycing.virgin_local_to_global(self.bl, glo, self.center, **kwargs)
def prepare_wave(self, prevOE, nrays, rw=None):
"""Creates the beam arrays used in wave diffraction calculations.
*prevOE* is the diffracting element: a descendant from
:class:`~xrt.backends.raycing.oes.OE`,
:class:`~xrt.backends.raycing.apertures.RectangularAperture` or
:class:`~xrt.backends.raycing.apertures.RoundAperture`.
*nrays* of samples are randomly distributed over the slit area.
"""
if rw is None:
from . import waves as rw
nrays = int(nrays)
wave = rs.Beam(nrays=nrays, forceState=1, withAmplitudes=True)
xy = np.random.rand(nrays, 2)
dX = self.limOptX[1] - self.limOptX[0]
dZ = self.limOptY[1] - self.limOptY[0]
wave.x[:] = xy[:, 0] * dX + self.limOptX[0]
wave.z[:] = xy[:, 1] * dZ + self.limOptY[0]
wave.area = dX * dZ
wave.dS = wave.area / nrays
wave.toOE = self
glo = rs.Beam(copyFrom=wave)
self.local_to_global(glo)
rw.prepare_wave(prevOE, wave, glo.x, glo.y, glo.z)
return wave
def propagate_wave(self, wave=None, beam=None, nrays='auto'):
"""
Propagates the incoming *wave* through an aperture using the
Kirchhoff diffraction theorem. Returned global and local beams can be
used correspondingly for the consequent ray and wave propagation
calculations.
*wave*: Beam object
Local beam on the surface of the previous optical element.
*beam*: Beam object
Incident global beam, only used for alignment purpose.
*nrays*: 'auto' or int
Dimension of the created wave. If 'auto' - the same as the incoming
wave.
.. Returned values: beamGlobal, beamLocal
"""
from . import waves as rw
waveSize = len(wave.x) if nrays == 'auto' else int(nrays)
prevOE = self.bl.oesDict[wave.parentId]
if raycing._VERBOSITY_ > 10:
print("Diffract", self.name, " Prev OE:", prevOE.name)
if self.bl is not None:
if beam is not None:
self.bl.auto_align(self, beam)
elif 'source' in str(type(prevOE)):
self.bl.auto_align(self, wave)
else:
self.bl.auto_align(self, prevOE.local_to_global(
wave, returnBeam=True))
waveOnSelf = self.prepare_wave(prevOE, waveSize, rw=rw)
if 'source' in str(type(prevOE)):
retGlo = prevOE.shine(wave=waveOnSelf)
else:
retGlo = rw.diffract(wave, waveOnSelf)
waveOnSelf.parentId = self.name
return retGlo, waveOnSelf
class SetOfRectangularAperturesOnZActuator(RectangularAperture):
"""Implements a set of coplanar apertures with a Z actuator."""
def __init__(self, bl, name, center, apertures, centerZs, dXs, dZs,
x='auto', z='auto', alarmLevel=None):
"""
*apertures*: sequence of str
Names of apertures. The last one must be one of 'bottom-edge' or
'top-edge'.
*centerZs*: sequence of float
Z coordinates of the aperture centers relative to center[2].
The last one specifies the edge.
*dXs* and *dZs*: sequence of float
Openings in x and z local axes which correspond to
*apertures[:-1]*.
*x, z*: 3-tuples or 'auto'.
Normalized 3D vectors in the global system which determine the
local x and z axes lying in the aperture plane. If *x* is 'auto',
it is horizontal and normal to the beam line. If *z* is 'auto', it
is vertical. Both *x* and *z* can also be set as instance
attributes.
"""
self.bl = bl
if bl is not None:
if self not in bl.slits:
bl.slits.append(self)
self.ordinalNum = len(bl.slits)
self.lostNum = -self.ordinalNum - 1000
raycing.set_name(self, name)
# if name not in [None, 'None', '']:
# self.name = name
# elif not hasattr(self, 'name'):
# self.name = '{0}{1}'.format(self.__class__.__name__,
# self.ordinalNum)
if bl is not None:
if self.bl.flowSource != 'Qook':
bl.oesDict[self.name] = [self, 1]
self.center = center
# if any([xc == 'auto' for xc in self.center]):
# self._center = copy.copy(self.center)
self._x = x
self._z = z
self._set_orientation()
self.zActuator = center[2]
self.z0 = center[2]
self.apertures = apertures
self.centerZs = centerZs
self.dXs = dXs
self.dZs = dZs
self.zlims = None
self.alarmLevel = alarmLevel
# For plotting footprint images:
self.surface = self.apertures
self.limOptX = [0, 0]
self.limOptY = [0, 0]
self.limOptX[0] = [-dx*0.5 for dx in dXs]
self.limOptX[1] = [dx*0.5 for dx in dXs]
self.limOptX[0].append(-500)
self.limOptX[1].append(500)
self.limPhysX = self.limOptX
self.limPhysY = self.limOptY
self.shape = 'rect'
self.spotLimits = []
def select_aperture(self, apertureName, targetZ):
"""Updates self.curAperture index and finds dz offset corresponding to
the requested aperture."""
ca = self.apertures.index(apertureName)
self.curAperture = ca
if ca < len(self.apertures) - 1:
self.kind = 'left', 'right', 'bottom', 'top'
dx = self.dXs[ca] * 0.5
dz = self.dZs[ca] * 0.5
cz = targetZ - self.bl.height
self.opening = -dx, dx, cz-dz, cz+dz
self.zActuator = self.z0 + targetZ - self.centerZs[ca]
else:
if self.apertures[-1] == 'top-edge':
self.kind = 'bottom',
elif self.apertures[-1] == 'bottom-edge':
self.kind = 'top',
else:
raise ValueError('not "top-edge" nor "bottom-edge"!')
self.opening = self.centerZs[-1] - self.bl.height,
self.zActuator = self.z0
maxHalfdZ = max(self.dZs) * 0.5
minZ = min(self.centerZs) + self.zActuator - self.z0
maxZ = max(self.centerZs) + self.zActuator - self.z0
self.zlims = [min(minZ, targetZ) - self.bl.height - maxHalfdZ,
max(maxZ, targetZ) - self.bl.height + maxHalfdZ]
self.set_optical_limits()
def set_optical_limits(self):
"""For plotting footprint images with the envelope apertures."""
addToCz = -self.bl.height + self.zActuator - self.z0
self.limOptY[0] = \
[cz + addToCz - dz*0.5 for cz, dz in zip(self.centerZs, self.dZs)]
self.limOptY[1] = \
[cz + addToCz + dz*0.5 for cz, dz in zip(self.centerZs, self.dZs)]
self.limOptY[0].append(self.centerZs[-1] + addToCz)
self.limOptY[1].append(200)
class RoundAperture(object):
"""Implements a round aperture meant to represent a pipe or a flange."""
def __init__(self, bl=None, name='',
center=[0, 0, 0], r=1, x='auto', z='auto', alarmLevel=None):
""" A round aperture aperture.
*r* is the radius.
*x, z*: 3-tuples or 'auto'.
Normalized 3D vectors in the global system which determine the
local x and z axes lying in the aperture plane. If *x* is 'auto',
it is horizontal and normal to the beam line. If *z* is 'auto', it
is vertical. Both *x* and *z* can also be set as instance
attributes.
"""
self.bl = bl
if bl is not None:
if self not in bl.slits:
bl.slits.append(self)
self.ordinalNum = len(bl.slits)
self.lostNum = -self.ordinalNum - 1000
raycing.set_name(self, name)
# if name not in [None, 'None', '']:
# self.name = name
# elif not hasattr(self, 'name'):
# self.name = '{0}{1}'.format(self.__class__.__name__,
# self.ordinalNum)
if bl is not None:
if self.bl.flowSource != 'Qook':
bl.oesDict[self.name] = [self, 1]
self.center = center
# if any([xc == 'auto' for xc in self.center]):
# self._center = copy.copy(self.center)
self._x = x
self._z = z
self._set_orientation()
self.r = r
self.alarmLevel = alarmLevel
# For plotting footprint images with the envelope aperture:
self.surface = name,
self.limOptX = [-r, r]
self.limOptY = [-r, r]
self.limPhysX = self.limOptX
self.limPhysY = self.limOptY
self.shape = 'round'
self.spotLimits = []
@property
def x(self):
return self._x
@x.setter
def x(self, x):
self._x = copy.copy(x)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def z(self):
return self._z
@z.setter
def z(self, z):
self._z = copy.copy(z)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def center(self):
return self._center if self._centerVal is None else self._centerVal
@center.setter
def center(self, center):
if any([x == 'auto' for x in center]):
self._center = copy.copy(center)
self._centerVal = None
self._centerInit = copy.copy(center)
else:
self._centerVal = center
def _set_orientation(self):
"""Determines the local x, y and z as vectors in the global system."""
if isinstance(self._x, raycing.basestring):
self._x = None
if isinstance(self._z, raycing.basestring):
self._z = None
self.xyz = raycing.xyz_from_xz(self, self._x, self._z)
def set_orientation(self, x=None, z=None):
"""Compatibility method. All calculations moved to setters."""
self._x = x
self._z = z
self._set_orientation()
def get_divergence(self, source):
"""Gets the full divergence given the aperture radius."""
ss = [a - b for a, b in zip(self.center - source.center)]
return self.r * 2 * (np.dot(ss, ss) ** -0.5)
def propagate(self, beam=None, needNewGlobal=False):
"""Assigns the "lost" value to *beam.state* array for the rays
intercepted by the aperture. The "lost" value is
``-self.ordinalNum - 1000.``
.. Returned values: beamLocal
"""
if self.bl is not None:
self.bl.auto_align(self, beam)
good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
path = -lo.y[good] / lo.b[good]
lo.x[good] += lo.a[good] * path
lo.z[good] += lo.c[good] * path
lo.r = (lo.x[good]**2 + lo.z[good]**2)**0.5
lo.path[good] += path
badIndices = np.zeros(len(beam.x), dtype=bool)
badIndices[good] = lo.r > self.r
beam.state[badIndices] = self.lostNum
lo.state[good] = beam.state[good]
lo.y[good] = 0.
if hasattr(lo, 'Es'):
propPhase = np.exp(1e7j * (lo.E[good]/CHBAR) * path)
lo.Es[good] *= propPhase
lo.Ep[good] *= propPhase
if self.alarmLevel is not None:
raycing.check_alarm(self, good, beam)
if needNewGlobal:
glo = rs.Beam(copyFrom=lo)
raycing.virgin_local_to_global(self.bl, glo, self.center, good)
return glo, lo
else:
raycing.append_to_flow(self.propagate, [lo],
inspect.currentframe())
return lo
def local_to_global(self, glo, **kwargs):
raycing.virgin_local_to_global(self.bl, glo, self.center, **kwargs)
def prepare_wave(self, prevOE, nrays, rw=None):
"""Creates the beam arrays used in wave diffraction calculations.
*prevOE* is the diffracting element: a descendant from
:class:`~xrt.backends.raycing.oes.OE`,
:class:`~xrt.backends.raycing.apertures.RectangularAperture` or
:class:`~xrt.backends.raycing.apertures.RoundAperture`.
*nrays* of samples are randomly distributed over the slit area.
"""
if rw is None:
from . import waves as rw
nrays = int(nrays)
wave = rs.Beam(nrays=nrays, forceState=1, withAmplitudes=True)
xy = np.random.rand(nrays, 2)
r = xy[:, 0]**0.5 * self.r
phi = xy[:, 1] * 2*np.pi
wave.x[:] = r * np.cos(phi)
wave.z[:] = r * np.sin(phi)
wave.area = np.pi * self.r**2
wave.dS = wave.area / nrays
wave.toOE = self
glo = rs.Beam(copyFrom=wave)
self.local_to_global(glo)
rw.prepare_wave(prevOE, wave, glo.x, glo.y, glo.z)
return wave
def propagate_wave(self, wave=None, beam=None, nrays='auto'):
"""
Propagates the incoming *wave* through an aperture using the
Kirchhoff diffraction theorem. Returned global and local beams can be
used correspondingly for the consequent ray and wave propagation
calculations.
*wave*: Beam object
Local beam on the surface of the previous optical element.
*beam*: Beam object
Incident global beam, only used for alignment purpose.
*nrays*: 'auto' or int
Dimension of the created wave. If 'auto' - the same as the incoming
wave.
.. Returned values: beamLocal
"""
from . import waves as rw
waveSize = len(wave.x) if nrays == 'auto' else int(nrays)
prevOE = self.bl.oesDict[wave.parentId]
if self.bl is not None:
if beam is not None:
self.bl.auto_align(self, beam)
elif 'source' in str(type(prevOE)):
self.bl.auto_align(self, wave)
else:
self.bl.auto_align(self, prevOE.local_to_global(
wave, returnBeam=True))
waveOnSelf = self.prepare_wave(prevOE, waveSize, rw=rw)
if 'source' in str(type(prevOE)):
prevOE.shine(wave=waveOnSelf)
else:
rw.diffract(wave, waveOnSelf)
waveOnSelf.parentId = self.name
return self.local_to_global(waveOnSelf, returnBeam=True), waveOnSelf
class RoundBeamStop(RoundAperture):
"""Implements a round beamstop. Descends from RoundAperture and has the
same parameters."""
def propagate(self, beam=None, needNewGlobal=False):
"""Assigns the "lost" value to *beam.state* array for the rays
intercepted by the aperture. The "lost" value is
``-self.ordinalNum - 1000.``
.. Returned values: beamLocal
"""
if self.bl is not None:
self.bl.auto_align(self, beam)
good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
with np.errstate(divide='ignore'):
path = -lo.y[good] / lo.b[good]
indBad = np.where(np.isnan(path))
path[indBad] = 0.
indBad = np.where(np.isinf(path))
path[indBad] = 0.
lo.x[good] += lo.a[good] * path
lo.z[good] += lo.c[good] * path
lo.r = (lo.x[good]**2 + lo.z[good]**2)**0.5
lo.path[good] += path
badIndices = np.zeros(len(beam.x), dtype=bool)
badIndices[good] = lo.r < self.r
beam.state[badIndices] = self.lostNum
lo.state[good] = beam.state[good]
lo.y[good] = 0.
if hasattr(lo, 'Es'):
propPhase = np.exp(1e7j * (lo.E[good]/CHBAR) * path)
lo.Es[good] *= propPhase
lo.Ep[good] *= propPhase
if self.alarmLevel is not None:
raycing.check_alarm(self, good, beam)
if needNewGlobal:
glo = rs.Beam(copyFrom=lo)
raycing.virgin_local_to_global(self.bl, glo, self.center, good)
glo.path[good] += beam.path[good]
return glo, lo
else:
raycing.append_to_flow(self.propagate, [lo],
inspect.currentframe())
return lo
class DoubleSlit(RectangularAperture):
"""Implements an aperture or an obstacle with a combination of horizontal
and/or vertical edge(s)."""
def __init__(self, *args, **kwargs):
"""Same parameters as in :class:`RectangularAperture` and additionally
*shadeFraction* as a value from 0 to 1.
"""
self.shadeFraction = kwargs.pop('shadeFraction', 0.5)
super(DoubleSlit, self).__init__(*args, **kwargs)
def propagate(self, beam=None, needNewGlobal=False):
"""Assigns the "lost" value to *beam.state* array for the rays
intercepted by the aperture. The "lost" value is
``-self.ordinalNum - 1000.``
.. Returned values: beamLocal
"""
if self.bl is not None:
self.bl.auto_align(self, beam)
shadeMin = (1 - self.shadeFraction) * 0.5
shadeMax = shadeMin + self.shadeFraction
good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
path = -lo.y[good] / lo.b[good]
lo.x[good] += lo.a[good] * path
lo.z[good] += lo.c[good] * path
lo.path[good] += path
badIndices = np.zeros(len(beam.x), dtype=bool)
for akind, d in zip(self.kind, self.opening):
if akind.startswith('l'):
badIndices[good] = badIndices[good] | (lo.x[good] < d)
elif akind.startswith('r'):
badIndices[good] = badIndices[good] | (lo.x[good] > d)
elif akind.startswith('b'):
badIndices[good] = badIndices[good] | (lo.z[good] < d)
dsb = d
elif akind.startswith('t'):
badIndices[good] = badIndices[good] | (lo.z[good] > d)
dst = d
sb = dsb + (dst - dsb) * shadeMin
st = dsb + (dst - dsb) * shadeMax
badIndices[good] = \
badIndices[good] | ((lo.z[good] > sb) & (lo.z[good] < st))
beam.state[badIndices] = self.lostNum
lo.state[good] = beam.state[good]
lo.y[good] = 0.
if hasattr(lo, 'Es'):
propPhase = np.exp(1e7j * (lo.E[good]/CHBAR) * path)
lo.Es[good] *= propPhase
lo.Ep[good] *= propPhase
if self.alarmLevel is not None:
raycing.check_alarm(self, good, beam)
if needNewGlobal:
glo = rs.Beam(copyFrom=lo)
raycing.virgin_local_to_global(self.bl, glo, self.center, good)
glo.path[good] += beam.path[good]
return glo, lo
else:
raycing.append_to_flow(self.propagate, [lo],
inspect.currentframe())
return lo
class PolygonalAperture(object):
"""Implements an aperture or an obstacle defined as a set of polygon
vertices."""
def __init__(self, bl=None, name='', center=[0, 0, 0],
opening=None, x='auto', z='auto', alarmLevel=None):
"""
*bl*: instance of :class:`~xrt.backends.raycing.BeamLine`
Container for beamline elements. Optical elements are added to its
`slits` list.
*name*: str
User-specified name, can be used for diagnostics output.
*center*: 3-sequence of floats
3D point in global system.
*opening*: sequence
Coordinates [(x0, y0),...(xN, yN)] of the polygon vertices.
*x, z*: 3-tuples or 'auto'.
Normalized 3D vectors in the global system which determine the
local x and z axes lying in the aperture plane. If *x* is 'auto',
it is horizontal and normal to the beam line. If *z* is 'auto', it
is vertical. Both *x* and *z* can also be set as instance
attributes.
*alarmLevel*: float or None.
Allowed fraction of number of rays absorbed at the aperture
relative to the number of incident rays. If exceeded, an alarm
output is printed in the console.
"""
self.bl = bl
if bl is not None:
bl.slits.append(self)
self.ordinalNum = len(bl.slits)
self.lostNum = -self.ordinalNum - 1000
if name in [None, 'None', '']:
self.name = '{0}{1}'.format(
self.__class__.__name__,
self.ordinalNum if bl is not None else '')
else:
self.name = name
if bl is not None:
if self.bl.flowSource != 'Qook':
bl.oesDict[self.name] = [self, 1]
self.center = center
# if any([xc == 'auto' for xc in self.center]):
# self._center = self.center
self._x = x
self._z = z
self._set_orientation()
self.opening = opening
self.vertices = np.array(self.opening)
self.alarmLevel = alarmLevel
# For plotting footprint images with the envelope aperture:
self.surface = name,
self.limOptX = [-500, 500]
self.limOptY = [-500, 500]
self.limPhysX = self.limOptX
self.limPhysY = self.limOptY
if opening is not None:
self.set_optical_limits()
self.shape = 'polygon'
@property
def x(self):
return self._x
@x.setter
def x(self, x):
self._x = copy.copy(x)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def z(self):
return self._z
@z.setter
def z(self, z):
self._z = copy.copy(z)
self._set_orientation()
# self.update_orientation_quaternion()
@property
def center(self):
return self._center if self._centerVal is None else self._centerVal
@center.setter
def center(self, center):
if any([x == 'auto' for x in center]):
self._center = copy.copy(center)
self._centerVal = None
self._centerInit = copy.copy(center)
else:
self._centerVal = center
def _set_orientation(self):
"""Determines the local x, y and z as vectors in the global system."""
if isinstance(self._x, raycing.basestring):
self._x = None
if isinstance(self._z, raycing.basestring):
self._z = None
self.xyz = raycing.xyz_from_xz(self, self._x, self._z)
def set_orientation(self, x=None, z=None):
"""Compatibility method. All calculations moved to setters."""
self._x = x
self._z = z
self._set_orientation()
def set_optical_limits(self):
"""For plotting footprint images with the envelope aperture."""
self.limOptX = [np.nanmin(self.vertices[:, 0]),
np.nanmax(self.vertices[:, 0])]
self.limOptY = [np.nanmin(self.vertices[:, 1]),
np.nanmax(self.vertices[:, 1])]
def propagate(self, beam=None, needNewGlobal=False):
"""Assigns the "lost" value to *beam.state* array for the rays
intercepted by the aperture. The "lost" value is
``-self.ordinalNum - 1000.``
.. Returned values: beamLocal
"""
if self.bl is not None:
self.bl.auto_align(self, beam)
good = beam.state > 0
# beam in local coordinates
lo = rs.Beam(copyFrom=beam)
bl = self.bl if self.xyz == 'auto' else self.xyz
raycing.global_to_virgin_local(bl, beam, lo, self.center, good)
path = -lo.y[good] / lo.b[good]
lo.x[good] += lo.a[good] * path
lo.z[good] += lo.c[good] * path
lo.path[good] += path
footprint = mplPath(self.vertices)
badIndices = np.invert(footprint.contains_points(np.array(
list(zip(lo.x, lo.z)))))
beam.state[badIndices] = self.lostNum
lo.state[good] = beam.state[good]
lo.y[good] = 0.
if hasattr(lo, 'Es'):
propPhase = np.exp(1e7j * (lo.E[good]/CHBAR) * path)
lo.Es[good] *= propPhase
lo.Ep[good] *= propPhase
if self.alarmLevel is not None:
raycing.check_alarm(self, good, beam)
if needNewGlobal:
glo = rs.Beam(copyFrom=lo)
raycing.virgin_local_to_global(self.bl, glo, self.center, good)
return glo, lo
else:
raycing.append_to_flow(self.propagate, [lo],
inspect.currentframe())
return lo
def local_to_global(self, glo, **kwargs):
raycing.virgin_local_to_global(self.bl, glo, self.center, **kwargs)
def prepare_wave(self, prevOE, nrays):
"""Creates the beam arrays used in wave diffraction calculations.
*prevOE* is the diffracting element: a descendant from
:class:`~xrt.backends.raycing.oes.OE`,
:class:`~xrt.backends.raycing.apertures.RectangularAperture` or
:class:`~xrt.backends.raycing.apertures.RoundAperture`.
*nrays* of samples are randomly distributed over the slit area.
"""
from . import waves as rw
nrays = int(nrays)
wave = rs.Beam(nrays=nrays, forceState=1, withAmplitudes=True)
dX = self.limOptX[1] - self.limOptX[0]
dZ = self.limOptY[1] - self.limOptY[0]
footprint = mplPath(self.vertices)
randRays = 0
goodX = []
goodY = []
while randRays < nrays:
xy = np.random.rand(nrays, 2)
rndX = xy[:, 0] * dX + self.limOptX[0]
rndY = xy[:, 1] * dZ + self.limOptY[0]
inDots = footprint.contains_points(list(zip(rndX, rndY)))
goodX = rndX[inDots] if randRays == 0 else\
np.append(goodX, rndX[inDots])
goodY = rndY[inDots] if randRays == 0 else\
np.append(goodY, rndY[inDots])
randRays = len(goodX)
if raycing._VERBOSITY_ > 10:
print("Generated {0} dots of {1}".format(randRays, nrays))
wave.x[:] = goodX[:nrays]
wave.z[:] = goodY[:nrays]
wave.area = 0.5 * np.abs(
np.dot(self.vertices[:, 0], np.roll(self.vertices[:, 1], 1)) -
np.dot(self.vertices[:, 1], np.roll(self.vertices[:, 0], 1)))
wave.dS = wave.area / nrays
wave.toOE = self
glo = rs.Beam(copyFrom=wave)
self.local_to_global(glo)
rw.prepare_wave(prevOE, wave, glo.x, glo.y, glo.z)
return wave
class GridAperture(PolygonalAperture):
"""Implements a grid of rectangular apertures.
See `tests/raycing/test_polygonal_aperture.py`"""
def __init__(self, bl=None, name='', center=[0, 0, 0],
x='auto', z='auto', alarmLevel=None,
dx=0.5, dz=0.5, px=1.0, pz=1.0, nx=7, nz=7):
"""
*dx* and *dz*: float
Opening sizes (horizontal and vertical).
*px* and *pz*: float
Pitch steps (periods).
*nx* and *nz*: int
The number of grid openings, counted from the central hole in -ve
and +ve directions, making (2*nx+1)*(2*nz+1) rectangular holes in
total.
"""
# 4 corners + the 1st one to close the path + nan to disconnect patches
cellx = np.array([dx, -dx, -dx, dx, dx, np.nan]) * 0.5
cellz = np.array([dz, dz, -dz, -dz, dz, np.nan]) * 0.5
xc = np.linspace(-1, 1, 2*nx+1) * px * nx
zc = np.linspace(-1, 1, 2*nz+1) * pz * nz
xm, zm = np.meshgrid(xc, zc)
xi = (xm.ravel(order='F') + cellx[:, np.newaxis]).ravel(order='F')
zi = (zm.ravel(order='F') + cellz[:, np.newaxis]).ravel(order='F')
opening = np.column_stack((xi, zi))
super().__init__(bl=bl, name=name, center=center, opening=opening,
x=x, z=z, alarmLevel=alarmLevel)
class SiemensStar(PolygonalAperture):
"""Implements a Siemens Star pattern.
See `tests/raycing/test_polygonal_aperture.py`"""
def __init__(self, bl=None, name='', center=[0, 0, 0],
x='auto', z='auto', alarmLevel=None, nSpokes=9,
r=0, rx=0, rz=0, phi0=0, vortex=0, vortexNradial=7):
"""
*nSpokes*: int
The number of spoke openings.
*r* or (*rx* and *rz*): float
The radius of spokes or ellipse semiaxes.
*phi0*: float
The angle of rotation of the star. At 0 (default), the center of
the top spoke is vertical.
*vortex*: float
Lets the spokes bend. At =1, they bend by one spoke position.
*vortexNradial*: int
Used with non-zero *vortex*. The number of segments in the bent
spokes.
"""
if r:
slitRx = r
slitRz = slitRx
else:
slitRx = rx
slitRz = rz
star = (np.linspace(0, 2*np.pi, nSpokes*2, endpoint=False) -
np.pi/nSpokes/2 - phi0)
if vortex:
xstack = []
ystack = []
for ir in reversed(range(vortexNradial)):
fr = (ir+1.) / vortexNradial
dphi = 2*np.pi * vortex / nSpokes * fr
starX = (fr * slitRx * np.sin(star+dphi)).reshape((nSpokes, 2))
starY = (fr * slitRz * np.cos(star+dphi)).reshape((nSpokes, 2))
xstack.insert(0, starX[:, 0:1])
xstack.append(starX[:, 1:2])
ystack.insert(0, starY[:, 0:1])
ystack.append(starY[:, 1:2])
else:
starX = slitRx * np.sin(star)
starY = slitRz * np.cos(star)
xstack = [starX.reshape((nSpokes, 2))]
ystack = [starY.reshape((nSpokes, 2))]
xstack.append(np.zeros((nSpokes, 1)))
ystack.append(np.zeros((nSpokes, 1)))
starXs = np.hstack(xstack)
starYs = np.hstack(ystack)
opening = list(zip(starXs.flatten(), starYs.flatten()))
super().__init__(bl=bl, name=name, center=center, opening=opening,
x=x, z=z, alarmLevel=alarmLevel)
|