-
Notifications
You must be signed in to change notification settings - Fork 1
/
gif_maker.py
146 lines (118 loc) · 4.65 KB
/
gif_maker.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
import fem1d as myfem
import numpy as np
import matplotlib.pyplot as plt
import imageio.v2 as imageio
import os
def plot(u, N, timestep, method, filename=None):
x_user = np.linspace(0, 1, N)
x_analyt = np.linspace(0, 1, 1000)
# plt.plot(x_analyt, myfem.u_analyt(x_analyt, 1), label='Analytical Solution') ### use for dif step sizes
plt.plot(x_analyt, myfem.u_analyt(x_analyt, timestep/nt), label='Analytical Solution') # use for through time
plt.plot(x_user, u[:, timestep], label=f'User Solution ({method})')
plt.xlabel('x')
plt.ylabel('u(x, t)')
plt.title(f'Comparison of Analytical and Numerical Solutions using {method}')
plt.legend()
info_text = f'Nodes: {N}\nTime Step: {timestep}\nTime Range: [0, 1]'
# info_text = f'Nodes: {N}\nTime Step Size: {timestep}\nTime Range: [0, 1]'
plt.text(0.05, 0.95, info_text, transform=plt.gca().transAxes, fontsize=9, verticalalignment='top')
# Perserving axis
plt.xlim(0, 1)
#plt.ylim(0, 1) # Use to show through time
#plt.ylim(0, 0.5) # Use to show dif time step sizes at last time step
if filename:
plt.savefig(filename)
plt.close()
def create_gif_dif_numNodes():
global nt
nt = 551
num_of_steps = nt
nnElem = 2
xl = 0
xr = 1
N = [i for i in range(2, 20, 1)]
method = 'BE'
# method = 'FE'
# Create 'resources' directory if it doesn't exist
if not os.path.exists('resources'):
os.makedirs('resources')
filenames = []
for numNodes in N:
nt = num_of_steps # Assuming nt should vary with step_size
h, x, iee = myfem.create_mesh(numNodes, xl, xr)
K, M, F = myfem.initialize_system(numNodes, nnElem, nt, h, iee)
u = myfem.solve_system(numNodes, nt, x, M, K, F, iee, method)
filename = os.path.join('resources', f'plot_nodes_{numNodes}.png')
plot(u, numNodes, nt, method, filename)
filenames.append(filename)
# Create a GIF
gif_filename = os.path.join('resources', 'solution_evolution.gif')
with imageio.get_writer(gif_filename, mode='I', fps = 2) as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
os.remove(filename) # Remove file after adding it to the GIF
print(f"GIF created: {gif_filename}")
return
def create_gif_dif_stepSizes():
global nt
num_of_steps = [i for i in range(1, 600, 10)]
nnElem = 2
xl = 0
xr = 1
N = 11 # number of nodes
# method = 'FE'
method = 'BE'
# Create 'resources' directory if it doesn't exist
if not os.path.exists('resources'):
os.makedirs('resources')
filenames = []
for steps in num_of_steps:
nt = steps # Assuming nt should vary with step_size
h, x, iee = myfem.create_mesh(N, xl, xr)
K, M, F = myfem.initialize_system(N, nnElem, nt, h, iee)
u = myfem.solve_system(N, nt, x, M, K, F, iee, method)
filename = os.path.join('resources', f'plot_stepsize_{steps}.png')
plot(u, N, nt, method, filename)
filenames.append(filename)
# Create a GIF
gif_filename = os.path.join('resources', 'solution_evolution.gif')
with imageio.get_writer(gif_filename, mode='I', fps = 10) as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
os.remove(filename) # Remove file after adding it to the GIF
print(f"GIF created: {gif_filename}")
def create_gif_through_time():
global h, nnElem, nt
nnElem = 2 # Nodes per element
t = 1 # initial time
xl = 0
xr = 1
N = 11
nt = 551
# method = 'FE'
method = 'BE'
h, x, iee = myfem.create_mesh(N, xl, xr)
K, M, F = myfem.initialize_system( N, nnElem, nt, h, iee)
u = myfem.solve_system(N, nt, x, M, K, F, iee, method)
timestep_to_plot = nt # using the last time step for plot
if not os.path.exists('resources'):
os.makedirs('resources')
filenames = []
for timestep in range(int((nt+1)/10)):
filename = os.path.join('resources', f'frame_{timestep*10}.png')
plot(u, N, timestep*10, method, filename)
filenames.append(filename)
# Create a GIF
gif_filename = os.path.join('resources', 'solution_evolution.gif')
with imageio.get_writer(gif_filename, mode='I', fps = 60) as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
os.remove(filename)
print(f"GIF created: {gif_filename}")
if __name__ == "__main__":
create_gif_through_time()
# create_gif_dif_stepSizes()
# create_gif_dif_numNodes()