Skip to content

Commit

Permalink
fixed zero order reflection and added print
Browse files Browse the repository at this point in the history
statements
  • Loading branch information
Wang, Yuheng (DLSLtd, RAL, SCI) committed Nov 8, 2023
1 parent d0cc4cb commit 539eb99
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
8 changes: 6 additions & 2 deletions pgmcomponents/elements/grating.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def compute_corners(self)-> np.ndarray:

return self._corners

def reflect(self, *args)-> list:
def reflect(self, *args, zero_order = False)-> list:
"""
A method to reflect rays off the grating.
Expand Down Expand Up @@ -536,7 +536,11 @@ def reflect(self, *args)-> list:
continue
ray_array = ray.vector
grating_normal = self._grating_plane.normal
reflected_ray_array = ray_array - 2 * np.dot(ray_array, grating_normal) * grating_normal
if zero_order:

reflected_ray_array = ray_array - 2 * np.dot(ray_array, -grating_normal) * grating_normal
else:
reflected_ray_array = ray_array - 2 * np.dot(ray_array, grating_normal) * grating_normal
reflected_ray_array = reflected_ray_array / np.linalg.norm(reflected_ray_array)
reflected_ray = Ray3D(plane_intersection, reflected_ray_array)
reflected_rays.append(reflected_ray)
Expand Down
4 changes: 3 additions & 1 deletion pgmcomponents/elements/pgm.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,9 @@ def draw_sideview(self, ax):
line = Line2D(r_z, r_x, color='green', linewidth=1, label='Dispersed Rays')
ax.add_line(line)

zero_order_rays = self.grating.reflect(self.rays)
zero_order_rays = self.grating.reflect(self.rays, zero_order=True)


for index, ray in enumerate(zero_order_rays):
r_z = np.array([
grating_int[index].z,
Expand Down
20 changes: 4 additions & 16 deletions pgmcomponents/gui/gui_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,12 @@ def window(self, pgm: PGM, window: sg.Window)-> None:
float(values['distance']),
float(values['id_length']),
num_of_sigmas=float(values['num_of_sigmas']))
print('save Beam size h is ', beam_size_h)
beam_size_v = calc_beam_size(float(values['electron_size_v']),
float(values['electron_div_v']),
pgm.wavelength,
float(values['distance']),
float(values['id_length']),
num_of_sigmas=float(values['num_of_sigmas']))

print('save Beam size v is ', beam_size_v)
self.beam_size_h = beam_size_h
self.beam_size_v = beam_size_v
pgm.beam_width = beam_size_h
Expand Down Expand Up @@ -517,23 +514,14 @@ def window(self, pgm: PGM, window: sg.Window)-> None:

if check_fill() and values['calculate_q']:
try:
print("called in second loop")
print('Wavelength is ', pgm.wavelength)
print('Distance is ', float(values['distance']))
print('ID length is ', float(values['id_length']))
print('Number of sigmas is ', float(values['num_of_sigmas']))
print('Electron size h is ', float(values['electron_size_h']))
print('Electron size v is ', float(values['electron_size_v']))
print('Electron div h is ', float(values['electron_div_h']))
print('Electron div v is ', float(values['electron_div_v']))

beam_size_h = calc_beam_size(float(values['electron_size_h']),
float(values['electron_div_h']),
pgm.wavelength,
float(values['distance']),
float(values['id_length']),
num_of_sigmas=float(values['num_of_sigmas']))
print('tick Beam size h is ', beam_size_h)


beam_size_v = calc_beam_size(float(values['electron_size_v']),
float(values['electron_div_v']),
Expand All @@ -548,7 +536,7 @@ def window(self, pgm: PGM, window: sg.Window)-> None:
self.beam_size_v = beam_size_v
pgm.beam_height = beam_size_v
pgm.beam_width = beam_size_h
print('tick Beam size v is ', beam_size_v)

pass

except Exception as e:
Expand Down Expand Up @@ -579,7 +567,7 @@ class OffsetsControl(object):

def __init__(self, values, key):
self.values = values
print('values:',values)

self.key = key

layout = [
Expand Down Expand Up @@ -857,7 +845,7 @@ def update_and_draw(window,
"""

print(Fore.RED + 'updated in fn' + Style.RESET_ALL)

energy_input = float(values['-ENERGY-'])
cff_input = float(values['-CFF-'])
order_input = int(values['-ORDER-'])
Expand Down
15 changes: 8 additions & 7 deletions pgmcomponents/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def main():
version = sg.__version__
version_str = version.split('.')
print(Fore.GREEN,"Welcome to pgm-gui!", Fore.RESET)
print("Checking PySimpleGUI version...")
print(Fore.YELLOW,"Checking PySimpleGUI version...", Fore.RESET)
if len(version_str) <= 3:
print(Fore.RED + "PySimpleGUI is not up-to-date, see popup!" + Fore.RESET)
response = sg.PopupOKCancel(f"Your PySimpleGUI version is {version}\nThis program requires version the github dev version.\n Press OK to begin upgrade sequence.")
Expand All @@ -48,7 +48,7 @@ def main():
menu = [['File', ['Open workspace', 'Save workspace', 'Exit']],
['Export', ['Export Mirror', 'Export Grating', 'Export Beams', 'Export All']],
['Help', 'About...'], ]
print("Initialising control widgets...")
print(Fore.CYAN,"Initialising control widgets...", Fore.RESET)
energy_control = EPICScontrol('Energy (eV)', 250.,10., '-ENERGY-', pgm=pgm)
cff_control = EPICScontrol(u'Cff', 2.,0.1, '-CFF-',pgm=pgm)
order_control = EPICScontrol('Order', 1,1, '-ORDER-',pgm=pgm)
Expand All @@ -58,8 +58,9 @@ def main():
offsets_control = OffsetsControl(offset_defaults, '-OFFSETS-')
up_events = {'-ENERGY-_up':energy_control, '-CFF-_up':cff_control, '-ORDER-_up':order_control, '-LINE_DENSITY-_up':line_density_control}
down_events = {'-ENERGY-_down':energy_control, '-CFF-_down':cff_control, '-ORDER-_down':order_control, '-LINE_DENSITY-_down':line_density_control}
print(Fore.GREEN, "Control widgets initialised!", Fore.RESET)

print("Initialising widgets...")
print(Fore.CYAN, "Initialising widgets...", Fore.RESET)

beam_config = Beam_Config()
topview_widget = Topview_Widget(pgm, size=(1000,500))
Expand All @@ -83,9 +84,10 @@ def main():
],
[sideview_widget.frame]
]]
print("Initialising window...")
print(Fore.GREEN, "Widgets initialised!", Fore.RESET)
print(Fore.CYAN, "Initialising window...", Fore.RESET)
window = sg.Window('PGM Simulation', layout, finalize=True,icon='icon.png', resizable=True, return_keyboard_events=True)

print(Fore.GREEN, "Window initialised!", Fore.RESET)

_,_ = pgm.grating.compute_angles()
_=pgm.mirror.compute_corners()
Expand All @@ -110,8 +112,7 @@ def main():
]
while True:
event, values = window.read()
print(event)
print(pgm.beam_height, pgm.beam_width, pgm.beam_offset)
print(Fore.LIGHTYELLOW_EX, 'Event:',Fore.RESET, event)
if event in update_events:

update_and_draw(window, pgm, values, topview_widget, sideview_widget, energy_control, cff_control, order_control, line_density_control, offsets_control)
Expand Down

0 comments on commit 539eb99

Please sign in to comment.