-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmems_ctrnn.py
278 lines (233 loc) · 10.4 KB
/
mems_ctrnn.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import numpy as np
import math
class MEMS_CTRNN:
def __init__(self, new_size=0):
self.set_circuit_size(new_size)
def calc_params(self):
# Cross-sectional area
self.mem_A = self.mem_d * self.mem_b
# Effective young modulus
self.mem_E = self.mem_E1 / (1-self.mem_nu ** 2)
# Second moment of area I_yy
self.mem_Iyy = self.mem_b * self.mem_d ** 3 / 12
# Emessivity constant
self.mem_eps = self.mem_K * 8.845187817620e-12
# Straight beam natural frequency
self.mem_wm = 22.3733 * math.sqrt(self.mem_E * self.mem_Iyy /
self.mem_rho / self.mem_A /
self.mem_L ** 4)
self.mem_Sigma = self.mem_c / (self.mem_rho * self.mem_A)
self.mem_Kstar = 1.0378584523852825 * self.mem_wm ** 2 / \
self.mem_Sigma ** 2
self.mem_K3Old = 0.06486615327408016 * self.mem_A * self.mem_wm ** 2\
/ self.mem_Iyy / self.mem_Sigma ** 2
self.mem_K3 = self.mem_g0**2 * self.mem_K3Old
self.mem_win = 2 / 3.0 * self.mem_b * self.mem_eps \
/ (self.mem_A * self.mem_rho *
self.mem_Sigma ** 2 * self.mem_g0 ** 3)
self.states = np.full(self.size, self.mem_h/self.mem_g0, dtype=float)
# Print MEMS Parameteres
def print_mems_param(self):
print(f'mem_L = {self.mem_L}, mem_b = {self.mem_b}, ' +
f'mem_g0 = {self.mem_g0}, mem_d = {self.mem_d}')
print(f'mem_h = {self.mem_h}, mem_E1 = {self.mem_E1}, ' +
f'mem_nu = {self.mem_nu}, mem_rho = {self.mem_rho}, ')
print(f'mem_c = {self.mem_c}, mem_K = {self.mem_K}, ' +
f'mem_ythr = {self.mem_ythr},' +
f' mem_state_stopper = {self.mem_state_stopper}')
print(f'mem_A = {self.mem_A}, mem_E = {self.mem_E}, ' +
f'mem_Iyy = {self.mem_Iyy}, mem_eps = {self.mem_eps}')
print(f'mem_wm = {self.mem_wm}, mem_Sigma = {self.mem_Sigma}, ' +
f'mem_Kstar = {self.mem_Kstar}, mem_K3Old = {self.mem_K3Old}')
print(f'mem_K3 = {self.mem_K3}, mem_win = {self.mem_win}')
# Show the Model details
def print_model(self):
self.print_mems_param()
print('-----------------------------------------')
for i in range(self.size):
print('Neuron Number :', i)
print('taus:', self.taus[i])
print('v_biases:', self.v_biases[i])
print('hs:', self.hs[i])
print('It\'s the Weights:')
for j in range(self.size):
print('Weight: ({}, {}) = {}'.format(i, j,
self.weights[i][j]))
print('-----------------------------------------')
# Show the Model details
def print_model_abstract(self):
t = ''
r = ''
v = ''
h = ''
e = ''
s = ''
w = ''
for i in range(self.size):
t += str(round(self.taus[i], 9)) + ', '
r += str(round(self.Rtaus[i], 9)) + ', '
v += str(round(self.v_biases[i], 9)) + ', '
h += str(round(self.hs[i], 9)) + ', '
e += str(round(self.external_inputs[i], 9)) + ', '
s += str(round(self.states[i], 9)) + ', '
for j in range(self.size):
w += str(round(self.weights[i][j], 9)) + ', '
w += '\n'
self.print_mems_param()
print("taus:", t)
print("Rtaus:", r)
print("v_biases:", v)
print("hs:", h)
print("external_inputs:", e)
print("states:", s)
print("weight:\n", w)
# Accessors
def circuit_size(self):
return self.size
def set_circuit_size(self, new_size):
self.size = new_size
self.states = np.full(new_size, 0.0, dtype=float)
self.outputs = np.full(new_size, 0.0, dtype=float)
self.v_biases = np.full(new_size, 0.0, dtype=float)
self.v_outs = np.full(new_size, 0.0, dtype=float)
self.hs = np.full(new_size, 1.0, dtype=float)
self.taus = np.full(new_size, 1.0, dtype=float)
self.Rtaus = np.full(new_size, 1.0, dtype=float)
self.external_inputs = np.full(new_size, 0.0, dtype=float)
self.weights = np.full((new_size, new_size), 0.0, dtype=float)
def set_connection_weight(self, i, j, value):
self.weights[i][j] = value
def neuron_time_constant(self, i):
return self.taus[i]
def set_neuron_time_constant(self, i, value=None):
if value is None:
for j, v in enumerate(i):
self.taus[j] = v
self.Rtaus[j] = 1/v
else:
self.taus[i] = value
self.Rtaus[i] = 1/value
def neuron_external_input(self, i):
return self.external_inputs[i]
def set_neuron_external_input(self, i, value):
self.external_inputs[i] = value
# Integrate a circuit one step using 4th-order Runge-Kutta.
def euler_step(self, step_size=None, use_dim_equation=False,
use_defelection_feedback=False):
if step_size is not None:
self.step_size = step_size
# Calculate the v_0
if use_defelection_feedback is True:
for i in range(self.size):
aa = -1 / (self.mem_g0 + self.hs[i])
bb = self.hs[i] / (self.mem_g0 + self.hs[i])
self.v_outs[i] = (self.external_inputs[i] + self.v_biases[i]) \
* (aa * self.mem_g0 * self.states[i] + bb)
else:
for i in range(self.size):
if self.states[i] < self.mem_ythr:
self.v_outs[i] = self.external_inputs[i] + self.v_biases[i]
else:
self.v_outs[i] = 0
# Update the state of all neurons.
for i in range(self.size):
v_mem = self.external_inputs[i] + self.v_biases[i]
for j in range(self.size):
v_mem += self.weights[j][i] * self.v_outs[j]
v_mem = min(max(v_mem, 0.0), 110)
mem_theta = 1.0378584523852825 * self.hs[i] * \
self.mem_wm ** 2 / self.mem_Sigma ** 2 / self.mem_g0
k1 = self.mem_Kstar - self.hs[i] ** 2 * self.mem_K3Old
if use_dim_equation is False:
self.states[i] += self.step_size / self.Rtaus[i] * \
(-k1 * self.states[i] - self.mem_K3 *
(self.states[i] ** 3) +
mem_theta - self.mem_win * (v_mem ** 2) /
math.sqrt((1 + self.states[i]) ** 3))
else:
self.states[i] += (8 * step_size) / \
(3 * self.mem_L * self.mem_c * self.mem_g0) * \
(
((-2 * math.pi ** 4 / self.mem_L ** 3) *
self.mem_E * self.mem_Iyy *
(self.mem_g0 * self.states[i] - self.mem_h)) -
(math.pi ** 4 / (8 * self.mem_L ** 3) * self.mem_E *
self.mem_A * (self.mem_g0 ** 2 * self.states[i] ** 2 -
self.mem_h ** 2) *
self.mem_g0 * self.states[i]) -
(self.mem_eps * self.mem_b * v_mem ** 2 * self.mem_L /
(4 * math.sqrt(self.mem_g0 *
(self.mem_g0 + self.mem_g0 *
self.states[i])
** 3)))
)
# if self.states[i] < self.mem_state_stopper:
# self.states[i] = self.mem_state_stopper
# Input and output from file
def load(self, path):
with open(path, 'r') as fi:
lines = fi.readlines()
# Read the size
self.size = int(lines[0])
self.set_circuit_size(self.size)
self.step_size = float(lines[2])
# Read Mems Parameteres
self.mem_L = float(lines[4])
self.mem_b = float(lines[6])
self.mem_g0 = float(lines[8])
self.mem_d = float(lines[10])
self.mem_h = float(lines[12])
self.mem_E1 = float(lines[14])
self.mem_nu = float(lines[16])
self.mem_rho = float(lines[18])
self.mem_c = float(lines[20])
self.mem_K = float(lines[22])
self.mem_ythr = float(lines[24])
self.mem_state_stopper = float(lines[26])
# Read the time constants
d = lines[28].split()
for i in range(self.size):
self.taus[i] = d[i]
self.Rtaus[i] = 1/self.taus[i]
# Read the v_biases
d = lines[30].split()
for i in range(self.size):
self.v_biases[i] = d[i]
# Read the h's
d = lines[32].split()
for i in range(self.size):
self.hs[i] = d[i]
# Read the weights
for i in range(self.size):
d = lines[34+i].split()
for j in range(self.size):
self.weights[i][j] = d[j]
self.calc_params()
def save(self, path):
with open(path, 'w') as fi:
# Write the size
fi.write(str(self.size) + '\n\n')
fi.write(str(self.step_size) + '\n\n')
# Write the Mems Parameteres
fi.write(str(self.mem_L) + '\n\n')
fi.write(str(self.mem_b) + '\n\n')
fi.write(str(self.mem_g0) + '\n\n')
fi.write(str(self.mem_d) + '\n\n')
fi.write(str(self.mem_h) + '\n\n')
fi.write(str(self.mem_E1) + '\n\n')
fi.write(str(self.mem_nu) + '\n\n')
fi.write(str(self.mem_rho) + '\n\n')
fi.write(str(self.mem_c) + '\n\n')
fi.write(str(self.mem_K) + '\n\n')
fi.write(str(self.mem_ythr) + '\n\n')
fi.write(str(self.mem_state_stopper) + '\n\n')
# Write the time constants
fi.write(' '.join([str(i) for i in self.taus]) + '\n\n')
# Write the biases
fi.write(' '.join([str(i) for i in self.v_biases]) + '\n\n')
# Write the gains
fi.write(' '.join([str(i) for i in self.hs]) + '\n\n')
# Write the weights
for i in range(self.size):
fi.write(' '.join([str(i) for i in self.weights[i]]) +
'\n')