-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
348 lines (279 loc) · 13.9 KB
/
main.py
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
# Copyright (C) 2018 Fabian Schmid (fabian.schmid@mpq.mpg.de)
# This file is part of shg-cavity-calculator
#
# shg-cavity-calculator is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
# shg-cavity-calculator is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
# You should have received a copy of the GNU General Public License along with
# shg-cavity-calculator. If not, see <https://www.gnu.org/licenses/>.
import sys
import json
import logging
from PyQt5 import QtWidgets, QtCore
import pyqtgraph
import numpy as np
import cavity
import widgets
import dialogs
#import shg_efficiency
DEFAULT_CAVITY_CONFIG = "default_cavity_config.json"
LICENSEPATH = "COPYING"
class MainWindow(QtWidgets.QMainWindow):
"""Main application class."""
def switch_cavity_setup(self, brewster):
"""Switch between Brewster-cut and plane crystal.
:param brewster: True for a Brewster-cut crystal, False for a plane crystal
"""
self.cavity_parameters["Brewster"] = brewster
self.update_cavity_mode()
def update_cavity_mode(self):
"""Calculate the cavity mode and update the first three plots."""
try:
result = cavity.solve_cavity_s_range(self.cavity_parameters)
except ValueError:
self.output_widget.display_result(None)
logging.info("Cavity unstable for current parameters.")
return
self.update_plots(result)
s_min, s_max = cavity.s_bounds(self.cavity_parameters)
self.input_widget.update_s_parameter_range(s_min+100E-6,
s_max-100E-6)
if self.cavity_parameters["s"] < s_min or self.cavity_parameters["s"] > s_max:
# Set the vertical lines roughly in the middle of the plots
self.update_s(1E3*(s_min+s_max)/2)
self.input_widget.update_parameters(self.cavity_parameters)
else:
self.update_s(1E3*self.cavity_parameters["s"])
def update_s(self, value):
"""Change the distance s between the focussing mirrors and the crystal surface and move the
vertical lines in the first three plots.
:param value: The value to set s to in mm.
"""
self.cavity_parameters["s"] = value/1E3
try:
result = cavity.solve_cavity(self.cavity_parameters)
except ValueError:
self.output_widget.display_result(None)
logging.info("Cavity unstable for current s value.")
return
self.output_widget.display_result(result)
self.update_s_lines(self.cavity_parameters["s"])
def update_angle(self, value):
"""Change the mirror incidence angle and update the first three plots.
:param value: The value in degree to set the angle to.
"""
self.cavity_parameters["alpha"] = 2*np.pi*value/360.
self.update_cavity_mode()
def update_mirror_radius(self, value):
"""Change the mirror radius of curvature and update the first three plots.
:param value: The value in mm to set the radius of curvature to.
"""
self.cavity_parameters["f"] = value*1E-3/2.
self.update_cavity_mode()
def update_crystal_length(self, value):
"""Change the length l of the nonlinear crystal and update the first three plots.
:param value: The value in mm to set the length to.
"""
self.cavity_parameters["l"] = value*1E-3
self.update_cavity_mode()
def update_distance_secondary_focus(self, value):
"""Change the distance v between the focussing mirror and the secondary focus and update
the first three plots.
:param value: The value in mm to set the distance to.
"""
self.cavity_parameters["v"] = value*1E-3
self.update_cavity_mode()
def update_wavelength(self, value):
"""Change the wavelength to the value obtained from the input field and update the first
three plots.
:param value: The value in nm to set the wavelength to.
"""
self.cavity_parameters["wavelength"] = value*1E-9
self.update_cavity_mode()
def update_refractive_index(self, value):
"""Change the crystal refractive index to the value obtained from the input field and update
the first three plots.
:param value: The value to set the refractive index to.
"""
self.cavity_parameters["eta"] = value
self.update_cavity_mode()
def update_b_parameter(self, value):
"""Change the crystal B parameter (Boyd-Kleinman) to the value obtained from the input field
and update the first three plots.
:param value: The value to set the B parameter to.
"""
self.cavity_parameters["B"] = value
self.update_cavity_mode()
def load_cavity_parameters(self, filename):
"""Load cavity parameters from a json file and update the UI widgets.
:param filename: Path to the json configuration file.
"""
try:
with open(filename) as config_file:
self.cavity_parameters = json.load(config_file)
except FileNotFoundError:
logging.error("Cavity configuration file not found: %s", filename)
self.input_widget.update_parameters(self.cavity_parameters)
self.setup_widget.update_parameters(self.cavity_parameters)
def load_default_cavity_parameters(self):
"""Load the default cavity parameters, and update the UI widgets.
"""
self.load_cavity_parameters(DEFAULT_CAVITY_CONFIG)
self.update_cavity_mode()
def save_cavity_parameters(self, filename):
"""Save cavity parameters to a json file.
:param filename: Path to the json configuration file.
"""
try:
with open(filename, "w") as config_file:
json.dump(self.cavity_parameters, config_file)
except PermissionError as error:
logging.error("Could not write configuration file: %s", error)
def open_parameter_file_dialog(self):
"""Open a QFileDialog and load a saved cavity configuration from the chosen file."""
cavity_config_path = QtWidgets.QFileDialog.getOpenFileName(
filter="Configuration files (*.json)")[0]
if cavity_config_path == "":
logging.info("No file chosen for loading cavity config.")
return
self.load_cavity_parameters(cavity_config_path)
self.update_cavity_mode()
def save_parameter_file_dialog(self):
"""Open a QFileDialog and save the current cavity configuration to the chosen file."""
cavity_config_path = QtWidgets.QFileDialog.getSaveFileName(
filter="Configuration files (*.json)")[0]
if cavity_config_path == "":
logging.info("No file chosen for saving cavity config.")
return
self.save_cavity_parameters(cavity_config_path)
def init_plots(self):
"""Initialize the plots in the graphics_layout_widget."""
crystal_focus_plot = self.graphics_layout_widget.addPlot(row=0, col=0)
crystal_focus_plot.addLegend()
self.crystal_waist_curve_tangential = crystal_focus_plot.plot(
name="tangential", pen=pyqtgraph.mkPen(color="r"))
self.crystal_waist_curve_sagittal = crystal_focus_plot.plot(
name="sagittal", pen=pyqtgraph.mkPen(color="g"))
self.crystal_focus_s_line = pyqtgraph.InfiniteLine()
crystal_focus_plot.addItem(self.crystal_focus_s_line)
crystal_focus_plot.setTitle("Crystal focus beam waists")
crystal_focus_plot.setLabels(
left="Beam waist (µm)", bottom="Distance focusing mirror to crystal surface (mm)")
crystal_ellipticity_plot = self.graphics_layout_widget.addPlot(row=0, col=1)
self.crystal_ellipticity_curve = crystal_ellipticity_plot.plot(
pen=pyqtgraph.mkPen(color="r"))
self.crystal_ellipticity_s_line = pyqtgraph.InfiniteLine()
crystal_ellipticity_plot.addItem(self.crystal_ellipticity_s_line)
crystal_ellipticity_plot.setTitle("Crystal focus ellipticity")
crystal_ellipticity_plot.setLabels(
left="Ellipticity", bottom="Distance focusing mirror to crystal surface (mm)")
collimated_ellipticity_plot = self.graphics_layout_widget.addPlot(row=1, col=0)
self.collimated_ellipticity_curve = collimated_ellipticity_plot.plot(
pen=pyqtgraph.mkPen(color="r"))
self.collimated_ellipticity_s_line = pyqtgraph.InfiniteLine()
collimated_ellipticity_plot.addItem(self.collimated_ellipticity_s_line)
collimated_ellipticity_plot.setTitle("Collimated arm ellipticity")
collimated_ellipticity_plot.setLabels(
left="Ellipticity", bottom="Distance focusing mirror to crystal surface (mm)")
def update_plots(self, result_dict):
"""Update the first three plots showing beam waists and ellipticities vs. the distance s
between focussing mirror and crystal.
:param result_dict: A dict containing the parameters of the cavity eigenmodes (see
cavity.solve_cavity_s_range() docstring for details)
"""
self.crystal_waist_curve_tangential.setData(
1E3*np.array(result_dict["s values"]),
1E6*np.array(result_dict["tangential waists crystal"]))
self.crystal_waist_curve_sagittal.setData(
1E3*np.array(result_dict["s values"]),
1E6*np.array(result_dict["sagittal waists crystal"]))
self.crystal_ellipticity_curve.setData(
1E3*np.array(result_dict["s values"]),
np.array(result_dict["ellipticities crystal"]))
self.collimated_ellipticity_curve.setData(
1E3*np.array(result_dict["s values"]),
np.array(result_dict["ellipticities collimated"]))
def update_s_lines(self, s_value):
"""Update the position of the vertical lines in the first three plots.
:param s_value: The position of the vertical lines in m.
"""
self.crystal_focus_s_line.setValue(1E3*s_value)
self.crystal_ellipticity_s_line.setValue(1E3*s_value)
self.collimated_ellipticity_s_line.setValue(1E3*s_value)
def closeEvent(self, e):
"""QCloseEvent handler to handle application shutdown.
:param e: QCloseEvent sent to the widget.
"""
# Save window size and position
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
e.accept()
def _setup_logging(self):
"""Configure the application logging setup."""
logging.basicConfig(level=logging.INFO)
def _setup_ui(self):
"""Set up the application's ui elements."""
self.setWindowTitle("SHG Cavity Calculator")
central_widget = QtWidgets.QWidget()
horizontal_layout = QtWidgets.QHBoxLayout()
central_widget.setLayout(horizontal_layout)
self.setCentralWidget(central_widget)
vertical_layout = QtWidgets.QVBoxLayout()
horizontal_layout.addLayout(vertical_layout)
horizontal_layout.addWidget(self.graphics_layout_widget)
vertical_layout.addWidget(self.setup_widget)
vertical_layout.addWidget(self.output_widget)
vertical_layout.addWidget(self.input_widget)
vertical_layout.addStretch()
def _create_menus(self):
"""Create the application menu bar and its entries."""
self.menu_file.addAction(self.action_load_cavity_config)
self.menu_file.addAction(self.action_save_cavity_config)
self.menu_file.addAction(self.action_load_default_config)
self.menu_help.addAction(self.action_about)
self.menuBar().addMenu(self.menu_file)
self.menuBar().addMenu(self.menu_help)
def __init__(self):
self._setup_logging()
super().__init__()
self.graphics_layout_widget = pyqtgraph.GraphicsLayoutWidget()
self.init_plots()
self.setup_widget = widgets.SetupWidget(self, True)
self.output_widget = widgets.OutputWidget(self)
self.input_widget = widgets.InputWidget(self)
self._setup_ui()
self.menu_file = QtWidgets.QMenu("File")
self.menu_help = QtWidgets.QMenu("Help")
self.action_load_cavity_config = QtWidgets.QAction("Load Cavity Config")
self.action_save_cavity_config = QtWidgets.QAction("Save Cavity Config")
self.action_load_default_config = QtWidgets.QAction("Load Default Cavity Config")
self.action_about = QtWidgets.QAction("About")
self.about_dialog = dialogs.AboutDialog(LICENSEPATH)
self._create_menus()
# Load default parameters
self.cavity_parameters = {}
self.load_cavity_parameters(DEFAULT_CAVITY_CONFIG)
# Load previous window size and position
self.settings = QtCore.QSettings("MPQ", "shg-cavity-calculator")
geometry = self.settings.value("geometry")
windowState = self.settings.value("windowState")
if geometry is not None:
self.restoreGeometry(geometry)
if windowState is not None:
self.restoreState(windowState)
# Connect UI elements
self.action_load_default_config.triggered.connect(self.load_default_cavity_parameters)
self.action_load_cavity_config.triggered.connect(self.open_parameter_file_dialog)
self.action_save_cavity_config.triggered.connect(self.save_parameter_file_dialog)
self.action_about.triggered.connect(self.about_dialog.exec)
self.update_cavity_mode()
self.show()
if __name__ == "__main__":
qt_app = QtWidgets.QApplication(sys.argv)
MAIN_WINDOW = MainWindow()
sys.exit(qt_app.exec_())