-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdn_animation.py
197 lines (175 loc) · 7.39 KB
/
dn_animation.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
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import gridspec
import matplotlib.animation as manimation
from kmc_dopant_networks_utils import visualize_traffic, visualize_V_and_traffic
# NB! Requires installation: sudo apt-get install ffmpeg
# This module contains code for 2 animations. GetWriter is a function shared by both.
# initScatterAnimation and animateTransition is used to animate DopantPlacement search in SimulatedAnnealing search.
# trafficAnimation is used to animate the change in potential landscape and electron jump traffic as the input changes.
# It also animates a graph in the side which shows the output current in parallel.
def getWriter(fps, title):
'''
This returns a writer object that is used to make the animation. This object is used to write frames that
will be generated using matplotlib, to form an animation.
Input arguments
---------------
fps; int
Frames per second.
title; string
Title of the animation.
Returns
-------
writer: FFMPegWriter
returns writer object used to make the animation.
'''
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title=title)
writer = FFMpegWriter(fps=fps, metadata=metadata)
return writer
def initScatterAnimation(kmc):
'''
Used in the dopant placement annealing search animation.
Input arguments
---------------
Returns
-------
'''
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(right=kmc.xdim)
ax.set_ylim(top=kmc.ydim)
acceptors, = ax.plot(kmc.xCoords[:kmc.N], kmc.yCoords[:kmc.N], 'o', color='black')
history_acceptors, = ax.plot(kmc.xCoords[:kmc.N], kmc.yCoords[:kmc.N], 'o', color='black')
donors, = ax.plot(kmc.xCoords[kmc.N:], kmc.yCoords[kmc.N:], 'o', color='red')
history_donors, = ax.plot(kmc.xCoords[kmc.N:], kmc.yCoords[kmc.N:], 'o', color='red')
text_element = ax.text(0.5, -0.1, "Error: , time: , strategy: ", horizontalalignment='center')
return acceptors, donors, history_acceptors, history_donors, text_element, fig
def animateTransition(kmc, donors, acceptors, history_donors, history_acceptors, text_element, index, target_pos, writer, splits, refresh_history, alpha, text):
'''
Used in the dopant placement annealing search animation.
Input arguments
---------------
Returns
-------
'''
acceptorDataX = [kmc.xCoords[j] for j in range(kmc.N)]
acceptorDataY = [kmc.yCoords[j] for j in range(kmc.N)]
donorDataX = [kmc.xCoords[j] for j in range(kmc.N, kmc.N+kmc.M)]
donorDataY = [kmc.yCoords[j] for j in range(kmc.N, kmc.N+kmc.M)]
text_element.set_text(text)
for i in range(splits):
if index < kmc.N:
acceptorDataX[index] = (kmc.xCoords[index]*(splits-i)+target_pos[0]*i) / splits
acceptorDataY[index] = (kmc.yCoords[index]*(splits-i)+target_pos[1]*i) / splits
else:
donorDataX[index-kmc.N] = (kmc.xCoords[index]*(splits-i)+target_pos[0]*i) / splits
donorDataY[index-kmc.N] = (kmc.yCoords[index]*(splits-i)+target_pos[1]*i) / splits
donors.set_data(donorDataX, donorDataY)
acceptors.set_data(acceptorDataX, acceptorDataY)
if refresh_history:
history_acceptors.set_data(acceptorDataX, acceptorDataY)
history_acceptors.set_alpha(0.5)
history_donors.set_data(donorDataX, donorDataY)
history_donors.set_alpha(0.5)
else:
history_acceptors.set_alpha(alpha)
history_donors.set_alpha(alpha)
writer.grab_frame()
def trafficAnimation(kmc_dn, search_results, writer, file_name):
'''
Used in the dopant placement annealing search animation.
Input arguments
---------------
kmc_dn: kmc_dn
Dopant network object that is used for animation. This provides the dopant placement and physical
parameters.
search_results: array
This is an array of entries, where each entry is in the format (electrodes, currents, traffic, time, ideal_current).
This is precalculated and used to perform the animation, as each entry represents one frame.
writer: FFMPegWriter
Writer object used to write frames to. This is initialized in the getWriter function.
file_name: string
Name of the file we write the animation video to.
Returns
None
The output is the file, so nothing is returned.
-------
'''
fig = plt.figure(figsize=(20, 10))
gs = gridspec.GridSpec(1, 2, width_ratios=[3, 1])
vmin = 300
vmax = -300
highest_current = 0
x_data = [i for i in range(len(search_results))]
y_data_max = -1
y_data_min = 1
y_data = []
y_data_expected = []
for entry in search_results:
kmc_dn.electrodes = entry[0]
kmc_dn.current = entry[1]
y_data.append(entry[1][-1])
if entry[1][-1] < y_data_min:
y_data_min = entry[1][-1]
if entry[1][-1] > y_data_max:
y_data_max = entry[1][-1]
kmc_dn.traffic = entry[2]
kmc_dn.time = entry[3]
kmc_dn.update_V()
x = np.arange(0, kmc_dn.xdim, kmc_dn.res)
y = np.arange(0, kmc_dn.ydim, kmc_dn.res)
for i in range(len(x)):
for j in range(len(y)):
val = kmc_dn.V(x[i], y[j])
if val > vmax:
vmax = val
if val < vmin:
vmin = val
for row in kmc_dn.traffic:
for ele in row:
if highest_current < ele / kmc_dn.time:
highest_current = ele / kmc_dn.time
for entry in search_results:
expected = entry[4]
y_data_expected.append(y_data_min + (y_data_max-y_data_min)*expected)
with writer.saving(fig, file_name, 100):
i = 0
text_init = ["I1: ", "I2: "]
for i in range(len(search_results[0][0])-3):
text_init.append("C%d: "%(i+1))
text_init.append("O: ")
text_positions = []
for elec in kmc_dn.electrodes:
x = elec[0]
y = elec[1]
if elec[0] < 0.01:
x = -0.3
if elec[0] > 0.99:
x = 1.02
if elec[1] < 0.01:
y = -0.1
if elec[1] > 0.99:
y = 1.05
text_positions.append((x, y))
for entry in search_results:
kmc_dn.electrodes = entry[0]
kmc_dn.current = entry[1]
kmc_dn.traffic = entry[2]
time = entry[3]
kmc_dn.update_V()
plt.clf()
ax0 = plt.subplot(gs[0])
text = []
for h in range(len(text_init)):
text.append("%s%.2g V"%(text_init[h], kmc_dn.electrodes[h][3]/150))
visualize_V_and_traffic(kmc_dn, ax_given=ax0, figure=fig, max_traffic=highest_current*time, v_min=vmin, v_max=vmax, text=text, text_positions=text_positions)
plotax = plt.subplot(gs[1])
plotax.set_xlim(0, len(search_results))
plotax.plot(x_data[:i], y_data[:i])
plotax.plot(x_data[:i], y_data_expected[:i], color='k')
writer.grab_frame()
i+=1