modified gui
[pde-python.git] / gui.py
blob585b8209b3a0afbea9f658e3b76506c8a38a7da9
1 #-*- coding: utf-8
3 # Политика префиксов в именах
4 # QLineEdit - edit_
5 # QLabel - label_
6 # QPushButton - button_
9 import sys
11 from PyQt4.QtCore import *
12 from PyQt4.QtGui import *
14 import matplotlib
15 from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
16 from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
17 from matplotlib.figure import Figure
19 import numpy as np
21 from grid import ExplicitSolver, ImplicitSolver
22 from waves import Exm
23 import const as c
25 class Dummy(object):
26 pass
28 class AppForm(QMainWindow):
30 def __init__(self, parent=None):
31 QMainWindow.__init__(self, parent)
32 self.setWindowTitle(u'Курсовой проект')
34 self.create_main_frame()
35 self.setWindowState(Qt.WindowMaximized)
37 self.on_grid_change()
39 self.data = Dummy()
40 # self.test_graph()
42 def create_main_frame(self):
43 self.main_frame = QWidget()
44 self.init_work_frame()
45 self.init_param_frame()
47 self.splitter = QSplitter()
48 self.splitter.addWidget(self.param_frame)
49 right_frame = QWidget()
50 right_layout = QVBoxLayout(right_frame)
51 right_layout.addWidget(self.work_frame)
53 scheme_frame = QWidget()
54 scheme_layout = QGridLayout(scheme_frame)
55 self.a_group = self._make_analytic()
56 self.exp_group = self._make_explicit()
57 self.imp_group = self._make_implicit()
58 scheme_layout.addWidget(self.a_group, 0, 0, 1, 1)
59 scheme_layout.addWidget(self.exp_group, 0, 1, 1, 1)
60 scheme_layout.addWidget(self.imp_group, 0, 2, 1, 1)
62 self.button_scheme_run = QPushButton(u'Пуск')
63 self.connect(self.button_scheme_run, SIGNAL("clicked()"), self.on_scheme_run)
64 self.button_scheme_run.setFocus(Qt.ActiveWindowFocusReason)
66 right_layout.addWidget(scheme_frame)
67 right_layout.addWidget(self.button_scheme_run)
69 self.splitter.addWidget(right_frame)
70 self.main_layout = QGridLayout(self.main_frame)
71 # self.main_layout.addWidget(self.param_frame, 0, 0)
72 # self.main_layout.addWidget(self.work_frame, 0, 1)
73 self.main_layout.addWidget(self.splitter)
76 self.setCentralWidget(self.main_frame)
78 def init_param_frame(self):
79 self.param_frame = QWidget()
80 param_group = QGroupBox(u"&Параметры")
81 param_layout = QGridLayout(self.param_frame)
82 param_layout.addWidget(param_group, 0, 0)
84 self.edit_LY = QLineEdit(str(c.LY))
85 self.edit_LY.setMinimumWidth(40)
86 self.edit_LZ = QLineEdit(str(c.LZ))
87 self.edit_LZ.setMinimumWidth(40)
88 self.edit_C = QLineEdit(str(c.C))
89 self.edit_C.setMinimumWidth(40)
90 self.edit_LAMBDA = QLineEdit(str(c.LAMBDA))
91 self.edit_LAMBDA.setMinimumWidth(40)
92 self.edit_T = QLineEdit('1e-14')
93 self.edit_dim_y = QLineEdit(str(50))
94 self.edit_dim_z = QLineEdit(str(50))
96 pg_layout = QFormLayout(param_group)
97 pg_layout.addRow(u'Ширина ly', self.edit_LY)
98 pg_layout.addRow(u'Высота lz', self.edit_LZ)
99 pg_layout.addRow(u'Скорость C', self.edit_C)
100 pg_layout.addRow(u'Волна λ', self.edit_LAMBDA)
101 pg_layout.addRow(u'Момент T', self.edit_T)
102 pg_layout.addRow(u'Размер по y (I)', self.edit_dim_y)
103 pg_layout.addRow(u'Размер по z (J)', self.edit_dim_z)
104 self.param_frame.setMaximumWidth(250)
106 self.connect(self.edit_LY, SIGNAL("returnPressed()"), self.on_grid_change)
107 self.connect(self.edit_LZ, SIGNAL("returnPressed()"), self.on_grid_change)
108 self.connect(self.edit_dim_y, SIGNAL("returnPressed()"), self.on_grid_change)
109 self.connect(self.edit_dim_z, SIGNAL("returnPressed()"), self.on_grid_change)
111 def _make_analytic(self):
112 a_group = QGroupBox(u"Аналитическое решение")
113 a_group.setCheckable(True)
114 a_layout = QFormLayout(a_group)
116 self.edit_length = QLineEdit(str(500))
117 a_layout.addRow(u"Элементов ряда", self.edit_length)
118 self.edit_length.setMaximumWidth(100)
120 return a_group
122 def _make_explicit(self):
123 e_group = QGroupBox(u"Явная схема")
124 e_group.setCheckable(True)
125 e_group.setChecked(False)
126 e_layout = QFormLayout(e_group)
127 self.edit_exp_step_t = QLineEdit()
128 e_layout.addRow(u"Шаг по t", self.edit_exp_step_t)
130 return e_group
132 def _make_implicit(self):
133 i_group = QGroupBox(u"Неявная схема")
134 i_group.setCheckable(True)
135 i_group.setChecked(False)
136 i_layout = QFormLayout(i_group)
137 self.edit_imp_step_t = QLineEdit()
138 i_layout.addRow(u"Шаг по t", self.edit_imp_step_t)
140 return i_group
142 def init_work_frame(self):
143 self.work_frame = QTabWidget()
145 self.dpi = 100
146 self.z_fig = Figure((5.0, 4.0), dpi = self.dpi)
147 self.z_canvas = FigureCanvas(self.z_fig)
148 self.z_canvas.setParent(self.main_frame)
149 self.z_axes = self.z_fig.add_subplot(111)
151 self.graph_frame = QWidget()
152 graph_layout = QGridLayout(self.graph_frame)
153 graph_layout.addWidget(self.z_canvas, 0, 0, 1, 1)
155 self.mpl_toolbar = NavigationToolbar(self.z_canvas, self.graph_frame)
156 # self.mpl_toolbar.setOrientation(Qt.Vertical)
157 graph_layout.addWidget(self.mpl_toolbar, 1, 0, 1, 1)
161 self.devia_frame = QWidget()
162 self.dpi = 100
163 self.d_fig = Figure((5.0, 4.0), dpi = self.dpi)
164 self.d_canvas = FigureCanvas(self.d_fig)
165 self.d_canvas.setParent(self.main_frame)
166 self.d_axes = self.d_fig.add_subplot(111)
167 devia_layout = QGridLayout(self.devia_frame)
168 devia_layout.addWidget(self.d_canvas, 0, 0, 1, 1)
169 devia_toolbar = NavigationToolbar(self.d_canvas, self.devia_frame)
170 devia_layout.addWidget(devia_toolbar, 1, 0, 1, 1)
172 self.about_frame = QWidget()
174 self.work_frame.addTab(self.graph_frame, u"&Разностная схема")
175 self.work_frame.addTab(self.devia_frame, u"&Отклонение")
176 # self.work_frame.addTab(self.about_frame, u"&О программе")
178 def on_grid_change(self):
179 ly_str = unicode(self.edit_LY.text())
180 lz_str = unicode(self.edit_LZ.text())
181 ny_str = unicode(self.edit_dim_y.text())
182 nz_str = unicode(self.edit_dim_z.text())
184 ly = np.float64(ly_str)
185 lz = np.float64(lz_str)
186 ny = int(ny_str)
187 nz = int(nz_str)
188 hy = (ly / ny)
189 print hy
190 hz = (lz / nz)
191 print hz
192 h_exp = min(hy, hz) / (np.sqrt(2) * c.C)
193 h_imp = hy / c.C
194 exp_l = ("%g" % h_exp).split('e')
195 imp_l = ("%g" % h_imp).split('e')
196 print exp_l
197 print imp_l
198 ht_exp = '1e' + exp_l[1]
199 ht_imp = str(np.trunc(np.float64(imp_l[0]))) + 'e' + imp_l[1]
200 self.edit_exp_step_t.setText(ht_exp)
201 self.edit_imp_step_t.setText(ht_imp)
204 def clear_axes(self, axes):
205 axes.clear()
206 axes.grid(True)
207 axes.set_xlabel('z')
208 axes.set_ylabel('Ex')
210 def test_graph(self):
211 self.clear_axes(self.z_axes)
212 self.z_canvas.draw()
214 def _parse_a(self):
215 return int(unicode(self.edit_length.text()))
217 def _parse_grid(self):
218 I = int(unicode(self.edit_dim_y.text()))
219 J = int(unicode(self.edit_dim_z.text()))
220 return I, J
222 def _parse_exp(self):
223 s = unicode(self.edit_exp_step_t.text()).strip()
224 if s == '':
225 return None
226 return np.float64(s)
228 def _parse_imp(self):
229 s = unicode(self.edit_imp_step_t.text()).strip()
230 if s == '':
231 return None
232 return np.float64(s)
234 def _parse_args(self):
235 ly, lz, c, lambda_, t = [np.float64(unicode(p.text()).strip()) \
236 for p in [self.edit_LY, self.edit_LZ, self.edit_C, self.edit_LAMBDA, self.edit_T]]
237 return ly, lz, c, lambda_, t
239 def on_scheme_run(self):
240 is_analytic, is_explicit, is_implicit = [g.isChecked() \
241 for g in [self.a_group, self.exp_group, self.imp_group]]
243 if not (is_analytic or is_explicit or is_implicit):
244 return
246 I, J = self._parse_grid()
248 if is_analytic:
249 m = self._parse_a()
250 if is_explicit or is_implicit:
251 exp_ht = self._parse_exp()
252 imp_ht = self._parse_imp()
254 ly, lz, c, lambda_, t = self._parse_args()
255 self.data.z = np.linspace(0, lz, J + 1)
257 self.z_axes.clear()
258 lines = []
259 legs = []
260 if is_analytic:
261 self.data.E_a = [Exm(ly / 2, z_, t, m) for z_ in self.data.z]
262 line, = self.z_axes.plot(self.data.z, self.data.E_a, color='red')
263 lines.append(line)
264 legs.append('Analytical')
265 if is_explicit:
266 solver = ExplicitSolver(I, J, t, exp_ht)
267 u = solver.solve()
268 self.data.E_exp = u[u.shape[0] / 2, :]
269 line, = self.z_axes.plot(self.data.z, self.data.E_exp, color='blue')
270 lines.append(line)
271 legs.append('Explicit scheme')
272 if is_implicit:
273 solver = ImplicitSolver(I, J, t, imp_ht)
274 u = solver.solve()
275 self.data.E_imp = u[u.shape[0] / 2, :]
276 line, = self.z_axes.plot(self.data.z, self.data.E_imp, color='green')
277 lines.append(line)
278 lines.append('Implicit scheme')
279 self.z_axes.grid(True)
280 self.z_axes.set_xlim(0, max(self.data.z))
281 self.z_axes.set_xlabel('z')
282 self.z_axes.set_ylabel('Ex')
283 self.z_axes.legend(lines, legs)
284 self.z_canvas.draw()
286 def on_deviation_run(self):
287 flags = (is_analytic, is_explicit, is_implicit) = [g.isChecked() \
288 for g in [self.a_group, self.exp_group, self.imp_group]]
290 if not (is_analytic or is_explicit or is_implicit):
291 return
292 if len(filter(None, flags)) < 2:
293 return