Advertisement
Guest User

Untitled

a guest
Feb 5th, 2022
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 25.88 KB | None | 0 0
  1. import distutils.util
  2. import sys
  3.  
  4. import numpy as np
  5. import pyvista as pv
  6. from PyQt5 import QtCore, QtWidgets, QtGui
  7. from PyQt5.QtWidgets import *
  8. # from <filename> of the UI python initialization (content not changed)
  9. from PyQt5.QtCore import pyqtSlot
  10. from pyvistaqt import QtInteractor
  11. from qtpy import QtWidgets
  12.  
  13. from pyvistaGUI import (Ui_MainWindow)
  14.  
  15.  
  16. def coverage_calc(x_1, y_1, x_2, y_2):
  17.     # Calculate Slope
  18.     slope = (y_1 - y_2) / (x_1 - x_2)
  19.  
  20.     angle = np.degrees(np.arctan(slope))
  21.  
  22.     coverage_angle = (180 - (angle * 2)) * 0.5
  23.  
  24.     return coverage_angle
  25.  
  26.  
  27. def elliptical_calc(waveguide_throat, ellipse_x, ellipse_y, depth_factor, angle_factor):
  28.     array_length = 100
  29.     # now create the actual structured grid
  30.     # 2d circular grid
  31.     r, phi = np.mgrid[0:1:array_length * 1j, 0:np.pi / 2:array_length * 1j]
  32.  
  33.     # transform to ellipse on the outside, circle on the inside
  34.     x = (ellipse_x / 2 * r + waveguide_throat / 2 * (1 - r)) * np.cos(phi)
  35.     y = (ellipse_y / 2 * r + waveguide_throat / 2 * (1 - r)) * np.sin(phi)
  36.  
  37.     # compute z profile
  38.     angle_factor = angle_factor / 10000
  39.     z = (ellipse_x / 2 * r / angle_factor) ** (1 / depth_factor)
  40.  
  41.     throat = np.array(np.column_stack((x[0, 0:array_length], y[0, 0:array_length],
  42.                                        z[0, 0:array_length])))
  43.     ellipse = np.array(np.column_stack((x[array_length - 1, 0:array_length], y[array_length - 1, 0:array_length],
  44.                                         z[array_length - 1, 0:array_length])))
  45.     horizontal_line = np.array(
  46.         np.column_stack((x[0:array_length, 0], y[0:array_length, 0], z[0:array_length, 0])))
  47.     vertical_line = np.array(np.column_stack((x[0:array_length, array_length - 1], y[0:array_length, array_length - 1],
  48.                                               z[0:array_length, array_length - 1])))
  49.     center_line = np.array(np.column_stack((x[0:array_length, 50], y[0:array_length, 50], z[0:array_length, 50])))
  50.  
  51.     hor_calc_coverage_angle = coverage_calc(horizontal_line[49, 0], horizontal_line[49, 2],
  52.                                             horizontal_line[51, 0], horizontal_line[51, 2])
  53.  
  54.     ver_calc_coverage_angle = coverage_calc(vertical_line[49, 1], vertical_line[49, 2],
  55.                                             vertical_line[51, 1], vertical_line[51, 2])
  56.     elliptical_mesh = pv.StructuredGrid(x, y, z)
  57.  
  58.     freq_range, impedance_range = calc_impedance(x, y, z, array_length)
  59.  
  60.     return throat, ellipse, center_line, vertical_line, horizontal_line, hor_calc_coverage_angle, \
  61.            ver_calc_coverage_angle, elliptical_mesh, freq_range, impedance_range
  62.  
  63.  
  64. def rectangular_calc(waveguide_throat, rectangle_x, rectangle_y, depth_factor, angle_factor):
  65.     array_length = 100
  66.     rectangle_x = rectangle_x / 2
  67.     rectangle_y = rectangle_y / 2
  68.     # waveguide throat line
  69.     phi = np.linspace(0, np.pi / 2, array_length)
  70.     x_interior = waveguide_throat / 2 * np.cos(phi)
  71.     y_interior = waveguide_throat / 2 * np.sin(phi)
  72.  
  73.     # theta is angle where x and y intersect
  74.     theta = np.arctan2(rectangle_y, rectangle_x)
  75.     # find array index which maps to corner of rectangle
  76.     corner_index = (array_length * (2 * theta / np.pi)).round().astype(int)
  77.     # construct rectangular coordinate manually
  78.     x_exterior = np.zeros_like(x_interior)
  79.     y_exterior = x_exterior.copy()
  80.     phi_aux = np.linspace(0, theta, corner_index)
  81.     x_exterior[:corner_index] = rectangle_x
  82.     y_exterior[:corner_index] = rectangle_x * np.tan(phi_aux)
  83.     phi_aux = np.linspace(np.pi / 2, theta, array_length - corner_index, endpoint=False)[::-1]
  84.     x_exterior[corner_index:] = rectangle_y / np.tan(phi_aux)
  85.     y_exterior[corner_index:] = rectangle_y
  86.  
  87.     # interpolate between two curves
  88.     r = np.linspace(0, 1, array_length)[:, None]  # shape (array_length, 1) for broadcasting
  89.     x = x_exterior * r + x_interior * (1 - r)
  90.     y = y_exterior * r + y_interior * (1 - r)
  91.  
  92.     # compute z profile
  93.     angle_factor = angle_factor / 10000
  94.     z = (rectangle_x / 2 * r / angle_factor) ** (1 / depth_factor)
  95.     # explicitly broadcast to the shape of x and y
  96.     z = np.broadcast_to(z, x.shape)
  97.  
  98.     # prepare data for export
  99.     throat = np.array(np.column_stack((x[0, 0:array_length], y[0, 0:array_length],
  100.                                        z[0, 0:array_length])))
  101.     x_rectangle = np.array(np.column_stack((x[array_length - 1, 0:corner_index],
  102.                                             y[array_length - 1, 0:corner_index],
  103.                                             z[array_length - 1, 0:corner_index])))
  104.  
  105.     y_rectangle = np.array(np.column_stack((x[array_length - 1, corner_index - 1:array_length],
  106.                                             y[array_length - 1, corner_index - 1:array_length],
  107.                                             z[array_length - 1, corner_index - 1:array_length])))
  108.  
  109.     horizontal_line = np.array(
  110.         np.column_stack((x[0:array_length, 0], y[0:array_length, 0], z[0:array_length, 0])))
  111.  
  112.     vertical_line = np.array(np.column_stack((x[0:array_length, array_length - 1], y[0:array_length, array_length - 1],
  113.                                               z[0:array_length, array_length - 1])))
  114.  
  115.     center_line = np.array(np.column_stack((x[0:array_length, corner_index - 1], y[0:array_length, corner_index - 1],
  116.                                             z[0:array_length, corner_index - 1])))
  117.  
  118.     y_mid_center_line = np.array(np.column_stack((x[0: array_length, int(corner_index / 2)],
  119.                                                   y[0: array_length, int(corner_index / 2)],
  120.                                                   z[0: array_length, int(corner_index / 2)])))
  121.  
  122.     x_mid_center_line = np.array(np.column_stack((
  123.         x[0: array_length, int(array_length - corner_index)],
  124.         y[0: array_length, int(array_length - corner_index)],
  125.         z[0: array_length, int(array_length - corner_index)])))
  126.  
  127.     hor_calc_coverage_angle = coverage_calc(horizontal_line[49, 0], horizontal_line[49, 2],
  128.                                             horizontal_line[51, 0], horizontal_line[51, 2])
  129.  
  130.     ver_calc_coverage_angle = coverage_calc(vertical_line[49, 1], vertical_line[49, 2],
  131.                                             vertical_line[51, 1], vertical_line[51, 2])
  132.     # Create mesh for 3D viewing
  133.     rectangular_mesh = pv.StructuredGrid(x, y, z)
  134.  
  135.     freq_range, impedance_range = calc_impedance(x, y, z, array_length)
  136.  
  137.     return throat, x_rectangle, y_rectangle, center_line, vertical_line, horizontal_line, hor_calc_coverage_angle, \
  138.            ver_calc_coverage_angle, rectangular_mesh, x_mid_center_line, y_mid_center_line, freq_range, impedance_range
  139.  
  140.  
  141. def cutoff_frequency(coverage_angle, throat_diameter):
  142.     coverage_angle = 0.5 * (coverage_angle * (np.pi / 180))
  143.  
  144.     throat_radius = (throat_diameter / 2)
  145.  
  146.     cutoff_freq = (44 * (np.sin(coverage_angle) / (throat_radius / 1000)))
  147.  
  148.     return cutoff_freq
  149.  
  150.  
  151. def phase_plug_calc(plug_dia, dome_dia, plug_offset, array_length):
  152.     s, chi = np.mgrid[0:1:array_length * 1j, 0:np.pi / 2:array_length * 1j]
  153.  
  154.     # Create phase plug dome dependant on 2 different situations
  155.     if plug_dia == dome_dia:
  156.  
  157.         x_phaseplug = ((dome_dia / 2 * (1 - s)) * np.cos(chi))
  158.         y_phaseplug = ((dome_dia / 2 * (1 - s)) * np.sin(chi))
  159.         z_phaseplug = np.sqrt(abs((dome_dia / 2) ** 2 - (x_phaseplug ** 2) - (y_phaseplug ** 2))) + plug_offset
  160.  
  161.     elif plug_dia < dome_dia > 0:
  162.  
  163.         alpha_angle = 2 * np.arcsin(plug_dia / dome_dia)
  164.         plug_modification = (plug_dia / 2) / (np.tan(alpha_angle / 2))
  165.  
  166.         x_phaseplug = ((plug_dia / 2 * (1 - s)) * np.cos(chi))
  167.         y_phaseplug = ((plug_dia / 2 * (1 - s)) * np.sin(chi))
  168.         z_phaseplug = np.sqrt(
  169.             abs((dome_dia / 2) ** 2 - (x_phaseplug ** 2) - (y_phaseplug ** 2))) + plug_offset - plug_modification
  170.  
  171.     phaseplug = pv.StructuredGrid(x_phaseplug, y_phaseplug, z_phaseplug)
  172.     phaseplug_line = np.array(np.column_stack((x_phaseplug[0:array_length, 0], y_phaseplug[0:array_length, 0],
  173.                                                z_phaseplug[0:array_length, 0])))
  174.  
  175.     return phaseplug, phaseplug_line
  176.  
  177.  
  178. def no_file_selected():
  179.     msg = QMessageBox()
  180.     msg.setIcon(QMessageBox.Warning)
  181.     msg.setWindowIcon(QtGui.QIcon("D:/Python Projects/Waveguide Designer/Icon/Waveguide_Designer.ico"))
  182.     msg.setText("No File Was Selected!")
  183.     msg.setWindowTitle("Warning")
  184.     retval = msg.exec_()
  185.  
  186.  
  187. def calc_impedance(x, y, z, array_length):
  188.     cross_section_area = []
  189.     average_impedance = []
  190.     impedance = []
  191.  
  192.     frequency_range = np.linspace(20, 20000, 4000)
  193.     frequency_time = 1 / frequency_range
  194.     adiabatic_bulk_mod_air = 141178.8
  195.     omega_range = frequency_range * 2 * np.pi
  196.     wave_number = (2 * np.pi) / (343 / frequency_range)
  197.  
  198.     for i in range(0, array_length):
  199.         cross_section = (np.column_stack((x[i, 0:array_length], y[i, 0:array_length], z[i, 0:array_length])))
  200.         cross_section = np.append(cross_section, [0, 0, z[i, 0]])
  201.  
  202.         cross_section_points = pv.PolyData(cross_section)
  203.  
  204.         cross_section_2d = cross_section_points.delaunay_2d()
  205.  
  206.         cross_section_area.append((cross_section_2d.area * 4) * .000001)
  207.  
  208.     position_x = (z[0:array_length, 0]) / 1000
  209.  
  210.     for i in range(0, len(frequency_range)):
  211.  
  212.         for j in range(0, len(position_x)):
  213.             y_m = np.sin((wave_number[i] * position_x[j]) - (omega_range[i] * frequency_time[i]))
  214.  
  215.             particle_velocity = -1 * y_m * omega_range[i] * np.cos \
  216.                 ((wave_number[i] * position_x[j]) - (omega_range[i] * frequency_time[i]))
  217.  
  218.             acoustic_pressure = -1 * adiabatic_bulk_mod_air * y_m * wave_number[i] * \
  219.                                 np.cos((wave_number[i] * position_x[j]) - (omega_range[i] * frequency_time[i]))
  220.  
  221.             impedance.append(acoustic_pressure / (particle_velocity * cross_section_area[j]))
  222.  
  223.         average_impedance.append((sum(impedance) / len(impedance)))
  224.         impedance.clear()
  225.  
  226.     return frequency_range, average_impedance
  227.  
  228.  
  229. class MyMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
  230.     def __init__(self, parent=None):
  231.         super(MyMainWindow, self).__init__(parent)
  232.         self.setupUi(self)
  233.         # Define plotter and then attach to gridlayout to allow resizing with window, add axes to identify xyz
  234.         self.plotter = QtInteractor(self.frame)
  235.         self.plotter.set_background(color='white')
  236.         self.plotter.show_axes()
  237.         self.gridLayout_5.addWidget(self.plotter.interactor)
  238.  
  239.         self.impedance_plotter = QtInteractor(self.frame_impedance)
  240.         self.impedance_plotter.set_background(color='white')
  241.         self.gridLayout_8.addWidget(self.impedance_plotter.interactor)
  242.  
  243.         # Set checkbox to not checked
  244.         self.groupBox_phaseplug.setEnabled(False)
  245.         # Define buttons and checkbox state check
  246.         self.pushButton_generate_waveguide.clicked.connect(self.generate_waveguide)
  247.         self.pushButton_save_button.clicked.connect(self.on_click2)
  248.         self.checkBox_phaseplug.stateChanged.connect(self.check_state)
  249.  
  250.         self.ver_checkbox.stateChanged.connect(self.check_cross_checkbox)
  251.         self.hor_checkbox.stateChanged.connect(self.check_cross_checkbox)
  252.         self.setWindowIcon(QtGui.QIcon('Waveguide_Designer.ico'))
  253.  
  254.         self.actionSave_Waveguide_Parameters.triggered.connect(self.parameters_save)
  255.         self.actionLoad_Waveguide_Parameters.triggered.connect(self.parameters_load)
  256.         self.actionSave_Comsol_Parameters.triggered.connect(self.comsol_parameters)
  257.  
  258.         self.lineEdit_throat_diameter.setValidator(
  259.             QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  260.         self.lineEdit_width.setValidator(QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  261.         self.lineEdit_height.setValidator(QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  262.         self.lineEdit_depth_factor.setValidator(
  263.             QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  264.         self.lineEdit_plugoffset.setValidator(QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  265.         self.lineEdit_dome_diameter.setValidator(
  266.             QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  267.         self.lineEdit_plug_diameter.setValidator(
  268.             QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  269.         self.lineEdit_angle_factor.setValidator(
  270.             QtGui.QDoubleValidator(notation=QtGui.QDoubleValidator.StandardNotation))
  271.  
  272.         self.show()
  273.  
  274.     @pyqtSlot()
  275.     def closeEvent(self, QCloseEvent):
  276.         super().closeEvent(QCloseEvent)
  277.         self.plotter.close()
  278.         self.impedance_plotter.close()
  279.  
  280.     def generate_waveguide(self):
  281.         # Clear plotter each time to plot new waveguides
  282.         self.plotter.clear()
  283.         self.impedance_plotter.clear()
  284.         # Get Parameters from LineEdits in Waveguide Groupbox
  285.         throat_diameter = float(self.lineEdit_throat_diameter.text())  # (1)
  286.         angle_factor = float(self.lineEdit_angle_factor.text())  # (3)
  287.         width = float(self.lineEdit_width.text())  # (4)
  288.         height = float(self.lineEdit_height.text())  # (5)
  289.         depth_factor = float(self.lineEdit_depth_factor.text())  # (6)
  290.  
  291.         if self.radioButton_elliptical.isChecked():
  292.             self.circle_array, self.ellipse_array, self.center_array, self.ver_array, self.hor_array, \
  293.             self.hor_coverage_angle, self.ver_coverage_angle, waveguide_mesh, \
  294.             frequency_range, impedance_range \
  295.                 = elliptical_calc(throat_diameter, width, height, depth_factor, angle_factor)
  296.  
  297.         elif self.radioButton_rectangular.isChecked():
  298.             self.circle_array, self.x_rectangle, self.y_rectangle, self.center_array, self.ver_array, self.hor_array, \
  299.             self.hor_coverage_angle, self.ver_coverage_angle, waveguide_mesh, self.x_midline, self.y_midline, \
  300.             frequency_range, \
  301.             impedance_range = rectangular_calc(throat_diameter, width, height, depth_factor, angle_factor)
  302.  
  303.         # Assign plot to impedance frame
  304.  
  305.         chart = pv.Chart2D()
  306.         _ = chart.plot(frequency_range, impedance_range)
  307.         chart.title = "Impedance Curve"
  308.  
  309.         self.impedance_plotter.add_chart(chart)
  310.  
  311.         # max height of waveguide, passed to label
  312.         z_max = round(np.max(self.ver_array[:, 2]), 2)
  313.  
  314.         cutoff_freq = cutoff_frequency(self.hor_coverage_angle, throat_diameter)
  315.  
  316.         hor_coverage_angle = str(int(self.hor_coverage_angle))
  317.         ver_coverage_angle = str(int(self.ver_coverage_angle))
  318.         cutoff_freq = str(int(cutoff_freq))
  319.         self.lineEdit_coverage_angle.setText((hor_coverage_angle + ' deg'))
  320.         self.lineEdit_ver_coverage_angle.setText((ver_coverage_angle + ' deg'))
  321.         self.lineEdit_cutoff_freq.setText((cutoff_freq + ' Hz'))
  322.  
  323.         # Reflect mesh twice and merge twice to create a entire surface
  324.         waveguide_mesh_reflected = waveguide_mesh.reflect((0, 1, 0))
  325.         merged = waveguide_mesh.merge(waveguide_mesh_reflected)
  326.         merged_mirror = merged.reflect((1, 0, 0))
  327.         waveguide_mesh = merged.merge(merged_mirror)
  328.         # Select scalars to plot "cmap" colors without needing matplotlib
  329.         waveguide_mesh['Data'] = waveguide_mesh.points[:, 2]
  330.         # Define mesh to be used with cross-section checkboxes and define waveguide slice variables
  331.         self.waveguide_whole = waveguide_mesh
  332.         self.waveguide_slice_x = waveguide_mesh.slice(normal='x', origin=(0.1, 0, 0))
  333.         self.waveguide_slice_y = waveguide_mesh.slice(normal='y')
  334.  
  335.         if not (self.checkBox_phaseplug.isChecked()):
  336.             # If phaseplug checkbox is not checked, pass an empty array to avoid any issues
  337.             self.phaseplug_array = np.array([])
  338.  
  339.             self.plotter.add_mesh(waveguide_mesh, show_scalar_bar=False)
  340.             self.plotter.add_text(f'Waveguide height is: ({z_max}) mm', color='black', font_size=8)
  341.  
  342.         else:
  343.             # If checkbox is checked, gather data from PhasePlug Groupbox
  344.             phase_plug_dia = float(self.lineEdit_plug_diameter.text())
  345.             dome_dia = float(self.lineEdit_dome_diameter.text())
  346.             phase_plug_offset = float(self.lineEdit_plugoffset.text())
  347.             # Calculate phaseplug mesh and array
  348.             phaseplug_mesh, self.phaseplug_array = phase_plug_calc(phase_plug_dia, dome_dia, phase_plug_offset,
  349.                                                                    array_length=100)
  350.             # mirror meshes and form phasing plug
  351.             phaseplug_mesh_reflected = phaseplug_mesh.reflect((0, 1, 0))
  352.             merged_mesh = phaseplug_mesh.merge(phaseplug_mesh_reflected)
  353.             merged_mirror = merged_mesh.reflect((1, 0, 0))
  354.             phaseplug_mesh = merged_mesh.merge(merged_mirror)
  355.  
  356.             phaseplug_mesh['Data'] = phaseplug_mesh.points[:, 2]
  357.             # Define global variable to hold phaseplug mesh and define xy phaseplug slices
  358.             self.phaseplug_whole = phaseplug_mesh
  359.             self.phaseplug_slice_x = phaseplug_mesh.slice(normal='x', origin=(0.1, 0, 0))
  360.             self.phaseplug_slice_y = phaseplug_mesh.slice(normal='y')
  361.  
  362.             self.plotter.add_mesh(waveguide_mesh, show_scalar_bar=False)
  363.             self.plotter.add_mesh(phaseplug_mesh, show_scalar_bar=False)
  364.             self.plotter.add_text(f'Waveguide height is: ({z_max}) mm', color='black', font_size=8)
  365.  
  366.     def on_click2(self):
  367.         # Button to save text to folder. Folder is specified by user.
  368.  
  369.         save_text = str(QtWidgets.QFileDialog.getExistingDirectory(self, "Select Directory"))
  370.  
  371.         if save_text == '':
  372.             no_file_selected()
  373.         else:
  374.  
  375.             np.savetxt(save_text + "/Throat.txt", self.circle_array, delimiter=" ")
  376.             np.savetxt(save_text + "/Horizontal.txt", self.hor_array, delimiter=" ")
  377.             np.savetxt(save_text + "/Vertical.txt", self.ver_array, delimiter=" ")
  378.             np.savetxt(save_text + "/Center.txt", self.center_array, delimiter=" ")
  379.  
  380.             if self.radioButton_elliptical.isChecked():
  381.                 np.savetxt(save_text + "/Ellipse.txt", self.ellipse_array, delimiter=" ")
  382.                 if self.checkBox_phaseplug.isChecked():
  383.                     np.savetxt(save_text + "/Phaseplug.txt", self.phaseplug_array, delimiter=" ")
  384.  
  385.             elif self.radioButton_rectangular.isChecked():
  386.                 np.savetxt(save_text + "/X_Rectangle.txt", self.x_rectangle, delimiter=" ")
  387.                 np.savetxt(save_text + "/Y_Rectangle.txt", self.y_rectangle, delimiter=" ")
  388.                 np.savetxt(save_text + "/X_mid_Rectangle.txt", self.x_midline, delimiter=" ")
  389.                 np.savetxt(save_text + "/Y_mid_Rectangle.txt", self.y_midline, delimiter=" ")
  390.  
  391.                 if self.checkBox_phaseplug.isChecked():
  392.                     np.savetxt(save_text + "/Phaseplug.txt", self.phaseplug_array, delimiter=" ")
  393.  
  394.     def check_state(self, state):
  395.         # This function will check the state of the phaseplug_checkbox and if it is checked it will enable it and accept
  396.         # input, otherwise it greys out the inputs and clears the content
  397.         if state == 0:
  398.             self.groupBox_phaseplug.setEnabled(False)
  399.             self.lineEdit_dome_diameter.clear()
  400.             self.lineEdit_plug_diameter.clear()
  401.             self.lineEdit_plugoffset.clear()
  402.         else:
  403.             self.groupBox_phaseplug.setEnabled(True)
  404.  
  405.     def check_cross_checkbox(self, state):
  406.         # Vertical and Horizontal checkboxes are checked via if/elif loop. If checked, if loop is run, else runs not
  407.         # checked loop. If both checkboxes are unchecked, replot the waveguide.
  408.         if self.checkBox_phaseplug.isChecked():
  409.  
  410.             if state == QtCore.Qt.Checked:
  411.  
  412.                 if self.sender() == self.ver_checkbox:
  413.                     self.hor_checkbox.setChecked(False)
  414.                     self.plotter.clear()
  415.                     self.plotter.add_mesh(self.waveguide_slice_x, show_scalar_bar=False, line_width=2)
  416.                     self.plotter.add_mesh(self.phaseplug_slice_x, show_scalar_bar=False, line_width=2)
  417.                     self.plotter.view_yz()
  418.  
  419.                 elif self.sender() == self.hor_checkbox:
  420.                     self.ver_checkbox.setChecked(False)
  421.                     self.plotter.clear()
  422.                     self.plotter.add_mesh(self.waveguide_slice_y, show_scalar_bar=False, line_width=2)
  423.                     self.plotter.add_mesh(self.phaseplug_slice_y, show_scalar_bar=False, line_width=2)
  424.                     self.plotter.view_xz()
  425.  
  426.             elif state != QtCore.Qt.Checked:
  427.                 self.pushButton_generate_waveguide.click()
  428.  
  429.         else:
  430.             if state == QtCore.Qt.Checked:
  431.  
  432.                 if self.sender() == self.ver_checkbox:
  433.                     self.hor_checkbox.setChecked(False)
  434.                     self.plotter.clear()
  435.                     self.plotter.add_mesh(self.waveguide_slice_x, show_scalar_bar=False, line_width=2)
  436.                     self.plotter.view_yz()
  437.  
  438.                 elif self.sender() == self.hor_checkbox:
  439.                     self.ver_checkbox.setChecked(False)
  440.                     self.plotter.clear()
  441.                     self.plotter.add_mesh(self.waveguide_slice_y, show_scalar_bar=False, line_width=2)
  442.                     self.plotter.view_xz()
  443.             elif state != QtCore.Qt.Checked:
  444.                 self.pushButton_generate_waveguide.click()
  445.  
  446.     def parameters_save(self):
  447.         file_name = QtWidgets.QFileDialog.getSaveFileName(self, "Save File", '/', "WGP (*.wgp)")[0]
  448.  
  449.         if file_name == '':
  450.             no_file_selected()
  451.  
  452.         else:
  453.  
  454.             parameter_list = [str(self.lineEdit_throat_diameter.text()) + "\n",
  455.                               str(self.lineEdit_width.text()) + "\n",
  456.                               str(self.lineEdit_height.text()) + "\n",
  457.                               str(self.lineEdit_angle_factor.text()) + "\n",
  458.                               str(self.lineEdit_depth_factor.text()) + "\n",
  459.                               str(self.checkBox_phaseplug.isChecked()) + "\n",
  460.                               str(self.radioButton_elliptical.isChecked()) + "\n",
  461.                               str(self.radioButton_rectangular.isChecked()) + "\n"
  462.                               ]
  463.  
  464.             if self.checkBox_phaseplug.isChecked():
  465.                 parameter_phaseplug = [
  466.                     str(self.lineEdit_plug_diameter.text()) + "\n",
  467.                     str(self.lineEdit_dome_diameter.text()) + "\n",
  468.                     str(self.lineEdit_plugoffset.text())
  469.                 ]
  470.                 parameter_list.extend(parameter_phaseplug)
  471.  
  472.             file_parameters = open(file_name, "w")
  473.             file_parameters.writelines(parameter_list)
  474.  
  475.     def parameters_load(self):
  476.         parameter_file = QtWidgets.QFileDialog.getOpenFileName(self, "Select Waveguide Parameter File",
  477.                                                                "", 'WGP (*.wgp)')[0]
  478.         if parameter_file == '':
  479.             no_file_selected()
  480.  
  481.         else:
  482.  
  483.             parameters = open(parameter_file, "r")
  484.  
  485.             content = parameters.read().splitlines()
  486.  
  487.             self.lineEdit_throat_diameter.setText(content[0])
  488.             self.lineEdit_width.setText(content[1])
  489.             self.lineEdit_height.setText(content[2])
  490.             self.lineEdit_angle_factor.setText(content[3])
  491.             self.lineEdit_depth_factor.setText(content[4])
  492.             self.checkBox_phaseplug.setChecked(bool(distutils.util.strtobool(content[5])))
  493.             self.radioButton_elliptical.setChecked(bool(distutils.util.strtobool(content[6])))
  494.             self.radioButton_rectangular.setChecked(bool(distutils.util.strtobool(content[7])))
  495.  
  496.             if bool(distutils.util.strtobool(content[5])):
  497.                 self.lineEdit_plug_diameter.setText(content[8])
  498.                 self.lineEdit_dome_diameter.setText(content[9])
  499.                 self.lineEdit_plugoffset.setText(content[10])
  500.  
  501.             self.pushButton_generate_waveguide.click()
  502.  
  503.     def comsol_parameters(self):
  504.         file_name = QtWidgets.QFileDialog.getSaveFileName(self, "Save Comsol Parameters", '/', "txt (*.txt)")[0]
  505.  
  506.         if file_name == '':
  507.             no_file_selected()
  508.  
  509.         else:
  510.             comsol_params = [
  511.                 "x_ellipse " + str(self.lineEdit_width.text()) + "[mm] Ellipse X " + "\n",
  512.                 "y_ellipse " + str(self.lineEdit_height.text()) + "[mm] Ellipse Y " + "\n",
  513.                 "throat " + str(self.lineEdit_throat_diameter.text()) + "[mm] Waveguide Throat " + "\n",
  514.                 "depth_factor " + str(self.lineEdit_depth_factor.text()) + " depth factor " + "\n",
  515.                 "angle_factor " + str(self.lineEdit_angle_factor.text()) + " angle factor " + "\n"
  516.             ]
  517.             if self.checkBox_phaseplug.isChecked():
  518.                 comsol_phaseplug = [
  519.                     "plug_dia " + str(self.lineEdit_plug_diameter.text()) + "[mm] phase plug diameter " + "\n",
  520.                     "dome_dia " + str(self.lineEdit_dome_diameter.text()) + "[mm] tweeter dome diameter " + "\n",
  521.                     "plug_offset " + str(self.lineEdit_plugoffset.text()) + "[mm] phase plug offset " + "\n"
  522.                 ]
  523.                 comsol_params.extend(comsol_phaseplug)
  524.             else:
  525.                 comsol_phaseplug = [
  526.                     "plug_dia 0 [mm] phase plug diameter " + "\n",
  527.                     "dome_dia 0 [mm] tweeter dome diameter " + "\n",
  528.                     "plug_offset 0 [mm] phase plug offset " + "\n"
  529.                 ]
  530.                 comsol_params.extend(comsol_phaseplug)
  531.  
  532.             file_parameters = open(file_name, "w")
  533.             file_parameters.writelines(comsol_params)
  534.  
  535.  
  536. if __name__ == "__main__":
  537.     # MAIN APP
  538.     app = QtWidgets.QApplication(sys.argv)
  539.     win = MyMainWindow()
  540.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement