-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOne-dimensional-heat-equation.py
132 lines (121 loc) · 4.74 KB
/
One-dimensional-heat-equation.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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.patches import Rectangle
import PySimpleGUI as sg
import math as m
import matplotlib as mt
import time as ti
from matplotlib import animation
# mt.use('agg')
length = 1
d = 1
time = 4
dx = 0.02
temperature_start = 10
temperature = 0
class Canvas(FigureCanvasTkAgg):
"""
Create a canvas for matplotlib pyplot under tkinter/PySimpleGUI canvas
"""
def __init__(self, figure=None, master=None):
super().__init__(figure=figure, master=master)
self.canvas = self.get_tk_widget()
self.canvas.pack(side='top', fill='both', expand=1)
def cm_to_inch(value):
return value/2.54
def renew(n,p,t,t_next):
for j in range(1, n - 1):
# t_next[j] = p * t[j + 1] + (1 - 2 * p) * t[j] + p * t[j - 1];
t_next[j] = p * t[j + 1] + (1 - 2 * p) * t[j] + p * t[j - 1]
# print(t_next)
for j in range(1, n - 1):
t[j] = t_next[j]
return t, t_next
def plot_figure(length,d,time,dx,temperature_start,temperature):
dt = (dx * dx) / (2 * d)
n = int((length / dx)) + 1
t = [0 for i in range(n)]
t_next = [0 for i in range(n)]
x = [i*dx for i in range(n)]
t[0] = temperature_start
t_next[0] = temperature_start
t_next[n - 1] = temperature
p = (dt * d) / (dx * dx)
for i in range(1, n):
t[i] = temperature
line, = ax.plot(x, t, color='g') # Запомнить ссылку на линию графика
plt.ion()
k = 0.0
while k <= time:
ax.relim(visible_only=True) # Пересчет границ осей
# ax.autoscale_view(True) # Автоматическое масштабирование осей
ax.autoscale()
canvas.draw()
plt.pause(0.001)
t, t_next = renew(n,p,t,t_next)
line.set_data(x, t) # Обновление данных графика
event, values = window.read(timeout=0)
if event in (sg.WIN_CLOSED, 'Exit'):
ax.cla()
plt.ioff()
break
plt.ioff()
# plt.show()
sg.theme('DefaultNoMoreNagging')
layout = [
[sg.Canvas(size=(640, 480), key='Canvas')],
[sg.Text(text="length"),sg.Spin([i/10 for i in range(1,1000)], initial_value=length,enable_events=True, k='-L-'),
sg.Text(text="d"),sg.Spin([i/10 for i in range(1,1000)], initial_value=d,enable_events=True, k='-D-'),
sg.Text(text="time"),sg.Spin([i for i in range(1,70)], initial_value=time,enable_events=True, k='-T-'),
sg.Text(text="dx"),sg.Spin([i/100 for i in range(1,1000)], initial_value=dx,enable_events=True, k='-DX-'),
sg.Text(text="temperature_start"),sg.Spin([i/10 for i in range(0,1000)], initial_value=temperature_start,enable_events=True, k='-TS-'),
sg.Text(text="temperature"),sg.Spin([i/10 for i in range(0,1000)], initial_value=temperature,enable_events=True, k='-TR-')],
[sg.Button('Рассчитать')],
# [sg.Text(text="α"),
# sg.Slider(range=(0, 90), default_value=a, size=(10, 20), expand_x=True, enable_events=True, orientation='h', key='Slider')],
# [sg.Text(text="v0"),
# sg.Slider(range=(0, 100), default_value=v0, size=(10, 20), expand_x=True, enable_events=True, orientation='h', key='v')],
# [sg.Checkbox('Препятствие', default=False,enable_events=True, k='-P-')],
[sg.Push(), sg.Button('Exit'), sg.Push()],
]
window = sg.Window('Температуропроводность', layout, finalize=True, resizable=True)
fig = Figure(figsize=(cm_to_inch(15), cm_to_inch(10)))
ax = fig.add_subplot()
canvas = Canvas(fig, window['Canvas'].Widget)
# fig = Figure(figsize=(cm_to_inch(15), cm_to_inch(10)))
# plt.ion()
# ax = fig.add_subplot()
# n = int((length / dx)) + 1
# x = [i*dx for i in range(n)]
# t = [0 for i in range(n)]
# line, = ax.plot(x, t, color='g') # Запомните ссылку на линию графика
# canvas = Canvas(fig, window['Canvas'].Widget)
plot_figure(length,d,time,dx,temperature_start,temperature)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Рассчитать':
plot_figure(length,d,time,dx,temperature_start,temperature)
elif event == '-L-':
# print(values)
length = values[event]
elif event == '-D-':
# print(values)
d = values[event]
elif event == '-T-':
# print(values)
time = values[event]
elif event == '-DX-':
# print(values)
dx = values[event]
elif event == '-TS-':
# print(values)
temperature_start = values[event]
elif event == '-TR-':
# print(values)
temperature = values[event]
# 8. Close window to exit
window.close()